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