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