1 /*
2  * Copyright 2010 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  * Authors: Ben Skeggs
23  */
24 #include "priv.h"
25 
26 #include <core/gpuobj.h>
27 #include <subdev/fb.h>
28 
29 void
30 nvkm_vm_map_at(struct nvkm_vma *vma, u64 delta, struct nvkm_mem *node)
31 {
32 	struct nvkm_vm *vm = vma->vm;
33 	struct nvkm_mmu *mmu = vm->mmu;
34 	struct nvkm_mm_node *r;
35 	int big = vma->node->type != mmu->func->spg_shift;
36 	u32 offset = vma->node->offset + (delta >> 12);
37 	u32 bits = vma->node->type - 12;
38 	u32 pde  = (offset >> mmu->func->pgt_bits) - vm->fpde;
39 	u32 pte  = (offset & ((1 << mmu->func->pgt_bits) - 1)) >> bits;
40 	u32 max  = 1 << (mmu->func->pgt_bits - bits);
41 	u32 end, len;
42 
43 	delta = 0;
44 	list_for_each_entry(r, &node->regions, rl_entry) {
45 		u64 phys = (u64)r->offset << 12;
46 		u32 num  = r->length >> bits;
47 
48 		while (num) {
49 			struct nvkm_memory *pgt = vm->pgt[pde].mem[big];
50 
51 			end = (pte + num);
52 			if (unlikely(end >= max))
53 				end = max;
54 			len = end - pte;
55 
56 			mmu->func->map(vma, pgt, node, pte, len, phys, delta);
57 
58 			num -= len;
59 			pte += len;
60 			if (unlikely(end >= max)) {
61 				phys += len << (bits + 12);
62 				pde++;
63 				pte = 0;
64 			}
65 
66 			delta += (u64)len << vma->node->type;
67 		}
68 	}
69 
70 	mmu->func->flush(vm);
71 }
72 
73 static void
74 nvkm_vm_map_sg_table(struct nvkm_vma *vma, u64 delta, u64 length,
75 		     struct nvkm_mem *mem)
76 {
77 	struct nvkm_vm *vm = vma->vm;
78 	struct nvkm_mmu *mmu = vm->mmu;
79 	int big = vma->node->type != mmu->func->spg_shift;
80 	u32 offset = vma->node->offset + (delta >> 12);
81 	u32 bits = vma->node->type - 12;
82 	u32 num  = length >> vma->node->type;
83 	u32 pde  = (offset >> mmu->func->pgt_bits) - vm->fpde;
84 	u32 pte  = (offset & ((1 << mmu->func->pgt_bits) - 1)) >> bits;
85 	u32 max  = 1 << (mmu->func->pgt_bits - bits);
86 	unsigned m, sglen;
87 	u32 end, len;
88 	int i;
89 	struct scatterlist *sg;
90 
91 	for_each_sg(mem->sg->sgl, sg, mem->sg->nents, i) {
92 		struct nvkm_memory *pgt = vm->pgt[pde].mem[big];
93 		sglen = sg_dma_len(sg) >> PAGE_SHIFT;
94 
95 		end = pte + sglen;
96 		if (unlikely(end >= max))
97 			end = max;
98 		len = end - pte;
99 
100 		for (m = 0; m < len; m++) {
101 			dma_addr_t addr = sg_dma_address(sg) + (m << PAGE_SHIFT);
102 
103 			mmu->func->map_sg(vma, pgt, mem, pte, 1, &addr);
104 			num--;
105 			pte++;
106 
107 			if (num == 0)
108 				goto finish;
109 		}
110 		if (unlikely(end >= max)) {
111 			pde++;
112 			pte = 0;
113 		}
114 		if (m < sglen) {
115 			for (; m < sglen; m++) {
116 				dma_addr_t addr = sg_dma_address(sg) + (m << PAGE_SHIFT);
117 
118 				mmu->func->map_sg(vma, pgt, mem, pte, 1, &addr);
119 				num--;
120 				pte++;
121 				if (num == 0)
122 					goto finish;
123 			}
124 		}
125 
126 	}
127 finish:
128 	mmu->func->flush(vm);
129 }
130 
131 static void
132 nvkm_vm_map_sg(struct nvkm_vma *vma, u64 delta, u64 length,
133 	       struct nvkm_mem *mem)
134 {
135 	struct nvkm_vm *vm = vma->vm;
136 	struct nvkm_mmu *mmu = vm->mmu;
137 	dma_addr_t *list = mem->pages;
138 	int big = vma->node->type != mmu->func->spg_shift;
139 	u32 offset = vma->node->offset + (delta >> 12);
140 	u32 bits = vma->node->type - 12;
141 	u32 num  = length >> vma->node->type;
142 	u32 pde  = (offset >> mmu->func->pgt_bits) - vm->fpde;
143 	u32 pte  = (offset & ((1 << mmu->func->pgt_bits) - 1)) >> bits;
144 	u32 max  = 1 << (mmu->func->pgt_bits - bits);
145 	u32 end, len;
146 
147 	while (num) {
148 		struct nvkm_memory *pgt = vm->pgt[pde].mem[big];
149 
150 		end = (pte + num);
151 		if (unlikely(end >= max))
152 			end = max;
153 		len = end - pte;
154 
155 		mmu->func->map_sg(vma, pgt, mem, pte, len, list);
156 
157 		num  -= len;
158 		pte  += len;
159 		list += len;
160 		if (unlikely(end >= max)) {
161 			pde++;
162 			pte = 0;
163 		}
164 	}
165 
166 	mmu->func->flush(vm);
167 }
168 
169 void
170 nvkm_vm_map(struct nvkm_vma *vma, struct nvkm_mem *node)
171 {
172 	if (node->sg)
173 		nvkm_vm_map_sg_table(vma, 0, node->size << 12, node);
174 	else
175 	if (node->pages)
176 		nvkm_vm_map_sg(vma, 0, node->size << 12, node);
177 	else
178 		nvkm_vm_map_at(vma, 0, node);
179 }
180 
181 void
182 nvkm_vm_unmap_at(struct nvkm_vma *vma, u64 delta, u64 length)
183 {
184 	struct nvkm_vm *vm = vma->vm;
185 	struct nvkm_mmu *mmu = vm->mmu;
186 	int big = vma->node->type != mmu->func->spg_shift;
187 	u32 offset = vma->node->offset + (delta >> 12);
188 	u32 bits = vma->node->type - 12;
189 	u32 num  = length >> vma->node->type;
190 	u32 pde  = (offset >> mmu->func->pgt_bits) - vm->fpde;
191 	u32 pte  = (offset & ((1 << mmu->func->pgt_bits) - 1)) >> bits;
192 	u32 max  = 1 << (mmu->func->pgt_bits - bits);
193 	u32 end, len;
194 
195 	while (num) {
196 		struct nvkm_memory *pgt = vm->pgt[pde].mem[big];
197 
198 		end = (pte + num);
199 		if (unlikely(end >= max))
200 			end = max;
201 		len = end - pte;
202 
203 		mmu->func->unmap(vma, pgt, pte, len);
204 
205 		num -= len;
206 		pte += len;
207 		if (unlikely(end >= max)) {
208 			pde++;
209 			pte = 0;
210 		}
211 	}
212 
213 	mmu->func->flush(vm);
214 }
215 
216 void
217 nvkm_vm_unmap(struct nvkm_vma *vma)
218 {
219 	nvkm_vm_unmap_at(vma, 0, (u64)vma->node->length << 12);
220 }
221 
222 static void
223 nvkm_vm_unmap_pgt(struct nvkm_vm *vm, int big, u32 fpde, u32 lpde)
224 {
225 	struct nvkm_mmu *mmu = vm->mmu;
226 	struct nvkm_vm_pgd *vpgd;
227 	struct nvkm_vm_pgt *vpgt;
228 	struct nvkm_memory *pgt;
229 	u32 pde;
230 
231 	for (pde = fpde; pde <= lpde; pde++) {
232 		vpgt = &vm->pgt[pde - vm->fpde];
233 		if (--vpgt->refcount[big])
234 			continue;
235 
236 		pgt = vpgt->mem[big];
237 		vpgt->mem[big] = NULL;
238 
239 		list_for_each_entry(vpgd, &vm->pgd_list, head) {
240 			mmu->func->map_pgt(vpgd->obj, pde, vpgt->mem);
241 		}
242 
243 		nvkm_memory_del(&pgt);
244 	}
245 }
246 
247 static int
248 nvkm_vm_map_pgt(struct nvkm_vm *vm, u32 pde, u32 type)
249 {
250 	struct nvkm_mmu *mmu = vm->mmu;
251 	struct nvkm_vm_pgt *vpgt = &vm->pgt[pde - vm->fpde];
252 	struct nvkm_vm_pgd *vpgd;
253 	int big = (type != mmu->func->spg_shift);
254 	u32 pgt_size;
255 	int ret;
256 
257 	pgt_size  = (1 << (mmu->func->pgt_bits + 12)) >> type;
258 	pgt_size *= 8;
259 
260 	ret = nvkm_memory_new(mmu->subdev.device, NVKM_MEM_TARGET_INST,
261 			      pgt_size, 0x1000, true, &vpgt->mem[big]);
262 	if (unlikely(ret))
263 		return ret;
264 
265 	list_for_each_entry(vpgd, &vm->pgd_list, head) {
266 		mmu->func->map_pgt(vpgd->obj, pde, vpgt->mem);
267 	}
268 
269 	vpgt->refcount[big]++;
270 	return 0;
271 }
272 
273 int
274 nvkm_vm_get(struct nvkm_vm *vm, u64 size, u32 page_shift, u32 access,
275 	    struct nvkm_vma *vma)
276 {
277 	struct nvkm_mmu *mmu = vm->mmu;
278 	u32 align = (1 << page_shift) >> 12;
279 	u32 msize = size >> 12;
280 	u32 fpde, lpde, pde;
281 	int ret;
282 
283 	mutex_lock(&vm->mutex);
284 	ret = nvkm_mm_head(&vm->mm, 0, page_shift, msize, msize, align,
285 			   &vma->node);
286 	if (unlikely(ret != 0)) {
287 		mutex_unlock(&vm->mutex);
288 		return ret;
289 	}
290 
291 	fpde = (vma->node->offset >> mmu->func->pgt_bits);
292 	lpde = (vma->node->offset + vma->node->length - 1) >> mmu->func->pgt_bits;
293 
294 	for (pde = fpde; pde <= lpde; pde++) {
295 		struct nvkm_vm_pgt *vpgt = &vm->pgt[pde - vm->fpde];
296 		int big = (vma->node->type != mmu->func->spg_shift);
297 
298 		if (likely(vpgt->refcount[big])) {
299 			vpgt->refcount[big]++;
300 			continue;
301 		}
302 
303 		ret = nvkm_vm_map_pgt(vm, pde, vma->node->type);
304 		if (ret) {
305 			if (pde != fpde)
306 				nvkm_vm_unmap_pgt(vm, big, fpde, pde - 1);
307 			nvkm_mm_free(&vm->mm, &vma->node);
308 			mutex_unlock(&vm->mutex);
309 			return ret;
310 		}
311 	}
312 	mutex_unlock(&vm->mutex);
313 
314 	vma->vm = NULL;
315 	nvkm_vm_ref(vm, &vma->vm, NULL);
316 	vma->offset = (u64)vma->node->offset << 12;
317 	vma->access = access;
318 	return 0;
319 }
320 
321 void
322 nvkm_vm_put(struct nvkm_vma *vma)
323 {
324 	struct nvkm_mmu *mmu;
325 	struct nvkm_vm *vm;
326 	u32 fpde, lpde;
327 
328 	if (unlikely(vma->node == NULL))
329 		return;
330 	vm = vma->vm;
331 	mmu = vm->mmu;
332 
333 	fpde = (vma->node->offset >> mmu->func->pgt_bits);
334 	lpde = (vma->node->offset + vma->node->length - 1) >> mmu->func->pgt_bits;
335 
336 	mutex_lock(&vm->mutex);
337 	nvkm_vm_unmap_pgt(vm, vma->node->type != mmu->func->spg_shift, fpde, lpde);
338 	nvkm_mm_free(&vm->mm, &vma->node);
339 	mutex_unlock(&vm->mutex);
340 
341 	nvkm_vm_ref(NULL, &vma->vm, NULL);
342 }
343 
344 int
345 nvkm_vm_boot(struct nvkm_vm *vm, u64 size)
346 {
347 	struct nvkm_mmu *mmu = vm->mmu;
348 	struct nvkm_memory *pgt;
349 	int ret;
350 
351 	ret = nvkm_memory_new(mmu->subdev.device, NVKM_MEM_TARGET_INST,
352 			      (size >> mmu->func->spg_shift) * 8, 0x1000, true, &pgt);
353 	if (ret == 0) {
354 		vm->pgt[0].refcount[0] = 1;
355 		vm->pgt[0].mem[0] = pgt;
356 		nvkm_memory_boot(pgt, vm);
357 	}
358 
359 	return ret;
360 }
361 
362 int
363 nvkm_vm_create(struct nvkm_mmu *mmu, u64 offset, u64 length, u64 mm_offset,
364 	       u32 block, struct lock_class_key *key, struct nvkm_vm **pvm)
365 {
366 	static struct lock_class_key _key;
367 	struct nvkm_vm *vm;
368 	u64 mm_length = (offset + length) - mm_offset;
369 	int ret;
370 
371 	vm = kzalloc(sizeof(*vm), GFP_KERNEL);
372 	if (!vm)
373 		return -ENOMEM;
374 
375 	__mutex_init(&vm->mutex, "&vm->mutex", key ? key : &_key);
376 	INIT_LIST_HEAD(&vm->pgd_list);
377 	vm->mmu = mmu;
378 	kref_init(&vm->refcount);
379 	vm->fpde = offset >> (mmu->func->pgt_bits + 12);
380 	vm->lpde = (offset + length - 1) >> (mmu->func->pgt_bits + 12);
381 
382 	vm->pgt  = vzalloc((vm->lpde - vm->fpde + 1) * sizeof(*vm->pgt));
383 	if (!vm->pgt) {
384 		kfree(vm);
385 		return -ENOMEM;
386 	}
387 
388 	ret = nvkm_mm_init(&vm->mm, mm_offset >> 12, mm_length >> 12,
389 			   block >> 12);
390 	if (ret) {
391 		vfree(vm->pgt);
392 		kfree(vm);
393 		return ret;
394 	}
395 
396 	*pvm = vm;
397 
398 	return 0;
399 }
400 
401 int
402 nvkm_vm_new(struct nvkm_device *device, u64 offset, u64 length, u64 mm_offset,
403 	    struct lock_class_key *key, struct nvkm_vm **pvm)
404 {
405 	struct nvkm_mmu *mmu = device->mmu;
406 	if (!mmu->func->create)
407 		return -EINVAL;
408 	return mmu->func->create(mmu, offset, length, mm_offset, key, pvm);
409 }
410 
411 static int
412 nvkm_vm_link(struct nvkm_vm *vm, struct nvkm_gpuobj *pgd)
413 {
414 	struct nvkm_mmu *mmu = vm->mmu;
415 	struct nvkm_vm_pgd *vpgd;
416 	int i;
417 
418 	if (!pgd)
419 		return 0;
420 
421 	vpgd = kzalloc(sizeof(*vpgd), GFP_KERNEL);
422 	if (!vpgd)
423 		return -ENOMEM;
424 
425 	vpgd->obj = pgd;
426 
427 	mutex_lock(&vm->mutex);
428 	for (i = vm->fpde; i <= vm->lpde; i++)
429 		mmu->func->map_pgt(pgd, i, vm->pgt[i - vm->fpde].mem);
430 	list_add(&vpgd->head, &vm->pgd_list);
431 	mutex_unlock(&vm->mutex);
432 	return 0;
433 }
434 
435 static void
436 nvkm_vm_unlink(struct nvkm_vm *vm, struct nvkm_gpuobj *mpgd)
437 {
438 	struct nvkm_vm_pgd *vpgd, *tmp;
439 
440 	if (!mpgd)
441 		return;
442 
443 	mutex_lock(&vm->mutex);
444 	list_for_each_entry_safe(vpgd, tmp, &vm->pgd_list, head) {
445 		if (vpgd->obj == mpgd) {
446 			list_del(&vpgd->head);
447 			kfree(vpgd);
448 			break;
449 		}
450 	}
451 	mutex_unlock(&vm->mutex);
452 }
453 
454 static void
455 nvkm_vm_del(struct kref *kref)
456 {
457 	struct nvkm_vm *vm = container_of(kref, typeof(*vm), refcount);
458 	struct nvkm_vm_pgd *vpgd, *tmp;
459 
460 	list_for_each_entry_safe(vpgd, tmp, &vm->pgd_list, head) {
461 		nvkm_vm_unlink(vm, vpgd->obj);
462 	}
463 
464 	nvkm_mm_fini(&vm->mm);
465 	vfree(vm->pgt);
466 	kfree(vm);
467 }
468 
469 int
470 nvkm_vm_ref(struct nvkm_vm *ref, struct nvkm_vm **ptr, struct nvkm_gpuobj *pgd)
471 {
472 	if (ref) {
473 		int ret = nvkm_vm_link(ref, pgd);
474 		if (ret)
475 			return ret;
476 
477 		kref_get(&ref->refcount);
478 	}
479 
480 	if (*ptr) {
481 		nvkm_vm_unlink(*ptr, pgd);
482 		kref_put(&(*ptr)->refcount, nvkm_vm_del);
483 	}
484 
485 	*ptr = ref;
486 	return 0;
487 }
488 
489 static int
490 nvkm_mmu_oneinit(struct nvkm_subdev *subdev)
491 {
492 	struct nvkm_mmu *mmu = nvkm_mmu(subdev);
493 	if (mmu->func->oneinit)
494 		return mmu->func->oneinit(mmu);
495 	return 0;
496 }
497 
498 static int
499 nvkm_mmu_init(struct nvkm_subdev *subdev)
500 {
501 	struct nvkm_mmu *mmu = nvkm_mmu(subdev);
502 	if (mmu->func->init)
503 		mmu->func->init(mmu);
504 	return 0;
505 }
506 
507 static void *
508 nvkm_mmu_dtor(struct nvkm_subdev *subdev)
509 {
510 	struct nvkm_mmu *mmu = nvkm_mmu(subdev);
511 	if (mmu->func->dtor)
512 		return mmu->func->dtor(mmu);
513 	return mmu;
514 }
515 
516 static const struct nvkm_subdev_func
517 nvkm_mmu = {
518 	.dtor = nvkm_mmu_dtor,
519 	.oneinit = nvkm_mmu_oneinit,
520 	.init = nvkm_mmu_init,
521 };
522 
523 void
524 nvkm_mmu_ctor(const struct nvkm_mmu_func *func, struct nvkm_device *device,
525 	      int index, struct nvkm_mmu *mmu)
526 {
527 	nvkm_subdev_ctor(&nvkm_mmu, device, index, 0, &mmu->subdev);
528 	mmu->func = func;
529 	mmu->limit = func->limit;
530 	mmu->dma_bits = func->dma_bits;
531 	mmu->lpg_shift = func->lpg_shift;
532 }
533 
534 int
535 nvkm_mmu_new_(const struct nvkm_mmu_func *func, struct nvkm_device *device,
536 	      int index, struct nvkm_mmu **pmmu)
537 {
538 	if (!(*pmmu = kzalloc(sizeof(**pmmu), GFP_KERNEL)))
539 		return -ENOMEM;
540 	nvkm_mmu_ctor(func, device, index, *pmmu);
541 	return 0;
542 }
543