panthor_gem.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. /**
  12. * struct panthor_gem_object - Driver specific GEM object.
  13. */
  14. struct panthor_gem_object {
  15. /** @base: Inherit from drm_gem_shmem_object. */
  16. struct drm_gem_shmem_object base;
  17. /**
  18. * @exclusive_vm_root_gem: Root GEM of the exclusive VM this GEM object
  19. * is attached to.
  20. *
  21. * If @exclusive_vm_root_gem != NULL, any attempt to bind the GEM to a
  22. * different VM will fail.
  23. *
  24. * All FW memory objects have this field set to the root GEM of the MCU
  25. * VM.
  26. */
  27. struct drm_gem_object *exclusive_vm_root_gem;
  28. /**
  29. * @gpuva_list_lock: Custom GPUVA lock.
  30. *
  31. * Used to protect insertion of drm_gpuva elements to the
  32. * drm_gem_object.gpuva.list list.
  33. *
  34. * We can't use the GEM resv for that, because drm_gpuva_link() is
  35. * called in a dma-signaling path, where we're not allowed to take
  36. * resv locks.
  37. */
  38. struct mutex gpuva_list_lock;
  39. /** @flags: Combination of drm_panthor_bo_flags flags. */
  40. u32 flags;
  41. };
  42. /**
  43. * struct panthor_kernel_bo - Kernel buffer object.
  44. *
  45. * These objects are only manipulated by the kernel driver and not
  46. * directly exposed to the userspace. The GPU address of a kernel
  47. * BO might be passed to userspace though.
  48. */
  49. struct panthor_kernel_bo {
  50. /**
  51. * @obj: The GEM object backing this kernel buffer object.
  52. */
  53. struct drm_gem_object *obj;
  54. /**
  55. * @vm: VM this private buffer is attached to.
  56. */
  57. struct panthor_vm *vm;
  58. /**
  59. * @va_node: VA space allocated to this GEM.
  60. */
  61. struct drm_mm_node va_node;
  62. /**
  63. * @kmap: Kernel CPU mapping of @gem.
  64. */
  65. void *kmap;
  66. };
  67. static inline
  68. struct panthor_gem_object *to_panthor_bo(struct drm_gem_object *obj)
  69. {
  70. return container_of(to_drm_gem_shmem_obj(obj), struct panthor_gem_object, base);
  71. }
  72. struct drm_gem_object *panthor_gem_create_object(struct drm_device *ddev, size_t size);
  73. struct drm_gem_object *
  74. panthor_gem_prime_import_sg_table(struct drm_device *ddev,
  75. struct dma_buf_attachment *attach,
  76. struct sg_table *sgt);
  77. int
  78. panthor_gem_create_with_handle(struct drm_file *file,
  79. struct drm_device *ddev,
  80. struct panthor_vm *exclusive_vm,
  81. u64 *size, u32 flags, uint32_t *handle);
  82. static inline u64
  83. panthor_kernel_bo_gpuva(struct panthor_kernel_bo *bo)
  84. {
  85. return bo->va_node.start;
  86. }
  87. static inline size_t
  88. panthor_kernel_bo_size(struct panthor_kernel_bo *bo)
  89. {
  90. return bo->obj->size;
  91. }
  92. static inline int
  93. panthor_kernel_bo_vmap(struct panthor_kernel_bo *bo)
  94. {
  95. struct iosys_map map;
  96. int ret;
  97. if (bo->kmap)
  98. return 0;
  99. ret = drm_gem_vmap_unlocked(bo->obj, &map);
  100. if (ret)
  101. return ret;
  102. bo->kmap = map.vaddr;
  103. return 0;
  104. }
  105. static inline void
  106. panthor_kernel_bo_vunmap(struct panthor_kernel_bo *bo)
  107. {
  108. if (bo->kmap) {
  109. struct iosys_map map = IOSYS_MAP_INIT_VADDR(bo->kmap);
  110. drm_gem_vunmap_unlocked(bo->obj, &map);
  111. bo->kmap = NULL;
  112. }
  113. }
  114. struct panthor_kernel_bo *
  115. panthor_kernel_bo_create(struct panthor_device *ptdev, struct panthor_vm *vm,
  116. size_t size, u32 bo_flags, u32 vm_map_flags,
  117. u64 gpu_va);
  118. void panthor_kernel_bo_destroy(struct panthor_kernel_bo *bo);
  119. #endif /* __PANTHOR_GEM_H__ */