Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 1 | /* |
| 2 | * A fairly generic DMA-API to IOMMU-API glue layer. |
| 3 | * |
| 4 | * Copyright (C) 2014-2015 ARM Ltd. |
| 5 | * |
| 6 | * based in part on arch/arm/mm/dma-mapping.c: |
| 7 | * Copyright (C) 2000-2004 Russell King |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/device.h> |
| 23 | #include <linux/dma-iommu.h> |
Robin Murphy | 5b11e9c | 2015-12-18 17:01:46 +0000 | [diff] [blame] | 24 | #include <linux/gfp.h> |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 25 | #include <linux/huge_mm.h> |
| 26 | #include <linux/iommu.h> |
| 27 | #include <linux/iova.h> |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 28 | #include <linux/irq.h> |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 29 | #include <linux/mm.h> |
Robin Murphy | fade1ec | 2016-09-12 17:14:00 +0100 | [diff] [blame] | 30 | #include <linux/pci.h> |
Robin Murphy | 5b11e9c | 2015-12-18 17:01:46 +0000 | [diff] [blame] | 31 | #include <linux/scatterlist.h> |
| 32 | #include <linux/vmalloc.h> |
Patrick Daly | 2330148 | 2017-10-12 16:18:25 -0700 | [diff] [blame] | 33 | #include <linux/arm-smmu-errata.h> |
| 34 | #include <soc/qcom/secure_buffer.h> |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 35 | |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 36 | struct iommu_dma_msi_page { |
| 37 | struct list_head list; |
| 38 | dma_addr_t iova; |
| 39 | phys_addr_t phys; |
| 40 | }; |
| 41 | |
| 42 | struct iommu_dma_cookie { |
| 43 | struct iova_domain iovad; |
| 44 | struct list_head msi_page_list; |
| 45 | spinlock_t msi_lock; |
Patrick Daly | 2330148 | 2017-10-12 16:18:25 -0700 | [diff] [blame] | 46 | u32 min_iova_align; |
| 47 | struct page *guard_page; |
Prakash Gupta | c2e909a | 2018-03-29 11:23:06 +0530 | [diff] [blame] | 48 | u32 force_guard_page_len; |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | static inline struct iova_domain *cookie_iovad(struct iommu_domain *domain) |
| 52 | { |
| 53 | return &((struct iommu_dma_cookie *)domain->iova_cookie)->iovad; |
| 54 | } |
| 55 | |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 56 | int iommu_dma_init(void) |
| 57 | { |
| 58 | return iova_cache_get(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * iommu_get_dma_cookie - Acquire DMA-API resources for a domain |
| 63 | * @domain: IOMMU domain to prepare for DMA-API usage |
| 64 | * |
| 65 | * IOMMU drivers should normally call this from their domain_alloc |
| 66 | * callback when domain->type == IOMMU_DOMAIN_DMA. |
| 67 | */ |
| 68 | int iommu_get_dma_cookie(struct iommu_domain *domain) |
| 69 | { |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 70 | struct iommu_dma_cookie *cookie; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 71 | |
| 72 | if (domain->iova_cookie) |
| 73 | return -EEXIST; |
| 74 | |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 75 | cookie = kzalloc(sizeof(*cookie), GFP_KERNEL); |
| 76 | if (!cookie) |
| 77 | return -ENOMEM; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 78 | |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 79 | spin_lock_init(&cookie->msi_lock); |
| 80 | INIT_LIST_HEAD(&cookie->msi_page_list); |
| 81 | domain->iova_cookie = cookie; |
| 82 | return 0; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 83 | } |
| 84 | EXPORT_SYMBOL(iommu_get_dma_cookie); |
| 85 | |
| 86 | /** |
| 87 | * iommu_put_dma_cookie - Release a domain's DMA mapping resources |
| 88 | * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie() |
| 89 | * |
| 90 | * IOMMU drivers should normally call this from their domain_free callback. |
| 91 | */ |
| 92 | void iommu_put_dma_cookie(struct iommu_domain *domain) |
| 93 | { |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 94 | struct iommu_dma_cookie *cookie = domain->iova_cookie; |
| 95 | struct iommu_dma_msi_page *msi, *tmp; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 96 | |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 97 | if (!cookie) |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 98 | return; |
| 99 | |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 100 | if (cookie->iovad.granule) |
| 101 | put_iova_domain(&cookie->iovad); |
| 102 | |
| 103 | list_for_each_entry_safe(msi, tmp, &cookie->msi_page_list, list) { |
| 104 | list_del(&msi->list); |
| 105 | kfree(msi); |
| 106 | } |
| 107 | kfree(cookie); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 108 | domain->iova_cookie = NULL; |
| 109 | } |
| 110 | EXPORT_SYMBOL(iommu_put_dma_cookie); |
| 111 | |
Robin Murphy | fade1ec | 2016-09-12 17:14:00 +0100 | [diff] [blame] | 112 | static void iova_reserve_pci_windows(struct pci_dev *dev, |
| 113 | struct iova_domain *iovad) |
| 114 | { |
| 115 | struct pci_host_bridge *bridge = pci_find_host_bridge(dev->bus); |
| 116 | struct resource_entry *window; |
| 117 | unsigned long lo, hi; |
| 118 | |
| 119 | resource_list_for_each_entry(window, &bridge->windows) { |
Robin Murphy | f0c31c6 | 2017-03-16 17:00:17 +0000 | [diff] [blame] | 120 | if (resource_type(window->res) != IORESOURCE_MEM) |
Robin Murphy | fade1ec | 2016-09-12 17:14:00 +0100 | [diff] [blame] | 121 | continue; |
| 122 | |
| 123 | lo = iova_pfn(iovad, window->res->start - window->offset); |
| 124 | hi = iova_pfn(iovad, window->res->end - window->offset); |
| 125 | reserve_iova(iovad, lo, hi); |
| 126 | } |
| 127 | } |
| 128 | |
Patrick Daly | 2330148 | 2017-10-12 16:18:25 -0700 | [diff] [blame] | 129 | static int iommu_dma_arm_smmu_errata_init(struct iommu_domain *domain) |
| 130 | { |
| 131 | struct iommu_dma_cookie *cookie = domain->iova_cookie; |
| 132 | int vmid = VMID_HLOS; |
| 133 | int min_iova_align = 0; |
Prakash Gupta | c2e909a | 2018-03-29 11:23:06 +0530 | [diff] [blame] | 134 | int force_iova_guard_page = 0; |
| 135 | |
Patrick Daly | 2330148 | 2017-10-12 16:18:25 -0700 | [diff] [blame] | 136 | |
| 137 | iommu_domain_get_attr(domain, |
Patrick Daly | 83174c1 | 2017-10-26 12:31:15 -0700 | [diff] [blame] | 138 | DOMAIN_ATTR_MMU500_ERRATA_MIN_ALIGN, |
Patrick Daly | 2330148 | 2017-10-12 16:18:25 -0700 | [diff] [blame] | 139 | &min_iova_align); |
| 140 | iommu_domain_get_attr(domain, DOMAIN_ATTR_SECURE_VMID, &vmid); |
Prakash Gupta | c2e909a | 2018-03-29 11:23:06 +0530 | [diff] [blame] | 141 | iommu_domain_get_attr(domain, |
| 142 | DOMAIN_ATTR_FORCE_IOVA_GUARD_PAGE, |
| 143 | &force_iova_guard_page); |
| 144 | |
Patrick Daly | 2330148 | 2017-10-12 16:18:25 -0700 | [diff] [blame] | 145 | if (vmid >= VMID_LAST || vmid < 0) |
| 146 | vmid = VMID_HLOS; |
| 147 | |
Prakash Gupta | c2e909a | 2018-03-29 11:23:06 +0530 | [diff] [blame] | 148 | cookie->min_iova_align = (min_iova_align) ? ARM_SMMU_MIN_IOVA_ALIGN : |
| 149 | PAGE_SIZE; |
| 150 | |
| 151 | if (force_iova_guard_page) |
| 152 | cookie->force_guard_page_len = PAGE_SIZE; |
| 153 | |
| 154 | cookie->guard_page = |
| 155 | arm_smmu_errata_get_guard_page(vmid); |
| 156 | if (!cookie->guard_page) |
| 157 | return -ENOMEM; |
| 158 | |
Patrick Daly | 2330148 | 2017-10-12 16:18:25 -0700 | [diff] [blame] | 159 | return 0; |
| 160 | } |
| 161 | |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 162 | /** |
| 163 | * iommu_dma_init_domain - Initialise a DMA mapping domain |
| 164 | * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie() |
| 165 | * @base: IOVA at which the mappable address space starts |
| 166 | * @size: Size of IOVA space |
Robin Murphy | fade1ec | 2016-09-12 17:14:00 +0100 | [diff] [blame] | 167 | * @dev: Device the domain is being initialised for |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 168 | * |
| 169 | * @base and @size should be exact multiples of IOMMU page granularity to |
| 170 | * avoid rounding surprises. If necessary, we reserve the page at address 0 |
| 171 | * to ensure it is an invalid IOVA. It is safe to reinitialise a domain, but |
| 172 | * any change which could make prior IOVAs invalid will fail. |
| 173 | */ |
Robin Murphy | fade1ec | 2016-09-12 17:14:00 +0100 | [diff] [blame] | 174 | int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base, |
| 175 | u64 size, struct device *dev) |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 176 | { |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 177 | struct iova_domain *iovad = cookie_iovad(domain); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 178 | unsigned long order, base_pfn, end_pfn; |
| 179 | |
| 180 | if (!iovad) |
| 181 | return -ENODEV; |
| 182 | |
Patrick Daly | 2330148 | 2017-10-12 16:18:25 -0700 | [diff] [blame] | 183 | if (iommu_dma_arm_smmu_errata_init(domain)) |
| 184 | return -ENODEV; |
| 185 | |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 186 | /* Use the smallest supported page size for IOVA granularity */ |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 187 | order = __ffs(domain->pgsize_bitmap); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 188 | base_pfn = max_t(unsigned long, 1, base >> order); |
| 189 | end_pfn = (base + size - 1) >> order; |
| 190 | |
| 191 | /* Check the domain allows at least some access to the device... */ |
| 192 | if (domain->geometry.force_aperture) { |
| 193 | if (base > domain->geometry.aperture_end || |
| 194 | base + size <= domain->geometry.aperture_start) { |
| 195 | pr_warn("specified DMA range outside IOMMU capability\n"); |
| 196 | return -EFAULT; |
| 197 | } |
| 198 | /* ...then finally give it a kicking to make sure it fits */ |
| 199 | base_pfn = max_t(unsigned long, base_pfn, |
| 200 | domain->geometry.aperture_start >> order); |
| 201 | end_pfn = min_t(unsigned long, end_pfn, |
| 202 | domain->geometry.aperture_end >> order); |
| 203 | } |
| 204 | |
| 205 | /* All we can safely do with an existing domain is enlarge it */ |
| 206 | if (iovad->start_pfn) { |
| 207 | if (1UL << order != iovad->granule || |
| 208 | base_pfn != iovad->start_pfn || |
| 209 | end_pfn < iovad->dma_32bit_pfn) { |
| 210 | pr_warn("Incompatible range for DMA domain\n"); |
| 211 | return -EFAULT; |
| 212 | } |
| 213 | iovad->dma_32bit_pfn = end_pfn; |
| 214 | } else { |
| 215 | init_iova_domain(iovad, 1UL << order, base_pfn, end_pfn); |
Robin Murphy | fade1ec | 2016-09-12 17:14:00 +0100 | [diff] [blame] | 216 | if (dev && dev_is_pci(dev)) |
| 217 | iova_reserve_pci_windows(to_pci_dev(dev), iovad); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 218 | } |
| 219 | return 0; |
| 220 | } |
| 221 | EXPORT_SYMBOL(iommu_dma_init_domain); |
| 222 | |
| 223 | /** |
| 224 | * dma_direction_to_prot - Translate DMA API directions to IOMMU API page flags |
| 225 | * @dir: Direction of DMA transfer |
| 226 | * @coherent: Is the DMA master cache-coherent? |
| 227 | * |
| 228 | * Return: corresponding IOMMU API page protection flags |
| 229 | */ |
| 230 | int dma_direction_to_prot(enum dma_data_direction dir, bool coherent) |
| 231 | { |
| 232 | int prot = coherent ? IOMMU_CACHE : 0; |
| 233 | |
| 234 | switch (dir) { |
| 235 | case DMA_BIDIRECTIONAL: |
| 236 | return prot | IOMMU_READ | IOMMU_WRITE; |
| 237 | case DMA_TO_DEVICE: |
| 238 | return prot | IOMMU_READ; |
| 239 | case DMA_FROM_DEVICE: |
| 240 | return prot | IOMMU_WRITE; |
| 241 | default: |
| 242 | return 0; |
| 243 | } |
| 244 | } |
| 245 | |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 246 | static dma_addr_t iommu_dma_alloc_iova(struct iommu_domain *domain, |
| 247 | size_t size, dma_addr_t dma_limit, struct device *dev) |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 248 | { |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 249 | struct iova_domain *iovad = cookie_iovad(domain); |
Patrick Daly | 2330148 | 2017-10-12 16:18:25 -0700 | [diff] [blame] | 250 | struct iommu_dma_cookie *cookie = domain->iova_cookie; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 251 | unsigned long shift = iova_shift(iovad); |
Patrick Daly | 2330148 | 2017-10-12 16:18:25 -0700 | [diff] [blame] | 252 | unsigned long iova_len; |
Robin Murphy | e53859e | 2017-03-31 15:46:07 +0100 | [diff] [blame] | 253 | unsigned long iova = 0; |
Patrick Daly | 67e617b | 2017-07-20 21:07:08 -0700 | [diff] [blame] | 254 | dma_addr_t limit; |
Patrick Daly | 2330148 | 2017-10-12 16:18:25 -0700 | [diff] [blame] | 255 | unsigned long guard_len; |
| 256 | dma_addr_t ret_iova; |
Robin Murphy | e53859e | 2017-03-31 15:46:07 +0100 | [diff] [blame] | 257 | |
Patrick Daly | 2330148 | 2017-10-12 16:18:25 -0700 | [diff] [blame] | 258 | if (cookie->min_iova_align) |
Prakash Gupta | c2e909a | 2018-03-29 11:23:06 +0530 | [diff] [blame] | 259 | guard_len = ALIGN(size + cookie->force_guard_page_len, |
| 260 | cookie->min_iova_align) - size; |
Patrick Daly | 2330148 | 2017-10-12 16:18:25 -0700 | [diff] [blame] | 261 | else |
| 262 | guard_len = 0; |
| 263 | iova_len = (size + guard_len) >> shift; |
Robin Murphy | e53859e | 2017-03-31 15:46:07 +0100 | [diff] [blame] | 264 | /* |
| 265 | * Freeing non-power-of-two-sized allocations back into the IOVA caches |
| 266 | * will come back to bite us badly, so we have to waste a bit of space |
| 267 | * rounding up anything cacheable to make sure that can't happen. The |
| 268 | * order of the unadjusted size will still match upon freeing. |
| 269 | */ |
| 270 | if (iova_len < (1 << (IOVA_RANGE_CACHE_MAX_SIZE - 1))) |
| 271 | iova_len = roundup_pow_of_two(iova_len); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 272 | |
Robin Murphy | c987ff0 | 2016-08-09 17:31:35 +0100 | [diff] [blame] | 273 | if (domain->geometry.force_aperture) |
| 274 | dma_limit = min(dma_limit, domain->geometry.aperture_end); |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 275 | |
Patrick Daly | 67e617b | 2017-07-20 21:07:08 -0700 | [diff] [blame] | 276 | /* |
| 277 | * Ensure iova is within range specified in iommu_dma_init_domain(). |
| 278 | * This also prevents unnecessary work iterating through the entire |
| 279 | * rb_tree. |
| 280 | */ |
| 281 | limit = min_t(dma_addr_t, dma_limit >> shift, iovad->dma_32bit_pfn); |
| 282 | iova = alloc_iova_fast(iovad, iova_len, limit); |
Robin Murphy | e53859e | 2017-03-31 15:46:07 +0100 | [diff] [blame] | 283 | |
Patrick Daly | 2330148 | 2017-10-12 16:18:25 -0700 | [diff] [blame] | 284 | ret_iova = (dma_addr_t)iova << shift; |
| 285 | |
| 286 | if (guard_len && |
| 287 | iommu_map(domain, ret_iova + size, |
| 288 | page_to_phys(cookie->guard_page), |
| 289 | guard_len, ARM_SMMU_GUARD_PROT)) { |
| 290 | |
| 291 | free_iova_fast(iovad, iova, iova_len); |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | return ret_iova; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 296 | } |
| 297 | |
Patrick Daly | 2330148 | 2017-10-12 16:18:25 -0700 | [diff] [blame] | 298 | static void iommu_dma_free_iova(struct iommu_domain *domain, |
| 299 | struct iommu_dma_cookie *cookie, |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 300 | dma_addr_t iova, size_t size) |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 301 | { |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 302 | struct iova_domain *iovad = &cookie->iovad; |
Robin Murphy | e53859e | 2017-03-31 15:46:07 +0100 | [diff] [blame] | 303 | unsigned long shift = iova_shift(iovad); |
Patrick Daly | 2330148 | 2017-10-12 16:18:25 -0700 | [diff] [blame] | 304 | unsigned long guard_len; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 305 | |
Prakash Gupta | c2e909a | 2018-03-29 11:23:06 +0530 | [diff] [blame] | 306 | if (cookie->min_iova_align) |
| 307 | guard_len = ALIGN(size + cookie->force_guard_page_len, |
| 308 | cookie->min_iova_align) - size; |
| 309 | else |
Patrick Daly | 2330148 | 2017-10-12 16:18:25 -0700 | [diff] [blame] | 310 | guard_len = 0; |
Prakash Gupta | c2e909a | 2018-03-29 11:23:06 +0530 | [diff] [blame] | 311 | |
| 312 | if (guard_len) |
| 313 | iommu_unmap(domain, iova + size, guard_len); |
Patrick Daly | 2330148 | 2017-10-12 16:18:25 -0700 | [diff] [blame] | 314 | |
| 315 | free_iova_fast(iovad, iova >> shift, (size + guard_len) >> shift); |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static void __iommu_dma_unmap(struct iommu_domain *domain, dma_addr_t dma_addr, |
| 319 | size_t size) |
| 320 | { |
| 321 | struct iova_domain *iovad = cookie_iovad(domain); |
| 322 | size_t iova_off = iova_offset(iovad, dma_addr); |
| 323 | |
| 324 | dma_addr -= iova_off; |
| 325 | size = iova_align(iovad, size + iova_off); |
| 326 | |
| 327 | WARN_ON(iommu_unmap(domain, dma_addr, size) != size); |
Patrick Daly | 2330148 | 2017-10-12 16:18:25 -0700 | [diff] [blame] | 328 | iommu_dma_free_iova(domain, domain->iova_cookie, dma_addr, size); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | static void __iommu_dma_free_pages(struct page **pages, int count) |
| 332 | { |
| 333 | while (count--) |
| 334 | __free_page(pages[count]); |
| 335 | kvfree(pages); |
| 336 | } |
| 337 | |
Robin Murphy | 3b6b7e1 | 2016-04-13 17:29:10 +0100 | [diff] [blame] | 338 | static struct page **__iommu_dma_alloc_pages(unsigned int count, |
| 339 | unsigned long order_mask, gfp_t gfp) |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 340 | { |
| 341 | struct page **pages; |
| 342 | unsigned int i = 0, array_size = count * sizeof(*pages); |
Robin Murphy | 3b6b7e1 | 2016-04-13 17:29:10 +0100 | [diff] [blame] | 343 | |
| 344 | order_mask &= (2U << MAX_ORDER) - 1; |
| 345 | if (!order_mask) |
| 346 | return NULL; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 347 | |
| 348 | if (array_size <= PAGE_SIZE) |
| 349 | pages = kzalloc(array_size, GFP_KERNEL); |
| 350 | else |
| 351 | pages = vzalloc(array_size); |
| 352 | if (!pages) |
| 353 | return NULL; |
| 354 | |
| 355 | /* IOMMU can map any pages, so himem can also be used here */ |
| 356 | gfp |= __GFP_NOWARN | __GFP_HIGHMEM; |
| 357 | |
| 358 | while (count) { |
| 359 | struct page *page = NULL; |
Robin Murphy | 3b6b7e1 | 2016-04-13 17:29:10 +0100 | [diff] [blame] | 360 | unsigned int order_size; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 361 | |
| 362 | /* |
| 363 | * Higher-order allocations are a convenience rather |
| 364 | * than a necessity, hence using __GFP_NORETRY until |
Robin Murphy | 3b6b7e1 | 2016-04-13 17:29:10 +0100 | [diff] [blame] | 365 | * falling back to minimum-order allocations. |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 366 | */ |
Robin Murphy | 3b6b7e1 | 2016-04-13 17:29:10 +0100 | [diff] [blame] | 367 | for (order_mask &= (2U << __fls(count)) - 1; |
| 368 | order_mask; order_mask &= ~order_size) { |
| 369 | unsigned int order = __fls(order_mask); |
| 370 | |
| 371 | order_size = 1U << order; |
Vinayak Menon | 9d37384 | 2019-01-02 20:44:21 +0530 | [diff] [blame^] | 372 | page = alloc_pages(order ? |
| 373 | (gfp | __GFP_NORETRY) & |
| 374 | ~__GFP_RECLAIM : gfp, order); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 375 | if (!page) |
| 376 | continue; |
Robin Murphy | 3b6b7e1 | 2016-04-13 17:29:10 +0100 | [diff] [blame] | 377 | if (!order) |
| 378 | break; |
| 379 | if (!PageCompound(page)) { |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 380 | split_page(page, order); |
| 381 | break; |
Robin Murphy | 3b6b7e1 | 2016-04-13 17:29:10 +0100 | [diff] [blame] | 382 | } else if (!split_huge_page(page)) { |
| 383 | break; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 384 | } |
Robin Murphy | 3b6b7e1 | 2016-04-13 17:29:10 +0100 | [diff] [blame] | 385 | __free_pages(page, order); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 386 | } |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 387 | if (!page) { |
| 388 | __iommu_dma_free_pages(pages, i); |
| 389 | return NULL; |
| 390 | } |
Robin Murphy | 3b6b7e1 | 2016-04-13 17:29:10 +0100 | [diff] [blame] | 391 | count -= order_size; |
| 392 | while (order_size--) |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 393 | pages[i++] = page++; |
| 394 | } |
| 395 | return pages; |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * iommu_dma_free - Free a buffer allocated by iommu_dma_alloc() |
| 400 | * @dev: Device which owns this buffer |
| 401 | * @pages: Array of buffer pages as returned by iommu_dma_alloc() |
| 402 | * @size: Size of buffer in bytes |
| 403 | * @handle: DMA address of buffer |
| 404 | * |
| 405 | * Frees both the pages associated with the buffer, and the array |
| 406 | * describing them |
| 407 | */ |
| 408 | void iommu_dma_free(struct device *dev, struct page **pages, size_t size, |
| 409 | dma_addr_t *handle) |
| 410 | { |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 411 | __iommu_dma_unmap(iommu_get_domain_for_dev(dev), *handle, size); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 412 | __iommu_dma_free_pages(pages, PAGE_ALIGN(size) >> PAGE_SHIFT); |
| 413 | *handle = DMA_ERROR_CODE; |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * iommu_dma_alloc - Allocate and map a buffer contiguous in IOVA space |
| 418 | * @dev: Device to allocate memory for. Must be a real device |
| 419 | * attached to an iommu_dma_domain |
| 420 | * @size: Size of buffer in bytes |
| 421 | * @gfp: Allocation flags |
Robin Murphy | 3b6b7e1 | 2016-04-13 17:29:10 +0100 | [diff] [blame] | 422 | * @attrs: DMA attributes for this allocation |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 423 | * @prot: IOMMU mapping flags |
| 424 | * @handle: Out argument for allocated DMA handle |
| 425 | * @flush_page: Arch callback which must ensure PAGE_SIZE bytes from the |
| 426 | * given VA/PA are visible to the given non-coherent device. |
| 427 | * |
| 428 | * If @size is less than PAGE_SIZE, then a full CPU page will be allocated, |
| 429 | * but an IOMMU which supports smaller pages might not map the whole thing. |
| 430 | * |
| 431 | * Return: Array of struct page pointers describing the buffer, |
| 432 | * or NULL on failure. |
| 433 | */ |
Robin Murphy | 3b6b7e1 | 2016-04-13 17:29:10 +0100 | [diff] [blame] | 434 | struct page **iommu_dma_alloc(struct device *dev, size_t size, gfp_t gfp, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 435 | unsigned long attrs, int prot, dma_addr_t *handle, |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 436 | void (*flush_page)(struct device *, const void *, phys_addr_t)) |
| 437 | { |
| 438 | struct iommu_domain *domain = iommu_get_domain_for_dev(dev); |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 439 | struct iommu_dma_cookie *cookie = domain->iova_cookie; |
| 440 | struct iova_domain *iovad = &cookie->iovad; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 441 | struct page **pages; |
| 442 | struct sg_table sgt; |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 443 | dma_addr_t iova; |
Robin Murphy | 3b6b7e1 | 2016-04-13 17:29:10 +0100 | [diff] [blame] | 444 | unsigned int count, min_size, alloc_sizes = domain->pgsize_bitmap; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 445 | |
| 446 | *handle = DMA_ERROR_CODE; |
| 447 | |
Robin Murphy | 3b6b7e1 | 2016-04-13 17:29:10 +0100 | [diff] [blame] | 448 | min_size = alloc_sizes & -alloc_sizes; |
| 449 | if (min_size < PAGE_SIZE) { |
| 450 | min_size = PAGE_SIZE; |
| 451 | alloc_sizes |= PAGE_SIZE; |
| 452 | } else { |
| 453 | size = ALIGN(size, min_size); |
| 454 | } |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 455 | if (attrs & DMA_ATTR_ALLOC_SINGLE_PAGES) |
Robin Murphy | 3b6b7e1 | 2016-04-13 17:29:10 +0100 | [diff] [blame] | 456 | alloc_sizes = min_size; |
| 457 | |
| 458 | count = PAGE_ALIGN(size) >> PAGE_SHIFT; |
| 459 | pages = __iommu_dma_alloc_pages(count, alloc_sizes >> PAGE_SHIFT, gfp); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 460 | if (!pages) |
| 461 | return NULL; |
| 462 | |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 463 | size = iova_align(iovad, size); |
| 464 | iova = iommu_dma_alloc_iova(domain, size, dev->coherent_dma_mask, dev); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 465 | if (!iova) |
| 466 | goto out_free_pages; |
| 467 | |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 468 | if (sg_alloc_table_from_pages(&sgt, pages, count, 0, size, GFP_KERNEL)) |
| 469 | goto out_free_iova; |
| 470 | |
| 471 | if (!(prot & IOMMU_CACHE)) { |
| 472 | struct sg_mapping_iter miter; |
| 473 | /* |
| 474 | * The CPU-centric flushing implied by SG_MITER_TO_SG isn't |
| 475 | * sufficient here, so skip it by using the "wrong" direction. |
| 476 | */ |
| 477 | sg_miter_start(&miter, sgt.sgl, sgt.orig_nents, SG_MITER_FROM_SG); |
| 478 | while (sg_miter_next(&miter)) |
| 479 | flush_page(dev, miter.addr, page_to_phys(miter.page)); |
| 480 | sg_miter_stop(&miter); |
| 481 | } |
| 482 | |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 483 | if (iommu_map_sg(domain, iova, sgt.sgl, sgt.orig_nents, prot) |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 484 | < size) |
| 485 | goto out_free_sg; |
| 486 | |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 487 | *handle = iova; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 488 | sg_free_table(&sgt); |
| 489 | return pages; |
| 490 | |
| 491 | out_free_sg: |
| 492 | sg_free_table(&sgt); |
| 493 | out_free_iova: |
Patrick Daly | 2330148 | 2017-10-12 16:18:25 -0700 | [diff] [blame] | 494 | iommu_dma_free_iova(domain, cookie, iova, size); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 495 | out_free_pages: |
| 496 | __iommu_dma_free_pages(pages, count); |
| 497 | return NULL; |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * iommu_dma_mmap - Map a buffer into provided user VMA |
| 502 | * @pages: Array representing buffer from iommu_dma_alloc() |
| 503 | * @size: Size of buffer in bytes |
| 504 | * @vma: VMA describing requested userspace mapping |
| 505 | * |
| 506 | * Maps the pages of the buffer in @pages into @vma. The caller is responsible |
| 507 | * for verifying the correct size and protection of @vma beforehand. |
| 508 | */ |
| 509 | |
| 510 | int iommu_dma_mmap(struct page **pages, size_t size, struct vm_area_struct *vma) |
| 511 | { |
| 512 | unsigned long uaddr = vma->vm_start; |
| 513 | unsigned int i, count = PAGE_ALIGN(size) >> PAGE_SHIFT; |
| 514 | int ret = -ENXIO; |
| 515 | |
| 516 | for (i = vma->vm_pgoff; i < count && uaddr < vma->vm_end; i++) { |
| 517 | ret = vm_insert_page(vma, uaddr, pages[i]); |
| 518 | if (ret) |
| 519 | break; |
| 520 | uaddr += PAGE_SIZE; |
| 521 | } |
| 522 | return ret; |
| 523 | } |
| 524 | |
Robin Murphy | c84ed86 | 2016-11-14 12:16:26 +0000 | [diff] [blame] | 525 | static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys, |
| 526 | size_t size, int prot) |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 527 | { |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 528 | struct iommu_domain *domain = iommu_get_domain_for_dev(dev); |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 529 | struct iommu_dma_cookie *cookie = domain->iova_cookie; |
| 530 | struct iova_domain *iovad = &cookie->iovad; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 531 | size_t iova_off = iova_offset(iovad, phys); |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 532 | dma_addr_t iova; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 533 | |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 534 | size = iova_align(iovad, size + iova_off); |
| 535 | iova = iommu_dma_alloc_iova(domain, size, dma_get_mask(dev), dev); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 536 | if (!iova) |
| 537 | return DMA_ERROR_CODE; |
| 538 | |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 539 | if (iommu_map(domain, iova, phys - iova_off, size, prot)) { |
Patrick Daly | 2330148 | 2017-10-12 16:18:25 -0700 | [diff] [blame] | 540 | iommu_dma_free_iova(domain, cookie, iova, size); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 541 | return DMA_ERROR_CODE; |
| 542 | } |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 543 | return iova + iova_off; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 544 | } |
| 545 | |
Robin Murphy | c84ed86 | 2016-11-14 12:16:26 +0000 | [diff] [blame] | 546 | dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page, |
| 547 | unsigned long offset, size_t size, int prot) |
| 548 | { |
| 549 | return __iommu_dma_map(dev, page_to_phys(page) + offset, size, prot); |
| 550 | } |
| 551 | |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 552 | void iommu_dma_unmap_page(struct device *dev, dma_addr_t handle, size_t size, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 553 | enum dma_data_direction dir, unsigned long attrs) |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 554 | { |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 555 | __iommu_dma_unmap(iommu_get_domain_for_dev(dev), handle, size); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /* |
| 559 | * Prepare a successfully-mapped scatterlist to give back to the caller. |
Robin Murphy | 809eac5 | 2016-04-11 12:32:31 +0100 | [diff] [blame] | 560 | * |
| 561 | * At this point the segments are already laid out by iommu_dma_map_sg() to |
| 562 | * avoid individually crossing any boundaries, so we merely need to check a |
| 563 | * segment's start address to avoid concatenating across one. |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 564 | */ |
| 565 | static int __finalise_sg(struct device *dev, struct scatterlist *sg, int nents, |
| 566 | dma_addr_t dma_addr) |
| 567 | { |
Robin Murphy | 809eac5 | 2016-04-11 12:32:31 +0100 | [diff] [blame] | 568 | struct scatterlist *s, *cur = sg; |
| 569 | unsigned long seg_mask = dma_get_seg_boundary(dev); |
| 570 | unsigned int cur_len = 0, max_len = dma_get_max_seg_size(dev); |
| 571 | int i, count = 0; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 572 | |
| 573 | for_each_sg(sg, s, nents, i) { |
Robin Murphy | 809eac5 | 2016-04-11 12:32:31 +0100 | [diff] [blame] | 574 | /* Restore this segment's original unaligned fields first */ |
| 575 | unsigned int s_iova_off = sg_dma_address(s); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 576 | unsigned int s_length = sg_dma_len(s); |
Robin Murphy | 809eac5 | 2016-04-11 12:32:31 +0100 | [diff] [blame] | 577 | unsigned int s_iova_len = s->length; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 578 | |
Robin Murphy | 809eac5 | 2016-04-11 12:32:31 +0100 | [diff] [blame] | 579 | s->offset += s_iova_off; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 580 | s->length = s_length; |
Robin Murphy | 809eac5 | 2016-04-11 12:32:31 +0100 | [diff] [blame] | 581 | sg_dma_address(s) = DMA_ERROR_CODE; |
| 582 | sg_dma_len(s) = 0; |
| 583 | |
| 584 | /* |
| 585 | * Now fill in the real DMA data. If... |
| 586 | * - there is a valid output segment to append to |
| 587 | * - and this segment starts on an IOVA page boundary |
| 588 | * - but doesn't fall at a segment boundary |
| 589 | * - and wouldn't make the resulting output segment too long |
| 590 | */ |
| 591 | if (cur_len && !s_iova_off && (dma_addr & seg_mask) && |
| 592 | (cur_len + s_length <= max_len)) { |
| 593 | /* ...then concatenate it with the previous one */ |
| 594 | cur_len += s_length; |
| 595 | } else { |
| 596 | /* Otherwise start the next output segment */ |
| 597 | if (i > 0) |
| 598 | cur = sg_next(cur); |
| 599 | cur_len = s_length; |
| 600 | count++; |
| 601 | |
| 602 | sg_dma_address(cur) = dma_addr + s_iova_off; |
| 603 | } |
| 604 | |
| 605 | sg_dma_len(cur) = cur_len; |
| 606 | dma_addr += s_iova_len; |
| 607 | |
| 608 | if (s_length + s_iova_off < s_iova_len) |
| 609 | cur_len = 0; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 610 | } |
Robin Murphy | 809eac5 | 2016-04-11 12:32:31 +0100 | [diff] [blame] | 611 | return count; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | /* |
| 615 | * If mapping failed, then just restore the original list, |
| 616 | * but making sure the DMA fields are invalidated. |
| 617 | */ |
| 618 | static void __invalidate_sg(struct scatterlist *sg, int nents) |
| 619 | { |
| 620 | struct scatterlist *s; |
| 621 | int i; |
| 622 | |
| 623 | for_each_sg(sg, s, nents, i) { |
| 624 | if (sg_dma_address(s) != DMA_ERROR_CODE) |
Robin Murphy | 07b48ac | 2016-03-10 19:28:12 +0000 | [diff] [blame] | 625 | s->offset += sg_dma_address(s); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 626 | if (sg_dma_len(s)) |
| 627 | s->length = sg_dma_len(s); |
| 628 | sg_dma_address(s) = DMA_ERROR_CODE; |
| 629 | sg_dma_len(s) = 0; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | /* |
| 634 | * The DMA API client is passing in a scatterlist which could describe |
| 635 | * any old buffer layout, but the IOMMU API requires everything to be |
| 636 | * aligned to IOMMU pages. Hence the need for this complicated bit of |
| 637 | * impedance-matching, to be able to hand off a suitably-aligned list, |
| 638 | * but still preserve the original offsets and sizes for the caller. |
| 639 | */ |
| 640 | int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg, |
| 641 | int nents, int prot) |
| 642 | { |
| 643 | struct iommu_domain *domain = iommu_get_domain_for_dev(dev); |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 644 | struct iommu_dma_cookie *cookie = domain->iova_cookie; |
| 645 | struct iova_domain *iovad = &cookie->iovad; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 646 | struct scatterlist *s, *prev = NULL; |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 647 | dma_addr_t iova; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 648 | size_t iova_len = 0; |
Robin Murphy | 809eac5 | 2016-04-11 12:32:31 +0100 | [diff] [blame] | 649 | unsigned long mask = dma_get_seg_boundary(dev); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 650 | int i; |
| 651 | |
| 652 | /* |
| 653 | * Work out how much IOVA space we need, and align the segments to |
| 654 | * IOVA granules for the IOMMU driver to handle. With some clever |
| 655 | * trickery we can modify the list in-place, but reversibly, by |
Robin Murphy | 809eac5 | 2016-04-11 12:32:31 +0100 | [diff] [blame] | 656 | * stashing the unaligned parts in the as-yet-unused DMA fields. |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 657 | */ |
| 658 | for_each_sg(sg, s, nents, i) { |
Robin Murphy | 809eac5 | 2016-04-11 12:32:31 +0100 | [diff] [blame] | 659 | size_t s_iova_off = iova_offset(iovad, s->offset); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 660 | size_t s_length = s->length; |
Robin Murphy | 809eac5 | 2016-04-11 12:32:31 +0100 | [diff] [blame] | 661 | size_t pad_len = (mask - iova_len + 1) & mask; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 662 | |
Robin Murphy | 809eac5 | 2016-04-11 12:32:31 +0100 | [diff] [blame] | 663 | sg_dma_address(s) = s_iova_off; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 664 | sg_dma_len(s) = s_length; |
Robin Murphy | 809eac5 | 2016-04-11 12:32:31 +0100 | [diff] [blame] | 665 | s->offset -= s_iova_off; |
| 666 | s_length = iova_align(iovad, s_length + s_iova_off); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 667 | s->length = s_length; |
| 668 | |
| 669 | /* |
Robin Murphy | 809eac5 | 2016-04-11 12:32:31 +0100 | [diff] [blame] | 670 | * Due to the alignment of our single IOVA allocation, we can |
| 671 | * depend on these assumptions about the segment boundary mask: |
| 672 | * - If mask size >= IOVA size, then the IOVA range cannot |
| 673 | * possibly fall across a boundary, so we don't care. |
| 674 | * - If mask size < IOVA size, then the IOVA range must start |
| 675 | * exactly on a boundary, therefore we can lay things out |
| 676 | * based purely on segment lengths without needing to know |
| 677 | * the actual addresses beforehand. |
| 678 | * - The mask must be a power of 2, so pad_len == 0 if |
| 679 | * iova_len == 0, thus we cannot dereference prev the first |
| 680 | * time through here (i.e. before it has a meaningful value). |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 681 | */ |
Robin Murphy | 809eac5 | 2016-04-11 12:32:31 +0100 | [diff] [blame] | 682 | if (pad_len && pad_len < s_length - 1) { |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 683 | prev->length += pad_len; |
| 684 | iova_len += pad_len; |
| 685 | } |
| 686 | |
| 687 | iova_len += s_length; |
| 688 | prev = s; |
| 689 | } |
| 690 | |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 691 | iova = iommu_dma_alloc_iova(domain, iova_len, dma_get_mask(dev), dev); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 692 | if (!iova) |
| 693 | goto out_restore_sg; |
| 694 | |
| 695 | /* |
| 696 | * We'll leave any physical concatenation to the IOMMU driver's |
| 697 | * implementation - it knows better than we do. |
| 698 | */ |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 699 | if (iommu_map_sg(domain, iova, sg, nents, prot) < iova_len) |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 700 | goto out_free_iova; |
| 701 | |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 702 | return __finalise_sg(dev, sg, nents, iova); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 703 | |
| 704 | out_free_iova: |
Patrick Daly | 2330148 | 2017-10-12 16:18:25 -0700 | [diff] [blame] | 705 | iommu_dma_free_iova(domain, cookie, iova, iova_len); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 706 | out_restore_sg: |
| 707 | __invalidate_sg(sg, nents); |
| 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 712 | enum dma_data_direction dir, unsigned long attrs) |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 713 | { |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 714 | dma_addr_t start, end; |
| 715 | struct scatterlist *tmp; |
| 716 | int i; |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 717 | /* |
| 718 | * The scatterlist segments are mapped into a single |
| 719 | * contiguous IOVA allocation, so this is incredibly easy. |
| 720 | */ |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 721 | start = sg_dma_address(sg); |
| 722 | for_each_sg(sg_next(sg), tmp, nents - 1, i) { |
| 723 | if (sg_dma_len(tmp) == 0) |
| 724 | break; |
| 725 | sg = tmp; |
| 726 | } |
| 727 | end = sg_dma_address(sg) + sg_dma_len(sg); |
| 728 | __iommu_dma_unmap(iommu_get_domain_for_dev(dev), start, end - start); |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 729 | } |
| 730 | |
Robin Murphy | c84ed86 | 2016-11-14 12:16:26 +0000 | [diff] [blame] | 731 | dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys, |
| 732 | size_t size, enum dma_data_direction dir, unsigned long attrs) |
| 733 | { |
| 734 | return __iommu_dma_map(dev, phys, size, |
| 735 | dma_direction_to_prot(dir, false) | IOMMU_MMIO); |
| 736 | } |
| 737 | |
| 738 | void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle, |
| 739 | size_t size, enum dma_data_direction dir, unsigned long attrs) |
| 740 | { |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 741 | __iommu_dma_unmap(iommu_get_domain_for_dev(dev), handle, size); |
Robin Murphy | c84ed86 | 2016-11-14 12:16:26 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Robin Murphy | 0db2e5d | 2015-10-01 20:13:58 +0100 | [diff] [blame] | 744 | int iommu_dma_supported(struct device *dev, u64 mask) |
| 745 | { |
| 746 | /* |
| 747 | * 'Special' IOMMUs which don't have the same addressing capability |
| 748 | * as the CPU will have to wait until we have some way to query that |
| 749 | * before they'll be able to use this framework. |
| 750 | */ |
| 751 | return 1; |
| 752 | } |
| 753 | |
| 754 | int iommu_dma_mapping_error(struct device *dev, dma_addr_t dma_addr) |
| 755 | { |
| 756 | return dma_addr == DMA_ERROR_CODE; |
| 757 | } |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 758 | |
| 759 | static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev, |
| 760 | phys_addr_t msi_addr, struct iommu_domain *domain) |
| 761 | { |
| 762 | struct iommu_dma_cookie *cookie = domain->iova_cookie; |
| 763 | struct iommu_dma_msi_page *msi_page; |
| 764 | struct iova_domain *iovad = &cookie->iovad; |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 765 | dma_addr_t iova; |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 766 | int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO; |
| 767 | |
| 768 | msi_addr &= ~(phys_addr_t)iova_mask(iovad); |
| 769 | list_for_each_entry(msi_page, &cookie->msi_page_list, list) |
| 770 | if (msi_page->phys == msi_addr) |
| 771 | return msi_page; |
| 772 | |
| 773 | msi_page = kzalloc(sizeof(*msi_page), GFP_ATOMIC); |
| 774 | if (!msi_page) |
| 775 | return NULL; |
| 776 | |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 777 | iova = iommu_dma_alloc_iova(domain, iovad->granule, dma_get_mask(dev), |
| 778 | dev); |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 779 | if (!iova) |
| 780 | goto out_free_page; |
| 781 | |
| 782 | msi_page->phys = msi_addr; |
Robin Murphy | 0e08002 | 2017-03-31 15:46:05 +0100 | [diff] [blame] | 783 | msi_page->iova = iova; |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 784 | if (iommu_map(domain, msi_page->iova, msi_addr, iovad->granule, prot)) |
| 785 | goto out_free_iova; |
| 786 | |
| 787 | INIT_LIST_HEAD(&msi_page->list); |
| 788 | list_add(&msi_page->list, &cookie->msi_page_list); |
| 789 | return msi_page; |
| 790 | |
| 791 | out_free_iova: |
Patrick Daly | 2330148 | 2017-10-12 16:18:25 -0700 | [diff] [blame] | 792 | iommu_dma_free_iova(domain, cookie, iova, iovad->granule); |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 793 | out_free_page: |
| 794 | kfree(msi_page); |
| 795 | return NULL; |
| 796 | } |
| 797 | |
| 798 | void iommu_dma_map_msi_msg(int irq, struct msi_msg *msg) |
| 799 | { |
| 800 | struct device *dev = msi_desc_to_dev(irq_get_msi_desc(irq)); |
| 801 | struct iommu_domain *domain = iommu_get_domain_for_dev(dev); |
| 802 | struct iommu_dma_cookie *cookie; |
| 803 | struct iommu_dma_msi_page *msi_page; |
| 804 | phys_addr_t msi_addr = (u64)msg->address_hi << 32 | msg->address_lo; |
| 805 | unsigned long flags; |
| 806 | |
| 807 | if (!domain || !domain->iova_cookie) |
| 808 | return; |
| 809 | |
| 810 | cookie = domain->iova_cookie; |
| 811 | |
| 812 | /* |
| 813 | * We disable IRQs to rule out a possible inversion against |
| 814 | * irq_desc_lock if, say, someone tries to retarget the affinity |
| 815 | * of an MSI from within an IPI handler. |
| 816 | */ |
| 817 | spin_lock_irqsave(&cookie->msi_lock, flags); |
| 818 | msi_page = iommu_dma_get_msi_page(dev, msi_addr, domain); |
| 819 | spin_unlock_irqrestore(&cookie->msi_lock, flags); |
| 820 | |
| 821 | if (WARN_ON(!msi_page)) { |
| 822 | /* |
| 823 | * We're called from a void callback, so the best we can do is |
| 824 | * 'fail' by filling the message with obviously bogus values. |
| 825 | * Since we got this far due to an IOMMU being present, it's |
| 826 | * not like the existing address would have worked anyway... |
| 827 | */ |
| 828 | msg->address_hi = ~0U; |
| 829 | msg->address_lo = ~0U; |
| 830 | msg->data = ~0U; |
| 831 | } else { |
| 832 | msg->address_hi = upper_32_bits(msi_page->iova); |
| 833 | msg->address_lo &= iova_mask(&cookie->iovad); |
| 834 | msg->address_lo += lower_32_bits(msi_page->iova); |
| 835 | } |
| 836 | } |