kfd_interrupt.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. * KFD Interrupts.
  24. *
  25. * AMD GPUs deliver interrupts by pushing an interrupt description onto the
  26. * interrupt ring and then sending an interrupt. KGD receives the interrupt
  27. * in ISR and sends us a pointer to each new entry on the interrupt ring.
  28. *
  29. * We generally can't process interrupt-signaled events from ISR, so we call
  30. * out to each interrupt client module (currently only the scheduler) to ask if
  31. * each interrupt is interesting. If they return true, then it requires further
  32. * processing so we copy it to an internal interrupt ring and call each
  33. * interrupt client again from a work-queue.
  34. *
  35. * There's no acknowledgment for the interrupts we use. The hardware simply
  36. * queues a new interrupt each time without waiting.
  37. *
  38. * The fixed-size internal queue means that it's possible for us to lose
  39. * interrupts because we have no back-pressure to the hardware.
  40. */
  41. #include <linux/slab.h>
  42. #include <linux/device.h>
  43. #include "kfd_priv.h"
  44. #define KFD_INTERRUPT_RING_SIZE 1024
  45. static void interrupt_wq(struct work_struct *);
  46. int kfd_interrupt_init(struct kfd_dev *kfd)
  47. {
  48. void *interrupt_ring = kmalloc_array(KFD_INTERRUPT_RING_SIZE,
  49. kfd->device_info->ih_ring_entry_size,
  50. GFP_KERNEL);
  51. if (!interrupt_ring)
  52. return -ENOMEM;
  53. kfd->interrupt_ring = interrupt_ring;
  54. kfd->interrupt_ring_size =
  55. KFD_INTERRUPT_RING_SIZE * kfd->device_info->ih_ring_entry_size;
  56. atomic_set(&kfd->interrupt_ring_wptr, 0);
  57. atomic_set(&kfd->interrupt_ring_rptr, 0);
  58. spin_lock_init(&kfd->interrupt_lock);
  59. INIT_WORK(&kfd->interrupt_work, interrupt_wq);
  60. kfd->interrupts_active = true;
  61. /*
  62. * After this function returns, the interrupt will be enabled. This
  63. * barrier ensures that the interrupt running on a different processor
  64. * sees all the above writes.
  65. */
  66. smp_wmb();
  67. return 0;
  68. }
  69. void kfd_interrupt_exit(struct kfd_dev *kfd)
  70. {
  71. /*
  72. * Stop the interrupt handler from writing to the ring and scheduling
  73. * workqueue items. The spinlock ensures that any interrupt running
  74. * after we have unlocked sees interrupts_active = false.
  75. */
  76. unsigned long flags;
  77. spin_lock_irqsave(&kfd->interrupt_lock, flags);
  78. kfd->interrupts_active = false;
  79. spin_unlock_irqrestore(&kfd->interrupt_lock, flags);
  80. /*
  81. * Flush_scheduled_work ensures that there are no outstanding
  82. * work-queue items that will access interrupt_ring. New work items
  83. * can't be created because we stopped interrupt handling above.
  84. */
  85. flush_scheduled_work();
  86. kfree(kfd->interrupt_ring);
  87. }
  88. /*
  89. * This assumes that it can't be called concurrently with itself
  90. * but only with dequeue_ih_ring_entry.
  91. */
  92. bool enqueue_ih_ring_entry(struct kfd_dev *kfd, const void *ih_ring_entry)
  93. {
  94. unsigned int rptr = atomic_read(&kfd->interrupt_ring_rptr);
  95. unsigned int wptr = atomic_read(&kfd->interrupt_ring_wptr);
  96. if ((rptr - wptr) % kfd->interrupt_ring_size ==
  97. kfd->device_info->ih_ring_entry_size) {
  98. /* This is very bad, the system is likely to hang. */
  99. dev_err_ratelimited(kfd_chardev(),
  100. "Interrupt ring overflow, dropping interrupt.\n");
  101. return false;
  102. }
  103. memcpy(kfd->interrupt_ring + wptr, ih_ring_entry,
  104. kfd->device_info->ih_ring_entry_size);
  105. wptr = (wptr + kfd->device_info->ih_ring_entry_size) %
  106. kfd->interrupt_ring_size;
  107. smp_wmb(); /* Ensure memcpy'd data is visible before wptr update. */
  108. atomic_set(&kfd->interrupt_ring_wptr, wptr);
  109. return true;
  110. }
  111. /*
  112. * This assumes that it can't be called concurrently with itself
  113. * but only with enqueue_ih_ring_entry.
  114. */
  115. static bool dequeue_ih_ring_entry(struct kfd_dev *kfd, void *ih_ring_entry)
  116. {
  117. /*
  118. * Assume that wait queues have an implicit barrier, i.e. anything that
  119. * happened in the ISR before it queued work is visible.
  120. */
  121. unsigned int wptr = atomic_read(&kfd->interrupt_ring_wptr);
  122. unsigned int rptr = atomic_read(&kfd->interrupt_ring_rptr);
  123. if (rptr == wptr)
  124. return false;
  125. memcpy(ih_ring_entry, kfd->interrupt_ring + rptr,
  126. kfd->device_info->ih_ring_entry_size);
  127. rptr = (rptr + kfd->device_info->ih_ring_entry_size) %
  128. kfd->interrupt_ring_size;
  129. /*
  130. * Ensure the rptr write update is not visible until
  131. * memcpy has finished reading.
  132. */
  133. smp_mb();
  134. atomic_set(&kfd->interrupt_ring_rptr, rptr);
  135. return true;
  136. }
  137. static void interrupt_wq(struct work_struct *work)
  138. {
  139. struct kfd_dev *dev = container_of(work, struct kfd_dev,
  140. interrupt_work);
  141. uint32_t ih_ring_entry[DIV_ROUND_UP(
  142. dev->device_info->ih_ring_entry_size,
  143. sizeof(uint32_t))];
  144. while (dequeue_ih_ring_entry(dev, ih_ring_entry))
  145. dev->device_info->event_interrupt_class->interrupt_wq(dev,
  146. ih_ring_entry);
  147. }
  148. bool interrupt_is_wanted(struct kfd_dev *dev, const uint32_t *ih_ring_entry)
  149. {
  150. /* integer and bitwise OR so there is no boolean short-circuiting */
  151. unsigned wanted = 0;
  152. wanted |= dev->device_info->event_interrupt_class->interrupt_isr(dev,
  153. ih_ring_entry);
  154. return wanted != 0;
  155. }