blob: 9127e57f383c1005010797e61c97da5ec37664be [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 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)) {
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);
David Weinehall52a05c32016-08-22 13:32:44 +0300186 struct pci_dev *pdev = dev_priv->drm.pdev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300187 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800188 struct fb_info *info;
189 struct drm_framebuffer *fb;
Chris Wilson8ef85612016-04-28 09:56:39 +0100190 struct i915_vma *vma;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800191 bool prealloc = false;
Chris Wilson406ea8d2016-07-20 13:31:55 +0100192 void __iomem *vaddr;
Chris Wilson8ef85612016-04-28 09:56:39 +0100193 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
Tvrtko Ursulin51f13852015-06-30 10:06:27 +0100218 mutex_lock(&dev->struct_mutex);
219
Chris Wilson0c823122015-12-04 16:05:26 +0000220 /* Pin the GGTT vma for our access via info->screen_base.
221 * This also validates that any existing fb inherited from the
222 * BIOS is suitable for own access.
223 */
Chris Wilson058d88c2016-08-15 10:49:06 +0100224 vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base, DRM_ROTATE_0);
225 if (IS_ERR(vma)) {
226 ret = PTR_ERR(vma);
Chris Wilson0c823122015-12-04 16:05:26 +0000227 goto out_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +0100228 }
Chris Wilson0c823122015-12-04 16:05:26 +0000229
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
Dave Airlie4410f392009-06-16 15:34:38 -0700248 /* setup aperture base/size for vesafb takeover */
Marcin Slusarz1471ca92010-05-16 17:27:03 +0200249 info->apertures->ranges[0].base = dev->mode_config.fb_base;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300250 info->apertures->ranges[0].size = ggtt->mappable_end;
Dave Airlie4410f392009-06-16 15:34:38 -0700251
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100252 info->fix.smem_start = dev->mode_config.fb_base + i915_ggtt_offset(vma);
Chris Wilson8ef85612016-04-28 09:56:39 +0100253 info->fix.smem_len = vma->node.size;
Jesse Barnes79e53942008-11-07 14:24:08 -0800254
Chris Wilson8ef85612016-04-28 09:56:39 +0100255 vaddr = i915_vma_pin_iomap(vma);
256 if (IS_ERR(vaddr)) {
Lukas Wunner366e39b2015-11-18 16:29:51 +0100257 DRM_ERROR("Failed to remap framebuffer into virtual memory\n");
Chris Wilson8ef85612016-04-28 09:56:39 +0100258 ret = PTR_ERR(vaddr);
Archit Taneja21cff142015-07-31 16:21:56 +0530259 goto out_destroy_fbi;
Jesse Barnes79e53942008-11-07 14:24:08 -0800260 }
Chris Wilson8ef85612016-04-28 09:56:39 +0100261 info->screen_base = vaddr;
262 info->screen_size = vma->node.size;
Jesse Barnes79e53942008-11-07 14:24:08 -0800263
Jesse Barnes24576d22013-03-26 09:25:45 -0700264 /* This driver doesn't need a VT switch to restore the mode on resume */
265 info->skip_vt_switch = true;
266
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200267 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
Dave Airlie38651672010-03-30 05:34:13 +0000268 drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height);
Jesse Barnes79e53942008-11-07 14:24:08 -0800269
Chris Wilson88afe712012-12-16 12:15:41 +0000270 /* If the object is shmemfs backed, it will have given us zeroed pages.
271 * If the object is stolen however, it will be full of whatever
272 * garbage was left in there.
273 */
Chris Wilson058d88c2016-08-15 10:49:06 +0100274 if (intel_fb->obj->stolen && !prealloc)
Chris Wilson88afe712012-12-16 12:15:41 +0000275 memset_io(info->screen_base, 0, info->screen_size);
276
Sascha Hauerfb2a99e2012-02-06 10:58:19 +0100277 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
Jesse Barnes79e53942008-11-07 14:24:08 -0800278
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100279 DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08x\n",
280 fb->width, fb->height, i915_ggtt_offset(vma));
Chris Wilson058d88c2016-08-15 10:49:06 +0100281 ifbdev->vma = vma;
Jesse Barnes79e53942008-11-07 14:24:08 -0800282
283 mutex_unlock(&dev->struct_mutex);
David Weinehall52a05c32016-08-22 13:32:44 +0300284 vga_switcheroo_client_fb_set(pdev, info);
Jesse Barnes79e53942008-11-07 14:24:08 -0800285 return 0;
286
Archit Taneja21cff142015-07-31 16:21:56 +0530287out_destroy_fbi:
288 drm_fb_helper_release_fbi(helper);
Chris Wilsonb4476f52009-02-11 14:26:36 +0000289out_unpin:
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300290 intel_unpin_fb_obj(&ifbdev->fb->base, DRM_ROTATE_0);
Chris Wilson0c823122015-12-04 16:05:26 +0000291out_unlock:
Jesse Barnes79e53942008-11-07 14:24:08 -0800292 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -0800293 return ret;
294}
295
Paulo Zanoni67437682013-09-24 13:52:56 -0300296/** Sets the color ramps on behalf of RandR */
297static void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
298 u16 blue, int regno)
299{
300 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
301
302 intel_crtc->lut_r[regno] = red >> 8;
303 intel_crtc->lut_g[regno] = green >> 8;
304 intel_crtc->lut_b[regno] = blue >> 8;
305}
306
307static void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
308 u16 *blue, int regno)
309{
310 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
311
312 *red = intel_crtc->lut_r[regno] << 8;
313 *green = intel_crtc->lut_g[regno] << 8;
314 *blue = intel_crtc->lut_b[regno] << 8;
315}
316
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800317static struct drm_fb_helper_crtc *
318intel_fb_helper_crtc(struct drm_fb_helper *fb_helper, struct drm_crtc *crtc)
319{
320 int i;
321
322 for (i = 0; i < fb_helper->crtc_count; i++)
323 if (fb_helper->crtc_info[i].mode_set.crtc == crtc)
324 return &fb_helper->crtc_info[i];
325
326 return NULL;
327}
328
329/*
330 * Try to read the BIOS display configuration and use it for the initial
331 * fb configuration.
332 *
333 * The BIOS or boot loader will generally create an initial display
334 * configuration for us that includes some set of active pipes and displays.
335 * This routine tries to figure out which pipes and connectors are active
336 * and stuffs them into the crtcs and modes array given to us by the
337 * drm_fb_helper code.
338 *
339 * The overall sequence is:
340 * intel_fbdev_init - from driver load
341 * intel_fbdev_init_bios - initialize the intel_fbdev using BIOS data
342 * drm_fb_helper_init - build fb helper structs
343 * drm_fb_helper_single_add_all_connectors - more fb helper structs
344 * intel_fbdev_initial_config - apply the config
345 * drm_fb_helper_initial_config - call ->probe then register_framebuffer()
346 * drm_setup_crtcs - build crtc config for fbdev
347 * intel_fb_initial_config - find active connectors etc
348 * drm_fb_helper_single_fb_probe - set up fbdev
349 * intelfb_create - re-use or alloc fb, build out fbdev structs
350 *
351 * Note that we don't make special consideration whether we could actually
352 * switch to the selected modes without a full modeset. E.g. when the display
353 * is in VGA mode we need to recalculate watermarks and set a new high-res
354 * framebuffer anyway.
355 */
356static bool intel_fb_initial_config(struct drm_fb_helper *fb_helper,
357 struct drm_fb_helper_crtc **crtcs,
358 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000359 struct drm_fb_offset *offsets,
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800360 bool *enabled, int width, int height)
361{
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800362 struct drm_device *dev = fb_helper->dev;
Chris Wilson86f40bb2016-07-02 15:36:02 +0100363 unsigned long conn_configured, mask;
364 unsigned int count = min(fb_helper->connector_count, BITS_PER_LONG);
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 int pass = 0;
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800371
Chris Wilson86f40bb2016-07-02 15:36:02 +0100372 save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL);
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800373 if (!save_enabled)
374 return false;
375
Chris Wilson86f40bb2016-07-02 15:36:02 +0100376 memcpy(save_enabled, enabled, count);
377 mask = BIT(count) - 1;
378 conn_configured = 0;
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000379retry:
Chris Wilson86f40bb2016-07-02 15:36:02 +0100380 for (i = 0; i < count; i++) {
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800381 struct drm_fb_helper_connector *fb_conn;
382 struct drm_connector *connector;
383 struct drm_encoder *encoder;
384 struct drm_fb_helper_crtc *new_crtc;
Lionel Landwerlin82cf4352016-03-16 10:57:16 +0000385 struct intel_crtc *intel_crtc;
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800386
387 fb_conn = fb_helper->connector_info[i];
388 connector = fb_conn->connector;
Daniel Vetter7e696e42014-03-04 21:08:42 +0100389
Chris Wilson86f40bb2016-07-02 15:36:02 +0100390 if (conn_configured & BIT(i))
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000391 continue;
392
393 if (pass == 0 && !connector->has_tile)
394 continue;
395
Daniel Vetter7e696e42014-03-04 21:08:42 +0100396 if (connector->status == connector_status_connected)
397 num_connectors_detected++;
398
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800399 if (!enabled[i]) {
Chris Wilsonc0c97622014-05-13 13:45:09 +0100400 DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300401 connector->name);
Chris Wilson86f40bb2016-07-02 15:36:02 +0100402 conn_configured |= BIT(i);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800403 continue;
404 }
405
Chris Wilsond84a0f32014-08-18 10:35:29 -0700406 if (connector->force == DRM_FORCE_OFF) {
407 DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
408 connector->name);
409 enabled[i] = false;
410 continue;
411 }
412
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100413 encoder = connector->state->best_encoder;
414 if (!encoder || WARN_ON(!connector->state->crtc)) {
Chris Wilsond84a0f32014-08-18 10:35:29 -0700415 if (connector->force > DRM_FORCE_OFF)
416 goto bail;
417
Chris Wilsonc0c97622014-05-13 13:45:09 +0100418 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300419 connector->name);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800420 enabled[i] = false;
Chris Wilson86f40bb2016-07-02 15:36:02 +0100421 conn_configured |= BIT(i);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800422 continue;
423 }
424
Daniel Vetter7e696e42014-03-04 21:08:42 +0100425 num_connectors_enabled++;
426
Lionel Landwerlin82cf4352016-03-16 10:57:16 +0000427 intel_crtc = to_intel_crtc(connector->state->crtc);
428 for (j = 0; j < 256; j++) {
429 intel_crtc->lut_r[j] = j;
430 intel_crtc->lut_g[j] = j;
431 intel_crtc->lut_b[j] = j;
432 }
433
Chris Wilson86f40bb2016-07-02 15:36:02 +0100434 new_crtc = intel_fb_helper_crtc(fb_helper,
435 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 */
Chris Wilson86f40bb2016-07-02 15:36:02 +0100442 for (j = 0; j < 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;
Chris Wilson86f40bb2016-07-02 15:36:02 +0100501 conn_configured |= BIT(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000502 }
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");
Chris Wilson86f40bb2016-07-02 15:36:02 +0100525 memcpy(enabled, save_enabled, count);
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800526 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 Wilson43cee312016-06-21 09:16:54 +0100541static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
Jesse Barnes79e53942008-11-07 14:24:08 -0800542{
Chris Wilson0c823122015-12-04 16:05:26 +0000543 /* We rely on the object-free to release the VMA pinning for
544 * the info->screen_base mmaping. Leaking the VMA is simpler than
545 * trying to rectify all the possible error paths leading here.
546 */
Chris Wilsonddfe1562013-08-06 17:43:07 +0100547
Archit Taneja21cff142015-07-31 16:21:56 +0530548 drm_fb_helper_unregister_fbi(&ifbdev->helper);
549 drm_fb_helper_release_fbi(&ifbdev->helper);
Jesse Barnes79e53942008-11-07 14:24:08 -0800550
Dave Airlie4abe3522010-03-30 05:34:18 +0000551 drm_fb_helper_fini(&ifbdev->helper);
Jesse Barnes79e53942008-11-07 14:24:08 -0800552
Lukas Wunner54632ab2015-11-18 13:43:20 +0100553 if (ifbdev->fb) {
Chris Wilson43cee312016-06-21 09:16:54 +0100554 mutex_lock(&ifbdev->helper.dev->struct_mutex);
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300555 intel_unpin_fb_obj(&ifbdev->fb->base, DRM_ROTATE_0);
Chris Wilson43cee312016-06-21 09:16:54 +0100556 mutex_unlock(&ifbdev->helper.dev->struct_mutex);
Chris Wilsonfb4b8ce2016-04-28 09:56:35 +0100557
Lukas Wunner54632ab2015-11-18 13:43:20 +0100558 drm_framebuffer_remove(&ifbdev->fb->base);
559 }
Chris Wilson43cee312016-06-21 09:16:54 +0100560
561 kfree(ifbdev);
Jesse Barnes79e53942008-11-07 14:24:08 -0800562}
Dave Airlie38651672010-03-30 05:34:13 +0000563
Jesse Barnesd978ef12014-03-07 08:57:51 -0800564/*
565 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
566 * The core display code will have read out the current plane configuration,
567 * so we use that to figure out if there's an object for us to use as the
568 * fb, and if so, we re-use it for the fbdev configuration.
569 *
570 * Note we only support a single fb shared across pipes for boot (mostly for
571 * fbcon), so we just find the biggest and use that.
572 */
573static bool intel_fbdev_init_bios(struct drm_device *dev,
574 struct intel_fbdev *ifbdev)
575{
576 struct intel_framebuffer *fb = NULL;
577 struct drm_crtc *crtc;
578 struct intel_crtc *intel_crtc;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800579 unsigned int max_size = 0;
580
Jesse Barnesd978ef12014-03-07 08:57:51 -0800581 /* Find the largest fb */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100582 for_each_crtc(dev, crtc) {
Maarten Lankhorst8e9ba312015-07-13 16:30:16 +0200583 struct drm_i915_gem_object *obj =
584 intel_fb_obj(crtc->primary->state->fb);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800585 intel_crtc = to_intel_crtc(crtc);
586
Maarten Lankhorstd5515992015-08-27 15:44:03 +0200587 if (!crtc->state->active || !obj) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800588 DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
589 pipe_name(intel_crtc->pipe));
590 continue;
591 }
592
Maarten Lankhorst8e9ba312015-07-13 16:30:16 +0200593 if (obj->base.size > max_size) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800594 DRM_DEBUG_KMS("found possible fb from plane %c\n",
595 pipe_name(intel_crtc->pipe));
Maarten Lankhorst8e9ba312015-07-13 16:30:16 +0200596 fb = to_intel_framebuffer(crtc->primary->state->fb);
597 max_size = obj->base.size;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800598 }
599 }
600
601 if (!fb) {
602 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
603 goto out;
604 }
605
606 /* Now make sure all the pipes will fit into it */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100607 for_each_crtc(dev, crtc) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800608 unsigned int cur_size;
609
610 intel_crtc = to_intel_crtc(crtc);
611
Maarten Lankhorstd5515992015-08-27 15:44:03 +0200612 if (!crtc->state->active) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800613 DRM_DEBUG_KMS("pipe %c not active, skipping\n",
614 pipe_name(intel_crtc->pipe));
615 continue;
616 }
617
618 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
619 pipe_name(intel_crtc->pipe));
620
621 /*
622 * See if the plane fb we found above will fit on this
Chris Wilsonbc104d12014-03-20 15:11:21 +0000623 * pipe. Note we need to use the selected fb's pitch and bpp
624 * rather than the current pipe's, since they differ.
Jesse Barnesd978ef12014-03-07 08:57:51 -0800625 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200626 cur_size = intel_crtc->config->base.adjusted_mode.crtc_hdisplay;
Chris Wilsonbc104d12014-03-20 15:11:21 +0000627 cur_size = cur_size * fb->base.bits_per_pixel / 8;
628 if (fb->base.pitches[0] < cur_size) {
629 DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
630 pipe_name(intel_crtc->pipe),
631 cur_size, fb->base.pitches[0]);
Chris Wilsonbc104d12014-03-20 15:11:21 +0000632 fb = NULL;
633 break;
634 }
635
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200636 cur_size = intel_crtc->config->base.adjusted_mode.crtc_vdisplay;
Damien Lespiauec2c9812015-01-20 12:51:45 +0000637 cur_size = intel_fb_align_height(dev, cur_size,
Daniel Vetter091df6c2015-02-10 17:16:10 +0000638 fb->base.pixel_format,
639 fb->base.modifier[0]);
Chris Wilsonbc104d12014-03-20 15:11:21 +0000640 cur_size *= fb->base.pitches[0];
641 DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
642 pipe_name(intel_crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200643 intel_crtc->config->base.adjusted_mode.crtc_hdisplay,
644 intel_crtc->config->base.adjusted_mode.crtc_vdisplay,
Chris Wilsonbc104d12014-03-20 15:11:21 +0000645 fb->base.bits_per_pixel,
Jesse Barnesd978ef12014-03-07 08:57:51 -0800646 cur_size);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800647
648 if (cur_size > max_size) {
649 DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
650 pipe_name(intel_crtc->pipe),
651 cur_size, max_size);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800652 fb = NULL;
653 break;
654 }
655
656 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
657 pipe_name(intel_crtc->pipe),
658 max_size, cur_size);
659 }
660
Jesse Barnesd978ef12014-03-07 08:57:51 -0800661 if (!fb) {
662 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
663 goto out;
664 }
665
Jesse Barnes484b41d2014-03-07 08:57:55 -0800666 ifbdev->preferred_bpp = fb->base.bits_per_pixel;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800667 ifbdev->fb = fb;
668
Jesse Barnes484b41d2014-03-07 08:57:55 -0800669 drm_framebuffer_reference(&ifbdev->fb->base);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800670
671 /* Final pass to check if any active pipes don't have fbs */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100672 for_each_crtc(dev, crtc) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800673 intel_crtc = to_intel_crtc(crtc);
674
Maarten Lankhorstd5515992015-08-27 15:44:03 +0200675 if (!crtc->state->active)
Jesse Barnesd978ef12014-03-07 08:57:51 -0800676 continue;
677
Dave Airlie66e514c2014-04-03 07:51:54 +1000678 WARN(!crtc->primary->fb,
Jesse Barnesd978ef12014-03-07 08:57:51 -0800679 "re-used BIOS config but lost an fb on crtc %d\n",
680 crtc->base.id);
681 }
682
683
684 DRM_DEBUG_KMS("using BIOS fb for initial console\n");
685 return true;
686
Jesse Barnesd978ef12014-03-07 08:57:51 -0800687out:
688
689 return false;
690}
691
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100692static void intel_fbdev_suspend_worker(struct work_struct *work)
693{
Chris Wilson91c8a322016-07-05 10:40:23 +0100694 intel_fbdev_set_suspend(&container_of(work,
695 struct drm_i915_private,
696 fbdev_suspend_work)->drm,
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100697 FBINFO_STATE_RUNNING,
698 true);
699}
700
Dave Airlie38651672010-03-30 05:34:13 +0000701int intel_fbdev_init(struct drm_device *dev)
702{
Dave Airlie8be48d92010-03-30 05:34:14 +0000703 struct intel_fbdev *ifbdev;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100704 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson5a793952010-06-06 10:50:03 +0100705 int ret;
Dave Airlie8be48d92010-03-30 05:34:14 +0000706
Jesse Barnesd978ef12014-03-07 08:57:51 -0800707 if (WARN_ON(INTEL_INFO(dev)->num_pipes == 0))
708 return -ENODEV;
709
710 ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
711 if (ifbdev == NULL)
Dave Airlie8be48d92010-03-30 05:34:14 +0000712 return -ENOMEM;
713
Thierry Reding10a23102014-06-27 17:19:24 +0200714 drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
715
Jesse Barnesd978ef12014-03-07 08:57:51 -0800716 if (!intel_fbdev_init_bios(dev, ifbdev))
717 ifbdev->preferred_bpp = 32;
Dave Airlie8be48d92010-03-30 05:34:14 +0000718
Chris Wilson5a793952010-06-06 10:50:03 +0100719 ret = drm_fb_helper_init(dev, &ifbdev->helper,
Jesse Barnesd978ef12014-03-07 08:57:51 -0800720 INTEL_INFO(dev)->num_pipes, 4);
Chris Wilson5a793952010-06-06 10:50:03 +0100721 if (ret) {
722 kfree(ifbdev);
723 return ret;
724 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000725
Jesse Barnesd978ef12014-03-07 08:57:51 -0800726 dev_priv->fbdev = ifbdev;
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100727 INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
728
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000729 drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
Daniel Vetter20afbda2012-12-11 14:05:07 +0100730
Dave Airlie38651672010-03-30 05:34:13 +0000731 return 0;
732}
733
Ville Syrjäläe00bf692015-11-06 15:08:33 +0200734static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
Daniel Vetter20afbda2012-12-11 14:05:07 +0100735{
Chris Wilson43cee312016-06-21 09:16:54 +0100736 struct intel_fbdev *ifbdev = data;
Daniel Vetter20afbda2012-12-11 14:05:07 +0100737
738 /* Due to peculiar init order wrt to hpd handling this is separate. */
Lukas Wunner366e39b2015-11-18 16:29:51 +0100739 if (drm_fb_helper_initial_config(&ifbdev->helper,
740 ifbdev->preferred_bpp))
Chris Wilson43cee312016-06-21 09:16:54 +0100741 intel_fbdev_fini(ifbdev->helper.dev);
Daniel Vetter20afbda2012-12-11 14:05:07 +0100742}
743
Ville Syrjäläe00bf692015-11-06 15:08:33 +0200744void intel_fbdev_initial_config_async(struct drm_device *dev)
745{
Chris Wilson43cee312016-06-21 09:16:54 +0100746 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
747
Clint Taylor2cff6782017-01-18 13:38:43 -0800748 if (!ifbdev)
749 return;
750
Chris Wilson43cee312016-06-21 09:16:54 +0100751 ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
752}
753
754static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
755{
756 if (!ifbdev->cookie)
757 return;
758
759 /* Only serialises with all preceding async calls, hence +1 */
760 async_synchronize_cookie(ifbdev->cookie + 1);
761 ifbdev->cookie = 0;
Ville Syrjäläe00bf692015-11-06 15:08:33 +0200762}
763
Dave Airlie38651672010-03-30 05:34:13 +0000764void intel_fbdev_fini(struct drm_device *dev)
765{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100766 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson43cee312016-06-21 09:16:54 +0100767 struct intel_fbdev *ifbdev = dev_priv->fbdev;
768
769 if (!ifbdev)
Dave Airlie8be48d92010-03-30 05:34:14 +0000770 return;
771
Chris Wilson0b8c0e92016-07-13 18:34:44 +0100772 cancel_work_sync(&dev_priv->fbdev_suspend_work);
Lukas Wunner366e39b2015-11-18 16:29:51 +0100773 if (!current_is_async())
Chris Wilson43cee312016-06-21 09:16:54 +0100774 intel_fbdev_sync(ifbdev);
775
776 intel_fbdev_destroy(ifbdev);
Dave Airlie38651672010-03-30 05:34:13 +0000777 dev_priv->fbdev = NULL;
778}
Dave Airlie3fa016a2012-03-28 10:48:49 +0100779
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100780void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
Dave Airlie3fa016a2012-03-28 10:48:49 +0100781{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100782 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula1ffc5282013-05-07 18:54:05 +0300783 struct intel_fbdev *ifbdev = dev_priv->fbdev;
784 struct fb_info *info;
785
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100786 if (!ifbdev || !ifbdev->fb)
Dave Airlie3fa016a2012-03-28 10:48:49 +0100787 return;
788
Jani Nikula1ffc5282013-05-07 18:54:05 +0300789 info = ifbdev->helper.fbdev;
790
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100791 if (synchronous) {
792 /* Flush any pending work to turn the console on, and then
793 * wait to turn it off. It must be synchronous as we are
794 * about to suspend or unload the driver.
795 *
796 * Note that from within the work-handler, we cannot flush
797 * ourselves, so only flush outstanding work upon suspend!
798 */
799 if (state != FBINFO_STATE_RUNNING)
800 flush_work(&dev_priv->fbdev_suspend_work);
801 console_lock();
802 } else {
803 /*
804 * The console lock can be pretty contented on resume due
805 * to all the printk activity. Try to keep it out of the hot
806 * path of resume if possible.
807 */
808 WARN_ON(state != FBINFO_STATE_RUNNING);
809 if (!console_trylock()) {
810 /* Don't block our own workqueue as this can
811 * be run in parallel with other i915.ko tasks.
812 */
813 schedule_work(&dev_priv->fbdev_suspend_work);
814 return;
815 }
816 }
817
Jani Nikula1ffc5282013-05-07 18:54:05 +0300818 /* On resume from hibernation: If the object is shmemfs backed, it has
819 * been restored from swap. If the object is stolen however, it will be
820 * full of whatever garbage was left in there.
821 */
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800822 if (state == FBINFO_STATE_RUNNING && ifbdev->fb->obj->stolen)
Jani Nikula1ffc5282013-05-07 18:54:05 +0300823 memset_io(info->screen_base, 0, info->screen_size);
824
Archit Taneja21cff142015-07-31 16:21:56 +0530825 drm_fb_helper_set_suspend(&ifbdev->helper, state);
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100826 console_unlock();
Dave Airlie3fa016a2012-03-28 10:48:49 +0100827}
828
Daniel Vetter0632fef2013-10-08 17:44:49 +0200829void intel_fbdev_output_poll_changed(struct drm_device *dev)
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000830{
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100831 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
832
833 if (ifbdev && ifbdev->fb)
834 drm_fb_helper_hotplug_event(&ifbdev->helper);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000835}
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100836
Daniel Vetter0632fef2013-10-08 17:44:49 +0200837void intel_fbdev_restore_mode(struct drm_device *dev)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100838{
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100839 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100840
Rodrigo Vivid04df732015-07-08 16:25:21 -0700841 if (!ifbdev)
Ben Widawskye3c74752013-04-05 13:12:39 -0700842 return;
843
Chris Wilsone77018f2016-06-21 09:16:55 +0100844 intel_fbdev_sync(ifbdev);
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100845 if (!ifbdev->fb)
846 return;
Chris Wilsone77018f2016-06-21 09:16:55 +0100847
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100848 if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper)) {
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100849 DRM_DEBUG("failed to restore crtc mode\n");
Rodrigo Vivid04df732015-07-08 16:25:21 -0700850 } else {
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100851 mutex_lock(&dev->struct_mutex);
Rodrigo Vivid04df732015-07-08 16:25:21 -0700852 intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT);
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100853 mutex_unlock(&dev->struct_mutex);
Rodrigo Vivid04df732015-07-08 16:25:21 -0700854 }
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100855}