1 // SPDX-License-Identifier: GPL-2.0-only
2 /**************************************************************************
3 * Copyright (c) 2007-2011, Intel Corporation.
6 **************************************************************************/
9 #include <linux/pfn_t.h>
11 #include <drm/drm_crtc_helper.h>
12 #include <drm/drm_drv.h>
13 #include <drm/drm_fb_helper.h>
14 #include <drm/drm_framebuffer.h>
23 static vm_fault_t psb_fbdev_vm_fault(struct vm_fault *vmf)
25 struct vm_area_struct *vma = vmf->vma;
26 struct fb_info *info = vma->vm_private_data;
27 unsigned long address = vmf->address - (vmf->pgoff << PAGE_SHIFT);
28 unsigned long pfn = info->fix.smem_start >> PAGE_SHIFT;
29 vm_fault_t err = VM_FAULT_SIGBUS;
30 unsigned long page_num = vma_pages(vma);
33 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
35 for (i = 0; i < page_num; ++i) {
36 err = vmf_insert_mixed(vma, address, __pfn_to_pfn_t(pfn, PFN_DEV));
37 if (unlikely(err & VM_FAULT_ERROR))
46 static const struct vm_operations_struct psb_fbdev_vm_ops = {
47 .fault = psb_fbdev_vm_fault,
54 #define CMAP_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16)
56 static int psb_fbdev_fb_setcolreg(unsigned int regno,
57 unsigned int red, unsigned int green,
58 unsigned int blue, unsigned int transp,
61 struct drm_fb_helper *fb_helper = info->par;
62 struct drm_framebuffer *fb = fb_helper->fb;
71 red = CMAP_TOHW(red, info->var.red.length);
72 blue = CMAP_TOHW(blue, info->var.blue.length);
73 green = CMAP_TOHW(green, info->var.green.length);
74 transp = CMAP_TOHW(transp, info->var.transp.length);
76 v = (red << info->var.red.offset) |
77 (green << info->var.green.offset) |
78 (blue << info->var.blue.offset) |
79 (transp << info->var.transp.offset);
82 switch (fb->format->cpp[0] * 8) {
84 ((uint32_t *) info->pseudo_palette)[regno] = v;
88 ((uint32_t *) info->pseudo_palette)[regno] = v;
96 static int psb_fbdev_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
98 if (vma->vm_pgoff != 0)
100 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
104 * If this is a GEM object then info->screen_base is the virtual
105 * kernel remapping of the object. FIXME: Review if this is
106 * suitable for our mmap work
108 vma->vm_ops = &psb_fbdev_vm_ops;
109 vma->vm_private_data = info;
110 vm_flags_set(vma, VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP);
115 static void psb_fbdev_fb_destroy(struct fb_info *info)
117 struct drm_fb_helper *fb_helper = info->par;
118 struct drm_framebuffer *fb = fb_helper->fb;
119 struct drm_gem_object *obj = fb->obj[0];
121 drm_fb_helper_fini(fb_helper);
123 drm_framebuffer_unregister_private(fb);
125 drm_framebuffer_cleanup(fb);
128 drm_gem_object_put(obj);
130 drm_client_release(&fb_helper->client);
132 drm_fb_helper_unprepare(fb_helper);
136 static const struct fb_ops psb_fbdev_fb_ops = {
137 .owner = THIS_MODULE,
138 __FB_DEFAULT_IOMEM_OPS_RDWR,
139 DRM_FB_HELPER_DEFAULT_OPS,
140 .fb_setcolreg = psb_fbdev_fb_setcolreg,
141 __FB_DEFAULT_IOMEM_OPS_DRAW,
142 .fb_mmap = psb_fbdev_fb_mmap,
143 .fb_destroy = psb_fbdev_fb_destroy,
146 static const struct drm_fb_helper_funcs psb_fbdev_fb_helper_funcs = {
153 int psb_fbdev_driver_fbdev_probe(struct drm_fb_helper *fb_helper,
154 struct drm_fb_helper_surface_size *sizes)
156 struct drm_device *dev = fb_helper->dev;
157 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
158 struct pci_dev *pdev = to_pci_dev(dev->dev);
159 struct fb_info *info;
160 struct drm_framebuffer *fb;
161 struct drm_mode_fb_cmd2 mode_cmd = { };
164 struct psb_gem_object *backing;
165 struct drm_gem_object *obj;
168 /* No 24-bit packed mode */
169 if (sizes->surface_bpp == 24) {
170 sizes->surface_bpp = 32;
171 sizes->surface_depth = 24;
173 bpp = sizes->surface_bpp;
174 depth = sizes->surface_depth;
177 * If the mode does not fit in 32 bit then switch to 16 bit to get
178 * a console on full resolution. The X mode setting server will
179 * allocate its own 32-bit GEM framebuffer.
181 size = ALIGN(sizes->surface_width * DIV_ROUND_UP(bpp, 8), 64) *
182 sizes->surface_height;
183 size = ALIGN(size, PAGE_SIZE);
185 if (size > dev_priv->vram_stolen_size) {
186 sizes->surface_bpp = 16;
187 sizes->surface_depth = 16;
189 bpp = sizes->surface_bpp;
190 depth = sizes->surface_depth;
192 mode_cmd.width = sizes->surface_width;
193 mode_cmd.height = sizes->surface_height;
194 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * DIV_ROUND_UP(bpp, 8), 64);
195 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
197 size = mode_cmd.pitches[0] * mode_cmd.height;
198 size = ALIGN(size, PAGE_SIZE);
200 /* Allocate the framebuffer in the GTT with stolen page backing */
201 backing = psb_gem_create(dev, size, "fb", true, PAGE_SIZE);
203 return PTR_ERR(backing);
204 obj = &backing->base;
206 fb = psb_framebuffer_create(dev, &mode_cmd, obj);
209 goto err_drm_gem_object_put;
212 fb_helper->funcs = &psb_fbdev_fb_helper_funcs;
215 info = drm_fb_helper_alloc_info(fb_helper);
218 goto err_drm_framebuffer_unregister_private;
221 info->fbops = &psb_fbdev_fb_ops;
223 /* Accessed stolen memory directly */
224 info->screen_base = dev_priv->vram_addr + backing->offset;
225 info->screen_size = size;
227 drm_fb_helper_fill_info(info, fb_helper, sizes);
229 info->fix.smem_start = dev_priv->stolen_base + backing->offset;
230 info->fix.smem_len = size;
231 info->fix.ywrapstep = 0;
232 info->fix.ypanstep = 0;
233 info->fix.mmio_start = pci_resource_start(pdev, 0);
234 info->fix.mmio_len = pci_resource_len(pdev, 0);
236 fb_memset_io(info->screen_base, 0, info->screen_size);
238 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
240 dev_dbg(dev->dev, "allocated %dx%d fb\n", fb->width, fb->height);
244 err_drm_framebuffer_unregister_private:
245 drm_framebuffer_unregister_private(fb);
247 drm_framebuffer_cleanup(fb);
249 err_drm_gem_object_put:
250 drm_gem_object_put(obj);