pvr_gem.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* SPDX-License-Identifier: GPL-2.0-only OR MIT */
  2. /* Copyright (c) 2023 Imagination Technologies Ltd. */
  3. #ifndef PVR_GEM_H
  4. #define PVR_GEM_H
  5. #include "pvr_rogue_heap_config.h"
  6. #include "pvr_rogue_meta.h"
  7. #include <uapi/drm/pvr_drm.h>
  8. #include <drm/drm_gem.h>
  9. #include <drm/drm_gem_shmem_helper.h>
  10. #include <drm/drm_mm.h>
  11. #include <linux/bitfield.h>
  12. #include <linux/bits.h>
  13. #include <linux/const.h>
  14. #include <linux/compiler_attributes.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mutex.h>
  17. #include <linux/refcount.h>
  18. #include <linux/scatterlist.h>
  19. #include <linux/sizes.h>
  20. #include <linux/types.h>
  21. /* Forward declaration from "pvr_device.h". */
  22. struct pvr_device;
  23. struct pvr_file;
  24. /**
  25. * DOC: Flags for DRM_IOCTL_PVR_CREATE_BO (kernel-only)
  26. *
  27. * Kernel-only values allowed in &pvr_gem_object->flags. The majority of options
  28. * for this field are specified in the UAPI header "pvr_drm.h" with a
  29. * DRM_PVR_BO_ prefix. To distinguish these internal options (which must exist
  30. * in ranges marked as "reserved" in the UAPI header), we drop the DRM prefix.
  31. * The public options should be used directly, DRM prefix and all.
  32. *
  33. * To avoid potentially confusing gaps in the UAPI options, these kernel-only
  34. * options are specified "in reverse", starting at bit 63.
  35. *
  36. * We use "reserved" to refer to bits defined here and not exposed in the UAPI.
  37. * Bits not defined anywhere are "undefined".
  38. *
  39. * CPU mapping options
  40. * :PVR_BO_CPU_CACHED: By default, all GEM objects are mapped write-combined on the CPU. Set this
  41. * flag to override this behaviour and map the object cached.
  42. *
  43. * Firmware options
  44. * :PVR_BO_FW_NO_CLEAR_ON_RESET: By default, all FW objects are cleared and reinitialised on hard
  45. * reset. Set this flag to override this behaviour and preserve buffer contents on reset.
  46. */
  47. #define PVR_BO_CPU_CACHED BIT_ULL(63)
  48. #define PVR_BO_FW_NO_CLEAR_ON_RESET BIT_ULL(62)
  49. #define PVR_BO_KERNEL_FLAGS_MASK (PVR_BO_CPU_CACHED | PVR_BO_FW_NO_CLEAR_ON_RESET)
  50. /* Bits 61..3 are undefined. */
  51. /* Bits 2..0 are defined in the UAPI. */
  52. /* Other utilities. */
  53. #define PVR_BO_UNDEFINED_MASK ~(PVR_BO_KERNEL_FLAGS_MASK | DRM_PVR_BO_FLAGS_MASK)
  54. /*
  55. * All firmware-mapped memory uses (mostly) the same flags. Specifically,
  56. * firmware-mapped memory should be:
  57. * * Read/write on the device,
  58. * * Read/write on the CPU, and
  59. * * Write-combined on the CPU.
  60. *
  61. * The only variation is in caching on the device.
  62. */
  63. #define PVR_BO_FW_FLAGS_DEVICE_CACHED (ULL(0))
  64. #define PVR_BO_FW_FLAGS_DEVICE_UNCACHED DRM_PVR_BO_BYPASS_DEVICE_CACHE
  65. /**
  66. * struct pvr_gem_object - powervr-specific wrapper for &struct drm_gem_object
  67. */
  68. struct pvr_gem_object {
  69. /**
  70. * @base: The underlying &struct drm_gem_shmem_object.
  71. *
  72. * Do not access this member directly, instead call
  73. * shem_gem_from_pvr_gem().
  74. */
  75. struct drm_gem_shmem_object base;
  76. /**
  77. * @flags: Options set at creation-time. Some of these options apply to
  78. * the creation operation itself (which are stored here for reference)
  79. * with the remainder used for mapping options to both the device and
  80. * CPU. These are used every time this object is mapped, but may be
  81. * changed after creation.
  82. *
  83. * Must be a combination of DRM_PVR_BO_* and/or PVR_BO_* flags.
  84. *
  85. * .. note::
  86. *
  87. * This member is declared const to indicate that none of these
  88. * options may change or be changed throughout the object's
  89. * lifetime.
  90. */
  91. u64 flags;
  92. };
  93. static_assert(offsetof(struct pvr_gem_object, base) == 0,
  94. "offsetof(struct pvr_gem_object, base) not zero");
  95. #define shmem_gem_from_pvr_gem(pvr_obj) (&(pvr_obj)->base)
  96. #define shmem_gem_to_pvr_gem(shmem_obj) container_of_const(shmem_obj, struct pvr_gem_object, base)
  97. #define gem_from_pvr_gem(pvr_obj) (&(pvr_obj)->base.base)
  98. #define gem_to_pvr_gem(gem_obj) container_of_const(gem_obj, struct pvr_gem_object, base.base)
  99. /* Functions defined in pvr_gem.c */
  100. struct drm_gem_object *pvr_gem_create_object(struct drm_device *drm_dev, size_t size);
  101. struct pvr_gem_object *pvr_gem_object_create(struct pvr_device *pvr_dev,
  102. size_t size, u64 flags);
  103. int pvr_gem_object_into_handle(struct pvr_gem_object *pvr_obj,
  104. struct pvr_file *pvr_file, u32 *handle);
  105. struct pvr_gem_object *pvr_gem_object_from_handle(struct pvr_file *pvr_file,
  106. u32 handle);
  107. static __always_inline struct sg_table *
  108. pvr_gem_object_get_pages_sgt(struct pvr_gem_object *pvr_obj)
  109. {
  110. return drm_gem_shmem_get_pages_sgt(shmem_gem_from_pvr_gem(pvr_obj));
  111. }
  112. void *pvr_gem_object_vmap(struct pvr_gem_object *pvr_obj);
  113. void pvr_gem_object_vunmap(struct pvr_gem_object *pvr_obj);
  114. int pvr_gem_get_dma_addr(struct pvr_gem_object *pvr_obj, u32 offset,
  115. dma_addr_t *dma_addr_out);
  116. /**
  117. * pvr_gem_object_get() - Acquire reference on pvr_gem_object
  118. * @pvr_obj: Pointer to object to acquire reference on.
  119. */
  120. static __always_inline void
  121. pvr_gem_object_get(struct pvr_gem_object *pvr_obj)
  122. {
  123. drm_gem_object_get(gem_from_pvr_gem(pvr_obj));
  124. }
  125. /**
  126. * pvr_gem_object_put() - Release reference on pvr_gem_object
  127. * @pvr_obj: Pointer to object to release reference on.
  128. */
  129. static __always_inline void
  130. pvr_gem_object_put(struct pvr_gem_object *pvr_obj)
  131. {
  132. drm_gem_object_put(gem_from_pvr_gem(pvr_obj));
  133. }
  134. static __always_inline size_t
  135. pvr_gem_object_size(struct pvr_gem_object *pvr_obj)
  136. {
  137. return gem_from_pvr_gem(pvr_obj)->size;
  138. }
  139. #endif /* PVR_GEM_H */