amdgpu_ih.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright 2014 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. */
  23. #include <drm/drmP.h>
  24. #include "amdgpu.h"
  25. #include "amdgpu_ih.h"
  26. #include "amdgpu_amdkfd.h"
  27. /**
  28. * amdgpu_ih_ring_alloc - allocate memory for the IH ring
  29. *
  30. * @adev: amdgpu_device pointer
  31. *
  32. * Allocate a ring buffer for the interrupt controller.
  33. * Returns 0 for success, errors for failure.
  34. */
  35. static int amdgpu_ih_ring_alloc(struct amdgpu_device *adev)
  36. {
  37. int r;
  38. /* Allocate ring buffer */
  39. if (adev->irq.ih.ring_obj == NULL) {
  40. r = amdgpu_bo_create_kernel(adev, adev->irq.ih.ring_size,
  41. PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
  42. &adev->irq.ih.ring_obj,
  43. &adev->irq.ih.gpu_addr,
  44. (void **)&adev->irq.ih.ring);
  45. if (r) {
  46. DRM_ERROR("amdgpu: failed to create ih ring buffer (%d).\n", r);
  47. return r;
  48. }
  49. }
  50. return 0;
  51. }
  52. /**
  53. * amdgpu_ih_ring_init - initialize the IH state
  54. *
  55. * @adev: amdgpu_device pointer
  56. *
  57. * Initializes the IH state and allocates a buffer
  58. * for the IH ring buffer.
  59. * Returns 0 for success, errors for failure.
  60. */
  61. int amdgpu_ih_ring_init(struct amdgpu_device *adev, unsigned ring_size,
  62. bool use_bus_addr)
  63. {
  64. u32 rb_bufsz;
  65. int r;
  66. /* Align ring size */
  67. rb_bufsz = order_base_2(ring_size / 4);
  68. ring_size = (1 << rb_bufsz) * 4;
  69. adev->irq.ih.ring_size = ring_size;
  70. adev->irq.ih.ptr_mask = adev->irq.ih.ring_size - 1;
  71. adev->irq.ih.rptr = 0;
  72. adev->irq.ih.use_bus_addr = use_bus_addr;
  73. if (adev->irq.ih.use_bus_addr) {
  74. if (!adev->irq.ih.ring) {
  75. /* add 8 bytes for the rptr/wptr shadows and
  76. * add them to the end of the ring allocation.
  77. */
  78. adev->irq.ih.ring = pci_alloc_consistent(adev->pdev,
  79. adev->irq.ih.ring_size + 8,
  80. &adev->irq.ih.rb_dma_addr);
  81. if (adev->irq.ih.ring == NULL)
  82. return -ENOMEM;
  83. memset((void *)adev->irq.ih.ring, 0, adev->irq.ih.ring_size + 8);
  84. adev->irq.ih.wptr_offs = (adev->irq.ih.ring_size / 4) + 0;
  85. adev->irq.ih.rptr_offs = (adev->irq.ih.ring_size / 4) + 1;
  86. }
  87. return 0;
  88. } else {
  89. r = amdgpu_device_wb_get(adev, &adev->irq.ih.wptr_offs);
  90. if (r) {
  91. dev_err(adev->dev, "(%d) ih wptr_offs wb alloc failed\n", r);
  92. return r;
  93. }
  94. r = amdgpu_device_wb_get(adev, &adev->irq.ih.rptr_offs);
  95. if (r) {
  96. amdgpu_device_wb_free(adev, adev->irq.ih.wptr_offs);
  97. dev_err(adev->dev, "(%d) ih rptr_offs wb alloc failed\n", r);
  98. return r;
  99. }
  100. return amdgpu_ih_ring_alloc(adev);
  101. }
  102. }
  103. /**
  104. * amdgpu_ih_ring_fini - tear down the IH state
  105. *
  106. * @adev: amdgpu_device pointer
  107. *
  108. * Tears down the IH state and frees buffer
  109. * used for the IH ring buffer.
  110. */
  111. void amdgpu_ih_ring_fini(struct amdgpu_device *adev)
  112. {
  113. if (adev->irq.ih.use_bus_addr) {
  114. if (adev->irq.ih.ring) {
  115. /* add 8 bytes for the rptr/wptr shadows and
  116. * add them to the end of the ring allocation.
  117. */
  118. pci_free_consistent(adev->pdev, adev->irq.ih.ring_size + 8,
  119. (void *)adev->irq.ih.ring,
  120. adev->irq.ih.rb_dma_addr);
  121. adev->irq.ih.ring = NULL;
  122. }
  123. } else {
  124. amdgpu_bo_free_kernel(&adev->irq.ih.ring_obj,
  125. &adev->irq.ih.gpu_addr,
  126. (void **)&adev->irq.ih.ring);
  127. amdgpu_device_wb_free(adev, adev->irq.ih.wptr_offs);
  128. amdgpu_device_wb_free(adev, adev->irq.ih.rptr_offs);
  129. }
  130. }
  131. /**
  132. * amdgpu_ih_process - interrupt handler
  133. *
  134. * @adev: amdgpu_device pointer
  135. *
  136. * Interrupt hander (VI), walk the IH ring.
  137. * Returns irq process return code.
  138. */
  139. int amdgpu_ih_process(struct amdgpu_device *adev)
  140. {
  141. struct amdgpu_iv_entry entry;
  142. u32 wptr;
  143. if (!adev->irq.ih.enabled || adev->shutdown)
  144. return IRQ_NONE;
  145. wptr = amdgpu_ih_get_wptr(adev);
  146. restart_ih:
  147. /* is somebody else already processing irqs? */
  148. if (atomic_xchg(&adev->irq.ih.lock, 1))
  149. return IRQ_NONE;
  150. DRM_DEBUG("%s: rptr %d, wptr %d\n", __func__, adev->irq.ih.rptr, wptr);
  151. /* Order reading of wptr vs. reading of IH ring data */
  152. rmb();
  153. while (adev->irq.ih.rptr != wptr) {
  154. u32 ring_index = adev->irq.ih.rptr >> 2;
  155. /* Prescreening of high-frequency interrupts */
  156. if (!amdgpu_ih_prescreen_iv(adev)) {
  157. adev->irq.ih.rptr &= adev->irq.ih.ptr_mask;
  158. continue;
  159. }
  160. /* Before dispatching irq to IP blocks, send it to amdkfd */
  161. amdgpu_amdkfd_interrupt(adev,
  162. (const void *) &adev->irq.ih.ring[ring_index]);
  163. entry.iv_entry = (const uint32_t *)
  164. &adev->irq.ih.ring[ring_index];
  165. amdgpu_ih_decode_iv(adev, &entry);
  166. adev->irq.ih.rptr &= adev->irq.ih.ptr_mask;
  167. amdgpu_irq_dispatch(adev, &entry);
  168. }
  169. amdgpu_ih_set_rptr(adev);
  170. atomic_set(&adev->irq.ih.lock, 0);
  171. /* make sure wptr hasn't changed while processing */
  172. wptr = amdgpu_ih_get_wptr(adev);
  173. if (wptr != adev->irq.ih.rptr)
  174. goto restart_ih;
  175. return IRQ_HANDLED;
  176. }
  177. /**
  178. * amdgpu_ih_add_fault - Add a page fault record
  179. *
  180. * @adev: amdgpu device pointer
  181. * @key: 64-bit encoding of PASID and address
  182. *
  183. * This should be called when a retry page fault interrupt is
  184. * received. If this is a new page fault, it will be added to a hash
  185. * table. The return value indicates whether this is a new fault, or
  186. * a fault that was already known and is already being handled.
  187. *
  188. * If there are too many pending page faults, this will fail. Retry
  189. * interrupts should be ignored in this case until there is enough
  190. * free space.
  191. *
  192. * Returns 0 if the fault was added, 1 if the fault was already known,
  193. * -ENOSPC if there are too many pending faults.
  194. */
  195. int amdgpu_ih_add_fault(struct amdgpu_device *adev, u64 key)
  196. {
  197. unsigned long flags;
  198. int r = -ENOSPC;
  199. if (WARN_ON_ONCE(!adev->irq.ih.faults))
  200. /* Should be allocated in <IP>_ih_sw_init on GPUs that
  201. * support retry faults and require retry filtering.
  202. */
  203. return r;
  204. spin_lock_irqsave(&adev->irq.ih.faults->lock, flags);
  205. /* Only let the hash table fill up to 50% for best performance */
  206. if (adev->irq.ih.faults->count >= (1 << (AMDGPU_PAGEFAULT_HASH_BITS-1)))
  207. goto unlock_out;
  208. r = chash_table_copy_in(&adev->irq.ih.faults->hash, key, NULL);
  209. if (!r)
  210. adev->irq.ih.faults->count++;
  211. /* chash_table_copy_in should never fail unless we're losing count */
  212. WARN_ON_ONCE(r < 0);
  213. unlock_out:
  214. spin_unlock_irqrestore(&adev->irq.ih.faults->lock, flags);
  215. return r;
  216. }
  217. /**
  218. * amdgpu_ih_clear_fault - Remove a page fault record
  219. *
  220. * @adev: amdgpu device pointer
  221. * @key: 64-bit encoding of PASID and address
  222. *
  223. * This should be called when a page fault has been handled. Any
  224. * future interrupt with this key will be processed as a new
  225. * page fault.
  226. */
  227. void amdgpu_ih_clear_fault(struct amdgpu_device *adev, u64 key)
  228. {
  229. unsigned long flags;
  230. int r;
  231. if (!adev->irq.ih.faults)
  232. return;
  233. spin_lock_irqsave(&adev->irq.ih.faults->lock, flags);
  234. r = chash_table_remove(&adev->irq.ih.faults->hash, key, NULL);
  235. if (!WARN_ON_ONCE(r < 0)) {
  236. adev->irq.ih.faults->count--;
  237. WARN_ON_ONCE(adev->irq.ih.faults->count < 0);
  238. }
  239. spin_unlock_irqrestore(&adev->irq.ih.faults->lock, flags);
  240. }