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 <core/event.h>
26 #include <subdev/mmu.h>
27 
28 #include <nvif/clb069.h>
29 #include <nvif/unpack.h>
30 
31 static int
nvkm_ufault_uevent(struct nvkm_object * object,void * argv,u32 argc,struct nvkm_uevent * uevent)32 nvkm_ufault_uevent(struct nvkm_object *object, void *argv, u32 argc, struct nvkm_uevent *uevent)
33 {
34 	struct nvkm_fault_buffer *buffer = nvkm_fault_buffer(object);
35 	union nvif_clb069_event_args *args = argv;
36 
37 	if (!uevent)
38 		return 0;
39 	if (argc != sizeof(args->vn))
40 		return -ENOSYS;
41 
42 	return nvkm_uevent_add(uevent, &buffer->fault->event, buffer->id,
43 			       NVKM_FAULT_BUFFER_EVENT_PENDING, NULL);
44 }
45 
46 static int
nvkm_ufault_map(struct nvkm_object * object,void * argv,u32 argc,enum nvkm_object_map * type,u64 * addr,u64 * size)47 nvkm_ufault_map(struct nvkm_object *object, void *argv, u32 argc,
48 		enum nvkm_object_map *type, u64 *addr, u64 *size)
49 {
50 	struct nvkm_fault_buffer *buffer = nvkm_fault_buffer(object);
51 	struct nvkm_device *device = buffer->fault->subdev.device;
52 	*type = NVKM_OBJECT_MAP_IO;
53 	*addr = device->func->resource_addr(device, 3) + buffer->addr;
54 	*size = nvkm_memory_size(buffer->mem);
55 	return 0;
56 }
57 
58 static int
nvkm_ufault_fini(struct nvkm_object * object,bool suspend)59 nvkm_ufault_fini(struct nvkm_object *object, bool suspend)
60 {
61 	struct nvkm_fault_buffer *buffer = nvkm_fault_buffer(object);
62 	buffer->fault->func->buffer.fini(buffer);
63 	return 0;
64 }
65 
66 static int
nvkm_ufault_init(struct nvkm_object * object)67 nvkm_ufault_init(struct nvkm_object *object)
68 {
69 	struct nvkm_fault_buffer *buffer = nvkm_fault_buffer(object);
70 	buffer->fault->func->buffer.init(buffer);
71 	return 0;
72 }
73 
74 static void *
nvkm_ufault_dtor(struct nvkm_object * object)75 nvkm_ufault_dtor(struct nvkm_object *object)
76 {
77 	return NULL;
78 }
79 
80 static const struct nvkm_object_func
81 nvkm_ufault = {
82 	.dtor = nvkm_ufault_dtor,
83 	.init = nvkm_ufault_init,
84 	.fini = nvkm_ufault_fini,
85 	.map = nvkm_ufault_map,
86 	.uevent = nvkm_ufault_uevent,
87 };
88 
89 int
nvkm_ufault_new(struct nvkm_device * device,const struct nvkm_oclass * oclass,void * argv,u32 argc,struct nvkm_object ** pobject)90 nvkm_ufault_new(struct nvkm_device *device, const struct nvkm_oclass *oclass,
91 		void *argv, u32 argc, struct nvkm_object **pobject)
92 {
93 	union {
94 		struct nvif_clb069_v0 v0;
95 	} *args = argv;
96 	struct nvkm_fault *fault = device->fault;
97 	struct nvkm_fault_buffer *buffer = fault->buffer[fault->func->user.rp];
98 	int ret = -ENOSYS;
99 
100 	if (!(ret = nvif_unpack(ret, &argv, &argc, args->v0, 0, 0, false))) {
101 		args->v0.entries = buffer->entries;
102 		args->v0.get = buffer->get;
103 		args->v0.put = buffer->put;
104 	} else
105 		return ret;
106 
107 	nvkm_object_ctor(&nvkm_ufault, oclass, &buffer->object);
108 	*pobject = &buffer->object;
109 	return 0;
110 }
111