amdgpu_ring.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright 2016 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Christian König
  23. */
  24. #ifndef __AMDGPU_RING_H__
  25. #define __AMDGPU_RING_H__
  26. #include <drm/amdgpu_drm.h>
  27. #include <drm/gpu_scheduler.h>
  28. #include <drm/drm_print.h>
  29. /* max number of rings */
  30. #define AMDGPU_MAX_RINGS 21
  31. #define AMDGPU_MAX_GFX_RINGS 1
  32. #define AMDGPU_MAX_COMPUTE_RINGS 8
  33. #define AMDGPU_MAX_VCE_RINGS 3
  34. #define AMDGPU_MAX_UVD_ENC_RINGS 2
  35. /* some special values for the owner field */
  36. #define AMDGPU_FENCE_OWNER_UNDEFINED ((void *)0ul)
  37. #define AMDGPU_FENCE_OWNER_VM ((void *)1ul)
  38. #define AMDGPU_FENCE_OWNER_KFD ((void *)2ul)
  39. #define AMDGPU_FENCE_FLAG_64BIT (1 << 0)
  40. #define AMDGPU_FENCE_FLAG_INT (1 << 1)
  41. #define AMDGPU_FENCE_FLAG_TC_WB_ONLY (1 << 2)
  42. #define to_amdgpu_ring(s) container_of((s), struct amdgpu_ring, sched)
  43. enum amdgpu_ring_type {
  44. AMDGPU_RING_TYPE_GFX,
  45. AMDGPU_RING_TYPE_COMPUTE,
  46. AMDGPU_RING_TYPE_SDMA,
  47. AMDGPU_RING_TYPE_UVD,
  48. AMDGPU_RING_TYPE_VCE,
  49. AMDGPU_RING_TYPE_KIQ,
  50. AMDGPU_RING_TYPE_UVD_ENC,
  51. AMDGPU_RING_TYPE_VCN_DEC,
  52. AMDGPU_RING_TYPE_VCN_ENC,
  53. AMDGPU_RING_TYPE_VCN_JPEG
  54. };
  55. struct amdgpu_device;
  56. struct amdgpu_ring;
  57. struct amdgpu_ib;
  58. struct amdgpu_cs_parser;
  59. struct amdgpu_job;
  60. /*
  61. * Fences.
  62. */
  63. struct amdgpu_fence_driver {
  64. uint64_t gpu_addr;
  65. volatile uint32_t *cpu_addr;
  66. /* sync_seq is protected by ring emission lock */
  67. uint32_t sync_seq;
  68. atomic_t last_seq;
  69. bool initialized;
  70. struct amdgpu_irq_src *irq_src;
  71. unsigned irq_type;
  72. unsigned num_fences_mask;
  73. spinlock_t lock;
  74. struct dma_fence **fences;
  75. };
  76. int amdgpu_fence_driver_init(struct amdgpu_device *adev);
  77. void amdgpu_fence_driver_fini(struct amdgpu_device *adev);
  78. void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring);
  79. int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring,
  80. unsigned num_hw_submission);
  81. int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
  82. struct amdgpu_irq_src *irq_src,
  83. unsigned irq_type);
  84. void amdgpu_fence_driver_suspend(struct amdgpu_device *adev);
  85. void amdgpu_fence_driver_resume(struct amdgpu_device *adev);
  86. int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **fence,
  87. unsigned flags);
  88. int amdgpu_fence_emit_polling(struct amdgpu_ring *ring, uint32_t *s);
  89. void amdgpu_fence_process(struct amdgpu_ring *ring);
  90. int amdgpu_fence_wait_empty(struct amdgpu_ring *ring);
  91. signed long amdgpu_fence_wait_polling(struct amdgpu_ring *ring,
  92. uint32_t wait_seq,
  93. signed long timeout);
  94. unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring);
  95. /*
  96. * Rings.
  97. */
  98. /* provided by hw blocks that expose a ring buffer for commands */
  99. struct amdgpu_ring_funcs {
  100. enum amdgpu_ring_type type;
  101. uint32_t align_mask;
  102. u32 nop;
  103. bool support_64bit_ptrs;
  104. unsigned vmhub;
  105. unsigned extra_dw;
  106. /* ring read/write ptr handling */
  107. u64 (*get_rptr)(struct amdgpu_ring *ring);
  108. u64 (*get_wptr)(struct amdgpu_ring *ring);
  109. void (*set_wptr)(struct amdgpu_ring *ring);
  110. /* validating and patching of IBs */
  111. int (*parse_cs)(struct amdgpu_cs_parser *p, uint32_t ib_idx);
  112. int (*patch_cs_in_place)(struct amdgpu_cs_parser *p, uint32_t ib_idx);
  113. /* constants to calculate how many DW are needed for an emit */
  114. unsigned emit_frame_size;
  115. unsigned emit_ib_size;
  116. /* command emit functions */
  117. void (*emit_ib)(struct amdgpu_ring *ring,
  118. struct amdgpu_ib *ib,
  119. unsigned vmid, bool ctx_switch);
  120. void (*emit_fence)(struct amdgpu_ring *ring, uint64_t addr,
  121. uint64_t seq, unsigned flags);
  122. void (*emit_pipeline_sync)(struct amdgpu_ring *ring);
  123. void (*emit_vm_flush)(struct amdgpu_ring *ring, unsigned vmid,
  124. uint64_t pd_addr);
  125. void (*emit_hdp_flush)(struct amdgpu_ring *ring);
  126. void (*emit_gds_switch)(struct amdgpu_ring *ring, uint32_t vmid,
  127. uint32_t gds_base, uint32_t gds_size,
  128. uint32_t gws_base, uint32_t gws_size,
  129. uint32_t oa_base, uint32_t oa_size);
  130. /* testing functions */
  131. int (*test_ring)(struct amdgpu_ring *ring);
  132. int (*test_ib)(struct amdgpu_ring *ring, long timeout);
  133. /* insert NOP packets */
  134. void (*insert_nop)(struct amdgpu_ring *ring, uint32_t count);
  135. void (*insert_start)(struct amdgpu_ring *ring);
  136. void (*insert_end)(struct amdgpu_ring *ring);
  137. /* pad the indirect buffer to the necessary number of dw */
  138. void (*pad_ib)(struct amdgpu_ring *ring, struct amdgpu_ib *ib);
  139. unsigned (*init_cond_exec)(struct amdgpu_ring *ring);
  140. void (*patch_cond_exec)(struct amdgpu_ring *ring, unsigned offset);
  141. /* note usage for clock and power gating */
  142. void (*begin_use)(struct amdgpu_ring *ring);
  143. void (*end_use)(struct amdgpu_ring *ring);
  144. void (*emit_switch_buffer) (struct amdgpu_ring *ring);
  145. void (*emit_cntxcntl) (struct amdgpu_ring *ring, uint32_t flags);
  146. void (*emit_rreg)(struct amdgpu_ring *ring, uint32_t reg);
  147. void (*emit_wreg)(struct amdgpu_ring *ring, uint32_t reg, uint32_t val);
  148. void (*emit_reg_wait)(struct amdgpu_ring *ring, uint32_t reg,
  149. uint32_t val, uint32_t mask);
  150. void (*emit_reg_write_reg_wait)(struct amdgpu_ring *ring,
  151. uint32_t reg0, uint32_t reg1,
  152. uint32_t ref, uint32_t mask);
  153. void (*emit_tmz)(struct amdgpu_ring *ring, bool start);
  154. /* priority functions */
  155. void (*set_priority) (struct amdgpu_ring *ring,
  156. enum drm_sched_priority priority);
  157. /* Try to soft recover the ring to make the fence signal */
  158. void (*soft_recovery)(struct amdgpu_ring *ring, unsigned vmid);
  159. };
  160. struct amdgpu_ring {
  161. struct amdgpu_device *adev;
  162. const struct amdgpu_ring_funcs *funcs;
  163. struct amdgpu_fence_driver fence_drv;
  164. struct drm_gpu_scheduler sched;
  165. struct amdgpu_bo *ring_obj;
  166. volatile uint32_t *ring;
  167. unsigned rptr_offs;
  168. u64 wptr;
  169. u64 wptr_old;
  170. unsigned ring_size;
  171. unsigned max_dw;
  172. int count_dw;
  173. uint64_t gpu_addr;
  174. uint64_t ptr_mask;
  175. uint32_t buf_mask;
  176. bool ready;
  177. u32 idx;
  178. u32 me;
  179. u32 pipe;
  180. u32 queue;
  181. struct amdgpu_bo *mqd_obj;
  182. uint64_t mqd_gpu_addr;
  183. void *mqd_ptr;
  184. uint64_t eop_gpu_addr;
  185. u32 doorbell_index;
  186. bool use_doorbell;
  187. bool use_pollmem;
  188. unsigned wptr_offs;
  189. unsigned fence_offs;
  190. uint64_t current_ctx;
  191. char name[16];
  192. unsigned cond_exe_offs;
  193. u64 cond_exe_gpu_addr;
  194. volatile u32 *cond_exe_cpu_addr;
  195. unsigned vm_inv_eng;
  196. struct dma_fence *vmid_wait;
  197. bool has_compute_vm_bug;
  198. atomic_t num_jobs[DRM_SCHED_PRIORITY_MAX];
  199. struct mutex priority_mutex;
  200. /* protected by priority_mutex */
  201. int priority;
  202. #if defined(CONFIG_DEBUG_FS)
  203. struct dentry *ent;
  204. #endif
  205. };
  206. #define amdgpu_ring_parse_cs(r, p, ib) ((r)->funcs->parse_cs((p), (ib)))
  207. #define amdgpu_ring_patch_cs_in_place(r, p, ib) ((r)->funcs->patch_cs_in_place((p), (ib)))
  208. #define amdgpu_ring_test_ring(r) (r)->funcs->test_ring((r))
  209. #define amdgpu_ring_test_ib(r, t) (r)->funcs->test_ib((r), (t))
  210. #define amdgpu_ring_get_rptr(r) (r)->funcs->get_rptr((r))
  211. #define amdgpu_ring_get_wptr(r) (r)->funcs->get_wptr((r))
  212. #define amdgpu_ring_set_wptr(r) (r)->funcs->set_wptr((r))
  213. #define amdgpu_ring_emit_ib(r, ib, vmid, c) (r)->funcs->emit_ib((r), (ib), (vmid), (c))
  214. #define amdgpu_ring_emit_pipeline_sync(r) (r)->funcs->emit_pipeline_sync((r))
  215. #define amdgpu_ring_emit_vm_flush(r, vmid, addr) (r)->funcs->emit_vm_flush((r), (vmid), (addr))
  216. #define amdgpu_ring_emit_fence(r, addr, seq, flags) (r)->funcs->emit_fence((r), (addr), (seq), (flags))
  217. #define amdgpu_ring_emit_gds_switch(r, v, db, ds, wb, ws, ab, as) (r)->funcs->emit_gds_switch((r), (v), (db), (ds), (wb), (ws), (ab), (as))
  218. #define amdgpu_ring_emit_hdp_flush(r) (r)->funcs->emit_hdp_flush((r))
  219. #define amdgpu_ring_emit_switch_buffer(r) (r)->funcs->emit_switch_buffer((r))
  220. #define amdgpu_ring_emit_cntxcntl(r, d) (r)->funcs->emit_cntxcntl((r), (d))
  221. #define amdgpu_ring_emit_rreg(r, d) (r)->funcs->emit_rreg((r), (d))
  222. #define amdgpu_ring_emit_wreg(r, d, v) (r)->funcs->emit_wreg((r), (d), (v))
  223. #define amdgpu_ring_emit_reg_wait(r, d, v, m) (r)->funcs->emit_reg_wait((r), (d), (v), (m))
  224. #define amdgpu_ring_emit_reg_write_reg_wait(r, d0, d1, v, m) (r)->funcs->emit_reg_write_reg_wait((r), (d0), (d1), (v), (m))
  225. #define amdgpu_ring_emit_tmz(r, b) (r)->funcs->emit_tmz((r), (b))
  226. #define amdgpu_ring_pad_ib(r, ib) ((r)->funcs->pad_ib((r), (ib)))
  227. #define amdgpu_ring_init_cond_exec(r) (r)->funcs->init_cond_exec((r))
  228. #define amdgpu_ring_patch_cond_exec(r,o) (r)->funcs->patch_cond_exec((r),(o))
  229. int amdgpu_ring_alloc(struct amdgpu_ring *ring, unsigned ndw);
  230. void amdgpu_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count);
  231. void amdgpu_ring_generic_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib);
  232. void amdgpu_ring_commit(struct amdgpu_ring *ring);
  233. void amdgpu_ring_undo(struct amdgpu_ring *ring);
  234. void amdgpu_ring_priority_get(struct amdgpu_ring *ring,
  235. enum drm_sched_priority priority);
  236. void amdgpu_ring_priority_put(struct amdgpu_ring *ring,
  237. enum drm_sched_priority priority);
  238. int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
  239. unsigned ring_size, struct amdgpu_irq_src *irq_src,
  240. unsigned irq_type);
  241. void amdgpu_ring_fini(struct amdgpu_ring *ring);
  242. void amdgpu_ring_emit_reg_write_reg_wait_helper(struct amdgpu_ring *ring,
  243. uint32_t reg0, uint32_t val0,
  244. uint32_t reg1, uint32_t val1);
  245. bool amdgpu_ring_soft_recovery(struct amdgpu_ring *ring, unsigned int vmid,
  246. struct dma_fence *fence);
  247. static inline void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
  248. {
  249. int i = 0;
  250. while (i <= ring->buf_mask)
  251. ring->ring[i++] = ring->funcs->nop;
  252. }
  253. static inline void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v)
  254. {
  255. if (ring->count_dw <= 0)
  256. DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
  257. ring->ring[ring->wptr++ & ring->buf_mask] = v;
  258. ring->wptr &= ring->ptr_mask;
  259. ring->count_dw--;
  260. }
  261. static inline void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
  262. void *src, int count_dw)
  263. {
  264. unsigned occupied, chunk1, chunk2;
  265. void *dst;
  266. if (unlikely(ring->count_dw < count_dw))
  267. DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
  268. occupied = ring->wptr & ring->buf_mask;
  269. dst = (void *)&ring->ring[occupied];
  270. chunk1 = ring->buf_mask + 1 - occupied;
  271. chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
  272. chunk2 = count_dw - chunk1;
  273. chunk1 <<= 2;
  274. chunk2 <<= 2;
  275. if (chunk1)
  276. memcpy(dst, src, chunk1);
  277. if (chunk2) {
  278. src += chunk1;
  279. dst = (void *)ring->ring;
  280. memcpy(dst, src, chunk2);
  281. }
  282. ring->wptr += count_dw;
  283. ring->wptr &= ring->ptr_mask;
  284. ring->count_dw -= count_dw;
  285. }
  286. #endif