pvr_context.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* SPDX-License-Identifier: GPL-2.0-only OR MIT */
  2. /* Copyright (c) 2023 Imagination Technologies Ltd. */
  3. #ifndef PVR_CONTEXT_H
  4. #define PVR_CONTEXT_H
  5. #include <drm/gpu_scheduler.h>
  6. #include <linux/compiler_attributes.h>
  7. #include <linux/dma-fence.h>
  8. #include <linux/kref.h>
  9. #include <linux/types.h>
  10. #include <linux/xarray.h>
  11. #include <uapi/drm/pvr_drm.h>
  12. #include "pvr_cccb.h"
  13. #include "pvr_device.h"
  14. #include "pvr_queue.h"
  15. /* Forward declaration from pvr_gem.h. */
  16. struct pvr_fw_object;
  17. enum pvr_context_priority {
  18. PVR_CTX_PRIORITY_LOW = 0,
  19. PVR_CTX_PRIORITY_MEDIUM,
  20. PVR_CTX_PRIORITY_HIGH,
  21. };
  22. /**
  23. * struct pvr_context - Context data
  24. */
  25. struct pvr_context {
  26. /** @ref_count: Refcount for context. */
  27. struct kref ref_count;
  28. /** @pvr_dev: Pointer to owning device. */
  29. struct pvr_device *pvr_dev;
  30. /** @vm_ctx: Pointer to associated VM context. */
  31. struct pvr_vm_context *vm_ctx;
  32. /** @type: Type of context. */
  33. enum drm_pvr_ctx_type type;
  34. /** @flags: Context flags. */
  35. u32 flags;
  36. /** @priority: Context priority*/
  37. enum pvr_context_priority priority;
  38. /** @fw_obj: FW object representing FW-side context data. */
  39. struct pvr_fw_object *fw_obj;
  40. /** @data: Pointer to local copy of FW context data. */
  41. void *data;
  42. /** @data_size: Size of FW context data, in bytes. */
  43. u32 data_size;
  44. /** @ctx_id: FW context ID. */
  45. u32 ctx_id;
  46. /**
  47. * @faulty: Set to 1 when the context queues had unfinished job when
  48. * a GPU reset happened.
  49. *
  50. * In that case, the context is in an inconsistent state and can't be
  51. * used anymore.
  52. */
  53. atomic_t faulty;
  54. /** @queues: Union containing all kind of queues. */
  55. union {
  56. struct {
  57. /** @geometry: Geometry queue. */
  58. struct pvr_queue *geometry;
  59. /** @fragment: Fragment queue. */
  60. struct pvr_queue *fragment;
  61. };
  62. /** @compute: Compute queue. */
  63. struct pvr_queue *compute;
  64. /** @compute: Transfer queue. */
  65. struct pvr_queue *transfer;
  66. } queues;
  67. /** @file_link: pvr_file PVR context list link. */
  68. struct list_head file_link;
  69. };
  70. static __always_inline struct pvr_queue *
  71. pvr_context_get_queue_for_job(struct pvr_context *ctx, enum drm_pvr_job_type type)
  72. {
  73. switch (type) {
  74. case DRM_PVR_JOB_TYPE_GEOMETRY:
  75. return ctx->type == DRM_PVR_CTX_TYPE_RENDER ? ctx->queues.geometry : NULL;
  76. case DRM_PVR_JOB_TYPE_FRAGMENT:
  77. return ctx->type == DRM_PVR_CTX_TYPE_RENDER ? ctx->queues.fragment : NULL;
  78. case DRM_PVR_JOB_TYPE_COMPUTE:
  79. return ctx->type == DRM_PVR_CTX_TYPE_COMPUTE ? ctx->queues.compute : NULL;
  80. case DRM_PVR_JOB_TYPE_TRANSFER_FRAG:
  81. return ctx->type == DRM_PVR_CTX_TYPE_TRANSFER_FRAG ? ctx->queues.transfer : NULL;
  82. }
  83. return NULL;
  84. }
  85. /**
  86. * pvr_context_get() - Take additional reference on context.
  87. * @ctx: Context pointer.
  88. *
  89. * Call pvr_context_put() to release.
  90. *
  91. * Returns:
  92. * * The requested context on success, or
  93. * * %NULL if no context pointer passed.
  94. */
  95. static __always_inline struct pvr_context *
  96. pvr_context_get(struct pvr_context *ctx)
  97. {
  98. if (ctx)
  99. kref_get(&ctx->ref_count);
  100. return ctx;
  101. }
  102. /**
  103. * pvr_context_get_if_referenced() - Take an additional reference on a still
  104. * referenced context.
  105. * @ctx: Context pointer.
  106. *
  107. * Call pvr_context_put() to release.
  108. *
  109. * Returns:
  110. * * True on success, or
  111. * * false if no context pointer passed, or the context wasn't still
  112. * * referenced.
  113. */
  114. static __always_inline bool
  115. pvr_context_get_if_referenced(struct pvr_context *ctx)
  116. {
  117. return ctx != NULL && kref_get_unless_zero(&ctx->ref_count) != 0;
  118. }
  119. /**
  120. * pvr_context_lookup() - Lookup context pointer from handle and file.
  121. * @pvr_file: Pointer to pvr_file structure.
  122. * @handle: Context handle.
  123. *
  124. * Takes reference on context. Call pvr_context_put() to release.
  125. *
  126. * Return:
  127. * * The requested context on success, or
  128. * * %NULL on failure (context does not exist, or does not belong to @pvr_file).
  129. */
  130. static __always_inline struct pvr_context *
  131. pvr_context_lookup(struct pvr_file *pvr_file, u32 handle)
  132. {
  133. struct pvr_context *ctx;
  134. /* Take the array lock to protect against context removal. */
  135. xa_lock(&pvr_file->ctx_handles);
  136. ctx = pvr_context_get(xa_load(&pvr_file->ctx_handles, handle));
  137. xa_unlock(&pvr_file->ctx_handles);
  138. return ctx;
  139. }
  140. /**
  141. * pvr_context_lookup_id() - Lookup context pointer from ID.
  142. * @pvr_dev: Device pointer.
  143. * @id: FW context ID.
  144. *
  145. * Takes reference on context. Call pvr_context_put() to release.
  146. *
  147. * Return:
  148. * * The requested context on success, or
  149. * * %NULL on failure (context does not exist).
  150. */
  151. static __always_inline struct pvr_context *
  152. pvr_context_lookup_id(struct pvr_device *pvr_dev, u32 id)
  153. {
  154. struct pvr_context *ctx;
  155. /* Take the array lock to protect against context removal. */
  156. xa_lock(&pvr_dev->ctx_ids);
  157. /* Contexts are removed from the ctx_ids set in the context release path,
  158. * meaning the ref_count reached zero before they get removed. We need
  159. * to make sure we're not trying to acquire a context that's being
  160. * destroyed.
  161. */
  162. ctx = xa_load(&pvr_dev->ctx_ids, id);
  163. if (!kref_get_unless_zero(&ctx->ref_count))
  164. ctx = NULL;
  165. xa_unlock(&pvr_dev->ctx_ids);
  166. return ctx;
  167. }
  168. static __always_inline u32
  169. pvr_context_get_fw_addr(struct pvr_context *ctx)
  170. {
  171. u32 ctx_fw_addr = 0;
  172. pvr_fw_object_get_fw_addr(ctx->fw_obj, &ctx_fw_addr);
  173. return ctx_fw_addr;
  174. }
  175. void pvr_context_put(struct pvr_context *ctx);
  176. int pvr_context_create(struct pvr_file *pvr_file, struct drm_pvr_ioctl_create_context_args *args);
  177. int pvr_context_destroy(struct pvr_file *pvr_file, u32 handle);
  178. void pvr_destroy_contexts_for_file(struct pvr_file *pvr_file);
  179. void pvr_context_device_init(struct pvr_device *pvr_dev);
  180. void pvr_context_device_fini(struct pvr_device *pvr_dev);
  181. #endif /* PVR_CONTEXT_H */