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_atomic.h>
22 #include <drm/drm_crtc.h>
23 #include <drm/drm_fb_helper.h>
24 #include <drm/drm_crtc_helper.h>
25 #include <drm/drm_gem_cma_helper.h>
26 #include <drm/drm_fb_cma_helper.h>
27 #include <linux/dma-buf.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/module.h>
30 #include <linux/reservation.h>
32 #define DEFAULT_FBDEFIO_DELAY_MS 50
35 struct drm_framebuffer fb
;
36 struct drm_gem_cma_object
*obj
[4];
39 struct drm_fbdev_cma
{
40 struct drm_fb_helper fb_helper
;
41 struct drm_fb_cma
*fb
;
45 * DOC: framebuffer cma helper functions
47 * Provides helper functions for creating a cma (contiguous memory allocator)
50 * drm_fb_cma_create() is used in the &drm_mode_config_funcs ->fb_create
51 * callback function to create a cma backed framebuffer.
53 * An fbdev framebuffer backed by cma is also available by calling
54 * drm_fbdev_cma_init(). drm_fbdev_cma_fini() tears it down.
55 * If the &drm_framebuffer_funcs ->dirty callback is set, fb_deferred_io
56 * will be set up automatically. dirty() is called by
57 * drm_fb_helper_deferred_io() in process context (struct delayed_work).
59 * Example fbdev deferred io code::
61 * static int driver_fbdev_fb_dirty(struct drm_framebuffer *fb,
62 * struct drm_file *file_priv,
63 * unsigned flags, unsigned color,
64 * struct drm_clip_rect *clips,
67 * struct drm_gem_cma_object *cma = drm_fb_cma_get_gem_obj(fb, 0);
68 * ... push changes ...
72 * static struct drm_framebuffer_funcs driver_fbdev_fb_funcs = {
73 * .destroy = drm_fb_cma_destroy,
74 * .create_handle = drm_fb_cma_create_handle,
75 * .dirty = driver_fbdev_fb_dirty,
78 * static int driver_fbdev_create(struct drm_fb_helper *helper,
79 * struct drm_fb_helper_surface_size *sizes)
81 * return drm_fbdev_cma_create_with_funcs(helper, sizes,
82 * &driver_fbdev_fb_funcs);
85 * static const struct drm_fb_helper_funcs driver_fb_helper_funcs = {
86 * .fb_probe = driver_fbdev_create,
90 * fbdev = drm_fbdev_cma_init_with_funcs(dev, 16,
91 * dev->mode_config.num_crtc,
92 * dev->mode_config.num_connector,
93 * &driver_fb_helper_funcs);
97 static inline struct drm_fbdev_cma
*to_fbdev_cma(struct drm_fb_helper
*helper
)
99 return container_of(helper
, struct drm_fbdev_cma
, fb_helper
);
102 static inline struct drm_fb_cma
*to_fb_cma(struct drm_framebuffer
*fb
)
104 return container_of(fb
, struct drm_fb_cma
, fb
);
107 void drm_fb_cma_destroy(struct drm_framebuffer
*fb
)
109 struct drm_fb_cma
*fb_cma
= to_fb_cma(fb
);
112 for (i
= 0; i
< 4; i
++) {
114 drm_gem_object_unreference_unlocked(&fb_cma
->obj
[i
]->base
);
117 drm_framebuffer_cleanup(fb
);
120 EXPORT_SYMBOL(drm_fb_cma_destroy
);
122 int drm_fb_cma_create_handle(struct drm_framebuffer
*fb
,
123 struct drm_file
*file_priv
, unsigned int *handle
)
125 struct drm_fb_cma
*fb_cma
= to_fb_cma(fb
);
127 return drm_gem_handle_create(file_priv
,
128 &fb_cma
->obj
[0]->base
, handle
);
130 EXPORT_SYMBOL(drm_fb_cma_create_handle
);
132 static struct drm_framebuffer_funcs drm_fb_cma_funcs
= {
133 .destroy
= drm_fb_cma_destroy
,
134 .create_handle
= drm_fb_cma_create_handle
,
137 static struct drm_fb_cma
*drm_fb_cma_alloc(struct drm_device
*dev
,
138 const struct drm_mode_fb_cmd2
*mode_cmd
,
139 struct drm_gem_cma_object
**obj
,
140 unsigned int num_planes
, const struct drm_framebuffer_funcs
*funcs
)
142 struct drm_fb_cma
*fb_cma
;
146 fb_cma
= kzalloc(sizeof(*fb_cma
), GFP_KERNEL
);
148 return ERR_PTR(-ENOMEM
);
150 drm_helper_mode_fill_fb_struct(&fb_cma
->fb
, mode_cmd
);
152 for (i
= 0; i
< num_planes
; i
++)
153 fb_cma
->obj
[i
] = obj
[i
];
155 ret
= drm_framebuffer_init(dev
, &fb_cma
->fb
, funcs
);
157 dev_err(dev
->dev
, "Failed to initialize framebuffer: %d\n", ret
);
166 * drm_fb_cma_create_with_funcs() - helper function for the
167 * &drm_mode_config_funcs ->fb_create
170 * @file_priv: drm file for the ioctl call
171 * @mode_cmd: metadata from the userspace fb creation request
172 * @funcs: vtable to be used for the new framebuffer object
174 * This can be used to set &drm_framebuffer_funcs for drivers that need the
175 * dirty() callback. Use drm_fb_cma_create() if you don't need to change
176 * &drm_framebuffer_funcs.
178 struct drm_framebuffer
*drm_fb_cma_create_with_funcs(struct drm_device
*dev
,
179 struct drm_file
*file_priv
, const struct drm_mode_fb_cmd2
*mode_cmd
,
180 const struct drm_framebuffer_funcs
*funcs
)
182 const struct drm_format_info
*info
;
183 struct drm_fb_cma
*fb_cma
;
184 struct drm_gem_cma_object
*objs
[4];
185 struct drm_gem_object
*obj
;
189 info
= drm_format_info(mode_cmd
->pixel_format
);
191 return ERR_PTR(-EINVAL
);
193 for (i
= 0; i
< info
->num_planes
; i
++) {
194 unsigned int width
= mode_cmd
->width
/ (i
? info
->hsub
: 1);
195 unsigned int height
= mode_cmd
->height
/ (i
? info
->vsub
: 1);
196 unsigned int min_size
;
198 obj
= drm_gem_object_lookup(file_priv
, mode_cmd
->handles
[i
]);
200 dev_err(dev
->dev
, "Failed to lookup GEM object\n");
202 goto err_gem_object_unreference
;
205 min_size
= (height
- 1) * mode_cmd
->pitches
[i
]
206 + width
* info
->cpp
[i
]
207 + mode_cmd
->offsets
[i
];
209 if (obj
->size
< min_size
) {
210 drm_gem_object_unreference_unlocked(obj
);
212 goto err_gem_object_unreference
;
214 objs
[i
] = to_drm_gem_cma_obj(obj
);
217 fb_cma
= drm_fb_cma_alloc(dev
, mode_cmd
, objs
, i
, funcs
);
218 if (IS_ERR(fb_cma
)) {
219 ret
= PTR_ERR(fb_cma
);
220 goto err_gem_object_unreference
;
225 err_gem_object_unreference
:
226 for (i
--; i
>= 0; i
--)
227 drm_gem_object_unreference_unlocked(&objs
[i
]->base
);
230 EXPORT_SYMBOL_GPL(drm_fb_cma_create_with_funcs
);
233 * drm_fb_cma_create() - &drm_mode_config_funcs ->fb_create callback function
235 * @file_priv: drm file for the ioctl call
236 * @mode_cmd: metadata from the userspace fb creation request
238 * If your hardware has special alignment or pitch requirements these should be
239 * checked before calling this function. Use drm_fb_cma_create_with_funcs() if
240 * you need to set &drm_framebuffer_funcs ->dirty.
242 struct drm_framebuffer
*drm_fb_cma_create(struct drm_device
*dev
,
243 struct drm_file
*file_priv
, const struct drm_mode_fb_cmd2
*mode_cmd
)
245 return drm_fb_cma_create_with_funcs(dev
, file_priv
, mode_cmd
,
248 EXPORT_SYMBOL_GPL(drm_fb_cma_create
);
251 * drm_fb_cma_get_gem_obj() - Get CMA GEM object for framebuffer
252 * @fb: The framebuffer
253 * @plane: Which plane
255 * Return the CMA GEM object for given framebuffer.
257 * This function will usually be called from the CRTC callback functions.
259 struct drm_gem_cma_object
*drm_fb_cma_get_gem_obj(struct drm_framebuffer
*fb
,
262 struct drm_fb_cma
*fb_cma
= to_fb_cma(fb
);
267 return fb_cma
->obj
[plane
];
269 EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_obj
);
272 * drm_fb_cma_prepare_fb() - Prepare CMA framebuffer
273 * @plane: Which plane
274 * @state: Plane state attach fence to
276 * This should be put into prepare_fb hook of struct &drm_plane_helper_funcs .
278 * This function checks if the plane FB has an dma-buf attached, extracts
279 * the exclusive fence and attaches it to plane state for the atomic helper
282 * There is no need for cleanup_fb for CMA based framebuffer drivers.
284 int drm_fb_cma_prepare_fb(struct drm_plane
*plane
,
285 struct drm_plane_state
*state
)
287 struct dma_buf
*dma_buf
;
288 struct dma_fence
*fence
;
290 if ((plane
->state
->fb
== state
->fb
) || !state
->fb
)
293 dma_buf
= drm_fb_cma_get_gem_obj(state
->fb
, 0)->base
.dma_buf
;
295 fence
= reservation_object_get_excl_rcu(dma_buf
->resv
);
296 drm_atomic_set_fence_for_plane(state
, fence
);
301 EXPORT_SYMBOL_GPL(drm_fb_cma_prepare_fb
);
303 #ifdef CONFIG_DEBUG_FS
304 static void drm_fb_cma_describe(struct drm_framebuffer
*fb
, struct seq_file
*m
)
306 struct drm_fb_cma
*fb_cma
= to_fb_cma(fb
);
307 const struct drm_format_info
*info
;
310 seq_printf(m
, "fb: %dx%d@%4.4s\n", fb
->width
, fb
->height
,
311 (char *)&fb
->pixel_format
);
313 info
= drm_format_info(fb
->pixel_format
);
315 for (i
= 0; i
< info
->num_planes
; i
++) {
316 seq_printf(m
, " %d: offset=%d pitch=%d, obj: ",
317 i
, fb
->offsets
[i
], fb
->pitches
[i
]);
318 drm_gem_cma_describe(fb_cma
->obj
[i
], m
);
323 * drm_fb_cma_debugfs_show() - Helper to list CMA framebuffer objects
326 * @arg: private data for the callback
328 int drm_fb_cma_debugfs_show(struct seq_file
*m
, void *arg
)
330 struct drm_info_node
*node
= (struct drm_info_node
*) m
->private;
331 struct drm_device
*dev
= node
->minor
->dev
;
332 struct drm_framebuffer
*fb
;
334 mutex_lock(&dev
->mode_config
.fb_lock
);
335 drm_for_each_fb(fb
, dev
)
336 drm_fb_cma_describe(fb
, m
);
337 mutex_unlock(&dev
->mode_config
.fb_lock
);
341 EXPORT_SYMBOL_GPL(drm_fb_cma_debugfs_show
);
344 static int drm_fb_cma_mmap(struct fb_info
*info
, struct vm_area_struct
*vma
)
346 return dma_mmap_writecombine(info
->device
, vma
, info
->screen_base
,
347 info
->fix
.smem_start
, info
->fix
.smem_len
);
350 static struct fb_ops drm_fbdev_cma_ops
= {
351 .owner
= THIS_MODULE
,
352 DRM_FB_HELPER_DEFAULT_OPS
,
353 .fb_fillrect
= drm_fb_helper_sys_fillrect
,
354 .fb_copyarea
= drm_fb_helper_sys_copyarea
,
355 .fb_imageblit
= drm_fb_helper_sys_imageblit
,
356 .fb_mmap
= drm_fb_cma_mmap
,
359 static int drm_fbdev_cma_deferred_io_mmap(struct fb_info
*info
,
360 struct vm_area_struct
*vma
)
362 fb_deferred_io_mmap(info
, vma
);
363 vma
->vm_page_prot
= pgprot_writecombine(vma
->vm_page_prot
);
368 static int drm_fbdev_cma_defio_init(struct fb_info
*fbi
,
369 struct drm_gem_cma_object
*cma_obj
)
371 struct fb_deferred_io
*fbdefio
;
372 struct fb_ops
*fbops
;
375 * Per device structures are needed because:
376 * fbops: fb_deferred_io_cleanup() clears fbops.fb_mmap
377 * fbdefio: individual delays
379 fbdefio
= kzalloc(sizeof(*fbdefio
), GFP_KERNEL
);
380 fbops
= kzalloc(sizeof(*fbops
), GFP_KERNEL
);
381 if (!fbdefio
|| !fbops
) {
387 /* can't be offset from vaddr since dirty() uses cma_obj */
388 fbi
->screen_buffer
= cma_obj
->vaddr
;
389 /* fb_deferred_io_fault() needs a physical address */
390 fbi
->fix
.smem_start
= page_to_phys(virt_to_page(fbi
->screen_buffer
));
392 *fbops
= *fbi
->fbops
;
395 fbdefio
->delay
= msecs_to_jiffies(DEFAULT_FBDEFIO_DELAY_MS
);
396 fbdefio
->deferred_io
= drm_fb_helper_deferred_io
;
397 fbi
->fbdefio
= fbdefio
;
398 fb_deferred_io_init(fbi
);
399 fbi
->fbops
->fb_mmap
= drm_fbdev_cma_deferred_io_mmap
;
404 static void drm_fbdev_cma_defio_fini(struct fb_info
*fbi
)
409 fb_deferred_io_cleanup(fbi
);
415 * For use in a (struct drm_fb_helper_funcs *)->fb_probe callback function that
416 * needs custom struct drm_framebuffer_funcs, like dirty() for deferred_io use.
418 int drm_fbdev_cma_create_with_funcs(struct drm_fb_helper
*helper
,
419 struct drm_fb_helper_surface_size
*sizes
,
420 const struct drm_framebuffer_funcs
*funcs
)
422 struct drm_fbdev_cma
*fbdev_cma
= to_fbdev_cma(helper
);
423 struct drm_mode_fb_cmd2 mode_cmd
= { 0 };
424 struct drm_device
*dev
= helper
->dev
;
425 struct drm_gem_cma_object
*obj
;
426 struct drm_framebuffer
*fb
;
427 unsigned int bytes_per_pixel
;
428 unsigned long offset
;
433 DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n",
434 sizes
->surface_width
, sizes
->surface_height
,
437 bytes_per_pixel
= DIV_ROUND_UP(sizes
->surface_bpp
, 8);
439 mode_cmd
.width
= sizes
->surface_width
;
440 mode_cmd
.height
= sizes
->surface_height
;
441 mode_cmd
.pitches
[0] = sizes
->surface_width
* bytes_per_pixel
;
442 mode_cmd
.pixel_format
= drm_mode_legacy_fb_format(sizes
->surface_bpp
,
443 sizes
->surface_depth
);
445 size
= mode_cmd
.pitches
[0] * mode_cmd
.height
;
446 obj
= drm_gem_cma_create(dev
, size
);
450 fbi
= drm_fb_helper_alloc_fbi(helper
);
453 goto err_gem_free_object
;
456 fbdev_cma
->fb
= drm_fb_cma_alloc(dev
, &mode_cmd
, &obj
, 1, funcs
);
457 if (IS_ERR(fbdev_cma
->fb
)) {
458 dev_err(dev
->dev
, "Failed to allocate DRM framebuffer.\n");
459 ret
= PTR_ERR(fbdev_cma
->fb
);
460 goto err_fb_info_destroy
;
463 fb
= &fbdev_cma
->fb
->fb
;
467 fbi
->flags
= FBINFO_FLAG_DEFAULT
;
468 fbi
->fbops
= &drm_fbdev_cma_ops
;
470 drm_fb_helper_fill_fix(fbi
, fb
->pitches
[0], fb
->depth
);
471 drm_fb_helper_fill_var(fbi
, helper
, sizes
->fb_width
, sizes
->fb_height
);
473 offset
= fbi
->var
.xoffset
* bytes_per_pixel
;
474 offset
+= fbi
->var
.yoffset
* fb
->pitches
[0];
476 dev
->mode_config
.fb_base
= (resource_size_t
)obj
->paddr
;
477 fbi
->screen_base
= obj
->vaddr
+ offset
;
478 fbi
->fix
.smem_start
= (unsigned long)(obj
->paddr
+ offset
);
479 fbi
->screen_size
= size
;
480 fbi
->fix
.smem_len
= size
;
483 ret
= drm_fbdev_cma_defio_init(fbi
, obj
);
485 goto err_cma_destroy
;
491 drm_framebuffer_unregister_private(&fbdev_cma
->fb
->fb
);
492 drm_fb_cma_destroy(&fbdev_cma
->fb
->fb
);
494 drm_fb_helper_release_fbi(helper
);
496 drm_gem_object_unreference_unlocked(&obj
->base
);
499 EXPORT_SYMBOL(drm_fbdev_cma_create_with_funcs
);
501 static int drm_fbdev_cma_create(struct drm_fb_helper
*helper
,
502 struct drm_fb_helper_surface_size
*sizes
)
504 return drm_fbdev_cma_create_with_funcs(helper
, sizes
, &drm_fb_cma_funcs
);
507 static const struct drm_fb_helper_funcs drm_fb_cma_helper_funcs
= {
508 .fb_probe
= drm_fbdev_cma_create
,
512 * drm_fbdev_cma_init_with_funcs() - Allocate and initializes a drm_fbdev_cma struct
514 * @preferred_bpp: Preferred bits per pixel for the device
515 * @num_crtc: Number of CRTCs
516 * @max_conn_count: Maximum number of connectors
517 * @funcs: fb helper functions, in particular fb_probe()
519 * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
521 struct drm_fbdev_cma
*drm_fbdev_cma_init_with_funcs(struct drm_device
*dev
,
522 unsigned int preferred_bpp
, unsigned int num_crtc
,
523 unsigned int max_conn_count
, const struct drm_fb_helper_funcs
*funcs
)
525 struct drm_fbdev_cma
*fbdev_cma
;
526 struct drm_fb_helper
*helper
;
529 fbdev_cma
= kzalloc(sizeof(*fbdev_cma
), GFP_KERNEL
);
531 dev_err(dev
->dev
, "Failed to allocate drm fbdev.\n");
532 return ERR_PTR(-ENOMEM
);
535 helper
= &fbdev_cma
->fb_helper
;
537 drm_fb_helper_prepare(dev
, helper
, funcs
);
539 ret
= drm_fb_helper_init(dev
, helper
, num_crtc
, max_conn_count
);
541 dev_err(dev
->dev
, "Failed to initialize drm fb helper.\n");
545 ret
= drm_fb_helper_single_add_all_connectors(helper
);
547 dev_err(dev
->dev
, "Failed to add connectors.\n");
548 goto err_drm_fb_helper_fini
;
552 ret
= drm_fb_helper_initial_config(helper
, preferred_bpp
);
554 dev_err(dev
->dev
, "Failed to set initial hw configuration.\n");
555 goto err_drm_fb_helper_fini
;
560 err_drm_fb_helper_fini
:
561 drm_fb_helper_fini(helper
);
567 EXPORT_SYMBOL_GPL(drm_fbdev_cma_init_with_funcs
);
570 * drm_fbdev_cma_init() - Allocate and initializes a drm_fbdev_cma struct
572 * @preferred_bpp: Preferred bits per pixel for the device
573 * @num_crtc: Number of CRTCs
574 * @max_conn_count: Maximum number of connectors
576 * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
578 struct drm_fbdev_cma
*drm_fbdev_cma_init(struct drm_device
*dev
,
579 unsigned int preferred_bpp
, unsigned int num_crtc
,
580 unsigned int max_conn_count
)
582 return drm_fbdev_cma_init_with_funcs(dev
, preferred_bpp
, num_crtc
,
583 max_conn_count
, &drm_fb_cma_helper_funcs
);
585 EXPORT_SYMBOL_GPL(drm_fbdev_cma_init
);
588 * drm_fbdev_cma_fini() - Free drm_fbdev_cma struct
589 * @fbdev_cma: The drm_fbdev_cma struct
591 void drm_fbdev_cma_fini(struct drm_fbdev_cma
*fbdev_cma
)
593 drm_fb_helper_unregister_fbi(&fbdev_cma
->fb_helper
);
594 if (fbdev_cma
->fb_helper
.fbdev
)
595 drm_fbdev_cma_defio_fini(fbdev_cma
->fb_helper
.fbdev
);
596 drm_fb_helper_release_fbi(&fbdev_cma
->fb_helper
);
599 drm_framebuffer_unregister_private(&fbdev_cma
->fb
->fb
);
600 drm_fb_cma_destroy(&fbdev_cma
->fb
->fb
);
603 drm_fb_helper_fini(&fbdev_cma
->fb_helper
);
606 EXPORT_SYMBOL_GPL(drm_fbdev_cma_fini
);
609 * drm_fbdev_cma_restore_mode() - Restores initial framebuffer mode
610 * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
612 * This function is usually called from the DRM drivers lastclose callback.
614 void drm_fbdev_cma_restore_mode(struct drm_fbdev_cma
*fbdev_cma
)
617 drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev_cma
->fb_helper
);
619 EXPORT_SYMBOL_GPL(drm_fbdev_cma_restore_mode
);
622 * drm_fbdev_cma_hotplug_event() - Poll for hotpulug events
623 * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
625 * This function is usually called from the DRM drivers output_poll_changed
628 void drm_fbdev_cma_hotplug_event(struct drm_fbdev_cma
*fbdev_cma
)
631 drm_fb_helper_hotplug_event(&fbdev_cma
->fb_helper
);
633 EXPORT_SYMBOL_GPL(drm_fbdev_cma_hotplug_event
);
636 * drm_fbdev_cma_set_suspend - wrapper around drm_fb_helper_set_suspend
637 * @fbdev_cma: The drm_fbdev_cma struct, may be NULL
638 * @state: desired state, zero to resume, non-zero to suspend
640 * Calls drm_fb_helper_set_suspend, which is a wrapper around
641 * fb_set_suspend implemented by fbdev core.
643 void drm_fbdev_cma_set_suspend(struct drm_fbdev_cma
*fbdev_cma
, int state
)
646 drm_fb_helper_set_suspend(&fbdev_cma
->fb_helper
, state
);
648 EXPORT_SYMBOL(drm_fbdev_cma_set_suspend
);