GNU Linux-libre 4.14.324-gnu1
[releases.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_fini(&ifbdev->helper);
511
512         if (ifbdev->vma) {
513                 mutex_lock(&ifbdev->helper.dev->struct_mutex);
514                 intel_unpin_fb_vma(ifbdev->vma);
515                 mutex_unlock(&ifbdev->helper.dev->struct_mutex);
516         }
517
518         if (ifbdev->fb)
519                 drm_framebuffer_remove(&ifbdev->fb->base);
520
521         kfree(ifbdev);
522 }
523
524 /*
525  * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
526  * The core display code will have read out the current plane configuration,
527  * so we use that to figure out if there's an object for us to use as the
528  * fb, and if so, we re-use it for the fbdev configuration.
529  *
530  * Note we only support a single fb shared across pipes for boot (mostly for
531  * fbcon), so we just find the biggest and use that.
532  */
533 static bool intel_fbdev_init_bios(struct drm_device *dev,
534                                  struct intel_fbdev *ifbdev)
535 {
536         struct intel_framebuffer *fb = NULL;
537         struct drm_crtc *crtc;
538         struct intel_crtc *intel_crtc;
539         unsigned int max_size = 0;
540
541         /* Find the largest fb */
542         for_each_crtc(dev, crtc) {
543                 struct drm_i915_gem_object *obj =
544                         intel_fb_obj(crtc->primary->state->fb);
545                 intel_crtc = to_intel_crtc(crtc);
546
547                 if (!crtc->state->active || !obj) {
548                         DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
549                                       pipe_name(intel_crtc->pipe));
550                         continue;
551                 }
552
553                 if (obj->base.size > max_size) {
554                         DRM_DEBUG_KMS("found possible fb from plane %c\n",
555                                       pipe_name(intel_crtc->pipe));
556                         fb = to_intel_framebuffer(crtc->primary->state->fb);
557                         max_size = obj->base.size;
558                 }
559         }
560
561         if (!fb) {
562                 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
563                 goto out;
564         }
565
566         /* Now make sure all the pipes will fit into it */
567         for_each_crtc(dev, crtc) {
568                 unsigned int cur_size;
569
570                 intel_crtc = to_intel_crtc(crtc);
571
572                 if (!crtc->state->active) {
573                         DRM_DEBUG_KMS("pipe %c not active, skipping\n",
574                                       pipe_name(intel_crtc->pipe));
575                         continue;
576                 }
577
578                 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
579                               pipe_name(intel_crtc->pipe));
580
581                 /*
582                  * See if the plane fb we found above will fit on this
583                  * pipe.  Note we need to use the selected fb's pitch and bpp
584                  * rather than the current pipe's, since they differ.
585                  */
586                 cur_size = intel_crtc->config->base.adjusted_mode.crtc_hdisplay;
587                 cur_size = cur_size * fb->base.format->cpp[0];
588                 if (fb->base.pitches[0] < cur_size) {
589                         DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
590                                       pipe_name(intel_crtc->pipe),
591                                       cur_size, fb->base.pitches[0]);
592                         fb = NULL;
593                         break;
594                 }
595
596                 cur_size = intel_crtc->config->base.adjusted_mode.crtc_vdisplay;
597                 cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
598                 cur_size *= fb->base.pitches[0];
599                 DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
600                               pipe_name(intel_crtc->pipe),
601                               intel_crtc->config->base.adjusted_mode.crtc_hdisplay,
602                               intel_crtc->config->base.adjusted_mode.crtc_vdisplay,
603                               fb->base.format->cpp[0] * 8,
604                               cur_size);
605
606                 if (cur_size > max_size) {
607                         DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
608                                       pipe_name(intel_crtc->pipe),
609                                       cur_size, max_size);
610                         fb = NULL;
611                         break;
612                 }
613
614                 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
615                               pipe_name(intel_crtc->pipe),
616                               max_size, cur_size);
617         }
618
619         if (!fb) {
620                 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
621                 goto out;
622         }
623
624         ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
625         ifbdev->fb = fb;
626
627         drm_framebuffer_reference(&ifbdev->fb->base);
628
629         /* Final pass to check if any active pipes don't have fbs */
630         for_each_crtc(dev, crtc) {
631                 intel_crtc = to_intel_crtc(crtc);
632
633                 if (!crtc->state->active)
634                         continue;
635
636                 WARN(!crtc->primary->fb,
637                      "re-used BIOS config but lost an fb on crtc %d\n",
638                      crtc->base.id);
639         }
640
641
642         DRM_DEBUG_KMS("using BIOS fb for initial console\n");
643         return true;
644
645 out:
646
647         return false;
648 }
649
650 static void intel_fbdev_suspend_worker(struct work_struct *work)
651 {
652         intel_fbdev_set_suspend(&container_of(work,
653                                               struct drm_i915_private,
654                                               fbdev_suspend_work)->drm,
655                                 FBINFO_STATE_RUNNING,
656                                 true);
657 }
658
659 int intel_fbdev_init(struct drm_device *dev)
660 {
661         struct drm_i915_private *dev_priv = to_i915(dev);
662         struct intel_fbdev *ifbdev;
663         int ret;
664
665         if (WARN_ON(INTEL_INFO(dev_priv)->num_pipes == 0))
666                 return -ENODEV;
667
668         ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
669         if (ifbdev == NULL)
670                 return -ENOMEM;
671
672         drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
673
674         if (!intel_fbdev_init_bios(dev, ifbdev))
675                 ifbdev->preferred_bpp = 32;
676
677         ret = drm_fb_helper_init(dev, &ifbdev->helper, 4);
678         if (ret) {
679                 kfree(ifbdev);
680                 return ret;
681         }
682
683         dev_priv->fbdev = ifbdev;
684         INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
685
686         drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
687
688         return 0;
689 }
690
691 static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
692 {
693         struct intel_fbdev *ifbdev = data;
694
695         /* Due to peculiar init order wrt to hpd handling this is separate. */
696         if (drm_fb_helper_initial_config(&ifbdev->helper,
697                                          ifbdev->preferred_bpp))
698                 intel_fbdev_unregister(to_i915(ifbdev->helper.dev));
699 }
700
701 void intel_fbdev_initial_config_async(struct drm_device *dev)
702 {
703         struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
704
705         if (!ifbdev)
706                 return;
707
708         ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
709 }
710
711 static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
712 {
713         if (!ifbdev->cookie)
714                 return;
715
716         /* Only serialises with all preceding async calls, hence +1 */
717         async_synchronize_cookie(ifbdev->cookie + 1);
718         ifbdev->cookie = 0;
719 }
720
721 void intel_fbdev_unregister(struct drm_i915_private *dev_priv)
722 {
723         struct intel_fbdev *ifbdev = dev_priv->fbdev;
724
725         if (!ifbdev)
726                 return;
727
728         cancel_work_sync(&dev_priv->fbdev_suspend_work);
729         if (!current_is_async())
730                 intel_fbdev_sync(ifbdev);
731
732         drm_fb_helper_unregister_fbi(&ifbdev->helper);
733 }
734
735 void intel_fbdev_fini(struct drm_i915_private *dev_priv)
736 {
737         struct intel_fbdev *ifbdev = fetch_and_zero(&dev_priv->fbdev);
738
739         if (!ifbdev)
740                 return;
741
742         intel_fbdev_destroy(ifbdev);
743 }
744
745 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
746 {
747         struct drm_i915_private *dev_priv = to_i915(dev);
748         struct intel_fbdev *ifbdev = dev_priv->fbdev;
749         struct fb_info *info;
750
751         if (!ifbdev || !ifbdev->vma)
752                 return;
753
754         info = ifbdev->helper.fbdev;
755
756         if (synchronous) {
757                 /* Flush any pending work to turn the console on, and then
758                  * wait to turn it off. It must be synchronous as we are
759                  * about to suspend or unload the driver.
760                  *
761                  * Note that from within the work-handler, we cannot flush
762                  * ourselves, so only flush outstanding work upon suspend!
763                  */
764                 if (state != FBINFO_STATE_RUNNING)
765                         flush_work(&dev_priv->fbdev_suspend_work);
766                 console_lock();
767         } else {
768                 /*
769                  * The console lock can be pretty contented on resume due
770                  * to all the printk activity.  Try to keep it out of the hot
771                  * path of resume if possible.
772                  */
773                 WARN_ON(state != FBINFO_STATE_RUNNING);
774                 if (!console_trylock()) {
775                         /* Don't block our own workqueue as this can
776                          * be run in parallel with other i915.ko tasks.
777                          */
778                         schedule_work(&dev_priv->fbdev_suspend_work);
779                         return;
780                 }
781         }
782
783         /* On resume from hibernation: If the object is shmemfs backed, it has
784          * been restored from swap. If the object is stolen however, it will be
785          * full of whatever garbage was left in there.
786          */
787         if (state == FBINFO_STATE_RUNNING && ifbdev->fb->obj->stolen)
788                 memset_io(info->screen_base, 0, info->screen_size);
789
790         drm_fb_helper_set_suspend(&ifbdev->helper, state);
791         console_unlock();
792 }
793
794 void intel_fbdev_output_poll_changed(struct drm_device *dev)
795 {
796         struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
797
798         if (!ifbdev)
799                 return;
800
801         intel_fbdev_sync(ifbdev);
802         if (ifbdev->vma)
803                 drm_fb_helper_hotplug_event(&ifbdev->helper);
804 }
805
806 void intel_fbdev_restore_mode(struct drm_device *dev)
807 {
808         struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
809
810         if (!ifbdev)
811                 return;
812
813         intel_fbdev_sync(ifbdev);
814         if (!ifbdev->vma)
815                 return;
816
817         if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper) == 0)
818                 intel_fbdev_invalidate(ifbdev);
819 }