]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/gpu/drm/nouveau/nvkm/subdev/instmem/gk20a.c
2f60f0d18aeb15d3ba715eb7422c6d515745ce55
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / nouveau / nvkm / subdev / instmem / gk20a.c
1 /*
2 * Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved.
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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 /*
24 * GK20A does not have dedicated video memory, and to accurately represent this
25 * fact Nouveau will not create a RAM device for it. Therefore its instmem
26 * implementation must be done directly on top of system memory, while
27 * preserving coherency for read and write operations.
28 *
29 * Instmem can be allocated through two means:
30 * 1) If an IOMMU unit has been probed, the IOMMU API is used to make memory
31 * pages contiguous to the GPU. This is the preferred way.
32 * 2) If no IOMMU unit is probed, the DMA API is used to allocate physically
33 * contiguous memory.
34 *
35 * In both cases CPU read and writes are performed by creating a write-combined
36 * mapping. The GPU L2 cache must thus be flushed/invalidated when required. To
37 * be conservative we do this every time we acquire or release an instobj, but
38 * ideally L2 management should be handled at a higher level.
39 *
40 * To improve performance, CPU mappings are not removed upon instobj release.
41 * Instead they are placed into a LRU list to be recycled when the mapped space
42 * goes beyond a certain threshold. At the moment this limit is 1MB.
43 */
44 #include "priv.h"
45
46 #include <core/memory.h>
47 #include <core/mm.h>
48 #include <core/tegra.h>
49 #include <subdev/fb.h>
50 #include <subdev/ltc.h>
51 #include <subdev/mmu.h>
52
53 struct gk20a_instobj {
54 struct nvkm_memory memory;
55 struct nvkm_mm_node *mn;
56 struct gk20a_instmem *imem;
57
58 /* CPU mapping */
59 u32 *vaddr;
60 };
61 #define gk20a_instobj(p) container_of((p), struct gk20a_instobj, memory)
62
63 /*
64 * Used for objects allocated using the DMA API
65 */
66 struct gk20a_instobj_dma {
67 struct gk20a_instobj base;
68
69 dma_addr_t handle;
70 struct nvkm_mm_node r;
71 };
72 #define gk20a_instobj_dma(p) \
73 container_of(gk20a_instobj(p), struct gk20a_instobj_dma, base)
74
75 /*
76 * Used for objects flattened using the IOMMU API
77 */
78 struct gk20a_instobj_iommu {
79 struct gk20a_instobj base;
80
81 /* to link into gk20a_instmem::vaddr_lru */
82 struct list_head vaddr_node;
83 /* how many clients are using vaddr? */
84 u32 use_cpt;
85
86 /* will point to the higher half of pages */
87 dma_addr_t *dma_addrs;
88 /* array of base.mem->size pages (+ dma_addr_ts) */
89 struct page *pages[];
90 };
91 #define gk20a_instobj_iommu(p) \
92 container_of(gk20a_instobj(p), struct gk20a_instobj_iommu, base)
93
94 struct gk20a_instmem {
95 struct nvkm_instmem base;
96
97 /* protects vaddr_* and gk20a_instobj::vaddr* */
98 struct mutex lock;
99
100 /* CPU mappings LRU */
101 unsigned int vaddr_use;
102 unsigned int vaddr_max;
103 struct list_head vaddr_lru;
104
105 /* Only used if IOMMU if present */
106 struct mutex *mm_mutex;
107 struct nvkm_mm *mm;
108 struct iommu_domain *domain;
109 unsigned long iommu_pgshift;
110 u16 iommu_bit;
111
112 /* Only used by DMA API */
113 unsigned long attrs;
114 };
115 #define gk20a_instmem(p) container_of((p), struct gk20a_instmem, base)
116
117 static enum nvkm_memory_target
118 gk20a_instobj_target(struct nvkm_memory *memory)
119 {
120 return NVKM_MEM_TARGET_NCOH;
121 }
122
123 static u8
124 gk20a_instobj_page(struct nvkm_memory *memory)
125 {
126 return 12;
127 }
128
129 static u64
130 gk20a_instobj_addr(struct nvkm_memory *memory)
131 {
132 return (u64)gk20a_instobj(memory)->mn->offset << 12;
133 }
134
135 static u64
136 gk20a_instobj_size(struct nvkm_memory *memory)
137 {
138 return (u64)gk20a_instobj(memory)->mn->length << 12;
139 }
140
141 /*
142 * Recycle the vaddr of obj. Must be called with gk20a_instmem::lock held.
143 */
144 static void
145 gk20a_instobj_iommu_recycle_vaddr(struct gk20a_instobj_iommu *obj)
146 {
147 struct gk20a_instmem *imem = obj->base.imem;
148 /* there should not be any user left... */
149 WARN_ON(obj->use_cpt);
150 list_del(&obj->vaddr_node);
151 vunmap(obj->base.vaddr);
152 obj->base.vaddr = NULL;
153 imem->vaddr_use -= nvkm_memory_size(&obj->base.memory);
154 nvkm_debug(&imem->base.subdev, "vaddr used: %x/%x\n", imem->vaddr_use,
155 imem->vaddr_max);
156 }
157
158 /*
159 * Must be called while holding gk20a_instmem::lock
160 */
161 static void
162 gk20a_instmem_vaddr_gc(struct gk20a_instmem *imem, const u64 size)
163 {
164 while (imem->vaddr_use + size > imem->vaddr_max) {
165 /* no candidate that can be unmapped, abort... */
166 if (list_empty(&imem->vaddr_lru))
167 break;
168
169 gk20a_instobj_iommu_recycle_vaddr(
170 list_first_entry(&imem->vaddr_lru,
171 struct gk20a_instobj_iommu, vaddr_node));
172 }
173 }
174
175 static void __iomem *
176 gk20a_instobj_acquire_dma(struct nvkm_memory *memory)
177 {
178 struct gk20a_instobj *node = gk20a_instobj(memory);
179 struct gk20a_instmem *imem = node->imem;
180 struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
181
182 nvkm_ltc_flush(ltc);
183
184 return node->vaddr;
185 }
186
187 static void __iomem *
188 gk20a_instobj_acquire_iommu(struct nvkm_memory *memory)
189 {
190 struct gk20a_instobj_iommu *node = gk20a_instobj_iommu(memory);
191 struct gk20a_instmem *imem = node->base.imem;
192 struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
193 const u64 size = nvkm_memory_size(memory);
194
195 nvkm_ltc_flush(ltc);
196
197 mutex_lock(&imem->lock);
198
199 if (node->base.vaddr) {
200 if (!node->use_cpt) {
201 /* remove from LRU list since mapping in use again */
202 list_del(&node->vaddr_node);
203 }
204 goto out;
205 }
206
207 /* try to free some address space if we reached the limit */
208 gk20a_instmem_vaddr_gc(imem, size);
209
210 /* map the pages */
211 node->base.vaddr = vmap(node->pages, size >> PAGE_SHIFT, VM_MAP,
212 pgprot_writecombine(PAGE_KERNEL));
213 if (!node->base.vaddr) {
214 nvkm_error(&imem->base.subdev, "cannot map instobj - "
215 "this is not going to end well...\n");
216 goto out;
217 }
218
219 imem->vaddr_use += size;
220 nvkm_debug(&imem->base.subdev, "vaddr used: %x/%x\n",
221 imem->vaddr_use, imem->vaddr_max);
222
223 out:
224 node->use_cpt++;
225 mutex_unlock(&imem->lock);
226
227 return node->base.vaddr;
228 }
229
230 static void
231 gk20a_instobj_release_dma(struct nvkm_memory *memory)
232 {
233 struct gk20a_instobj *node = gk20a_instobj(memory);
234 struct gk20a_instmem *imem = node->imem;
235 struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
236
237 /* in case we got a write-combined mapping */
238 wmb();
239 nvkm_ltc_invalidate(ltc);
240 }
241
242 static void
243 gk20a_instobj_release_iommu(struct nvkm_memory *memory)
244 {
245 struct gk20a_instobj_iommu *node = gk20a_instobj_iommu(memory);
246 struct gk20a_instmem *imem = node->base.imem;
247 struct nvkm_ltc *ltc = imem->base.subdev.device->ltc;
248
249 mutex_lock(&imem->lock);
250
251 /* we should at least have one user to release... */
252 if (WARN_ON(node->use_cpt == 0))
253 goto out;
254
255 /* add unused objs to the LRU list to recycle their mapping */
256 if (--node->use_cpt == 0)
257 list_add_tail(&node->vaddr_node, &imem->vaddr_lru);
258
259 out:
260 mutex_unlock(&imem->lock);
261
262 wmb();
263 nvkm_ltc_invalidate(ltc);
264 }
265
266 static u32
267 gk20a_instobj_rd32(struct nvkm_memory *memory, u64 offset)
268 {
269 struct gk20a_instobj *node = gk20a_instobj(memory);
270
271 return node->vaddr[offset / 4];
272 }
273
274 static void
275 gk20a_instobj_wr32(struct nvkm_memory *memory, u64 offset, u32 data)
276 {
277 struct gk20a_instobj *node = gk20a_instobj(memory);
278
279 node->vaddr[offset / 4] = data;
280 }
281
282 static int
283 gk20a_instobj_map(struct nvkm_memory *memory, u64 offset, struct nvkm_vmm *vmm,
284 struct nvkm_vma *vma, void *argv, u32 argc)
285 {
286 struct gk20a_instobj *node = gk20a_instobj(memory);
287 struct nvkm_vmm_map map = {
288 .memory = &node->memory,
289 .offset = offset,
290 .mem = node->mn,
291 };
292
293 if (vma->vm) {
294 struct nvkm_mem mem = {
295 .mem = node->mn,
296 .memory = &node->memory,
297 };
298 nvkm_vm_map_at(vma, 0, &mem);
299 return 0;
300 }
301
302 return nvkm_vmm_map(vmm, vma, argv, argc, &map);
303 }
304
305 static void *
306 gk20a_instobj_dtor_dma(struct nvkm_memory *memory)
307 {
308 struct gk20a_instobj_dma *node = gk20a_instobj_dma(memory);
309 struct gk20a_instmem *imem = node->base.imem;
310 struct device *dev = imem->base.subdev.device->dev;
311
312 if (unlikely(!node->base.vaddr))
313 goto out;
314
315 dma_free_attrs(dev, (u64)node->base.mn->length << PAGE_SHIFT,
316 node->base.vaddr, node->handle, imem->attrs);
317
318 out:
319 return node;
320 }
321
322 static void *
323 gk20a_instobj_dtor_iommu(struct nvkm_memory *memory)
324 {
325 struct gk20a_instobj_iommu *node = gk20a_instobj_iommu(memory);
326 struct gk20a_instmem *imem = node->base.imem;
327 struct device *dev = imem->base.subdev.device->dev;
328 struct nvkm_mm_node *r = node->base.mn;
329 int i;
330
331 if (unlikely(!r))
332 goto out;
333
334 mutex_lock(&imem->lock);
335
336 /* vaddr has already been recycled */
337 if (node->base.vaddr)
338 gk20a_instobj_iommu_recycle_vaddr(node);
339
340 mutex_unlock(&imem->lock);
341
342 /* clear IOMMU bit to unmap pages */
343 r->offset &= ~BIT(imem->iommu_bit - imem->iommu_pgshift);
344
345 /* Unmap pages from GPU address space and free them */
346 for (i = 0; i < node->base.mn->length; i++) {
347 iommu_unmap(imem->domain,
348 (r->offset + i) << imem->iommu_pgshift, PAGE_SIZE);
349 dma_unmap_page(dev, node->dma_addrs[i], PAGE_SIZE,
350 DMA_BIDIRECTIONAL);
351 __free_page(node->pages[i]);
352 }
353
354 /* Release area from GPU address space */
355 mutex_lock(imem->mm_mutex);
356 nvkm_mm_free(imem->mm, &r);
357 mutex_unlock(imem->mm_mutex);
358
359 out:
360 return node;
361 }
362
363 static const struct nvkm_memory_func
364 gk20a_instobj_func_dma = {
365 .dtor = gk20a_instobj_dtor_dma,
366 .target = gk20a_instobj_target,
367 .page = gk20a_instobj_page,
368 .addr = gk20a_instobj_addr,
369 .size = gk20a_instobj_size,
370 .acquire = gk20a_instobj_acquire_dma,
371 .release = gk20a_instobj_release_dma,
372 .map = gk20a_instobj_map,
373 };
374
375 static const struct nvkm_memory_func
376 gk20a_instobj_func_iommu = {
377 .dtor = gk20a_instobj_dtor_iommu,
378 .target = gk20a_instobj_target,
379 .page = gk20a_instobj_page,
380 .addr = gk20a_instobj_addr,
381 .size = gk20a_instobj_size,
382 .acquire = gk20a_instobj_acquire_iommu,
383 .release = gk20a_instobj_release_iommu,
384 .map = gk20a_instobj_map,
385 };
386
387 static const struct nvkm_memory_ptrs
388 gk20a_instobj_ptrs = {
389 .rd32 = gk20a_instobj_rd32,
390 .wr32 = gk20a_instobj_wr32,
391 };
392
393 static int
394 gk20a_instobj_ctor_dma(struct gk20a_instmem *imem, u32 npages, u32 align,
395 struct gk20a_instobj **_node)
396 {
397 struct gk20a_instobj_dma *node;
398 struct nvkm_subdev *subdev = &imem->base.subdev;
399 struct device *dev = subdev->device->dev;
400
401 if (!(node = kzalloc(sizeof(*node), GFP_KERNEL)))
402 return -ENOMEM;
403 *_node = &node->base;
404
405 nvkm_memory_ctor(&gk20a_instobj_func_dma, &node->base.memory);
406 node->base.memory.ptrs = &gk20a_instobj_ptrs;
407
408 node->base.vaddr = dma_alloc_attrs(dev, npages << PAGE_SHIFT,
409 &node->handle, GFP_KERNEL,
410 imem->attrs);
411 if (!node->base.vaddr) {
412 nvkm_error(subdev, "cannot allocate DMA memory\n");
413 return -ENOMEM;
414 }
415
416 /* alignment check */
417 if (unlikely(node->handle & (align - 1)))
418 nvkm_warn(subdev,
419 "memory not aligned as requested: %pad (0x%x)\n",
420 &node->handle, align);
421
422 /* present memory for being mapped using small pages */
423 node->r.type = 12;
424 node->r.offset = node->handle >> 12;
425 node->r.length = (npages << PAGE_SHIFT) >> 12;
426
427 node->base.mn = &node->r;
428 return 0;
429 }
430
431 static int
432 gk20a_instobj_ctor_iommu(struct gk20a_instmem *imem, u32 npages, u32 align,
433 struct gk20a_instobj **_node)
434 {
435 struct gk20a_instobj_iommu *node;
436 struct nvkm_subdev *subdev = &imem->base.subdev;
437 struct device *dev = subdev->device->dev;
438 struct nvkm_mm_node *r;
439 int ret;
440 int i;
441
442 /*
443 * despite their variable size, instmem allocations are small enough
444 * (< 1 page) to be handled by kzalloc
445 */
446 if (!(node = kzalloc(sizeof(*node) + ((sizeof(node->pages[0]) +
447 sizeof(*node->dma_addrs)) * npages), GFP_KERNEL)))
448 return -ENOMEM;
449 *_node = &node->base;
450 node->dma_addrs = (void *)(node->pages + npages);
451
452 nvkm_memory_ctor(&gk20a_instobj_func_iommu, &node->base.memory);
453 node->base.memory.ptrs = &gk20a_instobj_ptrs;
454
455 /* Allocate backing memory */
456 for (i = 0; i < npages; i++) {
457 struct page *p = alloc_page(GFP_KERNEL);
458 dma_addr_t dma_adr;
459
460 if (p == NULL) {
461 ret = -ENOMEM;
462 goto free_pages;
463 }
464 node->pages[i] = p;
465 dma_adr = dma_map_page(dev, p, 0, PAGE_SIZE, DMA_BIDIRECTIONAL);
466 if (dma_mapping_error(dev, dma_adr)) {
467 nvkm_error(subdev, "DMA mapping error!\n");
468 ret = -ENOMEM;
469 goto free_pages;
470 }
471 node->dma_addrs[i] = dma_adr;
472 }
473
474 mutex_lock(imem->mm_mutex);
475 /* Reserve area from GPU address space */
476 ret = nvkm_mm_head(imem->mm, 0, 1, npages, npages,
477 align >> imem->iommu_pgshift, &r);
478 mutex_unlock(imem->mm_mutex);
479 if (ret) {
480 nvkm_error(subdev, "IOMMU space is full!\n");
481 goto free_pages;
482 }
483
484 /* Map into GPU address space */
485 for (i = 0; i < npages; i++) {
486 u32 offset = (r->offset + i) << imem->iommu_pgshift;
487
488 ret = iommu_map(imem->domain, offset, node->dma_addrs[i],
489 PAGE_SIZE, IOMMU_READ | IOMMU_WRITE);
490 if (ret < 0) {
491 nvkm_error(subdev, "IOMMU mapping failure: %d\n", ret);
492
493 while (i-- > 0) {
494 offset -= PAGE_SIZE;
495 iommu_unmap(imem->domain, offset, PAGE_SIZE);
496 }
497 goto release_area;
498 }
499 }
500
501 /* IOMMU bit tells that an address is to be resolved through the IOMMU */
502 r->offset |= BIT(imem->iommu_bit - imem->iommu_pgshift);
503
504 node->base.mn = r;
505 return 0;
506
507 release_area:
508 mutex_lock(imem->mm_mutex);
509 nvkm_mm_free(imem->mm, &r);
510 mutex_unlock(imem->mm_mutex);
511
512 free_pages:
513 for (i = 0; i < npages && node->pages[i] != NULL; i++) {
514 dma_addr_t dma_addr = node->dma_addrs[i];
515 if (dma_addr)
516 dma_unmap_page(dev, dma_addr, PAGE_SIZE,
517 DMA_BIDIRECTIONAL);
518 __free_page(node->pages[i]);
519 }
520
521 return ret;
522 }
523
524 static int
525 gk20a_instobj_new(struct nvkm_instmem *base, u32 size, u32 align, bool zero,
526 struct nvkm_memory **pmemory)
527 {
528 struct gk20a_instmem *imem = gk20a_instmem(base);
529 struct nvkm_subdev *subdev = &imem->base.subdev;
530 struct gk20a_instobj *node = NULL;
531 int ret;
532
533 nvkm_debug(subdev, "%s (%s): size: %x align: %x\n", __func__,
534 imem->domain ? "IOMMU" : "DMA", size, align);
535
536 /* Round size and align to page bounds */
537 size = max(roundup(size, PAGE_SIZE), PAGE_SIZE);
538 align = max(roundup(align, PAGE_SIZE), PAGE_SIZE);
539
540 if (imem->domain)
541 ret = gk20a_instobj_ctor_iommu(imem, size >> PAGE_SHIFT,
542 align, &node);
543 else
544 ret = gk20a_instobj_ctor_dma(imem, size >> PAGE_SHIFT,
545 align, &node);
546 *pmemory = node ? &node->memory : NULL;
547 if (ret)
548 return ret;
549
550 node->imem = imem;
551
552 nvkm_debug(subdev, "alloc size: 0x%x, align: 0x%x, gaddr: 0x%llx\n",
553 size, align, (u64)node->mn->offset << 12);
554
555 return 0;
556 }
557
558 static void *
559 gk20a_instmem_dtor(struct nvkm_instmem *base)
560 {
561 struct gk20a_instmem *imem = gk20a_instmem(base);
562
563 /* perform some sanity checks... */
564 if (!list_empty(&imem->vaddr_lru))
565 nvkm_warn(&base->subdev, "instobj LRU not empty!\n");
566
567 if (imem->vaddr_use != 0)
568 nvkm_warn(&base->subdev, "instobj vmap area not empty! "
569 "0x%x bytes still mapped\n", imem->vaddr_use);
570
571 return imem;
572 }
573
574 static const struct nvkm_instmem_func
575 gk20a_instmem = {
576 .dtor = gk20a_instmem_dtor,
577 .memory_new = gk20a_instobj_new,
578 .zero = false,
579 };
580
581 int
582 gk20a_instmem_new(struct nvkm_device *device, int index,
583 struct nvkm_instmem **pimem)
584 {
585 struct nvkm_device_tegra *tdev = device->func->tegra(device);
586 struct gk20a_instmem *imem;
587
588 if (!(imem = kzalloc(sizeof(*imem), GFP_KERNEL)))
589 return -ENOMEM;
590 nvkm_instmem_ctor(&gk20a_instmem, device, index, &imem->base);
591 mutex_init(&imem->lock);
592 *pimem = &imem->base;
593
594 /* do not allow more than 1MB of CPU-mapped instmem */
595 imem->vaddr_use = 0;
596 imem->vaddr_max = 0x100000;
597 INIT_LIST_HEAD(&imem->vaddr_lru);
598
599 if (tdev->iommu.domain) {
600 imem->mm_mutex = &tdev->iommu.mutex;
601 imem->mm = &tdev->iommu.mm;
602 imem->domain = tdev->iommu.domain;
603 imem->iommu_pgshift = tdev->iommu.pgshift;
604 imem->iommu_bit = tdev->func->iommu_bit;
605
606 nvkm_info(&imem->base.subdev, "using IOMMU\n");
607 } else {
608 imem->attrs = DMA_ATTR_NON_CONSISTENT |
609 DMA_ATTR_WEAK_ORDERING |
610 DMA_ATTR_WRITE_COMBINE;
611
612 nvkm_info(&imem->base.subdev, "using DMA API\n");
613 }
614
615 return 0;
616 }