blob: 4003e4908c098fda76421277d5f1750b15864001 [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
Jesse Barnesd1d70672014-05-28 14:39:03 -070027#include <linux/async.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080028#include <linux/module.h>
29#include <linux/kernel.h>
Chris Wilson82e3b8c2014-08-13 13:09:46 +010030#include <linux/console.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080031#include <linux/errno.h>
32#include <linux/string.h>
33#include <linux/mm.h>
34#include <linux/tty.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080035#include <linux/sysrq.h>
36#include <linux/delay.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080037#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"
Chris Wilson5d723d72016-08-04 16:32:35 +010044#include "intel_frontbuffer.h"
David Howells760285e2012-10-02 18:01:07 +010045#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080046#include "i915_drv.h"
47
Daniel Vettere9910772014-06-18 15:05:19 +020048static int intel_fbdev_set_par(struct fb_info *info)
49{
50 struct drm_fb_helper *fb_helper = info->par;
51 struct intel_fbdev *ifbdev =
52 container_of(fb_helper, struct intel_fbdev, helper);
53 int ret;
54
55 ret = drm_fb_helper_set_par(info);
56
57 if (ret == 0) {
Daniel Vettere9910772014-06-18 15:05:19 +020058 mutex_lock(&fb_helper->dev->struct_mutex);
Rodrigo Viviaba6da32015-07-09 09:56:41 -070059 intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT);
Daniel Vettere9910772014-06-18 15:05:19 +020060 mutex_unlock(&fb_helper->dev->struct_mutex);
61 }
62
63 return ret;
64}
65
Rodrigo Vivi03e515f2015-03-09 17:57:07 -070066static int intel_fbdev_blank(int blank, struct fb_info *info)
67{
68 struct drm_fb_helper *fb_helper = info->par;
69 struct intel_fbdev *ifbdev =
70 container_of(fb_helper, struct intel_fbdev, helper);
71 int ret;
72
73 ret = drm_fb_helper_blank(blank, info);
74
75 if (ret == 0) {
Rodrigo Vivi03e515f2015-03-09 17:57:07 -070076 mutex_lock(&fb_helper->dev->struct_mutex);
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -070077 intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT);
Rodrigo Vivi03e515f2015-03-09 17:57:07 -070078 mutex_unlock(&fb_helper->dev->struct_mutex);
79 }
80
81 return ret;
82}
83
Rodrigo Vivid9a946b2015-05-28 10:26:58 -070084static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
85 struct fb_info *info)
86{
87 struct drm_fb_helper *fb_helper = info->par;
88 struct intel_fbdev *ifbdev =
89 container_of(fb_helper, struct intel_fbdev, helper);
90
91 int ret;
92 ret = drm_fb_helper_pan_display(var, info);
93
94 if (ret == 0) {
Rodrigo Vivid9a946b2015-05-28 10:26:58 -070095 mutex_lock(&fb_helper->dev->struct_mutex);
Rodrigo Vivi77a0d1c2015-06-18 11:43:24 -070096 intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT);
Rodrigo Vivid9a946b2015-05-28 10:26:58 -070097 mutex_unlock(&fb_helper->dev->struct_mutex);
98 }
99
100 return ret;
101}
102
Jesse Barnes79e53942008-11-07 14:24:08 -0800103static struct fb_ops intelfb_ops = {
104 .owner = THIS_MODULE,
Dave Airlie785b93e2009-08-28 15:46:53 +1000105 .fb_check_var = drm_fb_helper_check_var,
Daniel Vettere9910772014-06-18 15:05:19 +0200106 .fb_set_par = intel_fbdev_set_par,
Archit Taneja21cff142015-07-31 16:21:56 +0530107 .fb_fillrect = drm_fb_helper_cfb_fillrect,
108 .fb_copyarea = drm_fb_helper_cfb_copyarea,
109 .fb_imageblit = drm_fb_helper_cfb_imageblit,
Rodrigo Vivid9a946b2015-05-28 10:26:58 -0700110 .fb_pan_display = intel_fbdev_pan_display,
Rodrigo Vivi03e515f2015-03-09 17:57:07 -0700111 .fb_blank = intel_fbdev_blank,
Dave Airlie068143d2009-10-05 09:58:02 +1000112 .fb_setcmap = drm_fb_helper_setcmap,
Jesse Barnes81255562010-08-02 12:07:50 -0700113 .fb_debug_enter = drm_fb_helper_debug_enter,
114 .fb_debug_leave = drm_fb_helper_debug_leave,
Jesse Barnes79e53942008-11-07 14:24:08 -0800115};
116
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800117static int intelfb_alloc(struct drm_fb_helper *helper,
118 struct drm_fb_helper_surface_size *sizes)
Jesse Barnes79e53942008-11-07 14:24:08 -0800119{
Ville Syrjälä34308242013-05-31 20:07:05 +0300120 struct intel_fbdev *ifbdev =
121 container_of(helper, struct intel_fbdev, helper);
Lukas Wunner4601b932015-12-19 15:40:39 +0100122 struct drm_framebuffer *fb;
Ville Syrjälä34308242013-05-31 20:07:05 +0300123 struct drm_device *dev = helper->dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -0300124 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300125 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ville Syrjälä3a7f2f62012-05-24 21:08:56 +0300126 struct drm_mode_fb_cmd2 mode_cmd = {};
Paulo Zanoni3badb492015-09-23 12:52:23 -0300127 struct drm_i915_gem_object *obj = NULL;
James Simmons57084d02010-12-20 19:10:39 +0000128 int size, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -0800129
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000130 /* we don't do packed 24bpp */
Dave Airlie38651672010-03-30 05:34:13 +0000131 if (sizes->surface_bpp == 24)
132 sizes->surface_bpp = 32;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000133
Dave Airlie38651672010-03-30 05:34:13 +0000134 mode_cmd.width = sizes->surface_width;
135 mode_cmd.height = sizes->surface_height;
Jesse Barnes79e53942008-11-07 14:24:08 -0800136
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300137 mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
138 DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800139 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
140 sizes->surface_depth);
Jesse Barnes79e53942008-11-07 14:24:08 -0800141
Tvrtko Ursulin51f13852015-06-30 10:06:27 +0100142 mutex_lock(&dev->struct_mutex);
143
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800144 size = mode_cmd.pitches[0] * mode_cmd.height;
Fabian Frederick1267a262014-07-01 20:39:41 +0200145 size = PAGE_ALIGN(size);
Paulo Zanoni3badb492015-09-23 12:52:23 -0300146
147 /* If the FB is too big, just don't use it since fbdev is not very
148 * important and we should probably use that space with FBC or other
149 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300150 if (size * 2 < ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -0300151 obj = i915_gem_object_create_stolen(dev, size);
Chris Wilson0ffb0ff2012-11-15 11:32:27 +0000152 if (obj == NULL)
Dave Gordond37cd8a2016-04-22 19:14:32 +0100153 obj = i915_gem_object_create(dev, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100154 if (IS_ERR(obj)) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700155 DRM_ERROR("failed to allocate framebuffer\n");
Chris Wilsonfe3db792016-04-25 13:32:13 +0100156 ret = PTR_ERR(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -0800157 goto out;
158 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800159
Daniel Vettera8bb6812014-02-10 18:00:39 +0100160 fb = __intel_framebuffer_create(dev, &mode_cmd, obj);
161 if (IS_ERR(fb)) {
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100162 i915_gem_object_put(obj);
Daniel Vettera8bb6812014-02-10 18:00:39 +0100163 ret = PTR_ERR(fb);
Lukas Wunnerca40ba852015-10-22 13:37:18 +0200164 goto out;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +0000165 }
166
Tvrtko Ursulin51f13852015-06-30 10:06:27 +0100167 mutex_unlock(&dev->struct_mutex);
168
Daniel Vettera8bb6812014-02-10 18:00:39 +0100169 ifbdev->fb = to_intel_framebuffer(fb);
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800170
171 return 0;
172
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800173out:
Tvrtko Ursulin51f13852015-06-30 10:06:27 +0100174 mutex_unlock(&dev->struct_mutex);
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800175 return ret;
176}
177
178static int intelfb_create(struct drm_fb_helper *helper,
179 struct drm_fb_helper_surface_size *sizes)
180{
181 struct intel_fbdev *ifbdev =
182 container_of(helper, struct intel_fbdev, helper);
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800183 struct intel_framebuffer *intel_fb = ifbdev->fb;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800184 struct drm_device *dev = helper->dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300185 struct drm_i915_private *dev_priv = to_i915(dev);
186 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800187 struct fb_info *info;
188 struct drm_framebuffer *fb;
Chris Wilson8ef85612016-04-28 09:56:39 +0100189 struct i915_vma *vma;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800190 bool prealloc = false;
Chris Wilson406ea8d2016-07-20 13:31:55 +0100191 void __iomem *vaddr;
Chris Wilson8ef85612016-04-28 09:56:39 +0100192 int ret;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800193
Chris Wilsonedd586f2014-04-23 08:54:31 +0100194 if (intel_fb &&
195 (sizes->fb_width > intel_fb->base.width ||
196 sizes->fb_height > intel_fb->base.height)) {
197 DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d),"
198 " releasing it\n",
199 intel_fb->base.width, intel_fb->base.height,
200 sizes->fb_width, sizes->fb_height);
201 drm_framebuffer_unreference(&intel_fb->base);
202 intel_fb = ifbdev->fb = NULL;
203 }
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800204 if (!intel_fb || WARN_ON(!intel_fb->obj)) {
Jesse Barnes48e92122013-11-26 11:25:54 -0800205 DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800206 ret = intelfb_alloc(helper, sizes);
207 if (ret)
Tvrtko Ursulin51f13852015-06-30 10:06:27 +0100208 return ret;
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800209 intel_fb = ifbdev->fb;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800210 } else {
Jesse Barnes48e92122013-11-26 11:25:54 -0800211 DRM_DEBUG_KMS("re-using BIOS fb\n");
Jesse Barnesd978ef12014-03-07 08:57:51 -0800212 prealloc = true;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800213 sizes->fb_width = intel_fb->base.width;
214 sizes->fb_height = intel_fb->base.height;
215 }
216
Tvrtko Ursulin51f13852015-06-30 10:06:27 +0100217 mutex_lock(&dev->struct_mutex);
218
Chris Wilson0c823122015-12-04 16:05:26 +0000219 /* Pin the GGTT vma for our access via info->screen_base.
220 * This also validates that any existing fb inherited from the
221 * BIOS is suitable for own access.
222 */
Chris Wilson058d88c2016-08-15 10:49:06 +0100223 vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base, DRM_ROTATE_0);
224 if (IS_ERR(vma)) {
225 ret = PTR_ERR(vma);
Chris Wilson0c823122015-12-04 16:05:26 +0000226 goto out_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +0100227 }
Chris Wilson0c823122015-12-04 16:05:26 +0000228
Archit Taneja21cff142015-07-31 16:21:56 +0530229 info = drm_fb_helper_alloc_fbi(helper);
230 if (IS_ERR(info)) {
Lukas Wunner366e39b2015-11-18 16:29:51 +0100231 DRM_ERROR("Failed to allocate fb_info\n");
Archit Taneja21cff142015-07-31 16:21:56 +0530232 ret = PTR_ERR(info);
Chris Wilsonb4476f52009-02-11 14:26:36 +0000233 goto out_unpin;
Jesse Barnes79e53942008-11-07 14:24:08 -0800234 }
235
Ville Syrjälä34308242013-05-31 20:07:05 +0300236 info->par = helper;
Jesse Barnes79e53942008-11-07 14:24:08 -0800237
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800238 fb = &ifbdev->fb->base;
Dave Airlie38651672010-03-30 05:34:13 +0000239
240 ifbdev->helper.fb = fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000241
Jesse Barnes79e53942008-11-07 14:24:08 -0800242 strcpy(info->fix.id, "inteldrmfb");
Jesse Barnes79e53942008-11-07 14:24:08 -0800243
Jesse Barnes8fd4bd22010-06-23 12:56:12 -0700244 info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
Jesse Barnes79e53942008-11-07 14:24:08 -0800245 info->fbops = &intelfb_ops;
246
Dave Airlie4410f392009-06-16 15:34:38 -0700247 /* setup aperture base/size for vesafb takeover */
Marcin Slusarz1471ca92010-05-16 17:27:03 +0200248 info->apertures->ranges[0].base = dev->mode_config.fb_base;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300249 info->apertures->ranges[0].size = ggtt->mappable_end;
Dave Airlie4410f392009-06-16 15:34:38 -0700250
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100251 info->fix.smem_start = dev->mode_config.fb_base + i915_ggtt_offset(vma);
Chris Wilson8ef85612016-04-28 09:56:39 +0100252 info->fix.smem_len = vma->node.size;
Jesse Barnes79e53942008-11-07 14:24:08 -0800253
Chris Wilson8ef85612016-04-28 09:56:39 +0100254 vaddr = i915_vma_pin_iomap(vma);
255 if (IS_ERR(vaddr)) {
Lukas Wunner366e39b2015-11-18 16:29:51 +0100256 DRM_ERROR("Failed to remap framebuffer into virtual memory\n");
Chris Wilson8ef85612016-04-28 09:56:39 +0100257 ret = PTR_ERR(vaddr);
Archit Taneja21cff142015-07-31 16:21:56 +0530258 goto out_destroy_fbi;
Jesse Barnes79e53942008-11-07 14:24:08 -0800259 }
Chris Wilson8ef85612016-04-28 09:56:39 +0100260 info->screen_base = vaddr;
261 info->screen_size = vma->node.size;
Jesse Barnes79e53942008-11-07 14:24:08 -0800262
Jesse Barnes24576d22013-03-26 09:25:45 -0700263 /* This driver doesn't need a VT switch to restore the mode on resume */
264 info->skip_vt_switch = true;
265
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200266 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
Dave Airlie38651672010-03-30 05:34:13 +0000267 drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height);
Jesse Barnes79e53942008-11-07 14:24:08 -0800268
Chris Wilson88afe712012-12-16 12:15:41 +0000269 /* If the object is shmemfs backed, it will have given us zeroed pages.
270 * If the object is stolen however, it will be full of whatever
271 * garbage was left in there.
272 */
Chris Wilson058d88c2016-08-15 10:49:06 +0100273 if (intel_fb->obj->stolen && !prealloc)
Chris Wilson88afe712012-12-16 12:15:41 +0000274 memset_io(info->screen_base, 0, info->screen_size);
275
Sascha Hauerfb2a99e2012-02-06 10:58:19 +0100276 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
Jesse Barnes79e53942008-11-07 14:24:08 -0800277
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100278 DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08x\n",
279 fb->width, fb->height, i915_ggtt_offset(vma));
Chris Wilson058d88c2016-08-15 10:49:06 +0100280 ifbdev->vma = vma;
Jesse Barnes79e53942008-11-07 14:24:08 -0800281
282 mutex_unlock(&dev->struct_mutex);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000283 vga_switcheroo_client_fb_set(dev->pdev, info);
Jesse Barnes79e53942008-11-07 14:24:08 -0800284 return 0;
285
Archit Taneja21cff142015-07-31 16:21:56 +0530286out_destroy_fbi:
287 drm_fb_helper_release_fbi(helper);
Chris Wilsonb4476f52009-02-11 14:26:36 +0000288out_unpin:
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300289 intel_unpin_fb_obj(&ifbdev->fb->base, DRM_ROTATE_0);
Chris Wilson0c823122015-12-04 16:05:26 +0000290out_unlock:
Jesse Barnes79e53942008-11-07 14:24:08 -0800291 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -0800292 return ret;
293}
294
Paulo Zanoni67437682013-09-24 13:52:56 -0300295/** Sets the color ramps on behalf of RandR */
296static void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
297 u16 blue, int regno)
298{
299 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
300
301 intel_crtc->lut_r[regno] = red >> 8;
302 intel_crtc->lut_g[regno] = green >> 8;
303 intel_crtc->lut_b[regno] = blue >> 8;
304}
305
306static void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
307 u16 *blue, int regno)
308{
309 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
310
311 *red = intel_crtc->lut_r[regno] << 8;
312 *green = intel_crtc->lut_g[regno] << 8;
313 *blue = intel_crtc->lut_b[regno] << 8;
314}
315
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800316static struct drm_fb_helper_crtc *
317intel_fb_helper_crtc(struct drm_fb_helper *fb_helper, struct drm_crtc *crtc)
318{
319 int i;
320
321 for (i = 0; i < fb_helper->crtc_count; i++)
322 if (fb_helper->crtc_info[i].mode_set.crtc == crtc)
323 return &fb_helper->crtc_info[i];
324
325 return NULL;
326}
327
328/*
329 * Try to read the BIOS display configuration and use it for the initial
330 * fb configuration.
331 *
332 * The BIOS or boot loader will generally create an initial display
333 * configuration for us that includes some set of active pipes and displays.
334 * This routine tries to figure out which pipes and connectors are active
335 * and stuffs them into the crtcs and modes array given to us by the
336 * drm_fb_helper code.
337 *
338 * The overall sequence is:
339 * intel_fbdev_init - from driver load
340 * intel_fbdev_init_bios - initialize the intel_fbdev using BIOS data
341 * drm_fb_helper_init - build fb helper structs
342 * drm_fb_helper_single_add_all_connectors - more fb helper structs
343 * intel_fbdev_initial_config - apply the config
344 * drm_fb_helper_initial_config - call ->probe then register_framebuffer()
345 * drm_setup_crtcs - build crtc config for fbdev
346 * intel_fb_initial_config - find active connectors etc
347 * drm_fb_helper_single_fb_probe - set up fbdev
348 * intelfb_create - re-use or alloc fb, build out fbdev structs
349 *
350 * Note that we don't make special consideration whether we could actually
351 * switch to the selected modes without a full modeset. E.g. when the display
352 * is in VGA mode we need to recalculate watermarks and set a new high-res
353 * framebuffer anyway.
354 */
355static bool intel_fb_initial_config(struct drm_fb_helper *fb_helper,
356 struct drm_fb_helper_crtc **crtcs,
357 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000358 struct drm_fb_offset *offsets,
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800359 bool *enabled, int width, int height)
360{
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800361 struct drm_device *dev = fb_helper->dev;
Chris Wilson86f40bb2016-07-02 15:36:02 +0100362 unsigned long conn_configured, mask;
363 unsigned int count = min(fb_helper->connector_count, BITS_PER_LONG);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800364 int i, j;
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800365 bool *save_enabled;
Daniel Vetter7c2bb532014-03-04 21:08:41 +0100366 bool fallback = true;
Daniel Vetter7e696e42014-03-04 21:08:42 +0100367 int num_connectors_enabled = 0;
368 int num_connectors_detected = 0;
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000369 int pass = 0;
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800370
Chris Wilson86f40bb2016-07-02 15:36:02 +0100371 save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL);
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800372 if (!save_enabled)
373 return false;
374
Chris Wilson86f40bb2016-07-02 15:36:02 +0100375 memcpy(save_enabled, enabled, count);
376 mask = BIT(count) - 1;
377 conn_configured = 0;
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000378retry:
Chris Wilson86f40bb2016-07-02 15:36:02 +0100379 for (i = 0; i < count; i++) {
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800380 struct drm_fb_helper_connector *fb_conn;
381 struct drm_connector *connector;
382 struct drm_encoder *encoder;
383 struct drm_fb_helper_crtc *new_crtc;
Lionel Landwerlin82cf4352016-03-16 10:57:16 +0000384 struct intel_crtc *intel_crtc;
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800385
386 fb_conn = fb_helper->connector_info[i];
387 connector = fb_conn->connector;
Daniel Vetter7e696e42014-03-04 21:08:42 +0100388
Chris Wilson86f40bb2016-07-02 15:36:02 +0100389 if (conn_configured & BIT(i))
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000390 continue;
391
392 if (pass == 0 && !connector->has_tile)
393 continue;
394
Daniel Vetter7e696e42014-03-04 21:08:42 +0100395 if (connector->status == connector_status_connected)
396 num_connectors_detected++;
397
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800398 if (!enabled[i]) {
Chris Wilsonc0c97622014-05-13 13:45:09 +0100399 DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300400 connector->name);
Chris Wilson86f40bb2016-07-02 15:36:02 +0100401 conn_configured |= BIT(i);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800402 continue;
403 }
404
Chris Wilsond84a0f32014-08-18 10:35:29 -0700405 if (connector->force == DRM_FORCE_OFF) {
406 DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
407 connector->name);
408 enabled[i] = false;
409 continue;
410 }
411
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100412 encoder = connector->state->best_encoder;
413 if (!encoder || WARN_ON(!connector->state->crtc)) {
Chris Wilsond84a0f32014-08-18 10:35:29 -0700414 if (connector->force > DRM_FORCE_OFF)
415 goto bail;
416
Chris Wilsonc0c97622014-05-13 13:45:09 +0100417 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300418 connector->name);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800419 enabled[i] = false;
Chris Wilson86f40bb2016-07-02 15:36:02 +0100420 conn_configured |= BIT(i);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800421 continue;
422 }
423
Daniel Vetter7e696e42014-03-04 21:08:42 +0100424 num_connectors_enabled++;
425
Lionel Landwerlin82cf4352016-03-16 10:57:16 +0000426 intel_crtc = to_intel_crtc(connector->state->crtc);
427 for (j = 0; j < 256; j++) {
428 intel_crtc->lut_r[j] = j;
429 intel_crtc->lut_g[j] = j;
430 intel_crtc->lut_b[j] = j;
431 }
432
Chris Wilson86f40bb2016-07-02 15:36:02 +0100433 new_crtc = intel_fb_helper_crtc(fb_helper,
434 connector->state->crtc);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800435
436 /*
437 * Make sure we're not trying to drive multiple connectors
438 * with a single CRTC, since our cloning support may not
439 * match the BIOS.
440 */
Chris Wilson86f40bb2016-07-02 15:36:02 +0100441 for (j = 0; j < count; j++) {
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800442 if (crtcs[j] == new_crtc) {
Daniel Vetter7e696e42014-03-04 21:08:42 +0100443 DRM_DEBUG_KMS("fallback: cloned configuration\n");
Chris Wilsond84a0f32014-08-18 10:35:29 -0700444 goto bail;
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800445 }
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800446 }
447
Chris Wilsonc0c97622014-05-13 13:45:09 +0100448 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300449 connector->name);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800450
451 /* go for command line mode first */
452 modes[i] = drm_pick_cmdline_mode(fb_conn, width, height);
453
454 /* try for preferred next */
455 if (!modes[i]) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000456 DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
457 connector->name, connector->has_tile);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800458 modes[i] = drm_has_preferred_mode(fb_conn, width,
459 height);
460 }
461
Chris Wilsonafba0b52014-05-13 16:07:37 +0100462 /* No preferred mode marked by the EDID? Are there any modes? */
463 if (!modes[i] && !list_empty(&connector->modes)) {
464 DRM_DEBUG_KMS("using first mode listed on connector %s\n",
Dave Airlie8d4ad9d2014-06-05 20:28:59 +1000465 connector->name);
Chris Wilsonafba0b52014-05-13 16:07:37 +0100466 modes[i] = list_first_entry(&connector->modes,
467 struct drm_display_mode,
468 head);
469 }
470
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800471 /* last resort: use current mode */
472 if (!modes[i]) {
473 /*
474 * IMPORTANT: We want to use the adjusted mode (i.e.
475 * after the panel fitter upscaling) as the initial
476 * config, not the input mode, which is what crtc->mode
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +0200477 * usually contains. But since our current
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800478 * code puts a mode derived from the post-pfit timings
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +0200479 * into crtc->mode this works out correctly.
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100480 *
481 * This is crtc->mode and not crtc->state->mode for the
482 * fastboot check to work correctly. crtc_state->mode has
483 * I915_MODE_FLAG_INHERITED, which we clear to force check
484 * state.
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800485 */
Chris Wilsonc0c97622014-05-13 13:45:09 +0100486 DRM_DEBUG_KMS("looking for current mode on connector %s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300487 connector->name);
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100488 modes[i] = &connector->state->crtc->mode;
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800489 }
490 crtcs[i] = new_crtc;
491
Ville Syrjälä78108b72016-05-27 20:59:19 +0300492 DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300493 connector->name,
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100494 connector->state->crtc->base.id,
Ville Syrjälä78108b72016-05-27 20:59:19 +0300495 connector->state->crtc->name,
Chris Wilsonc0c97622014-05-13 13:45:09 +0100496 modes[i]->hdisplay, modes[i]->vdisplay,
497 modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" :"");
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800498
Daniel Vetter7c2bb532014-03-04 21:08:41 +0100499 fallback = false;
Chris Wilson86f40bb2016-07-02 15:36:02 +0100500 conn_configured |= BIT(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000501 }
502
503 if ((conn_configured & mask) != mask) {
504 pass++;
505 goto retry;
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800506 }
507
Daniel Vetter7e696e42014-03-04 21:08:42 +0100508 /*
509 * If the BIOS didn't enable everything it could, fall back to have the
510 * same user experiencing of lighting up as much as possible like the
511 * fbdev helper library.
512 */
513 if (num_connectors_enabled != num_connectors_detected &&
514 num_connectors_enabled < INTEL_INFO(dev)->num_pipes) {
515 DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
516 DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
517 num_connectors_detected);
518 fallback = true;
519 }
520
Daniel Vetter7c2bb532014-03-04 21:08:41 +0100521 if (fallback) {
Chris Wilsond84a0f32014-08-18 10:35:29 -0700522bail:
Daniel Vetter7e696e42014-03-04 21:08:42 +0100523 DRM_DEBUG_KMS("Not using firmware configuration\n");
Chris Wilson86f40bb2016-07-02 15:36:02 +0100524 memcpy(enabled, save_enabled, count);
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800525 kfree(save_enabled);
526 return false;
527 }
528
529 kfree(save_enabled);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800530 return true;
531}
532
Thierry Reding3a493872014-06-27 17:19:23 +0200533static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800534 .initial_config = intel_fb_initial_config,
Dave Airlie4abe3522010-03-30 05:34:18 +0000535 .gamma_set = intel_crtc_fb_gamma_set,
536 .gamma_get = intel_crtc_fb_gamma_get,
Daniel Vettercd5428a2013-01-21 23:42:49 +0100537 .fb_probe = intelfb_create,
Dave Airlie4abe3522010-03-30 05:34:18 +0000538};
539
Chris Wilson43cee312016-06-21 09:16:54 +0100540static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
Jesse Barnes79e53942008-11-07 14:24:08 -0800541{
Chris Wilson0c823122015-12-04 16:05:26 +0000542 /* We rely on the object-free to release the VMA pinning for
543 * the info->screen_base mmaping. Leaking the VMA is simpler than
544 * trying to rectify all the possible error paths leading here.
545 */
Chris Wilsonddfe1562013-08-06 17:43:07 +0100546
Archit Taneja21cff142015-07-31 16:21:56 +0530547 drm_fb_helper_unregister_fbi(&ifbdev->helper);
548 drm_fb_helper_release_fbi(&ifbdev->helper);
Jesse Barnes79e53942008-11-07 14:24:08 -0800549
Dave Airlie4abe3522010-03-30 05:34:18 +0000550 drm_fb_helper_fini(&ifbdev->helper);
Jesse Barnes79e53942008-11-07 14:24:08 -0800551
Lukas Wunner54632ab2015-11-18 13:43:20 +0100552 if (ifbdev->fb) {
Chris Wilson43cee312016-06-21 09:16:54 +0100553 mutex_lock(&ifbdev->helper.dev->struct_mutex);
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300554 intel_unpin_fb_obj(&ifbdev->fb->base, DRM_ROTATE_0);
Chris Wilson43cee312016-06-21 09:16:54 +0100555 mutex_unlock(&ifbdev->helper.dev->struct_mutex);
Chris Wilsonfb4b8ce2016-04-28 09:56:35 +0100556
Lukas Wunner54632ab2015-11-18 13:43:20 +0100557 drm_framebuffer_remove(&ifbdev->fb->base);
558 }
Chris Wilson43cee312016-06-21 09:16:54 +0100559
560 kfree(ifbdev);
Jesse Barnes79e53942008-11-07 14:24:08 -0800561}
Dave Airlie38651672010-03-30 05:34:13 +0000562
Jesse Barnesd978ef12014-03-07 08:57:51 -0800563/*
564 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
565 * The core display code will have read out the current plane configuration,
566 * so we use that to figure out if there's an object for us to use as the
567 * fb, and if so, we re-use it for the fbdev configuration.
568 *
569 * Note we only support a single fb shared across pipes for boot (mostly for
570 * fbcon), so we just find the biggest and use that.
571 */
572static bool intel_fbdev_init_bios(struct drm_device *dev,
573 struct intel_fbdev *ifbdev)
574{
575 struct intel_framebuffer *fb = NULL;
576 struct drm_crtc *crtc;
577 struct intel_crtc *intel_crtc;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800578 unsigned int max_size = 0;
579
Jesse Barnesd978ef12014-03-07 08:57:51 -0800580 /* Find the largest fb */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100581 for_each_crtc(dev, crtc) {
Maarten Lankhorst8e9ba312015-07-13 16:30:16 +0200582 struct drm_i915_gem_object *obj =
583 intel_fb_obj(crtc->primary->state->fb);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800584 intel_crtc = to_intel_crtc(crtc);
585
Maarten Lankhorstd5515992015-08-27 15:44:03 +0200586 if (!crtc->state->active || !obj) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800587 DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
588 pipe_name(intel_crtc->pipe));
589 continue;
590 }
591
Maarten Lankhorst8e9ba312015-07-13 16:30:16 +0200592 if (obj->base.size > max_size) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800593 DRM_DEBUG_KMS("found possible fb from plane %c\n",
594 pipe_name(intel_crtc->pipe));
Maarten Lankhorst8e9ba312015-07-13 16:30:16 +0200595 fb = to_intel_framebuffer(crtc->primary->state->fb);
596 max_size = obj->base.size;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800597 }
598 }
599
600 if (!fb) {
601 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
602 goto out;
603 }
604
605 /* Now make sure all the pipes will fit into it */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100606 for_each_crtc(dev, crtc) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800607 unsigned int cur_size;
608
609 intel_crtc = to_intel_crtc(crtc);
610
Maarten Lankhorstd5515992015-08-27 15:44:03 +0200611 if (!crtc->state->active) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800612 DRM_DEBUG_KMS("pipe %c not active, skipping\n",
613 pipe_name(intel_crtc->pipe));
614 continue;
615 }
616
617 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
618 pipe_name(intel_crtc->pipe));
619
620 /*
621 * See if the plane fb we found above will fit on this
Chris Wilsonbc104d12014-03-20 15:11:21 +0000622 * pipe. Note we need to use the selected fb's pitch and bpp
623 * rather than the current pipe's, since they differ.
Jesse Barnesd978ef12014-03-07 08:57:51 -0800624 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200625 cur_size = intel_crtc->config->base.adjusted_mode.crtc_hdisplay;
Chris Wilsonbc104d12014-03-20 15:11:21 +0000626 cur_size = cur_size * fb->base.bits_per_pixel / 8;
627 if (fb->base.pitches[0] < cur_size) {
628 DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
629 pipe_name(intel_crtc->pipe),
630 cur_size, fb->base.pitches[0]);
Chris Wilsonbc104d12014-03-20 15:11:21 +0000631 fb = NULL;
632 break;
633 }
634
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200635 cur_size = intel_crtc->config->base.adjusted_mode.crtc_vdisplay;
Damien Lespiauec2c9812015-01-20 12:51:45 +0000636 cur_size = intel_fb_align_height(dev, cur_size,
Daniel Vetter091df6c2015-02-10 17:16:10 +0000637 fb->base.pixel_format,
638 fb->base.modifier[0]);
Chris Wilsonbc104d12014-03-20 15:11:21 +0000639 cur_size *= fb->base.pitches[0];
640 DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
641 pipe_name(intel_crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200642 intel_crtc->config->base.adjusted_mode.crtc_hdisplay,
643 intel_crtc->config->base.adjusted_mode.crtc_vdisplay,
Chris Wilsonbc104d12014-03-20 15:11:21 +0000644 fb->base.bits_per_pixel,
Jesse Barnesd978ef12014-03-07 08:57:51 -0800645 cur_size);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800646
647 if (cur_size > max_size) {
648 DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
649 pipe_name(intel_crtc->pipe),
650 cur_size, max_size);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800651 fb = NULL;
652 break;
653 }
654
655 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
656 pipe_name(intel_crtc->pipe),
657 max_size, cur_size);
658 }
659
Jesse Barnesd978ef12014-03-07 08:57:51 -0800660 if (!fb) {
661 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
662 goto out;
663 }
664
Jesse Barnes484b41d2014-03-07 08:57:55 -0800665 ifbdev->preferred_bpp = fb->base.bits_per_pixel;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800666 ifbdev->fb = fb;
667
Jesse Barnes484b41d2014-03-07 08:57:55 -0800668 drm_framebuffer_reference(&ifbdev->fb->base);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800669
670 /* Final pass to check if any active pipes don't have fbs */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100671 for_each_crtc(dev, crtc) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800672 intel_crtc = to_intel_crtc(crtc);
673
Maarten Lankhorstd5515992015-08-27 15:44:03 +0200674 if (!crtc->state->active)
Jesse Barnesd978ef12014-03-07 08:57:51 -0800675 continue;
676
Dave Airlie66e514c2014-04-03 07:51:54 +1000677 WARN(!crtc->primary->fb,
Jesse Barnesd978ef12014-03-07 08:57:51 -0800678 "re-used BIOS config but lost an fb on crtc %d\n",
679 crtc->base.id);
680 }
681
682
683 DRM_DEBUG_KMS("using BIOS fb for initial console\n");
684 return true;
685
Jesse Barnesd978ef12014-03-07 08:57:51 -0800686out:
687
688 return false;
689}
690
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100691static void intel_fbdev_suspend_worker(struct work_struct *work)
692{
Chris Wilson91c8a322016-07-05 10:40:23 +0100693 intel_fbdev_set_suspend(&container_of(work,
694 struct drm_i915_private,
695 fbdev_suspend_work)->drm,
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100696 FBINFO_STATE_RUNNING,
697 true);
698}
699
Dave Airlie38651672010-03-30 05:34:13 +0000700int intel_fbdev_init(struct drm_device *dev)
701{
Dave Airlie8be48d92010-03-30 05:34:14 +0000702 struct intel_fbdev *ifbdev;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100703 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson5a793952010-06-06 10:50:03 +0100704 int ret;
Dave Airlie8be48d92010-03-30 05:34:14 +0000705
Jesse Barnesd978ef12014-03-07 08:57:51 -0800706 if (WARN_ON(INTEL_INFO(dev)->num_pipes == 0))
707 return -ENODEV;
708
709 ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
710 if (ifbdev == NULL)
Dave Airlie8be48d92010-03-30 05:34:14 +0000711 return -ENOMEM;
712
Thierry Reding10a23102014-06-27 17:19:24 +0200713 drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
714
Jesse Barnesd978ef12014-03-07 08:57:51 -0800715 if (!intel_fbdev_init_bios(dev, ifbdev))
716 ifbdev->preferred_bpp = 32;
Dave Airlie8be48d92010-03-30 05:34:14 +0000717
Chris Wilson5a793952010-06-06 10:50:03 +0100718 ret = drm_fb_helper_init(dev, &ifbdev->helper,
Jesse Barnesd978ef12014-03-07 08:57:51 -0800719 INTEL_INFO(dev)->num_pipes, 4);
Chris Wilson5a793952010-06-06 10:50:03 +0100720 if (ret) {
721 kfree(ifbdev);
722 return ret;
723 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000724
Jesse Barnesd978ef12014-03-07 08:57:51 -0800725 dev_priv->fbdev = ifbdev;
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100726 INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
727
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000728 drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
Daniel Vetter20afbda2012-12-11 14:05:07 +0100729
Dave Airlie38651672010-03-30 05:34:13 +0000730 return 0;
731}
732
Ville Syrjäläe00bf692015-11-06 15:08:33 +0200733static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
Daniel Vetter20afbda2012-12-11 14:05:07 +0100734{
Chris Wilson43cee312016-06-21 09:16:54 +0100735 struct intel_fbdev *ifbdev = data;
Daniel Vetter20afbda2012-12-11 14:05:07 +0100736
737 /* Due to peculiar init order wrt to hpd handling this is separate. */
Lukas Wunner366e39b2015-11-18 16:29:51 +0100738 if (drm_fb_helper_initial_config(&ifbdev->helper,
739 ifbdev->preferred_bpp))
Chris Wilson43cee312016-06-21 09:16:54 +0100740 intel_fbdev_fini(ifbdev->helper.dev);
Daniel Vetter20afbda2012-12-11 14:05:07 +0100741}
742
Ville Syrjäläe00bf692015-11-06 15:08:33 +0200743void intel_fbdev_initial_config_async(struct drm_device *dev)
744{
Chris Wilson43cee312016-06-21 09:16:54 +0100745 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
746
747 ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
748}
749
750static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
751{
752 if (!ifbdev->cookie)
753 return;
754
755 /* Only serialises with all preceding async calls, hence +1 */
756 async_synchronize_cookie(ifbdev->cookie + 1);
757 ifbdev->cookie = 0;
Ville Syrjäläe00bf692015-11-06 15:08:33 +0200758}
759
Dave Airlie38651672010-03-30 05:34:13 +0000760void intel_fbdev_fini(struct drm_device *dev)
761{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100762 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson43cee312016-06-21 09:16:54 +0100763 struct intel_fbdev *ifbdev = dev_priv->fbdev;
764
765 if (!ifbdev)
Dave Airlie8be48d92010-03-30 05:34:14 +0000766 return;
767
Chris Wilson0b8c0e92016-07-13 18:34:44 +0100768 cancel_work_sync(&dev_priv->fbdev_suspend_work);
Lukas Wunner366e39b2015-11-18 16:29:51 +0100769 if (!current_is_async())
Chris Wilson43cee312016-06-21 09:16:54 +0100770 intel_fbdev_sync(ifbdev);
771
772 intel_fbdev_destroy(ifbdev);
Dave Airlie38651672010-03-30 05:34:13 +0000773 dev_priv->fbdev = NULL;
774}
Dave Airlie3fa016a2012-03-28 10:48:49 +0100775
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100776void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
Dave Airlie3fa016a2012-03-28 10:48:49 +0100777{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100778 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula1ffc5282013-05-07 18:54:05 +0300779 struct intel_fbdev *ifbdev = dev_priv->fbdev;
780 struct fb_info *info;
781
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100782 if (!ifbdev || !ifbdev->fb)
Dave Airlie3fa016a2012-03-28 10:48:49 +0100783 return;
784
Jani Nikula1ffc5282013-05-07 18:54:05 +0300785 info = ifbdev->helper.fbdev;
786
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100787 if (synchronous) {
788 /* Flush any pending work to turn the console on, and then
789 * wait to turn it off. It must be synchronous as we are
790 * about to suspend or unload the driver.
791 *
792 * Note that from within the work-handler, we cannot flush
793 * ourselves, so only flush outstanding work upon suspend!
794 */
795 if (state != FBINFO_STATE_RUNNING)
796 flush_work(&dev_priv->fbdev_suspend_work);
797 console_lock();
798 } else {
799 /*
800 * The console lock can be pretty contented on resume due
801 * to all the printk activity. Try to keep it out of the hot
802 * path of resume if possible.
803 */
804 WARN_ON(state != FBINFO_STATE_RUNNING);
805 if (!console_trylock()) {
806 /* Don't block our own workqueue as this can
807 * be run in parallel with other i915.ko tasks.
808 */
809 schedule_work(&dev_priv->fbdev_suspend_work);
810 return;
811 }
812 }
813
Jani Nikula1ffc5282013-05-07 18:54:05 +0300814 /* On resume from hibernation: If the object is shmemfs backed, it has
815 * been restored from swap. If the object is stolen however, it will be
816 * full of whatever garbage was left in there.
817 */
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800818 if (state == FBINFO_STATE_RUNNING && ifbdev->fb->obj->stolen)
Jani Nikula1ffc5282013-05-07 18:54:05 +0300819 memset_io(info->screen_base, 0, info->screen_size);
820
Archit Taneja21cff142015-07-31 16:21:56 +0530821 drm_fb_helper_set_suspend(&ifbdev->helper, state);
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100822 console_unlock();
Dave Airlie3fa016a2012-03-28 10:48:49 +0100823}
824
Daniel Vetter0632fef2013-10-08 17:44:49 +0200825void intel_fbdev_output_poll_changed(struct drm_device *dev)
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000826{
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100827 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
828
829 if (ifbdev && ifbdev->fb)
830 drm_fb_helper_hotplug_event(&ifbdev->helper);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000831}
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100832
Daniel Vetter0632fef2013-10-08 17:44:49 +0200833void intel_fbdev_restore_mode(struct drm_device *dev)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100834{
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100835 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100836
Rodrigo Vivid04df732015-07-08 16:25:21 -0700837 if (!ifbdev)
Ben Widawskye3c74752013-04-05 13:12:39 -0700838 return;
839
Chris Wilsone77018f2016-06-21 09:16:55 +0100840 intel_fbdev_sync(ifbdev);
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100841 if (!ifbdev->fb)
842 return;
Chris Wilsone77018f2016-06-21 09:16:55 +0100843
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100844 if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper)) {
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100845 DRM_DEBUG("failed to restore crtc mode\n");
Rodrigo Vivid04df732015-07-08 16:25:21 -0700846 } else {
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100847 mutex_lock(&dev->struct_mutex);
Rodrigo Vivid04df732015-07-08 16:25:21 -0700848 intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT);
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100849 mutex_unlock(&dev->struct_mutex);
Rodrigo Vivid04df732015-07-08 16:25:21 -0700850 }
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100851}