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_fbdev.h"
50 #include "intel_frontbuffer.h"
52 static struct intel_frontbuffer *to_frontbuffer(struct intel_fbdev *ifbdev)
54 return ifbdev->fb->frontbuffer;
57 static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev)
59 intel_frontbuffer_invalidate(to_frontbuffer(ifbdev), ORIGIN_CPU);
62 static int intel_fbdev_set_par(struct fb_info *info)
64 struct drm_fb_helper *fb_helper = info->par;
65 struct intel_fbdev *ifbdev =
66 container_of(fb_helper, struct intel_fbdev, helper);
69 ret = drm_fb_helper_set_par(info);
71 intel_fbdev_invalidate(ifbdev);
76 static int intel_fbdev_blank(int blank, struct fb_info *info)
78 struct drm_fb_helper *fb_helper = info->par;
79 struct intel_fbdev *ifbdev =
80 container_of(fb_helper, struct intel_fbdev, helper);
83 ret = drm_fb_helper_blank(blank, info);
85 intel_fbdev_invalidate(ifbdev);
90 static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
93 struct drm_fb_helper *fb_helper = info->par;
94 struct intel_fbdev *ifbdev =
95 container_of(fb_helper, struct intel_fbdev, helper);
98 ret = drm_fb_helper_pan_display(var, info);
100 intel_fbdev_invalidate(ifbdev);
105 static const struct fb_ops intelfb_ops = {
106 .owner = THIS_MODULE,
107 DRM_FB_HELPER_DEFAULT_OPS,
108 .fb_set_par = intel_fbdev_set_par,
109 .fb_fillrect = drm_fb_helper_cfb_fillrect,
110 .fb_copyarea = drm_fb_helper_cfb_copyarea,
111 .fb_imageblit = drm_fb_helper_cfb_imageblit,
112 .fb_pan_display = intel_fbdev_pan_display,
113 .fb_blank = intel_fbdev_blank,
116 static int intelfb_alloc(struct drm_fb_helper *helper,
117 struct drm_fb_helper_surface_size *sizes)
119 struct intel_fbdev *ifbdev =
120 container_of(helper, struct intel_fbdev, helper);
121 struct drm_framebuffer *fb;
122 struct drm_device *dev = helper->dev;
123 struct drm_i915_private *dev_priv = to_i915(dev);
124 struct drm_mode_fb_cmd2 mode_cmd = {};
125 struct drm_i915_gem_object *obj;
128 /* we don't do packed 24bpp */
129 if (sizes->surface_bpp == 24)
130 sizes->surface_bpp = 32;
132 mode_cmd.width = sizes->surface_width;
133 mode_cmd.height = sizes->surface_height;
135 mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
136 DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
137 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
138 sizes->surface_depth);
140 size = mode_cmd.pitches[0] * mode_cmd.height;
141 size = PAGE_ALIGN(size);
143 obj = ERR_PTR(-ENODEV);
144 if (HAS_LMEM(dev_priv)) {
145 obj = i915_gem_object_create_lmem(dev_priv, size,
146 I915_BO_ALLOC_CONTIGUOUS);
149 * If the FB is too big, just don't use it since fbdev is not very
150 * important and we should probably use that space with FBC or other
153 if (size * 2 < dev_priv->stolen_usable_size)
154 obj = i915_gem_object_create_stolen(dev_priv, size);
156 obj = i915_gem_object_create_shmem(dev_priv, size);
160 drm_err(&dev_priv->drm, "failed to allocate framebuffer\n");
164 fb = intel_framebuffer_create(obj, &mode_cmd);
165 i915_gem_object_put(obj);
169 ifbdev->fb = to_intel_framebuffer(fb);
173 static int intelfb_create(struct drm_fb_helper *helper,
174 struct drm_fb_helper_surface_size *sizes)
176 struct intel_fbdev *ifbdev =
177 container_of(helper, struct intel_fbdev, helper);
178 struct intel_framebuffer *intel_fb = ifbdev->fb;
179 struct drm_device *dev = helper->dev;
180 struct drm_i915_private *dev_priv = to_i915(dev);
181 struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
182 struct i915_ggtt *ggtt = &dev_priv->ggtt;
183 const struct i915_ggtt_view view = {
184 .type = I915_GGTT_VIEW_NORMAL,
186 intel_wakeref_t wakeref;
187 struct fb_info *info;
188 struct i915_vma *vma;
189 unsigned long flags = 0;
190 bool prealloc = false;
192 struct drm_i915_gem_object *obj;
196 (sizes->fb_width > intel_fb->base.width ||
197 sizes->fb_height > intel_fb->base.height)) {
198 drm_dbg_kms(&dev_priv->drm,
199 "BIOS fb too small (%dx%d), we require (%dx%d),"
201 intel_fb->base.width, intel_fb->base.height,
202 sizes->fb_width, sizes->fb_height);
203 drm_framebuffer_put(&intel_fb->base);
204 intel_fb = ifbdev->fb = NULL;
206 if (!intel_fb || drm_WARN_ON(dev, !intel_fb_obj(&intel_fb->base))) {
207 drm_dbg_kms(&dev_priv->drm,
208 "no BIOS fb, allocating a new one\n");
209 ret = intelfb_alloc(helper, sizes);
212 intel_fb = ifbdev->fb;
214 drm_dbg_kms(&dev_priv->drm, "re-using BIOS fb\n");
216 sizes->fb_width = intel_fb->base.width;
217 sizes->fb_height = intel_fb->base.height;
220 wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
222 /* Pin the GGTT vma for our access via info->screen_base.
223 * This also validates that any existing fb inherited from the
224 * BIOS is suitable for own access.
226 vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base, false,
227 &view, false, &flags);
233 intel_frontbuffer_flush(to_frontbuffer(ifbdev), ORIGIN_DIRTYFB);
235 info = drm_fb_helper_alloc_fbi(helper);
237 drm_err(&dev_priv->drm, "Failed to allocate fb_info\n");
242 ifbdev->helper.fb = &ifbdev->fb->base;
244 info->fbops = &intelfb_ops;
246 /* setup aperture base/size for vesafb takeover */
247 obj = intel_fb_obj(&intel_fb->base);
248 if (i915_gem_object_is_lmem(obj)) {
249 struct intel_memory_region *mem = obj->mm.region;
251 info->apertures->ranges[0].base = mem->io_start;
252 info->apertures->ranges[0].size = mem->total;
254 /* Use fbdev's framebuffer from lmem for discrete */
255 info->fix.smem_start =
256 (unsigned long)(mem->io_start +
257 i915_gem_object_get_dma_address(obj, 0));
258 info->fix.smem_len = obj->base.size;
260 info->apertures->ranges[0].base = ggtt->gmadr.start;
261 info->apertures->ranges[0].size = ggtt->mappable_end;
263 /* Our framebuffer is the entirety of fbdev's system memory */
264 info->fix.smem_start =
265 (unsigned long)(ggtt->gmadr.start + vma->node.start);
266 info->fix.smem_len = vma->node.size;
269 vaddr = i915_vma_pin_iomap(vma);
271 drm_err(&dev_priv->drm,
272 "Failed to remap framebuffer into virtual memory\n");
273 ret = PTR_ERR(vaddr);
276 info->screen_base = vaddr;
277 info->screen_size = vma->node.size;
279 drm_fb_helper_fill_info(info, &ifbdev->helper, sizes);
281 /* If the object is shmemfs backed, it will have given us zeroed pages.
282 * If the object is stolen however, it will be full of whatever
283 * garbage was left in there.
285 if (!i915_gem_object_is_shmem(vma->obj) && !prealloc)
286 memset_io(info->screen_base, 0, info->screen_size);
288 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
290 drm_dbg_kms(&dev_priv->drm, "allocated %dx%d fb: 0x%08x\n",
291 ifbdev->fb->base.width, ifbdev->fb->base.height,
292 i915_ggtt_offset(vma));
294 ifbdev->vma_flags = flags;
296 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
297 vga_switcheroo_client_fb_set(pdev, info);
301 intel_unpin_fb_vma(vma, flags);
303 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
307 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
308 .fb_probe = intelfb_create,
311 static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
313 /* We rely on the object-free to release the VMA pinning for
314 * the info->screen_base mmaping. Leaking the VMA is simpler than
315 * trying to rectify all the possible error paths leading here.
318 drm_fb_helper_fini(&ifbdev->helper);
321 intel_unpin_fb_vma(ifbdev->vma, ifbdev->vma_flags);
324 drm_framebuffer_remove(&ifbdev->fb->base);
330 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
331 * The core display code will have read out the current plane configuration,
332 * so we use that to figure out if there's an object for us to use as the
333 * fb, and if so, we re-use it for the fbdev configuration.
335 * Note we only support a single fb shared across pipes for boot (mostly for
336 * fbcon), so we just find the biggest and use that.
338 static bool intel_fbdev_init_bios(struct drm_device *dev,
339 struct intel_fbdev *ifbdev)
341 struct drm_i915_private *i915 = to_i915(dev);
342 struct intel_framebuffer *fb = NULL;
343 struct intel_crtc *crtc;
344 unsigned int max_size = 0;
346 /* Find the largest fb */
347 for_each_intel_crtc(dev, crtc) {
348 struct intel_crtc_state *crtc_state =
349 to_intel_crtc_state(crtc->base.state);
350 struct intel_plane *plane =
351 to_intel_plane(crtc->base.primary);
352 struct intel_plane_state *plane_state =
353 to_intel_plane_state(plane->base.state);
354 struct drm_i915_gem_object *obj =
355 intel_fb_obj(plane_state->uapi.fb);
357 if (!crtc_state->uapi.active) {
358 drm_dbg_kms(&i915->drm,
359 "[CRTC:%d:%s] not active, skipping\n",
360 crtc->base.base.id, crtc->base.name);
365 drm_dbg_kms(&i915->drm,
366 "[PLANE:%d:%s] no fb, skipping\n",
367 plane->base.base.id, plane->base.name);
371 if (obj->base.size > max_size) {
372 drm_dbg_kms(&i915->drm,
373 "found possible fb from [PLANE:%d:%s]\n",
374 plane->base.base.id, plane->base.name);
375 fb = to_intel_framebuffer(plane_state->uapi.fb);
376 max_size = obj->base.size;
381 drm_dbg_kms(&i915->drm,
382 "no active fbs found, not using BIOS config\n");
386 /* Now make sure all the pipes will fit into it */
387 for_each_intel_crtc(dev, crtc) {
388 struct intel_crtc_state *crtc_state =
389 to_intel_crtc_state(crtc->base.state);
390 struct intel_plane *plane =
391 to_intel_plane(crtc->base.primary);
392 unsigned int cur_size;
394 if (!crtc_state->uapi.active) {
395 drm_dbg_kms(&i915->drm,
396 "[CRTC:%d:%s] not active, skipping\n",
397 crtc->base.base.id, crtc->base.name);
401 drm_dbg_kms(&i915->drm, "checking [PLANE:%d:%s] for BIOS fb\n",
402 plane->base.base.id, plane->base.name);
405 * See if the plane fb we found above will fit on this
406 * pipe. Note we need to use the selected fb's pitch and bpp
407 * rather than the current pipe's, since they differ.
409 cur_size = crtc_state->uapi.adjusted_mode.crtc_hdisplay;
410 cur_size = cur_size * fb->base.format->cpp[0];
411 if (fb->base.pitches[0] < cur_size) {
412 drm_dbg_kms(&i915->drm,
413 "fb not wide enough for [PLANE:%d:%s] (%d vs %d)\n",
414 plane->base.base.id, plane->base.name,
415 cur_size, fb->base.pitches[0]);
420 cur_size = crtc_state->uapi.adjusted_mode.crtc_vdisplay;
421 cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
422 cur_size *= fb->base.pitches[0];
423 drm_dbg_kms(&i915->drm,
424 "[CRTC:%d:%s] area: %dx%d, bpp: %d, size: %d\n",
425 crtc->base.base.id, crtc->base.name,
426 crtc_state->uapi.adjusted_mode.crtc_hdisplay,
427 crtc_state->uapi.adjusted_mode.crtc_vdisplay,
428 fb->base.format->cpp[0] * 8,
431 if (cur_size > max_size) {
432 drm_dbg_kms(&i915->drm,
433 "fb not big enough for [PLANE:%d:%s] (%d vs %d)\n",
434 plane->base.base.id, plane->base.name,
440 drm_dbg_kms(&i915->drm,
441 "fb big enough [PLANE:%d:%s] (%d >= %d)\n",
442 plane->base.base.id, plane->base.name,
447 drm_dbg_kms(&i915->drm,
448 "BIOS fb not suitable for all pipes, not using\n");
452 ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
455 drm_framebuffer_get(&ifbdev->fb->base);
457 /* Final pass to check if any active pipes don't have fbs */
458 for_each_intel_crtc(dev, crtc) {
459 struct intel_crtc_state *crtc_state =
460 to_intel_crtc_state(crtc->base.state);
461 struct intel_plane *plane =
462 to_intel_plane(crtc->base.primary);
463 struct intel_plane_state *plane_state =
464 to_intel_plane_state(plane->base.state);
466 if (!crtc_state->uapi.active)
469 drm_WARN(dev, !plane_state->uapi.fb,
470 "re-used BIOS config but lost an fb on [PLANE:%d:%s]\n",
471 plane->base.base.id, plane->base.name);
475 drm_dbg_kms(&i915->drm, "using BIOS fb for initial console\n");
483 static void intel_fbdev_suspend_worker(struct work_struct *work)
485 intel_fbdev_set_suspend(&container_of(work,
486 struct drm_i915_private,
487 fbdev_suspend_work)->drm,
488 FBINFO_STATE_RUNNING,
492 int intel_fbdev_init(struct drm_device *dev)
494 struct drm_i915_private *dev_priv = to_i915(dev);
495 struct intel_fbdev *ifbdev;
498 if (drm_WARN_ON(dev, !HAS_DISPLAY(dev_priv)))
501 ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
505 mutex_init(&ifbdev->hpd_lock);
506 drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
508 if (!intel_fbdev_init_bios(dev, ifbdev))
509 ifbdev->preferred_bpp = 32;
511 ret = drm_fb_helper_init(dev, &ifbdev->helper);
517 dev_priv->fbdev = ifbdev;
518 INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
523 static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
525 struct intel_fbdev *ifbdev = data;
527 /* Due to peculiar init order wrt to hpd handling this is separate. */
528 if (drm_fb_helper_initial_config(&ifbdev->helper,
529 ifbdev->preferred_bpp))
530 intel_fbdev_unregister(to_i915(ifbdev->helper.dev));
533 void intel_fbdev_initial_config_async(struct drm_device *dev)
535 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
540 ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
543 static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
548 /* Only serialises with all preceding async calls, hence +1 */
549 async_synchronize_cookie(ifbdev->cookie + 1);
553 void intel_fbdev_unregister(struct drm_i915_private *dev_priv)
555 struct intel_fbdev *ifbdev = dev_priv->fbdev;
560 cancel_work_sync(&dev_priv->fbdev_suspend_work);
561 if (!current_is_async())
562 intel_fbdev_sync(ifbdev);
564 drm_fb_helper_unregister_fbi(&ifbdev->helper);
567 void intel_fbdev_fini(struct drm_i915_private *dev_priv)
569 struct intel_fbdev *ifbdev = fetch_and_zero(&dev_priv->fbdev);
574 intel_fbdev_destroy(ifbdev);
577 /* Suspends/resumes fbdev processing of incoming HPD events. When resuming HPD
578 * processing, fbdev will perform a full connector reprobe if a hotplug event
579 * was received while HPD was suspended.
581 static void intel_fbdev_hpd_set_suspend(struct drm_i915_private *i915, int state)
583 struct intel_fbdev *ifbdev = i915->fbdev;
584 bool send_hpd = false;
586 mutex_lock(&ifbdev->hpd_lock);
587 ifbdev->hpd_suspended = state == FBINFO_STATE_SUSPENDED;
588 send_hpd = !ifbdev->hpd_suspended && ifbdev->hpd_waiting;
589 ifbdev->hpd_waiting = false;
590 mutex_unlock(&ifbdev->hpd_lock);
593 drm_dbg_kms(&i915->drm, "Handling delayed fbcon HPD event\n");
594 drm_fb_helper_hotplug_event(&ifbdev->helper);
598 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
600 struct drm_i915_private *dev_priv = to_i915(dev);
601 struct intel_fbdev *ifbdev = dev_priv->fbdev;
602 struct fb_info *info;
604 if (!ifbdev || !ifbdev->vma)
607 info = ifbdev->helper.fbdev;
610 /* Flush any pending work to turn the console on, and then
611 * wait to turn it off. It must be synchronous as we are
612 * about to suspend or unload the driver.
614 * Note that from within the work-handler, we cannot flush
615 * ourselves, so only flush outstanding work upon suspend!
617 if (state != FBINFO_STATE_RUNNING)
618 flush_work(&dev_priv->fbdev_suspend_work);
623 * The console lock can be pretty contented on resume due
624 * to all the printk activity. Try to keep it out of the hot
625 * path of resume if possible.
627 drm_WARN_ON(dev, state != FBINFO_STATE_RUNNING);
628 if (!console_trylock()) {
629 /* Don't block our own workqueue as this can
630 * be run in parallel with other i915.ko tasks.
632 schedule_work(&dev_priv->fbdev_suspend_work);
637 /* On resume from hibernation: If the object is shmemfs backed, it has
638 * been restored from swap. If the object is stolen however, it will be
639 * full of whatever garbage was left in there.
641 if (state == FBINFO_STATE_RUNNING &&
642 !i915_gem_object_is_shmem(intel_fb_obj(&ifbdev->fb->base)))
643 memset_io(info->screen_base, 0, info->screen_size);
645 drm_fb_helper_set_suspend(&ifbdev->helper, state);
648 intel_fbdev_hpd_set_suspend(dev_priv, state);
651 void intel_fbdev_output_poll_changed(struct drm_device *dev)
653 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
659 intel_fbdev_sync(ifbdev);
661 mutex_lock(&ifbdev->hpd_lock);
662 send_hpd = !ifbdev->hpd_suspended;
663 ifbdev->hpd_waiting = true;
664 mutex_unlock(&ifbdev->hpd_lock);
666 if (send_hpd && (ifbdev->vma || ifbdev->helper.deferred_setup))
667 drm_fb_helper_hotplug_event(&ifbdev->helper);
670 void intel_fbdev_restore_mode(struct drm_device *dev)
672 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
677 intel_fbdev_sync(ifbdev);
681 if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper) == 0)
682 intel_fbdev_invalidate(ifbdev);