amdgpu_ih.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_init - initialize the IH state
  29. *
  30. * @adev: amdgpu_device pointer
  31. * @ih: ih ring to initialize
  32. * @ring_size: ring size to allocate
  33. * @use_bus_addr: true when we can use dma_alloc_coherent
  34. *
  35. * Initializes the IH state and allocates a buffer
  36. * for the IH ring buffer.
  37. * Returns 0 for success, errors for failure.
  38. */
  39. int amdgpu_ih_ring_init(struct amdgpu_device *adev, struct amdgpu_ih_ring *ih,
  40. unsigned ring_size, bool use_bus_addr)
  41. {
  42. u32 rb_bufsz;
  43. int r;
  44. /* Align ring size */
  45. rb_bufsz = order_base_2(ring_size / 4);
  46. ring_size = (1 << rb_bufsz) * 4;
  47. ih->ring_size = ring_size;
  48. ih->ptr_mask = ih->ring_size - 1;
  49. ih->rptr = 0;
  50. ih->use_bus_addr = use_bus_addr;
  51. if (use_bus_addr) {
  52. if (ih->ring)
  53. return 0;
  54. /* add 8 bytes for the rptr/wptr shadows and
  55. * add them to the end of the ring allocation.
  56. */
  57. ih->ring = dma_alloc_coherent(adev->dev, ih->ring_size + 8,
  58. &ih->rb_dma_addr, GFP_KERNEL);
  59. if (ih->ring == NULL)
  60. return -ENOMEM;
  61. memset((void *)ih->ring, 0, ih->ring_size + 8);
  62. ih->wptr_offs = (ih->ring_size / 4) + 0;
  63. ih->rptr_offs = (ih->ring_size / 4) + 1;
  64. } else {
  65. r = amdgpu_device_wb_get(adev, &ih->wptr_offs);
  66. if (r)
  67. return r;
  68. r = amdgpu_device_wb_get(adev, &ih->rptr_offs);
  69. if (r) {
  70. amdgpu_device_wb_free(adev, ih->wptr_offs);
  71. return r;
  72. }
  73. r = amdgpu_bo_create_kernel(adev, ih->ring_size, PAGE_SIZE,
  74. AMDGPU_GEM_DOMAIN_GTT,
  75. &ih->ring_obj, &ih->gpu_addr,
  76. (void **)&ih->ring);
  77. if (r) {
  78. amdgpu_device_wb_free(adev, ih->rptr_offs);
  79. amdgpu_device_wb_free(adev, ih->wptr_offs);
  80. return r;
  81. }
  82. }
  83. return 0;
  84. }
  85. /**
  86. * amdgpu_ih_ring_fini - tear down the IH state
  87. *
  88. * @adev: amdgpu_device pointer
  89. * @ih: ih ring to tear down
  90. *
  91. * Tears down the IH state and frees buffer
  92. * used for the IH ring buffer.
  93. */
  94. void amdgpu_ih_ring_fini(struct amdgpu_device *adev, struct amdgpu_ih_ring *ih)
  95. {
  96. if (ih->use_bus_addr) {
  97. if (!ih->ring)
  98. return;
  99. /* add 8 bytes for the rptr/wptr shadows and
  100. * add them to the end of the ring allocation.
  101. */
  102. dma_free_coherent(adev->dev, ih->ring_size + 8,
  103. (void *)ih->ring, ih->rb_dma_addr);
  104. ih->ring = NULL;
  105. } else {
  106. amdgpu_bo_free_kernel(&ih->ring_obj, &ih->gpu_addr,
  107. (void **)&ih->ring);
  108. amdgpu_device_wb_free(adev, ih->wptr_offs);
  109. amdgpu_device_wb_free(adev, ih->rptr_offs);
  110. }
  111. }
  112. /**
  113. * amdgpu_ih_process - interrupt handler
  114. *
  115. * @adev: amdgpu_device pointer
  116. * @ih: ih ring to process
  117. *
  118. * Interrupt hander (VI), walk the IH ring.
  119. * Returns irq process return code.
  120. */
  121. int amdgpu_ih_process(struct amdgpu_device *adev, struct amdgpu_ih_ring *ih)
  122. {
  123. struct amdgpu_iv_entry entry;
  124. u32 wptr;
  125. if (!ih->enabled || adev->shutdown)
  126. return IRQ_NONE;
  127. wptr = amdgpu_ih_get_wptr(adev);
  128. restart_ih:
  129. /* is somebody else already processing irqs? */
  130. if (atomic_xchg(&ih->lock, 1))
  131. return IRQ_NONE;
  132. DRM_DEBUG("%s: rptr %d, wptr %d\n", __func__, ih->rptr, wptr);
  133. /* Order reading of wptr vs. reading of IH ring data */
  134. rmb();
  135. while (ih->rptr != wptr) {
  136. u32 ring_index = ih->rptr >> 2;
  137. /* Prescreening of high-frequency interrupts */
  138. if (!amdgpu_ih_prescreen_iv(adev)) {
  139. ih->rptr &= ih->ptr_mask;
  140. continue;
  141. }
  142. /* Before dispatching irq to IP blocks, send it to amdkfd */
  143. amdgpu_amdkfd_interrupt(adev,
  144. (const void *) &ih->ring[ring_index]);
  145. entry.iv_entry = (const uint32_t *)&ih->ring[ring_index];
  146. amdgpu_ih_decode_iv(adev, &entry);
  147. ih->rptr &= ih->ptr_mask;
  148. amdgpu_irq_dispatch(adev, &entry);
  149. }
  150. amdgpu_ih_set_rptr(adev);
  151. atomic_set(&ih->lock, 0);
  152. /* make sure wptr hasn't changed while processing */
  153. wptr = amdgpu_ih_get_wptr(adev);
  154. if (wptr != ih->rptr)
  155. goto restart_ih;
  156. return IRQ_HANDLED;
  157. }