Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0 OR MIT
0002 /*
0003  * Copyright 2014-2022 Advanced Micro Devices, Inc.
0004  *
0005  * Permission is hereby granted, free of charge, to any person obtaining a
0006  * copy of this software and associated documentation files (the "Software"),
0007  * to deal in the Software without restriction, including without limitation
0008  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0009  * and/or sell copies of the Software, and to permit persons to whom the
0010  * Software is furnished to do so, subject to the following conditions:
0011  *
0012  * The above copyright notice and this permission notice shall be included in
0013  * all copies or substantial portions of the Software.
0014  *
0015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0018  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0019  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0020  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0021  * OTHER DEALINGS IN THE SOFTWARE.
0022  */
0023 
0024 /*
0025  * KFD Interrupts.
0026  *
0027  * AMD GPUs deliver interrupts by pushing an interrupt description onto the
0028  * interrupt ring and then sending an interrupt. KGD receives the interrupt
0029  * in ISR and sends us a pointer to each new entry on the interrupt ring.
0030  *
0031  * We generally can't process interrupt-signaled events from ISR, so we call
0032  * out to each interrupt client module (currently only the scheduler) to ask if
0033  * each interrupt is interesting. If they return true, then it requires further
0034  * processing so we copy it to an internal interrupt ring and call each
0035  * interrupt client again from a work-queue.
0036  *
0037  * There's no acknowledgment for the interrupts we use. The hardware simply
0038  * queues a new interrupt each time without waiting.
0039  *
0040  * The fixed-size internal queue means that it's possible for us to lose
0041  * interrupts because we have no back-pressure to the hardware.
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      * After this function returns, the interrupt will be enabled. This
0079      * barrier ensures that the interrupt running on a different processor
0080      * sees all the above writes.
0081      */
0082     smp_wmb();
0083 
0084     return 0;
0085 }
0086 
0087 void kfd_interrupt_exit(struct kfd_dev *kfd)
0088 {
0089     /*
0090      * Stop the interrupt handler from writing to the ring and scheduling
0091      * workqueue items. The spinlock ensures that any interrupt running
0092      * after we have unlocked sees interrupts_active = false.
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      * flush_work ensures that there are no outstanding
0102      * work-queue items that will access interrupt_ring. New work items
0103      * can't be created because we stopped interrupt handling above.
0104      */
0105     flush_workqueue(kfd->ih_wq);
0106 
0107     kfifo_free(&kfd->ih_fifo);
0108 }
0109 
0110 /*
0111  * Assumption: single reader/writer. This function is not re-entrant
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  * Assumption: single reader/writer. This function is not re-entrant
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             /* If we spent more than a second processing signals,
0161              * reschedule the worker to avoid soft-lockup warnings
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     /* integer and bitwise OR so there is no boolean short-circuiting */
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 }