xref: /linux/drivers/gpu/drm/nouveau/nvif/vmm.c (revision e7d759f31ca295d589f7420719c311870bb3166f)
1 /*
2  * Copyright 2017 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 <nvif/vmm.h>
23 #include <nvif/mem.h>
24 
25 #include <nvif/if000c.h>
26 
27 int
28 nvif_vmm_unmap(struct nvif_vmm *vmm, u64 addr)
29 {
30 	return nvif_object_mthd(&vmm->object, NVIF_VMM_V0_UNMAP,
31 				&(struct nvif_vmm_unmap_v0) { .addr = addr },
32 				sizeof(struct nvif_vmm_unmap_v0));
33 }
34 
35 int
36 nvif_vmm_map(struct nvif_vmm *vmm, u64 addr, u64 size, void *argv, u32 argc,
37 	     struct nvif_mem *mem, u64 offset)
38 {
39 	struct nvif_vmm_map_v0 *args;
40 	u8 stack[48];
41 	int ret;
42 
43 	if (sizeof(*args) + argc > sizeof(stack)) {
44 		if (!(args = kmalloc(sizeof(*args) + argc, GFP_KERNEL)))
45 			return -ENOMEM;
46 	} else {
47 		args = (void *)stack;
48 	}
49 
50 	args->version = 0;
51 	args->addr = addr;
52 	args->size = size;
53 	args->memory = nvif_handle(&mem->object);
54 	args->offset = offset;
55 	memcpy(args->data, argv, argc);
56 
57 	ret = nvif_object_mthd(&vmm->object, NVIF_VMM_V0_MAP,
58 			       args, sizeof(*args) + argc);
59 	if (args != (void *)stack)
60 		kfree(args);
61 	return ret;
62 }
63 
64 void
65 nvif_vmm_put(struct nvif_vmm *vmm, struct nvif_vma *vma)
66 {
67 	if (vma->size) {
68 		WARN_ON(nvif_object_mthd(&vmm->object, NVIF_VMM_V0_PUT,
69 					 &(struct nvif_vmm_put_v0) {
70 						.addr = vma->addr,
71 					 }, sizeof(struct nvif_vmm_put_v0)));
72 		vma->size = 0;
73 	}
74 }
75 
76 int
77 nvif_vmm_get(struct nvif_vmm *vmm, enum nvif_vmm_get type, bool sparse,
78 	     u8 page, u8 align, u64 size, struct nvif_vma *vma)
79 {
80 	struct nvif_vmm_get_v0 args;
81 	int ret;
82 
83 	args.version = vma->size = 0;
84 	args.sparse = sparse;
85 	args.page = page;
86 	args.align = align;
87 	args.size = size;
88 
89 	switch (type) {
90 	case ADDR: args.type = NVIF_VMM_GET_V0_ADDR; break;
91 	case PTES: args.type = NVIF_VMM_GET_V0_PTES; break;
92 	case LAZY: args.type = NVIF_VMM_GET_V0_LAZY; break;
93 	default:
94 		WARN_ON(1);
95 		return -EINVAL;
96 	}
97 
98 	ret = nvif_object_mthd(&vmm->object, NVIF_VMM_V0_GET,
99 			       &args, sizeof(args));
100 	if (ret == 0) {
101 		vma->addr = args.addr;
102 		vma->size = args.size;
103 	}
104 	return ret;
105 }
106 
107 int
108 nvif_vmm_raw_get(struct nvif_vmm *vmm, u64 addr, u64 size,
109 		 u8 shift)
110 {
111 	struct nvif_vmm_raw_v0 args = {
112 		.version = 0,
113 		.op = NVIF_VMM_RAW_V0_GET,
114 		.addr = addr,
115 		.size = size,
116 		.shift = shift,
117 	};
118 
119 	return nvif_object_mthd(&vmm->object, NVIF_VMM_V0_RAW,
120 				&args, sizeof(args));
121 }
122 
123 int
124 nvif_vmm_raw_put(struct nvif_vmm *vmm, u64 addr, u64 size, u8 shift)
125 {
126 	struct nvif_vmm_raw_v0 args = {
127 		.version = 0,
128 		.op = NVIF_VMM_RAW_V0_PUT,
129 		.addr = addr,
130 		.size = size,
131 		.shift = shift,
132 	};
133 
134 	return nvif_object_mthd(&vmm->object, NVIF_VMM_V0_RAW,
135 				&args, sizeof(args));
136 }
137 
138 int
139 nvif_vmm_raw_map(struct nvif_vmm *vmm, u64 addr, u64 size, u8 shift,
140 		 void *argv, u32 argc, struct nvif_mem *mem, u64 offset)
141 {
142 	struct nvif_vmm_raw_v0 args = {
143 		.version = 0,
144 		.op = NVIF_VMM_RAW_V0_MAP,
145 		.addr = addr,
146 		.size = size,
147 		.shift = shift,
148 		.memory = nvif_handle(&mem->object),
149 		.offset = offset,
150 		.argv = (u64)(uintptr_t)argv,
151 		.argc = argc,
152 	};
153 
154 
155 	return nvif_object_mthd(&vmm->object, NVIF_VMM_V0_RAW,
156 				&args, sizeof(args));
157 }
158 
159 int
160 nvif_vmm_raw_unmap(struct nvif_vmm *vmm, u64 addr, u64 size,
161 		   u8 shift, bool sparse)
162 {
163 	struct nvif_vmm_raw_v0 args = {
164 		.version = 0,
165 		.op = NVIF_VMM_RAW_V0_UNMAP,
166 		.addr = addr,
167 		.size = size,
168 		.shift = shift,
169 		.sparse = sparse,
170 	};
171 
172 	return nvif_object_mthd(&vmm->object, NVIF_VMM_V0_RAW,
173 				&args, sizeof(args));
174 }
175 
176 int
177 nvif_vmm_raw_sparse(struct nvif_vmm *vmm, u64 addr, u64 size, bool ref)
178 {
179 	struct nvif_vmm_raw_v0 args = {
180 		.version = 0,
181 		.op = NVIF_VMM_RAW_V0_SPARSE,
182 		.addr = addr,
183 		.size = size,
184 		.ref = ref,
185 	};
186 
187 	return nvif_object_mthd(&vmm->object, NVIF_VMM_V0_RAW,
188 				&args, sizeof(args));
189 }
190 
191 void
192 nvif_vmm_dtor(struct nvif_vmm *vmm)
193 {
194 	kfree(vmm->page);
195 	nvif_object_dtor(&vmm->object);
196 }
197 
198 int
199 nvif_vmm_ctor(struct nvif_mmu *mmu, const char *name, s32 oclass,
200 	      enum nvif_vmm_type type, u64 addr, u64 size, void *argv, u32 argc,
201 	      struct nvif_vmm *vmm)
202 {
203 	struct nvif_vmm_v0 *args;
204 	u32 argn = sizeof(*args) + argc;
205 	int ret = -ENOSYS, i;
206 
207 	vmm->object.client = NULL;
208 	vmm->page = NULL;
209 
210 	if (!(args = kmalloc(argn, GFP_KERNEL)))
211 		return -ENOMEM;
212 	args->version = 0;
213 	args->addr = addr;
214 	args->size = size;
215 
216 	switch (type) {
217 	case UNMANAGED: args->type = NVIF_VMM_V0_TYPE_UNMANAGED; break;
218 	case MANAGED: args->type = NVIF_VMM_V0_TYPE_MANAGED; break;
219 	case RAW: args->type = NVIF_VMM_V0_TYPE_RAW; break;
220 	default:
221 		WARN_ON(1);
222 		return -EINVAL;
223 	}
224 
225 	memcpy(args->data, argv, argc);
226 
227 	ret = nvif_object_ctor(&mmu->object, name ? name : "nvifVmm", 0,
228 			       oclass, args, argn, &vmm->object);
229 	if (ret)
230 		goto done;
231 
232 	vmm->start = args->addr;
233 	vmm->limit = args->size;
234 
235 	vmm->page_nr = args->page_nr;
236 	vmm->page = kmalloc_array(vmm->page_nr, sizeof(*vmm->page),
237 				  GFP_KERNEL);
238 	if (!vmm->page) {
239 		ret = -ENOMEM;
240 		goto done;
241 	}
242 
243 	for (i = 0; i < vmm->page_nr; i++) {
244 		struct nvif_vmm_page_v0 args = { .index = i };
245 
246 		ret = nvif_object_mthd(&vmm->object, NVIF_VMM_V0_PAGE,
247 				       &args, sizeof(args));
248 		if (ret)
249 			break;
250 
251 		vmm->page[i].shift = args.shift;
252 		vmm->page[i].sparse = args.sparse;
253 		vmm->page[i].vram = args.vram;
254 		vmm->page[i].host = args.host;
255 		vmm->page[i].comp = args.comp;
256 	}
257 
258 done:
259 	if (ret)
260 		nvif_vmm_dtor(vmm);
261 	kfree(args);
262 	return ret;
263 }
264