1 // SPDX-License-Identifier: GPL-2.0-only
2 /**************************************************************************
3 * Copyright (c) 2007-2011, Intel Corporation.
6 **************************************************************************/
8 #include <linux/pfn_t.h>
10 #include <drm/drm_crtc_helper.h>
11 #include <drm/drm_drv.h>
12 #include <drm/drm_fb_helper.h>
13 #include <drm/drm_framebuffer.h>
22 static vm_fault_t psb_fbdev_vm_fault(struct vm_fault *vmf)
24 struct vm_area_struct *vma = vmf->vma;
25 struct fb_info *info = vma->vm_private_data;
26 unsigned long address = vmf->address - (vmf->pgoff << PAGE_SHIFT);
27 unsigned long pfn = info->fix.smem_start >> PAGE_SHIFT;
28 vm_fault_t err = VM_FAULT_SIGBUS;
29 unsigned long page_num = vma_pages(vma);
32 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
34 for (i = 0; i < page_num; ++i) {
35 err = vmf_insert_mixed(vma, address, __pfn_to_pfn_t(pfn, PFN_DEV));
36 if (unlikely(err & VM_FAULT_ERROR))
45 static const struct vm_operations_struct psb_fbdev_vm_ops = {
46 .fault = psb_fbdev_vm_fault,
53 #define CMAP_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16)
55 static int psb_fbdev_fb_setcolreg(unsigned int regno,
56 unsigned int red, unsigned int green,
57 unsigned int blue, unsigned int transp,
60 struct drm_fb_helper *fb_helper = info->par;
61 struct drm_framebuffer *fb = fb_helper->fb;
70 red = CMAP_TOHW(red, info->var.red.length);
71 blue = CMAP_TOHW(blue, info->var.blue.length);
72 green = CMAP_TOHW(green, info->var.green.length);
73 transp = CMAP_TOHW(transp, info->var.transp.length);
75 v = (red << info->var.red.offset) |
76 (green << info->var.green.offset) |
77 (blue << info->var.blue.offset) |
78 (transp << info->var.transp.offset);
81 switch (fb->format->cpp[0] * 8) {
83 ((uint32_t *) info->pseudo_palette)[regno] = v;
87 ((uint32_t *) info->pseudo_palette)[regno] = v;
95 static int psb_fbdev_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
97 if (vma->vm_pgoff != 0)
99 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
103 * If this is a GEM object then info->screen_base is the virtual
104 * kernel remapping of the object. FIXME: Review if this is
105 * suitable for our mmap work
107 vma->vm_ops = &psb_fbdev_vm_ops;
108 vma->vm_private_data = info;
109 vm_flags_set(vma, VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP);
114 static void psb_fbdev_fb_destroy(struct fb_info *info)
116 struct drm_fb_helper *fb_helper = info->par;
117 struct drm_framebuffer *fb = fb_helper->fb;
118 struct drm_gem_object *obj = fb->obj[0];
120 drm_fb_helper_fini(fb_helper);
122 drm_framebuffer_unregister_private(fb);
124 drm_framebuffer_cleanup(fb);
127 drm_gem_object_put(obj);
129 drm_client_release(&fb_helper->client);
131 drm_fb_helper_unprepare(fb_helper);
135 static const struct fb_ops psb_fbdev_fb_ops = {
136 .owner = THIS_MODULE,
137 DRM_FB_HELPER_DEFAULT_OPS,
138 .fb_setcolreg = psb_fbdev_fb_setcolreg,
139 .fb_read = drm_fb_helper_cfb_read,
140 .fb_write = drm_fb_helper_cfb_write,
141 .fb_fillrect = drm_fb_helper_cfb_fillrect,
142 .fb_copyarea = drm_fb_helper_cfb_copyarea,
143 .fb_imageblit = drm_fb_helper_cfb_imageblit,
144 .fb_mmap = psb_fbdev_fb_mmap,
145 .fb_destroy = psb_fbdev_fb_destroy,
149 * struct drm_fb_helper_funcs
152 static int psb_fbdev_fb_probe(struct drm_fb_helper *fb_helper,
153 struct drm_fb_helper_surface_size *sizes)
155 struct drm_device *dev = fb_helper->dev;
156 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
157 struct pci_dev *pdev = to_pci_dev(dev->dev);
158 struct fb_info *info;
159 struct drm_framebuffer *fb;
160 struct drm_mode_fb_cmd2 mode_cmd = { };
163 struct psb_gem_object *backing;
164 struct drm_gem_object *obj;
167 /* No 24-bit packed mode */
168 if (sizes->surface_bpp == 24) {
169 sizes->surface_bpp = 32;
170 sizes->surface_depth = 24;
172 bpp = sizes->surface_bpp;
173 depth = sizes->surface_depth;
176 * If the mode does not fit in 32 bit then switch to 16 bit to get
177 * a console on full resolution. The X mode setting server will
178 * allocate its own 32-bit GEM framebuffer.
180 size = ALIGN(sizes->surface_width * DIV_ROUND_UP(bpp, 8), 64) *
181 sizes->surface_height;
182 size = ALIGN(size, PAGE_SIZE);
184 if (size > dev_priv->vram_stolen_size) {
185 sizes->surface_bpp = 16;
186 sizes->surface_depth = 16;
188 bpp = sizes->surface_bpp;
189 depth = sizes->surface_depth;
191 mode_cmd.width = sizes->surface_width;
192 mode_cmd.height = sizes->surface_height;
193 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * DIV_ROUND_UP(bpp, 8), 64);
194 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
196 size = mode_cmd.pitches[0] * mode_cmd.height;
197 size = ALIGN(size, PAGE_SIZE);
199 /* Allocate the framebuffer in the GTT with stolen page backing */
200 backing = psb_gem_create(dev, size, "fb", true, PAGE_SIZE);
202 return PTR_ERR(backing);
203 obj = &backing->base;
205 fb = psb_framebuffer_create(dev, &mode_cmd, obj);
208 goto err_drm_gem_object_put;
213 info = drm_fb_helper_alloc_info(fb_helper);
216 goto err_drm_framebuffer_unregister_private;
219 info->fbops = &psb_fbdev_fb_ops;
220 info->flags = FBINFO_DEFAULT;
221 /* Accessed stolen memory directly */
222 info->screen_base = dev_priv->vram_addr + backing->offset;
223 info->screen_size = size;
225 drm_fb_helper_fill_info(info, fb_helper, sizes);
227 info->fix.smem_start = dev_priv->stolen_base + backing->offset;
228 info->fix.smem_len = size;
229 info->fix.ywrapstep = 0;
230 info->fix.ypanstep = 0;
231 info->fix.mmio_start = pci_resource_start(pdev, 0);
232 info->fix.mmio_len = pci_resource_len(pdev, 0);
234 memset(info->screen_base, 0, info->screen_size);
236 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
238 dev_dbg(dev->dev, "allocated %dx%d fb\n", fb->width, fb->height);
242 err_drm_framebuffer_unregister_private:
243 drm_framebuffer_unregister_private(fb);
245 drm_framebuffer_cleanup(fb);
247 err_drm_gem_object_put:
248 drm_gem_object_put(obj);
252 static const struct drm_fb_helper_funcs psb_fbdev_fb_helper_funcs = {
253 .fb_probe = psb_fbdev_fb_probe,
257 * struct drm_client_funcs and setup code
260 static void psb_fbdev_client_unregister(struct drm_client_dev *client)
262 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
264 if (fb_helper->info) {
265 drm_fb_helper_unregister_info(fb_helper);
267 drm_fb_helper_unprepare(fb_helper);
268 drm_client_release(&fb_helper->client);
273 static int psb_fbdev_client_restore(struct drm_client_dev *client)
275 drm_fb_helper_lastclose(client->dev);
280 static int psb_fbdev_client_hotplug(struct drm_client_dev *client)
282 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
283 struct drm_device *dev = client->dev;
287 return drm_fb_helper_hotplug_event(dev->fb_helper);
289 ret = drm_fb_helper_init(dev, fb_helper);
293 if (!drm_drv_uses_atomic_modeset(dev))
294 drm_helper_disable_unused_functions(dev);
296 ret = drm_fb_helper_initial_config(fb_helper);
298 goto err_drm_fb_helper_fini;
302 err_drm_fb_helper_fini:
303 drm_fb_helper_fini(fb_helper);
305 drm_err(dev, "Failed to setup gma500 fbdev emulation (ret=%d)\n", ret);
309 static const struct drm_client_funcs psb_fbdev_client_funcs = {
310 .owner = THIS_MODULE,
311 .unregister = psb_fbdev_client_unregister,
312 .restore = psb_fbdev_client_restore,
313 .hotplug = psb_fbdev_client_hotplug,
316 void psb_fbdev_setup(struct drm_psb_private *dev_priv)
318 struct drm_device *dev = &dev_priv->dev;
319 struct drm_fb_helper *fb_helper;
322 fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL);
325 drm_fb_helper_prepare(dev, fb_helper, 32, &psb_fbdev_fb_helper_funcs);
327 ret = drm_client_init(dev, &fb_helper->client, "fbdev-gma500", &psb_fbdev_client_funcs);
329 drm_err(dev, "Failed to register client: %d\n", ret);
330 goto err_drm_fb_helper_unprepare;
333 ret = psb_fbdev_client_hotplug(&fb_helper->client);
335 drm_dbg_kms(dev, "client hotplug ret=%d\n", ret);
337 drm_client_register(&fb_helper->client);
341 err_drm_fb_helper_unprepare:
342 drm_fb_helper_unprepare(fb_helper);