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