drm_gem_dma_helper.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __DRM_GEM_DMA_HELPER_H__
  3. #define __DRM_GEM_DMA_HELPER_H__
  4. #include <drm/drm_file.h>
  5. #include <drm/drm_ioctl.h>
  6. #include <drm/drm_gem.h>
  7. struct drm_mode_create_dumb;
  8. /**
  9. * struct drm_gem_dma_object - GEM object backed by DMA memory allocations
  10. * @base: base GEM object
  11. * @dma_addr: DMA address of the backing memory
  12. * @sgt: scatter/gather table for imported PRIME buffers. The table can have
  13. * more than one entry but they are guaranteed to have contiguous
  14. * DMA addresses.
  15. * @vaddr: kernel virtual address of the backing memory
  16. * @map_noncoherent: if true, the GEM object is backed by non-coherent memory
  17. */
  18. struct drm_gem_dma_object {
  19. struct drm_gem_object base;
  20. dma_addr_t dma_addr;
  21. struct sg_table *sgt;
  22. /* For objects with DMA memory allocated by GEM DMA */
  23. void *vaddr;
  24. bool map_noncoherent;
  25. };
  26. #define to_drm_gem_dma_obj(gem_obj) \
  27. container_of(gem_obj, struct drm_gem_dma_object, base)
  28. struct drm_gem_dma_object *drm_gem_dma_create(struct drm_device *drm,
  29. size_t size);
  30. void drm_gem_dma_free(struct drm_gem_dma_object *dma_obj);
  31. void drm_gem_dma_print_info(const struct drm_gem_dma_object *dma_obj,
  32. struct drm_printer *p, unsigned int indent);
  33. struct sg_table *drm_gem_dma_get_sg_table(struct drm_gem_dma_object *dma_obj);
  34. int drm_gem_dma_vmap(struct drm_gem_dma_object *dma_obj,
  35. struct iosys_map *map);
  36. int drm_gem_dma_mmap(struct drm_gem_dma_object *dma_obj, struct vm_area_struct *vma);
  37. extern const struct vm_operations_struct drm_gem_dma_vm_ops;
  38. /*
  39. * GEM object functions
  40. */
  41. /**
  42. * drm_gem_dma_object_free - GEM object function for drm_gem_dma_free()
  43. * @obj: GEM object to free
  44. *
  45. * This function wraps drm_gem_dma_free_object(). Drivers that employ the DMA helpers
  46. * should use it as their &drm_gem_object_funcs.free handler.
  47. */
  48. static inline void drm_gem_dma_object_free(struct drm_gem_object *obj)
  49. {
  50. struct drm_gem_dma_object *dma_obj = to_drm_gem_dma_obj(obj);
  51. drm_gem_dma_free(dma_obj);
  52. }
  53. /**
  54. * drm_gem_dma_object_print_info() - Print &drm_gem_dma_object info for debugfs
  55. * @p: DRM printer
  56. * @indent: Tab indentation level
  57. * @obj: GEM object
  58. *
  59. * This function wraps drm_gem_dma_print_info(). Drivers that employ the DMA helpers
  60. * should use this function as their &drm_gem_object_funcs.print_info handler.
  61. */
  62. static inline void drm_gem_dma_object_print_info(struct drm_printer *p, unsigned int indent,
  63. const struct drm_gem_object *obj)
  64. {
  65. const struct drm_gem_dma_object *dma_obj = to_drm_gem_dma_obj(obj);
  66. drm_gem_dma_print_info(dma_obj, p, indent);
  67. }
  68. /**
  69. * drm_gem_dma_object_get_sg_table - GEM object function for drm_gem_dma_get_sg_table()
  70. * @obj: GEM object
  71. *
  72. * This function wraps drm_gem_dma_get_sg_table(). Drivers that employ the DMA helpers should
  73. * use it as their &drm_gem_object_funcs.get_sg_table handler.
  74. *
  75. * Returns:
  76. * A pointer to the scatter/gather table of pinned pages or NULL on failure.
  77. */
  78. static inline struct sg_table *drm_gem_dma_object_get_sg_table(struct drm_gem_object *obj)
  79. {
  80. struct drm_gem_dma_object *dma_obj = to_drm_gem_dma_obj(obj);
  81. return drm_gem_dma_get_sg_table(dma_obj);
  82. }
  83. /*
  84. * drm_gem_dma_object_vmap - GEM object function for drm_gem_dma_vmap()
  85. * @obj: GEM object
  86. * @map: Returns the kernel virtual address of the DMA GEM object's backing store.
  87. *
  88. * This function wraps drm_gem_dma_vmap(). Drivers that employ the DMA helpers should
  89. * use it as their &drm_gem_object_funcs.vmap handler.
  90. *
  91. * Returns:
  92. * 0 on success or a negative error code on failure.
  93. */
  94. static inline int drm_gem_dma_object_vmap(struct drm_gem_object *obj,
  95. struct iosys_map *map)
  96. {
  97. struct drm_gem_dma_object *dma_obj = to_drm_gem_dma_obj(obj);
  98. return drm_gem_dma_vmap(dma_obj, map);
  99. }
  100. /**
  101. * drm_gem_dma_object_mmap - GEM object function for drm_gem_dma_mmap()
  102. * @obj: GEM object
  103. * @vma: VMA for the area to be mapped
  104. *
  105. * This function wraps drm_gem_dma_mmap(). Drivers that employ the dma helpers should
  106. * use it as their &drm_gem_object_funcs.mmap handler.
  107. *
  108. * Returns:
  109. * 0 on success or a negative error code on failure.
  110. */
  111. static inline int drm_gem_dma_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
  112. {
  113. struct drm_gem_dma_object *dma_obj = to_drm_gem_dma_obj(obj);
  114. return drm_gem_dma_mmap(dma_obj, vma);
  115. }
  116. /*
  117. * Driver ops
  118. */
  119. /* create memory region for DRM framebuffer */
  120. int drm_gem_dma_dumb_create_internal(struct drm_file *file_priv,
  121. struct drm_device *drm,
  122. struct drm_mode_create_dumb *args);
  123. /* create memory region for DRM framebuffer */
  124. int drm_gem_dma_dumb_create(struct drm_file *file_priv,
  125. struct drm_device *drm,
  126. struct drm_mode_create_dumb *args);
  127. struct drm_gem_object *
  128. drm_gem_dma_prime_import_sg_table(struct drm_device *dev,
  129. struct dma_buf_attachment *attach,
  130. struct sg_table *sgt);
  131. /**
  132. * DRM_GEM_DMA_DRIVER_OPS_WITH_DUMB_CREATE - DMA GEM driver operations
  133. * @dumb_create_func: callback function for .dumb_create
  134. *
  135. * This macro provides a shortcut for setting the default GEM operations in the
  136. * &drm_driver structure.
  137. *
  138. * This macro is a variant of DRM_GEM_DMA_DRIVER_OPS for drivers that
  139. * override the default implementation of &struct rm_driver.dumb_create. Use
  140. * DRM_GEM_DMA_DRIVER_OPS if possible. Drivers that require a virtual address
  141. * on imported buffers should use
  142. * DRM_GEM_DMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE() instead.
  143. */
  144. #define DRM_GEM_DMA_DRIVER_OPS_WITH_DUMB_CREATE(dumb_create_func) \
  145. .dumb_create = (dumb_create_func), \
  146. .prime_handle_to_fd = drm_gem_prime_handle_to_fd, \
  147. .prime_fd_to_handle = drm_gem_prime_fd_to_handle, \
  148. .gem_prime_import_sg_table = drm_gem_dma_prime_import_sg_table, \
  149. .gem_prime_mmap = drm_gem_prime_mmap
  150. /**
  151. * DRM_GEM_DMA_DRIVER_OPS - DMA GEM driver operations
  152. *
  153. * This macro provides a shortcut for setting the default GEM operations in the
  154. * &drm_driver structure.
  155. *
  156. * Drivers that come with their own implementation of
  157. * &struct drm_driver.dumb_create should use
  158. * DRM_GEM_DMA_DRIVER_OPS_WITH_DUMB_CREATE() instead. Use
  159. * DRM_GEM_DMA_DRIVER_OPS if possible. Drivers that require a virtual address
  160. * on imported buffers should use DRM_GEM_DMA_DRIVER_OPS_VMAP instead.
  161. */
  162. #define DRM_GEM_DMA_DRIVER_OPS \
  163. DRM_GEM_DMA_DRIVER_OPS_WITH_DUMB_CREATE(drm_gem_dma_dumb_create)
  164. /**
  165. * DRM_GEM_DMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE - DMA GEM driver operations
  166. * ensuring a virtual address
  167. * on the buffer
  168. * @dumb_create_func: callback function for .dumb_create
  169. *
  170. * This macro provides a shortcut for setting the default GEM operations in the
  171. * &drm_driver structure for drivers that need the virtual address also on
  172. * imported buffers.
  173. *
  174. * This macro is a variant of DRM_GEM_DMA_DRIVER_OPS_VMAP for drivers that
  175. * override the default implementation of &struct drm_driver.dumb_create. Use
  176. * DRM_GEM_DMA_DRIVER_OPS_VMAP if possible. Drivers that do not require a
  177. * virtual address on imported buffers should use
  178. * DRM_GEM_DMA_DRIVER_OPS_WITH_DUMB_CREATE() instead.
  179. */
  180. #define DRM_GEM_DMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE(dumb_create_func) \
  181. .dumb_create = dumb_create_func, \
  182. .prime_handle_to_fd = drm_gem_prime_handle_to_fd, \
  183. .prime_fd_to_handle = drm_gem_prime_fd_to_handle, \
  184. .gem_prime_import_sg_table = drm_gem_dma_prime_import_sg_table_vmap, \
  185. .gem_prime_mmap = drm_gem_prime_mmap
  186. /**
  187. * DRM_GEM_DMA_DRIVER_OPS_VMAP - DMA GEM driver operations ensuring a virtual
  188. * address on the buffer
  189. *
  190. * This macro provides a shortcut for setting the default GEM operations in the
  191. * &drm_driver structure for drivers that need the virtual address also on
  192. * imported buffers.
  193. *
  194. * Drivers that come with their own implementation of
  195. * &struct drm_driver.dumb_create should use
  196. * DRM_GEM_DMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE() instead. Use
  197. * DRM_GEM_DMA_DRIVER_OPS_VMAP if possible. Drivers that do not require a
  198. * virtual address on imported buffers should use DRM_GEM_DMA_DRIVER_OPS
  199. * instead.
  200. */
  201. #define DRM_GEM_DMA_DRIVER_OPS_VMAP \
  202. DRM_GEM_DMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE(drm_gem_dma_dumb_create)
  203. struct drm_gem_object *
  204. drm_gem_dma_prime_import_sg_table_vmap(struct drm_device *drm,
  205. struct dma_buf_attachment *attach,
  206. struct sg_table *sgt);
  207. /*
  208. * File ops
  209. */
  210. #ifndef CONFIG_MMU
  211. unsigned long drm_gem_dma_get_unmapped_area(struct file *filp,
  212. unsigned long addr,
  213. unsigned long len,
  214. unsigned long pgoff,
  215. unsigned long flags);
  216. #define DRM_GEM_DMA_UNMAPPED_AREA_FOPS \
  217. .get_unmapped_area = drm_gem_dma_get_unmapped_area,
  218. #else
  219. #define DRM_GEM_DMA_UNMAPPED_AREA_FOPS
  220. #endif
  221. /**
  222. * DEFINE_DRM_GEM_DMA_FOPS() - macro to generate file operations for DMA drivers
  223. * @name: name for the generated structure
  224. *
  225. * This macro autogenerates a suitable &struct file_operations for DMA based
  226. * drivers, which can be assigned to &drm_driver.fops. Note that this structure
  227. * cannot be shared between drivers, because it contains a reference to the
  228. * current module using THIS_MODULE.
  229. *
  230. * Note that the declaration is already marked as static - if you need a
  231. * non-static version of this you're probably doing it wrong and will break the
  232. * THIS_MODULE reference by accident.
  233. */
  234. #define DEFINE_DRM_GEM_DMA_FOPS(name) \
  235. static const struct file_operations name = {\
  236. .owner = THIS_MODULE,\
  237. .open = drm_open,\
  238. .release = drm_release,\
  239. .unlocked_ioctl = drm_ioctl,\
  240. .compat_ioctl = drm_compat_ioctl,\
  241. .poll = drm_poll,\
  242. .read = drm_read,\
  243. .llseek = noop_llseek,\
  244. .mmap = drm_gem_mmap,\
  245. DRM_GEM_DMA_UNMAPPED_AREA_FOPS \
  246. }
  247. #endif /* __DRM_GEM_DMA_HELPER_H__ */