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 
48 #include "i915_drv.h"
49 #include "intel_display_types.h"
50 #include "intel_fb.h"
51 #include "intel_fb_pin.h"
52 #include "intel_fbdev.h"
53 #include "intel_fbdev_fb.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 	int preferred_bpp;
62 
63 	/* Whether or not fbdev hpd processing is temporarily suspended */
64 	bool hpd_suspended: 1;
65 	/* Set when a hotplug was received while HPD processing was suspended */
66 	bool hpd_waiting: 1;
67 
68 	/* Protects hpd_suspended */
69 	struct mutex hpd_lock;
70 };
71 
72 static struct intel_fbdev *to_intel_fbdev(struct drm_fb_helper *fb_helper)
73 {
74 	return container_of(fb_helper, struct intel_fbdev, helper);
75 }
76 
77 static struct intel_frontbuffer *to_frontbuffer(struct intel_fbdev *ifbdev)
78 {
79 	return ifbdev->fb->frontbuffer;
80 }
81 
82 static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev)
83 {
84 	intel_frontbuffer_invalidate(to_frontbuffer(ifbdev), ORIGIN_CPU);
85 }
86 
87 FB_GEN_DEFAULT_DEFERRED_IOMEM_OPS(intel_fbdev,
88 				  drm_fb_helper_damage_range,
89 				  drm_fb_helper_damage_area)
90 
91 static int intel_fbdev_set_par(struct fb_info *info)
92 {
93 	struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
94 	int ret;
95 
96 	ret = drm_fb_helper_set_par(info);
97 	if (ret == 0)
98 		intel_fbdev_invalidate(ifbdev);
99 
100 	return ret;
101 }
102 
103 static int intel_fbdev_blank(int blank, struct fb_info *info)
104 {
105 	struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
106 	int ret;
107 
108 	ret = drm_fb_helper_blank(blank, info);
109 	if (ret == 0)
110 		intel_fbdev_invalidate(ifbdev);
111 
112 	return ret;
113 }
114 
115 static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
116 				   struct fb_info *info)
117 {
118 	struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
119 	int ret;
120 
121 	ret = drm_fb_helper_pan_display(var, info);
122 	if (ret == 0)
123 		intel_fbdev_invalidate(ifbdev);
124 
125 	return ret;
126 }
127 
128 static int intel_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma)
129 {
130 	struct intel_fbdev *fbdev = to_intel_fbdev(info->par);
131 	struct drm_gem_object *bo = drm_gem_fb_get_obj(&fbdev->fb->base, 0);
132 	struct drm_i915_gem_object *obj = to_intel_bo(bo);
133 
134 	return i915_gem_fb_mmap(obj, vma);
135 }
136 
137 static void intel_fbdev_fb_destroy(struct fb_info *info)
138 {
139 	struct drm_fb_helper *fb_helper = info->par;
140 	struct intel_fbdev *ifbdev = container_of(fb_helper, struct intel_fbdev, helper);
141 
142 	drm_fb_helper_fini(&ifbdev->helper);
143 
144 	/*
145 	 * We rely on the object-free to release the VMA pinning for
146 	 * the info->screen_base mmaping. Leaking the VMA is simpler than
147 	 * trying to rectify all the possible error paths leading here.
148 	 */
149 	intel_fb_unpin_vma(ifbdev->vma, ifbdev->vma_flags);
150 	drm_framebuffer_remove(&ifbdev->fb->base);
151 
152 	drm_client_release(&fb_helper->client);
153 	drm_fb_helper_unprepare(&ifbdev->helper);
154 	kfree(ifbdev);
155 }
156 
157 __diag_push();
158 __diag_ignore_all("-Woverride-init", "Allow field initialization overrides for fb ops");
159 
160 static const struct fb_ops intelfb_ops = {
161 	.owner = THIS_MODULE,
162 	__FB_DEFAULT_DEFERRED_OPS_RDWR(intel_fbdev),
163 	DRM_FB_HELPER_DEFAULT_OPS,
164 	.fb_set_par = intel_fbdev_set_par,
165 	.fb_blank = intel_fbdev_blank,
166 	.fb_pan_display = intel_fbdev_pan_display,
167 	__FB_DEFAULT_DEFERRED_OPS_DRAW(intel_fbdev),
168 	.fb_mmap = intel_fbdev_mmap,
169 	.fb_destroy = intel_fbdev_fb_destroy,
170 };
171 
172 __diag_pop();
173 
174 static int intelfb_create(struct drm_fb_helper *helper,
175 			  struct drm_fb_helper_surface_size *sizes)
176 {
177 	struct intel_fbdev *ifbdev = to_intel_fbdev(helper);
178 	struct intel_framebuffer *fb = ifbdev->fb;
179 	struct drm_device *dev = helper->dev;
180 	struct drm_i915_private *dev_priv = to_i915(dev);
181 	const struct i915_gtt_view view = {
182 		.type = I915_GTT_VIEW_NORMAL,
183 	};
184 	intel_wakeref_t wakeref;
185 	struct fb_info *info;
186 	struct i915_vma *vma;
187 	unsigned long flags = 0;
188 	bool prealloc = false;
189 	struct drm_i915_gem_object *obj;
190 	int ret;
191 
192 	mutex_lock(&ifbdev->hpd_lock);
193 	ret = ifbdev->hpd_suspended ? -EAGAIN : 0;
194 	mutex_unlock(&ifbdev->hpd_lock);
195 	if (ret)
196 		return ret;
197 
198 	ifbdev->fb = NULL;
199 
200 	if (fb &&
201 	    (sizes->fb_width > fb->base.width ||
202 	     sizes->fb_height > fb->base.height)) {
203 		drm_dbg_kms(&dev_priv->drm,
204 			    "BIOS fb too small (%dx%d), we require (%dx%d),"
205 			    " releasing it\n",
206 			    fb->base.width, fb->base.height,
207 			    sizes->fb_width, sizes->fb_height);
208 		drm_framebuffer_put(&fb->base);
209 		fb = NULL;
210 	}
211 	if (!fb || drm_WARN_ON(dev, !intel_fb_obj(&fb->base))) {
212 		drm_dbg_kms(&dev_priv->drm,
213 			    "no BIOS fb, allocating a new one\n");
214 		fb = intel_fbdev_fb_alloc(helper, sizes);
215 		if (IS_ERR(fb))
216 			return PTR_ERR(fb);
217 	} else {
218 		drm_dbg_kms(&dev_priv->drm, "re-using BIOS fb\n");
219 		prealloc = true;
220 		sizes->fb_width = fb->base.width;
221 		sizes->fb_height = fb->base.height;
222 	}
223 
224 	wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
225 
226 	/* Pin the GGTT vma for our access via info->screen_base.
227 	 * This also validates that any existing fb inherited from the
228 	 * BIOS is suitable for own access.
229 	 */
230 	vma = intel_fb_pin_to_ggtt(&fb->base, false,
231 				   &view, false, &flags);
232 	if (IS_ERR(vma)) {
233 		ret = PTR_ERR(vma);
234 		goto out_unlock;
235 	}
236 
237 	info = drm_fb_helper_alloc_info(helper);
238 	if (IS_ERR(info)) {
239 		drm_err(&dev_priv->drm, "Failed to allocate fb_info (%pe)\n", info);
240 		ret = PTR_ERR(info);
241 		goto out_unpin;
242 	}
243 
244 	ifbdev->helper.fb = &fb->base;
245 
246 	info->fbops = &intelfb_ops;
247 
248 	obj = intel_fb_obj(&fb->base);
249 
250 	ret = intel_fbdev_fb_fill_info(dev_priv, info, obj, vma);
251 	if (ret)
252 		goto out_unpin;
253 
254 	drm_fb_helper_fill_info(info, &ifbdev->helper, sizes);
255 
256 	/* If the object is shmemfs backed, it will have given us zeroed pages.
257 	 * If the object is stolen however, it will be full of whatever
258 	 * garbage was left in there.
259 	 */
260 	if (!i915_gem_object_is_shmem(obj) && !prealloc)
261 		memset_io(info->screen_base, 0, info->screen_size);
262 
263 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
264 
265 	drm_dbg_kms(&dev_priv->drm, "allocated %dx%d fb: 0x%08x\n",
266 		    fb->base.width, fb->base.height,
267 		    i915_ggtt_offset(vma));
268 	ifbdev->fb = fb;
269 	ifbdev->vma = vma;
270 	ifbdev->vma_flags = flags;
271 
272 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
273 
274 	return 0;
275 
276 out_unpin:
277 	intel_fb_unpin_vma(vma, flags);
278 out_unlock:
279 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
280 	return ret;
281 }
282 
283 static int intelfb_dirty(struct drm_fb_helper *helper, struct drm_clip_rect *clip)
284 {
285 	if (!(clip->x1 < clip->x2 && clip->y1 < clip->y2))
286 		return 0;
287 
288 	if (helper->fb->funcs->dirty)
289 		return helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, clip, 1);
290 
291 	return 0;
292 }
293 
294 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
295 	.fb_probe = intelfb_create,
296 	.fb_dirty = intelfb_dirty,
297 };
298 
299 /*
300  * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
301  * The core display code will have read out the current plane configuration,
302  * so we use that to figure out if there's an object for us to use as the
303  * fb, and if so, we re-use it for the fbdev configuration.
304  *
305  * Note we only support a single fb shared across pipes for boot (mostly for
306  * fbcon), so we just find the biggest and use that.
307  */
308 static bool intel_fbdev_init_bios(struct drm_device *dev,
309 				  struct intel_fbdev *ifbdev)
310 {
311 	struct drm_i915_private *i915 = to_i915(dev);
312 	struct intel_framebuffer *fb = NULL;
313 	struct intel_crtc *crtc;
314 	unsigned int max_size = 0;
315 
316 	/* Find the largest fb */
317 	for_each_intel_crtc(dev, crtc) {
318 		struct intel_crtc_state *crtc_state =
319 			to_intel_crtc_state(crtc->base.state);
320 		struct intel_plane *plane =
321 			to_intel_plane(crtc->base.primary);
322 		struct intel_plane_state *plane_state =
323 			to_intel_plane_state(plane->base.state);
324 		struct drm_i915_gem_object *obj =
325 			intel_fb_obj(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 (intel_bo_to_drm_bo(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 = intel_bo_to_drm_bo(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 	    !i915_gem_object_is_shmem(intel_fb_obj(&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