blob: 6f12adc063650c1ad4f696922dee1215a98398c0 [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
Chris Wilsonfabef822017-02-15 10:59:19 +000048static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev)
49{
50 struct drm_i915_gem_object *obj = ifbdev->fb->obj;
Chris Wilsone3c017f2018-02-20 13:42:07 +000051 unsigned int origin =
52 ifbdev->vma_flags & PLANE_HAS_FENCE ? ORIGIN_GTT : ORIGIN_CPU;
Chris Wilsonfabef822017-02-15 10:59:19 +000053
54 intel_fb_obj_invalidate(obj, origin);
55}
56
Daniel Vettere9910772014-06-18 15:05:19 +020057static int intel_fbdev_set_par(struct fb_info *info)
58{
59 struct drm_fb_helper *fb_helper = info->par;
60 struct intel_fbdev *ifbdev =
61 container_of(fb_helper, struct intel_fbdev, helper);
62 int ret;
63
64 ret = drm_fb_helper_set_par(info);
Chris Wilsonfabef822017-02-15 10:59:19 +000065 if (ret == 0)
66 intel_fbdev_invalidate(ifbdev);
Daniel Vettere9910772014-06-18 15:05:19 +020067
68 return ret;
69}
70
Rodrigo Vivi03e515f2015-03-09 17:57:07 -070071static int intel_fbdev_blank(int blank, struct fb_info *info)
72{
73 struct drm_fb_helper *fb_helper = info->par;
74 struct intel_fbdev *ifbdev =
75 container_of(fb_helper, struct intel_fbdev, helper);
76 int ret;
77
78 ret = drm_fb_helper_blank(blank, info);
Chris Wilsonfabef822017-02-15 10:59:19 +000079 if (ret == 0)
80 intel_fbdev_invalidate(ifbdev);
Rodrigo Vivi03e515f2015-03-09 17:57:07 -070081
82 return ret;
83}
84
Rodrigo Vivid9a946b2015-05-28 10:26:58 -070085static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
86 struct fb_info *info)
87{
88 struct drm_fb_helper *fb_helper = info->par;
89 struct intel_fbdev *ifbdev =
90 container_of(fb_helper, struct intel_fbdev, helper);
Rodrigo Vivid9a946b2015-05-28 10:26:58 -070091 int ret;
Rodrigo Vivid9a946b2015-05-28 10:26:58 -070092
Chris Wilsonfabef822017-02-15 10:59:19 +000093 ret = drm_fb_helper_pan_display(var, info);
94 if (ret == 0)
95 intel_fbdev_invalidate(ifbdev);
Rodrigo Vivid9a946b2015-05-28 10:26:58 -070096
97 return ret;
98}
99
Jesse Barnes79e53942008-11-07 14:24:08 -0800100static struct fb_ops intelfb_ops = {
101 .owner = THIS_MODULE,
Stefan Christa36384d2016-11-14 00:03:27 +0100102 DRM_FB_HELPER_DEFAULT_OPS,
Daniel Vettere9910772014-06-18 15:05:19 +0200103 .fb_set_par = intel_fbdev_set_par,
Archit Taneja21cff142015-07-31 16:21:56 +0530104 .fb_fillrect = drm_fb_helper_cfb_fillrect,
105 .fb_copyarea = drm_fb_helper_cfb_copyarea,
106 .fb_imageblit = drm_fb_helper_cfb_imageblit,
Rodrigo Vivid9a946b2015-05-28 10:26:58 -0700107 .fb_pan_display = intel_fbdev_pan_display,
Rodrigo Vivi03e515f2015-03-09 17:57:07 -0700108 .fb_blank = intel_fbdev_blank,
Jesse Barnes79e53942008-11-07 14:24:08 -0800109};
110
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800111static int intelfb_alloc(struct drm_fb_helper *helper,
112 struct drm_fb_helper_surface_size *sizes)
Jesse Barnes79e53942008-11-07 14:24:08 -0800113{
Ville Syrjälä34308242013-05-31 20:07:05 +0300114 struct intel_fbdev *ifbdev =
115 container_of(helper, struct intel_fbdev, helper);
Lukas Wunner4601b932015-12-19 15:40:39 +0100116 struct drm_framebuffer *fb;
Ville Syrjälä34308242013-05-31 20:07:05 +0300117 struct drm_device *dev = helper->dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -0300118 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä3a7f2f62012-05-24 21:08:56 +0300119 struct drm_mode_fb_cmd2 mode_cmd = {};
Chris Wilson24dbf512017-02-15 10:59:18 +0000120 struct drm_i915_gem_object *obj;
James Simmons57084d02010-12-20 19:10:39 +0000121 int size, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -0800122
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000123 /* we don't do packed 24bpp */
Dave Airlie38651672010-03-30 05:34:13 +0000124 if (sizes->surface_bpp == 24)
125 sizes->surface_bpp = 32;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000126
Dave Airlie38651672010-03-30 05:34:13 +0000127 mode_cmd.width = sizes->surface_width;
128 mode_cmd.height = sizes->surface_height;
Jesse Barnes79e53942008-11-07 14:24:08 -0800129
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300130 mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
131 DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800132 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
133 sizes->surface_depth);
Jesse Barnes79e53942008-11-07 14:24:08 -0800134
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800135 size = mode_cmd.pitches[0] * mode_cmd.height;
Fabian Frederick1267a262014-07-01 20:39:41 +0200136 size = PAGE_ALIGN(size);
Paulo Zanoni3badb492015-09-23 12:52:23 -0300137
138 /* If the FB is too big, just don't use it since fbdev is not very
139 * important and we should probably use that space with FBC or other
140 * features. */
Chris Wilson24dbf512017-02-15 10:59:18 +0000141 obj = NULL;
Matthew Auldb1ace602017-12-11 15:18:21 +0000142 if (size * 2 < dev_priv->stolen_usable_size)
Tvrtko Ursulin187685c2016-12-01 14:16:36 +0000143 obj = i915_gem_object_create_stolen(dev_priv, size);
Chris Wilson0ffb0ff2012-11-15 11:32:27 +0000144 if (obj == NULL)
Tvrtko Ursulin12d79d72016-12-01 14:16:37 +0000145 obj = i915_gem_object_create(dev_priv, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100146 if (IS_ERR(obj)) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700147 DRM_ERROR("failed to allocate framebuffer\n");
Chris Wilsonfe3db792016-04-25 13:32:13 +0100148 ret = PTR_ERR(obj);
Chris Wilson24dbf512017-02-15 10:59:18 +0000149 goto err;
Jesse Barnes79e53942008-11-07 14:24:08 -0800150 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800151
Chris Wilson24dbf512017-02-15 10:59:18 +0000152 fb = intel_framebuffer_create(obj, &mode_cmd);
Daniel Vettera8bb6812014-02-10 18:00:39 +0100153 if (IS_ERR(fb)) {
154 ret = PTR_ERR(fb);
Chris Wilson24dbf512017-02-15 10:59:18 +0000155 goto err_obj;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +0000156 }
157
Daniel Vettera8bb6812014-02-10 18:00:39 +0100158 ifbdev->fb = to_intel_framebuffer(fb);
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800159
160 return 0;
161
Chris Wilson24dbf512017-02-15 10:59:18 +0000162err_obj:
163 i915_gem_object_put(obj);
164err:
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800165 return ret;
166}
167
168static int intelfb_create(struct drm_fb_helper *helper,
169 struct drm_fb_helper_surface_size *sizes)
170{
171 struct intel_fbdev *ifbdev =
172 container_of(helper, struct intel_fbdev, helper);
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800173 struct intel_framebuffer *intel_fb = ifbdev->fb;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800174 struct drm_device *dev = helper->dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300175 struct drm_i915_private *dev_priv = to_i915(dev);
David Weinehall52a05c32016-08-22 13:32:44 +0300176 struct pci_dev *pdev = dev_priv->drm.pdev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300177 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800178 struct fb_info *info;
179 struct drm_framebuffer *fb;
Chris Wilson8ef85612016-04-28 09:56:39 +0100180 struct i915_vma *vma;
Chris Wilson59354852018-02-20 13:42:06 +0000181 unsigned long flags = 0;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800182 bool prealloc = false;
Chris Wilson406ea8d2016-07-20 13:31:55 +0100183 void __iomem *vaddr;
Chris Wilson8ef85612016-04-28 09:56:39 +0100184 int ret;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800185
Chris Wilsonedd586f2014-04-23 08:54:31 +0100186 if (intel_fb &&
187 (sizes->fb_width > intel_fb->base.width ||
188 sizes->fb_height > intel_fb->base.height)) {
189 DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d),"
190 " releasing it\n",
191 intel_fb->base.width, intel_fb->base.height,
192 sizes->fb_width, sizes->fb_height);
Harsha Sharmac3ed1102017-10-09 17:36:43 +0530193 drm_framebuffer_put(&intel_fb->base);
Chris Wilsonedd586f2014-04-23 08:54:31 +0100194 intel_fb = ifbdev->fb = NULL;
195 }
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800196 if (!intel_fb || WARN_ON(!intel_fb->obj)) {
Jesse Barnes48e92122013-11-26 11:25:54 -0800197 DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800198 ret = intelfb_alloc(helper, sizes);
199 if (ret)
Tvrtko Ursulin51f13852015-06-30 10:06:27 +0100200 return ret;
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800201 intel_fb = ifbdev->fb;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800202 } else {
Jesse Barnes48e92122013-11-26 11:25:54 -0800203 DRM_DEBUG_KMS("re-using BIOS fb\n");
Jesse Barnesd978ef12014-03-07 08:57:51 -0800204 prealloc = true;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800205 sizes->fb_width = intel_fb->base.width;
206 sizes->fb_height = intel_fb->base.height;
207 }
208
Tvrtko Ursulin51f13852015-06-30 10:06:27 +0100209 mutex_lock(&dev->struct_mutex);
Ville Syrjäläc6364232017-09-01 22:54:56 +0300210 intel_runtime_pm_get(dev_priv);
Tvrtko Ursulin51f13852015-06-30 10:06:27 +0100211
Chris Wilson0c823122015-12-04 16:05:26 +0000212 /* Pin the GGTT vma for our access via info->screen_base.
213 * This also validates that any existing fb inherited from the
214 * BIOS is suitable for own access.
215 */
Chris Wilson59354852018-02-20 13:42:06 +0000216 vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base,
217 DRM_MODE_ROTATE_0,
Ville Syrjäläf7a02ad2018-02-21 20:48:07 +0200218 false, &flags);
Chris Wilson058d88c2016-08-15 10:49:06 +0100219 if (IS_ERR(vma)) {
220 ret = PTR_ERR(vma);
Chris Wilson0c823122015-12-04 16:05:26 +0000221 goto out_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +0100222 }
Chris Wilson0c823122015-12-04 16:05:26 +0000223
Archit Taneja21cff142015-07-31 16:21:56 +0530224 info = drm_fb_helper_alloc_fbi(helper);
225 if (IS_ERR(info)) {
Lukas Wunner366e39b2015-11-18 16:29:51 +0100226 DRM_ERROR("Failed to allocate fb_info\n");
Archit Taneja21cff142015-07-31 16:21:56 +0530227 ret = PTR_ERR(info);
Chris Wilsonb4476f52009-02-11 14:26:36 +0000228 goto out_unpin;
Jesse Barnes79e53942008-11-07 14:24:08 -0800229 }
230
Ville Syrjälä34308242013-05-31 20:07:05 +0300231 info->par = helper;
Jesse Barnes79e53942008-11-07 14:24:08 -0800232
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800233 fb = &ifbdev->fb->base;
Dave Airlie38651672010-03-30 05:34:13 +0000234
235 ifbdev->helper.fb = fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000236
Jesse Barnes79e53942008-11-07 14:24:08 -0800237 strcpy(info->fix.id, "inteldrmfb");
Jesse Barnes79e53942008-11-07 14:24:08 -0800238
Jesse Barnes79e53942008-11-07 14:24:08 -0800239 info->fbops = &intelfb_ops;
240
Dave Airlie4410f392009-06-16 15:34:38 -0700241 /* setup aperture base/size for vesafb takeover */
Marcin Slusarz1471ca92010-05-16 17:27:03 +0200242 info->apertures->ranges[0].base = dev->mode_config.fb_base;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300243 info->apertures->ranges[0].size = ggtt->mappable_end;
Dave Airlie4410f392009-06-16 15:34:38 -0700244
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100245 info->fix.smem_start = dev->mode_config.fb_base + i915_ggtt_offset(vma);
Chris Wilson8ef85612016-04-28 09:56:39 +0100246 info->fix.smem_len = vma->node.size;
Jesse Barnes79e53942008-11-07 14:24:08 -0800247
Chris Wilson8ef85612016-04-28 09:56:39 +0100248 vaddr = i915_vma_pin_iomap(vma);
249 if (IS_ERR(vaddr)) {
Lukas Wunner366e39b2015-11-18 16:29:51 +0100250 DRM_ERROR("Failed to remap framebuffer into virtual memory\n");
Chris Wilson8ef85612016-04-28 09:56:39 +0100251 ret = PTR_ERR(vaddr);
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100252 goto out_unpin;
Jesse Barnes79e53942008-11-07 14:24:08 -0800253 }
Chris Wilson8ef85612016-04-28 09:56:39 +0100254 info->screen_base = vaddr;
255 info->screen_size = vma->node.size;
Jesse Barnes79e53942008-11-07 14:24:08 -0800256
Jesse Barnes24576d22013-03-26 09:25:45 -0700257 /* This driver doesn't need a VT switch to restore the mode on resume */
258 info->skip_vt_switch = true;
259
Ville Syrjäläb00c6002016-12-14 23:31:35 +0200260 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
Dave Airlie38651672010-03-30 05:34:13 +0000261 drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height);
Jesse Barnes79e53942008-11-07 14:24:08 -0800262
Chris Wilson88afe712012-12-16 12:15:41 +0000263 /* If the object is shmemfs backed, it will have given us zeroed pages.
264 * If the object is stolen however, it will be full of whatever
265 * garbage was left in there.
266 */
Chris Wilson058d88c2016-08-15 10:49:06 +0100267 if (intel_fb->obj->stolen && !prealloc)
Chris Wilson88afe712012-12-16 12:15:41 +0000268 memset_io(info->screen_base, 0, info->screen_size);
269
Sascha Hauerfb2a99e2012-02-06 10:58:19 +0100270 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
Jesse Barnes79e53942008-11-07 14:24:08 -0800271
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100272 DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08x\n",
273 fb->width, fb->height, i915_ggtt_offset(vma));
Chris Wilson058d88c2016-08-15 10:49:06 +0100274 ifbdev->vma = vma;
Chris Wilson59354852018-02-20 13:42:06 +0000275 ifbdev->vma_flags = flags;
Jesse Barnes79e53942008-11-07 14:24:08 -0800276
Ville Syrjäläc6364232017-09-01 22:54:56 +0300277 intel_runtime_pm_put(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -0800278 mutex_unlock(&dev->struct_mutex);
David Weinehall52a05c32016-08-22 13:32:44 +0300279 vga_switcheroo_client_fb_set(pdev, info);
Jesse Barnes79e53942008-11-07 14:24:08 -0800280 return 0;
281
Chris Wilsonb4476f52009-02-11 14:26:36 +0000282out_unpin:
Chris Wilson59354852018-02-20 13:42:06 +0000283 intel_unpin_fb_vma(vma, flags);
Chris Wilson0c823122015-12-04 16:05:26 +0000284out_unlock:
Ville Syrjäläc6364232017-09-01 22:54:56 +0300285 intel_runtime_pm_put(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -0800286 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -0800287 return ret;
288}
289
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800290static struct drm_fb_helper_crtc *
291intel_fb_helper_crtc(struct drm_fb_helper *fb_helper, struct drm_crtc *crtc)
292{
293 int i;
294
295 for (i = 0; i < fb_helper->crtc_count; i++)
296 if (fb_helper->crtc_info[i].mode_set.crtc == crtc)
297 return &fb_helper->crtc_info[i];
298
299 return NULL;
300}
301
302/*
303 * Try to read the BIOS display configuration and use it for the initial
304 * fb configuration.
305 *
306 * The BIOS or boot loader will generally create an initial display
307 * configuration for us that includes some set of active pipes and displays.
308 * This routine tries to figure out which pipes and connectors are active
309 * and stuffs them into the crtcs and modes array given to us by the
310 * drm_fb_helper code.
311 *
312 * The overall sequence is:
313 * intel_fbdev_init - from driver load
314 * intel_fbdev_init_bios - initialize the intel_fbdev using BIOS data
315 * drm_fb_helper_init - build fb helper structs
316 * drm_fb_helper_single_add_all_connectors - more fb helper structs
317 * intel_fbdev_initial_config - apply the config
318 * drm_fb_helper_initial_config - call ->probe then register_framebuffer()
319 * drm_setup_crtcs - build crtc config for fbdev
320 * intel_fb_initial_config - find active connectors etc
321 * drm_fb_helper_single_fb_probe - set up fbdev
322 * intelfb_create - re-use or alloc fb, build out fbdev structs
323 *
324 * Note that we don't make special consideration whether we could actually
325 * switch to the selected modes without a full modeset. E.g. when the display
326 * is in VGA mode we need to recalculate watermarks and set a new high-res
327 * framebuffer anyway.
328 */
329static bool intel_fb_initial_config(struct drm_fb_helper *fb_helper,
330 struct drm_fb_helper_crtc **crtcs,
331 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000332 struct drm_fb_offset *offsets,
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800333 bool *enabled, int width, int height)
334{
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +0000335 struct drm_i915_private *dev_priv = to_i915(fb_helper->dev);
Chris Wilson754a7652017-02-24 11:43:06 +0000336 unsigned long conn_configured, conn_seq, mask;
Chris Wilson86f40bb2016-07-02 15:36:02 +0100337 unsigned int count = min(fb_helper->connector_count, BITS_PER_LONG);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800338 int i, j;
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800339 bool *save_enabled;
Daniel Vettere13a0582017-07-05 06:56:29 +0200340 bool fallback = true, ret = true;
Daniel Vetter7e696e42014-03-04 21:08:42 +0100341 int num_connectors_enabled = 0;
342 int num_connectors_detected = 0;
Daniel Vettere13a0582017-07-05 06:56:29 +0200343 struct drm_modeset_acquire_ctx ctx;
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800344
Chris Wilson86f40bb2016-07-02 15:36:02 +0100345 save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL);
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800346 if (!save_enabled)
347 return false;
348
Daniel Vettere13a0582017-07-05 06:56:29 +0200349 drm_modeset_acquire_init(&ctx, 0);
350
351 while (drm_modeset_lock_all_ctx(fb_helper->dev, &ctx) != 0)
352 drm_modeset_backoff(&ctx);
353
Chris Wilson86f40bb2016-07-02 15:36:02 +0100354 memcpy(save_enabled, enabled, count);
Joonas Lahtinen3c779a42017-02-08 15:12:09 +0200355 mask = GENMASK(count - 1, 0);
Chris Wilson86f40bb2016-07-02 15:36:02 +0100356 conn_configured = 0;
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000357retry:
Chris Wilson754a7652017-02-24 11:43:06 +0000358 conn_seq = conn_configured;
Chris Wilson86f40bb2016-07-02 15:36:02 +0100359 for (i = 0; i < count; i++) {
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800360 struct drm_fb_helper_connector *fb_conn;
361 struct drm_connector *connector;
362 struct drm_encoder *encoder;
363 struct drm_fb_helper_crtc *new_crtc;
364
365 fb_conn = fb_helper->connector_info[i];
366 connector = fb_conn->connector;
Daniel Vetter7e696e42014-03-04 21:08:42 +0100367
Chris Wilson86f40bb2016-07-02 15:36:02 +0100368 if (conn_configured & BIT(i))
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000369 continue;
370
Chris Wilson754a7652017-02-24 11:43:06 +0000371 if (conn_seq == 0 && !connector->has_tile)
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000372 continue;
373
Daniel Vetter7e696e42014-03-04 21:08:42 +0100374 if (connector->status == connector_status_connected)
375 num_connectors_detected++;
376
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800377 if (!enabled[i]) {
Chris Wilsonc0c97622014-05-13 13:45:09 +0100378 DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300379 connector->name);
Chris Wilson86f40bb2016-07-02 15:36:02 +0100380 conn_configured |= BIT(i);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800381 continue;
382 }
383
Chris Wilsond84a0f32014-08-18 10:35:29 -0700384 if (connector->force == DRM_FORCE_OFF) {
385 DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
386 connector->name);
387 enabled[i] = false;
388 continue;
389 }
390
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100391 encoder = connector->state->best_encoder;
392 if (!encoder || WARN_ON(!connector->state->crtc)) {
Chris Wilsond84a0f32014-08-18 10:35:29 -0700393 if (connector->force > DRM_FORCE_OFF)
394 goto bail;
395
Chris Wilsonc0c97622014-05-13 13:45:09 +0100396 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300397 connector->name);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800398 enabled[i] = false;
Chris Wilson86f40bb2016-07-02 15:36:02 +0100399 conn_configured |= BIT(i);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800400 continue;
401 }
402
Daniel Vetter7e696e42014-03-04 21:08:42 +0100403 num_connectors_enabled++;
404
Chris Wilson86f40bb2016-07-02 15:36:02 +0100405 new_crtc = intel_fb_helper_crtc(fb_helper,
406 connector->state->crtc);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800407
408 /*
409 * Make sure we're not trying to drive multiple connectors
410 * with a single CRTC, since our cloning support may not
411 * match the BIOS.
412 */
Chris Wilson86f40bb2016-07-02 15:36:02 +0100413 for (j = 0; j < count; j++) {
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800414 if (crtcs[j] == new_crtc) {
Daniel Vetter7e696e42014-03-04 21:08:42 +0100415 DRM_DEBUG_KMS("fallback: cloned configuration\n");
Chris Wilsond84a0f32014-08-18 10:35:29 -0700416 goto bail;
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800417 }
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800418 }
419
Chris Wilsonc0c97622014-05-13 13:45:09 +0100420 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300421 connector->name);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800422
423 /* go for command line mode first */
Vincent Abrioua09759e2017-01-06 17:44:43 +0100424 modes[i] = drm_pick_cmdline_mode(fb_conn);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800425
426 /* try for preferred next */
427 if (!modes[i]) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000428 DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
429 connector->name, connector->has_tile);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800430 modes[i] = drm_has_preferred_mode(fb_conn, width,
431 height);
432 }
433
Chris Wilsonafba0b52014-05-13 16:07:37 +0100434 /* No preferred mode marked by the EDID? Are there any modes? */
435 if (!modes[i] && !list_empty(&connector->modes)) {
436 DRM_DEBUG_KMS("using first mode listed on connector %s\n",
Dave Airlie8d4ad9d2014-06-05 20:28:59 +1000437 connector->name);
Chris Wilsonafba0b52014-05-13 16:07:37 +0100438 modes[i] = list_first_entry(&connector->modes,
439 struct drm_display_mode,
440 head);
441 }
442
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800443 /* last resort: use current mode */
444 if (!modes[i]) {
445 /*
446 * IMPORTANT: We want to use the adjusted mode (i.e.
447 * after the panel fitter upscaling) as the initial
448 * config, not the input mode, which is what crtc->mode
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +0200449 * usually contains. But since our current
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800450 * code puts a mode derived from the post-pfit timings
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +0200451 * into crtc->mode this works out correctly.
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100452 *
453 * This is crtc->mode and not crtc->state->mode for the
454 * fastboot check to work correctly. crtc_state->mode has
455 * I915_MODE_FLAG_INHERITED, which we clear to force check
456 * state.
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800457 */
Chris Wilsonc0c97622014-05-13 13:45:09 +0100458 DRM_DEBUG_KMS("looking for current mode on connector %s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300459 connector->name);
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100460 modes[i] = &connector->state->crtc->mode;
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800461 }
462 crtcs[i] = new_crtc;
463
Ville Syrjälä78108b72016-05-27 20:59:19 +0300464 DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300465 connector->name,
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100466 connector->state->crtc->base.id,
Ville Syrjälä78108b72016-05-27 20:59:19 +0300467 connector->state->crtc->name,
Chris Wilsonc0c97622014-05-13 13:45:09 +0100468 modes[i]->hdisplay, modes[i]->vdisplay,
469 modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" :"");
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800470
Daniel Vetter7c2bb532014-03-04 21:08:41 +0100471 fallback = false;
Chris Wilson86f40bb2016-07-02 15:36:02 +0100472 conn_configured |= BIT(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000473 }
474
Chris Wilson754a7652017-02-24 11:43:06 +0000475 if ((conn_configured & mask) != mask && conn_configured != conn_seq)
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000476 goto retry;
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800477
Daniel Vetter7e696e42014-03-04 21:08:42 +0100478 /*
479 * If the BIOS didn't enable everything it could, fall back to have the
480 * same user experiencing of lighting up as much as possible like the
481 * fbdev helper library.
482 */
483 if (num_connectors_enabled != num_connectors_detected &&
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +0000484 num_connectors_enabled < INTEL_INFO(dev_priv)->num_pipes) {
Daniel Vetter7e696e42014-03-04 21:08:42 +0100485 DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
486 DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
487 num_connectors_detected);
488 fallback = true;
489 }
490
Daniel Vetter7c2bb532014-03-04 21:08:41 +0100491 if (fallback) {
Chris Wilsond84a0f32014-08-18 10:35:29 -0700492bail:
Daniel Vetter7e696e42014-03-04 21:08:42 +0100493 DRM_DEBUG_KMS("Not using firmware configuration\n");
Chris Wilson86f40bb2016-07-02 15:36:02 +0100494 memcpy(enabled, save_enabled, count);
Daniel Vettere13a0582017-07-05 06:56:29 +0200495 ret = false;
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800496 }
497
Daniel Vettere13a0582017-07-05 06:56:29 +0200498 drm_modeset_drop_locks(&ctx);
499 drm_modeset_acquire_fini(&ctx);
500
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800501 kfree(save_enabled);
Daniel Vettere13a0582017-07-05 06:56:29 +0200502 return ret;
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800503}
504
Thierry Reding3a493872014-06-27 17:19:23 +0200505static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800506 .initial_config = intel_fb_initial_config,
Daniel Vettercd5428a2013-01-21 23:42:49 +0100507 .fb_probe = intelfb_create,
Dave Airlie4abe3522010-03-30 05:34:18 +0000508};
509
Chris Wilson43cee312016-06-21 09:16:54 +0100510static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
Jesse Barnes79e53942008-11-07 14:24:08 -0800511{
Chris Wilson0c823122015-12-04 16:05:26 +0000512 /* We rely on the object-free to release the VMA pinning for
513 * the info->screen_base mmaping. Leaking the VMA is simpler than
514 * trying to rectify all the possible error paths leading here.
515 */
Chris Wilsonddfe1562013-08-06 17:43:07 +0100516
Dave Airlie4abe3522010-03-30 05:34:18 +0000517 drm_fb_helper_fini(&ifbdev->helper);
Jesse Barnes79e53942008-11-07 14:24:08 -0800518
Chris Wilson15727ed2017-06-22 17:02:11 +0100519 if (ifbdev->vma) {
Chris Wilson43cee312016-06-21 09:16:54 +0100520 mutex_lock(&ifbdev->helper.dev->struct_mutex);
Chris Wilson59354852018-02-20 13:42:06 +0000521 intel_unpin_fb_vma(ifbdev->vma, ifbdev->vma_flags);
Chris Wilson43cee312016-06-21 09:16:54 +0100522 mutex_unlock(&ifbdev->helper.dev->struct_mutex);
Lukas Wunner54632ab2015-11-18 13:43:20 +0100523 }
Chris Wilson43cee312016-06-21 09:16:54 +0100524
Chris Wilson15727ed2017-06-22 17:02:11 +0100525 if (ifbdev->fb)
526 drm_framebuffer_remove(&ifbdev->fb->base);
527
Chris Wilson43cee312016-06-21 09:16:54 +0100528 kfree(ifbdev);
Jesse Barnes79e53942008-11-07 14:24:08 -0800529}
Dave Airlie38651672010-03-30 05:34:13 +0000530
Jesse Barnesd978ef12014-03-07 08:57:51 -0800531/*
532 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
533 * The core display code will have read out the current plane configuration,
534 * so we use that to figure out if there's an object for us to use as the
535 * fb, and if so, we re-use it for the fbdev configuration.
536 *
537 * Note we only support a single fb shared across pipes for boot (mostly for
538 * fbcon), so we just find the biggest and use that.
539 */
540static bool intel_fbdev_init_bios(struct drm_device *dev,
541 struct intel_fbdev *ifbdev)
542{
543 struct intel_framebuffer *fb = NULL;
544 struct drm_crtc *crtc;
545 struct intel_crtc *intel_crtc;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800546 unsigned int max_size = 0;
547
Jesse Barnesd978ef12014-03-07 08:57:51 -0800548 /* Find the largest fb */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100549 for_each_crtc(dev, crtc) {
Maarten Lankhorst8e9ba312015-07-13 16:30:16 +0200550 struct drm_i915_gem_object *obj =
551 intel_fb_obj(crtc->primary->state->fb);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800552 intel_crtc = to_intel_crtc(crtc);
553
Maarten Lankhorstd5515992015-08-27 15:44:03 +0200554 if (!crtc->state->active || !obj) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800555 DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
556 pipe_name(intel_crtc->pipe));
557 continue;
558 }
559
Maarten Lankhorst8e9ba312015-07-13 16:30:16 +0200560 if (obj->base.size > max_size) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800561 DRM_DEBUG_KMS("found possible fb from plane %c\n",
562 pipe_name(intel_crtc->pipe));
Maarten Lankhorst8e9ba312015-07-13 16:30:16 +0200563 fb = to_intel_framebuffer(crtc->primary->state->fb);
564 max_size = obj->base.size;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800565 }
566 }
567
568 if (!fb) {
569 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
570 goto out;
571 }
572
573 /* Now make sure all the pipes will fit into it */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100574 for_each_crtc(dev, crtc) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800575 unsigned int cur_size;
576
577 intel_crtc = to_intel_crtc(crtc);
578
Maarten Lankhorstd5515992015-08-27 15:44:03 +0200579 if (!crtc->state->active) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800580 DRM_DEBUG_KMS("pipe %c not active, skipping\n",
581 pipe_name(intel_crtc->pipe));
582 continue;
583 }
584
585 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
586 pipe_name(intel_crtc->pipe));
587
588 /*
589 * See if the plane fb we found above will fit on this
Chris Wilsonbc104d12014-03-20 15:11:21 +0000590 * pipe. Note we need to use the selected fb's pitch and bpp
591 * rather than the current pipe's, since they differ.
Jesse Barnesd978ef12014-03-07 08:57:51 -0800592 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200593 cur_size = intel_crtc->config->base.adjusted_mode.crtc_hdisplay;
Ville Syrjälä272725c2016-12-14 23:32:20 +0200594 cur_size = cur_size * fb->base.format->cpp[0];
Chris Wilsonbc104d12014-03-20 15:11:21 +0000595 if (fb->base.pitches[0] < cur_size) {
596 DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
597 pipe_name(intel_crtc->pipe),
598 cur_size, fb->base.pitches[0]);
Chris Wilsonbc104d12014-03-20 15:11:21 +0000599 fb = NULL;
600 break;
601 }
602
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200603 cur_size = intel_crtc->config->base.adjusted_mode.crtc_vdisplay;
Ville Syrjäläd88c4af2017-03-07 21:42:06 +0200604 cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
Chris Wilsonbc104d12014-03-20 15:11:21 +0000605 cur_size *= fb->base.pitches[0];
606 DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
607 pipe_name(intel_crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200608 intel_crtc->config->base.adjusted_mode.crtc_hdisplay,
609 intel_crtc->config->base.adjusted_mode.crtc_vdisplay,
Ville Syrjälä272725c2016-12-14 23:32:20 +0200610 fb->base.format->cpp[0] * 8,
Jesse Barnesd978ef12014-03-07 08:57:51 -0800611 cur_size);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800612
613 if (cur_size > max_size) {
614 DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
615 pipe_name(intel_crtc->pipe),
616 cur_size, max_size);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800617 fb = NULL;
618 break;
619 }
620
621 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
622 pipe_name(intel_crtc->pipe),
623 max_size, cur_size);
624 }
625
Jesse Barnesd978ef12014-03-07 08:57:51 -0800626 if (!fb) {
627 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
628 goto out;
629 }
630
Ville Syrjälä272725c2016-12-14 23:32:20 +0200631 ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800632 ifbdev->fb = fb;
633
Harsha Sharmac3ed1102017-10-09 17:36:43 +0530634 drm_framebuffer_get(&ifbdev->fb->base);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800635
636 /* Final pass to check if any active pipes don't have fbs */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100637 for_each_crtc(dev, crtc) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800638 intel_crtc = to_intel_crtc(crtc);
639
Maarten Lankhorstd5515992015-08-27 15:44:03 +0200640 if (!crtc->state->active)
Jesse Barnesd978ef12014-03-07 08:57:51 -0800641 continue;
642
Dave Airlie66e514c2014-04-03 07:51:54 +1000643 WARN(!crtc->primary->fb,
Jesse Barnesd978ef12014-03-07 08:57:51 -0800644 "re-used BIOS config but lost an fb on crtc %d\n",
645 crtc->base.id);
646 }
647
648
649 DRM_DEBUG_KMS("using BIOS fb for initial console\n");
650 return true;
651
Jesse Barnesd978ef12014-03-07 08:57:51 -0800652out:
653
654 return false;
655}
656
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100657static void intel_fbdev_suspend_worker(struct work_struct *work)
658{
Chris Wilson91c8a322016-07-05 10:40:23 +0100659 intel_fbdev_set_suspend(&container_of(work,
660 struct drm_i915_private,
661 fbdev_suspend_work)->drm,
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100662 FBINFO_STATE_RUNNING,
663 true);
664}
665
Dave Airlie38651672010-03-30 05:34:13 +0000666int intel_fbdev_init(struct drm_device *dev)
667{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100668 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +0000669 struct intel_fbdev *ifbdev;
Chris Wilson5a793952010-06-06 10:50:03 +0100670 int ret;
Dave Airlie8be48d92010-03-30 05:34:14 +0000671
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +0000672 if (WARN_ON(INTEL_INFO(dev_priv)->num_pipes == 0))
Jesse Barnesd978ef12014-03-07 08:57:51 -0800673 return -ENODEV;
674
675 ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
676 if (ifbdev == NULL)
Dave Airlie8be48d92010-03-30 05:34:14 +0000677 return -ENOMEM;
678
Thierry Reding10a23102014-06-27 17:19:24 +0200679 drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
680
Jesse Barnesd978ef12014-03-07 08:57:51 -0800681 if (!intel_fbdev_init_bios(dev, ifbdev))
682 ifbdev->preferred_bpp = 32;
Dave Airlie8be48d92010-03-30 05:34:14 +0000683
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200684 ret = drm_fb_helper_init(dev, &ifbdev->helper, 4);
Chris Wilson5a793952010-06-06 10:50:03 +0100685 if (ret) {
686 kfree(ifbdev);
687 return ret;
688 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000689
Jesse Barnesd978ef12014-03-07 08:57:51 -0800690 dev_priv->fbdev = ifbdev;
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100691 INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
692
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000693 drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
Daniel Vetter20afbda2012-12-11 14:05:07 +0100694
Dave Airlie38651672010-03-30 05:34:13 +0000695 return 0;
696}
697
Ville Syrjäläe00bf692015-11-06 15:08:33 +0200698static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
Daniel Vetter20afbda2012-12-11 14:05:07 +0100699{
Chris Wilson43cee312016-06-21 09:16:54 +0100700 struct intel_fbdev *ifbdev = data;
Daniel Vetter20afbda2012-12-11 14:05:07 +0100701
702 /* Due to peculiar init order wrt to hpd handling this is separate. */
Lukas Wunner366e39b2015-11-18 16:29:51 +0100703 if (drm_fb_helper_initial_config(&ifbdev->helper,
Chris Wilsonad88d7f2017-11-25 19:41:55 +0000704 ifbdev->preferred_bpp))
Daniel Vetter4f256d82017-07-15 00:46:55 +0200705 intel_fbdev_unregister(to_i915(ifbdev->helper.dev));
Daniel Vetter20afbda2012-12-11 14:05:07 +0100706}
707
Ville Syrjäläe00bf692015-11-06 15:08:33 +0200708void intel_fbdev_initial_config_async(struct drm_device *dev)
709{
Chris Wilson43cee312016-06-21 09:16:54 +0100710 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
711
Clint Taylor5b8cd072017-01-18 13:38:43 -0800712 if (!ifbdev)
713 return;
714
Chris Wilson43cee312016-06-21 09:16:54 +0100715 ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
716}
717
718static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
719{
720 if (!ifbdev->cookie)
721 return;
722
723 /* Only serialises with all preceding async calls, hence +1 */
724 async_synchronize_cookie(ifbdev->cookie + 1);
725 ifbdev->cookie = 0;
Ville Syrjäläe00bf692015-11-06 15:08:33 +0200726}
727
Daniel Vetter4f256d82017-07-15 00:46:55 +0200728void intel_fbdev_unregister(struct drm_i915_private *dev_priv)
Dave Airlie38651672010-03-30 05:34:13 +0000729{
Chris Wilson43cee312016-06-21 09:16:54 +0100730 struct intel_fbdev *ifbdev = dev_priv->fbdev;
731
732 if (!ifbdev)
Dave Airlie8be48d92010-03-30 05:34:14 +0000733 return;
734
Chris Wilson0b8c0e92016-07-13 18:34:44 +0100735 cancel_work_sync(&dev_priv->fbdev_suspend_work);
Lukas Wunner366e39b2015-11-18 16:29:51 +0100736 if (!current_is_async())
Chris Wilson43cee312016-06-21 09:16:54 +0100737 intel_fbdev_sync(ifbdev);
738
Daniel Vetter4f256d82017-07-15 00:46:55 +0200739 drm_fb_helper_unregister_fbi(&ifbdev->helper);
740}
741
742void intel_fbdev_fini(struct drm_i915_private *dev_priv)
743{
744 struct intel_fbdev *ifbdev = fetch_and_zero(&dev_priv->fbdev);
745
746 if (!ifbdev)
747 return;
748
Chris Wilson43cee312016-06-21 09:16:54 +0100749 intel_fbdev_destroy(ifbdev);
Dave Airlie38651672010-03-30 05:34:13 +0000750}
Dave Airlie3fa016a2012-03-28 10:48:49 +0100751
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100752void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
Dave Airlie3fa016a2012-03-28 10:48:49 +0100753{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100754 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula1ffc5282013-05-07 18:54:05 +0300755 struct intel_fbdev *ifbdev = dev_priv->fbdev;
756 struct fb_info *info;
757
Chris Wilson15727ed2017-06-22 17:02:11 +0100758 if (!ifbdev || !ifbdev->vma)
Dave Airlie3fa016a2012-03-28 10:48:49 +0100759 return;
760
Jani Nikula1ffc5282013-05-07 18:54:05 +0300761 info = ifbdev->helper.fbdev;
762
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100763 if (synchronous) {
764 /* Flush any pending work to turn the console on, and then
765 * wait to turn it off. It must be synchronous as we are
766 * about to suspend or unload the driver.
767 *
768 * Note that from within the work-handler, we cannot flush
769 * ourselves, so only flush outstanding work upon suspend!
770 */
771 if (state != FBINFO_STATE_RUNNING)
772 flush_work(&dev_priv->fbdev_suspend_work);
773 console_lock();
774 } else {
775 /*
776 * The console lock can be pretty contented on resume due
777 * to all the printk activity. Try to keep it out of the hot
778 * path of resume if possible.
779 */
780 WARN_ON(state != FBINFO_STATE_RUNNING);
781 if (!console_trylock()) {
782 /* Don't block our own workqueue as this can
783 * be run in parallel with other i915.ko tasks.
784 */
785 schedule_work(&dev_priv->fbdev_suspend_work);
786 return;
787 }
788 }
789
Jani Nikula1ffc5282013-05-07 18:54:05 +0300790 /* On resume from hibernation: If the object is shmemfs backed, it has
791 * been restored from swap. If the object is stolen however, it will be
792 * full of whatever garbage was left in there.
793 */
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800794 if (state == FBINFO_STATE_RUNNING && ifbdev->fb->obj->stolen)
Jani Nikula1ffc5282013-05-07 18:54:05 +0300795 memset_io(info->screen_base, 0, info->screen_size);
796
Archit Taneja21cff142015-07-31 16:21:56 +0530797 drm_fb_helper_set_suspend(&ifbdev->helper, state);
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100798 console_unlock();
Dave Airlie3fa016a2012-03-28 10:48:49 +0100799}
800
Daniel Vetter0632fef2013-10-08 17:44:49 +0200801void intel_fbdev_output_poll_changed(struct drm_device *dev)
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000802{
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100803 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
804
Chris Wilsonad88d7f2017-11-25 19:41:55 +0000805 if (!ifbdev)
806 return;
807
808 intel_fbdev_sync(ifbdev);
809 if (ifbdev->vma)
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100810 drm_fb_helper_hotplug_event(&ifbdev->helper);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000811}
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100812
Daniel Vetter0632fef2013-10-08 17:44:49 +0200813void intel_fbdev_restore_mode(struct drm_device *dev)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100814{
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100815 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100816
Rodrigo Vivid04df732015-07-08 16:25:21 -0700817 if (!ifbdev)
Ben Widawskye3c74752013-04-05 13:12:39 -0700818 return;
819
Chris Wilsone77018f2016-06-21 09:16:55 +0100820 intel_fbdev_sync(ifbdev);
Chris Wilson15727ed2017-06-22 17:02:11 +0100821 if (!ifbdev->vma)
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100822 return;
Chris Wilsone77018f2016-06-21 09:16:55 +0100823
Chris Wilsonfabef822017-02-15 10:59:19 +0000824 if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper) == 0)
825 intel_fbdev_invalidate(ifbdev);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100826}