2 * Copyright © 2007 David Airlie
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:
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
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.
27 #include <linux/async.h>
28 #include <linux/console.h>
29 #include <linux/delay.h>
30 #include <linux/errno.h>
32 #include <linux/init.h>
33 #include <linux/kernel.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>
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>
46 #include "gem/i915_gem_lmem.h"
47 #include "gem/i915_gem_mman.h"
50 #include "intel_display_types.h"
52 #include "intel_fb_pin.h"
53 #include "intel_fbdev.h"
54 #include "intel_frontbuffer.h"
57 struct drm_fb_helper helper;
58 struct intel_framebuffer *fb;
60 unsigned long vma_flags;
61 async_cookie_t cookie;
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 */
69 /* Protects hpd_suspended */
70 struct mutex hpd_lock;
73 static struct intel_fbdev *to_intel_fbdev(struct drm_fb_helper *fb_helper)
75 return container_of(fb_helper, struct intel_fbdev, helper);
78 static struct intel_frontbuffer *to_frontbuffer(struct intel_fbdev *ifbdev)
80 return ifbdev->fb->frontbuffer;
83 static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev)
85 intel_frontbuffer_invalidate(to_frontbuffer(ifbdev), ORIGIN_CPU);
88 FB_GEN_DEFAULT_DEFERRED_IO_OPS(intel_fbdev,
89 drm_fb_helper_damage_range,
90 drm_fb_helper_damage_area)
92 static int intel_fbdev_set_par(struct fb_info *info)
94 struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
97 ret = drm_fb_helper_set_par(info);
99 intel_fbdev_invalidate(ifbdev);
104 static int intel_fbdev_blank(int blank, struct fb_info *info)
106 struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
109 ret = drm_fb_helper_blank(blank, info);
111 intel_fbdev_invalidate(ifbdev);
116 static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
117 struct fb_info *info)
119 struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
122 ret = drm_fb_helper_pan_display(var, info);
124 intel_fbdev_invalidate(ifbdev);
129 static int intel_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma)
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);
135 return i915_gem_fb_mmap(obj, vma);
139 __diag_ignore_all("-Woverride-init", "Allow overriding the default ops");
141 static const struct fb_ops intelfb_ops = {
142 .owner = THIS_MODULE,
143 __FB_DEFAULT_DEFERRED_OPS_RDWR(intel_fbdev),
144 DRM_FB_HELPER_DEFAULT_OPS,
145 .fb_set_par = intel_fbdev_set_par,
146 .fb_blank = intel_fbdev_blank,
147 .fb_pan_display = intel_fbdev_pan_display,
148 __FB_DEFAULT_DEFERRED_OPS_DRAW(intel_fbdev),
149 .fb_mmap = intel_fbdev_mmap,
154 static int intelfb_alloc(struct drm_fb_helper *helper,
155 struct drm_fb_helper_surface_size *sizes)
157 struct intel_fbdev *ifbdev = to_intel_fbdev(helper);
158 struct drm_framebuffer *fb;
159 struct drm_device *dev = helper->dev;
160 struct drm_i915_private *dev_priv = to_i915(dev);
161 struct drm_mode_fb_cmd2 mode_cmd = {};
162 struct drm_i915_gem_object *obj;
165 /* we don't do packed 24bpp */
166 if (sizes->surface_bpp == 24)
167 sizes->surface_bpp = 32;
169 mode_cmd.width = sizes->surface_width;
170 mode_cmd.height = sizes->surface_height;
172 mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
173 DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
174 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
175 sizes->surface_depth);
177 size = mode_cmd.pitches[0] * mode_cmd.height;
178 size = PAGE_ALIGN(size);
180 obj = ERR_PTR(-ENODEV);
181 if (HAS_LMEM(dev_priv)) {
182 obj = i915_gem_object_create_lmem(dev_priv, size,
183 I915_BO_ALLOC_CONTIGUOUS |
187 * If the FB is too big, just don't use it since fbdev is not very
188 * important and we should probably use that space with FBC or other
191 if (size * 2 < dev_priv->dsm.usable_size)
192 obj = i915_gem_object_create_stolen(dev_priv, size);
194 obj = i915_gem_object_create_shmem(dev_priv, size);
198 drm_err(&dev_priv->drm, "failed to allocate framebuffer (%pe)\n", obj);
202 fb = intel_framebuffer_create(obj, &mode_cmd);
203 i915_gem_object_put(obj);
207 ifbdev->fb = to_intel_framebuffer(fb);
211 static int intelfb_create(struct drm_fb_helper *helper,
212 struct drm_fb_helper_surface_size *sizes)
214 struct intel_fbdev *ifbdev = to_intel_fbdev(helper);
215 struct intel_framebuffer *intel_fb = ifbdev->fb;
216 struct drm_device *dev = helper->dev;
217 struct drm_i915_private *dev_priv = to_i915(dev);
218 struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
219 struct i915_ggtt *ggtt = to_gt(dev_priv)->ggtt;
220 const struct i915_gtt_view view = {
221 .type = I915_GTT_VIEW_NORMAL,
223 intel_wakeref_t wakeref;
224 struct fb_info *info;
225 struct i915_vma *vma;
226 unsigned long flags = 0;
227 bool prealloc = false;
229 struct drm_i915_gem_object *obj;
230 struct i915_gem_ww_ctx ww;
233 mutex_lock(&ifbdev->hpd_lock);
234 ret = ifbdev->hpd_suspended ? -EAGAIN : 0;
235 mutex_unlock(&ifbdev->hpd_lock);
240 (sizes->fb_width > intel_fb->base.width ||
241 sizes->fb_height > intel_fb->base.height)) {
242 drm_dbg_kms(&dev_priv->drm,
243 "BIOS fb too small (%dx%d), we require (%dx%d),"
245 intel_fb->base.width, intel_fb->base.height,
246 sizes->fb_width, sizes->fb_height);
247 drm_framebuffer_put(&intel_fb->base);
248 intel_fb = ifbdev->fb = NULL;
250 if (!intel_fb || drm_WARN_ON(dev, !intel_fb_obj(&intel_fb->base))) {
251 drm_dbg_kms(&dev_priv->drm,
252 "no BIOS fb, allocating a new one\n");
253 ret = intelfb_alloc(helper, sizes);
256 intel_fb = ifbdev->fb;
258 drm_dbg_kms(&dev_priv->drm, "re-using BIOS fb\n");
260 sizes->fb_width = intel_fb->base.width;
261 sizes->fb_height = intel_fb->base.height;
264 wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
266 /* Pin the GGTT vma for our access via info->screen_base.
267 * This also validates that any existing fb inherited from the
268 * BIOS is suitable for own access.
270 vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base, false,
271 &view, false, &flags);
277 info = drm_fb_helper_alloc_info(helper);
279 drm_err(&dev_priv->drm, "Failed to allocate fb_info (%pe)\n", info);
284 ifbdev->helper.fb = &ifbdev->fb->base;
286 info->fbops = &intelfb_ops;
288 obj = intel_fb_obj(&intel_fb->base);
289 if (i915_gem_object_is_lmem(obj)) {
290 struct intel_memory_region *mem = obj->mm.region;
292 /* Use fbdev's framebuffer from lmem for discrete */
293 info->fix.smem_start =
294 (unsigned long)(mem->io_start +
295 i915_gem_object_get_dma_address(obj, 0));
296 info->fix.smem_len = obj->base.size;
298 /* Our framebuffer is the entirety of fbdev's system memory */
299 info->fix.smem_start =
300 (unsigned long)(ggtt->gmadr.start + i915_ggtt_offset(vma));
301 info->fix.smem_len = vma->size;
304 for_i915_gem_ww(&ww, ret, false) {
305 ret = i915_gem_object_lock(vma->obj, &ww);
310 vaddr = i915_vma_pin_iomap(vma);
312 drm_err(&dev_priv->drm,
313 "Failed to remap framebuffer into virtual memory (%pe)\n", vaddr);
314 ret = PTR_ERR(vaddr);
322 info->screen_base = vaddr;
323 info->screen_size = vma->size;
325 drm_fb_helper_fill_info(info, &ifbdev->helper, sizes);
327 /* If the object is shmemfs backed, it will have given us zeroed pages.
328 * If the object is stolen however, it will be full of whatever
329 * garbage was left in there.
331 if (!i915_gem_object_is_shmem(vma->obj) && !prealloc)
332 memset_io(info->screen_base, 0, info->screen_size);
334 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
336 drm_dbg_kms(&dev_priv->drm, "allocated %dx%d fb: 0x%08x\n",
337 ifbdev->fb->base.width, ifbdev->fb->base.height,
338 i915_ggtt_offset(vma));
340 ifbdev->vma_flags = flags;
342 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
343 vga_switcheroo_client_fb_set(pdev, info);
347 intel_unpin_fb_vma(vma, flags);
349 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
353 static int intelfb_dirty(struct drm_fb_helper *helper, struct drm_clip_rect *clip)
355 if (!(clip->x1 < clip->x2 && clip->y1 < clip->y2))
358 if (helper->fb->funcs->dirty)
359 return helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, clip, 1);
364 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
365 .fb_probe = intelfb_create,
366 .fb_dirty = intelfb_dirty,
369 static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
371 /* We rely on the object-free to release the VMA pinning for
372 * the info->screen_base mmaping. Leaking the VMA is simpler than
373 * trying to rectify all the possible error paths leading here.
376 drm_fb_helper_fini(&ifbdev->helper);
379 intel_unpin_fb_vma(ifbdev->vma, ifbdev->vma_flags);
382 drm_framebuffer_remove(&ifbdev->fb->base);
384 drm_fb_helper_unprepare(&ifbdev->helper);
389 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
390 * The core display code will have read out the current plane configuration,
391 * so we use that to figure out if there's an object for us to use as the
392 * fb, and if so, we re-use it for the fbdev configuration.
394 * Note we only support a single fb shared across pipes for boot (mostly for
395 * fbcon), so we just find the biggest and use that.
397 static bool intel_fbdev_init_bios(struct drm_device *dev,
398 struct intel_fbdev *ifbdev)
400 struct drm_i915_private *i915 = to_i915(dev);
401 struct intel_framebuffer *fb = NULL;
402 struct intel_crtc *crtc;
403 unsigned int max_size = 0;
405 /* Find the largest fb */
406 for_each_intel_crtc(dev, crtc) {
407 struct intel_crtc_state *crtc_state =
408 to_intel_crtc_state(crtc->base.state);
409 struct intel_plane *plane =
410 to_intel_plane(crtc->base.primary);
411 struct intel_plane_state *plane_state =
412 to_intel_plane_state(plane->base.state);
413 struct drm_i915_gem_object *obj =
414 intel_fb_obj(plane_state->uapi.fb);
416 if (!crtc_state->uapi.active) {
417 drm_dbg_kms(&i915->drm,
418 "[CRTC:%d:%s] not active, skipping\n",
419 crtc->base.base.id, crtc->base.name);
424 drm_dbg_kms(&i915->drm,
425 "[PLANE:%d:%s] no fb, skipping\n",
426 plane->base.base.id, plane->base.name);
430 if (obj->base.size > max_size) {
431 drm_dbg_kms(&i915->drm,
432 "found possible fb from [PLANE:%d:%s]\n",
433 plane->base.base.id, plane->base.name);
434 fb = to_intel_framebuffer(plane_state->uapi.fb);
435 max_size = obj->base.size;
440 drm_dbg_kms(&i915->drm,
441 "no active fbs found, not using BIOS config\n");
445 /* Now make sure all the pipes will fit into it */
446 for_each_intel_crtc(dev, crtc) {
447 struct intel_crtc_state *crtc_state =
448 to_intel_crtc_state(crtc->base.state);
449 struct intel_plane *plane =
450 to_intel_plane(crtc->base.primary);
451 unsigned int cur_size;
453 if (!crtc_state->uapi.active) {
454 drm_dbg_kms(&i915->drm,
455 "[CRTC:%d:%s] not active, skipping\n",
456 crtc->base.base.id, crtc->base.name);
460 drm_dbg_kms(&i915->drm, "checking [PLANE:%d:%s] for BIOS fb\n",
461 plane->base.base.id, plane->base.name);
464 * See if the plane fb we found above will fit on this
465 * pipe. Note we need to use the selected fb's pitch and bpp
466 * rather than the current pipe's, since they differ.
468 cur_size = crtc_state->uapi.adjusted_mode.crtc_hdisplay;
469 cur_size = cur_size * fb->base.format->cpp[0];
470 if (fb->base.pitches[0] < cur_size) {
471 drm_dbg_kms(&i915->drm,
472 "fb not wide enough for [PLANE:%d:%s] (%d vs %d)\n",
473 plane->base.base.id, plane->base.name,
474 cur_size, fb->base.pitches[0]);
479 cur_size = crtc_state->uapi.adjusted_mode.crtc_vdisplay;
480 cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
481 cur_size *= fb->base.pitches[0];
482 drm_dbg_kms(&i915->drm,
483 "[CRTC:%d:%s] area: %dx%d, bpp: %d, size: %d\n",
484 crtc->base.base.id, crtc->base.name,
485 crtc_state->uapi.adjusted_mode.crtc_hdisplay,
486 crtc_state->uapi.adjusted_mode.crtc_vdisplay,
487 fb->base.format->cpp[0] * 8,
490 if (cur_size > max_size) {
491 drm_dbg_kms(&i915->drm,
492 "fb not big enough for [PLANE:%d:%s] (%d vs %d)\n",
493 plane->base.base.id, plane->base.name,
499 drm_dbg_kms(&i915->drm,
500 "fb big enough [PLANE:%d:%s] (%d >= %d)\n",
501 plane->base.base.id, plane->base.name,
506 drm_dbg_kms(&i915->drm,
507 "BIOS fb not suitable for all pipes, not using\n");
511 ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
514 drm_framebuffer_get(&ifbdev->fb->base);
516 /* Final pass to check if any active pipes don't have fbs */
517 for_each_intel_crtc(dev, crtc) {
518 struct intel_crtc_state *crtc_state =
519 to_intel_crtc_state(crtc->base.state);
520 struct intel_plane *plane =
521 to_intel_plane(crtc->base.primary);
522 struct intel_plane_state *plane_state =
523 to_intel_plane_state(plane->base.state);
525 if (!crtc_state->uapi.active)
528 drm_WARN(dev, !plane_state->uapi.fb,
529 "re-used BIOS config but lost an fb on [PLANE:%d:%s]\n",
530 plane->base.base.id, plane->base.name);
534 drm_dbg_kms(&i915->drm, "using BIOS fb for initial console\n");
542 static void intel_fbdev_suspend_worker(struct work_struct *work)
544 intel_fbdev_set_suspend(&container_of(work,
545 struct drm_i915_private,
546 display.fbdev.suspend_work)->drm,
547 FBINFO_STATE_RUNNING,
551 int intel_fbdev_init(struct drm_device *dev)
553 struct drm_i915_private *dev_priv = to_i915(dev);
554 struct intel_fbdev *ifbdev;
557 if (drm_WARN_ON(dev, !HAS_DISPLAY(dev_priv)))
560 ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
564 mutex_init(&ifbdev->hpd_lock);
565 drm_fb_helper_prepare(dev, &ifbdev->helper, 32, &intel_fb_helper_funcs);
567 if (intel_fbdev_init_bios(dev, ifbdev))
568 ifbdev->helper.preferred_bpp = ifbdev->preferred_bpp;
570 ifbdev->preferred_bpp = ifbdev->helper.preferred_bpp;
572 ret = drm_fb_helper_init(dev, &ifbdev->helper);
578 dev_priv->display.fbdev.fbdev = ifbdev;
579 INIT_WORK(&dev_priv->display.fbdev.suspend_work, intel_fbdev_suspend_worker);
584 static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
586 struct intel_fbdev *ifbdev = data;
588 /* Due to peculiar init order wrt to hpd handling this is separate. */
589 if (drm_fb_helper_initial_config(&ifbdev->helper))
590 intel_fbdev_unregister(to_i915(ifbdev->helper.dev));
593 void intel_fbdev_initial_config_async(struct drm_i915_private *dev_priv)
595 struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
600 ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
603 static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
608 /* Only serialises with all preceding async calls, hence +1 */
609 async_synchronize_cookie(ifbdev->cookie + 1);
613 void intel_fbdev_unregister(struct drm_i915_private *dev_priv)
615 struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
620 intel_fbdev_set_suspend(&dev_priv->drm, FBINFO_STATE_SUSPENDED, true);
622 if (!current_is_async())
623 intel_fbdev_sync(ifbdev);
625 drm_fb_helper_unregister_info(&ifbdev->helper);
628 void intel_fbdev_fini(struct drm_i915_private *dev_priv)
630 struct intel_fbdev *ifbdev = fetch_and_zero(&dev_priv->display.fbdev.fbdev);
635 intel_fbdev_destroy(ifbdev);
638 /* Suspends/resumes fbdev processing of incoming HPD events. When resuming HPD
639 * processing, fbdev will perform a full connector reprobe if a hotplug event
640 * was received while HPD was suspended.
642 static void intel_fbdev_hpd_set_suspend(struct drm_i915_private *i915, int state)
644 struct intel_fbdev *ifbdev = i915->display.fbdev.fbdev;
645 bool send_hpd = false;
647 mutex_lock(&ifbdev->hpd_lock);
648 ifbdev->hpd_suspended = state == FBINFO_STATE_SUSPENDED;
649 send_hpd = !ifbdev->hpd_suspended && ifbdev->hpd_waiting;
650 ifbdev->hpd_waiting = false;
651 mutex_unlock(&ifbdev->hpd_lock);
654 drm_dbg_kms(&i915->drm, "Handling delayed fbcon HPD event\n");
655 drm_fb_helper_hotplug_event(&ifbdev->helper);
659 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
661 struct drm_i915_private *dev_priv = to_i915(dev);
662 struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
663 struct fb_info *info;
668 if (drm_WARN_ON(&dev_priv->drm, !HAS_DISPLAY(dev_priv)))
674 info = ifbdev->helper.info;
677 /* Flush any pending work to turn the console on, and then
678 * wait to turn it off. It must be synchronous as we are
679 * about to suspend or unload the driver.
681 * Note that from within the work-handler, we cannot flush
682 * ourselves, so only flush outstanding work upon suspend!
684 if (state != FBINFO_STATE_RUNNING)
685 flush_work(&dev_priv->display.fbdev.suspend_work);
690 * The console lock can be pretty contented on resume due
691 * to all the printk activity. Try to keep it out of the hot
692 * path of resume if possible.
694 drm_WARN_ON(dev, state != FBINFO_STATE_RUNNING);
695 if (!console_trylock()) {
696 /* Don't block our own workqueue as this can
697 * be run in parallel with other i915.ko tasks.
699 schedule_work(&dev_priv->display.fbdev.suspend_work);
704 /* On resume from hibernation: If the object is shmemfs backed, it has
705 * been restored from swap. If the object is stolen however, it will be
706 * full of whatever garbage was left in there.
708 if (state == FBINFO_STATE_RUNNING &&
709 !i915_gem_object_is_shmem(intel_fb_obj(&ifbdev->fb->base)))
710 memset_io(info->screen_base, 0, info->screen_size);
712 drm_fb_helper_set_suspend(&ifbdev->helper, state);
716 intel_fbdev_hpd_set_suspend(dev_priv, state);
719 void intel_fbdev_output_poll_changed(struct drm_device *dev)
721 struct intel_fbdev *ifbdev = to_i915(dev)->display.fbdev.fbdev;
727 intel_fbdev_sync(ifbdev);
729 mutex_lock(&ifbdev->hpd_lock);
730 send_hpd = !ifbdev->hpd_suspended;
731 ifbdev->hpd_waiting = true;
732 mutex_unlock(&ifbdev->hpd_lock);
734 if (send_hpd && (ifbdev->vma || ifbdev->helper.deferred_setup))
735 drm_fb_helper_hotplug_event(&ifbdev->helper);
738 void intel_fbdev_restore_mode(struct drm_i915_private *dev_priv)
740 struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
745 intel_fbdev_sync(ifbdev);
749 if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper) == 0)
750 intel_fbdev_invalidate(ifbdev);
753 struct intel_framebuffer *intel_fbdev_framebuffer(struct intel_fbdev *fbdev)
755 if (!fbdev || !fbdev->helper.fb)
758 return to_intel_framebuffer(fbdev->helper.fb);