Merge tag 'drm-intel-next-2023-03-07' of git://anongit.freedesktop.org/drm/drm-intel...
[linux-2.6-microblaze.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_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
136 static 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->dsm.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
193 static 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         obj = intel_fb_obj(&intel_fb->base);
271         if (i915_gem_object_is_lmem(obj)) {
272                 struct intel_memory_region *mem = obj->mm.region;
273
274                 /* Use fbdev's framebuffer from lmem for discrete */
275                 info->fix.smem_start =
276                         (unsigned long)(mem->io_start +
277                                         i915_gem_object_get_dma_address(obj, 0));
278                 info->fix.smem_len = obj->base.size;
279         } else {
280                 /* Our framebuffer is the entirety of fbdev's system memory */
281                 info->fix.smem_start =
282                         (unsigned long)(ggtt->gmadr.start + i915_ggtt_offset(vma));
283                 info->fix.smem_len = vma->size;
284         }
285
286         vaddr = i915_vma_pin_iomap(vma);
287         if (IS_ERR(vaddr)) {
288                 drm_err(&dev_priv->drm,
289                         "Failed to remap framebuffer into virtual memory (%pe)\n", vaddr);
290                 ret = PTR_ERR(vaddr);
291                 goto out_unpin;
292         }
293         info->screen_base = vaddr;
294         info->screen_size = vma->size;
295
296         drm_fb_helper_fill_info(info, &ifbdev->helper, sizes);
297
298         /* If the object is shmemfs backed, it will have given us zeroed pages.
299          * If the object is stolen however, it will be full of whatever
300          * garbage was left in there.
301          */
302         if (!i915_gem_object_is_shmem(vma->obj) && !prealloc)
303                 memset_io(info->screen_base, 0, info->screen_size);
304
305         /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
306
307         drm_dbg_kms(&dev_priv->drm, "allocated %dx%d fb: 0x%08x\n",
308                     ifbdev->fb->base.width, ifbdev->fb->base.height,
309                     i915_ggtt_offset(vma));
310         ifbdev->vma = vma;
311         ifbdev->vma_flags = flags;
312
313         intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
314         vga_switcheroo_client_fb_set(pdev, info);
315         return 0;
316
317 out_unpin:
318         intel_unpin_fb_vma(vma, flags);
319 out_unlock:
320         intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
321         return ret;
322 }
323
324 static int intelfb_dirty(struct drm_fb_helper *helper, struct drm_clip_rect *clip)
325 {
326         if (!(clip->x1 < clip->x2 && clip->y1 < clip->y2))
327                 return 0;
328
329         if (helper->fb->funcs->dirty)
330                 return helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, clip, 1);
331
332         return 0;
333 }
334
335 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
336         .fb_probe = intelfb_create,
337         .fb_dirty = intelfb_dirty,
338 };
339
340 static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
341 {
342         /* We rely on the object-free to release the VMA pinning for
343          * the info->screen_base mmaping. Leaking the VMA is simpler than
344          * trying to rectify all the possible error paths leading here.
345          */
346
347         drm_fb_helper_fini(&ifbdev->helper);
348
349         if (ifbdev->vma)
350                 intel_unpin_fb_vma(ifbdev->vma, ifbdev->vma_flags);
351
352         if (ifbdev->fb)
353                 drm_framebuffer_remove(&ifbdev->fb->base);
354
355         drm_fb_helper_unprepare(&ifbdev->helper);
356         kfree(ifbdev);
357 }
358
359 /*
360  * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
361  * The core display code will have read out the current plane configuration,
362  * so we use that to figure out if there's an object for us to use as the
363  * fb, and if so, we re-use it for the fbdev configuration.
364  *
365  * Note we only support a single fb shared across pipes for boot (mostly for
366  * fbcon), so we just find the biggest and use that.
367  */
368 static bool intel_fbdev_init_bios(struct drm_device *dev,
369                                   struct intel_fbdev *ifbdev)
370 {
371         struct drm_i915_private *i915 = to_i915(dev);
372         struct intel_framebuffer *fb = NULL;
373         struct intel_crtc *crtc;
374         unsigned int max_size = 0;
375
376         /* Find the largest fb */
377         for_each_intel_crtc(dev, crtc) {
378                 struct intel_crtc_state *crtc_state =
379                         to_intel_crtc_state(crtc->base.state);
380                 struct intel_plane *plane =
381                         to_intel_plane(crtc->base.primary);
382                 struct intel_plane_state *plane_state =
383                         to_intel_plane_state(plane->base.state);
384                 struct drm_i915_gem_object *obj =
385                         intel_fb_obj(plane_state->uapi.fb);
386
387                 if (!crtc_state->uapi.active) {
388                         drm_dbg_kms(&i915->drm,
389                                     "[CRTC:%d:%s] not active, skipping\n",
390                                     crtc->base.base.id, crtc->base.name);
391                         continue;
392                 }
393
394                 if (!obj) {
395                         drm_dbg_kms(&i915->drm,
396                                     "[PLANE:%d:%s] no fb, skipping\n",
397                                     plane->base.base.id, plane->base.name);
398                         continue;
399                 }
400
401                 if (obj->base.size > max_size) {
402                         drm_dbg_kms(&i915->drm,
403                                     "found possible fb from [PLANE:%d:%s]\n",
404                                     plane->base.base.id, plane->base.name);
405                         fb = to_intel_framebuffer(plane_state->uapi.fb);
406                         max_size = obj->base.size;
407                 }
408         }
409
410         if (!fb) {
411                 drm_dbg_kms(&i915->drm,
412                             "no active fbs found, not using BIOS config\n");
413                 goto out;
414         }
415
416         /* Now make sure all the pipes will fit into it */
417         for_each_intel_crtc(dev, crtc) {
418                 struct intel_crtc_state *crtc_state =
419                         to_intel_crtc_state(crtc->base.state);
420                 struct intel_plane *plane =
421                         to_intel_plane(crtc->base.primary);
422                 unsigned int cur_size;
423
424                 if (!crtc_state->uapi.active) {
425                         drm_dbg_kms(&i915->drm,
426                                     "[CRTC:%d:%s] not active, skipping\n",
427                                     crtc->base.base.id, crtc->base.name);
428                         continue;
429                 }
430
431                 drm_dbg_kms(&i915->drm, "checking [PLANE:%d:%s] for BIOS fb\n",
432                             plane->base.base.id, plane->base.name);
433
434                 /*
435                  * See if the plane fb we found above will fit on this
436                  * pipe.  Note we need to use the selected fb's pitch and bpp
437                  * rather than the current pipe's, since they differ.
438                  */
439                 cur_size = crtc_state->uapi.adjusted_mode.crtc_hdisplay;
440                 cur_size = cur_size * fb->base.format->cpp[0];
441                 if (fb->base.pitches[0] < cur_size) {
442                         drm_dbg_kms(&i915->drm,
443                                     "fb not wide enough for [PLANE:%d:%s] (%d vs %d)\n",
444                                     plane->base.base.id, plane->base.name,
445                                     cur_size, fb->base.pitches[0]);
446                         fb = NULL;
447                         break;
448                 }
449
450                 cur_size = crtc_state->uapi.adjusted_mode.crtc_vdisplay;
451                 cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
452                 cur_size *= fb->base.pitches[0];
453                 drm_dbg_kms(&i915->drm,
454                             "[CRTC:%d:%s] area: %dx%d, bpp: %d, size: %d\n",
455                             crtc->base.base.id, crtc->base.name,
456                             crtc_state->uapi.adjusted_mode.crtc_hdisplay,
457                             crtc_state->uapi.adjusted_mode.crtc_vdisplay,
458                             fb->base.format->cpp[0] * 8,
459                             cur_size);
460
461                 if (cur_size > max_size) {
462                         drm_dbg_kms(&i915->drm,
463                                     "fb not big enough for [PLANE:%d:%s] (%d vs %d)\n",
464                                     plane->base.base.id, plane->base.name,
465                                     cur_size, max_size);
466                         fb = NULL;
467                         break;
468                 }
469
470                 drm_dbg_kms(&i915->drm,
471                             "fb big enough [PLANE:%d:%s] (%d >= %d)\n",
472                             plane->base.base.id, plane->base.name,
473                             max_size, cur_size);
474         }
475
476         if (!fb) {
477                 drm_dbg_kms(&i915->drm,
478                             "BIOS fb not suitable for all pipes, not using\n");
479                 goto out;
480         }
481
482         ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
483         ifbdev->fb = fb;
484
485         drm_framebuffer_get(&ifbdev->fb->base);
486
487         /* Final pass to check if any active pipes don't have fbs */
488         for_each_intel_crtc(dev, crtc) {
489                 struct intel_crtc_state *crtc_state =
490                         to_intel_crtc_state(crtc->base.state);
491                 struct intel_plane *plane =
492                         to_intel_plane(crtc->base.primary);
493                 struct intel_plane_state *plane_state =
494                         to_intel_plane_state(plane->base.state);
495
496                 if (!crtc_state->uapi.active)
497                         continue;
498
499                 drm_WARN(dev, !plane_state->uapi.fb,
500                          "re-used BIOS config but lost an fb on [PLANE:%d:%s]\n",
501                          plane->base.base.id, plane->base.name);
502         }
503
504
505         drm_dbg_kms(&i915->drm, "using BIOS fb for initial console\n");
506         return true;
507
508 out:
509
510         return false;
511 }
512
513 static void intel_fbdev_suspend_worker(struct work_struct *work)
514 {
515         intel_fbdev_set_suspend(&container_of(work,
516                                               struct drm_i915_private,
517                                               display.fbdev.suspend_work)->drm,
518                                 FBINFO_STATE_RUNNING,
519                                 true);
520 }
521
522 int intel_fbdev_init(struct drm_device *dev)
523 {
524         struct drm_i915_private *dev_priv = to_i915(dev);
525         struct intel_fbdev *ifbdev;
526         int ret;
527
528         if (drm_WARN_ON(dev, !HAS_DISPLAY(dev_priv)))
529                 return -ENODEV;
530
531         ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
532         if (ifbdev == NULL)
533                 return -ENOMEM;
534
535         mutex_init(&ifbdev->hpd_lock);
536         drm_fb_helper_prepare(dev, &ifbdev->helper, 32, &intel_fb_helper_funcs);
537
538         if (intel_fbdev_init_bios(dev, ifbdev))
539                 ifbdev->helper.preferred_bpp = ifbdev->preferred_bpp;
540         else
541                 ifbdev->preferred_bpp = ifbdev->helper.preferred_bpp;
542
543         ret = drm_fb_helper_init(dev, &ifbdev->helper);
544         if (ret) {
545                 kfree(ifbdev);
546                 return ret;
547         }
548
549         dev_priv->display.fbdev.fbdev = ifbdev;
550         INIT_WORK(&dev_priv->display.fbdev.suspend_work, intel_fbdev_suspend_worker);
551
552         return 0;
553 }
554
555 static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
556 {
557         struct intel_fbdev *ifbdev = data;
558
559         /* Due to peculiar init order wrt to hpd handling this is separate. */
560         if (drm_fb_helper_initial_config(&ifbdev->helper))
561                 intel_fbdev_unregister(to_i915(ifbdev->helper.dev));
562 }
563
564 void intel_fbdev_initial_config_async(struct drm_i915_private *dev_priv)
565 {
566         struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
567
568         if (!ifbdev)
569                 return;
570
571         ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
572 }
573
574 static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
575 {
576         if (!ifbdev->cookie)
577                 return;
578
579         /* Only serialises with all preceding async calls, hence +1 */
580         async_synchronize_cookie(ifbdev->cookie + 1);
581         ifbdev->cookie = 0;
582 }
583
584 void intel_fbdev_unregister(struct drm_i915_private *dev_priv)
585 {
586         struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
587
588         if (!ifbdev)
589                 return;
590
591         intel_fbdev_set_suspend(&dev_priv->drm, FBINFO_STATE_SUSPENDED, true);
592
593         if (!current_is_async())
594                 intel_fbdev_sync(ifbdev);
595
596         drm_fb_helper_unregister_info(&ifbdev->helper);
597 }
598
599 void intel_fbdev_fini(struct drm_i915_private *dev_priv)
600 {
601         struct intel_fbdev *ifbdev = fetch_and_zero(&dev_priv->display.fbdev.fbdev);
602
603         if (!ifbdev)
604                 return;
605
606         intel_fbdev_destroy(ifbdev);
607 }
608
609 /* Suspends/resumes fbdev processing of incoming HPD events. When resuming HPD
610  * processing, fbdev will perform a full connector reprobe if a hotplug event
611  * was received while HPD was suspended.
612  */
613 static void intel_fbdev_hpd_set_suspend(struct drm_i915_private *i915, int state)
614 {
615         struct intel_fbdev *ifbdev = i915->display.fbdev.fbdev;
616         bool send_hpd = false;
617
618         mutex_lock(&ifbdev->hpd_lock);
619         ifbdev->hpd_suspended = state == FBINFO_STATE_SUSPENDED;
620         send_hpd = !ifbdev->hpd_suspended && ifbdev->hpd_waiting;
621         ifbdev->hpd_waiting = false;
622         mutex_unlock(&ifbdev->hpd_lock);
623
624         if (send_hpd) {
625                 drm_dbg_kms(&i915->drm, "Handling delayed fbcon HPD event\n");
626                 drm_fb_helper_hotplug_event(&ifbdev->helper);
627         }
628 }
629
630 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
631 {
632         struct drm_i915_private *dev_priv = to_i915(dev);
633         struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
634         struct fb_info *info;
635
636         if (!ifbdev)
637                 return;
638
639         if (drm_WARN_ON(&dev_priv->drm, !HAS_DISPLAY(dev_priv)))
640                 return;
641
642         if (!ifbdev->vma)
643                 goto set_suspend;
644
645         info = ifbdev->helper.info;
646
647         if (synchronous) {
648                 /* Flush any pending work to turn the console on, and then
649                  * wait to turn it off. It must be synchronous as we are
650                  * about to suspend or unload the driver.
651                  *
652                  * Note that from within the work-handler, we cannot flush
653                  * ourselves, so only flush outstanding work upon suspend!
654                  */
655                 if (state != FBINFO_STATE_RUNNING)
656                         flush_work(&dev_priv->display.fbdev.suspend_work);
657
658                 console_lock();
659         } else {
660                 /*
661                  * The console lock can be pretty contented on resume due
662                  * to all the printk activity.  Try to keep it out of the hot
663                  * path of resume if possible.
664                  */
665                 drm_WARN_ON(dev, state != FBINFO_STATE_RUNNING);
666                 if (!console_trylock()) {
667                         /* Don't block our own workqueue as this can
668                          * be run in parallel with other i915.ko tasks.
669                          */
670                         schedule_work(&dev_priv->display.fbdev.suspend_work);
671                         return;
672                 }
673         }
674
675         /* On resume from hibernation: If the object is shmemfs backed, it has
676          * been restored from swap. If the object is stolen however, it will be
677          * full of whatever garbage was left in there.
678          */
679         if (state == FBINFO_STATE_RUNNING &&
680             !i915_gem_object_is_shmem(intel_fb_obj(&ifbdev->fb->base)))
681                 memset_io(info->screen_base, 0, info->screen_size);
682
683         drm_fb_helper_set_suspend(&ifbdev->helper, state);
684         console_unlock();
685
686 set_suspend:
687         intel_fbdev_hpd_set_suspend(dev_priv, state);
688 }
689
690 void intel_fbdev_output_poll_changed(struct drm_device *dev)
691 {
692         struct intel_fbdev *ifbdev = to_i915(dev)->display.fbdev.fbdev;
693         bool send_hpd;
694
695         if (!ifbdev)
696                 return;
697
698         intel_fbdev_sync(ifbdev);
699
700         mutex_lock(&ifbdev->hpd_lock);
701         send_hpd = !ifbdev->hpd_suspended;
702         ifbdev->hpd_waiting = true;
703         mutex_unlock(&ifbdev->hpd_lock);
704
705         if (send_hpd && (ifbdev->vma || ifbdev->helper.deferred_setup))
706                 drm_fb_helper_hotplug_event(&ifbdev->helper);
707 }
708
709 void intel_fbdev_restore_mode(struct drm_i915_private *dev_priv)
710 {
711         struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
712
713         if (!ifbdev)
714                 return;
715
716         intel_fbdev_sync(ifbdev);
717         if (!ifbdev->vma)
718                 return;
719
720         if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper) == 0)
721                 intel_fbdev_invalidate(ifbdev);
722 }
723
724 struct intel_framebuffer *intel_fbdev_framebuffer(struct intel_fbdev *fbdev)
725 {
726         if (!fbdev || !fbdev->helper.fb)
727                 return NULL;
728
729         return to_intel_framebuffer(fbdev->helper.fb);
730 }