drm/i915: Fix double unref in intelfb_alloc failure path
[deliverable/linux.git] / drivers / gpu / drm / i915 / intel_fbdev.c
CommitLineData
79e53945
JB
1/*
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
d1d70677 27#include <linux/async.h>
79e53945
JB
28#include <linux/module.h>
29#include <linux/kernel.h>
82e3b8c1 30#include <linux/console.h>
79e53945
JB
31#include <linux/errno.h>
32#include <linux/string.h>
33#include <linux/mm.h>
34#include <linux/tty.h>
79e53945
JB
35#include <linux/sysrq.h>
36#include <linux/delay.h>
37#include <linux/fb.h>
38#include <linux/init.h>
6a9ee8af 39#include <linux/vga_switcheroo.h>
79e53945 40
760285e7
DH
41#include <drm/drmP.h>
42#include <drm/drm_crtc.h>
43#include <drm/drm_fb_helper.h>
79e53945 44#include "intel_drv.h"
760285e7 45#include <drm/i915_drm.h>
79e53945
JB
46#include "i915_drv.h"
47
e991077e
DV
48static 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) {
e991077e 58 mutex_lock(&fb_helper->dev->struct_mutex);
aba6da3e 59 intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT);
e991077e
DV
60 mutex_unlock(&fb_helper->dev->struct_mutex);
61 }
62
63 return ret;
64}
65
03e515f7
RV
66static 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) {
03e515f7 76 mutex_lock(&fb_helper->dev->struct_mutex);
77a0d1ca 77 intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT);
03e515f7
RV
78 mutex_unlock(&fb_helper->dev->struct_mutex);
79 }
80
81 return ret;
82}
83
d9a946b5
RV
84static 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) {
d9a946b5 95 mutex_lock(&fb_helper->dev->struct_mutex);
77a0d1ca 96 intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT);
d9a946b5
RV
97 mutex_unlock(&fb_helper->dev->struct_mutex);
98 }
99
100 return ret;
101}
102
79e53945
JB
103static struct fb_ops intelfb_ops = {
104 .owner = THIS_MODULE,
785b93ef 105 .fb_check_var = drm_fb_helper_check_var,
e991077e 106 .fb_set_par = intel_fbdev_set_par,
21cff148
AT
107 .fb_fillrect = drm_fb_helper_cfb_fillrect,
108 .fb_copyarea = drm_fb_helper_cfb_copyarea,
109 .fb_imageblit = drm_fb_helper_cfb_imageblit,
d9a946b5 110 .fb_pan_display = intel_fbdev_pan_display,
03e515f7 111 .fb_blank = intel_fbdev_blank,
068143d3 112 .fb_setcmap = drm_fb_helper_setcmap,
81255565
JB
113 .fb_debug_enter = drm_fb_helper_debug_enter,
114 .fb_debug_leave = drm_fb_helper_debug_leave,
79e53945
JB
115};
116
8b4f49e0
JB
117static int intelfb_alloc(struct drm_fb_helper *helper,
118 struct drm_fb_helper_surface_size *sizes)
79e53945 119{
3430824b
VS
120 struct intel_fbdev *ifbdev =
121 container_of(helper, struct intel_fbdev, helper);
a8bb6818 122 struct drm_framebuffer *fb;
3430824b 123 struct drm_device *dev = helper->dev;
3badb49f 124 struct drm_i915_private *dev_priv = to_i915(dev);
3a7f2f6a 125 struct drm_mode_fb_cmd2 mode_cmd = {};
3badb49f 126 struct drm_i915_gem_object *obj = NULL;
57084d05 127 int size, ret;
79e53945 128
b8c00ac5 129 /* we don't do packed 24bpp */
38651674
DA
130 if (sizes->surface_bpp == 24)
131 sizes->surface_bpp = 32;
b8c00ac5 132
38651674
DA
133 mode_cmd.width = sizes->surface_width;
134 mode_cmd.height = sizes->surface_height;
79e53945 135
de45eaf7
PZ
136 mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
137 DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
308e5bcb
JB
138 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
139 sizes->surface_depth);
79e53945 140
308e5bcb 141 size = mode_cmd.pitches[0] * mode_cmd.height;
1267a26b 142 size = PAGE_ALIGN(size);
3badb49f
PZ
143
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. */
147 if (size * 2 < dev_priv->gtt.stolen_usable_size)
148 obj = i915_gem_object_create_stolen(dev, size);
0ffb0ff2
CW
149 if (obj == NULL)
150 obj = i915_gem_alloc_object(dev, size);
05394f39 151 if (!obj) {
1ae8c0a5 152 DRM_ERROR("failed to allocate framebuffer\n");
79e53945
JB
153 ret = -ENOMEM;
154 goto out;
155 }
79e53945 156
a8bb6818
DV
157 fb = __intel_framebuffer_create(dev, &mode_cmd, obj);
158 if (IS_ERR(fb)) {
ca40ba85 159 drm_gem_object_unreference(&obj->base);
a8bb6818 160 ret = PTR_ERR(fb);
ca40ba85 161 goto out;
850c4cdc
TU
162 }
163
164 /* Flush everything out, we'll be doing GTT only from now on */
7580d774 165 ret = intel_pin_and_fence_fb_obj(NULL, fb, NULL);
850c4cdc
TU
166 if (ret) {
167 DRM_ERROR("failed to pin obj: %d\n", ret);
168 goto out_fb;
a8bb6818
DV
169 }
170
171 ifbdev->fb = to_intel_framebuffer(fb);
8b4f49e0
JB
172
173 return 0;
174
850c4cdc
TU
175out_fb:
176 drm_framebuffer_remove(fb);
8b4f49e0
JB
177out:
178 return ret;
179}
180
181static int intelfb_create(struct drm_fb_helper *helper,
182 struct drm_fb_helper_surface_size *sizes)
183{
184 struct intel_fbdev *ifbdev =
185 container_of(helper, struct intel_fbdev, helper);
8bcd4553 186 struct intel_framebuffer *intel_fb = ifbdev->fb;
8b4f49e0
JB
187 struct drm_device *dev = helper->dev;
188 struct drm_i915_private *dev_priv = dev->dev_private;
189 struct fb_info *info;
190 struct drm_framebuffer *fb;
191 struct drm_i915_gem_object *obj;
192 int size, ret;
d978ef14 193 bool prealloc = false;
8b4f49e0
JB
194
195 mutex_lock(&dev->struct_mutex);
196
edd586fe
CW
197 if (intel_fb &&
198 (sizes->fb_width > intel_fb->base.width ||
199 sizes->fb_height > intel_fb->base.height)) {
200 DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d),"
201 " releasing it\n",
202 intel_fb->base.width, intel_fb->base.height,
203 sizes->fb_width, sizes->fb_height);
204 drm_framebuffer_unreference(&intel_fb->base);
205 intel_fb = ifbdev->fb = NULL;
206 }
8bcd4553 207 if (!intel_fb || WARN_ON(!intel_fb->obj)) {
48e92120 208 DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
8b4f49e0
JB
209 ret = intelfb_alloc(helper, sizes);
210 if (ret)
211 goto out_unlock;
8bcd4553 212 intel_fb = ifbdev->fb;
8b4f49e0 213 } else {
48e92120 214 DRM_DEBUG_KMS("re-using BIOS fb\n");
d978ef14 215 prealloc = true;
8b4f49e0
JB
216 sizes->fb_width = intel_fb->base.width;
217 sizes->fb_height = intel_fb->base.height;
218 }
219
220 obj = intel_fb->obj;
221 size = obj->base.size;
222
21cff148
AT
223 info = drm_fb_helper_alloc_fbi(helper);
224 if (IS_ERR(info)) {
225 ret = PTR_ERR(info);
b4476f52 226 goto out_unpin;
79e53945
JB
227 }
228
3430824b 229 info->par = helper;
79e53945 230
8bcd4553 231 fb = &ifbdev->fb->base;
79e53945 232
38651674 233 ifbdev->helper.fb = fb;
785b93ef 234
79e53945 235 strcpy(info->fix.id, "inteldrmfb");
79e53945 236
8fd4bd22 237 info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
79e53945
JB
238 info->fbops = &intelfb_ops;
239
4410f391 240 /* setup aperture base/size for vesafb takeover */
1471ca9a 241 info->apertures->ranges[0].base = dev->mode_config.fb_base;
93d18799 242 info->apertures->ranges[0].size = dev_priv->gtt.mappable_end;
4410f391 243
f343c5f6 244 info->fix.smem_start = dev->mode_config.fb_base + i915_gem_obj_ggtt_offset(obj);
79e53945
JB
245 info->fix.smem_len = size;
246
dd2757f8 247 info->screen_base =
f343c5f6 248 ioremap_wc(dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj),
dd2757f8 249 size);
79e53945
JB
250 if (!info->screen_base) {
251 ret = -ENOSPC;
21cff148 252 goto out_destroy_fbi;
79e53945
JB
253 }
254 info->screen_size = size;
255
24576d23
JB
256 /* This driver doesn't need a VT switch to restore the mode on resume */
257 info->skip_vt_switch = true;
258
01f2c773 259 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
38651674 260 drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height);
79e53945 261
88afe715
CW
262 /* If the object is shmemfs backed, it will have given us zeroed pages.
263 * If the object is stolen however, it will be full of whatever
264 * garbage was left in there.
265 */
d978ef14 266 if (ifbdev->fb->obj->stolen && !prealloc)
88afe715
CW
267 memset_io(info->screen_base, 0, info->screen_size);
268
fb2a99e1 269 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
79e53945 270
088e0df4 271 DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08llx, bo %p\n",
8be48d92 272 fb->width, fb->height,
f343c5f6 273 i915_gem_obj_ggtt_offset(obj), obj);
79e53945
JB
274
275 mutex_unlock(&dev->struct_mutex);
6a9ee8af 276 vga_switcheroo_client_fb_set(dev->pdev, info);
79e53945
JB
277 return 0;
278
21cff148
AT
279out_destroy_fbi:
280 drm_fb_helper_release_fbi(helper);
b4476f52 281out_unpin:
d7f46fc4 282 i915_gem_object_ggtt_unpin(obj);
05394f39 283 drm_gem_object_unreference(&obj->base);
8b4f49e0 284out_unlock:
79e53945 285 mutex_unlock(&dev->struct_mutex);
79e53945
JB
286 return ret;
287}
288
67437680
PZ
289/** Sets the color ramps on behalf of RandR */
290static void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
291 u16 blue, int regno)
292{
293 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
294
295 intel_crtc->lut_r[regno] = red >> 8;
296 intel_crtc->lut_g[regno] = green >> 8;
297 intel_crtc->lut_b[regno] = blue >> 8;
298}
299
300static void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
301 u16 *blue, int regno)
302{
303 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
304
305 *red = intel_crtc->lut_r[regno] << 8;
306 *green = intel_crtc->lut_g[regno] << 8;
307 *blue = intel_crtc->lut_b[regno] << 8;
308}
309
eb1bfe80
JB
310static struct drm_fb_helper_crtc *
311intel_fb_helper_crtc(struct drm_fb_helper *fb_helper, struct drm_crtc *crtc)
312{
313 int i;
314
315 for (i = 0; i < fb_helper->crtc_count; i++)
316 if (fb_helper->crtc_info[i].mode_set.crtc == crtc)
317 return &fb_helper->crtc_info[i];
318
319 return NULL;
320}
321
322/*
323 * Try to read the BIOS display configuration and use it for the initial
324 * fb configuration.
325 *
326 * The BIOS or boot loader will generally create an initial display
327 * configuration for us that includes some set of active pipes and displays.
328 * This routine tries to figure out which pipes and connectors are active
329 * and stuffs them into the crtcs and modes array given to us by the
330 * drm_fb_helper code.
331 *
332 * The overall sequence is:
333 * intel_fbdev_init - from driver load
334 * intel_fbdev_init_bios - initialize the intel_fbdev using BIOS data
335 * drm_fb_helper_init - build fb helper structs
336 * drm_fb_helper_single_add_all_connectors - more fb helper structs
337 * intel_fbdev_initial_config - apply the config
338 * drm_fb_helper_initial_config - call ->probe then register_framebuffer()
339 * drm_setup_crtcs - build crtc config for fbdev
340 * intel_fb_initial_config - find active connectors etc
341 * drm_fb_helper_single_fb_probe - set up fbdev
342 * intelfb_create - re-use or alloc fb, build out fbdev structs
343 *
344 * Note that we don't make special consideration whether we could actually
345 * switch to the selected modes without a full modeset. E.g. when the display
346 * is in VGA mode we need to recalculate watermarks and set a new high-res
347 * framebuffer anyway.
348 */
349static bool intel_fb_initial_config(struct drm_fb_helper *fb_helper,
350 struct drm_fb_helper_crtc **crtcs,
351 struct drm_display_mode **modes,
b0ee9e7f 352 struct drm_fb_offset *offsets,
eb1bfe80
JB
353 bool *enabled, int width, int height)
354{
02f5eebb 355 struct drm_device *dev = fb_helper->dev;
eb1bfe80 356 int i, j;
02f5eebb 357 bool *save_enabled;
7c2bb531 358 bool fallback = true;
7e696e4c
DV
359 int num_connectors_enabled = 0;
360 int num_connectors_detected = 0;
b0ee9e7f
DA
361 uint64_t conn_configured = 0, mask;
362 int pass = 0;
02f5eebb
JB
363
364 save_enabled = kcalloc(dev->mode_config.num_connector, sizeof(bool),
365 GFP_KERNEL);
366 if (!save_enabled)
367 return false;
368
369 memcpy(save_enabled, enabled, dev->mode_config.num_connector);
b0ee9e7f
DA
370 mask = (1 << fb_helper->connector_count) - 1;
371retry:
eb1bfe80
JB
372 for (i = 0; i < fb_helper->connector_count; i++) {
373 struct drm_fb_helper_connector *fb_conn;
374 struct drm_connector *connector;
375 struct drm_encoder *encoder;
376 struct drm_fb_helper_crtc *new_crtc;
377
378 fb_conn = fb_helper->connector_info[i];
379 connector = fb_conn->connector;
7e696e4c 380
b0ee9e7f
DA
381 if (conn_configured & (1 << i))
382 continue;
383
384 if (pass == 0 && !connector->has_tile)
385 continue;
386
7e696e4c
DV
387 if (connector->status == connector_status_connected)
388 num_connectors_detected++;
389
eb1bfe80 390 if (!enabled[i]) {
c0c97622 391 DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
c23cc417 392 connector->name);
b0ee9e7f 393 conn_configured |= (1 << i);
eb1bfe80
JB
394 continue;
395 }
396
d84a0f32
CW
397 if (connector->force == DRM_FORCE_OFF) {
398 DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
399 connector->name);
400 enabled[i] = false;
401 continue;
402 }
403
eb1bfe80
JB
404 encoder = connector->encoder;
405 if (!encoder || WARN_ON(!encoder->crtc)) {
d84a0f32
CW
406 if (connector->force > DRM_FORCE_OFF)
407 goto bail;
408
c0c97622 409 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
c23cc417 410 connector->name);
eb1bfe80 411 enabled[i] = false;
b0ee9e7f 412 conn_configured |= (1 << i);
eb1bfe80
JB
413 continue;
414 }
415
7e696e4c
DV
416 num_connectors_enabled++;
417
eb1bfe80
JB
418 new_crtc = intel_fb_helper_crtc(fb_helper, encoder->crtc);
419
420 /*
421 * Make sure we're not trying to drive multiple connectors
422 * with a single CRTC, since our cloning support may not
423 * match the BIOS.
424 */
425 for (j = 0; j < fb_helper->connector_count; j++) {
02f5eebb 426 if (crtcs[j] == new_crtc) {
7e696e4c 427 DRM_DEBUG_KMS("fallback: cloned configuration\n");
d84a0f32 428 goto bail;
02f5eebb 429 }
eb1bfe80
JB
430 }
431
c0c97622 432 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
c23cc417 433 connector->name);
eb1bfe80
JB
434
435 /* go for command line mode first */
436 modes[i] = drm_pick_cmdline_mode(fb_conn, width, height);
437
438 /* try for preferred next */
439 if (!modes[i]) {
b0ee9e7f
DA
440 DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
441 connector->name, connector->has_tile);
eb1bfe80
JB
442 modes[i] = drm_has_preferred_mode(fb_conn, width,
443 height);
444 }
445
afba0b5a
CW
446 /* No preferred mode marked by the EDID? Are there any modes? */
447 if (!modes[i] && !list_empty(&connector->modes)) {
448 DRM_DEBUG_KMS("using first mode listed on connector %s\n",
8d4ad9d4 449 connector->name);
afba0b5a
CW
450 modes[i] = list_first_entry(&connector->modes,
451 struct drm_display_mode,
452 head);
453 }
454
eb1bfe80
JB
455 /* last resort: use current mode */
456 if (!modes[i]) {
457 /*
458 * IMPORTANT: We want to use the adjusted mode (i.e.
459 * after the panel fitter upscaling) as the initial
460 * config, not the input mode, which is what crtc->mode
5c1e3426 461 * usually contains. But since our current
eb1bfe80 462 * code puts a mode derived from the post-pfit timings
5c1e3426 463 * into crtc->mode this works out correctly.
eb1bfe80 464 */
c0c97622 465 DRM_DEBUG_KMS("looking for current mode on connector %s\n",
c23cc417 466 connector->name);
5c1e3426 467 modes[i] = &encoder->crtc->mode;
eb1bfe80
JB
468 }
469 crtcs[i] = new_crtc;
470
fdaf66de 471 DRM_DEBUG_KMS("connector %s on pipe %c [CRTC:%d]: %dx%d%s\n",
c23cc417 472 connector->name,
c0c97622 473 pipe_name(to_intel_crtc(encoder->crtc)->pipe),
eb1bfe80 474 encoder->crtc->base.id,
c0c97622
CW
475 modes[i]->hdisplay, modes[i]->vdisplay,
476 modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" :"");
02f5eebb 477
7c2bb531 478 fallback = false;
b0ee9e7f
DA
479 conn_configured |= (1 << i);
480 }
481
482 if ((conn_configured & mask) != mask) {
483 pass++;
484 goto retry;
02f5eebb
JB
485 }
486
7e696e4c
DV
487 /*
488 * If the BIOS didn't enable everything it could, fall back to have the
489 * same user experiencing of lighting up as much as possible like the
490 * fbdev helper library.
491 */
492 if (num_connectors_enabled != num_connectors_detected &&
493 num_connectors_enabled < INTEL_INFO(dev)->num_pipes) {
494 DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
495 DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
496 num_connectors_detected);
497 fallback = true;
498 }
499
7c2bb531 500 if (fallback) {
d84a0f32 501bail:
7e696e4c 502 DRM_DEBUG_KMS("Not using firmware configuration\n");
02f5eebb
JB
503 memcpy(enabled, save_enabled, dev->mode_config.num_connector);
504 kfree(save_enabled);
505 return false;
eb1bfe80
JB
506 }
507
02f5eebb 508 kfree(save_enabled);
eb1bfe80
JB
509 return true;
510}
511
3a493879 512static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
eb1bfe80 513 .initial_config = intel_fb_initial_config,
4abe3520
DA
514 .gamma_set = intel_crtc_fb_gamma_set,
515 .gamma_get = intel_crtc_fb_gamma_get,
cd5428a5 516 .fb_probe = intelfb_create,
4abe3520 517};
79e53945 518
7b4f3990
CW
519static void intel_fbdev_destroy(struct drm_device *dev,
520 struct intel_fbdev *ifbdev)
79e53945 521{
ddfe1567 522
21cff148
AT
523 drm_fb_helper_unregister_fbi(&ifbdev->helper);
524 drm_fb_helper_release_fbi(&ifbdev->helper);
79e53945 525
4abe3520 526 drm_fb_helper_fini(&ifbdev->helper);
79e53945 527
8bcd4553 528 drm_framebuffer_unregister_private(&ifbdev->fb->base);
9d661251 529 drm_framebuffer_remove(&ifbdev->fb->base);
79e53945 530}
38651674 531
d978ef14
JB
532/*
533 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
534 * The core display code will have read out the current plane configuration,
535 * so we use that to figure out if there's an object for us to use as the
536 * fb, and if so, we re-use it for the fbdev configuration.
537 *
538 * Note we only support a single fb shared across pipes for boot (mostly for
539 * fbcon), so we just find the biggest and use that.
540 */
541static bool intel_fbdev_init_bios(struct drm_device *dev,
542 struct intel_fbdev *ifbdev)
543{
544 struct intel_framebuffer *fb = NULL;
545 struct drm_crtc *crtc;
546 struct intel_crtc *intel_crtc;
d978ef14
JB
547 unsigned int max_size = 0;
548
d978ef14 549 /* Find the largest fb */
70e1e0ec 550 for_each_crtc(dev, crtc) {
8e9ba31a
ML
551 struct drm_i915_gem_object *obj =
552 intel_fb_obj(crtc->primary->state->fb);
d978ef14
JB
553 intel_crtc = to_intel_crtc(crtc);
554
d5515991 555 if (!crtc->state->active || !obj) {
d978ef14
JB
556 DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
557 pipe_name(intel_crtc->pipe));
558 continue;
559 }
560
8e9ba31a 561 if (obj->base.size > max_size) {
d978ef14
JB
562 DRM_DEBUG_KMS("found possible fb from plane %c\n",
563 pipe_name(intel_crtc->pipe));
8e9ba31a
ML
564 fb = to_intel_framebuffer(crtc->primary->state->fb);
565 max_size = obj->base.size;
d978ef14
JB
566 }
567 }
568
569 if (!fb) {
570 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
571 goto out;
572 }
573
574 /* Now make sure all the pipes will fit into it */
70e1e0ec 575 for_each_crtc(dev, crtc) {
d978ef14
JB
576 unsigned int cur_size;
577
578 intel_crtc = to_intel_crtc(crtc);
579
d5515991 580 if (!crtc->state->active) {
d978ef14
JB
581 DRM_DEBUG_KMS("pipe %c not active, skipping\n",
582 pipe_name(intel_crtc->pipe));
583 continue;
584 }
585
586 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
587 pipe_name(intel_crtc->pipe));
588
589 /*
590 * See if the plane fb we found above will fit on this
bc104d1f
CW
591 * pipe. Note we need to use the selected fb's pitch and bpp
592 * rather than the current pipe's, since they differ.
d978ef14 593 */
6e3c9717 594 cur_size = intel_crtc->config->base.adjusted_mode.crtc_hdisplay;
bc104d1f
CW
595 cur_size = cur_size * fb->base.bits_per_pixel / 8;
596 if (fb->base.pitches[0] < cur_size) {
597 DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
598 pipe_name(intel_crtc->pipe),
599 cur_size, fb->base.pitches[0]);
bc104d1f
CW
600 fb = NULL;
601 break;
602 }
603
6e3c9717 604 cur_size = intel_crtc->config->base.adjusted_mode.crtc_vdisplay;
ec2c981e 605 cur_size = intel_fb_align_height(dev, cur_size,
091df6cb
DV
606 fb->base.pixel_format,
607 fb->base.modifier[0]);
bc104d1f
CW
608 cur_size *= fb->base.pitches[0];
609 DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
610 pipe_name(intel_crtc->pipe),
6e3c9717
ACO
611 intel_crtc->config->base.adjusted_mode.crtc_hdisplay,
612 intel_crtc->config->base.adjusted_mode.crtc_vdisplay,
bc104d1f 613 fb->base.bits_per_pixel,
d978ef14 614 cur_size);
d978ef14
JB
615
616 if (cur_size > max_size) {
617 DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
618 pipe_name(intel_crtc->pipe),
619 cur_size, max_size);
d978ef14
JB
620 fb = NULL;
621 break;
622 }
623
624 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
625 pipe_name(intel_crtc->pipe),
626 max_size, cur_size);
627 }
628
d978ef14
JB
629 if (!fb) {
630 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
631 goto out;
632 }
633
484b41dd 634 ifbdev->preferred_bpp = fb->base.bits_per_pixel;
d978ef14
JB
635 ifbdev->fb = fb;
636
484b41dd 637 drm_framebuffer_reference(&ifbdev->fb->base);
d978ef14
JB
638
639 /* Final pass to check if any active pipes don't have fbs */
70e1e0ec 640 for_each_crtc(dev, crtc) {
d978ef14
JB
641 intel_crtc = to_intel_crtc(crtc);
642
d5515991 643 if (!crtc->state->active)
d978ef14
JB
644 continue;
645
66e514c1 646 WARN(!crtc->primary->fb,
d978ef14
JB
647 "re-used BIOS config but lost an fb on crtc %d\n",
648 crtc->base.id);
649 }
650
651
652 DRM_DEBUG_KMS("using BIOS fb for initial console\n");
653 return true;
654
d978ef14
JB
655out:
656
657 return false;
658}
659
82e3b8c1
CW
660static void intel_fbdev_suspend_worker(struct work_struct *work)
661{
662 intel_fbdev_set_suspend(container_of(work,
663 struct drm_i915_private,
664 fbdev_suspend_work)->dev,
665 FBINFO_STATE_RUNNING,
666 true);
667}
668
38651674
DA
669int intel_fbdev_init(struct drm_device *dev)
670{
8be48d92 671 struct intel_fbdev *ifbdev;
b51b32cd 672 struct drm_i915_private *dev_priv = dev->dev_private;
5a79395b 673 int ret;
8be48d92 674
d978ef14
JB
675 if (WARN_ON(INTEL_INFO(dev)->num_pipes == 0))
676 return -ENODEV;
677
678 ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
679 if (ifbdev == NULL)
8be48d92
DA
680 return -ENOMEM;
681
10a23102
TR
682 drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
683
d978ef14
JB
684 if (!intel_fbdev_init_bios(dev, ifbdev))
685 ifbdev->preferred_bpp = 32;
4abe3520 686
5a79395b 687 ret = drm_fb_helper_init(dev, &ifbdev->helper,
d978ef14 688 INTEL_INFO(dev)->num_pipes, 4);
5a79395b
CW
689 if (ret) {
690 kfree(ifbdev);
691 return ret;
692 }
8be48d92 693
28cc504e
RC
694 ifbdev->helper.atomic = true;
695
d978ef14 696 dev_priv->fbdev = ifbdev;
82e3b8c1
CW
697 INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
698
0b4c0f3f 699 drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
20afbda2 700
79e53945
JB
701 return 0;
702}
38651674 703
d1d70677 704void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
20afbda2 705{
d1d70677 706 struct drm_i915_private *dev_priv = data;
d978ef14 707 struct intel_fbdev *ifbdev = dev_priv->fbdev;
20afbda2
DV
708
709 /* Due to peculiar init order wrt to hpd handling this is separate. */
d978ef14 710 drm_fb_helper_initial_config(&ifbdev->helper, ifbdev->preferred_bpp);
20afbda2
DV
711}
712
38651674
DA
713void intel_fbdev_fini(struct drm_device *dev)
714{
b51b32cd 715 struct drm_i915_private *dev_priv = dev->dev_private;
8be48d92
DA
716 if (!dev_priv->fbdev)
717 return;
718
82e3b8c1
CW
719 flush_work(&dev_priv->fbdev_suspend_work);
720
d1d70677 721 async_synchronize_full();
38651674 722 intel_fbdev_destroy(dev, dev_priv->fbdev);
8be48d92 723 kfree(dev_priv->fbdev);
38651674
DA
724 dev_priv->fbdev = NULL;
725}
3fa016a0 726
82e3b8c1 727void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
3fa016a0 728{
b51b32cd 729 struct drm_i915_private *dev_priv = dev->dev_private;
1ffc5289
JN
730 struct intel_fbdev *ifbdev = dev_priv->fbdev;
731 struct fb_info *info;
732
733 if (!ifbdev)
3fa016a0
DA
734 return;
735
1ffc5289
JN
736 info = ifbdev->helper.fbdev;
737
82e3b8c1
CW
738 if (synchronous) {
739 /* Flush any pending work to turn the console on, and then
740 * wait to turn it off. It must be synchronous as we are
741 * about to suspend or unload the driver.
742 *
743 * Note that from within the work-handler, we cannot flush
744 * ourselves, so only flush outstanding work upon suspend!
745 */
746 if (state != FBINFO_STATE_RUNNING)
747 flush_work(&dev_priv->fbdev_suspend_work);
748 console_lock();
749 } else {
750 /*
751 * The console lock can be pretty contented on resume due
752 * to all the printk activity. Try to keep it out of the hot
753 * path of resume if possible.
754 */
755 WARN_ON(state != FBINFO_STATE_RUNNING);
756 if (!console_trylock()) {
757 /* Don't block our own workqueue as this can
758 * be run in parallel with other i915.ko tasks.
759 */
760 schedule_work(&dev_priv->fbdev_suspend_work);
761 return;
762 }
763 }
764
1ffc5289
JN
765 /* On resume from hibernation: If the object is shmemfs backed, it has
766 * been restored from swap. If the object is stolen however, it will be
767 * full of whatever garbage was left in there.
768 */
8bcd4553 769 if (state == FBINFO_STATE_RUNNING && ifbdev->fb->obj->stolen)
1ffc5289
JN
770 memset_io(info->screen_base, 0, info->screen_size);
771
21cff148 772 drm_fb_helper_set_suspend(&ifbdev->helper, state);
82e3b8c1 773 console_unlock();
3fa016a0
DA
774}
775
0632fef6 776void intel_fbdev_output_poll_changed(struct drm_device *dev)
eb1f8e4f 777{
b51b32cd 778 struct drm_i915_private *dev_priv = dev->dev_private;
d978ef14
JB
779 if (dev_priv->fbdev)
780 drm_fb_helper_hotplug_event(&dev_priv->fbdev->helper);
eb1f8e4f 781}
e8e7a2b8 782
0632fef6 783void intel_fbdev_restore_mode(struct drm_device *dev)
e8e7a2b8
DA
784{
785 int ret;
b51b32cd 786 struct drm_i915_private *dev_priv = dev->dev_private;
d04df732
RV
787 struct intel_fbdev *ifbdev = dev_priv->fbdev;
788 struct drm_fb_helper *fb_helper;
e8e7a2b8 789
d04df732 790 if (!ifbdev)
e3c74757
BW
791 return;
792
d04df732
RV
793 fb_helper = &ifbdev->helper;
794
795 ret = drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
796 if (ret) {
e8e7a2b8 797 DRM_DEBUG("failed to restore crtc mode\n");
d04df732
RV
798 } else {
799 mutex_lock(&fb_helper->dev->struct_mutex);
800 intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT);
801 mutex_unlock(&fb_helper->dev->struct_mutex);
802 }
e8e7a2b8 803}
This page took 0.457771 seconds and 5 git commands to generate.