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