blob: 947ac95eb24a98dffb436ebffba9f2de2dfab7d4 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Sascha Hauerb9d47452012-06-27 15:30:18 +02002#ifndef __DRM_GEM_CMA_HELPER_H__
3#define __DRM_GEM_CMA_HELPER_H__
4
Sam Ravnborg785caba2019-01-08 20:29:39 +01005#include <drm/drm_file.h>
6#include <drm/drm_ioctl.h>
Daniel Vetterd9fc9412014-09-23 15:46:53 +02007#include <drm/drm_gem.h>
Shawn Guo740c22a2014-03-24 20:14:48 +08008
Sam Ravnborg785caba2019-01-08 20:29:39 +01009struct drm_mode_create_dumb;
10
Thierry Redingd7883f82014-11-03 13:56:55 +010011/**
12 * struct drm_gem_cma_object - GEM object backed by CMA memory allocations
13 * @base: base GEM object
14 * @paddr: physical address of the backing memory
Liviu Dudau998fb1a2017-11-10 13:33:10 +000015 * @sgt: scatter/gather table for imported PRIME buffers. The table can have
16 * more than one entry but they are guaranteed to have contiguous
17 * DMA addresses.
Thierry Redingd7883f82014-11-03 13:56:55 +010018 * @vaddr: kernel virtual address of the backing memory
19 */
Sascha Hauerb9d47452012-06-27 15:30:18 +020020struct drm_gem_cma_object {
21 struct drm_gem_object base;
22 dma_addr_t paddr;
Laurent Pinchart71d72822013-02-17 01:57:30 +010023 struct sg_table *sgt;
24
25 /* For objects with DMA memory allocated by GEM CMA */
Sascha Hauerb9d47452012-06-27 15:30:18 +020026 void *vaddr;
27};
28
Noralf Trønnes8d25cce2017-11-07 20:13:42 +010029#define to_drm_gem_cma_obj(gem_obj) \
30 container_of(gem_obj, struct drm_gem_cma_object, base)
Sascha Hauerb9d47452012-06-27 15:30:18 +020031
Eric Anholt75cccac2017-04-17 16:31:24 -070032#ifndef CONFIG_MMU
33#define DRM_GEM_CMA_UNMAPPED_AREA_FOPS \
34 .get_unmapped_area = drm_gem_cma_get_unmapped_area,
35#else
36#define DRM_GEM_CMA_UNMAPPED_AREA_FOPS
37#endif
38
Daniel Vetterd55f7e52017-03-08 15:12:56 +010039/**
40 * DEFINE_DRM_GEM_CMA_FOPS() - macro to generate file operations for CMA drivers
41 * @name: name for the generated structure
42 *
43 * This macro autogenerates a suitable &struct file_operations for CMA based
44 * drivers, which can be assigned to &drm_driver.fops. Note that this structure
45 * cannot be shared between drivers, because it contains a reference to the
46 * current module using THIS_MODULE.
47 *
48 * Note that the declaration is already marked as static - if you need a
49 * non-static version of this you're probably doing it wrong and will break the
50 * THIS_MODULE reference by accident.
51 */
52#define DEFINE_DRM_GEM_CMA_FOPS(name) \
53 static const struct file_operations name = {\
54 .owner = THIS_MODULE,\
55 .open = drm_open,\
56 .release = drm_release,\
57 .unlocked_ioctl = drm_ioctl,\
58 .compat_ioctl = drm_compat_ioctl,\
59 .poll = drm_poll,\
60 .read = drm_read,\
61 .llseek = noop_llseek,\
62 .mmap = drm_gem_cma_mmap,\
Eric Anholt75cccac2017-04-17 16:31:24 -070063 DRM_GEM_CMA_UNMAPPED_AREA_FOPS \
Daniel Vetterd55f7e52017-03-08 15:12:56 +010064 }
65
Thierry Redingd7883f82014-11-03 13:56:55 +010066/* free GEM object */
Sascha Hauerb9d47452012-06-27 15:30:18 +020067void drm_gem_cma_free_object(struct drm_gem_object *gem_obj);
68
Thierry Redingd7883f82014-11-03 13:56:55 +010069/* create memory region for DRM framebuffer */
Thierry Reding6d178292014-11-03 11:48:49 +010070int drm_gem_cma_dumb_create_internal(struct drm_file *file_priv,
71 struct drm_device *drm,
72 struct drm_mode_create_dumb *args);
73
74/* create memory region for DRM framebuffer */
Sascha Hauerb9d47452012-06-27 15:30:18 +020075int drm_gem_cma_dumb_create(struct drm_file *file_priv,
Thierry Redingd7883f82014-11-03 13:56:55 +010076 struct drm_device *drm,
77 struct drm_mode_create_dumb *args);
Sascha Hauerb9d47452012-06-27 15:30:18 +020078
Thierry Redingd7883f82014-11-03 13:56:55 +010079/* set vm_flags and we can change the VM attribute to other one at here */
Sascha Hauerb9d47452012-06-27 15:30:18 +020080int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma);
81
Thierry Redingd7883f82014-11-03 13:56:55 +010082/* allocate physical memory */
Sascha Hauerb9d47452012-06-27 15:30:18 +020083struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm,
Thierry Redingd7883f82014-11-03 13:56:55 +010084 size_t size);
Sascha Hauerb9d47452012-06-27 15:30:18 +020085
86extern const struct vm_operations_struct drm_gem_cma_vm_ops;
87
Benjamin Gaignard62a0d982017-01-04 10:12:57 +010088#ifndef CONFIG_MMU
89unsigned long drm_gem_cma_get_unmapped_area(struct file *filp,
90 unsigned long addr,
91 unsigned long len,
92 unsigned long pgoff,
93 unsigned long flags);
Benjamin Gaignard62a0d982017-01-04 10:12:57 +010094#endif
95
Noralf Trønnesd6892012017-11-07 20:13:43 +010096void drm_gem_cma_print_info(struct drm_printer *p, unsigned int indent,
97 const struct drm_gem_object *obj);
Rob Clark6f646092012-12-10 10:46:43 -060098
Joonyoung Shim78467dc2013-06-28 14:24:54 +090099struct sg_table *drm_gem_cma_prime_get_sg_table(struct drm_gem_object *obj);
100struct drm_gem_object *
Maarten Lankhorstb5e9c1a2014-01-09 11:03:14 +0100101drm_gem_cma_prime_import_sg_table(struct drm_device *dev,
102 struct dma_buf_attachment *attach,
Joonyoung Shim78467dc2013-06-28 14:24:54 +0900103 struct sg_table *sgt);
104int drm_gem_cma_prime_mmap(struct drm_gem_object *obj,
105 struct vm_area_struct *vma);
106void *drm_gem_cma_prime_vmap(struct drm_gem_object *obj);
107void drm_gem_cma_prime_vunmap(struct drm_gem_object *obj, void *vaddr);
108
Noralf Trønnesb9068cd2018-11-10 15:56:46 +0100109struct drm_gem_object *
110drm_cma_gem_create_object_default_funcs(struct drm_device *dev, size_t size);
111
112/**
113 * DRM_GEM_CMA_VMAP_DRIVER_OPS - CMA GEM driver operations ensuring a virtual
114 * address on the buffer
115 *
116 * This macro provides a shortcut for setting the default GEM operations in the
117 * &drm_driver structure for drivers that need the virtual address also on
118 * imported buffers.
119 */
120#define DRM_GEM_CMA_VMAP_DRIVER_OPS \
121 .gem_create_object = drm_cma_gem_create_object_default_funcs, \
122 .dumb_create = drm_gem_cma_dumb_create, \
123 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, \
124 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, \
125 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table_vmap, \
126 .gem_prime_mmap = drm_gem_prime_mmap
127
128struct drm_gem_object *
129drm_gem_cma_prime_import_sg_table_vmap(struct drm_device *drm,
130 struct dma_buf_attachment *attach,
131 struct sg_table *sgt);
132
Sascha Hauerb9d47452012-06-27 15:30:18 +0200133#endif /* __DRM_GEM_CMA_HELPER_H__ */