1 /**************************************************************************
2 * Copyright (c) 2007-2011, Intel Corporation.
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
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>
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>
34 #include <drm/drm_crtc.h>
35 #include <drm/drm_fb_helper.h>
38 #include "psb_intel_reg.h"
39 #include "psb_intel_drv.h"
40 #include "framebuffer.h"
43 static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb);
44 static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb,
45 struct drm_file *file_priv,
46 unsigned int *handle);
48 static const struct drm_framebuffer_funcs psb_fb_funcs = {
49 .destroy = psb_user_framebuffer_destroy,
50 .create_handle = psb_user_framebuffer_create_handle,
53 #define CMAP_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16)
55 static int psbfb_setcolreg(unsigned regno, unsigned red, unsigned green,
56 unsigned blue, unsigned transp,
59 struct psb_fbdev *fbdev = info->par;
60 struct drm_framebuffer *fb = fbdev->psb_fb_helper.fb;
69 red = CMAP_TOHW(red, info->var.red.length);
70 blue = CMAP_TOHW(blue, info->var.blue.length);
71 green = CMAP_TOHW(green, info->var.green.length);
72 transp = CMAP_TOHW(transp, info->var.transp.length);
74 v = (red << info->var.red.offset) |
75 (green << info->var.green.offset) |
76 (blue << info->var.blue.offset) |
77 (transp << info->var.transp.offset);
80 switch (fb->format->cpp[0] * 8) {
82 ((uint32_t *) info->pseudo_palette)[regno] = v;
86 ((uint32_t *) info->pseudo_palette)[regno] = v;
94 static int psbfb_pan(struct fb_var_screeninfo *var, struct fb_info *info)
96 struct psb_fbdev *fbdev = info->par;
97 struct psb_framebuffer *psbfb = &fbdev->pfb;
98 struct drm_device *dev = psbfb->base.dev;
101 * We have to poke our nose in here. The core fb code assumes
102 * panning is part of the hardware that can be invoked before
103 * the actual fb is mapped. In our case that isn't quite true.
105 if (psbfb->gtt->npage) {
106 /* GTT roll shifts in 4K pages, we need to shift the right
108 int pages = info->fix.line_length >> 12;
109 psb_gtt_roll(dev, psbfb->gtt, var->yoffset * pages);
114 static int psbfb_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
116 struct psb_framebuffer *psbfb = vma->vm_private_data;
117 struct drm_device *dev = psbfb->base.dev;
118 struct drm_psb_private *dev_priv = dev->dev_private;
121 unsigned long address;
124 unsigned long phys_addr = (unsigned long)dev_priv->stolen_base +
127 page_num = vma_pages(vma);
128 address = vmf->address - (vmf->pgoff << PAGE_SHIFT);
130 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
132 for (i = 0; i < page_num; i++) {
133 pfn = (phys_addr >> PAGE_SHIFT);
135 ret = vm_insert_mixed(vma, address,
136 __pfn_to_pfn_t(pfn, PFN_DEV));
137 if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0)))
139 else if (unlikely(ret != 0)) {
140 ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
143 address += PAGE_SIZE;
144 phys_addr += PAGE_SIZE;
146 return VM_FAULT_NOPAGE;
149 static void psbfb_vm_open(struct vm_area_struct *vma)
153 static void psbfb_vm_close(struct vm_area_struct *vma)
157 static const struct vm_operations_struct psbfb_vm_ops = {
158 .fault = psbfb_vm_fault,
159 .open = psbfb_vm_open,
160 .close = psbfb_vm_close
163 static int psbfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
165 struct psb_fbdev *fbdev = info->par;
166 struct psb_framebuffer *psbfb = &fbdev->pfb;
168 if (vma->vm_pgoff != 0)
170 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
173 if (!psbfb->addr_space)
174 psbfb->addr_space = vma->vm_file->f_mapping;
176 * If this is a GEM object then info->screen_base is the virtual
177 * kernel remapping of the object. FIXME: Review if this is
178 * suitable for our mmap work
180 vma->vm_ops = &psbfb_vm_ops;
181 vma->vm_private_data = (void *)psbfb;
182 vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP;
186 static struct fb_ops psbfb_ops = {
187 .owner = THIS_MODULE,
188 DRM_FB_HELPER_DEFAULT_OPS,
189 .fb_setcolreg = psbfb_setcolreg,
190 .fb_fillrect = drm_fb_helper_cfb_fillrect,
191 .fb_copyarea = psbfb_copyarea,
192 .fb_imageblit = drm_fb_helper_cfb_imageblit,
193 .fb_mmap = psbfb_mmap,
194 .fb_sync = psbfb_sync,
197 static struct fb_ops psbfb_roll_ops = {
198 .owner = THIS_MODULE,
199 DRM_FB_HELPER_DEFAULT_OPS,
200 .fb_setcolreg = psbfb_setcolreg,
201 .fb_fillrect = drm_fb_helper_cfb_fillrect,
202 .fb_copyarea = drm_fb_helper_cfb_copyarea,
203 .fb_imageblit = drm_fb_helper_cfb_imageblit,
204 .fb_pan_display = psbfb_pan,
205 .fb_mmap = psbfb_mmap,
208 static struct fb_ops psbfb_unaccel_ops = {
209 .owner = THIS_MODULE,
210 DRM_FB_HELPER_DEFAULT_OPS,
211 .fb_setcolreg = psbfb_setcolreg,
212 .fb_fillrect = drm_fb_helper_cfb_fillrect,
213 .fb_copyarea = drm_fb_helper_cfb_copyarea,
214 .fb_imageblit = drm_fb_helper_cfb_imageblit,
215 .fb_mmap = psbfb_mmap,
219 * psb_framebuffer_init - initialize a framebuffer
220 * @dev: our DRM device
221 * @fb: framebuffer to set up
222 * @mode_cmd: mode description
223 * @gt: backing object
225 * Configure and fill in the boilerplate for our frame buffer. Return
226 * 0 on success or an error code if we fail.
228 static int psb_framebuffer_init(struct drm_device *dev,
229 struct psb_framebuffer *fb,
230 const struct drm_mode_fb_cmd2 *mode_cmd,
231 struct gtt_range *gt)
233 const struct drm_format_info *info;
237 * Reject unknown formats, YUV formats, and formats with more than
240 info = drm_format_info(mode_cmd->pixel_format);
241 if (!info || !info->depth || info->cpp[0] > 4)
244 if (mode_cmd->pitches[0] & 63)
247 drm_helper_mode_fill_fb_struct(dev, &fb->base, mode_cmd);
249 ret = drm_framebuffer_init(dev, &fb->base, &psb_fb_funcs);
251 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
258 * psb_framebuffer_create - create a framebuffer backed by gt
259 * @dev: our DRM device
260 * @mode_cmd: the description of the requested mode
261 * @gt: the backing object
263 * Create a framebuffer object backed by the gt, and fill in the
264 * boilerplate required
266 * TODO: review object references
269 static struct drm_framebuffer *psb_framebuffer_create
270 (struct drm_device *dev,
271 const struct drm_mode_fb_cmd2 *mode_cmd,
272 struct gtt_range *gt)
274 struct psb_framebuffer *fb;
277 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
279 return ERR_PTR(-ENOMEM);
281 ret = psb_framebuffer_init(dev, fb, mode_cmd, gt);
290 * psbfb_alloc - allocate frame buffer memory
291 * @dev: the DRM device
292 * @aligned_size: space needed
294 * Allocate the frame buffer. In the usual case we get a GTT range that
295 * is stolen memory backed and life is simple. If there isn't sufficient
296 * we fail as we don't have the virtual mapping space to really vmap it
297 * and the kernel console code can't handle non linear framebuffers.
299 * Re-address this as and if the framebuffer layer grows this ability.
301 static struct gtt_range *psbfb_alloc(struct drm_device *dev, int aligned_size)
303 struct gtt_range *backing;
304 /* Begin by trying to use stolen memory backing */
305 backing = psb_gtt_alloc_range(dev, aligned_size, "fb", 1, PAGE_SIZE);
307 drm_gem_private_object_init(dev, &backing->gem, aligned_size);
314 * psbfb_create - create a framebuffer
315 * @fbdev: the framebuffer device
316 * @sizes: specification of the layout
318 * Create a framebuffer to the specifications provided
320 static int psbfb_create(struct psb_fbdev *fbdev,
321 struct drm_fb_helper_surface_size *sizes)
323 struct drm_device *dev = fbdev->psb_fb_helper.dev;
324 struct drm_psb_private *dev_priv = dev->dev_private;
325 struct fb_info *info;
326 struct drm_framebuffer *fb;
327 struct psb_framebuffer *psbfb = &fbdev->pfb;
328 struct drm_mode_fb_cmd2 mode_cmd;
331 struct gtt_range *backing;
336 mode_cmd.width = sizes->surface_width;
337 mode_cmd.height = sizes->surface_height;
338 bpp = sizes->surface_bpp;
339 depth = sizes->surface_depth;
341 /* No 24bit packed */
347 * Acceleration via the GTT requires pitch to be
348 * power of two aligned. Preferably page but less
349 * is ok with some fonts
351 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 4096 >> pitch_lines);
353 size = mode_cmd.pitches[0] * mode_cmd.height;
354 size = ALIGN(size, PAGE_SIZE);
356 /* Allocate the fb in the GTT with stolen page backing */
357 backing = psbfb_alloc(dev, size);
364 } while (backing == NULL && pitch_lines <= 16);
366 /* The final pitch we accepted if we succeeded */
369 if (backing == NULL) {
371 * We couldn't get the space we wanted, fall back to the
372 * display engine requirement instead. The HW requires
373 * the pitch to be 64 byte aligned
376 gtt_roll = 0; /* Don't use GTT accelerated scrolling */
379 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 64);
381 size = mode_cmd.pitches[0] * mode_cmd.height;
382 size = ALIGN(size, PAGE_SIZE);
384 /* Allocate the framebuffer in the GTT with stolen page backing */
385 backing = psbfb_alloc(dev, size);
390 memset(dev_priv->vram_addr + backing->offset, 0, size);
392 info = drm_fb_helper_alloc_fbi(&fbdev->psb_fb_helper);
399 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
401 ret = psb_framebuffer_init(dev, psbfb, &mode_cmd, backing);
408 fbdev->psb_fb_helper.fb = fb;
410 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
411 strcpy(info->fix.id, "psbdrmfb");
413 info->flags = FBINFO_DEFAULT;
414 if (dev_priv->ops->accel_2d && pitch_lines > 8) /* 2D engine */
415 info->fbops = &psbfb_ops;
416 else if (gtt_roll) { /* GTT rolling seems best */
417 info->fbops = &psbfb_roll_ops;
418 info->flags |= FBINFO_HWACCEL_YPAN;
419 } else /* Software */
420 info->fbops = &psbfb_unaccel_ops;
422 info->fix.smem_start = dev->mode_config.fb_base;
423 info->fix.smem_len = size;
424 info->fix.ywrapstep = gtt_roll;
425 info->fix.ypanstep = 0;
427 /* Accessed stolen memory directly */
428 info->screen_base = dev_priv->vram_addr + backing->offset;
429 info->screen_size = size;
431 if (dev_priv->gtt.stolen_size) {
432 info->apertures->ranges[0].base = dev->mode_config.fb_base;
433 info->apertures->ranges[0].size = dev_priv->gtt.stolen_size;
436 drm_fb_helper_fill_var(info, &fbdev->psb_fb_helper,
437 sizes->fb_width, sizes->fb_height);
439 info->fix.mmio_start = pci_resource_start(dev->pdev, 0);
440 info->fix.mmio_len = pci_resource_len(dev->pdev, 0);
442 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
444 dev_dbg(dev->dev, "allocated %dx%d fb\n",
445 psbfb->base.width, psbfb->base.height);
449 psb_gtt_free_range(dev, backing);
454 * psb_user_framebuffer_create - create framebuffer
455 * @dev: our DRM device
459 * Create a new framebuffer backed by a userspace GEM object
461 static struct drm_framebuffer *psb_user_framebuffer_create
462 (struct drm_device *dev, struct drm_file *filp,
463 const struct drm_mode_fb_cmd2 *cmd)
466 struct drm_gem_object *obj;
469 * Find the GEM object and thus the gtt range object that is
472 obj = drm_gem_object_lookup(filp, cmd->handles[0]);
474 return ERR_PTR(-ENOENT);
476 /* Let the core code do all the work */
477 r = container_of(obj, struct gtt_range, gem);
478 return psb_framebuffer_create(dev, cmd, r);
481 static void psbfb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
484 struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
486 gma_crtc->lut_r[regno] = red >> 8;
487 gma_crtc->lut_g[regno] = green >> 8;
488 gma_crtc->lut_b[regno] = blue >> 8;
491 static void psbfb_gamma_get(struct drm_crtc *crtc, u16 *red,
492 u16 *green, u16 *blue, int regno)
494 struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
496 *red = gma_crtc->lut_r[regno] << 8;
497 *green = gma_crtc->lut_g[regno] << 8;
498 *blue = gma_crtc->lut_b[regno] << 8;
501 static int psbfb_probe(struct drm_fb_helper *helper,
502 struct drm_fb_helper_surface_size *sizes)
504 struct psb_fbdev *psb_fbdev =
505 container_of(helper, struct psb_fbdev, psb_fb_helper);
506 struct drm_device *dev = psb_fbdev->psb_fb_helper.dev;
507 struct drm_psb_private *dev_priv = dev->dev_private;
510 bytespp = sizes->surface_bpp / 8;
511 if (bytespp == 3) /* no 24bit packed */
514 /* If the mode will not fit in 32bit then switch to 16bit to get
515 a console on full resolution. The X mode setting server will
516 allocate its own 32bit GEM framebuffer */
517 if (ALIGN(sizes->fb_width * bytespp, 64) * sizes->fb_height >
518 dev_priv->vram_stolen_size) {
519 sizes->surface_bpp = 16;
520 sizes->surface_depth = 16;
523 return psbfb_create(psb_fbdev, sizes);
526 static const struct drm_fb_helper_funcs psb_fb_helper_funcs = {
527 .gamma_set = psbfb_gamma_set,
528 .gamma_get = psbfb_gamma_get,
529 .fb_probe = psbfb_probe,
532 static int psb_fbdev_destroy(struct drm_device *dev, struct psb_fbdev *fbdev)
534 struct psb_framebuffer *psbfb = &fbdev->pfb;
536 drm_fb_helper_unregister_fbi(&fbdev->psb_fb_helper);
538 drm_fb_helper_fini(&fbdev->psb_fb_helper);
539 drm_framebuffer_unregister_private(&psbfb->base);
540 drm_framebuffer_cleanup(&psbfb->base);
543 drm_gem_object_unreference_unlocked(&psbfb->gtt->gem);
547 int psb_fbdev_init(struct drm_device *dev)
549 struct psb_fbdev *fbdev;
550 struct drm_psb_private *dev_priv = dev->dev_private;
553 fbdev = kzalloc(sizeof(struct psb_fbdev), GFP_KERNEL);
555 dev_err(dev->dev, "no memory\n");
559 dev_priv->fbdev = fbdev;
561 drm_fb_helper_prepare(dev, &fbdev->psb_fb_helper, &psb_fb_helper_funcs);
563 ret = drm_fb_helper_init(dev, &fbdev->psb_fb_helper,
568 ret = drm_fb_helper_single_add_all_connectors(&fbdev->psb_fb_helper);
572 /* disable all the possible outputs/crtcs before entering KMS mode */
573 drm_helper_disable_unused_functions(dev);
575 ret = drm_fb_helper_initial_config(&fbdev->psb_fb_helper, 32);
582 drm_fb_helper_fini(&fbdev->psb_fb_helper);
588 static void psb_fbdev_fini(struct drm_device *dev)
590 struct drm_psb_private *dev_priv = dev->dev_private;
592 if (!dev_priv->fbdev)
595 psb_fbdev_destroy(dev, dev_priv->fbdev);
596 kfree(dev_priv->fbdev);
597 dev_priv->fbdev = NULL;
600 static void psbfb_output_poll_changed(struct drm_device *dev)
602 struct drm_psb_private *dev_priv = dev->dev_private;
603 struct psb_fbdev *fbdev = (struct psb_fbdev *)dev_priv->fbdev;
604 drm_fb_helper_hotplug_event(&fbdev->psb_fb_helper);
608 * psb_user_framebuffer_create_handle - add hamdle to a framebuffer
610 * @file_priv: our DRM file
611 * @handle: returned handle
613 * Our framebuffer object is a GTT range which also contains a GEM
614 * object. We need to turn it into a handle for userspace. GEM will do
617 static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb,
618 struct drm_file *file_priv,
619 unsigned int *handle)
621 struct psb_framebuffer *psbfb = to_psb_fb(fb);
622 struct gtt_range *r = psbfb->gtt;
623 return drm_gem_handle_create(file_priv, &r->gem, handle);
627 * psb_user_framebuffer_destroy - destruct user created fb
630 * User framebuffers are backed by GEM objects so all we have to do is
631 * clean up a bit and drop the reference, GEM will handle the fallout
633 static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb)
635 struct psb_framebuffer *psbfb = to_psb_fb(fb);
636 struct gtt_range *r = psbfb->gtt;
638 /* Let DRM do its clean up */
639 drm_framebuffer_cleanup(fb);
640 /* We are no longer using the resource in GEM */
641 drm_gem_object_unreference_unlocked(&r->gem);
645 static const struct drm_mode_config_funcs psb_mode_funcs = {
646 .fb_create = psb_user_framebuffer_create,
647 .output_poll_changed = psbfb_output_poll_changed,
650 static void psb_setup_outputs(struct drm_device *dev)
652 struct drm_psb_private *dev_priv = dev->dev_private;
653 struct drm_connector *connector;
655 drm_mode_create_scaling_mode_property(dev);
657 /* It is ok for this to fail - we just don't get backlight control */
658 if (!dev_priv->backlight_property)
659 dev_priv->backlight_property = drm_property_create_range(dev, 0,
660 "backlight", 0, 100);
661 dev_priv->ops->output_init(dev);
663 list_for_each_entry(connector, &dev->mode_config.connector_list,
665 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
666 struct drm_encoder *encoder = &gma_encoder->base;
667 int crtc_mask = 0, clone_mask = 0;
670 switch (gma_encoder->type) {
671 case INTEL_OUTPUT_ANALOG:
672 crtc_mask = (1 << 0);
673 clone_mask = (1 << INTEL_OUTPUT_ANALOG);
675 case INTEL_OUTPUT_SDVO:
676 crtc_mask = dev_priv->ops->sdvo_mask;
677 clone_mask = (1 << INTEL_OUTPUT_SDVO);
679 case INTEL_OUTPUT_LVDS:
680 crtc_mask = dev_priv->ops->lvds_mask;
681 clone_mask = (1 << INTEL_OUTPUT_LVDS);
683 case INTEL_OUTPUT_MIPI:
684 crtc_mask = (1 << 0);
685 clone_mask = (1 << INTEL_OUTPUT_MIPI);
687 case INTEL_OUTPUT_MIPI2:
688 crtc_mask = (1 << 2);
689 clone_mask = (1 << INTEL_OUTPUT_MIPI2);
691 case INTEL_OUTPUT_HDMI:
692 crtc_mask = dev_priv->ops->hdmi_mask;
693 clone_mask = (1 << INTEL_OUTPUT_HDMI);
695 case INTEL_OUTPUT_DISPLAYPORT:
696 crtc_mask = (1 << 0) | (1 << 1);
697 clone_mask = (1 << INTEL_OUTPUT_DISPLAYPORT);
699 case INTEL_OUTPUT_EDP:
700 crtc_mask = (1 << 1);
701 clone_mask = (1 << INTEL_OUTPUT_EDP);
703 encoder->possible_crtcs = crtc_mask;
704 encoder->possible_clones =
705 gma_connector_clones(dev, clone_mask);
709 void psb_modeset_init(struct drm_device *dev)
711 struct drm_psb_private *dev_priv = dev->dev_private;
712 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
715 drm_mode_config_init(dev);
717 dev->mode_config.min_width = 0;
718 dev->mode_config.min_height = 0;
720 dev->mode_config.funcs = &psb_mode_funcs;
722 /* set memory base */
723 /* Oaktrail and Poulsbo should use BAR 2*/
724 pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *)
725 &(dev->mode_config.fb_base));
727 /* num pipes is 2 for PSB but 1 for Mrst */
728 for (i = 0; i < dev_priv->num_pipe; i++)
729 psb_intel_crtc_init(dev, i, mode_dev);
731 dev->mode_config.max_width = 4096;
732 dev->mode_config.max_height = 4096;
734 psb_setup_outputs(dev);
736 if (dev_priv->ops->errata)
737 dev_priv->ops->errata(dev);
739 dev_priv->modeset = true;
742 void psb_modeset_cleanup(struct drm_device *dev)
744 struct drm_psb_private *dev_priv = dev->dev_private;
745 if (dev_priv->modeset) {
746 drm_kms_helper_poll_fini(dev);
748 drm_mode_config_cleanup(dev);