panthor_gem.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* SPDX-License-Identifier: GPL-2.0 or MIT */
  2. /* Copyright 2019 Linaro, Ltd, Rob Herring <robh@kernel.org> */
  3. /* Copyright 2023 Collabora ltd. */
  4. #ifndef __PANTHOR_GEM_H__
  5. #define __PANTHOR_GEM_H__
  6. #include <drm/drm_gem_shmem_helper.h>
  7. #include <drm/drm_mm.h>
  8. #include <linux/iosys-map.h>
  9. #include <linux/rwsem.h>
  10. struct panthor_vm;
  11. #define PANTHOR_BO_LABEL_MAXLEN 4096
  12. enum panthor_debugfs_gem_state_flags {
  13. PANTHOR_DEBUGFS_GEM_STATE_IMPORTED_BIT = 0,
  14. PANTHOR_DEBUGFS_GEM_STATE_EXPORTED_BIT = 1,
  15. /** @PANTHOR_DEBUGFS_GEM_STATE_FLAG_IMPORTED: GEM BO is PRIME imported. */
  16. PANTHOR_DEBUGFS_GEM_STATE_FLAG_IMPORTED = BIT(PANTHOR_DEBUGFS_GEM_STATE_IMPORTED_BIT),
  17. /** @PANTHOR_DEBUGFS_GEM_STATE_FLAG_EXPORTED: GEM BO is PRIME exported. */
  18. PANTHOR_DEBUGFS_GEM_STATE_FLAG_EXPORTED = BIT(PANTHOR_DEBUGFS_GEM_STATE_EXPORTED_BIT),
  19. };
  20. enum panthor_debugfs_gem_usage_flags {
  21. PANTHOR_DEBUGFS_GEM_USAGE_KERNEL_BIT = 0,
  22. PANTHOR_DEBUGFS_GEM_USAGE_FW_MAPPED_BIT = 1,
  23. /** @PANTHOR_DEBUGFS_GEM_USAGE_FLAG_KERNEL: BO is for kernel use only. */
  24. PANTHOR_DEBUGFS_GEM_USAGE_FLAG_KERNEL = BIT(PANTHOR_DEBUGFS_GEM_USAGE_KERNEL_BIT),
  25. /** @PANTHOR_DEBUGFS_GEM_USAGE_FLAG_FW_MAPPED: BO is mapped on the FW VM. */
  26. PANTHOR_DEBUGFS_GEM_USAGE_FLAG_FW_MAPPED = BIT(PANTHOR_DEBUGFS_GEM_USAGE_FW_MAPPED_BIT),
  27. };
  28. /**
  29. * struct panthor_gem_debugfs - GEM object's DebugFS list information
  30. */
  31. struct panthor_gem_debugfs {
  32. /**
  33. * @node: Node used to insert the object in the device-wide list of
  34. * GEM objects, to display information about it through a DebugFS file.
  35. */
  36. struct list_head node;
  37. /** @creator: Information about the UM process which created the GEM. */
  38. struct {
  39. /** @creator.process_name: Group leader name in owning thread's process */
  40. char process_name[TASK_COMM_LEN];
  41. /** @creator.tgid: PID of the thread's group leader within its process */
  42. pid_t tgid;
  43. } creator;
  44. /** @flags: Combination of panthor_debugfs_gem_usage_flags flags */
  45. u32 flags;
  46. };
  47. /**
  48. * struct panthor_gem_object - Driver specific GEM object.
  49. */
  50. struct panthor_gem_object {
  51. /** @base: Inherit from drm_gem_shmem_object. */
  52. struct drm_gem_shmem_object base;
  53. /**
  54. * @exclusive_vm_root_gem: Root GEM of the exclusive VM this GEM object
  55. * is attached to.
  56. *
  57. * If @exclusive_vm_root_gem != NULL, any attempt to bind the GEM to a
  58. * different VM will fail.
  59. *
  60. * All FW memory objects have this field set to the root GEM of the MCU
  61. * VM.
  62. */
  63. struct drm_gem_object *exclusive_vm_root_gem;
  64. /** @flags: Combination of drm_panthor_bo_flags flags. */
  65. u32 flags;
  66. /**
  67. * @label: BO tagging fields. The label can be assigned within the
  68. * driver itself or through a specific IOCTL.
  69. */
  70. struct {
  71. /**
  72. * @label.str: Pointer to NULL-terminated string,
  73. */
  74. const char *str;
  75. /** @lock.str: Protects access to the @label.str field. */
  76. struct mutex lock;
  77. } label;
  78. #ifdef CONFIG_DEBUG_FS
  79. struct panthor_gem_debugfs debugfs;
  80. #endif
  81. };
  82. /**
  83. * struct panthor_kernel_bo - Kernel buffer object.
  84. *
  85. * These objects are only manipulated by the kernel driver and not
  86. * directly exposed to the userspace. The GPU address of a kernel
  87. * BO might be passed to userspace though.
  88. */
  89. struct panthor_kernel_bo {
  90. /**
  91. * @obj: The GEM object backing this kernel buffer object.
  92. */
  93. struct drm_gem_object *obj;
  94. /**
  95. * @vm: VM this private buffer is attached to.
  96. */
  97. struct panthor_vm *vm;
  98. /**
  99. * @va_node: VA space allocated to this GEM.
  100. */
  101. struct drm_mm_node va_node;
  102. /**
  103. * @kmap: Kernel CPU mapping of @gem.
  104. */
  105. void *kmap;
  106. };
  107. static inline
  108. struct panthor_gem_object *to_panthor_bo(struct drm_gem_object *obj)
  109. {
  110. return container_of(to_drm_gem_shmem_obj(obj), struct panthor_gem_object, base);
  111. }
  112. void panthor_gem_init(struct panthor_device *ptdev);
  113. struct drm_gem_object *panthor_gem_create_object(struct drm_device *ddev, size_t size);
  114. int
  115. panthor_gem_create_with_handle(struct drm_file *file,
  116. struct drm_device *ddev,
  117. struct panthor_vm *exclusive_vm,
  118. u64 *size, u32 flags, uint32_t *handle);
  119. void panthor_gem_bo_set_label(struct drm_gem_object *obj, const char *label);
  120. void panthor_gem_kernel_bo_set_label(struct panthor_kernel_bo *bo, const char *label);
  121. int panthor_gem_sync(struct drm_gem_object *obj,
  122. u32 type, u64 offset, u64 size);
  123. struct drm_gem_object *
  124. panthor_gem_prime_import(struct drm_device *dev,
  125. struct dma_buf *dma_buf);
  126. static inline u64
  127. panthor_kernel_bo_gpuva(struct panthor_kernel_bo *bo)
  128. {
  129. return bo->va_node.start;
  130. }
  131. static inline size_t
  132. panthor_kernel_bo_size(struct panthor_kernel_bo *bo)
  133. {
  134. return bo->obj->size;
  135. }
  136. static inline int
  137. panthor_kernel_bo_vmap(struct panthor_kernel_bo *bo)
  138. {
  139. struct iosys_map map;
  140. int ret;
  141. if (bo->kmap)
  142. return 0;
  143. ret = drm_gem_vmap(bo->obj, &map);
  144. if (ret)
  145. return ret;
  146. bo->kmap = map.vaddr;
  147. return 0;
  148. }
  149. static inline void
  150. panthor_kernel_bo_vunmap(struct panthor_kernel_bo *bo)
  151. {
  152. if (bo->kmap) {
  153. struct iosys_map map = IOSYS_MAP_INIT_VADDR(bo->kmap);
  154. drm_gem_vunmap(bo->obj, &map);
  155. bo->kmap = NULL;
  156. }
  157. }
  158. struct panthor_kernel_bo *
  159. panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm,
  160. size_t size, u32 bo_flags, u32 vm_map_flags,
  161. u64 gpu_va, const char *name);
  162. void panthor_kernel_bo_destroy(struct panthor_kernel_bo *bo);
  163. #ifdef CONFIG_DEBUG_FS
  164. void panthor_gem_debugfs_print_bos(struct panthor_device *pfdev,
  165. struct seq_file *m);
  166. #endif
  167. #endif /* __PANTHOR_GEM_H__ */