1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) Rockchip Electronics Co.Ltd
4 * Author: Felix Zeng <felix.zeng@rock-chips.com>
5 */
6
7 #include "rknpu_iommu.h"
8
rknpu_iommu_dma_alloc_iova(struct iommu_domain * domain,size_t size,u64 dma_limit,struct device * dev)9 dma_addr_t rknpu_iommu_dma_alloc_iova(struct iommu_domain *domain, size_t size,
10 u64 dma_limit, struct device *dev)
11 {
12 struct rknpu_iommu_dma_cookie *cookie = (void *)domain->iova_cookie;
13 struct iova_domain *iovad = &cookie->iovad;
14 unsigned long shift, iova_len, iova = 0;
15 #if (KERNEL_VERSION(5, 4, 0) > LINUX_VERSION_CODE)
16 dma_addr_t limit;
17 #endif
18
19 shift = iova_shift(iovad);
20 iova_len = size >> shift;
21
22 #if KERNEL_VERSION(6, 1, 0) > LINUX_VERSION_CODE
23 /*
24 * Freeing non-power-of-two-sized allocations back into the IOVA caches
25 * will come back to bite us badly, so we have to waste a bit of space
26 * rounding up anything cacheable to make sure that can't happen. The
27 * order of the unadjusted size will still match upon freeing.
28 */
29 if (iova_len < (1 << (IOVA_RANGE_CACHE_MAX_SIZE - 1)))
30 iova_len = roundup_pow_of_two(iova_len);
31 #endif
32
33 #if (KERNEL_VERSION(5, 10, 0) <= LINUX_VERSION_CODE)
34 dma_limit = min_not_zero(dma_limit, dev->bus_dma_limit);
35 #else
36 if (dev->bus_dma_mask)
37 dma_limit &= dev->bus_dma_mask;
38 #endif
39
40 if (domain->geometry.force_aperture)
41 dma_limit =
42 min_t(u64, dma_limit, domain->geometry.aperture_end);
43
44 #if (KERNEL_VERSION(5, 4, 0) <= LINUX_VERSION_CODE)
45 iova = alloc_iova_fast(iovad, iova_len, dma_limit >> shift, true);
46 #else
47 limit = min_t(dma_addr_t, dma_limit >> shift, iovad->end_pfn);
48
49 iova = alloc_iova_fast(iovad, iova_len, limit, true);
50 #endif
51
52 return (dma_addr_t)iova << shift;
53 }
54
rknpu_iommu_dma_free_iova(struct rknpu_iommu_dma_cookie * cookie,dma_addr_t iova,size_t size)55 void rknpu_iommu_dma_free_iova(struct rknpu_iommu_dma_cookie *cookie,
56 dma_addr_t iova, size_t size)
57 {
58 struct iova_domain *iovad = &cookie->iovad;
59
60 free_iova_fast(iovad, iova_pfn(iovad, iova), size >> iova_shift(iovad));
61 }
62