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