blob: b8af35187d226df3273380448aded0f5bc0d7c7c [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;
51 unsigned int origin = ifbdev->vma->fence ? ORIGIN_GTT : ORIGIN_CPU;
52
53 intel_fb_obj_invalidate(obj, origin);
54}
55
Daniel Vettere9910772014-06-18 15:05:19 +020056static int intel_fbdev_set_par(struct fb_info *info)
57{
58 struct drm_fb_helper *fb_helper = info->par;
59 struct intel_fbdev *ifbdev =
60 container_of(fb_helper, struct intel_fbdev, helper);
61 int ret;
62
63 ret = drm_fb_helper_set_par(info);
Chris Wilsonfabef822017-02-15 10:59:19 +000064 if (ret == 0)
65 intel_fbdev_invalidate(ifbdev);
Daniel Vettere9910772014-06-18 15:05:19 +020066
67 return ret;
68}
69
Rodrigo Vivi03e515f2015-03-09 17:57:07 -070070static int intel_fbdev_blank(int blank, struct fb_info *info)
71{
72 struct drm_fb_helper *fb_helper = info->par;
73 struct intel_fbdev *ifbdev =
74 container_of(fb_helper, struct intel_fbdev, helper);
75 int ret;
76
77 ret = drm_fb_helper_blank(blank, info);
Chris Wilsonfabef822017-02-15 10:59:19 +000078 if (ret == 0)
79 intel_fbdev_invalidate(ifbdev);
Rodrigo Vivi03e515f2015-03-09 17:57:07 -070080
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);
Rodrigo Vivid9a946b2015-05-28 10:26:58 -070090 int ret;
Rodrigo Vivid9a946b2015-05-28 10:26:58 -070091
Chris Wilsonfabef822017-02-15 10:59:19 +000092 ret = drm_fb_helper_pan_display(var, info);
93 if (ret == 0)
94 intel_fbdev_invalidate(ifbdev);
Rodrigo Vivid9a946b2015-05-28 10:26:58 -070095
96 return ret;
97}
98
Jesse Barnes79e53942008-11-07 14:24:08 -080099static struct fb_ops intelfb_ops = {
100 .owner = THIS_MODULE,
Stefan Christa36384d2016-11-14 00:03:27 +0100101 DRM_FB_HELPER_DEFAULT_OPS,
Daniel Vettere9910772014-06-18 15:05:19 +0200102 .fb_set_par = intel_fbdev_set_par,
Archit Taneja21cff142015-07-31 16:21:56 +0530103 .fb_fillrect = drm_fb_helper_cfb_fillrect,
104 .fb_copyarea = drm_fb_helper_cfb_copyarea,
105 .fb_imageblit = drm_fb_helper_cfb_imageblit,
Rodrigo Vivid9a946b2015-05-28 10:26:58 -0700106 .fb_pan_display = intel_fbdev_pan_display,
Rodrigo Vivi03e515f2015-03-09 17:57:07 -0700107 .fb_blank = intel_fbdev_blank,
Jesse Barnes79e53942008-11-07 14:24:08 -0800108};
109
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800110static int intelfb_alloc(struct drm_fb_helper *helper,
111 struct drm_fb_helper_surface_size *sizes)
Jesse Barnes79e53942008-11-07 14:24:08 -0800112{
Ville Syrjälä34308242013-05-31 20:07:05 +0300113 struct intel_fbdev *ifbdev =
114 container_of(helper, struct intel_fbdev, helper);
Lukas Wunner4601b932015-12-19 15:40:39 +0100115 struct drm_framebuffer *fb;
Ville Syrjälä34308242013-05-31 20:07:05 +0300116 struct drm_device *dev = helper->dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -0300117 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300118 struct i915_ggtt *ggtt = &dev_priv->ggtt;
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;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300142 if (size * 2 < ggtt->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;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800181 bool prealloc = false;
Chris Wilson406ea8d2016-07-20 13:31:55 +0100182 void __iomem *vaddr;
Chris Wilson8ef85612016-04-28 09:56:39 +0100183 int ret;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800184
Chris Wilsonedd586f2014-04-23 08:54:31 +0100185 if (intel_fb &&
186 (sizes->fb_width > intel_fb->base.width ||
187 sizes->fb_height > intel_fb->base.height)) {
188 DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d),"
189 " releasing it\n",
190 intel_fb->base.width, intel_fb->base.height,
191 sizes->fb_width, sizes->fb_height);
Harsha Sharmac3ed1102017-10-09 17:36:43 +0530192 drm_framebuffer_put(&intel_fb->base);
Chris Wilsonedd586f2014-04-23 08:54:31 +0100193 intel_fb = ifbdev->fb = NULL;
194 }
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800195 if (!intel_fb || WARN_ON(!intel_fb->obj)) {
Jesse Barnes48e92122013-11-26 11:25:54 -0800196 DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800197 ret = intelfb_alloc(helper, sizes);
198 if (ret)
Tvrtko Ursulin51f13852015-06-30 10:06:27 +0100199 return ret;
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800200 intel_fb = ifbdev->fb;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800201 } else {
Jesse Barnes48e92122013-11-26 11:25:54 -0800202 DRM_DEBUG_KMS("re-using BIOS fb\n");
Jesse Barnesd978ef12014-03-07 08:57:51 -0800203 prealloc = true;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800204 sizes->fb_width = intel_fb->base.width;
205 sizes->fb_height = intel_fb->base.height;
206 }
207
Tvrtko Ursulin51f13852015-06-30 10:06:27 +0100208 mutex_lock(&dev->struct_mutex);
Ville Syrjäläc6364232017-09-01 22:54:56 +0300209 intel_runtime_pm_get(dev_priv);
Tvrtko Ursulin51f13852015-06-30 10:06:27 +0100210
Chris Wilson0c823122015-12-04 16:05:26 +0000211 /* Pin the GGTT vma for our access via info->screen_base.
212 * This also validates that any existing fb inherited from the
213 * BIOS is suitable for own access.
214 */
Robert Fossc2c446a2017-05-19 16:50:17 -0400215 vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base, DRM_MODE_ROTATE_0);
Chris Wilson058d88c2016-08-15 10:49:06 +0100216 if (IS_ERR(vma)) {
217 ret = PTR_ERR(vma);
Chris Wilson0c823122015-12-04 16:05:26 +0000218 goto out_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +0100219 }
Chris Wilson0c823122015-12-04 16:05:26 +0000220
Archit Taneja21cff142015-07-31 16:21:56 +0530221 info = drm_fb_helper_alloc_fbi(helper);
222 if (IS_ERR(info)) {
Lukas Wunner366e39b2015-11-18 16:29:51 +0100223 DRM_ERROR("Failed to allocate fb_info\n");
Archit Taneja21cff142015-07-31 16:21:56 +0530224 ret = PTR_ERR(info);
Chris Wilsonb4476f52009-02-11 14:26:36 +0000225 goto out_unpin;
Jesse Barnes79e53942008-11-07 14:24:08 -0800226 }
227
Ville Syrjälä34308242013-05-31 20:07:05 +0300228 info->par = helper;
Jesse Barnes79e53942008-11-07 14:24:08 -0800229
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800230 fb = &ifbdev->fb->base;
Dave Airlie38651672010-03-30 05:34:13 +0000231
232 ifbdev->helper.fb = fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000233
Jesse Barnes79e53942008-11-07 14:24:08 -0800234 strcpy(info->fix.id, "inteldrmfb");
Jesse Barnes79e53942008-11-07 14:24:08 -0800235
Jesse Barnes79e53942008-11-07 14:24:08 -0800236 info->fbops = &intelfb_ops;
237
Dave Airlie4410f392009-06-16 15:34:38 -0700238 /* setup aperture base/size for vesafb takeover */
Marcin Slusarz1471ca92010-05-16 17:27:03 +0200239 info->apertures->ranges[0].base = dev->mode_config.fb_base;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300240 info->apertures->ranges[0].size = ggtt->mappable_end;
Dave Airlie4410f392009-06-16 15:34:38 -0700241
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100242 info->fix.smem_start = dev->mode_config.fb_base + i915_ggtt_offset(vma);
Chris Wilson8ef85612016-04-28 09:56:39 +0100243 info->fix.smem_len = vma->node.size;
Jesse Barnes79e53942008-11-07 14:24:08 -0800244
Chris Wilson8ef85612016-04-28 09:56:39 +0100245 vaddr = i915_vma_pin_iomap(vma);
246 if (IS_ERR(vaddr)) {
Lukas Wunner366e39b2015-11-18 16:29:51 +0100247 DRM_ERROR("Failed to remap framebuffer into virtual memory\n");
Chris Wilson8ef85612016-04-28 09:56:39 +0100248 ret = PTR_ERR(vaddr);
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100249 goto out_unpin;
Jesse Barnes79e53942008-11-07 14:24:08 -0800250 }
Chris Wilson8ef85612016-04-28 09:56:39 +0100251 info->screen_base = vaddr;
252 info->screen_size = vma->node.size;
Jesse Barnes79e53942008-11-07 14:24:08 -0800253
Jesse Barnes24576d22013-03-26 09:25:45 -0700254 /* This driver doesn't need a VT switch to restore the mode on resume */
255 info->skip_vt_switch = true;
256
Ville Syrjäläb00c6002016-12-14 23:31:35 +0200257 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
Dave Airlie38651672010-03-30 05:34:13 +0000258 drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height);
Jesse Barnes79e53942008-11-07 14:24:08 -0800259
Chris Wilson88afe712012-12-16 12:15:41 +0000260 /* If the object is shmemfs backed, it will have given us zeroed pages.
261 * If the object is stolen however, it will be full of whatever
262 * garbage was left in there.
263 */
Chris Wilson058d88c2016-08-15 10:49:06 +0100264 if (intel_fb->obj->stolen && !prealloc)
Chris Wilson88afe712012-12-16 12:15:41 +0000265 memset_io(info->screen_base, 0, info->screen_size);
266
Sascha Hauerfb2a99e2012-02-06 10:58:19 +0100267 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
Jesse Barnes79e53942008-11-07 14:24:08 -0800268
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100269 DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08x\n",
270 fb->width, fb->height, i915_ggtt_offset(vma));
Chris Wilson058d88c2016-08-15 10:49:06 +0100271 ifbdev->vma = vma;
Jesse Barnes79e53942008-11-07 14:24:08 -0800272
Ville Syrjäläc6364232017-09-01 22:54:56 +0300273 intel_runtime_pm_put(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -0800274 mutex_unlock(&dev->struct_mutex);
David Weinehall52a05c32016-08-22 13:32:44 +0300275 vga_switcheroo_client_fb_set(pdev, info);
Jesse Barnes79e53942008-11-07 14:24:08 -0800276 return 0;
277
Chris Wilsonb4476f52009-02-11 14:26:36 +0000278out_unpin:
Chris Wilsonbe1e3412017-01-16 15:21:27 +0000279 intel_unpin_fb_vma(vma);
Chris Wilson0c823122015-12-04 16:05:26 +0000280out_unlock:
Ville Syrjäläc6364232017-09-01 22:54:56 +0300281 intel_runtime_pm_put(dev_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -0800282 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -0800283 return ret;
284}
285
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800286static struct drm_fb_helper_crtc *
287intel_fb_helper_crtc(struct drm_fb_helper *fb_helper, struct drm_crtc *crtc)
288{
289 int i;
290
291 for (i = 0; i < fb_helper->crtc_count; i++)
292 if (fb_helper->crtc_info[i].mode_set.crtc == crtc)
293 return &fb_helper->crtc_info[i];
294
295 return NULL;
296}
297
298/*
299 * Try to read the BIOS display configuration and use it for the initial
300 * fb configuration.
301 *
302 * The BIOS or boot loader will generally create an initial display
303 * configuration for us that includes some set of active pipes and displays.
304 * This routine tries to figure out which pipes and connectors are active
305 * and stuffs them into the crtcs and modes array given to us by the
306 * drm_fb_helper code.
307 *
308 * The overall sequence is:
309 * intel_fbdev_init - from driver load
310 * intel_fbdev_init_bios - initialize the intel_fbdev using BIOS data
311 * drm_fb_helper_init - build fb helper structs
312 * drm_fb_helper_single_add_all_connectors - more fb helper structs
313 * intel_fbdev_initial_config - apply the config
314 * drm_fb_helper_initial_config - call ->probe then register_framebuffer()
315 * drm_setup_crtcs - build crtc config for fbdev
316 * intel_fb_initial_config - find active connectors etc
317 * drm_fb_helper_single_fb_probe - set up fbdev
318 * intelfb_create - re-use or alloc fb, build out fbdev structs
319 *
320 * Note that we don't make special consideration whether we could actually
321 * switch to the selected modes without a full modeset. E.g. when the display
322 * is in VGA mode we need to recalculate watermarks and set a new high-res
323 * framebuffer anyway.
324 */
325static bool intel_fb_initial_config(struct drm_fb_helper *fb_helper,
326 struct drm_fb_helper_crtc **crtcs,
327 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000328 struct drm_fb_offset *offsets,
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800329 bool *enabled, int width, int height)
330{
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +0000331 struct drm_i915_private *dev_priv = to_i915(fb_helper->dev);
Chris Wilson754a7652017-02-24 11:43:06 +0000332 unsigned long conn_configured, conn_seq, mask;
Chris Wilson86f40bb2016-07-02 15:36:02 +0100333 unsigned int count = min(fb_helper->connector_count, BITS_PER_LONG);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800334 int i, j;
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800335 bool *save_enabled;
Daniel Vettere13a0582017-07-05 06:56:29 +0200336 bool fallback = true, ret = true;
Daniel Vetter7e696e42014-03-04 21:08:42 +0100337 int num_connectors_enabled = 0;
338 int num_connectors_detected = 0;
Daniel Vettere13a0582017-07-05 06:56:29 +0200339 struct drm_modeset_acquire_ctx ctx;
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800340
Chris Wilson86f40bb2016-07-02 15:36:02 +0100341 save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL);
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800342 if (!save_enabled)
343 return false;
344
Daniel Vettere13a0582017-07-05 06:56:29 +0200345 drm_modeset_acquire_init(&ctx, 0);
346
347 while (drm_modeset_lock_all_ctx(fb_helper->dev, &ctx) != 0)
348 drm_modeset_backoff(&ctx);
349
Chris Wilson86f40bb2016-07-02 15:36:02 +0100350 memcpy(save_enabled, enabled, count);
Joonas Lahtinen3c779a42017-02-08 15:12:09 +0200351 mask = GENMASK(count - 1, 0);
Chris Wilson86f40bb2016-07-02 15:36:02 +0100352 conn_configured = 0;
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000353retry:
Chris Wilson754a7652017-02-24 11:43:06 +0000354 conn_seq = conn_configured;
Chris Wilson86f40bb2016-07-02 15:36:02 +0100355 for (i = 0; i < count; i++) {
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800356 struct drm_fb_helper_connector *fb_conn;
357 struct drm_connector *connector;
358 struct drm_encoder *encoder;
359 struct drm_fb_helper_crtc *new_crtc;
360
361 fb_conn = fb_helper->connector_info[i];
362 connector = fb_conn->connector;
Daniel Vetter7e696e42014-03-04 21:08:42 +0100363
Chris Wilson86f40bb2016-07-02 15:36:02 +0100364 if (conn_configured & BIT(i))
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000365 continue;
366
Chris Wilson754a7652017-02-24 11:43:06 +0000367 if (conn_seq == 0 && !connector->has_tile)
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000368 continue;
369
Daniel Vetter7e696e42014-03-04 21:08:42 +0100370 if (connector->status == connector_status_connected)
371 num_connectors_detected++;
372
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800373 if (!enabled[i]) {
Chris Wilsonc0c97622014-05-13 13:45:09 +0100374 DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300375 connector->name);
Chris Wilson86f40bb2016-07-02 15:36:02 +0100376 conn_configured |= BIT(i);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800377 continue;
378 }
379
Chris Wilsond84a0f32014-08-18 10:35:29 -0700380 if (connector->force == DRM_FORCE_OFF) {
381 DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
382 connector->name);
383 enabled[i] = false;
384 continue;
385 }
386
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100387 encoder = connector->state->best_encoder;
388 if (!encoder || WARN_ON(!connector->state->crtc)) {
Chris Wilsond84a0f32014-08-18 10:35:29 -0700389 if (connector->force > DRM_FORCE_OFF)
390 goto bail;
391
Chris Wilsonc0c97622014-05-13 13:45:09 +0100392 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300393 connector->name);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800394 enabled[i] = false;
Chris Wilson86f40bb2016-07-02 15:36:02 +0100395 conn_configured |= BIT(i);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800396 continue;
397 }
398
Daniel Vetter7e696e42014-03-04 21:08:42 +0100399 num_connectors_enabled++;
400
Chris Wilson86f40bb2016-07-02 15:36:02 +0100401 new_crtc = intel_fb_helper_crtc(fb_helper,
402 connector->state->crtc);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800403
404 /*
405 * Make sure we're not trying to drive multiple connectors
406 * with a single CRTC, since our cloning support may not
407 * match the BIOS.
408 */
Chris Wilson86f40bb2016-07-02 15:36:02 +0100409 for (j = 0; j < count; j++) {
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800410 if (crtcs[j] == new_crtc) {
Daniel Vetter7e696e42014-03-04 21:08:42 +0100411 DRM_DEBUG_KMS("fallback: cloned configuration\n");
Chris Wilsond84a0f32014-08-18 10:35:29 -0700412 goto bail;
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800413 }
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800414 }
415
Chris Wilsonc0c97622014-05-13 13:45:09 +0100416 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300417 connector->name);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800418
419 /* go for command line mode first */
Vincent Abrioua09759e2017-01-06 17:44:43 +0100420 modes[i] = drm_pick_cmdline_mode(fb_conn);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800421
422 /* try for preferred next */
423 if (!modes[i]) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000424 DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
425 connector->name, connector->has_tile);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800426 modes[i] = drm_has_preferred_mode(fb_conn, width,
427 height);
428 }
429
Chris Wilsonafba0b52014-05-13 16:07:37 +0100430 /* No preferred mode marked by the EDID? Are there any modes? */
431 if (!modes[i] && !list_empty(&connector->modes)) {
432 DRM_DEBUG_KMS("using first mode listed on connector %s\n",
Dave Airlie8d4ad9d2014-06-05 20:28:59 +1000433 connector->name);
Chris Wilsonafba0b52014-05-13 16:07:37 +0100434 modes[i] = list_first_entry(&connector->modes,
435 struct drm_display_mode,
436 head);
437 }
438
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800439 /* last resort: use current mode */
440 if (!modes[i]) {
441 /*
442 * IMPORTANT: We want to use the adjusted mode (i.e.
443 * after the panel fitter upscaling) as the initial
444 * config, not the input mode, which is what crtc->mode
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +0200445 * usually contains. But since our current
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800446 * code puts a mode derived from the post-pfit timings
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +0200447 * into crtc->mode this works out correctly.
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100448 *
449 * This is crtc->mode and not crtc->state->mode for the
450 * fastboot check to work correctly. crtc_state->mode has
451 * I915_MODE_FLAG_INHERITED, which we clear to force check
452 * state.
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800453 */
Chris Wilsonc0c97622014-05-13 13:45:09 +0100454 DRM_DEBUG_KMS("looking for current mode on connector %s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300455 connector->name);
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100456 modes[i] = &connector->state->crtc->mode;
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800457 }
458 crtcs[i] = new_crtc;
459
Ville Syrjälä78108b72016-05-27 20:59:19 +0300460 DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300461 connector->name,
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100462 connector->state->crtc->base.id,
Ville Syrjälä78108b72016-05-27 20:59:19 +0300463 connector->state->crtc->name,
Chris Wilsonc0c97622014-05-13 13:45:09 +0100464 modes[i]->hdisplay, modes[i]->vdisplay,
465 modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" :"");
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800466
Daniel Vetter7c2bb532014-03-04 21:08:41 +0100467 fallback = false;
Chris Wilson86f40bb2016-07-02 15:36:02 +0100468 conn_configured |= BIT(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000469 }
470
Chris Wilson754a7652017-02-24 11:43:06 +0000471 if ((conn_configured & mask) != mask && conn_configured != conn_seq)
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000472 goto retry;
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800473
Daniel Vetter7e696e42014-03-04 21:08:42 +0100474 /*
475 * If the BIOS didn't enable everything it could, fall back to have the
476 * same user experiencing of lighting up as much as possible like the
477 * fbdev helper library.
478 */
479 if (num_connectors_enabled != num_connectors_detected &&
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +0000480 num_connectors_enabled < INTEL_INFO(dev_priv)->num_pipes) {
Daniel Vetter7e696e42014-03-04 21:08:42 +0100481 DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
482 DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
483 num_connectors_detected);
484 fallback = true;
485 }
486
Daniel Vetter7c2bb532014-03-04 21:08:41 +0100487 if (fallback) {
Chris Wilsond84a0f32014-08-18 10:35:29 -0700488bail:
Daniel Vetter7e696e42014-03-04 21:08:42 +0100489 DRM_DEBUG_KMS("Not using firmware configuration\n");
Chris Wilson86f40bb2016-07-02 15:36:02 +0100490 memcpy(enabled, save_enabled, count);
Daniel Vettere13a0582017-07-05 06:56:29 +0200491 ret = false;
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800492 }
493
Daniel Vettere13a0582017-07-05 06:56:29 +0200494 drm_modeset_drop_locks(&ctx);
495 drm_modeset_acquire_fini(&ctx);
496
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800497 kfree(save_enabled);
Daniel Vettere13a0582017-07-05 06:56:29 +0200498 return ret;
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800499}
500
Thierry Reding3a493872014-06-27 17:19:23 +0200501static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800502 .initial_config = intel_fb_initial_config,
Daniel Vettercd5428a2013-01-21 23:42:49 +0100503 .fb_probe = intelfb_create,
Dave Airlie4abe3522010-03-30 05:34:18 +0000504};
505
Chris Wilson43cee312016-06-21 09:16:54 +0100506static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
Jesse Barnes79e53942008-11-07 14:24:08 -0800507{
Chris Wilson0c823122015-12-04 16:05:26 +0000508 /* We rely on the object-free to release the VMA pinning for
509 * the info->screen_base mmaping. Leaking the VMA is simpler than
510 * trying to rectify all the possible error paths leading here.
511 */
Chris Wilsonddfe1562013-08-06 17:43:07 +0100512
Dave Airlie4abe3522010-03-30 05:34:18 +0000513 drm_fb_helper_fini(&ifbdev->helper);
Jesse Barnes79e53942008-11-07 14:24:08 -0800514
Chris Wilson15727ed2017-06-22 17:02:11 +0100515 if (ifbdev->vma) {
Chris Wilson43cee312016-06-21 09:16:54 +0100516 mutex_lock(&ifbdev->helper.dev->struct_mutex);
Chris Wilsonbe1e3412017-01-16 15:21:27 +0000517 intel_unpin_fb_vma(ifbdev->vma);
Chris Wilson43cee312016-06-21 09:16:54 +0100518 mutex_unlock(&ifbdev->helper.dev->struct_mutex);
Lukas Wunner54632ab2015-11-18 13:43:20 +0100519 }
Chris Wilson43cee312016-06-21 09:16:54 +0100520
Chris Wilson15727ed2017-06-22 17:02:11 +0100521 if (ifbdev->fb)
522 drm_framebuffer_remove(&ifbdev->fb->base);
523
Chris Wilson43cee312016-06-21 09:16:54 +0100524 kfree(ifbdev);
Jesse Barnes79e53942008-11-07 14:24:08 -0800525}
Dave Airlie38651672010-03-30 05:34:13 +0000526
Jesse Barnesd978ef12014-03-07 08:57:51 -0800527/*
528 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
529 * The core display code will have read out the current plane configuration,
530 * so we use that to figure out if there's an object for us to use as the
531 * fb, and if so, we re-use it for the fbdev configuration.
532 *
533 * Note we only support a single fb shared across pipes for boot (mostly for
534 * fbcon), so we just find the biggest and use that.
535 */
536static bool intel_fbdev_init_bios(struct drm_device *dev,
537 struct intel_fbdev *ifbdev)
538{
539 struct intel_framebuffer *fb = NULL;
540 struct drm_crtc *crtc;
541 struct intel_crtc *intel_crtc;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800542 unsigned int max_size = 0;
543
Jesse Barnesd978ef12014-03-07 08:57:51 -0800544 /* Find the largest fb */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100545 for_each_crtc(dev, crtc) {
Maarten Lankhorst8e9ba312015-07-13 16:30:16 +0200546 struct drm_i915_gem_object *obj =
547 intel_fb_obj(crtc->primary->state->fb);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800548 intel_crtc = to_intel_crtc(crtc);
549
Maarten Lankhorstd5515992015-08-27 15:44:03 +0200550 if (!crtc->state->active || !obj) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800551 DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
552 pipe_name(intel_crtc->pipe));
553 continue;
554 }
555
Maarten Lankhorst8e9ba312015-07-13 16:30:16 +0200556 if (obj->base.size > max_size) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800557 DRM_DEBUG_KMS("found possible fb from plane %c\n",
558 pipe_name(intel_crtc->pipe));
Maarten Lankhorst8e9ba312015-07-13 16:30:16 +0200559 fb = to_intel_framebuffer(crtc->primary->state->fb);
560 max_size = obj->base.size;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800561 }
562 }
563
564 if (!fb) {
565 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
566 goto out;
567 }
568
569 /* Now make sure all the pipes will fit into it */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100570 for_each_crtc(dev, crtc) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800571 unsigned int cur_size;
572
573 intel_crtc = to_intel_crtc(crtc);
574
Maarten Lankhorstd5515992015-08-27 15:44:03 +0200575 if (!crtc->state->active) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800576 DRM_DEBUG_KMS("pipe %c not active, skipping\n",
577 pipe_name(intel_crtc->pipe));
578 continue;
579 }
580
581 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
582 pipe_name(intel_crtc->pipe));
583
584 /*
585 * See if the plane fb we found above will fit on this
Chris Wilsonbc104d12014-03-20 15:11:21 +0000586 * pipe. Note we need to use the selected fb's pitch and bpp
587 * rather than the current pipe's, since they differ.
Jesse Barnesd978ef12014-03-07 08:57:51 -0800588 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200589 cur_size = intel_crtc->config->base.adjusted_mode.crtc_hdisplay;
Ville Syrjälä272725c2016-12-14 23:32:20 +0200590 cur_size = cur_size * fb->base.format->cpp[0];
Chris Wilsonbc104d12014-03-20 15:11:21 +0000591 if (fb->base.pitches[0] < cur_size) {
592 DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
593 pipe_name(intel_crtc->pipe),
594 cur_size, fb->base.pitches[0]);
Chris Wilsonbc104d12014-03-20 15:11:21 +0000595 fb = NULL;
596 break;
597 }
598
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200599 cur_size = intel_crtc->config->base.adjusted_mode.crtc_vdisplay;
Ville Syrjäläd88c4af2017-03-07 21:42:06 +0200600 cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
Chris Wilsonbc104d12014-03-20 15:11:21 +0000601 cur_size *= fb->base.pitches[0];
602 DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
603 pipe_name(intel_crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200604 intel_crtc->config->base.adjusted_mode.crtc_hdisplay,
605 intel_crtc->config->base.adjusted_mode.crtc_vdisplay,
Ville Syrjälä272725c2016-12-14 23:32:20 +0200606 fb->base.format->cpp[0] * 8,
Jesse Barnesd978ef12014-03-07 08:57:51 -0800607 cur_size);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800608
609 if (cur_size > max_size) {
610 DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
611 pipe_name(intel_crtc->pipe),
612 cur_size, max_size);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800613 fb = NULL;
614 break;
615 }
616
617 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
618 pipe_name(intel_crtc->pipe),
619 max_size, cur_size);
620 }
621
Jesse Barnesd978ef12014-03-07 08:57:51 -0800622 if (!fb) {
623 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
624 goto out;
625 }
626
Ville Syrjälä272725c2016-12-14 23:32:20 +0200627 ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800628 ifbdev->fb = fb;
629
Harsha Sharmac3ed1102017-10-09 17:36:43 +0530630 drm_framebuffer_get(&ifbdev->fb->base);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800631
632 /* Final pass to check if any active pipes don't have fbs */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100633 for_each_crtc(dev, crtc) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800634 intel_crtc = to_intel_crtc(crtc);
635
Maarten Lankhorstd5515992015-08-27 15:44:03 +0200636 if (!crtc->state->active)
Jesse Barnesd978ef12014-03-07 08:57:51 -0800637 continue;
638
Dave Airlie66e514c2014-04-03 07:51:54 +1000639 WARN(!crtc->primary->fb,
Jesse Barnesd978ef12014-03-07 08:57:51 -0800640 "re-used BIOS config but lost an fb on crtc %d\n",
641 crtc->base.id);
642 }
643
644
645 DRM_DEBUG_KMS("using BIOS fb for initial console\n");
646 return true;
647
Jesse Barnesd978ef12014-03-07 08:57:51 -0800648out:
649
650 return false;
651}
652
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100653static void intel_fbdev_suspend_worker(struct work_struct *work)
654{
Chris Wilson91c8a322016-07-05 10:40:23 +0100655 intel_fbdev_set_suspend(&container_of(work,
656 struct drm_i915_private,
657 fbdev_suspend_work)->drm,
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100658 FBINFO_STATE_RUNNING,
659 true);
660}
661
Dave Airlie38651672010-03-30 05:34:13 +0000662int intel_fbdev_init(struct drm_device *dev)
663{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100664 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +0000665 struct intel_fbdev *ifbdev;
Chris Wilson5a793952010-06-06 10:50:03 +0100666 int ret;
Dave Airlie8be48d92010-03-30 05:34:14 +0000667
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +0000668 if (WARN_ON(INTEL_INFO(dev_priv)->num_pipes == 0))
Jesse Barnesd978ef12014-03-07 08:57:51 -0800669 return -ENODEV;
670
671 ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
672 if (ifbdev == NULL)
Dave Airlie8be48d92010-03-30 05:34:14 +0000673 return -ENOMEM;
674
Thierry Reding10a23102014-06-27 17:19:24 +0200675 drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
676
Jesse Barnesd978ef12014-03-07 08:57:51 -0800677 if (!intel_fbdev_init_bios(dev, ifbdev))
678 ifbdev->preferred_bpp = 32;
Dave Airlie8be48d92010-03-30 05:34:14 +0000679
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200680 ret = drm_fb_helper_init(dev, &ifbdev->helper, 4);
Chris Wilson5a793952010-06-06 10:50:03 +0100681 if (ret) {
682 kfree(ifbdev);
683 return ret;
684 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000685
Jesse Barnesd978ef12014-03-07 08:57:51 -0800686 dev_priv->fbdev = ifbdev;
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100687 INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
688
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000689 drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
Daniel Vetter20afbda2012-12-11 14:05:07 +0100690
Dave Airlie38651672010-03-30 05:34:13 +0000691 return 0;
692}
693
Ville Syrjäläe00bf692015-11-06 15:08:33 +0200694static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
Daniel Vetter20afbda2012-12-11 14:05:07 +0100695{
Chris Wilson43cee312016-06-21 09:16:54 +0100696 struct intel_fbdev *ifbdev = data;
Daniel Vetter20afbda2012-12-11 14:05:07 +0100697
698 /* Due to peculiar init order wrt to hpd handling this is separate. */
Lukas Wunner366e39b2015-11-18 16:29:51 +0100699 if (drm_fb_helper_initial_config(&ifbdev->helper,
Daniel Vetter4f256d82017-07-15 00:46:55 +0200700 ifbdev->preferred_bpp)) {
701 intel_fbdev_unregister(to_i915(ifbdev->helper.dev));
702 intel_fbdev_fini(to_i915(ifbdev->helper.dev));
703 }
Daniel Vetter20afbda2012-12-11 14:05:07 +0100704}
705
Ville Syrjäläe00bf692015-11-06 15:08:33 +0200706void intel_fbdev_initial_config_async(struct drm_device *dev)
707{
Chris Wilson43cee312016-06-21 09:16:54 +0100708 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
709
Clint Taylor5b8cd072017-01-18 13:38:43 -0800710 if (!ifbdev)
711 return;
712
Chris Wilson43cee312016-06-21 09:16:54 +0100713 ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
714}
715
716static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
717{
718 if (!ifbdev->cookie)
719 return;
720
721 /* Only serialises with all preceding async calls, hence +1 */
722 async_synchronize_cookie(ifbdev->cookie + 1);
723 ifbdev->cookie = 0;
Ville Syrjäläe00bf692015-11-06 15:08:33 +0200724}
725
Daniel Vetter4f256d82017-07-15 00:46:55 +0200726void intel_fbdev_unregister(struct drm_i915_private *dev_priv)
Dave Airlie38651672010-03-30 05:34:13 +0000727{
Chris Wilson43cee312016-06-21 09:16:54 +0100728 struct intel_fbdev *ifbdev = dev_priv->fbdev;
729
730 if (!ifbdev)
Dave Airlie8be48d92010-03-30 05:34:14 +0000731 return;
732
Chris Wilson0b8c0e92016-07-13 18:34:44 +0100733 cancel_work_sync(&dev_priv->fbdev_suspend_work);
Lukas Wunner366e39b2015-11-18 16:29:51 +0100734 if (!current_is_async())
Chris Wilson43cee312016-06-21 09:16:54 +0100735 intel_fbdev_sync(ifbdev);
736
Daniel Vetter4f256d82017-07-15 00:46:55 +0200737 drm_fb_helper_unregister_fbi(&ifbdev->helper);
738}
739
740void intel_fbdev_fini(struct drm_i915_private *dev_priv)
741{
742 struct intel_fbdev *ifbdev = fetch_and_zero(&dev_priv->fbdev);
743
744 if (!ifbdev)
745 return;
746
Chris Wilson43cee312016-06-21 09:16:54 +0100747 intel_fbdev_destroy(ifbdev);
Dave Airlie38651672010-03-30 05:34:13 +0000748}
Dave Airlie3fa016a2012-03-28 10:48:49 +0100749
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100750void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
Dave Airlie3fa016a2012-03-28 10:48:49 +0100751{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100752 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula1ffc5282013-05-07 18:54:05 +0300753 struct intel_fbdev *ifbdev = dev_priv->fbdev;
754 struct fb_info *info;
755
Chris Wilson15727ed2017-06-22 17:02:11 +0100756 if (!ifbdev || !ifbdev->vma)
Dave Airlie3fa016a2012-03-28 10:48:49 +0100757 return;
758
Jani Nikula1ffc5282013-05-07 18:54:05 +0300759 info = ifbdev->helper.fbdev;
760
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100761 if (synchronous) {
762 /* Flush any pending work to turn the console on, and then
763 * wait to turn it off. It must be synchronous as we are
764 * about to suspend or unload the driver.
765 *
766 * Note that from within the work-handler, we cannot flush
767 * ourselves, so only flush outstanding work upon suspend!
768 */
769 if (state != FBINFO_STATE_RUNNING)
770 flush_work(&dev_priv->fbdev_suspend_work);
771 console_lock();
772 } else {
773 /*
774 * The console lock can be pretty contented on resume due
775 * to all the printk activity. Try to keep it out of the hot
776 * path of resume if possible.
777 */
778 WARN_ON(state != FBINFO_STATE_RUNNING);
779 if (!console_trylock()) {
780 /* Don't block our own workqueue as this can
781 * be run in parallel with other i915.ko tasks.
782 */
783 schedule_work(&dev_priv->fbdev_suspend_work);
784 return;
785 }
786 }
787
Jani Nikula1ffc5282013-05-07 18:54:05 +0300788 /* On resume from hibernation: If the object is shmemfs backed, it has
789 * been restored from swap. If the object is stolen however, it will be
790 * full of whatever garbage was left in there.
791 */
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800792 if (state == FBINFO_STATE_RUNNING && ifbdev->fb->obj->stolen)
Jani Nikula1ffc5282013-05-07 18:54:05 +0300793 memset_io(info->screen_base, 0, info->screen_size);
794
Archit Taneja21cff142015-07-31 16:21:56 +0530795 drm_fb_helper_set_suspend(&ifbdev->helper, state);
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100796 console_unlock();
Dave Airlie3fa016a2012-03-28 10:48:49 +0100797}
798
Daniel Vetter0632fef2013-10-08 17:44:49 +0200799void intel_fbdev_output_poll_changed(struct drm_device *dev)
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000800{
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100801 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
802
Daniel Vetter88be58b2017-07-06 15:00:19 +0200803 if (ifbdev)
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100804 drm_fb_helper_hotplug_event(&ifbdev->helper);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000805}
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100806
Daniel Vetter0632fef2013-10-08 17:44:49 +0200807void intel_fbdev_restore_mode(struct drm_device *dev)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100808{
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100809 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100810
Rodrigo Vivid04df732015-07-08 16:25:21 -0700811 if (!ifbdev)
Ben Widawskye3c74752013-04-05 13:12:39 -0700812 return;
813
Chris Wilsone77018f2016-06-21 09:16:55 +0100814 intel_fbdev_sync(ifbdev);
Chris Wilson15727ed2017-06-22 17:02:11 +0100815 if (!ifbdev->vma)
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100816 return;
Chris Wilsone77018f2016-06-21 09:16:55 +0100817
Chris Wilsonfabef822017-02-15 10:59:19 +0000818 if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper) == 0)
819 intel_fbdev_invalidate(ifbdev);
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100820}