amdgpu_ih.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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_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_wb_get(adev, &adev->irq.ih.rptr_offs);
  95. if (r) {
  96. amdgpu_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_wb_free(adev, adev->irq.ih.wptr_offs);
  128. amdgpu_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. /* Before dispatching irq to IP blocks, send it to amdkfd */
  156. amdgpu_amdkfd_interrupt(adev,
  157. (const void *) &adev->irq.ih.ring[ring_index]);
  158. entry.iv_entry = (const uint32_t *)
  159. &adev->irq.ih.ring[ring_index];
  160. amdgpu_ih_decode_iv(adev, &entry);
  161. adev->irq.ih.rptr &= adev->irq.ih.ptr_mask;
  162. amdgpu_irq_dispatch(adev, &entry);
  163. }
  164. amdgpu_ih_set_rptr(adev);
  165. atomic_set(&adev->irq.ih.lock, 0);
  166. /* make sure wptr hasn't changed while processing */
  167. wptr = amdgpu_ih_get_wptr(adev);
  168. if (wptr != adev->irq.ih.rptr)
  169. goto restart_ih;
  170. return IRQ_HANDLED;
  171. }