1920d2b5eSBen Skeggs /*
2920d2b5eSBen Skeggs * Copyright 2017 Red Hat Inc.
3920d2b5eSBen Skeggs *
4920d2b5eSBen Skeggs * Permission is hereby granted, free of charge, to any person obtaining a
5920d2b5eSBen Skeggs * copy of this software and associated documentation files (the "Software"),
6920d2b5eSBen Skeggs * to deal in the Software without restriction, including without limitation
7920d2b5eSBen Skeggs * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8920d2b5eSBen Skeggs * and/or sell copies of the Software, and to permit persons to whom the
9920d2b5eSBen Skeggs * Software is furnished to do so, subject to the following conditions:
10920d2b5eSBen Skeggs *
11920d2b5eSBen Skeggs * The above copyright notice and this permission notice shall be included in
12920d2b5eSBen Skeggs * all copies or substantial portions of the Software.
13920d2b5eSBen Skeggs *
14920d2b5eSBen Skeggs * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15920d2b5eSBen Skeggs * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16920d2b5eSBen Skeggs * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17920d2b5eSBen Skeggs * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18920d2b5eSBen Skeggs * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19920d2b5eSBen Skeggs * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20920d2b5eSBen Skeggs * OTHER DEALINGS IN THE SOFTWARE.
21920d2b5eSBen Skeggs */
22920d2b5eSBen Skeggs #include <nvif/vmm.h>
23920d2b5eSBen Skeggs #include <nvif/mem.h>
24920d2b5eSBen Skeggs
25920d2b5eSBen Skeggs #include <nvif/if000c.h>
26920d2b5eSBen Skeggs
27920d2b5eSBen Skeggs int
nvif_vmm_unmap(struct nvif_vmm * vmm,u64 addr)28920d2b5eSBen Skeggs nvif_vmm_unmap(struct nvif_vmm *vmm, u64 addr)
29920d2b5eSBen Skeggs {
30920d2b5eSBen Skeggs return nvif_object_mthd(&vmm->object, NVIF_VMM_V0_UNMAP,
31920d2b5eSBen Skeggs &(struct nvif_vmm_unmap_v0) { .addr = addr },
32920d2b5eSBen Skeggs sizeof(struct nvif_vmm_unmap_v0));
33920d2b5eSBen Skeggs }
34920d2b5eSBen Skeggs
35920d2b5eSBen Skeggs int
nvif_vmm_map(struct nvif_vmm * vmm,u64 addr,u64 size,void * argv,u32 argc,struct nvif_mem * mem,u64 offset)36920d2b5eSBen Skeggs nvif_vmm_map(struct nvif_vmm *vmm, u64 addr, u64 size, void *argv, u32 argc,
37920d2b5eSBen Skeggs struct nvif_mem *mem, u64 offset)
38920d2b5eSBen Skeggs {
39920d2b5eSBen Skeggs struct nvif_vmm_map_v0 *args;
409dfbd731SArnd Bergmann u8 stack[48];
41920d2b5eSBen Skeggs int ret;
42920d2b5eSBen Skeggs
43920d2b5eSBen Skeggs if (sizeof(*args) + argc > sizeof(stack)) {
44920d2b5eSBen Skeggs if (!(args = kmalloc(sizeof(*args) + argc, GFP_KERNEL)))
45920d2b5eSBen Skeggs return -ENOMEM;
46920d2b5eSBen Skeggs } else {
47920d2b5eSBen Skeggs args = (void *)stack;
48920d2b5eSBen Skeggs }
49920d2b5eSBen Skeggs
50920d2b5eSBen Skeggs args->version = 0;
51920d2b5eSBen Skeggs args->addr = addr;
52920d2b5eSBen Skeggs args->size = size;
53920d2b5eSBen Skeggs args->memory = nvif_handle(&mem->object);
54920d2b5eSBen Skeggs args->offset = offset;
55920d2b5eSBen Skeggs memcpy(args->data, argv, argc);
56920d2b5eSBen Skeggs
57920d2b5eSBen Skeggs ret = nvif_object_mthd(&vmm->object, NVIF_VMM_V0_MAP,
58920d2b5eSBen Skeggs args, sizeof(*args) + argc);
59920d2b5eSBen Skeggs if (args != (void *)stack)
60920d2b5eSBen Skeggs kfree(args);
61920d2b5eSBen Skeggs return ret;
62920d2b5eSBen Skeggs }
63920d2b5eSBen Skeggs
64920d2b5eSBen Skeggs void
nvif_vmm_put(struct nvif_vmm * vmm,struct nvif_vma * vma)65920d2b5eSBen Skeggs nvif_vmm_put(struct nvif_vmm *vmm, struct nvif_vma *vma)
66920d2b5eSBen Skeggs {
67920d2b5eSBen Skeggs if (vma->size) {
68920d2b5eSBen Skeggs WARN_ON(nvif_object_mthd(&vmm->object, NVIF_VMM_V0_PUT,
69920d2b5eSBen Skeggs &(struct nvif_vmm_put_v0) {
70920d2b5eSBen Skeggs .addr = vma->addr,
71920d2b5eSBen Skeggs }, sizeof(struct nvif_vmm_put_v0)));
72920d2b5eSBen Skeggs vma->size = 0;
73920d2b5eSBen Skeggs }
74920d2b5eSBen Skeggs }
75920d2b5eSBen Skeggs
76920d2b5eSBen Skeggs int
nvif_vmm_get(struct nvif_vmm * vmm,enum nvif_vmm_get type,bool sparse,u8 page,u8 align,u64 size,struct nvif_vma * vma)77920d2b5eSBen Skeggs nvif_vmm_get(struct nvif_vmm *vmm, enum nvif_vmm_get type, bool sparse,
78920d2b5eSBen Skeggs u8 page, u8 align, u64 size, struct nvif_vma *vma)
79920d2b5eSBen Skeggs {
80920d2b5eSBen Skeggs struct nvif_vmm_get_v0 args;
81920d2b5eSBen Skeggs int ret;
82920d2b5eSBen Skeggs
83920d2b5eSBen Skeggs args.version = vma->size = 0;
84920d2b5eSBen Skeggs args.sparse = sparse;
85920d2b5eSBen Skeggs args.page = page;
86920d2b5eSBen Skeggs args.align = align;
87920d2b5eSBen Skeggs args.size = size;
88920d2b5eSBen Skeggs
89920d2b5eSBen Skeggs switch (type) {
90920d2b5eSBen Skeggs case ADDR: args.type = NVIF_VMM_GET_V0_ADDR; break;
91920d2b5eSBen Skeggs case PTES: args.type = NVIF_VMM_GET_V0_PTES; break;
92920d2b5eSBen Skeggs case LAZY: args.type = NVIF_VMM_GET_V0_LAZY; break;
93920d2b5eSBen Skeggs default:
94920d2b5eSBen Skeggs WARN_ON(1);
95920d2b5eSBen Skeggs return -EINVAL;
96920d2b5eSBen Skeggs }
97920d2b5eSBen Skeggs
98920d2b5eSBen Skeggs ret = nvif_object_mthd(&vmm->object, NVIF_VMM_V0_GET,
99920d2b5eSBen Skeggs &args, sizeof(args));
100920d2b5eSBen Skeggs if (ret == 0) {
101920d2b5eSBen Skeggs vma->addr = args.addr;
102920d2b5eSBen Skeggs vma->size = args.size;
103920d2b5eSBen Skeggs }
104920d2b5eSBen Skeggs return ret;
105920d2b5eSBen Skeggs }
106920d2b5eSBen Skeggs
107*6b252cf4SDanilo Krummrich int
nvif_vmm_raw_get(struct nvif_vmm * vmm,u64 addr,u64 size,u8 shift)108*6b252cf4SDanilo Krummrich nvif_vmm_raw_get(struct nvif_vmm *vmm, u64 addr, u64 size,
109*6b252cf4SDanilo Krummrich u8 shift)
110*6b252cf4SDanilo Krummrich {
111*6b252cf4SDanilo Krummrich struct nvif_vmm_raw_v0 args = {
112*6b252cf4SDanilo Krummrich .version = 0,
113*6b252cf4SDanilo Krummrich .op = NVIF_VMM_RAW_V0_GET,
114*6b252cf4SDanilo Krummrich .addr = addr,
115*6b252cf4SDanilo Krummrich .size = size,
116*6b252cf4SDanilo Krummrich .shift = shift,
117*6b252cf4SDanilo Krummrich };
118*6b252cf4SDanilo Krummrich
119*6b252cf4SDanilo Krummrich return nvif_object_mthd(&vmm->object, NVIF_VMM_V0_RAW,
120*6b252cf4SDanilo Krummrich &args, sizeof(args));
121*6b252cf4SDanilo Krummrich }
122*6b252cf4SDanilo Krummrich
123*6b252cf4SDanilo Krummrich int
nvif_vmm_raw_put(struct nvif_vmm * vmm,u64 addr,u64 size,u8 shift)124*6b252cf4SDanilo Krummrich nvif_vmm_raw_put(struct nvif_vmm *vmm, u64 addr, u64 size, u8 shift)
125*6b252cf4SDanilo Krummrich {
126*6b252cf4SDanilo Krummrich struct nvif_vmm_raw_v0 args = {
127*6b252cf4SDanilo Krummrich .version = 0,
128*6b252cf4SDanilo Krummrich .op = NVIF_VMM_RAW_V0_PUT,
129*6b252cf4SDanilo Krummrich .addr = addr,
130*6b252cf4SDanilo Krummrich .size = size,
131*6b252cf4SDanilo Krummrich .shift = shift,
132*6b252cf4SDanilo Krummrich };
133*6b252cf4SDanilo Krummrich
134*6b252cf4SDanilo Krummrich return nvif_object_mthd(&vmm->object, NVIF_VMM_V0_RAW,
135*6b252cf4SDanilo Krummrich &args, sizeof(args));
136*6b252cf4SDanilo Krummrich }
137*6b252cf4SDanilo Krummrich
138*6b252cf4SDanilo Krummrich int
nvif_vmm_raw_map(struct nvif_vmm * vmm,u64 addr,u64 size,u8 shift,void * argv,u32 argc,struct nvif_mem * mem,u64 offset)139*6b252cf4SDanilo Krummrich nvif_vmm_raw_map(struct nvif_vmm *vmm, u64 addr, u64 size, u8 shift,
140*6b252cf4SDanilo Krummrich void *argv, u32 argc, struct nvif_mem *mem, u64 offset)
141*6b252cf4SDanilo Krummrich {
142*6b252cf4SDanilo Krummrich struct nvif_vmm_raw_v0 args = {
143*6b252cf4SDanilo Krummrich .version = 0,
144*6b252cf4SDanilo Krummrich .op = NVIF_VMM_RAW_V0_MAP,
145*6b252cf4SDanilo Krummrich .addr = addr,
146*6b252cf4SDanilo Krummrich .size = size,
147*6b252cf4SDanilo Krummrich .shift = shift,
148*6b252cf4SDanilo Krummrich .memory = nvif_handle(&mem->object),
149*6b252cf4SDanilo Krummrich .offset = offset,
150*6b252cf4SDanilo Krummrich .argv = (u64)(uintptr_t)argv,
151*6b252cf4SDanilo Krummrich .argc = argc,
152*6b252cf4SDanilo Krummrich };
153*6b252cf4SDanilo Krummrich
154*6b252cf4SDanilo Krummrich
155*6b252cf4SDanilo Krummrich return nvif_object_mthd(&vmm->object, NVIF_VMM_V0_RAW,
156*6b252cf4SDanilo Krummrich &args, sizeof(args));
157*6b252cf4SDanilo Krummrich }
158*6b252cf4SDanilo Krummrich
159*6b252cf4SDanilo Krummrich int
nvif_vmm_raw_unmap(struct nvif_vmm * vmm,u64 addr,u64 size,u8 shift,bool sparse)160*6b252cf4SDanilo Krummrich nvif_vmm_raw_unmap(struct nvif_vmm *vmm, u64 addr, u64 size,
161*6b252cf4SDanilo Krummrich u8 shift, bool sparse)
162*6b252cf4SDanilo Krummrich {
163*6b252cf4SDanilo Krummrich struct nvif_vmm_raw_v0 args = {
164*6b252cf4SDanilo Krummrich .version = 0,
165*6b252cf4SDanilo Krummrich .op = NVIF_VMM_RAW_V0_UNMAP,
166*6b252cf4SDanilo Krummrich .addr = addr,
167*6b252cf4SDanilo Krummrich .size = size,
168*6b252cf4SDanilo Krummrich .shift = shift,
169*6b252cf4SDanilo Krummrich .sparse = sparse,
170*6b252cf4SDanilo Krummrich };
171*6b252cf4SDanilo Krummrich
172*6b252cf4SDanilo Krummrich return nvif_object_mthd(&vmm->object, NVIF_VMM_V0_RAW,
173*6b252cf4SDanilo Krummrich &args, sizeof(args));
174*6b252cf4SDanilo Krummrich }
175*6b252cf4SDanilo Krummrich
176*6b252cf4SDanilo Krummrich int
nvif_vmm_raw_sparse(struct nvif_vmm * vmm,u64 addr,u64 size,bool ref)177*6b252cf4SDanilo Krummrich nvif_vmm_raw_sparse(struct nvif_vmm *vmm, u64 addr, u64 size, bool ref)
178*6b252cf4SDanilo Krummrich {
179*6b252cf4SDanilo Krummrich struct nvif_vmm_raw_v0 args = {
180*6b252cf4SDanilo Krummrich .version = 0,
181*6b252cf4SDanilo Krummrich .op = NVIF_VMM_RAW_V0_SPARSE,
182*6b252cf4SDanilo Krummrich .addr = addr,
183*6b252cf4SDanilo Krummrich .size = size,
184*6b252cf4SDanilo Krummrich .ref = ref,
185*6b252cf4SDanilo Krummrich };
186*6b252cf4SDanilo Krummrich
187*6b252cf4SDanilo Krummrich return nvif_object_mthd(&vmm->object, NVIF_VMM_V0_RAW,
188*6b252cf4SDanilo Krummrich &args, sizeof(args));
189*6b252cf4SDanilo Krummrich }
190*6b252cf4SDanilo Krummrich
191920d2b5eSBen Skeggs void
nvif_vmm_dtor(struct nvif_vmm * vmm)19245faf3d7SBen Skeggs nvif_vmm_dtor(struct nvif_vmm *vmm)
193920d2b5eSBen Skeggs {
194920d2b5eSBen Skeggs kfree(vmm->page);
1959ac596a4SBen Skeggs nvif_object_dtor(&vmm->object);
196920d2b5eSBen Skeggs }
197920d2b5eSBen Skeggs
198920d2b5eSBen Skeggs int
nvif_vmm_ctor(struct nvif_mmu * mmu,const char * name,s32 oclass,enum nvif_vmm_type type,u64 addr,u64 size,void * argv,u32 argc,struct nvif_vmm * vmm)199*6b252cf4SDanilo Krummrich nvif_vmm_ctor(struct nvif_mmu *mmu, const char *name, s32 oclass,
200*6b252cf4SDanilo Krummrich enum nvif_vmm_type type, u64 addr, u64 size, void *argv, u32 argc,
201*6b252cf4SDanilo Krummrich struct nvif_vmm *vmm)
202920d2b5eSBen Skeggs {
203920d2b5eSBen Skeggs struct nvif_vmm_v0 *args;
204920d2b5eSBen Skeggs u32 argn = sizeof(*args) + argc;
205920d2b5eSBen Skeggs int ret = -ENOSYS, i;
206920d2b5eSBen Skeggs
207920d2b5eSBen Skeggs vmm->object.client = NULL;
208920d2b5eSBen Skeggs vmm->page = NULL;
209920d2b5eSBen Skeggs
210920d2b5eSBen Skeggs if (!(args = kmalloc(argn, GFP_KERNEL)))
211920d2b5eSBen Skeggs return -ENOMEM;
212920d2b5eSBen Skeggs args->version = 0;
213920d2b5eSBen Skeggs args->addr = addr;
214920d2b5eSBen Skeggs args->size = size;
215*6b252cf4SDanilo Krummrich
216*6b252cf4SDanilo Krummrich switch (type) {
217*6b252cf4SDanilo Krummrich case UNMANAGED: args->type = NVIF_VMM_V0_TYPE_UNMANAGED; break;
218*6b252cf4SDanilo Krummrich case MANAGED: args->type = NVIF_VMM_V0_TYPE_MANAGED; break;
219*6b252cf4SDanilo Krummrich case RAW: args->type = NVIF_VMM_V0_TYPE_RAW; break;
220*6b252cf4SDanilo Krummrich default:
221*6b252cf4SDanilo Krummrich WARN_ON(1);
222*6b252cf4SDanilo Krummrich return -EINVAL;
223*6b252cf4SDanilo Krummrich }
224*6b252cf4SDanilo Krummrich
225920d2b5eSBen Skeggs memcpy(args->data, argv, argc);
226920d2b5eSBen Skeggs
22745faf3d7SBen Skeggs ret = nvif_object_ctor(&mmu->object, name ? name : "nvifVmm", 0,
22845faf3d7SBen Skeggs oclass, args, argn, &vmm->object);
229920d2b5eSBen Skeggs if (ret)
230920d2b5eSBen Skeggs goto done;
231920d2b5eSBen Skeggs
232920d2b5eSBen Skeggs vmm->start = args->addr;
233920d2b5eSBen Skeggs vmm->limit = args->size;
234920d2b5eSBen Skeggs
235920d2b5eSBen Skeggs vmm->page_nr = args->page_nr;
2366da2ec56SKees Cook vmm->page = kmalloc_array(vmm->page_nr, sizeof(*vmm->page),
2376da2ec56SKees Cook GFP_KERNEL);
238920d2b5eSBen Skeggs if (!vmm->page) {
239920d2b5eSBen Skeggs ret = -ENOMEM;
240920d2b5eSBen Skeggs goto done;
241920d2b5eSBen Skeggs }
242920d2b5eSBen Skeggs
243920d2b5eSBen Skeggs for (i = 0; i < vmm->page_nr; i++) {
244920d2b5eSBen Skeggs struct nvif_vmm_page_v0 args = { .index = i };
245920d2b5eSBen Skeggs
246920d2b5eSBen Skeggs ret = nvif_object_mthd(&vmm->object, NVIF_VMM_V0_PAGE,
247920d2b5eSBen Skeggs &args, sizeof(args));
248920d2b5eSBen Skeggs if (ret)
249920d2b5eSBen Skeggs break;
250920d2b5eSBen Skeggs
251920d2b5eSBen Skeggs vmm->page[i].shift = args.shift;
252920d2b5eSBen Skeggs vmm->page[i].sparse = args.sparse;
253920d2b5eSBen Skeggs vmm->page[i].vram = args.vram;
254920d2b5eSBen Skeggs vmm->page[i].host = args.host;
255920d2b5eSBen Skeggs vmm->page[i].comp = args.comp;
256920d2b5eSBen Skeggs }
257920d2b5eSBen Skeggs
258920d2b5eSBen Skeggs done:
259920d2b5eSBen Skeggs if (ret)
26045faf3d7SBen Skeggs nvif_vmm_dtor(vmm);
261920d2b5eSBen Skeggs kfree(args);
262920d2b5eSBen Skeggs return ret;
263920d2b5eSBen Skeggs }
264