162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 OR MIT 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright 2014-2022 Advanced Micro Devices, Inc. 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 662306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 762306a36Sopenharmony_ci * to deal in the Software without restriction, including without limitation 862306a36Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 962306a36Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 1062306a36Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 1162306a36Sopenharmony_ci * 1262306a36Sopenharmony_ci * The above copyright notice and this permission notice shall be included in 1362306a36Sopenharmony_ci * all copies or substantial portions of the Software. 1462306a36Sopenharmony_ci * 1562306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1662306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1762306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1862306a36Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 1962306a36Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 2062306a36Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 2162306a36Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE. 2262306a36Sopenharmony_ci */ 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ci/* 2562306a36Sopenharmony_ci * KFD Interrupts. 2662306a36Sopenharmony_ci * 2762306a36Sopenharmony_ci * AMD GPUs deliver interrupts by pushing an interrupt description onto the 2862306a36Sopenharmony_ci * interrupt ring and then sending an interrupt. KGD receives the interrupt 2962306a36Sopenharmony_ci * in ISR and sends us a pointer to each new entry on the interrupt ring. 3062306a36Sopenharmony_ci * 3162306a36Sopenharmony_ci * We generally can't process interrupt-signaled events from ISR, so we call 3262306a36Sopenharmony_ci * out to each interrupt client module (currently only the scheduler) to ask if 3362306a36Sopenharmony_ci * each interrupt is interesting. If they return true, then it requires further 3462306a36Sopenharmony_ci * processing so we copy it to an internal interrupt ring and call each 3562306a36Sopenharmony_ci * interrupt client again from a work-queue. 3662306a36Sopenharmony_ci * 3762306a36Sopenharmony_ci * There's no acknowledgment for the interrupts we use. The hardware simply 3862306a36Sopenharmony_ci * queues a new interrupt each time without waiting. 3962306a36Sopenharmony_ci * 4062306a36Sopenharmony_ci * The fixed-size internal queue means that it's possible for us to lose 4162306a36Sopenharmony_ci * interrupts because we have no back-pressure to the hardware. 4262306a36Sopenharmony_ci */ 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci#include <linux/slab.h> 4562306a36Sopenharmony_ci#include <linux/device.h> 4662306a36Sopenharmony_ci#include <linux/kfifo.h> 4762306a36Sopenharmony_ci#include "kfd_priv.h" 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ci#define KFD_IH_NUM_ENTRIES 8192 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_cistatic void interrupt_wq(struct work_struct *); 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ciint kfd_interrupt_init(struct kfd_node *node) 5462306a36Sopenharmony_ci{ 5562306a36Sopenharmony_ci int r; 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ci r = kfifo_alloc(&node->ih_fifo, 5862306a36Sopenharmony_ci KFD_IH_NUM_ENTRIES * node->kfd->device_info.ih_ring_entry_size, 5962306a36Sopenharmony_ci GFP_KERNEL); 6062306a36Sopenharmony_ci if (r) { 6162306a36Sopenharmony_ci dev_err(node->adev->dev, "Failed to allocate IH fifo\n"); 6262306a36Sopenharmony_ci return r; 6362306a36Sopenharmony_ci } 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci node->ih_wq = alloc_workqueue("KFD IH", WQ_HIGHPRI, 1); 6662306a36Sopenharmony_ci if (unlikely(!node->ih_wq)) { 6762306a36Sopenharmony_ci kfifo_free(&node->ih_fifo); 6862306a36Sopenharmony_ci dev_err(node->adev->dev, "Failed to allocate KFD IH workqueue\n"); 6962306a36Sopenharmony_ci return -ENOMEM; 7062306a36Sopenharmony_ci } 7162306a36Sopenharmony_ci spin_lock_init(&node->interrupt_lock); 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ci INIT_WORK(&node->interrupt_work, interrupt_wq); 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ci node->interrupts_active = true; 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_ci /* 7862306a36Sopenharmony_ci * After this function returns, the interrupt will be enabled. This 7962306a36Sopenharmony_ci * barrier ensures that the interrupt running on a different processor 8062306a36Sopenharmony_ci * sees all the above writes. 8162306a36Sopenharmony_ci */ 8262306a36Sopenharmony_ci smp_wmb(); 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci return 0; 8562306a36Sopenharmony_ci} 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_civoid kfd_interrupt_exit(struct kfd_node *node) 8862306a36Sopenharmony_ci{ 8962306a36Sopenharmony_ci /* 9062306a36Sopenharmony_ci * Stop the interrupt handler from writing to the ring and scheduling 9162306a36Sopenharmony_ci * workqueue items. The spinlock ensures that any interrupt running 9262306a36Sopenharmony_ci * after we have unlocked sees interrupts_active = false. 9362306a36Sopenharmony_ci */ 9462306a36Sopenharmony_ci unsigned long flags; 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci spin_lock_irqsave(&node->interrupt_lock, flags); 9762306a36Sopenharmony_ci node->interrupts_active = false; 9862306a36Sopenharmony_ci spin_unlock_irqrestore(&node->interrupt_lock, flags); 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ci /* 10162306a36Sopenharmony_ci * flush_work ensures that there are no outstanding 10262306a36Sopenharmony_ci * work-queue items that will access interrupt_ring. New work items 10362306a36Sopenharmony_ci * can't be created because we stopped interrupt handling above. 10462306a36Sopenharmony_ci */ 10562306a36Sopenharmony_ci flush_workqueue(node->ih_wq); 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci kfifo_free(&node->ih_fifo); 10862306a36Sopenharmony_ci} 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ci/* 11162306a36Sopenharmony_ci * Assumption: single reader/writer. This function is not re-entrant 11262306a36Sopenharmony_ci */ 11362306a36Sopenharmony_cibool enqueue_ih_ring_entry(struct kfd_node *node, const void *ih_ring_entry) 11462306a36Sopenharmony_ci{ 11562306a36Sopenharmony_ci int count; 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci count = kfifo_in(&node->ih_fifo, ih_ring_entry, 11862306a36Sopenharmony_ci node->kfd->device_info.ih_ring_entry_size); 11962306a36Sopenharmony_ci if (count != node->kfd->device_info.ih_ring_entry_size) { 12062306a36Sopenharmony_ci dev_dbg_ratelimited(node->adev->dev, 12162306a36Sopenharmony_ci "Interrupt ring overflow, dropping interrupt %d\n", 12262306a36Sopenharmony_ci count); 12362306a36Sopenharmony_ci return false; 12462306a36Sopenharmony_ci } 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ci return true; 12762306a36Sopenharmony_ci} 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_ci/* 13062306a36Sopenharmony_ci * Assumption: single reader/writer. This function is not re-entrant 13162306a36Sopenharmony_ci */ 13262306a36Sopenharmony_cistatic bool dequeue_ih_ring_entry(struct kfd_node *node, void *ih_ring_entry) 13362306a36Sopenharmony_ci{ 13462306a36Sopenharmony_ci int count; 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ci count = kfifo_out(&node->ih_fifo, ih_ring_entry, 13762306a36Sopenharmony_ci node->kfd->device_info.ih_ring_entry_size); 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ci WARN_ON(count && count != node->kfd->device_info.ih_ring_entry_size); 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_ci return count == node->kfd->device_info.ih_ring_entry_size; 14262306a36Sopenharmony_ci} 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_cistatic void interrupt_wq(struct work_struct *work) 14562306a36Sopenharmony_ci{ 14662306a36Sopenharmony_ci struct kfd_node *dev = container_of(work, struct kfd_node, 14762306a36Sopenharmony_ci interrupt_work); 14862306a36Sopenharmony_ci uint32_t ih_ring_entry[KFD_MAX_RING_ENTRY_SIZE]; 14962306a36Sopenharmony_ci unsigned long start_jiffies = jiffies; 15062306a36Sopenharmony_ci 15162306a36Sopenharmony_ci if (dev->kfd->device_info.ih_ring_entry_size > sizeof(ih_ring_entry)) { 15262306a36Sopenharmony_ci dev_err_once(dev->adev->dev, "Ring entry too small\n"); 15362306a36Sopenharmony_ci return; 15462306a36Sopenharmony_ci } 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_ci while (dequeue_ih_ring_entry(dev, ih_ring_entry)) { 15762306a36Sopenharmony_ci dev->kfd->device_info.event_interrupt_class->interrupt_wq(dev, 15862306a36Sopenharmony_ci ih_ring_entry); 15962306a36Sopenharmony_ci if (time_is_before_jiffies(start_jiffies + HZ)) { 16062306a36Sopenharmony_ci /* If we spent more than a second processing signals, 16162306a36Sopenharmony_ci * reschedule the worker to avoid soft-lockup warnings 16262306a36Sopenharmony_ci */ 16362306a36Sopenharmony_ci queue_work(dev->ih_wq, &dev->interrupt_work); 16462306a36Sopenharmony_ci break; 16562306a36Sopenharmony_ci } 16662306a36Sopenharmony_ci } 16762306a36Sopenharmony_ci} 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_cibool interrupt_is_wanted(struct kfd_node *dev, 17062306a36Sopenharmony_ci const uint32_t *ih_ring_entry, 17162306a36Sopenharmony_ci uint32_t *patched_ihre, bool *flag) 17262306a36Sopenharmony_ci{ 17362306a36Sopenharmony_ci /* integer and bitwise OR so there is no boolean short-circuiting */ 17462306a36Sopenharmony_ci unsigned int wanted = 0; 17562306a36Sopenharmony_ci 17662306a36Sopenharmony_ci wanted |= dev->kfd->device_info.event_interrupt_class->interrupt_isr(dev, 17762306a36Sopenharmony_ci ih_ring_entry, patched_ihre, flag); 17862306a36Sopenharmony_ci 17962306a36Sopenharmony_ci return wanted != 0; 18062306a36Sopenharmony_ci} 181