drm/msm/hdmi: Enable HPD after HDMI IRQ is set up
[linux/fpc-iii.git] / drivers / gpu / drm / gma500 / framebuffer.c
blobadefae58b5fcd6e09d513aed236c12d220e406c5
1 /**************************************************************************
2 * Copyright (c) 2007-2011, Intel Corporation.
3 * All Rights Reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 **************************************************************************/
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/string.h>
24 #include <linux/pfn_t.h>
25 #include <linux/mm.h>
26 #include <linux/tty.h>
27 #include <linux/slab.h>
28 #include <linux/delay.h>
29 #include <linux/init.h>
30 #include <linux/console.h>
32 #include <drm/drmP.h>
33 #include <drm/drm.h>
34 #include <drm/drm_crtc.h>
35 #include <drm/drm_fb_helper.h>
36 #include <drm/drm_gem_framebuffer_helper.h>
38 #include "psb_drv.h"
39 #include "psb_intel_reg.h"
40 #include "psb_intel_drv.h"
41 #include "framebuffer.h"
42 #include "gtt.h"
44 static const struct drm_framebuffer_funcs psb_fb_funcs = {
45 .destroy = drm_gem_fb_destroy,
46 .create_handle = drm_gem_fb_create_handle,
49 #define CMAP_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16)
51 static int psbfb_setcolreg(unsigned regno, unsigned red, unsigned green,
52 unsigned blue, unsigned transp,
53 struct fb_info *info)
55 struct psb_fbdev *fbdev = info->par;
56 struct drm_framebuffer *fb = fbdev->psb_fb_helper.fb;
57 uint32_t v;
59 if (!fb)
60 return -ENOMEM;
62 if (regno > 255)
63 return 1;
65 red = CMAP_TOHW(red, info->var.red.length);
66 blue = CMAP_TOHW(blue, info->var.blue.length);
67 green = CMAP_TOHW(green, info->var.green.length);
68 transp = CMAP_TOHW(transp, info->var.transp.length);
70 v = (red << info->var.red.offset) |
71 (green << info->var.green.offset) |
72 (blue << info->var.blue.offset) |
73 (transp << info->var.transp.offset);
75 if (regno < 16) {
76 switch (fb->format->cpp[0] * 8) {
77 case 16:
78 ((uint32_t *) info->pseudo_palette)[regno] = v;
79 break;
80 case 24:
81 case 32:
82 ((uint32_t *) info->pseudo_palette)[regno] = v;
83 break;
87 return 0;
90 static int psbfb_pan(struct fb_var_screeninfo *var, struct fb_info *info)
92 struct psb_fbdev *fbdev = info->par;
93 struct psb_framebuffer *psbfb = &fbdev->pfb;
94 struct drm_device *dev = psbfb->base.dev;
95 struct gtt_range *gtt = to_gtt_range(psbfb->base.obj[0]);
98 * We have to poke our nose in here. The core fb code assumes
99 * panning is part of the hardware that can be invoked before
100 * the actual fb is mapped. In our case that isn't quite true.
102 if (gtt->npage) {
103 /* GTT roll shifts in 4K pages, we need to shift the right
104 number of pages */
105 int pages = info->fix.line_length >> 12;
106 psb_gtt_roll(dev, gtt, var->yoffset * pages);
108 return 0;
111 static vm_fault_t psbfb_vm_fault(struct vm_fault *vmf)
113 struct vm_area_struct *vma = vmf->vma;
114 struct psb_framebuffer *psbfb = vma->vm_private_data;
115 struct drm_device *dev = psbfb->base.dev;
116 struct drm_psb_private *dev_priv = dev->dev_private;
117 struct gtt_range *gtt = to_gtt_range(psbfb->base.obj[0]);
118 int page_num;
119 int i;
120 unsigned long address;
121 vm_fault_t ret = VM_FAULT_SIGBUS;
122 unsigned long pfn;
123 unsigned long phys_addr = (unsigned long)dev_priv->stolen_base +
124 gtt->offset;
126 page_num = vma_pages(vma);
127 address = vmf->address - (vmf->pgoff << PAGE_SHIFT);
129 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
131 for (i = 0; i < page_num; i++) {
132 pfn = (phys_addr >> PAGE_SHIFT);
134 ret = vmf_insert_mixed(vma, address,
135 __pfn_to_pfn_t(pfn, PFN_DEV));
136 if (unlikely(ret & VM_FAULT_ERROR))
137 break;
138 address += PAGE_SIZE;
139 phys_addr += PAGE_SIZE;
141 return ret;
144 static void psbfb_vm_open(struct vm_area_struct *vma)
148 static void psbfb_vm_close(struct vm_area_struct *vma)
152 static const struct vm_operations_struct psbfb_vm_ops = {
153 .fault = psbfb_vm_fault,
154 .open = psbfb_vm_open,
155 .close = psbfb_vm_close
158 static int psbfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
160 struct psb_fbdev *fbdev = info->par;
161 struct psb_framebuffer *psbfb = &fbdev->pfb;
163 if (vma->vm_pgoff != 0)
164 return -EINVAL;
165 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
166 return -EINVAL;
168 if (!psbfb->addr_space)
169 psbfb->addr_space = vma->vm_file->f_mapping;
171 * If this is a GEM object then info->screen_base is the virtual
172 * kernel remapping of the object. FIXME: Review if this is
173 * suitable for our mmap work
175 vma->vm_ops = &psbfb_vm_ops;
176 vma->vm_private_data = (void *)psbfb;
177 vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP;
178 return 0;
181 static struct fb_ops psbfb_ops = {
182 .owner = THIS_MODULE,
183 DRM_FB_HELPER_DEFAULT_OPS,
184 .fb_setcolreg = psbfb_setcolreg,
185 .fb_fillrect = drm_fb_helper_cfb_fillrect,
186 .fb_copyarea = psbfb_copyarea,
187 .fb_imageblit = drm_fb_helper_cfb_imageblit,
188 .fb_mmap = psbfb_mmap,
189 .fb_sync = psbfb_sync,
192 static struct fb_ops psbfb_roll_ops = {
193 .owner = THIS_MODULE,
194 DRM_FB_HELPER_DEFAULT_OPS,
195 .fb_setcolreg = psbfb_setcolreg,
196 .fb_fillrect = drm_fb_helper_cfb_fillrect,
197 .fb_copyarea = drm_fb_helper_cfb_copyarea,
198 .fb_imageblit = drm_fb_helper_cfb_imageblit,
199 .fb_pan_display = psbfb_pan,
200 .fb_mmap = psbfb_mmap,
203 static struct fb_ops psbfb_unaccel_ops = {
204 .owner = THIS_MODULE,
205 DRM_FB_HELPER_DEFAULT_OPS,
206 .fb_setcolreg = psbfb_setcolreg,
207 .fb_fillrect = drm_fb_helper_cfb_fillrect,
208 .fb_copyarea = drm_fb_helper_cfb_copyarea,
209 .fb_imageblit = drm_fb_helper_cfb_imageblit,
210 .fb_mmap = psbfb_mmap,
214 * psb_framebuffer_init - initialize a framebuffer
215 * @dev: our DRM device
216 * @fb: framebuffer to set up
217 * @mode_cmd: mode description
218 * @gt: backing object
220 * Configure and fill in the boilerplate for our frame buffer. Return
221 * 0 on success or an error code if we fail.
223 static int psb_framebuffer_init(struct drm_device *dev,
224 struct psb_framebuffer *fb,
225 const struct drm_mode_fb_cmd2 *mode_cmd,
226 struct gtt_range *gt)
228 const struct drm_format_info *info;
229 int ret;
232 * Reject unknown formats, YUV formats, and formats with more than
233 * 4 bytes per pixel.
235 info = drm_format_info(mode_cmd->pixel_format);
236 if (!info || !info->depth || info->cpp[0] > 4)
237 return -EINVAL;
239 if (mode_cmd->pitches[0] & 63)
240 return -EINVAL;
242 drm_helper_mode_fill_fb_struct(dev, &fb->base, mode_cmd);
243 fb->base.obj[0] = &gt->gem;
244 ret = drm_framebuffer_init(dev, &fb->base, &psb_fb_funcs);
245 if (ret) {
246 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
247 return ret;
249 return 0;
253 * psb_framebuffer_create - create a framebuffer backed by gt
254 * @dev: our DRM device
255 * @mode_cmd: the description of the requested mode
256 * @gt: the backing object
258 * Create a framebuffer object backed by the gt, and fill in the
259 * boilerplate required
261 * TODO: review object references
264 static struct drm_framebuffer *psb_framebuffer_create
265 (struct drm_device *dev,
266 const struct drm_mode_fb_cmd2 *mode_cmd,
267 struct gtt_range *gt)
269 struct psb_framebuffer *fb;
270 int ret;
272 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
273 if (!fb)
274 return ERR_PTR(-ENOMEM);
276 ret = psb_framebuffer_init(dev, fb, mode_cmd, gt);
277 if (ret) {
278 kfree(fb);
279 return ERR_PTR(ret);
281 return &fb->base;
285 * psbfb_alloc - allocate frame buffer memory
286 * @dev: the DRM device
287 * @aligned_size: space needed
289 * Allocate the frame buffer. In the usual case we get a GTT range that
290 * is stolen memory backed and life is simple. If there isn't sufficient
291 * we fail as we don't have the virtual mapping space to really vmap it
292 * and the kernel console code can't handle non linear framebuffers.
294 * Re-address this as and if the framebuffer layer grows this ability.
296 static struct gtt_range *psbfb_alloc(struct drm_device *dev, int aligned_size)
298 struct gtt_range *backing;
299 /* Begin by trying to use stolen memory backing */
300 backing = psb_gtt_alloc_range(dev, aligned_size, "fb", 1, PAGE_SIZE);
301 if (backing) {
302 drm_gem_private_object_init(dev, &backing->gem, aligned_size);
303 return backing;
305 return NULL;
309 * psbfb_create - create a framebuffer
310 * @fbdev: the framebuffer device
311 * @sizes: specification of the layout
313 * Create a framebuffer to the specifications provided
315 static int psbfb_create(struct psb_fbdev *fbdev,
316 struct drm_fb_helper_surface_size *sizes)
318 struct drm_device *dev = fbdev->psb_fb_helper.dev;
319 struct drm_psb_private *dev_priv = dev->dev_private;
320 struct fb_info *info;
321 struct drm_framebuffer *fb;
322 struct psb_framebuffer *psbfb = &fbdev->pfb;
323 struct drm_mode_fb_cmd2 mode_cmd;
324 int size;
325 int ret;
326 struct gtt_range *backing;
327 u32 bpp, depth;
328 int gtt_roll = 0;
329 int pitch_lines = 0;
331 mode_cmd.width = sizes->surface_width;
332 mode_cmd.height = sizes->surface_height;
333 bpp = sizes->surface_bpp;
334 depth = sizes->surface_depth;
336 /* No 24bit packed */
337 if (bpp == 24)
338 bpp = 32;
340 do {
342 * Acceleration via the GTT requires pitch to be
343 * power of two aligned. Preferably page but less
344 * is ok with some fonts
346 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 4096 >> pitch_lines);
348 size = mode_cmd.pitches[0] * mode_cmd.height;
349 size = ALIGN(size, PAGE_SIZE);
351 /* Allocate the fb in the GTT with stolen page backing */
352 backing = psbfb_alloc(dev, size);
354 if (pitch_lines)
355 pitch_lines *= 2;
356 else
357 pitch_lines = 1;
358 gtt_roll++;
359 } while (backing == NULL && pitch_lines <= 16);
361 /* The final pitch we accepted if we succeeded */
362 pitch_lines /= 2;
364 if (backing == NULL) {
366 * We couldn't get the space we wanted, fall back to the
367 * display engine requirement instead. The HW requires
368 * the pitch to be 64 byte aligned
371 gtt_roll = 0; /* Don't use GTT accelerated scrolling */
372 pitch_lines = 64;
374 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 64);
376 size = mode_cmd.pitches[0] * mode_cmd.height;
377 size = ALIGN(size, PAGE_SIZE);
379 /* Allocate the framebuffer in the GTT with stolen page backing */
380 backing = psbfb_alloc(dev, size);
381 if (backing == NULL)
382 return -ENOMEM;
385 memset(dev_priv->vram_addr + backing->offset, 0, size);
387 info = drm_fb_helper_alloc_fbi(&fbdev->psb_fb_helper);
388 if (IS_ERR(info)) {
389 ret = PTR_ERR(info);
390 goto out;
392 info->par = fbdev;
394 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
396 ret = psb_framebuffer_init(dev, psbfb, &mode_cmd, backing);
397 if (ret)
398 goto out;
400 fb = &psbfb->base;
401 psbfb->fbdev = info;
403 fbdev->psb_fb_helper.fb = fb;
405 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
406 strcpy(info->fix.id, "psbdrmfb");
408 info->flags = FBINFO_DEFAULT;
409 if (dev_priv->ops->accel_2d && pitch_lines > 8) /* 2D engine */
410 info->fbops = &psbfb_ops;
411 else if (gtt_roll) { /* GTT rolling seems best */
412 info->fbops = &psbfb_roll_ops;
413 info->flags |= FBINFO_HWACCEL_YPAN;
414 } else /* Software */
415 info->fbops = &psbfb_unaccel_ops;
417 info->fix.smem_start = dev->mode_config.fb_base;
418 info->fix.smem_len = size;
419 info->fix.ywrapstep = gtt_roll;
420 info->fix.ypanstep = 0;
422 /* Accessed stolen memory directly */
423 info->screen_base = dev_priv->vram_addr + backing->offset;
424 info->screen_size = size;
426 if (dev_priv->gtt.stolen_size) {
427 info->apertures->ranges[0].base = dev->mode_config.fb_base;
428 info->apertures->ranges[0].size = dev_priv->gtt.stolen_size;
431 drm_fb_helper_fill_var(info, &fbdev->psb_fb_helper,
432 sizes->fb_width, sizes->fb_height);
434 info->fix.mmio_start = pci_resource_start(dev->pdev, 0);
435 info->fix.mmio_len = pci_resource_len(dev->pdev, 0);
437 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
439 dev_dbg(dev->dev, "allocated %dx%d fb\n",
440 psbfb->base.width, psbfb->base.height);
442 return 0;
443 out:
444 psb_gtt_free_range(dev, backing);
445 return ret;
449 * psb_user_framebuffer_create - create framebuffer
450 * @dev: our DRM device
451 * @filp: client file
452 * @cmd: mode request
454 * Create a new framebuffer backed by a userspace GEM object
456 static struct drm_framebuffer *psb_user_framebuffer_create
457 (struct drm_device *dev, struct drm_file *filp,
458 const struct drm_mode_fb_cmd2 *cmd)
460 struct gtt_range *r;
461 struct drm_gem_object *obj;
464 * Find the GEM object and thus the gtt range object that is
465 * to back this space
467 obj = drm_gem_object_lookup(filp, cmd->handles[0]);
468 if (obj == NULL)
469 return ERR_PTR(-ENOENT);
471 /* Let the core code do all the work */
472 r = container_of(obj, struct gtt_range, gem);
473 return psb_framebuffer_create(dev, cmd, r);
476 static int psbfb_probe(struct drm_fb_helper *helper,
477 struct drm_fb_helper_surface_size *sizes)
479 struct psb_fbdev *psb_fbdev =
480 container_of(helper, struct psb_fbdev, psb_fb_helper);
481 struct drm_device *dev = psb_fbdev->psb_fb_helper.dev;
482 struct drm_psb_private *dev_priv = dev->dev_private;
483 int bytespp;
485 bytespp = sizes->surface_bpp / 8;
486 if (bytespp == 3) /* no 24bit packed */
487 bytespp = 4;
489 /* If the mode will not fit in 32bit then switch to 16bit to get
490 a console on full resolution. The X mode setting server will
491 allocate its own 32bit GEM framebuffer */
492 if (ALIGN(sizes->fb_width * bytespp, 64) * sizes->fb_height >
493 dev_priv->vram_stolen_size) {
494 sizes->surface_bpp = 16;
495 sizes->surface_depth = 16;
498 return psbfb_create(psb_fbdev, sizes);
501 static const struct drm_fb_helper_funcs psb_fb_helper_funcs = {
502 .fb_probe = psbfb_probe,
505 static int psb_fbdev_destroy(struct drm_device *dev, struct psb_fbdev *fbdev)
507 struct psb_framebuffer *psbfb = &fbdev->pfb;
509 drm_fb_helper_unregister_fbi(&fbdev->psb_fb_helper);
511 drm_fb_helper_fini(&fbdev->psb_fb_helper);
512 drm_framebuffer_unregister_private(&psbfb->base);
513 drm_framebuffer_cleanup(&psbfb->base);
515 if (psbfb->base.obj[0])
516 drm_gem_object_put_unlocked(psbfb->base.obj[0]);
517 return 0;
520 int psb_fbdev_init(struct drm_device *dev)
522 struct psb_fbdev *fbdev;
523 struct drm_psb_private *dev_priv = dev->dev_private;
524 int ret;
526 fbdev = kzalloc(sizeof(struct psb_fbdev), GFP_KERNEL);
527 if (!fbdev) {
528 dev_err(dev->dev, "no memory\n");
529 return -ENOMEM;
532 dev_priv->fbdev = fbdev;
534 drm_fb_helper_prepare(dev, &fbdev->psb_fb_helper, &psb_fb_helper_funcs);
536 ret = drm_fb_helper_init(dev, &fbdev->psb_fb_helper,
537 INTELFB_CONN_LIMIT);
538 if (ret)
539 goto free;
541 ret = drm_fb_helper_single_add_all_connectors(&fbdev->psb_fb_helper);
542 if (ret)
543 goto fini;
545 /* disable all the possible outputs/crtcs before entering KMS mode */
546 drm_helper_disable_unused_functions(dev);
548 ret = drm_fb_helper_initial_config(&fbdev->psb_fb_helper, 32);
549 if (ret)
550 goto fini;
552 return 0;
554 fini:
555 drm_fb_helper_fini(&fbdev->psb_fb_helper);
556 free:
557 kfree(fbdev);
558 return ret;
561 static void psb_fbdev_fini(struct drm_device *dev)
563 struct drm_psb_private *dev_priv = dev->dev_private;
565 if (!dev_priv->fbdev)
566 return;
568 psb_fbdev_destroy(dev, dev_priv->fbdev);
569 kfree(dev_priv->fbdev);
570 dev_priv->fbdev = NULL;
573 static const struct drm_mode_config_funcs psb_mode_funcs = {
574 .fb_create = psb_user_framebuffer_create,
575 .output_poll_changed = drm_fb_helper_output_poll_changed,
578 static void psb_setup_outputs(struct drm_device *dev)
580 struct drm_psb_private *dev_priv = dev->dev_private;
581 struct drm_connector *connector;
583 drm_mode_create_scaling_mode_property(dev);
585 /* It is ok for this to fail - we just don't get backlight control */
586 if (!dev_priv->backlight_property)
587 dev_priv->backlight_property = drm_property_create_range(dev, 0,
588 "backlight", 0, 100);
589 dev_priv->ops->output_init(dev);
591 list_for_each_entry(connector, &dev->mode_config.connector_list,
592 head) {
593 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
594 struct drm_encoder *encoder = &gma_encoder->base;
595 int crtc_mask = 0, clone_mask = 0;
597 /* valid crtcs */
598 switch (gma_encoder->type) {
599 case INTEL_OUTPUT_ANALOG:
600 crtc_mask = (1 << 0);
601 clone_mask = (1 << INTEL_OUTPUT_ANALOG);
602 break;
603 case INTEL_OUTPUT_SDVO:
604 crtc_mask = dev_priv->ops->sdvo_mask;
605 clone_mask = (1 << INTEL_OUTPUT_SDVO);
606 break;
607 case INTEL_OUTPUT_LVDS:
608 crtc_mask = dev_priv->ops->lvds_mask;
609 clone_mask = (1 << INTEL_OUTPUT_LVDS);
610 break;
611 case INTEL_OUTPUT_MIPI:
612 crtc_mask = (1 << 0);
613 clone_mask = (1 << INTEL_OUTPUT_MIPI);
614 break;
615 case INTEL_OUTPUT_MIPI2:
616 crtc_mask = (1 << 2);
617 clone_mask = (1 << INTEL_OUTPUT_MIPI2);
618 break;
619 case INTEL_OUTPUT_HDMI:
620 crtc_mask = dev_priv->ops->hdmi_mask;
621 clone_mask = (1 << INTEL_OUTPUT_HDMI);
622 break;
623 case INTEL_OUTPUT_DISPLAYPORT:
624 crtc_mask = (1 << 0) | (1 << 1);
625 clone_mask = (1 << INTEL_OUTPUT_DISPLAYPORT);
626 break;
627 case INTEL_OUTPUT_EDP:
628 crtc_mask = (1 << 1);
629 clone_mask = (1 << INTEL_OUTPUT_EDP);
631 encoder->possible_crtcs = crtc_mask;
632 encoder->possible_clones =
633 gma_connector_clones(dev, clone_mask);
637 void psb_modeset_init(struct drm_device *dev)
639 struct drm_psb_private *dev_priv = dev->dev_private;
640 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
641 int i;
643 drm_mode_config_init(dev);
645 dev->mode_config.min_width = 0;
646 dev->mode_config.min_height = 0;
648 dev->mode_config.funcs = &psb_mode_funcs;
650 /* set memory base */
651 /* Oaktrail and Poulsbo should use BAR 2*/
652 pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *)
653 &(dev->mode_config.fb_base));
655 /* num pipes is 2 for PSB but 1 for Mrst */
656 for (i = 0; i < dev_priv->num_pipe; i++)
657 psb_intel_crtc_init(dev, i, mode_dev);
659 dev->mode_config.max_width = 4096;
660 dev->mode_config.max_height = 4096;
662 psb_setup_outputs(dev);
664 if (dev_priv->ops->errata)
665 dev_priv->ops->errata(dev);
667 dev_priv->modeset = true;
670 void psb_modeset_cleanup(struct drm_device *dev)
672 struct drm_psb_private *dev_priv = dev->dev_private;
673 if (dev_priv->modeset) {
674 drm_kms_helper_poll_fini(dev);
675 psb_fbdev_fini(dev);
676 drm_mode_config_cleanup(dev);