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