blob: bdf64dec190e30a1a2cec22ceda07e0167536920 [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 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)) {
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;
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800365 int i, j;
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800366 bool *save_enabled;
Daniel Vetter7c2bb532014-03-04 21:08:41 +0100367 bool fallback = true;
Daniel Vetter7e696e42014-03-04 21:08:42 +0100368 int num_connectors_enabled = 0;
369 int num_connectors_detected = 0;
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000370 uint64_t conn_configured = 0, mask;
371 int pass = 0;
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800372
373 save_enabled = kcalloc(dev->mode_config.num_connector, sizeof(bool),
374 GFP_KERNEL);
375 if (!save_enabled)
376 return false;
377
378 memcpy(save_enabled, enabled, dev->mode_config.num_connector);
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000379 mask = (1 << fb_helper->connector_count) - 1;
380retry:
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800381 for (i = 0; i < fb_helper->connector_count; i++) {
382 struct drm_fb_helper_connector *fb_conn;
383 struct drm_connector *connector;
384 struct drm_encoder *encoder;
385 struct drm_fb_helper_crtc *new_crtc;
Lionel Landwerlin82cf4352016-03-16 10:57:16 +0000386 struct intel_crtc *intel_crtc;
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800387
388 fb_conn = fb_helper->connector_info[i];
389 connector = fb_conn->connector;
Daniel Vetter7e696e42014-03-04 21:08:42 +0100390
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000391 if (conn_configured & (1 << i))
392 continue;
393
394 if (pass == 0 && !connector->has_tile)
395 continue;
396
Daniel Vetter7e696e42014-03-04 21:08:42 +0100397 if (connector->status == connector_status_connected)
398 num_connectors_detected++;
399
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800400 if (!enabled[i]) {
Chris Wilsonc0c97622014-05-13 13:45:09 +0100401 DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300402 connector->name);
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000403 conn_configured |= (1 << i);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800404 continue;
405 }
406
Chris Wilsond84a0f32014-08-18 10:35:29 -0700407 if (connector->force == DRM_FORCE_OFF) {
408 DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
409 connector->name);
410 enabled[i] = false;
411 continue;
412 }
413
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100414 encoder = connector->state->best_encoder;
415 if (!encoder || WARN_ON(!connector->state->crtc)) {
Chris Wilsond84a0f32014-08-18 10:35:29 -0700416 if (connector->force > DRM_FORCE_OFF)
417 goto bail;
418
Chris Wilsonc0c97622014-05-13 13:45:09 +0100419 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300420 connector->name);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800421 enabled[i] = false;
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000422 conn_configured |= (1 << i);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800423 continue;
424 }
425
Daniel Vetter7e696e42014-03-04 21:08:42 +0100426 num_connectors_enabled++;
427
Lionel Landwerlin82cf4352016-03-16 10:57:16 +0000428 intel_crtc = to_intel_crtc(connector->state->crtc);
429 for (j = 0; j < 256; j++) {
430 intel_crtc->lut_r[j] = j;
431 intel_crtc->lut_g[j] = j;
432 intel_crtc->lut_b[j] = j;
433 }
434
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100435 new_crtc = intel_fb_helper_crtc(fb_helper, connector->state->crtc);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800436
437 /*
438 * Make sure we're not trying to drive multiple connectors
439 * with a single CRTC, since our cloning support may not
440 * match the BIOS.
441 */
442 for (j = 0; j < fb_helper->connector_count; j++) {
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800443 if (crtcs[j] == new_crtc) {
Daniel Vetter7e696e42014-03-04 21:08:42 +0100444 DRM_DEBUG_KMS("fallback: cloned configuration\n");
Chris Wilsond84a0f32014-08-18 10:35:29 -0700445 goto bail;
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800446 }
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800447 }
448
Chris Wilsonc0c97622014-05-13 13:45:09 +0100449 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300450 connector->name);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800451
452 /* go for command line mode first */
453 modes[i] = drm_pick_cmdline_mode(fb_conn, width, height);
454
455 /* try for preferred next */
456 if (!modes[i]) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000457 DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
458 connector->name, connector->has_tile);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800459 modes[i] = drm_has_preferred_mode(fb_conn, width,
460 height);
461 }
462
Chris Wilsonafba0b52014-05-13 16:07:37 +0100463 /* No preferred mode marked by the EDID? Are there any modes? */
464 if (!modes[i] && !list_empty(&connector->modes)) {
465 DRM_DEBUG_KMS("using first mode listed on connector %s\n",
Dave Airlie8d4ad9d2014-06-05 20:28:59 +1000466 connector->name);
Chris Wilsonafba0b52014-05-13 16:07:37 +0100467 modes[i] = list_first_entry(&connector->modes,
468 struct drm_display_mode,
469 head);
470 }
471
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800472 /* last resort: use current mode */
473 if (!modes[i]) {
474 /*
475 * IMPORTANT: We want to use the adjusted mode (i.e.
476 * after the panel fitter upscaling) as the initial
477 * config, not the input mode, which is what crtc->mode
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +0200478 * usually contains. But since our current
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800479 * code puts a mode derived from the post-pfit timings
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +0200480 * into crtc->mode this works out correctly.
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100481 *
482 * This is crtc->mode and not crtc->state->mode for the
483 * fastboot check to work correctly. crtc_state->mode has
484 * I915_MODE_FLAG_INHERITED, which we clear to force check
485 * state.
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800486 */
Chris Wilsonc0c97622014-05-13 13:45:09 +0100487 DRM_DEBUG_KMS("looking for current mode on connector %s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300488 connector->name);
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100489 modes[i] = &connector->state->crtc->mode;
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800490 }
491 crtcs[i] = new_crtc;
492
Ville Syrjälä78108b72016-05-27 20:59:19 +0300493 DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300494 connector->name,
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100495 connector->state->crtc->base.id,
Ville Syrjälä78108b72016-05-27 20:59:19 +0300496 connector->state->crtc->name,
Chris Wilsonc0c97622014-05-13 13:45:09 +0100497 modes[i]->hdisplay, modes[i]->vdisplay,
498 modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" :"");
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800499
Daniel Vetter7c2bb532014-03-04 21:08:41 +0100500 fallback = false;
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000501 conn_configured |= (1 << i);
502 }
503
504 if ((conn_configured & mask) != mask) {
505 pass++;
506 goto retry;
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800507 }
508
Daniel Vetter7e696e42014-03-04 21:08:42 +0100509 /*
510 * If the BIOS didn't enable everything it could, fall back to have the
511 * same user experiencing of lighting up as much as possible like the
512 * fbdev helper library.
513 */
514 if (num_connectors_enabled != num_connectors_detected &&
515 num_connectors_enabled < INTEL_INFO(dev)->num_pipes) {
516 DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
517 DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
518 num_connectors_detected);
519 fallback = true;
520 }
521
Daniel Vetter7c2bb532014-03-04 21:08:41 +0100522 if (fallback) {
Chris Wilsond84a0f32014-08-18 10:35:29 -0700523bail:
Daniel Vetter7e696e42014-03-04 21:08:42 +0100524 DRM_DEBUG_KMS("Not using firmware configuration\n");
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800525 memcpy(enabled, save_enabled, dev->mode_config.num_connector);
526 kfree(save_enabled);
527 return false;
528 }
529
530 kfree(save_enabled);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800531 return true;
532}
533
Thierry Reding3a493872014-06-27 17:19:23 +0200534static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800535 .initial_config = intel_fb_initial_config,
Dave Airlie4abe3522010-03-30 05:34:18 +0000536 .gamma_set = intel_crtc_fb_gamma_set,
537 .gamma_get = intel_crtc_fb_gamma_get,
Daniel Vettercd5428a2013-01-21 23:42:49 +0100538 .fb_probe = intelfb_create,
Dave Airlie4abe3522010-03-30 05:34:18 +0000539};
540
Chris Wilson7b4f3992010-10-04 15:33:04 +0100541static void intel_fbdev_destroy(struct drm_device *dev,
542 struct intel_fbdev *ifbdev)
Jesse Barnes79e53942008-11-07 14:24:08 -0800543{
Chris Wilson0c823122015-12-04 16:05:26 +0000544 /* We rely on the object-free to release the VMA pinning for
545 * the info->screen_base mmaping. Leaking the VMA is simpler than
546 * trying to rectify all the possible error paths leading here.
547 */
Chris Wilsonddfe1562013-08-06 17:43:07 +0100548
Archit Taneja21cff142015-07-31 16:21:56 +0530549 drm_fb_helper_unregister_fbi(&ifbdev->helper);
550 drm_fb_helper_release_fbi(&ifbdev->helper);
Jesse Barnes79e53942008-11-07 14:24:08 -0800551
Dave Airlie4abe3522010-03-30 05:34:18 +0000552 drm_fb_helper_fini(&ifbdev->helper);
Jesse Barnes79e53942008-11-07 14:24:08 -0800553
Lukas Wunner54632ab2015-11-18 13:43:20 +0100554 if (ifbdev->fb) {
555 drm_framebuffer_unregister_private(&ifbdev->fb->base);
Chris Wilsonfb4b8ce2016-04-28 09:56:35 +0100556
557 mutex_lock(&dev->struct_mutex);
558 intel_unpin_fb_obj(&ifbdev->fb->base, BIT(DRM_ROTATE_0));
559 mutex_unlock(&dev->struct_mutex);
560
Lukas Wunner54632ab2015-11-18 13:43:20 +0100561 drm_framebuffer_remove(&ifbdev->fb->base);
562 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800563}
Dave Airlie38651672010-03-30 05:34:13 +0000564
Jesse Barnesd978ef12014-03-07 08:57:51 -0800565/*
566 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
567 * The core display code will have read out the current plane configuration,
568 * so we use that to figure out if there's an object for us to use as the
569 * fb, and if so, we re-use it for the fbdev configuration.
570 *
571 * Note we only support a single fb shared across pipes for boot (mostly for
572 * fbcon), so we just find the biggest and use that.
573 */
574static bool intel_fbdev_init_bios(struct drm_device *dev,
575 struct intel_fbdev *ifbdev)
576{
577 struct intel_framebuffer *fb = NULL;
578 struct drm_crtc *crtc;
579 struct intel_crtc *intel_crtc;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800580 unsigned int max_size = 0;
581
Jesse Barnesd978ef12014-03-07 08:57:51 -0800582 /* Find the largest fb */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100583 for_each_crtc(dev, crtc) {
Maarten Lankhorst8e9ba312015-07-13 16:30:16 +0200584 struct drm_i915_gem_object *obj =
585 intel_fb_obj(crtc->primary->state->fb);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800586 intel_crtc = to_intel_crtc(crtc);
587
Maarten Lankhorstd5515992015-08-27 15:44:03 +0200588 if (!crtc->state->active || !obj) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800589 DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
590 pipe_name(intel_crtc->pipe));
591 continue;
592 }
593
Maarten Lankhorst8e9ba312015-07-13 16:30:16 +0200594 if (obj->base.size > max_size) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800595 DRM_DEBUG_KMS("found possible fb from plane %c\n",
596 pipe_name(intel_crtc->pipe));
Maarten Lankhorst8e9ba312015-07-13 16:30:16 +0200597 fb = to_intel_framebuffer(crtc->primary->state->fb);
598 max_size = obj->base.size;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800599 }
600 }
601
602 if (!fb) {
603 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
604 goto out;
605 }
606
607 /* Now make sure all the pipes will fit into it */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100608 for_each_crtc(dev, crtc) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800609 unsigned int cur_size;
610
611 intel_crtc = to_intel_crtc(crtc);
612
Maarten Lankhorstd5515992015-08-27 15:44:03 +0200613 if (!crtc->state->active) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800614 DRM_DEBUG_KMS("pipe %c not active, skipping\n",
615 pipe_name(intel_crtc->pipe));
616 continue;
617 }
618
619 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
620 pipe_name(intel_crtc->pipe));
621
622 /*
623 * See if the plane fb we found above will fit on this
Chris Wilsonbc104d12014-03-20 15:11:21 +0000624 * pipe. Note we need to use the selected fb's pitch and bpp
625 * rather than the current pipe's, since they differ.
Jesse Barnesd978ef12014-03-07 08:57:51 -0800626 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200627 cur_size = intel_crtc->config->base.adjusted_mode.crtc_hdisplay;
Chris Wilsonbc104d12014-03-20 15:11:21 +0000628 cur_size = cur_size * fb->base.bits_per_pixel / 8;
629 if (fb->base.pitches[0] < cur_size) {
630 DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
631 pipe_name(intel_crtc->pipe),
632 cur_size, fb->base.pitches[0]);
Chris Wilsonbc104d12014-03-20 15:11:21 +0000633 fb = NULL;
634 break;
635 }
636
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200637 cur_size = intel_crtc->config->base.adjusted_mode.crtc_vdisplay;
Damien Lespiauec2c9812015-01-20 12:51:45 +0000638 cur_size = intel_fb_align_height(dev, cur_size,
Daniel Vetter091df6c2015-02-10 17:16:10 +0000639 fb->base.pixel_format,
640 fb->base.modifier[0]);
Chris Wilsonbc104d12014-03-20 15:11:21 +0000641 cur_size *= fb->base.pitches[0];
642 DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
643 pipe_name(intel_crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200644 intel_crtc->config->base.adjusted_mode.crtc_hdisplay,
645 intel_crtc->config->base.adjusted_mode.crtc_vdisplay,
Chris Wilsonbc104d12014-03-20 15:11:21 +0000646 fb->base.bits_per_pixel,
Jesse Barnesd978ef12014-03-07 08:57:51 -0800647 cur_size);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800648
649 if (cur_size > max_size) {
650 DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
651 pipe_name(intel_crtc->pipe),
652 cur_size, max_size);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800653 fb = NULL;
654 break;
655 }
656
657 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
658 pipe_name(intel_crtc->pipe),
659 max_size, cur_size);
660 }
661
Jesse Barnesd978ef12014-03-07 08:57:51 -0800662 if (!fb) {
663 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
664 goto out;
665 }
666
Jesse Barnes484b41d2014-03-07 08:57:55 -0800667 ifbdev->preferred_bpp = fb->base.bits_per_pixel;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800668 ifbdev->fb = fb;
669
Jesse Barnes484b41d2014-03-07 08:57:55 -0800670 drm_framebuffer_reference(&ifbdev->fb->base);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800671
672 /* Final pass to check if any active pipes don't have fbs */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100673 for_each_crtc(dev, crtc) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800674 intel_crtc = to_intel_crtc(crtc);
675
Maarten Lankhorstd5515992015-08-27 15:44:03 +0200676 if (!crtc->state->active)
Jesse Barnesd978ef12014-03-07 08:57:51 -0800677 continue;
678
Dave Airlie66e514c2014-04-03 07:51:54 +1000679 WARN(!crtc->primary->fb,
Jesse Barnesd978ef12014-03-07 08:57:51 -0800680 "re-used BIOS config but lost an fb on crtc %d\n",
681 crtc->base.id);
682 }
683
684
685 DRM_DEBUG_KMS("using BIOS fb for initial console\n");
686 return true;
687
Jesse Barnesd978ef12014-03-07 08:57:51 -0800688out:
689
690 return false;
691}
692
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100693static void intel_fbdev_suspend_worker(struct work_struct *work)
694{
695 intel_fbdev_set_suspend(container_of(work,
696 struct drm_i915_private,
697 fbdev_suspend_work)->dev,
698 FBINFO_STATE_RUNNING,
699 true);
700}
701
Dave Airlie38651672010-03-30 05:34:13 +0000702int intel_fbdev_init(struct drm_device *dev)
703{
Dave Airlie8be48d92010-03-30 05:34:14 +0000704 struct intel_fbdev *ifbdev;
Ville Syrjäläb51b32c2013-05-31 20:07:06 +0300705 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson5a793952010-06-06 10:50:03 +0100706 int ret;
Dave Airlie8be48d92010-03-30 05:34:14 +0000707
Jesse Barnesd978ef12014-03-07 08:57:51 -0800708 if (WARN_ON(INTEL_INFO(dev)->num_pipes == 0))
709 return -ENODEV;
710
711 ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
712 if (ifbdev == NULL)
Dave Airlie8be48d92010-03-30 05:34:14 +0000713 return -ENOMEM;
714
Thierry Reding10a23102014-06-27 17:19:24 +0200715 drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
716
Jesse Barnesd978ef12014-03-07 08:57:51 -0800717 if (!intel_fbdev_init_bios(dev, ifbdev))
718 ifbdev->preferred_bpp = 32;
Dave Airlie8be48d92010-03-30 05:34:14 +0000719
Chris Wilson5a793952010-06-06 10:50:03 +0100720 ret = drm_fb_helper_init(dev, &ifbdev->helper,
Jesse Barnesd978ef12014-03-07 08:57:51 -0800721 INTEL_INFO(dev)->num_pipes, 4);
Chris Wilson5a793952010-06-06 10:50:03 +0100722 if (ret) {
723 kfree(ifbdev);
724 return ret;
725 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000726
Rob Clark28cc5042015-08-25 15:36:00 -0400727 ifbdev->helper.atomic = true;
728
Jesse Barnesd978ef12014-03-07 08:57:51 -0800729 dev_priv->fbdev = ifbdev;
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100730 INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
731
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000732 drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
Daniel Vetter20afbda2012-12-11 14:05:07 +0100733
Dave Airlie38651672010-03-30 05:34:13 +0000734 return 0;
735}
736
Ville Syrjäläe00bf692015-11-06 15:08:33 +0200737static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
Daniel Vetter20afbda2012-12-11 14:05:07 +0100738{
Jesse Barnesd1d70672014-05-28 14:39:03 -0700739 struct drm_i915_private *dev_priv = data;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800740 struct intel_fbdev *ifbdev = dev_priv->fbdev;
Daniel Vetter20afbda2012-12-11 14:05:07 +0100741
742 /* Due to peculiar init order wrt to hpd handling this is separate. */
Lukas Wunner366e39b2015-11-18 16:29:51 +0100743 if (drm_fb_helper_initial_config(&ifbdev->helper,
744 ifbdev->preferred_bpp))
745 intel_fbdev_fini(dev_priv->dev);
Daniel Vetter20afbda2012-12-11 14:05:07 +0100746}
747
Ville Syrjäläe00bf692015-11-06 15:08:33 +0200748void intel_fbdev_initial_config_async(struct drm_device *dev)
749{
750 async_schedule(intel_fbdev_initial_config, to_i915(dev));
751}
752
Dave Airlie38651672010-03-30 05:34:13 +0000753void intel_fbdev_fini(struct drm_device *dev)
754{
Ville Syrjäläb51b32c2013-05-31 20:07:06 +0300755 struct drm_i915_private *dev_priv = dev->dev_private;
Dave Airlie8be48d92010-03-30 05:34:14 +0000756 if (!dev_priv->fbdev)
757 return;
758
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100759 flush_work(&dev_priv->fbdev_suspend_work);
760
Lukas Wunner366e39b2015-11-18 16:29:51 +0100761 if (!current_is_async())
762 async_synchronize_full();
Dave Airlie38651672010-03-30 05:34:13 +0000763 intel_fbdev_destroy(dev, dev_priv->fbdev);
Dave Airlie8be48d92010-03-30 05:34:14 +0000764 kfree(dev_priv->fbdev);
Dave Airlie38651672010-03-30 05:34:13 +0000765 dev_priv->fbdev = NULL;
766}
Dave Airlie3fa016a2012-03-28 10:48:49 +0100767
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100768void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
Dave Airlie3fa016a2012-03-28 10:48:49 +0100769{
Ville Syrjäläb51b32c2013-05-31 20:07:06 +0300770 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula1ffc5282013-05-07 18:54:05 +0300771 struct intel_fbdev *ifbdev = dev_priv->fbdev;
772 struct fb_info *info;
773
774 if (!ifbdev)
Dave Airlie3fa016a2012-03-28 10:48:49 +0100775 return;
776
Jani Nikula1ffc5282013-05-07 18:54:05 +0300777 info = ifbdev->helper.fbdev;
778
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100779 if (synchronous) {
780 /* Flush any pending work to turn the console on, and then
781 * wait to turn it off. It must be synchronous as we are
782 * about to suspend or unload the driver.
783 *
784 * Note that from within the work-handler, we cannot flush
785 * ourselves, so only flush outstanding work upon suspend!
786 */
787 if (state != FBINFO_STATE_RUNNING)
788 flush_work(&dev_priv->fbdev_suspend_work);
789 console_lock();
790 } else {
791 /*
792 * The console lock can be pretty contented on resume due
793 * to all the printk activity. Try to keep it out of the hot
794 * path of resume if possible.
795 */
796 WARN_ON(state != FBINFO_STATE_RUNNING);
797 if (!console_trylock()) {
798 /* Don't block our own workqueue as this can
799 * be run in parallel with other i915.ko tasks.
800 */
801 schedule_work(&dev_priv->fbdev_suspend_work);
802 return;
803 }
804 }
805
Jani Nikula1ffc5282013-05-07 18:54:05 +0300806 /* On resume from hibernation: If the object is shmemfs backed, it has
807 * been restored from swap. If the object is stolen however, it will be
808 * full of whatever garbage was left in there.
809 */
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800810 if (state == FBINFO_STATE_RUNNING && ifbdev->fb->obj->stolen)
Jani Nikula1ffc5282013-05-07 18:54:05 +0300811 memset_io(info->screen_base, 0, info->screen_size);
812
Archit Taneja21cff142015-07-31 16:21:56 +0530813 drm_fb_helper_set_suspend(&ifbdev->helper, state);
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100814 console_unlock();
Dave Airlie3fa016a2012-03-28 10:48:49 +0100815}
816
Daniel Vetter0632fef2013-10-08 17:44:49 +0200817void intel_fbdev_output_poll_changed(struct drm_device *dev)
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000818{
Ville Syrjäläb51b32c2013-05-31 20:07:06 +0300819 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800820 if (dev_priv->fbdev)
821 drm_fb_helper_hotplug_event(&dev_priv->fbdev->helper);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000822}
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100823
Daniel Vetter0632fef2013-10-08 17:44:49 +0200824void intel_fbdev_restore_mode(struct drm_device *dev)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100825{
826 int ret;
Ville Syrjäläb51b32c2013-05-31 20:07:06 +0300827 struct drm_i915_private *dev_priv = dev->dev_private;
Rodrigo Vivid04df732015-07-08 16:25:21 -0700828 struct intel_fbdev *ifbdev = dev_priv->fbdev;
829 struct drm_fb_helper *fb_helper;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100830
Rodrigo Vivid04df732015-07-08 16:25:21 -0700831 if (!ifbdev)
Ben Widawskye3c74752013-04-05 13:12:39 -0700832 return;
833
Rodrigo Vivid04df732015-07-08 16:25:21 -0700834 fb_helper = &ifbdev->helper;
835
836 ret = drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
837 if (ret) {
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100838 DRM_DEBUG("failed to restore crtc mode\n");
Rodrigo Vivid04df732015-07-08 16:25:21 -0700839 } else {
840 mutex_lock(&fb_helper->dev->struct_mutex);
841 intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT);
842 mutex_unlock(&fb_helper->dev->struct_mutex);
843 }
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100844}