Path: blob/master/drivers/gpu/drm/amd/amdkfd/kfd_interrupt.c
26516 views
// SPDX-License-Identifier: GPL-2.0 OR MIT1/*2* Copyright 2014-2022 Advanced Micro Devices, Inc.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice shall be included in12* all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR18* 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 OR20* OTHER DEALINGS IN THE SOFTWARE.21*/2223/*24* KFD Interrupts.25*26* AMD GPUs deliver interrupts by pushing an interrupt description onto the27* interrupt ring and then sending an interrupt. KGD receives the interrupt28* in ISR and sends us a pointer to each new entry on the interrupt ring.29*30* We generally can't process interrupt-signaled events from ISR, so we call31* out to each interrupt client module (currently only the scheduler) to ask if32* each interrupt is interesting. If they return true, then it requires further33* processing so we copy it to an internal interrupt ring and call each34* interrupt client again from a work-queue.35*36* There's no acknowledgment for the interrupts we use. The hardware simply37* queues a new interrupt each time without waiting.38*39* The fixed-size internal queue means that it's possible for us to lose40* interrupts because we have no back-pressure to the hardware.41*/4243#include <linux/slab.h>44#include <linux/device.h>45#include <linux/kfifo.h>46#include "kfd_priv.h"4748#define KFD_IH_NUM_ENTRIES 163844950static void interrupt_wq(struct work_struct *);5152int kfd_interrupt_init(struct kfd_node *node)53{54int r;5556r = kfifo_alloc(&node->ih_fifo,57KFD_IH_NUM_ENTRIES * node->kfd->device_info.ih_ring_entry_size,58GFP_KERNEL);59if (r) {60dev_err(node->adev->dev, "Failed to allocate IH fifo\n");61return r;62}6364if (!node->kfd->ih_wq) {65node->kfd->ih_wq = alloc_workqueue("KFD IH", WQ_HIGHPRI | WQ_UNBOUND,66node->kfd->num_nodes);67if (unlikely(!node->kfd->ih_wq)) {68kfifo_free(&node->ih_fifo);69dev_err(node->adev->dev, "Failed to allocate KFD IH workqueue\n");70return -ENOMEM;71}72}73spin_lock_init(&node->interrupt_lock);7475INIT_WORK(&node->interrupt_work, interrupt_wq);7677node->interrupts_active = true;7879/*80* After this function returns, the interrupt will be enabled. This81* barrier ensures that the interrupt running on a different processor82* sees all the above writes.83*/84smp_wmb();8586return 0;87}8889void kfd_interrupt_exit(struct kfd_node *node)90{91/*92* Stop the interrupt handler from writing to the ring and scheduling93* workqueue items. The spinlock ensures that any interrupt running94* after we have unlocked sees interrupts_active = false.95*/96unsigned long flags;9798spin_lock_irqsave(&node->interrupt_lock, flags);99node->interrupts_active = false;100spin_unlock_irqrestore(&node->interrupt_lock, flags);101kfifo_free(&node->ih_fifo);102}103104/*105* Assumption: single reader/writer. This function is not re-entrant106*/107bool enqueue_ih_ring_entry(struct kfd_node *node, const void *ih_ring_entry)108{109if (kfifo_is_full(&node->ih_fifo)) {110dev_warn_ratelimited(node->adev->dev, "KFD node %d ih_fifo overflow\n",111node->node_id);112return false;113}114115kfifo_in(&node->ih_fifo, ih_ring_entry, node->kfd->device_info.ih_ring_entry_size);116return true;117}118119/*120* Assumption: single reader/writer. This function is not re-entrant121*/122static bool dequeue_ih_ring_entry(struct kfd_node *node, u32 **ih_ring_entry)123{124int count;125126if (kfifo_is_empty(&node->ih_fifo))127return false;128129count = kfifo_out_linear_ptr(&node->ih_fifo, ih_ring_entry,130node->kfd->device_info.ih_ring_entry_size);131WARN_ON(count != node->kfd->device_info.ih_ring_entry_size);132return count == node->kfd->device_info.ih_ring_entry_size;133}134135static void interrupt_wq(struct work_struct *work)136{137struct kfd_node *dev = container_of(work, struct kfd_node, interrupt_work);138uint32_t *ih_ring_entry;139unsigned long start_jiffies = jiffies;140141while (dequeue_ih_ring_entry(dev, &ih_ring_entry)) {142dev->kfd->device_info.event_interrupt_class->interrupt_wq(dev,143ih_ring_entry);144kfifo_skip_count(&dev->ih_fifo, dev->kfd->device_info.ih_ring_entry_size);145146if (time_is_before_jiffies(start_jiffies + HZ)) {147/* If we spent more than a second processing signals,148* reschedule the worker to avoid soft-lockup warnings149*/150queue_work(dev->kfd->ih_wq, &dev->interrupt_work);151break;152}153}154}155156bool interrupt_is_wanted(struct kfd_node *dev,157const uint32_t *ih_ring_entry,158uint32_t *patched_ihre, bool *flag)159{160/* integer and bitwise OR so there is no boolean short-circuiting */161unsigned int wanted = 0;162163wanted |= dev->kfd->device_info.event_interrupt_class->interrupt_isr(dev,164ih_ring_entry, patched_ihre, flag);165166return wanted != 0;167}168169170