blob: cf052a39558d255a8fd08eaff1097bbfbaa9cbbc [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2007 David Airlie
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 *
23 * Authors:
24 * David Airlie
25 */
26
27#include <linux/module.h>
28#include <linux/kernel.h>
Chris Wilson82e3b8c2014-08-13 13:09:46 +010029#include <linux/console.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/errno.h>
31#include <linux/string.h>
32#include <linux/mm.h>
33#include <linux/tty.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080034#include <linux/sysrq.h>
35#include <linux/delay.h>
36#include <linux/fb.h>
37#include <linux/init.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100038#include <linux/vga_switcheroo.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080039
David Howells760285e2012-10-02 18:01:07 +010040#include <drm/drmP.h>
41#include <drm/drm_crtc.h>
42#include <drm/drm_fb_helper.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080043#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010044#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080045#include "i915_drv.h"
46
Daniel Vettere9910772014-06-18 15:05:19 +020047static int intel_fbdev_set_par(struct fb_info *info)
48{
49 struct drm_fb_helper *fb_helper = info->par;
50 struct intel_fbdev *ifbdev =
51 container_of(fb_helper, struct intel_fbdev, helper);
52 int ret;
53
54 ret = drm_fb_helper_set_par(info);
55
56 if (ret == 0) {
57 /*
58 * FIXME: fbdev presumes that all callbacks also work from
59 * atomic contexts and relies on that for emergency oops
60 * printing. KMS totally doesn't do that and the locking here is
61 * by far not the only place this goes wrong. Ignore this for
62 * now until we solve this for real.
63 */
64 mutex_lock(&fb_helper->dev->struct_mutex);
65 ret = i915_gem_object_set_to_gtt_domain(ifbdev->fb->obj,
66 true);
67 mutex_unlock(&fb_helper->dev->struct_mutex);
68 }
69
70 return ret;
71}
72
Jesse Barnes79e53942008-11-07 14:24:08 -080073static struct fb_ops intelfb_ops = {
74 .owner = THIS_MODULE,
Dave Airlie785b93e2009-08-28 15:46:53 +100075 .fb_check_var = drm_fb_helper_check_var,
Daniel Vettere9910772014-06-18 15:05:19 +020076 .fb_set_par = intel_fbdev_set_par,
Jesse Barnes79e53942008-11-07 14:24:08 -080077 .fb_fillrect = cfb_fillrect,
78 .fb_copyarea = cfb_copyarea,
79 .fb_imageblit = cfb_imageblit,
Dave Airlie785b93e2009-08-28 15:46:53 +100080 .fb_pan_display = drm_fb_helper_pan_display,
81 .fb_blank = drm_fb_helper_blank,
Dave Airlie068143d2009-10-05 09:58:02 +100082 .fb_setcmap = drm_fb_helper_setcmap,
Jesse Barnes81255562010-08-02 12:07:50 -070083 .fb_debug_enter = drm_fb_helper_debug_enter,
84 .fb_debug_leave = drm_fb_helper_debug_leave,
Jesse Barnes79e53942008-11-07 14:24:08 -080085};
86
Jesse Barnes8b4f49e2013-11-25 15:51:16 -080087static int intelfb_alloc(struct drm_fb_helper *helper,
88 struct drm_fb_helper_surface_size *sizes)
Jesse Barnes79e53942008-11-07 14:24:08 -080089{
Ville Syrjälä34308242013-05-31 20:07:05 +030090 struct intel_fbdev *ifbdev =
91 container_of(helper, struct intel_fbdev, helper);
Daniel Vettera8bb6812014-02-10 18:00:39 +010092 struct drm_framebuffer *fb;
Ville Syrjälä34308242013-05-31 20:07:05 +030093 struct drm_device *dev = helper->dev;
Ville Syrjälä3a7f2f62012-05-24 21:08:56 +030094 struct drm_mode_fb_cmd2 mode_cmd = {};
Chris Wilson05394f32010-11-08 19:18:58 +000095 struct drm_i915_gem_object *obj;
James Simmons57084d02010-12-20 19:10:39 +000096 int size, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080097
Dave Airlieb8c00ac2009-10-06 13:54:01 +100098 /* we don't do packed 24bpp */
Dave Airlie38651672010-03-30 05:34:13 +000099 if (sizes->surface_bpp == 24)
100 sizes->surface_bpp = 32;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000101
Dave Airlie38651672010-03-30 05:34:13 +0000102 mode_cmd.width = sizes->surface_width;
103 mode_cmd.height = sizes->surface_height;
Jesse Barnes79e53942008-11-07 14:24:08 -0800104
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300105 mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
106 DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800107 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
108 sizes->surface_depth);
Jesse Barnes79e53942008-11-07 14:24:08 -0800109
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800110 size = mode_cmd.pitches[0] * mode_cmd.height;
Fabian Frederick1267a262014-07-01 20:39:41 +0200111 size = PAGE_ALIGN(size);
Chris Wilson0ffb0ff2012-11-15 11:32:27 +0000112 obj = i915_gem_object_create_stolen(dev, size);
113 if (obj == NULL)
114 obj = i915_gem_alloc_object(dev, size);
Chris Wilson05394f32010-11-08 19:18:58 +0000115 if (!obj) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700116 DRM_ERROR("failed to allocate framebuffer\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800117 ret = -ENOMEM;
118 goto out;
119 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800120
Chris Wilson72133422010-09-13 23:56:38 +0100121 /* Flush everything out, we'll be doing GTT only from now on */
Ben Widawskyc43b5632012-04-16 14:07:40 -0700122 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -0800123 if (ret) {
Daniel Vettera8bb6812014-02-10 18:00:39 +0100124 DRM_ERROR("failed to pin obj: %d\n", ret);
Jesse Barnes79e53942008-11-07 14:24:08 -0800125 goto out_unref;
126 }
127
Daniel Vettera8bb6812014-02-10 18:00:39 +0100128 fb = __intel_framebuffer_create(dev, &mode_cmd, obj);
129 if (IS_ERR(fb)) {
130 ret = PTR_ERR(fb);
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800131 goto out_unpin;
Daniel Vettera8bb6812014-02-10 18:00:39 +0100132 }
133
134 ifbdev->fb = to_intel_framebuffer(fb);
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800135
136 return 0;
137
138out_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800139 i915_gem_object_ggtt_unpin(obj);
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800140out_unref:
141 drm_gem_object_unreference(&obj->base);
142out:
143 return ret;
144}
145
146static int intelfb_create(struct drm_fb_helper *helper,
147 struct drm_fb_helper_surface_size *sizes)
148{
149 struct intel_fbdev *ifbdev =
150 container_of(helper, struct intel_fbdev, helper);
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800151 struct intel_framebuffer *intel_fb = ifbdev->fb;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800152 struct drm_device *dev = helper->dev;
153 struct drm_i915_private *dev_priv = dev->dev_private;
154 struct fb_info *info;
155 struct drm_framebuffer *fb;
156 struct drm_i915_gem_object *obj;
157 int size, ret;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800158 bool prealloc = false;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800159
160 mutex_lock(&dev->struct_mutex);
161
Chris Wilsonedd586f2014-04-23 08:54:31 +0100162 if (intel_fb &&
163 (sizes->fb_width > intel_fb->base.width ||
164 sizes->fb_height > intel_fb->base.height)) {
165 DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d),"
166 " releasing it\n",
167 intel_fb->base.width, intel_fb->base.height,
168 sizes->fb_width, sizes->fb_height);
169 drm_framebuffer_unreference(&intel_fb->base);
170 intel_fb = ifbdev->fb = NULL;
171 }
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800172 if (!intel_fb || WARN_ON(!intel_fb->obj)) {
Jesse Barnes48e92122013-11-26 11:25:54 -0800173 DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800174 ret = intelfb_alloc(helper, sizes);
175 if (ret)
176 goto out_unlock;
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800177 intel_fb = ifbdev->fb;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800178 } else {
Jesse Barnes48e92122013-11-26 11:25:54 -0800179 DRM_DEBUG_KMS("re-using BIOS fb\n");
Jesse Barnesd978ef12014-03-07 08:57:51 -0800180 prealloc = true;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800181 sizes->fb_width = intel_fb->base.width;
182 sizes->fb_height = intel_fb->base.height;
183 }
184
185 obj = intel_fb->obj;
186 size = obj->base.size;
187
188 info = framebuffer_alloc(0, &dev->pdev->dev);
Jesse Barnes79e53942008-11-07 14:24:08 -0800189 if (!info) {
190 ret = -ENOMEM;
Chris Wilsonb4476f52009-02-11 14:26:36 +0000191 goto out_unpin;
Jesse Barnes79e53942008-11-07 14:24:08 -0800192 }
193
Ville Syrjälä34308242013-05-31 20:07:05 +0300194 info->par = helper;
Jesse Barnes79e53942008-11-07 14:24:08 -0800195
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800196 fb = &ifbdev->fb->base;
Dave Airlie38651672010-03-30 05:34:13 +0000197
198 ifbdev->helper.fb = fb;
199 ifbdev->helper.fbdev = info;
Dave Airlie785b93e2009-08-28 15:46:53 +1000200
Jesse Barnes79e53942008-11-07 14:24:08 -0800201 strcpy(info->fix.id, "inteldrmfb");
Jesse Barnes79e53942008-11-07 14:24:08 -0800202
Jesse Barnes8fd4bd22010-06-23 12:56:12 -0700203 info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
Jesse Barnes79e53942008-11-07 14:24:08 -0800204 info->fbops = &intelfb_ops;
205
Chris Wilson35c30472010-12-22 14:07:12 +0000206 ret = fb_alloc_cmap(&info->cmap, 256, 0);
207 if (ret) {
208 ret = -ENOMEM;
209 goto out_unpin;
210 }
Dave Airlie4410f392009-06-16 15:34:38 -0700211 /* setup aperture base/size for vesafb takeover */
Marcin Slusarz1471ca92010-05-16 17:27:03 +0200212 info->apertures = alloc_apertures(1);
213 if (!info->apertures) {
214 ret = -ENOMEM;
215 goto out_unpin;
216 }
217 info->apertures->ranges[0].base = dev->mode_config.fb_base;
Ben Widawsky93d18792013-01-17 12:45:17 -0800218 info->apertures->ranges[0].size = dev_priv->gtt.mappable_end;
Dave Airlie4410f392009-06-16 15:34:38 -0700219
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700220 info->fix.smem_start = dev->mode_config.fb_base + i915_gem_obj_ggtt_offset(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -0800221 info->fix.smem_len = size;
222
Daniel Vetterdd2757f2012-06-07 15:55:57 +0200223 info->screen_base =
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700224 ioremap_wc(dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj),
Daniel Vetterdd2757f2012-06-07 15:55:57 +0200225 size);
Jesse Barnes79e53942008-11-07 14:24:08 -0800226 if (!info->screen_base) {
227 ret = -ENOSPC;
Chris Wilsonb4476f52009-02-11 14:26:36 +0000228 goto out_unpin;
Jesse Barnes79e53942008-11-07 14:24:08 -0800229 }
230 info->screen_size = size;
231
Jesse Barnes24576d22013-03-26 09:25:45 -0700232 /* This driver doesn't need a VT switch to restore the mode on resume */
233 info->skip_vt_switch = true;
234
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200235 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
Dave Airlie38651672010-03-30 05:34:13 +0000236 drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height);
Jesse Barnes79e53942008-11-07 14:24:08 -0800237
Chris Wilson88afe712012-12-16 12:15:41 +0000238 /* If the object is shmemfs backed, it will have given us zeroed pages.
239 * If the object is stolen however, it will be full of whatever
240 * garbage was left in there.
241 */
Jesse Barnesd978ef12014-03-07 08:57:51 -0800242 if (ifbdev->fb->obj->stolen && !prealloc)
Chris Wilson88afe712012-12-16 12:15:41 +0000243 memset_io(info->screen_base, 0, info->screen_size);
244
Sascha Hauerfb2a99e2012-02-06 10:58:19 +0100245 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
Jesse Barnes79e53942008-11-07 14:24:08 -0800246
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700247 DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08lx, bo %p\n",
Dave Airlie8be48d92010-03-30 05:34:14 +0000248 fb->width, fb->height,
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700249 i915_gem_obj_ggtt_offset(obj), obj);
Jesse Barnes79e53942008-11-07 14:24:08 -0800250
251 mutex_unlock(&dev->struct_mutex);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000252 vga_switcheroo_client_fb_set(dev->pdev, info);
Jesse Barnes79e53942008-11-07 14:24:08 -0800253 return 0;
254
Chris Wilsonb4476f52009-02-11 14:26:36 +0000255out_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800256 i915_gem_object_ggtt_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +0000257 drm_gem_object_unreference(&obj->base);
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800258out_unlock:
Jesse Barnes79e53942008-11-07 14:24:08 -0800259 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -0800260 return ret;
261}
262
Paulo Zanoni67437682013-09-24 13:52:56 -0300263/** Sets the color ramps on behalf of RandR */
264static void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
265 u16 blue, int regno)
266{
267 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
268
269 intel_crtc->lut_r[regno] = red >> 8;
270 intel_crtc->lut_g[regno] = green >> 8;
271 intel_crtc->lut_b[regno] = blue >> 8;
272}
273
274static void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
275 u16 *blue, int regno)
276{
277 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
278
279 *red = intel_crtc->lut_r[regno] << 8;
280 *green = intel_crtc->lut_g[regno] << 8;
281 *blue = intel_crtc->lut_b[regno] << 8;
282}
283
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800284static struct drm_fb_helper_crtc *
285intel_fb_helper_crtc(struct drm_fb_helper *fb_helper, struct drm_crtc *crtc)
286{
287 int i;
288
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];
292
293 return NULL;
294}
295
296/*
297 * Try to read the BIOS display configuration and use it for the initial
298 * fb configuration.
299 *
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.
305 *
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
317 *
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.
322 */
323static 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)
327{
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800328 struct drm_device *dev = fb_helper->dev;
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800329 int i, j;
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800330 bool *save_enabled;
Daniel Vetter7c2bb532014-03-04 21:08:41 +0100331 bool fallback = true;
Daniel Vetter7e696e42014-03-04 21:08:42 +0100332 int num_connectors_enabled = 0;
333 int num_connectors_detected = 0;
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800334
Jesse Barnesef34ab82014-02-20 12:28:07 -0800335 /*
336 * If the user specified any force options, just bail here
337 * and use that config.
338 */
339 for (i = 0; i < fb_helper->connector_count; i++) {
340 struct drm_fb_helper_connector *fb_conn;
341 struct drm_connector *connector;
342
343 fb_conn = fb_helper->connector_info[i];
344 connector = fb_conn->connector;
345
346 if (!enabled[i])
347 continue;
348
349 if (connector->force != DRM_FORCE_UNSPECIFIED)
350 return false;
351 }
352
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800353 save_enabled = kcalloc(dev->mode_config.num_connector, sizeof(bool),
354 GFP_KERNEL);
355 if (!save_enabled)
356 return false;
357
358 memcpy(save_enabled, enabled, dev->mode_config.num_connector);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800359
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;
365
366 fb_conn = fb_helper->connector_info[i];
367 connector = fb_conn->connector;
Daniel Vetter7e696e42014-03-04 21:08:42 +0100368
369 if (connector->status == connector_status_connected)
370 num_connectors_detected++;
371
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800372 if (!enabled[i]) {
Chris Wilsonc0c97622014-05-13 13:45:09 +0100373 DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300374 connector->name);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800375 continue;
376 }
377
378 encoder = connector->encoder;
379 if (!encoder || WARN_ON(!encoder->crtc)) {
Chris Wilsonc0c97622014-05-13 13:45:09 +0100380 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300381 connector->name);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800382 enabled[i] = false;
383 continue;
384 }
385
Daniel Vetter7e696e42014-03-04 21:08:42 +0100386 num_connectors_enabled++;
387
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800388 new_crtc = intel_fb_helper_crtc(fb_helper, encoder->crtc);
389
390 /*
391 * Make sure we're not trying to drive multiple connectors
392 * with a single CRTC, since our cloning support may not
393 * match the BIOS.
394 */
395 for (j = 0; j < fb_helper->connector_count; j++) {
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800396 if (crtcs[j] == new_crtc) {
Daniel Vetter7e696e42014-03-04 21:08:42 +0100397 DRM_DEBUG_KMS("fallback: cloned configuration\n");
Daniel Vetter7c2bb532014-03-04 21:08:41 +0100398 fallback = true;
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800399 goto out;
400 }
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800401 }
402
Chris Wilsonc0c97622014-05-13 13:45:09 +0100403 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300404 connector->name);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800405
406 /* go for command line mode first */
407 modes[i] = drm_pick_cmdline_mode(fb_conn, width, height);
408
409 /* try for preferred next */
410 if (!modes[i]) {
Chris Wilsonc0c97622014-05-13 13:45:09 +0100411 DRM_DEBUG_KMS("looking for preferred mode on connector %s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300412 connector->name);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800413 modes[i] = drm_has_preferred_mode(fb_conn, width,
414 height);
415 }
416
Chris Wilsonafba0b52014-05-13 16:07:37 +0100417 /* 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",
Dave Airlie8d4ad9d2014-06-05 20:28:59 +1000420 connector->name);
Chris Wilsonafba0b52014-05-13 16:07:37 +0100421 modes[i] = list_first_entry(&connector->modes,
422 struct drm_display_mode,
423 head);
424 }
425
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800426 /* last resort: use current mode */
427 if (!modes[i]) {
428 /*
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
437 * something we own.
438 */
Chris Wilsonc0c97622014-05-13 13:45:09 +0100439 DRM_DEBUG_KMS("looking for current mode on connector %s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300440 connector->name);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800441 intel_mode_from_pipe_config(&encoder->crtc->hwmode,
442 &to_intel_crtc(encoder->crtc)->config);
443 modes[i] = &encoder->crtc->hwmode;
444 }
445 crtcs[i] = new_crtc;
446
Damien Lespiaufdaf66d2014-06-06 18:22:06 +0100447 DRM_DEBUG_KMS("connector %s on pipe %c [CRTC:%d]: %dx%d%s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300448 connector->name,
Chris Wilsonc0c97622014-05-13 13:45:09 +0100449 pipe_name(to_intel_crtc(encoder->crtc)->pipe),
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800450 encoder->crtc->base.id,
Chris Wilsonc0c97622014-05-13 13:45:09 +0100451 modes[i]->hdisplay, modes[i]->vdisplay,
452 modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" :"");
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800453
Daniel Vetter7c2bb532014-03-04 21:08:41 +0100454 fallback = false;
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800455 }
456
Daniel Vetter7e696e42014-03-04 21:08:42 +0100457 /*
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.
461 */
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);
467 fallback = true;
468 }
469
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800470out:
Daniel Vetter7c2bb532014-03-04 21:08:41 +0100471 if (fallback) {
Daniel Vetter7e696e42014-03-04 21:08:42 +0100472 DRM_DEBUG_KMS("Not using firmware configuration\n");
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800473 memcpy(enabled, save_enabled, dev->mode_config.num_connector);
474 kfree(save_enabled);
475 return false;
476 }
477
478 kfree(save_enabled);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800479 return true;
480}
481
Thierry Reding3a493872014-06-27 17:19:23 +0200482static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800483 .initial_config = intel_fb_initial_config,
Dave Airlie4abe3522010-03-30 05:34:18 +0000484 .gamma_set = intel_crtc_fb_gamma_set,
485 .gamma_get = intel_crtc_fb_gamma_get,
Daniel Vettercd5428a2013-01-21 23:42:49 +0100486 .fb_probe = intelfb_create,
Dave Airlie4abe3522010-03-30 05:34:18 +0000487};
488
Chris Wilson7b4f3992010-10-04 15:33:04 +0100489static void intel_fbdev_destroy(struct drm_device *dev,
490 struct intel_fbdev *ifbdev)
Jesse Barnes79e53942008-11-07 14:24:08 -0800491{
Dave Airlie8be48d92010-03-30 05:34:14 +0000492 if (ifbdev->helper.fbdev) {
Chris Wilsonddfe1562013-08-06 17:43:07 +0100493 struct fb_info *info = ifbdev->helper.fbdev;
494
Jesse Barnes79e53942008-11-07 14:24:08 -0800495 unregister_framebuffer(info);
496 iounmap(info->screen_base);
Dave Airlie4abe3522010-03-30 05:34:18 +0000497 if (info->cmap.len)
498 fb_dealloc_cmap(&info->cmap);
Chris Wilsonddfe1562013-08-06 17:43:07 +0100499
Jesse Barnes79e53942008-11-07 14:24:08 -0800500 framebuffer_release(info);
501 }
502
Dave Airlie4abe3522010-03-30 05:34:18 +0000503 drm_fb_helper_fini(&ifbdev->helper);
Jesse Barnes79e53942008-11-07 14:24:08 -0800504
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800505 drm_framebuffer_unregister_private(&ifbdev->fb->base);
Imre Deak9d661252014-02-12 00:01:10 +0200506 drm_framebuffer_remove(&ifbdev->fb->base);
Jesse Barnes79e53942008-11-07 14:24:08 -0800507}
Dave Airlie38651672010-03-30 05:34:13 +0000508
Jesse Barnesd978ef12014-03-07 08:57:51 -0800509/*
510 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
511 * The core display code will have read out the current plane configuration,
512 * so we use that to figure out if there's an object for us to use as the
513 * fb, and if so, we re-use it for the fbdev configuration.
514 *
515 * Note we only support a single fb shared across pipes for boot (mostly for
516 * fbcon), so we just find the biggest and use that.
517 */
518static bool intel_fbdev_init_bios(struct drm_device *dev,
519 struct intel_fbdev *ifbdev)
520{
521 struct intel_framebuffer *fb = NULL;
522 struct drm_crtc *crtc;
523 struct intel_crtc *intel_crtc;
524 struct intel_plane_config *plane_config = NULL;
525 unsigned int max_size = 0;
526
527 if (!i915.fastboot)
528 return false;
529
530 /* Find the largest fb */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100531 for_each_crtc(dev, crtc) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800532 intel_crtc = to_intel_crtc(crtc);
533
Dave Airlie66e514c2014-04-03 07:51:54 +1000534 if (!intel_crtc->active || !crtc->primary->fb) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800535 DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
536 pipe_name(intel_crtc->pipe));
537 continue;
538 }
539
540 if (intel_crtc->plane_config.size > max_size) {
541 DRM_DEBUG_KMS("found possible fb from plane %c\n",
542 pipe_name(intel_crtc->pipe));
543 plane_config = &intel_crtc->plane_config;
Dave Airlie66e514c2014-04-03 07:51:54 +1000544 fb = to_intel_framebuffer(crtc->primary->fb);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800545 max_size = plane_config->size;
546 }
547 }
548
549 if (!fb) {
550 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
551 goto out;
552 }
553
554 /* Now make sure all the pipes will fit into it */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100555 for_each_crtc(dev, crtc) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800556 unsigned int cur_size;
557
558 intel_crtc = to_intel_crtc(crtc);
559
560 if (!intel_crtc->active) {
561 DRM_DEBUG_KMS("pipe %c not active, skipping\n",
562 pipe_name(intel_crtc->pipe));
563 continue;
564 }
565
566 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
567 pipe_name(intel_crtc->pipe));
568
569 /*
570 * See if the plane fb we found above will fit on this
Chris Wilsonbc104d12014-03-20 15:11:21 +0000571 * pipe. Note we need to use the selected fb's pitch and bpp
572 * rather than the current pipe's, since they differ.
Jesse Barnesd978ef12014-03-07 08:57:51 -0800573 */
Chris Wilsonbc104d12014-03-20 15:11:21 +0000574 cur_size = intel_crtc->config.adjusted_mode.crtc_hdisplay;
575 cur_size = cur_size * fb->base.bits_per_pixel / 8;
576 if (fb->base.pitches[0] < cur_size) {
577 DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
578 pipe_name(intel_crtc->pipe),
579 cur_size, fb->base.pitches[0]);
580 plane_config = NULL;
581 fb = NULL;
582 break;
583 }
584
585 cur_size = intel_crtc->config.adjusted_mode.crtc_vdisplay;
586 cur_size = ALIGN(cur_size, plane_config->tiled ? (IS_GEN2(dev) ? 16 : 8) : 1);
587 cur_size *= fb->base.pitches[0];
588 DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
589 pipe_name(intel_crtc->pipe),
590 intel_crtc->config.adjusted_mode.crtc_hdisplay,
591 intel_crtc->config.adjusted_mode.crtc_vdisplay,
592 fb->base.bits_per_pixel,
Jesse Barnesd978ef12014-03-07 08:57:51 -0800593 cur_size);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800594
595 if (cur_size > max_size) {
596 DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
597 pipe_name(intel_crtc->pipe),
598 cur_size, max_size);
599 plane_config = NULL;
600 fb = NULL;
601 break;
602 }
603
604 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
605 pipe_name(intel_crtc->pipe),
606 max_size, cur_size);
607 }
608
Jesse Barnesd978ef12014-03-07 08:57:51 -0800609 if (!fb) {
610 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
611 goto out;
612 }
613
Jesse Barnes484b41d2014-03-07 08:57:55 -0800614 ifbdev->preferred_bpp = fb->base.bits_per_pixel;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800615 ifbdev->fb = fb;
616
Jesse Barnes484b41d2014-03-07 08:57:55 -0800617 drm_framebuffer_reference(&ifbdev->fb->base);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800618
619 /* Final pass to check if any active pipes don't have fbs */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100620 for_each_crtc(dev, crtc) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800621 intel_crtc = to_intel_crtc(crtc);
622
623 if (!intel_crtc->active)
624 continue;
625
Dave Airlie66e514c2014-04-03 07:51:54 +1000626 WARN(!crtc->primary->fb,
Jesse Barnesd978ef12014-03-07 08:57:51 -0800627 "re-used BIOS config but lost an fb on crtc %d\n",
628 crtc->base.id);
629 }
630
631
632 DRM_DEBUG_KMS("using BIOS fb for initial console\n");
633 return true;
634
Jesse Barnesd978ef12014-03-07 08:57:51 -0800635out:
636
637 return false;
638}
639
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100640static void intel_fbdev_suspend_worker(struct work_struct *work)
641{
642 intel_fbdev_set_suspend(container_of(work,
643 struct drm_i915_private,
644 fbdev_suspend_work)->dev,
645 FBINFO_STATE_RUNNING,
646 true);
647}
648
Dave Airlie38651672010-03-30 05:34:13 +0000649int intel_fbdev_init(struct drm_device *dev)
650{
Dave Airlie8be48d92010-03-30 05:34:14 +0000651 struct intel_fbdev *ifbdev;
Ville Syrjäläb51b32c2013-05-31 20:07:06 +0300652 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson5a793952010-06-06 10:50:03 +0100653 int ret;
Dave Airlie8be48d92010-03-30 05:34:14 +0000654
Jesse Barnesd978ef12014-03-07 08:57:51 -0800655 if (WARN_ON(INTEL_INFO(dev)->num_pipes == 0))
656 return -ENODEV;
657
658 ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
659 if (ifbdev == NULL)
Dave Airlie8be48d92010-03-30 05:34:14 +0000660 return -ENOMEM;
661
Thierry Reding10a23102014-06-27 17:19:24 +0200662 drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
663
Jesse Barnesd978ef12014-03-07 08:57:51 -0800664 if (!intel_fbdev_init_bios(dev, ifbdev))
665 ifbdev->preferred_bpp = 32;
Dave Airlie8be48d92010-03-30 05:34:14 +0000666
Chris Wilson5a793952010-06-06 10:50:03 +0100667 ret = drm_fb_helper_init(dev, &ifbdev->helper,
Jesse Barnesd978ef12014-03-07 08:57:51 -0800668 INTEL_INFO(dev)->num_pipes, 4);
Chris Wilson5a793952010-06-06 10:50:03 +0100669 if (ret) {
670 kfree(ifbdev);
671 return ret;
672 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000673
Jesse Barnesd978ef12014-03-07 08:57:51 -0800674 dev_priv->fbdev = ifbdev;
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100675 INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
676
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000677 drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
Daniel Vetter20afbda2012-12-11 14:05:07 +0100678
Dave Airlie38651672010-03-30 05:34:13 +0000679 return 0;
680}
681
Daniel Vetter20afbda2012-12-11 14:05:07 +0100682void intel_fbdev_initial_config(struct drm_device *dev)
683{
Ville Syrjäläb51b32c2013-05-31 20:07:06 +0300684 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800685 struct intel_fbdev *ifbdev = dev_priv->fbdev;
Daniel Vetter20afbda2012-12-11 14:05:07 +0100686
687 /* Due to peculiar init order wrt to hpd handling this is separate. */
Jesse Barnesd978ef12014-03-07 08:57:51 -0800688 drm_fb_helper_initial_config(&ifbdev->helper, ifbdev->preferred_bpp);
Daniel Vetter20afbda2012-12-11 14:05:07 +0100689}
690
Dave Airlie38651672010-03-30 05:34:13 +0000691void intel_fbdev_fini(struct drm_device *dev)
692{
Ville Syrjäläb51b32c2013-05-31 20:07:06 +0300693 struct drm_i915_private *dev_priv = dev->dev_private;
Dave Airlie8be48d92010-03-30 05:34:14 +0000694 if (!dev_priv->fbdev)
695 return;
696
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100697 flush_work(&dev_priv->fbdev_suspend_work);
698
Dave Airlie38651672010-03-30 05:34:13 +0000699 intel_fbdev_destroy(dev, dev_priv->fbdev);
Dave Airlie8be48d92010-03-30 05:34:14 +0000700 kfree(dev_priv->fbdev);
Dave Airlie38651672010-03-30 05:34:13 +0000701 dev_priv->fbdev = NULL;
702}
Dave Airlie3fa016a2012-03-28 10:48:49 +0100703
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100704void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
Dave Airlie3fa016a2012-03-28 10:48:49 +0100705{
Ville Syrjäläb51b32c2013-05-31 20:07:06 +0300706 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula1ffc5282013-05-07 18:54:05 +0300707 struct intel_fbdev *ifbdev = dev_priv->fbdev;
708 struct fb_info *info;
709
710 if (!ifbdev)
Dave Airlie3fa016a2012-03-28 10:48:49 +0100711 return;
712
Jani Nikula1ffc5282013-05-07 18:54:05 +0300713 info = ifbdev->helper.fbdev;
714
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100715 if (synchronous) {
716 /* Flush any pending work to turn the console on, and then
717 * wait to turn it off. It must be synchronous as we are
718 * about to suspend or unload the driver.
719 *
720 * Note that from within the work-handler, we cannot flush
721 * ourselves, so only flush outstanding work upon suspend!
722 */
723 if (state != FBINFO_STATE_RUNNING)
724 flush_work(&dev_priv->fbdev_suspend_work);
725 console_lock();
726 } else {
727 /*
728 * The console lock can be pretty contented on resume due
729 * to all the printk activity. Try to keep it out of the hot
730 * path of resume if possible.
731 */
732 WARN_ON(state != FBINFO_STATE_RUNNING);
733 if (!console_trylock()) {
734 /* Don't block our own workqueue as this can
735 * be run in parallel with other i915.ko tasks.
736 */
737 schedule_work(&dev_priv->fbdev_suspend_work);
738 return;
739 }
740 }
741
Jani Nikula1ffc5282013-05-07 18:54:05 +0300742 /* On resume from hibernation: If the object is shmemfs backed, it has
743 * been restored from swap. If the object is stolen however, it will be
744 * full of whatever garbage was left in there.
745 */
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800746 if (state == FBINFO_STATE_RUNNING && ifbdev->fb->obj->stolen)
Jani Nikula1ffc5282013-05-07 18:54:05 +0300747 memset_io(info->screen_base, 0, info->screen_size);
748
749 fb_set_suspend(info, state);
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100750 console_unlock();
Dave Airlie3fa016a2012-03-28 10:48:49 +0100751}
752
Daniel Vetter0632fef2013-10-08 17:44:49 +0200753void intel_fbdev_output_poll_changed(struct drm_device *dev)
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000754{
Ville Syrjäläb51b32c2013-05-31 20:07:06 +0300755 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800756 if (dev_priv->fbdev)
757 drm_fb_helper_hotplug_event(&dev_priv->fbdev->helper);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000758}
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100759
Daniel Vetter0632fef2013-10-08 17:44:49 +0200760void intel_fbdev_restore_mode(struct drm_device *dev)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100761{
762 int ret;
Ville Syrjäläb51b32c2013-05-31 20:07:06 +0300763 struct drm_i915_private *dev_priv = dev->dev_private;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100764
Jesse Barnesd978ef12014-03-07 08:57:51 -0800765 if (!dev_priv->fbdev)
Ben Widawskye3c74752013-04-05 13:12:39 -0700766 return;
767
Rob Clark5ea1f752014-05-30 12:29:48 -0400768 ret = drm_fb_helper_restore_fbdev_mode_unlocked(&dev_priv->fbdev->helper);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100769 if (ret)
770 DRM_DEBUG("failed to restore crtc mode\n");
771}