blob: beb08982dc0b5c3ea9711de7220c383e6ea41b92 [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,
Stefan Christa36384d2016-11-14 00:03:27 +0100105 DRM_FB_HELPER_DEFAULT_OPS,
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,
Jesse Barnes79e53942008-11-07 14:24:08 -0800112};
113
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800114static int intelfb_alloc(struct drm_fb_helper *helper,
115 struct drm_fb_helper_surface_size *sizes)
Jesse Barnes79e53942008-11-07 14:24:08 -0800116{
Ville Syrjälä34308242013-05-31 20:07:05 +0300117 struct intel_fbdev *ifbdev =
118 container_of(helper, struct intel_fbdev, helper);
Lukas Wunner4601b932015-12-19 15:40:39 +0100119 struct drm_framebuffer *fb;
Ville Syrjälä34308242013-05-31 20:07:05 +0300120 struct drm_device *dev = helper->dev;
Paulo Zanoni3badb492015-09-23 12:52:23 -0300121 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300122 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ville Syrjälä3a7f2f62012-05-24 21:08:56 +0300123 struct drm_mode_fb_cmd2 mode_cmd = {};
Paulo Zanoni3badb492015-09-23 12:52:23 -0300124 struct drm_i915_gem_object *obj = NULL;
James Simmons57084d02010-12-20 19:10:39 +0000125 int size, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -0800126
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000127 /* we don't do packed 24bpp */
Dave Airlie38651672010-03-30 05:34:13 +0000128 if (sizes->surface_bpp == 24)
129 sizes->surface_bpp = 32;
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000130
Dave Airlie38651672010-03-30 05:34:13 +0000131 mode_cmd.width = sizes->surface_width;
132 mode_cmd.height = sizes->surface_height;
Jesse Barnes79e53942008-11-07 14:24:08 -0800133
Paulo Zanonide45eaf2013-10-18 18:48:24 -0300134 mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
135 DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800136 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
137 sizes->surface_depth);
Jesse Barnes79e53942008-11-07 14:24:08 -0800138
Tvrtko Ursulin51f13852015-06-30 10:06:27 +0100139 mutex_lock(&dev->struct_mutex);
140
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800141 size = mode_cmd.pitches[0] * mode_cmd.height;
Fabian Frederick1267a262014-07-01 20:39:41 +0200142 size = PAGE_ALIGN(size);
Paulo Zanoni3badb492015-09-23 12:52:23 -0300143
144 /* If the FB is too big, just don't use it since fbdev is not very
145 * important and we should probably use that space with FBC or other
146 * features. */
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300147 if (size * 2 < ggtt->stolen_usable_size)
Paulo Zanoni3badb492015-09-23 12:52:23 -0300148 obj = i915_gem_object_create_stolen(dev, size);
Chris Wilson0ffb0ff2012-11-15 11:32:27 +0000149 if (obj == NULL)
Dave Gordond37cd8a2016-04-22 19:14:32 +0100150 obj = i915_gem_object_create(dev, size);
Chris Wilsonfe3db792016-04-25 13:32:13 +0100151 if (IS_ERR(obj)) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700152 DRM_ERROR("failed to allocate framebuffer\n");
Chris Wilsonfe3db792016-04-25 13:32:13 +0100153 ret = PTR_ERR(obj);
Jesse Barnes79e53942008-11-07 14:24:08 -0800154 goto out;
155 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800156
Daniel Vettera8bb6812014-02-10 18:00:39 +0100157 fb = __intel_framebuffer_create(dev, &mode_cmd, obj);
158 if (IS_ERR(fb)) {
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100159 i915_gem_object_put(obj);
Daniel Vettera8bb6812014-02-10 18:00:39 +0100160 ret = PTR_ERR(fb);
Lukas Wunnerca40ba852015-10-22 13:37:18 +0200161 goto out;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +0000162 }
163
Tvrtko Ursulin51f13852015-06-30 10:06:27 +0100164 mutex_unlock(&dev->struct_mutex);
165
Daniel Vettera8bb6812014-02-10 18:00:39 +0100166 ifbdev->fb = to_intel_framebuffer(fb);
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800167
168 return 0;
169
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800170out:
Tvrtko Ursulin51f13852015-06-30 10:06:27 +0100171 mutex_unlock(&dev->struct_mutex);
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800172 return ret;
173}
174
175static int intelfb_create(struct drm_fb_helper *helper,
176 struct drm_fb_helper_surface_size *sizes)
177{
178 struct intel_fbdev *ifbdev =
179 container_of(helper, struct intel_fbdev, helper);
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800180 struct intel_framebuffer *intel_fb = ifbdev->fb;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800181 struct drm_device *dev = helper->dev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300182 struct drm_i915_private *dev_priv = to_i915(dev);
David Weinehall52a05c32016-08-22 13:32:44 +0300183 struct pci_dev *pdev = dev_priv->drm.pdev;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300184 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800185 struct fb_info *info;
186 struct drm_framebuffer *fb;
Chris Wilson8ef85612016-04-28 09:56:39 +0100187 struct i915_vma *vma;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800188 bool prealloc = false;
Chris Wilson406ea8d2016-07-20 13:31:55 +0100189 void __iomem *vaddr;
Chris Wilson8ef85612016-04-28 09:56:39 +0100190 int ret;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800191
Chris Wilsonedd586f2014-04-23 08:54:31 +0100192 if (intel_fb &&
193 (sizes->fb_width > intel_fb->base.width ||
194 sizes->fb_height > intel_fb->base.height)) {
195 DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d),"
196 " releasing it\n",
197 intel_fb->base.width, intel_fb->base.height,
198 sizes->fb_width, sizes->fb_height);
199 drm_framebuffer_unreference(&intel_fb->base);
200 intel_fb = ifbdev->fb = NULL;
201 }
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800202 if (!intel_fb || WARN_ON(!intel_fb->obj)) {
Jesse Barnes48e92122013-11-26 11:25:54 -0800203 DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800204 ret = intelfb_alloc(helper, sizes);
205 if (ret)
Tvrtko Ursulin51f13852015-06-30 10:06:27 +0100206 return ret;
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800207 intel_fb = ifbdev->fb;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800208 } else {
Jesse Barnes48e92122013-11-26 11:25:54 -0800209 DRM_DEBUG_KMS("re-using BIOS fb\n");
Jesse Barnesd978ef12014-03-07 08:57:51 -0800210 prealloc = true;
Jesse Barnes8b4f49e2013-11-25 15:51:16 -0800211 sizes->fb_width = intel_fb->base.width;
212 sizes->fb_height = intel_fb->base.height;
213 }
214
Tvrtko Ursulin51f13852015-06-30 10:06:27 +0100215 mutex_lock(&dev->struct_mutex);
216
Chris Wilson0c823122015-12-04 16:05:26 +0000217 /* Pin the GGTT vma for our access via info->screen_base.
218 * This also validates that any existing fb inherited from the
219 * BIOS is suitable for own access.
220 */
Chris Wilson058d88c2016-08-15 10:49:06 +0100221 vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base, DRM_ROTATE_0);
222 if (IS_ERR(vma)) {
223 ret = PTR_ERR(vma);
Chris Wilson0c823122015-12-04 16:05:26 +0000224 goto out_unlock;
Chris Wilson058d88c2016-08-15 10:49:06 +0100225 }
Chris Wilson0c823122015-12-04 16:05:26 +0000226
Archit Taneja21cff142015-07-31 16:21:56 +0530227 info = drm_fb_helper_alloc_fbi(helper);
228 if (IS_ERR(info)) {
Lukas Wunner366e39b2015-11-18 16:29:51 +0100229 DRM_ERROR("Failed to allocate fb_info\n");
Archit Taneja21cff142015-07-31 16:21:56 +0530230 ret = PTR_ERR(info);
Chris Wilsonb4476f52009-02-11 14:26:36 +0000231 goto out_unpin;
Jesse Barnes79e53942008-11-07 14:24:08 -0800232 }
233
Ville Syrjälä34308242013-05-31 20:07:05 +0300234 info->par = helper;
Jesse Barnes79e53942008-11-07 14:24:08 -0800235
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800236 fb = &ifbdev->fb->base;
Dave Airlie38651672010-03-30 05:34:13 +0000237
238 ifbdev->helper.fb = fb;
Dave Airlie785b93e2009-08-28 15:46:53 +1000239
Jesse Barnes79e53942008-11-07 14:24:08 -0800240 strcpy(info->fix.id, "inteldrmfb");
Jesse Barnes79e53942008-11-07 14:24:08 -0800241
Jesse Barnes8fd4bd22010-06-23 12:56:12 -0700242 info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
Jesse Barnes79e53942008-11-07 14:24:08 -0800243 info->fbops = &intelfb_ops;
244
Dave Airlie4410f392009-06-16 15:34:38 -0700245 /* setup aperture base/size for vesafb takeover */
Marcin Slusarz1471ca92010-05-16 17:27:03 +0200246 info->apertures->ranges[0].base = dev->mode_config.fb_base;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300247 info->apertures->ranges[0].size = ggtt->mappable_end;
Dave Airlie4410f392009-06-16 15:34:38 -0700248
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100249 info->fix.smem_start = dev->mode_config.fb_base + i915_ggtt_offset(vma);
Chris Wilson8ef85612016-04-28 09:56:39 +0100250 info->fix.smem_len = vma->node.size;
Jesse Barnes79e53942008-11-07 14:24:08 -0800251
Chris Wilson8ef85612016-04-28 09:56:39 +0100252 vaddr = i915_vma_pin_iomap(vma);
253 if (IS_ERR(vaddr)) {
Lukas Wunner366e39b2015-11-18 16:29:51 +0100254 DRM_ERROR("Failed to remap framebuffer into virtual memory\n");
Chris Wilson8ef85612016-04-28 09:56:39 +0100255 ret = PTR_ERR(vaddr);
Archit Taneja21cff142015-07-31 16:21:56 +0530256 goto out_destroy_fbi;
Jesse Barnes79e53942008-11-07 14:24:08 -0800257 }
Chris Wilson8ef85612016-04-28 09:56:39 +0100258 info->screen_base = vaddr;
259 info->screen_size = vma->node.size;
Jesse Barnes79e53942008-11-07 14:24:08 -0800260
Jesse Barnes24576d22013-03-26 09:25:45 -0700261 /* This driver doesn't need a VT switch to restore the mode on resume */
262 info->skip_vt_switch = true;
263
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200264 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
Dave Airlie38651672010-03-30 05:34:13 +0000265 drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height);
Jesse Barnes79e53942008-11-07 14:24:08 -0800266
Chris Wilson88afe712012-12-16 12:15:41 +0000267 /* If the object is shmemfs backed, it will have given us zeroed pages.
268 * If the object is stolen however, it will be full of whatever
269 * garbage was left in there.
270 */
Chris Wilson058d88c2016-08-15 10:49:06 +0100271 if (intel_fb->obj->stolen && !prealloc)
Chris Wilson88afe712012-12-16 12:15:41 +0000272 memset_io(info->screen_base, 0, info->screen_size);
273
Sascha Hauerfb2a99e2012-02-06 10:58:19 +0100274 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
Jesse Barnes79e53942008-11-07 14:24:08 -0800275
Chris Wilsonbde13eb2016-08-15 10:49:07 +0100276 DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08x\n",
277 fb->width, fb->height, i915_ggtt_offset(vma));
Chris Wilson058d88c2016-08-15 10:49:06 +0100278 ifbdev->vma = vma;
Jesse Barnes79e53942008-11-07 14:24:08 -0800279
280 mutex_unlock(&dev->struct_mutex);
David Weinehall52a05c32016-08-22 13:32:44 +0300281 vga_switcheroo_client_fb_set(pdev, info);
Jesse Barnes79e53942008-11-07 14:24:08 -0800282 return 0;
283
Archit Taneja21cff142015-07-31 16:21:56 +0530284out_destroy_fbi:
285 drm_fb_helper_release_fbi(helper);
Chris Wilsonb4476f52009-02-11 14:26:36 +0000286out_unpin:
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300287 intel_unpin_fb_obj(&ifbdev->fb->base, DRM_ROTATE_0);
Chris Wilson0c823122015-12-04 16:05:26 +0000288out_unlock:
Jesse Barnes79e53942008-11-07 14:24:08 -0800289 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -0800290 return ret;
291}
292
Paulo Zanoni67437682013-09-24 13:52:56 -0300293/** Sets the color ramps on behalf of RandR */
294static void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
295 u16 blue, int regno)
296{
297 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
298
299 intel_crtc->lut_r[regno] = red >> 8;
300 intel_crtc->lut_g[regno] = green >> 8;
301 intel_crtc->lut_b[regno] = blue >> 8;
302}
303
304static void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
305 u16 *blue, int regno)
306{
307 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
308
309 *red = intel_crtc->lut_r[regno] << 8;
310 *green = intel_crtc->lut_g[regno] << 8;
311 *blue = intel_crtc->lut_b[regno] << 8;
312}
313
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800314static struct drm_fb_helper_crtc *
315intel_fb_helper_crtc(struct drm_fb_helper *fb_helper, struct drm_crtc *crtc)
316{
317 int i;
318
319 for (i = 0; i < fb_helper->crtc_count; i++)
320 if (fb_helper->crtc_info[i].mode_set.crtc == crtc)
321 return &fb_helper->crtc_info[i];
322
323 return NULL;
324}
325
326/*
327 * Try to read the BIOS display configuration and use it for the initial
328 * fb configuration.
329 *
330 * The BIOS or boot loader will generally create an initial display
331 * configuration for us that includes some set of active pipes and displays.
332 * This routine tries to figure out which pipes and connectors are active
333 * and stuffs them into the crtcs and modes array given to us by the
334 * drm_fb_helper code.
335 *
336 * The overall sequence is:
337 * intel_fbdev_init - from driver load
338 * intel_fbdev_init_bios - initialize the intel_fbdev using BIOS data
339 * drm_fb_helper_init - build fb helper structs
340 * drm_fb_helper_single_add_all_connectors - more fb helper structs
341 * intel_fbdev_initial_config - apply the config
342 * drm_fb_helper_initial_config - call ->probe then register_framebuffer()
343 * drm_setup_crtcs - build crtc config for fbdev
344 * intel_fb_initial_config - find active connectors etc
345 * drm_fb_helper_single_fb_probe - set up fbdev
346 * intelfb_create - re-use or alloc fb, build out fbdev structs
347 *
348 * Note that we don't make special consideration whether we could actually
349 * switch to the selected modes without a full modeset. E.g. when the display
350 * is in VGA mode we need to recalculate watermarks and set a new high-res
351 * framebuffer anyway.
352 */
353static bool intel_fb_initial_config(struct drm_fb_helper *fb_helper,
354 struct drm_fb_helper_crtc **crtcs,
355 struct drm_display_mode **modes,
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000356 struct drm_fb_offset *offsets,
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800357 bool *enabled, int width, int height)
358{
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +0000359 struct drm_i915_private *dev_priv = to_i915(fb_helper->dev);
Chris Wilson86f40bb2016-07-02 15:36:02 +0100360 unsigned long conn_configured, mask;
361 unsigned int count = min(fb_helper->connector_count, BITS_PER_LONG);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800362 int i, j;
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800363 bool *save_enabled;
Daniel Vetter7c2bb532014-03-04 21:08:41 +0100364 bool fallback = true;
Daniel Vetter7e696e42014-03-04 21:08:42 +0100365 int num_connectors_enabled = 0;
366 int num_connectors_detected = 0;
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000367 int pass = 0;
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800368
Chris Wilson86f40bb2016-07-02 15:36:02 +0100369 save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL);
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800370 if (!save_enabled)
371 return false;
372
Chris Wilson86f40bb2016-07-02 15:36:02 +0100373 memcpy(save_enabled, enabled, count);
374 mask = BIT(count) - 1;
375 conn_configured = 0;
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000376retry:
Chris Wilson86f40bb2016-07-02 15:36:02 +0100377 for (i = 0; i < count; i++) {
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800378 struct drm_fb_helper_connector *fb_conn;
379 struct drm_connector *connector;
380 struct drm_encoder *encoder;
381 struct drm_fb_helper_crtc *new_crtc;
Lionel Landwerlin82cf4352016-03-16 10:57:16 +0000382 struct intel_crtc *intel_crtc;
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800383
384 fb_conn = fb_helper->connector_info[i];
385 connector = fb_conn->connector;
Daniel Vetter7e696e42014-03-04 21:08:42 +0100386
Chris Wilson86f40bb2016-07-02 15:36:02 +0100387 if (conn_configured & BIT(i))
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000388 continue;
389
390 if (pass == 0 && !connector->has_tile)
391 continue;
392
Daniel Vetter7e696e42014-03-04 21:08:42 +0100393 if (connector->status == connector_status_connected)
394 num_connectors_detected++;
395
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800396 if (!enabled[i]) {
Chris Wilsonc0c97622014-05-13 13:45:09 +0100397 DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300398 connector->name);
Chris Wilson86f40bb2016-07-02 15:36:02 +0100399 conn_configured |= BIT(i);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800400 continue;
401 }
402
Chris Wilsond84a0f32014-08-18 10:35:29 -0700403 if (connector->force == DRM_FORCE_OFF) {
404 DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
405 connector->name);
406 enabled[i] = false;
407 continue;
408 }
409
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100410 encoder = connector->state->best_encoder;
411 if (!encoder || WARN_ON(!connector->state->crtc)) {
Chris Wilsond84a0f32014-08-18 10:35:29 -0700412 if (connector->force > DRM_FORCE_OFF)
413 goto bail;
414
Chris Wilsonc0c97622014-05-13 13:45:09 +0100415 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300416 connector->name);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800417 enabled[i] = false;
Chris Wilson86f40bb2016-07-02 15:36:02 +0100418 conn_configured |= BIT(i);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800419 continue;
420 }
421
Daniel Vetter7e696e42014-03-04 21:08:42 +0100422 num_connectors_enabled++;
423
Lionel Landwerlin82cf4352016-03-16 10:57:16 +0000424 intel_crtc = to_intel_crtc(connector->state->crtc);
425 for (j = 0; j < 256; j++) {
426 intel_crtc->lut_r[j] = j;
427 intel_crtc->lut_g[j] = j;
428 intel_crtc->lut_b[j] = j;
429 }
430
Chris Wilson86f40bb2016-07-02 15:36:02 +0100431 new_crtc = intel_fb_helper_crtc(fb_helper,
432 connector->state->crtc);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800433
434 /*
435 * Make sure we're not trying to drive multiple connectors
436 * with a single CRTC, since our cloning support may not
437 * match the BIOS.
438 */
Chris Wilson86f40bb2016-07-02 15:36:02 +0100439 for (j = 0; j < count; j++) {
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800440 if (crtcs[j] == new_crtc) {
Daniel Vetter7e696e42014-03-04 21:08:42 +0100441 DRM_DEBUG_KMS("fallback: cloned configuration\n");
Chris Wilsond84a0f32014-08-18 10:35:29 -0700442 goto bail;
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800443 }
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800444 }
445
Chris Wilsonc0c97622014-05-13 13:45:09 +0100446 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300447 connector->name);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800448
449 /* go for command line mode first */
450 modes[i] = drm_pick_cmdline_mode(fb_conn, width, height);
451
452 /* try for preferred next */
453 if (!modes[i]) {
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000454 DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
455 connector->name, connector->has_tile);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800456 modes[i] = drm_has_preferred_mode(fb_conn, width,
457 height);
458 }
459
Chris Wilsonafba0b52014-05-13 16:07:37 +0100460 /* No preferred mode marked by the EDID? Are there any modes? */
461 if (!modes[i] && !list_empty(&connector->modes)) {
462 DRM_DEBUG_KMS("using first mode listed on connector %s\n",
Dave Airlie8d4ad9d2014-06-05 20:28:59 +1000463 connector->name);
Chris Wilsonafba0b52014-05-13 16:07:37 +0100464 modes[i] = list_first_entry(&connector->modes,
465 struct drm_display_mode,
466 head);
467 }
468
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800469 /* last resort: use current mode */
470 if (!modes[i]) {
471 /*
472 * IMPORTANT: We want to use the adjusted mode (i.e.
473 * after the panel fitter upscaling) as the initial
474 * config, not the input mode, which is what crtc->mode
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +0200475 * usually contains. But since our current
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800476 * code puts a mode derived from the post-pfit timings
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +0200477 * into crtc->mode this works out correctly.
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100478 *
479 * This is crtc->mode and not crtc->state->mode for the
480 * fastboot check to work correctly. crtc_state->mode has
481 * I915_MODE_FLAG_INHERITED, which we clear to force check
482 * state.
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800483 */
Chris Wilsonc0c97622014-05-13 13:45:09 +0100484 DRM_DEBUG_KMS("looking for current mode on connector %s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300485 connector->name);
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100486 modes[i] = &connector->state->crtc->mode;
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800487 }
488 crtcs[i] = new_crtc;
489
Ville Syrjälä78108b72016-05-27 20:59:19 +0300490 DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n",
Jani Nikulac23cc412014-06-03 14:56:17 +0300491 connector->name,
Maarten Lankhorste28661b2016-02-17 09:18:39 +0100492 connector->state->crtc->base.id,
Ville Syrjälä78108b72016-05-27 20:59:19 +0300493 connector->state->crtc->name,
Chris Wilsonc0c97622014-05-13 13:45:09 +0100494 modes[i]->hdisplay, modes[i]->vdisplay,
495 modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" :"");
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800496
Daniel Vetter7c2bb532014-03-04 21:08:41 +0100497 fallback = false;
Chris Wilson86f40bb2016-07-02 15:36:02 +0100498 conn_configured |= BIT(i);
Dave Airlieb0ee9e72014-10-20 16:31:53 +1000499 }
500
501 if ((conn_configured & mask) != mask) {
502 pass++;
503 goto retry;
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800504 }
505
Daniel Vetter7e696e42014-03-04 21:08:42 +0100506 /*
507 * If the BIOS didn't enable everything it could, fall back to have the
508 * same user experiencing of lighting up as much as possible like the
509 * fbdev helper library.
510 */
511 if (num_connectors_enabled != num_connectors_detected &&
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +0000512 num_connectors_enabled < INTEL_INFO(dev_priv)->num_pipes) {
Daniel Vetter7e696e42014-03-04 21:08:42 +0100513 DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
514 DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
515 num_connectors_detected);
516 fallback = true;
517 }
518
Daniel Vetter7c2bb532014-03-04 21:08:41 +0100519 if (fallback) {
Chris Wilsond84a0f32014-08-18 10:35:29 -0700520bail:
Daniel Vetter7e696e42014-03-04 21:08:42 +0100521 DRM_DEBUG_KMS("Not using firmware configuration\n");
Chris Wilson86f40bb2016-07-02 15:36:02 +0100522 memcpy(enabled, save_enabled, count);
Jesse Barnes02f5eeb2014-02-12 15:03:40 -0800523 kfree(save_enabled);
524 return false;
525 }
526
527 kfree(save_enabled);
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800528 return true;
529}
530
Thierry Reding3a493872014-06-27 17:19:23 +0200531static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
Jesse Barneseb1bfe82014-02-12 12:26:25 -0800532 .initial_config = intel_fb_initial_config,
Dave Airlie4abe3522010-03-30 05:34:18 +0000533 .gamma_set = intel_crtc_fb_gamma_set,
534 .gamma_get = intel_crtc_fb_gamma_get,
Daniel Vettercd5428a2013-01-21 23:42:49 +0100535 .fb_probe = intelfb_create,
Dave Airlie4abe3522010-03-30 05:34:18 +0000536};
537
Chris Wilson43cee312016-06-21 09:16:54 +0100538static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
Jesse Barnes79e53942008-11-07 14:24:08 -0800539{
Chris Wilson0c823122015-12-04 16:05:26 +0000540 /* We rely on the object-free to release the VMA pinning for
541 * the info->screen_base mmaping. Leaking the VMA is simpler than
542 * trying to rectify all the possible error paths leading here.
543 */
Chris Wilsonddfe1562013-08-06 17:43:07 +0100544
Archit Taneja21cff142015-07-31 16:21:56 +0530545 drm_fb_helper_unregister_fbi(&ifbdev->helper);
546 drm_fb_helper_release_fbi(&ifbdev->helper);
Jesse Barnes79e53942008-11-07 14:24:08 -0800547
Dave Airlie4abe3522010-03-30 05:34:18 +0000548 drm_fb_helper_fini(&ifbdev->helper);
Jesse Barnes79e53942008-11-07 14:24:08 -0800549
Lukas Wunner54632ab2015-11-18 13:43:20 +0100550 if (ifbdev->fb) {
Chris Wilson43cee312016-06-21 09:16:54 +0100551 mutex_lock(&ifbdev->helper.dev->struct_mutex);
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +0300552 intel_unpin_fb_obj(&ifbdev->fb->base, DRM_ROTATE_0);
Chris Wilson43cee312016-06-21 09:16:54 +0100553 mutex_unlock(&ifbdev->helper.dev->struct_mutex);
Chris Wilsonfb4b8ce2016-04-28 09:56:35 +0100554
Lukas Wunner54632ab2015-11-18 13:43:20 +0100555 drm_framebuffer_remove(&ifbdev->fb->base);
556 }
Chris Wilson43cee312016-06-21 09:16:54 +0100557
558 kfree(ifbdev);
Jesse Barnes79e53942008-11-07 14:24:08 -0800559}
Dave Airlie38651672010-03-30 05:34:13 +0000560
Jesse Barnesd978ef12014-03-07 08:57:51 -0800561/*
562 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
563 * The core display code will have read out the current plane configuration,
564 * so we use that to figure out if there's an object for us to use as the
565 * fb, and if so, we re-use it for the fbdev configuration.
566 *
567 * Note we only support a single fb shared across pipes for boot (mostly for
568 * fbcon), so we just find the biggest and use that.
569 */
570static bool intel_fbdev_init_bios(struct drm_device *dev,
571 struct intel_fbdev *ifbdev)
572{
573 struct intel_framebuffer *fb = NULL;
574 struct drm_crtc *crtc;
575 struct intel_crtc *intel_crtc;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800576 unsigned int max_size = 0;
577
Jesse Barnesd978ef12014-03-07 08:57:51 -0800578 /* Find the largest fb */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100579 for_each_crtc(dev, crtc) {
Maarten Lankhorst8e9ba312015-07-13 16:30:16 +0200580 struct drm_i915_gem_object *obj =
581 intel_fb_obj(crtc->primary->state->fb);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800582 intel_crtc = to_intel_crtc(crtc);
583
Maarten Lankhorstd5515992015-08-27 15:44:03 +0200584 if (!crtc->state->active || !obj) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800585 DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
586 pipe_name(intel_crtc->pipe));
587 continue;
588 }
589
Maarten Lankhorst8e9ba312015-07-13 16:30:16 +0200590 if (obj->base.size > max_size) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800591 DRM_DEBUG_KMS("found possible fb from plane %c\n",
592 pipe_name(intel_crtc->pipe));
Maarten Lankhorst8e9ba312015-07-13 16:30:16 +0200593 fb = to_intel_framebuffer(crtc->primary->state->fb);
594 max_size = obj->base.size;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800595 }
596 }
597
598 if (!fb) {
599 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
600 goto out;
601 }
602
603 /* Now make sure all the pipes will fit into it */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100604 for_each_crtc(dev, crtc) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800605 unsigned int cur_size;
606
607 intel_crtc = to_intel_crtc(crtc);
608
Maarten Lankhorstd5515992015-08-27 15:44:03 +0200609 if (!crtc->state->active) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800610 DRM_DEBUG_KMS("pipe %c not active, skipping\n",
611 pipe_name(intel_crtc->pipe));
612 continue;
613 }
614
615 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
616 pipe_name(intel_crtc->pipe));
617
618 /*
619 * See if the plane fb we found above will fit on this
Chris Wilsonbc104d12014-03-20 15:11:21 +0000620 * pipe. Note we need to use the selected fb's pitch and bpp
621 * rather than the current pipe's, since they differ.
Jesse Barnesd978ef12014-03-07 08:57:51 -0800622 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200623 cur_size = intel_crtc->config->base.adjusted_mode.crtc_hdisplay;
Chris Wilsonbc104d12014-03-20 15:11:21 +0000624 cur_size = cur_size * fb->base.bits_per_pixel / 8;
625 if (fb->base.pitches[0] < cur_size) {
626 DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
627 pipe_name(intel_crtc->pipe),
628 cur_size, fb->base.pitches[0]);
Chris Wilsonbc104d12014-03-20 15:11:21 +0000629 fb = NULL;
630 break;
631 }
632
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200633 cur_size = intel_crtc->config->base.adjusted_mode.crtc_vdisplay;
Damien Lespiauec2c9812015-01-20 12:51:45 +0000634 cur_size = intel_fb_align_height(dev, cur_size,
Daniel Vetter091df6c2015-02-10 17:16:10 +0000635 fb->base.pixel_format,
Ville Syrjäläbae781b2016-11-16 13:33:16 +0200636 fb->base.modifier);
Chris Wilsonbc104d12014-03-20 15:11:21 +0000637 cur_size *= fb->base.pitches[0];
638 DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
639 pipe_name(intel_crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200640 intel_crtc->config->base.adjusted_mode.crtc_hdisplay,
641 intel_crtc->config->base.adjusted_mode.crtc_vdisplay,
Chris Wilsonbc104d12014-03-20 15:11:21 +0000642 fb->base.bits_per_pixel,
Jesse Barnesd978ef12014-03-07 08:57:51 -0800643 cur_size);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800644
645 if (cur_size > max_size) {
646 DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
647 pipe_name(intel_crtc->pipe),
648 cur_size, max_size);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800649 fb = NULL;
650 break;
651 }
652
653 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
654 pipe_name(intel_crtc->pipe),
655 max_size, cur_size);
656 }
657
Jesse Barnesd978ef12014-03-07 08:57:51 -0800658 if (!fb) {
659 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
660 goto out;
661 }
662
Jesse Barnes484b41d2014-03-07 08:57:55 -0800663 ifbdev->preferred_bpp = fb->base.bits_per_pixel;
Jesse Barnesd978ef12014-03-07 08:57:51 -0800664 ifbdev->fb = fb;
665
Jesse Barnes484b41d2014-03-07 08:57:55 -0800666 drm_framebuffer_reference(&ifbdev->fb->base);
Jesse Barnesd978ef12014-03-07 08:57:51 -0800667
668 /* Final pass to check if any active pipes don't have fbs */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100669 for_each_crtc(dev, crtc) {
Jesse Barnesd978ef12014-03-07 08:57:51 -0800670 intel_crtc = to_intel_crtc(crtc);
671
Maarten Lankhorstd5515992015-08-27 15:44:03 +0200672 if (!crtc->state->active)
Jesse Barnesd978ef12014-03-07 08:57:51 -0800673 continue;
674
Dave Airlie66e514c2014-04-03 07:51:54 +1000675 WARN(!crtc->primary->fb,
Jesse Barnesd978ef12014-03-07 08:57:51 -0800676 "re-used BIOS config but lost an fb on crtc %d\n",
677 crtc->base.id);
678 }
679
680
681 DRM_DEBUG_KMS("using BIOS fb for initial console\n");
682 return true;
683
Jesse Barnesd978ef12014-03-07 08:57:51 -0800684out:
685
686 return false;
687}
688
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100689static void intel_fbdev_suspend_worker(struct work_struct *work)
690{
Chris Wilson91c8a322016-07-05 10:40:23 +0100691 intel_fbdev_set_suspend(&container_of(work,
692 struct drm_i915_private,
693 fbdev_suspend_work)->drm,
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100694 FBINFO_STATE_RUNNING,
695 true);
696}
697
Dave Airlie38651672010-03-30 05:34:13 +0000698int intel_fbdev_init(struct drm_device *dev)
699{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100700 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +0000701 struct intel_fbdev *ifbdev;
Chris Wilson5a793952010-06-06 10:50:03 +0100702 int ret;
Dave Airlie8be48d92010-03-30 05:34:14 +0000703
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +0000704 if (WARN_ON(INTEL_INFO(dev_priv)->num_pipes == 0))
Jesse Barnesd978ef12014-03-07 08:57:51 -0800705 return -ENODEV;
706
707 ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
708 if (ifbdev == NULL)
Dave Airlie8be48d92010-03-30 05:34:14 +0000709 return -ENOMEM;
710
Thierry Reding10a23102014-06-27 17:19:24 +0200711 drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
712
Jesse Barnesd978ef12014-03-07 08:57:51 -0800713 if (!intel_fbdev_init_bios(dev, ifbdev))
714 ifbdev->preferred_bpp = 32;
Dave Airlie8be48d92010-03-30 05:34:14 +0000715
Chris Wilson5a793952010-06-06 10:50:03 +0100716 ret = drm_fb_helper_init(dev, &ifbdev->helper,
Tvrtko Ursulinb7f05d42016-11-09 11:30:45 +0000717 INTEL_INFO(dev_priv)->num_pipes, 4);
Chris Wilson5a793952010-06-06 10:50:03 +0100718 if (ret) {
719 kfree(ifbdev);
720 return ret;
721 }
Dave Airlie4abe3522010-03-30 05:34:18 +0000722
Jesse Barnesd978ef12014-03-07 08:57:51 -0800723 dev_priv->fbdev = ifbdev;
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100724 INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
725
Dave Airlie0b4c0f32010-03-30 05:34:15 +0000726 drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
Daniel Vetter20afbda2012-12-11 14:05:07 +0100727
Dave Airlie38651672010-03-30 05:34:13 +0000728 return 0;
729}
730
Ville Syrjäläe00bf692015-11-06 15:08:33 +0200731static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
Daniel Vetter20afbda2012-12-11 14:05:07 +0100732{
Chris Wilson43cee312016-06-21 09:16:54 +0100733 struct intel_fbdev *ifbdev = data;
Daniel Vetter20afbda2012-12-11 14:05:07 +0100734
735 /* Due to peculiar init order wrt to hpd handling this is separate. */
Lukas Wunner366e39b2015-11-18 16:29:51 +0100736 if (drm_fb_helper_initial_config(&ifbdev->helper,
737 ifbdev->preferred_bpp))
Chris Wilson43cee312016-06-21 09:16:54 +0100738 intel_fbdev_fini(ifbdev->helper.dev);
Daniel Vetter20afbda2012-12-11 14:05:07 +0100739}
740
Ville Syrjäläe00bf692015-11-06 15:08:33 +0200741void intel_fbdev_initial_config_async(struct drm_device *dev)
742{
Chris Wilson43cee312016-06-21 09:16:54 +0100743 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
744
745 ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
746}
747
748static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
749{
750 if (!ifbdev->cookie)
751 return;
752
753 /* Only serialises with all preceding async calls, hence +1 */
754 async_synchronize_cookie(ifbdev->cookie + 1);
755 ifbdev->cookie = 0;
Ville Syrjäläe00bf692015-11-06 15:08:33 +0200756}
757
Dave Airlie38651672010-03-30 05:34:13 +0000758void intel_fbdev_fini(struct drm_device *dev)
759{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100760 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson43cee312016-06-21 09:16:54 +0100761 struct intel_fbdev *ifbdev = dev_priv->fbdev;
762
763 if (!ifbdev)
Dave Airlie8be48d92010-03-30 05:34:14 +0000764 return;
765
Chris Wilson0b8c0e92016-07-13 18:34:44 +0100766 cancel_work_sync(&dev_priv->fbdev_suspend_work);
Lukas Wunner366e39b2015-11-18 16:29:51 +0100767 if (!current_is_async())
Chris Wilson43cee312016-06-21 09:16:54 +0100768 intel_fbdev_sync(ifbdev);
769
770 intel_fbdev_destroy(ifbdev);
Dave Airlie38651672010-03-30 05:34:13 +0000771 dev_priv->fbdev = NULL;
772}
Dave Airlie3fa016a2012-03-28 10:48:49 +0100773
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100774void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
Dave Airlie3fa016a2012-03-28 10:48:49 +0100775{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100776 struct drm_i915_private *dev_priv = to_i915(dev);
Jani Nikula1ffc5282013-05-07 18:54:05 +0300777 struct intel_fbdev *ifbdev = dev_priv->fbdev;
778 struct fb_info *info;
779
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100780 if (!ifbdev || !ifbdev->fb)
Dave Airlie3fa016a2012-03-28 10:48:49 +0100781 return;
782
Jani Nikula1ffc5282013-05-07 18:54:05 +0300783 info = ifbdev->helper.fbdev;
784
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100785 if (synchronous) {
786 /* Flush any pending work to turn the console on, and then
787 * wait to turn it off. It must be synchronous as we are
788 * about to suspend or unload the driver.
789 *
790 * Note that from within the work-handler, we cannot flush
791 * ourselves, so only flush outstanding work upon suspend!
792 */
793 if (state != FBINFO_STATE_RUNNING)
794 flush_work(&dev_priv->fbdev_suspend_work);
795 console_lock();
796 } else {
797 /*
798 * The console lock can be pretty contented on resume due
799 * to all the printk activity. Try to keep it out of the hot
800 * path of resume if possible.
801 */
802 WARN_ON(state != FBINFO_STATE_RUNNING);
803 if (!console_trylock()) {
804 /* Don't block our own workqueue as this can
805 * be run in parallel with other i915.ko tasks.
806 */
807 schedule_work(&dev_priv->fbdev_suspend_work);
808 return;
809 }
810 }
811
Jani Nikula1ffc5282013-05-07 18:54:05 +0300812 /* On resume from hibernation: If the object is shmemfs backed, it has
813 * been restored from swap. If the object is stolen however, it will be
814 * full of whatever garbage was left in there.
815 */
Jesse Barnes8bcd4552014-02-07 12:10:38 -0800816 if (state == FBINFO_STATE_RUNNING && ifbdev->fb->obj->stolen)
Jani Nikula1ffc5282013-05-07 18:54:05 +0300817 memset_io(info->screen_base, 0, info->screen_size);
818
Archit Taneja21cff142015-07-31 16:21:56 +0530819 drm_fb_helper_set_suspend(&ifbdev->helper, state);
Chris Wilson82e3b8c2014-08-13 13:09:46 +0100820 console_unlock();
Dave Airlie3fa016a2012-03-28 10:48:49 +0100821}
822
Daniel Vetter0632fef2013-10-08 17:44:49 +0200823void intel_fbdev_output_poll_changed(struct drm_device *dev)
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000824{
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100825 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
826
827 if (ifbdev && ifbdev->fb)
828 drm_fb_helper_hotplug_event(&ifbdev->helper);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000829}
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100830
Daniel Vetter0632fef2013-10-08 17:44:49 +0200831void intel_fbdev_restore_mode(struct drm_device *dev)
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100832{
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100833 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100834
Rodrigo Vivid04df732015-07-08 16:25:21 -0700835 if (!ifbdev)
Ben Widawskye3c74752013-04-05 13:12:39 -0700836 return;
837
Chris Wilsone77018f2016-06-21 09:16:55 +0100838 intel_fbdev_sync(ifbdev);
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100839 if (!ifbdev->fb)
840 return;
Chris Wilsone77018f2016-06-21 09:16:55 +0100841
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100842 if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper)) {
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100843 DRM_DEBUG("failed to restore crtc mode\n");
Rodrigo Vivid04df732015-07-08 16:25:21 -0700844 } else {
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100845 mutex_lock(&dev->struct_mutex);
Rodrigo Vivid04df732015-07-08 16:25:21 -0700846 intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT);
Chris Wilsonc45eb4f2016-07-13 18:34:45 +0100847 mutex_unlock(&dev->struct_mutex);
Rodrigo Vivid04df732015-07-08 16:25:21 -0700848 }
Dave Airliee8e7a2b2011-04-21 22:18:32 +0100849}