Home | History | Annotate | Line # | Download | only in display
intel_fbdev.c revision 1.8
      1 /*	$NetBSD: intel_fbdev.c,v 1.8 2021/12/19 11:45:01 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: intel_fbdev.c,v 1.8 2021/12/19 11:45:01 riastradh Exp $");
     31 
     32 #include <linux/async.h>
     33 #include <linux/console.h>
     34 #include <linux/delay.h>
     35 #include <linux/errno.h>
     36 #include <linux/init.h>
     37 #include <linux/kernel.h>
     38 #include <linux/mm.h>
     39 #include <linux/module.h>
     40 #include <linux/string.h>
     41 #include <linux/sysrq.h>
     42 #include <linux/tty.h>
     43 #include <linux/vga_switcheroo.h>
     44 
     45 #include <drm/drm_crtc.h>
     46 #include <drm/drm_fb_helper.h>
     47 #include <drm/drm_fourcc.h>
     48 #include <drm/i915_drm.h>
     49 
     50 #include "i915_drv.h"
     51 #include "intel_display_types.h"
     52 #include "intel_fbdev.h"
     53 #include "intel_frontbuffer.h"
     54 
     55 static struct intel_frontbuffer *to_frontbuffer(struct intel_fbdev *ifbdev)
     56 {
     57 	return ifbdev->fb->frontbuffer;
     58 }
     59 
     60 static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev)
     61 {
     62 	intel_frontbuffer_invalidate(to_frontbuffer(ifbdev), ORIGIN_CPU);
     63 }
     64 
     65 #ifdef __NetBSD__
     66 #include "intelfb.h"
     67 #include <linux/nbsd-namespace.h>
     68 #endif
     69 
     70 #ifndef __NetBSD__
     71 static int intel_fbdev_set_par(struct fb_info *info)
     72 {
     73 	struct drm_fb_helper *fb_helper = info->par;
     74 	struct intel_fbdev *ifbdev =
     75 		container_of(fb_helper, struct intel_fbdev, helper);
     76 	int ret;
     77 
     78 	ret = drm_fb_helper_set_par(info);
     79 	if (ret == 0)
     80 		intel_fbdev_invalidate(ifbdev);
     81 
     82 	return ret;
     83 }
     84 
     85 static int intel_fbdev_blank(int blank, struct fb_info *info)
     86 {
     87 	struct drm_fb_helper *fb_helper = info->par;
     88 	struct intel_fbdev *ifbdev =
     89 		container_of(fb_helper, struct intel_fbdev, helper);
     90 	int ret;
     91 
     92 	ret = drm_fb_helper_blank(blank, info);
     93 	if (ret == 0)
     94 		intel_fbdev_invalidate(ifbdev);
     95 
     96 	return ret;
     97 }
     98 
     99 static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
    100 				   struct fb_info *info)
    101 {
    102 	struct drm_fb_helper *fb_helper = info->par;
    103 	struct intel_fbdev *ifbdev =
    104 		container_of(fb_helper, struct intel_fbdev, helper);
    105 	int ret;
    106 
    107 	ret = drm_fb_helper_pan_display(var, info);
    108 	if (ret == 0)
    109 		intel_fbdev_invalidate(ifbdev);
    110 
    111 	return ret;
    112 }
    113 
    114 static const struct fb_ops intelfb_ops = {
    115 	.owner = THIS_MODULE,
    116 	DRM_FB_HELPER_DEFAULT_OPS,
    117 	.fb_set_par = intel_fbdev_set_par,
    118 	.fb_fillrect = drm_fb_helper_cfb_fillrect,
    119 	.fb_copyarea = drm_fb_helper_cfb_copyarea,
    120 	.fb_imageblit = drm_fb_helper_cfb_imageblit,
    121 	.fb_pan_display = intel_fbdev_pan_display,
    122 	.fb_blank = intel_fbdev_blank,
    123 };
    124 #endif
    125 
    126 static int intelfb_alloc(struct drm_fb_helper *helper,
    127 			 struct drm_fb_helper_surface_size *sizes)
    128 {
    129 	struct intel_fbdev *ifbdev =
    130 		container_of(helper, struct intel_fbdev, helper);
    131 	struct drm_framebuffer *fb;
    132 	struct drm_device *dev = helper->dev;
    133 	struct drm_i915_private *dev_priv = to_i915(dev);
    134 	struct drm_mode_fb_cmd2 mode_cmd = {};
    135 	struct drm_i915_gem_object *obj;
    136 	int size;
    137 
    138 	/* we don't do packed 24bpp */
    139 	if (sizes->surface_bpp == 24)
    140 		sizes->surface_bpp = 32;
    141 
    142 	mode_cmd.width = sizes->surface_width;
    143 	mode_cmd.height = sizes->surface_height;
    144 
    145 	mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
    146 				    DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
    147 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
    148 							  sizes->surface_depth);
    149 
    150 	size = mode_cmd.pitches[0] * mode_cmd.height;
    151 	size = PAGE_ALIGN(size);
    152 
    153 	/* If the FB is too big, just don't use it since fbdev is not very
    154 	 * important and we should probably use that space with FBC or other
    155 	 * features. */
    156 	obj = ERR_PTR(-ENODEV);
    157 	if (size * 2 < dev_priv->stolen_usable_size)
    158 		obj = i915_gem_object_create_stolen(dev_priv, size);
    159 	if (IS_ERR(obj))
    160 		obj = i915_gem_object_create_shmem(dev_priv, size);
    161 	if (IS_ERR(obj)) {
    162 		DRM_ERROR("failed to allocate framebuffer\n");
    163 		return PTR_ERR(obj);
    164 	}
    165 
    166 	fb = intel_framebuffer_create(obj, &mode_cmd);
    167 	i915_gem_object_put(obj);
    168 	if (IS_ERR(fb))
    169 		return PTR_ERR(fb);
    170 
    171 	ifbdev->fb = to_intel_framebuffer(fb);
    172 	return 0;
    173 }
    174 
    175 #ifdef __NetBSD__
    176 #  define	__iomem		__i915_vma_iomem
    177 #endif
    178 
    179 static int intelfb_create(struct drm_fb_helper *helper,
    180 			  struct drm_fb_helper_surface_size *sizes)
    181 {
    182 	struct intel_fbdev *ifbdev =
    183 		container_of(helper, struct intel_fbdev, helper);
    184 	struct intel_framebuffer *intel_fb = ifbdev->fb;
    185 	struct drm_device *dev = helper->dev;
    186 	struct drm_i915_private *dev_priv = to_i915(dev);
    187 	struct pci_dev *pdev = dev_priv->drm.pdev;
    188 	struct i915_ggtt *ggtt = &dev_priv->ggtt;
    189 	const struct i915_ggtt_view view = {
    190 		.type = I915_GGTT_VIEW_NORMAL,
    191 	};
    192 	intel_wakeref_t wakeref;
    193 #ifndef __NetBSD__
    194 	struct fb_info *info;
    195 #endif
    196 	struct i915_vma *vma;
    197 	unsigned long flags = 0;
    198 	bool prealloc = false;
    199 	void __iomem *vaddr;
    200 	int ret;
    201 
    202 	if (intel_fb &&
    203 	    (sizes->fb_width > intel_fb->base.width ||
    204 	     sizes->fb_height > intel_fb->base.height)) {
    205 		DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d),"
    206 			      " releasing it\n",
    207 			      intel_fb->base.width, intel_fb->base.height,
    208 			      sizes->fb_width, sizes->fb_height);
    209 		drm_framebuffer_put(&intel_fb->base);
    210 		intel_fb = ifbdev->fb = NULL;
    211 	}
    212 	if (!intel_fb || WARN_ON(!intel_fb_obj(&intel_fb->base))) {
    213 		DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
    214 		ret = intelfb_alloc(helper, sizes);
    215 		if (ret)
    216 			return ret;
    217 		intel_fb = ifbdev->fb;
    218 	} else {
    219 		DRM_DEBUG_KMS("re-using BIOS fb\n");
    220 		prealloc = true;
    221 		sizes->fb_width = intel_fb->base.width;
    222 		sizes->fb_height = intel_fb->base.height;
    223 	}
    224 
    225 	wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
    226 
    227 	/* Pin the GGTT vma for our access via info->screen_base.
    228 	 * This also validates that any existing fb inherited from the
    229 	 * BIOS is suitable for own access.
    230 	 */
    231 	vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base,
    232 					 &view, false, &flags);
    233 	if (IS_ERR(vma)) {
    234 		ret = PTR_ERR(vma);
    235 		goto out_unlock;
    236 	}
    237 
    238 	intel_frontbuffer_flush(to_frontbuffer(ifbdev), ORIGIN_DIRTYFB);
    239 
    240 #ifdef __NetBSD__
    241     {
    242 	static const struct intelfb_attach_args zero_ifa;
    243 	struct intelfb_attach_args ifa = zero_ifa;
    244 
    245 	__USE(ggtt);
    246 	__USE(pdev);
    247 
    248 	vaddr = i915_vma_pin_iomap(vma);
    249 	if (IS_ERR(vaddr)) {
    250 		DRM_ERROR("Failed to remap framebuffer into virtual memory\n");
    251 		ret = PTR_ERR(vaddr);
    252 		goto out_unpin;
    253 	}
    254 
    255         if (vma->obj->stolen && !prealloc)
    256 		memset_io(vaddr, 0, vma->node.size);
    257 
    258 	ifa.ifa_drm_dev = dev;
    259 	ifa.ifa_fb_helper = helper;
    260 	ifa.ifa_fb_sizes = *sizes;
    261 	ifa.ifa_fb_vaddr = vaddr;
    262 
    263 	/*
    264 	 * XXX Should do this asynchronously, since we hold
    265 	 * dev->struct_mutex.
    266 	 */
    267 	helper->fbdev = config_found(dev->dev, &ifa, NULL,
    268 	    CFARGS(.iattr = "intelfbbus"));
    269 	if (helper->fbdev == NULL) {
    270 		DRM_ERROR("unable to attach intelfb\n");
    271 		ret = -ENXIO;
    272 		goto out_unpin;
    273 	}
    274 	ifbdev->helper.fb = &ifbdev->fb->base;
    275     }
    276 #else
    277 	info = drm_fb_helper_alloc_fbi(helper);
    278 	if (IS_ERR(info)) {
    279 		DRM_ERROR("Failed to allocate fb_info\n");
    280 		ret = PTR_ERR(info);
    281 		goto out_unpin;
    282 	}
    283 
    284 	ifbdev->helper.fb = &ifbdev->fb->base;
    285 
    286 	info->fbops = &intelfb_ops;
    287 
    288 	/* setup aperture base/size for vesafb takeover */
    289 	info->apertures->ranges[0].base = ggtt->gmadr.start;
    290 	info->apertures->ranges[0].size = ggtt->mappable_end;
    291 
    292 	/* Our framebuffer is the entirety of fbdev's system memory */
    293 	info->fix.smem_start =
    294 		(unsigned long)(ggtt->gmadr.start + vma->node.start);
    295 	info->fix.smem_len = vma->node.size;
    296 
    297 	vaddr = i915_vma_pin_iomap(vma);
    298 	if (IS_ERR(vaddr)) {
    299 		DRM_ERROR("Failed to remap framebuffer into virtual memory\n");
    300 		ret = PTR_ERR(vaddr);
    301 		goto out_unpin;
    302 	}
    303 	info->screen_base = vaddr;
    304 	info->screen_size = vma->node.size;
    305 
    306 	drm_fb_helper_fill_info(info, &ifbdev->helper, sizes);
    307 
    308 	/* If the object is shmemfs backed, it will have given us zeroed pages.
    309 	 * If the object is stolen however, it will be full of whatever
    310 	 * garbage was left in there.
    311 	 */
    312 	if (vma->obj->stolen && !prealloc)
    313 		memset_io(info->screen_base, 0, info->screen_size);
    314 #endif
    315 
    316 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
    317 
    318 	DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08"PRIx32"\n",
    319 		      ifbdev->fb->base.width, ifbdev->fb->base.height,
    320 		      i915_ggtt_offset(vma));
    321 	ifbdev->vma = vma;
    322 	ifbdev->vma_flags = flags;
    323 
    324 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
    325 #ifndef __NetBSD__
    326 	vga_switcheroo_client_fb_set(pdev, info);
    327 #endif
    328 	return 0;
    329 
    330 out_unpin:
    331 	intel_unpin_fb_vma(vma, flags);
    332 out_unlock:
    333 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
    334 	return ret;
    335 }
    336 
    337 #ifdef __NetBSD__
    338 #  undef	__iomem
    339 #endif
    340 
    341 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
    342 	.fb_probe = intelfb_create,
    343 };
    344 
    345 static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
    346 {
    347 	/* We rely on the object-free to release the VMA pinning for
    348 	 * the info->screen_base mmaping. Leaking the VMA is simpler than
    349 	 * trying to rectify all the possible error paths leading here.
    350 	 */
    351 
    352 	drm_fb_helper_fini(&ifbdev->helper);
    353 
    354 	if (ifbdev->vma)
    355 		intel_unpin_fb_vma(ifbdev->vma, ifbdev->vma_flags);
    356 
    357 	if (ifbdev->fb)
    358 		drm_framebuffer_remove(&ifbdev->fb->base);
    359 
    360 	kfree(ifbdev);
    361 }
    362 
    363 /*
    364  * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
    365  * The core display code will have read out the current plane configuration,
    366  * so we use that to figure out if there's an object for us to use as the
    367  * fb, and if so, we re-use it for the fbdev configuration.
    368  *
    369  * Note we only support a single fb shared across pipes for boot (mostly for
    370  * fbcon), so we just find the biggest and use that.
    371  */
    372 static bool intel_fbdev_init_bios(struct drm_device *dev,
    373 				 struct intel_fbdev *ifbdev)
    374 {
    375 	struct intel_framebuffer *fb = NULL;
    376 	struct drm_crtc *crtc;
    377 	struct intel_crtc *intel_crtc;
    378 	unsigned int max_size = 0;
    379 
    380 	/* Find the largest fb */
    381 	for_each_crtc(dev, crtc) {
    382 		struct drm_i915_gem_object *obj =
    383 			intel_fb_obj(crtc->primary->state->fb);
    384 		intel_crtc = to_intel_crtc(crtc);
    385 
    386 		if (!crtc->state->active || !obj) {
    387 			DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
    388 				      pipe_name(intel_crtc->pipe));
    389 			continue;
    390 		}
    391 
    392 		if (obj->base.size > max_size) {
    393 			DRM_DEBUG_KMS("found possible fb from plane %c\n",
    394 				      pipe_name(intel_crtc->pipe));
    395 			fb = to_intel_framebuffer(crtc->primary->state->fb);
    396 			max_size = obj->base.size;
    397 		}
    398 	}
    399 
    400 	if (!fb) {
    401 		DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
    402 		goto out;
    403 	}
    404 
    405 	/* Now make sure all the pipes will fit into it */
    406 	for_each_crtc(dev, crtc) {
    407 		unsigned int cur_size;
    408 
    409 		intel_crtc = to_intel_crtc(crtc);
    410 
    411 		if (!crtc->state->active) {
    412 			DRM_DEBUG_KMS("pipe %c not active, skipping\n",
    413 				      pipe_name(intel_crtc->pipe));
    414 			continue;
    415 		}
    416 
    417 		DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
    418 			      pipe_name(intel_crtc->pipe));
    419 
    420 		/*
    421 		 * See if the plane fb we found above will fit on this
    422 		 * pipe.  Note we need to use the selected fb's pitch and bpp
    423 		 * rather than the current pipe's, since they differ.
    424 		 */
    425 		cur_size = crtc->state->adjusted_mode.crtc_hdisplay;
    426 		cur_size = cur_size * fb->base.format->cpp[0];
    427 		if (fb->base.pitches[0] < cur_size) {
    428 			DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
    429 				      pipe_name(intel_crtc->pipe),
    430 				      cur_size, fb->base.pitches[0]);
    431 			fb = NULL;
    432 			break;
    433 		}
    434 
    435 		cur_size = crtc->state->adjusted_mode.crtc_vdisplay;
    436 		cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
    437 		cur_size *= fb->base.pitches[0];
    438 		DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
    439 			      pipe_name(intel_crtc->pipe),
    440 			      crtc->state->adjusted_mode.crtc_hdisplay,
    441 			      crtc->state->adjusted_mode.crtc_vdisplay,
    442 			      fb->base.format->cpp[0] * 8,
    443 			      cur_size);
    444 
    445 		if (cur_size > max_size) {
    446 			DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
    447 				      pipe_name(intel_crtc->pipe),
    448 				      cur_size, max_size);
    449 			fb = NULL;
    450 			break;
    451 		}
    452 
    453 		DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
    454 			      pipe_name(intel_crtc->pipe),
    455 			      max_size, cur_size);
    456 	}
    457 
    458 	if (!fb) {
    459 		DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
    460 		goto out;
    461 	}
    462 
    463 	ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
    464 	ifbdev->fb = fb;
    465 
    466 	drm_framebuffer_get(&ifbdev->fb->base);
    467 
    468 	/* Final pass to check if any active pipes don't have fbs */
    469 	for_each_crtc(dev, crtc) {
    470 		intel_crtc = to_intel_crtc(crtc);
    471 
    472 		if (!crtc->state->active)
    473 			continue;
    474 
    475 		WARN(!crtc->primary->state->fb,
    476 		     "re-used BIOS config but lost an fb on crtc %d\n",
    477 		     crtc->base.id);
    478 	}
    479 
    480 
    481 	DRM_DEBUG_KMS("using BIOS fb for initial console\n");
    482 	return true;
    483 
    484 out:
    485 
    486 	return false;
    487 }
    488 
    489 static void intel_fbdev_suspend_worker(struct work_struct *work)
    490 {
    491 #ifndef __NetBSD__		/* XXX fb suspend */
    492 	intel_fbdev_set_suspend(&container_of(work,
    493 					      struct drm_i915_private,
    494 					      fbdev_suspend_work)->drm,
    495 				FBINFO_STATE_RUNNING,
    496 				true);
    497 #endif
    498 }
    499 
    500 int intel_fbdev_init(struct drm_device *dev)
    501 {
    502 	struct drm_i915_private *dev_priv = to_i915(dev);
    503 	struct intel_fbdev *ifbdev;
    504 	int ret;
    505 
    506 	if (WARN_ON(!HAS_DISPLAY(dev_priv) || !INTEL_DISPLAY_ENABLED(dev_priv)))
    507 		return -ENODEV;
    508 
    509 	ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
    510 	if (ifbdev == NULL)
    511 		return -ENOMEM;
    512 
    513 	mutex_init(&ifbdev->hpd_lock);
    514 	drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
    515 
    516 	if (!intel_fbdev_init_bios(dev, ifbdev))
    517 		ifbdev->preferred_bpp = 32;
    518 
    519 	ret = drm_fb_helper_init(dev, &ifbdev->helper, 4);
    520 	if (ret) {
    521 		kfree(ifbdev);
    522 		return ret;
    523 	}
    524 
    525 	dev_priv->fbdev = ifbdev;
    526 	INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
    527 
    528 	drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
    529 
    530 	return 0;
    531 }
    532 
    533 static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
    534 {
    535 	struct intel_fbdev *ifbdev = data;
    536 
    537 	/* Due to peculiar init order wrt to hpd handling this is separate. */
    538 	if (drm_fb_helper_initial_config(&ifbdev->helper,
    539 					 ifbdev->preferred_bpp))
    540 		intel_fbdev_unregister(to_i915(ifbdev->helper.dev));
    541 }
    542 
    543 void intel_fbdev_initial_config_async(struct drm_device *dev)
    544 {
    545 	struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
    546 
    547 	if (!ifbdev)
    548 		return;
    549 
    550 	ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
    551 }
    552 
    553 static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
    554 {
    555 	if (!ifbdev->cookie)
    556 		return;
    557 
    558 	/* Only serialises with all preceding async calls, hence +1 */
    559 	async_synchronize_cookie(ifbdev->cookie + 1);
    560 	ifbdev->cookie = 0;
    561 }
    562 
    563 void intel_fbdev_unregister(struct drm_i915_private *dev_priv)
    564 {
    565 	struct intel_fbdev *ifbdev = dev_priv->fbdev;
    566 
    567 	if (!ifbdev)
    568 		return;
    569 
    570 	cancel_work_sync(&dev_priv->fbdev_suspend_work);
    571 #ifndef __NetBSD__		/* XXX fb async */
    572 	if (!current_is_async())
    573 #endif
    574 		intel_fbdev_sync(ifbdev);
    575 
    576 	drm_fb_helper_unregister_fbi(&ifbdev->helper);
    577 }
    578 
    579 void intel_fbdev_fini(struct drm_i915_private *dev_priv)
    580 {
    581 	struct intel_fbdev *ifbdev = fetch_and_zero(&dev_priv->fbdev);
    582 
    583 	if (!ifbdev)
    584 		return;
    585 
    586 	intel_fbdev_destroy(ifbdev);
    587 }
    588 
    589 /* Suspends/resumes fbdev processing of incoming HPD events. When resuming HPD
    590  * processing, fbdev will perform a full connector reprobe if a hotplug event
    591  * was received while HPD was suspended.
    592  */
    593 #ifndef __NetBSD__		/* XXX fb suspend */
    594 static void intel_fbdev_hpd_set_suspend(struct intel_fbdev *ifbdev, int state)
    595 {
    596 	bool send_hpd = false;
    597 
    598 	mutex_lock(&ifbdev->hpd_lock);
    599 	ifbdev->hpd_suspended = state == FBINFO_STATE_SUSPENDED;
    600 	send_hpd = !ifbdev->hpd_suspended && ifbdev->hpd_waiting;
    601 	ifbdev->hpd_waiting = false;
    602 	mutex_unlock(&ifbdev->hpd_lock);
    603 
    604 	if (send_hpd) {
    605 		DRM_DEBUG_KMS("Handling delayed fbcon HPD event\n");
    606 		drm_fb_helper_hotplug_event(&ifbdev->helper);
    607 	}
    608 }
    609 #endif	/* __NetBSD__ */
    610 
    611 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
    612 {
    613 #ifndef __NetBSD__		/* XXX fb suspend */
    614 	struct drm_i915_private *dev_priv = to_i915(dev);
    615 	struct intel_fbdev *ifbdev = dev_priv->fbdev;
    616 	struct fb_info *info;
    617 
    618 	if (!ifbdev || !ifbdev->vma)
    619 		return;
    620 
    621 	info = ifbdev->helper.fbdev;
    622 
    623 	if (synchronous) {
    624 		/* Flush any pending work to turn the console on, and then
    625 		 * wait to turn it off. It must be synchronous as we are
    626 		 * about to suspend or unload the driver.
    627 		 *
    628 		 * Note that from within the work-handler, we cannot flush
    629 		 * ourselves, so only flush outstanding work upon suspend!
    630 		 */
    631 		if (state != FBINFO_STATE_RUNNING)
    632 			flush_work(&dev_priv->fbdev_suspend_work);
    633 
    634 		console_lock();
    635 	} else {
    636 		/*
    637 		 * The console lock can be pretty contented on resume due
    638 		 * to all the printk activity.  Try to keep it out of the hot
    639 		 * path of resume if possible.
    640 		 */
    641 		WARN_ON(state != FBINFO_STATE_RUNNING);
    642 		if (!console_trylock()) {
    643 			/* Don't block our own workqueue as this can
    644 			 * be run in parallel with other i915.ko tasks.
    645 			 */
    646 			schedule_work(&dev_priv->fbdev_suspend_work);
    647 			return;
    648 		}
    649 	}
    650 
    651 	/* On resume from hibernation: If the object is shmemfs backed, it has
    652 	 * been restored from swap. If the object is stolen however, it will be
    653 	 * full of whatever garbage was left in there.
    654 	 */
    655 	if (state == FBINFO_STATE_RUNNING &&
    656 	    intel_fb_obj(&ifbdev->fb->base)->stolen)
    657 		memset_io(info->screen_base, 0, info->screen_size);
    658 
    659 	drm_fb_helper_set_suspend(&ifbdev->helper, state);
    660 	console_unlock();
    661 
    662 	intel_fbdev_hpd_set_suspend(ifbdev, state);
    663 #endif
    664 }
    665 
    666 void intel_fbdev_output_poll_changed(struct drm_device *dev)
    667 {
    668 	struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
    669 	bool send_hpd;
    670 
    671 	if (!ifbdev)
    672 		return;
    673 
    674 	intel_fbdev_sync(ifbdev);
    675 
    676 	mutex_lock(&ifbdev->hpd_lock);
    677 	send_hpd = !ifbdev->hpd_suspended;
    678 	ifbdev->hpd_waiting = true;
    679 	mutex_unlock(&ifbdev->hpd_lock);
    680 
    681 	if (send_hpd && (ifbdev->vma || ifbdev->helper.deferred_setup))
    682 		drm_fb_helper_hotplug_event(&ifbdev->helper);
    683 }
    684 
    685 void intel_fbdev_restore_mode(struct drm_device *dev)
    686 {
    687 	struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
    688 
    689 	if (!ifbdev)
    690 		return;
    691 
    692 	intel_fbdev_sync(ifbdev);
    693 	if (!ifbdev->vma)
    694 		return;
    695 
    696 	if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper) == 0)
    697 		intel_fbdev_invalidate(ifbdev);
    698 }
    699