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