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
144 obj = ERR_PTR(-ENODEV);
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 wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
209 /* Pin the GGTT vma for our access via info->screen_base.
210 * This also validates that any existing fb inherited from the
211 * BIOS is suitable for own access.
213 vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base,
214 &view, false, &flags);
220 intel_frontbuffer_flush(to_frontbuffer(ifbdev), ORIGIN_DIRTYFB);
222 info = drm_fb_helper_alloc_fbi(helper);
224 DRM_ERROR("Failed to allocate fb_info\n");
229 ifbdev->helper.fb = &ifbdev->fb->base;
231 info->fbops = &intelfb_ops;
233 /* setup aperture base/size for vesafb takeover */
234 info->apertures->ranges[0].base = ggtt->gmadr.start;
235 info->apertures->ranges[0].size = ggtt->mappable_end;
237 /* Our framebuffer is the entirety of fbdev's system memory */
238 info->fix.smem_start =
239 (unsigned long)(ggtt->gmadr.start + vma->node.start);
240 info->fix.smem_len = vma->node.size;
242 vaddr = i915_vma_pin_iomap(vma);
244 DRM_ERROR("Failed to remap framebuffer into virtual memory\n");
245 ret = PTR_ERR(vaddr);
248 info->screen_base = vaddr;
249 info->screen_size = vma->node.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 vga_switcheroo_client_fb_set(pdev, info);
273 intel_unpin_fb_vma(vma, flags);
275 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
279 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
280 .fb_probe = intelfb_create,
283 static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
285 /* We rely on the object-free to release the VMA pinning for
286 * the info->screen_base mmaping. Leaking the VMA is simpler than
287 * trying to rectify all the possible error paths leading here.
290 drm_fb_helper_fini(&ifbdev->helper);
293 intel_unpin_fb_vma(ifbdev->vma, ifbdev->vma_flags);
296 drm_framebuffer_remove(&ifbdev->fb->base);
302 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
303 * The core display code will have read out the current plane configuration,
304 * so we use that to figure out if there's an object for us to use as the
305 * fb, and if so, we re-use it for the fbdev configuration.
307 * Note we only support a single fb shared across pipes for boot (mostly for
308 * fbcon), so we just find the biggest and use that.
310 static bool intel_fbdev_init_bios(struct drm_device *dev,
311 struct intel_fbdev *ifbdev)
313 struct intel_framebuffer *fb = NULL;
314 struct drm_crtc *crtc;
315 struct intel_crtc *intel_crtc;
316 unsigned int max_size = 0;
318 /* Find the largest fb */
319 for_each_crtc(dev, crtc) {
320 struct drm_i915_gem_object *obj =
321 intel_fb_obj(crtc->primary->state->fb);
322 intel_crtc = to_intel_crtc(crtc);
324 if (!crtc->state->active || !obj) {
325 DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
326 pipe_name(intel_crtc->pipe));
330 if (obj->base.size > max_size) {
331 DRM_DEBUG_KMS("found possible fb from plane %c\n",
332 pipe_name(intel_crtc->pipe));
333 fb = to_intel_framebuffer(crtc->primary->state->fb);
334 max_size = obj->base.size;
339 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
343 /* Now make sure all the pipes will fit into it */
344 for_each_crtc(dev, crtc) {
345 unsigned int cur_size;
347 intel_crtc = to_intel_crtc(crtc);
349 if (!crtc->state->active) {
350 DRM_DEBUG_KMS("pipe %c not active, skipping\n",
351 pipe_name(intel_crtc->pipe));
355 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
356 pipe_name(intel_crtc->pipe));
359 * See if the plane fb we found above will fit on this
360 * pipe. Note we need to use the selected fb's pitch and bpp
361 * rather than the current pipe's, since they differ.
363 cur_size = crtc->state->adjusted_mode.crtc_hdisplay;
364 cur_size = cur_size * fb->base.format->cpp[0];
365 if (fb->base.pitches[0] < cur_size) {
366 DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
367 pipe_name(intel_crtc->pipe),
368 cur_size, fb->base.pitches[0]);
373 cur_size = crtc->state->adjusted_mode.crtc_vdisplay;
374 cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
375 cur_size *= fb->base.pitches[0];
376 DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
377 pipe_name(intel_crtc->pipe),
378 crtc->state->adjusted_mode.crtc_hdisplay,
379 crtc->state->adjusted_mode.crtc_vdisplay,
380 fb->base.format->cpp[0] * 8,
383 if (cur_size > max_size) {
384 DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
385 pipe_name(intel_crtc->pipe),
391 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
392 pipe_name(intel_crtc->pipe),
397 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
401 ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
404 drm_framebuffer_get(&ifbdev->fb->base);
406 /* Final pass to check if any active pipes don't have fbs */
407 for_each_crtc(dev, crtc) {
408 intel_crtc = to_intel_crtc(crtc);
410 if (!crtc->state->active)
413 WARN(!crtc->primary->state->fb,
414 "re-used BIOS config but lost an fb on crtc %d\n",
419 DRM_DEBUG_KMS("using BIOS fb for initial console\n");
427 static void intel_fbdev_suspend_worker(struct work_struct *work)
429 intel_fbdev_set_suspend(&container_of(work,
430 struct drm_i915_private,
431 fbdev_suspend_work)->drm,
432 FBINFO_STATE_RUNNING,
436 int intel_fbdev_init(struct drm_device *dev)
438 struct drm_i915_private *dev_priv = to_i915(dev);
439 struct intel_fbdev *ifbdev;
442 if (WARN_ON(!HAS_DISPLAY(dev_priv) || !INTEL_DISPLAY_ENABLED(dev_priv)))
445 ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
449 mutex_init(&ifbdev->hpd_lock);
450 drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
452 if (!intel_fbdev_init_bios(dev, ifbdev))
453 ifbdev->preferred_bpp = 32;
455 ret = drm_fb_helper_init(dev, &ifbdev->helper, 4);
461 dev_priv->fbdev = ifbdev;
462 INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
464 drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
469 static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
471 struct intel_fbdev *ifbdev = data;
473 /* Due to peculiar init order wrt to hpd handling this is separate. */
474 if (drm_fb_helper_initial_config(&ifbdev->helper,
475 ifbdev->preferred_bpp))
476 intel_fbdev_unregister(to_i915(ifbdev->helper.dev));
479 void intel_fbdev_initial_config_async(struct drm_device *dev)
481 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
486 ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
489 static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
494 /* Only serialises with all preceding async calls, hence +1 */
495 async_synchronize_cookie(ifbdev->cookie + 1);
499 void intel_fbdev_unregister(struct drm_i915_private *dev_priv)
501 struct intel_fbdev *ifbdev = dev_priv->fbdev;
506 cancel_work_sync(&dev_priv->fbdev_suspend_work);
507 if (!current_is_async())
508 intel_fbdev_sync(ifbdev);
510 drm_fb_helper_unregister_fbi(&ifbdev->helper);
513 void intel_fbdev_fini(struct drm_i915_private *dev_priv)
515 struct intel_fbdev *ifbdev = fetch_and_zero(&dev_priv->fbdev);
520 intel_fbdev_destroy(ifbdev);
523 /* Suspends/resumes fbdev processing of incoming HPD events. When resuming HPD
524 * processing, fbdev will perform a full connector reprobe if a hotplug event
525 * was received while HPD was suspended.
527 static void intel_fbdev_hpd_set_suspend(struct intel_fbdev *ifbdev, int state)
529 bool send_hpd = false;
531 mutex_lock(&ifbdev->hpd_lock);
532 ifbdev->hpd_suspended = state == FBINFO_STATE_SUSPENDED;
533 send_hpd = !ifbdev->hpd_suspended && ifbdev->hpd_waiting;
534 ifbdev->hpd_waiting = false;
535 mutex_unlock(&ifbdev->hpd_lock);
538 DRM_DEBUG_KMS("Handling delayed fbcon HPD event\n");
539 drm_fb_helper_hotplug_event(&ifbdev->helper);
543 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
545 struct drm_i915_private *dev_priv = to_i915(dev);
546 struct intel_fbdev *ifbdev = dev_priv->fbdev;
547 struct fb_info *info;
549 if (!ifbdev || !ifbdev->vma)
552 info = ifbdev->helper.fbdev;
555 /* Flush any pending work to turn the console on, and then
556 * wait to turn it off. It must be synchronous as we are
557 * about to suspend or unload the driver.
559 * Note that from within the work-handler, we cannot flush
560 * ourselves, so only flush outstanding work upon suspend!
562 if (state != FBINFO_STATE_RUNNING)
563 flush_work(&dev_priv->fbdev_suspend_work);
568 * The console lock can be pretty contented on resume due
569 * to all the printk activity. Try to keep it out of the hot
570 * path of resume if possible.
572 WARN_ON(state != FBINFO_STATE_RUNNING);
573 if (!console_trylock()) {
574 /* Don't block our own workqueue as this can
575 * be run in parallel with other i915.ko tasks.
577 schedule_work(&dev_priv->fbdev_suspend_work);
582 /* On resume from hibernation: If the object is shmemfs backed, it has
583 * been restored from swap. If the object is stolen however, it will be
584 * full of whatever garbage was left in there.
586 if (state == FBINFO_STATE_RUNNING &&
587 intel_fb_obj(&ifbdev->fb->base)->stolen)
588 memset_io(info->screen_base, 0, info->screen_size);
590 drm_fb_helper_set_suspend(&ifbdev->helper, state);
593 intel_fbdev_hpd_set_suspend(ifbdev, state);
596 void intel_fbdev_output_poll_changed(struct drm_device *dev)
598 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
604 intel_fbdev_sync(ifbdev);
606 mutex_lock(&ifbdev->hpd_lock);
607 send_hpd = !ifbdev->hpd_suspended;
608 ifbdev->hpd_waiting = true;
609 mutex_unlock(&ifbdev->hpd_lock);
611 if (send_hpd && (ifbdev->vma || ifbdev->helper.deferred_setup))
612 drm_fb_helper_hotplug_event(&ifbdev->helper);
615 void intel_fbdev_restore_mode(struct drm_device *dev)
617 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
622 intel_fbdev_sync(ifbdev);
626 if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper) == 0)
627 intel_fbdev_invalidate(ifbdev);