]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/gpu/drm/i915/display/intel_fbdev.c
drm/i915/fbdev: Restore physical addresses for fb_mmap()
[mirror_ubuntu-jammy-kernel.git] / drivers / gpu / drm / i915 / display / intel_fbdev.c
CommitLineData
79e53945
JB
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
d1d70677 27#include <linux/async.h>
82e3b8c1 28#include <linux/console.h>
6dfccb95 29#include <linux/delay.h>
79e53945 30#include <linux/errno.h>
6dfccb95
JN
31#include <linux/init.h>
32#include <linux/kernel.h>
79e53945 33#include <linux/mm.h>
6dfccb95
JN
34#include <linux/module.h>
35#include <linux/string.h>
79e53945 36#include <linux/sysrq.h>
6dfccb95 37#include <linux/tty.h>
6a9ee8af 38#include <linux/vga_switcheroo.h>
79e53945 39
760285e7
DH
40#include <drm/drm_crtc.h>
41#include <drm/drm_fb_helper.h>
fcd70cd3 42#include <drm/drm_fourcc.h>
6dfccb95 43#include <drm/i915_drm.h>
fcd70cd3 44
6dfccb95 45#include "i915_drv.h"
1d455f8d 46#include "intel_display_types.h"
6dfccb95 47#include "intel_fbdev.h"
5d723d7a 48#include "intel_frontbuffer.h"
79e53945 49
8e7cb179 50static struct intel_frontbuffer *to_frontbuffer(struct intel_fbdev *ifbdev)
fabef825 51{
8e7cb179
CW
52 return ifbdev->fb->frontbuffer;
53}
fabef825 54
8e7cb179
CW
55static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev)
56{
57 intel_frontbuffer_invalidate(to_frontbuffer(ifbdev), ORIGIN_CPU);
fabef825
CW
58}
59
e991077e
DV
60static int intel_fbdev_set_par(struct fb_info *info)
61{
62 struct drm_fb_helper *fb_helper = info->par;
63 struct intel_fbdev *ifbdev =
64 container_of(fb_helper, struct intel_fbdev, helper);
65 int ret;
66
67 ret = drm_fb_helper_set_par(info);
fabef825
CW
68 if (ret == 0)
69 intel_fbdev_invalidate(ifbdev);
e991077e
DV
70
71 return ret;
72}
73
03e515f7
RV
74static int intel_fbdev_blank(int blank, struct fb_info *info)
75{
76 struct drm_fb_helper *fb_helper = info->par;
77 struct intel_fbdev *ifbdev =
78 container_of(fb_helper, struct intel_fbdev, helper);
79 int ret;
80
81 ret = drm_fb_helper_blank(blank, info);
fabef825
CW
82 if (ret == 0)
83 intel_fbdev_invalidate(ifbdev);
03e515f7
RV
84
85 return ret;
86}
87
d9a946b5
RV
88static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
89 struct fb_info *info)
90{
91 struct drm_fb_helper *fb_helper = info->par;
92 struct intel_fbdev *ifbdev =
93 container_of(fb_helper, struct intel_fbdev, helper);
d9a946b5 94 int ret;
d9a946b5 95
fabef825
CW
96 ret = drm_fb_helper_pan_display(var, info);
97 if (ret == 0)
98 intel_fbdev_invalidate(ifbdev);
d9a946b5
RV
99
100 return ret;
101}
102
79e53945
JB
103static struct fb_ops intelfb_ops = {
104 .owner = THIS_MODULE,
a36384dd 105 DRM_FB_HELPER_DEFAULT_OPS,
e991077e 106 .fb_set_par = intel_fbdev_set_par,
21cff148
AT
107 .fb_fillrect = drm_fb_helper_cfb_fillrect,
108 .fb_copyarea = drm_fb_helper_cfb_copyarea,
109 .fb_imageblit = drm_fb_helper_cfb_imageblit,
d9a946b5 110 .fb_pan_display = intel_fbdev_pan_display,
03e515f7 111 .fb_blank = intel_fbdev_blank,
79e53945
JB
112};
113
8b4f49e0
JB
114static int intelfb_alloc(struct drm_fb_helper *helper,
115 struct drm_fb_helper_surface_size *sizes)
79e53945 116{
3430824b
VS
117 struct intel_fbdev *ifbdev =
118 container_of(helper, struct intel_fbdev, helper);
4601b933 119 struct drm_framebuffer *fb;
3430824b 120 struct drm_device *dev = helper->dev;
3badb49f 121 struct drm_i915_private *dev_priv = to_i915(dev);
3a7f2f6a 122 struct drm_mode_fb_cmd2 mode_cmd = {};
24dbf51a 123 struct drm_i915_gem_object *obj;
8e7cb179 124 int size;
79e53945 125
b8c00ac5 126 /* we don't do packed 24bpp */
38651674
DA
127 if (sizes->surface_bpp == 24)
128 sizes->surface_bpp = 32;
b8c00ac5 129
38651674
DA
130 mode_cmd.width = sizes->surface_width;
131 mode_cmd.height = sizes->surface_height;
79e53945 132
de45eaf7
PZ
133 mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
134 DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
308e5bcb
JB
135 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
136 sizes->surface_depth);
79e53945 137
308e5bcb 138 size = mode_cmd.pitches[0] * mode_cmd.height;
1267a26b 139 size = PAGE_ALIGN(size);
3badb49f
PZ
140
141 /* If the FB is too big, just don't use it since fbdev is not very
142 * important and we should probably use that space with FBC or other
143 * features. */
24dbf51a 144 obj = NULL;
b1ace601 145 if (size * 2 < dev_priv->stolen_usable_size)
187685cb 146 obj = i915_gem_object_create_stolen(dev_priv, size);
0ffb0ff2 147 if (obj == NULL)
8475355f 148 obj = i915_gem_object_create_shmem(dev_priv, size);
fe3db79b 149 if (IS_ERR(obj)) {
1ae8c0a5 150 DRM_ERROR("failed to allocate framebuffer\n");
8e7cb179 151 return PTR_ERR(obj);
79e53945 152 }
79e53945 153
24dbf51a 154 fb = intel_framebuffer_create(obj, &mode_cmd);
8e7cb179
CW
155 i915_gem_object_put(obj);
156 if (IS_ERR(fb))
157 return PTR_ERR(fb);
850c4cdc 158
a8bb6818 159 ifbdev->fb = to_intel_framebuffer(fb);
8b4f49e0 160 return 0;
8b4f49e0
JB
161}
162
163static int intelfb_create(struct drm_fb_helper *helper,
164 struct drm_fb_helper_surface_size *sizes)
165{
166 struct intel_fbdev *ifbdev =
167 container_of(helper, struct intel_fbdev, helper);
8bcd4553 168 struct intel_framebuffer *intel_fb = ifbdev->fb;
8b4f49e0 169 struct drm_device *dev = helper->dev;
72e96d64 170 struct drm_i915_private *dev_priv = to_i915(dev);
52a05c30 171 struct pci_dev *pdev = dev_priv->drm.pdev;
72e96d64 172 struct i915_ggtt *ggtt = &dev_priv->ggtt;
f5929c53
VS
173 const struct i915_ggtt_view view = {
174 .type = I915_GGTT_VIEW_NORMAL,
175 };
1d264d91
CW
176 intel_wakeref_t wakeref;
177 struct fb_info *info;
8ef8561f 178 struct i915_vma *vma;
5935485f 179 unsigned long flags = 0;
d978ef14 180 bool prealloc = false;
406ea8d2 181 void __iomem *vaddr;
8ef8561f 182 int ret;
8b4f49e0 183
edd586fe
CW
184 if (intel_fb &&
185 (sizes->fb_width > intel_fb->base.width ||
186 sizes->fb_height > intel_fb->base.height)) {
187 DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d),"
188 " releasing it\n",
189 intel_fb->base.width, intel_fb->base.height,
190 sizes->fb_width, sizes->fb_height);
c3ed1103 191 drm_framebuffer_put(&intel_fb->base);
edd586fe
CW
192 intel_fb = ifbdev->fb = NULL;
193 }
a5ff7a45 194 if (!intel_fb || WARN_ON(!intel_fb_obj(&intel_fb->base))) {
48e92120 195 DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
8b4f49e0
JB
196 ret = intelfb_alloc(helper, sizes);
197 if (ret)
51f1385b 198 return ret;
8bcd4553 199 intel_fb = ifbdev->fb;
8b4f49e0 200 } else {
48e92120 201 DRM_DEBUG_KMS("re-using BIOS fb\n");
d978ef14 202 prealloc = true;
8b4f49e0
JB
203 sizes->fb_width = intel_fb->base.width;
204 sizes->fb_height = intel_fb->base.height;
205 }
206
51f1385b 207 mutex_lock(&dev->struct_mutex);
d858d569 208 wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
51f1385b 209
0c82312f
CW
210 /* Pin the GGTT vma for our access via info->screen_base.
211 * This also validates that any existing fb inherited from the
212 * BIOS is suitable for own access.
213 */
5935485f 214 vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base,
f5929c53 215 &view, false, &flags);
058d88c4
CW
216 if (IS_ERR(vma)) {
217 ret = PTR_ERR(vma);
0c82312f 218 goto out_unlock;
058d88c4 219 }
0c82312f 220
8e7cb179 221 intel_frontbuffer_flush(to_frontbuffer(ifbdev), ORIGIN_DIRTYFB);
07bcd99b 222
21cff148
AT
223 info = drm_fb_helper_alloc_fbi(helper);
224 if (IS_ERR(info)) {
366e39b4 225 DRM_ERROR("Failed to allocate fb_info\n");
21cff148 226 ret = PTR_ERR(info);
b4476f52 227 goto out_unpin;
79e53945
JB
228 }
229
8e7cb179 230 ifbdev->helper.fb = &ifbdev->fb->base;
785b93ef 231
79e53945
JB
232 info->fbops = &intelfb_ops;
233
4410f391 234 /* setup aperture base/size for vesafb takeover */
5f889b9a 235 info->apertures->ranges[0].base = ggtt->gmadr.start;
72e96d64 236 info->apertures->ranges[0].size = ggtt->mappable_end;
4410f391 237
71d12262
CW
238 /* Our framebuffer is the entirety of fbdev's system memory */
239 info->fix.smem_start =
240 (unsigned long)(ggtt->gmadr.start + vma->node.start);
241 info->fix.smem_len = vma->node.size;
242
8ef8561f
CW
243 vaddr = i915_vma_pin_iomap(vma);
244 if (IS_ERR(vaddr)) {
366e39b4 245 DRM_ERROR("Failed to remap framebuffer into virtual memory\n");
8ef8561f 246 ret = PTR_ERR(vaddr);
da7bdda2 247 goto out_unpin;
79e53945 248 }
8ef8561f
CW
249 info->screen_base = vaddr;
250 info->screen_size = vma->node.size;
79e53945 251
7a0f9ef9 252 drm_fb_helper_fill_info(info, &ifbdev->helper, sizes);
79e53945 253
88afe715
CW
254 /* If the object is shmemfs backed, it will have given us zeroed pages.
255 * If the object is stolen however, it will be full of whatever
256 * garbage was left in there.
257 */
8e7cb179 258 if (vma->obj->stolen && !prealloc)
88afe715
CW
259 memset_io(info->screen_base, 0, info->screen_size);
260
fb2a99e1 261 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
79e53945 262
bde13ebd 263 DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08x\n",
8e7cb179
CW
264 ifbdev->fb->base.width, ifbdev->fb->base.height,
265 i915_ggtt_offset(vma));
058d88c4 266 ifbdev->vma = vma;
5935485f 267 ifbdev->vma_flags = flags;
79e53945 268
d858d569 269 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
79e53945 270 mutex_unlock(&dev->struct_mutex);
52a05c30 271 vga_switcheroo_client_fb_set(pdev, info);
79e53945
JB
272 return 0;
273
b4476f52 274out_unpin:
5935485f 275 intel_unpin_fb_vma(vma, flags);
0c82312f 276out_unlock:
d858d569 277 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
79e53945 278 mutex_unlock(&dev->struct_mutex);
79e53945
JB
279 return ret;
280}
281
3a493879 282static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
cd5428a5 283 .fb_probe = intelfb_create,
4abe3520 284};
79e53945 285
43cee314 286static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
79e53945 287{
0c82312f
CW
288 /* We rely on the object-free to release the VMA pinning for
289 * the info->screen_base mmaping. Leaking the VMA is simpler than
290 * trying to rectify all the possible error paths leading here.
291 */
ddfe1567 292
4abe3520 293 drm_fb_helper_fini(&ifbdev->helper);
79e53945 294
15727ed0 295 if (ifbdev->vma) {
43cee314 296 mutex_lock(&ifbdev->helper.dev->struct_mutex);
5935485f 297 intel_unpin_fb_vma(ifbdev->vma, ifbdev->vma_flags);
43cee314 298 mutex_unlock(&ifbdev->helper.dev->struct_mutex);
15727ed0 299 }
fb4b8ce1 300
15727ed0 301 if (ifbdev->fb)
54632abe 302 drm_framebuffer_remove(&ifbdev->fb->base);
43cee314
CW
303
304 kfree(ifbdev);
79e53945 305}
38651674 306
d978ef14
JB
307/*
308 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
309 * The core display code will have read out the current plane configuration,
310 * so we use that to figure out if there's an object for us to use as the
311 * fb, and if so, we re-use it for the fbdev configuration.
312 *
313 * Note we only support a single fb shared across pipes for boot (mostly for
314 * fbcon), so we just find the biggest and use that.
315 */
316static bool intel_fbdev_init_bios(struct drm_device *dev,
317 struct intel_fbdev *ifbdev)
318{
319 struct intel_framebuffer *fb = NULL;
320 struct drm_crtc *crtc;
321 struct intel_crtc *intel_crtc;
d978ef14
JB
322 unsigned int max_size = 0;
323
d978ef14 324 /* Find the largest fb */
70e1e0ec 325 for_each_crtc(dev, crtc) {
8e9ba31a
ML
326 struct drm_i915_gem_object *obj =
327 intel_fb_obj(crtc->primary->state->fb);
d978ef14
JB
328 intel_crtc = to_intel_crtc(crtc);
329
d5515991 330 if (!crtc->state->active || !obj) {
d978ef14
JB
331 DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
332 pipe_name(intel_crtc->pipe));
333 continue;
334 }
335
8e9ba31a 336 if (obj->base.size > max_size) {
d978ef14
JB
337 DRM_DEBUG_KMS("found possible fb from plane %c\n",
338 pipe_name(intel_crtc->pipe));
8e9ba31a
ML
339 fb = to_intel_framebuffer(crtc->primary->state->fb);
340 max_size = obj->base.size;
d978ef14
JB
341 }
342 }
343
344 if (!fb) {
345 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
346 goto out;
347 }
348
349 /* Now make sure all the pipes will fit into it */
70e1e0ec 350 for_each_crtc(dev, crtc) {
d978ef14
JB
351 unsigned int cur_size;
352
353 intel_crtc = to_intel_crtc(crtc);
354
d5515991 355 if (!crtc->state->active) {
d978ef14
JB
356 DRM_DEBUG_KMS("pipe %c not active, skipping\n",
357 pipe_name(intel_crtc->pipe));
358 continue;
359 }
360
361 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
362 pipe_name(intel_crtc->pipe));
363
364 /*
365 * See if the plane fb we found above will fit on this
bc104d1f
CW
366 * pipe. Note we need to use the selected fb's pitch and bpp
367 * rather than the current pipe's, since they differ.
d978ef14 368 */
6e3d9dd0 369 cur_size = crtc->state->adjusted_mode.crtc_hdisplay;
272725c7 370 cur_size = cur_size * fb->base.format->cpp[0];
bc104d1f
CW
371 if (fb->base.pitches[0] < cur_size) {
372 DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
373 pipe_name(intel_crtc->pipe),
374 cur_size, fb->base.pitches[0]);
bc104d1f
CW
375 fb = NULL;
376 break;
377 }
378
6e3d9dd0 379 cur_size = crtc->state->adjusted_mode.crtc_vdisplay;
d88c4afd 380 cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
bc104d1f
CW
381 cur_size *= fb->base.pitches[0];
382 DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
383 pipe_name(intel_crtc->pipe),
6e3d9dd0
ML
384 crtc->state->adjusted_mode.crtc_hdisplay,
385 crtc->state->adjusted_mode.crtc_vdisplay,
272725c7 386 fb->base.format->cpp[0] * 8,
d978ef14 387 cur_size);
d978ef14
JB
388
389 if (cur_size > max_size) {
390 DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
391 pipe_name(intel_crtc->pipe),
392 cur_size, max_size);
d978ef14
JB
393 fb = NULL;
394 break;
395 }
396
397 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
398 pipe_name(intel_crtc->pipe),
399 max_size, cur_size);
400 }
401
d978ef14
JB
402 if (!fb) {
403 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
404 goto out;
405 }
406
272725c7 407 ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
d978ef14
JB
408 ifbdev->fb = fb;
409
c3ed1103 410 drm_framebuffer_get(&ifbdev->fb->base);
d978ef14
JB
411
412 /* Final pass to check if any active pipes don't have fbs */
70e1e0ec 413 for_each_crtc(dev, crtc) {
d978ef14
JB
414 intel_crtc = to_intel_crtc(crtc);
415
d5515991 416 if (!crtc->state->active)
d978ef14
JB
417 continue;
418
8bc20f65 419 WARN(!crtc->primary->state->fb,
d978ef14
JB
420 "re-used BIOS config but lost an fb on crtc %d\n",
421 crtc->base.id);
422 }
423
424
425 DRM_DEBUG_KMS("using BIOS fb for initial console\n");
426 return true;
427
d978ef14
JB
428out:
429
430 return false;
431}
432
82e3b8c1
CW
433static void intel_fbdev_suspend_worker(struct work_struct *work)
434{
91c8a326
CW
435 intel_fbdev_set_suspend(&container_of(work,
436 struct drm_i915_private,
437 fbdev_suspend_work)->drm,
82e3b8c1
CW
438 FBINFO_STATE_RUNNING,
439 true);
440}
441
38651674
DA
442int intel_fbdev_init(struct drm_device *dev)
443{
fac5e23e 444 struct drm_i915_private *dev_priv = to_i915(dev);
b7f05d4a 445 struct intel_fbdev *ifbdev;
5a79395b 446 int ret;
8be48d92 447
e1bf094b 448 if (WARN_ON(!HAS_DISPLAY(dev_priv)))
d978ef14
JB
449 return -ENODEV;
450
451 ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
452 if (ifbdev == NULL)
8be48d92
DA
453 return -ENOMEM;
454
fe5ec656 455 mutex_init(&ifbdev->hpd_lock);
10a23102
TR
456 drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
457
d978ef14
JB
458 if (!intel_fbdev_init_bios(dev, ifbdev))
459 ifbdev->preferred_bpp = 32;
4abe3520 460
e4563f6b 461 ret = drm_fb_helper_init(dev, &ifbdev->helper, 4);
5a79395b
CW
462 if (ret) {
463 kfree(ifbdev);
464 return ret;
465 }
8be48d92 466
d978ef14 467 dev_priv->fbdev = ifbdev;
82e3b8c1
CW
468 INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
469
0b4c0f3f 470 drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
20afbda2 471
79e53945
JB
472 return 0;
473}
38651674 474
e00bf696 475static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
20afbda2 476{
43cee314 477 struct intel_fbdev *ifbdev = data;
20afbda2
DV
478
479 /* Due to peculiar init order wrt to hpd handling this is separate. */
366e39b4 480 if (drm_fb_helper_initial_config(&ifbdev->helper,
ad88d7fc 481 ifbdev->preferred_bpp))
4f256d82 482 intel_fbdev_unregister(to_i915(ifbdev->helper.dev));
20afbda2
DV
483}
484
e00bf696
VS
485void intel_fbdev_initial_config_async(struct drm_device *dev)
486{
43cee314
CW
487 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
488
5b8cd075
CT
489 if (!ifbdev)
490 return;
491
43cee314
CW
492 ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
493}
494
495static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
496{
497 if (!ifbdev->cookie)
498 return;
499
500 /* Only serialises with all preceding async calls, hence +1 */
501 async_synchronize_cookie(ifbdev->cookie + 1);
502 ifbdev->cookie = 0;
e00bf696
VS
503}
504
4f256d82 505void intel_fbdev_unregister(struct drm_i915_private *dev_priv)
38651674 506{
43cee314
CW
507 struct intel_fbdev *ifbdev = dev_priv->fbdev;
508
509 if (!ifbdev)
8be48d92
DA
510 return;
511
0b8c0e9c 512 cancel_work_sync(&dev_priv->fbdev_suspend_work);
366e39b4 513 if (!current_is_async())
43cee314
CW
514 intel_fbdev_sync(ifbdev);
515
4f256d82
DV
516 drm_fb_helper_unregister_fbi(&ifbdev->helper);
517}
518
519void intel_fbdev_fini(struct drm_i915_private *dev_priv)
520{
521 struct intel_fbdev *ifbdev = fetch_and_zero(&dev_priv->fbdev);
522
523 if (!ifbdev)
524 return;
525
43cee314 526 intel_fbdev_destroy(ifbdev);
38651674 527}
3fa016a0 528
fe5ec656
LP
529/* Suspends/resumes fbdev processing of incoming HPD events. When resuming HPD
530 * processing, fbdev will perform a full connector reprobe if a hotplug event
531 * was received while HPD was suspended.
532 */
533static void intel_fbdev_hpd_set_suspend(struct intel_fbdev *ifbdev, int state)
534{
535 bool send_hpd = false;
536
537 mutex_lock(&ifbdev->hpd_lock);
538 ifbdev->hpd_suspended = state == FBINFO_STATE_SUSPENDED;
539 send_hpd = !ifbdev->hpd_suspended && ifbdev->hpd_waiting;
540 ifbdev->hpd_waiting = false;
541 mutex_unlock(&ifbdev->hpd_lock);
542
543 if (send_hpd) {
544 DRM_DEBUG_KMS("Handling delayed fbcon HPD event\n");
545 drm_fb_helper_hotplug_event(&ifbdev->helper);
546 }
547}
548
82e3b8c1 549void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
3fa016a0 550{
fac5e23e 551 struct drm_i915_private *dev_priv = to_i915(dev);
1ffc5289
JN
552 struct intel_fbdev *ifbdev = dev_priv->fbdev;
553 struct fb_info *info;
554
15727ed0 555 if (!ifbdev || !ifbdev->vma)
3fa016a0
DA
556 return;
557
1ffc5289
JN
558 info = ifbdev->helper.fbdev;
559
82e3b8c1
CW
560 if (synchronous) {
561 /* Flush any pending work to turn the console on, and then
562 * wait to turn it off. It must be synchronous as we are
563 * about to suspend or unload the driver.
564 *
565 * Note that from within the work-handler, we cannot flush
566 * ourselves, so only flush outstanding work upon suspend!
567 */
568 if (state != FBINFO_STATE_RUNNING)
569 flush_work(&dev_priv->fbdev_suspend_work);
fe5ec656 570
82e3b8c1
CW
571 console_lock();
572 } else {
573 /*
574 * The console lock can be pretty contented on resume due
575 * to all the printk activity. Try to keep it out of the hot
576 * path of resume if possible.
577 */
578 WARN_ON(state != FBINFO_STATE_RUNNING);
579 if (!console_trylock()) {
580 /* Don't block our own workqueue as this can
581 * be run in parallel with other i915.ko tasks.
582 */
583 schedule_work(&dev_priv->fbdev_suspend_work);
584 return;
585 }
586 }
587
1ffc5289
JN
588 /* On resume from hibernation: If the object is shmemfs backed, it has
589 * been restored from swap. If the object is stolen however, it will be
590 * full of whatever garbage was left in there.
591 */
a5ff7a45
DS
592 if (state == FBINFO_STATE_RUNNING &&
593 intel_fb_obj(&ifbdev->fb->base)->stolen)
1ffc5289
JN
594 memset_io(info->screen_base, 0, info->screen_size);
595
21cff148 596 drm_fb_helper_set_suspend(&ifbdev->helper, state);
82e3b8c1 597 console_unlock();
fe5ec656
LP
598
599 intel_fbdev_hpd_set_suspend(ifbdev, state);
3fa016a0
DA
600}
601
0632fef6 602void intel_fbdev_output_poll_changed(struct drm_device *dev)
eb1f8e4f 603{
c45eb4fe 604 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
fe5ec656 605 bool send_hpd;
c45eb4fe 606
ad88d7fc
CW
607 if (!ifbdev)
608 return;
609
610 intel_fbdev_sync(ifbdev);
fe5ec656
LP
611
612 mutex_lock(&ifbdev->hpd_lock);
613 send_hpd = !ifbdev->hpd_suspended;
614 ifbdev->hpd_waiting = true;
615 mutex_unlock(&ifbdev->hpd_lock);
616
617 if (send_hpd && (ifbdev->vma || ifbdev->helper.deferred_setup))
c45eb4fe 618 drm_fb_helper_hotplug_event(&ifbdev->helper);
eb1f8e4f 619}
e8e7a2b8 620
0632fef6 621void intel_fbdev_restore_mode(struct drm_device *dev)
e8e7a2b8 622{
c45eb4fe 623 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
e8e7a2b8 624
d04df732 625 if (!ifbdev)
e3c74757
BW
626 return;
627
e77018f7 628 intel_fbdev_sync(ifbdev);
15727ed0 629 if (!ifbdev->vma)
c45eb4fe 630 return;
e77018f7 631
fabef825
CW
632 if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper) == 0)
633 intel_fbdev_invalidate(ifbdev);
e8e7a2b8 634}