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