0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044 #include <linux/slab.h>
0045 #include <linux/device.h>
0046 #include <linux/kfifo.h>
0047 #include "kfd_priv.h"
0048
0049 #define KFD_IH_NUM_ENTRIES 8192
0050
0051 static void interrupt_wq(struct work_struct *);
0052
0053 int kfd_interrupt_init(struct kfd_dev *kfd)
0054 {
0055 int r;
0056
0057 r = kfifo_alloc(&kfd->ih_fifo,
0058 KFD_IH_NUM_ENTRIES * kfd->device_info.ih_ring_entry_size,
0059 GFP_KERNEL);
0060 if (r) {
0061 dev_err(kfd->adev->dev, "Failed to allocate IH fifo\n");
0062 return r;
0063 }
0064
0065 kfd->ih_wq = alloc_workqueue("KFD IH", WQ_HIGHPRI, 1);
0066 if (unlikely(!kfd->ih_wq)) {
0067 kfifo_free(&kfd->ih_fifo);
0068 dev_err(kfd->adev->dev, "Failed to allocate KFD IH workqueue\n");
0069 return -ENOMEM;
0070 }
0071 spin_lock_init(&kfd->interrupt_lock);
0072
0073 INIT_WORK(&kfd->interrupt_work, interrupt_wq);
0074
0075 kfd->interrupts_active = true;
0076
0077
0078
0079
0080
0081
0082 smp_wmb();
0083
0084 return 0;
0085 }
0086
0087 void kfd_interrupt_exit(struct kfd_dev *kfd)
0088 {
0089
0090
0091
0092
0093
0094 unsigned long flags;
0095
0096 spin_lock_irqsave(&kfd->interrupt_lock, flags);
0097 kfd->interrupts_active = false;
0098 spin_unlock_irqrestore(&kfd->interrupt_lock, flags);
0099
0100
0101
0102
0103
0104
0105 flush_workqueue(kfd->ih_wq);
0106
0107 kfifo_free(&kfd->ih_fifo);
0108 }
0109
0110
0111
0112
0113 bool enqueue_ih_ring_entry(struct kfd_dev *kfd, const void *ih_ring_entry)
0114 {
0115 int count;
0116
0117 count = kfifo_in(&kfd->ih_fifo, ih_ring_entry,
0118 kfd->device_info.ih_ring_entry_size);
0119 if (count != kfd->device_info.ih_ring_entry_size) {
0120 dev_dbg_ratelimited(kfd->adev->dev,
0121 "Interrupt ring overflow, dropping interrupt %d\n",
0122 count);
0123 return false;
0124 }
0125
0126 return true;
0127 }
0128
0129
0130
0131
0132 static bool dequeue_ih_ring_entry(struct kfd_dev *kfd, void *ih_ring_entry)
0133 {
0134 int count;
0135
0136 count = kfifo_out(&kfd->ih_fifo, ih_ring_entry,
0137 kfd->device_info.ih_ring_entry_size);
0138
0139 WARN_ON(count && count != kfd->device_info.ih_ring_entry_size);
0140
0141 return count == kfd->device_info.ih_ring_entry_size;
0142 }
0143
0144 static void interrupt_wq(struct work_struct *work)
0145 {
0146 struct kfd_dev *dev = container_of(work, struct kfd_dev,
0147 interrupt_work);
0148 uint32_t ih_ring_entry[KFD_MAX_RING_ENTRY_SIZE];
0149 unsigned long start_jiffies = jiffies;
0150
0151 if (dev->device_info.ih_ring_entry_size > sizeof(ih_ring_entry)) {
0152 dev_err_once(dev->adev->dev, "Ring entry too small\n");
0153 return;
0154 }
0155
0156 while (dequeue_ih_ring_entry(dev, ih_ring_entry)) {
0157 dev->device_info.event_interrupt_class->interrupt_wq(dev,
0158 ih_ring_entry);
0159 if (time_is_before_jiffies(start_jiffies + HZ)) {
0160
0161
0162
0163 queue_work(dev->ih_wq, &dev->interrupt_work);
0164 break;
0165 }
0166 }
0167 }
0168
0169 bool interrupt_is_wanted(struct kfd_dev *dev,
0170 const uint32_t *ih_ring_entry,
0171 uint32_t *patched_ihre, bool *flag)
0172 {
0173
0174 unsigned int wanted = 0;
0175
0176 wanted |= dev->device_info.event_interrupt_class->interrupt_isr(dev,
0177 ih_ring_entry, patched_ihre, flag);
0178
0179 return wanted != 0;
0180 }