amdgpu_ring.c 13 KB

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