amdgpu_ring.h 8.9 KB

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