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_display_types.h"
47 #include "intel_fbdev.h"
48 #include "intel_frontbuffer.h"
50 static struct intel_frontbuffer *to_frontbuffer(struct intel_fbdev *ifbdev)
52 return ifbdev->fb->frontbuffer;
55 static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev)
57 intel_frontbuffer_invalidate(to_frontbuffer(ifbdev), ORIGIN_CPU);
60 static int intel_fbdev_set_par(struct fb_info *info)
62 struct drm_fb_helper *fb_helper = info->par;
63 struct intel_fbdev *ifbdev =
64 container_of(fb_helper, struct intel_fbdev, helper);
67 ret = drm_fb_helper_set_par(info);
69 intel_fbdev_invalidate(ifbdev);
74 static int intel_fbdev_blank(int blank, struct fb_info *info)
76 struct drm_fb_helper *fb_helper = info->par;
77 struct intel_fbdev *ifbdev =
78 container_of(fb_helper, struct intel_fbdev, helper);
81 ret = drm_fb_helper_blank(blank, info);
83 intel_fbdev_invalidate(ifbdev);
88 static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
91 struct drm_fb_helper *fb_helper = info->par;
92 struct intel_fbdev *ifbdev =
93 container_of(fb_helper, struct intel_fbdev, helper);
96 ret = drm_fb_helper_pan_display(var, info);
98 intel_fbdev_invalidate(ifbdev);
103 static struct fb_ops intelfb_ops = {
104 .owner = THIS_MODULE,
105 DRM_FB_HELPER_DEFAULT_OPS,
106 .fb_set_par = intel_fbdev_set_par,
107 .fb_fillrect = drm_fb_helper_cfb_fillrect,
108 .fb_copyarea = drm_fb_helper_cfb_copyarea,
109 .fb_imageblit = drm_fb_helper_cfb_imageblit,
110 .fb_pan_display = intel_fbdev_pan_display,
111 .fb_blank = intel_fbdev_blank,
114 static int intelfb_alloc(struct drm_fb_helper *helper,
115 struct drm_fb_helper_surface_size *sizes)
117 struct intel_fbdev *ifbdev =
118 container_of(helper, struct intel_fbdev, helper);
119 struct drm_framebuffer *fb;
120 struct drm_device *dev = helper->dev;
121 struct drm_i915_private *dev_priv = to_i915(dev);
122 struct drm_mode_fb_cmd2 mode_cmd = {};
123 struct drm_i915_gem_object *obj;
126 /* we don't do packed 24bpp */
127 if (sizes->surface_bpp == 24)
128 sizes->surface_bpp = 32;
130 mode_cmd.width = sizes->surface_width;
131 mode_cmd.height = sizes->surface_height;
133 mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
134 DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
135 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
136 sizes->surface_depth);
138 size = mode_cmd.pitches[0] * mode_cmd.height;
139 size = PAGE_ALIGN(size);
141 /* If the FB is too big, just don't use it since fbdev is not very
142 * important and we should probably use that space with FBC or other
145 if (size * 2 < dev_priv->stolen_usable_size)
146 obj = i915_gem_object_create_stolen(dev_priv, size);
148 obj = i915_gem_object_create_shmem(dev_priv, size);
150 DRM_ERROR("failed to allocate framebuffer\n");
154 fb = intel_framebuffer_create(obj, &mode_cmd);
155 i915_gem_object_put(obj);
159 ifbdev->fb = to_intel_framebuffer(fb);
163 static int intelfb_create(struct drm_fb_helper *helper,
164 struct drm_fb_helper_surface_size *sizes)
166 struct intel_fbdev *ifbdev =
167 container_of(helper, struct intel_fbdev, helper);
168 struct intel_framebuffer *intel_fb = ifbdev->fb;
169 struct drm_device *dev = helper->dev;
170 struct drm_i915_private *dev_priv = to_i915(dev);
171 struct pci_dev *pdev = dev_priv->drm.pdev;
172 struct i915_ggtt *ggtt = &dev_priv->ggtt;
173 const struct i915_ggtt_view view = {
174 .type = I915_GGTT_VIEW_NORMAL,
176 intel_wakeref_t wakeref;
177 struct fb_info *info;
178 struct i915_vma *vma;
179 unsigned long flags = 0;
180 bool prealloc = false;
185 (sizes->fb_width > intel_fb->base.width ||
186 sizes->fb_height > intel_fb->base.height)) {
187 DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d),"
189 intel_fb->base.width, intel_fb->base.height,
190 sizes->fb_width, sizes->fb_height);
191 drm_framebuffer_put(&intel_fb->base);
192 intel_fb = ifbdev->fb = NULL;
194 if (!intel_fb || WARN_ON(!intel_fb_obj(&intel_fb->base))) {
195 DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
196 ret = intelfb_alloc(helper, sizes);
199 intel_fb = ifbdev->fb;
201 DRM_DEBUG_KMS("re-using BIOS fb\n");
203 sizes->fb_width = intel_fb->base.width;
204 sizes->fb_height = intel_fb->base.height;
207 mutex_lock(&dev->struct_mutex);
208 wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
210 /* Pin the GGTT vma for our access via info->screen_base.
211 * This also validates that any existing fb inherited from the
212 * BIOS is suitable for own access.
214 vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base,
215 &view, false, &flags);
221 intel_frontbuffer_flush(to_frontbuffer(ifbdev), ORIGIN_DIRTYFB);
223 info = drm_fb_helper_alloc_fbi(helper);
225 DRM_ERROR("Failed to allocate fb_info\n");
230 ifbdev->helper.fb = &ifbdev->fb->base;
232 info->fbops = &intelfb_ops;
234 /* setup aperture base/size for vesafb takeover */
235 info->apertures->ranges[0].base = ggtt->gmadr.start;
236 info->apertures->ranges[0].size = ggtt->mappable_end;
238 vaddr = i915_vma_pin_iomap(vma);
240 DRM_ERROR("Failed to remap framebuffer into virtual memory\n");
241 ret = PTR_ERR(vaddr);
244 info->screen_base = vaddr;
245 info->screen_size = vma->node.size;
247 /* Our framebuffer is the entirety of fbdev's system memory */
248 info->fix.smem_start = (unsigned long)info->screen_base;
249 info->fix.smem_len = info->screen_size;
251 drm_fb_helper_fill_info(info, &ifbdev->helper, sizes);
253 /* If the object is shmemfs backed, it will have given us zeroed pages.
254 * If the object is stolen however, it will be full of whatever
255 * garbage was left in there.
257 if (vma->obj->stolen && !prealloc)
258 memset_io(info->screen_base, 0, info->screen_size);
260 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
262 DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08x\n",
263 ifbdev->fb->base.width, ifbdev->fb->base.height,
264 i915_ggtt_offset(vma));
266 ifbdev->vma_flags = flags;
268 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
269 mutex_unlock(&dev->struct_mutex);
270 vga_switcheroo_client_fb_set(pdev, info);
274 intel_unpin_fb_vma(vma, flags);
276 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
277 mutex_unlock(&dev->struct_mutex);
281 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
282 .fb_probe = intelfb_create,
285 static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
287 /* We rely on the object-free to release the VMA pinning for
288 * the info->screen_base mmaping. Leaking the VMA is simpler than
289 * trying to rectify all the possible error paths leading here.
292 drm_fb_helper_fini(&ifbdev->helper);
295 mutex_lock(&ifbdev->helper.dev->struct_mutex);
296 intel_unpin_fb_vma(ifbdev->vma, ifbdev->vma_flags);
297 mutex_unlock(&ifbdev->helper.dev->struct_mutex);
301 drm_framebuffer_remove(&ifbdev->fb->base);
307 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
308 * The core display code will have read out the current plane configuration,
309 * so we use that to figure out if there's an object for us to use as the
310 * fb, and if so, we re-use it for the fbdev configuration.
312 * Note we only support a single fb shared across pipes for boot (mostly for
313 * fbcon), so we just find the biggest and use that.
315 static bool intel_fbdev_init_bios(struct drm_device *dev,
316 struct intel_fbdev *ifbdev)
318 struct intel_framebuffer *fb = NULL;
319 struct drm_crtc *crtc;
320 struct intel_crtc *intel_crtc;
321 unsigned int max_size = 0;
323 /* Find the largest fb */
324 for_each_crtc(dev, crtc) {
325 struct drm_i915_gem_object *obj =
326 intel_fb_obj(crtc->primary->state->fb);
327 intel_crtc = to_intel_crtc(crtc);
329 if (!crtc->state->active || !obj) {
330 DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
331 pipe_name(intel_crtc->pipe));
335 if (obj->base.size > max_size) {
336 DRM_DEBUG_KMS("found possible fb from plane %c\n",
337 pipe_name(intel_crtc->pipe));
338 fb = to_intel_framebuffer(crtc->primary->state->fb);
339 max_size = obj->base.size;
344 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
348 /* Now make sure all the pipes will fit into it */
349 for_each_crtc(dev, crtc) {
350 unsigned int cur_size;
352 intel_crtc = to_intel_crtc(crtc);
354 if (!crtc->state->active) {
355 DRM_DEBUG_KMS("pipe %c not active, skipping\n",
356 pipe_name(intel_crtc->pipe));
360 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
361 pipe_name(intel_crtc->pipe));
364 * See if the plane fb we found above will fit on this
365 * pipe. Note we need to use the selected fb's pitch and bpp
366 * rather than the current pipe's, since they differ.
368 cur_size = crtc->state->adjusted_mode.crtc_hdisplay;
369 cur_size = cur_size * fb->base.format->cpp[0];
370 if (fb->base.pitches[0] < cur_size) {
371 DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
372 pipe_name(intel_crtc->pipe),
373 cur_size, fb->base.pitches[0]);
378 cur_size = crtc->state->adjusted_mode.crtc_vdisplay;
379 cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
380 cur_size *= fb->base.pitches[0];
381 DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
382 pipe_name(intel_crtc->pipe),
383 crtc->state->adjusted_mode.crtc_hdisplay,
384 crtc->state->adjusted_mode.crtc_vdisplay,
385 fb->base.format->cpp[0] * 8,
388 if (cur_size > max_size) {
389 DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
390 pipe_name(intel_crtc->pipe),
396 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
397 pipe_name(intel_crtc->pipe),
402 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
406 ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
409 drm_framebuffer_get(&ifbdev->fb->base);
411 /* Final pass to check if any active pipes don't have fbs */
412 for_each_crtc(dev, crtc) {
413 intel_crtc = to_intel_crtc(crtc);
415 if (!crtc->state->active)
418 WARN(!crtc->primary->state->fb,
419 "re-used BIOS config but lost an fb on crtc %d\n",
424 DRM_DEBUG_KMS("using BIOS fb for initial console\n");
432 static void intel_fbdev_suspend_worker(struct work_struct *work)
434 intel_fbdev_set_suspend(&container_of(work,
435 struct drm_i915_private,
436 fbdev_suspend_work)->drm,
437 FBINFO_STATE_RUNNING,
441 int intel_fbdev_init(struct drm_device *dev)
443 struct drm_i915_private *dev_priv = to_i915(dev);
444 struct intel_fbdev *ifbdev;
447 if (WARN_ON(!HAS_DISPLAY(dev_priv) || !INTEL_DISPLAY_ENABLED(dev_priv)))
450 ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
454 mutex_init(&ifbdev->hpd_lock);
455 drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
457 if (!intel_fbdev_init_bios(dev, ifbdev))
458 ifbdev->preferred_bpp = 32;
460 ret = drm_fb_helper_init(dev, &ifbdev->helper, 4);
466 dev_priv->fbdev = ifbdev;
467 INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
469 drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
474 static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
476 struct intel_fbdev *ifbdev = data;
478 /* Due to peculiar init order wrt to hpd handling this is separate. */
479 if (drm_fb_helper_initial_config(&ifbdev->helper,
480 ifbdev->preferred_bpp))
481 intel_fbdev_unregister(to_i915(ifbdev->helper.dev));
484 void intel_fbdev_initial_config_async(struct drm_device *dev)
486 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
491 ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
494 static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
499 /* Only serialises with all preceding async calls, hence +1 */
500 async_synchronize_cookie(ifbdev->cookie + 1);
504 void intel_fbdev_unregister(struct drm_i915_private *dev_priv)
506 struct intel_fbdev *ifbdev = dev_priv->fbdev;
511 cancel_work_sync(&dev_priv->fbdev_suspend_work);
512 if (!current_is_async())
513 intel_fbdev_sync(ifbdev);
515 drm_fb_helper_unregister_fbi(&ifbdev->helper);
518 void intel_fbdev_fini(struct drm_i915_private *dev_priv)
520 struct intel_fbdev *ifbdev = fetch_and_zero(&dev_priv->fbdev);
525 intel_fbdev_destroy(ifbdev);
528 /* Suspends/resumes fbdev processing of incoming HPD events. When resuming HPD
529 * processing, fbdev will perform a full connector reprobe if a hotplug event
530 * was received while HPD was suspended.
532 static void intel_fbdev_hpd_set_suspend(struct intel_fbdev *ifbdev, int state)
534 bool send_hpd = false;
536 mutex_lock(&ifbdev->hpd_lock);
537 ifbdev->hpd_suspended = state == FBINFO_STATE_SUSPENDED;
538 send_hpd = !ifbdev->hpd_suspended && ifbdev->hpd_waiting;
539 ifbdev->hpd_waiting = false;
540 mutex_unlock(&ifbdev->hpd_lock);
543 DRM_DEBUG_KMS("Handling delayed fbcon HPD event\n");
544 drm_fb_helper_hotplug_event(&ifbdev->helper);
548 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
550 struct drm_i915_private *dev_priv = to_i915(dev);
551 struct intel_fbdev *ifbdev = dev_priv->fbdev;
552 struct fb_info *info;
554 if (!ifbdev || !ifbdev->vma)
557 info = ifbdev->helper.fbdev;
560 /* Flush any pending work to turn the console on, and then
561 * wait to turn it off. It must be synchronous as we are
562 * about to suspend or unload the driver.
564 * Note that from within the work-handler, we cannot flush
565 * ourselves, so only flush outstanding work upon suspend!
567 if (state != FBINFO_STATE_RUNNING)
568 flush_work(&dev_priv->fbdev_suspend_work);
573 * The console lock can be pretty contented on resume due
574 * to all the printk activity. Try to keep it out of the hot
575 * path of resume if possible.
577 WARN_ON(state != FBINFO_STATE_RUNNING);
578 if (!console_trylock()) {
579 /* Don't block our own workqueue as this can
580 * be run in parallel with other i915.ko tasks.
582 schedule_work(&dev_priv->fbdev_suspend_work);
587 /* On resume from hibernation: If the object is shmemfs backed, it has
588 * been restored from swap. If the object is stolen however, it will be
589 * full of whatever garbage was left in there.
591 if (state == FBINFO_STATE_RUNNING &&
592 intel_fb_obj(&ifbdev->fb->base)->stolen)
593 memset_io(info->screen_base, 0, info->screen_size);
595 drm_fb_helper_set_suspend(&ifbdev->helper, state);
598 intel_fbdev_hpd_set_suspend(ifbdev, state);
601 void intel_fbdev_output_poll_changed(struct drm_device *dev)
603 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
609 intel_fbdev_sync(ifbdev);
611 mutex_lock(&ifbdev->hpd_lock);
612 send_hpd = !ifbdev->hpd_suspended;
613 ifbdev->hpd_waiting = true;
614 mutex_unlock(&ifbdev->hpd_lock);
616 if (send_hpd && (ifbdev->vma || ifbdev->helper.deferred_setup))
617 drm_fb_helper_hotplug_event(&ifbdev->helper);
620 void intel_fbdev_restore_mode(struct drm_device *dev)
622 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
627 intel_fbdev_sync(ifbdev);
631 if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper) == 0)
632 intel_fbdev_invalidate(ifbdev);