kfd_interrupt.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <linux/kfifo.h>
  44. #include "kfd_priv.h"
  45. #define KFD_IH_NUM_ENTRIES 8192
  46. static void interrupt_wq(struct work_struct *);
  47. int kfd_interrupt_init(struct kfd_dev *kfd)
  48. {
  49. int r;
  50. r = kfifo_alloc(&kfd->ih_fifo,
  51. KFD_IH_NUM_ENTRIES * kfd->device_info->ih_ring_entry_size,
  52. GFP_KERNEL);
  53. if (r) {
  54. dev_err(kfd_chardev(), "Failed to allocate IH fifo\n");
  55. return r;
  56. }
  57. kfd->ih_wq = alloc_workqueue("KFD IH", WQ_HIGHPRI, 1);
  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_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_workqueue(kfd->ih_wq);
  86. kfifo_free(&kfd->ih_fifo);
  87. }
  88. /*
  89. * Assumption: single reader/writer. This function is not re-entrant
  90. */
  91. bool enqueue_ih_ring_entry(struct kfd_dev *kfd, const void *ih_ring_entry)
  92. {
  93. int count;
  94. count = kfifo_in(&kfd->ih_fifo, ih_ring_entry,
  95. kfd->device_info->ih_ring_entry_size);
  96. if (count != kfd->device_info->ih_ring_entry_size) {
  97. dev_err_ratelimited(kfd_chardev(),
  98. "Interrupt ring overflow, dropping interrupt %d\n",
  99. count);
  100. return false;
  101. }
  102. return true;
  103. }
  104. /*
  105. * Assumption: single reader/writer. This function is not re-entrant
  106. */
  107. static bool dequeue_ih_ring_entry(struct kfd_dev *kfd, void *ih_ring_entry)
  108. {
  109. int count;
  110. count = kfifo_out(&kfd->ih_fifo, ih_ring_entry,
  111. kfd->device_info->ih_ring_entry_size);
  112. WARN_ON(count && count != kfd->device_info->ih_ring_entry_size);
  113. return count == kfd->device_info->ih_ring_entry_size;
  114. }
  115. static void interrupt_wq(struct work_struct *work)
  116. {
  117. struct kfd_dev *dev = container_of(work, struct kfd_dev,
  118. interrupt_work);
  119. uint32_t ih_ring_entry[DIV_ROUND_UP(
  120. dev->device_info->ih_ring_entry_size,
  121. sizeof(uint32_t))];
  122. while (dequeue_ih_ring_entry(dev, ih_ring_entry))
  123. dev->device_info->event_interrupt_class->interrupt_wq(dev,
  124. ih_ring_entry);
  125. }
  126. bool interrupt_is_wanted(struct kfd_dev *dev, const uint32_t *ih_ring_entry)
  127. {
  128. /* integer and bitwise OR so there is no boolean short-circuiting */
  129. unsigned int wanted = 0;
  130. wanted |= dev->device_info->event_interrupt_class->interrupt_isr(dev,
  131. ih_ring_entry);
  132. return wanted != 0;
  133. }