drm: add connector/encoder name creation
[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/module.h>
29 #include <linux/kernel.h>
30 #include <linux/errno.h>
31 #include <linux/string.h>
32 #include <linux/mm.h>
33 #include <linux/delay.h>
34 #include <linux/fb.h>
35
36 #include <drm/drmP.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_fb_helper.h>
39 #include "intel_drv.h"
40 #include <drm/i915_drm.h>
41 #include "i915_drv.h"
42
43 #if 0
44 static struct fb_ops intelfb_ops = {
45         .owner = THIS_MODULE,
46         .fb_check_var = drm_fb_helper_check_var,
47         .fb_set_par = drm_fb_helper_set_par,
48         .fb_fillrect = cfb_fillrect,
49         .fb_copyarea = cfb_copyarea,
50         .fb_imageblit = cfb_imageblit,
51         .fb_pan_display = drm_fb_helper_pan_display,
52         .fb_blank = drm_fb_helper_blank,
53         .fb_setcmap = drm_fb_helper_setcmap,
54         .fb_debug_enter = drm_fb_helper_debug_enter,
55         .fb_debug_leave = drm_fb_helper_debug_leave,
56 };
57 #endif
58
59 static int intelfb_alloc(struct drm_fb_helper *helper,
60                          struct drm_fb_helper_surface_size *sizes)
61 {
62         struct intel_fbdev *ifbdev =
63                 container_of(helper, struct intel_fbdev, helper);
64         struct drm_framebuffer *fb;
65         struct drm_device *dev = helper->dev;
66         struct drm_mode_fb_cmd2 mode_cmd = {};
67         struct drm_i915_gem_object *obj;
68         int size, ret;
69
70         /* we don't do packed 24bpp */
71         if (sizes->surface_bpp == 24)
72                 sizes->surface_bpp = 32;
73
74         mode_cmd.width = sizes->surface_width;
75         mode_cmd.height = sizes->surface_height;
76
77         mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
78                                     DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
79         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
80                                                           sizes->surface_depth);
81
82         size = mode_cmd.pitches[0] * mode_cmd.height;
83         size = ALIGN(size, PAGE_SIZE);
84         obj = i915_gem_object_create_stolen(dev, size);
85         if (obj == NULL)
86                 obj = i915_gem_alloc_object(dev, size);
87         if (!obj) {
88                 DRM_ERROR("failed to allocate framebuffer\n");
89                 ret = -ENOMEM;
90                 goto out;
91         }
92
93         /* Flush everything out, we'll be doing GTT only from now on */
94         ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
95         if (ret) {
96                 DRM_ERROR("failed to pin obj: %d\n", ret);
97                 goto out_unref;
98         }
99
100         fb = __intel_framebuffer_create(dev, &mode_cmd, obj);
101         if (IS_ERR(fb)) {
102                 ret = PTR_ERR(fb);
103                 goto out_unpin;
104         }
105
106         ifbdev->fb = to_intel_framebuffer(fb);
107
108         return 0;
109
110 out_unpin:
111         i915_gem_object_ggtt_unpin(obj);
112 out_unref:
113         drm_gem_object_unreference(&obj->base);
114 out:
115         return ret;
116 }
117
118 static int intelfb_create(struct drm_fb_helper *helper,
119                           struct drm_fb_helper_surface_size *sizes)
120 {
121         struct intel_fbdev *ifbdev =
122                 container_of(helper, struct intel_fbdev, helper);
123         struct intel_framebuffer *intel_fb = ifbdev->fb;
124         struct drm_device *dev = helper->dev;
125 #if 0
126         struct drm_i915_private *dev_priv = dev->dev_private;
127 #endif
128         struct fb_info *info;
129         struct drm_framebuffer *fb;
130         struct drm_i915_gem_object *obj;
131         device_t vga_dev;
132         int size, ret;
133         bool prealloc = false;
134
135         mutex_lock(&dev->struct_mutex);
136
137         if (intel_fb &&
138             (sizes->fb_width > intel_fb->base.width ||
139              sizes->fb_height > intel_fb->base.height)) {
140                 DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d),"
141                               " releasing it\n",
142                               intel_fb->base.width, intel_fb->base.height,
143                               sizes->fb_width, sizes->fb_height);
144                 drm_framebuffer_unreference(&intel_fb->base);
145                 intel_fb = ifbdev->fb = NULL;
146         }
147         if (!intel_fb || WARN_ON(!intel_fb->obj)) {
148                 DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
149                 ret = intelfb_alloc(helper, sizes);
150                 if (ret)
151                         goto out_unlock;
152                 intel_fb = ifbdev->fb;
153         } else {
154                 DRM_DEBUG_KMS("re-using BIOS fb\n");
155                 prealloc = true;
156                 sizes->fb_width = intel_fb->base.width;
157                 sizes->fb_height = intel_fb->base.height;
158         }
159
160         obj = intel_fb->obj;
161         size = obj->base.size;
162
163 #if 0
164         info = framebuffer_alloc(0, &dev->pdev->dev);
165 #else
166         info = kmalloc(sizeof(struct fb_info), M_DRM, M_WAITOK | M_ZERO);
167 #endif
168         if (!info) {
169                 ret = -ENOMEM;
170                 goto out_unpin;
171         }
172 #if 0
173         info->par = helper;
174 #endif
175
176 #ifdef __DragonFly__
177         vga_dev = device_get_parent(dev->dev);
178         info = kmalloc(sizeof(struct fb_info), M_DRM, M_WAITOK | M_ZERO);
179         info->width = sizes->fb_width;
180         info->height = sizes->fb_height;
181         info->stride =
182             ALIGN(sizes->surface_width * ((sizes->surface_bpp + 7) / 8), 64);
183         info->depth = sizes->surface_bpp;
184         info->paddr = dev->agp->base + i915_gem_obj_ggtt_offset(obj);
185         info->is_vga_boot_display = vga_pci_is_boot_display(vga_dev);
186         info->vaddr =
187             (vm_offset_t)pmap_mapdev_attr(info->paddr,
188                 sizes->surface_height * info->stride,
189                 VM_MEMATTR_WRITE_COMBINING);
190 #endif
191
192         fb = &ifbdev->fb->base;
193
194         ifbdev->helper.fb = fb;
195         ifbdev->helper.fbdev = info;
196
197 #if 0
198         strcpy(info->fix.id, "inteldrmfb");
199
200         info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
201         info->fbops = &intelfb_ops;
202
203         ret = fb_alloc_cmap(&info->cmap, 256, 0);
204         if (ret) {
205                 ret = -ENOMEM;
206                 goto out_unpin;
207         }
208         /* setup aperture base/size for vesafb takeover */
209         info->apertures = alloc_apertures(1);
210         if (!info->apertures) {
211                 ret = -ENOMEM;
212                 goto out_unpin;
213         }
214         info->apertures->ranges[0].base = dev->mode_config.fb_base;
215         info->apertures->ranges[0].size = dev_priv->gtt.mappable_end;
216
217         info->fix.smem_start = dev->mode_config.fb_base + i915_gem_obj_ggtt_offset(obj);
218         info->fix.smem_len = size;
219
220         info->screen_base =
221                 ioremap_wc(dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj),
222                            size);
223         if (!info->screen_base) {
224                 ret = -ENOSPC;
225                 goto out_unpin;
226         }
227         info->screen_size = size;
228
229         /* This driver doesn't need a VT switch to restore the mode on resume */
230         info->skip_vt_switch = true;
231
232         drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
233         drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height);
234
235         /* If the object is shmemfs backed, it will have given us zeroed pages.
236          * If the object is stolen however, it will be full of whatever
237          * garbage was left in there.
238          */
239         if (ifbdev->fb->obj->stolen && !prealloc)
240                 memset_io(info->screen_base, 0, info->screen_size);
241
242         /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
243 #endif
244
245         DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08lx, bo %p\n",
246                       fb->width, fb->height,
247                       i915_gem_obj_ggtt_offset(obj), obj);
248
249         mutex_unlock(&dev->struct_mutex);
250 #if 0
251         vga_switcheroo_client_fb_set(dev->pdev, info);
252 #endif
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 %d [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 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 0
493         if (ifbdev->helper.fbdev) {
494                 struct fb_info *info = ifbdev->helper.fbdev;
495
496                 unregister_framebuffer(info);
497                 iounmap(info->screen_base);
498                 if (info->cmap.len)
499                         fb_dealloc_cmap(&info->cmap);
500
501                 framebuffer_release(info);
502         }
503 #endif
504
505         drm_fb_helper_fini(&ifbdev->helper);
506
507         drm_framebuffer_unregister_private(&ifbdev->fb->base);
508         drm_framebuffer_unreference(&ifbdev->fb->base);
509 }
510
511 /*
512  * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
513  * The core display code will have read out the current plane configuration,
514  * so we use that to figure out if there's an object for us to use as the
515  * fb, and if so, we re-use it for the fbdev configuration.
516  *
517  * Note we only support a single fb shared across pipes for boot (mostly for
518  * fbcon), so we just find the biggest and use that.
519  */
520 static bool intel_fbdev_init_bios(struct drm_device *dev,
521                                  struct intel_fbdev *ifbdev)
522 {
523         struct intel_framebuffer *fb = NULL;
524         struct drm_crtc *crtc;
525         struct intel_crtc *intel_crtc;
526         struct intel_plane_config *plane_config = NULL;
527         unsigned int max_size = 0;
528
529         if (!i915.fastboot)
530                 return false;
531
532         /* Find the largest fb */
533         for_each_crtc(dev, crtc) {
534                 intel_crtc = to_intel_crtc(crtc);
535
536                 if (!intel_crtc->active || !crtc->primary->fb) {
537                         DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
538                                       pipe_name(intel_crtc->pipe));
539                         continue;
540                 }
541
542                 if (intel_crtc->plane_config.size > max_size) {
543                         DRM_DEBUG_KMS("found possible fb from plane %c\n",
544                                       pipe_name(intel_crtc->pipe));
545                         plane_config = &intel_crtc->plane_config;
546                         fb = to_intel_framebuffer(crtc->primary->fb);
547                         max_size = plane_config->size;
548                 }
549         }
550
551         if (!fb) {
552                 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
553                 goto out;
554         }
555
556         /* Now make sure all the pipes will fit into it */
557         for_each_crtc(dev, crtc) {
558                 unsigned int cur_size;
559
560                 intel_crtc = to_intel_crtc(crtc);
561
562                 if (!intel_crtc->active) {
563                         DRM_DEBUG_KMS("pipe %c not active, skipping\n",
564                                       pipe_name(intel_crtc->pipe));
565                         continue;
566                 }
567
568                 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
569                               pipe_name(intel_crtc->pipe));
570
571                 /*
572                  * See if the plane fb we found above will fit on this
573                  * pipe.  Note we need to use the selected fb's pitch and bpp
574                  * rather than the current pipe's, since they differ.
575                  */
576                 cur_size = intel_crtc->config.adjusted_mode.crtc_hdisplay;
577                 cur_size = cur_size * fb->base.bits_per_pixel / 8;
578                 if (fb->base.pitches[0] < cur_size) {
579                         DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
580                                       pipe_name(intel_crtc->pipe),
581                                       cur_size, fb->base.pitches[0]);
582                         plane_config = NULL;
583                         fb = NULL;
584                         break;
585                 }
586
587                 cur_size = intel_crtc->config.adjusted_mode.crtc_vdisplay;
588                 cur_size = ALIGN(cur_size, plane_config->tiled ? (IS_GEN2(dev) ? 16 : 8) : 1);
589                 cur_size *= fb->base.pitches[0];
590                 DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
591                               pipe_name(intel_crtc->pipe),
592                               intel_crtc->config.adjusted_mode.crtc_hdisplay,
593                               intel_crtc->config.adjusted_mode.crtc_vdisplay,
594                               fb->base.bits_per_pixel,
595                               cur_size);
596
597                 if (cur_size > max_size) {
598                         DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
599                                       pipe_name(intel_crtc->pipe),
600                                       cur_size, max_size);
601                         plane_config = NULL;
602                         fb = NULL;
603                         break;
604                 }
605
606                 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
607                               pipe_name(intel_crtc->pipe),
608                               max_size, cur_size);
609         }
610
611         if (!fb) {
612                 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
613                 goto out;
614         }
615
616         ifbdev->preferred_bpp = fb->base.bits_per_pixel;
617         ifbdev->fb = fb;
618
619         drm_framebuffer_reference(&ifbdev->fb->base);
620
621         /* Final pass to check if any active pipes don't have fbs */
622         for_each_crtc(dev, crtc) {
623                 intel_crtc = to_intel_crtc(crtc);
624
625                 if (!intel_crtc->active)
626                         continue;
627
628                 WARN(!crtc->primary->fb,
629                      "re-used BIOS config but lost an fb on crtc %d\n",
630                      crtc->base.id);
631         }
632
633
634         DRM_DEBUG_KMS("using BIOS fb for initial console\n");
635         return true;
636
637 out:
638
639         return false;
640 }
641
642 int intel_fbdev_init(struct drm_device *dev)
643 {
644         struct intel_fbdev *ifbdev;
645         struct drm_i915_private *dev_priv = dev->dev_private;
646         int ret;
647
648         if (WARN_ON(INTEL_INFO(dev)->num_pipes == 0))
649                 return -ENODEV;
650
651         ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
652         if (ifbdev == NULL)
653                 return -ENOMEM;
654
655         ifbdev->helper.funcs = &intel_fb_helper_funcs;
656         if (!intel_fbdev_init_bios(dev, ifbdev))
657                 ifbdev->preferred_bpp = 32;
658
659         ret = drm_fb_helper_init(dev, &ifbdev->helper,
660                                  INTEL_INFO(dev)->num_pipes, 4);
661         if (ret) {
662                 kfree(ifbdev);
663                 return ret;
664         }
665
666         dev_priv->fbdev = ifbdev;
667         drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
668
669         return 0;
670 }
671
672 void intel_fbdev_initial_config(struct drm_device *dev)
673 {
674         struct drm_i915_private *dev_priv = dev->dev_private;
675
676         /* Due to peculiar init order wrt to hpd handling this is separate. */
677         drm_fb_helper_initial_config(&dev_priv->fbdev->helper, 32);
678 }
679
680 void intel_fbdev_fini(struct drm_device *dev)
681 {
682         struct drm_i915_private *dev_priv = dev->dev_private;
683         if (!dev_priv->fbdev)
684                 return;
685
686         intel_fbdev_destroy(dev, dev_priv->fbdev);
687         kfree(dev_priv->fbdev);
688         dev_priv->fbdev = NULL;
689 }
690
691 void intel_fbdev_set_suspend(struct drm_device *dev, int state)
692 {
693 #if 0
694         struct drm_i915_private *dev_priv = dev->dev_private;
695         struct intel_fbdev *ifbdev = dev_priv->fbdev;
696         struct fb_info *info;
697
698         if (!ifbdev)
699                 return;
700
701         info = ifbdev->helper.fbdev;
702
703         /* On resume from hibernation: If the object is shmemfs backed, it has
704          * been restored from swap. If the object is stolen however, it will be
705          * full of whatever garbage was left in there.
706          */
707         if (state == FBINFO_STATE_RUNNING && ifbdev->ifb.obj->stolen)
708                 memset_io(info->screen_base, 0, info->screen_size);
709
710         fb_set_suspend(info, state);
711 #endif
712 }
713
714 void intel_fbdev_output_poll_changed(struct drm_device *dev)
715 {
716         struct drm_i915_private *dev_priv = dev->dev_private;
717         drm_fb_helper_hotplug_event(&dev_priv->fbdev->helper);
718 }
719
720 void intel_fbdev_restore_mode(struct drm_device *dev)
721 {
722         int ret;
723         struct drm_i915_private *dev_priv = dev->dev_private;
724
725         if (INTEL_INFO(dev)->num_pipes == 0)
726                 return;
727
728         ret = drm_fb_helper_restore_fbdev_mode_unlocked(&dev_priv->fbdev->helper);
729         if (ret)
730                 DRM_DEBUG("failed to restore crtc mode\n");
731 }