1 /*
2  * Copyright 2018 Red Hat 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 #include "priv.h"
23 
24 #include <core/memory.h>
25 #include <subdev/mmu.h>
26 #include <engine/fifo.h>
27 
28 static void
29 gv100_fault_buffer_process(struct nvkm_fault_buffer *buffer)
30 {
31 	struct nvkm_device *device = buffer->fault->subdev.device;
32 	struct nvkm_memory *mem = buffer->mem;
33 	const u32 foff = buffer->id * 0x14;
34 	u32 get = nvkm_rd32(device, 0x100e2c + foff);
35 	u32 put = nvkm_rd32(device, 0x100e30 + foff);
36 	if (put == get)
37 		return;
38 
39 	nvkm_kmap(mem);
40 	while (get != put) {
41 		const u32   base = get * buffer->fault->func->buffer.entry_size;
42 		const u32 instlo = nvkm_ro32(mem, base + 0x00);
43 		const u32 insthi = nvkm_ro32(mem, base + 0x04);
44 		const u32 addrlo = nvkm_ro32(mem, base + 0x08);
45 		const u32 addrhi = nvkm_ro32(mem, base + 0x0c);
46 		const u32 timelo = nvkm_ro32(mem, base + 0x10);
47 		const u32 timehi = nvkm_ro32(mem, base + 0x14);
48 		const u32  info0 = nvkm_ro32(mem, base + 0x18);
49 		const u32  info1 = nvkm_ro32(mem, base + 0x1c);
50 		struct nvkm_fault_data info;
51 
52 		if (++get == buffer->entries)
53 			get = 0;
54 		nvkm_wr32(device, 0x100e2c + foff, get);
55 
56 		info.addr   = ((u64)addrhi << 32) | addrlo;
57 		info.inst   = ((u64)insthi << 32) | instlo;
58 		info.time   = ((u64)timehi << 32) | timelo;
59 		info.engine = (info0 & 0x000000ff);
60 		info.valid  = (info1 & 0x80000000) >> 31;
61 		info.gpc    = (info1 & 0x1f000000) >> 24;
62 		info.hub    = (info1 & 0x00100000) >> 20;
63 		info.access = (info1 & 0x000f0000) >> 16;
64 		info.client = (info1 & 0x00007f00) >> 8;
65 		info.reason = (info1 & 0x0000001f);
66 
67 		nvkm_fifo_fault(device->fifo, &info);
68 	}
69 	nvkm_done(mem);
70 }
71 
72 static void
73 gv100_fault_buffer_fini(struct nvkm_fault_buffer *buffer)
74 {
75 	struct nvkm_device *device = buffer->fault->subdev.device;
76 	const u32 intr = buffer->id ? 0x08000000 : 0x20000000;
77 	const u32 foff = buffer->id * 0x14;
78 
79 	nvkm_mask(device, 0x100a34, intr, intr);
80 	nvkm_mask(device, 0x100e34 + foff, 0x80000000, 0x00000000);
81 }
82 
83 static void
84 gv100_fault_buffer_init(struct nvkm_fault_buffer *buffer)
85 {
86 	struct nvkm_device *device = buffer->fault->subdev.device;
87 	const u32 intr = buffer->id ? 0x08000000 : 0x20000000;
88 	const u32 foff = buffer->id * 0x14;
89 
90 	nvkm_mask(device, 0x100e34 + foff, 0xc0000000, 0x40000000);
91 	nvkm_wr32(device, 0x100e28 + foff, upper_32_bits(buffer->vma->addr));
92 	nvkm_wr32(device, 0x100e24 + foff, lower_32_bits(buffer->vma->addr));
93 	nvkm_mask(device, 0x100e34 + foff, 0x80000000, 0x80000000);
94 	nvkm_mask(device, 0x100a2c, intr, intr);
95 }
96 
97 static u32
98 gv100_fault_buffer_entries(struct nvkm_fault_buffer *buffer)
99 {
100 	struct nvkm_device *device = buffer->fault->subdev.device;
101 	const u32 foff = buffer->id * 0x14;
102 	nvkm_mask(device, 0x100e34 + foff, 0x40000000, 0x40000000);
103 	return nvkm_rd32(device, 0x100e34 + foff) & 0x000fffff;
104 }
105 
106 static int
107 gv100_fault_ntfy_nrpfb(struct nvkm_notify *notify)
108 {
109 	struct nvkm_fault *fault = container_of(notify, typeof(*fault), nrpfb);
110 	gv100_fault_buffer_process(fault->buffer[0]);
111 	return NVKM_NOTIFY_KEEP;
112 }
113 
114 static void
115 gv100_fault_intr_fault(struct nvkm_fault *fault)
116 {
117 	struct nvkm_subdev *subdev = &fault->subdev;
118 	struct nvkm_device *device = subdev->device;
119 	struct nvkm_fault_data info;
120 	const u32 addrlo = nvkm_rd32(device, 0x100e4c);
121 	const u32 addrhi = nvkm_rd32(device, 0x100e50);
122 	const u32  info0 = nvkm_rd32(device, 0x100e54);
123 	const u32 insthi = nvkm_rd32(device, 0x100e58);
124 	const u32  info1 = nvkm_rd32(device, 0x100e5c);
125 
126 	info.addr = ((u64)addrhi << 32) | addrlo;
127 	info.inst = ((u64)insthi << 32) | (info0 & 0xfffff000);
128 	info.time = 0;
129 	info.engine = (info0 & 0x000000ff);
130 	info.valid  = (info1 & 0x80000000) >> 31;
131 	info.gpc    = (info1 & 0x1f000000) >> 24;
132 	info.hub    = (info1 & 0x00100000) >> 20;
133 	info.access = (info1 & 0x000f0000) >> 16;
134 	info.client = (info1 & 0x00007f00) >> 8;
135 	info.reason = (info1 & 0x0000001f);
136 
137 	nvkm_fifo_fault(device->fifo, &info);
138 }
139 
140 static void
141 gv100_fault_intr(struct nvkm_fault *fault)
142 {
143 	struct nvkm_subdev *subdev = &fault->subdev;
144 	struct nvkm_device *device = subdev->device;
145 	u32 stat = nvkm_rd32(device, 0x100a20);
146 
147 	if (stat & 0x80000000) {
148 		gv100_fault_intr_fault(fault);
149 		nvkm_wr32(device, 0x100e60, 0x80000000);
150 		stat &= ~0x80000000;
151 	}
152 
153 	if (stat & 0x20000000) {
154 		if (fault->buffer[0]) {
155 			nvkm_event_send(&fault->event, 1, 0, NULL, 0);
156 			stat &= ~0x20000000;
157 		}
158 	}
159 
160 	if (stat) {
161 		nvkm_debug(subdev, "intr %08x\n", stat);
162 	}
163 }
164 
165 static void
166 gv100_fault_fini(struct nvkm_fault *fault)
167 {
168 	nvkm_notify_put(&fault->nrpfb);
169 	nvkm_mask(fault->subdev.device, 0x100a34, 0x80000000, 0x80000000);
170 }
171 
172 static void
173 gv100_fault_init(struct nvkm_fault *fault)
174 {
175 	nvkm_mask(fault->subdev.device, 0x100a2c, 0x80000000, 0x80000000);
176 	nvkm_notify_get(&fault->nrpfb);
177 }
178 
179 static int
180 gv100_fault_oneinit(struct nvkm_fault *fault)
181 {
182 	return nvkm_notify_init(&fault->buffer[0]->object, &fault->event,
183 				gv100_fault_ntfy_nrpfb, false, NULL, 0, 0,
184 				&fault->nrpfb);
185 }
186 
187 static const struct nvkm_fault_func
188 gv100_fault = {
189 	.oneinit = gv100_fault_oneinit,
190 	.init = gv100_fault_init,
191 	.fini = gv100_fault_fini,
192 	.intr = gv100_fault_intr,
193 	.buffer.nr = 2,
194 	.buffer.entry_size = 32,
195 	.buffer.entries = gv100_fault_buffer_entries,
196 	.buffer.init = gv100_fault_buffer_init,
197 	.buffer.fini = gv100_fault_buffer_fini,
198 };
199 
200 int
201 gv100_fault_new(struct nvkm_device *device, int index,
202 		struct nvkm_fault **pfault)
203 {
204 	return nvkm_fault_new_(&gv100_fault, device, index, pfault);
205 }
206