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