Home | History | Annotate | Line # | Download | only in nouveau
      1 /*	$NetBSD: nouveau_fbcon.c,v 1.16 2021/12/20 20:34:58 chs 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.16 2021/12/20 20:34:58 chs 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/init.h>
     41 #include <linux/screen_info.h>
     42 #include <linux/vga_switcheroo.h>
     43 #include <linux/console.h>
     44 
     45 #include <drm/drm_crtc.h>
     46 #include <drm/drm_crtc_helper.h>
     47 #include <drm/drm_fb_helper.h>
     48 #include <drm/drm_fourcc.h>
     49 #include <drm/drm_atomic.h>
     50 
     51 #include "nouveau_drv.h"
     52 #include "nouveau_gem.h"
     53 #include "nouveau_bo.h"
     54 #include "nouveau_fbcon.h"
     55 #include "nouveau_chan.h"
     56 #include "nouveau_vmm.h"
     57 
     58 #include "nouveau_crtc.h"
     59 
     60 #ifdef __NetBSD__
     61 #include "nouveaufb.h"
     62 #include <linux/nbsd-namespace.h>
     63 #endif
     64 
     65 #ifdef __NetBSD__		/* XXX nouveau fbaccel */
     66 int nouveau_nofbaccel = 1;
     67 #else
     68 MODULE_PARM_DESC(nofbaccel, "Disable fbcon acceleration");
     69 int nouveau_nofbaccel = 0;
     70 module_param_named(nofbaccel, nouveau_nofbaccel, int, 0400);
     71 #endif
     72 
     73 MODULE_PARM_DESC(fbcon_bpp, "fbcon bits-per-pixel (default: auto)");
     74 static int nouveau_fbcon_bpp;
     75 module_param_named(fbcon_bpp, nouveau_fbcon_bpp, int, 0400);
     76 
     77 #ifndef __NetBSD__		/* XXX nouveau fbaccel */
     78 static void
     79 nouveau_fbcon_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
     80 {
     81 	struct nouveau_fbdev *fbcon = info->par;
     82 	struct nouveau_drm *drm = nouveau_drm(fbcon->helper.dev);
     83 	struct nvif_device *device = &drm->client.device;
     84 	int ret;
     85 
     86 	if (info->state != FBINFO_STATE_RUNNING)
     87 		return;
     88 
     89 	ret = -ENODEV;
     90 	if (!in_interrupt() && !(info->flags & FBINFO_HWACCEL_DISABLED) &&
     91 	    mutex_trylock(&drm->client.mutex)) {
     92 		if (device->info.family < NV_DEVICE_INFO_V0_TESLA)
     93 			ret = nv04_fbcon_fillrect(info, rect);
     94 		else
     95 		if (device->info.family < NV_DEVICE_INFO_V0_FERMI)
     96 			ret = nv50_fbcon_fillrect(info, rect);
     97 		else
     98 			ret = nvc0_fbcon_fillrect(info, rect);
     99 		mutex_unlock(&drm->client.mutex);
    100 	}
    101 
    102 	if (ret == 0)
    103 		return;
    104 
    105 	if (ret != -ENODEV)
    106 		nouveau_fbcon_gpu_lockup(info);
    107 	drm_fb_helper_cfb_fillrect(info, rect);
    108 }
    109 
    110 static void
    111 nouveau_fbcon_copyarea(struct fb_info *info, const struct fb_copyarea *image)
    112 {
    113 	struct nouveau_fbdev *fbcon = info->par;
    114 	struct nouveau_drm *drm = nouveau_drm(fbcon->helper.dev);
    115 	struct nvif_device *device = &drm->client.device;
    116 	int ret;
    117 
    118 	if (info->state != FBINFO_STATE_RUNNING)
    119 		return;
    120 
    121 	ret = -ENODEV;
    122 	if (!in_interrupt() && !(info->flags & FBINFO_HWACCEL_DISABLED) &&
    123 	    mutex_trylock(&drm->client.mutex)) {
    124 		if (device->info.family < NV_DEVICE_INFO_V0_TESLA)
    125 			ret = nv04_fbcon_copyarea(info, image);
    126 		else
    127 		if (device->info.family < NV_DEVICE_INFO_V0_FERMI)
    128 			ret = nv50_fbcon_copyarea(info, image);
    129 		else
    130 			ret = nvc0_fbcon_copyarea(info, image);
    131 		mutex_unlock(&drm->client.mutex);
    132 	}
    133 
    134 	if (ret == 0)
    135 		return;
    136 
    137 	if (ret != -ENODEV)
    138 		nouveau_fbcon_gpu_lockup(info);
    139 	drm_fb_helper_cfb_copyarea(info, image);
    140 }
    141 
    142 static void
    143 nouveau_fbcon_imageblit(struct fb_info *info, const struct fb_image *image)
    144 {
    145 	struct nouveau_fbdev *fbcon = info->par;
    146 	struct nouveau_drm *drm = nouveau_drm(fbcon->helper.dev);
    147 	struct nvif_device *device = &drm->client.device;
    148 	int ret;
    149 
    150 	if (info->state != FBINFO_STATE_RUNNING)
    151 		return;
    152 
    153 	ret = -ENODEV;
    154 	if (!in_interrupt() && !(info->flags & FBINFO_HWACCEL_DISABLED) &&
    155 	    mutex_trylock(&drm->client.mutex)) {
    156 		if (device->info.family < NV_DEVICE_INFO_V0_TESLA)
    157 			ret = nv04_fbcon_imageblit(info, image);
    158 		else
    159 		if (device->info.family < NV_DEVICE_INFO_V0_FERMI)
    160 			ret = nv50_fbcon_imageblit(info, image);
    161 		else
    162 			ret = nvc0_fbcon_imageblit(info, image);
    163 		mutex_unlock(&drm->client.mutex);
    164 	}
    165 
    166 	if (ret == 0)
    167 		return;
    168 
    169 	if (ret != -ENODEV)
    170 		nouveau_fbcon_gpu_lockup(info);
    171 	drm_fb_helper_cfb_imageblit(info, image);
    172 }
    173 
    174 static int
    175 nouveau_fbcon_sync(struct fb_info *info)
    176 {
    177 	struct nouveau_fbdev *fbcon = info->par;
    178 	struct nouveau_drm *drm = nouveau_drm(fbcon->helper.dev);
    179 	struct nouveau_channel *chan = drm->channel;
    180 	int ret;
    181 
    182 	if (!chan || !chan->accel_done || in_interrupt() ||
    183 	    info->state != FBINFO_STATE_RUNNING ||
    184 	    info->flags & FBINFO_HWACCEL_DISABLED)
    185 		return 0;
    186 
    187 	if (!mutex_trylock(&drm->client.mutex))
    188 		return 0;
    189 
    190 	ret = nouveau_channel_idle(chan);
    191 	mutex_unlock(&drm->client.mutex);
    192 	if (ret) {
    193 		nouveau_fbcon_gpu_lockup(info);
    194 		return 0;
    195 	}
    196 
    197 	chan->accel_done = false;
    198 	return 0;
    199 }
    200 
    201 static int
    202 nouveau_fbcon_open(struct fb_info *info, int user)
    203 {
    204 	struct nouveau_fbdev *fbcon = info->par;
    205 	struct nouveau_drm *drm = nouveau_drm(fbcon->helper.dev);
    206 	int ret = pm_runtime_get_sync(drm->dev->dev);
    207 	if (ret < 0 && ret != -EACCES)
    208 		return ret;
    209 	return 0;
    210 }
    211 
    212 static int
    213 nouveau_fbcon_release(struct fb_info *info, int user)
    214 {
    215 	struct nouveau_fbdev *fbcon = info->par;
    216 	struct nouveau_drm *drm = nouveau_drm(fbcon->helper.dev);
    217 	pm_runtime_put(drm->dev->dev);
    218 	return 0;
    219 }
    220 
    221 static const struct fb_ops nouveau_fbcon_ops = {
    222 	.owner = THIS_MODULE,
    223 	DRM_FB_HELPER_DEFAULT_OPS,
    224 	.fb_open = nouveau_fbcon_open,
    225 	.fb_release = nouveau_fbcon_release,
    226 	.fb_fillrect = nouveau_fbcon_fillrect,
    227 	.fb_copyarea = nouveau_fbcon_copyarea,
    228 	.fb_imageblit = nouveau_fbcon_imageblit,
    229 	.fb_sync = nouveau_fbcon_sync,
    230 };
    231 
    232 static const struct fb_ops nouveau_fbcon_sw_ops = {
    233 	.owner = THIS_MODULE,
    234 	DRM_FB_HELPER_DEFAULT_OPS,
    235 	.fb_open = nouveau_fbcon_open,
    236 	.fb_release = nouveau_fbcon_release,
    237 	.fb_fillrect = drm_fb_helper_cfb_fillrect,
    238 	.fb_copyarea = drm_fb_helper_cfb_copyarea,
    239 	.fb_imageblit = drm_fb_helper_cfb_imageblit,
    240 };
    241 #endif	/* XXX nouveau fbaccel */
    242 
    243 void
    244 nouveau_fbcon_accel_save_disable(struct drm_device *dev)
    245 {
    246 #ifndef __NetBSD__		/* XXX nouveau fbaccel */
    247 	struct nouveau_drm *drm = nouveau_drm(dev);
    248 	if (drm->fbcon && drm->fbcon->helper.fbdev) {
    249 		drm->fbcon->saved_flags = drm->fbcon->helper.fbdev->flags;
    250 		drm->fbcon->helper.fbdev->flags |= FBINFO_HWACCEL_DISABLED;
    251 	}
    252 #endif
    253 }
    254 
    255 void
    256 nouveau_fbcon_accel_restore(struct drm_device *dev)
    257 {
    258 #ifndef __NetBSD__		/* XXX nouveau fbaccel */
    259 	struct nouveau_drm *drm = nouveau_drm(dev);
    260 	if (drm->fbcon && drm->fbcon->helper.fbdev) {
    261 		drm->fbcon->helper.fbdev->flags = drm->fbcon->saved_flags;
    262 	}
    263 #endif
    264 }
    265 
    266 static void
    267 nouveau_fbcon_accel_fini(struct drm_device *dev)
    268 {
    269 	struct nouveau_drm *drm = nouveau_drm(dev);
    270 	struct nouveau_fbdev *fbcon = drm->fbcon;
    271 	if (fbcon && drm->channel) {
    272 		console_lock();
    273 #ifndef __NetBSD__		/* XXX nouveau fbaccel */
    274 		if (fbcon->helper.fbdev)
    275 			fbcon->helper.fbdev->flags |= FBINFO_HWACCEL_DISABLED;
    276 #endif
    277 		console_unlock();
    278 		nouveau_channel_idle(drm->channel);
    279 		nvif_object_fini(&fbcon->twod);
    280 		nvif_object_fini(&fbcon->blit);
    281 		nvif_object_fini(&fbcon->gdi);
    282 		nvif_object_fini(&fbcon->patt);
    283 		nvif_object_fini(&fbcon->rop);
    284 		nvif_object_fini(&fbcon->clip);
    285 		nvif_object_fini(&fbcon->surf2d);
    286 	}
    287 }
    288 
    289 #ifndef __NetBSD__		/* XXX nouveau fbaccel */
    290 static void
    291 nouveau_fbcon_accel_init(struct drm_device *dev)
    292 {
    293 	struct nouveau_drm *drm = nouveau_drm(dev);
    294 	struct nouveau_fbdev *fbcon = drm->fbcon;
    295 	struct fb_info *info = fbcon->helper.fbdev;
    296 	int ret;
    297 
    298 	if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA)
    299 		ret = nv04_fbcon_accel_init(info);
    300 	else
    301 	if (drm->client.device.info.family < NV_DEVICE_INFO_V0_FERMI)
    302 		ret = nv50_fbcon_accel_init(info);
    303 	else
    304 		ret = nvc0_fbcon_accel_init(info);
    305 
    306 	if (ret == 0)
    307 		info->fbops = &nouveau_fbcon_ops;
    308 }
    309 #endif
    310 
    311 static void
    312 nouveau_fbcon_zfill(struct drm_device *dev, struct nouveau_fbdev *fbcon)
    313 {
    314 #ifdef __NetBSD__		/* XXX nouveau fbaccel */
    315 	struct drm_framebuffer *fb = fbcon->helper.fb;
    316 	struct nouveau_framebuffer *nvfb = container_of(fb,
    317 	    struct nouveau_framebuffer, base);
    318 	struct nouveau_bo *nvbo = nvfb->nvbo;
    319 
    320 	(void)memset(__UNVOLATILE(nvbo_kmap_obj_iovirtual(nvbo)), 0,
    321 	    nvbo->bo.num_pages << PAGE_SHIFT);
    322 #else
    323 	struct fb_info *info = fbcon->helper.fbdev;
    324 	struct fb_fillrect rect;
    325 
    326 	/* Clear the entire fbcon.  The drm will program every connector
    327 	 * with it's preferred mode.  If the sizes differ, one display will
    328 	 * quite likely have garbage around the console.
    329 	 */
    330 	rect.dx = rect.dy = 0;
    331 	rect.width = info->var.xres_virtual;
    332 	rect.height = info->var.yres_virtual;
    333 	rect.color = 0;
    334 	rect.rop = ROP_COPY;
    335 	info->fbops->fb_fillrect(info, &rect);
    336 #endif
    337 }
    338 
    339 #ifdef __NetBSD__
    340 static int
    341 nouveau_fbcon_print(void *aux, const char *pnp)
    342 {
    343 	if (pnp)
    344 		aprint_normal("nouveaufbbus at %s", pnp);
    345 
    346 	return (UNCONF);
    347 }
    348 #endif
    349 
    350 static int
    351 nouveau_fbcon_create(struct drm_fb_helper *helper,
    352 		     struct drm_fb_helper_surface_size *sizes)
    353 {
    354 	struct nouveau_fbdev *fbcon =
    355 		container_of(helper, struct nouveau_fbdev, helper);
    356 	struct drm_device *dev = fbcon->helper.dev;
    357 	struct nouveau_drm *drm = nouveau_drm(dev);
    358 	struct nvif_device *device = &drm->client.device;
    359 #ifndef __NetBSD__
    360 	struct fb_info *info;
    361 #endif
    362 	struct nouveau_framebuffer *fb;
    363 	struct nouveau_channel *chan;
    364 	struct nouveau_bo *nvbo;
    365 	struct drm_mode_fb_cmd2 mode_cmd;
    366 	int ret;
    367 
    368 	mode_cmd.width = sizes->surface_width;
    369 	mode_cmd.height = sizes->surface_height;
    370 
    371 	mode_cmd.pitches[0] = mode_cmd.width * (sizes->surface_bpp >> 3);
    372 	mode_cmd.pitches[0] = roundup(mode_cmd.pitches[0], 256);
    373 
    374 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
    375 							  sizes->surface_depth);
    376 
    377 	ret = nouveau_gem_new(&drm->client, mode_cmd.pitches[0] *
    378 			      mode_cmd.height, 0, NOUVEAU_GEM_DOMAIN_VRAM,
    379 			      0, 0x0000, &nvbo);
    380 	if (ret) {
    381 		NV_ERROR(drm, "failed to allocate framebuffer\n");
    382 		goto out;
    383 	}
    384 
    385 	ret = nouveau_framebuffer_new(dev, &mode_cmd, nvbo, &fb);
    386 	if (ret)
    387 		goto out_unref;
    388 
    389 	ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_VRAM, false);
    390 	if (ret) {
    391 		NV_ERROR(drm, "failed to pin fb: %d\n", ret);
    392 		goto out_unref;
    393 	}
    394 
    395 	ret = nouveau_bo_map(nvbo);
    396 	if (ret) {
    397 		NV_ERROR(drm, "failed to map fb: %d\n", ret);
    398 		goto out_unpin;
    399 	}
    400 
    401 	chan = nouveau_nofbaccel ? NULL : drm->channel;
    402 	if (chan && device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
    403 		ret = nouveau_vma_new(nvbo, chan->vmm, &fb->vma);
    404 		if (ret) {
    405 			NV_ERROR(drm, "failed to map fb into chan: %d\n", ret);
    406 			chan = NULL;
    407 		}
    408 	}
    409 
    410 #ifdef __NetBSD__
    411 	fbcon->helper.fb = &fb->base;
    412 
    413 	nouveau_fbcon_zfill(dev, fbcon);
    414 
    415 	struct nouveaufb_attach_args nfa;
    416 
    417 	memset(&nfa, 0, sizeof(nfa));
    418 	nfa.nfa_fb_helper = &fbcon->helper;
    419 	nfa.nfa_fb_sizes = *sizes;
    420 	nfa.nfa_fb_ptr = nvbo_kmap_obj_iovirtual(nvbo);
    421 	nfa.nfa_fb_linebytes = mode_cmd.pitches[0];
    422 
    423 	KERNEL_LOCK(1, NULL);
    424 	helper->fbdev = config_found(dev->dev, &nfa, nouveau_fbcon_print,
    425 	    CFARGS(.iattr = "nouveaufbbus"));
    426 	KERNEL_UNLOCK_ONE(NULL);
    427 	if (helper->fbdev == NULL) {
    428 		goto out_unlock;
    429 	}
    430 
    431 	return 0;
    432 #else
    433 	info = drm_fb_helper_alloc_fbi(helper);
    434 	if (IS_ERR(info)) {
    435 		ret = PTR_ERR(info);
    436 		goto out_unlock;
    437 	}
    438 
    439 	/* setup helper */
    440 	fbcon->helper.fb = &fb->base;
    441 
    442 	if (!chan)
    443 		info->flags = FBINFO_HWACCEL_DISABLED;
    444 	else
    445 		info->flags = FBINFO_HWACCEL_COPYAREA |
    446 			      FBINFO_HWACCEL_FILLRECT |
    447 			      FBINFO_HWACCEL_IMAGEBLIT;
    448 	info->fbops = &nouveau_fbcon_sw_ops;
    449 	info->fix.smem_start = fb->nvbo->bo.mem.bus.base +
    450 			       fb->nvbo->bo.mem.bus.offset;
    451 	info->fix.smem_len = fb->nvbo->bo.mem.num_pages << PAGE_SHIFT;
    452 
    453 	info->screen_base = nvbo_kmap_obj_iovirtual(fb->nvbo);
    454 	info->screen_size = fb->nvbo->bo.mem.num_pages << PAGE_SHIFT;
    455 
    456 	drm_fb_helper_fill_info(info, &fbcon->helper, sizes);
    457 
    458 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
    459 
    460 	if (chan)
    461 		nouveau_fbcon_accel_init(dev);
    462 	nouveau_fbcon_zfill(dev, fbcon);
    463 
    464 	/* To allow resizeing without swapping buffers */
    465 	NV_INFO(drm, "allocated %dx%d fb: 0x%jx, bo %p\n",
    466 		nouveau_fb->base.width, nouveau_fb->base.height,
    467 		(uintmax_t)nvbo->bo.offset, nvbo);
    468 
    469 	vga_switcheroo_client_fb_set(dev->pdev, info);
    470 	return 0;
    471 #endif	/* defined(__NetBSD__) */
    472 
    473 out_unlock:
    474 	if (chan)
    475 		nouveau_vma_del(&fb->vma);
    476 	nouveau_bo_unmap(fb->nvbo);
    477 out_unpin:
    478 	nouveau_bo_unpin(fb->nvbo);
    479 out_unref:
    480 	nouveau_bo_ref(NULL, &fb->nvbo);
    481 out:
    482 	return ret;
    483 }
    484 
    485 static int
    486 nouveau_fbcon_destroy(struct drm_device *dev, struct nouveau_fbdev *fbcon)
    487 {
    488 	struct nouveau_framebuffer *nouveau_fb = nouveau_framebuffer(fbcon->helper.fb);
    489 
    490 	drm_fb_helper_unregister_fbi(&fbcon->helper);
    491 	drm_fb_helper_fini(&fbcon->helper);
    492 
    493 	if (nouveau_fb && nouveau_fb->nvbo) {
    494 		nouveau_vma_del(&nouveau_fb->vma);
    495 		nouveau_bo_unmap(nouveau_fb->nvbo);
    496 		nouveau_bo_unpin(nouveau_fb->nvbo);
    497 		drm_framebuffer_put(&nouveau_fb->base);
    498 	}
    499 
    500 	return 0;
    501 }
    502 
    503 #ifndef __NetBSD__		/* XXX nouveau fbaccel */
    504 void nouveau_fbcon_gpu_lockup(struct fb_info *info)
    505 {
    506 	struct nouveau_fbdev *fbcon = info->par;
    507 	struct nouveau_drm *drm = nouveau_drm(fbcon->helper.dev);
    508 
    509 	NV_ERROR(drm, "GPU lockup - switching to software fbcon\n");
    510 	info->flags |= FBINFO_HWACCEL_DISABLED;
    511 }
    512 #endif
    513 
    514 static const struct drm_fb_helper_funcs nouveau_fbcon_helper_funcs = {
    515 	.fb_probe = nouveau_fbcon_create,
    516 };
    517 
    518 static void
    519 nouveau_fbcon_set_suspend_work(struct work_struct *work)
    520 {
    521 	struct nouveau_drm *drm = container_of(work, typeof(*drm), fbcon_work);
    522 	int state = READ_ONCE(drm->fbcon_new_state);
    523 
    524 	if (state == FBINFO_STATE_RUNNING)
    525 		pm_runtime_get_sync(drm->dev->dev);
    526 
    527 	console_lock();
    528 	if (state == FBINFO_STATE_RUNNING)
    529 		nouveau_fbcon_accel_restore(drm->dev);
    530 	drm_fb_helper_set_suspend(&drm->fbcon->helper, state);
    531 	if (state != FBINFO_STATE_RUNNING)
    532 		nouveau_fbcon_accel_save_disable(drm->dev);
    533 	console_unlock();
    534 
    535 	if (state == FBINFO_STATE_RUNNING) {
    536 		nouveau_fbcon_hotplug_resume(drm->fbcon);
    537 		pm_runtime_mark_last_busy(drm->dev->dev);
    538 		pm_runtime_put_sync(drm->dev->dev);
    539 	}
    540 }
    541 
    542 void
    543 nouveau_fbcon_set_suspend(struct drm_device *dev, int state)
    544 {
    545 	struct nouveau_drm *drm = nouveau_drm(dev);
    546 
    547 	if (!drm->fbcon)
    548 		return;
    549 
    550 	drm->fbcon_new_state = state;
    551 	/* Since runtime resume can happen as a result of a sysfs operation,
    552 	 * it's possible we already have the console locked. So handle fbcon
    553 	 * init/deinit from a seperate work thread
    554 	 */
    555 	schedule_work(&drm->fbcon_work);
    556 }
    557 
    558 void
    559 nouveau_fbcon_output_poll_changed(struct drm_device *dev)
    560 {
    561 	struct nouveau_drm *drm = nouveau_drm(dev);
    562 	struct nouveau_fbdev *fbcon = drm->fbcon;
    563 	int ret;
    564 
    565 	if (!fbcon)
    566 		return;
    567 
    568 	mutex_lock(&fbcon->hotplug_lock);
    569 
    570 	ret = pm_runtime_get(dev->dev);
    571 	if (ret == 1 || ret == -EACCES) {
    572 		drm_fb_helper_hotplug_event(&fbcon->helper);
    573 
    574 		pm_runtime_mark_last_busy(dev->dev);
    575 		pm_runtime_put_autosuspend(dev->dev);
    576 	} else if (ret == 0) {
    577 		/* If the GPU was already in the process of suspending before
    578 		 * this event happened, then we can't block here as we'll
    579 		 * deadlock the runtime pmops since they wait for us to
    580 		 * finish. So, just defer this event for when we runtime
    581 		 * resume again. It will be handled by fbcon_work.
    582 		 */
    583 		NV_DEBUG(drm, "fbcon HPD event deferred until runtime resume\n");
    584 		fbcon->hotplug_waiting = true;
    585 		pm_runtime_put_noidle(drm->dev->dev);
    586 	} else {
    587 		DRM_WARN("fbcon HPD event lost due to RPM failure: %d\n",
    588 			 ret);
    589 	}
    590 
    591 	mutex_unlock(&fbcon->hotplug_lock);
    592 }
    593 
    594 void
    595 nouveau_fbcon_hotplug_resume(struct nouveau_fbdev *fbcon)
    596 {
    597 	struct nouveau_drm *drm;
    598 
    599 	if (!fbcon)
    600 		return;
    601 	drm = nouveau_drm(fbcon->helper.dev);
    602 
    603 	mutex_lock(&fbcon->hotplug_lock);
    604 	if (fbcon->hotplug_waiting) {
    605 		fbcon->hotplug_waiting = false;
    606 
    607 		NV_DEBUG(drm, "Handling deferred fbcon HPD events\n");
    608 		drm_fb_helper_hotplug_event(&fbcon->helper);
    609 	}
    610 	mutex_unlock(&fbcon->hotplug_lock);
    611 }
    612 
    613 int
    614 nouveau_fbcon_init(struct drm_device *dev)
    615 {
    616 	struct nouveau_drm *drm = nouveau_drm(dev);
    617 	struct nouveau_fbdev *fbcon;
    618 	int preferred_bpp = nouveau_fbcon_bpp;
    619 	int ret;
    620 
    621 	if (!dev->mode_config.num_crtc ||
    622 	    (dev->pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
    623 		return 0;
    624 
    625 	fbcon = kzalloc(sizeof(struct nouveau_fbdev), GFP_KERNEL);
    626 	if (!fbcon)
    627 		return -ENOMEM;
    628 
    629 	drm->fbcon = fbcon;
    630 	INIT_WORK(&drm->fbcon_work, nouveau_fbcon_set_suspend_work);
    631 	mutex_init(&fbcon->hotplug_lock);
    632 
    633 	drm_fb_helper_prepare(dev, &fbcon->helper, &nouveau_fbcon_helper_funcs);
    634 
    635 	ret = drm_fb_helper_init(dev, &fbcon->helper, 4);
    636 	if (ret)
    637 		goto free;
    638 
    639 	ret = drm_fb_helper_single_add_all_connectors(&fbcon->helper);
    640 	if (ret)
    641 		goto fini;
    642 
    643 	if (preferred_bpp != 8 && preferred_bpp != 16 && preferred_bpp != 32) {
    644 		if (drm->client.device.info.ram_size <= 32 * 1024 * 1024)
    645 			preferred_bpp = 8;
    646 		else
    647 		if (drm->client.device.info.ram_size <= 64 * 1024 * 1024)
    648 			preferred_bpp = 16;
    649 		else
    650 			preferred_bpp = 32;
    651 	}
    652 
    653 	/* disable all the possible outputs/crtcs before entering KMS mode */
    654 	if (!drm_drv_uses_atomic_modeset(dev))
    655 		drm_helper_disable_unused_functions(dev);
    656 
    657 	ret = drm_fb_helper_initial_config(&fbcon->helper, preferred_bpp);
    658 	if (ret)
    659 		goto fini;
    660 
    661 #ifndef __NetBSD__
    662 	if (fbcon->helper.fbdev)
    663 		fbcon->helper.fbdev->pixmap.buf_align = 4;
    664 #endif
    665 	return 0;
    666 
    667 fini:
    668 	drm_fb_helper_fini(&fbcon->helper);
    669 free:
    670 	kfree(fbcon);
    671 	return ret;
    672 }
    673 
    674 void
    675 nouveau_fbcon_fini(struct drm_device *dev)
    676 {
    677 	struct nouveau_drm *drm = nouveau_drm(dev);
    678 
    679 	if (!drm->fbcon)
    680 		return;
    681 
    682 	nouveau_fbcon_accel_fini(dev);
    683 	nouveau_fbcon_destroy(dev, drm->fbcon);
    684 	mutex_destroy(&drm->fbcon->hotplug_lock);
    685 	kfree(drm->fbcon);
    686 	drm->fbcon = NULL;
    687 }
    688