Home | History | Annotate | Line # | Download | only in nouveau
nouveau_fbcon.c revision 1.7
      1 /*	$NetBSD: nouveau_fbcon.c,v 1.7 2020/02/12 20:25:48 jdolecek Exp $	*/
      2 
      3 /*
      4  * Copyright  2007 David Airlie
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice (including the next
     14  * paragraph) shall be included in all copies or substantial portions of the
     15  * Software.
     16  *
     17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     23  * DEALINGS IN THE SOFTWARE.
     24  *
     25  * Authors:
     26  *     David Airlie
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: nouveau_fbcon.c,v 1.7 2020/02/12 20:25:48 jdolecek Exp $");
     31 
     32 #include <linux/module.h>
     33 #include <linux/kernel.h>
     34 #include <linux/errno.h>
     35 #include <linux/string.h>
     36 #include <linux/mm.h>
     37 #include <linux/tty.h>
     38 #include <linux/sysrq.h>
     39 #include <linux/delay.h>
     40 #include <linux/fb.h>
     41 #include <linux/init.h>
     42 #include <linux/screen_info.h>
     43 #include <linux/vga_switcheroo.h>
     44 #include <linux/console.h>
     45 
     46 #include <drm/drmP.h>
     47 #include <drm/drm_crtc.h>
     48 #include <drm/drm_crtc_helper.h>
     49 #include <drm/drm_fb_helper.h>
     50 
     51 #include "nouveau_drm.h"
     52 #include "nouveau_gem.h"
     53 #include "nouveau_bo.h"
     54 #include "nouveau_fbcon.h"
     55 #include "nouveau_chan.h"
     56 
     57 #include "nouveau_crtc.h"
     58 
     59 #ifdef __NetBSD__
     60 #include "nouveaufb.h"
     61 #endif
     62 
     63 #ifdef __NetBSD__		/* XXX nouveau fbaccel */
     64 int nouveau_nofbaccel = 1;
     65 #else
     66 MODULE_PARM_DESC(nofbaccel, "Disable fbcon acceleration");
     67 int nouveau_nofbaccel = 0;
     68 module_param_named(nofbaccel, nouveau_nofbaccel, int, 0400);
     69 
     70 static void
     71 nouveau_fbcon_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
     72 {
     73 	struct nouveau_fbdev *fbcon = info->par;
     74 	struct nouveau_drm *drm = nouveau_drm(fbcon->dev);
     75 	struct nvif_device *device = &drm->device;
     76 	int ret;
     77 
     78 	if (info->state != FBINFO_STATE_RUNNING)
     79 		return;
     80 
     81 	ret = -ENODEV;
     82 	if (!in_interrupt() && !(info->flags & FBINFO_HWACCEL_DISABLED) &&
     83 	    mutex_trylock(&drm->client.mutex)) {
     84 		if (device->info.family < NV_DEVICE_INFO_V0_TESLA)
     85 			ret = nv04_fbcon_fillrect(info, rect);
     86 		else
     87 		if (device->info.family < NV_DEVICE_INFO_V0_FERMI)
     88 			ret = nv50_fbcon_fillrect(info, rect);
     89 		else
     90 			ret = nvc0_fbcon_fillrect(info, rect);
     91 		mutex_unlock(&drm->client.mutex);
     92 	}
     93 
     94 	if (ret == 0)
     95 		return;
     96 
     97 	if (ret != -ENODEV)
     98 		nouveau_fbcon_gpu_lockup(info);
     99 	drm_fb_helper_cfb_fillrect(info, rect);
    100 }
    101 
    102 static void
    103 nouveau_fbcon_copyarea(struct fb_info *info, const struct fb_copyarea *image)
    104 {
    105 	struct nouveau_fbdev *fbcon = info->par;
    106 	struct nouveau_drm *drm = nouveau_drm(fbcon->dev);
    107 	struct nvif_device *device = &drm->device;
    108 	int ret;
    109 
    110 	if (info->state != FBINFO_STATE_RUNNING)
    111 		return;
    112 
    113 	ret = -ENODEV;
    114 	if (!in_interrupt() && !(info->flags & FBINFO_HWACCEL_DISABLED) &&
    115 	    mutex_trylock(&drm->client.mutex)) {
    116 		if (device->info.family < NV_DEVICE_INFO_V0_TESLA)
    117 			ret = nv04_fbcon_copyarea(info, image);
    118 		else
    119 		if (device->info.family < NV_DEVICE_INFO_V0_FERMI)
    120 			ret = nv50_fbcon_copyarea(info, image);
    121 		else
    122 			ret = nvc0_fbcon_copyarea(info, image);
    123 		mutex_unlock(&drm->client.mutex);
    124 	}
    125 
    126 	if (ret == 0)
    127 		return;
    128 
    129 	if (ret != -ENODEV)
    130 		nouveau_fbcon_gpu_lockup(info);
    131 	drm_fb_helper_cfb_copyarea(info, image);
    132 }
    133 
    134 static void
    135 nouveau_fbcon_imageblit(struct fb_info *info, const struct fb_image *image)
    136 {
    137 	struct nouveau_fbdev *fbcon = info->par;
    138 	struct nouveau_drm *drm = nouveau_drm(fbcon->dev);
    139 	struct nvif_device *device = &drm->device;
    140 	int ret;
    141 
    142 	if (info->state != FBINFO_STATE_RUNNING)
    143 		return;
    144 
    145 	ret = -ENODEV;
    146 	if (!in_interrupt() && !(info->flags & FBINFO_HWACCEL_DISABLED) &&
    147 	    mutex_trylock(&drm->client.mutex)) {
    148 		if (device->info.family < NV_DEVICE_INFO_V0_TESLA)
    149 			ret = nv04_fbcon_imageblit(info, image);
    150 		else
    151 		if (device->info.family < NV_DEVICE_INFO_V0_FERMI)
    152 			ret = nv50_fbcon_imageblit(info, image);
    153 		else
    154 			ret = nvc0_fbcon_imageblit(info, image);
    155 		mutex_unlock(&drm->client.mutex);
    156 	}
    157 
    158 	if (ret == 0)
    159 		return;
    160 
    161 	if (ret != -ENODEV)
    162 		nouveau_fbcon_gpu_lockup(info);
    163 	drm_fb_helper_cfb_imageblit(info, image);
    164 }
    165 
    166 static int
    167 nouveau_fbcon_sync(struct fb_info *info)
    168 {
    169 	struct nouveau_fbdev *fbcon = info->par;
    170 	struct nouveau_drm *drm = nouveau_drm(fbcon->dev);
    171 	struct nouveau_channel *chan = drm->channel;
    172 	int ret;
    173 
    174 	if (!chan || !chan->accel_done || in_interrupt() ||
    175 	    info->state != FBINFO_STATE_RUNNING ||
    176 	    info->flags & FBINFO_HWACCEL_DISABLED)
    177 		return 0;
    178 
    179 	if (!mutex_trylock(&drm->client.mutex))
    180 		return 0;
    181 
    182 	ret = nouveau_channel_idle(chan);
    183 	mutex_unlock(&drm->client.mutex);
    184 	if (ret) {
    185 		nouveau_fbcon_gpu_lockup(info);
    186 		return 0;
    187 	}
    188 
    189 	chan->accel_done = false;
    190 	return 0;
    191 }
    192 
    193 static int
    194 nouveau_fbcon_open(struct fb_info *info, int user)
    195 {
    196 	struct nouveau_fbdev *fbcon = info->par;
    197 	struct nouveau_drm *drm = nouveau_drm(fbcon->dev);
    198 	int ret = pm_runtime_get_sync(drm->dev->dev);
    199 	if (ret < 0 && ret != -EACCES)
    200 		return ret;
    201 	return 0;
    202 }
    203 
    204 static int
    205 nouveau_fbcon_release(struct fb_info *info, int user)
    206 {
    207 	struct nouveau_fbdev *fbcon = info->par;
    208 	struct nouveau_drm *drm = nouveau_drm(fbcon->dev);
    209 	pm_runtime_put(drm->dev->dev);
    210 	return 0;
    211 }
    212 
    213 static struct fb_ops nouveau_fbcon_ops = {
    214 	.owner = THIS_MODULE,
    215 	.fb_open = nouveau_fbcon_open,
    216 	.fb_release = nouveau_fbcon_release,
    217 	.fb_check_var = drm_fb_helper_check_var,
    218 	.fb_set_par = drm_fb_helper_set_par,
    219 	.fb_fillrect = nouveau_fbcon_fillrect,
    220 	.fb_copyarea = nouveau_fbcon_copyarea,
    221 	.fb_imageblit = nouveau_fbcon_imageblit,
    222 	.fb_sync = nouveau_fbcon_sync,
    223 	.fb_pan_display = drm_fb_helper_pan_display,
    224 	.fb_blank = drm_fb_helper_blank,
    225 	.fb_setcmap = drm_fb_helper_setcmap,
    226 	.fb_debug_enter = drm_fb_helper_debug_enter,
    227 	.fb_debug_leave = drm_fb_helper_debug_leave,
    228 };
    229 
    230 static struct fb_ops nouveau_fbcon_sw_ops = {
    231 	.owner = THIS_MODULE,
    232 	.fb_open = nouveau_fbcon_open,
    233 	.fb_release = nouveau_fbcon_release,
    234 	.fb_check_var = drm_fb_helper_check_var,
    235 	.fb_set_par = drm_fb_helper_set_par,
    236 	.fb_fillrect = drm_fb_helper_cfb_fillrect,
    237 	.fb_copyarea = drm_fb_helper_cfb_copyarea,
    238 	.fb_imageblit = drm_fb_helper_cfb_imageblit,
    239 	.fb_pan_display = drm_fb_helper_pan_display,
    240 	.fb_blank = drm_fb_helper_blank,
    241 	.fb_setcmap = drm_fb_helper_setcmap,
    242 	.fb_debug_enter = drm_fb_helper_debug_enter,
    243 	.fb_debug_leave = drm_fb_helper_debug_leave,
    244 };
    245 #endif	/* XXX nouveau fbaccel */
    246 
    247 void
    248 nouveau_fbcon_accel_save_disable(struct drm_device *dev)
    249 {
    250 #ifndef __NetBSD__		/* XXX nouveau fbaccel */
    251 	struct nouveau_drm *drm = nouveau_drm(dev);
    252 	if (drm->fbcon) {
    253 		drm->fbcon->saved_flags = drm->fbcon->helper.fbdev->flags;
    254 		drm->fbcon->helper.fbdev->flags |= FBINFO_HWACCEL_DISABLED;
    255 	}
    256 #endif
    257 }
    258 
    259 void
    260 nouveau_fbcon_accel_restore(struct drm_device *dev)
    261 {
    262 #ifndef __NetBSD__		/* XXX nouveau fbaccel */
    263 	struct nouveau_drm *drm = nouveau_drm(dev);
    264 	if (drm->fbcon) {
    265 		drm->fbcon->helper.fbdev->flags = drm->fbcon->saved_flags;
    266 	}
    267 #endif
    268 }
    269 
    270 static void
    271 nouveau_fbcon_accel_fini(struct drm_device *dev)
    272 {
    273 	struct nouveau_drm *drm = nouveau_drm(dev);
    274 	struct nouveau_fbdev *fbcon = drm->fbcon;
    275 	if (fbcon && drm->channel) {
    276 #ifndef __NetBSD__		/* XXX nouveau fbaccel */
    277 		console_lock();
    278 		fbcon->helper.fbdev->flags |= FBINFO_HWACCEL_DISABLED;
    279 		console_unlock();
    280 #endif
    281 		nouveau_channel_idle(drm->channel);
    282 		nvif_object_fini(&fbcon->twod);
    283 		nvif_object_fini(&fbcon->blit);
    284 		nvif_object_fini(&fbcon->gdi);
    285 		nvif_object_fini(&fbcon->patt);
    286 		nvif_object_fini(&fbcon->rop);
    287 		nvif_object_fini(&fbcon->clip);
    288 		nvif_object_fini(&fbcon->surf2d);
    289 	}
    290 }
    291 
    292 #ifndef __NetBSD__		/* XXX nouveau fbaccel */
    293 static void
    294 nouveau_fbcon_accel_init(struct drm_device *dev)
    295 {
    296 	struct nouveau_drm *drm = nouveau_drm(dev);
    297 	struct nouveau_fbdev *fbcon = drm->fbcon;
    298 	struct fb_info *info = fbcon->helper.fbdev;
    299 	int ret;
    300 
    301 	if (drm->device.info.family < NV_DEVICE_INFO_V0_TESLA)
    302 		ret = nv04_fbcon_accel_init(info);
    303 	else
    304 	if (drm->device.info.family < NV_DEVICE_INFO_V0_FERMI)
    305 		ret = nv50_fbcon_accel_init(info);
    306 	else
    307 		ret = nvc0_fbcon_accel_init(info);
    308 
    309 	if (ret == 0)
    310 		info->fbops = &nouveau_fbcon_ops;
    311 }
    312 #endif
    313 
    314 static void nouveau_fbcon_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
    315 				    u16 blue, int regno)
    316 {
    317 	struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
    318 
    319 	nv_crtc->lut.r[regno] = red;
    320 	nv_crtc->lut.g[regno] = green;
    321 	nv_crtc->lut.b[regno] = blue;
    322 }
    323 
    324 static void nouveau_fbcon_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
    325 				    u16 *blue, int regno)
    326 {
    327 	struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
    328 
    329 	*red = nv_crtc->lut.r[regno];
    330 	*green = nv_crtc->lut.g[regno];
    331 	*blue = nv_crtc->lut.b[regno];
    332 }
    333 
    334 static void
    335 nouveau_fbcon_zfill(struct drm_device *dev, struct nouveau_fbdev *fbcon)
    336 {
    337 #ifdef __NetBSD__		/* XXX nouveau fbaccel */
    338 	struct nouveau_bo *const nvbo = fbcon->nouveau_fb.nvbo;
    339 
    340 	(void)memset(__UNVOLATILE(nvbo_kmap_obj_iovirtual(nvbo)), 0,
    341 	    nvbo->bo.num_pages << PAGE_SHIFT);
    342 #else
    343 	struct fb_info *info = fbcon->helper.fbdev;
    344 	struct fb_fillrect rect;
    345 
    346 	/* Clear the entire fbcon.  The drm will program every connector
    347 	 * with it's preferred mode.  If the sizes differ, one display will
    348 	 * quite likely have garbage around the console.
    349 	 */
    350 	rect.dx = rect.dy = 0;
    351 	rect.width = info->var.xres_virtual;
    352 	rect.height = info->var.yres_virtual;
    353 	rect.color = 0;
    354 	rect.rop = ROP_COPY;
    355 	info->fbops->fb_fillrect(info, &rect);
    356 #endif
    357 }
    358 
    359 #ifdef __NetBSD__
    360 static int
    361 nouveau_fbcon_print(void *aux, const char *pnp)
    362 {
    363 	if (pnp)
    364 		aprint_normal("nouveaufbbus at %s", pnp);
    365 
    366 	return (UNCONF);
    367 }
    368 #endif
    369 
    370 static int
    371 nouveau_fbcon_create(struct drm_fb_helper *helper,
    372 		     struct drm_fb_helper_surface_size *sizes)
    373 {
    374 	struct nouveau_fbdev *fbcon =
    375 		container_of(helper, struct nouveau_fbdev, helper);
    376 	struct drm_device *dev = fbcon->dev;
    377 	struct nouveau_drm *drm = nouveau_drm(dev);
    378 	struct nvif_device *device = &drm->device;
    379 #ifndef __NetBSD__
    380 	struct fb_info *info;
    381 #endif
    382 	struct drm_framebuffer *fb;
    383 	struct nouveau_framebuffer *nouveau_fb;
    384 	struct nouveau_channel *chan;
    385 	struct nouveau_bo *nvbo;
    386 	struct drm_mode_fb_cmd2 mode_cmd;
    387 	int size, ret;
    388 
    389 	mode_cmd.width = sizes->surface_width;
    390 	mode_cmd.height = sizes->surface_height;
    391 
    392 	mode_cmd.pitches[0] = mode_cmd.width * (sizes->surface_bpp >> 3);
    393 	mode_cmd.pitches[0] = roundup(mode_cmd.pitches[0], 256);
    394 
    395 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
    396 							  sizes->surface_depth);
    397 
    398 	size = mode_cmd.pitches[0] * mode_cmd.height;
    399 	size = roundup(size, PAGE_SIZE);
    400 
    401 	ret = nouveau_gem_new(dev, size, 0, NOUVEAU_GEM_DOMAIN_VRAM,
    402 			      0, 0x0000, &nvbo);
    403 	if (ret) {
    404 		NV_ERROR(drm, "failed to allocate framebuffer\n");
    405 		goto out;
    406 	}
    407 
    408 	ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_VRAM, false);
    409 	if (ret) {
    410 		NV_ERROR(drm, "failed to pin fb: %d\n", ret);
    411 		goto out_unref;
    412 	}
    413 
    414 	ret = nouveau_bo_map(nvbo);
    415 	if (ret) {
    416 		NV_ERROR(drm, "failed to map fb: %d\n", ret);
    417 		goto out_unpin;
    418 	}
    419 
    420 	chan = nouveau_nofbaccel ? NULL : drm->channel;
    421 	if (chan && device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
    422 		ret = nouveau_bo_vma_add(nvbo, drm->client.vm,
    423 					&fbcon->nouveau_fb.vma);
    424 		if (ret) {
    425 			NV_ERROR(drm, "failed to map fb into chan: %d\n", ret);
    426 			chan = NULL;
    427 		}
    428 	}
    429 
    430 #ifdef __NetBSD__
    431 	nouveau_framebuffer_init(dev, &fbcon->nouveau_fb, &mode_cmd, nvbo);
    432 	nouveau_fb = &fbcon->nouveau_fb;
    433 	fb = &nouveau_fb->base;
    434 
    435 	nouveau_fbcon_zfill(dev, fbcon);
    436 
    437     {
    438 	static const struct nouveaufb_attach_args zero_nfa;
    439 	struct nouveaufb_attach_args nfa = zero_nfa;
    440 
    441 	nfa.nfa_fb_helper = helper;
    442 	nfa.nfa_fb_sizes = *sizes;
    443 	nfa.nfa_fb_ptr = nvbo_kmap_obj_iovirtual(nvbo);
    444 	nfa.nfa_fb_linebytes = mode_cmd.pitches[0];
    445 
    446 	helper->fbdev = config_found_ia(dev->dev, "nouveaufbbus", &nfa,
    447 	    nouveau_fbcon_print);
    448 	if (helper->fbdev == NULL) {
    449 		goto out_unlock;
    450 	}
    451     }
    452 	helper->fb = fb;
    453 
    454 	return 0;
    455 #else
    456 	mutex_lock(&dev->struct_mutex);
    457 
    458 	info = drm_fb_helper_alloc_fbi(helper);
    459 	if (IS_ERR(info)) {
    460 		ret = PTR_ERR(info);
    461 		goto out_unlock;
    462 	}
    463 	info->skip_vt_switch = 1;
    464 
    465 	info->par = fbcon;
    466 
    467 	nouveau_framebuffer_init(dev, &fbcon->nouveau_fb, &mode_cmd, nvbo);
    468 
    469 	nouveau_fb = &fbcon->nouveau_fb;
    470 	fb = &nouveau_fb->base;
    471 
    472 	/* setup helper */
    473 	fbcon->helper.fb = fb;
    474 
    475 	strcpy(info->fix.id, "nouveaufb");
    476 	if (!chan)
    477 		info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_DISABLED;
    478 	else
    479 		info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA |
    480 			      FBINFO_HWACCEL_FILLRECT |
    481 			      FBINFO_HWACCEL_IMAGEBLIT;
    482 	info->flags |= FBINFO_CAN_FORCE_OUTPUT;
    483 	info->fbops = &nouveau_fbcon_sw_ops;
    484 	info->fix.smem_start = nvbo->bo.mem.bus.base +
    485 			       nvbo->bo.mem.bus.offset;
    486 	info->fix.smem_len = size;
    487 
    488 	info->screen_base = nvbo_kmap_obj_iovirtual(nouveau_fb->nvbo);
    489 	info->screen_size = size;
    490 
    491 	drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
    492 	drm_fb_helper_fill_var(info, &fbcon->helper, sizes->fb_width, sizes->fb_height);
    493 
    494 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
    495 
    496 	mutex_unlock(&dev->struct_mutex);
    497 
    498 	if (chan)
    499 		nouveau_fbcon_accel_init(dev);
    500 	nouveau_fbcon_zfill(dev, fbcon);
    501 
    502 	/* To allow resizeing without swapping buffers */
    503 	NV_INFO(drm, "allocated %dx%d fb: 0x%jx, bo %p\n",
    504 		nouveau_fb->base.width, nouveau_fb->base.height,
    505 		(uintmax_t)nvbo->bo.offset, nvbo);
    506 
    507 	vga_switcheroo_client_fb_set(dev->pdev, info);
    508 	return 0;
    509 #endif	/* defined(__NetBSD__) */
    510 
    511 out_unlock:
    512 #ifndef __NetBSD__
    513 	mutex_unlock(&dev->struct_mutex);
    514 #endif
    515 	if (chan)
    516 		nouveau_bo_vma_del(nvbo, &fbcon->nouveau_fb.vma);
    517 	nouveau_bo_unmap(nvbo);
    518 out_unpin:
    519 	nouveau_bo_unpin(nvbo);
    520 out_unref:
    521 	nouveau_gem_object_del(&nvbo->gem);
    522 out:
    523 	return ret;
    524 }
    525 
    526 void
    527 nouveau_fbcon_output_poll_changed(struct drm_device *dev)
    528 {
    529 	struct nouveau_drm *drm = nouveau_drm(dev);
    530 	if (drm->fbcon)
    531 		drm_fb_helper_hotplug_event(&drm->fbcon->helper);
    532 }
    533 
    534 static int
    535 nouveau_fbcon_destroy(struct drm_device *dev, struct nouveau_fbdev *fbcon)
    536 {
    537 	struct nouveau_framebuffer *nouveau_fb = &fbcon->nouveau_fb;
    538 
    539 #ifdef __NetBSD__
    540 	if (fbcon->helper.fbdev) {
    541 		int ret;
    542 
    543 		/* XXX errno NetBSD->Linux */
    544 		ret = -config_detach(fbcon->helper.fbdev, DETACH_FORCE);
    545 		if (ret)
    546 			DRM_ERROR("failed to detach nouveaufb: %d\n", ret);
    547 		fbcon->helper.fbdev = NULL;
    548 	}
    549 #else
    550 	drm_fb_helper_unregister_fbi(&fbcon->helper);
    551 	drm_fb_helper_release_fbi(&fbcon->helper);
    552 #endif
    553 
    554 	if (nouveau_fb->nvbo) {
    555 		nouveau_bo_unmap(nouveau_fb->nvbo);
    556 		nouveau_bo_vma_del(nouveau_fb->nvbo, &nouveau_fb->vma);
    557 		nouveau_bo_unpin(nouveau_fb->nvbo);
    558 		drm_gem_object_unreference_unlocked(&nouveau_fb->nvbo->gem);
    559 		nouveau_fb->nvbo = NULL;
    560 	}
    561 	drm_fb_helper_fini(&fbcon->helper);
    562 	drm_framebuffer_unregister_private(&nouveau_fb->base);
    563 	drm_framebuffer_cleanup(&nouveau_fb->base);
    564 	return 0;
    565 }
    566 
    567 #ifndef __NetBSD__		/* XXX nouveau fbaccel */
    568 void nouveau_fbcon_gpu_lockup(struct fb_info *info)
    569 {
    570 	struct nouveau_fbdev *fbcon = info->par;
    571 	struct nouveau_drm *drm = nouveau_drm(fbcon->dev);
    572 
    573 	NV_ERROR(drm, "GPU lockup - switching to software fbcon\n");
    574 	info->flags |= FBINFO_HWACCEL_DISABLED;
    575 }
    576 #endif
    577 
    578 static const struct drm_fb_helper_funcs nouveau_fbcon_helper_funcs = {
    579 	.gamma_set = nouveau_fbcon_gamma_set,
    580 	.gamma_get = nouveau_fbcon_gamma_get,
    581 	.fb_probe = nouveau_fbcon_create,
    582 };
    583 
    584 void
    585 nouveau_fbcon_set_suspend(struct drm_device *dev, int state)
    586 {
    587 	struct nouveau_drm *drm = nouveau_drm(dev);
    588 	if (drm->fbcon) {
    589 #ifndef __NetBSD__
    590 		console_lock();
    591 		if (state == FBINFO_STATE_RUNNING)
    592 			nouveau_fbcon_accel_restore(dev);
    593 		drm_fb_helper_set_suspend(&drm->fbcon->helper, state);
    594 		if (state != FBINFO_STATE_RUNNING)
    595 			nouveau_fbcon_accel_save_disable(dev);
    596 		console_unlock();
    597 #endif
    598 	}
    599 }
    600 
    601 int
    602 nouveau_fbcon_init(struct drm_device *dev)
    603 {
    604 	struct nouveau_drm *drm = nouveau_drm(dev);
    605 	struct nouveau_fbdev *fbcon;
    606 	int preferred_bpp;
    607 	int ret;
    608 
    609 	if (!dev->mode_config.num_crtc ||
    610 	    (dev->pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
    611 		return 0;
    612 
    613 	fbcon = kzalloc(sizeof(struct nouveau_fbdev), GFP_KERNEL);
    614 	if (!fbcon)
    615 		return -ENOMEM;
    616 
    617 	fbcon->dev = dev;
    618 	drm->fbcon = fbcon;
    619 
    620 	drm_fb_helper_prepare(dev, &fbcon->helper, &nouveau_fbcon_helper_funcs);
    621 
    622 	ret = drm_fb_helper_init(dev, &fbcon->helper,
    623 				 dev->mode_config.num_crtc, 4);
    624 	if (ret)
    625 		goto free;
    626 
    627 	ret = drm_fb_helper_single_add_all_connectors(&fbcon->helper);
    628 	if (ret)
    629 		goto fini;
    630 
    631 	if (drm->device.info.ram_size <= 32 * 1024 * 1024)
    632 		preferred_bpp = 8;
    633 	else
    634 	if (drm->device.info.ram_size <= 64 * 1024 * 1024)
    635 		preferred_bpp = 16;
    636 	else
    637 		preferred_bpp = 32;
    638 
    639 	/* disable all the possible outputs/crtcs before entering KMS mode */
    640 	drm_helper_disable_unused_functions(dev);
    641 
    642 	ret = drm_fb_helper_initial_config(&fbcon->helper, preferred_bpp);
    643 	if (ret)
    644 		goto fini;
    645 
    646 #ifndef __NetBSD__
    647 	if (fbcon->helper.fbdev)
    648 		fbcon->helper.fbdev->pixmap.buf_align = 4;
    649 #endif
    650 	return 0;
    651 
    652 fini:
    653 	drm_fb_helper_fini(&fbcon->helper);
    654 free:
    655 	kfree(fbcon);
    656 	return ret;
    657 }
    658 
    659 void
    660 nouveau_fbcon_fini(struct drm_device *dev)
    661 {
    662 	struct nouveau_drm *drm = nouveau_drm(dev);
    663 
    664 	if (!drm->fbcon)
    665 		return;
    666 
    667 	nouveau_fbcon_accel_fini(dev);
    668 	nouveau_fbcon_destroy(dev, drm->fbcon);
    669 	kfree(drm->fbcon);
    670 	drm->fbcon = NULL;
    671 }
    672