drm/i915: use current mode if the size matches the preferred mode
[profile/ivi/kernel-x86-ivi.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/module.h>
28 #include <linux/kernel.h>
29 #include <linux/errno.h>
30 #include <linux/string.h>
31 #include <linux/mm.h>
32 #include <linux/tty.h>
33 #include <linux/sysrq.h>
34 #include <linux/delay.h>
35 #include <linux/fb.h>
36 #include <linux/init.h>
37 #include <linux/vga_switcheroo.h>
38
39 #include <drm/drmP.h>
40 #include <drm/drm_crtc.h>
41 #include <drm/drm_fb_helper.h>
42 #include "intel_drv.h"
43 #include <drm/i915_drm.h>
44 #include "i915_drv.h"
45
46 static struct fb_ops intelfb_ops = {
47         .owner = THIS_MODULE,
48         .fb_check_var = drm_fb_helper_check_var,
49         .fb_set_par = drm_fb_helper_set_par,
50         .fb_fillrect = cfb_fillrect,
51         .fb_copyarea = cfb_copyarea,
52         .fb_imageblit = cfb_imageblit,
53         .fb_pan_display = drm_fb_helper_pan_display,
54         .fb_blank = drm_fb_helper_blank,
55         .fb_setcmap = drm_fb_helper_setcmap,
56         .fb_debug_enter = drm_fb_helper_debug_enter,
57         .fb_debug_leave = drm_fb_helper_debug_leave,
58 };
59
60 static int intelfb_alloc(struct drm_fb_helper *helper,
61                          struct drm_fb_helper_surface_size *sizes)
62 {
63         struct intel_fbdev *ifbdev =
64                 container_of(helper, struct intel_fbdev, helper);
65         struct drm_device *dev = helper->dev;
66         struct drm_mode_fb_cmd2 mode_cmd = {};
67         struct drm_i915_gem_object *obj;
68         int size, ret;
69
70         /* we don't do packed 24bpp */
71         if (sizes->surface_bpp == 24)
72                 sizes->surface_bpp = 32;
73
74         mode_cmd.width = sizes->surface_width;
75         mode_cmd.height = sizes->surface_height;
76
77         mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
78                                     DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
79         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
80                                                           sizes->surface_depth);
81
82         size = mode_cmd.pitches[0] * mode_cmd.height;
83         size = ALIGN(size, PAGE_SIZE);
84         obj = i915_gem_object_create_stolen(dev, size);
85         if (obj == NULL)
86                 obj = i915_gem_alloc_object(dev, size);
87         if (!obj) {
88                 DRM_ERROR("failed to allocate framebuffer\n");
89                 ret = -ENOMEM;
90                 goto out;
91         }
92
93         /* Flush everything out, we'll be doing GTT only from now on */
94         ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
95         if (ret) {
96                 DRM_ERROR("failed to pin fb: %d\n", ret);
97                 goto out_unref;
98         }
99
100         ret = intel_framebuffer_init(dev, &ifbdev->ifb, &mode_cmd, obj);
101         if (ret)
102                 goto out_unpin;
103
104         return 0;
105
106 out_unpin:
107         i915_gem_object_unpin(obj);
108 out_unref:
109         drm_gem_object_unreference(&obj->base);
110 out:
111         return ret;
112 }
113
114 static int intelfb_create(struct drm_fb_helper *helper,
115                           struct drm_fb_helper_surface_size *sizes)
116 {
117         struct intel_fbdev *ifbdev =
118                 container_of(helper, struct intel_fbdev, helper);
119         struct intel_framebuffer *intel_fb = &ifbdev->ifb;
120         struct drm_device *dev = helper->dev;
121         struct drm_i915_private *dev_priv = dev->dev_private;
122         struct fb_info *info;
123         struct drm_framebuffer *fb;
124         struct drm_i915_gem_object *obj;
125         int size, ret;
126         bool prealloc = false;
127
128         mutex_lock(&dev->struct_mutex);
129
130         if (!intel_fb->obj) {
131                 DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
132                 ret = intelfb_alloc(helper, sizes);
133                 if (ret)
134                         goto out_unlock;
135         } else {
136                 DRM_DEBUG_KMS("re-using BIOS fb\n");
137                 prealloc = true;
138                 sizes->fb_width = intel_fb->base.width;
139                 sizes->fb_height = intel_fb->base.height;
140         }
141
142         obj = intel_fb->obj;
143         size = obj->base.size;
144
145         info = framebuffer_alloc(0, &dev->pdev->dev);
146         if (!info) {
147                 ret = -ENOMEM;
148                 goto out_unpin;
149         }
150
151         info->par = helper;
152
153         fb = &ifbdev->ifb.base;
154
155         ifbdev->helper.fb = fb;
156         ifbdev->helper.fbdev = info;
157
158         strcpy(info->fix.id, "inteldrmfb");
159
160         info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
161         info->fbops = &intelfb_ops;
162
163         ret = fb_alloc_cmap(&info->cmap, 256, 0);
164         if (ret) {
165                 ret = -ENOMEM;
166                 goto out_unpin;
167         }
168         /* setup aperture base/size for vesafb takeover */
169         info->apertures = alloc_apertures(1);
170         if (!info->apertures) {
171                 ret = -ENOMEM;
172                 goto out_unpin;
173         }
174         info->apertures->ranges[0].base = dev->mode_config.fb_base;
175         info->apertures->ranges[0].size = dev_priv->gtt.mappable_end;
176
177         info->fix.smem_start = dev->mode_config.fb_base + i915_gem_obj_ggtt_offset(obj);
178         info->fix.smem_len = size;
179
180         info->screen_base =
181                 ioremap_wc(dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj),
182                            size);
183         if (!info->screen_base) {
184                 ret = -ENOSPC;
185                 goto out_unpin;
186         }
187         info->screen_size = size;
188
189         /* This driver doesn't need a VT switch to restore the mode on resume */
190         info->skip_vt_switch = true;
191
192         drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
193         drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height);
194
195         /* If the object is shmemfs backed, it will have given us zeroed pages.
196          * If the object is stolen however, it will be full of whatever
197          * garbage was left in there.
198          */
199         if (ifbdev->ifb.obj->stolen && !prealloc)
200                 memset_io(info->screen_base, 0, info->screen_size);
201
202         /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
203
204         DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08lx, bo %p\n",
205                       fb->width, fb->height,
206                       i915_gem_obj_ggtt_offset(obj), obj);
207
208         mutex_unlock(&dev->struct_mutex);
209         vga_switcheroo_client_fb_set(dev->pdev, info);
210         return 0;
211
212 out_unpin:
213         i915_gem_object_unpin(obj);
214         drm_gem_object_unreference(&obj->base);
215 out_unlock:
216         mutex_unlock(&dev->struct_mutex);
217         return ret;
218 }
219
220 /** Sets the color ramps on behalf of RandR */
221 static void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
222                                     u16 blue, int regno)
223 {
224         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
225
226         intel_crtc->lut_r[regno] = red >> 8;
227         intel_crtc->lut_g[regno] = green >> 8;
228         intel_crtc->lut_b[regno] = blue >> 8;
229 }
230
231 static void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
232                                     u16 *blue, int regno)
233 {
234         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
235
236         *red = intel_crtc->lut_r[regno] << 8;
237         *green = intel_crtc->lut_g[regno] << 8;
238         *blue = intel_crtc->lut_b[regno] << 8;
239 }
240
241 static struct drm_fb_helper_crtc *
242 intel_fb_helper_crtc(struct drm_fb_helper *fb_helper, struct drm_crtc *crtc)
243 {
244         int i;
245
246         for (i = 0; i < fb_helper->crtc_count; i++)
247                 if (fb_helper->crtc_info[i].mode_set.crtc == crtc)
248                         return &fb_helper->crtc_info[i];
249
250         return NULL;
251 }
252
253 /*
254  * Try to read the BIOS display configuration and use it for the initial
255  * fb configuration.
256  *
257  * The BIOS or boot loader will generally create an initial display
258  * configuration for us that includes some set of active pipes and displays.
259  * This routine tries to figure out which pipes and connectors are active
260  * and stuffs them into the crtcs and modes array given to us by the
261  * drm_fb_helper code.
262  *
263  * The overall sequence is:
264  *   intel_fbdev_init - from driver load
265  *     intel_fbdev_init_bios - initialize the intel_fbdev using BIOS data
266  *     drm_fb_helper_init - build fb helper structs
267  *     drm_fb_helper_single_add_all_connectors - more fb helper structs
268  *   intel_fbdev_initial_config - apply the config
269  *     drm_fb_helper_initial_config - call ->probe then register_framebuffer()
270  *         drm_setup_crtcs - build crtc config for fbdev
271  *           intel_fb_initial_config - find active connectors etc
272  *         drm_fb_helper_single_fb_probe - set up fbdev
273  *           intelfb_create - re-use or alloc fb, build out fbdev structs
274  *
275  * Note that we don't make special consideration whether we could actually
276  * switch to the selected modes without a full modeset. E.g. when the display
277  * is in VGA mode we need to recalculate watermarks and set a new high-res
278  * framebuffer anyway.
279  */
280 static bool intel_fb_initial_config(struct drm_fb_helper *fb_helper,
281                                     struct drm_fb_helper_crtc **crtcs,
282                                     struct drm_display_mode **modes,
283                                     bool *enabled, int width, int height)
284 {
285         int i, j;
286
287         for (i = 0; i < fb_helper->connector_count; i++) {
288                 struct drm_fb_helper_connector *fb_conn;
289                 struct drm_connector *connector;
290                 struct drm_encoder *encoder;
291                 struct drm_fb_helper_crtc *new_crtc;
292
293                 fb_conn = fb_helper->connector_info[i];
294                 connector = fb_conn->connector;
295                 if (!enabled[i]) {
296                         DRM_DEBUG_KMS("connector %d not enabled, skipping\n",
297                                       connector->base.id);
298                         continue;
299                 }
300
301                 encoder = connector->encoder;
302                 if (!encoder || WARN_ON(!encoder->crtc)) {
303                         DRM_DEBUG_KMS("connector %d has no encoder or crtc, skipping\n",
304                                       connector->base.id);
305                         enabled[i] = false;
306                         continue;
307                 }
308
309                 new_crtc = intel_fb_helper_crtc(fb_helper, encoder->crtc);
310
311                 /*
312                  * Make sure we're not trying to drive multiple connectors
313                  * with a single CRTC, since our cloning support may not
314                  * match the BIOS.
315                  */
316                 for (j = 0; j < fb_helper->connector_count; j++) {
317                         if (crtcs[j] == new_crtc)
318                                 return false;
319                 }
320
321                 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
322                               fb_conn->connector->base.id);
323
324                 /* go for command line mode first */
325                 modes[i] = drm_pick_cmdline_mode(fb_conn, width, height);
326
327                 /* try for preferred next or match current */
328                 if (!modes[i]) {
329                         struct drm_display_mode *preferred;
330
331                         DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
332                                       fb_conn->connector->base.id);
333                         preferred = drm_has_preferred_mode(fb_conn, width,
334                                                            height);
335                         intel_mode_from_pipe_config(&encoder->crtc->hwmode,
336                                                     &to_intel_crtc(encoder->crtc)->config);
337                         modes[i] = &encoder->crtc->hwmode;
338
339                         if (preferred &&
340                             !drm_mode_same_size(preferred, modes[i])) {
341                                 DRM_DEBUG_KMS("using preferred mode %s "
342                                               "instead of current mode %s "
343                                               "on connector %d\n",
344                                               preferred->name,
345                                               modes[i]->name,
346                                               fb_conn->connector->base.id);
347                                 modes[i] = preferred;
348                         }
349                 }
350
351                 crtcs[i] = new_crtc;
352
353                 DRM_DEBUG_KMS("connector %s on crtc %d: %s\n",
354                               drm_get_connector_name(connector),
355                               encoder->crtc->base.id,
356                               modes[i]->name);
357         }
358
359         return true;
360 }
361
362 static struct drm_fb_helper_funcs intel_fb_helper_funcs = {
363         .initial_config = intel_fb_initial_config,
364         .gamma_set = intel_crtc_fb_gamma_set,
365         .gamma_get = intel_crtc_fb_gamma_get,
366         .fb_probe = intelfb_create,
367 };
368
369 static void intel_fbdev_destroy(struct drm_device *dev,
370                                 struct intel_fbdev *ifbdev)
371 {
372         if (ifbdev->helper.fbdev) {
373                 struct fb_info *info = ifbdev->helper.fbdev;
374
375                 unregister_framebuffer(info);
376                 iounmap(info->screen_base);
377                 if (info->cmap.len)
378                         fb_dealloc_cmap(&info->cmap);
379
380                 framebuffer_release(info);
381         }
382
383         drm_fb_helper_fini(&ifbdev->helper);
384
385         drm_framebuffer_unregister_private(&ifbdev->ifb.base);
386         intel_framebuffer_fini(&ifbdev->ifb);
387 }
388
389 /*
390  * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
391  * The core display code will have read out the current plane configuration,
392  * so we use that to figure out if there's an object for us to use as the
393  * fb, and if so, we re-use it for the fbdev configuration.
394  *
395  * Note we only support a single fb shared across pipes for boot (mostly for
396  * fbcon), so we just find the biggest and use that.
397  */
398 static bool intel_fbdev_init_bios(struct drm_device *dev,
399                                  struct intel_fbdev *ifbdev)
400 {
401         struct intel_framebuffer *fb = NULL;
402         struct drm_crtc *crtc;
403         struct intel_crtc *intel_crtc;
404         struct intel_plane_config *plane_config = NULL;
405         unsigned int max_size = 0;
406
407         if (!i915_fastboot)
408                 return false;
409
410         /* Find the largest fb */
411         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
412                 intel_crtc = to_intel_crtc(crtc);
413
414                 if (!intel_crtc->active || !intel_crtc->plane_config.fb) {
415                         DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
416                                       pipe_name(intel_crtc->pipe));
417                         continue;
418                 }
419
420                 if (intel_crtc->plane_config.size > max_size) {
421                         DRM_DEBUG_KMS("found possible fb from plane %c\n",
422                                       pipe_name(intel_crtc->pipe));
423                         plane_config = &intel_crtc->plane_config;
424                         fb = plane_config->fb;
425                         max_size = plane_config->size;
426                 }
427         }
428
429         if (!fb) {
430                 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
431                 goto out;
432         }
433
434         /* Now make sure all the pipes will fit into it */
435         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
436                 unsigned int cur_size;
437
438                 intel_crtc = to_intel_crtc(crtc);
439
440                 if (!intel_crtc->active) {
441                         DRM_DEBUG_KMS("pipe %c not active, skipping\n",
442                                       pipe_name(intel_crtc->pipe));
443                         continue;
444                 }
445
446                 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
447                               pipe_name(intel_crtc->pipe));
448
449                 /*
450                  * See if the plane fb we found above will fit on this
451                  * pipe.  Note we need to use the selected fb's bpp rather
452                  * than the current pipe's, since they could be different.
453                  */
454                 cur_size = intel_crtc->config.adjusted_mode.crtc_hdisplay *
455                         intel_crtc->config.adjusted_mode.crtc_vdisplay;
456                 DRM_DEBUG_KMS("pipe %c area: %d\n", pipe_name(intel_crtc->pipe),
457                               cur_size);
458                 cur_size *= fb->base.bits_per_pixel / 8;
459                 DRM_DEBUG_KMS("total size %d (bpp %d)\n", cur_size,
460                               fb->base.bits_per_pixel / 8);
461
462                 if (cur_size > max_size) {
463                         DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
464                                       pipe_name(intel_crtc->pipe),
465                                       cur_size, max_size);
466                         plane_config = NULL;
467                         fb = NULL;
468                         break;
469                 }
470
471                 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
472                               pipe_name(intel_crtc->pipe),
473                               max_size, cur_size);
474         }
475
476         /* Free unused fbs */
477         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
478                 struct intel_framebuffer *cur_fb;
479
480                 intel_crtc = to_intel_crtc(crtc);
481                 cur_fb = intel_crtc->plane_config.fb;
482
483                 if (cur_fb && cur_fb != fb)
484                         drm_framebuffer_unreference(&cur_fb->base);
485         }
486
487         if (!fb) {
488                 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
489                 goto out;
490         }
491
492         ifbdev->preferred_bpp = plane_config->fb->base.bits_per_pixel;
493         ifbdev->ifb = *fb;
494
495         /* Assuming a single fb across all pipes here */
496         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
497                 intel_crtc = to_intel_crtc(crtc);
498
499                 if (!intel_crtc->active)
500                         continue;
501
502                 /*
503                  * This should only fail on the first one so we don't need
504                  * to cleanup any secondary crtc->fbs
505                  */
506                 if (intel_pin_and_fence_fb_obj(dev, fb->obj, NULL))
507                         goto out_unref_obj;
508
509                 crtc->fb = &fb->base;
510                 drm_gem_object_reference(&fb->obj->base);
511                 drm_framebuffer_reference(&fb->base);
512         }
513
514         /* Final pass to check if any active pipes don't have fbs */
515         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
516                 intel_crtc = to_intel_crtc(crtc);
517
518                 if (!intel_crtc->active)
519                         continue;
520
521                 WARN(!crtc->fb,
522                      "re-used BIOS config but lost an fb on crtc %d\n",
523                      crtc->base.id);
524         }
525
526
527         DRM_DEBUG_KMS("using BIOS fb for initial console\n");
528         return true;
529
530 out_unref_obj:
531         drm_framebuffer_unreference(&fb->base);
532 out:
533
534         return false;
535 }
536
537 int intel_fbdev_init(struct drm_device *dev)
538 {
539         struct intel_fbdev *ifbdev;
540         struct drm_i915_private *dev_priv = dev->dev_private;
541         int ret;
542
543         if (WARN_ON(INTEL_INFO(dev)->num_pipes == 0))
544                 return -ENODEV;
545
546         ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
547         if (ifbdev == NULL)
548                 return -ENOMEM;
549
550         ifbdev->helper.funcs = &intel_fb_helper_funcs;
551         if (!intel_fbdev_init_bios(dev, ifbdev))
552                 ifbdev->preferred_bpp = 32;
553
554         ret = drm_fb_helper_init(dev, &ifbdev->helper,
555                                  INTEL_INFO(dev)->num_pipes, 4);
556         if (ret) {
557                 kfree(ifbdev);
558                 return ret;
559         }
560
561         dev_priv->fbdev = ifbdev;
562         drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
563
564         return 0;
565 }
566
567 void intel_fbdev_initial_config(struct drm_device *dev)
568 {
569         struct drm_i915_private *dev_priv = dev->dev_private;
570         struct intel_fbdev *ifbdev = dev_priv->fbdev;
571
572         /* Due to peculiar init order wrt to hpd handling this is separate. */
573         drm_fb_helper_initial_config(&ifbdev->helper, ifbdev->preferred_bpp);
574 }
575
576 void intel_fbdev_fini(struct drm_device *dev)
577 {
578         struct drm_i915_private *dev_priv = dev->dev_private;
579         if (!dev_priv->fbdev)
580                 return;
581
582         intel_fbdev_destroy(dev, dev_priv->fbdev);
583         kfree(dev_priv->fbdev);
584         dev_priv->fbdev = NULL;
585 }
586
587 void intel_fbdev_set_suspend(struct drm_device *dev, int state)
588 {
589         struct drm_i915_private *dev_priv = dev->dev_private;
590         struct intel_fbdev *ifbdev = dev_priv->fbdev;
591         struct fb_info *info;
592
593         if (!ifbdev)
594                 return;
595
596         info = ifbdev->helper.fbdev;
597
598         /* On resume from hibernation: If the object is shmemfs backed, it has
599          * been restored from swap. If the object is stolen however, it will be
600          * full of whatever garbage was left in there.
601          */
602         if (state == FBINFO_STATE_RUNNING && ifbdev->ifb.obj->stolen)
603                 memset_io(info->screen_base, 0, info->screen_size);
604
605         fb_set_suspend(info, state);
606 }
607
608 void intel_fbdev_output_poll_changed(struct drm_device *dev)
609 {
610         struct drm_i915_private *dev_priv = dev->dev_private;
611         if (dev_priv->fbdev)
612                 drm_fb_helper_hotplug_event(&dev_priv->fbdev->helper);
613 }
614
615 void intel_fbdev_restore_mode(struct drm_device *dev)
616 {
617         int ret;
618         struct drm_i915_private *dev_priv = dev->dev_private;
619
620         if (!dev_priv->fbdev)
621                 return;
622
623         drm_modeset_lock_all(dev);
624
625         ret = drm_fb_helper_restore_fbdev_mode(&dev_priv->fbdev->helper);
626         if (ret)
627                 DRM_DEBUG("failed to restore crtc mode\n");
628
629         drm_modeset_unlock_all(dev);
630 }