amdgpu_ring.c 12 KB

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