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/module.h>
28 #include <linux/kernel.h>
29 #include <linux/errno.h>
30 #include <linux/string.h>
32 #include <linux/tty.h>
33 #include <linux/sysrq.h>
34 #include <linux/delay.h>
36 #include <linux/init.h>
37 #include <linux/vga_switcheroo.h>
40 #include <drm/drm_crtc.h>
41 #include <drm/drm_fb_helper.h>
42 #include "intel_drv.h"
43 #include <drm/i915_drm.h>
46 static struct fb_ops intelfb_ops = {
48 .fb_check_var = drm_fb_helper_check_var,
49 .fb_set_par = drm_fb_helper_set_par,
50 .fb_fillrect = cfb_fillrect,
51 .fb_copyarea = cfb_copyarea,
52 .fb_imageblit = cfb_imageblit,
53 .fb_pan_display = drm_fb_helper_pan_display,
54 .fb_blank = drm_fb_helper_blank,
55 .fb_setcmap = drm_fb_helper_setcmap,
56 .fb_debug_enter = drm_fb_helper_debug_enter,
57 .fb_debug_leave = drm_fb_helper_debug_leave,
60 static int intelfb_alloc(struct drm_fb_helper *helper,
61 struct drm_fb_helper_surface_size *sizes)
63 struct intel_fbdev *ifbdev =
64 container_of(helper, struct intel_fbdev, helper);
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 fb: %d\n", ret);
100 ret = intel_framebuffer_init(dev, &ifbdev->ifb, &mode_cmd, obj);
107 i915_gem_object_unpin(obj);
109 drm_gem_object_unreference(&obj->base);
114 static int intelfb_create(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 intel_framebuffer *intel_fb = &ifbdev->ifb;
120 struct drm_device *dev = helper->dev;
121 struct drm_i915_private *dev_priv = dev->dev_private;
122 struct fb_info *info;
123 struct drm_framebuffer *fb;
124 struct drm_i915_gem_object *obj;
126 bool prealloc = false;
128 mutex_lock(&dev->struct_mutex);
130 if (!intel_fb->obj) {
131 DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
132 ret = intelfb_alloc(helper, sizes);
136 DRM_DEBUG_KMS("re-using BIOS fb\n");
138 sizes->fb_width = intel_fb->base.width;
139 sizes->fb_height = intel_fb->base.height;
143 size = obj->base.size;
145 info = framebuffer_alloc(0, &dev->pdev->dev);
153 fb = &ifbdev->ifb.base;
155 ifbdev->helper.fb = fb;
156 ifbdev->helper.fbdev = info;
158 strcpy(info->fix.id, "inteldrmfb");
160 info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
161 info->fbops = &intelfb_ops;
163 ret = fb_alloc_cmap(&info->cmap, 256, 0);
168 /* setup aperture base/size for vesafb takeover */
169 info->apertures = alloc_apertures(1);
170 if (!info->apertures) {
174 info->apertures->ranges[0].base = dev->mode_config.fb_base;
175 info->apertures->ranges[0].size = dev_priv->gtt.mappable_end;
177 info->fix.smem_start = dev->mode_config.fb_base + i915_gem_obj_ggtt_offset(obj);
178 info->fix.smem_len = size;
181 ioremap_wc(dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj),
183 if (!info->screen_base) {
187 info->screen_size = size;
189 /* This driver doesn't need a VT switch to restore the mode on resume */
190 info->skip_vt_switch = true;
192 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
193 drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height);
195 /* If the object is shmemfs backed, it will have given us zeroed pages.
196 * If the object is stolen however, it will be full of whatever
197 * garbage was left in there.
199 if (ifbdev->ifb.obj->stolen && !prealloc)
200 memset_io(info->screen_base, 0, info->screen_size);
202 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
204 DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08lx, bo %p\n",
205 fb->width, fb->height,
206 i915_gem_obj_ggtt_offset(obj), obj);
208 mutex_unlock(&dev->struct_mutex);
209 vga_switcheroo_client_fb_set(dev->pdev, info);
213 i915_gem_object_unpin(obj);
214 drm_gem_object_unreference(&obj->base);
216 mutex_unlock(&dev->struct_mutex);
220 /** Sets the color ramps on behalf of RandR */
221 static void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
224 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
226 intel_crtc->lut_r[regno] = red >> 8;
227 intel_crtc->lut_g[regno] = green >> 8;
228 intel_crtc->lut_b[regno] = blue >> 8;
231 static void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
232 u16 *blue, int regno)
234 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
236 *red = intel_crtc->lut_r[regno] << 8;
237 *green = intel_crtc->lut_g[regno] << 8;
238 *blue = intel_crtc->lut_b[regno] << 8;
241 static struct drm_fb_helper_crtc *
242 intel_fb_helper_crtc(struct drm_fb_helper *fb_helper, struct drm_crtc *crtc)
246 for (i = 0; i < fb_helper->crtc_count; i++)
247 if (fb_helper->crtc_info[i].mode_set.crtc == crtc)
248 return &fb_helper->crtc_info[i];
254 * Try to read the BIOS display configuration and use it for the initial
257 * The BIOS or boot loader will generally create an initial display
258 * configuration for us that includes some set of active pipes and displays.
259 * This routine tries to figure out which pipes and connectors are active
260 * and stuffs them into the crtcs and modes array given to us by the
261 * drm_fb_helper code.
263 * The overall sequence is:
264 * intel_fbdev_init - from driver load
265 * intel_fbdev_init_bios - initialize the intel_fbdev using BIOS data
266 * drm_fb_helper_init - build fb helper structs
267 * drm_fb_helper_single_add_all_connectors - more fb helper structs
268 * intel_fbdev_initial_config - apply the config
269 * drm_fb_helper_initial_config - call ->probe then register_framebuffer()
270 * drm_setup_crtcs - build crtc config for fbdev
271 * intel_fb_initial_config - find active connectors etc
272 * drm_fb_helper_single_fb_probe - set up fbdev
273 * intelfb_create - re-use or alloc fb, build out fbdev structs
275 * Note that we don't make special consideration whether we could actually
276 * switch to the selected modes without a full modeset. E.g. when the display
277 * is in VGA mode we need to recalculate watermarks and set a new high-res
278 * framebuffer anyway.
280 static bool intel_fb_initial_config(struct drm_fb_helper *fb_helper,
281 struct drm_fb_helper_crtc **crtcs,
282 struct drm_display_mode **modes,
283 bool *enabled, int width, int height)
287 for (i = 0; i < fb_helper->connector_count; i++) {
288 struct drm_fb_helper_connector *fb_conn;
289 struct drm_connector *connector;
290 struct drm_encoder *encoder;
291 struct drm_fb_helper_crtc *new_crtc;
293 fb_conn = fb_helper->connector_info[i];
294 connector = fb_conn->connector;
296 DRM_DEBUG_KMS("connector %d not enabled, skipping\n",
301 encoder = connector->encoder;
302 if (!encoder || WARN_ON(!encoder->crtc)) {
303 DRM_DEBUG_KMS("connector %d has no encoder or crtc, skipping\n",
309 new_crtc = intel_fb_helper_crtc(fb_helper, encoder->crtc);
312 * Make sure we're not trying to drive multiple connectors
313 * with a single CRTC, since our cloning support may not
316 for (j = 0; j < fb_helper->connector_count; j++) {
317 if (crtcs[j] == new_crtc)
321 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
322 fb_conn->connector->base.id);
324 /* go for command line mode first */
325 modes[i] = drm_pick_cmdline_mode(fb_conn, width, height);
327 /* try for preferred next */
329 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
330 fb_conn->connector->base.id);
331 modes[i] = drm_has_preferred_mode(fb_conn, width,
335 /* last resort: use current mode */
338 * IMPORTANT: We want to use the adjusted mode (i.e.
339 * after the panel fitter upscaling) as the initial
340 * config, not the input mode, which is what crtc->mode
341 * usually contains. But since our current fastboot
342 * code puts a mode derived from the post-pfit timings
343 * into crtc->mode this works out correctly. We don't
344 * use hwmode anywhere right now, so use it for this
345 * since the fb helper layer wants a pointer to
348 intel_mode_from_pipe_config(&encoder->crtc->hwmode,
349 &to_intel_crtc(encoder->crtc)->config);
350 modes[i] = &encoder->crtc->hwmode;
354 DRM_DEBUG_KMS("connector %s on crtc %d: %s\n",
355 drm_get_connector_name(connector),
356 encoder->crtc->base.id,
363 static struct drm_fb_helper_funcs intel_fb_helper_funcs = {
364 .initial_config = intel_fb_initial_config,
365 .gamma_set = intel_crtc_fb_gamma_set,
366 .gamma_get = intel_crtc_fb_gamma_get,
367 .fb_probe = intelfb_create,
370 static void intel_fbdev_destroy(struct drm_device *dev,
371 struct intel_fbdev *ifbdev)
373 if (ifbdev->helper.fbdev) {
374 struct fb_info *info = ifbdev->helper.fbdev;
376 unregister_framebuffer(info);
377 iounmap(info->screen_base);
379 fb_dealloc_cmap(&info->cmap);
381 framebuffer_release(info);
384 drm_fb_helper_fini(&ifbdev->helper);
386 drm_framebuffer_unregister_private(&ifbdev->ifb.base);
387 intel_framebuffer_fini(&ifbdev->ifb);
391 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
392 * The core display code will have read out the current plane configuration,
393 * so we use that to figure out if there's an object for us to use as the
394 * fb, and if so, we re-use it for the fbdev configuration.
396 * Note we only support a single fb shared across pipes for boot (mostly for
397 * fbcon), so we just find the biggest and use that.
399 static bool intel_fbdev_init_bios(struct drm_device *dev,
400 struct intel_fbdev *ifbdev)
402 struct intel_framebuffer *fb = NULL;
403 struct drm_crtc *crtc;
404 struct intel_crtc *intel_crtc;
405 struct intel_plane_config *plane_config = NULL;
406 unsigned int max_size = 0;
411 /* Find the largest fb */
412 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
413 intel_crtc = to_intel_crtc(crtc);
415 if (!intel_crtc->active || !intel_crtc->plane_config.fb) {
416 DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
417 pipe_name(intel_crtc->pipe));
421 if (intel_crtc->plane_config.size > max_size) {
422 DRM_DEBUG_KMS("found possible fb from plane %c\n",
423 pipe_name(intel_crtc->pipe));
424 plane_config = &intel_crtc->plane_config;
425 fb = plane_config->fb;
426 max_size = plane_config->size;
431 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
435 /* Now make sure all the pipes will fit into it */
436 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
437 unsigned int cur_size;
439 intel_crtc = to_intel_crtc(crtc);
441 if (!intel_crtc->active) {
442 DRM_DEBUG_KMS("pipe %c not active, skipping\n",
443 pipe_name(intel_crtc->pipe));
447 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
448 pipe_name(intel_crtc->pipe));
451 * See if the plane fb we found above will fit on this
452 * pipe. Note we need to use the selected fb's bpp rather
453 * than the current pipe's, since they could be different.
455 cur_size = intel_crtc->config.adjusted_mode.crtc_hdisplay *
456 intel_crtc->config.adjusted_mode.crtc_vdisplay;
457 DRM_DEBUG_KMS("pipe %c area: %d\n", pipe_name(intel_crtc->pipe),
459 cur_size *= fb->base.bits_per_pixel / 8;
460 DRM_DEBUG_KMS("total size %d (bpp %d)\n", cur_size,
461 fb->base.bits_per_pixel / 8);
463 if (cur_size > max_size) {
464 DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
465 pipe_name(intel_crtc->pipe),
472 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
473 pipe_name(intel_crtc->pipe),
477 /* Free unused fbs */
478 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
479 struct intel_framebuffer *cur_fb;
481 intel_crtc = to_intel_crtc(crtc);
482 cur_fb = intel_crtc->plane_config.fb;
484 if (cur_fb && cur_fb != fb)
485 drm_framebuffer_unreference(&cur_fb->base);
489 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
493 ifbdev->preferred_bpp = plane_config->fb->base.bits_per_pixel;
496 /* Assuming a single fb across all pipes here */
497 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
498 intel_crtc = to_intel_crtc(crtc);
500 if (!intel_crtc->active)
504 * This should only fail on the first one so we don't need
505 * to cleanup any secondary crtc->fbs
507 if (intel_pin_and_fence_fb_obj(dev, fb->obj, NULL))
510 crtc->fb = &fb->base;
511 drm_gem_object_reference(&fb->obj->base);
512 drm_framebuffer_reference(&fb->base);
515 /* Final pass to check if any active pipes don't have fbs */
516 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
517 intel_crtc = to_intel_crtc(crtc);
519 if (!intel_crtc->active)
523 "re-used BIOS config but lost an fb on crtc %d\n",
528 DRM_DEBUG_KMS("using BIOS fb for initial console\n");
532 drm_framebuffer_unreference(&fb->base);
538 int intel_fbdev_init(struct drm_device *dev)
540 struct intel_fbdev *ifbdev;
541 struct drm_i915_private *dev_priv = dev->dev_private;
544 if (WARN_ON(INTEL_INFO(dev)->num_pipes == 0))
547 ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
551 ifbdev->helper.funcs = &intel_fb_helper_funcs;
552 if (!intel_fbdev_init_bios(dev, ifbdev))
553 ifbdev->preferred_bpp = 32;
555 ret = drm_fb_helper_init(dev, &ifbdev->helper,
556 INTEL_INFO(dev)->num_pipes, 4);
562 dev_priv->fbdev = ifbdev;
563 drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
568 void intel_fbdev_initial_config(struct drm_device *dev)
570 struct drm_i915_private *dev_priv = dev->dev_private;
571 struct intel_fbdev *ifbdev = dev_priv->fbdev;
573 /* Due to peculiar init order wrt to hpd handling this is separate. */
574 drm_fb_helper_initial_config(&ifbdev->helper, ifbdev->preferred_bpp);
577 void intel_fbdev_fini(struct drm_device *dev)
579 struct drm_i915_private *dev_priv = dev->dev_private;
580 if (!dev_priv->fbdev)
583 intel_fbdev_destroy(dev, dev_priv->fbdev);
584 kfree(dev_priv->fbdev);
585 dev_priv->fbdev = NULL;
588 void intel_fbdev_set_suspend(struct drm_device *dev, int state)
590 struct drm_i915_private *dev_priv = dev->dev_private;
591 struct intel_fbdev *ifbdev = dev_priv->fbdev;
592 struct fb_info *info;
597 info = ifbdev->helper.fbdev;
599 /* On resume from hibernation: If the object is shmemfs backed, it has
600 * been restored from swap. If the object is stolen however, it will be
601 * full of whatever garbage was left in there.
603 if (state == FBINFO_STATE_RUNNING && ifbdev->ifb.obj->stolen)
604 memset_io(info->screen_base, 0, info->screen_size);
606 fb_set_suspend(info, state);
609 void intel_fbdev_output_poll_changed(struct drm_device *dev)
611 struct drm_i915_private *dev_priv = dev->dev_private;
613 drm_fb_helper_hotplug_event(&dev_priv->fbdev->helper);
616 void intel_fbdev_restore_mode(struct drm_device *dev)
619 struct drm_i915_private *dev_priv = dev->dev_private;
621 if (!dev_priv->fbdev)
624 drm_modeset_lock_all(dev);
626 ret = drm_fb_helper_restore_fbdev_mode(&dev_priv->fbdev->helper);
628 DRM_DEBUG("failed to restore crtc mode\n");
630 drm_modeset_unlock_all(dev);