amdgpu_ring.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. enum amdgpu_ring_type {
  43. AMDGPU_RING_TYPE_GFX,
  44. AMDGPU_RING_TYPE_COMPUTE,
  45. AMDGPU_RING_TYPE_SDMA,
  46. AMDGPU_RING_TYPE_UVD,
  47. AMDGPU_RING_TYPE_VCE,
  48. AMDGPU_RING_TYPE_KIQ,
  49. AMDGPU_RING_TYPE_UVD_ENC,
  50. AMDGPU_RING_TYPE_VCN_DEC,
  51. AMDGPU_RING_TYPE_VCN_ENC
  52. };
  53. struct amdgpu_device;
  54. struct amdgpu_ring;
  55. struct amdgpu_ib;
  56. struct amdgpu_cs_parser;
  57. struct amdgpu_job;
  58. /*
  59. * Fences.
  60. */
  61. struct amdgpu_fence_driver {
  62. uint64_t gpu_addr;
  63. volatile uint32_t *cpu_addr;
  64. /* sync_seq is protected by ring emission lock */
  65. uint32_t sync_seq;
  66. atomic_t last_seq;
  67. bool initialized;
  68. struct amdgpu_irq_src *irq_src;
  69. unsigned irq_type;
  70. struct timer_list fallback_timer;
  71. unsigned num_fences_mask;
  72. spinlock_t lock;
  73. struct dma_fence **fences;
  74. };
  75. int amdgpu_fence_driver_init(struct amdgpu_device *adev);
  76. void amdgpu_fence_driver_fini(struct amdgpu_device *adev);
  77. void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring);
  78. int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring,
  79. unsigned num_hw_submission);
  80. int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
  81. struct amdgpu_irq_src *irq_src,
  82. unsigned irq_type);
  83. void amdgpu_fence_driver_suspend(struct amdgpu_device *adev);
  84. void amdgpu_fence_driver_resume(struct amdgpu_device *adev);
  85. int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **fence,
  86. unsigned flags);
  87. int amdgpu_fence_emit_polling(struct amdgpu_ring *ring, uint32_t *s);
  88. void amdgpu_fence_process(struct amdgpu_ring *ring);
  89. int amdgpu_fence_wait_empty(struct amdgpu_ring *ring);
  90. signed long amdgpu_fence_wait_polling(struct amdgpu_ring *ring,
  91. uint32_t wait_seq,
  92. signed long timeout);
  93. unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring);
  94. /*
  95. * Rings.
  96. */
  97. /* provided by hw blocks that expose a ring buffer for commands */
  98. struct amdgpu_ring_funcs {
  99. enum amdgpu_ring_type type;
  100. uint32_t align_mask;
  101. u32 nop;
  102. bool support_64bit_ptrs;
  103. unsigned vmhub;
  104. /* ring read/write ptr handling */
  105. u64 (*get_rptr)(struct amdgpu_ring *ring);
  106. u64 (*get_wptr)(struct amdgpu_ring *ring);
  107. void (*set_wptr)(struct amdgpu_ring *ring);
  108. /* validating and patching of IBs */
  109. int (*parse_cs)(struct amdgpu_cs_parser *p, uint32_t ib_idx);
  110. /* constants to calculate how many DW are needed for an emit */
  111. unsigned emit_frame_size;
  112. unsigned emit_ib_size;
  113. /* command emit functions */
  114. void (*emit_ib)(struct amdgpu_ring *ring,
  115. struct amdgpu_ib *ib,
  116. unsigned vmid, bool ctx_switch);
  117. void (*emit_fence)(struct amdgpu_ring *ring, uint64_t addr,
  118. uint64_t seq, unsigned flags);
  119. void (*emit_pipeline_sync)(struct amdgpu_ring *ring);
  120. void (*emit_vm_flush)(struct amdgpu_ring *ring, unsigned vmid,
  121. uint64_t pd_addr);
  122. void (*emit_hdp_flush)(struct amdgpu_ring *ring);
  123. void (*emit_gds_switch)(struct amdgpu_ring *ring, uint32_t vmid,
  124. uint32_t gds_base, uint32_t gds_size,
  125. uint32_t gws_base, uint32_t gws_size,
  126. uint32_t oa_base, uint32_t oa_size);
  127. /* testing functions */
  128. int (*test_ring)(struct amdgpu_ring *ring);
  129. int (*test_ib)(struct amdgpu_ring *ring, long timeout);
  130. /* insert NOP packets */
  131. void (*insert_nop)(struct amdgpu_ring *ring, uint32_t count);
  132. void (*insert_start)(struct amdgpu_ring *ring);
  133. void (*insert_end)(struct amdgpu_ring *ring);
  134. /* pad the indirect buffer to the necessary number of dw */
  135. void (*pad_ib)(struct amdgpu_ring *ring, struct amdgpu_ib *ib);
  136. unsigned (*init_cond_exec)(struct amdgpu_ring *ring);
  137. void (*patch_cond_exec)(struct amdgpu_ring *ring, unsigned offset);
  138. /* note usage for clock and power gating */
  139. void (*begin_use)(struct amdgpu_ring *ring);
  140. void (*end_use)(struct amdgpu_ring *ring);
  141. void (*emit_switch_buffer) (struct amdgpu_ring *ring);
  142. void (*emit_cntxcntl) (struct amdgpu_ring *ring, uint32_t flags);
  143. void (*emit_rreg)(struct amdgpu_ring *ring, uint32_t reg);
  144. void (*emit_wreg)(struct amdgpu_ring *ring, uint32_t reg, uint32_t val);
  145. void (*emit_reg_wait)(struct amdgpu_ring *ring, uint32_t reg,
  146. uint32_t val, uint32_t mask);
  147. void (*emit_reg_write_reg_wait)(struct amdgpu_ring *ring,
  148. uint32_t reg0, uint32_t reg1,
  149. uint32_t ref, uint32_t mask);
  150. void (*emit_tmz)(struct amdgpu_ring *ring, bool start);
  151. /* priority functions */
  152. void (*set_priority) (struct amdgpu_ring *ring,
  153. enum drm_sched_priority priority);
  154. };
  155. struct amdgpu_ring {
  156. struct amdgpu_device *adev;
  157. const struct amdgpu_ring_funcs *funcs;
  158. struct amdgpu_fence_driver fence_drv;
  159. struct drm_gpu_scheduler sched;
  160. struct list_head lru_list;
  161. struct amdgpu_bo *ring_obj;
  162. volatile uint32_t *ring;
  163. unsigned rptr_offs;
  164. u64 wptr;
  165. u64 wptr_old;
  166. unsigned ring_size;
  167. unsigned max_dw;
  168. int count_dw;
  169. uint64_t gpu_addr;
  170. uint64_t ptr_mask;
  171. uint32_t buf_mask;
  172. bool ready;
  173. u32 idx;
  174. u32 me;
  175. u32 pipe;
  176. u32 queue;
  177. struct amdgpu_bo *mqd_obj;
  178. uint64_t mqd_gpu_addr;
  179. void *mqd_ptr;
  180. uint64_t eop_gpu_addr;
  181. u32 doorbell_index;
  182. bool use_doorbell;
  183. bool use_pollmem;
  184. unsigned wptr_offs;
  185. unsigned fence_offs;
  186. uint64_t current_ctx;
  187. char name[16];
  188. unsigned cond_exe_offs;
  189. u64 cond_exe_gpu_addr;
  190. volatile u32 *cond_exe_cpu_addr;
  191. unsigned vm_inv_eng;
  192. struct dma_fence *vmid_wait;
  193. bool has_compute_vm_bug;
  194. atomic_t num_jobs[DRM_SCHED_PRIORITY_MAX];
  195. struct mutex priority_mutex;
  196. /* protected by priority_mutex */
  197. int priority;
  198. #if defined(CONFIG_DEBUG_FS)
  199. struct dentry *ent;
  200. #endif
  201. };
  202. int amdgpu_ring_alloc(struct amdgpu_ring *ring, unsigned ndw);
  203. void amdgpu_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count);
  204. void amdgpu_ring_generic_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib);
  205. void amdgpu_ring_commit(struct amdgpu_ring *ring);
  206. void amdgpu_ring_undo(struct amdgpu_ring *ring);
  207. void amdgpu_ring_priority_get(struct amdgpu_ring *ring,
  208. enum drm_sched_priority priority);
  209. void amdgpu_ring_priority_put(struct amdgpu_ring *ring,
  210. enum drm_sched_priority priority);
  211. int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
  212. unsigned ring_size, struct amdgpu_irq_src *irq_src,
  213. unsigned irq_type);
  214. void amdgpu_ring_fini(struct amdgpu_ring *ring);
  215. int amdgpu_ring_lru_get(struct amdgpu_device *adev, int type,
  216. int *blacklist, int num_blacklist,
  217. bool lru_pipe_order, struct amdgpu_ring **ring);
  218. void amdgpu_ring_lru_touch(struct amdgpu_device *adev, struct amdgpu_ring *ring);
  219. void amdgpu_ring_emit_reg_write_reg_wait_helper(struct amdgpu_ring *ring,
  220. uint32_t reg0, uint32_t val0,
  221. uint32_t reg1, uint32_t val1);
  222. static inline void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
  223. {
  224. int i = 0;
  225. while (i <= ring->buf_mask)
  226. ring->ring[i++] = ring->funcs->nop;
  227. }
  228. static inline void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v)
  229. {
  230. if (ring->count_dw <= 0)
  231. DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
  232. ring->ring[ring->wptr++ & ring->buf_mask] = v;
  233. ring->wptr &= ring->ptr_mask;
  234. ring->count_dw--;
  235. }
  236. static inline void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
  237. void *src, int count_dw)
  238. {
  239. unsigned occupied, chunk1, chunk2;
  240. void *dst;
  241. if (unlikely(ring->count_dw < count_dw))
  242. DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
  243. occupied = ring->wptr & ring->buf_mask;
  244. dst = (void *)&ring->ring[occupied];
  245. chunk1 = ring->buf_mask + 1 - occupied;
  246. chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
  247. chunk2 = count_dw - chunk1;
  248. chunk1 <<= 2;
  249. chunk2 <<= 2;
  250. if (chunk1)
  251. memcpy(dst, src, chunk1);
  252. if (chunk2) {
  253. src += chunk1;
  254. dst = (void *)ring->ring;
  255. memcpy(dst, src, chunk2);
  256. }
  257. ring->wptr += count_dw;
  258. ring->wptr &= ring->ptr_mask;
  259. ring->count_dw -= count_dw;
  260. }
  261. #endif