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/gsp.h>
26 #include <subdev/mc.h>
27 #include <subdev/mmu.h>
28 #include <subdev/vfn.h>
29 #include <engine/fifo.h>
30 
31 #include <nvif/class.h>
32 
33 static irqreturn_t
tu102_fault_buffer_notify(struct nvkm_inth * inth)34 tu102_fault_buffer_notify(struct nvkm_inth *inth)
35 {
36 	struct nvkm_fault_buffer *buffer = container_of(inth, typeof(*buffer), inth);
37 
38 	nvkm_event_ntfy(&buffer->fault->event, buffer->id, NVKM_FAULT_BUFFER_EVENT_PENDING);
39 	return IRQ_HANDLED;
40 }
41 
42 static void
tu102_fault_buffer_intr(struct nvkm_fault_buffer * buffer,bool enable)43 tu102_fault_buffer_intr(struct nvkm_fault_buffer *buffer, bool enable)
44 {
45 	if (enable)
46 		nvkm_inth_allow(&buffer->inth);
47 	else
48 		nvkm_inth_block(&buffer->inth);
49 }
50 
51 static void
tu102_fault_buffer_fini(struct nvkm_fault_buffer * buffer)52 tu102_fault_buffer_fini(struct nvkm_fault_buffer *buffer)
53 {
54 	struct nvkm_device *device = buffer->fault->subdev.device;
55 	const u32 foff = buffer->id * 0x20;
56 
57 	nvkm_mask(device, 0xb83010 + foff, 0x80000000, 0x00000000);
58 }
59 
60 static void
tu102_fault_buffer_init(struct nvkm_fault_buffer * buffer)61 tu102_fault_buffer_init(struct nvkm_fault_buffer *buffer)
62 {
63 	struct nvkm_device *device = buffer->fault->subdev.device;
64 	const u32 foff = buffer->id * 0x20;
65 
66 	nvkm_mask(device, 0xb83010 + foff, 0xc0000000, 0x40000000);
67 	nvkm_wr32(device, 0xb83004 + foff, upper_32_bits(buffer->addr));
68 	nvkm_wr32(device, 0xb83000 + foff, lower_32_bits(buffer->addr));
69 	nvkm_mask(device, 0xb83010 + foff, 0x80000000, 0x80000000);
70 }
71 
72 static void
tu102_fault_buffer_info(struct nvkm_fault_buffer * buffer)73 tu102_fault_buffer_info(struct nvkm_fault_buffer *buffer)
74 {
75 	struct nvkm_device *device = buffer->fault->subdev.device;
76 	const u32 foff = buffer->id * 0x20;
77 
78 	nvkm_mask(device, 0xb83010 + foff, 0x40000000, 0x40000000);
79 
80 	buffer->entries = nvkm_rd32(device, 0xb83010 + foff) & 0x000fffff;
81 	buffer->get = 0xb83008 + foff;
82 	buffer->put = 0xb8300c + foff;
83 }
84 
85 static irqreturn_t
tu102_fault_info_fault(struct nvkm_inth * inth)86 tu102_fault_info_fault(struct nvkm_inth *inth)
87 {
88 	struct nvkm_fault *fault = container_of(inth, typeof(*fault), info_fault);
89 	struct nvkm_subdev *subdev = &fault->subdev;
90 	struct nvkm_device *device = subdev->device;
91 	struct nvkm_fault_data info;
92 	const u32 addrlo = nvkm_rd32(device, 0xb83080);
93 	const u32 addrhi = nvkm_rd32(device, 0xb83084);
94 	const u32  info0 = nvkm_rd32(device, 0xb83088);
95 	const u32 insthi = nvkm_rd32(device, 0xb8308c);
96 	const u32  info1 = nvkm_rd32(device, 0xb83090);
97 
98 	info.addr = ((u64)addrhi << 32) | addrlo;
99 	info.inst = ((u64)insthi << 32) | (info0 & 0xfffff000);
100 	info.time = 0;
101 	info.engine = (info0 & 0x000000ff);
102 	info.valid  = (info1 & 0x80000000) >> 31;
103 	info.gpc    = (info1 & 0x1f000000) >> 24;
104 	info.hub    = (info1 & 0x00100000) >> 20;
105 	info.access = (info1 & 0x000f0000) >> 16;
106 	info.client = (info1 & 0x00007f00) >> 8;
107 	info.reason = (info1 & 0x0000001f);
108 
109 	nvkm_fifo_fault(device->fifo, &info);
110 
111 	nvkm_wr32(device, 0xb83094, 0x80000000);
112 	return IRQ_HANDLED;
113 }
114 
115 static void
tu102_fault_fini(struct nvkm_fault * fault)116 tu102_fault_fini(struct nvkm_fault *fault)
117 {
118 	nvkm_event_ntfy_block(&fault->nrpfb);
119 	flush_work(&fault->nrpfb_work);
120 
121 	if (fault->buffer[0])
122 		fault->func->buffer.fini(fault->buffer[0]);
123 
124 	nvkm_inth_block(&fault->info_fault);
125 }
126 
127 static void
tu102_fault_init(struct nvkm_fault * fault)128 tu102_fault_init(struct nvkm_fault *fault)
129 {
130 	nvkm_inth_allow(&fault->info_fault);
131 
132 	fault->func->buffer.init(fault->buffer[0]);
133 	nvkm_event_ntfy_allow(&fault->nrpfb);
134 }
135 
136 static int
tu102_fault_oneinit(struct nvkm_fault * fault)137 tu102_fault_oneinit(struct nvkm_fault *fault)
138 {
139 	struct nvkm_device *device = fault->subdev.device;
140 	struct nvkm_intr *intr = &device->vfn->intr;
141 	int ret, i;
142 
143 	ret = nvkm_inth_add(intr, nvkm_rd32(device, 0x100ee0) & 0x0000ffff,
144 			    NVKM_INTR_PRIO_NORMAL, &fault->subdev, tu102_fault_info_fault,
145 			    &fault->info_fault);
146 	if (ret)
147 		return ret;
148 
149 	for (i = 0; i < fault->buffer_nr; i++) {
150 		ret = nvkm_inth_add(intr, nvkm_rd32(device, 0x100ee4 + (i * 4)) >> 16,
151 				    NVKM_INTR_PRIO_NORMAL, &fault->subdev,
152 				    tu102_fault_buffer_notify, &fault->buffer[i]->inth);
153 		if (ret)
154 			return ret;
155 	}
156 
157 	return gv100_fault_oneinit(fault);
158 }
159 
160 static const struct nvkm_fault_func
161 tu102_fault = {
162 	.oneinit = tu102_fault_oneinit,
163 	.init = tu102_fault_init,
164 	.fini = tu102_fault_fini,
165 	.buffer.nr = 2,
166 	.buffer.entry_size = 32,
167 	.buffer.info = tu102_fault_buffer_info,
168 	.buffer.pin = gp100_fault_buffer_pin,
169 	.buffer.init = tu102_fault_buffer_init,
170 	.buffer.fini = tu102_fault_buffer_fini,
171 	.buffer.intr = tu102_fault_buffer_intr,
172 	.user = { { 0, 0, VOLTA_FAULT_BUFFER_A }, 1 },
173 };
174 
175 int
tu102_fault_new(struct nvkm_device * device,enum nvkm_subdev_type type,int inst,struct nvkm_fault ** pfault)176 tu102_fault_new(struct nvkm_device *device, enum nvkm_subdev_type type, int inst,
177 		struct nvkm_fault **pfault)
178 {
179 	int ret;
180 
181 	if (nvkm_gsp_rm(device->gsp))
182 		return -ENODEV;
183 
184 	ret = nvkm_fault_new_(&tu102_fault, device, type, inst, pfault);
185 	if (ret)
186 		return ret;
187 
188 	INIT_WORK(&(*pfault)->nrpfb_work, gv100_fault_buffer_process);
189 	return 0;
190 }
191