Home | History | Annotate | Line # | Download | only in amdkfd
      1 /*	$NetBSD: kfd_int_process_v9.c,v 1.2 2021/12/18 23:44:59 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright 2016-2018 Advanced Micro Devices, Inc.
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included in
     14  * all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  * OTHER DEALINGS IN THE SOFTWARE.
     23  */
     24 
     25 #include <sys/cdefs.h>
     26 __KERNEL_RCSID(0, "$NetBSD: kfd_int_process_v9.c,v 1.2 2021/12/18 23:44:59 riastradh Exp $");
     27 
     28 #include "kfd_priv.h"
     29 #include "kfd_events.h"
     30 #include "soc15_int.h"
     31 #include "kfd_device_queue_manager.h"
     32 
     33 static bool event_interrupt_isr_v9(struct kfd_dev *dev,
     34 					const uint32_t *ih_ring_entry,
     35 					uint32_t *patched_ihre,
     36 					bool *patched_flag)
     37 {
     38 	uint16_t source_id, client_id, pasid, vmid;
     39 	const uint32_t *data = ih_ring_entry;
     40 
     41 	/* Only handle interrupts from KFD VMIDs */
     42 	vmid = SOC15_VMID_FROM_IH_ENTRY(ih_ring_entry);
     43 	if (vmid < dev->vm_info.first_vmid_kfd ||
     44 	    vmid > dev->vm_info.last_vmid_kfd)
     45 		return 0;
     46 
     47 	source_id = SOC15_SOURCE_ID_FROM_IH_ENTRY(ih_ring_entry);
     48 	client_id = SOC15_CLIENT_ID_FROM_IH_ENTRY(ih_ring_entry);
     49 	pasid = SOC15_PASID_FROM_IH_ENTRY(ih_ring_entry);
     50 
     51 	/* This is a known issue for gfx9. Under non HWS, pasid is not set
     52 	 * in the interrupt payload, so we need to find out the pasid on our
     53 	 * own.
     54 	 */
     55 	if (!pasid && dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
     56 		const uint32_t pasid_mask = 0xffff;
     57 
     58 		*patched_flag = true;
     59 		memcpy(patched_ihre, ih_ring_entry,
     60 				dev->device_info->ih_ring_entry_size);
     61 
     62 		pasid = dev->dqm->vmid_pasid[vmid];
     63 
     64 		/* Patch the pasid field */
     65 		patched_ihre[3] = cpu_to_le32((le32_to_cpu(patched_ihre[3])
     66 					& ~pasid_mask) | pasid);
     67 	}
     68 
     69 	pr_debug("client id 0x%x, source id %d, vmid %d, pasid 0x%x. raw data:\n",
     70 		 client_id, source_id, vmid, pasid);
     71 	pr_debug("%8X, %8X, %8X, %8X, %8X, %8X, %8X, %8X.\n",
     72 		 data[0], data[1], data[2], data[3],
     73 		 data[4], data[5], data[6], data[7]);
     74 
     75 	/* If there is no valid PASID, it's likely a bug */
     76 	if (WARN_ONCE(pasid == 0, "Bug: No PASID in KFD interrupt"))
     77 		return 0;
     78 
     79 	/* Interrupt types we care about: various signals and faults.
     80 	 * They will be forwarded to a work queue (see below).
     81 	 */
     82 	return source_id == SOC15_INTSRC_CP_END_OF_PIPE ||
     83 		source_id == SOC15_INTSRC_SDMA_TRAP ||
     84 		source_id == SOC15_INTSRC_SQ_INTERRUPT_MSG ||
     85 		source_id == SOC15_INTSRC_CP_BAD_OPCODE ||
     86 		client_id == SOC15_IH_CLIENTID_VMC ||
     87 		client_id == SOC15_IH_CLIENTID_VMC1 ||
     88 		client_id == SOC15_IH_CLIENTID_UTCL2;
     89 }
     90 
     91 static void event_interrupt_wq_v9(struct kfd_dev *dev,
     92 					const uint32_t *ih_ring_entry)
     93 {
     94 	uint16_t source_id, client_id, pasid, vmid;
     95 	uint32_t context_id;
     96 
     97 	source_id = SOC15_SOURCE_ID_FROM_IH_ENTRY(ih_ring_entry);
     98 	client_id = SOC15_CLIENT_ID_FROM_IH_ENTRY(ih_ring_entry);
     99 	pasid = SOC15_PASID_FROM_IH_ENTRY(ih_ring_entry);
    100 	vmid = SOC15_VMID_FROM_IH_ENTRY(ih_ring_entry);
    101 	context_id = SOC15_CONTEXT_ID0_FROM_IH_ENTRY(ih_ring_entry);
    102 
    103 	if (source_id == SOC15_INTSRC_CP_END_OF_PIPE)
    104 		kfd_signal_event_interrupt(pasid, context_id, 32);
    105 	else if (source_id == SOC15_INTSRC_SDMA_TRAP)
    106 		kfd_signal_event_interrupt(pasid, context_id & 0xfffffff, 28);
    107 	else if (source_id == SOC15_INTSRC_SQ_INTERRUPT_MSG)
    108 		kfd_signal_event_interrupt(pasid, context_id & 0xffffff, 24);
    109 	else if (source_id == SOC15_INTSRC_CP_BAD_OPCODE)
    110 		kfd_signal_hw_exception_event(pasid);
    111 	else if (client_id == SOC15_IH_CLIENTID_VMC ||
    112 		client_id == SOC15_IH_CLIENTID_VMC1 ||
    113 		 client_id == SOC15_IH_CLIENTID_UTCL2) {
    114 		struct kfd_vm_fault_info info = {0};
    115 		uint16_t ring_id = SOC15_RING_ID_FROM_IH_ENTRY(ih_ring_entry);
    116 
    117 		info.vmid = vmid;
    118 		info.mc_id = client_id;
    119 		info.page_addr = ih_ring_entry[4] |
    120 			(uint64_t)(ih_ring_entry[5] & 0xf) << 32;
    121 		info.prot_valid = ring_id & 0x08;
    122 		info.prot_read  = ring_id & 0x10;
    123 		info.prot_write = ring_id & 0x20;
    124 
    125 		kfd_process_vm_fault(dev->dqm, pasid);
    126 		kfd_signal_vm_fault_event(dev, pasid, &info);
    127 	}
    128 }
    129 
    130 const struct kfd_event_interrupt_class event_interrupt_class_v9 = {
    131 	.interrupt_isr = event_interrupt_isr_v9,
    132 	.interrupt_wq = event_interrupt_wq_v9,
    133 };
    134