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/console.h>
28 #include <linux/delay.h>
29 #include <linux/errno.h>
30 #include <linux/fb.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_crtc_helper.h>
42 #include <drm/drm_fb_helper.h>
43 #include <drm/drm_fourcc.h>
44 #include <drm/drm_gem.h>
45 #include <drm/drm_gem_framebuffer_helper.h>
46 #include <drm/drm_print.h>
47 
48 #include "i915_drv.h"
49 #include "i915_vma.h"
50 #include "intel_bo.h"
51 #include "intel_display_types.h"
52 #include "intel_fb.h"
53 #include "intel_fb_pin.h"
54 #include "intel_fbdev.h"
55 #include "intel_fbdev_fb.h"
56 #include "intel_frontbuffer.h"
57 
58 struct intel_fbdev {
59 	struct drm_fb_helper helper;
60 	struct intel_framebuffer *fb;
61 	struct i915_vma *vma;
62 	unsigned long vma_flags;
63 	int preferred_bpp;
64 
65 	/* Whether or not fbdev hpd processing is temporarily suspended */
66 	bool hpd_suspended: 1;
67 	/* Set when a hotplug was received while HPD processing was suspended */
68 	bool hpd_waiting: 1;
69 
70 	/* Protects hpd_suspended */
71 	struct mutex hpd_lock;
72 };
73 
74 static struct intel_fbdev *to_intel_fbdev(struct drm_fb_helper *fb_helper)
75 {
76 	return container_of(fb_helper, struct intel_fbdev, helper);
77 }
78 
79 static struct intel_frontbuffer *to_frontbuffer(struct intel_fbdev *ifbdev)
80 {
81 	return ifbdev->fb->frontbuffer;
82 }
83 
84 static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev)
85 {
86 	intel_frontbuffer_invalidate(to_frontbuffer(ifbdev), ORIGIN_CPU);
87 }
88 
89 FB_GEN_DEFAULT_DEFERRED_IOMEM_OPS(intel_fbdev,
90 				  drm_fb_helper_damage_range,
91 				  drm_fb_helper_damage_area)
92 
93 static int intel_fbdev_set_par(struct fb_info *info)
94 {
95 	struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
96 	int ret;
97 
98 	ret = drm_fb_helper_set_par(info);
99 	if (ret == 0)
100 		intel_fbdev_invalidate(ifbdev);
101 
102 	return ret;
103 }
104 
105 static int intel_fbdev_blank(int blank, struct fb_info *info)
106 {
107 	struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
108 	int ret;
109 
110 	ret = drm_fb_helper_blank(blank, info);
111 	if (ret == 0)
112 		intel_fbdev_invalidate(ifbdev);
113 
114 	return ret;
115 }
116 
117 static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
118 				   struct fb_info *info)
119 {
120 	struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
121 	int ret;
122 
123 	ret = drm_fb_helper_pan_display(var, info);
124 	if (ret == 0)
125 		intel_fbdev_invalidate(ifbdev);
126 
127 	return ret;
128 }
129 
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 *obj = drm_gem_fb_get_obj(&fbdev->fb->base, 0);
134 
135 	return intel_bo_fb_mmap(obj, vma);
136 }
137 
138 static void intel_fbdev_fb_destroy(struct fb_info *info)
139 {
140 	struct drm_fb_helper *fb_helper = info->par;
141 	struct intel_fbdev *ifbdev = container_of(fb_helper, struct intel_fbdev, helper);
142 
143 	drm_fb_helper_fini(&ifbdev->helper);
144 
145 	/*
146 	 * We rely on the object-free to release the VMA pinning for
147 	 * the info->screen_base mmaping. Leaking the VMA is simpler than
148 	 * trying to rectify all the possible error paths leading here.
149 	 */
150 	intel_fb_unpin_vma(ifbdev->vma, ifbdev->vma_flags);
151 	drm_framebuffer_remove(&ifbdev->fb->base);
152 
153 	drm_client_release(&fb_helper->client);
154 	drm_fb_helper_unprepare(&ifbdev->helper);
155 	kfree(ifbdev);
156 }
157 
158 __diag_push();
159 __diag_ignore_all("-Woverride-init", "Allow field initialization overrides for fb ops");
160 
161 static const struct fb_ops intelfb_ops = {
162 	.owner = THIS_MODULE,
163 	__FB_DEFAULT_DEFERRED_OPS_RDWR(intel_fbdev),
164 	DRM_FB_HELPER_DEFAULT_OPS,
165 	.fb_set_par = intel_fbdev_set_par,
166 	.fb_blank = intel_fbdev_blank,
167 	.fb_pan_display = intel_fbdev_pan_display,
168 	__FB_DEFAULT_DEFERRED_OPS_DRAW(intel_fbdev),
169 	.fb_mmap = intel_fbdev_mmap,
170 	.fb_destroy = intel_fbdev_fb_destroy,
171 };
172 
173 __diag_pop();
174 
175 static int intelfb_create(struct drm_fb_helper *helper,
176 			  struct drm_fb_helper_surface_size *sizes)
177 {
178 	struct intel_fbdev *ifbdev = to_intel_fbdev(helper);
179 	struct intel_framebuffer *fb = ifbdev->fb;
180 	struct drm_device *dev = helper->dev;
181 	struct drm_i915_private *dev_priv = to_i915(dev);
182 	intel_wakeref_t wakeref;
183 	struct fb_info *info;
184 	struct i915_vma *vma;
185 	unsigned long flags = 0;
186 	bool prealloc = false;
187 	struct drm_gem_object *obj;
188 	int ret;
189 
190 	mutex_lock(&ifbdev->hpd_lock);
191 	ret = ifbdev->hpd_suspended ? -EAGAIN : 0;
192 	mutex_unlock(&ifbdev->hpd_lock);
193 	if (ret)
194 		return ret;
195 
196 	ifbdev->fb = NULL;
197 
198 	if (fb &&
199 	    (sizes->fb_width > fb->base.width ||
200 	     sizes->fb_height > fb->base.height)) {
201 		drm_dbg_kms(&dev_priv->drm,
202 			    "BIOS fb too small (%dx%d), we require (%dx%d),"
203 			    " releasing it\n",
204 			    fb->base.width, fb->base.height,
205 			    sizes->fb_width, sizes->fb_height);
206 		drm_framebuffer_put(&fb->base);
207 		fb = NULL;
208 	}
209 	if (!fb || drm_WARN_ON(dev, !intel_fb_bo(&fb->base))) {
210 		drm_dbg_kms(&dev_priv->drm,
211 			    "no BIOS fb, allocating a new one\n");
212 		fb = intel_fbdev_fb_alloc(helper, sizes);
213 		if (IS_ERR(fb))
214 			return PTR_ERR(fb);
215 	} else {
216 		drm_dbg_kms(&dev_priv->drm, "re-using BIOS fb\n");
217 		prealloc = true;
218 		sizes->fb_width = fb->base.width;
219 		sizes->fb_height = fb->base.height;
220 	}
221 
222 	wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
223 
224 	/* Pin the GGTT vma for our access via info->screen_base.
225 	 * This also validates that any existing fb inherited from the
226 	 * BIOS is suitable for own access.
227 	 */
228 	vma = intel_fb_pin_to_ggtt(&fb->base, &fb->normal_view.gtt,
229 				   fb->min_alignment, 0,
230 				   intel_fb_view_vtd_guard(&fb->base, &fb->normal_view,
231 							   DRM_MODE_ROTATE_0),
232 				   false, &flags);
233 	if (IS_ERR(vma)) {
234 		ret = PTR_ERR(vma);
235 		goto out_unlock;
236 	}
237 
238 	info = drm_fb_helper_alloc_info(helper);
239 	if (IS_ERR(info)) {
240 		drm_err(&dev_priv->drm, "Failed to allocate fb_info (%pe)\n", info);
241 		ret = PTR_ERR(info);
242 		goto out_unpin;
243 	}
244 
245 	ifbdev->helper.fb = &fb->base;
246 
247 	info->fbops = &intelfb_ops;
248 
249 	obj = intel_fb_bo(&fb->base);
250 
251 	ret = intel_fbdev_fb_fill_info(dev_priv, info, obj, vma);
252 	if (ret)
253 		goto out_unpin;
254 
255 	drm_fb_helper_fill_info(info, &ifbdev->helper, sizes);
256 
257 	/* If the object is shmemfs backed, it will have given us zeroed pages.
258 	 * If the object is stolen however, it will be full of whatever
259 	 * garbage was left in there.
260 	 */
261 	if (!intel_bo_is_shmem(obj) && !prealloc)
262 		memset_io(info->screen_base, 0, info->screen_size);
263 
264 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
265 
266 	drm_dbg_kms(&dev_priv->drm, "allocated %dx%d fb: 0x%08x\n",
267 		    fb->base.width, fb->base.height,
268 		    i915_ggtt_offset(vma));
269 	ifbdev->fb = fb;
270 	ifbdev->vma = vma;
271 	ifbdev->vma_flags = flags;
272 
273 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
274 
275 	return 0;
276 
277 out_unpin:
278 	intel_fb_unpin_vma(vma, flags);
279 out_unlock:
280 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
281 	return ret;
282 }
283 
284 static int intelfb_dirty(struct drm_fb_helper *helper, struct drm_clip_rect *clip)
285 {
286 	if (!(clip->x1 < clip->x2 && clip->y1 < clip->y2))
287 		return 0;
288 
289 	if (helper->fb->funcs->dirty)
290 		return helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, clip, 1);
291 
292 	return 0;
293 }
294 
295 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
296 	.fb_probe = intelfb_create,
297 	.fb_dirty = intelfb_dirty,
298 };
299 
300 /*
301  * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
302  * The core display code will have read out the current plane configuration,
303  * so we use that to figure out if there's an object for us to use as the
304  * fb, and if so, we re-use it for the fbdev configuration.
305  *
306  * Note we only support a single fb shared across pipes for boot (mostly for
307  * fbcon), so we just find the biggest and use that.
308  */
309 static bool intel_fbdev_init_bios(struct drm_device *dev,
310 				  struct intel_fbdev *ifbdev)
311 {
312 	struct drm_i915_private *i915 = to_i915(dev);
313 	struct intel_framebuffer *fb = NULL;
314 	struct intel_crtc *crtc;
315 	unsigned int max_size = 0;
316 
317 	/* Find the largest fb */
318 	for_each_intel_crtc(dev, crtc) {
319 		struct intel_crtc_state *crtc_state =
320 			to_intel_crtc_state(crtc->base.state);
321 		struct intel_plane *plane =
322 			to_intel_plane(crtc->base.primary);
323 		struct intel_plane_state *plane_state =
324 			to_intel_plane_state(plane->base.state);
325 		struct drm_gem_object *obj = intel_fb_bo(plane_state->uapi.fb);
326 
327 		if (!crtc_state->uapi.active) {
328 			drm_dbg_kms(&i915->drm,
329 				    "[CRTC:%d:%s] not active, skipping\n",
330 				    crtc->base.base.id, crtc->base.name);
331 			continue;
332 		}
333 
334 		if (!obj) {
335 			drm_dbg_kms(&i915->drm,
336 				    "[PLANE:%d:%s] no fb, skipping\n",
337 				    plane->base.base.id, plane->base.name);
338 			continue;
339 		}
340 
341 		if (obj->size > max_size) {
342 			drm_dbg_kms(&i915->drm,
343 				    "found possible fb from [PLANE:%d:%s]\n",
344 				    plane->base.base.id, plane->base.name);
345 			fb = to_intel_framebuffer(plane_state->uapi.fb);
346 			max_size = obj->size;
347 		}
348 	}
349 
350 	if (!fb) {
351 		drm_dbg_kms(&i915->drm,
352 			    "no active fbs found, not using BIOS config\n");
353 		goto out;
354 	}
355 
356 	/* Now make sure all the pipes will fit into it */
357 	for_each_intel_crtc(dev, crtc) {
358 		struct intel_crtc_state *crtc_state =
359 			to_intel_crtc_state(crtc->base.state);
360 		struct intel_plane *plane =
361 			to_intel_plane(crtc->base.primary);
362 		unsigned int cur_size;
363 
364 		if (!crtc_state->uapi.active) {
365 			drm_dbg_kms(&i915->drm,
366 				    "[CRTC:%d:%s] not active, skipping\n",
367 				    crtc->base.base.id, crtc->base.name);
368 			continue;
369 		}
370 
371 		drm_dbg_kms(&i915->drm, "checking [PLANE:%d:%s] for BIOS fb\n",
372 			    plane->base.base.id, plane->base.name);
373 
374 		/*
375 		 * See if the plane fb we found above will fit on this
376 		 * pipe.  Note we need to use the selected fb's pitch and bpp
377 		 * rather than the current pipe's, since they differ.
378 		 */
379 		cur_size = crtc_state->uapi.adjusted_mode.crtc_hdisplay;
380 		cur_size = cur_size * fb->base.format->cpp[0];
381 		if (fb->base.pitches[0] < cur_size) {
382 			drm_dbg_kms(&i915->drm,
383 				    "fb not wide enough for [PLANE:%d:%s] (%d vs %d)\n",
384 				    plane->base.base.id, plane->base.name,
385 				    cur_size, fb->base.pitches[0]);
386 			fb = NULL;
387 			break;
388 		}
389 
390 		cur_size = crtc_state->uapi.adjusted_mode.crtc_vdisplay;
391 		cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
392 		cur_size *= fb->base.pitches[0];
393 		drm_dbg_kms(&i915->drm,
394 			    "[CRTC:%d:%s] area: %dx%d, bpp: %d, size: %d\n",
395 			    crtc->base.base.id, crtc->base.name,
396 			    crtc_state->uapi.adjusted_mode.crtc_hdisplay,
397 			    crtc_state->uapi.adjusted_mode.crtc_vdisplay,
398 			    fb->base.format->cpp[0] * 8,
399 			    cur_size);
400 
401 		if (cur_size > max_size) {
402 			drm_dbg_kms(&i915->drm,
403 				    "fb not big enough for [PLANE:%d:%s] (%d vs %d)\n",
404 				    plane->base.base.id, plane->base.name,
405 				    cur_size, max_size);
406 			fb = NULL;
407 			break;
408 		}
409 
410 		drm_dbg_kms(&i915->drm,
411 			    "fb big enough [PLANE:%d:%s] (%d >= %d)\n",
412 			    plane->base.base.id, plane->base.name,
413 			    max_size, cur_size);
414 	}
415 
416 	if (!fb) {
417 		drm_dbg_kms(&i915->drm,
418 			    "BIOS fb not suitable for all pipes, not using\n");
419 		goto out;
420 	}
421 
422 	ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
423 	ifbdev->fb = fb;
424 
425 	drm_framebuffer_get(&ifbdev->fb->base);
426 
427 	/* Final pass to check if any active pipes don't have fbs */
428 	for_each_intel_crtc(dev, crtc) {
429 		struct intel_crtc_state *crtc_state =
430 			to_intel_crtc_state(crtc->base.state);
431 		struct intel_plane *plane =
432 			to_intel_plane(crtc->base.primary);
433 		struct intel_plane_state *plane_state =
434 			to_intel_plane_state(plane->base.state);
435 
436 		if (!crtc_state->uapi.active)
437 			continue;
438 
439 		drm_WARN(dev, !plane_state->uapi.fb,
440 			 "re-used BIOS config but lost an fb on [PLANE:%d:%s]\n",
441 			 plane->base.base.id, plane->base.name);
442 	}
443 
444 
445 	drm_dbg_kms(&i915->drm, "using BIOS fb for initial console\n");
446 	return true;
447 
448 out:
449 
450 	return false;
451 }
452 
453 static void intel_fbdev_suspend_worker(struct work_struct *work)
454 {
455 	intel_fbdev_set_suspend(&container_of(work,
456 					      struct drm_i915_private,
457 					      display.fbdev.suspend_work)->drm,
458 				FBINFO_STATE_RUNNING,
459 				true);
460 }
461 
462 /* Suspends/resumes fbdev processing of incoming HPD events. When resuming HPD
463  * processing, fbdev will perform a full connector reprobe if a hotplug event
464  * was received while HPD was suspended.
465  */
466 static void intel_fbdev_hpd_set_suspend(struct drm_i915_private *i915, int state)
467 {
468 	struct intel_fbdev *ifbdev = i915->display.fbdev.fbdev;
469 	bool send_hpd = false;
470 
471 	mutex_lock(&ifbdev->hpd_lock);
472 	ifbdev->hpd_suspended = state == FBINFO_STATE_SUSPENDED;
473 	send_hpd = !ifbdev->hpd_suspended && ifbdev->hpd_waiting;
474 	ifbdev->hpd_waiting = false;
475 	mutex_unlock(&ifbdev->hpd_lock);
476 
477 	if (send_hpd) {
478 		drm_dbg_kms(&i915->drm, "Handling delayed fbcon HPD event\n");
479 		drm_fb_helper_hotplug_event(&ifbdev->helper);
480 	}
481 }
482 
483 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
484 {
485 	struct drm_i915_private *dev_priv = to_i915(dev);
486 	struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
487 	struct fb_info *info;
488 
489 	if (!ifbdev)
490 		return;
491 
492 	if (drm_WARN_ON(&dev_priv->drm, !HAS_DISPLAY(dev_priv)))
493 		return;
494 
495 	if (!ifbdev->vma)
496 		goto set_suspend;
497 
498 	info = ifbdev->helper.info;
499 
500 	if (synchronous) {
501 		/* Flush any pending work to turn the console on, and then
502 		 * wait to turn it off. It must be synchronous as we are
503 		 * about to suspend or unload the driver.
504 		 *
505 		 * Note that from within the work-handler, we cannot flush
506 		 * ourselves, so only flush outstanding work upon suspend!
507 		 */
508 		if (state != FBINFO_STATE_RUNNING)
509 			flush_work(&dev_priv->display.fbdev.suspend_work);
510 
511 		console_lock();
512 	} else {
513 		/*
514 		 * The console lock can be pretty contented on resume due
515 		 * to all the printk activity.  Try to keep it out of the hot
516 		 * path of resume if possible.
517 		 */
518 		drm_WARN_ON(dev, state != FBINFO_STATE_RUNNING);
519 		if (!console_trylock()) {
520 			/* Don't block our own workqueue as this can
521 			 * be run in parallel with other i915.ko tasks.
522 			 */
523 			queue_work(dev_priv->unordered_wq,
524 				   &dev_priv->display.fbdev.suspend_work);
525 			return;
526 		}
527 	}
528 
529 	/* On resume from hibernation: If the object is shmemfs backed, it has
530 	 * been restored from swap. If the object is stolen however, it will be
531 	 * full of whatever garbage was left in there.
532 	 */
533 	if (state == FBINFO_STATE_RUNNING &&
534 	    !intel_bo_is_shmem(intel_fb_bo(&ifbdev->fb->base)))
535 		memset_io(info->screen_base, 0, info->screen_size);
536 
537 	drm_fb_helper_set_suspend(&ifbdev->helper, state);
538 	console_unlock();
539 
540 set_suspend:
541 	intel_fbdev_hpd_set_suspend(dev_priv, state);
542 }
543 
544 static int intel_fbdev_output_poll_changed(struct drm_device *dev)
545 {
546 	struct intel_fbdev *ifbdev = to_i915(dev)->display.fbdev.fbdev;
547 	bool send_hpd;
548 
549 	if (!ifbdev)
550 		return -EINVAL;
551 
552 	mutex_lock(&ifbdev->hpd_lock);
553 	send_hpd = !ifbdev->hpd_suspended;
554 	ifbdev->hpd_waiting = true;
555 	mutex_unlock(&ifbdev->hpd_lock);
556 
557 	if (send_hpd && (ifbdev->vma || ifbdev->helper.deferred_setup))
558 		drm_fb_helper_hotplug_event(&ifbdev->helper);
559 
560 	return 0;
561 }
562 
563 static int intel_fbdev_restore_mode(struct drm_i915_private *dev_priv)
564 {
565 	struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
566 	int ret;
567 
568 	if (!ifbdev)
569 		return -EINVAL;
570 
571 	if (!ifbdev->vma)
572 		return -ENOMEM;
573 
574 	ret = drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper);
575 	if (ret)
576 		return ret;
577 
578 	intel_fbdev_invalidate(ifbdev);
579 
580 	return 0;
581 }
582 
583 /*
584  * Fbdev client and struct drm_client_funcs
585  */
586 
587 static void intel_fbdev_client_unregister(struct drm_client_dev *client)
588 {
589 	struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
590 	struct drm_device *dev = fb_helper->dev;
591 	struct pci_dev *pdev = to_pci_dev(dev->dev);
592 
593 	if (fb_helper->info) {
594 		vga_switcheroo_client_fb_set(pdev, NULL);
595 		drm_fb_helper_unregister_info(fb_helper);
596 	} else {
597 		drm_fb_helper_unprepare(fb_helper);
598 		drm_client_release(&fb_helper->client);
599 		kfree(fb_helper);
600 	}
601 }
602 
603 static int intel_fbdev_client_restore(struct drm_client_dev *client)
604 {
605 	struct drm_i915_private *dev_priv = to_i915(client->dev);
606 	int ret;
607 
608 	ret = intel_fbdev_restore_mode(dev_priv);
609 	if (ret)
610 		return ret;
611 
612 	vga_switcheroo_process_delayed_switch();
613 
614 	return 0;
615 }
616 
617 static int intel_fbdev_client_hotplug(struct drm_client_dev *client)
618 {
619 	struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
620 	struct drm_device *dev = client->dev;
621 	struct pci_dev *pdev = to_pci_dev(dev->dev);
622 	int ret;
623 
624 	if (dev->fb_helper)
625 		return intel_fbdev_output_poll_changed(dev);
626 
627 	ret = drm_fb_helper_init(dev, fb_helper);
628 	if (ret)
629 		goto err_drm_err;
630 
631 	ret = drm_fb_helper_initial_config(fb_helper);
632 	if (ret)
633 		goto err_drm_fb_helper_fini;
634 
635 	vga_switcheroo_client_fb_set(pdev, fb_helper->info);
636 
637 	return 0;
638 
639 err_drm_fb_helper_fini:
640 	drm_fb_helper_fini(fb_helper);
641 err_drm_err:
642 	drm_err(dev, "Failed to setup i915 fbdev emulation (ret=%d)\n", ret);
643 	return ret;
644 }
645 
646 static const struct drm_client_funcs intel_fbdev_client_funcs = {
647 	.owner		= THIS_MODULE,
648 	.unregister	= intel_fbdev_client_unregister,
649 	.restore	= intel_fbdev_client_restore,
650 	.hotplug	= intel_fbdev_client_hotplug,
651 };
652 
653 void intel_fbdev_setup(struct drm_i915_private *i915)
654 {
655 	struct drm_device *dev = &i915->drm;
656 	struct intel_fbdev *ifbdev;
657 	int ret;
658 
659 	if (!HAS_DISPLAY(i915))
660 		return;
661 
662 	ifbdev = kzalloc(sizeof(*ifbdev), GFP_KERNEL);
663 	if (!ifbdev)
664 		return;
665 	drm_fb_helper_prepare(dev, &ifbdev->helper, 32, &intel_fb_helper_funcs);
666 
667 	i915->display.fbdev.fbdev = ifbdev;
668 	INIT_WORK(&i915->display.fbdev.suspend_work, intel_fbdev_suspend_worker);
669 	mutex_init(&ifbdev->hpd_lock);
670 	if (intel_fbdev_init_bios(dev, ifbdev))
671 		ifbdev->helper.preferred_bpp = ifbdev->preferred_bpp;
672 	else
673 		ifbdev->preferred_bpp = ifbdev->helper.preferred_bpp;
674 
675 	ret = drm_client_init(dev, &ifbdev->helper.client, "intel-fbdev",
676 			      &intel_fbdev_client_funcs);
677 	if (ret) {
678 		drm_err(dev, "Failed to register client: %d\n", ret);
679 		goto err_drm_fb_helper_unprepare;
680 	}
681 
682 	drm_client_register(&ifbdev->helper.client);
683 
684 	return;
685 
686 err_drm_fb_helper_unprepare:
687 	drm_fb_helper_unprepare(&ifbdev->helper);
688 	mutex_destroy(&ifbdev->hpd_lock);
689 	kfree(ifbdev);
690 }
691 
692 struct intel_framebuffer *intel_fbdev_framebuffer(struct intel_fbdev *fbdev)
693 {
694 	if (!fbdev || !fbdev->helper.fb)
695 		return NULL;
696 
697 	return to_intel_framebuffer(fbdev->helper.fb);
698 }
699 
700 struct i915_vma *intel_fbdev_vma_pointer(struct intel_fbdev *fbdev)
701 {
702 	return fbdev ? fbdev->vma : NULL;
703 }
704