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>
44 #include "gem/i915_gem_lmem.h"
47 #include "intel_display_types.h"
49 #include "intel_fb_pin.h"
50 #include "intel_fbdev.h"
51 #include "intel_frontbuffer.h"
54 struct drm_fb_helper helper;
55 struct intel_framebuffer *fb;
57 unsigned long vma_flags;
58 async_cookie_t cookie;
61 /* Whether or not fbdev hpd processing is temporarily suspended */
62 bool hpd_suspended: 1;
63 /* Set when a hotplug was received while HPD processing was suspended */
66 /* Protects hpd_suspended */
67 struct mutex hpd_lock;
70 static struct intel_frontbuffer *to_frontbuffer(struct intel_fbdev *ifbdev)
72 return ifbdev->fb->frontbuffer;
75 static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev)
77 intel_frontbuffer_invalidate(to_frontbuffer(ifbdev), ORIGIN_CPU);
80 static int intel_fbdev_set_par(struct fb_info *info)
82 struct drm_fb_helper *fb_helper = info->par;
83 struct intel_fbdev *ifbdev =
84 container_of(fb_helper, struct intel_fbdev, helper);
87 ret = drm_fb_helper_set_par(info);
89 intel_fbdev_invalidate(ifbdev);
94 static int intel_fbdev_blank(int blank, struct fb_info *info)
96 struct drm_fb_helper *fb_helper = info->par;
97 struct intel_fbdev *ifbdev =
98 container_of(fb_helper, struct intel_fbdev, helper);
101 ret = drm_fb_helper_blank(blank, info);
103 intel_fbdev_invalidate(ifbdev);
108 static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
109 struct fb_info *info)
111 struct drm_fb_helper *fb_helper = info->par;
112 struct intel_fbdev *ifbdev =
113 container_of(fb_helper, struct intel_fbdev, helper);
116 ret = drm_fb_helper_pan_display(var, info);
118 intel_fbdev_invalidate(ifbdev);
123 static const struct fb_ops intelfb_ops = {
124 .owner = THIS_MODULE,
125 DRM_FB_HELPER_DEFAULT_OPS,
126 .fb_set_par = intel_fbdev_set_par,
127 .fb_read = drm_fb_helper_cfb_read,
128 .fb_write = drm_fb_helper_cfb_write,
129 .fb_fillrect = drm_fb_helper_cfb_fillrect,
130 .fb_copyarea = drm_fb_helper_cfb_copyarea,
131 .fb_imageblit = drm_fb_helper_cfb_imageblit,
132 .fb_pan_display = intel_fbdev_pan_display,
133 .fb_blank = intel_fbdev_blank,
136 static int intelfb_alloc(struct drm_fb_helper *helper,
137 struct drm_fb_helper_surface_size *sizes)
139 struct intel_fbdev *ifbdev =
140 container_of(helper, struct intel_fbdev, helper);
141 struct drm_framebuffer *fb;
142 struct drm_device *dev = helper->dev;
143 struct drm_i915_private *dev_priv = to_i915(dev);
144 struct drm_mode_fb_cmd2 mode_cmd = {};
145 struct drm_i915_gem_object *obj;
148 /* we don't do packed 24bpp */
149 if (sizes->surface_bpp == 24)
150 sizes->surface_bpp = 32;
152 mode_cmd.width = sizes->surface_width;
153 mode_cmd.height = sizes->surface_height;
155 mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
156 DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
157 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
158 sizes->surface_depth);
160 size = mode_cmd.pitches[0] * mode_cmd.height;
161 size = PAGE_ALIGN(size);
163 obj = ERR_PTR(-ENODEV);
164 if (HAS_LMEM(dev_priv)) {
165 obj = i915_gem_object_create_lmem(dev_priv, size,
166 I915_BO_ALLOC_CONTIGUOUS);
169 * If the FB is too big, just don't use it since fbdev is not very
170 * important and we should probably use that space with FBC or other
173 if (size * 2 < dev_priv->stolen_usable_size)
174 obj = i915_gem_object_create_stolen(dev_priv, size);
176 obj = i915_gem_object_create_shmem(dev_priv, size);
180 drm_err(&dev_priv->drm, "failed to allocate framebuffer (%pe)\n", obj);
184 fb = intel_framebuffer_create(obj, &mode_cmd);
185 i915_gem_object_put(obj);
189 ifbdev->fb = to_intel_framebuffer(fb);
193 static int intelfb_create(struct drm_fb_helper *helper,
194 struct drm_fb_helper_surface_size *sizes)
196 struct intel_fbdev *ifbdev =
197 container_of(helper, struct intel_fbdev, helper);
198 struct intel_framebuffer *intel_fb = ifbdev->fb;
199 struct drm_device *dev = helper->dev;
200 struct drm_i915_private *dev_priv = to_i915(dev);
201 struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
202 struct i915_ggtt *ggtt = to_gt(dev_priv)->ggtt;
203 const struct i915_gtt_view view = {
204 .type = I915_GTT_VIEW_NORMAL,
206 intel_wakeref_t wakeref;
207 struct fb_info *info;
208 struct i915_vma *vma;
209 unsigned long flags = 0;
210 bool prealloc = false;
212 struct drm_i915_gem_object *obj;
215 mutex_lock(&ifbdev->hpd_lock);
216 ret = ifbdev->hpd_suspended ? -EAGAIN : 0;
217 mutex_unlock(&ifbdev->hpd_lock);
222 (sizes->fb_width > intel_fb->base.width ||
223 sizes->fb_height > intel_fb->base.height)) {
224 drm_dbg_kms(&dev_priv->drm,
225 "BIOS fb too small (%dx%d), we require (%dx%d),"
227 intel_fb->base.width, intel_fb->base.height,
228 sizes->fb_width, sizes->fb_height);
229 drm_framebuffer_put(&intel_fb->base);
230 intel_fb = ifbdev->fb = NULL;
232 if (!intel_fb || drm_WARN_ON(dev, !intel_fb_obj(&intel_fb->base))) {
233 drm_dbg_kms(&dev_priv->drm,
234 "no BIOS fb, allocating a new one\n");
235 ret = intelfb_alloc(helper, sizes);
238 intel_fb = ifbdev->fb;
240 drm_dbg_kms(&dev_priv->drm, "re-using BIOS fb\n");
242 sizes->fb_width = intel_fb->base.width;
243 sizes->fb_height = intel_fb->base.height;
246 wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
248 /* Pin the GGTT vma for our access via info->screen_base.
249 * This also validates that any existing fb inherited from the
250 * BIOS is suitable for own access.
252 vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base, false,
253 &view, false, &flags);
259 info = drm_fb_helper_alloc_info(helper);
261 drm_err(&dev_priv->drm, "Failed to allocate fb_info (%pe)\n", info);
266 ifbdev->helper.fb = &ifbdev->fb->base;
268 info->fbops = &intelfb_ops;
270 /* setup aperture base/size for vesafb takeover */
271 obj = intel_fb_obj(&intel_fb->base);
272 if (i915_gem_object_is_lmem(obj)) {
273 struct intel_memory_region *mem = obj->mm.region;
275 info->apertures->ranges[0].base = mem->io_start;
276 info->apertures->ranges[0].size = mem->io_size;
278 /* Use fbdev's framebuffer from lmem for discrete */
279 info->fix.smem_start =
280 (unsigned long)(mem->io_start +
281 i915_gem_object_get_dma_address(obj, 0));
282 info->fix.smem_len = obj->base.size;
284 info->apertures->ranges[0].base = ggtt->gmadr.start;
285 info->apertures->ranges[0].size = ggtt->mappable_end;
287 /* Our framebuffer is the entirety of fbdev's system memory */
288 info->fix.smem_start =
289 (unsigned long)(ggtt->gmadr.start + vma->node.start);
290 info->fix.smem_len = vma->size;
293 vaddr = i915_vma_pin_iomap(vma);
295 drm_err(&dev_priv->drm,
296 "Failed to remap framebuffer into virtual memory (%pe)\n", vaddr);
297 ret = PTR_ERR(vaddr);
300 info->screen_base = vaddr;
301 info->screen_size = vma->size;
303 drm_fb_helper_fill_info(info, &ifbdev->helper, sizes);
305 /* If the object is shmemfs backed, it will have given us zeroed pages.
306 * If the object is stolen however, it will be full of whatever
307 * garbage was left in there.
309 if (!i915_gem_object_is_shmem(vma->obj) && !prealloc)
310 memset_io(info->screen_base, 0, info->screen_size);
312 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
314 drm_dbg_kms(&dev_priv->drm, "allocated %dx%d fb: 0x%08x\n",
315 ifbdev->fb->base.width, ifbdev->fb->base.height,
316 i915_ggtt_offset(vma));
318 ifbdev->vma_flags = flags;
320 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
321 vga_switcheroo_client_fb_set(pdev, info);
325 intel_unpin_fb_vma(vma, flags);
327 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
331 static int intelfb_dirty(struct drm_fb_helper *helper, struct drm_clip_rect *clip)
333 if (!(clip->x1 < clip->x2 && clip->y1 < clip->y2))
336 if (helper->fb->funcs->dirty)
337 return helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, clip, 1);
342 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
343 .fb_probe = intelfb_create,
344 .fb_dirty = intelfb_dirty,
347 static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
349 /* We rely on the object-free to release the VMA pinning for
350 * the info->screen_base mmaping. Leaking the VMA is simpler than
351 * trying to rectify all the possible error paths leading here.
354 drm_fb_helper_fini(&ifbdev->helper);
357 intel_unpin_fb_vma(ifbdev->vma, ifbdev->vma_flags);
360 drm_framebuffer_remove(&ifbdev->fb->base);
366 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
367 * The core display code will have read out the current plane configuration,
368 * so we use that to figure out if there's an object for us to use as the
369 * fb, and if so, we re-use it for the fbdev configuration.
371 * Note we only support a single fb shared across pipes for boot (mostly for
372 * fbcon), so we just find the biggest and use that.
374 static bool intel_fbdev_init_bios(struct drm_device *dev,
375 struct intel_fbdev *ifbdev)
377 struct drm_i915_private *i915 = to_i915(dev);
378 struct intel_framebuffer *fb = NULL;
379 struct intel_crtc *crtc;
380 unsigned int max_size = 0;
382 /* Find the largest fb */
383 for_each_intel_crtc(dev, crtc) {
384 struct intel_crtc_state *crtc_state =
385 to_intel_crtc_state(crtc->base.state);
386 struct intel_plane *plane =
387 to_intel_plane(crtc->base.primary);
388 struct intel_plane_state *plane_state =
389 to_intel_plane_state(plane->base.state);
390 struct drm_i915_gem_object *obj =
391 intel_fb_obj(plane_state->uapi.fb);
393 if (!crtc_state->uapi.active) {
394 drm_dbg_kms(&i915->drm,
395 "[CRTC:%d:%s] not active, skipping\n",
396 crtc->base.base.id, crtc->base.name);
401 drm_dbg_kms(&i915->drm,
402 "[PLANE:%d:%s] no fb, skipping\n",
403 plane->base.base.id, plane->base.name);
407 if (obj->base.size > max_size) {
408 drm_dbg_kms(&i915->drm,
409 "found possible fb from [PLANE:%d:%s]\n",
410 plane->base.base.id, plane->base.name);
411 fb = to_intel_framebuffer(plane_state->uapi.fb);
412 max_size = obj->base.size;
417 drm_dbg_kms(&i915->drm,
418 "no active fbs found, not using BIOS config\n");
422 /* Now make sure all the pipes will fit into it */
423 for_each_intel_crtc(dev, crtc) {
424 struct intel_crtc_state *crtc_state =
425 to_intel_crtc_state(crtc->base.state);
426 struct intel_plane *plane =
427 to_intel_plane(crtc->base.primary);
428 unsigned int cur_size;
430 if (!crtc_state->uapi.active) {
431 drm_dbg_kms(&i915->drm,
432 "[CRTC:%d:%s] not active, skipping\n",
433 crtc->base.base.id, crtc->base.name);
437 drm_dbg_kms(&i915->drm, "checking [PLANE:%d:%s] for BIOS fb\n",
438 plane->base.base.id, plane->base.name);
441 * See if the plane fb we found above will fit on this
442 * pipe. Note we need to use the selected fb's pitch and bpp
443 * rather than the current pipe's, since they differ.
445 cur_size = crtc_state->uapi.adjusted_mode.crtc_hdisplay;
446 cur_size = cur_size * fb->base.format->cpp[0];
447 if (fb->base.pitches[0] < cur_size) {
448 drm_dbg_kms(&i915->drm,
449 "fb not wide enough for [PLANE:%d:%s] (%d vs %d)\n",
450 plane->base.base.id, plane->base.name,
451 cur_size, fb->base.pitches[0]);
456 cur_size = crtc_state->uapi.adjusted_mode.crtc_vdisplay;
457 cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
458 cur_size *= fb->base.pitches[0];
459 drm_dbg_kms(&i915->drm,
460 "[CRTC:%d:%s] area: %dx%d, bpp: %d, size: %d\n",
461 crtc->base.base.id, crtc->base.name,
462 crtc_state->uapi.adjusted_mode.crtc_hdisplay,
463 crtc_state->uapi.adjusted_mode.crtc_vdisplay,
464 fb->base.format->cpp[0] * 8,
467 if (cur_size > max_size) {
468 drm_dbg_kms(&i915->drm,
469 "fb not big enough for [PLANE:%d:%s] (%d vs %d)\n",
470 plane->base.base.id, plane->base.name,
476 drm_dbg_kms(&i915->drm,
477 "fb big enough [PLANE:%d:%s] (%d >= %d)\n",
478 plane->base.base.id, plane->base.name,
483 drm_dbg_kms(&i915->drm,
484 "BIOS fb not suitable for all pipes, not using\n");
488 ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
491 drm_framebuffer_get(&ifbdev->fb->base);
493 /* Final pass to check if any active pipes don't have fbs */
494 for_each_intel_crtc(dev, crtc) {
495 struct intel_crtc_state *crtc_state =
496 to_intel_crtc_state(crtc->base.state);
497 struct intel_plane *plane =
498 to_intel_plane(crtc->base.primary);
499 struct intel_plane_state *plane_state =
500 to_intel_plane_state(plane->base.state);
502 if (!crtc_state->uapi.active)
505 drm_WARN(dev, !plane_state->uapi.fb,
506 "re-used BIOS config but lost an fb on [PLANE:%d:%s]\n",
507 plane->base.base.id, plane->base.name);
511 drm_dbg_kms(&i915->drm, "using BIOS fb for initial console\n");
519 static void intel_fbdev_suspend_worker(struct work_struct *work)
521 intel_fbdev_set_suspend(&container_of(work,
522 struct drm_i915_private,
523 display.fbdev.suspend_work)->drm,
524 FBINFO_STATE_RUNNING,
528 int intel_fbdev_init(struct drm_device *dev)
530 struct drm_i915_private *dev_priv = to_i915(dev);
531 struct intel_fbdev *ifbdev;
534 if (drm_WARN_ON(dev, !HAS_DISPLAY(dev_priv)))
537 ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
541 mutex_init(&ifbdev->hpd_lock);
542 drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
544 if (!intel_fbdev_init_bios(dev, ifbdev))
545 ifbdev->preferred_bpp = 32;
547 ret = drm_fb_helper_init(dev, &ifbdev->helper);
553 dev_priv->display.fbdev.fbdev = ifbdev;
554 INIT_WORK(&dev_priv->display.fbdev.suspend_work, intel_fbdev_suspend_worker);
559 static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
561 struct intel_fbdev *ifbdev = data;
563 /* Due to peculiar init order wrt to hpd handling this is separate. */
564 if (drm_fb_helper_initial_config(&ifbdev->helper,
565 ifbdev->preferred_bpp))
566 intel_fbdev_unregister(to_i915(ifbdev->helper.dev));
569 void intel_fbdev_initial_config_async(struct drm_device *dev)
571 struct intel_fbdev *ifbdev = to_i915(dev)->display.fbdev.fbdev;
576 ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
579 static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
584 /* Only serialises with all preceding async calls, hence +1 */
585 async_synchronize_cookie(ifbdev->cookie + 1);
589 void intel_fbdev_unregister(struct drm_i915_private *dev_priv)
591 struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
596 intel_fbdev_set_suspend(&dev_priv->drm, FBINFO_STATE_SUSPENDED, true);
598 if (!current_is_async())
599 intel_fbdev_sync(ifbdev);
601 drm_fb_helper_unregister_info(&ifbdev->helper);
604 void intel_fbdev_fini(struct drm_i915_private *dev_priv)
606 struct intel_fbdev *ifbdev = fetch_and_zero(&dev_priv->display.fbdev.fbdev);
611 intel_fbdev_destroy(ifbdev);
614 /* Suspends/resumes fbdev processing of incoming HPD events. When resuming HPD
615 * processing, fbdev will perform a full connector reprobe if a hotplug event
616 * was received while HPD was suspended.
618 static void intel_fbdev_hpd_set_suspend(struct drm_i915_private *i915, int state)
620 struct intel_fbdev *ifbdev = i915->display.fbdev.fbdev;
621 bool send_hpd = false;
623 mutex_lock(&ifbdev->hpd_lock);
624 ifbdev->hpd_suspended = state == FBINFO_STATE_SUSPENDED;
625 send_hpd = !ifbdev->hpd_suspended && ifbdev->hpd_waiting;
626 ifbdev->hpd_waiting = false;
627 mutex_unlock(&ifbdev->hpd_lock);
630 drm_dbg_kms(&i915->drm, "Handling delayed fbcon HPD event\n");
631 drm_fb_helper_hotplug_event(&ifbdev->helper);
635 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
637 struct drm_i915_private *dev_priv = to_i915(dev);
638 struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
639 struct fb_info *info;
641 if (!ifbdev || !ifbdev->vma)
644 info = ifbdev->helper.info;
647 /* Flush any pending work to turn the console on, and then
648 * wait to turn it off. It must be synchronous as we are
649 * about to suspend or unload the driver.
651 * Note that from within the work-handler, we cannot flush
652 * ourselves, so only flush outstanding work upon suspend!
654 if (state != FBINFO_STATE_RUNNING)
655 flush_work(&dev_priv->display.fbdev.suspend_work);
660 * The console lock can be pretty contented on resume due
661 * to all the printk activity. Try to keep it out of the hot
662 * path of resume if possible.
664 drm_WARN_ON(dev, state != FBINFO_STATE_RUNNING);
665 if (!console_trylock()) {
666 /* Don't block our own workqueue as this can
667 * be run in parallel with other i915.ko tasks.
669 schedule_work(&dev_priv->display.fbdev.suspend_work);
674 /* On resume from hibernation: If the object is shmemfs backed, it has
675 * been restored from swap. If the object is stolen however, it will be
676 * full of whatever garbage was left in there.
678 if (state == FBINFO_STATE_RUNNING &&
679 !i915_gem_object_is_shmem(intel_fb_obj(&ifbdev->fb->base)))
680 memset_io(info->screen_base, 0, info->screen_size);
682 drm_fb_helper_set_suspend(&ifbdev->helper, state);
686 intel_fbdev_hpd_set_suspend(dev_priv, state);
689 void intel_fbdev_output_poll_changed(struct drm_device *dev)
691 struct intel_fbdev *ifbdev = to_i915(dev)->display.fbdev.fbdev;
697 intel_fbdev_sync(ifbdev);
699 mutex_lock(&ifbdev->hpd_lock);
700 send_hpd = !ifbdev->hpd_suspended;
701 ifbdev->hpd_waiting = true;
702 mutex_unlock(&ifbdev->hpd_lock);
704 if (send_hpd && (ifbdev->vma || ifbdev->helper.deferred_setup))
705 drm_fb_helper_hotplug_event(&ifbdev->helper);
708 void intel_fbdev_restore_mode(struct drm_device *dev)
710 struct intel_fbdev *ifbdev = to_i915(dev)->display.fbdev.fbdev;
715 intel_fbdev_sync(ifbdev);
719 if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper) == 0)
720 intel_fbdev_invalidate(ifbdev);
723 struct intel_framebuffer *intel_fbdev_framebuffer(struct intel_fbdev *fbdev)
725 if (!fbdev || !fbdev->helper.fb)
728 return to_intel_framebuffer(fbdev->helper.fb);