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