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