xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/i915/display/intel_fbdev.c (revision 0f6253180151af85efabd9c64633c2365e1e18b9)
1 /*	$NetBSD: intel_fbdev.c,v 1.10 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: intel_fbdev.c,v 1.10 2021/12/20 20:34:58 chs 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 
to_frontbuffer(struct intel_fbdev * ifbdev)55 static struct intel_frontbuffer *to_frontbuffer(struct intel_fbdev *ifbdev)
56 {
57 	return ifbdev->fb->frontbuffer;
58 }
59 
intel_fbdev_invalidate(struct intel_fbdev * ifbdev)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__
intel_fbdev_set_par(struct fb_info * info)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 
intel_fbdev_blank(int blank,struct fb_info * info)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 
intel_fbdev_pan_display(struct fb_var_screeninfo * var,struct fb_info * info)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 
intelfb_alloc(struct drm_fb_helper * helper,struct drm_fb_helper_surface_size * sizes)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 
intelfb_create(struct drm_fb_helper * helper,struct drm_fb_helper_surface_size * sizes)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 	KERNEL_LOCK(1, NULL);
268 	helper->fbdev = config_found(dev->dev, &ifa, NULL,
269 	    CFARGS(.iattr = "intelfbbus"));
270 	KERNEL_UNLOCK_ONE(NULL);
271 	if (helper->fbdev == NULL) {
272 		DRM_ERROR("unable to attach intelfb\n");
273 		ret = -ENXIO;
274 		goto out_unpin;
275 	}
276 	ifbdev->helper.fb = &ifbdev->fb->base;
277     }
278 #else
279 	info = drm_fb_helper_alloc_fbi(helper);
280 	if (IS_ERR(info)) {
281 		DRM_ERROR("Failed to allocate fb_info\n");
282 		ret = PTR_ERR(info);
283 		goto out_unpin;
284 	}
285 
286 	ifbdev->helper.fb = &ifbdev->fb->base;
287 
288 	info->fbops = &intelfb_ops;
289 
290 	/* setup aperture base/size for vesafb takeover */
291 	info->apertures->ranges[0].base = ggtt->gmadr.start;
292 	info->apertures->ranges[0].size = ggtt->mappable_end;
293 
294 	/* Our framebuffer is the entirety of fbdev's system memory */
295 	info->fix.smem_start =
296 		(unsigned long)(ggtt->gmadr.start + vma->node.start);
297 	info->fix.smem_len = vma->node.size;
298 
299 	vaddr = i915_vma_pin_iomap(vma);
300 	if (IS_ERR(vaddr)) {
301 		DRM_ERROR("Failed to remap framebuffer into virtual memory\n");
302 		ret = PTR_ERR(vaddr);
303 		goto out_unpin;
304 	}
305 	info->screen_base = vaddr;
306 	info->screen_size = vma->node.size;
307 
308 	drm_fb_helper_fill_info(info, &ifbdev->helper, sizes);
309 
310 	/* If the object is shmemfs backed, it will have given us zeroed pages.
311 	 * If the object is stolen however, it will be full of whatever
312 	 * garbage was left in there.
313 	 */
314 	if (vma->obj->stolen && !prealloc)
315 		memset_io(info->screen_base, 0, info->screen_size);
316 #endif
317 
318 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
319 
320 	DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08"PRIx32"\n",
321 		      ifbdev->fb->base.width, ifbdev->fb->base.height,
322 		      i915_ggtt_offset(vma));
323 	ifbdev->vma = vma;
324 	ifbdev->vma_flags = flags;
325 
326 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
327 #ifndef __NetBSD__
328 	vga_switcheroo_client_fb_set(pdev, info);
329 #endif
330 	return 0;
331 
332 out_unpin:
333 	intel_unpin_fb_vma(vma, flags);
334 out_unlock:
335 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
336 	return ret;
337 }
338 
339 #ifdef __NetBSD__
340 #  undef	__iomem
341 #endif
342 
343 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
344 	.fb_probe = intelfb_create,
345 };
346 
intel_fbdev_destroy(struct intel_fbdev * ifbdev)347 static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
348 {
349 	/* We rely on the object-free to release the VMA pinning for
350 	 * the info->screen_base mmaping. Leaking the VMA is simpler than
351 	 * trying to rectify all the possible error paths leading here.
352 	 */
353 
354 	drm_fb_helper_fini(&ifbdev->helper);
355 
356 	if (ifbdev->vma)
357 		intel_unpin_fb_vma(ifbdev->vma, ifbdev->vma_flags);
358 
359 	if (ifbdev->fb)
360 		drm_framebuffer_remove(&ifbdev->fb->base);
361 
362 	mutex_destroy(&ifbdev->hpd_lock);
363 
364 	kfree(ifbdev);
365 }
366 
367 /*
368  * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
369  * The core display code will have read out the current plane configuration,
370  * so we use that to figure out if there's an object for us to use as the
371  * fb, and if so, we re-use it for the fbdev configuration.
372  *
373  * Note we only support a single fb shared across pipes for boot (mostly for
374  * fbcon), so we just find the biggest and use that.
375  */
intel_fbdev_init_bios(struct drm_device * dev,struct intel_fbdev * ifbdev)376 static bool intel_fbdev_init_bios(struct drm_device *dev,
377 				 struct intel_fbdev *ifbdev)
378 {
379 	struct intel_framebuffer *fb = NULL;
380 	struct drm_crtc *crtc;
381 	struct intel_crtc *intel_crtc;
382 	unsigned int max_size = 0;
383 
384 	/* Find the largest fb */
385 	for_each_crtc(dev, crtc) {
386 		struct drm_i915_gem_object *obj =
387 			intel_fb_obj(crtc->primary->state->fb);
388 		intel_crtc = to_intel_crtc(crtc);
389 
390 		if (!crtc->state->active || !obj) {
391 			DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
392 				      pipe_name(intel_crtc->pipe));
393 			continue;
394 		}
395 
396 		if (obj->base.size > max_size) {
397 			DRM_DEBUG_KMS("found possible fb from plane %c\n",
398 				      pipe_name(intel_crtc->pipe));
399 			fb = to_intel_framebuffer(crtc->primary->state->fb);
400 			max_size = obj->base.size;
401 		}
402 	}
403 
404 	if (!fb) {
405 		DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
406 		goto out;
407 	}
408 
409 	/* Now make sure all the pipes will fit into it */
410 	for_each_crtc(dev, crtc) {
411 		unsigned int cur_size;
412 
413 		intel_crtc = to_intel_crtc(crtc);
414 
415 		if (!crtc->state->active) {
416 			DRM_DEBUG_KMS("pipe %c not active, skipping\n",
417 				      pipe_name(intel_crtc->pipe));
418 			continue;
419 		}
420 
421 		DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
422 			      pipe_name(intel_crtc->pipe));
423 
424 		/*
425 		 * See if the plane fb we found above will fit on this
426 		 * pipe.  Note we need to use the selected fb's pitch and bpp
427 		 * rather than the current pipe's, since they differ.
428 		 */
429 		cur_size = crtc->state->adjusted_mode.crtc_hdisplay;
430 		cur_size = cur_size * fb->base.format->cpp[0];
431 		if (fb->base.pitches[0] < cur_size) {
432 			DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
433 				      pipe_name(intel_crtc->pipe),
434 				      cur_size, fb->base.pitches[0]);
435 			fb = NULL;
436 			break;
437 		}
438 
439 		cur_size = crtc->state->adjusted_mode.crtc_vdisplay;
440 		cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
441 		cur_size *= fb->base.pitches[0];
442 		DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
443 			      pipe_name(intel_crtc->pipe),
444 			      crtc->state->adjusted_mode.crtc_hdisplay,
445 			      crtc->state->adjusted_mode.crtc_vdisplay,
446 			      fb->base.format->cpp[0] * 8,
447 			      cur_size);
448 
449 		if (cur_size > max_size) {
450 			DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
451 				      pipe_name(intel_crtc->pipe),
452 				      cur_size, max_size);
453 			fb = NULL;
454 			break;
455 		}
456 
457 		DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
458 			      pipe_name(intel_crtc->pipe),
459 			      max_size, cur_size);
460 	}
461 
462 	if (!fb) {
463 		DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
464 		goto out;
465 	}
466 
467 	ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
468 	ifbdev->fb = fb;
469 
470 	drm_framebuffer_get(&ifbdev->fb->base);
471 
472 	/* Final pass to check if any active pipes don't have fbs */
473 	for_each_crtc(dev, crtc) {
474 		intel_crtc = to_intel_crtc(crtc);
475 
476 		if (!crtc->state->active)
477 			continue;
478 
479 		WARN(!crtc->primary->state->fb,
480 		     "re-used BIOS config but lost an fb on crtc %d\n",
481 		     crtc->base.id);
482 	}
483 
484 
485 	DRM_DEBUG_KMS("using BIOS fb for initial console\n");
486 	return true;
487 
488 out:
489 
490 	return false;
491 }
492 
intel_fbdev_suspend_worker(struct work_struct * work)493 static void intel_fbdev_suspend_worker(struct work_struct *work)
494 {
495 #ifndef __NetBSD__		/* XXX fb suspend */
496 	intel_fbdev_set_suspend(&container_of(work,
497 					      struct drm_i915_private,
498 					      fbdev_suspend_work)->drm,
499 				FBINFO_STATE_RUNNING,
500 				true);
501 #endif
502 }
503 
intel_fbdev_init(struct drm_device * dev)504 int intel_fbdev_init(struct drm_device *dev)
505 {
506 	struct drm_i915_private *dev_priv = to_i915(dev);
507 	struct intel_fbdev *ifbdev;
508 	int ret;
509 
510 	if (WARN_ON(!HAS_DISPLAY(dev_priv) || !INTEL_DISPLAY_ENABLED(dev_priv)))
511 		return -ENODEV;
512 
513 	ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
514 	if (ifbdev == NULL)
515 		return -ENOMEM;
516 
517 	mutex_init(&ifbdev->hpd_lock);
518 	drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
519 
520 	if (!intel_fbdev_init_bios(dev, ifbdev))
521 		ifbdev->preferred_bpp = 32;
522 
523 	ret = drm_fb_helper_init(dev, &ifbdev->helper, 4);
524 	if (ret) {
525 		kfree(ifbdev);
526 		return ret;
527 	}
528 
529 	dev_priv->fbdev = ifbdev;
530 	INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
531 
532 	drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
533 
534 	return 0;
535 }
536 
intel_fbdev_initial_config(void * data,async_cookie_t cookie)537 static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
538 {
539 	struct intel_fbdev *ifbdev = data;
540 
541 	/* Due to peculiar init order wrt to hpd handling this is separate. */
542 	if (drm_fb_helper_initial_config(&ifbdev->helper,
543 					 ifbdev->preferred_bpp))
544 		intel_fbdev_unregister(to_i915(ifbdev->helper.dev));
545 }
546 
intel_fbdev_initial_config_async(struct drm_device * dev)547 void intel_fbdev_initial_config_async(struct drm_device *dev)
548 {
549 	struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
550 
551 	if (!ifbdev)
552 		return;
553 
554 	ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
555 }
556 
intel_fbdev_sync(struct intel_fbdev * ifbdev)557 static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
558 {
559 	if (!ifbdev->cookie)
560 		return;
561 
562 	/* Only serialises with all preceding async calls, hence +1 */
563 	async_synchronize_cookie(ifbdev->cookie + 1);
564 	ifbdev->cookie = 0;
565 }
566 
intel_fbdev_unregister(struct drm_i915_private * dev_priv)567 void intel_fbdev_unregister(struct drm_i915_private *dev_priv)
568 {
569 	struct intel_fbdev *ifbdev = dev_priv->fbdev;
570 
571 	if (!ifbdev)
572 		return;
573 
574 	cancel_work_sync(&dev_priv->fbdev_suspend_work);
575 #ifndef __NetBSD__		/* XXX fb async */
576 	if (!current_is_async())
577 #endif
578 		intel_fbdev_sync(ifbdev);
579 
580 	drm_fb_helper_unregister_fbi(&ifbdev->helper);
581 }
582 
intel_fbdev_fini(struct drm_i915_private * dev_priv)583 void intel_fbdev_fini(struct drm_i915_private *dev_priv)
584 {
585 	struct intel_fbdev *ifbdev = fetch_and_zero(&dev_priv->fbdev);
586 
587 	if (!ifbdev)
588 		return;
589 
590 	intel_fbdev_destroy(ifbdev);
591 }
592 
593 /* Suspends/resumes fbdev processing of incoming HPD events. When resuming HPD
594  * processing, fbdev will perform a full connector reprobe if a hotplug event
595  * was received while HPD was suspended.
596  */
597 #ifndef __NetBSD__		/* XXX fb suspend */
intel_fbdev_hpd_set_suspend(struct intel_fbdev * ifbdev,int state)598 static void intel_fbdev_hpd_set_suspend(struct intel_fbdev *ifbdev, int state)
599 {
600 	bool send_hpd = false;
601 
602 	mutex_lock(&ifbdev->hpd_lock);
603 	ifbdev->hpd_suspended = state == FBINFO_STATE_SUSPENDED;
604 	send_hpd = !ifbdev->hpd_suspended && ifbdev->hpd_waiting;
605 	ifbdev->hpd_waiting = false;
606 	mutex_unlock(&ifbdev->hpd_lock);
607 
608 	if (send_hpd) {
609 		DRM_DEBUG_KMS("Handling delayed fbcon HPD event\n");
610 		drm_fb_helper_hotplug_event(&ifbdev->helper);
611 	}
612 }
613 #endif	/* __NetBSD__ */
614 
intel_fbdev_set_suspend(struct drm_device * dev,int state,bool synchronous)615 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
616 {
617 #ifndef __NetBSD__		/* XXX fb suspend */
618 	struct drm_i915_private *dev_priv = to_i915(dev);
619 	struct intel_fbdev *ifbdev = dev_priv->fbdev;
620 	struct fb_info *info;
621 
622 	if (!ifbdev || !ifbdev->vma)
623 		return;
624 
625 	info = ifbdev->helper.fbdev;
626 
627 	if (synchronous) {
628 		/* Flush any pending work to turn the console on, and then
629 		 * wait to turn it off. It must be synchronous as we are
630 		 * about to suspend or unload the driver.
631 		 *
632 		 * Note that from within the work-handler, we cannot flush
633 		 * ourselves, so only flush outstanding work upon suspend!
634 		 */
635 		if (state != FBINFO_STATE_RUNNING)
636 			flush_work(&dev_priv->fbdev_suspend_work);
637 
638 		console_lock();
639 	} else {
640 		/*
641 		 * The console lock can be pretty contented on resume due
642 		 * to all the printk activity.  Try to keep it out of the hot
643 		 * path of resume if possible.
644 		 */
645 		WARN_ON(state != FBINFO_STATE_RUNNING);
646 		if (!console_trylock()) {
647 			/* Don't block our own workqueue as this can
648 			 * be run in parallel with other i915.ko tasks.
649 			 */
650 			schedule_work(&dev_priv->fbdev_suspend_work);
651 			return;
652 		}
653 	}
654 
655 	/* On resume from hibernation: If the object is shmemfs backed, it has
656 	 * been restored from swap. If the object is stolen however, it will be
657 	 * full of whatever garbage was left in there.
658 	 */
659 	if (state == FBINFO_STATE_RUNNING &&
660 	    intel_fb_obj(&ifbdev->fb->base)->stolen)
661 		memset_io(info->screen_base, 0, info->screen_size);
662 
663 	drm_fb_helper_set_suspend(&ifbdev->helper, state);
664 	console_unlock();
665 
666 	intel_fbdev_hpd_set_suspend(ifbdev, state);
667 #endif
668 }
669 
intel_fbdev_output_poll_changed(struct drm_device * dev)670 void intel_fbdev_output_poll_changed(struct drm_device *dev)
671 {
672 	struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
673 	bool send_hpd;
674 
675 	if (!ifbdev)
676 		return;
677 
678 	intel_fbdev_sync(ifbdev);
679 
680 	mutex_lock(&ifbdev->hpd_lock);
681 	send_hpd = !ifbdev->hpd_suspended;
682 	ifbdev->hpd_waiting = true;
683 	mutex_unlock(&ifbdev->hpd_lock);
684 
685 	if (send_hpd && (ifbdev->vma || ifbdev->helper.deferred_setup))
686 		drm_fb_helper_hotplug_event(&ifbdev->helper);
687 }
688 
intel_fbdev_restore_mode(struct drm_device * dev)689 void intel_fbdev_restore_mode(struct drm_device *dev)
690 {
691 	struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
692 
693 	if (!ifbdev)
694 		return;
695 
696 	intel_fbdev_sync(ifbdev);
697 	if (!ifbdev->vma)
698 		return;
699 
700 	if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper) == 0)
701 		intel_fbdev_invalidate(ifbdev);
702 }
703