2 * drm kms/fb cma (contiguous memory allocator) helper functions
4 * Copyright (C) 2012 Analog Device Inc.
5 * Author: Lars-Peter Clausen <lars@metafoo.de>
8 * Copyright (C) 2012 Red Hat
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
21 #include <drm/drm_fb_helper.h>
22 #include <drm/drm_framebuffer.h>
23 #include <drm/drm_gem_cma_helper.h>
24 #include <drm/drm_gem_framebuffer_helper.h>
25 #include <drm/drm_fb_cma_helper.h>
26 #include <linux/module.h>
28 #define DEFAULT_FBDEFIO_DELAY_MS 50
30 struct drm_fbdev_cma {
31 struct drm_fb_helper fb_helper;
32 const struct drm_framebuffer_funcs *fb_funcs;
36 * DOC: framebuffer cma helper functions
38 * Provides helper functions for creating a cma (contiguous memory allocator)
41 * drm_fb_cma_create() is used in the &drm_mode_config_funcs.fb_create
42 * callback function to create a cma backed framebuffer.
44 * An fbdev framebuffer backed by cma is also available by calling
45 * drm_fbdev_cma_init(). drm_fbdev_cma_fini() tears it down.
46 * If the &drm_framebuffer_funcs.dirty callback is set, fb_deferred_io will be
47 * set up automatically. &drm_framebuffer_funcs.dirty is called by
48 * drm_fb_helper_deferred_io() in process context (&struct delayed_work).
50 * Example fbdev deferred io code::
52 * static int driver_fb_dirty(struct drm_framebuffer *fb,
53 * struct drm_file *file_priv,
54 * unsigned flags, unsigned color,
55 * struct drm_clip_rect *clips,
58 * struct drm_gem_cma_object *cma = drm_fb_cma_get_gem_obj(fb, 0);
59 * ... push changes ...
63 * static struct drm_framebuffer_funcs driver_fb_funcs = {
64 * .destroy = drm_fb_cma_destroy,
65 * .create_handle = drm_fb_cma_create_handle,
66 * .dirty = driver_fb_dirty,
71 * fbdev = drm_fbdev_cma_init_with_funcs(dev, 16,
72 * dev->mode_config.num_crtc,
73 * dev->mode_config.num_connector,
78 static inline struct drm_fbdev_cma *to_fbdev_cma(struct drm_fb_helper *helper)
80 return container_of(helper, struct drm_fbdev_cma, fb_helper);
83 void drm_fb_cma_destroy(struct drm_framebuffer *fb)
85 drm_gem_fb_destroy(fb);
87 EXPORT_SYMBOL(drm_fb_cma_destroy);
89 int drm_fb_cma_create_handle(struct drm_framebuffer *fb,
90 struct drm_file *file_priv, unsigned int *handle)
92 return drm_gem_fb_create_handle(fb, file_priv, handle);
94 EXPORT_SYMBOL(drm_fb_cma_create_handle);
97 * drm_fb_cma_create_with_funcs() - helper function for the
98 * &drm_mode_config_funcs.fb_create
101 * @file_priv: drm file for the ioctl call
102 * @mode_cmd: metadata from the userspace fb creation request
103 * @funcs: vtable to be used for the new framebuffer object
105 * This can be used to set &drm_framebuffer_funcs for drivers that need the
106 * &drm_framebuffer_funcs.dirty callback. Use drm_fb_cma_create() if you don't
107 * need to change &drm_framebuffer_funcs.
109 struct drm_framebuffer *drm_fb_cma_create_with_funcs(struct drm_device *dev,
110 struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd,
111 const struct drm_framebuffer_funcs *funcs)
113 return drm_gem_fb_create_with_funcs(dev, file_priv, mode_cmd, funcs);
115 EXPORT_SYMBOL_GPL(drm_fb_cma_create_with_funcs);
118 * drm_fb_cma_create() - &drm_mode_config_funcs.fb_create callback function
120 * @file_priv: drm file for the ioctl call
121 * @mode_cmd: metadata from the userspace fb creation request
123 * If your hardware has special alignment or pitch requirements these should be
124 * checked before calling this function. Use drm_fb_cma_create_with_funcs() if
125 * you need to set &drm_framebuffer_funcs.dirty.
127 struct drm_framebuffer *drm_fb_cma_create(struct drm_device *dev,
128 struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd)
130 return drm_gem_fb_create(dev, file_priv, mode_cmd);
132 EXPORT_SYMBOL_GPL(drm_fb_cma_create);
135 * drm_fb_cma_get_gem_obj() - Get CMA GEM object for framebuffer
136 * @fb: The framebuffer
137 * @plane: Which plane
139 * Return the CMA GEM object for given framebuffer.
141 * This function will usually be called from the CRTC callback functions.
143 struct drm_gem_cma_object *drm_fb_cma_get_gem_obj(struct drm_framebuffer *fb,
146 struct drm_gem_object *gem;
148 gem = drm_gem_fb_get_obj(fb, plane);
152 return to_drm_gem_cma_obj(gem);
154 EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_obj);
157 * drm_fb_cma_get_gem_addr() - Get physical address for framebuffer
158 * @fb: The framebuffer
159 * @state: Which state of drm plane
160 * @plane: Which plane
161 * Return the CMA GEM address for given framebuffer.
163 * This function will usually be called from the PLANE callback functions.
165 dma_addr_t drm_fb_cma_get_gem_addr(struct drm_framebuffer *fb,
166 struct drm_plane_state *state,
169 struct drm_gem_cma_object *obj;
172 obj = drm_fb_cma_get_gem_obj(fb, plane);
176 paddr = obj->paddr + fb->offsets[plane];
177 paddr += fb->format->cpp[plane] * (state->src_x >> 16);
178 paddr += fb->pitches[plane] * (state->src_y >> 16);
182 EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_addr);
185 * drm_fb_cma_prepare_fb() - Prepare CMA framebuffer
186 * @plane: Which plane
187 * @state: Plane state attach fence to
189 * This should be set as the &struct drm_plane_helper_funcs.prepare_fb hook.
191 * This function checks if the plane FB has an dma-buf attached, extracts
192 * the exclusive fence and attaches it to plane state for the atomic helper
195 * There is no need for cleanup_fb for CMA based framebuffer drivers.
197 int drm_fb_cma_prepare_fb(struct drm_plane *plane,
198 struct drm_plane_state *state)
200 return drm_gem_fb_prepare_fb(plane, state);
202 EXPORT_SYMBOL_GPL(drm_fb_cma_prepare_fb);
204 #ifdef CONFIG_DEBUG_FS
205 static void drm_fb_cma_describe(struct drm_framebuffer *fb, struct seq_file *m)
209 seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height,
210 (char *)&fb->format->format);
212 for (i = 0; i < fb->format->num_planes; i++) {
213 seq_printf(m, " %d: offset=%d pitch=%d, obj: ",
214 i, fb->offsets[i], fb->pitches[i]);
215 drm_gem_cma_describe(drm_fb_cma_get_gem_obj(fb, i), m);
220 * drm_fb_cma_debugfs_show() - Helper to list CMA framebuffer objects
223 * @arg: private data for the callback
225 int drm_fb_cma_debugfs_show(struct seq_file *m, void *arg)
227 struct drm_info_node *node = (struct drm_info_node *) m->private;
228 struct drm_device *dev = node->minor->dev;
229 struct drm_framebuffer *fb;
231 mutex_lock(&dev->mode_config.fb_lock);
232 drm_for_each_fb(fb, dev)
233 drm_fb_cma_describe(fb, m);
234 mutex_unlock(&dev->mode_config.fb_lock);
238 EXPORT_SYMBOL_GPL(drm_fb_cma_debugfs_show);
241 static int drm_fb_cma_mmap(struct fb_info *info, struct vm_area_struct *vma)
243 return dma_mmap_writecombine(info->device, vma, info->screen_base,
244 info->fix.smem_start, info->fix.smem_len);
247 static struct fb_ops drm_fbdev_cma_ops = {
248 .owner = THIS_MODULE,
249 DRM_FB_HELPER_DEFAULT_OPS,
250 .fb_fillrect = drm_fb_helper_sys_fillrect,
251 .fb_copyarea = drm_fb_helper_sys_copyarea,
252 .fb_imageblit = drm_fb_helper_sys_imageblit,
253 .fb_mmap = drm_fb_cma_mmap,
256 static int drm_fbdev_cma_deferred_io_mmap(struct fb_info *info,
257 struct vm_area_struct *vma)
259 fb_deferred_io_mmap(info, vma);
260 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
265 static int drm_fbdev_cma_defio_init(struct fb_info *fbi,
266 struct drm_gem_cma_object *cma_obj)
268 struct fb_deferred_io *fbdefio;
269 struct fb_ops *fbops;
272 * Per device structures are needed because:
273 * fbops: fb_deferred_io_cleanup() clears fbops.fb_mmap
274 * fbdefio: individual delays
276 fbdefio = kzalloc(sizeof(*fbdefio), GFP_KERNEL);
277 fbops = kzalloc(sizeof(*fbops), GFP_KERNEL);
278 if (!fbdefio || !fbops) {
284 /* can't be offset from vaddr since dirty() uses cma_obj */
285 fbi->screen_buffer = cma_obj->vaddr;
286 /* fb_deferred_io_fault() needs a physical address */
287 fbi->fix.smem_start = page_to_phys(virt_to_page(fbi->screen_buffer));
289 *fbops = *fbi->fbops;
292 fbdefio->delay = msecs_to_jiffies(DEFAULT_FBDEFIO_DELAY_MS);
293 fbdefio->deferred_io = drm_fb_helper_deferred_io;
294 fbi->fbdefio = fbdefio;
295 fb_deferred_io_init(fbi);
296 fbi->fbops->fb_mmap = drm_fbdev_cma_deferred_io_mmap;
301 static void drm_fbdev_cma_defio_fini(struct fb_info *fbi)
306 fb_deferred_io_cleanup(fbi);
312 drm_fbdev_cma_create(struct drm_fb_helper *helper,
313 struct drm_fb_helper_surface_size *sizes)
315 struct drm_fbdev_cma *fbdev_cma = to_fbdev_cma(helper);
316 struct drm_device *dev = helper->dev;
317 struct drm_gem_cma_object *obj;
318 struct drm_framebuffer *fb;
319 unsigned int bytes_per_pixel;
320 unsigned long offset;
325 DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n",
326 sizes->surface_width, sizes->surface_height,
329 bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
330 size = sizes->surface_width * sizes->surface_height * bytes_per_pixel;
331 obj = drm_gem_cma_create(dev, size);
335 fbi = drm_fb_helper_alloc_fbi(helper);
338 goto err_gem_free_object;
341 fb = drm_gem_fbdev_fb_create(dev, sizes, 0, &obj->base,
342 fbdev_cma->fb_funcs);
344 dev_err(dev->dev, "Failed to allocate DRM framebuffer.\n");
346 goto err_fb_info_destroy;
352 fbi->flags = FBINFO_FLAG_DEFAULT;
353 fbi->fbops = &drm_fbdev_cma_ops;
355 drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth);
356 drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
358 offset = fbi->var.xoffset * bytes_per_pixel;
359 offset += fbi->var.yoffset * fb->pitches[0];
361 dev->mode_config.fb_base = (resource_size_t)obj->paddr;
362 fbi->screen_base = obj->vaddr + offset;
363 fbi->fix.smem_start = (unsigned long)(obj->paddr + offset);
364 fbi->screen_size = size;
365 fbi->fix.smem_len = size;
367 if (fbdev_cma->fb_funcs->dirty) {
368 ret = drm_fbdev_cma_defio_init(fbi, obj);
370 goto err_cma_destroy;
376 drm_framebuffer_remove(fb);
378 drm_fb_helper_fini(helper);
380 drm_gem_object_put_unlocked(&obj->base);
384 static const struct drm_fb_helper_funcs drm_fb_cma_helper_funcs = {
385 .fb_probe = drm_fbdev_cma_create,
389 * drm_fbdev_cma_init_with_funcs() - Allocate and initializes a drm_fbdev_cma struct
391 * @preferred_bpp: Preferred bits per pixel for the device
392 * @max_conn_count: Maximum number of connectors
393 * @funcs: fb helper functions, in particular a custom dirty() callback
395 * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
397 struct drm_fbdev_cma *drm_fbdev_cma_init_with_funcs(struct drm_device *dev,
398 unsigned int preferred_bpp, unsigned int max_conn_count,
399 const struct drm_framebuffer_funcs *funcs)
401 struct drm_fbdev_cma *fbdev_cma;
402 struct drm_fb_helper *helper;
405 fbdev_cma = kzalloc(sizeof(*fbdev_cma), GFP_KERNEL);
407 dev_err(dev->dev, "Failed to allocate drm fbdev.\n");
408 return ERR_PTR(-ENOMEM);
410 fbdev_cma->fb_funcs = funcs;
412 helper = &fbdev_cma->fb_helper;
414 drm_fb_helper_prepare(dev, helper, &drm_fb_cma_helper_funcs);
416 ret = drm_fb_helper_init(dev, helper, max_conn_count);
418 dev_err(dev->dev, "Failed to initialize drm fb helper.\n");
422 ret = drm_fb_helper_single_add_all_connectors(helper);
424 dev_err(dev->dev, "Failed to add connectors.\n");
425 goto err_drm_fb_helper_fini;
429 ret = drm_fb_helper_initial_config(helper, preferred_bpp);
431 dev_err(dev->dev, "Failed to set initial hw configuration.\n");
432 goto err_drm_fb_helper_fini;
437 err_drm_fb_helper_fini:
438 drm_fb_helper_fini(helper);
444 EXPORT_SYMBOL_GPL(drm_fbdev_cma_init_with_funcs);
446 static const struct drm_framebuffer_funcs drm_fb_cma_funcs = {
447 .destroy = drm_gem_fb_destroy,
448 .create_handle = drm_gem_fb_create_handle,
452 * drm_fbdev_cma_init() - Allocate and initializes a drm_fbdev_cma struct
454 * @preferred_bpp: Preferred bits per pixel for the device
455 * @max_conn_count: Maximum number of connectors
457 * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
459 struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev,
460 unsigned int preferred_bpp, unsigned int max_conn_count)
462 return drm_fbdev_cma_init_with_funcs(dev, preferred_bpp,
466 EXPORT_SYMBOL_GPL(drm_fbdev_cma_init);
469 * drm_fbdev_cma_fini() - Free drm_fbdev_cma struct
470 * @fbdev_cma: The drm_fbdev_cma struct
472 void drm_fbdev_cma_fini(struct drm_fbdev_cma *fbdev_cma)
474 drm_fb_helper_unregister_fbi(&fbdev_cma->fb_helper);
475 if (fbdev_cma->fb_helper.fbdev)
476 drm_fbdev_cma_defio_fini(fbdev_cma->fb_helper.fbdev);
478 if (fbdev_cma->fb_helper.fb)
479 drm_framebuffer_remove(fbdev_cma->fb_helper.fb);
481 drm_fb_helper_fini(&fbdev_cma->fb_helper);
484 EXPORT_SYMBOL_GPL(drm_fbdev_cma_fini);
487 * drm_fbdev_cma_restore_mode() - Restores initial framebuffer mode
488 * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
490 * This function is usually called from the &drm_driver.lastclose callback.
492 void drm_fbdev_cma_restore_mode(struct drm_fbdev_cma *fbdev_cma)
495 drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev_cma->fb_helper);
497 EXPORT_SYMBOL_GPL(drm_fbdev_cma_restore_mode);
500 * drm_fbdev_cma_hotplug_event() - Poll for hotpulug events
501 * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
503 * This function is usually called from the &drm_mode_config.output_poll_changed
506 void drm_fbdev_cma_hotplug_event(struct drm_fbdev_cma *fbdev_cma)
509 drm_fb_helper_hotplug_event(&fbdev_cma->fb_helper);
511 EXPORT_SYMBOL_GPL(drm_fbdev_cma_hotplug_event);
514 * drm_fbdev_cma_set_suspend - wrapper around drm_fb_helper_set_suspend
515 * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
516 * @state: desired state, zero to resume, non-zero to suspend
518 * Calls drm_fb_helper_set_suspend, which is a wrapper around
519 * fb_set_suspend implemented by fbdev core.
521 void drm_fbdev_cma_set_suspend(struct drm_fbdev_cma *fbdev_cma, bool state)
524 drm_fb_helper_set_suspend(&fbdev_cma->fb_helper, state);
526 EXPORT_SYMBOL(drm_fbdev_cma_set_suspend);
529 * drm_fbdev_cma_set_suspend_unlocked - wrapper around
530 * drm_fb_helper_set_suspend_unlocked
531 * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
532 * @state: desired state, zero to resume, non-zero to suspend
534 * Calls drm_fb_helper_set_suspend, which is a wrapper around
535 * fb_set_suspend implemented by fbdev core.
537 void drm_fbdev_cma_set_suspend_unlocked(struct drm_fbdev_cma *fbdev_cma,
541 drm_fb_helper_set_suspend_unlocked(&fbdev_cma->fb_helper,
544 EXPORT_SYMBOL(drm_fbdev_cma_set_suspend_unlocked);