]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/gpu/drm/nouveau/nvkm/subdev/mmu/base.c
e0fa0cae79607767313ade8e8d4076659b53c283
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / nouveau / nvkm / subdev / mmu / base.c
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 "ummu.h"
25 #include "vmm.h"
26
27 #include <subdev/bar.h>
28 #include <subdev/fb.h>
29
30 #include <nvif/if500d.h>
31 #include <nvif/if900d.h>
32
33 struct nvkm_mmu_ptp {
34 struct nvkm_mmu_pt *pt;
35 struct list_head head;
36 u8 shift;
37 u16 mask;
38 u16 free;
39 };
40
41 static void
42 nvkm_mmu_ptp_put(struct nvkm_mmu *mmu, bool force, struct nvkm_mmu_pt *pt)
43 {
44 const int slot = pt->base >> pt->ptp->shift;
45 struct nvkm_mmu_ptp *ptp = pt->ptp;
46
47 /* If there were no free slots in the parent allocation before,
48 * there will be now, so return PTP to the cache.
49 */
50 if (!ptp->free)
51 list_add(&ptp->head, &mmu->ptp.list);
52 ptp->free |= BIT(slot);
53
54 /* If there's no more sub-allocations, destroy PTP. */
55 if (ptp->free == ptp->mask) {
56 nvkm_mmu_ptc_put(mmu, force, &ptp->pt);
57 list_del(&ptp->head);
58 kfree(ptp);
59 }
60
61 kfree(pt);
62 }
63
64 struct nvkm_mmu_pt *
65 nvkm_mmu_ptp_get(struct nvkm_mmu *mmu, u32 size, bool zero)
66 {
67 struct nvkm_mmu_pt *pt;
68 struct nvkm_mmu_ptp *ptp;
69 int slot;
70
71 if (!(pt = kzalloc(sizeof(*pt), GFP_KERNEL)))
72 return NULL;
73
74 ptp = list_first_entry_or_null(&mmu->ptp.list, typeof(*ptp), head);
75 if (!ptp) {
76 /* Need to allocate a new parent to sub-allocate from. */
77 if (!(ptp = kmalloc(sizeof(*ptp), GFP_KERNEL))) {
78 kfree(pt);
79 return NULL;
80 }
81
82 ptp->pt = nvkm_mmu_ptc_get(mmu, 0x1000, 0x1000, false);
83 if (!ptp->pt) {
84 kfree(ptp);
85 kfree(pt);
86 return NULL;
87 }
88
89 ptp->shift = order_base_2(size);
90 slot = nvkm_memory_size(ptp->pt->memory) >> ptp->shift;
91 ptp->mask = (1 << slot) - 1;
92 ptp->free = ptp->mask;
93 list_add(&ptp->head, &mmu->ptp.list);
94 }
95 pt->ptp = ptp;
96 pt->sub = true;
97
98 /* Sub-allocate from parent object, removing PTP from cache
99 * if there's no more free slots left.
100 */
101 slot = __ffs(ptp->free);
102 ptp->free &= ~BIT(slot);
103 if (!ptp->free)
104 list_del(&ptp->head);
105
106 pt->memory = pt->ptp->pt->memory;
107 pt->base = slot << ptp->shift;
108 pt->addr = pt->ptp->pt->addr + pt->base;
109 return pt;
110 }
111
112 struct nvkm_mmu_ptc {
113 struct list_head head;
114 struct list_head item;
115 u32 size;
116 u32 refs;
117 };
118
119 static inline struct nvkm_mmu_ptc *
120 nvkm_mmu_ptc_find(struct nvkm_mmu *mmu, u32 size)
121 {
122 struct nvkm_mmu_ptc *ptc;
123
124 list_for_each_entry(ptc, &mmu->ptc.list, head) {
125 if (ptc->size == size)
126 return ptc;
127 }
128
129 ptc = kmalloc(sizeof(*ptc), GFP_KERNEL);
130 if (ptc) {
131 INIT_LIST_HEAD(&ptc->item);
132 ptc->size = size;
133 ptc->refs = 0;
134 list_add(&ptc->head, &mmu->ptc.list);
135 }
136
137 return ptc;
138 }
139
140 void
141 nvkm_mmu_ptc_put(struct nvkm_mmu *mmu, bool force, struct nvkm_mmu_pt **ppt)
142 {
143 struct nvkm_mmu_pt *pt = *ppt;
144 if (pt) {
145 /* Handle sub-allocated page tables. */
146 if (pt->sub) {
147 mutex_lock(&mmu->ptp.mutex);
148 nvkm_mmu_ptp_put(mmu, force, pt);
149 mutex_unlock(&mmu->ptp.mutex);
150 return;
151 }
152
153 /* Either cache or free the object. */
154 mutex_lock(&mmu->ptc.mutex);
155 if (pt->ptc->refs < 8 /* Heuristic. */ && !force) {
156 list_add_tail(&pt->head, &pt->ptc->item);
157 pt->ptc->refs++;
158 } else {
159 nvkm_memory_unref(&pt->memory);
160 kfree(pt);
161 }
162 mutex_unlock(&mmu->ptc.mutex);
163 }
164 }
165
166 struct nvkm_mmu_pt *
167 nvkm_mmu_ptc_get(struct nvkm_mmu *mmu, u32 size, u32 align, bool zero)
168 {
169 struct nvkm_mmu_ptc *ptc;
170 struct nvkm_mmu_pt *pt;
171 int ret;
172
173 /* Sub-allocated page table (ie. GP100 LPT). */
174 if (align < 0x1000) {
175 mutex_lock(&mmu->ptp.mutex);
176 pt = nvkm_mmu_ptp_get(mmu, align, zero);
177 mutex_unlock(&mmu->ptp.mutex);
178 return pt;
179 }
180
181 /* Lookup cache for this page table size. */
182 mutex_lock(&mmu->ptc.mutex);
183 ptc = nvkm_mmu_ptc_find(mmu, size);
184 if (!ptc) {
185 mutex_unlock(&mmu->ptc.mutex);
186 return NULL;
187 }
188
189 /* If there's a free PT in the cache, reuse it. */
190 pt = list_first_entry_or_null(&ptc->item, typeof(*pt), head);
191 if (pt) {
192 if (zero)
193 nvkm_fo64(pt->memory, 0, 0, size >> 3);
194 list_del(&pt->head);
195 ptc->refs--;
196 mutex_unlock(&mmu->ptc.mutex);
197 return pt;
198 }
199 mutex_unlock(&mmu->ptc.mutex);
200
201 /* No such luck, we need to allocate. */
202 if (!(pt = kmalloc(sizeof(*pt), GFP_KERNEL)))
203 return NULL;
204 pt->ptc = ptc;
205 pt->sub = false;
206
207 ret = nvkm_memory_new(mmu->subdev.device, NVKM_MEM_TARGET_INST,
208 size, align, zero, &pt->memory);
209 if (ret) {
210 kfree(pt);
211 return NULL;
212 }
213
214 pt->base = 0;
215 pt->addr = nvkm_memory_addr(pt->memory);
216 return pt;
217 }
218
219 static void
220 nvkm_vm_map_(const struct nvkm_vmm_page *page, struct nvkm_vma *vma, u64 delta,
221 struct nvkm_mem *mem, nvkm_vmm_pte_func fn,
222 struct nvkm_vmm_map *map)
223 {
224 union {
225 struct nv50_vmm_map_v0 nv50;
226 struct gf100_vmm_map_v0 gf100;
227 } args;
228 struct nvkm_vmm *vmm = vma->vm;
229 void *argv = NULL;
230 u32 argc = 0;
231 int ret;
232
233 map->memory = mem->memory;
234 map->page = page;
235
236 if (vmm->func->valid) {
237 switch (vmm->mmu->subdev.device->card_type) {
238 case NV_50:
239 args.nv50.version = 0;
240 args.nv50.ro = !(vma->access & NV_MEM_ACCESS_WO);
241 args.nv50.priv = !!(vma->access & NV_MEM_ACCESS_SYS);
242 args.nv50.kind = (mem->memtype & 0x07f);
243 args.nv50.comp = (mem->memtype & 0x180) >> 7;
244 argv = &args.nv50;
245 argc = sizeof(args.nv50);
246 break;
247 case NV_C0:
248 case NV_E0:
249 case GM100:
250 case GP100: {
251 args.gf100.version = 0;
252 args.gf100.vol = (nvkm_memory_target(map->memory) != NVKM_MEM_TARGET_VRAM);
253 args.gf100.ro = !(vma->access & NV_MEM_ACCESS_WO);
254 args.gf100.priv = !!(vma->access & NV_MEM_ACCESS_SYS);
255 args.gf100.kind = (mem->memtype & 0x0ff);
256 argv = &args.gf100;
257 argc = sizeof(args.gf100);
258 }
259 break;
260 default:
261 break;
262 }
263
264 ret = vmm->func->valid(vmm, argv, argc, map);
265 if (WARN_ON(ret))
266 return;
267 }
268
269 mutex_lock(&vmm->mutex);
270 nvkm_vmm_ptes_map(vmm, page, vma->node->addr + delta,
271 vma->node->size, map, fn);
272 mutex_unlock(&vmm->mutex);
273
274 nvkm_memory_tags_put(vma->node->memory, vmm->mmu->subdev.device, &vma->node->tags);
275 nvkm_memory_unref(&vma->node->memory);
276 vma->node->memory = nvkm_memory_ref(map->memory);
277 vma->node->tags = map->tags;
278 }
279
280 void
281 nvkm_mmu_ptc_dump(struct nvkm_mmu *mmu)
282 {
283 struct nvkm_mmu_ptc *ptc;
284 list_for_each_entry(ptc, &mmu->ptc.list, head) {
285 struct nvkm_mmu_pt *pt, *tt;
286 list_for_each_entry_safe(pt, tt, &ptc->item, head) {
287 nvkm_memory_unref(&pt->memory);
288 list_del(&pt->head);
289 kfree(pt);
290 }
291 }
292 }
293
294 static void
295 nvkm_mmu_ptc_fini(struct nvkm_mmu *mmu)
296 {
297 struct nvkm_mmu_ptc *ptc, *ptct;
298
299 list_for_each_entry_safe(ptc, ptct, &mmu->ptc.list, head) {
300 WARN_ON(!list_empty(&ptc->item));
301 list_del(&ptc->head);
302 kfree(ptc);
303 }
304 }
305
306 static void
307 nvkm_mmu_ptc_init(struct nvkm_mmu *mmu)
308 {
309 mutex_init(&mmu->ptc.mutex);
310 INIT_LIST_HEAD(&mmu->ptc.list);
311 mutex_init(&mmu->ptp.mutex);
312 INIT_LIST_HEAD(&mmu->ptp.list);
313 }
314
315 void
316 nvkm_vm_map_at(struct nvkm_vma *vma, u64 delta, struct nvkm_mem *node)
317 {
318 const struct nvkm_vmm_page *page = &vma->vm->func->page[vma->node->page];
319 if (page->desc->func->unmap) {
320 struct nvkm_vmm_map map = { .mem = node->mem };
321 nvkm_vm_map_(page, vma, delta, node, page->desc->func->mem, &map);
322 return;
323 }
324 }
325
326 static void
327 nvkm_vm_map_sg_table(struct nvkm_vma *vma, u64 delta, u64 length,
328 struct nvkm_mem *mem)
329 {
330 const struct nvkm_vmm_page *page = &vma->vm->func->page[vma->node->page];
331 if (page->desc->func->unmap) {
332 struct nvkm_vmm_map map = { .sgl = mem->sg->sgl };
333 nvkm_vm_map_(page, vma, delta, mem, page->desc->func->sgl, &map);
334 return;
335 }
336 }
337
338 static void
339 nvkm_vm_map_sg(struct nvkm_vma *vma, u64 delta, u64 length,
340 struct nvkm_mem *mem)
341 {
342 const struct nvkm_vmm_page *page = &vma->vm->func->page[vma->node->page];
343 if (page->desc->func->unmap) {
344 struct nvkm_vmm_map map = { .dma = mem->pages };
345 nvkm_vm_map_(page, vma, delta, mem, page->desc->func->dma, &map);
346 return;
347 }
348 }
349
350 void
351 nvkm_vm_map(struct nvkm_vma *vma, struct nvkm_mem *node)
352 {
353 if (node->sg)
354 nvkm_vm_map_sg_table(vma, 0, node->size << 12, node);
355 else
356 if (node->pages)
357 nvkm_vm_map_sg(vma, 0, node->size << 12, node);
358 else
359 nvkm_vm_map_at(vma, 0, node);
360 }
361
362 void
363 nvkm_vm_unmap(struct nvkm_vma *vma)
364 {
365 nvkm_vmm_unmap(vma->vm, vma->node);
366 }
367
368 int
369 nvkm_vm_get(struct nvkm_vm *vm, u64 size, u32 page_shift, u32 access,
370 struct nvkm_vma *vma)
371 {
372 int ret;
373
374 mutex_lock(&vm->mutex);
375 ret = nvkm_vmm_get_locked(vm, true, false, false, page_shift, 0,
376 size, &vma->node);
377 mutex_unlock(&vm->mutex);
378 if (ret)
379 return ret;
380
381 vma->memory = NULL;
382 vma->tags = NULL;
383 vma->vm = NULL;
384 nvkm_vm_ref(vm, &vma->vm, NULL);
385 vma->offset = vma->addr = vma->node->addr;
386 vma->access = access;
387 return 0;
388 }
389
390 void
391 nvkm_vm_put(struct nvkm_vma *vma)
392 {
393 nvkm_vmm_put(vma->vm, &vma->node);
394 nvkm_vm_ref(NULL, &vma->vm, NULL);
395 }
396
397 int
398 nvkm_vm_boot(struct nvkm_vm *vm, u64 size)
399 {
400 return nvkm_vmm_boot(vm);
401 }
402
403 int
404 nvkm_vm_new(struct nvkm_device *device, u64 offset, u64 length, u64 mm_offset,
405 struct lock_class_key *key, struct nvkm_vm **pvm)
406 {
407 struct nvkm_mmu *mmu = device->mmu;
408
409 *pvm = NULL;
410 if (mmu->func->vmm.ctor) {
411 int ret = mmu->func->vmm.ctor(mmu, mm_offset,
412 offset + length - mm_offset,
413 NULL, 0, key, "legacy", pvm);
414 if (ret) {
415 nvkm_vm_ref(NULL, pvm, NULL);
416 return ret;
417 }
418
419 return ret;
420 }
421
422 return -EINVAL;
423 }
424
425 int
426 nvkm_vm_ref(struct nvkm_vm *ref, struct nvkm_vm **ptr, struct nvkm_memory *inst)
427 {
428 if (ref) {
429 if (inst) {
430 int ret = nvkm_vmm_join(ref, inst);
431 if (ret)
432 return ret;
433 }
434
435 nvkm_vmm_ref(ref);
436 }
437
438 if (*ptr) {
439 nvkm_vmm_part(*ptr, inst);
440 nvkm_vmm_unref(ptr);
441 }
442
443 *ptr = ref;
444 return 0;
445 }
446
447 static void
448 nvkm_mmu_type(struct nvkm_mmu *mmu, int heap, u8 type)
449 {
450 if (heap >= 0 && !WARN_ON(mmu->type_nr == ARRAY_SIZE(mmu->type))) {
451 mmu->type[mmu->type_nr].type = type | mmu->heap[heap].type;
452 mmu->type[mmu->type_nr].heap = heap;
453 mmu->type_nr++;
454 }
455 }
456
457 static int
458 nvkm_mmu_heap(struct nvkm_mmu *mmu, u8 type, u64 size)
459 {
460 if (size) {
461 if (!WARN_ON(mmu->heap_nr == ARRAY_SIZE(mmu->heap))) {
462 mmu->heap[mmu->heap_nr].type = type;
463 mmu->heap[mmu->heap_nr].size = size;
464 return mmu->heap_nr++;
465 }
466 }
467 return -EINVAL;
468 }
469
470 static void
471 nvkm_mmu_host(struct nvkm_mmu *mmu)
472 {
473 struct nvkm_device *device = mmu->subdev.device;
474 u8 type = NVKM_MEM_KIND * !!mmu->func->kind_sys;
475 int heap;
476
477 /* Non-mappable system memory. */
478 heap = nvkm_mmu_heap(mmu, NVKM_MEM_HOST, ~0ULL);
479 nvkm_mmu_type(mmu, heap, type);
480
481 /* Non-coherent, cached, system memory.
482 *
483 * Block-linear mappings of system memory must be done through
484 * BAR1, and cannot be supported on systems where we're unable
485 * to map BAR1 with write-combining.
486 */
487 type |= NVKM_MEM_MAPPABLE;
488 if (!device->bar || device->bar->iomap_uncached)
489 nvkm_mmu_type(mmu, heap, type & ~NVKM_MEM_KIND);
490 else
491 nvkm_mmu_type(mmu, heap, type);
492
493 /* Coherent, cached, system memory.
494 *
495 * Unsupported on systems that aren't able to support snooped
496 * mappings, and also for block-linear mappings which must be
497 * done through BAR1.
498 */
499 type |= NVKM_MEM_COHERENT;
500 if (device->func->cpu_coherent)
501 nvkm_mmu_type(mmu, heap, type & ~NVKM_MEM_KIND);
502
503 /* Uncached system memory. */
504 nvkm_mmu_type(mmu, heap, type |= NVKM_MEM_UNCACHED);
505 }
506
507 static void
508 nvkm_mmu_vram(struct nvkm_mmu *mmu)
509 {
510 struct nvkm_device *device = mmu->subdev.device;
511 struct nvkm_mm *mm = &device->fb->ram->vram;
512 const u32 sizeN = nvkm_mm_heap_size(mm, NVKM_RAM_MM_NORMAL);
513 const u32 sizeU = nvkm_mm_heap_size(mm, NVKM_RAM_MM_NOMAP);
514 const u32 sizeM = nvkm_mm_heap_size(mm, NVKM_RAM_MM_MIXED);
515 u8 type = NVKM_MEM_KIND * !!mmu->func->kind;
516 u8 heap = NVKM_MEM_VRAM;
517 int heapM, heapN, heapU;
518
519 /* Mixed-memory doesn't support compression or display. */
520 heapM = nvkm_mmu_heap(mmu, heap, sizeM << NVKM_RAM_MM_SHIFT);
521
522 heap |= NVKM_MEM_COMP;
523 heap |= NVKM_MEM_DISP;
524 heapN = nvkm_mmu_heap(mmu, heap, sizeN << NVKM_RAM_MM_SHIFT);
525 heapU = nvkm_mmu_heap(mmu, heap, sizeU << NVKM_RAM_MM_SHIFT);
526
527 /* Add non-mappable VRAM types first so that they're preferred
528 * over anything else. Mixed-memory will be slower than other
529 * heaps, it's prioritised last.
530 */
531 nvkm_mmu_type(mmu, heapU, type);
532 nvkm_mmu_type(mmu, heapN, type);
533 nvkm_mmu_type(mmu, heapM, type);
534
535 /* Add host memory types next, under the assumption that users
536 * wanting mappable memory want to use them as staging buffers
537 * or the like.
538 */
539 nvkm_mmu_host(mmu);
540
541 /* Mappable VRAM types go last, as they're basically the worst
542 * possible type to ask for unless there's no other choice.
543 */
544 if (device->bar) {
545 /* Write-combined BAR1 access. */
546 type |= NVKM_MEM_MAPPABLE;
547 if (!device->bar->iomap_uncached) {
548 nvkm_mmu_type(mmu, heapN, type);
549 nvkm_mmu_type(mmu, heapM, type);
550 }
551
552 /* Uncached BAR1 access. */
553 type |= NVKM_MEM_COHERENT;
554 type |= NVKM_MEM_UNCACHED;
555 nvkm_mmu_type(mmu, heapN, type);
556 nvkm_mmu_type(mmu, heapM, type);
557 }
558 }
559
560 static int
561 nvkm_mmu_oneinit(struct nvkm_subdev *subdev)
562 {
563 struct nvkm_mmu *mmu = nvkm_mmu(subdev);
564
565 /* Determine available memory types. */
566 if (mmu->subdev.device->fb && mmu->subdev.device->fb->ram)
567 nvkm_mmu_vram(mmu);
568 else
569 nvkm_mmu_host(mmu);
570
571 if (mmu->func->vmm.global) {
572 int ret = nvkm_vmm_new(subdev->device, 0, 0, NULL, 0, NULL,
573 "gart", &mmu->vmm);
574 if (ret)
575 return ret;
576 }
577
578 return 0;
579 }
580
581 static int
582 nvkm_mmu_init(struct nvkm_subdev *subdev)
583 {
584 struct nvkm_mmu *mmu = nvkm_mmu(subdev);
585 if (mmu->func->init)
586 mmu->func->init(mmu);
587 return 0;
588 }
589
590 static void *
591 nvkm_mmu_dtor(struct nvkm_subdev *subdev)
592 {
593 struct nvkm_mmu *mmu = nvkm_mmu(subdev);
594
595 nvkm_vmm_unref(&mmu->vmm);
596
597 nvkm_mmu_ptc_fini(mmu);
598 return mmu;
599 }
600
601 static const struct nvkm_subdev_func
602 nvkm_mmu = {
603 .dtor = nvkm_mmu_dtor,
604 .oneinit = nvkm_mmu_oneinit,
605 .init = nvkm_mmu_init,
606 };
607
608 void
609 nvkm_mmu_ctor(const struct nvkm_mmu_func *func, struct nvkm_device *device,
610 int index, struct nvkm_mmu *mmu)
611 {
612 nvkm_subdev_ctor(&nvkm_mmu, device, index, &mmu->subdev);
613 mmu->func = func;
614 mmu->limit = func->limit;
615 mmu->dma_bits = func->dma_bits;
616 mmu->lpg_shift = func->lpg_shift;
617 nvkm_mmu_ptc_init(mmu);
618 mmu->user.ctor = nvkm_ummu_new;
619 mmu->user.base = func->mmu.user;
620 }
621
622 int
623 nvkm_mmu_new_(const struct nvkm_mmu_func *func, struct nvkm_device *device,
624 int index, struct nvkm_mmu **pmmu)
625 {
626 if (!(*pmmu = kzalloc(sizeof(**pmmu), GFP_KERNEL)))
627 return -ENOMEM;
628 nvkm_mmu_ctor(func, device, index, *pmmu);
629 return 0;
630 }