amdgpu_ih.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. /**
  27. * amdgpu_ih_ring_alloc - allocate memory for the IH ring
  28. *
  29. * @adev: amdgpu_device pointer
  30. *
  31. * Allocate a ring buffer for the interrupt controller.
  32. * Returns 0 for success, errors for failure.
  33. */
  34. static int amdgpu_ih_ring_alloc(struct amdgpu_device *adev)
  35. {
  36. int r;
  37. /* Allocate ring buffer */
  38. if (adev->irq.ih.ring_obj == NULL) {
  39. r = amdgpu_bo_create(adev, adev->irq.ih.ring_size,
  40. PAGE_SIZE, true,
  41. AMDGPU_GEM_DOMAIN_GTT, 0,
  42. NULL, &adev->irq.ih.ring_obj);
  43. if (r) {
  44. DRM_ERROR("amdgpu: failed to create ih ring buffer (%d).\n", r);
  45. return r;
  46. }
  47. r = amdgpu_bo_reserve(adev->irq.ih.ring_obj, false);
  48. if (unlikely(r != 0))
  49. return r;
  50. r = amdgpu_bo_pin(adev->irq.ih.ring_obj,
  51. AMDGPU_GEM_DOMAIN_GTT,
  52. &adev->irq.ih.gpu_addr);
  53. if (r) {
  54. amdgpu_bo_unreserve(adev->irq.ih.ring_obj);
  55. DRM_ERROR("amdgpu: failed to pin ih ring buffer (%d).\n", r);
  56. return r;
  57. }
  58. r = amdgpu_bo_kmap(adev->irq.ih.ring_obj,
  59. (void **)&adev->irq.ih.ring);
  60. amdgpu_bo_unreserve(adev->irq.ih.ring_obj);
  61. if (r) {
  62. DRM_ERROR("amdgpu: failed to map ih ring buffer (%d).\n", r);
  63. return r;
  64. }
  65. }
  66. return 0;
  67. }
  68. /**
  69. * amdgpu_ih_ring_init - initialize the IH state
  70. *
  71. * @adev: amdgpu_device pointer
  72. *
  73. * Initializes the IH state and allocates a buffer
  74. * for the IH ring buffer.
  75. * Returns 0 for success, errors for failure.
  76. */
  77. int amdgpu_ih_ring_init(struct amdgpu_device *adev, unsigned ring_size,
  78. bool use_bus_addr)
  79. {
  80. u32 rb_bufsz;
  81. int r;
  82. /* Align ring size */
  83. rb_bufsz = order_base_2(ring_size / 4);
  84. ring_size = (1 << rb_bufsz) * 4;
  85. adev->irq.ih.ring_size = ring_size;
  86. adev->irq.ih.ptr_mask = adev->irq.ih.ring_size - 1;
  87. adev->irq.ih.rptr = 0;
  88. adev->irq.ih.use_bus_addr = use_bus_addr;
  89. if (adev->irq.ih.use_bus_addr) {
  90. if (!adev->irq.ih.ring) {
  91. /* add 8 bytes for the rptr/wptr shadows and
  92. * add them to the end of the ring allocation.
  93. */
  94. adev->irq.ih.ring = kzalloc(adev->irq.ih.ring_size + 8, GFP_KERNEL);
  95. if (adev->irq.ih.ring == NULL)
  96. return -ENOMEM;
  97. adev->irq.ih.rb_dma_addr = pci_map_single(adev->pdev,
  98. (void *)adev->irq.ih.ring,
  99. adev->irq.ih.ring_size,
  100. PCI_DMA_BIDIRECTIONAL);
  101. if (pci_dma_mapping_error(adev->pdev, adev->irq.ih.rb_dma_addr)) {
  102. dev_err(&adev->pdev->dev, "Failed to DMA MAP the IH RB page\n");
  103. kfree((void *)adev->irq.ih.ring);
  104. return -ENOMEM;
  105. }
  106. adev->irq.ih.wptr_offs = (adev->irq.ih.ring_size / 4) + 0;
  107. adev->irq.ih.rptr_offs = (adev->irq.ih.ring_size / 4) + 1;
  108. }
  109. return 0;
  110. } else {
  111. r = amdgpu_wb_get(adev, &adev->irq.ih.wptr_offs);
  112. if (r) {
  113. dev_err(adev->dev, "(%d) ih wptr_offs wb alloc failed\n", r);
  114. return r;
  115. }
  116. r = amdgpu_wb_get(adev, &adev->irq.ih.rptr_offs);
  117. if (r) {
  118. amdgpu_wb_free(adev, adev->irq.ih.wptr_offs);
  119. dev_err(adev->dev, "(%d) ih rptr_offs wb alloc failed\n", r);
  120. return r;
  121. }
  122. return amdgpu_ih_ring_alloc(adev);
  123. }
  124. }
  125. /**
  126. * amdgpu_ih_ring_fini - tear down the IH state
  127. *
  128. * @adev: amdgpu_device pointer
  129. *
  130. * Tears down the IH state and frees buffer
  131. * used for the IH ring buffer.
  132. */
  133. void amdgpu_ih_ring_fini(struct amdgpu_device *adev)
  134. {
  135. int r;
  136. if (adev->irq.ih.use_bus_addr) {
  137. if (adev->irq.ih.ring) {
  138. /* add 8 bytes for the rptr/wptr shadows and
  139. * add them to the end of the ring allocation.
  140. */
  141. pci_unmap_single(adev->pdev, adev->irq.ih.rb_dma_addr,
  142. adev->irq.ih.ring_size + 8, PCI_DMA_BIDIRECTIONAL);
  143. kfree((void *)adev->irq.ih.ring);
  144. adev->irq.ih.ring = NULL;
  145. }
  146. } else {
  147. if (adev->irq.ih.ring_obj) {
  148. r = amdgpu_bo_reserve(adev->irq.ih.ring_obj, false);
  149. if (likely(r == 0)) {
  150. amdgpu_bo_kunmap(adev->irq.ih.ring_obj);
  151. amdgpu_bo_unpin(adev->irq.ih.ring_obj);
  152. amdgpu_bo_unreserve(adev->irq.ih.ring_obj);
  153. }
  154. amdgpu_bo_unref(&adev->irq.ih.ring_obj);
  155. adev->irq.ih.ring = NULL;
  156. adev->irq.ih.ring_obj = NULL;
  157. }
  158. amdgpu_wb_free(adev, adev->irq.ih.wptr_offs);
  159. amdgpu_wb_free(adev, adev->irq.ih.rptr_offs);
  160. }
  161. }
  162. /**
  163. * amdgpu_ih_process - interrupt handler
  164. *
  165. * @adev: amdgpu_device pointer
  166. *
  167. * Interrupt hander (VI), walk the IH ring.
  168. * Returns irq process return code.
  169. */
  170. int amdgpu_ih_process(struct amdgpu_device *adev)
  171. {
  172. struct amdgpu_iv_entry entry;
  173. u32 wptr;
  174. if (!adev->irq.ih.enabled || adev->shutdown)
  175. return IRQ_NONE;
  176. wptr = amdgpu_ih_get_wptr(adev);
  177. restart_ih:
  178. /* is somebody else already processing irqs? */
  179. if (atomic_xchg(&adev->irq.ih.lock, 1))
  180. return IRQ_NONE;
  181. DRM_DEBUG("%s: rptr %d, wptr %d\n", __func__, adev->irq.ih.rptr, wptr);
  182. /* Order reading of wptr vs. reading of IH ring data */
  183. rmb();
  184. while (adev->irq.ih.rptr != wptr) {
  185. amdgpu_ih_decode_iv(adev, &entry);
  186. adev->irq.ih.rptr &= adev->irq.ih.ptr_mask;
  187. amdgpu_irq_dispatch(adev, &entry);
  188. }
  189. amdgpu_ih_set_rptr(adev);
  190. atomic_set(&adev->irq.ih.lock, 0);
  191. /* make sure wptr hasn't changed while processing */
  192. wptr = amdgpu_ih_get_wptr(adev);
  193. if (wptr != adev->irq.ih.rptr)
  194. goto restart_ih;
  195. return IRQ_HANDLED;
  196. }