amdgpu_ring.c 13 KB

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