xref: /linux/drivers/gpu/drm/i915/display/intel_fbdev.c (revision 569d7db70e5dcf13fbf072f10e9096577ac1e565)
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_framebuffer_helper.h>
45 
46 #include "gem/i915_gem_mman.h"
47 #include "gem/i915_gem_object.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_fbdev_fb.h"
55 #include "intel_frontbuffer.h"
56 
57 struct intel_fbdev {
58 	struct drm_fb_helper helper;
59 	struct intel_framebuffer *fb;
60 	struct i915_vma *vma;
61 	unsigned long vma_flags;
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 mutex hpd_lock;
71 };
72 
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 
78 static struct intel_frontbuffer *to_frontbuffer(struct intel_fbdev *ifbdev)
79 {
80 	return ifbdev->fb->frontbuffer;
81 }
82 
83 static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev)
84 {
85 	intel_frontbuffer_invalidate(to_frontbuffer(ifbdev), ORIGIN_CPU);
86 }
87 
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 
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 
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 static int intel_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma)
130 {
131 	struct intel_fbdev *fbdev = to_intel_fbdev(info->par);
132 	struct drm_gem_object *bo = drm_gem_fb_get_obj(&fbdev->fb->base, 0);
133 	struct drm_i915_gem_object *obj = to_intel_bo(bo);
134 
135 	return i915_gem_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 	const struct i915_gtt_view view = {
183 		.type = I915_GTT_VIEW_NORMAL,
184 	};
185 	intel_wakeref_t wakeref;
186 	struct fb_info *info;
187 	struct i915_vma *vma;
188 	unsigned long flags = 0;
189 	bool prealloc = false;
190 	struct drm_i915_gem_object *obj;
191 	int ret;
192 
193 	mutex_lock(&ifbdev->hpd_lock);
194 	ret = ifbdev->hpd_suspended ? -EAGAIN : 0;
195 	mutex_unlock(&ifbdev->hpd_lock);
196 	if (ret)
197 		return ret;
198 
199 	ifbdev->fb = NULL;
200 
201 	if (fb &&
202 	    (sizes->fb_width > fb->base.width ||
203 	     sizes->fb_height > fb->base.height)) {
204 		drm_dbg_kms(&dev_priv->drm,
205 			    "BIOS fb too small (%dx%d), we require (%dx%d),"
206 			    " releasing it\n",
207 			    fb->base.width, fb->base.height,
208 			    sizes->fb_width, sizes->fb_height);
209 		drm_framebuffer_put(&fb->base);
210 		fb = NULL;
211 	}
212 	if (!fb || drm_WARN_ON(dev, !intel_fb_obj(&fb->base))) {
213 		drm_dbg_kms(&dev_priv->drm,
214 			    "no BIOS fb, allocating a new one\n");
215 		fb = intel_fbdev_fb_alloc(helper, sizes);
216 		if (IS_ERR(fb))
217 			return PTR_ERR(fb);
218 	} else {
219 		drm_dbg_kms(&dev_priv->drm, "re-using BIOS fb\n");
220 		prealloc = true;
221 		sizes->fb_width = fb->base.width;
222 		sizes->fb_height = 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_fb_pin_to_ggtt(&fb->base, false,
232 				   &view, 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_obj(&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 (!i915_gem_object_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_i915_gem_object *obj =
326 			intel_fb_obj(plane_state->uapi.fb);
327 
328 		if (!crtc_state->uapi.active) {
329 			drm_dbg_kms(&i915->drm,
330 				    "[CRTC:%d:%s] not active, skipping\n",
331 				    crtc->base.base.id, crtc->base.name);
332 			continue;
333 		}
334 
335 		if (!obj) {
336 			drm_dbg_kms(&i915->drm,
337 				    "[PLANE:%d:%s] no fb, skipping\n",
338 				    plane->base.base.id, plane->base.name);
339 			continue;
340 		}
341 
342 		if (intel_bo_to_drm_bo(obj)->size > max_size) {
343 			drm_dbg_kms(&i915->drm,
344 				    "found possible fb from [PLANE:%d:%s]\n",
345 				    plane->base.base.id, plane->base.name);
346 			fb = to_intel_framebuffer(plane_state->uapi.fb);
347 			max_size = intel_bo_to_drm_bo(obj)->size;
348 		}
349 	}
350 
351 	if (!fb) {
352 		drm_dbg_kms(&i915->drm,
353 			    "no active fbs found, not using BIOS config\n");
354 		goto out;
355 	}
356 
357 	/* Now make sure all the pipes will fit into it */
358 	for_each_intel_crtc(dev, crtc) {
359 		struct intel_crtc_state *crtc_state =
360 			to_intel_crtc_state(crtc->base.state);
361 		struct intel_plane *plane =
362 			to_intel_plane(crtc->base.primary);
363 		unsigned int cur_size;
364 
365 		if (!crtc_state->uapi.active) {
366 			drm_dbg_kms(&i915->drm,
367 				    "[CRTC:%d:%s] not active, skipping\n",
368 				    crtc->base.base.id, crtc->base.name);
369 			continue;
370 		}
371 
372 		drm_dbg_kms(&i915->drm, "checking [PLANE:%d:%s] for BIOS fb\n",
373 			    plane->base.base.id, plane->base.name);
374 
375 		/*
376 		 * See if the plane fb we found above will fit on this
377 		 * pipe.  Note we need to use the selected fb's pitch and bpp
378 		 * rather than the current pipe's, since they differ.
379 		 */
380 		cur_size = crtc_state->uapi.adjusted_mode.crtc_hdisplay;
381 		cur_size = cur_size * fb->base.format->cpp[0];
382 		if (fb->base.pitches[0] < cur_size) {
383 			drm_dbg_kms(&i915->drm,
384 				    "fb not wide enough for [PLANE:%d:%s] (%d vs %d)\n",
385 				    plane->base.base.id, plane->base.name,
386 				    cur_size, fb->base.pitches[0]);
387 			fb = NULL;
388 			break;
389 		}
390 
391 		cur_size = crtc_state->uapi.adjusted_mode.crtc_vdisplay;
392 		cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
393 		cur_size *= fb->base.pitches[0];
394 		drm_dbg_kms(&i915->drm,
395 			    "[CRTC:%d:%s] area: %dx%d, bpp: %d, size: %d\n",
396 			    crtc->base.base.id, crtc->base.name,
397 			    crtc_state->uapi.adjusted_mode.crtc_hdisplay,
398 			    crtc_state->uapi.adjusted_mode.crtc_vdisplay,
399 			    fb->base.format->cpp[0] * 8,
400 			    cur_size);
401 
402 		if (cur_size > max_size) {
403 			drm_dbg_kms(&i915->drm,
404 				    "fb not big enough for [PLANE:%d:%s] (%d vs %d)\n",
405 				    plane->base.base.id, plane->base.name,
406 				    cur_size, max_size);
407 			fb = NULL;
408 			break;
409 		}
410 
411 		drm_dbg_kms(&i915->drm,
412 			    "fb big enough [PLANE:%d:%s] (%d >= %d)\n",
413 			    plane->base.base.id, plane->base.name,
414 			    max_size, cur_size);
415 	}
416 
417 	if (!fb) {
418 		drm_dbg_kms(&i915->drm,
419 			    "BIOS fb not suitable for all pipes, not using\n");
420 		goto out;
421 	}
422 
423 	ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
424 	ifbdev->fb = fb;
425 
426 	drm_framebuffer_get(&ifbdev->fb->base);
427 
428 	/* Final pass to check if any active pipes don't have fbs */
429 	for_each_intel_crtc(dev, crtc) {
430 		struct intel_crtc_state *crtc_state =
431 			to_intel_crtc_state(crtc->base.state);
432 		struct intel_plane *plane =
433 			to_intel_plane(crtc->base.primary);
434 		struct intel_plane_state *plane_state =
435 			to_intel_plane_state(plane->base.state);
436 
437 		if (!crtc_state->uapi.active)
438 			continue;
439 
440 		drm_WARN(dev, !plane_state->uapi.fb,
441 			 "re-used BIOS config but lost an fb on [PLANE:%d:%s]\n",
442 			 plane->base.base.id, plane->base.name);
443 	}
444 
445 
446 	drm_dbg_kms(&i915->drm, "using BIOS fb for initial console\n");
447 	return true;
448 
449 out:
450 
451 	return false;
452 }
453 
454 static void intel_fbdev_suspend_worker(struct work_struct *work)
455 {
456 	intel_fbdev_set_suspend(&container_of(work,
457 					      struct drm_i915_private,
458 					      display.fbdev.suspend_work)->drm,
459 				FBINFO_STATE_RUNNING,
460 				true);
461 }
462 
463 /* Suspends/resumes fbdev processing of incoming HPD events. When resuming HPD
464  * processing, fbdev will perform a full connector reprobe if a hotplug event
465  * was received while HPD was suspended.
466  */
467 static void intel_fbdev_hpd_set_suspend(struct drm_i915_private *i915, int state)
468 {
469 	struct intel_fbdev *ifbdev = i915->display.fbdev.fbdev;
470 	bool send_hpd = false;
471 
472 	mutex_lock(&ifbdev->hpd_lock);
473 	ifbdev->hpd_suspended = state == FBINFO_STATE_SUSPENDED;
474 	send_hpd = !ifbdev->hpd_suspended && ifbdev->hpd_waiting;
475 	ifbdev->hpd_waiting = false;
476 	mutex_unlock(&ifbdev->hpd_lock);
477 
478 	if (send_hpd) {
479 		drm_dbg_kms(&i915->drm, "Handling delayed fbcon HPD event\n");
480 		drm_fb_helper_hotplug_event(&ifbdev->helper);
481 	}
482 }
483 
484 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
485 {
486 	struct drm_i915_private *dev_priv = to_i915(dev);
487 	struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
488 	struct fb_info *info;
489 
490 	if (!ifbdev)
491 		return;
492 
493 	if (drm_WARN_ON(&dev_priv->drm, !HAS_DISPLAY(dev_priv)))
494 		return;
495 
496 	if (!ifbdev->vma)
497 		goto set_suspend;
498 
499 	info = ifbdev->helper.info;
500 
501 	if (synchronous) {
502 		/* Flush any pending work to turn the console on, and then
503 		 * wait to turn it off. It must be synchronous as we are
504 		 * about to suspend or unload the driver.
505 		 *
506 		 * Note that from within the work-handler, we cannot flush
507 		 * ourselves, so only flush outstanding work upon suspend!
508 		 */
509 		if (state != FBINFO_STATE_RUNNING)
510 			flush_work(&dev_priv->display.fbdev.suspend_work);
511 
512 		console_lock();
513 	} else {
514 		/*
515 		 * The console lock can be pretty contented on resume due
516 		 * to all the printk activity.  Try to keep it out of the hot
517 		 * path of resume if possible.
518 		 */
519 		drm_WARN_ON(dev, state != FBINFO_STATE_RUNNING);
520 		if (!console_trylock()) {
521 			/* Don't block our own workqueue as this can
522 			 * be run in parallel with other i915.ko tasks.
523 			 */
524 			queue_work(dev_priv->unordered_wq,
525 				   &dev_priv->display.fbdev.suspend_work);
526 			return;
527 		}
528 	}
529 
530 	/* On resume from hibernation: If the object is shmemfs backed, it has
531 	 * been restored from swap. If the object is stolen however, it will be
532 	 * full of whatever garbage was left in there.
533 	 */
534 	if (state == FBINFO_STATE_RUNNING &&
535 	    !i915_gem_object_is_shmem(intel_fb_obj(&ifbdev->fb->base)))
536 		memset_io(info->screen_base, 0, info->screen_size);
537 
538 	drm_fb_helper_set_suspend(&ifbdev->helper, state);
539 	console_unlock();
540 
541 set_suspend:
542 	intel_fbdev_hpd_set_suspend(dev_priv, state);
543 }
544 
545 static int intel_fbdev_output_poll_changed(struct drm_device *dev)
546 {
547 	struct intel_fbdev *ifbdev = to_i915(dev)->display.fbdev.fbdev;
548 	bool send_hpd;
549 
550 	if (!ifbdev)
551 		return -EINVAL;
552 
553 	mutex_lock(&ifbdev->hpd_lock);
554 	send_hpd = !ifbdev->hpd_suspended;
555 	ifbdev->hpd_waiting = true;
556 	mutex_unlock(&ifbdev->hpd_lock);
557 
558 	if (send_hpd && (ifbdev->vma || ifbdev->helper.deferred_setup))
559 		drm_fb_helper_hotplug_event(&ifbdev->helper);
560 
561 	return 0;
562 }
563 
564 static int intel_fbdev_restore_mode(struct drm_i915_private *dev_priv)
565 {
566 	struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
567 	int ret;
568 
569 	if (!ifbdev)
570 		return -EINVAL;
571 
572 	if (!ifbdev->vma)
573 		return -ENOMEM;
574 
575 	ret = drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper);
576 	if (ret)
577 		return ret;
578 
579 	intel_fbdev_invalidate(ifbdev);
580 
581 	return 0;
582 }
583 
584 /*
585  * Fbdev client and struct drm_client_funcs
586  */
587 
588 static void intel_fbdev_client_unregister(struct drm_client_dev *client)
589 {
590 	struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
591 	struct drm_device *dev = fb_helper->dev;
592 	struct pci_dev *pdev = to_pci_dev(dev->dev);
593 
594 	if (fb_helper->info) {
595 		vga_switcheroo_client_fb_set(pdev, NULL);
596 		drm_fb_helper_unregister_info(fb_helper);
597 	} else {
598 		drm_fb_helper_unprepare(fb_helper);
599 		drm_client_release(&fb_helper->client);
600 		kfree(fb_helper);
601 	}
602 }
603 
604 static int intel_fbdev_client_restore(struct drm_client_dev *client)
605 {
606 	struct drm_i915_private *dev_priv = to_i915(client->dev);
607 	int ret;
608 
609 	ret = intel_fbdev_restore_mode(dev_priv);
610 	if (ret)
611 		return ret;
612 
613 	vga_switcheroo_process_delayed_switch();
614 
615 	return 0;
616 }
617 
618 static int intel_fbdev_client_hotplug(struct drm_client_dev *client)
619 {
620 	struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
621 	struct drm_device *dev = client->dev;
622 	struct pci_dev *pdev = to_pci_dev(dev->dev);
623 	int ret;
624 
625 	if (dev->fb_helper)
626 		return intel_fbdev_output_poll_changed(dev);
627 
628 	ret = drm_fb_helper_init(dev, fb_helper);
629 	if (ret)
630 		goto err_drm_err;
631 
632 	ret = drm_fb_helper_initial_config(fb_helper);
633 	if (ret)
634 		goto err_drm_fb_helper_fini;
635 
636 	vga_switcheroo_client_fb_set(pdev, fb_helper->info);
637 
638 	return 0;
639 
640 err_drm_fb_helper_fini:
641 	drm_fb_helper_fini(fb_helper);
642 err_drm_err:
643 	drm_err(dev, "Failed to setup i915 fbdev emulation (ret=%d)\n", ret);
644 	return ret;
645 }
646 
647 static const struct drm_client_funcs intel_fbdev_client_funcs = {
648 	.owner		= THIS_MODULE,
649 	.unregister	= intel_fbdev_client_unregister,
650 	.restore	= intel_fbdev_client_restore,
651 	.hotplug	= intel_fbdev_client_hotplug,
652 };
653 
654 void intel_fbdev_setup(struct drm_i915_private *i915)
655 {
656 	struct drm_device *dev = &i915->drm;
657 	struct intel_fbdev *ifbdev;
658 	int ret;
659 
660 	if (!HAS_DISPLAY(i915))
661 		return;
662 
663 	ifbdev = kzalloc(sizeof(*ifbdev), GFP_KERNEL);
664 	if (!ifbdev)
665 		return;
666 	drm_fb_helper_prepare(dev, &ifbdev->helper, 32, &intel_fb_helper_funcs);
667 
668 	i915->display.fbdev.fbdev = ifbdev;
669 	INIT_WORK(&i915->display.fbdev.suspend_work, intel_fbdev_suspend_worker);
670 	mutex_init(&ifbdev->hpd_lock);
671 	if (intel_fbdev_init_bios(dev, ifbdev))
672 		ifbdev->helper.preferred_bpp = ifbdev->preferred_bpp;
673 	else
674 		ifbdev->preferred_bpp = ifbdev->helper.preferred_bpp;
675 
676 	ret = drm_client_init(dev, &ifbdev->helper.client, "intel-fbdev",
677 			      &intel_fbdev_client_funcs);
678 	if (ret) {
679 		drm_err(dev, "Failed to register client: %d\n", ret);
680 		goto err_drm_fb_helper_unprepare;
681 	}
682 
683 	drm_client_register(&ifbdev->helper.client);
684 
685 	return;
686 
687 err_drm_fb_helper_unprepare:
688 	drm_fb_helper_unprepare(&ifbdev->helper);
689 	mutex_destroy(&ifbdev->hpd_lock);
690 	kfree(ifbdev);
691 }
692 
693 struct intel_framebuffer *intel_fbdev_framebuffer(struct intel_fbdev *fbdev)
694 {
695 	if (!fbdev || !fbdev->helper.fb)
696 		return NULL;
697 
698 	return to_intel_framebuffer(fbdev->helper.fb);
699 }
700