David Woodhouse | 8a94ade | 2015-03-24 14:54:56 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2015 Intel Corporation. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms and conditions of the GNU General Public License, |
| 6 | * version 2, as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * Authors: David Woodhouse <dwmw2@infradead.org> |
| 14 | */ |
| 15 | |
| 16 | #include <linux/intel-iommu.h> |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 17 | #include <linux/mmu_notifier.h> |
| 18 | #include <linux/sched.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 19 | #include <linux/sched/mm.h> |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 20 | #include <linux/slab.h> |
| 21 | #include <linux/intel-svm.h> |
| 22 | #include <linux/rculist.h> |
| 23 | #include <linux/pci.h> |
| 24 | #include <linux/pci-ats.h> |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 25 | #include <linux/dmar.h> |
| 26 | #include <linux/interrupt.h> |
Ashok Raj | 9d8c3af | 2017-08-08 13:29:27 -0700 | [diff] [blame] | 27 | #include <asm/page.h> |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 28 | |
| 29 | static irqreturn_t prq_event_thread(int irq, void *d); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 30 | |
| 31 | struct pasid_entry { |
| 32 | u64 val; |
| 33 | }; |
David Woodhouse | 8a94ade | 2015-03-24 14:54:56 +0000 | [diff] [blame] | 34 | |
David Woodhouse | 907fea3 | 2015-10-13 14:11:13 +0100 | [diff] [blame] | 35 | struct pasid_state_entry { |
| 36 | u64 val; |
| 37 | }; |
| 38 | |
David Woodhouse | 8a94ade | 2015-03-24 14:54:56 +0000 | [diff] [blame] | 39 | int intel_svm_alloc_pasid_tables(struct intel_iommu *iommu) |
| 40 | { |
| 41 | struct page *pages; |
| 42 | int order; |
| 43 | |
David Woodhouse | 9101704 | 2016-09-12 10:49:11 +0800 | [diff] [blame] | 44 | /* Start at 2 because it's defined as 2^(1+PSS) */ |
| 45 | iommu->pasid_max = 2 << ecap_pss(iommu->ecap); |
David Woodhouse | 8a94ade | 2015-03-24 14:54:56 +0000 | [diff] [blame] | 46 | |
David Woodhouse | 9101704 | 2016-09-12 10:49:11 +0800 | [diff] [blame] | 47 | /* Eventually I'm promised we will get a multi-level PASID table |
| 48 | * and it won't have to be physically contiguous. Until then, |
| 49 | * limit the size because 8MiB contiguous allocations can be hard |
| 50 | * to come by. The limit of 0x20000, which is 1MiB for each of |
| 51 | * the PASID and PASID-state tables, is somewhat arbitrary. */ |
| 52 | if (iommu->pasid_max > 0x20000) |
| 53 | iommu->pasid_max = 0x20000; |
| 54 | |
| 55 | order = get_order(sizeof(struct pasid_entry) * iommu->pasid_max); |
David Woodhouse | 8a94ade | 2015-03-24 14:54:56 +0000 | [diff] [blame] | 56 | pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, order); |
| 57 | if (!pages) { |
| 58 | pr_warn("IOMMU: %s: Failed to allocate PASID table\n", |
| 59 | iommu->name); |
| 60 | return -ENOMEM; |
| 61 | } |
| 62 | iommu->pasid_table = page_address(pages); |
| 63 | pr_info("%s: Allocated order %d PASID table.\n", iommu->name, order); |
| 64 | |
| 65 | if (ecap_dis(iommu->ecap)) { |
David Woodhouse | 9101704 | 2016-09-12 10:49:11 +0800 | [diff] [blame] | 66 | /* Just making it explicit... */ |
| 67 | BUILD_BUG_ON(sizeof(struct pasid_entry) != sizeof(struct pasid_state_entry)); |
David Woodhouse | 8a94ade | 2015-03-24 14:54:56 +0000 | [diff] [blame] | 68 | pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, order); |
| 69 | if (pages) |
| 70 | iommu->pasid_state_table = page_address(pages); |
| 71 | else |
| 72 | pr_warn("IOMMU: %s: Failed to allocate PASID state table\n", |
| 73 | iommu->name); |
| 74 | } |
| 75 | |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 76 | idr_init(&iommu->pasid_idr); |
| 77 | |
David Woodhouse | 8a94ade | 2015-03-24 14:54:56 +0000 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | int intel_svm_free_pasid_tables(struct intel_iommu *iommu) |
| 82 | { |
David Woodhouse | 9101704 | 2016-09-12 10:49:11 +0800 | [diff] [blame] | 83 | int order = get_order(sizeof(struct pasid_entry) * iommu->pasid_max); |
David Woodhouse | 8a94ade | 2015-03-24 14:54:56 +0000 | [diff] [blame] | 84 | |
| 85 | if (iommu->pasid_table) { |
| 86 | free_pages((unsigned long)iommu->pasid_table, order); |
| 87 | iommu->pasid_table = NULL; |
| 88 | } |
| 89 | if (iommu->pasid_state_table) { |
| 90 | free_pages((unsigned long)iommu->pasid_state_table, order); |
| 91 | iommu->pasid_state_table = NULL; |
| 92 | } |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 93 | idr_destroy(&iommu->pasid_idr); |
David Woodhouse | 8a94ade | 2015-03-24 14:54:56 +0000 | [diff] [blame] | 94 | return 0; |
| 95 | } |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 96 | |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 97 | #define PRQ_ORDER 0 |
| 98 | |
| 99 | int intel_svm_enable_prq(struct intel_iommu *iommu) |
| 100 | { |
| 101 | struct page *pages; |
| 102 | int irq, ret; |
| 103 | |
| 104 | pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, PRQ_ORDER); |
| 105 | if (!pages) { |
| 106 | pr_warn("IOMMU: %s: Failed to allocate page request queue\n", |
| 107 | iommu->name); |
| 108 | return -ENOMEM; |
| 109 | } |
| 110 | iommu->prq = page_address(pages); |
| 111 | |
| 112 | irq = dmar_alloc_hwirq(DMAR_UNITS_SUPPORTED + iommu->seq_id, iommu->node, iommu); |
| 113 | if (irq <= 0) { |
| 114 | pr_err("IOMMU: %s: Failed to create IRQ vector for page request queue\n", |
| 115 | iommu->name); |
| 116 | ret = -EINVAL; |
| 117 | err: |
| 118 | free_pages((unsigned long)iommu->prq, PRQ_ORDER); |
| 119 | iommu->prq = NULL; |
| 120 | return ret; |
| 121 | } |
| 122 | iommu->pr_irq = irq; |
| 123 | |
| 124 | snprintf(iommu->prq_name, sizeof(iommu->prq_name), "dmar%d-prq", iommu->seq_id); |
| 125 | |
| 126 | ret = request_threaded_irq(irq, NULL, prq_event_thread, IRQF_ONESHOT, |
| 127 | iommu->prq_name, iommu); |
| 128 | if (ret) { |
| 129 | pr_err("IOMMU: %s: Failed to request IRQ for page request queue\n", |
| 130 | iommu->name); |
| 131 | dmar_free_hwirq(irq); |
Jerry Snitselaar | 72d5481 | 2017-12-20 09:48:56 -0700 | [diff] [blame^] | 132 | iommu->pr_irq = 0; |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 133 | goto err; |
| 134 | } |
| 135 | dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL); |
| 136 | dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL); |
| 137 | dmar_writeq(iommu->reg + DMAR_PQA_REG, virt_to_phys(iommu->prq) | PRQ_ORDER); |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | int intel_svm_finish_prq(struct intel_iommu *iommu) |
| 143 | { |
| 144 | dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL); |
| 145 | dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL); |
| 146 | dmar_writeq(iommu->reg + DMAR_PQA_REG, 0ULL); |
| 147 | |
Jerry Snitselaar | 72d5481 | 2017-12-20 09:48:56 -0700 | [diff] [blame^] | 148 | if (iommu->pr_irq) { |
| 149 | free_irq(iommu->pr_irq, iommu); |
| 150 | dmar_free_hwirq(iommu->pr_irq); |
| 151 | iommu->pr_irq = 0; |
| 152 | } |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 153 | |
| 154 | free_pages((unsigned long)iommu->prq, PRQ_ORDER); |
| 155 | iommu->prq = NULL; |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 160 | static void intel_flush_svm_range_dev (struct intel_svm *svm, struct intel_svm_dev *sdev, |
David Woodhouse | 5d52f48 | 2015-10-20 15:52:13 +0100 | [diff] [blame] | 161 | unsigned long address, unsigned long pages, int ih, int gl) |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 162 | { |
| 163 | struct qi_desc desc; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 164 | |
David Woodhouse | 5d52f48 | 2015-10-20 15:52:13 +0100 | [diff] [blame] | 165 | if (pages == -1) { |
David Woodhouse | e034992 | 2015-10-16 19:36:53 +0100 | [diff] [blame] | 166 | /* For global kernel pages we have to flush them in *all* PASIDs |
| 167 | * because that's the only option the hardware gives us. Despite |
| 168 | * the fact that they are actually only accessible through one. */ |
| 169 | if (gl) |
| 170 | desc.low = QI_EIOTLB_PASID(svm->pasid) | QI_EIOTLB_DID(sdev->did) | |
| 171 | QI_EIOTLB_GRAN(QI_GRAN_ALL_ALL) | QI_EIOTLB_TYPE; |
| 172 | else |
| 173 | desc.low = QI_EIOTLB_PASID(svm->pasid) | QI_EIOTLB_DID(sdev->did) | |
| 174 | QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) | QI_EIOTLB_TYPE; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 175 | desc.high = 0; |
| 176 | } else { |
David Woodhouse | 5d52f48 | 2015-10-20 15:52:13 +0100 | [diff] [blame] | 177 | int mask = ilog2(__roundup_pow_of_two(pages)); |
| 178 | |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 179 | desc.low = QI_EIOTLB_PASID(svm->pasid) | QI_EIOTLB_DID(sdev->did) | |
| 180 | QI_EIOTLB_GRAN(QI_GRAN_PSI_PASID) | QI_EIOTLB_TYPE; |
David Woodhouse | e034992 | 2015-10-16 19:36:53 +0100 | [diff] [blame] | 181 | desc.high = QI_EIOTLB_ADDR(address) | QI_EIOTLB_GL(gl) | |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 182 | QI_EIOTLB_IH(ih) | QI_EIOTLB_AM(mask); |
| 183 | } |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 184 | qi_submit_sync(&desc, svm->iommu); |
| 185 | |
| 186 | if (sdev->dev_iotlb) { |
| 187 | desc.low = QI_DEV_EIOTLB_PASID(svm->pasid) | QI_DEV_EIOTLB_SID(sdev->sid) | |
| 188 | QI_DEV_EIOTLB_QDEP(sdev->qdep) | QI_DEIOTLB_TYPE; |
David Woodhouse | 5d52f48 | 2015-10-20 15:52:13 +0100 | [diff] [blame] | 189 | if (pages == -1) { |
| 190 | desc.high = QI_DEV_EIOTLB_ADDR(-1ULL >> 1) | QI_DEV_EIOTLB_SIZE; |
| 191 | } else if (pages > 1) { |
| 192 | /* The least significant zero bit indicates the size. So, |
| 193 | * for example, an "address" value of 0x12345f000 will |
| 194 | * flush from 0x123440000 to 0x12347ffff (256KiB). */ |
| 195 | unsigned long last = address + ((unsigned long)(pages - 1) << VTD_PAGE_SHIFT); |
| 196 | unsigned long mask = __rounddown_pow_of_two(address ^ last);; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 197 | |
David Woodhouse | 5d52f48 | 2015-10-20 15:52:13 +0100 | [diff] [blame] | 198 | desc.high = QI_DEV_EIOTLB_ADDR((address & ~mask) | (mask - 1)) | QI_DEV_EIOTLB_SIZE; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 199 | } else { |
| 200 | desc.high = QI_DEV_EIOTLB_ADDR(address); |
| 201 | } |
| 202 | qi_submit_sync(&desc, svm->iommu); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | static void intel_flush_svm_range(struct intel_svm *svm, unsigned long address, |
David Woodhouse | 5d52f48 | 2015-10-20 15:52:13 +0100 | [diff] [blame] | 207 | unsigned long pages, int ih, int gl) |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 208 | { |
| 209 | struct intel_svm_dev *sdev; |
| 210 | |
David Woodhouse | 907fea3 | 2015-10-13 14:11:13 +0100 | [diff] [blame] | 211 | /* Try deferred invalidate if available */ |
| 212 | if (svm->iommu->pasid_state_table && |
| 213 | !cmpxchg64(&svm->iommu->pasid_state_table[svm->pasid].val, 0, 1ULL << 63)) |
| 214 | return; |
| 215 | |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 216 | rcu_read_lock(); |
| 217 | list_for_each_entry_rcu(sdev, &svm->devs, list) |
David Woodhouse | e034992 | 2015-10-16 19:36:53 +0100 | [diff] [blame] | 218 | intel_flush_svm_range_dev(svm, sdev, address, pages, ih, gl); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 219 | rcu_read_unlock(); |
| 220 | } |
| 221 | |
| 222 | static void intel_change_pte(struct mmu_notifier *mn, struct mm_struct *mm, |
| 223 | unsigned long address, pte_t pte) |
| 224 | { |
| 225 | struct intel_svm *svm = container_of(mn, struct intel_svm, notifier); |
| 226 | |
David Woodhouse | e034992 | 2015-10-16 19:36:53 +0100 | [diff] [blame] | 227 | intel_flush_svm_range(svm, address, 1, 1, 0); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 228 | } |
| 229 | |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 230 | /* Pages have been freed at this point */ |
| 231 | static void intel_invalidate_range(struct mmu_notifier *mn, |
| 232 | struct mm_struct *mm, |
| 233 | unsigned long start, unsigned long end) |
| 234 | { |
| 235 | struct intel_svm *svm = container_of(mn, struct intel_svm, notifier); |
| 236 | |
| 237 | intel_flush_svm_range(svm, start, |
David Woodhouse | e034992 | 2015-10-16 19:36:53 +0100 | [diff] [blame] | 238 | (end - start + PAGE_SIZE - 1) >> VTD_PAGE_SHIFT, 0, 0); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | |
David Woodhouse | 5a10ba2 | 2015-10-24 21:06:39 +0200 | [diff] [blame] | 242 | static void intel_flush_pasid_dev(struct intel_svm *svm, struct intel_svm_dev *sdev, int pasid) |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 243 | { |
| 244 | struct qi_desc desc; |
| 245 | |
| 246 | desc.high = 0; |
David Woodhouse | 5a10ba2 | 2015-10-24 21:06:39 +0200 | [diff] [blame] | 247 | desc.low = QI_PC_TYPE | QI_PC_DID(sdev->did) | QI_PC_PASID_SEL | QI_PC_PASID(pasid); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 248 | |
| 249 | qi_submit_sync(&desc, svm->iommu); |
| 250 | } |
| 251 | |
| 252 | static void intel_mm_release(struct mmu_notifier *mn, struct mm_struct *mm) |
| 253 | { |
| 254 | struct intel_svm *svm = container_of(mn, struct intel_svm, notifier); |
David Woodhouse | e57e58b | 2016-01-12 19:18:06 +0000 | [diff] [blame] | 255 | struct intel_svm_dev *sdev; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 256 | |
David Woodhouse | e57e58b | 2016-01-12 19:18:06 +0000 | [diff] [blame] | 257 | /* This might end up being called from exit_mmap(), *before* the page |
| 258 | * tables are cleared. And __mmu_notifier_release() will delete us from |
| 259 | * the list of notifiers so that our invalidate_range() callback doesn't |
| 260 | * get called when the page tables are cleared. So we need to protect |
| 261 | * against hardware accessing those page tables. |
| 262 | * |
| 263 | * We do it by clearing the entry in the PASID table and then flushing |
| 264 | * the IOTLB and the PASID table caches. This might upset hardware; |
| 265 | * perhaps we'll want to point the PASID to a dummy PGD (like the zero |
| 266 | * page) so that we end up taking a fault that the hardware really |
| 267 | * *has* to handle gracefully without affecting other processes. |
| 268 | */ |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 269 | svm->iommu->pasid_table[svm->pasid].val = 0; |
David Woodhouse | e57e58b | 2016-01-12 19:18:06 +0000 | [diff] [blame] | 270 | wmb(); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 271 | |
David Woodhouse | e57e58b | 2016-01-12 19:18:06 +0000 | [diff] [blame] | 272 | rcu_read_lock(); |
| 273 | list_for_each_entry_rcu(sdev, &svm->devs, list) { |
| 274 | intel_flush_pasid_dev(svm, sdev, svm->pasid); |
| 275 | intel_flush_svm_range_dev(svm, sdev, 0, -1, 0, !svm->mm); |
| 276 | } |
| 277 | rcu_read_unlock(); |
| 278 | |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static const struct mmu_notifier_ops intel_mmuops = { |
| 282 | .release = intel_mm_release, |
| 283 | .change_pte = intel_change_pte, |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 284 | .invalidate_range = intel_invalidate_range, |
| 285 | }; |
| 286 | |
| 287 | static DEFINE_MUTEX(pasid_mutex); |
| 288 | |
David Woodhouse | 0204a49 | 2015-10-13 17:18:10 +0100 | [diff] [blame] | 289 | int intel_svm_bind_mm(struct device *dev, int *pasid, int flags, struct svm_dev_ops *ops) |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 290 | { |
| 291 | struct intel_iommu *iommu = intel_svm_device_to_iommu(dev); |
| 292 | struct intel_svm_dev *sdev; |
| 293 | struct intel_svm *svm = NULL; |
David Woodhouse | 5cec753 | 2015-10-15 15:52:15 +0100 | [diff] [blame] | 294 | struct mm_struct *mm = NULL; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 295 | int pasid_max; |
| 296 | int ret; |
| 297 | |
Lu Baolu | 2e2e35d | 2017-11-03 10:51:32 -0600 | [diff] [blame] | 298 | if (WARN_ON(!iommu || !iommu->pasid_table)) |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 299 | return -EINVAL; |
| 300 | |
| 301 | if (dev_is_pci(dev)) { |
| 302 | pasid_max = pci_max_pasids(to_pci_dev(dev)); |
| 303 | if (pasid_max < 0) |
| 304 | return -EINVAL; |
| 305 | } else |
| 306 | pasid_max = 1 << 20; |
| 307 | |
David Woodhouse | 5cec753 | 2015-10-15 15:52:15 +0100 | [diff] [blame] | 308 | if ((flags & SVM_FLAG_SUPERVISOR_MODE)) { |
| 309 | if (!ecap_srs(iommu->ecap)) |
| 310 | return -EINVAL; |
| 311 | } else if (pasid) { |
| 312 | mm = get_task_mm(current); |
| 313 | BUG_ON(!mm); |
| 314 | } |
| 315 | |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 316 | mutex_lock(&pasid_mutex); |
David Woodhouse | 569e4f7 | 2015-10-15 13:59:14 +0100 | [diff] [blame] | 317 | if (pasid && !(flags & SVM_FLAG_PRIVATE_PASID)) { |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 318 | int i; |
| 319 | |
| 320 | idr_for_each_entry(&iommu->pasid_idr, svm, i) { |
David Woodhouse | 5cec753 | 2015-10-15 15:52:15 +0100 | [diff] [blame] | 321 | if (svm->mm != mm || |
David Woodhouse | 569e4f7 | 2015-10-15 13:59:14 +0100 | [diff] [blame] | 322 | (svm->flags & SVM_FLAG_PRIVATE_PASID)) |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 323 | continue; |
| 324 | |
| 325 | if (svm->pasid >= pasid_max) { |
| 326 | dev_warn(dev, |
| 327 | "Limited PASID width. Cannot use existing PASID %d\n", |
| 328 | svm->pasid); |
| 329 | ret = -ENOSPC; |
| 330 | goto out; |
| 331 | } |
| 332 | |
| 333 | list_for_each_entry(sdev, &svm->devs, list) { |
| 334 | if (dev == sdev->dev) { |
David Woodhouse | 0204a49 | 2015-10-13 17:18:10 +0100 | [diff] [blame] | 335 | if (sdev->ops != ops) { |
| 336 | ret = -EBUSY; |
| 337 | goto out; |
| 338 | } |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 339 | sdev->users++; |
| 340 | goto success; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | sdev = kzalloc(sizeof(*sdev), GFP_KERNEL); |
| 349 | if (!sdev) { |
| 350 | ret = -ENOMEM; |
| 351 | goto out; |
| 352 | } |
| 353 | sdev->dev = dev; |
| 354 | |
| 355 | ret = intel_iommu_enable_pasid(iommu, sdev); |
| 356 | if (ret || !pasid) { |
| 357 | /* If they don't actually want to assign a PASID, this is |
| 358 | * just an enabling check/preparation. */ |
| 359 | kfree(sdev); |
| 360 | goto out; |
| 361 | } |
| 362 | /* Finish the setup now we know we're keeping it */ |
| 363 | sdev->users = 1; |
David Woodhouse | 0204a49 | 2015-10-13 17:18:10 +0100 | [diff] [blame] | 364 | sdev->ops = ops; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 365 | init_rcu_head(&sdev->rcu); |
| 366 | |
| 367 | if (!svm) { |
| 368 | svm = kzalloc(sizeof(*svm), GFP_KERNEL); |
| 369 | if (!svm) { |
| 370 | ret = -ENOMEM; |
| 371 | kfree(sdev); |
| 372 | goto out; |
| 373 | } |
| 374 | svm->iommu = iommu; |
| 375 | |
David Woodhouse | 9101704 | 2016-09-12 10:49:11 +0800 | [diff] [blame] | 376 | if (pasid_max > iommu->pasid_max) |
| 377 | pasid_max = iommu->pasid_max; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 378 | |
David Woodhouse | 5a10ba2 | 2015-10-24 21:06:39 +0200 | [diff] [blame] | 379 | /* Do not use PASID 0 in caching mode (virtualised IOMMU) */ |
| 380 | ret = idr_alloc(&iommu->pasid_idr, svm, |
| 381 | !!cap_caching_mode(iommu->cap), |
| 382 | pasid_max - 1, GFP_KERNEL); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 383 | if (ret < 0) { |
| 384 | kfree(svm); |
| 385 | goto out; |
| 386 | } |
| 387 | svm->pasid = ret; |
| 388 | svm->notifier.ops = &intel_mmuops; |
David Woodhouse | 5cec753 | 2015-10-15 15:52:15 +0100 | [diff] [blame] | 389 | svm->mm = mm; |
David Woodhouse | 569e4f7 | 2015-10-15 13:59:14 +0100 | [diff] [blame] | 390 | svm->flags = flags; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 391 | INIT_LIST_HEAD_RCU(&svm->devs); |
| 392 | ret = -ENOMEM; |
David Woodhouse | 5cec753 | 2015-10-15 15:52:15 +0100 | [diff] [blame] | 393 | if (mm) { |
| 394 | ret = mmu_notifier_register(&svm->notifier, mm); |
| 395 | if (ret) { |
| 396 | idr_remove(&svm->iommu->pasid_idr, svm->pasid); |
| 397 | kfree(svm); |
| 398 | kfree(sdev); |
| 399 | goto out; |
| 400 | } |
| 401 | iommu->pasid_table[svm->pasid].val = (u64)__pa(mm->pgd) | 1; |
David Woodhouse | 5cec753 | 2015-10-15 15:52:15 +0100 | [diff] [blame] | 402 | } else |
| 403 | iommu->pasid_table[svm->pasid].val = (u64)__pa(init_mm.pgd) | 1 | (1ULL << 11); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 404 | wmb(); |
David Woodhouse | 5a10ba2 | 2015-10-24 21:06:39 +0200 | [diff] [blame] | 405 | /* In caching mode, we still have to flush with PASID 0 when |
| 406 | * a PASID table entry becomes present. Not entirely clear |
| 407 | * *why* that would be the case — surely we could just issue |
| 408 | * a flush with the PASID value that we've changed? The PASID |
| 409 | * is the index into the table, after all. It's not like domain |
| 410 | * IDs in the case of the equivalent context-entry change in |
| 411 | * caching mode. And for that matter it's not entirely clear why |
| 412 | * a VMM would be in the business of caching the PASID table |
| 413 | * anyway. Surely that can be left entirely to the guest? */ |
| 414 | if (cap_caching_mode(iommu->cap)) |
| 415 | intel_flush_pasid_dev(svm, sdev, 0); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 416 | } |
| 417 | list_add_rcu(&sdev->list, &svm->devs); |
| 418 | |
| 419 | success: |
| 420 | *pasid = svm->pasid; |
| 421 | ret = 0; |
| 422 | out: |
| 423 | mutex_unlock(&pasid_mutex); |
David Woodhouse | 5cec753 | 2015-10-15 15:52:15 +0100 | [diff] [blame] | 424 | if (mm) |
| 425 | mmput(mm); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 426 | return ret; |
| 427 | } |
| 428 | EXPORT_SYMBOL_GPL(intel_svm_bind_mm); |
| 429 | |
| 430 | int intel_svm_unbind_mm(struct device *dev, int pasid) |
| 431 | { |
| 432 | struct intel_svm_dev *sdev; |
| 433 | struct intel_iommu *iommu; |
| 434 | struct intel_svm *svm; |
| 435 | int ret = -EINVAL; |
| 436 | |
| 437 | mutex_lock(&pasid_mutex); |
| 438 | iommu = intel_svm_device_to_iommu(dev); |
| 439 | if (!iommu || !iommu->pasid_table) |
| 440 | goto out; |
| 441 | |
| 442 | svm = idr_find(&iommu->pasid_idr, pasid); |
| 443 | if (!svm) |
| 444 | goto out; |
| 445 | |
| 446 | list_for_each_entry(sdev, &svm->devs, list) { |
| 447 | if (dev == sdev->dev) { |
| 448 | ret = 0; |
| 449 | sdev->users--; |
| 450 | if (!sdev->users) { |
| 451 | list_del_rcu(&sdev->list); |
| 452 | /* Flush the PASID cache and IOTLB for this device. |
| 453 | * Note that we do depend on the hardware *not* using |
| 454 | * the PASID any more. Just as we depend on other |
| 455 | * devices never using PASIDs that they have no right |
| 456 | * to use. We have a *shared* PASID table, because it's |
| 457 | * large and has to be physically contiguous. So it's |
| 458 | * hard to be as defensive as we might like. */ |
David Woodhouse | 5a10ba2 | 2015-10-24 21:06:39 +0200 | [diff] [blame] | 459 | intel_flush_pasid_dev(svm, sdev, svm->pasid); |
David Woodhouse | e034992 | 2015-10-16 19:36:53 +0100 | [diff] [blame] | 460 | intel_flush_svm_range_dev(svm, sdev, 0, -1, 0, !svm->mm); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 461 | kfree_rcu(sdev, rcu); |
| 462 | |
| 463 | if (list_empty(&svm->devs)) { |
Lu Baolu | 4fa064b | 2017-11-03 10:51:34 -0600 | [diff] [blame] | 464 | svm->iommu->pasid_table[svm->pasid].val = 0; |
| 465 | wmb(); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 466 | |
| 467 | idr_remove(&svm->iommu->pasid_idr, svm->pasid); |
David Woodhouse | 5cec753 | 2015-10-15 15:52:15 +0100 | [diff] [blame] | 468 | if (svm->mm) |
David Woodhouse | e57e58b | 2016-01-12 19:18:06 +0000 | [diff] [blame] | 469 | mmu_notifier_unregister(&svm->notifier, svm->mm); |
| 470 | |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 471 | /* We mandate that no page faults may be outstanding |
| 472 | * for the PASID when intel_svm_unbind_mm() is called. |
| 473 | * If that is not obeyed, subtle errors will happen. |
| 474 | * Let's make them less subtle... */ |
| 475 | memset(svm, 0x6b, sizeof(*svm)); |
| 476 | kfree(svm); |
| 477 | } |
| 478 | } |
| 479 | break; |
| 480 | } |
| 481 | } |
| 482 | out: |
| 483 | mutex_unlock(&pasid_mutex); |
| 484 | |
| 485 | return ret; |
| 486 | } |
| 487 | EXPORT_SYMBOL_GPL(intel_svm_unbind_mm); |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 488 | |
CQ Tang | 15060ab | 2017-05-10 11:39:03 -0700 | [diff] [blame] | 489 | int intel_svm_is_pasid_valid(struct device *dev, int pasid) |
| 490 | { |
| 491 | struct intel_iommu *iommu; |
| 492 | struct intel_svm *svm; |
| 493 | int ret = -EINVAL; |
| 494 | |
| 495 | mutex_lock(&pasid_mutex); |
| 496 | iommu = intel_svm_device_to_iommu(dev); |
| 497 | if (!iommu || !iommu->pasid_table) |
| 498 | goto out; |
| 499 | |
| 500 | svm = idr_find(&iommu->pasid_idr, pasid); |
| 501 | if (!svm) |
| 502 | goto out; |
| 503 | |
| 504 | /* init_mm is used in this case */ |
| 505 | if (!svm->mm) |
| 506 | ret = 1; |
| 507 | else if (atomic_read(&svm->mm->mm_users) > 0) |
| 508 | ret = 1; |
| 509 | else |
| 510 | ret = 0; |
| 511 | |
| 512 | out: |
| 513 | mutex_unlock(&pasid_mutex); |
| 514 | |
| 515 | return ret; |
| 516 | } |
| 517 | EXPORT_SYMBOL_GPL(intel_svm_is_pasid_valid); |
| 518 | |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 519 | /* Page request queue descriptor */ |
| 520 | struct page_req_dsc { |
| 521 | u64 srr:1; |
| 522 | u64 bof:1; |
| 523 | u64 pasid_present:1; |
| 524 | u64 lpig:1; |
| 525 | u64 pasid:20; |
| 526 | u64 bus:8; |
| 527 | u64 private:23; |
| 528 | u64 prg_index:9; |
| 529 | u64 rd_req:1; |
| 530 | u64 wr_req:1; |
| 531 | u64 exe_req:1; |
| 532 | u64 priv_req:1; |
| 533 | u64 devfn:8; |
| 534 | u64 addr:52; |
| 535 | }; |
| 536 | |
| 537 | #define PRQ_RING_MASK ((0x1000 << PRQ_ORDER) - 0x10) |
Joerg Roedel | 7f8312a | 2015-11-17 16:11:39 +0100 | [diff] [blame] | 538 | |
| 539 | static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req) |
| 540 | { |
| 541 | unsigned long requested = 0; |
| 542 | |
| 543 | if (req->exe_req) |
| 544 | requested |= VM_EXEC; |
| 545 | |
| 546 | if (req->rd_req) |
| 547 | requested |= VM_READ; |
| 548 | |
| 549 | if (req->wr_req) |
| 550 | requested |= VM_WRITE; |
| 551 | |
| 552 | return (requested & ~vma->vm_flags) != 0; |
| 553 | } |
| 554 | |
Ashok Raj | 9d8c3af | 2017-08-08 13:29:27 -0700 | [diff] [blame] | 555 | static bool is_canonical_address(u64 addr) |
| 556 | { |
| 557 | int shift = 64 - (__VIRTUAL_MASK_SHIFT + 1); |
| 558 | long saddr = (long) addr; |
| 559 | |
| 560 | return (((saddr << shift) >> shift) == saddr); |
| 561 | } |
| 562 | |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 563 | static irqreturn_t prq_event_thread(int irq, void *d) |
| 564 | { |
| 565 | struct intel_iommu *iommu = d; |
| 566 | struct intel_svm *svm = NULL; |
| 567 | int head, tail, handled = 0; |
| 568 | |
David Woodhouse | 4692400 | 2016-02-15 12:42:38 +0000 | [diff] [blame] | 569 | /* Clear PPR bit before reading head/tail registers, to |
| 570 | * ensure that we get a new interrupt if needed. */ |
| 571 | writel(DMA_PRS_PPR, iommu->reg + DMAR_PRS_REG); |
| 572 | |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 573 | tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK; |
| 574 | head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK; |
| 575 | while (head != tail) { |
David Woodhouse | 0204a49 | 2015-10-13 17:18:10 +0100 | [diff] [blame] | 576 | struct intel_svm_dev *sdev; |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 577 | struct vm_area_struct *vma; |
| 578 | struct page_req_dsc *req; |
| 579 | struct qi_desc resp; |
| 580 | int ret, result; |
| 581 | u64 address; |
| 582 | |
| 583 | handled = 1; |
| 584 | |
| 585 | req = &iommu->prq[head / sizeof(*req)]; |
| 586 | |
| 587 | result = QI_RESP_FAILURE; |
David Woodhouse | 7f92a2e | 2015-10-16 17:22:31 +0100 | [diff] [blame] | 588 | address = (u64)req->addr << VTD_PAGE_SHIFT; |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 589 | if (!req->pasid_present) { |
| 590 | pr_err("%s: Page request without PASID: %08llx %08llx\n", |
| 591 | iommu->name, ((unsigned long long *)req)[0], |
| 592 | ((unsigned long long *)req)[1]); |
| 593 | goto bad_req; |
| 594 | } |
| 595 | |
| 596 | if (!svm || svm->pasid != req->pasid) { |
| 597 | rcu_read_lock(); |
| 598 | svm = idr_find(&iommu->pasid_idr, req->pasid); |
| 599 | /* It *can't* go away, because the driver is not permitted |
| 600 | * to unbind the mm while any page faults are outstanding. |
| 601 | * So we only need RCU to protect the internal idr code. */ |
| 602 | rcu_read_unlock(); |
| 603 | |
| 604 | if (!svm) { |
| 605 | pr_err("%s: Page request for invalid PASID %d: %08llx %08llx\n", |
| 606 | iommu->name, req->pasid, ((unsigned long long *)req)[0], |
| 607 | ((unsigned long long *)req)[1]); |
David Woodhouse | 26322ab | 2015-10-15 21:12:56 +0100 | [diff] [blame] | 608 | goto no_pasid; |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
| 612 | result = QI_RESP_INVALID; |
David Woodhouse | 5cec753 | 2015-10-15 15:52:15 +0100 | [diff] [blame] | 613 | /* Since we're using init_mm.pgd directly, we should never take |
| 614 | * any faults on kernel addresses. */ |
| 615 | if (!svm->mm) |
| 616 | goto bad_req; |
David Woodhouse | e57e58b | 2016-01-12 19:18:06 +0000 | [diff] [blame] | 617 | /* If the mm is already defunct, don't handle faults. */ |
Vegard Nossum | 388f793 | 2017-02-27 14:30:13 -0800 | [diff] [blame] | 618 | if (!mmget_not_zero(svm->mm)) |
David Woodhouse | e57e58b | 2016-01-12 19:18:06 +0000 | [diff] [blame] | 619 | goto bad_req; |
Ashok Raj | 9d8c3af | 2017-08-08 13:29:27 -0700 | [diff] [blame] | 620 | |
| 621 | /* If address is not canonical, return invalid response */ |
| 622 | if (!is_canonical_address(address)) |
| 623 | goto bad_req; |
| 624 | |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 625 | down_read(&svm->mm->mmap_sem); |
| 626 | vma = find_extend_vma(svm->mm, address); |
| 627 | if (!vma || address < vma->vm_start) |
| 628 | goto invalid; |
| 629 | |
Joerg Roedel | 7f8312a | 2015-11-17 16:11:39 +0100 | [diff] [blame] | 630 | if (access_error(vma, req)) |
| 631 | goto invalid; |
| 632 | |
Kirill A. Shutemov | dcddffd | 2016-07-26 15:25:18 -0700 | [diff] [blame] | 633 | ret = handle_mm_fault(vma, address, |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 634 | req->wr_req ? FAULT_FLAG_WRITE : 0); |
| 635 | if (ret & VM_FAULT_ERROR) |
| 636 | goto invalid; |
| 637 | |
| 638 | result = QI_RESP_SUCCESS; |
| 639 | invalid: |
| 640 | up_read(&svm->mm->mmap_sem); |
David Woodhouse | e57e58b | 2016-01-12 19:18:06 +0000 | [diff] [blame] | 641 | mmput(svm->mm); |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 642 | bad_req: |
| 643 | /* Accounting for major/minor faults? */ |
David Woodhouse | 0204a49 | 2015-10-13 17:18:10 +0100 | [diff] [blame] | 644 | rcu_read_lock(); |
| 645 | list_for_each_entry_rcu(sdev, &svm->devs, list) { |
Dan Carpenter | 3c7c2f3 | 2015-10-17 08:18:47 +0300 | [diff] [blame] | 646 | if (sdev->sid == PCI_DEVID(req->bus, req->devfn)) |
David Woodhouse | 0204a49 | 2015-10-13 17:18:10 +0100 | [diff] [blame] | 647 | break; |
| 648 | } |
| 649 | /* Other devices can go away, but the drivers are not permitted |
| 650 | * to unbind while any page faults might be in flight. So it's |
| 651 | * OK to drop the 'lock' here now we have it. */ |
| 652 | rcu_read_unlock(); |
| 653 | |
| 654 | if (WARN_ON(&sdev->list == &svm->devs)) |
| 655 | sdev = NULL; |
| 656 | |
| 657 | if (sdev && sdev->ops && sdev->ops->fault_cb) { |
| 658 | int rwxp = (req->rd_req << 3) | (req->wr_req << 2) | |
David Woodhouse | 0bdec95 | 2015-10-28 15:14:09 +0900 | [diff] [blame] | 659 | (req->exe_req << 1) | (req->priv_req); |
David Woodhouse | 0204a49 | 2015-10-13 17:18:10 +0100 | [diff] [blame] | 660 | sdev->ops->fault_cb(sdev->dev, req->pasid, req->addr, req->private, rwxp, result); |
| 661 | } |
David Woodhouse | 26322ab | 2015-10-15 21:12:56 +0100 | [diff] [blame] | 662 | /* We get here in the error case where the PASID lookup failed, |
| 663 | and these can be NULL. Do not use them below this point! */ |
| 664 | sdev = NULL; |
| 665 | svm = NULL; |
| 666 | no_pasid: |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 667 | if (req->lpig) { |
| 668 | /* Page Group Response */ |
| 669 | resp.low = QI_PGRP_PASID(req->pasid) | |
| 670 | QI_PGRP_DID((req->bus << 8) | req->devfn) | |
| 671 | QI_PGRP_PASID_P(req->pasid_present) | |
| 672 | QI_PGRP_RESP_TYPE; |
| 673 | resp.high = QI_PGRP_IDX(req->prg_index) | |
| 674 | QI_PGRP_PRIV(req->private) | QI_PGRP_RESP_CODE(result); |
| 675 | |
David Woodhouse | 26322ab | 2015-10-15 21:12:56 +0100 | [diff] [blame] | 676 | qi_submit_sync(&resp, iommu); |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 677 | } else if (req->srr) { |
| 678 | /* Page Stream Response */ |
| 679 | resp.low = QI_PSTRM_IDX(req->prg_index) | |
| 680 | QI_PSTRM_PRIV(req->private) | QI_PSTRM_BUS(req->bus) | |
| 681 | QI_PSTRM_PASID(req->pasid) | QI_PSTRM_RESP_TYPE; |
| 682 | resp.high = QI_PSTRM_ADDR(address) | QI_PSTRM_DEVFN(req->devfn) | |
| 683 | QI_PSTRM_RESP_CODE(result); |
| 684 | |
David Woodhouse | 26322ab | 2015-10-15 21:12:56 +0100 | [diff] [blame] | 685 | qi_submit_sync(&resp, iommu); |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | head = (head + sizeof(*req)) & PRQ_RING_MASK; |
| 689 | } |
| 690 | |
| 691 | dmar_writeq(iommu->reg + DMAR_PQH_REG, tail); |
| 692 | |
| 693 | return IRQ_RETVAL(handled); |
| 694 | } |