amdxdna_ctx.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2022-2024, Advanced Micro Devices, Inc.
  4. */
  5. #ifndef _AMDXDNA_CTX_H_
  6. #define _AMDXDNA_CTX_H_
  7. #include <linux/bitfield.h>
  8. #include "amdxdna_gem.h"
  9. struct amdxdna_hwctx_priv;
  10. enum ert_cmd_opcode {
  11. ERT_START_CU = 0,
  12. ERT_CMD_CHAIN = 19,
  13. ERT_START_NPU = 20,
  14. ERT_START_NPU_PREEMPT = 21,
  15. ERT_START_NPU_PREEMPT_ELF = 22,
  16. ERT_INVALID_CMD = ~0U,
  17. };
  18. enum ert_cmd_state {
  19. ERT_CMD_STATE_INVALID,
  20. ERT_CMD_STATE_NEW,
  21. ERT_CMD_STATE_QUEUED,
  22. ERT_CMD_STATE_RUNNING,
  23. ERT_CMD_STATE_COMPLETED,
  24. ERT_CMD_STATE_ERROR,
  25. ERT_CMD_STATE_ABORT,
  26. ERT_CMD_STATE_SUBMITTED,
  27. ERT_CMD_STATE_TIMEOUT,
  28. ERT_CMD_STATE_NORESPONSE,
  29. };
  30. /*
  31. * Interpretation of the beginning of data payload for ERT_START_NPU in
  32. * amdxdna_cmd. The rest of the payload in amdxdna_cmd is regular kernel args.
  33. */
  34. struct amdxdna_cmd_start_npu {
  35. u64 buffer; /* instruction buffer address */
  36. u32 buffer_size; /* size of buffer in bytes */
  37. u32 prop_count; /* properties count */
  38. u32 prop_args[]; /* properties and regular kernel arguments */
  39. };
  40. /*
  41. * Interpretation of the beginning of data payload for ERT_CMD_CHAIN in
  42. * amdxdna_cmd. The rest of the payload in amdxdna_cmd is cmd BO handles.
  43. */
  44. struct amdxdna_cmd_chain {
  45. u32 command_count;
  46. u32 submit_index;
  47. u32 error_index;
  48. u32 reserved[3];
  49. u64 data[] __counted_by(command_count);
  50. };
  51. /*
  52. * Interpretation of the beginning of data payload for ERT_START_NPU_PREEMPT in
  53. * amdxdna_cmd. The rest of the payload in amdxdna_cmd is regular kernel args.
  54. */
  55. struct amdxdna_cmd_preempt_data {
  56. u64 inst_buf; /* instruction buffer address */
  57. u64 save_buf; /* save buffer address */
  58. u64 restore_buf; /* restore buffer address */
  59. u32 inst_size; /* size of instruction buffer in bytes */
  60. u32 save_size; /* size of save buffer in bytes */
  61. u32 restore_size; /* size of restore buffer in bytes */
  62. u32 inst_prop_cnt; /* properties count */
  63. u32 prop_args[]; /* properties and regular kernel arguments */
  64. };
  65. /* Exec buffer command header format */
  66. #define AMDXDNA_CMD_STATE GENMASK(3, 0)
  67. #define AMDXDNA_CMD_EXTRA_CU_MASK GENMASK(11, 10)
  68. #define AMDXDNA_CMD_COUNT GENMASK(22, 12)
  69. #define AMDXDNA_CMD_OPCODE GENMASK(27, 23)
  70. struct amdxdna_cmd {
  71. u32 header;
  72. u32 data[];
  73. };
  74. #define INVALID_CU_IDX (~0U)
  75. struct amdxdna_hwctx {
  76. struct amdxdna_client *client;
  77. struct amdxdna_hwctx_priv *priv;
  78. char *name;
  79. u32 id;
  80. u32 max_opc;
  81. u32 num_tiles;
  82. u32 mem_size;
  83. u32 fw_ctx_id;
  84. u32 col_list_len;
  85. u32 *col_list;
  86. u32 start_col;
  87. u32 num_col;
  88. u32 num_unused_col;
  89. struct amdxdna_qos_info qos;
  90. struct amdxdna_hwctx_param_config_cu *cus;
  91. u32 syncobj_hdl;
  92. atomic64_t job_submit_cnt;
  93. atomic64_t job_free_cnt ____cacheline_aligned_in_smp;
  94. };
  95. #define drm_job_to_xdna_job(j) \
  96. container_of(j, struct amdxdna_sched_job, base)
  97. enum amdxdna_job_opcode {
  98. SYNC_DEBUG_BO,
  99. ATTACH_DEBUG_BO,
  100. DETACH_DEBUG_BO,
  101. };
  102. struct amdxdna_drv_cmd {
  103. enum amdxdna_job_opcode opcode;
  104. u32 result;
  105. };
  106. struct amdxdna_sched_job {
  107. struct drm_sched_job base;
  108. struct kref refcnt;
  109. struct amdxdna_hwctx *hwctx;
  110. struct mm_struct *mm;
  111. /* The fence to notice DRM scheduler that job is done by hardware */
  112. struct dma_fence *fence;
  113. /* user can wait on this fence */
  114. struct dma_fence *out_fence;
  115. bool job_done;
  116. bool job_timeout;
  117. u64 seq;
  118. struct amdxdna_drv_cmd *drv_cmd;
  119. struct amdxdna_gem_obj *cmd_bo;
  120. size_t bo_cnt;
  121. struct drm_gem_object *bos[] __counted_by(bo_cnt);
  122. };
  123. static inline u32
  124. amdxdna_cmd_get_op(struct amdxdna_gem_obj *abo)
  125. {
  126. struct amdxdna_cmd *cmd = abo->mem.kva;
  127. return FIELD_GET(AMDXDNA_CMD_OPCODE, cmd->header);
  128. }
  129. static inline void
  130. amdxdna_cmd_set_state(struct amdxdna_gem_obj *abo, enum ert_cmd_state s)
  131. {
  132. struct amdxdna_cmd *cmd = abo->mem.kva;
  133. cmd->header &= ~AMDXDNA_CMD_STATE;
  134. cmd->header |= FIELD_PREP(AMDXDNA_CMD_STATE, s);
  135. }
  136. static inline enum ert_cmd_state
  137. amdxdna_cmd_get_state(struct amdxdna_gem_obj *abo)
  138. {
  139. struct amdxdna_cmd *cmd = abo->mem.kva;
  140. return FIELD_GET(AMDXDNA_CMD_STATE, cmd->header);
  141. }
  142. void *amdxdna_cmd_get_payload(struct amdxdna_gem_obj *abo, u32 *size);
  143. u32 amdxdna_cmd_get_cu_idx(struct amdxdna_gem_obj *abo);
  144. int amdxdna_cmd_set_error(struct amdxdna_gem_obj *abo,
  145. struct amdxdna_sched_job *job, u32 cmd_idx,
  146. enum ert_cmd_state error_state);
  147. void amdxdna_sched_job_cleanup(struct amdxdna_sched_job *job);
  148. void amdxdna_hwctx_remove_all(struct amdxdna_client *client);
  149. int amdxdna_hwctx_walk(struct amdxdna_client *client, void *arg,
  150. int (*walk)(struct amdxdna_hwctx *hwctx, void *arg));
  151. int amdxdna_hwctx_sync_debug_bo(struct amdxdna_client *client, u32 debug_bo_hdl);
  152. int amdxdna_cmd_submit(struct amdxdna_client *client,
  153. struct amdxdna_drv_cmd *drv_cmd, u32 cmd_bo_hdls,
  154. u32 *arg_bo_hdls, u32 arg_bo_cnt,
  155. u32 hwctx_hdl, u64 *seq);
  156. int amdxdna_cmd_wait(struct amdxdna_client *client, u32 hwctx_hdl,
  157. u64 seq, u32 timeout);
  158. int amdxdna_drm_create_hwctx_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
  159. int amdxdna_drm_config_hwctx_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
  160. int amdxdna_drm_destroy_hwctx_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
  161. int amdxdna_drm_submit_cmd_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
  162. #endif /* _AMDXDNA_CTX_H_ */