2 * Copyright (C) 2015 Etnaviv Project
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "common.xml.h"
18 #include "etnaviv_cmdbuf.h"
19 #include "etnaviv_drv.h"
20 #include "etnaviv_gem.h"
21 #include "etnaviv_gpu.h"
22 #include "etnaviv_iommu.h"
23 #include "etnaviv_mmu.h"
25 static int etnaviv_fault_handler(struct iommu_domain *iommu, struct device *dev,
26 unsigned long iova, int flags, void *arg)
28 DBG("*** fault: iova=%08lx, flags=%d", iova, flags);
32 int etnaviv_iommu_map(struct etnaviv_iommu *iommu, u32 iova,
33 struct sg_table *sgt, unsigned len, int prot)
35 struct iommu_domain *domain = iommu->domain;
36 struct scatterlist *sg;
37 unsigned int da = iova;
44 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
45 u32 pa = sg_dma_address(sg) - sg->offset;
46 size_t bytes = sg_dma_len(sg) + sg->offset;
48 VERB("map[%d]: %08x %08x(%zx)", i, iova, pa, bytes);
50 ret = iommu_map(domain, da, pa, bytes, prot);
62 for_each_sg(sgt->sgl, sg, i, j) {
63 size_t bytes = sg_dma_len(sg) + sg->offset;
65 iommu_unmap(domain, da, bytes);
71 int etnaviv_iommu_unmap(struct etnaviv_iommu *iommu, u32 iova,
72 struct sg_table *sgt, unsigned len)
74 struct iommu_domain *domain = iommu->domain;
75 struct scatterlist *sg;
76 unsigned int da = iova;
79 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
80 size_t bytes = sg_dma_len(sg) + sg->offset;
83 unmapped = iommu_unmap(domain, da, bytes);
87 VERB("unmap[%d]: %08x(%zx)", i, iova, bytes);
89 BUG_ON(!PAGE_ALIGNED(bytes));
97 static void etnaviv_iommu_remove_mapping(struct etnaviv_iommu *mmu,
98 struct etnaviv_vram_mapping *mapping)
100 struct etnaviv_gem_object *etnaviv_obj = mapping->object;
102 etnaviv_iommu_unmap(mmu, mapping->vram_node.start,
103 etnaviv_obj->sgt, etnaviv_obj->base.size);
104 drm_mm_remove_node(&mapping->vram_node);
107 static int etnaviv_iommu_find_iova(struct etnaviv_iommu *mmu,
108 struct drm_mm_node *node, size_t size)
110 struct etnaviv_vram_mapping *free = NULL;
113 lockdep_assert_held(&mmu->lock);
116 struct etnaviv_vram_mapping *m, *n;
117 struct list_head list;
121 * XXX: The DRM_MM_SEARCH_BELOW is really a hack to trick
122 * drm_mm into giving out a low IOVA after address space
123 * rollover. This needs a proper fix.
125 ret = drm_mm_insert_node_in_range(&mmu->mm, node,
126 size, 0, mmu->last_iova, ~0UL,
127 mmu->last_iova ? DRM_MM_SEARCH_DEFAULT : DRM_MM_SEARCH_BELOW);
133 * If we did not search from the start of the MMU region,
134 * try again in case there are free slots.
136 if (mmu->last_iova) {
138 mmu->need_flush = true;
142 /* Try to retire some entries */
143 drm_mm_init_scan(&mmu->mm, size, 0, 0);
146 INIT_LIST_HEAD(&list);
147 list_for_each_entry(free, &mmu->mappings, mmu_node) {
148 /* If this vram node has not been used, skip this. */
149 if (!free->vram_node.mm)
153 * If the iova is pinned, then it's in-use,
154 * so we must keep its mapping.
159 list_add(&free->scan_node, &list);
160 if (drm_mm_scan_add_block(&free->vram_node)) {
167 /* Nothing found, clean up and fail */
168 list_for_each_entry_safe(m, n, &list, scan_node)
169 BUG_ON(drm_mm_scan_remove_block(&m->vram_node));
174 * drm_mm does not allow any other operations while
175 * scanning, so we have to remove all blocks first.
176 * If drm_mm_scan_remove_block() returns false, we
177 * can leave the block pinned.
179 list_for_each_entry_safe(m, n, &list, scan_node)
180 if (!drm_mm_scan_remove_block(&m->vram_node))
181 list_del_init(&m->scan_node);
184 * Unmap the blocks which need to be reaped from the MMU.
185 * Clear the mmu pointer to prevent the mapping_get finding
188 list_for_each_entry_safe(m, n, &list, scan_node) {
189 etnaviv_iommu_remove_mapping(mmu, m);
191 list_del_init(&m->mmu_node);
192 list_del_init(&m->scan_node);
196 * We removed enough mappings so that the new allocation will
197 * succeed, retry the allocation one more time.
204 int etnaviv_iommu_map_gem(struct etnaviv_iommu *mmu,
205 struct etnaviv_gem_object *etnaviv_obj, u32 memory_base,
206 struct etnaviv_vram_mapping *mapping)
208 struct sg_table *sgt = etnaviv_obj->sgt;
209 struct drm_mm_node *node;
212 lockdep_assert_held(&etnaviv_obj->lock);
214 mutex_lock(&mmu->lock);
216 /* v1 MMU can optimize single entry (contiguous) scatterlists */
217 if (mmu->version == ETNAVIV_IOMMU_V1 &&
218 sgt->nents == 1 && !(etnaviv_obj->flags & ETNA_BO_FORCE_MMU)) {
221 iova = sg_dma_address(sgt->sgl) - memory_base;
222 if (iova < 0x80000000 - sg_dma_len(sgt->sgl)) {
223 mapping->iova = iova;
224 list_add_tail(&mapping->mmu_node, &mmu->mappings);
225 mutex_unlock(&mmu->lock);
230 node = &mapping->vram_node;
232 ret = etnaviv_iommu_find_iova(mmu, node, etnaviv_obj->base.size);
234 mutex_unlock(&mmu->lock);
238 mmu->last_iova = node->start + etnaviv_obj->base.size;
239 mapping->iova = node->start;
240 ret = etnaviv_iommu_map(mmu, node->start, sgt, etnaviv_obj->base.size,
241 IOMMU_READ | IOMMU_WRITE);
244 drm_mm_remove_node(node);
245 mutex_unlock(&mmu->lock);
249 list_add_tail(&mapping->mmu_node, &mmu->mappings);
250 mmu->need_flush = true;
251 mutex_unlock(&mmu->lock);
256 void etnaviv_iommu_unmap_gem(struct etnaviv_iommu *mmu,
257 struct etnaviv_vram_mapping *mapping)
259 WARN_ON(mapping->use);
261 mutex_lock(&mmu->lock);
263 /* If the vram node is on the mm, unmap and remove the node */
264 if (mapping->vram_node.mm == &mmu->mm)
265 etnaviv_iommu_remove_mapping(mmu, mapping);
267 list_del(&mapping->mmu_node);
268 mmu->need_flush = true;
269 mutex_unlock(&mmu->lock);
272 void etnaviv_iommu_destroy(struct etnaviv_iommu *mmu)
274 drm_mm_takedown(&mmu->mm);
275 iommu_domain_free(mmu->domain);
279 struct etnaviv_iommu *etnaviv_iommu_new(struct etnaviv_gpu *gpu)
281 enum etnaviv_iommu_version version;
282 struct etnaviv_iommu *mmu;
284 mmu = kzalloc(sizeof(*mmu), GFP_KERNEL);
286 return ERR_PTR(-ENOMEM);
288 if (!(gpu->identity.minor_features1 & chipMinorFeatures1_MMU_VERSION)) {
289 mmu->domain = etnaviv_iommuv1_domain_alloc(gpu);
290 version = ETNAVIV_IOMMU_V1;
292 mmu->domain = etnaviv_iommuv2_domain_alloc(gpu);
293 version = ETNAVIV_IOMMU_V2;
297 dev_err(gpu->dev, "Failed to allocate GPU IOMMU domain\n");
299 return ERR_PTR(-ENOMEM);
303 mmu->version = version;
304 mutex_init(&mmu->lock);
305 INIT_LIST_HEAD(&mmu->mappings);
307 drm_mm_init(&mmu->mm, mmu->domain->geometry.aperture_start,
308 mmu->domain->geometry.aperture_end -
309 mmu->domain->geometry.aperture_start + 1);
311 iommu_set_fault_handler(mmu->domain, etnaviv_fault_handler, gpu->dev);
316 void etnaviv_iommu_restore(struct etnaviv_gpu *gpu)
318 if (gpu->mmu->version == ETNAVIV_IOMMU_V1)
319 etnaviv_iommuv1_restore(gpu);
321 etnaviv_iommuv2_restore(gpu);
324 int etnaviv_iommu_get_suballoc_va(struct etnaviv_gpu *gpu, dma_addr_t paddr,
325 struct drm_mm_node *vram_node, size_t size,
328 struct etnaviv_iommu *mmu = gpu->mmu;
330 if (mmu->version == ETNAVIV_IOMMU_V1) {
331 *iova = paddr - gpu->memory_base;
336 mutex_lock(&mmu->lock);
337 ret = etnaviv_iommu_find_iova(mmu, vram_node, size);
339 mutex_unlock(&mmu->lock);
342 ret = iommu_map(mmu->domain, vram_node->start, paddr, size,
345 drm_mm_remove_node(vram_node);
346 mutex_unlock(&mmu->lock);
349 mmu->last_iova = vram_node->start + size;
350 gpu->mmu->need_flush = true;
351 mutex_unlock(&mmu->lock);
353 *iova = (u32)vram_node->start;
358 void etnaviv_iommu_put_suballoc_va(struct etnaviv_gpu *gpu,
359 struct drm_mm_node *vram_node, size_t size,
362 struct etnaviv_iommu *mmu = gpu->mmu;
364 if (mmu->version == ETNAVIV_IOMMU_V2) {
365 mutex_lock(&mmu->lock);
366 iommu_unmap(mmu->domain,iova, size);
367 drm_mm_remove_node(vram_node);
368 mutex_unlock(&mmu->lock);
371 size_t etnaviv_iommu_dump_size(struct etnaviv_iommu *iommu)
373 struct etnaviv_iommu_ops *ops;
375 ops = container_of(iommu->domain->ops, struct etnaviv_iommu_ops, ops);
377 return ops->dump_size(iommu->domain);
380 void etnaviv_iommu_dump(struct etnaviv_iommu *iommu, void *buf)
382 struct etnaviv_iommu_ops *ops;
384 ops = container_of(iommu->domain->ops, struct etnaviv_iommu_ops, ops);
386 ops->dump(iommu->domain, buf);