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>
31 #include <linux/init.h>
32 #include <linux/kernel.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>
40 #include <drm/drm_crtc.h>
41 #include <drm/drm_fb_helper.h>
42 #include <drm/drm_fourcc.h>
43 #include <drm/drm_gem_framebuffer_helper.h>
45 #include "gem/i915_gem_lmem.h"
46 #include "gem/i915_gem_mman.h"
49 #include "intel_display_types.h"
51 #include "intel_fb_pin.h"
52 #include "intel_fbdev.h"
53 #include "intel_frontbuffer.h"
56 struct drm_fb_helper helper;
57 struct intel_framebuffer *fb;
59 unsigned long vma_flags;
60 async_cookie_t cookie;
63 /* Whether or not fbdev hpd processing is temporarily suspended */
64 bool hpd_suspended: 1;
65 /* Set when a hotplug was received while HPD processing was suspended */
68 /* Protects hpd_suspended */
69 struct mutex hpd_lock;
72 static struct intel_fbdev *to_intel_fbdev(struct drm_fb_helper *fb_helper)
74 return container_of(fb_helper, struct intel_fbdev, helper);
77 static struct intel_frontbuffer *to_frontbuffer(struct intel_fbdev *ifbdev)
79 return ifbdev->fb->frontbuffer;
82 static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev)
84 intel_frontbuffer_invalidate(to_frontbuffer(ifbdev), ORIGIN_CPU);
87 static int intel_fbdev_set_par(struct fb_info *info)
89 struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
92 ret = drm_fb_helper_set_par(info);
94 intel_fbdev_invalidate(ifbdev);
99 static int intel_fbdev_blank(int blank, struct fb_info *info)
101 struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
104 ret = drm_fb_helper_blank(blank, info);
106 intel_fbdev_invalidate(ifbdev);
111 static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
112 struct fb_info *info)
114 struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
117 ret = drm_fb_helper_pan_display(var, info);
119 intel_fbdev_invalidate(ifbdev);
124 static int intel_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma)
126 struct intel_fbdev *fbdev = to_intel_fbdev(info->par);
127 struct drm_gem_object *bo = drm_gem_fb_get_obj(&fbdev->fb->base, 0);
128 struct drm_i915_gem_object *obj = to_intel_bo(bo);
130 return i915_gem_fb_mmap(obj, vma);
133 static const struct fb_ops intelfb_ops = {
134 .owner = THIS_MODULE,
135 DRM_FB_HELPER_DEFAULT_OPS,
136 .fb_set_par = intel_fbdev_set_par,
137 .fb_read = drm_fb_helper_cfb_read,
138 .fb_write = drm_fb_helper_cfb_write,
139 .fb_fillrect = drm_fb_helper_cfb_fillrect,
140 .fb_copyarea = drm_fb_helper_cfb_copyarea,
141 .fb_imageblit = drm_fb_helper_cfb_imageblit,
142 .fb_pan_display = intel_fbdev_pan_display,
143 .fb_blank = intel_fbdev_blank,
144 .fb_mmap = intel_fbdev_mmap,
147 static int intelfb_alloc(struct drm_fb_helper *helper,
148 struct drm_fb_helper_surface_size *sizes)
150 struct intel_fbdev *ifbdev = to_intel_fbdev(helper);
151 struct drm_framebuffer *fb;
152 struct drm_device *dev = helper->dev;
153 struct drm_i915_private *dev_priv = to_i915(dev);
154 struct drm_mode_fb_cmd2 mode_cmd = {};
155 struct drm_i915_gem_object *obj;
158 /* we don't do packed 24bpp */
159 if (sizes->surface_bpp == 24)
160 sizes->surface_bpp = 32;
162 mode_cmd.width = sizes->surface_width;
163 mode_cmd.height = sizes->surface_height;
165 mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
166 DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
167 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
168 sizes->surface_depth);
170 size = mode_cmd.pitches[0] * mode_cmd.height;
171 size = PAGE_ALIGN(size);
173 obj = ERR_PTR(-ENODEV);
174 if (HAS_LMEM(dev_priv)) {
175 obj = i915_gem_object_create_lmem(dev_priv, size,
176 I915_BO_ALLOC_CONTIGUOUS |
180 * If the FB is too big, just don't use it since fbdev is not very
181 * important and we should probably use that space with FBC or other
184 if (size * 2 < dev_priv->stolen_usable_size)
185 obj = i915_gem_object_create_stolen(dev_priv, size);
187 obj = i915_gem_object_create_shmem(dev_priv, size);
191 drm_err(&dev_priv->drm, "failed to allocate framebuffer (%pe)\n", obj);
195 fb = intel_framebuffer_create(obj, &mode_cmd);
196 i915_gem_object_put(obj);
200 ifbdev->fb = to_intel_framebuffer(fb);
204 static int intelfb_create(struct drm_fb_helper *helper,
205 struct drm_fb_helper_surface_size *sizes)
207 struct intel_fbdev *ifbdev = to_intel_fbdev(helper);
208 struct intel_framebuffer *intel_fb = ifbdev->fb;
209 struct drm_device *dev = helper->dev;
210 struct drm_i915_private *dev_priv = to_i915(dev);
211 struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
212 struct i915_ggtt *ggtt = to_gt(dev_priv)->ggtt;
213 const struct i915_gtt_view view = {
214 .type = I915_GTT_VIEW_NORMAL,
216 intel_wakeref_t wakeref;
217 struct fb_info *info;
218 struct i915_vma *vma;
219 unsigned long flags = 0;
220 bool prealloc = false;
222 struct drm_i915_gem_object *obj;
225 mutex_lock(&ifbdev->hpd_lock);
226 ret = ifbdev->hpd_suspended ? -EAGAIN : 0;
227 mutex_unlock(&ifbdev->hpd_lock);
232 (sizes->fb_width > intel_fb->base.width ||
233 sizes->fb_height > intel_fb->base.height)) {
234 drm_dbg_kms(&dev_priv->drm,
235 "BIOS fb too small (%dx%d), we require (%dx%d),"
237 intel_fb->base.width, intel_fb->base.height,
238 sizes->fb_width, sizes->fb_height);
239 drm_framebuffer_put(&intel_fb->base);
240 intel_fb = ifbdev->fb = NULL;
242 if (!intel_fb || drm_WARN_ON(dev, !intel_fb_obj(&intel_fb->base))) {
243 drm_dbg_kms(&dev_priv->drm,
244 "no BIOS fb, allocating a new one\n");
245 ret = intelfb_alloc(helper, sizes);
248 intel_fb = ifbdev->fb;
250 drm_dbg_kms(&dev_priv->drm, "re-using BIOS fb\n");
252 sizes->fb_width = intel_fb->base.width;
253 sizes->fb_height = intel_fb->base.height;
256 wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
258 /* Pin the GGTT vma for our access via info->screen_base.
259 * This also validates that any existing fb inherited from the
260 * BIOS is suitable for own access.
262 vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base, false,
263 &view, false, &flags);
269 info = drm_fb_helper_alloc_info(helper);
271 drm_err(&dev_priv->drm, "Failed to allocate fb_info (%pe)\n", info);
276 ifbdev->helper.fb = &ifbdev->fb->base;
278 info->fbops = &intelfb_ops;
280 /* setup aperture base/size for vesafb takeover */
281 obj = intel_fb_obj(&intel_fb->base);
282 if (i915_gem_object_is_lmem(obj)) {
283 struct intel_memory_region *mem = obj->mm.region;
285 info->apertures->ranges[0].base = mem->io_start;
286 info->apertures->ranges[0].size = mem->io_size;
288 /* Use fbdev's framebuffer from lmem for discrete */
289 info->fix.smem_start =
290 (unsigned long)(mem->io_start +
291 i915_gem_object_get_dma_address(obj, 0));
292 info->fix.smem_len = obj->base.size;
294 info->apertures->ranges[0].base = ggtt->gmadr.start;
295 info->apertures->ranges[0].size = ggtt->mappable_end;
297 /* Our framebuffer is the entirety of fbdev's system memory */
298 info->fix.smem_start =
299 (unsigned long)(ggtt->gmadr.start + i915_ggtt_offset(vma));
300 info->fix.smem_len = vma->size;
303 vaddr = i915_vma_pin_iomap(vma);
305 drm_err(&dev_priv->drm,
306 "Failed to remap framebuffer into virtual memory (%pe)\n", vaddr);
307 ret = PTR_ERR(vaddr);
310 info->screen_base = vaddr;
311 info->screen_size = vma->size;
313 drm_fb_helper_fill_info(info, &ifbdev->helper, sizes);
315 /* If the object is shmemfs backed, it will have given us zeroed pages.
316 * If the object is stolen however, it will be full of whatever
317 * garbage was left in there.
319 if (!i915_gem_object_is_shmem(vma->obj) && !prealloc)
320 memset_io(info->screen_base, 0, info->screen_size);
322 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
324 drm_dbg_kms(&dev_priv->drm, "allocated %dx%d fb: 0x%08x\n",
325 ifbdev->fb->base.width, ifbdev->fb->base.height,
326 i915_ggtt_offset(vma));
328 ifbdev->vma_flags = flags;
330 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
331 vga_switcheroo_client_fb_set(pdev, info);
335 intel_unpin_fb_vma(vma, flags);
337 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
341 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
342 .fb_probe = intelfb_create,
345 static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
347 /* We rely on the object-free to release the VMA pinning for
348 * the info->screen_base mmaping. Leaking the VMA is simpler than
349 * trying to rectify all the possible error paths leading here.
352 drm_fb_helper_fini(&ifbdev->helper);
355 intel_unpin_fb_vma(ifbdev->vma, ifbdev->vma_flags);
358 drm_framebuffer_remove(&ifbdev->fb->base);
364 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
365 * The core display code will have read out the current plane configuration,
366 * so we use that to figure out if there's an object for us to use as the
367 * fb, and if so, we re-use it for the fbdev configuration.
369 * Note we only support a single fb shared across pipes for boot (mostly for
370 * fbcon), so we just find the biggest and use that.
372 static bool intel_fbdev_init_bios(struct drm_device *dev,
373 struct intel_fbdev *ifbdev)
375 struct drm_i915_private *i915 = to_i915(dev);
376 struct intel_framebuffer *fb = NULL;
377 struct intel_crtc *crtc;
378 unsigned int max_size = 0;
380 /* Find the largest fb */
381 for_each_intel_crtc(dev, crtc) {
382 struct intel_crtc_state *crtc_state =
383 to_intel_crtc_state(crtc->base.state);
384 struct intel_plane *plane =
385 to_intel_plane(crtc->base.primary);
386 struct intel_plane_state *plane_state =
387 to_intel_plane_state(plane->base.state);
388 struct drm_i915_gem_object *obj =
389 intel_fb_obj(plane_state->uapi.fb);
391 if (!crtc_state->uapi.active) {
392 drm_dbg_kms(&i915->drm,
393 "[CRTC:%d:%s] not active, skipping\n",
394 crtc->base.base.id, crtc->base.name);
399 drm_dbg_kms(&i915->drm,
400 "[PLANE:%d:%s] no fb, skipping\n",
401 plane->base.base.id, plane->base.name);
405 if (obj->base.size > max_size) {
406 drm_dbg_kms(&i915->drm,
407 "found possible fb from [PLANE:%d:%s]\n",
408 plane->base.base.id, plane->base.name);
409 fb = to_intel_framebuffer(plane_state->uapi.fb);
410 max_size = obj->base.size;
415 drm_dbg_kms(&i915->drm,
416 "no active fbs found, not using BIOS config\n");
420 /* Now make sure all the pipes will fit into it */
421 for_each_intel_crtc(dev, crtc) {
422 struct intel_crtc_state *crtc_state =
423 to_intel_crtc_state(crtc->base.state);
424 struct intel_plane *plane =
425 to_intel_plane(crtc->base.primary);
426 unsigned int cur_size;
428 if (!crtc_state->uapi.active) {
429 drm_dbg_kms(&i915->drm,
430 "[CRTC:%d:%s] not active, skipping\n",
431 crtc->base.base.id, crtc->base.name);
435 drm_dbg_kms(&i915->drm, "checking [PLANE:%d:%s] for BIOS fb\n",
436 plane->base.base.id, plane->base.name);
439 * See if the plane fb we found above will fit on this
440 * pipe. Note we need to use the selected fb's pitch and bpp
441 * rather than the current pipe's, since they differ.
443 cur_size = crtc_state->uapi.adjusted_mode.crtc_hdisplay;
444 cur_size = cur_size * fb->base.format->cpp[0];
445 if (fb->base.pitches[0] < cur_size) {
446 drm_dbg_kms(&i915->drm,
447 "fb not wide enough for [PLANE:%d:%s] (%d vs %d)\n",
448 plane->base.base.id, plane->base.name,
449 cur_size, fb->base.pitches[0]);
454 cur_size = crtc_state->uapi.adjusted_mode.crtc_vdisplay;
455 cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
456 cur_size *= fb->base.pitches[0];
457 drm_dbg_kms(&i915->drm,
458 "[CRTC:%d:%s] area: %dx%d, bpp: %d, size: %d\n",
459 crtc->base.base.id, crtc->base.name,
460 crtc_state->uapi.adjusted_mode.crtc_hdisplay,
461 crtc_state->uapi.adjusted_mode.crtc_vdisplay,
462 fb->base.format->cpp[0] * 8,
465 if (cur_size > max_size) {
466 drm_dbg_kms(&i915->drm,
467 "fb not big enough for [PLANE:%d:%s] (%d vs %d)\n",
468 plane->base.base.id, plane->base.name,
474 drm_dbg_kms(&i915->drm,
475 "fb big enough [PLANE:%d:%s] (%d >= %d)\n",
476 plane->base.base.id, plane->base.name,
481 drm_dbg_kms(&i915->drm,
482 "BIOS fb not suitable for all pipes, not using\n");
486 ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
489 drm_framebuffer_get(&ifbdev->fb->base);
491 /* Final pass to check if any active pipes don't have fbs */
492 for_each_intel_crtc(dev, crtc) {
493 struct intel_crtc_state *crtc_state =
494 to_intel_crtc_state(crtc->base.state);
495 struct intel_plane *plane =
496 to_intel_plane(crtc->base.primary);
497 struct intel_plane_state *plane_state =
498 to_intel_plane_state(plane->base.state);
500 if (!crtc_state->uapi.active)
503 drm_WARN(dev, !plane_state->uapi.fb,
504 "re-used BIOS config but lost an fb on [PLANE:%d:%s]\n",
505 plane->base.base.id, plane->base.name);
509 drm_dbg_kms(&i915->drm, "using BIOS fb for initial console\n");
517 static void intel_fbdev_suspend_worker(struct work_struct *work)
519 intel_fbdev_set_suspend(&container_of(work,
520 struct drm_i915_private,
521 display.fbdev.suspend_work)->drm,
522 FBINFO_STATE_RUNNING,
526 int intel_fbdev_init(struct drm_device *dev)
528 struct drm_i915_private *dev_priv = to_i915(dev);
529 struct intel_fbdev *ifbdev;
532 if (drm_WARN_ON(dev, !HAS_DISPLAY(dev_priv)))
535 ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
539 mutex_init(&ifbdev->hpd_lock);
540 drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
542 if (!intel_fbdev_init_bios(dev, ifbdev))
543 ifbdev->preferred_bpp = 32;
545 ret = drm_fb_helper_init(dev, &ifbdev->helper);
551 dev_priv->display.fbdev.fbdev = ifbdev;
552 INIT_WORK(&dev_priv->display.fbdev.suspend_work, intel_fbdev_suspend_worker);
557 static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
559 struct intel_fbdev *ifbdev = data;
561 /* Due to peculiar init order wrt to hpd handling this is separate. */
562 if (drm_fb_helper_initial_config(&ifbdev->helper,
563 ifbdev->preferred_bpp))
564 intel_fbdev_unregister(to_i915(ifbdev->helper.dev));
567 void intel_fbdev_initial_config_async(struct drm_device *dev)
569 struct intel_fbdev *ifbdev = to_i915(dev)->display.fbdev.fbdev;
574 ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
577 static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
582 /* Only serialises with all preceding async calls, hence +1 */
583 async_synchronize_cookie(ifbdev->cookie + 1);
587 void intel_fbdev_unregister(struct drm_i915_private *dev_priv)
589 struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
594 intel_fbdev_set_suspend(&dev_priv->drm, FBINFO_STATE_SUSPENDED, true);
596 if (!current_is_async())
597 intel_fbdev_sync(ifbdev);
599 drm_fb_helper_unregister_info(&ifbdev->helper);
602 void intel_fbdev_fini(struct drm_i915_private *dev_priv)
604 struct intel_fbdev *ifbdev = fetch_and_zero(&dev_priv->display.fbdev.fbdev);
609 intel_fbdev_destroy(ifbdev);
612 /* Suspends/resumes fbdev processing of incoming HPD events. When resuming HPD
613 * processing, fbdev will perform a full connector reprobe if a hotplug event
614 * was received while HPD was suspended.
616 static void intel_fbdev_hpd_set_suspend(struct drm_i915_private *i915, int state)
618 struct intel_fbdev *ifbdev = i915->display.fbdev.fbdev;
619 bool send_hpd = false;
621 mutex_lock(&ifbdev->hpd_lock);
622 ifbdev->hpd_suspended = state == FBINFO_STATE_SUSPENDED;
623 send_hpd = !ifbdev->hpd_suspended && ifbdev->hpd_waiting;
624 ifbdev->hpd_waiting = false;
625 mutex_unlock(&ifbdev->hpd_lock);
628 drm_dbg_kms(&i915->drm, "Handling delayed fbcon HPD event\n");
629 drm_fb_helper_hotplug_event(&ifbdev->helper);
633 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
635 struct drm_i915_private *dev_priv = to_i915(dev);
636 struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
637 struct fb_info *info;
639 if (!ifbdev || !ifbdev->vma)
642 info = ifbdev->helper.info;
645 /* Flush any pending work to turn the console on, and then
646 * wait to turn it off. It must be synchronous as we are
647 * about to suspend or unload the driver.
649 * Note that from within the work-handler, we cannot flush
650 * ourselves, so only flush outstanding work upon suspend!
652 if (state != FBINFO_STATE_RUNNING)
653 flush_work(&dev_priv->display.fbdev.suspend_work);
658 * The console lock can be pretty contented on resume due
659 * to all the printk activity. Try to keep it out of the hot
660 * path of resume if possible.
662 drm_WARN_ON(dev, state != FBINFO_STATE_RUNNING);
663 if (!console_trylock()) {
664 /* Don't block our own workqueue as this can
665 * be run in parallel with other i915.ko tasks.
667 schedule_work(&dev_priv->display.fbdev.suspend_work);
672 /* On resume from hibernation: If the object is shmemfs backed, it has
673 * been restored from swap. If the object is stolen however, it will be
674 * full of whatever garbage was left in there.
676 if (state == FBINFO_STATE_RUNNING &&
677 !i915_gem_object_is_shmem(intel_fb_obj(&ifbdev->fb->base)))
678 memset_io(info->screen_base, 0, info->screen_size);
680 drm_fb_helper_set_suspend(&ifbdev->helper, state);
684 intel_fbdev_hpd_set_suspend(dev_priv, state);
687 void intel_fbdev_output_poll_changed(struct drm_device *dev)
689 struct intel_fbdev *ifbdev = to_i915(dev)->display.fbdev.fbdev;
695 intel_fbdev_sync(ifbdev);
697 mutex_lock(&ifbdev->hpd_lock);
698 send_hpd = !ifbdev->hpd_suspended;
699 ifbdev->hpd_waiting = true;
700 mutex_unlock(&ifbdev->hpd_lock);
702 if (send_hpd && (ifbdev->vma || ifbdev->helper.deferred_setup))
703 drm_fb_helper_hotplug_event(&ifbdev->helper);
706 void intel_fbdev_restore_mode(struct drm_device *dev)
708 struct intel_fbdev *ifbdev = to_i915(dev)->display.fbdev.fbdev;
713 intel_fbdev_sync(ifbdev);
717 if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper) == 0)
718 intel_fbdev_invalidate(ifbdev);
721 struct intel_framebuffer *intel_fbdev_framebuffer(struct intel_fbdev *fbdev)
723 if (!fbdev || !fbdev->helper.fb)
726 return to_intel_framebuffer(fbdev->helper.fb);