amdgpu_ring.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 <drm/drmP.h>
  32. #include <drm/amdgpu_drm.h>
  33. #include "amdgpu.h"
  34. #include "atom.h"
  35. /*
  36. * Rings
  37. * Most engines on the GPU are fed via ring buffers. Ring
  38. * buffers are areas of GPU accessible memory that the host
  39. * writes commands into and the GPU reads commands out of.
  40. * There is a rptr (read pointer) that determines where the
  41. * GPU is currently reading, and a wptr (write pointer)
  42. * which determines where the host has written. When the
  43. * pointers are equal, the ring is idle. When the host
  44. * writes commands to the ring buffer, it increments the
  45. * wptr. The GPU then starts fetching commands and executes
  46. * them until the pointers are equal again.
  47. */
  48. static int amdgpu_debugfs_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring);
  49. /**
  50. * amdgpu_ring_alloc - allocate space on the ring buffer
  51. *
  52. * @adev: amdgpu_device pointer
  53. * @ring: amdgpu_ring structure holding ring information
  54. * @ndw: number of dwords to allocate in the ring buffer
  55. *
  56. * Allocate @ndw dwords in the ring buffer (all asics).
  57. * Returns 0 on success, error on failure.
  58. */
  59. int amdgpu_ring_alloc(struct amdgpu_ring *ring, unsigned ndw)
  60. {
  61. /* Align requested size with padding so unlock_commit can
  62. * pad safely */
  63. ndw = (ndw + ring->align_mask) & ~ring->align_mask;
  64. /* Make sure we aren't trying to allocate more space
  65. * than the maximum for one submission
  66. */
  67. if (WARN_ON_ONCE(ndw > ring->max_dw))
  68. return -ENOMEM;
  69. ring->count_dw = ndw;
  70. ring->wptr_old = ring->wptr;
  71. return 0;
  72. }
  73. /** amdgpu_ring_insert_nop - insert NOP packets
  74. *
  75. * @ring: amdgpu_ring structure holding ring information
  76. * @count: the number of NOP packets to insert
  77. *
  78. * This is the generic insert_nop function for rings except SDMA
  79. */
  80. void amdgpu_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count)
  81. {
  82. int i;
  83. for (i = 0; i < count; i++)
  84. amdgpu_ring_write(ring, ring->nop);
  85. }
  86. /**
  87. * amdgpu_ring_commit - tell the GPU to execute the new
  88. * commands on the ring buffer
  89. *
  90. * @adev: amdgpu_device pointer
  91. * @ring: amdgpu_ring structure holding ring information
  92. *
  93. * Update the wptr (write pointer) to tell the GPU to
  94. * execute new commands on the ring buffer (all asics).
  95. */
  96. void amdgpu_ring_commit(struct amdgpu_ring *ring)
  97. {
  98. uint32_t count;
  99. /* We pad to match fetch size */
  100. count = ring->align_mask + 1 - (ring->wptr & ring->align_mask);
  101. count %= ring->align_mask + 1;
  102. ring->funcs->insert_nop(ring, count);
  103. mb();
  104. amdgpu_ring_set_wptr(ring);
  105. }
  106. /**
  107. * amdgpu_ring_undo - reset the wptr
  108. *
  109. * @ring: amdgpu_ring structure holding ring information
  110. *
  111. * Reset the driver's copy of the wptr (all asics).
  112. */
  113. void amdgpu_ring_undo(struct amdgpu_ring *ring)
  114. {
  115. ring->wptr = ring->wptr_old;
  116. }
  117. /**
  118. * amdgpu_ring_backup - Back up the content of a ring
  119. *
  120. * @ring: the ring we want to back up
  121. *
  122. * Saves all unprocessed commits from a ring, returns the number of dwords saved.
  123. */
  124. unsigned amdgpu_ring_backup(struct amdgpu_ring *ring,
  125. uint32_t **data)
  126. {
  127. unsigned size, ptr, i;
  128. *data = NULL;
  129. if (ring->ring_obj == NULL)
  130. return 0;
  131. /* it doesn't make sense to save anything if all fences are signaled */
  132. if (!amdgpu_fence_count_emitted(ring))
  133. return 0;
  134. ptr = le32_to_cpu(*ring->next_rptr_cpu_addr);
  135. size = ring->wptr + (ring->ring_size / 4);
  136. size -= ptr;
  137. size &= ring->ptr_mask;
  138. if (size == 0)
  139. return 0;
  140. /* and then save the content of the ring */
  141. *data = kmalloc_array(size, sizeof(uint32_t), GFP_KERNEL);
  142. if (!*data)
  143. return 0;
  144. for (i = 0; i < size; ++i) {
  145. (*data)[i] = ring->ring[ptr++];
  146. ptr &= ring->ptr_mask;
  147. }
  148. return size;
  149. }
  150. /**
  151. * amdgpu_ring_restore - append saved commands to the ring again
  152. *
  153. * @ring: ring to append commands to
  154. * @size: number of dwords we want to write
  155. * @data: saved commands
  156. *
  157. * Allocates space on the ring and restore the previously saved commands.
  158. */
  159. int amdgpu_ring_restore(struct amdgpu_ring *ring,
  160. unsigned size, uint32_t *data)
  161. {
  162. int i, r;
  163. if (!size || !data)
  164. return 0;
  165. /* restore the saved ring content */
  166. r = amdgpu_ring_alloc(ring, size);
  167. if (r)
  168. return r;
  169. for (i = 0; i < size; ++i) {
  170. amdgpu_ring_write(ring, data[i]);
  171. }
  172. amdgpu_ring_commit(ring);
  173. kfree(data);
  174. return 0;
  175. }
  176. /**
  177. * amdgpu_ring_init - init driver ring struct.
  178. *
  179. * @adev: amdgpu_device pointer
  180. * @ring: amdgpu_ring structure holding ring information
  181. * @ring_size: size of the ring
  182. * @nop: nop packet for this ring
  183. *
  184. * Initialize the driver information for the selected ring (all asics).
  185. * Returns 0 on success, error on failure.
  186. */
  187. int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
  188. unsigned ring_size, u32 nop, u32 align_mask,
  189. struct amdgpu_irq_src *irq_src, unsigned irq_type,
  190. enum amdgpu_ring_type ring_type)
  191. {
  192. u32 rb_bufsz;
  193. int r;
  194. if (ring->adev == NULL) {
  195. if (adev->num_rings >= AMDGPU_MAX_RINGS)
  196. return -EINVAL;
  197. ring->adev = adev;
  198. ring->idx = adev->num_rings++;
  199. adev->rings[ring->idx] = ring;
  200. r = amdgpu_fence_driver_init_ring(ring);
  201. if (r)
  202. return r;
  203. }
  204. r = amdgpu_wb_get(adev, &ring->rptr_offs);
  205. if (r) {
  206. dev_err(adev->dev, "(%d) ring rptr_offs wb alloc failed\n", r);
  207. return r;
  208. }
  209. r = amdgpu_wb_get(adev, &ring->wptr_offs);
  210. if (r) {
  211. dev_err(adev->dev, "(%d) ring wptr_offs wb alloc failed\n", r);
  212. return r;
  213. }
  214. r = amdgpu_wb_get(adev, &ring->fence_offs);
  215. if (r) {
  216. dev_err(adev->dev, "(%d) ring fence_offs wb alloc failed\n", r);
  217. return r;
  218. }
  219. r = amdgpu_wb_get(adev, &ring->next_rptr_offs);
  220. if (r) {
  221. dev_err(adev->dev, "(%d) ring next_rptr wb alloc failed\n", r);
  222. return r;
  223. }
  224. ring->next_rptr_gpu_addr = adev->wb.gpu_addr + (ring->next_rptr_offs * 4);
  225. ring->next_rptr_cpu_addr = &adev->wb.wb[ring->next_rptr_offs];
  226. spin_lock_init(&ring->fence_lock);
  227. r = amdgpu_fence_driver_start_ring(ring, irq_src, irq_type);
  228. if (r) {
  229. dev_err(adev->dev, "failed initializing fences (%d).\n", r);
  230. return r;
  231. }
  232. /* Align ring size */
  233. rb_bufsz = order_base_2(ring_size / 8);
  234. ring_size = (1 << (rb_bufsz + 1)) * 4;
  235. ring->ring_size = ring_size;
  236. ring->align_mask = align_mask;
  237. ring->nop = nop;
  238. ring->type = ring_type;
  239. /* Allocate ring buffer */
  240. if (ring->ring_obj == NULL) {
  241. r = amdgpu_bo_create(adev, ring->ring_size, PAGE_SIZE, true,
  242. AMDGPU_GEM_DOMAIN_GTT, 0,
  243. NULL, NULL, &ring->ring_obj);
  244. if (r) {
  245. dev_err(adev->dev, "(%d) ring create failed\n", r);
  246. return r;
  247. }
  248. r = amdgpu_bo_reserve(ring->ring_obj, false);
  249. if (unlikely(r != 0))
  250. return r;
  251. r = amdgpu_bo_pin(ring->ring_obj, AMDGPU_GEM_DOMAIN_GTT,
  252. &ring->gpu_addr);
  253. if (r) {
  254. amdgpu_bo_unreserve(ring->ring_obj);
  255. dev_err(adev->dev, "(%d) ring pin failed\n", r);
  256. return r;
  257. }
  258. r = amdgpu_bo_kmap(ring->ring_obj,
  259. (void **)&ring->ring);
  260. amdgpu_bo_unreserve(ring->ring_obj);
  261. if (r) {
  262. dev_err(adev->dev, "(%d) ring map failed\n", r);
  263. return r;
  264. }
  265. }
  266. ring->ptr_mask = (ring->ring_size / 4) - 1;
  267. ring->max_dw = DIV_ROUND_UP(ring->ring_size / 4,
  268. amdgpu_sched_hw_submission);
  269. if (amdgpu_debugfs_ring_init(adev, ring)) {
  270. DRM_ERROR("Failed to register debugfs file for rings !\n");
  271. }
  272. return 0;
  273. }
  274. /**
  275. * amdgpu_ring_fini - tear down the driver ring struct.
  276. *
  277. * @adev: amdgpu_device pointer
  278. * @ring: amdgpu_ring structure holding ring information
  279. *
  280. * Tear down the driver information for the selected ring (all asics).
  281. */
  282. void amdgpu_ring_fini(struct amdgpu_ring *ring)
  283. {
  284. int r;
  285. struct amdgpu_bo *ring_obj;
  286. ring_obj = ring->ring_obj;
  287. ring->ready = false;
  288. ring->ring = NULL;
  289. ring->ring_obj = NULL;
  290. amdgpu_wb_free(ring->adev, ring->fence_offs);
  291. amdgpu_wb_free(ring->adev, ring->rptr_offs);
  292. amdgpu_wb_free(ring->adev, ring->wptr_offs);
  293. amdgpu_wb_free(ring->adev, ring->next_rptr_offs);
  294. if (ring_obj) {
  295. r = amdgpu_bo_reserve(ring_obj, false);
  296. if (likely(r == 0)) {
  297. amdgpu_bo_kunmap(ring_obj);
  298. amdgpu_bo_unpin(ring_obj);
  299. amdgpu_bo_unreserve(ring_obj);
  300. }
  301. amdgpu_bo_unref(&ring_obj);
  302. }
  303. }
  304. /**
  305. * amdgpu_ring_from_fence - get ring from fence
  306. *
  307. * @f: fence structure
  308. *
  309. * Extract the ring a fence belongs to. Handles both scheduler as
  310. * well as hardware fences.
  311. */
  312. struct amdgpu_ring *amdgpu_ring_from_fence(struct fence *f)
  313. {
  314. struct amdgpu_fence *a_fence;
  315. struct amd_sched_fence *s_fence;
  316. s_fence = to_amd_sched_fence(f);
  317. if (s_fence)
  318. return container_of(s_fence->sched, struct amdgpu_ring, sched);
  319. a_fence = to_amdgpu_fence(f);
  320. if (a_fence)
  321. return a_fence->ring;
  322. return NULL;
  323. }
  324. /*
  325. * Debugfs info
  326. */
  327. #if defined(CONFIG_DEBUG_FS)
  328. static int amdgpu_debugfs_ring_info(struct seq_file *m, void *data)
  329. {
  330. struct drm_info_node *node = (struct drm_info_node *) m->private;
  331. struct drm_device *dev = node->minor->dev;
  332. struct amdgpu_device *adev = dev->dev_private;
  333. int roffset = *(int*)node->info_ent->data;
  334. struct amdgpu_ring *ring = (void *)(((uint8_t*)adev) + roffset);
  335. uint32_t rptr, wptr, rptr_next;
  336. unsigned i;
  337. wptr = amdgpu_ring_get_wptr(ring);
  338. seq_printf(m, "wptr: 0x%08x [%5d]\n", wptr, wptr);
  339. rptr = amdgpu_ring_get_rptr(ring);
  340. rptr_next = le32_to_cpu(*ring->next_rptr_cpu_addr);
  341. seq_printf(m, "rptr: 0x%08x [%5d]\n", rptr, rptr);
  342. seq_printf(m, "driver's copy of the wptr: 0x%08x [%5d]\n",
  343. ring->wptr, ring->wptr);
  344. if (!ring->ready)
  345. return 0;
  346. /* print 8 dw before current rptr as often it's the last executed
  347. * packet that is the root issue
  348. */
  349. i = (rptr + ring->ptr_mask + 1 - 32) & ring->ptr_mask;
  350. while (i != rptr) {
  351. seq_printf(m, "r[%5d]=0x%08x", i, ring->ring[i]);
  352. if (i == rptr)
  353. seq_puts(m, " *");
  354. if (i == rptr_next)
  355. seq_puts(m, " #");
  356. seq_puts(m, "\n");
  357. i = (i + 1) & ring->ptr_mask;
  358. }
  359. while (i != wptr) {
  360. seq_printf(m, "r[%5d]=0x%08x", i, ring->ring[i]);
  361. if (i == rptr)
  362. seq_puts(m, " *");
  363. if (i == rptr_next)
  364. seq_puts(m, " #");
  365. seq_puts(m, "\n");
  366. i = (i + 1) & ring->ptr_mask;
  367. }
  368. return 0;
  369. }
  370. /* TODO: clean this up !*/
  371. static int amdgpu_gfx_index = offsetof(struct amdgpu_device, gfx.gfx_ring[0]);
  372. static int cayman_cp1_index = offsetof(struct amdgpu_device, gfx.compute_ring[0]);
  373. static int cayman_cp2_index = offsetof(struct amdgpu_device, gfx.compute_ring[1]);
  374. static int amdgpu_dma1_index = offsetof(struct amdgpu_device, sdma.instance[0].ring);
  375. static int amdgpu_dma2_index = offsetof(struct amdgpu_device, sdma.instance[1].ring);
  376. static int r600_uvd_index = offsetof(struct amdgpu_device, uvd.ring);
  377. static int si_vce1_index = offsetof(struct amdgpu_device, vce.ring[0]);
  378. static int si_vce2_index = offsetof(struct amdgpu_device, vce.ring[1]);
  379. static struct drm_info_list amdgpu_debugfs_ring_info_list[] = {
  380. {"amdgpu_ring_gfx", amdgpu_debugfs_ring_info, 0, &amdgpu_gfx_index},
  381. {"amdgpu_ring_cp1", amdgpu_debugfs_ring_info, 0, &cayman_cp1_index},
  382. {"amdgpu_ring_cp2", amdgpu_debugfs_ring_info, 0, &cayman_cp2_index},
  383. {"amdgpu_ring_dma1", amdgpu_debugfs_ring_info, 0, &amdgpu_dma1_index},
  384. {"amdgpu_ring_dma2", amdgpu_debugfs_ring_info, 0, &amdgpu_dma2_index},
  385. {"amdgpu_ring_uvd", amdgpu_debugfs_ring_info, 0, &r600_uvd_index},
  386. {"amdgpu_ring_vce1", amdgpu_debugfs_ring_info, 0, &si_vce1_index},
  387. {"amdgpu_ring_vce2", amdgpu_debugfs_ring_info, 0, &si_vce2_index},
  388. };
  389. #endif
  390. static int amdgpu_debugfs_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring)
  391. {
  392. #if defined(CONFIG_DEBUG_FS)
  393. unsigned i;
  394. for (i = 0; i < ARRAY_SIZE(amdgpu_debugfs_ring_info_list); ++i) {
  395. struct drm_info_list *info = &amdgpu_debugfs_ring_info_list[i];
  396. int roffset = *(int*)amdgpu_debugfs_ring_info_list[i].data;
  397. struct amdgpu_ring *other = (void *)(((uint8_t*)adev) + roffset);
  398. unsigned r;
  399. if (other != ring)
  400. continue;
  401. r = amdgpu_debugfs_add_files(adev, info, 1);
  402. if (r)
  403. return r;
  404. }
  405. #endif
  406. return 0;
  407. }