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