136780d7eSBen Skeggs /*
236780d7eSBen Skeggs * Copyright 2018 Red Hat Inc.
336780d7eSBen Skeggs *
436780d7eSBen Skeggs * Permission is hereby granted, free of charge, to any person obtaining a
536780d7eSBen Skeggs * copy of this software and associated documentation files (the "Software"),
636780d7eSBen Skeggs * to deal in the Software without restriction, including without limitation
736780d7eSBen Skeggs * the rights to use, copy, modify, merge, publish, distribute, sublicense,
836780d7eSBen Skeggs * and/or sell copies of the Software, and to permit persons to whom the
936780d7eSBen Skeggs * Software is furnished to do so, subject to the following conditions:
1036780d7eSBen Skeggs *
1136780d7eSBen Skeggs * The above copyright notice and this permission notice shall be included in
1236780d7eSBen Skeggs * all copies or substantial portions of the Software.
1336780d7eSBen Skeggs *
1436780d7eSBen Skeggs * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1536780d7eSBen Skeggs * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1636780d7eSBen Skeggs * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1736780d7eSBen Skeggs * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
1836780d7eSBen Skeggs * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1936780d7eSBen Skeggs * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2036780d7eSBen Skeggs * OTHER DEALINGS IN THE SOFTWARE.
2136780d7eSBen Skeggs */
2236780d7eSBen Skeggs #include "priv.h"
2336780d7eSBen Skeggs
2436780d7eSBen Skeggs #include <core/memory.h>
2536780d7eSBen Skeggs
2636780d7eSBen Skeggs static void
nvkm_fault_ntfy_fini(struct nvkm_event * event,int type,int index)2736780d7eSBen Skeggs nvkm_fault_ntfy_fini(struct nvkm_event *event, int type, int index)
2836780d7eSBen Skeggs {
2936780d7eSBen Skeggs struct nvkm_fault *fault = container_of(event, typeof(*fault), event);
303968d692SBen Skeggs fault->func->buffer.intr(fault->buffer[index], false);
3136780d7eSBen Skeggs }
3236780d7eSBen Skeggs
3336780d7eSBen Skeggs static void
nvkm_fault_ntfy_init(struct nvkm_event * event,int type,int index)3436780d7eSBen Skeggs nvkm_fault_ntfy_init(struct nvkm_event *event, int type, int index)
3536780d7eSBen Skeggs {
3636780d7eSBen Skeggs struct nvkm_fault *fault = container_of(event, typeof(*fault), event);
373968d692SBen Skeggs fault->func->buffer.intr(fault->buffer[index], true);
3836780d7eSBen Skeggs }
3936780d7eSBen Skeggs
4036780d7eSBen Skeggs static const struct nvkm_event_func
4136780d7eSBen Skeggs nvkm_fault_ntfy = {
4236780d7eSBen Skeggs .init = nvkm_fault_ntfy_init,
4336780d7eSBen Skeggs .fini = nvkm_fault_ntfy_fini,
4436780d7eSBen Skeggs };
4536780d7eSBen Skeggs
4636780d7eSBen Skeggs static void
nvkm_fault_intr(struct nvkm_subdev * subdev)4736780d7eSBen Skeggs nvkm_fault_intr(struct nvkm_subdev *subdev)
4836780d7eSBen Skeggs {
4936780d7eSBen Skeggs struct nvkm_fault *fault = nvkm_fault(subdev);
5036780d7eSBen Skeggs return fault->func->intr(fault);
5136780d7eSBen Skeggs }
5236780d7eSBen Skeggs
5336780d7eSBen Skeggs static int
nvkm_fault_fini(struct nvkm_subdev * subdev,bool suspend)5436780d7eSBen Skeggs nvkm_fault_fini(struct nvkm_subdev *subdev, bool suspend)
5536780d7eSBen Skeggs {
5636780d7eSBen Skeggs struct nvkm_fault *fault = nvkm_fault(subdev);
5736780d7eSBen Skeggs if (fault->func->fini)
5836780d7eSBen Skeggs fault->func->fini(fault);
5936780d7eSBen Skeggs return 0;
6036780d7eSBen Skeggs }
6136780d7eSBen Skeggs
6236780d7eSBen Skeggs static int
nvkm_fault_init(struct nvkm_subdev * subdev)6336780d7eSBen Skeggs nvkm_fault_init(struct nvkm_subdev *subdev)
6436780d7eSBen Skeggs {
6536780d7eSBen Skeggs struct nvkm_fault *fault = nvkm_fault(subdev);
6636780d7eSBen Skeggs if (fault->func->init)
6736780d7eSBen Skeggs fault->func->init(fault);
6836780d7eSBen Skeggs return 0;
6936780d7eSBen Skeggs }
7036780d7eSBen Skeggs
7136780d7eSBen Skeggs static int
nvkm_fault_oneinit_buffer(struct nvkm_fault * fault,int id)7236780d7eSBen Skeggs nvkm_fault_oneinit_buffer(struct nvkm_fault *fault, int id)
7336780d7eSBen Skeggs {
7436780d7eSBen Skeggs struct nvkm_subdev *subdev = &fault->subdev;
7536780d7eSBen Skeggs struct nvkm_device *device = subdev->device;
7636780d7eSBen Skeggs struct nvkm_fault_buffer *buffer;
7736780d7eSBen Skeggs int ret;
7836780d7eSBen Skeggs
7936780d7eSBen Skeggs if (!(buffer = kzalloc(sizeof(*buffer), GFP_KERNEL)))
8036780d7eSBen Skeggs return -ENOMEM;
8136780d7eSBen Skeggs buffer->fault = fault;
8236780d7eSBen Skeggs buffer->id = id;
8380972456SBen Skeggs fault->func->buffer.info(buffer);
8436780d7eSBen Skeggs fault->buffer[id] = buffer;
8536780d7eSBen Skeggs
8636780d7eSBen Skeggs nvkm_debug(subdev, "buffer %d: %d entries\n", id, buffer->entries);
8736780d7eSBen Skeggs
8836780d7eSBen Skeggs ret = nvkm_memory_new(device, NVKM_MEM_TARGET_INST, buffer->entries *
8936780d7eSBen Skeggs fault->func->buffer.entry_size, 0x1000, true,
9036780d7eSBen Skeggs &buffer->mem);
9136780d7eSBen Skeggs if (ret)
9236780d7eSBen Skeggs return ret;
9336780d7eSBen Skeggs
944d326469SBen Skeggs /* Pin fault buffer in BAR2. */
950ac7facbSThierry Reding buffer->addr = fault->func->buffer.pin(buffer);
964d326469SBen Skeggs if (buffer->addr == ~0ULL)
974d326469SBen Skeggs return -EFAULT;
9836780d7eSBen Skeggs
994d326469SBen Skeggs return 0;
10036780d7eSBen Skeggs }
10136780d7eSBen Skeggs
10236780d7eSBen Skeggs static int
nvkm_fault_oneinit(struct nvkm_subdev * subdev)10336780d7eSBen Skeggs nvkm_fault_oneinit(struct nvkm_subdev *subdev)
10436780d7eSBen Skeggs {
10536780d7eSBen Skeggs struct nvkm_fault *fault = nvkm_fault(subdev);
10636780d7eSBen Skeggs int ret, i;
10736780d7eSBen Skeggs
10836780d7eSBen Skeggs for (i = 0; i < ARRAY_SIZE(fault->buffer); i++) {
10936780d7eSBen Skeggs if (i < fault->func->buffer.nr) {
11036780d7eSBen Skeggs ret = nvkm_fault_oneinit_buffer(fault, i);
11136780d7eSBen Skeggs if (ret)
11236780d7eSBen Skeggs return ret;
11336780d7eSBen Skeggs fault->buffer_nr = i + 1;
11436780d7eSBen Skeggs }
11536780d7eSBen Skeggs }
11636780d7eSBen Skeggs
117f43e47c0SBen Skeggs ret = nvkm_event_init(&nvkm_fault_ntfy, subdev, 1, fault->buffer_nr, &fault->event);
11860cda665SBen Skeggs if (ret)
11960cda665SBen Skeggs return ret;
12060cda665SBen Skeggs
12160cda665SBen Skeggs if (fault->func->oneinit)
12260cda665SBen Skeggs ret = fault->func->oneinit(fault);
12360cda665SBen Skeggs return ret;
12436780d7eSBen Skeggs }
12536780d7eSBen Skeggs
12636780d7eSBen Skeggs static void *
nvkm_fault_dtor(struct nvkm_subdev * subdev)12736780d7eSBen Skeggs nvkm_fault_dtor(struct nvkm_subdev *subdev)
12836780d7eSBen Skeggs {
12936780d7eSBen Skeggs struct nvkm_fault *fault = nvkm_fault(subdev);
13036780d7eSBen Skeggs int i;
13136780d7eSBen Skeggs
132*55520832SBen Skeggs nvkm_event_ntfy_del(&fault->nrpfb);
13336780d7eSBen Skeggs nvkm_event_fini(&fault->event);
13436780d7eSBen Skeggs
13536780d7eSBen Skeggs for (i = 0; i < fault->buffer_nr; i++) {
13636780d7eSBen Skeggs if (fault->buffer[i]) {
13736780d7eSBen Skeggs nvkm_memory_unref(&fault->buffer[i]->mem);
13836780d7eSBen Skeggs kfree(fault->buffer[i]);
13936780d7eSBen Skeggs }
14036780d7eSBen Skeggs }
14136780d7eSBen Skeggs
14236780d7eSBen Skeggs return fault;
14336780d7eSBen Skeggs }
14436780d7eSBen Skeggs
14536780d7eSBen Skeggs static const struct nvkm_subdev_func
14636780d7eSBen Skeggs nvkm_fault = {
14736780d7eSBen Skeggs .dtor = nvkm_fault_dtor,
14836780d7eSBen Skeggs .oneinit = nvkm_fault_oneinit,
14936780d7eSBen Skeggs .init = nvkm_fault_init,
15036780d7eSBen Skeggs .fini = nvkm_fault_fini,
15136780d7eSBen Skeggs .intr = nvkm_fault_intr,
15236780d7eSBen Skeggs };
15336780d7eSBen Skeggs
15436780d7eSBen Skeggs int
nvkm_fault_new_(const struct nvkm_fault_func * func,struct nvkm_device * device,enum nvkm_subdev_type type,int inst,struct nvkm_fault ** pfault)15536780d7eSBen Skeggs nvkm_fault_new_(const struct nvkm_fault_func *func, struct nvkm_device *device,
15677689f1bSBen Skeggs enum nvkm_subdev_type type, int inst, struct nvkm_fault **pfault)
15736780d7eSBen Skeggs {
15836780d7eSBen Skeggs struct nvkm_fault *fault;
15936780d7eSBen Skeggs if (!(fault = *pfault = kzalloc(sizeof(*fault), GFP_KERNEL)))
16036780d7eSBen Skeggs return -ENOMEM;
16177689f1bSBen Skeggs nvkm_subdev_ctor(&nvkm_fault, device, type, inst, &fault->subdev);
16236780d7eSBen Skeggs fault->func = func;
16313e95729SBen Skeggs fault->user.ctor = nvkm_ufault_new;
16413e95729SBen Skeggs fault->user.base = func->user.base;
16536780d7eSBen Skeggs return 0;
16636780d7eSBen Skeggs }
167