drm_gem_cma_helper.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __DRM_GEM_CMA_HELPER_H__
  3. #define __DRM_GEM_CMA_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_cma_object - GEM object backed by CMA memory allocations
  10. * @base: base GEM object
  11. * @paddr: physical 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. */
  17. struct drm_gem_cma_object {
  18. struct drm_gem_object base;
  19. dma_addr_t paddr;
  20. struct sg_table *sgt;
  21. /* For objects with DMA memory allocated by GEM CMA */
  22. void *vaddr;
  23. };
  24. #define to_drm_gem_cma_obj(gem_obj) \
  25. container_of(gem_obj, struct drm_gem_cma_object, base)
  26. #ifndef CONFIG_MMU
  27. #define DRM_GEM_CMA_UNMAPPED_AREA_FOPS \
  28. .get_unmapped_area = drm_gem_cma_get_unmapped_area,
  29. #else
  30. #define DRM_GEM_CMA_UNMAPPED_AREA_FOPS
  31. #endif
  32. /**
  33. * DEFINE_DRM_GEM_CMA_FOPS() - macro to generate file operations for CMA drivers
  34. * @name: name for the generated structure
  35. *
  36. * This macro autogenerates a suitable &struct file_operations for CMA based
  37. * drivers, which can be assigned to &drm_driver.fops. Note that this structure
  38. * cannot be shared between drivers, because it contains a reference to the
  39. * current module using THIS_MODULE.
  40. *
  41. * Note that the declaration is already marked as static - if you need a
  42. * non-static version of this you're probably doing it wrong and will break the
  43. * THIS_MODULE reference by accident.
  44. */
  45. #define DEFINE_DRM_GEM_CMA_FOPS(name) \
  46. static const struct file_operations name = {\
  47. .owner = THIS_MODULE,\
  48. .open = drm_open,\
  49. .release = drm_release,\
  50. .unlocked_ioctl = drm_ioctl,\
  51. .compat_ioctl = drm_compat_ioctl,\
  52. .poll = drm_poll,\
  53. .read = drm_read,\
  54. .llseek = noop_llseek,\
  55. .mmap = drm_gem_cma_mmap,\
  56. DRM_GEM_CMA_UNMAPPED_AREA_FOPS \
  57. }
  58. /* free GEM object */
  59. void drm_gem_cma_free_object(struct drm_gem_object *gem_obj);
  60. /* create memory region for DRM framebuffer */
  61. int drm_gem_cma_dumb_create_internal(struct drm_file *file_priv,
  62. struct drm_device *drm,
  63. struct drm_mode_create_dumb *args);
  64. /* create memory region for DRM framebuffer */
  65. int drm_gem_cma_dumb_create(struct drm_file *file_priv,
  66. struct drm_device *drm,
  67. struct drm_mode_create_dumb *args);
  68. /* set vm_flags and we can change the VM attribute to other one at here */
  69. int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma);
  70. /* allocate physical memory */
  71. struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm,
  72. size_t size);
  73. extern const struct vm_operations_struct drm_gem_cma_vm_ops;
  74. #ifndef CONFIG_MMU
  75. unsigned long drm_gem_cma_get_unmapped_area(struct file *filp,
  76. unsigned long addr,
  77. unsigned long len,
  78. unsigned long pgoff,
  79. unsigned long flags);
  80. #endif
  81. void drm_gem_cma_print_info(struct drm_printer *p, unsigned int indent,
  82. const struct drm_gem_object *obj);
  83. struct sg_table *drm_gem_cma_prime_get_sg_table(struct drm_gem_object *obj);
  84. struct drm_gem_object *
  85. drm_gem_cma_prime_import_sg_table(struct drm_device *dev,
  86. struct dma_buf_attachment *attach,
  87. struct sg_table *sgt);
  88. int drm_gem_cma_prime_mmap(struct drm_gem_object *obj,
  89. struct vm_area_struct *vma);
  90. void *drm_gem_cma_prime_vmap(struct drm_gem_object *obj);
  91. void drm_gem_cma_prime_vunmap(struct drm_gem_object *obj, void *vaddr);
  92. struct drm_gem_object *
  93. drm_gem_cma_create_object_default_funcs(struct drm_device *dev, size_t size);
  94. /**
  95. * DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE - CMA GEM driver operations
  96. * @dumb_create_func: callback function for .dumb_create
  97. *
  98. * This macro provides a shortcut for setting the default GEM operations in the
  99. * &drm_driver structure.
  100. *
  101. * This macro is a variant of DRM_GEM_CMA_DRIVER_OPS for drivers that
  102. * override the default implementation of &struct rm_driver.dumb_create. Use
  103. * DRM_GEM_CMA_DRIVER_OPS if possible. Drivers that require a virtual address
  104. * on imported buffers should use
  105. * DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE() instead.
  106. */
  107. #define DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(dumb_create_func) \
  108. .gem_create_object = drm_gem_cma_create_object_default_funcs, \
  109. .dumb_create = (dumb_create_func), \
  110. .prime_handle_to_fd = drm_gem_prime_handle_to_fd, \
  111. .prime_fd_to_handle = drm_gem_prime_fd_to_handle, \
  112. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, \
  113. .gem_prime_mmap = drm_gem_cma_prime_mmap
  114. /**
  115. * DRM_GEM_CMA_DRIVER_OPS - CMA GEM driver operations
  116. *
  117. * This macro provides a shortcut for setting the default GEM operations in the
  118. * &drm_driver structure.
  119. *
  120. * Drivers that come with their own implementation of
  121. * &struct drm_driver.dumb_create should use
  122. * DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE() instead. Use
  123. * DRM_GEM_CMA_DRIVER_OPS if possible. Drivers that require a virtual address
  124. * on imported buffers should use DRM_GEM_CMA_DRIVER_OPS_VMAP instead.
  125. */
  126. #define DRM_GEM_CMA_DRIVER_OPS \
  127. DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(drm_gem_cma_dumb_create)
  128. /**
  129. * DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE - CMA GEM driver operations
  130. * ensuring a virtual address
  131. * on the buffer
  132. * @dumb_create_func: callback function for .dumb_create
  133. *
  134. * This macro provides a shortcut for setting the default GEM operations in the
  135. * &drm_driver structure for drivers that need the virtual address also on
  136. * imported buffers.
  137. *
  138. * This macro is a variant of DRM_GEM_CMA_DRIVER_OPS_VMAP for drivers that
  139. * override the default implementation of &struct drm_driver.dumb_create. Use
  140. * DRM_GEM_CMA_DRIVER_OPS_VMAP if possible. Drivers that do not require a
  141. * virtual address on imported buffers should use
  142. * DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE() instead.
  143. */
  144. #define DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE(dumb_create_func) \
  145. .gem_create_object = drm_gem_cma_create_object_default_funcs, \
  146. .dumb_create = dumb_create_func, \
  147. .prime_handle_to_fd = drm_gem_prime_handle_to_fd, \
  148. .prime_fd_to_handle = drm_gem_prime_fd_to_handle, \
  149. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table_vmap, \
  150. .gem_prime_mmap = drm_gem_prime_mmap
  151. /**
  152. * DRM_GEM_CMA_DRIVER_OPS_VMAP - CMA GEM driver operations ensuring a virtual
  153. * address on the buffer
  154. *
  155. * This macro provides a shortcut for setting the default GEM operations in the
  156. * &drm_driver structure for drivers that need the virtual address also on
  157. * imported buffers.
  158. *
  159. * Drivers that come with their own implementation of
  160. * &struct drm_driver.dumb_create should use
  161. * DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE() instead. Use
  162. * DRM_GEM_CMA_DRIVER_OPS_VMAP if possible. Drivers that do not require a
  163. * virtual address on imported buffers should use DRM_GEM_CMA_DRIVER_OPS
  164. * instead.
  165. */
  166. #define DRM_GEM_CMA_DRIVER_OPS_VMAP \
  167. DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE(drm_gem_cma_dumb_create)
  168. struct drm_gem_object *
  169. drm_gem_cma_prime_import_sg_table_vmap(struct drm_device *drm,
  170. struct dma_buf_attachment *attach,
  171. struct sg_table *sgt);
  172. #endif /* __DRM_GEM_CMA_HELPER_H__ */