amdgpu_ring.c 11 KB

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