Liam Mark | 4d4fbba | 2017-02-08 10:30:49 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2016-2017, The Linux Foundation. All rights reserved. |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/dma-contiguous.h> |
| 14 | #include <linux/dma-mapping.h> |
| 15 | #include <linux/dma-mapping-fast.h> |
| 16 | #include <linux/io-pgtable-fast.h> |
Patrick Daly | 7bcb546 | 2016-08-03 17:27:36 -0700 | [diff] [blame] | 17 | #include <linux/vmalloc.h> |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 18 | #include <asm/cacheflush.h> |
| 19 | #include <asm/dma-iommu.h> |
Charan Teja Reddy | 29f6140 | 2017-02-09 20:44:29 +0530 | [diff] [blame] | 20 | #include <linux/slab.h> |
| 21 | #include <linux/vmalloc.h> |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 22 | |
| 23 | /* some redundant definitions... :( TODO: move to io-pgtable-fast.h */ |
| 24 | #define FAST_PAGE_SHIFT 12 |
| 25 | #define FAST_PAGE_SIZE (1UL << FAST_PAGE_SHIFT) |
| 26 | #define FAST_PAGE_MASK (~(PAGE_SIZE - 1)) |
| 27 | #define FAST_PTE_ADDR_MASK ((av8l_fast_iopte)0xfffffffff000) |
Liam Mark | 83a9f86e | 2017-02-08 09:37:17 -0800 | [diff] [blame^] | 28 | #define FAST_MAIR_ATTR_IDX_CACHE 1 |
| 29 | #define FAST_PTE_ATTRINDX_SHIFT 2 |
| 30 | #define FAST_PTE_ATTRINDX_MASK 0x7 |
| 31 | #define FAST_PTE_SH_SHIFT 8 |
| 32 | #define FAST_PTE_SH_MASK (((av8l_fast_iopte)0x3) << FAST_PTE_SH_SHIFT) |
| 33 | #define FAST_PTE_SH_OS (((av8l_fast_iopte)2) << FAST_PTE_SH_SHIFT) |
| 34 | #define FAST_PTE_SH_IS (((av8l_fast_iopte)3) << FAST_PTE_SH_SHIFT) |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 35 | |
Mitchel Humpherys | 425d03d | 2016-06-23 13:25:12 -0700 | [diff] [blame] | 36 | static pgprot_t __get_dma_pgprot(unsigned long attrs, pgprot_t prot, |
| 37 | bool coherent) |
| 38 | { |
| 39 | if (attrs & DMA_ATTR_STRONGLY_ORDERED) |
| 40 | return pgprot_noncached(prot); |
| 41 | else if (!coherent || (attrs & DMA_ATTR_WRITE_COMBINE)) |
| 42 | return pgprot_writecombine(prot); |
| 43 | return prot; |
| 44 | } |
| 45 | |
| 46 | static int __get_iommu_pgprot(unsigned long attrs, int prot, |
| 47 | bool coherent) |
| 48 | { |
| 49 | if (!(attrs & DMA_ATTR_EXEC_MAPPING)) |
| 50 | prot |= IOMMU_NOEXEC; |
| 51 | if ((attrs & DMA_ATTR_STRONGLY_ORDERED)) |
| 52 | prot |= IOMMU_MMIO; |
| 53 | if (coherent) |
| 54 | prot |= IOMMU_CACHE; |
| 55 | |
| 56 | return prot; |
| 57 | } |
| 58 | |
Mitchel Humpherys | 9de66db | 2016-06-07 11:09:44 -0700 | [diff] [blame] | 59 | static void fast_dmac_clean_range(struct dma_fast_smmu_mapping *mapping, |
| 60 | void *start, void *end) |
| 61 | { |
| 62 | if (!mapping->is_smmu_pt_coherent) |
| 63 | dmac_clean_range(start, end); |
| 64 | } |
| 65 | |
Liam Mark | 83a9f86e | 2017-02-08 09:37:17 -0800 | [diff] [blame^] | 66 | static bool __fast_is_pte_coherent(av8l_fast_iopte *ptep) |
| 67 | { |
| 68 | int attr_idx = (*ptep & (FAST_PTE_ATTRINDX_MASK << |
| 69 | FAST_PTE_ATTRINDX_SHIFT)) >> |
| 70 | FAST_PTE_ATTRINDX_SHIFT; |
| 71 | |
| 72 | if ((attr_idx == FAST_MAIR_ATTR_IDX_CACHE) && |
| 73 | (((*ptep & FAST_PTE_SH_MASK) == FAST_PTE_SH_IS) || |
| 74 | (*ptep & FAST_PTE_SH_MASK) == FAST_PTE_SH_OS)) |
| 75 | return true; |
| 76 | |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | static bool is_dma_coherent(struct device *dev, unsigned long attrs) |
| 81 | { |
| 82 | bool is_coherent; |
| 83 | |
| 84 | if (attrs & DMA_ATTR_FORCE_COHERENT) |
| 85 | is_coherent = true; |
| 86 | else if (attrs & DMA_ATTR_FORCE_NON_COHERENT) |
| 87 | is_coherent = false; |
| 88 | else if (is_device_dma_coherent(dev)) |
| 89 | is_coherent = true; |
| 90 | else |
| 91 | is_coherent = false; |
| 92 | |
| 93 | return is_coherent; |
| 94 | } |
| 95 | |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 96 | /* |
| 97 | * Checks if the allocated range (ending at @end) covered the upcoming |
| 98 | * stale bit. We don't need to know exactly where the range starts since |
| 99 | * we already know where the candidate search range started. If, starting |
| 100 | * from the beginning of the candidate search range, we had to step over |
| 101 | * (or landed directly on top of) the upcoming stale bit, then we return |
| 102 | * true. |
| 103 | * |
| 104 | * Due to wrapping, there are two scenarios we'll need to check: (1) if the |
| 105 | * range [search_start, upcoming_stale] spans 0 (i.e. search_start > |
| 106 | * upcoming_stale), and, (2) if the range: [search_start, upcoming_stale] |
| 107 | * does *not* span 0 (i.e. search_start <= upcoming_stale). And for each |
| 108 | * of those two scenarios we need to handle three cases: (1) the bit was |
| 109 | * found before wrapping or |
| 110 | */ |
| 111 | static bool __bit_covered_stale(unsigned long upcoming_stale, |
| 112 | unsigned long search_start, |
| 113 | unsigned long end) |
| 114 | { |
| 115 | if (search_start > upcoming_stale) { |
| 116 | if (end >= search_start) { |
| 117 | /* |
| 118 | * We started searching above upcoming_stale and we |
| 119 | * didn't wrap, so we couldn't have crossed |
| 120 | * upcoming_stale. |
| 121 | */ |
| 122 | return false; |
| 123 | } |
| 124 | /* |
| 125 | * We wrapped. Did we cross (or land on top of) |
| 126 | * upcoming_stale? |
| 127 | */ |
| 128 | return end >= upcoming_stale; |
| 129 | } |
| 130 | |
| 131 | if (search_start <= upcoming_stale) { |
| 132 | if (end >= search_start) { |
| 133 | /* |
| 134 | * We didn't wrap. Did we cross (or land on top |
| 135 | * of) upcoming_stale? |
| 136 | */ |
| 137 | return end >= upcoming_stale; |
| 138 | } |
| 139 | /* |
| 140 | * We wrapped. So we must have crossed upcoming_stale |
| 141 | * (since we started searching below it). |
| 142 | */ |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | /* we should have covered all logical combinations... */ |
| 147 | WARN_ON(1); |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | static dma_addr_t __fast_smmu_alloc_iova(struct dma_fast_smmu_mapping *mapping, |
Mitchel Humpherys | 5c704e0 | 2015-12-21 15:06:34 -0800 | [diff] [blame] | 152 | unsigned long attrs, |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 153 | size_t size) |
| 154 | { |
| 155 | unsigned long bit, prev_search_start, nbits = size >> FAST_PAGE_SHIFT; |
| 156 | unsigned long align = (1 << get_order(size)) - 1; |
| 157 | |
| 158 | bit = bitmap_find_next_zero_area( |
| 159 | mapping->bitmap, mapping->num_4k_pages, mapping->next_start, |
| 160 | nbits, align); |
| 161 | if (unlikely(bit > mapping->num_4k_pages)) { |
| 162 | /* try wrapping */ |
| 163 | mapping->next_start = 0; /* TODO: SHOULD I REALLY DO THIS?!? */ |
| 164 | bit = bitmap_find_next_zero_area( |
| 165 | mapping->bitmap, mapping->num_4k_pages, 0, nbits, |
| 166 | align); |
| 167 | if (unlikely(bit > mapping->num_4k_pages)) |
| 168 | return DMA_ERROR_CODE; |
| 169 | } |
| 170 | |
| 171 | bitmap_set(mapping->bitmap, bit, nbits); |
| 172 | prev_search_start = mapping->next_start; |
| 173 | mapping->next_start = bit + nbits; |
| 174 | if (unlikely(mapping->next_start >= mapping->num_4k_pages)) |
| 175 | mapping->next_start = 0; |
| 176 | |
| 177 | /* |
| 178 | * If we just re-allocated a VA whose TLB hasn't been invalidated |
| 179 | * since it was last used and unmapped, we need to invalidate it |
| 180 | * here. We actually invalidate the entire TLB so that we don't |
| 181 | * have to invalidate the TLB again until we wrap back around. |
| 182 | */ |
| 183 | if (mapping->have_stale_tlbs && |
| 184 | __bit_covered_stale(mapping->upcoming_stale_bit, |
| 185 | prev_search_start, |
| 186 | bit + nbits - 1)) { |
Mitchel Humpherys | 5c704e0 | 2015-12-21 15:06:34 -0800 | [diff] [blame] | 187 | bool skip_sync = (attrs & DMA_ATTR_SKIP_CPU_SYNC); |
| 188 | |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 189 | iommu_tlbiall(mapping->domain); |
| 190 | mapping->have_stale_tlbs = false; |
Mitchel Humpherys | 5c704e0 | 2015-12-21 15:06:34 -0800 | [diff] [blame] | 191 | av8l_fast_clear_stale_ptes(mapping->pgtbl_pmds, skip_sync); |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | return (bit << FAST_PAGE_SHIFT) + mapping->base; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Checks whether the candidate bit will be allocated sooner than the |
| 199 | * current upcoming stale bit. We can say candidate will be upcoming |
| 200 | * sooner than the current upcoming stale bit if it lies between the |
| 201 | * starting bit of the next search range and the upcoming stale bit |
| 202 | * (allowing for wrap-around). |
| 203 | * |
| 204 | * Stated differently, we're checking the relative ordering of three |
| 205 | * unsigned numbers. So we need to check all 6 (i.e. 3!) permutations, |
| 206 | * namely: |
| 207 | * |
| 208 | * 0 |---A---B---C---| TOP (Case 1) |
| 209 | * 0 |---A---C---B---| TOP (Case 2) |
| 210 | * 0 |---B---A---C---| TOP (Case 3) |
| 211 | * 0 |---B---C---A---| TOP (Case 4) |
| 212 | * 0 |---C---A---B---| TOP (Case 5) |
| 213 | * 0 |---C---B---A---| TOP (Case 6) |
| 214 | * |
| 215 | * Note that since we're allowing numbers to wrap, the following three |
| 216 | * scenarios are all equivalent for Case 1: |
| 217 | * |
| 218 | * 0 |---A---B---C---| TOP |
| 219 | * 0 |---C---A---B---| TOP (C has wrapped. This is Case 5.) |
| 220 | * 0 |---B---C---A---| TOP (C and B have wrapped. This is Case 4.) |
| 221 | * |
| 222 | * In any of these cases, if we start searching from A, we will find B |
| 223 | * before we find C. |
| 224 | * |
| 225 | * We can also find two equivalent cases for Case 2: |
| 226 | * |
| 227 | * 0 |---A---C---B---| TOP |
| 228 | * 0 |---B---A---C---| TOP (B has wrapped. This is Case 3.) |
| 229 | * 0 |---C---B---A---| TOP (B and C have wrapped. This is Case 6.) |
| 230 | * |
| 231 | * In any of these cases, if we start searching from A, we will find C |
| 232 | * before we find B. |
| 233 | */ |
| 234 | static bool __bit_is_sooner(unsigned long candidate, |
| 235 | struct dma_fast_smmu_mapping *mapping) |
| 236 | { |
| 237 | unsigned long A = mapping->next_start; |
| 238 | unsigned long B = candidate; |
| 239 | unsigned long C = mapping->upcoming_stale_bit; |
| 240 | |
| 241 | if ((A < B && B < C) || /* Case 1 */ |
| 242 | (C < A && A < B) || /* Case 5 */ |
| 243 | (B < C && C < A)) /* Case 4 */ |
| 244 | return true; |
| 245 | |
| 246 | if ((A < C && C < B) || /* Case 2 */ |
| 247 | (B < A && A < C) || /* Case 3 */ |
| 248 | (C < B && B < A)) /* Case 6 */ |
| 249 | return false; |
| 250 | |
| 251 | /* |
| 252 | * For simplicity, we've been ignoring the possibility of any of |
| 253 | * our three numbers being equal. Handle those cases here (they |
| 254 | * shouldn't happen very often, (I think?)). |
| 255 | */ |
| 256 | |
| 257 | /* |
| 258 | * If candidate is the next bit to be searched then it's definitely |
| 259 | * sooner. |
| 260 | */ |
| 261 | if (A == B) |
| 262 | return true; |
| 263 | |
| 264 | /* |
| 265 | * If candidate is the next upcoming stale bit we'll return false |
| 266 | * to avoid doing `upcoming = candidate' in the caller (which would |
| 267 | * be useless since they're already equal) |
| 268 | */ |
| 269 | if (B == C) |
| 270 | return false; |
| 271 | |
| 272 | /* |
| 273 | * If next start is the upcoming stale bit then candidate can't |
| 274 | * possibly be sooner. The "soonest" bit is already selected. |
| 275 | */ |
| 276 | if (A == C) |
| 277 | return false; |
| 278 | |
| 279 | /* We should have covered all logical combinations. */ |
| 280 | WARN(1, "Well, that's awkward. A=%ld, B=%ld, C=%ld\n", A, B, C); |
| 281 | return true; |
| 282 | } |
| 283 | |
| 284 | static void __fast_smmu_free_iova(struct dma_fast_smmu_mapping *mapping, |
| 285 | dma_addr_t iova, size_t size) |
| 286 | { |
| 287 | unsigned long start_bit = (iova - mapping->base) >> FAST_PAGE_SHIFT; |
| 288 | unsigned long nbits = size >> FAST_PAGE_SHIFT; |
| 289 | |
| 290 | /* |
| 291 | * We don't invalidate TLBs on unmap. We invalidate TLBs on map |
| 292 | * when we're about to re-allocate a VA that was previously |
| 293 | * unmapped but hasn't yet been invalidated. So we need to keep |
| 294 | * track of which bit is the closest to being re-allocated here. |
| 295 | */ |
| 296 | if (__bit_is_sooner(start_bit, mapping)) |
| 297 | mapping->upcoming_stale_bit = start_bit; |
| 298 | |
| 299 | bitmap_clear(mapping->bitmap, start_bit, nbits); |
| 300 | mapping->have_stale_tlbs = true; |
| 301 | } |
| 302 | |
| 303 | |
| 304 | static void __fast_dma_page_cpu_to_dev(struct page *page, unsigned long off, |
| 305 | size_t size, enum dma_data_direction dir) |
| 306 | { |
| 307 | __dma_map_area(page_address(page) + off, size, dir); |
| 308 | } |
| 309 | |
| 310 | static void __fast_dma_page_dev_to_cpu(struct page *page, unsigned long off, |
| 311 | size_t size, enum dma_data_direction dir) |
| 312 | { |
| 313 | __dma_unmap_area(page_address(page) + off, size, dir); |
| 314 | |
| 315 | /* TODO: WHAT IS THIS? */ |
| 316 | /* |
| 317 | * Mark the D-cache clean for this page to avoid extra flushing. |
| 318 | */ |
| 319 | if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE) |
| 320 | set_bit(PG_dcache_clean, &page->flags); |
| 321 | } |
| 322 | |
| 323 | static int __fast_dma_direction_to_prot(enum dma_data_direction dir) |
| 324 | { |
| 325 | switch (dir) { |
| 326 | case DMA_BIDIRECTIONAL: |
| 327 | return IOMMU_READ | IOMMU_WRITE; |
| 328 | case DMA_TO_DEVICE: |
| 329 | return IOMMU_READ; |
| 330 | case DMA_FROM_DEVICE: |
| 331 | return IOMMU_WRITE; |
| 332 | default: |
| 333 | return 0; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | static dma_addr_t fast_smmu_map_page(struct device *dev, struct page *page, |
| 338 | unsigned long offset, size_t size, |
| 339 | enum dma_data_direction dir, |
| 340 | unsigned long attrs) |
| 341 | { |
| 342 | struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast; |
| 343 | dma_addr_t iova; |
| 344 | unsigned long flags; |
| 345 | av8l_fast_iopte *pmd; |
| 346 | phys_addr_t phys_plus_off = page_to_phys(page) + offset; |
| 347 | phys_addr_t phys_to_map = round_down(phys_plus_off, FAST_PAGE_SIZE); |
| 348 | unsigned long offset_from_phys_to_map = phys_plus_off & ~FAST_PAGE_MASK; |
| 349 | size_t len = ALIGN(size + offset_from_phys_to_map, FAST_PAGE_SIZE); |
| 350 | int nptes = len >> FAST_PAGE_SHIFT; |
| 351 | bool skip_sync = (attrs & DMA_ATTR_SKIP_CPU_SYNC); |
| 352 | int prot = __fast_dma_direction_to_prot(dir); |
Liam Mark | 83a9f86e | 2017-02-08 09:37:17 -0800 | [diff] [blame^] | 353 | bool is_coherent = is_dma_coherent(dev, attrs); |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 354 | |
Mitchel Humpherys | 425d03d | 2016-06-23 13:25:12 -0700 | [diff] [blame] | 355 | prot = __get_iommu_pgprot(attrs, prot, is_coherent); |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 356 | |
Mitchel Humpherys | 425d03d | 2016-06-23 13:25:12 -0700 | [diff] [blame] | 357 | if (!skip_sync && !is_coherent) |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 358 | __fast_dma_page_cpu_to_dev(phys_to_page(phys_to_map), |
| 359 | offset_from_phys_to_map, size, dir); |
| 360 | |
| 361 | spin_lock_irqsave(&mapping->lock, flags); |
| 362 | |
Mitchel Humpherys | 5c704e0 | 2015-12-21 15:06:34 -0800 | [diff] [blame] | 363 | iova = __fast_smmu_alloc_iova(mapping, attrs, len); |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 364 | |
| 365 | if (unlikely(iova == DMA_ERROR_CODE)) |
| 366 | goto fail; |
| 367 | |
| 368 | pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova); |
| 369 | |
| 370 | if (unlikely(av8l_fast_map_public(pmd, phys_to_map, len, prot))) |
| 371 | goto fail_free_iova; |
| 372 | |
Mitchel Humpherys | 9de66db | 2016-06-07 11:09:44 -0700 | [diff] [blame] | 373 | fast_dmac_clean_range(mapping, pmd, pmd + nptes); |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 374 | |
| 375 | spin_unlock_irqrestore(&mapping->lock, flags); |
| 376 | return iova + offset_from_phys_to_map; |
| 377 | |
| 378 | fail_free_iova: |
| 379 | __fast_smmu_free_iova(mapping, iova, size); |
| 380 | fail: |
| 381 | spin_unlock_irqrestore(&mapping->lock, flags); |
| 382 | return DMA_ERROR_CODE; |
| 383 | } |
| 384 | |
| 385 | static void fast_smmu_unmap_page(struct device *dev, dma_addr_t iova, |
| 386 | size_t size, enum dma_data_direction dir, |
| 387 | unsigned long attrs) |
| 388 | { |
| 389 | struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast; |
| 390 | unsigned long flags; |
| 391 | av8l_fast_iopte *pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova); |
| 392 | unsigned long offset = iova & ~FAST_PAGE_MASK; |
| 393 | size_t len = ALIGN(size + offset, FAST_PAGE_SIZE); |
| 394 | int nptes = len >> FAST_PAGE_SHIFT; |
| 395 | struct page *page = phys_to_page((*pmd & FAST_PTE_ADDR_MASK)); |
| 396 | bool skip_sync = (attrs & DMA_ATTR_SKIP_CPU_SYNC); |
Liam Mark | 83a9f86e | 2017-02-08 09:37:17 -0800 | [diff] [blame^] | 397 | bool is_coherent = is_dma_coherent(dev, attrs); |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 398 | |
Mitchel Humpherys | 425d03d | 2016-06-23 13:25:12 -0700 | [diff] [blame] | 399 | if (!skip_sync && !is_coherent) |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 400 | __fast_dma_page_dev_to_cpu(page, offset, size, dir); |
| 401 | |
| 402 | spin_lock_irqsave(&mapping->lock, flags); |
| 403 | av8l_fast_unmap_public(pmd, len); |
Mitchel Humpherys | 9de66db | 2016-06-07 11:09:44 -0700 | [diff] [blame] | 404 | fast_dmac_clean_range(mapping, pmd, pmd + nptes); |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 405 | __fast_smmu_free_iova(mapping, iova, len); |
| 406 | spin_unlock_irqrestore(&mapping->lock, flags); |
| 407 | } |
| 408 | |
Liam Mark | 78d7fb5 | 2016-12-01 13:05:31 -0800 | [diff] [blame] | 409 | static void fast_smmu_sync_single_for_cpu(struct device *dev, |
| 410 | dma_addr_t iova, size_t size, enum dma_data_direction dir) |
| 411 | { |
| 412 | struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast; |
| 413 | av8l_fast_iopte *pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova); |
| 414 | unsigned long offset = iova & ~FAST_PAGE_MASK; |
| 415 | struct page *page = phys_to_page((*pmd & FAST_PTE_ADDR_MASK)); |
| 416 | |
Liam Mark | 83a9f86e | 2017-02-08 09:37:17 -0800 | [diff] [blame^] | 417 | if (!__fast_is_pte_coherent(pmd)) |
Liam Mark | 78d7fb5 | 2016-12-01 13:05:31 -0800 | [diff] [blame] | 418 | __fast_dma_page_dev_to_cpu(page, offset, size, dir); |
| 419 | } |
| 420 | |
| 421 | static void fast_smmu_sync_single_for_device(struct device *dev, |
| 422 | dma_addr_t iova, size_t size, enum dma_data_direction dir) |
| 423 | { |
| 424 | struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast; |
| 425 | av8l_fast_iopte *pmd = iopte_pmd_offset(mapping->pgtbl_pmds, iova); |
| 426 | unsigned long offset = iova & ~FAST_PAGE_MASK; |
| 427 | struct page *page = phys_to_page((*pmd & FAST_PTE_ADDR_MASK)); |
| 428 | |
Liam Mark | 83a9f86e | 2017-02-08 09:37:17 -0800 | [diff] [blame^] | 429 | if (!__fast_is_pte_coherent(pmd)) |
Liam Mark | 78d7fb5 | 2016-12-01 13:05:31 -0800 | [diff] [blame] | 430 | __fast_dma_page_cpu_to_dev(page, offset, size, dir); |
| 431 | } |
| 432 | |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 433 | static int fast_smmu_map_sg(struct device *dev, struct scatterlist *sg, |
| 434 | int nents, enum dma_data_direction dir, |
| 435 | unsigned long attrs) |
| 436 | { |
| 437 | return -EINVAL; |
| 438 | } |
| 439 | |
| 440 | static void fast_smmu_unmap_sg(struct device *dev, |
| 441 | struct scatterlist *sg, int nents, |
| 442 | enum dma_data_direction dir, |
| 443 | unsigned long attrs) |
| 444 | { |
| 445 | WARN_ON_ONCE(1); |
| 446 | } |
| 447 | |
Liam Mark | 78d7fb5 | 2016-12-01 13:05:31 -0800 | [diff] [blame] | 448 | static void fast_smmu_sync_sg_for_cpu(struct device *dev, |
| 449 | struct scatterlist *sg, int nents, enum dma_data_direction dir) |
| 450 | { |
| 451 | WARN_ON_ONCE(1); |
| 452 | } |
| 453 | |
| 454 | static void fast_smmu_sync_sg_for_device(struct device *dev, |
| 455 | struct scatterlist *sg, int nents, enum dma_data_direction dir) |
| 456 | { |
| 457 | WARN_ON_ONCE(1); |
| 458 | } |
| 459 | |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 460 | static void __fast_smmu_free_pages(struct page **pages, int count) |
| 461 | { |
| 462 | int i; |
| 463 | |
| 464 | for (i = 0; i < count; i++) |
| 465 | __free_page(pages[i]); |
| 466 | kvfree(pages); |
| 467 | } |
| 468 | |
| 469 | static struct page **__fast_smmu_alloc_pages(unsigned int count, gfp_t gfp) |
| 470 | { |
| 471 | struct page **pages; |
| 472 | unsigned int i = 0, array_size = count * sizeof(*pages); |
| 473 | |
| 474 | if (array_size <= PAGE_SIZE) |
| 475 | pages = kzalloc(array_size, GFP_KERNEL); |
| 476 | else |
| 477 | pages = vzalloc(array_size); |
| 478 | if (!pages) |
| 479 | return NULL; |
| 480 | |
| 481 | /* IOMMU can map any pages, so himem can also be used here */ |
| 482 | gfp |= __GFP_NOWARN | __GFP_HIGHMEM; |
| 483 | |
| 484 | for (i = 0; i < count; ++i) { |
| 485 | struct page *page = alloc_page(gfp); |
| 486 | |
| 487 | if (!page) { |
| 488 | __fast_smmu_free_pages(pages, i); |
| 489 | return NULL; |
| 490 | } |
| 491 | pages[i] = page; |
| 492 | } |
| 493 | return pages; |
| 494 | } |
| 495 | |
| 496 | static void *fast_smmu_alloc(struct device *dev, size_t size, |
| 497 | dma_addr_t *handle, gfp_t gfp, |
| 498 | unsigned long attrs) |
| 499 | { |
| 500 | struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast; |
| 501 | struct sg_table sgt; |
| 502 | dma_addr_t dma_addr, iova_iter; |
| 503 | void *addr; |
| 504 | av8l_fast_iopte *ptep; |
| 505 | unsigned long flags; |
| 506 | struct sg_mapping_iter miter; |
| 507 | unsigned int count = ALIGN(size, SZ_4K) >> PAGE_SHIFT; |
| 508 | int prot = IOMMU_READ | IOMMU_WRITE; /* TODO: extract from attrs */ |
Liam Mark | 83a9f86e | 2017-02-08 09:37:17 -0800 | [diff] [blame^] | 509 | bool is_coherent = is_dma_coherent(dev, attrs); |
Mitchel Humpherys | 425d03d | 2016-06-23 13:25:12 -0700 | [diff] [blame] | 510 | pgprot_t remap_prot = __get_dma_pgprot(attrs, PAGE_KERNEL, is_coherent); |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 511 | struct page **pages; |
| 512 | |
Mitchel Humpherys | 425d03d | 2016-06-23 13:25:12 -0700 | [diff] [blame] | 513 | prot = __get_iommu_pgprot(attrs, prot, is_coherent); |
| 514 | |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 515 | *handle = DMA_ERROR_CODE; |
| 516 | |
| 517 | pages = __fast_smmu_alloc_pages(count, gfp); |
| 518 | if (!pages) { |
| 519 | dev_err(dev, "no pages\n"); |
| 520 | return NULL; |
| 521 | } |
| 522 | |
| 523 | size = ALIGN(size, SZ_4K); |
| 524 | if (sg_alloc_table_from_pages(&sgt, pages, count, 0, size, gfp)) { |
| 525 | dev_err(dev, "no sg tablen\n"); |
| 526 | goto out_free_pages; |
| 527 | } |
| 528 | |
Mitchel Humpherys | 425d03d | 2016-06-23 13:25:12 -0700 | [diff] [blame] | 529 | if (!is_coherent) { |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 530 | /* |
| 531 | * The CPU-centric flushing implied by SG_MITER_TO_SG isn't |
| 532 | * sufficient here, so skip it by using the "wrong" direction. |
| 533 | */ |
| 534 | sg_miter_start(&miter, sgt.sgl, sgt.orig_nents, |
| 535 | SG_MITER_FROM_SG); |
| 536 | while (sg_miter_next(&miter)) |
Kyle Yan | 65be4a5 | 2016-10-31 15:05:00 -0700 | [diff] [blame] | 537 | __dma_flush_area(miter.addr, miter.length); |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 538 | sg_miter_stop(&miter); |
| 539 | } |
| 540 | |
| 541 | spin_lock_irqsave(&mapping->lock, flags); |
Mitchel Humpherys | 5c704e0 | 2015-12-21 15:06:34 -0800 | [diff] [blame] | 542 | dma_addr = __fast_smmu_alloc_iova(mapping, attrs, size); |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 543 | if (dma_addr == DMA_ERROR_CODE) { |
| 544 | dev_err(dev, "no iova\n"); |
| 545 | spin_unlock_irqrestore(&mapping->lock, flags); |
| 546 | goto out_free_sg; |
| 547 | } |
| 548 | iova_iter = dma_addr; |
| 549 | sg_miter_start(&miter, sgt.sgl, sgt.orig_nents, |
| 550 | SG_MITER_FROM_SG | SG_MITER_ATOMIC); |
| 551 | while (sg_miter_next(&miter)) { |
| 552 | int nptes = miter.length >> FAST_PAGE_SHIFT; |
| 553 | |
| 554 | ptep = iopte_pmd_offset(mapping->pgtbl_pmds, iova_iter); |
| 555 | if (unlikely(av8l_fast_map_public( |
| 556 | ptep, page_to_phys(miter.page), |
| 557 | miter.length, prot))) { |
| 558 | dev_err(dev, "no map public\n"); |
| 559 | /* TODO: unwind previously successful mappings */ |
| 560 | goto out_free_iova; |
| 561 | } |
Mitchel Humpherys | 9de66db | 2016-06-07 11:09:44 -0700 | [diff] [blame] | 562 | fast_dmac_clean_range(mapping, ptep, ptep + nptes); |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 563 | iova_iter += miter.length; |
| 564 | } |
| 565 | sg_miter_stop(&miter); |
| 566 | spin_unlock_irqrestore(&mapping->lock, flags); |
| 567 | |
| 568 | addr = dma_common_pages_remap(pages, size, VM_USERMAP, remap_prot, |
| 569 | __builtin_return_address(0)); |
| 570 | if (!addr) { |
| 571 | dev_err(dev, "no common pages\n"); |
| 572 | goto out_unmap; |
| 573 | } |
| 574 | |
| 575 | *handle = dma_addr; |
| 576 | sg_free_table(&sgt); |
| 577 | return addr; |
| 578 | |
| 579 | out_unmap: |
| 580 | /* need to take the lock again for page tables and iova */ |
| 581 | spin_lock_irqsave(&mapping->lock, flags); |
| 582 | ptep = iopte_pmd_offset(mapping->pgtbl_pmds, dma_addr); |
| 583 | av8l_fast_unmap_public(ptep, size); |
Mitchel Humpherys | 9de66db | 2016-06-07 11:09:44 -0700 | [diff] [blame] | 584 | fast_dmac_clean_range(mapping, ptep, ptep + count); |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 585 | out_free_iova: |
| 586 | __fast_smmu_free_iova(mapping, dma_addr, size); |
| 587 | spin_unlock_irqrestore(&mapping->lock, flags); |
| 588 | out_free_sg: |
| 589 | sg_free_table(&sgt); |
| 590 | out_free_pages: |
| 591 | __fast_smmu_free_pages(pages, count); |
| 592 | return NULL; |
| 593 | } |
| 594 | |
| 595 | static void fast_smmu_free(struct device *dev, size_t size, |
| 596 | void *vaddr, dma_addr_t dma_handle, |
| 597 | unsigned long attrs) |
| 598 | { |
| 599 | struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast; |
| 600 | struct vm_struct *area; |
| 601 | struct page **pages; |
| 602 | size_t count = ALIGN(size, SZ_4K) >> FAST_PAGE_SHIFT; |
| 603 | av8l_fast_iopte *ptep; |
| 604 | unsigned long flags; |
| 605 | |
| 606 | size = ALIGN(size, SZ_4K); |
| 607 | |
| 608 | area = find_vm_area(vaddr); |
| 609 | if (WARN_ON_ONCE(!area)) |
| 610 | return; |
| 611 | |
| 612 | pages = area->pages; |
| 613 | dma_common_free_remap(vaddr, size, VM_USERMAP, false); |
| 614 | ptep = iopte_pmd_offset(mapping->pgtbl_pmds, dma_handle); |
| 615 | spin_lock_irqsave(&mapping->lock, flags); |
| 616 | av8l_fast_unmap_public(ptep, size); |
Mitchel Humpherys | 9de66db | 2016-06-07 11:09:44 -0700 | [diff] [blame] | 617 | fast_dmac_clean_range(mapping, ptep, ptep + count); |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 618 | __fast_smmu_free_iova(mapping, dma_handle, size); |
| 619 | spin_unlock_irqrestore(&mapping->lock, flags); |
| 620 | __fast_smmu_free_pages(pages, count); |
| 621 | } |
| 622 | |
Patrick Daly | 7bcb546 | 2016-08-03 17:27:36 -0700 | [diff] [blame] | 623 | static int fast_smmu_mmap_attrs(struct device *dev, struct vm_area_struct *vma, |
| 624 | void *cpu_addr, dma_addr_t dma_addr, |
| 625 | size_t size, unsigned long attrs) |
| 626 | { |
| 627 | struct vm_struct *area; |
| 628 | unsigned long uaddr = vma->vm_start; |
| 629 | struct page **pages; |
| 630 | int i, nr_pages, ret = 0; |
Liam Mark | 83a9f86e | 2017-02-08 09:37:17 -0800 | [diff] [blame^] | 631 | bool coherent = is_dma_coherent(dev, attrs); |
Patrick Daly | 7bcb546 | 2016-08-03 17:27:36 -0700 | [diff] [blame] | 632 | |
Mitchel Humpherys | 425d03d | 2016-06-23 13:25:12 -0700 | [diff] [blame] | 633 | vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot, |
| 634 | coherent); |
Patrick Daly | 7bcb546 | 2016-08-03 17:27:36 -0700 | [diff] [blame] | 635 | area = find_vm_area(cpu_addr); |
| 636 | if (!area) |
| 637 | return -EINVAL; |
| 638 | |
| 639 | pages = area->pages; |
| 640 | nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT; |
| 641 | for (i = vma->vm_pgoff; i < nr_pages && uaddr < vma->vm_end; i++) { |
| 642 | ret = vm_insert_page(vma, uaddr, pages[i]); |
| 643 | if (ret) |
| 644 | break; |
| 645 | uaddr += PAGE_SIZE; |
| 646 | } |
| 647 | |
| 648 | return ret; |
| 649 | } |
| 650 | |
Patrick Daly | 9c79f38 | 2017-06-12 18:15:25 -0700 | [diff] [blame] | 651 | static int fast_smmu_get_sgtable(struct device *dev, struct sg_table *sgt, |
| 652 | void *cpu_addr, dma_addr_t dma_addr, |
| 653 | size_t size, unsigned long attrs) |
| 654 | { |
| 655 | unsigned int n_pages = PAGE_ALIGN(size) >> PAGE_SHIFT; |
| 656 | struct vm_struct *area; |
| 657 | |
| 658 | area = find_vm_area(cpu_addr); |
| 659 | if (!area || !area->pages) |
| 660 | return -EINVAL; |
| 661 | |
| 662 | return sg_alloc_table_from_pages(sgt, area->pages, n_pages, 0, size, |
| 663 | GFP_KERNEL); |
| 664 | } |
| 665 | |
Patrick Daly | 199fa67 | 2017-05-04 15:30:16 -0700 | [diff] [blame] | 666 | static dma_addr_t fast_smmu_dma_map_resource( |
| 667 | struct device *dev, phys_addr_t phys_addr, |
| 668 | size_t size, enum dma_data_direction dir, |
| 669 | unsigned long attrs) |
| 670 | { |
| 671 | struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast; |
| 672 | size_t offset = phys_addr & ~FAST_PAGE_MASK; |
| 673 | size_t len = round_up(size + offset, FAST_PAGE_SIZE); |
| 674 | dma_addr_t dma_addr; |
| 675 | int prot; |
| 676 | unsigned long flags; |
| 677 | |
| 678 | spin_lock_irqsave(&mapping->lock, flags); |
| 679 | dma_addr = __fast_smmu_alloc_iova(mapping, attrs, len); |
| 680 | spin_unlock_irqrestore(&mapping->lock, flags); |
| 681 | |
| 682 | if (dma_addr == DMA_ERROR_CODE) |
| 683 | return dma_addr; |
| 684 | |
| 685 | prot = __fast_dma_direction_to_prot(dir); |
| 686 | prot |= IOMMU_MMIO; |
| 687 | |
| 688 | if (iommu_map(mapping->domain, dma_addr, phys_addr - offset, |
| 689 | len, prot)) { |
| 690 | spin_lock_irqsave(&mapping->lock, flags); |
| 691 | __fast_smmu_free_iova(mapping, dma_addr, len); |
| 692 | spin_unlock_irqrestore(&mapping->lock, flags); |
| 693 | return DMA_ERROR_CODE; |
| 694 | } |
| 695 | return dma_addr + offset; |
| 696 | } |
| 697 | |
| 698 | static void fast_smmu_dma_unmap_resource( |
| 699 | struct device *dev, dma_addr_t addr, |
| 700 | size_t size, enum dma_data_direction dir, |
| 701 | unsigned long attrs) |
| 702 | { |
| 703 | struct dma_fast_smmu_mapping *mapping = dev->archdata.mapping->fast; |
| 704 | size_t offset = addr & ~FAST_PAGE_MASK; |
| 705 | size_t len = round_up(size + offset, FAST_PAGE_SIZE); |
| 706 | unsigned long flags; |
| 707 | |
| 708 | iommu_unmap(mapping->domain, addr - offset, len); |
| 709 | spin_lock_irqsave(&mapping->lock, flags); |
| 710 | __fast_smmu_free_iova(mapping, addr, len); |
| 711 | spin_unlock_irqrestore(&mapping->lock, flags); |
| 712 | } |
| 713 | |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 714 | static int fast_smmu_mapping_error(struct device *dev, |
| 715 | dma_addr_t dma_addr) |
| 716 | { |
| 717 | return dma_addr == DMA_ERROR_CODE; |
| 718 | } |
| 719 | |
Mitchel Humpherys | 5c704e0 | 2015-12-21 15:06:34 -0800 | [diff] [blame] | 720 | static void __fast_smmu_mapped_over_stale(struct dma_fast_smmu_mapping *fast, |
| 721 | void *data) |
| 722 | { |
| 723 | av8l_fast_iopte *ptep = data; |
| 724 | dma_addr_t iova; |
| 725 | unsigned long bitmap_idx; |
| 726 | |
| 727 | bitmap_idx = (unsigned long)(ptep - fast->pgtbl_pmds); |
| 728 | iova = bitmap_idx << FAST_PAGE_SHIFT; |
| 729 | dev_err(fast->dev, "Mapped over stale tlb at %pa\n", &iova); |
| 730 | dev_err(fast->dev, "bitmap (failure at idx %lu):\n", bitmap_idx); |
| 731 | dev_err(fast->dev, "ptep: %p pmds: %p diff: %lu\n", ptep, |
Charan Teja Reddy | 29f6140 | 2017-02-09 20:44:29 +0530 | [diff] [blame] | 732 | fast->pgtbl_pmds, bitmap_idx); |
Mitchel Humpherys | 5c704e0 | 2015-12-21 15:06:34 -0800 | [diff] [blame] | 733 | print_hex_dump(KERN_ERR, "bmap: ", DUMP_PREFIX_ADDRESS, |
| 734 | 32, 8, fast->bitmap, fast->bitmap_size, false); |
| 735 | } |
| 736 | |
| 737 | static int fast_smmu_notify(struct notifier_block *self, |
| 738 | unsigned long action, void *data) |
| 739 | { |
| 740 | struct dma_fast_smmu_mapping *fast = container_of( |
| 741 | self, struct dma_fast_smmu_mapping, notifier); |
| 742 | |
| 743 | switch (action) { |
| 744 | case MAPPED_OVER_STALE_TLB: |
| 745 | __fast_smmu_mapped_over_stale(fast, data); |
| 746 | return NOTIFY_OK; |
| 747 | default: |
| 748 | WARN(1, "Unhandled notifier action"); |
| 749 | return NOTIFY_DONE; |
| 750 | } |
| 751 | } |
| 752 | |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 753 | static const struct dma_map_ops fast_smmu_dma_ops = { |
| 754 | .alloc = fast_smmu_alloc, |
| 755 | .free = fast_smmu_free, |
Patrick Daly | 7bcb546 | 2016-08-03 17:27:36 -0700 | [diff] [blame] | 756 | .mmap = fast_smmu_mmap_attrs, |
Patrick Daly | 9c79f38 | 2017-06-12 18:15:25 -0700 | [diff] [blame] | 757 | .get_sgtable = fast_smmu_get_sgtable, |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 758 | .map_page = fast_smmu_map_page, |
| 759 | .unmap_page = fast_smmu_unmap_page, |
Liam Mark | 78d7fb5 | 2016-12-01 13:05:31 -0800 | [diff] [blame] | 760 | .sync_single_for_cpu = fast_smmu_sync_single_for_cpu, |
| 761 | .sync_single_for_device = fast_smmu_sync_single_for_device, |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 762 | .map_sg = fast_smmu_map_sg, |
| 763 | .unmap_sg = fast_smmu_unmap_sg, |
Liam Mark | 78d7fb5 | 2016-12-01 13:05:31 -0800 | [diff] [blame] | 764 | .sync_sg_for_cpu = fast_smmu_sync_sg_for_cpu, |
| 765 | .sync_sg_for_device = fast_smmu_sync_sg_for_device, |
Patrick Daly | 199fa67 | 2017-05-04 15:30:16 -0700 | [diff] [blame] | 766 | .map_resource = fast_smmu_dma_map_resource, |
| 767 | .unmap_resource = fast_smmu_dma_unmap_resource, |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 768 | .mapping_error = fast_smmu_mapping_error, |
| 769 | }; |
| 770 | |
| 771 | /** |
| 772 | * __fast_smmu_create_mapping_sized |
| 773 | * @base: bottom of the VA range |
| 774 | * @size: size of the VA range in bytes |
| 775 | * |
| 776 | * Creates a mapping structure which holds information about used/unused IO |
| 777 | * address ranges, which is required to perform mapping with IOMMU aware |
| 778 | * functions. The only VA range supported is [0, 4GB). |
| 779 | * |
| 780 | * The client device need to be attached to the mapping with |
| 781 | * fast_smmu_attach_device function. |
| 782 | */ |
| 783 | static struct dma_fast_smmu_mapping *__fast_smmu_create_mapping_sized( |
Charan Teja Reddy | 29f6140 | 2017-02-09 20:44:29 +0530 | [diff] [blame] | 784 | dma_addr_t base, u64 size) |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 785 | { |
| 786 | struct dma_fast_smmu_mapping *fast; |
| 787 | |
| 788 | fast = kzalloc(sizeof(struct dma_fast_smmu_mapping), GFP_KERNEL); |
| 789 | if (!fast) |
| 790 | goto err; |
| 791 | |
| 792 | fast->base = base; |
| 793 | fast->size = size; |
| 794 | fast->num_4k_pages = size >> FAST_PAGE_SHIFT; |
| 795 | fast->bitmap_size = BITS_TO_LONGS(fast->num_4k_pages) * sizeof(long); |
| 796 | |
Liam Mark | 4d4fbba | 2017-02-08 10:30:49 -0800 | [diff] [blame] | 797 | fast->bitmap = kzalloc(fast->bitmap_size, GFP_KERNEL | __GFP_NOWARN | |
| 798 | __GFP_NORETRY); |
| 799 | if (!fast->bitmap) |
| 800 | fast->bitmap = vzalloc(fast->bitmap_size); |
| 801 | |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 802 | if (!fast->bitmap) |
| 803 | goto err2; |
| 804 | |
| 805 | spin_lock_init(&fast->lock); |
| 806 | |
| 807 | return fast; |
| 808 | err2: |
| 809 | kfree(fast); |
| 810 | err: |
| 811 | return ERR_PTR(-ENOMEM); |
| 812 | } |
| 813 | |
Patrick Daly | bc8b1cb | 2017-05-04 17:10:10 -0700 | [diff] [blame] | 814 | /* |
| 815 | * Based off of similar code from dma-iommu.c, but modified to use a different |
| 816 | * iova allocator |
| 817 | */ |
| 818 | static void fast_smmu_reserve_pci_windows(struct device *dev, |
| 819 | struct dma_fast_smmu_mapping *mapping) |
| 820 | { |
| 821 | struct pci_host_bridge *bridge; |
| 822 | struct resource_entry *window; |
| 823 | phys_addr_t start, end; |
| 824 | struct pci_dev *pci_dev; |
| 825 | unsigned long flags; |
| 826 | |
| 827 | if (!dev_is_pci(dev)) |
| 828 | return; |
| 829 | |
| 830 | pci_dev = to_pci_dev(dev); |
| 831 | bridge = pci_find_host_bridge(pci_dev->bus); |
| 832 | |
| 833 | spin_lock_irqsave(&mapping->lock, flags); |
| 834 | resource_list_for_each_entry(window, &bridge->windows) { |
| 835 | if (resource_type(window->res) != IORESOURCE_MEM && |
| 836 | resource_type(window->res) != IORESOURCE_IO) |
| 837 | continue; |
| 838 | |
| 839 | start = round_down(window->res->start - window->offset, |
| 840 | FAST_PAGE_SIZE); |
| 841 | end = round_up(window->res->end - window->offset, |
| 842 | FAST_PAGE_SIZE); |
| 843 | start = max_t(unsigned long, mapping->base, start); |
| 844 | end = min_t(unsigned long, mapping->base + mapping->size, end); |
| 845 | if (start >= end) |
| 846 | continue; |
| 847 | |
| 848 | dev_dbg(dev, "iova allocator reserved 0x%pa-0x%pa\n", |
| 849 | &start, &end); |
| 850 | |
| 851 | start = (start - mapping->base) >> FAST_PAGE_SHIFT; |
| 852 | end = (end - mapping->base) >> FAST_PAGE_SHIFT; |
| 853 | bitmap_set(mapping->bitmap, start, end - start); |
| 854 | } |
| 855 | spin_unlock_irqrestore(&mapping->lock, flags); |
| 856 | } |
| 857 | |
| 858 | |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 859 | /** |
| 860 | * fast_smmu_attach_device |
| 861 | * @dev: valid struct device pointer |
| 862 | * @mapping: io address space mapping structure (returned from |
| 863 | * fast_smmu_create_mapping) |
| 864 | * |
| 865 | * Attaches specified io address space mapping to the provided device, |
| 866 | * this replaces the dma operations (dma_map_ops pointer) with the |
| 867 | * IOMMU aware version. More than one client might be attached to |
| 868 | * the same io address space mapping. |
| 869 | */ |
| 870 | int fast_smmu_attach_device(struct device *dev, |
| 871 | struct dma_iommu_mapping *mapping) |
| 872 | { |
| 873 | int atomic_domain = 1; |
| 874 | struct iommu_domain *domain = mapping->domain; |
Mitchel Humpherys | ad9df1f | 2016-05-27 14:58:31 -0700 | [diff] [blame] | 875 | struct iommu_group *group; |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 876 | struct iommu_pgtbl_info info; |
Charan Teja Reddy | 29f6140 | 2017-02-09 20:44:29 +0530 | [diff] [blame] | 877 | u64 size = (u64)mapping->bits << PAGE_SHIFT; |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 878 | |
| 879 | if (mapping->base + size > (SZ_1G * 4ULL)) |
| 880 | return -EINVAL; |
| 881 | |
| 882 | if (iommu_domain_set_attr(domain, DOMAIN_ATTR_ATOMIC, |
| 883 | &atomic_domain)) |
| 884 | return -EINVAL; |
| 885 | |
| 886 | mapping->fast = __fast_smmu_create_mapping_sized(mapping->base, size); |
| 887 | if (IS_ERR(mapping->fast)) |
| 888 | return -ENOMEM; |
| 889 | mapping->fast->domain = domain; |
| 890 | mapping->fast->dev = dev; |
| 891 | |
Patrick Daly | bc8b1cb | 2017-05-04 17:10:10 -0700 | [diff] [blame] | 892 | fast_smmu_reserve_pci_windows(dev, mapping->fast); |
| 893 | |
Mitchel Humpherys | ad9df1f | 2016-05-27 14:58:31 -0700 | [diff] [blame] | 894 | group = dev->iommu_group; |
| 895 | if (!group) { |
| 896 | dev_err(dev, "No iommu associated with device\n"); |
| 897 | return -ENODEV; |
| 898 | } |
| 899 | |
| 900 | if (iommu_get_domain_for_dev(dev)) { |
| 901 | dev_err(dev, "Device already attached to other iommu_domain\n"); |
| 902 | return -EINVAL; |
| 903 | } |
| 904 | |
| 905 | if (iommu_attach_group(mapping->domain, group)) |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 906 | return -EINVAL; |
| 907 | |
| 908 | if (iommu_domain_get_attr(domain, DOMAIN_ATTR_PGTBL_INFO, |
| 909 | &info)) { |
| 910 | dev_err(dev, "Couldn't get page table info\n"); |
| 911 | fast_smmu_detach_device(dev, mapping); |
| 912 | return -EINVAL; |
| 913 | } |
| 914 | mapping->fast->pgtbl_pmds = info.pmds; |
| 915 | |
Mitchel Humpherys | 9de66db | 2016-06-07 11:09:44 -0700 | [diff] [blame] | 916 | if (iommu_domain_get_attr(domain, DOMAIN_ATTR_PAGE_TABLE_IS_COHERENT, |
| 917 | &mapping->fast->is_smmu_pt_coherent)) |
| 918 | return -EINVAL; |
| 919 | |
Mitchel Humpherys | 5c704e0 | 2015-12-21 15:06:34 -0800 | [diff] [blame] | 920 | mapping->fast->notifier.notifier_call = fast_smmu_notify; |
| 921 | av8l_register_notify(&mapping->fast->notifier); |
| 922 | |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 923 | dev->archdata.mapping = mapping; |
| 924 | set_dma_ops(dev, &fast_smmu_dma_ops); |
| 925 | |
| 926 | return 0; |
| 927 | } |
| 928 | EXPORT_SYMBOL(fast_smmu_attach_device); |
| 929 | |
| 930 | /** |
| 931 | * fast_smmu_detach_device |
| 932 | * @dev: valid struct device pointer |
| 933 | * |
| 934 | * Detaches the provided device from a previously attached map. |
| 935 | * This voids the dma operations (dma_map_ops pointer) |
| 936 | */ |
| 937 | void fast_smmu_detach_device(struct device *dev, |
| 938 | struct dma_iommu_mapping *mapping) |
| 939 | { |
Mitchel Humpherys | ad9df1f | 2016-05-27 14:58:31 -0700 | [diff] [blame] | 940 | iommu_detach_group(mapping->domain, dev->iommu_group); |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 941 | dev->archdata.mapping = NULL; |
| 942 | set_dma_ops(dev, NULL); |
| 943 | |
Liam Mark | 4d4fbba | 2017-02-08 10:30:49 -0800 | [diff] [blame] | 944 | kvfree(mapping->fast->bitmap); |
Mitchel Humpherys | 0e43f0a | 2015-10-08 15:03:09 -0700 | [diff] [blame] | 945 | kfree(mapping->fast); |
| 946 | } |
| 947 | EXPORT_SYMBOL(fast_smmu_detach_device); |