kfd_interrupt.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /*
  3. * Copyright 2014-2022 Advanced Micro Devices, Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. /*
  24. * KFD Interrupts.
  25. *
  26. * AMD GPUs deliver interrupts by pushing an interrupt description onto the
  27. * interrupt ring and then sending an interrupt. KGD receives the interrupt
  28. * in ISR and sends us a pointer to each new entry on the interrupt ring.
  29. *
  30. * We generally can't process interrupt-signaled events from ISR, so we call
  31. * out to each interrupt client module (currently only the scheduler) to ask if
  32. * each interrupt is interesting. If they return true, then it requires further
  33. * processing so we copy it to an internal interrupt ring and call each
  34. * interrupt client again from a work-queue.
  35. *
  36. * There's no acknowledgment for the interrupts we use. The hardware simply
  37. * queues a new interrupt each time without waiting.
  38. *
  39. * The fixed-size internal queue means that it's possible for us to lose
  40. * interrupts because we have no back-pressure to the hardware.
  41. */
  42. #include <linux/slab.h>
  43. #include <linux/device.h>
  44. #include <linux/kfifo.h>
  45. #include "kfd_priv.h"
  46. #define KFD_IH_NUM_ENTRIES 8192
  47. static void interrupt_wq(struct work_struct *);
  48. int kfd_interrupt_init(struct kfd_node *node)
  49. {
  50. int r;
  51. r = kfifo_alloc(&node->ih_fifo,
  52. KFD_IH_NUM_ENTRIES * node->kfd->device_info.ih_ring_entry_size,
  53. GFP_KERNEL);
  54. if (r) {
  55. dev_err(node->adev->dev, "Failed to allocate IH fifo\n");
  56. return r;
  57. }
  58. if (!node->kfd->ih_wq) {
  59. node->kfd->ih_wq = alloc_workqueue("KFD IH", WQ_HIGHPRI | WQ_UNBOUND,
  60. node->kfd->num_nodes);
  61. if (unlikely(!node->kfd->ih_wq)) {
  62. kfifo_free(&node->ih_fifo);
  63. dev_err(node->adev->dev, "Failed to allocate KFD IH workqueue\n");
  64. return -ENOMEM;
  65. }
  66. }
  67. spin_lock_init(&node->interrupt_lock);
  68. INIT_WORK(&node->interrupt_work, interrupt_wq);
  69. node->interrupts_active = true;
  70. /*
  71. * After this function returns, the interrupt will be enabled. This
  72. * barrier ensures that the interrupt running on a different processor
  73. * sees all the above writes.
  74. */
  75. smp_wmb();
  76. return 0;
  77. }
  78. void kfd_interrupt_exit(struct kfd_node *node)
  79. {
  80. /*
  81. * Stop the interrupt handler from writing to the ring and scheduling
  82. * workqueue items. The spinlock ensures that any interrupt running
  83. * after we have unlocked sees interrupts_active = false.
  84. */
  85. unsigned long flags;
  86. spin_lock_irqsave(&node->interrupt_lock, flags);
  87. node->interrupts_active = false;
  88. spin_unlock_irqrestore(&node->interrupt_lock, flags);
  89. kfifo_free(&node->ih_fifo);
  90. }
  91. /*
  92. * Assumption: single reader/writer. This function is not re-entrant
  93. */
  94. bool enqueue_ih_ring_entry(struct kfd_node *node, const void *ih_ring_entry)
  95. {
  96. int count;
  97. count = kfifo_in(&node->ih_fifo, ih_ring_entry,
  98. node->kfd->device_info.ih_ring_entry_size);
  99. if (count != node->kfd->device_info.ih_ring_entry_size) {
  100. dev_dbg_ratelimited(node->adev->dev,
  101. "Interrupt ring overflow, dropping interrupt %d\n",
  102. count);
  103. return false;
  104. }
  105. return true;
  106. }
  107. /*
  108. * Assumption: single reader/writer. This function is not re-entrant
  109. */
  110. static bool dequeue_ih_ring_entry(struct kfd_node *node, void *ih_ring_entry)
  111. {
  112. int count;
  113. count = kfifo_out(&node->ih_fifo, ih_ring_entry,
  114. node->kfd->device_info.ih_ring_entry_size);
  115. WARN_ON(count && count != node->kfd->device_info.ih_ring_entry_size);
  116. return count == node->kfd->device_info.ih_ring_entry_size;
  117. }
  118. static void interrupt_wq(struct work_struct *work)
  119. {
  120. struct kfd_node *dev = container_of(work, struct kfd_node,
  121. interrupt_work);
  122. uint32_t ih_ring_entry[KFD_MAX_RING_ENTRY_SIZE];
  123. unsigned long start_jiffies = jiffies;
  124. if (dev->kfd->device_info.ih_ring_entry_size > sizeof(ih_ring_entry)) {
  125. dev_err_once(dev->adev->dev, "Ring entry too small\n");
  126. return;
  127. }
  128. while (dequeue_ih_ring_entry(dev, ih_ring_entry)) {
  129. dev->kfd->device_info.event_interrupt_class->interrupt_wq(dev,
  130. ih_ring_entry);
  131. if (time_is_before_jiffies(start_jiffies + HZ)) {
  132. /* If we spent more than a second processing signals,
  133. * reschedule the worker to avoid soft-lockup warnings
  134. */
  135. queue_work(dev->kfd->ih_wq, &dev->interrupt_work);
  136. break;
  137. }
  138. }
  139. }
  140. bool interrupt_is_wanted(struct kfd_node *dev,
  141. const uint32_t *ih_ring_entry,
  142. uint32_t *patched_ihre, bool *flag)
  143. {
  144. /* integer and bitwise OR so there is no boolean short-circuiting */
  145. unsigned int wanted = 0;
  146. wanted |= dev->kfd->device_info.event_interrupt_class->interrupt_isr(dev,
  147. ih_ring_entry, patched_ihre, flag);
  148. return wanted != 0;
  149. }