Home | History | Annotate | Line # | Download | only in display
intel_fbdev.c revision 1.9
      1 /*	$NetBSD: intel_fbdev.c,v 1.9 2021/12/19 12:32:15 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.9 2021/12/19 12:32:15 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 	mutex_destroy(&ifbdev->hpd_lock);
    361 
    362 	kfree(ifbdev);
    363 }
    364 
    365 /*
    366  * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
    367  * The core display code will have read out the current plane configuration,
    368  * so we use that to figure out if there's an object for us to use as the
    369  * fb, and if so, we re-use it for the fbdev configuration.
    370  *
    371  * Note we only support a single fb shared across pipes for boot (mostly for
    372  * fbcon), so we just find the biggest and use that.
    373  */
    374 static bool intel_fbdev_init_bios(struct drm_device *dev,
    375 				 struct intel_fbdev *ifbdev)
    376 {
    377 	struct intel_framebuffer *fb = NULL;
    378 	struct drm_crtc *crtc;
    379 	struct intel_crtc *intel_crtc;
    380 	unsigned int max_size = 0;
    381 
    382 	/* Find the largest fb */
    383 	for_each_crtc(dev, crtc) {
    384 		struct drm_i915_gem_object *obj =
    385 			intel_fb_obj(crtc->primary->state->fb);
    386 		intel_crtc = to_intel_crtc(crtc);
    387 
    388 		if (!crtc->state->active || !obj) {
    389 			DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
    390 				      pipe_name(intel_crtc->pipe));
    391 			continue;
    392 		}
    393 
    394 		if (obj->base.size > max_size) {
    395 			DRM_DEBUG_KMS("found possible fb from plane %c\n",
    396 				      pipe_name(intel_crtc->pipe));
    397 			fb = to_intel_framebuffer(crtc->primary->state->fb);
    398 			max_size = obj->base.size;
    399 		}
    400 	}
    401 
    402 	if (!fb) {
    403 		DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
    404 		goto out;
    405 	}
    406 
    407 	/* Now make sure all the pipes will fit into it */
    408 	for_each_crtc(dev, crtc) {
    409 		unsigned int cur_size;
    410 
    411 		intel_crtc = to_intel_crtc(crtc);
    412 
    413 		if (!crtc->state->active) {
    414 			DRM_DEBUG_KMS("pipe %c not active, skipping\n",
    415 				      pipe_name(intel_crtc->pipe));
    416 			continue;
    417 		}
    418 
    419 		DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
    420 			      pipe_name(intel_crtc->pipe));
    421 
    422 		/*
    423 		 * See if the plane fb we found above will fit on this
    424 		 * pipe.  Note we need to use the selected fb's pitch and bpp
    425 		 * rather than the current pipe's, since they differ.
    426 		 */
    427 		cur_size = crtc->state->adjusted_mode.crtc_hdisplay;
    428 		cur_size = cur_size * fb->base.format->cpp[0];
    429 		if (fb->base.pitches[0] < cur_size) {
    430 			DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
    431 				      pipe_name(intel_crtc->pipe),
    432 				      cur_size, fb->base.pitches[0]);
    433 			fb = NULL;
    434 			break;
    435 		}
    436 
    437 		cur_size = crtc->state->adjusted_mode.crtc_vdisplay;
    438 		cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
    439 		cur_size *= fb->base.pitches[0];
    440 		DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
    441 			      pipe_name(intel_crtc->pipe),
    442 			      crtc->state->adjusted_mode.crtc_hdisplay,
    443 			      crtc->state->adjusted_mode.crtc_vdisplay,
    444 			      fb->base.format->cpp[0] * 8,
    445 			      cur_size);
    446 
    447 		if (cur_size > max_size) {
    448 			DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
    449 				      pipe_name(intel_crtc->pipe),
    450 				      cur_size, max_size);
    451 			fb = NULL;
    452 			break;
    453 		}
    454 
    455 		DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
    456 			      pipe_name(intel_crtc->pipe),
    457 			      max_size, cur_size);
    458 	}
    459 
    460 	if (!fb) {
    461 		DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
    462 		goto out;
    463 	}
    464 
    465 	ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
    466 	ifbdev->fb = fb;
    467 
    468 	drm_framebuffer_get(&ifbdev->fb->base);
    469 
    470 	/* Final pass to check if any active pipes don't have fbs */
    471 	for_each_crtc(dev, crtc) {
    472 		intel_crtc = to_intel_crtc(crtc);
    473 
    474 		if (!crtc->state->active)
    475 			continue;
    476 
    477 		WARN(!crtc->primary->state->fb,
    478 		     "re-used BIOS config but lost an fb on crtc %d\n",
    479 		     crtc->base.id);
    480 	}
    481 
    482 
    483 	DRM_DEBUG_KMS("using BIOS fb for initial console\n");
    484 	return true;
    485 
    486 out:
    487 
    488 	return false;
    489 }
    490 
    491 static void intel_fbdev_suspend_worker(struct work_struct *work)
    492 {
    493 #ifndef __NetBSD__		/* XXX fb suspend */
    494 	intel_fbdev_set_suspend(&container_of(work,
    495 					      struct drm_i915_private,
    496 					      fbdev_suspend_work)->drm,
    497 				FBINFO_STATE_RUNNING,
    498 				true);
    499 #endif
    500 }
    501 
    502 int intel_fbdev_init(struct drm_device *dev)
    503 {
    504 	struct drm_i915_private *dev_priv = to_i915(dev);
    505 	struct intel_fbdev *ifbdev;
    506 	int ret;
    507 
    508 	if (WARN_ON(!HAS_DISPLAY(dev_priv) || !INTEL_DISPLAY_ENABLED(dev_priv)))
    509 		return -ENODEV;
    510 
    511 	ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
    512 	if (ifbdev == NULL)
    513 		return -ENOMEM;
    514 
    515 	mutex_init(&ifbdev->hpd_lock);
    516 	drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
    517 
    518 	if (!intel_fbdev_init_bios(dev, ifbdev))
    519 		ifbdev->preferred_bpp = 32;
    520 
    521 	ret = drm_fb_helper_init(dev, &ifbdev->helper, 4);
    522 	if (ret) {
    523 		kfree(ifbdev);
    524 		return ret;
    525 	}
    526 
    527 	dev_priv->fbdev = ifbdev;
    528 	INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
    529 
    530 	drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
    531 
    532 	return 0;
    533 }
    534 
    535 static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
    536 {
    537 	struct intel_fbdev *ifbdev = data;
    538 
    539 	/* Due to peculiar init order wrt to hpd handling this is separate. */
    540 	if (drm_fb_helper_initial_config(&ifbdev->helper,
    541 					 ifbdev->preferred_bpp))
    542 		intel_fbdev_unregister(to_i915(ifbdev->helper.dev));
    543 }
    544 
    545 void intel_fbdev_initial_config_async(struct drm_device *dev)
    546 {
    547 	struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
    548 
    549 	if (!ifbdev)
    550 		return;
    551 
    552 	ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
    553 }
    554 
    555 static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
    556 {
    557 	if (!ifbdev->cookie)
    558 		return;
    559 
    560 	/* Only serialises with all preceding async calls, hence +1 */
    561 	async_synchronize_cookie(ifbdev->cookie + 1);
    562 	ifbdev->cookie = 0;
    563 }
    564 
    565 void intel_fbdev_unregister(struct drm_i915_private *dev_priv)
    566 {
    567 	struct intel_fbdev *ifbdev = dev_priv->fbdev;
    568 
    569 	if (!ifbdev)
    570 		return;
    571 
    572 	cancel_work_sync(&dev_priv->fbdev_suspend_work);
    573 #ifndef __NetBSD__		/* XXX fb async */
    574 	if (!current_is_async())
    575 #endif
    576 		intel_fbdev_sync(ifbdev);
    577 
    578 	drm_fb_helper_unregister_fbi(&ifbdev->helper);
    579 }
    580 
    581 void intel_fbdev_fini(struct drm_i915_private *dev_priv)
    582 {
    583 	struct intel_fbdev *ifbdev = fetch_and_zero(&dev_priv->fbdev);
    584 
    585 	if (!ifbdev)
    586 		return;
    587 
    588 	intel_fbdev_destroy(ifbdev);
    589 }
    590 
    591 /* Suspends/resumes fbdev processing of incoming HPD events. When resuming HPD
    592  * processing, fbdev will perform a full connector reprobe if a hotplug event
    593  * was received while HPD was suspended.
    594  */
    595 #ifndef __NetBSD__		/* XXX fb suspend */
    596 static void intel_fbdev_hpd_set_suspend(struct intel_fbdev *ifbdev, int state)
    597 {
    598 	bool send_hpd = false;
    599 
    600 	mutex_lock(&ifbdev->hpd_lock);
    601 	ifbdev->hpd_suspended = state == FBINFO_STATE_SUSPENDED;
    602 	send_hpd = !ifbdev->hpd_suspended && ifbdev->hpd_waiting;
    603 	ifbdev->hpd_waiting = false;
    604 	mutex_unlock(&ifbdev->hpd_lock);
    605 
    606 	if (send_hpd) {
    607 		DRM_DEBUG_KMS("Handling delayed fbcon HPD event\n");
    608 		drm_fb_helper_hotplug_event(&ifbdev->helper);
    609 	}
    610 }
    611 #endif	/* __NetBSD__ */
    612 
    613 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
    614 {
    615 #ifndef __NetBSD__		/* XXX fb suspend */
    616 	struct drm_i915_private *dev_priv = to_i915(dev);
    617 	struct intel_fbdev *ifbdev = dev_priv->fbdev;
    618 	struct fb_info *info;
    619 
    620 	if (!ifbdev || !ifbdev->vma)
    621 		return;
    622 
    623 	info = ifbdev->helper.fbdev;
    624 
    625 	if (synchronous) {
    626 		/* Flush any pending work to turn the console on, and then
    627 		 * wait to turn it off. It must be synchronous as we are
    628 		 * about to suspend or unload the driver.
    629 		 *
    630 		 * Note that from within the work-handler, we cannot flush
    631 		 * ourselves, so only flush outstanding work upon suspend!
    632 		 */
    633 		if (state != FBINFO_STATE_RUNNING)
    634 			flush_work(&dev_priv->fbdev_suspend_work);
    635 
    636 		console_lock();
    637 	} else {
    638 		/*
    639 		 * The console lock can be pretty contented on resume due
    640 		 * to all the printk activity.  Try to keep it out of the hot
    641 		 * path of resume if possible.
    642 		 */
    643 		WARN_ON(state != FBINFO_STATE_RUNNING);
    644 		if (!console_trylock()) {
    645 			/* Don't block our own workqueue as this can
    646 			 * be run in parallel with other i915.ko tasks.
    647 			 */
    648 			schedule_work(&dev_priv->fbdev_suspend_work);
    649 			return;
    650 		}
    651 	}
    652 
    653 	/* On resume from hibernation: If the object is shmemfs backed, it has
    654 	 * been restored from swap. If the object is stolen however, it will be
    655 	 * full of whatever garbage was left in there.
    656 	 */
    657 	if (state == FBINFO_STATE_RUNNING &&
    658 	    intel_fb_obj(&ifbdev->fb->base)->stolen)
    659 		memset_io(info->screen_base, 0, info->screen_size);
    660 
    661 	drm_fb_helper_set_suspend(&ifbdev->helper, state);
    662 	console_unlock();
    663 
    664 	intel_fbdev_hpd_set_suspend(ifbdev, state);
    665 #endif
    666 }
    667 
    668 void intel_fbdev_output_poll_changed(struct drm_device *dev)
    669 {
    670 	struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
    671 	bool send_hpd;
    672 
    673 	if (!ifbdev)
    674 		return;
    675 
    676 	intel_fbdev_sync(ifbdev);
    677 
    678 	mutex_lock(&ifbdev->hpd_lock);
    679 	send_hpd = !ifbdev->hpd_suspended;
    680 	ifbdev->hpd_waiting = true;
    681 	mutex_unlock(&ifbdev->hpd_lock);
    682 
    683 	if (send_hpd && (ifbdev->vma || ifbdev->helper.deferred_setup))
    684 		drm_fb_helper_hotplug_event(&ifbdev->helper);
    685 }
    686 
    687 void intel_fbdev_restore_mode(struct drm_device *dev)
    688 {
    689 	struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
    690 
    691 	if (!ifbdev)
    692 		return;
    693 
    694 	intel_fbdev_sync(ifbdev);
    695 	if (!ifbdev->vma)
    696 		return;
    697 
    698 	if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper) == 0)
    699 		intel_fbdev_invalidate(ifbdev);
    700 }
    701