1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2015-2018 Etnaviv Project
6 #include <linux/dma-mapping.h>
7 #include <linux/scatterlist.h>
9 #include "common.xml.h"
10 #include "etnaviv_cmdbuf.h"
11 #include "etnaviv_drv.h"
12 #include "etnaviv_gem.h"
13 #include "etnaviv_gpu.h"
14 #include "etnaviv_mmu.h"
16 static void etnaviv_context_unmap(struct etnaviv_iommu_context *context,
17 unsigned long iova, size_t size)
19 size_t unmapped_page, unmapped = 0;
20 size_t pgsize = SZ_4K;
22 if (!IS_ALIGNED(iova | size, pgsize)) {
23 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%zx\n",
28 while (unmapped < size) {
29 unmapped_page = context->global->ops->unmap(context, iova,
34 iova += unmapped_page;
35 unmapped += unmapped_page;
39 static int etnaviv_context_map(struct etnaviv_iommu_context *context,
40 unsigned long iova, phys_addr_t paddr,
41 size_t size, int prot)
43 unsigned long orig_iova = iova;
44 size_t pgsize = SZ_4K;
45 size_t orig_size = size;
48 if (!IS_ALIGNED(iova | paddr | size, pgsize)) {
49 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%zx\n",
50 iova, &paddr, size, pgsize);
55 ret = context->global->ops->map(context, iova, paddr, pgsize,
65 /* unroll mapping in case something went wrong */
67 etnaviv_context_unmap(context, orig_iova, orig_size - size);
72 static int etnaviv_iommu_map(struct etnaviv_iommu_context *context, u32 iova,
73 struct sg_table *sgt, unsigned len, int prot)
74 { struct scatterlist *sg;
75 unsigned int da = iova;
82 for_each_sgtable_dma_sg(sgt, sg, i) {
83 phys_addr_t pa = sg_dma_address(sg) - sg->offset;
84 size_t bytes = sg_dma_len(sg) + sg->offset;
86 VERB("map[%d]: %08x %pap(%zx)", i, iova, &pa, bytes);
88 ret = etnaviv_context_map(context, da, pa, bytes, prot);
100 etnaviv_context_unmap(context, iova, da - iova);
104 static void etnaviv_iommu_unmap(struct etnaviv_iommu_context *context, u32 iova,
105 struct sg_table *sgt, unsigned len)
107 struct scatterlist *sg;
108 unsigned int da = iova;
111 for_each_sgtable_dma_sg(sgt, sg, i) {
112 size_t bytes = sg_dma_len(sg) + sg->offset;
114 etnaviv_context_unmap(context, da, bytes);
116 VERB("unmap[%d]: %08x(%zx)", i, iova, bytes);
118 BUG_ON(!PAGE_ALIGNED(bytes));
123 context->flush_seq++;
126 static void etnaviv_iommu_remove_mapping(struct etnaviv_iommu_context *context,
127 struct etnaviv_vram_mapping *mapping)
129 struct etnaviv_gem_object *etnaviv_obj = mapping->object;
131 lockdep_assert_held(&context->lock);
133 etnaviv_iommu_unmap(context, mapping->vram_node.start,
134 etnaviv_obj->sgt, etnaviv_obj->base.size);
135 drm_mm_remove_node(&mapping->vram_node);
138 void etnaviv_iommu_reap_mapping(struct etnaviv_vram_mapping *mapping)
140 struct etnaviv_iommu_context *context = mapping->context;
142 lockdep_assert_held(&context->lock);
143 WARN_ON(mapping->use);
145 etnaviv_iommu_remove_mapping(context, mapping);
146 etnaviv_iommu_context_put(mapping->context);
147 mapping->context = NULL;
148 list_del_init(&mapping->mmu_node);
151 static int etnaviv_iommu_find_iova(struct etnaviv_iommu_context *context,
152 struct drm_mm_node *node, size_t size)
154 struct etnaviv_vram_mapping *free = NULL;
155 enum drm_mm_insert_mode mode = DRM_MM_INSERT_LOW;
158 lockdep_assert_held(&context->lock);
161 struct etnaviv_vram_mapping *m, *n;
162 struct drm_mm_scan scan;
163 struct list_head list;
166 ret = drm_mm_insert_node_in_range(&context->mm, node,
167 size, 0, 0, 0, U64_MAX, mode);
171 /* Try to retire some entries */
172 drm_mm_scan_init(&scan, &context->mm, size, 0, 0, mode);
175 INIT_LIST_HEAD(&list);
176 list_for_each_entry(free, &context->mappings, mmu_node) {
177 /* If this vram node has not been used, skip this. */
178 if (!free->vram_node.mm)
182 * If the iova is pinned, then it's in-use,
183 * so we must keep its mapping.
188 list_add(&free->scan_node, &list);
189 if (drm_mm_scan_add_block(&scan, &free->vram_node)) {
196 /* Nothing found, clean up and fail */
197 list_for_each_entry_safe(m, n, &list, scan_node)
198 BUG_ON(drm_mm_scan_remove_block(&scan, &m->vram_node));
203 * drm_mm does not allow any other operations while
204 * scanning, so we have to remove all blocks first.
205 * If drm_mm_scan_remove_block() returns false, we
206 * can leave the block pinned.
208 list_for_each_entry_safe(m, n, &list, scan_node)
209 if (!drm_mm_scan_remove_block(&scan, &m->vram_node))
210 list_del_init(&m->scan_node);
213 * Unmap the blocks which need to be reaped from the MMU.
214 * Clear the mmu pointer to prevent the mapping_get finding
217 list_for_each_entry_safe(m, n, &list, scan_node) {
218 etnaviv_iommu_reap_mapping(m);
219 list_del_init(&m->scan_node);
222 mode = DRM_MM_INSERT_EVICT;
225 * We removed enough mappings so that the new allocation will
226 * succeed, retry the allocation one more time.
233 static int etnaviv_iommu_insert_exact(struct etnaviv_iommu_context *context,
234 struct drm_mm_node *node, size_t size, u64 va)
236 struct etnaviv_vram_mapping *m, *n;
237 struct drm_mm_node *scan_node;
238 LIST_HEAD(scan_list);
241 lockdep_assert_held(&context->lock);
243 ret = drm_mm_insert_node_in_range(&context->mm, node, size, 0, 0, va,
244 va + size, DRM_MM_INSERT_LOWEST);
249 * When we can't insert the node, due to a existing mapping blocking
250 * the address space, there are two possible reasons:
251 * 1. Userspace genuinely messed up and tried to reuse address space
252 * before the last job using this VMA has finished executing.
253 * 2. The existing buffer mappings are idle, but the buffers are not
254 * destroyed yet (likely due to being referenced by another context) in
255 * which case the mappings will not be cleaned up and we must reap them
256 * here to make space for the new mapping.
259 drm_mm_for_each_node_in_range(scan_node, &context->mm, va, va + size) {
260 m = container_of(scan_node, struct etnaviv_vram_mapping,
266 list_add(&m->scan_node, &scan_list);
269 list_for_each_entry_safe(m, n, &scan_list, scan_node) {
270 etnaviv_iommu_reap_mapping(m);
271 list_del_init(&m->scan_node);
274 return drm_mm_insert_node_in_range(&context->mm, node, size, 0, 0, va,
275 va + size, DRM_MM_INSERT_LOWEST);
278 int etnaviv_iommu_map_gem(struct etnaviv_iommu_context *context,
279 struct etnaviv_gem_object *etnaviv_obj, u32 memory_base,
280 struct etnaviv_vram_mapping *mapping, u64 va)
282 struct sg_table *sgt = etnaviv_obj->sgt;
283 struct drm_mm_node *node;
286 lockdep_assert_held(&etnaviv_obj->lock);
288 mutex_lock(&context->lock);
290 /* v1 MMU can optimize single entry (contiguous) scatterlists */
291 if (context->global->version == ETNAVIV_IOMMU_V1 &&
292 sgt->nents == 1 && !(etnaviv_obj->flags & ETNA_BO_FORCE_MMU)) {
295 iova = sg_dma_address(sgt->sgl) - memory_base;
296 if (iova < 0x80000000 - sg_dma_len(sgt->sgl)) {
297 mapping->iova = iova;
298 mapping->context = etnaviv_iommu_context_get(context);
299 list_add_tail(&mapping->mmu_node, &context->mappings);
305 node = &mapping->vram_node;
308 ret = etnaviv_iommu_insert_exact(context, node,
309 etnaviv_obj->base.size, va);
311 ret = etnaviv_iommu_find_iova(context, node,
312 etnaviv_obj->base.size);
316 mapping->iova = node->start;
317 ret = etnaviv_iommu_map(context, node->start, sgt, etnaviv_obj->base.size,
318 ETNAVIV_PROT_READ | ETNAVIV_PROT_WRITE);
321 drm_mm_remove_node(node);
325 mapping->context = etnaviv_iommu_context_get(context);
326 list_add_tail(&mapping->mmu_node, &context->mappings);
328 mutex_unlock(&context->lock);
333 void etnaviv_iommu_unmap_gem(struct etnaviv_iommu_context *context,
334 struct etnaviv_vram_mapping *mapping)
336 WARN_ON(mapping->use);
338 mutex_lock(&context->lock);
340 /* Bail if the mapping has been reaped by another thread */
341 if (!mapping->context) {
342 mutex_unlock(&context->lock);
346 /* If the vram node is on the mm, unmap and remove the node */
347 if (mapping->vram_node.mm == &context->mm)
348 etnaviv_iommu_remove_mapping(context, mapping);
350 list_del(&mapping->mmu_node);
351 mutex_unlock(&context->lock);
352 etnaviv_iommu_context_put(context);
355 static void etnaviv_iommu_context_free(struct kref *kref)
357 struct etnaviv_iommu_context *context =
358 container_of(kref, struct etnaviv_iommu_context, refcount);
360 etnaviv_cmdbuf_suballoc_unmap(context, &context->cmdbuf_mapping);
362 context->global->ops->free(context);
364 void etnaviv_iommu_context_put(struct etnaviv_iommu_context *context)
366 kref_put(&context->refcount, etnaviv_iommu_context_free);
369 struct etnaviv_iommu_context *
370 etnaviv_iommu_context_init(struct etnaviv_iommu_global *global,
371 struct etnaviv_cmdbuf_suballoc *suballoc)
373 struct etnaviv_iommu_context *ctx;
376 if (global->version == ETNAVIV_IOMMU_V1)
377 ctx = etnaviv_iommuv1_context_alloc(global);
379 ctx = etnaviv_iommuv2_context_alloc(global);
384 ret = etnaviv_cmdbuf_suballoc_map(suballoc, ctx, &ctx->cmdbuf_mapping,
385 global->memory_base);
389 if (global->version == ETNAVIV_IOMMU_V1 &&
390 ctx->cmdbuf_mapping.iova > 0x80000000) {
392 "command buffer outside valid memory window\n");
399 etnaviv_cmdbuf_suballoc_unmap(ctx, &ctx->cmdbuf_mapping);
401 global->ops->free(ctx);
405 void etnaviv_iommu_restore(struct etnaviv_gpu *gpu,
406 struct etnaviv_iommu_context *context)
408 context->global->ops->restore(gpu, context);
411 int etnaviv_iommu_get_suballoc_va(struct etnaviv_iommu_context *context,
412 struct etnaviv_vram_mapping *mapping,
413 u32 memory_base, dma_addr_t paddr,
416 mutex_lock(&context->lock);
418 if (mapping->use > 0) {
420 mutex_unlock(&context->lock);
425 * For MMUv1 we don't add the suballoc region to the pagetables, as
426 * those GPUs can only work with cmdbufs accessed through the linear
427 * window. Instead we manufacture a mapping to make it look uniform
428 * to the upper layers.
430 if (context->global->version == ETNAVIV_IOMMU_V1) {
431 mapping->iova = paddr - memory_base;
433 struct drm_mm_node *node = &mapping->vram_node;
436 ret = etnaviv_iommu_find_iova(context, node, size);
438 mutex_unlock(&context->lock);
442 mapping->iova = node->start;
443 ret = etnaviv_context_map(context, node->start, paddr, size,
446 drm_mm_remove_node(node);
447 mutex_unlock(&context->lock);
451 context->flush_seq++;
454 list_add_tail(&mapping->mmu_node, &context->mappings);
457 mutex_unlock(&context->lock);
462 void etnaviv_iommu_put_suballoc_va(struct etnaviv_iommu_context *context,
463 struct etnaviv_vram_mapping *mapping)
465 struct drm_mm_node *node = &mapping->vram_node;
467 mutex_lock(&context->lock);
470 if (mapping->use > 0 || context->global->version == ETNAVIV_IOMMU_V1) {
471 mutex_unlock(&context->lock);
475 etnaviv_context_unmap(context, node->start, node->size);
476 drm_mm_remove_node(node);
477 mutex_unlock(&context->lock);
480 size_t etnaviv_iommu_dump_size(struct etnaviv_iommu_context *context)
482 return context->global->ops->dump_size(context);
485 void etnaviv_iommu_dump(struct etnaviv_iommu_context *context, void *buf)
487 context->global->ops->dump(context, buf);
490 int etnaviv_iommu_global_init(struct etnaviv_gpu *gpu)
492 enum etnaviv_iommu_version version = ETNAVIV_IOMMU_V1;
493 struct etnaviv_drm_private *priv = gpu->drm->dev_private;
494 struct etnaviv_iommu_global *global;
495 struct device *dev = gpu->drm->dev;
497 if (gpu->identity.minor_features1 & chipMinorFeatures1_MMU_VERSION)
498 version = ETNAVIV_IOMMU_V2;
500 if (priv->mmu_global) {
501 if (priv->mmu_global->version != version) {
503 "MMU version doesn't match global version\n");
507 priv->mmu_global->use++;
511 global = kzalloc(sizeof(*global), GFP_KERNEL);
515 global->bad_page_cpu = dma_alloc_wc(dev, SZ_4K, &global->bad_page_dma,
517 if (!global->bad_page_cpu)
520 memset32(global->bad_page_cpu, 0xdead55aa, SZ_4K / sizeof(u32));
522 if (version == ETNAVIV_IOMMU_V2) {
523 global->v2.pta_cpu = dma_alloc_wc(dev, ETNAVIV_PTA_SIZE,
524 &global->v2.pta_dma, GFP_KERNEL);
525 if (!global->v2.pta_cpu)
530 global->version = version;
532 mutex_init(&global->lock);
534 if (version == ETNAVIV_IOMMU_V1)
535 global->ops = &etnaviv_iommuv1_ops;
537 global->ops = &etnaviv_iommuv2_ops;
539 priv->mmu_global = global;
544 dma_free_wc(dev, SZ_4K, global->bad_page_cpu, global->bad_page_dma);
551 void etnaviv_iommu_global_fini(struct etnaviv_gpu *gpu)
553 struct etnaviv_drm_private *priv = gpu->drm->dev_private;
554 struct etnaviv_iommu_global *global = priv->mmu_global;
559 if (--global->use > 0)
562 if (global->v2.pta_cpu)
563 dma_free_wc(global->dev, ETNAVIV_PTA_SIZE,
564 global->v2.pta_cpu, global->v2.pta_dma);
566 if (global->bad_page_cpu)
567 dma_free_wc(global->dev, SZ_4K,
568 global->bad_page_cpu, global->bad_page_dma);
570 mutex_destroy(&global->lock);
573 priv->mmu_global = NULL;