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