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