1 /*	$NetBSD: cik_event_interrupt.c,v 1.3 2021/12/18 23:44:59 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2014 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: cik_event_interrupt.c,v 1.3 2021/12/18 23:44:59 riastradh Exp $");
27 
28 #include "kfd_priv.h"
29 #include "kfd_events.h"
30 #include "cik_int.h"
31 #include "amdgpu_amdkfd.h"
32 
cik_event_interrupt_isr(struct kfd_dev * dev,const uint32_t * ih_ring_entry,uint32_t * patched_ihre,bool * patched_flag)33 static bool cik_event_interrupt_isr(struct kfd_dev *dev,
34 					const uint32_t *ih_ring_entry,
35 					uint32_t *patched_ihre,
36 					bool *patched_flag)
37 {
38 	const struct cik_ih_ring_entry *ihre =
39 			(const struct cik_ih_ring_entry *)ih_ring_entry;
40 	const struct kfd2kgd_calls *f2g = dev->kfd2kgd;
41 	unsigned int vmid;
42 	uint16_t pasid;
43 	bool ret;
44 
45 	/* This workaround is due to HW/FW limitation on Hawaii that
46 	 * VMID and PASID are not written into ih_ring_entry
47 	 */
48 	if ((ihre->source_id == CIK_INTSRC_GFX_PAGE_INV_FAULT ||
49 		ihre->source_id == CIK_INTSRC_GFX_MEM_PROT_FAULT) &&
50 		dev->device_info->asic_family == CHIP_HAWAII) {
51 		struct cik_ih_ring_entry *tmp_ihre =
52 			(struct cik_ih_ring_entry *)patched_ihre;
53 
54 		*patched_flag = true;
55 		*tmp_ihre = *ihre;
56 
57 		vmid = f2g->read_vmid_from_vmfault_reg(dev->kgd);
58 		ret = f2g->get_atc_vmid_pasid_mapping_info(dev->kgd, vmid, &pasid);
59 
60 		tmp_ihre->ring_id &= 0x000000ff;
61 		tmp_ihre->ring_id |= vmid << 8;
62 		tmp_ihre->ring_id |= pasid << 16;
63 
64 		return ret && (pasid != 0) &&
65 			vmid >= dev->vm_info.first_vmid_kfd &&
66 			vmid <= dev->vm_info.last_vmid_kfd;
67 	}
68 
69 	/* Only handle interrupts from KFD VMIDs */
70 	vmid  = (ihre->ring_id & 0x0000ff00) >> 8;
71 	if (vmid < dev->vm_info.first_vmid_kfd ||
72 	    vmid > dev->vm_info.last_vmid_kfd)
73 		return 0;
74 
75 	/* If there is no valid PASID, it's likely a firmware bug */
76 	pasid = (ihre->ring_id & 0xffff0000) >> 16;
77 	if (WARN_ONCE(pasid == 0, "FW bug: No PASID in KFD interrupt"))
78 		return 0;
79 
80 	/* Interrupt types we care about: various signals and faults.
81 	 * They will be forwarded to a work queue (see below).
82 	 */
83 	return ihre->source_id == CIK_INTSRC_CP_END_OF_PIPE ||
84 		ihre->source_id == CIK_INTSRC_SDMA_TRAP ||
85 		ihre->source_id == CIK_INTSRC_SQ_INTERRUPT_MSG ||
86 		ihre->source_id == CIK_INTSRC_CP_BAD_OPCODE ||
87 		ihre->source_id == CIK_INTSRC_GFX_PAGE_INV_FAULT ||
88 		ihre->source_id == CIK_INTSRC_GFX_MEM_PROT_FAULT;
89 }
90 
cik_event_interrupt_wq(struct kfd_dev * dev,const uint32_t * ih_ring_entry)91 static void cik_event_interrupt_wq(struct kfd_dev *dev,
92 					const uint32_t *ih_ring_entry)
93 {
94 	const struct cik_ih_ring_entry *ihre =
95 			(const struct cik_ih_ring_entry *)ih_ring_entry;
96 	uint32_t context_id = ihre->data & 0xfffffff;
97 	unsigned int vmid  = (ihre->ring_id & 0x0000ff00) >> 8;
98 	unsigned int pasid = (ihre->ring_id & 0xffff0000) >> 16;
99 
100 	if (pasid == 0)
101 		return;
102 
103 	if (ihre->source_id == CIK_INTSRC_CP_END_OF_PIPE)
104 		kfd_signal_event_interrupt(pasid, context_id, 28);
105 	else if (ihre->source_id == CIK_INTSRC_SDMA_TRAP)
106 		kfd_signal_event_interrupt(pasid, context_id, 28);
107 	else if (ihre->source_id == CIK_INTSRC_SQ_INTERRUPT_MSG)
108 		kfd_signal_event_interrupt(pasid, context_id & 0xff, 8);
109 	else if (ihre->source_id == CIK_INTSRC_CP_BAD_OPCODE)
110 		kfd_signal_hw_exception_event(pasid);
111 	else if (ihre->source_id == CIK_INTSRC_GFX_PAGE_INV_FAULT ||
112 		ihre->source_id == CIK_INTSRC_GFX_MEM_PROT_FAULT) {
113 		struct kfd_vm_fault_info info;
114 
115 		kfd_process_vm_fault(dev->dqm, pasid);
116 
117 		memset(&info, 0, sizeof(info));
118 		amdgpu_amdkfd_gpuvm_get_vm_fault_info(dev->kgd, &info);
119 		if (!info.page_addr && !info.status)
120 			return;
121 
122 		if (info.vmid == vmid)
123 			kfd_signal_vm_fault_event(dev, pasid, &info);
124 		else
125 			kfd_signal_vm_fault_event(dev, pasid, NULL);
126 	}
127 }
128 
129 const struct kfd_event_interrupt_class event_interrupt_class_cik = {
130 	.interrupt_isr = cik_event_interrupt_isr,
131 	.interrupt_wq = cik_event_interrupt_wq,
132 };
133