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