Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 2 | #ifndef __DRM_GEM_CMA_HELPER_H__ |
| 3 | #define __DRM_GEM_CMA_HELPER_H__ |
| 4 | |
Sam Ravnborg | 785caba | 2019-01-08 20:29:39 +0100 | [diff] [blame] | 5 | #include <drm/drm_file.h> |
| 6 | #include <drm/drm_ioctl.h> |
Daniel Vetter | d9fc941 | 2014-09-23 15:46:53 +0200 | [diff] [blame] | 7 | #include <drm/drm_gem.h> |
Shawn Guo | 740c22a | 2014-03-24 20:14:48 +0800 | [diff] [blame] | 8 | |
Sam Ravnborg | 785caba | 2019-01-08 20:29:39 +0100 | [diff] [blame] | 9 | struct drm_mode_create_dumb; |
| 10 | |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 11 | /** |
| 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 Dudau | 998fb1a | 2017-11-10 13:33:10 +0000 | [diff] [blame] | 15 | * @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 Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 18 | * @vaddr: kernel virtual address of the backing memory |
| 19 | */ |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 20 | struct drm_gem_cma_object { |
| 21 | struct drm_gem_object base; |
| 22 | dma_addr_t paddr; |
Laurent Pinchart | 71d7282 | 2013-02-17 01:57:30 +0100 | [diff] [blame] | 23 | struct sg_table *sgt; |
| 24 | |
| 25 | /* For objects with DMA memory allocated by GEM CMA */ |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 26 | void *vaddr; |
| 27 | }; |
| 28 | |
Noralf Trønnes | 8d25cce | 2017-11-07 20:13:42 +0100 | [diff] [blame] | 29 | #define to_drm_gem_cma_obj(gem_obj) \ |
| 30 | container_of(gem_obj, struct drm_gem_cma_object, base) |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 31 | |
Eric Anholt | 75cccac | 2017-04-17 16:31:24 -0700 | [diff] [blame] | 32 | #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 Vetter | d55f7e5 | 2017-03-08 15:12:56 +0100 | [diff] [blame] | 39 | /** |
| 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,\ |
Thomas Zimmermann | f5ca8eb | 2020-11-23 12:56:46 +0100 | [diff] [blame] | 62 | .mmap = drm_gem_mmap,\ |
Eric Anholt | 75cccac | 2017-04-17 16:31:24 -0700 | [diff] [blame] | 63 | DRM_GEM_CMA_UNMAPPED_AREA_FOPS \ |
Daniel Vetter | d55f7e5 | 2017-03-08 15:12:56 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 66 | /* free GEM object */ |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 67 | void drm_gem_cma_free_object(struct drm_gem_object *gem_obj); |
| 68 | |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 69 | /* create memory region for DRM framebuffer */ |
Thierry Reding | 6d17829 | 2014-11-03 11:48:49 +0100 | [diff] [blame] | 70 | int 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 Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 75 | int drm_gem_cma_dumb_create(struct drm_file *file_priv, |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 76 | struct drm_device *drm, |
| 77 | struct drm_mode_create_dumb *args); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 78 | |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 79 | /* allocate physical memory */ |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 80 | struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm, |
Thierry Reding | d7883f8 | 2014-11-03 13:56:55 +0100 | [diff] [blame] | 81 | size_t size); |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 82 | |
| 83 | extern const struct vm_operations_struct drm_gem_cma_vm_ops; |
| 84 | |
Benjamin Gaignard | 62a0d98 | 2017-01-04 10:12:57 +0100 | [diff] [blame] | 85 | #ifndef CONFIG_MMU |
| 86 | unsigned long drm_gem_cma_get_unmapped_area(struct file *filp, |
| 87 | unsigned long addr, |
| 88 | unsigned long len, |
| 89 | unsigned long pgoff, |
| 90 | unsigned long flags); |
Benjamin Gaignard | 62a0d98 | 2017-01-04 10:12:57 +0100 | [diff] [blame] | 91 | #endif |
| 92 | |
Noralf Trønnes | d689201 | 2017-11-07 20:13:43 +0100 | [diff] [blame] | 93 | void drm_gem_cma_print_info(struct drm_printer *p, unsigned int indent, |
| 94 | const struct drm_gem_object *obj); |
Rob Clark | 6f64609 | 2012-12-10 10:46:43 -0600 | [diff] [blame] | 95 | |
Thomas Zimmermann | bc2532a | 2020-11-23 12:56:45 +0100 | [diff] [blame] | 96 | struct sg_table *drm_gem_cma_get_sg_table(struct drm_gem_object *obj); |
Joonyoung Shim | 78467dc | 2013-06-28 14:24:54 +0900 | [diff] [blame] | 97 | struct drm_gem_object * |
Maarten Lankhorst | b5e9c1a | 2014-01-09 11:03:14 +0100 | [diff] [blame] | 98 | drm_gem_cma_prime_import_sg_table(struct drm_device *dev, |
| 99 | struct dma_buf_attachment *attach, |
Joonyoung Shim | 78467dc | 2013-06-28 14:24:54 +0900 | [diff] [blame] | 100 | struct sg_table *sgt); |
Thomas Zimmermann | bc2532a | 2020-11-23 12:56:45 +0100 | [diff] [blame] | 101 | int drm_gem_cma_vmap(struct drm_gem_object *obj, struct dma_buf_map *map); |
Thomas Zimmermann | f5ca8eb | 2020-11-23 12:56:46 +0100 | [diff] [blame] | 102 | int drm_gem_cma_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma); |
Joonyoung Shim | 78467dc | 2013-06-28 14:24:54 +0900 | [diff] [blame] | 103 | |
Noralf Trønnes | b9068cd | 2018-11-10 15:56:46 +0100 | [diff] [blame] | 104 | /** |
Thomas Zimmermann | 654bf12 | 2020-06-05 09:32:07 +0200 | [diff] [blame] | 105 | * DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE - CMA GEM driver operations |
| 106 | * @dumb_create_func: callback function for .dumb_create |
| 107 | * |
| 108 | * This macro provides a shortcut for setting the default GEM operations in the |
| 109 | * &drm_driver structure. |
| 110 | * |
| 111 | * This macro is a variant of DRM_GEM_CMA_DRIVER_OPS for drivers that |
| 112 | * override the default implementation of &struct rm_driver.dumb_create. Use |
| 113 | * DRM_GEM_CMA_DRIVER_OPS if possible. Drivers that require a virtual address |
| 114 | * on imported buffers should use |
| 115 | * DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE() instead. |
| 116 | */ |
| 117 | #define DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(dumb_create_func) \ |
Thomas Zimmermann | 654bf12 | 2020-06-05 09:32:07 +0200 | [diff] [blame] | 118 | .dumb_create = (dumb_create_func), \ |
| 119 | .prime_handle_to_fd = drm_gem_prime_handle_to_fd, \ |
| 120 | .prime_fd_to_handle = drm_gem_prime_fd_to_handle, \ |
| 121 | .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, \ |
Thomas Zimmermann | f5ca8eb | 2020-11-23 12:56:46 +0100 | [diff] [blame] | 122 | .gem_prime_mmap = drm_gem_prime_mmap |
Thomas Zimmermann | 654bf12 | 2020-06-05 09:32:07 +0200 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * DRM_GEM_CMA_DRIVER_OPS - CMA GEM driver operations |
| 126 | * |
| 127 | * This macro provides a shortcut for setting the default GEM operations in the |
| 128 | * &drm_driver structure. |
| 129 | * |
| 130 | * Drivers that come with their own implementation of |
| 131 | * &struct drm_driver.dumb_create should use |
| 132 | * DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE() instead. Use |
| 133 | * DRM_GEM_CMA_DRIVER_OPS if possible. Drivers that require a virtual address |
| 134 | * on imported buffers should use DRM_GEM_CMA_DRIVER_OPS_VMAP instead. |
| 135 | */ |
| 136 | #define DRM_GEM_CMA_DRIVER_OPS \ |
| 137 | DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(drm_gem_cma_dumb_create) |
| 138 | |
| 139 | /** |
Thomas Zimmermann | 06d6620 | 2020-06-05 09:32:06 +0200 | [diff] [blame] | 140 | * DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE - CMA GEM driver operations |
| 141 | * ensuring a virtual address |
| 142 | * on the buffer |
| 143 | * @dumb_create_func: callback function for .dumb_create |
| 144 | * |
| 145 | * This macro provides a shortcut for setting the default GEM operations in the |
| 146 | * &drm_driver structure for drivers that need the virtual address also on |
| 147 | * imported buffers. |
| 148 | * |
| 149 | * This macro is a variant of DRM_GEM_CMA_DRIVER_OPS_VMAP for drivers that |
Thomas Zimmermann | 654bf12 | 2020-06-05 09:32:07 +0200 | [diff] [blame] | 150 | * override the default implementation of &struct drm_driver.dumb_create. Use |
| 151 | * DRM_GEM_CMA_DRIVER_OPS_VMAP if possible. Drivers that do not require a |
| 152 | * virtual address on imported buffers should use |
| 153 | * DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE() instead. |
Thomas Zimmermann | 06d6620 | 2020-06-05 09:32:06 +0200 | [diff] [blame] | 154 | */ |
| 155 | #define DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE(dumb_create_func) \ |
Thomas Zimmermann | 654bf12 | 2020-06-05 09:32:07 +0200 | [diff] [blame] | 156 | .dumb_create = dumb_create_func, \ |
Thomas Zimmermann | 06d6620 | 2020-06-05 09:32:06 +0200 | [diff] [blame] | 157 | .prime_handle_to_fd = drm_gem_prime_handle_to_fd, \ |
| 158 | .prime_fd_to_handle = drm_gem_prime_fd_to_handle, \ |
| 159 | .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table_vmap, \ |
| 160 | .gem_prime_mmap = drm_gem_prime_mmap |
| 161 | |
| 162 | /** |
| 163 | * DRM_GEM_CMA_DRIVER_OPS_VMAP - CMA GEM driver operations ensuring a virtual |
Noralf Trønnes | b9068cd | 2018-11-10 15:56:46 +0100 | [diff] [blame] | 164 | * address on the buffer |
| 165 | * |
| 166 | * This macro provides a shortcut for setting the default GEM operations in the |
| 167 | * &drm_driver structure for drivers that need the virtual address also on |
| 168 | * imported buffers. |
Thomas Zimmermann | 06d6620 | 2020-06-05 09:32:06 +0200 | [diff] [blame] | 169 | * |
| 170 | * Drivers that come with their own implementation of |
| 171 | * &struct drm_driver.dumb_create should use |
| 172 | * DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE() instead. Use |
Thomas Zimmermann | 654bf12 | 2020-06-05 09:32:07 +0200 | [diff] [blame] | 173 | * DRM_GEM_CMA_DRIVER_OPS_VMAP if possible. Drivers that do not require a |
| 174 | * virtual address on imported buffers should use DRM_GEM_CMA_DRIVER_OPS |
| 175 | * instead. |
Noralf Trønnes | b9068cd | 2018-11-10 15:56:46 +0100 | [diff] [blame] | 176 | */ |
Thomas Zimmermann | 06d6620 | 2020-06-05 09:32:06 +0200 | [diff] [blame] | 177 | #define DRM_GEM_CMA_DRIVER_OPS_VMAP \ |
| 178 | DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE(drm_gem_cma_dumb_create) |
Noralf Trønnes | b9068cd | 2018-11-10 15:56:46 +0100 | [diff] [blame] | 179 | |
| 180 | struct drm_gem_object * |
| 181 | drm_gem_cma_prime_import_sg_table_vmap(struct drm_device *drm, |
| 182 | struct dma_buf_attachment *attach, |
| 183 | struct sg_table *sgt); |
| 184 | |
Sascha Hauer | b9d4745 | 2012-06-27 15:30:18 +0200 | [diff] [blame] | 185 | #endif /* __DRM_GEM_CMA_HELPER_H__ */ |