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.
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/errno.h>
31 #include <linux/string.h>
33 #include <linux/delay.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_fb_helper.h>
39 #include "intel_drv.h"
40 #include <drm/i915_drm.h>
44 static struct fb_ops intelfb_ops = {
46 .fb_check_var = drm_fb_helper_check_var,
47 .fb_set_par = drm_fb_helper_set_par,
48 .fb_fillrect = cfb_fillrect,
49 .fb_copyarea = cfb_copyarea,
50 .fb_imageblit = cfb_imageblit,
51 .fb_pan_display = drm_fb_helper_pan_display,
52 .fb_blank = drm_fb_helper_blank,
53 .fb_setcmap = drm_fb_helper_setcmap,
54 .fb_debug_enter = drm_fb_helper_debug_enter,
55 .fb_debug_leave = drm_fb_helper_debug_leave,
59 static int intelfb_alloc(struct drm_fb_helper *helper,
60 struct drm_fb_helper_surface_size *sizes)
62 struct intel_fbdev *ifbdev =
63 container_of(helper, struct intel_fbdev, helper);
64 struct drm_framebuffer *fb;
65 struct drm_device *dev = helper->dev;
66 struct drm_mode_fb_cmd2 mode_cmd = {};
67 struct drm_i915_gem_object *obj;
70 /* we don't do packed 24bpp */
71 if (sizes->surface_bpp == 24)
72 sizes->surface_bpp = 32;
74 mode_cmd.width = sizes->surface_width;
75 mode_cmd.height = sizes->surface_height;
77 mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
78 DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
79 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
80 sizes->surface_depth);
82 size = mode_cmd.pitches[0] * mode_cmd.height;
83 size = ALIGN(size, PAGE_SIZE);
84 obj = i915_gem_object_create_stolen(dev, size);
86 obj = i915_gem_alloc_object(dev, size);
88 DRM_ERROR("failed to allocate framebuffer\n");
93 /* Flush everything out, we'll be doing GTT only from now on */
94 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
96 DRM_ERROR("failed to pin obj: %d\n", ret);
100 fb = __intel_framebuffer_create(dev, &mode_cmd, obj);
106 ifbdev->fb = to_intel_framebuffer(fb);
111 i915_gem_object_ggtt_unpin(obj);
113 drm_gem_object_unreference(&obj->base);
118 static int intelfb_create(struct drm_fb_helper *helper,
119 struct drm_fb_helper_surface_size *sizes)
121 struct intel_fbdev *ifbdev =
122 container_of(helper, struct intel_fbdev, helper);
123 struct intel_framebuffer *intel_fb = ifbdev->fb;
124 struct drm_device *dev = helper->dev;
126 struct drm_i915_private *dev_priv = dev->dev_private;
128 struct fb_info *info;
129 struct drm_framebuffer *fb;
130 struct drm_i915_gem_object *obj;
133 bool prealloc = false;
135 mutex_lock(&dev->struct_mutex);
138 (sizes->fb_width > intel_fb->base.width ||
139 sizes->fb_height > intel_fb->base.height)) {
140 DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d),"
142 intel_fb->base.width, intel_fb->base.height,
143 sizes->fb_width, sizes->fb_height);
144 drm_framebuffer_unreference(&intel_fb->base);
145 intel_fb = ifbdev->fb = NULL;
147 if (!intel_fb || WARN_ON(!intel_fb->obj)) {
148 DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
149 ret = intelfb_alloc(helper, sizes);
152 intel_fb = ifbdev->fb;
154 DRM_DEBUG_KMS("re-using BIOS fb\n");
156 sizes->fb_width = intel_fb->base.width;
157 sizes->fb_height = intel_fb->base.height;
161 size = obj->base.size;
164 info = framebuffer_alloc(0, &dev->pdev->dev);
166 info = kmalloc(sizeof(struct fb_info), M_DRM, M_WAITOK | M_ZERO);
177 vga_dev = device_get_parent(dev->dev);
178 info = kmalloc(sizeof(struct fb_info), M_DRM, M_WAITOK | M_ZERO);
179 info->width = sizes->fb_width;
180 info->height = sizes->fb_height;
182 ALIGN(sizes->surface_width * ((sizes->surface_bpp + 7) / 8), 64);
183 info->depth = sizes->surface_bpp;
184 info->paddr = dev->agp->base + i915_gem_obj_ggtt_offset(obj);
185 info->is_vga_boot_display = vga_pci_is_boot_display(vga_dev);
187 (vm_offset_t)pmap_mapdev_attr(info->paddr,
188 sizes->surface_height * info->stride,
189 VM_MEMATTR_WRITE_COMBINING);
192 fb = &ifbdev->fb->base;
194 ifbdev->helper.fb = fb;
195 ifbdev->helper.fbdev = info;
198 strcpy(info->fix.id, "inteldrmfb");
200 info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
201 info->fbops = &intelfb_ops;
203 ret = fb_alloc_cmap(&info->cmap, 256, 0);
208 /* setup aperture base/size for vesafb takeover */
209 info->apertures = alloc_apertures(1);
210 if (!info->apertures) {
214 info->apertures->ranges[0].base = dev->mode_config.fb_base;
215 info->apertures->ranges[0].size = dev_priv->gtt.mappable_end;
217 info->fix.smem_start = dev->mode_config.fb_base + i915_gem_obj_ggtt_offset(obj);
218 info->fix.smem_len = size;
221 ioremap_wc(dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj),
223 if (!info->screen_base) {
227 info->screen_size = size;
229 /* This driver doesn't need a VT switch to restore the mode on resume */
230 info->skip_vt_switch = true;
232 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
233 drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height);
235 /* If the object is shmemfs backed, it will have given us zeroed pages.
236 * If the object is stolen however, it will be full of whatever
237 * garbage was left in there.
239 if (ifbdev->fb->obj->stolen && !prealloc)
240 memset_io(info->screen_base, 0, info->screen_size);
242 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
245 DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08lx, bo %p\n",
246 fb->width, fb->height,
247 i915_gem_obj_ggtt_offset(obj), obj);
249 mutex_unlock(&dev->struct_mutex);
251 vga_switcheroo_client_fb_set(dev->pdev, info);
256 i915_gem_object_ggtt_unpin(obj);
257 drm_gem_object_unreference(&obj->base);
259 mutex_unlock(&dev->struct_mutex);
263 /** Sets the color ramps on behalf of RandR */
264 static void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
267 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
269 intel_crtc->lut_r[regno] = red >> 8;
270 intel_crtc->lut_g[regno] = green >> 8;
271 intel_crtc->lut_b[regno] = blue >> 8;
274 static void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
275 u16 *blue, int regno)
277 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
279 *red = intel_crtc->lut_r[regno] << 8;
280 *green = intel_crtc->lut_g[regno] << 8;
281 *blue = intel_crtc->lut_b[regno] << 8;
284 static struct drm_fb_helper_crtc *
285 intel_fb_helper_crtc(struct drm_fb_helper *fb_helper, struct drm_crtc *crtc)
289 for (i = 0; i < fb_helper->crtc_count; i++)
290 if (fb_helper->crtc_info[i].mode_set.crtc == crtc)
291 return &fb_helper->crtc_info[i];
297 * Try to read the BIOS display configuration and use it for the initial
300 * The BIOS or boot loader will generally create an initial display
301 * configuration for us that includes some set of active pipes and displays.
302 * This routine tries to figure out which pipes and connectors are active
303 * and stuffs them into the crtcs and modes array given to us by the
304 * drm_fb_helper code.
306 * The overall sequence is:
307 * intel_fbdev_init - from driver load
308 * intel_fbdev_init_bios - initialize the intel_fbdev using BIOS data
309 * drm_fb_helper_init - build fb helper structs
310 * drm_fb_helper_single_add_all_connectors - more fb helper structs
311 * intel_fbdev_initial_config - apply the config
312 * drm_fb_helper_initial_config - call ->probe then register_framebuffer()
313 * drm_setup_crtcs - build crtc config for fbdev
314 * intel_fb_initial_config - find active connectors etc
315 * drm_fb_helper_single_fb_probe - set up fbdev
316 * intelfb_create - re-use or alloc fb, build out fbdev structs
318 * Note that we don't make special consideration whether we could actually
319 * switch to the selected modes without a full modeset. E.g. when the display
320 * is in VGA mode we need to recalculate watermarks and set a new high-res
321 * framebuffer anyway.
323 static bool intel_fb_initial_config(struct drm_fb_helper *fb_helper,
324 struct drm_fb_helper_crtc **crtcs,
325 struct drm_display_mode **modes,
326 bool *enabled, int width, int height)
328 struct drm_device *dev = fb_helper->dev;
331 bool fallback = true;
332 int num_connectors_enabled = 0;
333 int num_connectors_detected = 0;
336 * If the user specified any force options, just bail here
337 * and use that config.
339 for (i = 0; i < fb_helper->connector_count; i++) {
340 struct drm_fb_helper_connector *fb_conn;
341 struct drm_connector *connector;
343 fb_conn = fb_helper->connector_info[i];
344 connector = fb_conn->connector;
349 if (connector->force != DRM_FORCE_UNSPECIFIED)
353 save_enabled = kcalloc(dev->mode_config.num_connector, sizeof(bool),
358 memcpy(save_enabled, enabled, dev->mode_config.num_connector);
360 for (i = 0; i < fb_helper->connector_count; i++) {
361 struct drm_fb_helper_connector *fb_conn;
362 struct drm_connector *connector;
363 struct drm_encoder *encoder;
364 struct drm_fb_helper_crtc *new_crtc;
366 fb_conn = fb_helper->connector_info[i];
367 connector = fb_conn->connector;
369 if (connector->status == connector_status_connected)
370 num_connectors_detected++;
373 DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
378 encoder = connector->encoder;
379 if (!encoder || WARN_ON(!encoder->crtc)) {
380 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
386 num_connectors_enabled++;
388 new_crtc = intel_fb_helper_crtc(fb_helper, encoder->crtc);
391 * Make sure we're not trying to drive multiple connectors
392 * with a single CRTC, since our cloning support may not
395 for (j = 0; j < fb_helper->connector_count; j++) {
396 if (crtcs[j] == new_crtc) {
397 DRM_DEBUG_KMS("fallback: cloned configuration\n");
403 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
406 /* go for command line mode first */
407 modes[i] = drm_pick_cmdline_mode(fb_conn, width, height);
409 /* try for preferred next */
411 DRM_DEBUG_KMS("looking for preferred mode on connector %s\n",
413 modes[i] = drm_has_preferred_mode(fb_conn, width,
417 /* No preferred mode marked by the EDID? Are there any modes? */
418 if (!modes[i] && !list_empty(&connector->modes)) {
419 DRM_DEBUG_KMS("using first mode listed on connector %s\n",
421 modes[i] = list_first_entry(&connector->modes,
422 struct drm_display_mode,
426 /* last resort: use current mode */
429 * IMPORTANT: We want to use the adjusted mode (i.e.
430 * after the panel fitter upscaling) as the initial
431 * config, not the input mode, which is what crtc->mode
432 * usually contains. But since our current fastboot
433 * code puts a mode derived from the post-pfit timings
434 * into crtc->mode this works out correctly. We don't
435 * use hwmode anywhere right now, so use it for this
436 * since the fb helper layer wants a pointer to
439 DRM_DEBUG_KMS("looking for current mode on connector %s\n",
441 intel_mode_from_pipe_config(&encoder->crtc->hwmode,
442 &to_intel_crtc(encoder->crtc)->config);
443 modes[i] = &encoder->crtc->hwmode;
447 DRM_DEBUG_KMS("connector %s on pipe %d [CRTC:%d]: %dx%d%s\n",
449 pipe_name(to_intel_crtc(encoder->crtc)->pipe),
450 encoder->crtc->base.id,
451 modes[i]->hdisplay, modes[i]->vdisplay,
452 modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" :"");
458 * If the BIOS didn't enable everything it could, fall back to have the
459 * same user experiencing of lighting up as much as possible like the
460 * fbdev helper library.
462 if (num_connectors_enabled != num_connectors_detected &&
463 num_connectors_enabled < INTEL_INFO(dev)->num_pipes) {
464 DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
465 DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
466 num_connectors_detected);
472 DRM_DEBUG_KMS("Not using firmware configuration\n");
473 memcpy(enabled, save_enabled, dev->mode_config.num_connector);
482 static struct drm_fb_helper_funcs intel_fb_helper_funcs = {
483 .initial_config = intel_fb_initial_config,
484 .gamma_set = intel_crtc_fb_gamma_set,
485 .gamma_get = intel_crtc_fb_gamma_get,
486 .fb_probe = intelfb_create,
489 static void intel_fbdev_destroy(struct drm_device *dev,
490 struct intel_fbdev *ifbdev)
493 if (ifbdev->helper.fbdev) {
494 struct fb_info *info = ifbdev->helper.fbdev;
496 unregister_framebuffer(info);
497 iounmap(info->screen_base);
499 fb_dealloc_cmap(&info->cmap);
501 framebuffer_release(info);
505 drm_fb_helper_fini(&ifbdev->helper);
507 drm_framebuffer_unregister_private(&ifbdev->fb->base);
508 drm_framebuffer_unreference(&ifbdev->fb->base);
512 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
513 * The core display code will have read out the current plane configuration,
514 * so we use that to figure out if there's an object for us to use as the
515 * fb, and if so, we re-use it for the fbdev configuration.
517 * Note we only support a single fb shared across pipes for boot (mostly for
518 * fbcon), so we just find the biggest and use that.
520 static bool intel_fbdev_init_bios(struct drm_device *dev,
521 struct intel_fbdev *ifbdev)
523 struct intel_framebuffer *fb = NULL;
524 struct drm_crtc *crtc;
525 struct intel_crtc *intel_crtc;
526 struct intel_plane_config *plane_config = NULL;
527 unsigned int max_size = 0;
532 /* Find the largest fb */
533 for_each_crtc(dev, crtc) {
534 intel_crtc = to_intel_crtc(crtc);
536 if (!intel_crtc->active || !crtc->primary->fb) {
537 DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
538 pipe_name(intel_crtc->pipe));
542 if (intel_crtc->plane_config.size > max_size) {
543 DRM_DEBUG_KMS("found possible fb from plane %c\n",
544 pipe_name(intel_crtc->pipe));
545 plane_config = &intel_crtc->plane_config;
546 fb = to_intel_framebuffer(crtc->primary->fb);
547 max_size = plane_config->size;
552 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
556 /* Now make sure all the pipes will fit into it */
557 for_each_crtc(dev, crtc) {
558 unsigned int cur_size;
560 intel_crtc = to_intel_crtc(crtc);
562 if (!intel_crtc->active) {
563 DRM_DEBUG_KMS("pipe %c not active, skipping\n",
564 pipe_name(intel_crtc->pipe));
568 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
569 pipe_name(intel_crtc->pipe));
572 * See if the plane fb we found above will fit on this
573 * pipe. Note we need to use the selected fb's pitch and bpp
574 * rather than the current pipe's, since they differ.
576 cur_size = intel_crtc->config.adjusted_mode.crtc_hdisplay;
577 cur_size = cur_size * fb->base.bits_per_pixel / 8;
578 if (fb->base.pitches[0] < cur_size) {
579 DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
580 pipe_name(intel_crtc->pipe),
581 cur_size, fb->base.pitches[0]);
587 cur_size = intel_crtc->config.adjusted_mode.crtc_vdisplay;
588 cur_size = ALIGN(cur_size, plane_config->tiled ? (IS_GEN2(dev) ? 16 : 8) : 1);
589 cur_size *= fb->base.pitches[0];
590 DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
591 pipe_name(intel_crtc->pipe),
592 intel_crtc->config.adjusted_mode.crtc_hdisplay,
593 intel_crtc->config.adjusted_mode.crtc_vdisplay,
594 fb->base.bits_per_pixel,
597 if (cur_size > max_size) {
598 DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
599 pipe_name(intel_crtc->pipe),
606 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
607 pipe_name(intel_crtc->pipe),
612 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
616 ifbdev->preferred_bpp = fb->base.bits_per_pixel;
619 drm_framebuffer_reference(&ifbdev->fb->base);
621 /* Final pass to check if any active pipes don't have fbs */
622 for_each_crtc(dev, crtc) {
623 intel_crtc = to_intel_crtc(crtc);
625 if (!intel_crtc->active)
628 WARN(!crtc->primary->fb,
629 "re-used BIOS config but lost an fb on crtc %d\n",
634 DRM_DEBUG_KMS("using BIOS fb for initial console\n");
642 int intel_fbdev_init(struct drm_device *dev)
644 struct intel_fbdev *ifbdev;
645 struct drm_i915_private *dev_priv = dev->dev_private;
648 if (WARN_ON(INTEL_INFO(dev)->num_pipes == 0))
651 ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
655 ifbdev->helper.funcs = &intel_fb_helper_funcs;
656 if (!intel_fbdev_init_bios(dev, ifbdev))
657 ifbdev->preferred_bpp = 32;
659 ret = drm_fb_helper_init(dev, &ifbdev->helper,
660 INTEL_INFO(dev)->num_pipes, 4);
666 dev_priv->fbdev = ifbdev;
667 drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
672 void intel_fbdev_initial_config(struct drm_device *dev)
674 struct drm_i915_private *dev_priv = dev->dev_private;
676 /* Due to peculiar init order wrt to hpd handling this is separate. */
677 drm_fb_helper_initial_config(&dev_priv->fbdev->helper, 32);
680 void intel_fbdev_fini(struct drm_device *dev)
682 struct drm_i915_private *dev_priv = dev->dev_private;
683 if (!dev_priv->fbdev)
686 intel_fbdev_destroy(dev, dev_priv->fbdev);
687 kfree(dev_priv->fbdev);
688 dev_priv->fbdev = NULL;
691 void intel_fbdev_set_suspend(struct drm_device *dev, int state)
694 struct drm_i915_private *dev_priv = dev->dev_private;
695 struct intel_fbdev *ifbdev = dev_priv->fbdev;
696 struct fb_info *info;
701 info = ifbdev->helper.fbdev;
703 /* On resume from hibernation: If the object is shmemfs backed, it has
704 * been restored from swap. If the object is stolen however, it will be
705 * full of whatever garbage was left in there.
707 if (state == FBINFO_STATE_RUNNING && ifbdev->ifb.obj->stolen)
708 memset_io(info->screen_base, 0, info->screen_size);
710 fb_set_suspend(info, state);
714 void intel_fbdev_output_poll_changed(struct drm_device *dev)
716 struct drm_i915_private *dev_priv = dev->dev_private;
717 drm_fb_helper_hotplug_event(&dev_priv->fbdev->helper);
720 void intel_fbdev_restore_mode(struct drm_device *dev)
723 struct drm_i915_private *dev_priv = dev->dev_private;
725 if (INTEL_INFO(dev)->num_pipes == 0)
728 ret = drm_fb_helper_restore_fbdev_mode_unlocked(&dev_priv->fbdev->helper);
730 DRM_DEBUG("failed to restore crtc mode\n");