blob: 3e3632c18733502816dc169daab852910fcee814 [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>
37#include <linux/fb.h>
38#include <linux/init.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100039#include <linux/vga_switcheroo.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080040
David Howells760285e2012-10-02 18:01:07 +010041#include <drm/drmP.h>
42#include <drm/drm_crtc.h>
43#include <drm/drm_fb_helper.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080044#include "intel_drv.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 Zanoni3badb49f2015-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 Zanoni3badb49f2015-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 Zanoni3badb49f2015-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 Zanoni3badb49f2015-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)) {
Lukas Wunnerca40ba852015-10-22 13:37:18 +0200162 drm_gem_object_unreference(&obj->base);
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 Barnes8b4f49e2013-11-25 15:51:16 -0800190 struct drm_i915_gem_object *obj;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800191 bool prealloc = false;
Chris Wilson8ef85612016-04-28 09:56:39 +0100192 void *vaddr;
193 int ret;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800194
Chris Wilsonedd586f2014-04-23 08:54:31 +0100195 if (intel_fb &&
196 (sizes->fb_width > intel_fb->base.width ||
197 sizes->fb_height > intel_fb->base.height)) {
198 DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d),"
199 " releasing it\n",
200 intel_fb->base.width, intel_fb->base.height,
201 sizes->fb_width, sizes->fb_height);
202 drm_framebuffer_unreference(&intel_fb->base);
203 intel_fb = ifbdev->fb = NULL;
204 }
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800205 if (!intel_fb || WARN_ON(!intel_fb->obj)) {
Jesse Barnes48e92122013-11-26 11:25:54 -0800206 DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800207 ret = intelfb_alloc(helper, sizes);
208 if (ret)
Tvrtko Ursulin51f13852015-06-30 10:06:27 +0100209 return ret;
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800210 intel_fb = ifbdev->fb;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800211 } else {
Jesse Barnes48e92122013-11-26 11:25:54 -0800212 DRM_DEBUG_KMS("re-using BIOS fb\n");
Jesse Barnesd978ef12014-03-07 08:57:51 -0800213 prealloc = true;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800214 sizes->fb_width = intel_fb->base.width;
215 sizes->fb_height = intel_fb->base.height;
216 }
217
218 obj = intel_fb->obj;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800219
Tvrtko Ursulin51f13852015-06-30 10:06:27 +0100220 mutex_lock(&dev->struct_mutex);
221
Chris Wilson0c823122015-12-04 16:05:26 +0000222 /* Pin the GGTT vma for our access via info->screen_base.
223 * This also validates that any existing fb inherited from the
224 * BIOS is suitable for own access.
225 */
Ville Syrjälä3465c582016-02-15 22:54:43 +0200226 ret = intel_pin_and_fence_fb_obj(&ifbdev->fb->base, BIT(DRM_ROTATE_0));
Chris Wilson0c823122015-12-04 16:05:26 +0000227 if (ret)
228 goto out_unlock;
229
Archit Taneja21cff142015-07-31 16:21:56 +0530230 info = drm_fb_helper_alloc_fbi(helper);
231 if (IS_ERR(info)) {
Lukas Wunner366e39b2015-11-18 16:29:51 +0100232 DRM_ERROR("Failed to allocate fb_info\n");
Archit Taneja21cff142015-07-31 16:21:56 +0530233 ret = PTR_ERR(info);
Chris Wilsonb4476f52009-02-11 14:26:36 +0000234 goto out_unpin;
Jesse Barnes79e53942008-11-07 14:24:08 -0800235 }
236
Ville Syrjälä34308242013-05-31 20:07:05 +0300237 info->par = helper;
Jesse Barnes79e53942008-11-07 14:24:08 -0800238
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800239 fb = &ifbdev->fb->base;
Dave Airlie38651672010-03-30 05:34:13 +0000240
241 ifbdev->helper.fb = fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000242
Jesse Barnes79e53942008-11-07 14:24:08 -0800243 strcpy(info->fix.id, "inteldrmfb");
Jesse Barnes79e53942008-11-07 14:24:08 -0800244
Jesse Barnes8fd4bd22010-06-23 12:56:12 -0700245 info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
Jesse Barnes79e53942008-11-07 14:24:08 -0800246 info->fbops = &intelfb_ops;
247
Chris Wilson8ef85612016-04-28 09:56:39 +0100248 vma = i915_gem_obj_to_ggtt(obj);
249
Dave Airlie4410f392009-06-16 15:34:38 -0700250 /* setup aperture base/size for vesafb takeover */
Marcin Slusarz1471ca92010-05-16 17:27:03 +0200251 info->apertures->ranges[0].base = dev->mode_config.fb_base;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300252 info->apertures->ranges[0].size = ggtt->mappable_end;
Dave Airlie4410f392009-06-16 15:34:38 -0700253
Chris Wilson8ef85612016-04-28 09:56:39 +0100254 info->fix.smem_start = dev->mode_config.fb_base + vma->node.start;
255 info->fix.smem_len = vma->node.size;
Jesse Barnes79e53942008-11-07 14:24:08 -0800256
Chris Wilson8ef85612016-04-28 09:56:39 +0100257 vaddr = i915_vma_pin_iomap(vma);
258 if (IS_ERR(vaddr)) {
Lukas Wunner366e39b2015-11-18 16:29:51 +0100259 DRM_ERROR("Failed to remap framebuffer into virtual memory\n");
Chris Wilson8ef85612016-04-28 09:56:39 +0100260 ret = PTR_ERR(vaddr);
Archit Taneja21cff142015-07-31 16:21:56 +0530261 goto out_destroy_fbi;
Jesse Barnes79e53942008-11-07 14:24:08 -0800262 }
Chris Wilson8ef85612016-04-28 09:56:39 +0100263 info->screen_base = vaddr;
264 info->screen_size = vma->node.size;
Jesse Barnes79e53942008-11-07 14:24:08 -0800265
Jesse Barnes24576d22013-03-26 09:25:45 -0700266 /* This driver doesn't need a VT switch to restore the mode on resume */
267 info->skip_vt_switch = true;
268
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200269 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
Dave Airlie38651672010-03-30 05:34:13 +0000270 drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height);
Jesse Barnes79e53942008-11-07 14:24:08 -0800271
Chris Wilson88afe712012-12-16 12:15:41 +0000272 /* If the object is shmemfs backed, it will have given us zeroed pages.
273 * If the object is stolen however, it will be full of whatever
274 * garbage was left in there.
275 */
Jesse Barnesd978ef12014-03-07 08:57:51 -0800276 if (ifbdev->fb->obj->stolen && !prealloc)
Chris Wilson88afe712012-12-16 12:15:41 +0000277 memset_io(info->screen_base, 0, info->screen_size);
278
Sascha Hauerfb2a99e2012-02-06 10:58:19 +0100279 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
Jesse Barnes79e53942008-11-07 14:24:08 -0800280
Michel Thierry088e0df2015-08-07 17:40:17 +0100281 DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08llx, bo %p\n",
Dave Airlie8be48d92010-03-30 05:34:14 +0000282 fb->width, fb->height,
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700283 i915_gem_obj_ggtt_offset(obj), obj);
Jesse Barnes79e53942008-11-07 14:24:08 -0800284
285 mutex_unlock(&dev->struct_mutex);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000286 vga_switcheroo_client_fb_set(dev->pdev, info);
Jesse Barnes79e53942008-11-07 14:24:08 -0800287 return 0;
288
Archit Taneja21cff142015-07-31 16:21:56 +0530289out_destroy_fbi:
290 drm_fb_helper_release_fbi(helper);
Chris Wilsonb4476f52009-02-11 14:26:36 +0000291out_unpin:
Chris Wilsonfb4b8ce2016-04-28 09:56:35 +0100292 intel_unpin_fb_obj(&ifbdev->fb->base, BIT(DRM_ROTATE_0));
Chris Wilson0c823122015-12-04 16:05:26 +0000293out_unlock:
Jesse Barnes79e53942008-11-07 14:24:08 -0800294 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -0800295 return ret;
296}
297
Paulo Zanoni67437682013-09-24 13:52:56 -0300298/** Sets the color ramps on behalf of RandR */
299static void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
300 u16 blue, int regno)
301{
302 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
303
304 intel_crtc->lut_r[regno] = red >> 8;
305 intel_crtc->lut_g[regno] = green >> 8;
306 intel_crtc->lut_b[regno] = blue >> 8;
307}
308
309static void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
310 u16 *blue, int regno)
311{
312 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
313
314 *red = intel_crtc->lut_r[regno] << 8;
315 *green = intel_crtc->lut_g[regno] << 8;
316 *blue = intel_crtc->lut_b[regno] << 8;
317}
318
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800319static struct drm_fb_helper_crtc *
320intel_fb_helper_crtc(struct drm_fb_helper *fb_helper, struct drm_crtc *crtc)
321{
322 int i;
323
324 for (i = 0; i < fb_helper->crtc_count; i++)
325 if (fb_helper->crtc_info[i].mode_set.crtc == crtc)
326 return &fb_helper->crtc_info[i];
327
328 return NULL;
329}
330
331/*
332 * Try to read the BIOS display configuration and use it for the initial
333 * fb configuration.
334 *
335 * The BIOS or boot loader will generally create an initial display
336 * configuration for us that includes some set of active pipes and displays.
337 * This routine tries to figure out which pipes and connectors are active
338 * and stuffs them into the crtcs and modes array given to us by the
339 * drm_fb_helper code.
340 *
341 * The overall sequence is:
342 * intel_fbdev_init - from driver load
343 * intel_fbdev_init_bios - initialize the intel_fbdev using BIOS data
344 * drm_fb_helper_init - build fb helper structs
345 * drm_fb_helper_single_add_all_connectors - more fb helper structs
346 * intel_fbdev_initial_config - apply the config
347 * drm_fb_helper_initial_config - call ->probe then register_framebuffer()
348 * drm_setup_crtcs - build crtc config for fbdev
349 * intel_fb_initial_config - find active connectors etc
350 * drm_fb_helper_single_fb_probe - set up fbdev
351 * intelfb_create - re-use or alloc fb, build out fbdev structs
352 *
353 * Note that we don't make special consideration whether we could actually
354 * switch to the selected modes without a full modeset. E.g. when the display
355 * is in VGA mode we need to recalculate watermarks and set a new high-res
356 * framebuffer anyway.
357 */
358static bool intel_fb_initial_config(struct drm_fb_helper *fb_helper,
359 struct drm_fb_helper_crtc **crtcs,
360 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000361 struct drm_fb_offset *offsets,
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800362 bool *enabled, int width, int height)
363{
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800364 struct drm_device *dev = fb_helper->dev;
Chris Wilson86f40bb2016-07-02 15:36:02 +0100365 unsigned long conn_configured, mask;
366 unsigned int count = min(fb_helper->connector_count, BITS_PER_LONG);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800367 int i, j;
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800368 bool *save_enabled;
Daniel Vetter7c2bb532014-03-04 21:08:41 +0100369 bool fallback = true;
Daniel Vetter7e696e42014-03-04 21:08:42 +0100370 int num_connectors_enabled = 0;
371 int num_connectors_detected = 0;
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000372 int pass = 0;
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800373
Chris Wilson86f40bb2016-07-02 15:36:02 +0100374 save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL);
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800375 if (!save_enabled)
376 return false;
377
Chris Wilson86f40bb2016-07-02 15:36:02 +0100378 memcpy(save_enabled, enabled, count);
379 mask = BIT(count) - 1;
380 conn_configured = 0;
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000381retry:
Chris Wilson86f40bb2016-07-02 15:36:02 +0100382 for (i = 0; i < count; i++) {
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800383 struct drm_fb_helper_connector *fb_conn;
384 struct drm_connector *connector;
385 struct drm_encoder *encoder;
386 struct drm_fb_helper_crtc *new_crtc;
Lionel Landwerlin82cf4352016-03-16 10:57:16 +0000387 struct intel_crtc *intel_crtc;
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800388
389 fb_conn = fb_helper->connector_info[i];
390 connector = fb_conn->connector;
Daniel Vetter7e696e42014-03-04 21:08:42 +0100391
Chris Wilson86f40bb2016-07-02 15:36:02 +0100392 if (conn_configured & BIT(i))
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000393 continue;
394
395 if (pass == 0 && !connector->has_tile)
396 continue;
397
Daniel Vetter7e696e42014-03-04 21:08:42 +0100398 if (connector->status == connector_status_connected)
399 num_connectors_detected++;
400
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800401 if (!enabled[i]) {
Chris Wilsonc0c97622014-05-13 13:45:09 +0100402 DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300403 connector->name);
Chris Wilson86f40bb2016-07-02 15:36:02 +0100404 conn_configured |= BIT(i);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800405 continue;
406 }
407
Chris Wilsond84a0f32014-08-18 10:35:29 -0700408 if (connector->force == DRM_FORCE_OFF) {
409 DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
410 connector->name);
411 enabled[i] = false;
412 continue;
413 }
414
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100415 encoder = connector->state->best_encoder;
416 if (!encoder || WARN_ON(!connector->state->crtc)) {
Chris Wilsond84a0f32014-08-18 10:35:29 -0700417 if (connector->force > DRM_FORCE_OFF)
418 goto bail;
419
Chris Wilsonc0c97622014-05-13 13:45:09 +0100420 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300421 connector->name);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800422 enabled[i] = false;
Chris Wilson86f40bb2016-07-02 15:36:02 +0100423 conn_configured |= BIT(i);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800424 continue;
425 }
426
Daniel Vetter7e696e42014-03-04 21:08:42 +0100427 num_connectors_enabled++;
428
Lionel Landwerlin82cf4352016-03-16 10:57:16 +0000429 intel_crtc = to_intel_crtc(connector->state->crtc);
430 for (j = 0; j < 256; j++) {
431 intel_crtc->lut_r[j] = j;
432 intel_crtc->lut_g[j] = j;
433 intel_crtc->lut_b[j] = j;
434 }
435
Chris Wilson86f40bb2016-07-02 15:36:02 +0100436 new_crtc = intel_fb_helper_crtc(fb_helper,
437 connector->state->crtc);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800438
439 /*
440 * Make sure we're not trying to drive multiple connectors
441 * with a single CRTC, since our cloning support may not
442 * match the BIOS.
443 */
Chris Wilson86f40bb2016-07-02 15:36:02 +0100444 for (j = 0; j < count; j++) {
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800445 if (crtcs[j] == new_crtc) {
Daniel Vetter7e696e42014-03-04 21:08:42 +0100446 DRM_DEBUG_KMS("fallback: cloned configuration\n");
Chris Wilsond84a0f32014-08-18 10:35:29 -0700447 goto bail;
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800448 }
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800449 }
450
Chris Wilsonc0c97622014-05-13 13:45:09 +0100451 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300452 connector->name);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800453
454 /* go for command line mode first */
455 modes[i] = drm_pick_cmdline_mode(fb_conn, width, height);
456
457 /* try for preferred next */
458 if (!modes[i]) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000459 DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
460 connector->name, connector->has_tile);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800461 modes[i] = drm_has_preferred_mode(fb_conn, width,
462 height);
463 }
464
Chris Wilsonafba0b52014-05-13 16:07:37 +0100465 /* No preferred mode marked by the EDID? Are there any modes? */
466 if (!modes[i] && !list_empty(&connector->modes)) {
467 DRM_DEBUG_KMS("using first mode listed on connector %s\n",
Dave Airlie8d4ad9d2014-06-05 20:28:59 +1000468 connector->name);
Chris Wilsonafba0b52014-05-13 16:07:37 +0100469 modes[i] = list_first_entry(&connector->modes,
470 struct drm_display_mode,
471 head);
472 }
473
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800474 /* last resort: use current mode */
475 if (!modes[i]) {
476 /*
477 * IMPORTANT: We want to use the adjusted mode (i.e.
478 * after the panel fitter upscaling) as the initial
479 * config, not the input mode, which is what crtc->mode
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +0200480 * usually contains. But since our current
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800481 * code puts a mode derived from the post-pfit timings
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +0200482 * into crtc->mode this works out correctly.
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100483 *
484 * This is crtc->mode and not crtc->state->mode for the
485 * fastboot check to work correctly. crtc_state->mode has
486 * I915_MODE_FLAG_INHERITED, which we clear to force check
487 * state.
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800488 */
Chris Wilsonc0c97622014-05-13 13:45:09 +0100489 DRM_DEBUG_KMS("looking for current mode on connector %s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300490 connector->name);
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100491 modes[i] = &connector->state->crtc->mode;
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800492 }
493 crtcs[i] = new_crtc;
494
Ville Syrjälä78108b72016-05-27 20:59:19 +0300495 DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300496 connector->name,
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100497 connector->state->crtc->base.id,
Ville Syrjälä78108b72016-05-27 20:59:19 +0300498 connector->state->crtc->name,
Chris Wilsonc0c97622014-05-13 13:45:09 +0100499 modes[i]->hdisplay, modes[i]->vdisplay,
500 modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" :"");
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800501
Daniel Vetter7c2bb532014-03-04 21:08:41 +0100502 fallback = false;
Chris Wilson86f40bb2016-07-02 15:36:02 +0100503 conn_configured |= BIT(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000504 }
505
506 if ((conn_configured & mask) != mask) {
507 pass++;
508 goto retry;
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800509 }
510
Daniel Vetter7e696e42014-03-04 21:08:42 +0100511 /*
512 * If the BIOS didn't enable everything it could, fall back to have the
513 * same user experiencing of lighting up as much as possible like the
514 * fbdev helper library.
515 */
516 if (num_connectors_enabled != num_connectors_detected &&
517 num_connectors_enabled < INTEL_INFO(dev)->num_pipes) {
518 DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
519 DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
520 num_connectors_detected);
521 fallback = true;
522 }
523
Daniel Vetter7c2bb532014-03-04 21:08:41 +0100524 if (fallback) {
Chris Wilsond84a0f32014-08-18 10:35:29 -0700525bail:
Daniel Vetter7e696e42014-03-04 21:08:42 +0100526 DRM_DEBUG_KMS("Not using firmware configuration\n");
Chris Wilson86f40bb2016-07-02 15:36:02 +0100527 memcpy(enabled, save_enabled, count);
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800528 kfree(save_enabled);
529 return false;
530 }
531
532 kfree(save_enabled);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800533 return true;
534}
535
Thierry Reding3a493872014-06-27 17:19:23 +0200536static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800537 .initial_config = intel_fb_initial_config,
Dave Airlie4abe3522010-03-30 05:34:18 +0000538 .gamma_set = intel_crtc_fb_gamma_set,
539 .gamma_get = intel_crtc_fb_gamma_get,
Daniel Vettercd5428a2013-01-21 23:42:49 +0100540 .fb_probe = intelfb_create,
Dave Airlie4abe3522010-03-30 05:34:18 +0000541};
542
Chris Wilson43cee312016-06-21 09:16:54 +0100543static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
Jesse Barnes79e53942008-11-07 14:24:08 -0800544{
Chris Wilson0c823122015-12-04 16:05:26 +0000545 /* We rely on the object-free to release the VMA pinning for
546 * the info->screen_base mmaping. Leaking the VMA is simpler than
547 * trying to rectify all the possible error paths leading here.
548 */
Chris Wilsonddfe1562013-08-06 17:43:07 +0100549
Archit Taneja21cff142015-07-31 16:21:56 +0530550 drm_fb_helper_unregister_fbi(&ifbdev->helper);
551 drm_fb_helper_release_fbi(&ifbdev->helper);
Jesse Barnes79e53942008-11-07 14:24:08 -0800552
Dave Airlie4abe3522010-03-30 05:34:18 +0000553 drm_fb_helper_fini(&ifbdev->helper);
Jesse Barnes79e53942008-11-07 14:24:08 -0800554
Lukas Wunner54632ab2015-11-18 13:43:20 +0100555 if (ifbdev->fb) {
Chris Wilson43cee312016-06-21 09:16:54 +0100556 mutex_lock(&ifbdev->helper.dev->struct_mutex);
Chris Wilsonfb4b8ce2016-04-28 09:56:35 +0100557 intel_unpin_fb_obj(&ifbdev->fb->base, BIT(DRM_ROTATE_0));
Chris Wilson43cee312016-06-21 09:16:54 +0100558 mutex_unlock(&ifbdev->helper.dev->struct_mutex);
Chris Wilsonfb4b8ce2016-04-28 09:56:35 +0100559
Lukas Wunner54632ab2015-11-18 13:43:20 +0100560 drm_framebuffer_remove(&ifbdev->fb->base);
561 }
Chris Wilson43cee312016-06-21 09:16:54 +0100562
563 kfree(ifbdev);
Jesse Barnes79e53942008-11-07 14:24:08 -0800564}
Dave Airlie38651672010-03-30 05:34:13 +0000565
Jesse Barnesd978ef12014-03-07 08:57:51 -0800566/*
567 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
568 * The core display code will have read out the current plane configuration,
569 * so we use that to figure out if there's an object for us to use as the
570 * fb, and if so, we re-use it for the fbdev configuration.
571 *
572 * Note we only support a single fb shared across pipes for boot (mostly for
573 * fbcon), so we just find the biggest and use that.
574 */
575static bool intel_fbdev_init_bios(struct drm_device *dev,
576 struct intel_fbdev *ifbdev)
577{
578 struct intel_framebuffer *fb = NULL;
579 struct drm_crtc *crtc;
580 struct intel_crtc *intel_crtc;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800581 unsigned int max_size = 0;
582
Jesse Barnesd978ef12014-03-07 08:57:51 -0800583 /* Find the largest fb */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100584 for_each_crtc(dev, crtc) {
Maarten Lankhorst8e9ba312015-07-13 16:30:16 +0200585 struct drm_i915_gem_object *obj =
586 intel_fb_obj(crtc->primary->state->fb);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800587 intel_crtc = to_intel_crtc(crtc);
588
Maarten Lankhorstd5515992015-08-27 15:44:03 +0200589 if (!crtc->state->active || !obj) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800590 DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
591 pipe_name(intel_crtc->pipe));
592 continue;
593 }
594
Maarten Lankhorst8e9ba312015-07-13 16:30:16 +0200595 if (obj->base.size > max_size) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800596 DRM_DEBUG_KMS("found possible fb from plane %c\n",
597 pipe_name(intel_crtc->pipe));
Maarten Lankhorst8e9ba312015-07-13 16:30:16 +0200598 fb = to_intel_framebuffer(crtc->primary->state->fb);
599 max_size = obj->base.size;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800600 }
601 }
602
603 if (!fb) {
604 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
605 goto out;
606 }
607
608 /* Now make sure all the pipes will fit into it */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100609 for_each_crtc(dev, crtc) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800610 unsigned int cur_size;
611
612 intel_crtc = to_intel_crtc(crtc);
613
Maarten Lankhorstd5515992015-08-27 15:44:03 +0200614 if (!crtc->state->active) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800615 DRM_DEBUG_KMS("pipe %c not active, skipping\n",
616 pipe_name(intel_crtc->pipe));
617 continue;
618 }
619
620 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
621 pipe_name(intel_crtc->pipe));
622
623 /*
624 * See if the plane fb we found above will fit on this
Chris Wilsonbc104d12014-03-20 15:11:21 +0000625 * pipe. Note we need to use the selected fb's pitch and bpp
626 * rather than the current pipe's, since they differ.
Jesse Barnesd978ef12014-03-07 08:57:51 -0800627 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200628 cur_size = intel_crtc->config->base.adjusted_mode.crtc_hdisplay;
Chris Wilsonbc104d12014-03-20 15:11:21 +0000629 cur_size = cur_size * fb->base.bits_per_pixel / 8;
630 if (fb->base.pitches[0] < cur_size) {
631 DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
632 pipe_name(intel_crtc->pipe),
633 cur_size, fb->base.pitches[0]);
Chris Wilsonbc104d12014-03-20 15:11:21 +0000634 fb = NULL;
635 break;
636 }
637
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200638 cur_size = intel_crtc->config->base.adjusted_mode.crtc_vdisplay;
Damien Lespiauec2c9812015-01-20 12:51:45 +0000639 cur_size = intel_fb_align_height(dev, cur_size,
Daniel Vetter091df6c2015-02-10 17:16:10 +0000640 fb->base.pixel_format,
641 fb->base.modifier[0]);
Chris Wilsonbc104d12014-03-20 15:11:21 +0000642 cur_size *= fb->base.pitches[0];
643 DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
644 pipe_name(intel_crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200645 intel_crtc->config->base.adjusted_mode.crtc_hdisplay,
646 intel_crtc->config->base.adjusted_mode.crtc_vdisplay,
Chris Wilsonbc104d12014-03-20 15:11:21 +0000647 fb->base.bits_per_pixel,
Jesse Barnesd978ef12014-03-07 08:57:51 -0800648 cur_size);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800649
650 if (cur_size > max_size) {
651 DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
652 pipe_name(intel_crtc->pipe),
653 cur_size, max_size);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800654 fb = NULL;
655 break;
656 }
657
658 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
659 pipe_name(intel_crtc->pipe),
660 max_size, cur_size);
661 }
662
Jesse Barnesd978ef12014-03-07 08:57:51 -0800663 if (!fb) {
664 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
665 goto out;
666 }
667
Jesse Barnes484b41d2014-03-07 08:57:55 -0800668 ifbdev->preferred_bpp = fb->base.bits_per_pixel;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800669 ifbdev->fb = fb;
670
Jesse Barnes484b41d2014-03-07 08:57:55 -0800671 drm_framebuffer_reference(&ifbdev->fb->base);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800672
673 /* Final pass to check if any active pipes don't have fbs */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100674 for_each_crtc(dev, crtc) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800675 intel_crtc = to_intel_crtc(crtc);
676
Maarten Lankhorstd5515992015-08-27 15:44:03 +0200677 if (!crtc->state->active)
Jesse Barnesd978ef12014-03-07 08:57:51 -0800678 continue;
679
Dave Airlie66e514c2014-04-03 07:51:54 +1000680 WARN(!crtc->primary->fb,
Jesse Barnesd978ef12014-03-07 08:57:51 -0800681 "re-used BIOS config but lost an fb on crtc %d\n",
682 crtc->base.id);
683 }
684
685
686 DRM_DEBUG_KMS("using BIOS fb for initial console\n");
687 return true;
688
Jesse Barnesd978ef12014-03-07 08:57:51 -0800689out:
690
691 return false;
692}
693
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100694static void intel_fbdev_suspend_worker(struct work_struct *work)
695{
Chris Wilson91c8a322016-07-05 10:40:23 +0100696 intel_fbdev_set_suspend(&container_of(work,
697 struct drm_i915_private,
698 fbdev_suspend_work)->drm,
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100699 FBINFO_STATE_RUNNING,
700 true);
701}
702
Dave Airlie38651672010-03-30 05:34:13 +0000703int intel_fbdev_init(struct drm_device *dev)
704{
Dave Airlie8be48d92010-03-30 05:34:14 +0000705 struct intel_fbdev *ifbdev;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100706 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson5a793952010-06-06 10:50:03 +0100707 int ret;
Dave Airlie8be48d92010-03-30 05:34:14 +0000708
Jesse Barnesd978ef12014-03-07 08:57:51 -0800709 if (WARN_ON(INTEL_INFO(dev)->num_pipes == 0))
710 return -ENODEV;
711
712 ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
713 if (ifbdev == NULL)
Dave Airlie8be48d92010-03-30 05:34:14 +0000714 return -ENOMEM;
715
Thierry Reding10a23102014-06-27 17:19:24 +0200716 drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
717
Jesse Barnesd978ef12014-03-07 08:57:51 -0800718 if (!intel_fbdev_init_bios(dev, ifbdev))
719 ifbdev->preferred_bpp = 32;
Dave Airlie8be48d92010-03-30 05:34:14 +0000720
Chris Wilson5a793952010-06-06 10:50:03 +0100721 ret = drm_fb_helper_init(dev, &ifbdev->helper,
Jesse Barnesd978ef12014-03-07 08:57:51 -0800722 INTEL_INFO(dev)->num_pipes, 4);
Chris Wilson5a793952010-06-06 10:50:03 +0100723 if (ret) {
724 kfree(ifbdev);
725 return ret;
726 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000727
Jesse Barnesd978ef12014-03-07 08:57:51 -0800728 dev_priv->fbdev = ifbdev;
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100729 INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
730
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000731 drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
Daniel Vetter20afbda2012-12-11 14:05:07 +0100732
Dave Airlie38651672010-03-30 05:34:13 +0000733 return 0;
734}
735
Ville Syrjäläe00bf692015-11-06 15:08:33 +0200736static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
Daniel Vetter20afbda2012-12-11 14:05:07 +0100737{
Chris Wilson43cee312016-06-21 09:16:54 +0100738 struct intel_fbdev *ifbdev = data;
Daniel Vetter20afbda2012-12-11 14:05:07 +0100739
740 /* Due to peculiar init order wrt to hpd handling this is separate. */
Lukas Wunner366e39b2015-11-18 16:29:51 +0100741 if (drm_fb_helper_initial_config(&ifbdev->helper,
742 ifbdev->preferred_bpp))
Chris Wilson43cee312016-06-21 09:16:54 +0100743 intel_fbdev_fini(ifbdev->helper.dev);
Daniel Vetter20afbda2012-12-11 14:05:07 +0100744}
745
Ville Syrjäläe00bf692015-11-06 15:08:33 +0200746void intel_fbdev_initial_config_async(struct drm_device *dev)
747{
Chris Wilson43cee312016-06-21 09:16:54 +0100748 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
749
750 ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
751}
752
753static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
754{
755 if (!ifbdev->cookie)
756 return;
757
758 /* Only serialises with all preceding async calls, hence +1 */
759 async_synchronize_cookie(ifbdev->cookie + 1);
760 ifbdev->cookie = 0;
Ville Syrjäläe00bf692015-11-06 15:08:33 +0200761}
762
Dave Airlie38651672010-03-30 05:34:13 +0000763void intel_fbdev_fini(struct drm_device *dev)
764{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100765 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson43cee312016-06-21 09:16:54 +0100766 struct intel_fbdev *ifbdev = dev_priv->fbdev;
767
768 if (!ifbdev)
Dave Airlie8be48d92010-03-30 05:34:14 +0000769 return;
770
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100771 flush_work(&dev_priv->fbdev_suspend_work);
Lukas Wunner366e39b2015-11-18 16:29:51 +0100772 if (!current_is_async())
Chris Wilson43cee312016-06-21 09:16:54 +0100773 intel_fbdev_sync(ifbdev);
774
775 intel_fbdev_destroy(ifbdev);
Dave Airlie38651672010-03-30 05:34:13 +0000776 dev_priv->fbdev = NULL;
777}
Dave Airlie3fa016a2012-03-28 10:48:49 +0100778
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100779void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
Dave Airlie3fa016a2012-03-28 10:48:49 +0100780{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100781 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula1ffc5282013-05-07 18:54:05 +0300782 struct intel_fbdev *ifbdev = dev_priv->fbdev;
783 struct fb_info *info;
784
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100785 if (!ifbdev || !ifbdev->fb)
Dave Airlie3fa016a2012-03-28 10:48:49 +0100786 return;
787
Jani Nikula1ffc5282013-05-07 18:54:05 +0300788 info = ifbdev->helper.fbdev;
789
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100790 if (synchronous) {
791 /* Flush any pending work to turn the console on, and then
792 * wait to turn it off. It must be synchronous as we are
793 * about to suspend or unload the driver.
794 *
795 * Note that from within the work-handler, we cannot flush
796 * ourselves, so only flush outstanding work upon suspend!
797 */
798 if (state != FBINFO_STATE_RUNNING)
799 flush_work(&dev_priv->fbdev_suspend_work);
800 console_lock();
801 } else {
802 /*
803 * The console lock can be pretty contented on resume due
804 * to all the printk activity. Try to keep it out of the hot
805 * path of resume if possible.
806 */
807 WARN_ON(state != FBINFO_STATE_RUNNING);
808 if (!console_trylock()) {
809 /* Don't block our own workqueue as this can
810 * be run in parallel with other i915.ko tasks.
811 */
812 schedule_work(&dev_priv->fbdev_suspend_work);
813 return;
814 }
815 }
816
Jani Nikula1ffc5282013-05-07 18:54:05 +0300817 /* On resume from hibernation: If the object is shmemfs backed, it has
818 * been restored from swap. If the object is stolen however, it will be
819 * full of whatever garbage was left in there.
820 */
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800821 if (state == FBINFO_STATE_RUNNING && ifbdev->fb->obj->stolen)
Jani Nikula1ffc5282013-05-07 18:54:05 +0300822 memset_io(info->screen_base, 0, info->screen_size);
823
Archit Taneja21cff142015-07-31 16:21:56 +0530824 drm_fb_helper_set_suspend(&ifbdev->helper, state);
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100825 console_unlock();
Dave Airlie3fa016a2012-03-28 10:48:49 +0100826}
827
Daniel Vetter0632fef2013-10-08 17:44:49 +0200828void intel_fbdev_output_poll_changed(struct drm_device *dev)
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000829{
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100830 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
831
832 if (ifbdev && ifbdev->fb)
833 drm_fb_helper_hotplug_event(&ifbdev->helper);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000834}
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100835
Daniel Vetter0632fef2013-10-08 17:44:49 +0200836void intel_fbdev_restore_mode(struct drm_device *dev)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100837{
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100838 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100839
Rodrigo Vivid04df732015-07-08 16:25:21 -0700840 if (!ifbdev)
Ben Widawskye3c74752013-04-05 13:12:39 -0700841 return;
842
Chris Wilsone77018f2016-06-21 09:16:55 +0100843 intel_fbdev_sync(ifbdev);
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100844 if (!ifbdev->fb)
845 return;
Chris Wilsone77018f2016-06-21 09:16:55 +0100846
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100847 if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper)) {
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100848 DRM_DEBUG("failed to restore crtc mode\n");
Rodrigo Vivid04df732015-07-08 16:25:21 -0700849 } else {
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100850 mutex_lock(&dev->struct_mutex);
Rodrigo Vivid04df732015-07-08 16:25:21 -0700851 intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT);
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100852 mutex_unlock(&dev->struct_mutex);
Rodrigo Vivid04df732015-07-08 16:25:21 -0700853 }
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100854}