Path: blob/master/drivers/gpu/drm/nouveau/nouveau_fbcon.c
15112 views
/*1* Copyright © 2007 David Airlie2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER20* DEALINGS IN THE SOFTWARE.21*22* Authors:23* David Airlie24*/2526#include <linux/module.h>27#include <linux/kernel.h>28#include <linux/errno.h>29#include <linux/string.h>30#include <linux/mm.h>31#include <linux/tty.h>32#include <linux/sysrq.h>33#include <linux/delay.h>34#include <linux/fb.h>35#include <linux/init.h>36#include <linux/screen_info.h>37#include <linux/vga_switcheroo.h>3839#include "drmP.h"40#include "drm.h"41#include "drm_crtc.h"42#include "drm_crtc_helper.h"43#include "drm_fb_helper.h"44#include "nouveau_drv.h"45#include "nouveau_drm.h"46#include "nouveau_crtc.h"47#include "nouveau_fb.h"48#include "nouveau_fbcon.h"49#include "nouveau_dma.h"5051static void52nouveau_fbcon_fillrect(struct fb_info *info, const struct fb_fillrect *rect)53{54struct nouveau_fbdev *nfbdev = info->par;55struct drm_device *dev = nfbdev->dev;56struct drm_nouveau_private *dev_priv = dev->dev_private;57int ret;5859if (info->state != FBINFO_STATE_RUNNING)60return;6162ret = -ENODEV;63if (!in_interrupt() && !(info->flags & FBINFO_HWACCEL_DISABLED) &&64mutex_trylock(&dev_priv->channel->mutex)) {65if (dev_priv->card_type < NV_50)66ret = nv04_fbcon_fillrect(info, rect);67else68if (dev_priv->card_type < NV_C0)69ret = nv50_fbcon_fillrect(info, rect);70else71ret = nvc0_fbcon_fillrect(info, rect);72mutex_unlock(&dev_priv->channel->mutex);73}7475if (ret == 0)76return;7778if (ret != -ENODEV)79nouveau_fbcon_gpu_lockup(info);80cfb_fillrect(info, rect);81}8283static void84nouveau_fbcon_copyarea(struct fb_info *info, const struct fb_copyarea *image)85{86struct nouveau_fbdev *nfbdev = info->par;87struct drm_device *dev = nfbdev->dev;88struct drm_nouveau_private *dev_priv = dev->dev_private;89int ret;9091if (info->state != FBINFO_STATE_RUNNING)92return;9394ret = -ENODEV;95if (!in_interrupt() && !(info->flags & FBINFO_HWACCEL_DISABLED) &&96mutex_trylock(&dev_priv->channel->mutex)) {97if (dev_priv->card_type < NV_50)98ret = nv04_fbcon_copyarea(info, image);99else100if (dev_priv->card_type < NV_C0)101ret = nv50_fbcon_copyarea(info, image);102else103ret = nvc0_fbcon_copyarea(info, image);104mutex_unlock(&dev_priv->channel->mutex);105}106107if (ret == 0)108return;109110if (ret != -ENODEV)111nouveau_fbcon_gpu_lockup(info);112cfb_copyarea(info, image);113}114115static void116nouveau_fbcon_imageblit(struct fb_info *info, const struct fb_image *image)117{118struct nouveau_fbdev *nfbdev = info->par;119struct drm_device *dev = nfbdev->dev;120struct drm_nouveau_private *dev_priv = dev->dev_private;121int ret;122123if (info->state != FBINFO_STATE_RUNNING)124return;125126ret = -ENODEV;127if (!in_interrupt() && !(info->flags & FBINFO_HWACCEL_DISABLED) &&128mutex_trylock(&dev_priv->channel->mutex)) {129if (dev_priv->card_type < NV_50)130ret = nv04_fbcon_imageblit(info, image);131else132if (dev_priv->card_type < NV_C0)133ret = nv50_fbcon_imageblit(info, image);134else135ret = nvc0_fbcon_imageblit(info, image);136mutex_unlock(&dev_priv->channel->mutex);137}138139if (ret == 0)140return;141142if (ret != -ENODEV)143nouveau_fbcon_gpu_lockup(info);144cfb_imageblit(info, image);145}146147static int148nouveau_fbcon_sync(struct fb_info *info)149{150struct nouveau_fbdev *nfbdev = info->par;151struct drm_device *dev = nfbdev->dev;152struct drm_nouveau_private *dev_priv = dev->dev_private;153struct nouveau_channel *chan = dev_priv->channel;154int ret, i;155156if (!chan || !chan->accel_done || in_interrupt() ||157info->state != FBINFO_STATE_RUNNING ||158info->flags & FBINFO_HWACCEL_DISABLED)159return 0;160161if (!mutex_trylock(&chan->mutex))162return 0;163164ret = RING_SPACE(chan, 4);165if (ret) {166mutex_unlock(&chan->mutex);167nouveau_fbcon_gpu_lockup(info);168return 0;169}170171if (dev_priv->card_type >= NV_C0) {172BEGIN_NVC0(chan, 2, NvSub2D, 0x010c, 1);173OUT_RING (chan, 0);174BEGIN_NVC0(chan, 2, NvSub2D, 0x0100, 1);175OUT_RING (chan, 0);176} else {177BEGIN_RING(chan, 0, 0x0104, 1);178OUT_RING (chan, 0);179BEGIN_RING(chan, 0, 0x0100, 1);180OUT_RING (chan, 0);181}182183nouveau_bo_wr32(chan->notifier_bo, chan->m2mf_ntfy/4 + 3, 0xffffffff);184FIRE_RING(chan);185mutex_unlock(&chan->mutex);186187ret = -EBUSY;188for (i = 0; i < 100000; i++) {189if (!nouveau_bo_rd32(chan->notifier_bo, chan->m2mf_ntfy/4 + 3)) {190ret = 0;191break;192}193DRM_UDELAY(1);194}195196if (ret) {197nouveau_fbcon_gpu_lockup(info);198return 0;199}200201chan->accel_done = false;202return 0;203}204205static struct fb_ops nouveau_fbcon_ops = {206.owner = THIS_MODULE,207.fb_check_var = drm_fb_helper_check_var,208.fb_set_par = drm_fb_helper_set_par,209.fb_fillrect = nouveau_fbcon_fillrect,210.fb_copyarea = nouveau_fbcon_copyarea,211.fb_imageblit = nouveau_fbcon_imageblit,212.fb_sync = nouveau_fbcon_sync,213.fb_pan_display = drm_fb_helper_pan_display,214.fb_blank = drm_fb_helper_blank,215.fb_setcmap = drm_fb_helper_setcmap,216.fb_debug_enter = drm_fb_helper_debug_enter,217.fb_debug_leave = drm_fb_helper_debug_leave,218};219220static struct fb_ops nouveau_fbcon_sw_ops = {221.owner = THIS_MODULE,222.fb_check_var = drm_fb_helper_check_var,223.fb_set_par = drm_fb_helper_set_par,224.fb_fillrect = cfb_fillrect,225.fb_copyarea = cfb_copyarea,226.fb_imageblit = cfb_imageblit,227.fb_pan_display = drm_fb_helper_pan_display,228.fb_blank = drm_fb_helper_blank,229.fb_setcmap = drm_fb_helper_setcmap,230.fb_debug_enter = drm_fb_helper_debug_enter,231.fb_debug_leave = drm_fb_helper_debug_leave,232};233234static void nouveau_fbcon_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,235u16 blue, int regno)236{237struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);238239nv_crtc->lut.r[regno] = red;240nv_crtc->lut.g[regno] = green;241nv_crtc->lut.b[regno] = blue;242}243244static void nouveau_fbcon_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,245u16 *blue, int regno)246{247struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);248249*red = nv_crtc->lut.r[regno];250*green = nv_crtc->lut.g[regno];251*blue = nv_crtc->lut.b[regno];252}253254static void255nouveau_fbcon_zfill(struct drm_device *dev, struct nouveau_fbdev *nfbdev)256{257struct fb_info *info = nfbdev->helper.fbdev;258struct fb_fillrect rect;259260/* Clear the entire fbcon. The drm will program every connector261* with it's preferred mode. If the sizes differ, one display will262* quite likely have garbage around the console.263*/264rect.dx = rect.dy = 0;265rect.width = info->var.xres_virtual;266rect.height = info->var.yres_virtual;267rect.color = 0;268rect.rop = ROP_COPY;269info->fbops->fb_fillrect(info, &rect);270}271272static int273nouveau_fbcon_create(struct nouveau_fbdev *nfbdev,274struct drm_fb_helper_surface_size *sizes)275{276struct drm_device *dev = nfbdev->dev;277struct drm_nouveau_private *dev_priv = dev->dev_private;278struct fb_info *info;279struct drm_framebuffer *fb;280struct nouveau_framebuffer *nouveau_fb;281struct nouveau_bo *nvbo;282struct drm_mode_fb_cmd mode_cmd;283struct pci_dev *pdev = dev->pdev;284struct device *device = &pdev->dev;285int size, ret;286287mode_cmd.width = sizes->surface_width;288mode_cmd.height = sizes->surface_height;289290mode_cmd.bpp = sizes->surface_bpp;291mode_cmd.pitch = mode_cmd.width * (mode_cmd.bpp >> 3);292mode_cmd.pitch = roundup(mode_cmd.pitch, 256);293mode_cmd.depth = sizes->surface_depth;294295size = mode_cmd.pitch * mode_cmd.height;296size = roundup(size, PAGE_SIZE);297298ret = nouveau_gem_new(dev, dev_priv->channel, size, 0,299NOUVEAU_GEM_DOMAIN_VRAM, 0, 0x0000, &nvbo);300if (ret) {301NV_ERROR(dev, "failed to allocate framebuffer\n");302goto out;303}304305ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_VRAM);306if (ret) {307NV_ERROR(dev, "failed to pin fb: %d\n", ret);308nouveau_bo_ref(NULL, &nvbo);309goto out;310}311312ret = nouveau_bo_map(nvbo);313if (ret) {314NV_ERROR(dev, "failed to map fb: %d\n", ret);315nouveau_bo_unpin(nvbo);316nouveau_bo_ref(NULL, &nvbo);317goto out;318}319320mutex_lock(&dev->struct_mutex);321322info = framebuffer_alloc(0, device);323if (!info) {324ret = -ENOMEM;325goto out_unref;326}327328ret = fb_alloc_cmap(&info->cmap, 256, 0);329if (ret) {330ret = -ENOMEM;331goto out_unref;332}333334info->par = nfbdev;335336nouveau_framebuffer_init(dev, &nfbdev->nouveau_fb, &mode_cmd, nvbo);337338nouveau_fb = &nfbdev->nouveau_fb;339fb = &nouveau_fb->base;340341/* setup helper */342nfbdev->helper.fb = fb;343nfbdev->helper.fbdev = info;344345strcpy(info->fix.id, "nouveaufb");346if (nouveau_nofbaccel)347info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_DISABLED;348else349info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA |350FBINFO_HWACCEL_FILLRECT |351FBINFO_HWACCEL_IMAGEBLIT;352info->flags |= FBINFO_CAN_FORCE_OUTPUT;353info->fbops = &nouveau_fbcon_sw_ops;354info->fix.smem_start = nvbo->bo.mem.bus.base +355nvbo->bo.mem.bus.offset;356info->fix.smem_len = size;357358info->screen_base = nvbo_kmap_obj_iovirtual(nouveau_fb->nvbo);359info->screen_size = size;360361drm_fb_helper_fill_fix(info, fb->pitch, fb->depth);362drm_fb_helper_fill_var(info, &nfbdev->helper, sizes->fb_width, sizes->fb_height);363364/* Set aperture base/size for vesafb takeover */365info->apertures = dev_priv->apertures;366if (!info->apertures) {367ret = -ENOMEM;368goto out_unref;369}370371info->pixmap.size = 64*1024;372info->pixmap.buf_align = 8;373info->pixmap.access_align = 32;374info->pixmap.flags = FB_PIXMAP_SYSTEM;375info->pixmap.scan_align = 1;376377mutex_unlock(&dev->struct_mutex);378379if (dev_priv->channel && !nouveau_nofbaccel) {380ret = -ENODEV;381if (dev_priv->card_type < NV_50)382ret = nv04_fbcon_accel_init(info);383else384if (dev_priv->card_type < NV_C0)385ret = nv50_fbcon_accel_init(info);386else387ret = nvc0_fbcon_accel_init(info);388389if (ret == 0)390info->fbops = &nouveau_fbcon_ops;391}392393nouveau_fbcon_zfill(dev, nfbdev);394395/* To allow resizeing without swapping buffers */396NV_INFO(dev, "allocated %dx%d fb: 0x%lx, bo %p\n",397nouveau_fb->base.width,398nouveau_fb->base.height,399nvbo->bo.offset, nvbo);400401vga_switcheroo_client_fb_set(dev->pdev, info);402return 0;403404out_unref:405mutex_unlock(&dev->struct_mutex);406out:407return ret;408}409410static int411nouveau_fbcon_find_or_create_single(struct drm_fb_helper *helper,412struct drm_fb_helper_surface_size *sizes)413{414struct nouveau_fbdev *nfbdev = (struct nouveau_fbdev *)helper;415int new_fb = 0;416int ret;417418if (!helper->fb) {419ret = nouveau_fbcon_create(nfbdev, sizes);420if (ret)421return ret;422new_fb = 1;423}424return new_fb;425}426427void428nouveau_fbcon_output_poll_changed(struct drm_device *dev)429{430struct drm_nouveau_private *dev_priv = dev->dev_private;431drm_fb_helper_hotplug_event(&dev_priv->nfbdev->helper);432}433434static int435nouveau_fbcon_destroy(struct drm_device *dev, struct nouveau_fbdev *nfbdev)436{437struct nouveau_framebuffer *nouveau_fb = &nfbdev->nouveau_fb;438struct fb_info *info;439440if (nfbdev->helper.fbdev) {441info = nfbdev->helper.fbdev;442unregister_framebuffer(info);443if (info->cmap.len)444fb_dealloc_cmap(&info->cmap);445framebuffer_release(info);446}447448if (nouveau_fb->nvbo) {449nouveau_bo_unmap(nouveau_fb->nvbo);450drm_gem_object_unreference_unlocked(nouveau_fb->nvbo->gem);451nouveau_fb->nvbo = NULL;452}453drm_fb_helper_fini(&nfbdev->helper);454drm_framebuffer_cleanup(&nouveau_fb->base);455return 0;456}457458void nouveau_fbcon_gpu_lockup(struct fb_info *info)459{460struct nouveau_fbdev *nfbdev = info->par;461struct drm_device *dev = nfbdev->dev;462463NV_ERROR(dev, "GPU lockup - switching to software fbcon\n");464info->flags |= FBINFO_HWACCEL_DISABLED;465}466467static struct drm_fb_helper_funcs nouveau_fbcon_helper_funcs = {468.gamma_set = nouveau_fbcon_gamma_set,469.gamma_get = nouveau_fbcon_gamma_get,470.fb_probe = nouveau_fbcon_find_or_create_single,471};472473474int nouveau_fbcon_init(struct drm_device *dev)475{476struct drm_nouveau_private *dev_priv = dev->dev_private;477struct nouveau_fbdev *nfbdev;478int ret;479480nfbdev = kzalloc(sizeof(struct nouveau_fbdev), GFP_KERNEL);481if (!nfbdev)482return -ENOMEM;483484nfbdev->dev = dev;485dev_priv->nfbdev = nfbdev;486nfbdev->helper.funcs = &nouveau_fbcon_helper_funcs;487488ret = drm_fb_helper_init(dev, &nfbdev->helper,489nv_two_heads(dev) ? 2 : 1, 4);490if (ret) {491kfree(nfbdev);492return ret;493}494495drm_fb_helper_single_add_all_connectors(&nfbdev->helper);496drm_fb_helper_initial_config(&nfbdev->helper, 32);497return 0;498}499500void nouveau_fbcon_fini(struct drm_device *dev)501{502struct drm_nouveau_private *dev_priv = dev->dev_private;503504if (!dev_priv->nfbdev)505return;506507nouveau_fbcon_destroy(dev, dev_priv->nfbdev);508kfree(dev_priv->nfbdev);509dev_priv->nfbdev = NULL;510}511512void nouveau_fbcon_save_disable_accel(struct drm_device *dev)513{514struct drm_nouveau_private *dev_priv = dev->dev_private;515516dev_priv->nfbdev->saved_flags = dev_priv->nfbdev->helper.fbdev->flags;517dev_priv->nfbdev->helper.fbdev->flags |= FBINFO_HWACCEL_DISABLED;518}519520void nouveau_fbcon_restore_accel(struct drm_device *dev)521{522struct drm_nouveau_private *dev_priv = dev->dev_private;523dev_priv->nfbdev->helper.fbdev->flags = dev_priv->nfbdev->saved_flags;524}525526void nouveau_fbcon_set_suspend(struct drm_device *dev, int state)527{528struct drm_nouveau_private *dev_priv = dev->dev_private;529fb_set_suspend(dev_priv->nfbdev->helper.fbdev, state);530}531532void nouveau_fbcon_zfill_all(struct drm_device *dev)533{534struct drm_nouveau_private *dev_priv = dev->dev_private;535nouveau_fbcon_zfill(dev, dev_priv->nfbdev);536}537538539