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 u32 pa = sg_dma_address(sg) - sg->offset;
84 size_t bytes = sg_dma_len(sg) + sg->offset;
86 VERB("map[%d]: %08x %08x(%zx)", i, iova, pa, bytes);
88 ret = etnaviv_context_map(context, da, pa, bytes, prot);
98 etnaviv_context_unmap(context, iova, da - iova);
102 static void etnaviv_iommu_unmap(struct etnaviv_iommu_context *context, u32 iova,
103 struct sg_table *sgt, unsigned len)
105 struct scatterlist *sg;
106 unsigned int da = iova;
109 for_each_sgtable_dma_sg(sgt, sg, i) {
110 size_t bytes = sg_dma_len(sg) + sg->offset;
112 etnaviv_context_unmap(context, da, bytes);
114 VERB("unmap[%d]: %08x(%zx)", i, iova, bytes);
116 BUG_ON(!PAGE_ALIGNED(bytes));
122 static void etnaviv_iommu_remove_mapping(struct etnaviv_iommu_context *context,
123 struct etnaviv_vram_mapping *mapping)
125 struct etnaviv_gem_object *etnaviv_obj = mapping->object;
127 lockdep_assert_held(&context->lock);
129 etnaviv_iommu_unmap(context, mapping->vram_node.start,
130 etnaviv_obj->sgt, etnaviv_obj->base.size);
131 drm_mm_remove_node(&mapping->vram_node);
134 static int etnaviv_iommu_find_iova(struct etnaviv_iommu_context *context,
135 struct drm_mm_node *node, size_t size)
137 struct etnaviv_vram_mapping *free = NULL;
138 enum drm_mm_insert_mode mode = DRM_MM_INSERT_LOW;
141 lockdep_assert_held(&context->lock);
144 struct etnaviv_vram_mapping *m, *n;
145 struct drm_mm_scan scan;
146 struct list_head list;
149 ret = drm_mm_insert_node_in_range(&context->mm, node,
150 size, 0, 0, 0, U64_MAX, mode);
154 /* Try to retire some entries */
155 drm_mm_scan_init(&scan, &context->mm, size, 0, 0, mode);
158 INIT_LIST_HEAD(&list);
159 list_for_each_entry(free, &context->mappings, mmu_node) {
160 /* If this vram node has not been used, skip this. */
161 if (!free->vram_node.mm)
165 * If the iova is pinned, then it's in-use,
166 * so we must keep its mapping.
171 list_add(&free->scan_node, &list);
172 if (drm_mm_scan_add_block(&scan, &free->vram_node)) {
179 /* Nothing found, clean up and fail */
180 list_for_each_entry_safe(m, n, &list, scan_node)
181 BUG_ON(drm_mm_scan_remove_block(&scan, &m->vram_node));
186 * drm_mm does not allow any other operations while
187 * scanning, so we have to remove all blocks first.
188 * If drm_mm_scan_remove_block() returns false, we
189 * can leave the block pinned.
191 list_for_each_entry_safe(m, n, &list, scan_node)
192 if (!drm_mm_scan_remove_block(&scan, &m->vram_node))
193 list_del_init(&m->scan_node);
196 * Unmap the blocks which need to be reaped from the MMU.
197 * Clear the mmu pointer to prevent the mapping_get finding
200 list_for_each_entry_safe(m, n, &list, scan_node) {
201 etnaviv_iommu_remove_mapping(context, m);
202 etnaviv_iommu_context_put(m->context);
204 list_del_init(&m->mmu_node);
205 list_del_init(&m->scan_node);
208 mode = DRM_MM_INSERT_EVICT;
211 * We removed enough mappings so that the new allocation will
212 * succeed, retry the allocation one more time.
219 static int etnaviv_iommu_insert_exact(struct etnaviv_iommu_context *context,
220 struct drm_mm_node *node, size_t size, u64 va)
222 lockdep_assert_held(&context->lock);
224 return drm_mm_insert_node_in_range(&context->mm, node, size, 0, 0, va,
225 va + size, DRM_MM_INSERT_LOWEST);
228 int etnaviv_iommu_map_gem(struct etnaviv_iommu_context *context,
229 struct etnaviv_gem_object *etnaviv_obj, u32 memory_base,
230 struct etnaviv_vram_mapping *mapping, u64 va)
232 struct sg_table *sgt = etnaviv_obj->sgt;
233 struct drm_mm_node *node;
236 lockdep_assert_held(&etnaviv_obj->lock);
238 mutex_lock(&context->lock);
240 /* v1 MMU can optimize single entry (contiguous) scatterlists */
241 if (context->global->version == ETNAVIV_IOMMU_V1 &&
242 sgt->nents == 1 && !(etnaviv_obj->flags & ETNA_BO_FORCE_MMU)) {
245 iova = sg_dma_address(sgt->sgl) - memory_base;
246 if (iova < 0x80000000 - sg_dma_len(sgt->sgl)) {
247 mapping->iova = iova;
248 list_add_tail(&mapping->mmu_node, &context->mappings);
254 node = &mapping->vram_node;
257 ret = etnaviv_iommu_insert_exact(context, node,
258 etnaviv_obj->base.size, va);
260 ret = etnaviv_iommu_find_iova(context, node,
261 etnaviv_obj->base.size);
265 mapping->iova = node->start;
266 ret = etnaviv_iommu_map(context, node->start, sgt, etnaviv_obj->base.size,
267 ETNAVIV_PROT_READ | ETNAVIV_PROT_WRITE);
270 drm_mm_remove_node(node);
274 list_add_tail(&mapping->mmu_node, &context->mappings);
275 context->flush_seq++;
277 mutex_unlock(&context->lock);
282 void etnaviv_iommu_unmap_gem(struct etnaviv_iommu_context *context,
283 struct etnaviv_vram_mapping *mapping)
285 WARN_ON(mapping->use);
287 mutex_lock(&context->lock);
289 /* If the vram node is on the mm, unmap and remove the node */
290 if (mapping->vram_node.mm == &context->mm)
291 etnaviv_iommu_remove_mapping(context, mapping);
293 list_del(&mapping->mmu_node);
294 context->flush_seq++;
295 mutex_unlock(&context->lock);
298 static void etnaviv_iommu_context_free(struct kref *kref)
300 struct etnaviv_iommu_context *context =
301 container_of(kref, struct etnaviv_iommu_context, refcount);
303 etnaviv_cmdbuf_suballoc_unmap(context, &context->cmdbuf_mapping);
305 context->global->ops->free(context);
307 void etnaviv_iommu_context_put(struct etnaviv_iommu_context *context)
309 kref_put(&context->refcount, etnaviv_iommu_context_free);
312 struct etnaviv_iommu_context *
313 etnaviv_iommu_context_init(struct etnaviv_iommu_global *global,
314 struct etnaviv_cmdbuf_suballoc *suballoc)
316 struct etnaviv_iommu_context *ctx;
319 if (global->version == ETNAVIV_IOMMU_V1)
320 ctx = etnaviv_iommuv1_context_alloc(global);
322 ctx = etnaviv_iommuv2_context_alloc(global);
327 ret = etnaviv_cmdbuf_suballoc_map(suballoc, ctx, &ctx->cmdbuf_mapping,
328 global->memory_base);
332 if (global->version == ETNAVIV_IOMMU_V1 &&
333 ctx->cmdbuf_mapping.iova > 0x80000000) {
335 "command buffer outside valid memory window\n");
342 etnaviv_cmdbuf_suballoc_unmap(ctx, &ctx->cmdbuf_mapping);
344 global->ops->free(ctx);
348 void etnaviv_iommu_restore(struct etnaviv_gpu *gpu,
349 struct etnaviv_iommu_context *context)
351 context->global->ops->restore(gpu, context);
354 int etnaviv_iommu_get_suballoc_va(struct etnaviv_iommu_context *context,
355 struct etnaviv_vram_mapping *mapping,
356 u32 memory_base, dma_addr_t paddr,
359 mutex_lock(&context->lock);
361 if (mapping->use > 0) {
363 mutex_unlock(&context->lock);
368 * For MMUv1 we don't add the suballoc region to the pagetables, as
369 * those GPUs can only work with cmdbufs accessed through the linear
370 * window. Instead we manufacture a mapping to make it look uniform
371 * to the upper layers.
373 if (context->global->version == ETNAVIV_IOMMU_V1) {
374 mapping->iova = paddr - memory_base;
376 struct drm_mm_node *node = &mapping->vram_node;
379 ret = etnaviv_iommu_find_iova(context, node, size);
381 mutex_unlock(&context->lock);
385 mapping->iova = node->start;
386 ret = etnaviv_context_map(context, node->start, paddr, size,
389 drm_mm_remove_node(node);
390 mutex_unlock(&context->lock);
394 context->flush_seq++;
397 list_add_tail(&mapping->mmu_node, &context->mappings);
400 mutex_unlock(&context->lock);
405 void etnaviv_iommu_put_suballoc_va(struct etnaviv_iommu_context *context,
406 struct etnaviv_vram_mapping *mapping)
408 struct drm_mm_node *node = &mapping->vram_node;
410 mutex_lock(&context->lock);
413 if (mapping->use > 0 || context->global->version == ETNAVIV_IOMMU_V1) {
414 mutex_unlock(&context->lock);
418 etnaviv_context_unmap(context, node->start, node->size);
419 drm_mm_remove_node(node);
420 mutex_unlock(&context->lock);
423 size_t etnaviv_iommu_dump_size(struct etnaviv_iommu_context *context)
425 return context->global->ops->dump_size(context);
428 void etnaviv_iommu_dump(struct etnaviv_iommu_context *context, void *buf)
430 context->global->ops->dump(context, buf);
433 int etnaviv_iommu_global_init(struct etnaviv_gpu *gpu)
435 enum etnaviv_iommu_version version = ETNAVIV_IOMMU_V1;
436 struct etnaviv_drm_private *priv = gpu->drm->dev_private;
437 struct etnaviv_iommu_global *global;
438 struct device *dev = gpu->drm->dev;
440 if (gpu->identity.minor_features1 & chipMinorFeatures1_MMU_VERSION)
441 version = ETNAVIV_IOMMU_V2;
443 if (priv->mmu_global) {
444 if (priv->mmu_global->version != version) {
446 "MMU version doesn't match global version\n");
450 priv->mmu_global->use++;
454 global = kzalloc(sizeof(*global), GFP_KERNEL);
458 global->bad_page_cpu = dma_alloc_wc(dev, SZ_4K, &global->bad_page_dma,
460 if (!global->bad_page_cpu)
463 memset32(global->bad_page_cpu, 0xdead55aa, SZ_4K / sizeof(u32));
465 if (version == ETNAVIV_IOMMU_V2) {
466 global->v2.pta_cpu = dma_alloc_wc(dev, ETNAVIV_PTA_SIZE,
467 &global->v2.pta_dma, GFP_KERNEL);
468 if (!global->v2.pta_cpu)
473 global->version = version;
475 mutex_init(&global->lock);
477 if (version == ETNAVIV_IOMMU_V1)
478 global->ops = &etnaviv_iommuv1_ops;
480 global->ops = &etnaviv_iommuv2_ops;
482 priv->mmu_global = global;
487 dma_free_wc(dev, SZ_4K, global->bad_page_cpu, global->bad_page_dma);
494 void etnaviv_iommu_global_fini(struct etnaviv_gpu *gpu)
496 struct etnaviv_drm_private *priv = gpu->drm->dev_private;
497 struct etnaviv_iommu_global *global = priv->mmu_global;
499 if (--global->use > 0)
502 if (global->v2.pta_cpu)
503 dma_free_wc(global->dev, ETNAVIV_PTA_SIZE,
504 global->v2.pta_cpu, global->v2.pta_dma);
506 if (global->bad_page_cpu)
507 dma_free_wc(global->dev, SZ_4K,
508 global->bad_page_cpu, global->bad_page_dma);
510 mutex_destroy(&global->lock);
513 priv->mmu_global = NULL;