2 * Copyright 2018 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
24 #include <core/memory.h>
25 #include <core/notify.h>
26 #include <subdev/bar.h>
27 #include <subdev/mmu.h>
30 nvkm_fault_ntfy_fini(struct nvkm_event *event, int type, int index)
32 struct nvkm_fault *fault = container_of(event, typeof(*fault), event);
33 fault->func->buffer.fini(fault->buffer[index]);
37 nvkm_fault_ntfy_init(struct nvkm_event *event, int type, int index)
39 struct nvkm_fault *fault = container_of(event, typeof(*fault), event);
40 fault->func->buffer.init(fault->buffer[index]);
44 nvkm_fault_ntfy_ctor(struct nvkm_object *object, void *argv, u32 argc,
45 struct nvkm_notify *notify)
47 struct nvkm_fault_buffer *buffer = nvkm_fault_buffer(object);
51 notify->index = buffer->id;
57 static const struct nvkm_event_func
59 .ctor = nvkm_fault_ntfy_ctor,
60 .init = nvkm_fault_ntfy_init,
61 .fini = nvkm_fault_ntfy_fini,
65 nvkm_fault_intr(struct nvkm_subdev *subdev)
67 struct nvkm_fault *fault = nvkm_fault(subdev);
68 return fault->func->intr(fault);
72 nvkm_fault_fini(struct nvkm_subdev *subdev, bool suspend)
74 struct nvkm_fault *fault = nvkm_fault(subdev);
75 if (fault->func->fini)
76 fault->func->fini(fault);
81 nvkm_fault_init(struct nvkm_subdev *subdev)
83 struct nvkm_fault *fault = nvkm_fault(subdev);
84 if (fault->func->init)
85 fault->func->init(fault);
90 nvkm_fault_oneinit_buffer(struct nvkm_fault *fault, int id)
92 struct nvkm_subdev *subdev = &fault->subdev;
93 struct nvkm_device *device = subdev->device;
94 struct nvkm_vmm *bar2 = nvkm_bar_bar2_vmm(device);
95 struct nvkm_fault_buffer *buffer;
98 if (!(buffer = kzalloc(sizeof(*buffer), GFP_KERNEL)))
100 buffer->fault = fault;
102 buffer->entries = fault->func->buffer.entries(buffer);
103 fault->buffer[id] = buffer;
105 nvkm_debug(subdev, "buffer %d: %d entries\n", id, buffer->entries);
107 ret = nvkm_memory_new(device, NVKM_MEM_TARGET_INST, buffer->entries *
108 fault->func->buffer.entry_size, 0x1000, true,
113 ret = nvkm_vmm_get(bar2, 12, nvkm_memory_size(buffer->mem),
118 return nvkm_memory_map(buffer->mem, 0, bar2, buffer->vma, NULL, 0);
122 nvkm_fault_oneinit(struct nvkm_subdev *subdev)
124 struct nvkm_fault *fault = nvkm_fault(subdev);
127 for (i = 0; i < ARRAY_SIZE(fault->buffer); i++) {
128 if (i < fault->func->buffer.nr) {
129 ret = nvkm_fault_oneinit_buffer(fault, i);
132 fault->buffer_nr = i + 1;
136 ret = nvkm_event_init(&nvkm_fault_ntfy, 1, fault->buffer_nr,
141 if (fault->func->oneinit)
142 ret = fault->func->oneinit(fault);
147 nvkm_fault_dtor(struct nvkm_subdev *subdev)
149 struct nvkm_vmm *bar2 = nvkm_bar_bar2_vmm(subdev->device);
150 struct nvkm_fault *fault = nvkm_fault(subdev);
153 nvkm_notify_fini(&fault->nrpfb);
154 nvkm_event_fini(&fault->event);
156 for (i = 0; i < fault->buffer_nr; i++) {
157 if (fault->buffer[i]) {
158 nvkm_vmm_put(bar2, &fault->buffer[i]->vma);
159 nvkm_memory_unref(&fault->buffer[i]->mem);
160 kfree(fault->buffer[i]);
167 static const struct nvkm_subdev_func
169 .dtor = nvkm_fault_dtor,
170 .oneinit = nvkm_fault_oneinit,
171 .init = nvkm_fault_init,
172 .fini = nvkm_fault_fini,
173 .intr = nvkm_fault_intr,
177 nvkm_fault_new_(const struct nvkm_fault_func *func, struct nvkm_device *device,
178 int index, struct nvkm_fault **pfault)
180 struct nvkm_fault *fault;
181 if (!(fault = *pfault = kzalloc(sizeof(*fault), GFP_KERNEL)))
183 nvkm_subdev_ctor(&nvkm_fault, device, index, &fault->subdev);