amdgpu_ring.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Copyright 2008 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. * Copyright 2009 Jerome Glisse.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Dave Airlie
  25. * Alex Deucher
  26. * Jerome Glisse
  27. * Christian König
  28. */
  29. #include <linux/seq_file.h>
  30. #include <linux/slab.h>
  31. #include <linux/debugfs.h>
  32. #include <drm/drmP.h>
  33. #include <drm/amdgpu_drm.h>
  34. #include "amdgpu.h"
  35. #include "atom.h"
  36. /*
  37. * Rings
  38. * Most engines on the GPU are fed via ring buffers. Ring
  39. * buffers are areas of GPU accessible memory that the host
  40. * writes commands into and the GPU reads commands out of.
  41. * There is a rptr (read pointer) that determines where the
  42. * GPU is currently reading, and a wptr (write pointer)
  43. * which determines where the host has written. When the
  44. * pointers are equal, the ring is idle. When the host
  45. * writes commands to the ring buffer, it increments the
  46. * wptr. The GPU then starts fetching commands and executes
  47. * them until the pointers are equal again.
  48. */
  49. static int amdgpu_debugfs_ring_init(struct amdgpu_device *adev,
  50. struct amdgpu_ring *ring);
  51. static void amdgpu_debugfs_ring_fini(struct amdgpu_ring *ring);
  52. /**
  53. * amdgpu_ring_alloc - allocate space on the ring buffer
  54. *
  55. * @adev: amdgpu_device pointer
  56. * @ring: amdgpu_ring structure holding ring information
  57. * @ndw: number of dwords to allocate in the ring buffer
  58. *
  59. * Allocate @ndw dwords in the ring buffer (all asics).
  60. * Returns 0 on success, error on failure.
  61. */
  62. int amdgpu_ring_alloc(struct amdgpu_ring *ring, unsigned ndw)
  63. {
  64. /* Align requested size with padding so unlock_commit can
  65. * pad safely */
  66. ndw = (ndw + ring->funcs->align_mask) & ~ring->funcs->align_mask;
  67. /* Make sure we aren't trying to allocate more space
  68. * than the maximum for one submission
  69. */
  70. if (WARN_ON_ONCE(ndw > ring->max_dw))
  71. return -ENOMEM;
  72. ring->count_dw = ndw;
  73. ring->wptr_old = ring->wptr;
  74. if (ring->funcs->begin_use)
  75. ring->funcs->begin_use(ring);
  76. return 0;
  77. }
  78. /** amdgpu_ring_insert_nop - insert NOP packets
  79. *
  80. * @ring: amdgpu_ring structure holding ring information
  81. * @count: the number of NOP packets to insert
  82. *
  83. * This is the generic insert_nop function for rings except SDMA
  84. */
  85. void amdgpu_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count)
  86. {
  87. int i;
  88. for (i = 0; i < count; i++)
  89. amdgpu_ring_write(ring, ring->funcs->nop);
  90. }
  91. /** amdgpu_ring_generic_pad_ib - pad IB with NOP packets
  92. *
  93. * @ring: amdgpu_ring structure holding ring information
  94. * @ib: IB to add NOP packets to
  95. *
  96. * This is the generic pad_ib function for rings except SDMA
  97. */
  98. void amdgpu_ring_generic_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib)
  99. {
  100. while (ib->length_dw & ring->funcs->align_mask)
  101. ib->ptr[ib->length_dw++] = ring->funcs->nop;
  102. }
  103. /**
  104. * amdgpu_ring_commit - tell the GPU to execute the new
  105. * commands on the ring buffer
  106. *
  107. * @adev: amdgpu_device pointer
  108. * @ring: amdgpu_ring structure holding ring information
  109. *
  110. * Update the wptr (write pointer) to tell the GPU to
  111. * execute new commands on the ring buffer (all asics).
  112. */
  113. void amdgpu_ring_commit(struct amdgpu_ring *ring)
  114. {
  115. uint32_t count;
  116. /* We pad to match fetch size */
  117. count = ring->funcs->align_mask + 1 -
  118. (ring->wptr & ring->funcs->align_mask);
  119. count %= ring->funcs->align_mask + 1;
  120. ring->funcs->insert_nop(ring, count);
  121. mb();
  122. amdgpu_ring_set_wptr(ring);
  123. if (ring->funcs->end_use)
  124. ring->funcs->end_use(ring);
  125. }
  126. /**
  127. * amdgpu_ring_undo - reset the wptr
  128. *
  129. * @ring: amdgpu_ring structure holding ring information
  130. *
  131. * Reset the driver's copy of the wptr (all asics).
  132. */
  133. void amdgpu_ring_undo(struct amdgpu_ring *ring)
  134. {
  135. ring->wptr = ring->wptr_old;
  136. if (ring->funcs->end_use)
  137. ring->funcs->end_use(ring);
  138. }
  139. /**
  140. * amdgpu_ring_init - init driver ring struct.
  141. *
  142. * @adev: amdgpu_device pointer
  143. * @ring: amdgpu_ring structure holding ring information
  144. * @max_ndw: maximum number of dw for ring alloc
  145. * @nop: nop packet for this ring
  146. *
  147. * Initialize the driver information for the selected ring (all asics).
  148. * Returns 0 on success, error on failure.
  149. */
  150. int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
  151. unsigned max_dw, struct amdgpu_irq_src *irq_src,
  152. unsigned irq_type)
  153. {
  154. int r;
  155. if (ring->adev == NULL) {
  156. if (adev->num_rings >= AMDGPU_MAX_RINGS)
  157. return -EINVAL;
  158. ring->adev = adev;
  159. ring->idx = adev->num_rings++;
  160. adev->rings[ring->idx] = ring;
  161. r = amdgpu_fence_driver_init_ring(ring,
  162. amdgpu_sched_hw_submission);
  163. if (r)
  164. return r;
  165. }
  166. r = amdgpu_wb_get(adev, &ring->rptr_offs);
  167. if (r) {
  168. dev_err(adev->dev, "(%d) ring rptr_offs wb alloc failed\n", r);
  169. return r;
  170. }
  171. r = amdgpu_wb_get(adev, &ring->wptr_offs);
  172. if (r) {
  173. dev_err(adev->dev, "(%d) ring wptr_offs wb alloc failed\n", r);
  174. return r;
  175. }
  176. r = amdgpu_wb_get(adev, &ring->fence_offs);
  177. if (r) {
  178. dev_err(adev->dev, "(%d) ring fence_offs wb alloc failed\n", r);
  179. return r;
  180. }
  181. r = amdgpu_wb_get(adev, &ring->cond_exe_offs);
  182. if (r) {
  183. dev_err(adev->dev, "(%d) ring cond_exec_polling wb alloc failed\n", r);
  184. return r;
  185. }
  186. ring->cond_exe_gpu_addr = adev->wb.gpu_addr + (ring->cond_exe_offs * 4);
  187. ring->cond_exe_cpu_addr = &adev->wb.wb[ring->cond_exe_offs];
  188. /* always set cond_exec_polling to CONTINUE */
  189. *ring->cond_exe_cpu_addr = 1;
  190. r = amdgpu_fence_driver_start_ring(ring, irq_src, irq_type);
  191. if (r) {
  192. dev_err(adev->dev, "failed initializing fences (%d).\n", r);
  193. return r;
  194. }
  195. ring->ring_size = roundup_pow_of_two(max_dw * 4 *
  196. amdgpu_sched_hw_submission);
  197. /* Allocate ring buffer */
  198. if (ring->ring_obj == NULL) {
  199. r = amdgpu_bo_create_kernel(adev, ring->ring_size, PAGE_SIZE,
  200. AMDGPU_GEM_DOMAIN_GTT,
  201. &ring->ring_obj,
  202. &ring->gpu_addr,
  203. (void **)&ring->ring);
  204. if (r) {
  205. dev_err(adev->dev, "(%d) ring create failed\n", r);
  206. return r;
  207. }
  208. amdgpu_ring_clear_ring(ring);
  209. }
  210. ring->ptr_mask = (ring->ring_size / 4) - 1;
  211. ring->max_dw = max_dw;
  212. if (amdgpu_debugfs_ring_init(adev, ring)) {
  213. DRM_ERROR("Failed to register debugfs file for rings !\n");
  214. }
  215. return 0;
  216. }
  217. /**
  218. * amdgpu_ring_fini - tear down the driver ring struct.
  219. *
  220. * @adev: amdgpu_device pointer
  221. * @ring: amdgpu_ring structure holding ring information
  222. *
  223. * Tear down the driver information for the selected ring (all asics).
  224. */
  225. void amdgpu_ring_fini(struct amdgpu_ring *ring)
  226. {
  227. ring->ready = false;
  228. amdgpu_wb_free(ring->adev, ring->cond_exe_offs);
  229. amdgpu_wb_free(ring->adev, ring->fence_offs);
  230. amdgpu_wb_free(ring->adev, ring->rptr_offs);
  231. amdgpu_wb_free(ring->adev, ring->wptr_offs);
  232. amdgpu_bo_free_kernel(&ring->ring_obj,
  233. &ring->gpu_addr,
  234. (void **)&ring->ring);
  235. amdgpu_debugfs_ring_fini(ring);
  236. ring->adev->rings[ring->idx] = NULL;
  237. }
  238. /*
  239. * Debugfs info
  240. */
  241. #if defined(CONFIG_DEBUG_FS)
  242. /* Layout of file is 12 bytes consisting of
  243. * - rptr
  244. * - wptr
  245. * - driver's copy of wptr
  246. *
  247. * followed by n-words of ring data
  248. */
  249. static ssize_t amdgpu_debugfs_ring_read(struct file *f, char __user *buf,
  250. size_t size, loff_t *pos)
  251. {
  252. struct amdgpu_ring *ring = file_inode(f)->i_private;
  253. int r, i;
  254. uint32_t value, result, early[3];
  255. if (*pos & 3 || size & 3)
  256. return -EINVAL;
  257. result = 0;
  258. if (*pos < 12) {
  259. early[0] = amdgpu_ring_get_rptr(ring);
  260. early[1] = amdgpu_ring_get_wptr(ring);
  261. early[2] = ring->wptr;
  262. for (i = *pos / 4; i < 3 && size; i++) {
  263. r = put_user(early[i], (uint32_t *)buf);
  264. if (r)
  265. return r;
  266. buf += 4;
  267. result += 4;
  268. size -= 4;
  269. *pos += 4;
  270. }
  271. }
  272. while (size) {
  273. if (*pos >= (ring->ring_size + 12))
  274. return result;
  275. value = ring->ring[(*pos - 12)/4];
  276. r = put_user(value, (uint32_t*)buf);
  277. if (r)
  278. return r;
  279. buf += 4;
  280. result += 4;
  281. size -= 4;
  282. *pos += 4;
  283. }
  284. return result;
  285. }
  286. static const struct file_operations amdgpu_debugfs_ring_fops = {
  287. .owner = THIS_MODULE,
  288. .read = amdgpu_debugfs_ring_read,
  289. .llseek = default_llseek
  290. };
  291. #endif
  292. static int amdgpu_debugfs_ring_init(struct amdgpu_device *adev,
  293. struct amdgpu_ring *ring)
  294. {
  295. #if defined(CONFIG_DEBUG_FS)
  296. struct drm_minor *minor = adev->ddev->primary;
  297. struct dentry *ent, *root = minor->debugfs_root;
  298. char name[32];
  299. sprintf(name, "amdgpu_ring_%s", ring->name);
  300. ent = debugfs_create_file(name,
  301. S_IFREG | S_IRUGO, root,
  302. ring, &amdgpu_debugfs_ring_fops);
  303. if (!ent)
  304. return -ENOMEM;
  305. i_size_write(ent->d_inode, ring->ring_size + 12);
  306. ring->ent = ent;
  307. #endif
  308. return 0;
  309. }
  310. static void amdgpu_debugfs_ring_fini(struct amdgpu_ring *ring)
  311. {
  312. #if defined(CONFIG_DEBUG_FS)
  313. debugfs_remove(ring->ent);
  314. #endif
  315. }