xref: /openbsd/sys/dev/pci/drm/i915/display/intel_fbdev.c (revision 4250d1c2)
1 /*
2  * Copyright © 2007 David Airlie
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *     David Airlie
25  */
26 
27 #include <linux/async.h>
28 #include <linux/console.h>
29 #include <linux/delay.h>
30 #include <linux/errno.h>
31 #include <linux/fb.h>
32 #include <linux/init.h>
33 #include <linux/kernel.h>
34 #include <linux/mm.h>
35 #include <linux/module.h>
36 #include <linux/string.h>
37 #include <linux/sysrq.h>
38 #include <linux/tty.h>
39 #include <linux/vga_switcheroo.h>
40 
41 #include <drm/drm_crtc.h>
42 #include <drm/drm_fb_helper.h>
43 #include <drm/drm_fourcc.h>
44 #include <drm/drm_gem_framebuffer_helper.h>
45 
46 #include "gem/i915_gem_lmem.h"
47 #include "gem/i915_gem_mman.h"
48 
49 #include "i915_drv.h"
50 #include "intel_display_types.h"
51 #include "intel_fb.h"
52 #include "intel_fb_pin.h"
53 #include "intel_fbdev.h"
54 #include "intel_frontbuffer.h"
55 
56 struct intel_fbdev {
57 	struct drm_fb_helper helper;
58 	struct intel_framebuffer *fb;
59 	struct i915_vma *vma;
60 	unsigned long vma_flags;
61 	async_cookie_t cookie;
62 	int preferred_bpp;
63 
64 	/* Whether or not fbdev hpd processing is temporarily suspended */
65 	bool hpd_suspended: 1;
66 	/* Set when a hotplug was received while HPD processing was suspended */
67 	bool hpd_waiting: 1;
68 
69 	/* Protects hpd_suspended */
70 	struct rwlock hpd_lock;
71 };
72 
to_intel_fbdev(struct drm_fb_helper * fb_helper)73 static struct intel_fbdev *to_intel_fbdev(struct drm_fb_helper *fb_helper)
74 {
75 	return container_of(fb_helper, struct intel_fbdev, helper);
76 }
77 
to_frontbuffer(struct intel_fbdev * ifbdev)78 static struct intel_frontbuffer *to_frontbuffer(struct intel_fbdev *ifbdev)
79 {
80 	return ifbdev->fb->frontbuffer;
81 }
82 
intel_fbdev_invalidate(struct intel_fbdev * ifbdev)83 static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev)
84 {
85 	intel_frontbuffer_invalidate(to_frontbuffer(ifbdev), ORIGIN_CPU);
86 }
87 
FB_GEN_DEFAULT_DEFERRED_IOMEM_OPS(intel_fbdev,drm_fb_helper_damage_range,drm_fb_helper_damage_area)88 FB_GEN_DEFAULT_DEFERRED_IOMEM_OPS(intel_fbdev,
89 				  drm_fb_helper_damage_range,
90 				  drm_fb_helper_damage_area)
91 
92 static int intel_fbdev_set_par(struct fb_info *info)
93 {
94 	struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
95 	int ret;
96 
97 	ret = drm_fb_helper_set_par(info);
98 	if (ret == 0)
99 		intel_fbdev_invalidate(ifbdev);
100 
101 	return ret;
102 }
103 
intel_fbdev_blank(int blank,struct fb_info * info)104 static int intel_fbdev_blank(int blank, struct fb_info *info)
105 {
106 	struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
107 	int ret;
108 
109 	ret = drm_fb_helper_blank(blank, info);
110 	if (ret == 0)
111 		intel_fbdev_invalidate(ifbdev);
112 
113 	return ret;
114 }
115 
intel_fbdev_pan_display(struct fb_var_screeninfo * var,struct fb_info * info)116 static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
117 				   struct fb_info *info)
118 {
119 	struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
120 	int ret;
121 
122 	ret = drm_fb_helper_pan_display(var, info);
123 	if (ret == 0)
124 		intel_fbdev_invalidate(ifbdev);
125 
126 	return ret;
127 }
128 
129 #ifdef notyet
intel_fbdev_mmap(struct fb_info * info,struct vm_area_struct * vma)130 static int intel_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma)
131 {
132 	struct intel_fbdev *fbdev = to_intel_fbdev(info->par);
133 	struct drm_gem_object *bo = drm_gem_fb_get_obj(&fbdev->fb->base, 0);
134 	struct drm_i915_gem_object *obj = to_intel_bo(bo);
135 
136 	return i915_gem_fb_mmap(obj, vma);
137 }
138 #endif
139 
140 static const struct fb_ops intelfb_ops = {
141 #ifdef notyet
142 	.owner = THIS_MODULE,
143 	__FB_DEFAULT_DEFERRED_OPS_RDWR(intel_fbdev),
144 	DRM_FB_HELPER_DEFAULT_OPS,
145 #endif
146 	.fb_set_par = intel_fbdev_set_par,
147 #ifdef notyet
148 	.fb_blank = intel_fbdev_blank,
149 	.fb_pan_display = intel_fbdev_pan_display,
150 	__FB_DEFAULT_DEFERRED_OPS_DRAW(intel_fbdev),
151 	.fb_mmap = intel_fbdev_mmap,
152 #endif
153 };
154 
intelfb_alloc(struct drm_fb_helper * helper,struct drm_fb_helper_surface_size * sizes)155 static int intelfb_alloc(struct drm_fb_helper *helper,
156 			 struct drm_fb_helper_surface_size *sizes)
157 {
158 	struct intel_fbdev *ifbdev = to_intel_fbdev(helper);
159 	struct drm_framebuffer *fb;
160 	struct drm_device *dev = helper->dev;
161 	struct drm_i915_private *dev_priv = to_i915(dev);
162 	struct drm_mode_fb_cmd2 mode_cmd = {};
163 	struct drm_i915_gem_object *obj;
164 	int size;
165 
166 	/* we don't do packed 24bpp */
167 	if (sizes->surface_bpp == 24)
168 		sizes->surface_bpp = 32;
169 
170 	mode_cmd.width = sizes->surface_width;
171 	mode_cmd.height = sizes->surface_height;
172 
173 	mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
174 				    DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
175 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
176 							  sizes->surface_depth);
177 
178 	size = mode_cmd.pitches[0] * mode_cmd.height;
179 	size = PAGE_ALIGN(size);
180 
181 	obj = ERR_PTR(-ENODEV);
182 	if (HAS_LMEM(dev_priv)) {
183 		obj = i915_gem_object_create_lmem(dev_priv, size,
184 						  I915_BO_ALLOC_CONTIGUOUS |
185 						  I915_BO_ALLOC_USER);
186 	} else {
187 		/*
188 		 * If the FB is too big, just don't use it since fbdev is not very
189 		 * important and we should probably use that space with FBC or other
190 		 * features.
191 		 *
192 		 * Also skip stolen on MTL as Wa_22018444074 mitigation.
193 		 */
194 		if (!(IS_METEORLAKE(dev_priv)) && size * 2 < dev_priv->dsm.usable_size)
195 			obj = i915_gem_object_create_stolen(dev_priv, size);
196 		if (IS_ERR(obj))
197 			obj = i915_gem_object_create_shmem(dev_priv, size);
198 	}
199 
200 	if (IS_ERR(obj)) {
201 		drm_err(&dev_priv->drm, "failed to allocate framebuffer (%pe)\n", obj);
202 		return PTR_ERR(obj);
203 	}
204 
205 	fb = intel_framebuffer_create(obj, &mode_cmd);
206 	i915_gem_object_put(obj);
207 	if (IS_ERR(fb))
208 		return PTR_ERR(fb);
209 
210 	ifbdev->fb = to_intel_framebuffer(fb);
211 	return 0;
212 }
213 
intelfb_create(struct drm_fb_helper * helper,struct drm_fb_helper_surface_size * sizes)214 static int intelfb_create(struct drm_fb_helper *helper,
215 			  struct drm_fb_helper_surface_size *sizes)
216 {
217 	struct intel_fbdev *ifbdev = to_intel_fbdev(helper);
218 	struct intel_framebuffer *intel_fb = ifbdev->fb;
219 	struct drm_device *dev = helper->dev;
220 	struct drm_i915_private *dev_priv = to_i915(dev);
221 	struct pci_dev *pdev = dev_priv->drm.pdev;
222 	struct i915_ggtt *ggtt = to_gt(dev_priv)->ggtt;
223 	const struct i915_gtt_view view = {
224 		.type = I915_GTT_VIEW_NORMAL,
225 	};
226 	intel_wakeref_t wakeref;
227 	struct fb_info *info;
228 	struct i915_vma *vma;
229 	unsigned long flags = 0;
230 	bool prealloc = false;
231 	void __iomem *vaddr;
232 	struct drm_i915_gem_object *obj;
233 	struct i915_gem_ww_ctx ww;
234 	int ret;
235 
236 	mutex_lock(&ifbdev->hpd_lock);
237 	ret = ifbdev->hpd_suspended ? -EAGAIN : 0;
238 	mutex_unlock(&ifbdev->hpd_lock);
239 	if (ret)
240 		return ret;
241 
242 	if (intel_fb &&
243 	    (sizes->fb_width > intel_fb->base.width ||
244 	     sizes->fb_height > intel_fb->base.height)) {
245 		drm_dbg_kms(&dev_priv->drm,
246 			    "BIOS fb too small (%dx%d), we require (%dx%d),"
247 			    " releasing it\n",
248 			    intel_fb->base.width, intel_fb->base.height,
249 			    sizes->fb_width, sizes->fb_height);
250 		drm_framebuffer_put(&intel_fb->base);
251 		intel_fb = ifbdev->fb = NULL;
252 	}
253 	if (!intel_fb || drm_WARN_ON(dev, !intel_fb_obj(&intel_fb->base))) {
254 		drm_dbg_kms(&dev_priv->drm,
255 			    "no BIOS fb, allocating a new one\n");
256 		ret = intelfb_alloc(helper, sizes);
257 		if (ret)
258 			return ret;
259 		intel_fb = ifbdev->fb;
260 	} else {
261 		drm_dbg_kms(&dev_priv->drm, "re-using BIOS fb\n");
262 		prealloc = true;
263 		sizes->fb_width = intel_fb->base.width;
264 		sizes->fb_height = intel_fb->base.height;
265 	}
266 
267 	wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
268 
269 	/* Pin the GGTT vma for our access via info->screen_base.
270 	 * This also validates that any existing fb inherited from the
271 	 * BIOS is suitable for own access.
272 	 */
273 	vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base, false,
274 					 &view, false, &flags);
275 	if (IS_ERR(vma)) {
276 		ret = PTR_ERR(vma);
277 		goto out_unlock;
278 	}
279 
280 	info = drm_fb_helper_alloc_info(helper);
281 	if (IS_ERR(info)) {
282 		drm_err(&dev_priv->drm, "Failed to allocate fb_info (%pe)\n", info);
283 		ret = PTR_ERR(info);
284 		goto out_unpin;
285 	}
286 
287 	ifbdev->helper.fb = &ifbdev->fb->base;
288 
289 	info->fbops = &intelfb_ops;
290 
291 	obj = intel_fb_obj(&intel_fb->base);
292 	if (i915_gem_object_is_lmem(obj)) {
293 		struct intel_memory_region *mem = obj->mm.region;
294 
295 		/* Use fbdev's framebuffer from lmem for discrete */
296 		info->fix.smem_start =
297 			(unsigned long)(mem->io.start +
298 					i915_gem_object_get_dma_address(obj, 0) -
299 					mem->region.start);
300 		info->fix.smem_len = obj->base.size;
301 	} else {
302 		/* Our framebuffer is the entirety of fbdev's system memory */
303 		info->fix.smem_start =
304 			(unsigned long)(ggtt->gmadr.start + i915_ggtt_offset(vma));
305 		info->fix.smem_len = vma->size;
306 	}
307 
308 	for_i915_gem_ww(&ww, ret, false) {
309 		ret = i915_gem_object_lock(vma->obj, &ww);
310 
311 		if (ret)
312 			continue;
313 
314 		vaddr = i915_vma_pin_iomap(vma);
315 		if (IS_ERR(vaddr)) {
316 			drm_err(&dev_priv->drm,
317 				"Failed to remap framebuffer into virtual memory (%pe)\n", vaddr);
318 			ret = PTR_ERR(vaddr);
319 			continue;
320 		}
321 	}
322 
323 	if (ret)
324 		goto out_unpin;
325 
326 	info->screen_base = vaddr;
327 	info->screen_size = vma->size;
328 
329 	drm_fb_helper_fill_info(info, &ifbdev->helper, sizes);
330 
331 	/* If the object is shmemfs backed, it will have given us zeroed pages.
332 	 * If the object is stolen however, it will be full of whatever
333 	 * garbage was left in there.
334 	 */
335 	if (!i915_gem_object_is_shmem(vma->obj) && !prealloc)
336 		memset_io(info->screen_base, 0, info->screen_size);
337 
338 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
339 
340 	drm_dbg_kms(&dev_priv->drm, "allocated %dx%d fb: 0x%08x\n",
341 		    ifbdev->fb->base.width, ifbdev->fb->base.height,
342 		    i915_ggtt_offset(vma));
343 	ifbdev->vma = vma;
344 	ifbdev->vma_flags = flags;
345 
346 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
347 	vga_switcheroo_client_fb_set(pdev, info);
348 {
349 	struct drm_framebuffer *fb = ifbdev->helper.fb;
350 	struct rasops_info *ri = &dev_priv->ro;
351 
352 	ri->ri_bits = vaddr;
353 	ri->ri_depth = fb->format->cpp[0] * 8;
354 	ri->ri_stride = fb->pitches[0];
355 	ri->ri_width = sizes->fb_width;
356 	ri->ri_height = sizes->fb_height;
357 
358 	switch (fb->format->format) {
359 	case DRM_FORMAT_XRGB8888:
360 		ri->ri_rnum = 8;
361 		ri->ri_rpos = 16;
362 		ri->ri_gnum = 8;
363 		ri->ri_gpos = 8;
364 		ri->ri_bnum = 8;
365 		ri->ri_bpos = 0;
366 		break;
367 	case DRM_FORMAT_RGB565:
368 		ri->ri_rnum = 5;
369 		ri->ri_rpos = 11;
370 		ri->ri_gnum = 6;
371 		ri->ri_gpos = 5;
372 		ri->ri_bnum = 5;
373 		ri->ri_bpos = 0;
374 		break;
375 	}
376 }
377 	return 0;
378 
379 out_unpin:
380 	intel_unpin_fb_vma(vma, flags);
381 out_unlock:
382 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
383 	return ret;
384 }
385 
intelfb_dirty(struct drm_fb_helper * helper,struct drm_clip_rect * clip)386 static int intelfb_dirty(struct drm_fb_helper *helper, struct drm_clip_rect *clip)
387 {
388 	if (!(clip->x1 < clip->x2 && clip->y1 < clip->y2))
389 		return 0;
390 
391 	if (helper->fb->funcs->dirty)
392 		return helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, clip, 1);
393 
394 	return 0;
395 }
396 
397 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
398 	.fb_probe = intelfb_create,
399 	.fb_dirty = intelfb_dirty,
400 };
401 
intel_fbdev_destroy(struct intel_fbdev * ifbdev)402 static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
403 {
404 	/* We rely on the object-free to release the VMA pinning for
405 	 * the info->screen_base mmaping. Leaking the VMA is simpler than
406 	 * trying to rectify all the possible error paths leading here.
407 	 */
408 
409 	drm_fb_helper_fini(&ifbdev->helper);
410 
411 	if (ifbdev->vma)
412 		intel_unpin_fb_vma(ifbdev->vma, ifbdev->vma_flags);
413 
414 	if (ifbdev->fb)
415 		drm_framebuffer_remove(&ifbdev->fb->base);
416 
417 	drm_fb_helper_unprepare(&ifbdev->helper);
418 	kfree(ifbdev);
419 }
420 
421 /*
422  * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
423  * The core display code will have read out the current plane configuration,
424  * so we use that to figure out if there's an object for us to use as the
425  * fb, and if so, we re-use it for the fbdev configuration.
426  *
427  * Note we only support a single fb shared across pipes for boot (mostly for
428  * fbcon), so we just find the biggest and use that.
429  */
intel_fbdev_init_bios(struct drm_device * dev,struct intel_fbdev * ifbdev)430 static bool intel_fbdev_init_bios(struct drm_device *dev,
431 				  struct intel_fbdev *ifbdev)
432 {
433 	struct drm_i915_private *i915 = to_i915(dev);
434 	struct intel_framebuffer *fb = NULL;
435 	struct intel_crtc *crtc;
436 	unsigned int max_size = 0;
437 
438 	/* Find the largest fb */
439 	for_each_intel_crtc(dev, crtc) {
440 		struct intel_crtc_state *crtc_state =
441 			to_intel_crtc_state(crtc->base.state);
442 		struct intel_plane *plane =
443 			to_intel_plane(crtc->base.primary);
444 		struct intel_plane_state *plane_state =
445 			to_intel_plane_state(plane->base.state);
446 		struct drm_i915_gem_object *obj =
447 			intel_fb_obj(plane_state->uapi.fb);
448 
449 		if (!crtc_state->uapi.active) {
450 			drm_dbg_kms(&i915->drm,
451 				    "[CRTC:%d:%s] not active, skipping\n",
452 				    crtc->base.base.id, crtc->base.name);
453 			continue;
454 		}
455 
456 		if (!obj) {
457 			drm_dbg_kms(&i915->drm,
458 				    "[PLANE:%d:%s] no fb, skipping\n",
459 				    plane->base.base.id, plane->base.name);
460 			continue;
461 		}
462 
463 		if (obj->base.size > max_size) {
464 			drm_dbg_kms(&i915->drm,
465 				    "found possible fb from [PLANE:%d:%s]\n",
466 				    plane->base.base.id, plane->base.name);
467 			fb = to_intel_framebuffer(plane_state->uapi.fb);
468 			max_size = obj->base.size;
469 		}
470 	}
471 
472 	if (!fb) {
473 		drm_dbg_kms(&i915->drm,
474 			    "no active fbs found, not using BIOS config\n");
475 		goto out;
476 	}
477 
478 	/* Now make sure all the pipes will fit into it */
479 	for_each_intel_crtc(dev, crtc) {
480 		struct intel_crtc_state *crtc_state =
481 			to_intel_crtc_state(crtc->base.state);
482 		struct intel_plane *plane =
483 			to_intel_plane(crtc->base.primary);
484 		unsigned int cur_size;
485 
486 		if (!crtc_state->uapi.active) {
487 			drm_dbg_kms(&i915->drm,
488 				    "[CRTC:%d:%s] not active, skipping\n",
489 				    crtc->base.base.id, crtc->base.name);
490 			continue;
491 		}
492 
493 		drm_dbg_kms(&i915->drm, "checking [PLANE:%d:%s] for BIOS fb\n",
494 			    plane->base.base.id, plane->base.name);
495 
496 		/*
497 		 * See if the plane fb we found above will fit on this
498 		 * pipe.  Note we need to use the selected fb's pitch and bpp
499 		 * rather than the current pipe's, since they differ.
500 		 */
501 		cur_size = crtc_state->uapi.adjusted_mode.crtc_hdisplay;
502 		cur_size = cur_size * fb->base.format->cpp[0];
503 		if (fb->base.pitches[0] < cur_size) {
504 			drm_dbg_kms(&i915->drm,
505 				    "fb not wide enough for [PLANE:%d:%s] (%d vs %d)\n",
506 				    plane->base.base.id, plane->base.name,
507 				    cur_size, fb->base.pitches[0]);
508 			fb = NULL;
509 			break;
510 		}
511 
512 		cur_size = crtc_state->uapi.adjusted_mode.crtc_vdisplay;
513 		cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
514 		cur_size *= fb->base.pitches[0];
515 		drm_dbg_kms(&i915->drm,
516 			    "[CRTC:%d:%s] area: %dx%d, bpp: %d, size: %d\n",
517 			    crtc->base.base.id, crtc->base.name,
518 			    crtc_state->uapi.adjusted_mode.crtc_hdisplay,
519 			    crtc_state->uapi.adjusted_mode.crtc_vdisplay,
520 			    fb->base.format->cpp[0] * 8,
521 			    cur_size);
522 
523 		if (cur_size > max_size) {
524 			drm_dbg_kms(&i915->drm,
525 				    "fb not big enough for [PLANE:%d:%s] (%d vs %d)\n",
526 				    plane->base.base.id, plane->base.name,
527 				    cur_size, max_size);
528 			fb = NULL;
529 			break;
530 		}
531 
532 		drm_dbg_kms(&i915->drm,
533 			    "fb big enough [PLANE:%d:%s] (%d >= %d)\n",
534 			    plane->base.base.id, plane->base.name,
535 			    max_size, cur_size);
536 	}
537 
538 	if (!fb) {
539 		drm_dbg_kms(&i915->drm,
540 			    "BIOS fb not suitable for all pipes, not using\n");
541 		goto out;
542 	}
543 
544 	ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
545 	ifbdev->fb = fb;
546 
547 	drm_framebuffer_get(&ifbdev->fb->base);
548 
549 	/* Final pass to check if any active pipes don't have fbs */
550 	for_each_intel_crtc(dev, crtc) {
551 		struct intel_crtc_state *crtc_state =
552 			to_intel_crtc_state(crtc->base.state);
553 		struct intel_plane *plane =
554 			to_intel_plane(crtc->base.primary);
555 		struct intel_plane_state *plane_state =
556 			to_intel_plane_state(plane->base.state);
557 
558 		if (!crtc_state->uapi.active)
559 			continue;
560 
561 		drm_WARN(dev, !plane_state->uapi.fb,
562 			 "re-used BIOS config but lost an fb on [PLANE:%d:%s]\n",
563 			 plane->base.base.id, plane->base.name);
564 	}
565 
566 
567 	drm_dbg_kms(&i915->drm, "using BIOS fb for initial console\n");
568 	return true;
569 
570 out:
571 
572 	return false;
573 }
574 
intel_fbdev_suspend_worker(struct work_struct * work)575 static void intel_fbdev_suspend_worker(struct work_struct *work)
576 {
577 	intel_fbdev_set_suspend(&container_of(work,
578 					      struct drm_i915_private,
579 					      display.fbdev.suspend_work)->drm,
580 				FBINFO_STATE_RUNNING,
581 				true);
582 }
583 
intel_fbdev_init(struct drm_device * dev)584 int intel_fbdev_init(struct drm_device *dev)
585 {
586 	struct drm_i915_private *dev_priv = to_i915(dev);
587 	struct intel_fbdev *ifbdev;
588 	int ret;
589 
590 	if (drm_WARN_ON(dev, !HAS_DISPLAY(dev_priv)))
591 		return -ENODEV;
592 
593 	ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
594 	if (ifbdev == NULL)
595 		return -ENOMEM;
596 
597 	rw_init(&ifbdev->hpd_lock, "hdplk");
598 	drm_fb_helper_prepare(dev, &ifbdev->helper, 32, &intel_fb_helper_funcs);
599 
600 	if (intel_fbdev_init_bios(dev, ifbdev))
601 		ifbdev->helper.preferred_bpp = ifbdev->preferred_bpp;
602 	else
603 		ifbdev->preferred_bpp = ifbdev->helper.preferred_bpp;
604 
605 	ret = drm_fb_helper_init(dev, &ifbdev->helper);
606 	if (ret) {
607 		kfree(ifbdev);
608 		return ret;
609 	}
610 
611 	dev_priv->display.fbdev.fbdev = ifbdev;
612 	INIT_WORK(&dev_priv->display.fbdev.suspend_work, intel_fbdev_suspend_worker);
613 
614 	return 0;
615 }
616 
intel_fbdev_initial_config(void * data,async_cookie_t cookie)617 static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
618 {
619 	struct intel_fbdev *ifbdev = data;
620 
621 	/* Due to peculiar init order wrt to hpd handling this is separate. */
622 	if (drm_fb_helper_initial_config(&ifbdev->helper))
623 		intel_fbdev_unregister(to_i915(ifbdev->helper.dev));
624 }
625 
intel_fbdev_initial_config_async(struct drm_i915_private * dev_priv)626 void intel_fbdev_initial_config_async(struct drm_i915_private *dev_priv)
627 {
628 	struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
629 
630 	if (!ifbdev)
631 		return;
632 
633 	ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
634 }
635 
intel_fbdev_sync(struct intel_fbdev * ifbdev)636 static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
637 {
638 #ifdef __linux__
639 	if (!ifbdev->cookie)
640 		return;
641 
642 	/* Only serialises with all preceding async calls, hence +1 */
643 	async_synchronize_cookie(ifbdev->cookie + 1);
644 	ifbdev->cookie = 0;
645 #endif
646 }
647 
intel_fbdev_unregister(struct drm_i915_private * dev_priv)648 void intel_fbdev_unregister(struct drm_i915_private *dev_priv)
649 {
650 	struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
651 
652 	if (!ifbdev)
653 		return;
654 
655 	intel_fbdev_set_suspend(&dev_priv->drm, FBINFO_STATE_SUSPENDED, true);
656 
657 #ifdef __linux__
658 	if (!current_is_async())
659 		intel_fbdev_sync(ifbdev);
660 #endif
661 
662 	drm_fb_helper_unregister_info(&ifbdev->helper);
663 }
664 
intel_fbdev_fini(struct drm_i915_private * dev_priv)665 void intel_fbdev_fini(struct drm_i915_private *dev_priv)
666 {
667 	struct intel_fbdev *ifbdev = fetch_and_zero(&dev_priv->display.fbdev.fbdev);
668 
669 	if (!ifbdev)
670 		return;
671 
672 	intel_fbdev_destroy(ifbdev);
673 }
674 
675 /* Suspends/resumes fbdev processing of incoming HPD events. When resuming HPD
676  * processing, fbdev will perform a full connector reprobe if a hotplug event
677  * was received while HPD was suspended.
678  */
intel_fbdev_hpd_set_suspend(struct drm_i915_private * i915,int state)679 static void intel_fbdev_hpd_set_suspend(struct drm_i915_private *i915, int state)
680 {
681 	struct intel_fbdev *ifbdev = i915->display.fbdev.fbdev;
682 	bool send_hpd = false;
683 
684 	mutex_lock(&ifbdev->hpd_lock);
685 	ifbdev->hpd_suspended = state == FBINFO_STATE_SUSPENDED;
686 	send_hpd = !ifbdev->hpd_suspended && ifbdev->hpd_waiting;
687 	ifbdev->hpd_waiting = false;
688 	mutex_unlock(&ifbdev->hpd_lock);
689 
690 	if (send_hpd) {
691 		drm_dbg_kms(&i915->drm, "Handling delayed fbcon HPD event\n");
692 		drm_fb_helper_hotplug_event(&ifbdev->helper);
693 	}
694 }
695 
intel_fbdev_set_suspend(struct drm_device * dev,int state,bool synchronous)696 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
697 {
698 #ifdef __linux__
699 	struct drm_i915_private *dev_priv = to_i915(dev);
700 	struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
701 	struct fb_info *info;
702 
703 	if (!ifbdev)
704 		return;
705 
706 	if (drm_WARN_ON(&dev_priv->drm, !HAS_DISPLAY(dev_priv)))
707 		return;
708 
709 	if (!ifbdev->vma)
710 		goto set_suspend;
711 
712 	info = ifbdev->helper.info;
713 
714 	if (synchronous) {
715 		/* Flush any pending work to turn the console on, and then
716 		 * wait to turn it off. It must be synchronous as we are
717 		 * about to suspend or unload the driver.
718 		 *
719 		 * Note that from within the work-handler, we cannot flush
720 		 * ourselves, so only flush outstanding work upon suspend!
721 		 */
722 		if (state != FBINFO_STATE_RUNNING)
723 			flush_work(&dev_priv->display.fbdev.suspend_work);
724 
725 		console_lock();
726 	} else {
727 		/*
728 		 * The console lock can be pretty contented on resume due
729 		 * to all the printk activity.  Try to keep it out of the hot
730 		 * path of resume if possible.
731 		 */
732 		drm_WARN_ON(dev, state != FBINFO_STATE_RUNNING);
733 		if (!console_trylock()) {
734 			/* Don't block our own workqueue as this can
735 			 * be run in parallel with other i915.ko tasks.
736 			 */
737 			queue_work(dev_priv->unordered_wq,
738 				   &dev_priv->display.fbdev.suspend_work);
739 			return;
740 		}
741 	}
742 
743 	/* On resume from hibernation: If the object is shmemfs backed, it has
744 	 * been restored from swap. If the object is stolen however, it will be
745 	 * full of whatever garbage was left in there.
746 	 */
747 	if (state == FBINFO_STATE_RUNNING &&
748 	    !i915_gem_object_is_shmem(intel_fb_obj(&ifbdev->fb->base)))
749 		memset_io(info->screen_base, 0, info->screen_size);
750 
751 	drm_fb_helper_set_suspend(&ifbdev->helper, state);
752 	console_unlock();
753 
754 set_suspend:
755 	intel_fbdev_hpd_set_suspend(dev_priv, state);
756 #endif
757 }
758 
intel_fbdev_output_poll_changed(struct drm_device * dev)759 void intel_fbdev_output_poll_changed(struct drm_device *dev)
760 {
761 	struct intel_fbdev *ifbdev = to_i915(dev)->display.fbdev.fbdev;
762 	bool send_hpd;
763 
764 	if (!ifbdev)
765 		return;
766 
767 	intel_fbdev_sync(ifbdev);
768 
769 	mutex_lock(&ifbdev->hpd_lock);
770 	send_hpd = !ifbdev->hpd_suspended;
771 	ifbdev->hpd_waiting = true;
772 	mutex_unlock(&ifbdev->hpd_lock);
773 
774 	if (send_hpd && (ifbdev->vma || ifbdev->helper.deferred_setup))
775 		drm_fb_helper_hotplug_event(&ifbdev->helper);
776 }
777 
intel_fbdev_restore_mode(struct drm_i915_private * dev_priv)778 void intel_fbdev_restore_mode(struct drm_i915_private *dev_priv)
779 {
780 	struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
781 
782 	if (!ifbdev)
783 		return;
784 
785 	intel_fbdev_sync(ifbdev);
786 	if (!ifbdev->vma)
787 		return;
788 
789 	if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper) == 0)
790 		intel_fbdev_invalidate(ifbdev);
791 }
792 
intel_fbdev_framebuffer(struct intel_fbdev * fbdev)793 struct intel_framebuffer *intel_fbdev_framebuffer(struct intel_fbdev *fbdev)
794 {
795 	if (!fbdev || !fbdev->helper.fb)
796 		return NULL;
797 
798 	return to_intel_framebuffer(fbdev->helper.fb);
799 }
800