fb70f5e12ce777ee2c55c3403158c3d4688d5f79
[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 */
328                 if (!modes[i]) {
329                         DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
330                                       fb_conn->connector->base.id);
331                         modes[i] = drm_has_preferred_mode(fb_conn, width,
332                                                           height);
333                 }
334
335                 /* last resort: use current mode */
336                 if (!modes[i]) {
337                         /*
338                          * IMPORTANT: We want to use the adjusted mode (i.e.
339                          * after the panel fitter upscaling) as the initial
340                          * config, not the input mode, which is what crtc->mode
341                          * usually contains. But since our current fastboot
342                          * code puts a mode derived from the post-pfit timings
343                          * into crtc->mode this works out correctly. We don't
344                          * use hwmode anywhere right now, so use it for this
345                          * since the fb helper layer wants a pointer to
346                          * something we own.
347                          */
348                         intel_mode_from_pipe_config(&encoder->crtc->hwmode,
349                                                     &to_intel_crtc(encoder->crtc)->config);
350                         modes[i] = &encoder->crtc->hwmode;
351                 }
352                 crtcs[i] = new_crtc;
353
354                 DRM_DEBUG_KMS("connector %s on crtc %d: %s\n",
355                               drm_get_connector_name(connector),
356                               encoder->crtc->base.id,
357                               modes[i]->name);
358         }
359
360         return true;
361 }
362
363 static struct drm_fb_helper_funcs intel_fb_helper_funcs = {
364         .initial_config = intel_fb_initial_config,
365         .gamma_set = intel_crtc_fb_gamma_set,
366         .gamma_get = intel_crtc_fb_gamma_get,
367         .fb_probe = intelfb_create,
368 };
369
370 static void intel_fbdev_destroy(struct drm_device *dev,
371                                 struct intel_fbdev *ifbdev)
372 {
373         if (ifbdev->helper.fbdev) {
374                 struct fb_info *info = ifbdev->helper.fbdev;
375
376                 unregister_framebuffer(info);
377                 iounmap(info->screen_base);
378                 if (info->cmap.len)
379                         fb_dealloc_cmap(&info->cmap);
380
381                 framebuffer_release(info);
382         }
383
384         drm_fb_helper_fini(&ifbdev->helper);
385
386         drm_framebuffer_unregister_private(&ifbdev->ifb.base);
387         intel_framebuffer_fini(&ifbdev->ifb);
388 }
389
390 /*
391  * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
392  * The core display code will have read out the current plane configuration,
393  * so we use that to figure out if there's an object for us to use as the
394  * fb, and if so, we re-use it for the fbdev configuration.
395  *
396  * Note we only support a single fb shared across pipes for boot (mostly for
397  * fbcon), so we just find the biggest and use that.
398  */
399 static bool intel_fbdev_init_bios(struct drm_device *dev,
400                                  struct intel_fbdev *ifbdev)
401 {
402         struct intel_framebuffer *fb = NULL;
403         struct drm_crtc *crtc;
404         struct intel_crtc *intel_crtc;
405         struct intel_plane_config *plane_config = NULL;
406         unsigned int max_size = 0;
407
408         if (!i915_fastboot)
409                 return false;
410
411         /* Find the largest fb */
412         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
413                 intel_crtc = to_intel_crtc(crtc);
414
415                 if (!intel_crtc->active || !intel_crtc->plane_config.fb) {
416                         DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
417                                       pipe_name(intel_crtc->pipe));
418                         continue;
419                 }
420
421                 if (intel_crtc->plane_config.size > max_size) {
422                         DRM_DEBUG_KMS("found possible fb from plane %c\n",
423                                       pipe_name(intel_crtc->pipe));
424                         plane_config = &intel_crtc->plane_config;
425                         fb = plane_config->fb;
426                         max_size = plane_config->size;
427                 }
428         }
429
430         if (!fb) {
431                 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
432                 goto out;
433         }
434
435         /* Now make sure all the pipes will fit into it */
436         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
437                 unsigned int cur_size;
438
439                 intel_crtc = to_intel_crtc(crtc);
440
441                 if (!intel_crtc->active) {
442                         DRM_DEBUG_KMS("pipe %c not active, skipping\n",
443                                       pipe_name(intel_crtc->pipe));
444                         continue;
445                 }
446
447                 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
448                               pipe_name(intel_crtc->pipe));
449
450                 /*
451                  * See if the plane fb we found above will fit on this
452                  * pipe.  Note we need to use the selected fb's bpp rather
453                  * than the current pipe's, since they could be different.
454                  */
455                 cur_size = intel_crtc->config.adjusted_mode.crtc_hdisplay *
456                         intel_crtc->config.adjusted_mode.crtc_vdisplay;
457                 DRM_DEBUG_KMS("pipe %c area: %d\n", pipe_name(intel_crtc->pipe),
458                               cur_size);
459                 cur_size *= fb->base.bits_per_pixel / 8;
460                 DRM_DEBUG_KMS("total size %d (bpp %d)\n", cur_size,
461                               fb->base.bits_per_pixel / 8);
462
463                 if (cur_size > max_size) {
464                         DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
465                                       pipe_name(intel_crtc->pipe),
466                                       cur_size, max_size);
467                         plane_config = NULL;
468                         fb = NULL;
469                         break;
470                 }
471
472                 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
473                               pipe_name(intel_crtc->pipe),
474                               max_size, cur_size);
475         }
476
477         /* Free unused fbs */
478         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
479                 struct intel_framebuffer *cur_fb;
480
481                 intel_crtc = to_intel_crtc(crtc);
482                 cur_fb = intel_crtc->plane_config.fb;
483
484                 if (cur_fb && cur_fb != fb)
485                         drm_framebuffer_unreference(&cur_fb->base);
486         }
487
488         if (!fb) {
489                 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
490                 goto out;
491         }
492
493         ifbdev->preferred_bpp = plane_config->fb->base.bits_per_pixel;
494         ifbdev->ifb = *fb;
495
496         /* Assuming a single fb across all pipes here */
497         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
498                 intel_crtc = to_intel_crtc(crtc);
499
500                 if (!intel_crtc->active)
501                         continue;
502
503                 /*
504                  * This should only fail on the first one so we don't need
505                  * to cleanup any secondary crtc->fbs
506                  */
507                 if (intel_pin_and_fence_fb_obj(dev, fb->obj, NULL))
508                         goto out_unref_obj;
509
510                 crtc->fb = &fb->base;
511                 drm_gem_object_reference(&fb->obj->base);
512                 drm_framebuffer_reference(&fb->base);
513         }
514
515         /* Final pass to check if any active pipes don't have fbs */
516         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
517                 intel_crtc = to_intel_crtc(crtc);
518
519                 if (!intel_crtc->active)
520                         continue;
521
522                 WARN(!crtc->fb,
523                      "re-used BIOS config but lost an fb on crtc %d\n",
524                      crtc->base.id);
525         }
526
527
528         DRM_DEBUG_KMS("using BIOS fb for initial console\n");
529         return true;
530
531 out_unref_obj:
532         drm_framebuffer_unreference(&fb->base);
533 out:
534
535         return false;
536 }
537
538 int intel_fbdev_init(struct drm_device *dev)
539 {
540         struct intel_fbdev *ifbdev;
541         struct drm_i915_private *dev_priv = dev->dev_private;
542         int ret;
543
544         if (WARN_ON(INTEL_INFO(dev)->num_pipes == 0))
545                 return -ENODEV;
546
547         ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
548         if (ifbdev == NULL)
549                 return -ENOMEM;
550
551         ifbdev->helper.funcs = &intel_fb_helper_funcs;
552         if (!intel_fbdev_init_bios(dev, ifbdev))
553                 ifbdev->preferred_bpp = 32;
554
555         ret = drm_fb_helper_init(dev, &ifbdev->helper,
556                                  INTEL_INFO(dev)->num_pipes, 4);
557         if (ret) {
558                 kfree(ifbdev);
559                 return ret;
560         }
561
562         dev_priv->fbdev = ifbdev;
563         drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
564
565         return 0;
566 }
567
568 void intel_fbdev_initial_config(struct drm_device *dev)
569 {
570         struct drm_i915_private *dev_priv = dev->dev_private;
571         struct intel_fbdev *ifbdev = dev_priv->fbdev;
572
573         /* Due to peculiar init order wrt to hpd handling this is separate. */
574         drm_fb_helper_initial_config(&ifbdev->helper, ifbdev->preferred_bpp);
575 }
576
577 void intel_fbdev_fini(struct drm_device *dev)
578 {
579         struct drm_i915_private *dev_priv = dev->dev_private;
580         if (!dev_priv->fbdev)
581                 return;
582
583         intel_fbdev_destroy(dev, dev_priv->fbdev);
584         kfree(dev_priv->fbdev);
585         dev_priv->fbdev = NULL;
586 }
587
588 void intel_fbdev_set_suspend(struct drm_device *dev, int state)
589 {
590         struct drm_i915_private *dev_priv = dev->dev_private;
591         struct intel_fbdev *ifbdev = dev_priv->fbdev;
592         struct fb_info *info;
593
594         if (!ifbdev)
595                 return;
596
597         info = ifbdev->helper.fbdev;
598
599         /* On resume from hibernation: If the object is shmemfs backed, it has
600          * been restored from swap. If the object is stolen however, it will be
601          * full of whatever garbage was left in there.
602          */
603         if (state == FBINFO_STATE_RUNNING && ifbdev->ifb.obj->stolen)
604                 memset_io(info->screen_base, 0, info->screen_size);
605
606         fb_set_suspend(info, state);
607 }
608
609 void intel_fbdev_output_poll_changed(struct drm_device *dev)
610 {
611         struct drm_i915_private *dev_priv = dev->dev_private;
612         if (dev_priv->fbdev)
613                 drm_fb_helper_hotplug_event(&dev_priv->fbdev->helper);
614 }
615
616 void intel_fbdev_restore_mode(struct drm_device *dev)
617 {
618         int ret;
619         struct drm_i915_private *dev_priv = dev->dev_private;
620
621         if (!dev_priv->fbdev)
622                 return;
623
624         drm_modeset_lock_all(dev);
625
626         ret = drm_fb_helper_restore_fbdev_mode(&dev_priv->fbdev->helper);
627         if (ret)
628                 DRM_DEBUG("failed to restore crtc mode\n");
629
630         drm_modeset_unlock_all(dev);
631 }