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/i915_drm.h>
46 #include "intel_drv.h"
47 #include "intel_fbdev.h"
48 #include "intel_frontbuffer.h"
50 static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev)
52 struct drm_i915_gem_object *obj = intel_fb_obj(&ifbdev->fb->base);
54 ifbdev->vma_flags & PLANE_HAS_FENCE ? ORIGIN_GTT : ORIGIN_CPU;
56 intel_fb_obj_invalidate(obj, origin);
59 static int intel_fbdev_set_par(struct fb_info *info)
61 struct drm_fb_helper *fb_helper = info->par;
62 struct intel_fbdev *ifbdev =
63 container_of(fb_helper, struct intel_fbdev, helper);
66 ret = drm_fb_helper_set_par(info);
68 intel_fbdev_invalidate(ifbdev);
73 static int intel_fbdev_blank(int blank, struct fb_info *info)
75 struct drm_fb_helper *fb_helper = info->par;
76 struct intel_fbdev *ifbdev =
77 container_of(fb_helper, struct intel_fbdev, helper);
80 ret = drm_fb_helper_blank(blank, info);
82 intel_fbdev_invalidate(ifbdev);
87 static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
90 struct drm_fb_helper *fb_helper = info->par;
91 struct intel_fbdev *ifbdev =
92 container_of(fb_helper, struct intel_fbdev, helper);
95 ret = drm_fb_helper_pan_display(var, info);
97 intel_fbdev_invalidate(ifbdev);
102 static struct fb_ops intelfb_ops = {
103 .owner = THIS_MODULE,
104 DRM_FB_HELPER_DEFAULT_OPS,
105 .fb_set_par = intel_fbdev_set_par,
106 .fb_fillrect = drm_fb_helper_cfb_fillrect,
107 .fb_copyarea = drm_fb_helper_cfb_copyarea,
108 .fb_imageblit = drm_fb_helper_cfb_imageblit,
109 .fb_pan_display = intel_fbdev_pan_display,
110 .fb_blank = intel_fbdev_blank,
113 static int intelfb_alloc(struct drm_fb_helper *helper,
114 struct drm_fb_helper_surface_size *sizes)
116 struct intel_fbdev *ifbdev =
117 container_of(helper, struct intel_fbdev, helper);
118 struct drm_framebuffer *fb;
119 struct drm_device *dev = helper->dev;
120 struct drm_i915_private *dev_priv = to_i915(dev);
121 struct drm_mode_fb_cmd2 mode_cmd = {};
122 struct drm_i915_gem_object *obj;
125 /* we don't do packed 24bpp */
126 if (sizes->surface_bpp == 24)
127 sizes->surface_bpp = 32;
129 mode_cmd.width = sizes->surface_width;
130 mode_cmd.height = sizes->surface_height;
132 mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
133 DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
134 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
135 sizes->surface_depth);
137 size = mode_cmd.pitches[0] * mode_cmd.height;
138 size = PAGE_ALIGN(size);
140 /* If the FB is too big, just don't use it since fbdev is not very
141 * important and we should probably use that space with FBC or other
144 if (size * 2 < dev_priv->stolen_usable_size)
145 obj = i915_gem_object_create_stolen(dev_priv, size);
147 obj = i915_gem_object_create(dev_priv, size);
149 DRM_ERROR("failed to allocate framebuffer\n");
154 fb = intel_framebuffer_create(obj, &mode_cmd);
160 ifbdev->fb = to_intel_framebuffer(fb);
165 i915_gem_object_put(obj);
170 static int intelfb_create(struct drm_fb_helper *helper,
171 struct drm_fb_helper_surface_size *sizes)
173 struct intel_fbdev *ifbdev =
174 container_of(helper, struct intel_fbdev, helper);
175 struct intel_framebuffer *intel_fb = ifbdev->fb;
176 struct drm_device *dev = helper->dev;
177 struct drm_i915_private *dev_priv = to_i915(dev);
178 struct pci_dev *pdev = dev_priv->drm.pdev;
179 struct i915_ggtt *ggtt = &dev_priv->ggtt;
180 const struct i915_ggtt_view view = {
181 .type = I915_GGTT_VIEW_NORMAL,
183 struct drm_framebuffer *fb;
184 intel_wakeref_t wakeref;
185 struct fb_info *info;
186 struct i915_vma *vma;
187 unsigned long flags = 0;
188 bool prealloc = false;
193 (sizes->fb_width > intel_fb->base.width ||
194 sizes->fb_height > intel_fb->base.height)) {
195 DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d),"
197 intel_fb->base.width, intel_fb->base.height,
198 sizes->fb_width, sizes->fb_height);
199 drm_framebuffer_put(&intel_fb->base);
200 intel_fb = ifbdev->fb = NULL;
202 if (!intel_fb || WARN_ON(!intel_fb_obj(&intel_fb->base))) {
203 DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
204 ret = intelfb_alloc(helper, sizes);
207 intel_fb = ifbdev->fb;
209 DRM_DEBUG_KMS("re-using BIOS fb\n");
211 sizes->fb_width = intel_fb->base.width;
212 sizes->fb_height = intel_fb->base.height;
215 mutex_lock(&dev->struct_mutex);
216 wakeref = intel_runtime_pm_get(dev_priv);
218 /* Pin the GGTT vma for our access via info->screen_base.
219 * This also validates that any existing fb inherited from the
220 * BIOS is suitable for own access.
222 vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base,
223 &view, false, &flags);
229 fb = &ifbdev->fb->base;
230 intel_fb_obj_flush(intel_fb_obj(fb), ORIGIN_DIRTYFB);
232 info = drm_fb_helper_alloc_fbi(helper);
234 DRM_ERROR("Failed to allocate fb_info\n");
239 ifbdev->helper.fb = fb;
241 info->fbops = &intelfb_ops;
243 /* setup aperture base/size for vesafb takeover */
244 info->apertures->ranges[0].base = dev->mode_config.fb_base;
245 info->apertures->ranges[0].size = ggtt->mappable_end;
247 info->fix.smem_start = dev->mode_config.fb_base + i915_ggtt_offset(vma);
248 info->fix.smem_len = vma->node.size;
250 vaddr = i915_vma_pin_iomap(vma);
252 DRM_ERROR("Failed to remap framebuffer into virtual memory\n");
253 ret = PTR_ERR(vaddr);
256 info->screen_base = vaddr;
257 info->screen_size = vma->node.size;
259 drm_fb_helper_fill_info(info, &ifbdev->helper, sizes);
261 /* If the object is shmemfs backed, it will have given us zeroed pages.
262 * If the object is stolen however, it will be full of whatever
263 * garbage was left in there.
265 if (intel_fb_obj(fb)->stolen && !prealloc)
266 memset_io(info->screen_base, 0, info->screen_size);
268 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
270 DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08x\n",
271 fb->width, fb->height, i915_ggtt_offset(vma));
273 ifbdev->vma_flags = flags;
275 intel_runtime_pm_put(dev_priv, wakeref);
276 mutex_unlock(&dev->struct_mutex);
277 vga_switcheroo_client_fb_set(pdev, info);
281 intel_unpin_fb_vma(vma, flags);
283 intel_runtime_pm_put(dev_priv, wakeref);
284 mutex_unlock(&dev->struct_mutex);
288 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
289 .fb_probe = intelfb_create,
292 static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
294 /* We rely on the object-free to release the VMA pinning for
295 * the info->screen_base mmaping. Leaking the VMA is simpler than
296 * trying to rectify all the possible error paths leading here.
299 drm_fb_helper_fini(&ifbdev->helper);
302 mutex_lock(&ifbdev->helper.dev->struct_mutex);
303 intel_unpin_fb_vma(ifbdev->vma, ifbdev->vma_flags);
304 mutex_unlock(&ifbdev->helper.dev->struct_mutex);
308 drm_framebuffer_remove(&ifbdev->fb->base);
314 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
315 * The core display code will have read out the current plane configuration,
316 * so we use that to figure out if there's an object for us to use as the
317 * fb, and if so, we re-use it for the fbdev configuration.
319 * Note we only support a single fb shared across pipes for boot (mostly for
320 * fbcon), so we just find the biggest and use that.
322 static bool intel_fbdev_init_bios(struct drm_device *dev,
323 struct intel_fbdev *ifbdev)
325 struct intel_framebuffer *fb = NULL;
326 struct drm_crtc *crtc;
327 struct intel_crtc *intel_crtc;
328 unsigned int max_size = 0;
330 /* Find the largest fb */
331 for_each_crtc(dev, crtc) {
332 struct drm_i915_gem_object *obj =
333 intel_fb_obj(crtc->primary->state->fb);
334 intel_crtc = to_intel_crtc(crtc);
336 if (!crtc->state->active || !obj) {
337 DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
338 pipe_name(intel_crtc->pipe));
342 if (obj->base.size > max_size) {
343 DRM_DEBUG_KMS("found possible fb from plane %c\n",
344 pipe_name(intel_crtc->pipe));
345 fb = to_intel_framebuffer(crtc->primary->state->fb);
346 max_size = obj->base.size;
351 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
355 /* Now make sure all the pipes will fit into it */
356 for_each_crtc(dev, crtc) {
357 unsigned int cur_size;
359 intel_crtc = to_intel_crtc(crtc);
361 if (!crtc->state->active) {
362 DRM_DEBUG_KMS("pipe %c not active, skipping\n",
363 pipe_name(intel_crtc->pipe));
367 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
368 pipe_name(intel_crtc->pipe));
371 * See if the plane fb we found above will fit on this
372 * pipe. Note we need to use the selected fb's pitch and bpp
373 * rather than the current pipe's, since they differ.
375 cur_size = crtc->state->adjusted_mode.crtc_hdisplay;
376 cur_size = cur_size * fb->base.format->cpp[0];
377 if (fb->base.pitches[0] < cur_size) {
378 DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
379 pipe_name(intel_crtc->pipe),
380 cur_size, fb->base.pitches[0]);
385 cur_size = crtc->state->adjusted_mode.crtc_vdisplay;
386 cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
387 cur_size *= fb->base.pitches[0];
388 DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
389 pipe_name(intel_crtc->pipe),
390 crtc->state->adjusted_mode.crtc_hdisplay,
391 crtc->state->adjusted_mode.crtc_vdisplay,
392 fb->base.format->cpp[0] * 8,
395 if (cur_size > max_size) {
396 DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
397 pipe_name(intel_crtc->pipe),
403 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
404 pipe_name(intel_crtc->pipe),
409 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
413 ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
416 drm_framebuffer_get(&ifbdev->fb->base);
418 /* Final pass to check if any active pipes don't have fbs */
419 for_each_crtc(dev, crtc) {
420 intel_crtc = to_intel_crtc(crtc);
422 if (!crtc->state->active)
425 WARN(!crtc->primary->state->fb,
426 "re-used BIOS config but lost an fb on crtc %d\n",
431 DRM_DEBUG_KMS("using BIOS fb for initial console\n");
439 static void intel_fbdev_suspend_worker(struct work_struct *work)
441 intel_fbdev_set_suspend(&container_of(work,
442 struct drm_i915_private,
443 fbdev_suspend_work)->drm,
444 FBINFO_STATE_RUNNING,
448 int intel_fbdev_init(struct drm_device *dev)
450 struct drm_i915_private *dev_priv = to_i915(dev);
451 struct intel_fbdev *ifbdev;
454 if (WARN_ON(!HAS_DISPLAY(dev_priv)))
457 ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
461 mutex_init(&ifbdev->hpd_lock);
462 drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
464 if (!intel_fbdev_init_bios(dev, ifbdev))
465 ifbdev->preferred_bpp = 32;
467 ret = drm_fb_helper_init(dev, &ifbdev->helper, 4);
473 dev_priv->fbdev = ifbdev;
474 INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
476 drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
481 static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
483 struct intel_fbdev *ifbdev = data;
485 /* Due to peculiar init order wrt to hpd handling this is separate. */
486 if (drm_fb_helper_initial_config(&ifbdev->helper,
487 ifbdev->preferred_bpp))
488 intel_fbdev_unregister(to_i915(ifbdev->helper.dev));
491 void intel_fbdev_initial_config_async(struct drm_device *dev)
493 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
498 ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
501 static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
506 /* Only serialises with all preceding async calls, hence +1 */
507 async_synchronize_cookie(ifbdev->cookie + 1);
511 void intel_fbdev_unregister(struct drm_i915_private *dev_priv)
513 struct intel_fbdev *ifbdev = dev_priv->fbdev;
518 cancel_work_sync(&dev_priv->fbdev_suspend_work);
519 if (!current_is_async())
520 intel_fbdev_sync(ifbdev);
522 drm_fb_helper_unregister_fbi(&ifbdev->helper);
525 void intel_fbdev_fini(struct drm_i915_private *dev_priv)
527 struct intel_fbdev *ifbdev = fetch_and_zero(&dev_priv->fbdev);
532 intel_fbdev_destroy(ifbdev);
535 /* Suspends/resumes fbdev processing of incoming HPD events. When resuming HPD
536 * processing, fbdev will perform a full connector reprobe if a hotplug event
537 * was received while HPD was suspended.
539 static void intel_fbdev_hpd_set_suspend(struct intel_fbdev *ifbdev, int state)
541 bool send_hpd = false;
543 mutex_lock(&ifbdev->hpd_lock);
544 ifbdev->hpd_suspended = state == FBINFO_STATE_SUSPENDED;
545 send_hpd = !ifbdev->hpd_suspended && ifbdev->hpd_waiting;
546 ifbdev->hpd_waiting = false;
547 mutex_unlock(&ifbdev->hpd_lock);
550 DRM_DEBUG_KMS("Handling delayed fbcon HPD event\n");
551 drm_fb_helper_hotplug_event(&ifbdev->helper);
555 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
557 struct drm_i915_private *dev_priv = to_i915(dev);
558 struct intel_fbdev *ifbdev = dev_priv->fbdev;
559 struct fb_info *info;
561 if (!ifbdev || !ifbdev->vma)
564 info = ifbdev->helper.fbdev;
567 /* Flush any pending work to turn the console on, and then
568 * wait to turn it off. It must be synchronous as we are
569 * about to suspend or unload the driver.
571 * Note that from within the work-handler, we cannot flush
572 * ourselves, so only flush outstanding work upon suspend!
574 if (state != FBINFO_STATE_RUNNING)
575 flush_work(&dev_priv->fbdev_suspend_work);
580 * The console lock can be pretty contented on resume due
581 * to all the printk activity. Try to keep it out of the hot
582 * path of resume if possible.
584 WARN_ON(state != FBINFO_STATE_RUNNING);
585 if (!console_trylock()) {
586 /* Don't block our own workqueue as this can
587 * be run in parallel with other i915.ko tasks.
589 schedule_work(&dev_priv->fbdev_suspend_work);
594 /* On resume from hibernation: If the object is shmemfs backed, it has
595 * been restored from swap. If the object is stolen however, it will be
596 * full of whatever garbage was left in there.
598 if (state == FBINFO_STATE_RUNNING &&
599 intel_fb_obj(&ifbdev->fb->base)->stolen)
600 memset_io(info->screen_base, 0, info->screen_size);
602 drm_fb_helper_set_suspend(&ifbdev->helper, state);
605 intel_fbdev_hpd_set_suspend(ifbdev, state);
608 void intel_fbdev_output_poll_changed(struct drm_device *dev)
610 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
616 intel_fbdev_sync(ifbdev);
618 mutex_lock(&ifbdev->hpd_lock);
619 send_hpd = !ifbdev->hpd_suspended;
620 ifbdev->hpd_waiting = true;
621 mutex_unlock(&ifbdev->hpd_lock);
623 if (send_hpd && (ifbdev->vma || ifbdev->helper.deferred_setup))
624 drm_fb_helper_hotplug_event(&ifbdev->helper);
627 void intel_fbdev_restore_mode(struct drm_device *dev)
629 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
634 intel_fbdev_sync(ifbdev);
638 if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper) == 0)
639 intel_fbdev_invalidate(ifbdev);