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