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/console.h>
28 #include <linux/delay.h>
29 #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_crtc_helper.h>
42 #include <drm/drm_fb_helper.h>
43 #include <drm/drm_fourcc.h>
44 #include <drm/drm_gem.h>
45 #include <drm/drm_gem_framebuffer_helper.h>
49 #include "intel_display_types.h"
51 #include "intel_fb_pin.h"
52 #include "intel_fbdev.h"
53 #include "intel_fbdev_fb.h"
54 #include "intel_frontbuffer.h"
57 struct drm_fb_helper helper;
58 struct intel_framebuffer *fb;
60 unsigned long vma_flags;
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 FB_GEN_DEFAULT_DEFERRED_IOMEM_OPS(intel_fbdev,
88 drm_fb_helper_damage_range,
89 drm_fb_helper_damage_area)
91 static int intel_fbdev_set_par(struct fb_info *info)
93 struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
96 ret = drm_fb_helper_set_par(info);
98 intel_fbdev_invalidate(ifbdev);
103 static int intel_fbdev_blank(int blank, struct fb_info *info)
105 struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
108 ret = drm_fb_helper_blank(blank, info);
110 intel_fbdev_invalidate(ifbdev);
115 static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
116 struct fb_info *info)
118 struct intel_fbdev *ifbdev = to_intel_fbdev(info->par);
121 ret = drm_fb_helper_pan_display(var, info);
123 intel_fbdev_invalidate(ifbdev);
128 static int intel_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma)
130 struct intel_fbdev *fbdev = to_intel_fbdev(info->par);
131 struct drm_gem_object *obj = drm_gem_fb_get_obj(&fbdev->fb->base, 0);
133 return intel_bo_fb_mmap(obj, vma);
136 static void intel_fbdev_fb_destroy(struct fb_info *info)
138 struct drm_fb_helper *fb_helper = info->par;
139 struct intel_fbdev *ifbdev = container_of(fb_helper, struct intel_fbdev, helper);
141 drm_fb_helper_fini(&ifbdev->helper);
144 * We rely on the object-free to release the VMA pinning for
145 * the info->screen_base mmaping. Leaking the VMA is simpler than
146 * trying to rectify all the possible error paths leading here.
148 intel_fb_unpin_vma(ifbdev->vma, ifbdev->vma_flags);
149 drm_framebuffer_remove(&ifbdev->fb->base);
151 drm_client_release(&fb_helper->client);
152 drm_fb_helper_unprepare(&ifbdev->helper);
157 __diag_ignore_all("-Woverride-init", "Allow field initialization overrides for fb ops");
159 static const struct fb_ops intelfb_ops = {
160 .owner = THIS_MODULE,
161 __FB_DEFAULT_DEFERRED_OPS_RDWR(intel_fbdev),
162 DRM_FB_HELPER_DEFAULT_OPS,
163 .fb_set_par = intel_fbdev_set_par,
164 .fb_blank = intel_fbdev_blank,
165 .fb_pan_display = intel_fbdev_pan_display,
166 __FB_DEFAULT_DEFERRED_OPS_DRAW(intel_fbdev),
167 .fb_mmap = intel_fbdev_mmap,
168 .fb_destroy = intel_fbdev_fb_destroy,
173 static int intelfb_create(struct drm_fb_helper *helper,
174 struct drm_fb_helper_surface_size *sizes)
176 struct intel_fbdev *ifbdev = to_intel_fbdev(helper);
177 struct intel_framebuffer *fb = ifbdev->fb;
178 struct drm_device *dev = helper->dev;
179 struct drm_i915_private *dev_priv = to_i915(dev);
180 const struct i915_gtt_view view = {
181 .type = I915_GTT_VIEW_NORMAL,
183 intel_wakeref_t wakeref;
184 struct fb_info *info;
185 struct i915_vma *vma;
186 unsigned long flags = 0;
187 bool prealloc = false;
188 struct drm_gem_object *obj;
191 mutex_lock(&ifbdev->hpd_lock);
192 ret = ifbdev->hpd_suspended ? -EAGAIN : 0;
193 mutex_unlock(&ifbdev->hpd_lock);
200 (sizes->fb_width > fb->base.width ||
201 sizes->fb_height > fb->base.height)) {
202 drm_dbg_kms(&dev_priv->drm,
203 "BIOS fb too small (%dx%d), we require (%dx%d),"
205 fb->base.width, fb->base.height,
206 sizes->fb_width, sizes->fb_height);
207 drm_framebuffer_put(&fb->base);
210 if (!fb || drm_WARN_ON(dev, !intel_fb_bo(&fb->base))) {
211 drm_dbg_kms(&dev_priv->drm,
212 "no BIOS fb, allocating a new one\n");
213 fb = intel_fbdev_fb_alloc(helper, sizes);
217 drm_dbg_kms(&dev_priv->drm, "re-using BIOS fb\n");
219 sizes->fb_width = fb->base.width;
220 sizes->fb_height = fb->base.height;
223 wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
225 /* Pin the GGTT vma for our access via info->screen_base.
226 * This also validates that any existing fb inherited from the
227 * BIOS is suitable for own access.
229 vma = intel_fb_pin_to_ggtt(&fb->base, &view,
230 fb->min_alignment, 0,
237 info = drm_fb_helper_alloc_info(helper);
239 drm_err(&dev_priv->drm, "Failed to allocate fb_info (%pe)\n", info);
244 ifbdev->helper.fb = &fb->base;
246 info->fbops = &intelfb_ops;
248 obj = intel_fb_bo(&fb->base);
250 ret = intel_fbdev_fb_fill_info(dev_priv, info, obj, vma);
254 drm_fb_helper_fill_info(info, &ifbdev->helper, sizes);
256 /* If the object is shmemfs backed, it will have given us zeroed pages.
257 * If the object is stolen however, it will be full of whatever
258 * garbage was left in there.
260 if (!intel_bo_is_shmem(obj) && !prealloc)
261 memset_io(info->screen_base, 0, info->screen_size);
263 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
265 drm_dbg_kms(&dev_priv->drm, "allocated %dx%d fb: 0x%08x\n",
266 fb->base.width, fb->base.height,
267 i915_ggtt_offset(vma));
270 ifbdev->vma_flags = flags;
272 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
277 intel_fb_unpin_vma(vma, flags);
279 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
283 static int intelfb_dirty(struct drm_fb_helper *helper, struct drm_clip_rect *clip)
285 if (!(clip->x1 < clip->x2 && clip->y1 < clip->y2))
288 if (helper->fb->funcs->dirty)
289 return helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, clip, 1);
294 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
295 .fb_probe = intelfb_create,
296 .fb_dirty = intelfb_dirty,
300 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
301 * The core display code will have read out the current plane configuration,
302 * so we use that to figure out if there's an object for us to use as the
303 * fb, and if so, we re-use it for the fbdev configuration.
305 * Note we only support a single fb shared across pipes for boot (mostly for
306 * fbcon), so we just find the biggest and use that.
308 static bool intel_fbdev_init_bios(struct drm_device *dev,
309 struct intel_fbdev *ifbdev)
311 struct drm_i915_private *i915 = to_i915(dev);
312 struct intel_framebuffer *fb = NULL;
313 struct intel_crtc *crtc;
314 unsigned int max_size = 0;
316 /* Find the largest fb */
317 for_each_intel_crtc(dev, crtc) {
318 struct intel_crtc_state *crtc_state =
319 to_intel_crtc_state(crtc->base.state);
320 struct intel_plane *plane =
321 to_intel_plane(crtc->base.primary);
322 struct intel_plane_state *plane_state =
323 to_intel_plane_state(plane->base.state);
324 struct drm_gem_object *obj = intel_fb_bo(plane_state->uapi.fb);
326 if (!crtc_state->uapi.active) {
327 drm_dbg_kms(&i915->drm,
328 "[CRTC:%d:%s] not active, skipping\n",
329 crtc->base.base.id, crtc->base.name);
334 drm_dbg_kms(&i915->drm,
335 "[PLANE:%d:%s] no fb, skipping\n",
336 plane->base.base.id, plane->base.name);
340 if (obj->size > max_size) {
341 drm_dbg_kms(&i915->drm,
342 "found possible fb from [PLANE:%d:%s]\n",
343 plane->base.base.id, plane->base.name);
344 fb = to_intel_framebuffer(plane_state->uapi.fb);
345 max_size = obj->size;
350 drm_dbg_kms(&i915->drm,
351 "no active fbs found, not using BIOS config\n");
355 /* Now make sure all the pipes will fit into it */
356 for_each_intel_crtc(dev, crtc) {
357 struct intel_crtc_state *crtc_state =
358 to_intel_crtc_state(crtc->base.state);
359 struct intel_plane *plane =
360 to_intel_plane(crtc->base.primary);
361 unsigned int cur_size;
363 if (!crtc_state->uapi.active) {
364 drm_dbg_kms(&i915->drm,
365 "[CRTC:%d:%s] not active, skipping\n",
366 crtc->base.base.id, crtc->base.name);
370 drm_dbg_kms(&i915->drm, "checking [PLANE:%d:%s] for BIOS fb\n",
371 plane->base.base.id, plane->base.name);
374 * See if the plane fb we found above will fit on this
375 * pipe. Note we need to use the selected fb's pitch and bpp
376 * rather than the current pipe's, since they differ.
378 cur_size = crtc_state->uapi.adjusted_mode.crtc_hdisplay;
379 cur_size = cur_size * fb->base.format->cpp[0];
380 if (fb->base.pitches[0] < cur_size) {
381 drm_dbg_kms(&i915->drm,
382 "fb not wide enough for [PLANE:%d:%s] (%d vs %d)\n",
383 plane->base.base.id, plane->base.name,
384 cur_size, fb->base.pitches[0]);
389 cur_size = crtc_state->uapi.adjusted_mode.crtc_vdisplay;
390 cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
391 cur_size *= fb->base.pitches[0];
392 drm_dbg_kms(&i915->drm,
393 "[CRTC:%d:%s] area: %dx%d, bpp: %d, size: %d\n",
394 crtc->base.base.id, crtc->base.name,
395 crtc_state->uapi.adjusted_mode.crtc_hdisplay,
396 crtc_state->uapi.adjusted_mode.crtc_vdisplay,
397 fb->base.format->cpp[0] * 8,
400 if (cur_size > max_size) {
401 drm_dbg_kms(&i915->drm,
402 "fb not big enough for [PLANE:%d:%s] (%d vs %d)\n",
403 plane->base.base.id, plane->base.name,
409 drm_dbg_kms(&i915->drm,
410 "fb big enough [PLANE:%d:%s] (%d >= %d)\n",
411 plane->base.base.id, plane->base.name,
416 drm_dbg_kms(&i915->drm,
417 "BIOS fb not suitable for all pipes, not using\n");
421 ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
424 drm_framebuffer_get(&ifbdev->fb->base);
426 /* Final pass to check if any active pipes don't have fbs */
427 for_each_intel_crtc(dev, crtc) {
428 struct intel_crtc_state *crtc_state =
429 to_intel_crtc_state(crtc->base.state);
430 struct intel_plane *plane =
431 to_intel_plane(crtc->base.primary);
432 struct intel_plane_state *plane_state =
433 to_intel_plane_state(plane->base.state);
435 if (!crtc_state->uapi.active)
438 drm_WARN(dev, !plane_state->uapi.fb,
439 "re-used BIOS config but lost an fb on [PLANE:%d:%s]\n",
440 plane->base.base.id, plane->base.name);
444 drm_dbg_kms(&i915->drm, "using BIOS fb for initial console\n");
452 static void intel_fbdev_suspend_worker(struct work_struct *work)
454 intel_fbdev_set_suspend(&container_of(work,
455 struct drm_i915_private,
456 display.fbdev.suspend_work)->drm,
457 FBINFO_STATE_RUNNING,
461 /* Suspends/resumes fbdev processing of incoming HPD events. When resuming HPD
462 * processing, fbdev will perform a full connector reprobe if a hotplug event
463 * was received while HPD was suspended.
465 static void intel_fbdev_hpd_set_suspend(struct drm_i915_private *i915, int state)
467 struct intel_fbdev *ifbdev = i915->display.fbdev.fbdev;
468 bool send_hpd = false;
470 mutex_lock(&ifbdev->hpd_lock);
471 ifbdev->hpd_suspended = state == FBINFO_STATE_SUSPENDED;
472 send_hpd = !ifbdev->hpd_suspended && ifbdev->hpd_waiting;
473 ifbdev->hpd_waiting = false;
474 mutex_unlock(&ifbdev->hpd_lock);
477 drm_dbg_kms(&i915->drm, "Handling delayed fbcon HPD event\n");
478 drm_fb_helper_hotplug_event(&ifbdev->helper);
482 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
484 struct drm_i915_private *dev_priv = to_i915(dev);
485 struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
486 struct fb_info *info;
491 if (drm_WARN_ON(&dev_priv->drm, !HAS_DISPLAY(dev_priv)))
497 info = ifbdev->helper.info;
500 /* Flush any pending work to turn the console on, and then
501 * wait to turn it off. It must be synchronous as we are
502 * about to suspend or unload the driver.
504 * Note that from within the work-handler, we cannot flush
505 * ourselves, so only flush outstanding work upon suspend!
507 if (state != FBINFO_STATE_RUNNING)
508 flush_work(&dev_priv->display.fbdev.suspend_work);
513 * The console lock can be pretty contented on resume due
514 * to all the printk activity. Try to keep it out of the hot
515 * path of resume if possible.
517 drm_WARN_ON(dev, state != FBINFO_STATE_RUNNING);
518 if (!console_trylock()) {
519 /* Don't block our own workqueue as this can
520 * be run in parallel with other i915.ko tasks.
522 queue_work(dev_priv->unordered_wq,
523 &dev_priv->display.fbdev.suspend_work);
528 /* On resume from hibernation: If the object is shmemfs backed, it has
529 * been restored from swap. If the object is stolen however, it will be
530 * full of whatever garbage was left in there.
532 if (state == FBINFO_STATE_RUNNING &&
533 !intel_bo_is_shmem(intel_fb_bo(&ifbdev->fb->base)))
534 memset_io(info->screen_base, 0, info->screen_size);
536 drm_fb_helper_set_suspend(&ifbdev->helper, state);
540 intel_fbdev_hpd_set_suspend(dev_priv, state);
543 static int intel_fbdev_output_poll_changed(struct drm_device *dev)
545 struct intel_fbdev *ifbdev = to_i915(dev)->display.fbdev.fbdev;
551 mutex_lock(&ifbdev->hpd_lock);
552 send_hpd = !ifbdev->hpd_suspended;
553 ifbdev->hpd_waiting = true;
554 mutex_unlock(&ifbdev->hpd_lock);
556 if (send_hpd && (ifbdev->vma || ifbdev->helper.deferred_setup))
557 drm_fb_helper_hotplug_event(&ifbdev->helper);
562 static int intel_fbdev_restore_mode(struct drm_i915_private *dev_priv)
564 struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev;
573 ret = drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper);
577 intel_fbdev_invalidate(ifbdev);
583 * Fbdev client and struct drm_client_funcs
586 static void intel_fbdev_client_unregister(struct drm_client_dev *client)
588 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
589 struct drm_device *dev = fb_helper->dev;
590 struct pci_dev *pdev = to_pci_dev(dev->dev);
592 if (fb_helper->info) {
593 vga_switcheroo_client_fb_set(pdev, NULL);
594 drm_fb_helper_unregister_info(fb_helper);
596 drm_fb_helper_unprepare(fb_helper);
597 drm_client_release(&fb_helper->client);
602 static int intel_fbdev_client_restore(struct drm_client_dev *client)
604 struct drm_i915_private *dev_priv = to_i915(client->dev);
607 ret = intel_fbdev_restore_mode(dev_priv);
611 vga_switcheroo_process_delayed_switch();
616 static int intel_fbdev_client_hotplug(struct drm_client_dev *client)
618 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
619 struct drm_device *dev = client->dev;
620 struct pci_dev *pdev = to_pci_dev(dev->dev);
624 return intel_fbdev_output_poll_changed(dev);
626 ret = drm_fb_helper_init(dev, fb_helper);
630 ret = drm_fb_helper_initial_config(fb_helper);
632 goto err_drm_fb_helper_fini;
634 vga_switcheroo_client_fb_set(pdev, fb_helper->info);
638 err_drm_fb_helper_fini:
639 drm_fb_helper_fini(fb_helper);
641 drm_err(dev, "Failed to setup i915 fbdev emulation (ret=%d)\n", ret);
645 static const struct drm_client_funcs intel_fbdev_client_funcs = {
646 .owner = THIS_MODULE,
647 .unregister = intel_fbdev_client_unregister,
648 .restore = intel_fbdev_client_restore,
649 .hotplug = intel_fbdev_client_hotplug,
652 void intel_fbdev_setup(struct drm_i915_private *i915)
654 struct drm_device *dev = &i915->drm;
655 struct intel_fbdev *ifbdev;
658 if (!HAS_DISPLAY(i915))
661 ifbdev = kzalloc(sizeof(*ifbdev), GFP_KERNEL);
664 drm_fb_helper_prepare(dev, &ifbdev->helper, 32, &intel_fb_helper_funcs);
666 i915->display.fbdev.fbdev = ifbdev;
667 INIT_WORK(&i915->display.fbdev.suspend_work, intel_fbdev_suspend_worker);
668 mutex_init(&ifbdev->hpd_lock);
669 if (intel_fbdev_init_bios(dev, ifbdev))
670 ifbdev->helper.preferred_bpp = ifbdev->preferred_bpp;
672 ifbdev->preferred_bpp = ifbdev->helper.preferred_bpp;
674 ret = drm_client_init(dev, &ifbdev->helper.client, "intel-fbdev",
675 &intel_fbdev_client_funcs);
677 drm_err(dev, "Failed to register client: %d\n", ret);
678 goto err_drm_fb_helper_unprepare;
681 drm_client_register(&ifbdev->helper.client);
685 err_drm_fb_helper_unprepare:
686 drm_fb_helper_unprepare(&ifbdev->helper);
687 mutex_destroy(&ifbdev->hpd_lock);
691 struct intel_framebuffer *intel_fbdev_framebuffer(struct intel_fbdev *fbdev)
693 if (!fbdev || !fbdev->helper.fb)
696 return to_intel_framebuffer(fbdev->helper.fb);