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