amdgpu_ring.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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->align_mask) & ~ring->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. return 0;
  75. }
  76. /** amdgpu_ring_insert_nop - insert NOP packets
  77. *
  78. * @ring: amdgpu_ring structure holding ring information
  79. * @count: the number of NOP packets to insert
  80. *
  81. * This is the generic insert_nop function for rings except SDMA
  82. */
  83. void amdgpu_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count)
  84. {
  85. int i;
  86. for (i = 0; i < count; i++)
  87. amdgpu_ring_write(ring, ring->nop);
  88. }
  89. /** amdgpu_ring_generic_pad_ib - pad IB with NOP packets
  90. *
  91. * @ring: amdgpu_ring structure holding ring information
  92. * @ib: IB to add NOP packets to
  93. *
  94. * This is the generic pad_ib function for rings except SDMA
  95. */
  96. void amdgpu_ring_generic_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib)
  97. {
  98. while (ib->length_dw & ring->align_mask)
  99. ib->ptr[ib->length_dw++] = ring->nop;
  100. }
  101. /**
  102. * amdgpu_ring_commit - tell the GPU to execute the new
  103. * commands on the ring buffer
  104. *
  105. * @adev: amdgpu_device pointer
  106. * @ring: amdgpu_ring structure holding ring information
  107. *
  108. * Update the wptr (write pointer) to tell the GPU to
  109. * execute new commands on the ring buffer (all asics).
  110. */
  111. void amdgpu_ring_commit(struct amdgpu_ring *ring)
  112. {
  113. uint32_t count;
  114. /* We pad to match fetch size */
  115. count = ring->align_mask + 1 - (ring->wptr & ring->align_mask);
  116. count %= ring->align_mask + 1;
  117. ring->funcs->insert_nop(ring, count);
  118. mb();
  119. amdgpu_ring_set_wptr(ring);
  120. }
  121. /**
  122. * amdgpu_ring_undo - reset the wptr
  123. *
  124. * @ring: amdgpu_ring structure holding ring information
  125. *
  126. * Reset the driver's copy of the wptr (all asics).
  127. */
  128. void amdgpu_ring_undo(struct amdgpu_ring *ring)
  129. {
  130. ring->wptr = ring->wptr_old;
  131. }
  132. /**
  133. * amdgpu_ring_backup - Back up the content of a ring
  134. *
  135. * @ring: the ring we want to back up
  136. *
  137. * Saves all unprocessed commits from a ring, returns the number of dwords saved.
  138. */
  139. unsigned amdgpu_ring_backup(struct amdgpu_ring *ring,
  140. uint32_t **data)
  141. {
  142. unsigned size, ptr, i;
  143. *data = NULL;
  144. if (ring->ring_obj == NULL)
  145. return 0;
  146. /* it doesn't make sense to save anything if all fences are signaled */
  147. if (!amdgpu_fence_count_emitted(ring))
  148. return 0;
  149. ptr = le32_to_cpu(*ring->next_rptr_cpu_addr);
  150. size = ring->wptr + (ring->ring_size / 4);
  151. size -= ptr;
  152. size &= ring->ptr_mask;
  153. if (size == 0)
  154. return 0;
  155. /* and then save the content of the ring */
  156. *data = kmalloc_array(size, sizeof(uint32_t), GFP_KERNEL);
  157. if (!*data)
  158. return 0;
  159. for (i = 0; i < size; ++i) {
  160. (*data)[i] = ring->ring[ptr++];
  161. ptr &= ring->ptr_mask;
  162. }
  163. return size;
  164. }
  165. /**
  166. * amdgpu_ring_restore - append saved commands to the ring again
  167. *
  168. * @ring: ring to append commands to
  169. * @size: number of dwords we want to write
  170. * @data: saved commands
  171. *
  172. * Allocates space on the ring and restore the previously saved commands.
  173. */
  174. int amdgpu_ring_restore(struct amdgpu_ring *ring,
  175. unsigned size, uint32_t *data)
  176. {
  177. int i, r;
  178. if (!size || !data)
  179. return 0;
  180. /* restore the saved ring content */
  181. r = amdgpu_ring_alloc(ring, size);
  182. if (r)
  183. return r;
  184. for (i = 0; i < size; ++i) {
  185. amdgpu_ring_write(ring, data[i]);
  186. }
  187. amdgpu_ring_commit(ring);
  188. kfree(data);
  189. return 0;
  190. }
  191. /**
  192. * amdgpu_ring_init - init driver ring struct.
  193. *
  194. * @adev: amdgpu_device pointer
  195. * @ring: amdgpu_ring structure holding ring information
  196. * @max_ndw: maximum number of dw for ring alloc
  197. * @nop: nop packet for this ring
  198. *
  199. * Initialize the driver information for the selected ring (all asics).
  200. * Returns 0 on success, error on failure.
  201. */
  202. int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
  203. unsigned max_dw, u32 nop, u32 align_mask,
  204. struct amdgpu_irq_src *irq_src, unsigned irq_type,
  205. enum amdgpu_ring_type ring_type)
  206. {
  207. int r;
  208. if (ring->adev == NULL) {
  209. if (adev->num_rings >= AMDGPU_MAX_RINGS)
  210. return -EINVAL;
  211. ring->adev = adev;
  212. ring->idx = adev->num_rings++;
  213. adev->rings[ring->idx] = ring;
  214. r = amdgpu_fence_driver_init_ring(ring,
  215. amdgpu_sched_hw_submission);
  216. if (r)
  217. return r;
  218. }
  219. r = amdgpu_wb_get(adev, &ring->rptr_offs);
  220. if (r) {
  221. dev_err(adev->dev, "(%d) ring rptr_offs wb alloc failed\n", r);
  222. return r;
  223. }
  224. r = amdgpu_wb_get(adev, &ring->wptr_offs);
  225. if (r) {
  226. dev_err(adev->dev, "(%d) ring wptr_offs wb alloc failed\n", r);
  227. return r;
  228. }
  229. r = amdgpu_wb_get(adev, &ring->fence_offs);
  230. if (r) {
  231. dev_err(adev->dev, "(%d) ring fence_offs wb alloc failed\n", r);
  232. return r;
  233. }
  234. r = amdgpu_wb_get(adev, &ring->next_rptr_offs);
  235. if (r) {
  236. dev_err(adev->dev, "(%d) ring next_rptr wb alloc failed\n", r);
  237. return r;
  238. }
  239. ring->next_rptr_gpu_addr = adev->wb.gpu_addr + ring->next_rptr_offs * 4;
  240. ring->next_rptr_cpu_addr = &adev->wb.wb[ring->next_rptr_offs];
  241. r = amdgpu_wb_get(adev, &ring->cond_exe_offs);
  242. if (r) {
  243. dev_err(adev->dev, "(%d) ring cond_exec_polling wb alloc failed\n", r);
  244. return r;
  245. }
  246. ring->cond_exe_gpu_addr = adev->wb.gpu_addr + (ring->cond_exe_offs * 4);
  247. ring->cond_exe_cpu_addr = &adev->wb.wb[ring->cond_exe_offs];
  248. spin_lock_init(&ring->fence_lock);
  249. r = amdgpu_fence_driver_start_ring(ring, irq_src, irq_type);
  250. if (r) {
  251. dev_err(adev->dev, "failed initializing fences (%d).\n", r);
  252. return r;
  253. }
  254. ring->ring_size = roundup_pow_of_two(max_dw * 4 *
  255. amdgpu_sched_hw_submission);
  256. ring->align_mask = align_mask;
  257. ring->nop = nop;
  258. ring->type = ring_type;
  259. /* Allocate ring buffer */
  260. if (ring->ring_obj == NULL) {
  261. r = amdgpu_bo_create(adev, ring->ring_size, PAGE_SIZE, true,
  262. AMDGPU_GEM_DOMAIN_GTT, 0,
  263. NULL, NULL, &ring->ring_obj);
  264. if (r) {
  265. dev_err(adev->dev, "(%d) ring create failed\n", r);
  266. return r;
  267. }
  268. r = amdgpu_bo_reserve(ring->ring_obj, false);
  269. if (unlikely(r != 0))
  270. return r;
  271. r = amdgpu_bo_pin(ring->ring_obj, AMDGPU_GEM_DOMAIN_GTT,
  272. &ring->gpu_addr);
  273. if (r) {
  274. amdgpu_bo_unreserve(ring->ring_obj);
  275. dev_err(adev->dev, "(%d) ring pin failed\n", r);
  276. return r;
  277. }
  278. r = amdgpu_bo_kmap(ring->ring_obj,
  279. (void **)&ring->ring);
  280. memset((void *)ring->ring, 0, ring->ring_size);
  281. amdgpu_bo_unreserve(ring->ring_obj);
  282. if (r) {
  283. dev_err(adev->dev, "(%d) ring map failed\n", r);
  284. return r;
  285. }
  286. }
  287. ring->ptr_mask = (ring->ring_size / 4) - 1;
  288. ring->max_dw = max_dw;
  289. if (amdgpu_debugfs_ring_init(adev, ring)) {
  290. DRM_ERROR("Failed to register debugfs file for rings !\n");
  291. }
  292. return 0;
  293. }
  294. /**
  295. * amdgpu_ring_fini - tear down the driver ring struct.
  296. *
  297. * @adev: amdgpu_device pointer
  298. * @ring: amdgpu_ring structure holding ring information
  299. *
  300. * Tear down the driver information for the selected ring (all asics).
  301. */
  302. void amdgpu_ring_fini(struct amdgpu_ring *ring)
  303. {
  304. int r;
  305. struct amdgpu_bo *ring_obj;
  306. ring_obj = ring->ring_obj;
  307. ring->ready = false;
  308. ring->ring = NULL;
  309. ring->ring_obj = NULL;
  310. amdgpu_wb_free(ring->adev, ring->cond_exe_offs);
  311. amdgpu_wb_free(ring->adev, ring->fence_offs);
  312. amdgpu_wb_free(ring->adev, ring->rptr_offs);
  313. amdgpu_wb_free(ring->adev, ring->wptr_offs);
  314. amdgpu_wb_free(ring->adev, ring->next_rptr_offs);
  315. if (ring_obj) {
  316. r = amdgpu_bo_reserve(ring_obj, false);
  317. if (likely(r == 0)) {
  318. amdgpu_bo_kunmap(ring_obj);
  319. amdgpu_bo_unpin(ring_obj);
  320. amdgpu_bo_unreserve(ring_obj);
  321. }
  322. amdgpu_bo_unref(&ring_obj);
  323. }
  324. amdgpu_debugfs_ring_fini(ring);
  325. }
  326. /*
  327. * Debugfs info
  328. */
  329. #if defined(CONFIG_DEBUG_FS)
  330. /* Layout of file is 12 bytes consisting of
  331. * - rptr
  332. * - wptr
  333. * - driver's copy of wptr
  334. *
  335. * followed by n-words of ring data
  336. */
  337. static ssize_t amdgpu_debugfs_ring_read(struct file *f, char __user *buf,
  338. size_t size, loff_t *pos)
  339. {
  340. struct amdgpu_ring *ring = (struct amdgpu_ring*)f->f_inode->i_private;
  341. int r, i;
  342. uint32_t value, result, early[3];
  343. if (*pos & 3 || size & 3)
  344. return -EINVAL;
  345. result = 0;
  346. if (*pos < 12) {
  347. early[0] = amdgpu_ring_get_rptr(ring);
  348. early[1] = amdgpu_ring_get_wptr(ring);
  349. early[2] = ring->wptr;
  350. for (i = *pos / 4; i < 3 && size; i++) {
  351. r = put_user(early[i], (uint32_t *)buf);
  352. if (r)
  353. return r;
  354. buf += 4;
  355. result += 4;
  356. size -= 4;
  357. *pos += 4;
  358. }
  359. }
  360. while (size) {
  361. if (*pos >= (ring->ring_size + 12))
  362. return result;
  363. value = ring->ring[(*pos - 12)/4];
  364. r = put_user(value, (uint32_t*)buf);
  365. if (r)
  366. return r;
  367. buf += 4;
  368. result += 4;
  369. size -= 4;
  370. *pos += 4;
  371. }
  372. return result;
  373. }
  374. static const struct file_operations amdgpu_debugfs_ring_fops = {
  375. .owner = THIS_MODULE,
  376. .read = amdgpu_debugfs_ring_read,
  377. .llseek = default_llseek
  378. };
  379. #endif
  380. static int amdgpu_debugfs_ring_init(struct amdgpu_device *adev,
  381. struct amdgpu_ring *ring)
  382. {
  383. #if defined(CONFIG_DEBUG_FS)
  384. struct drm_minor *minor = adev->ddev->primary;
  385. struct dentry *ent, *root = minor->debugfs_root;
  386. char name[32];
  387. sprintf(name, "amdgpu_ring_%s", ring->name);
  388. ent = debugfs_create_file(name,
  389. S_IFREG | S_IRUGO, root,
  390. ring, &amdgpu_debugfs_ring_fops);
  391. if (IS_ERR(ent))
  392. return PTR_ERR(ent);
  393. i_size_write(ent->d_inode, ring->ring_size + 12);
  394. ring->ent = ent;
  395. #endif
  396. return 0;
  397. }
  398. static void amdgpu_debugfs_ring_fini(struct amdgpu_ring *ring)
  399. {
  400. #if defined(CONFIG_DEBUG_FS)
  401. debugfs_remove(ring->ent);
  402. #endif
  403. }