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