Thomas Gleixner | 2025cf9 | 2019-05-29 07:18:02 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
David Woodhouse | 8a94ade | 2015-03-24 14:54:56 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright © 2015 Intel Corporation. |
| 4 | * |
David Woodhouse | 8a94ade | 2015-03-24 14:54:56 +0000 | [diff] [blame] | 5 | * Authors: David Woodhouse <dwmw2@infradead.org> |
| 6 | */ |
| 7 | |
| 8 | #include <linux/intel-iommu.h> |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 9 | #include <linux/mmu_notifier.h> |
| 10 | #include <linux/sched.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 11 | #include <linux/sched/mm.h> |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 12 | #include <linux/slab.h> |
| 13 | #include <linux/intel-svm.h> |
| 14 | #include <linux/rculist.h> |
| 15 | #include <linux/pci.h> |
| 16 | #include <linux/pci-ats.h> |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 17 | #include <linux/dmar.h> |
| 18 | #include <linux/interrupt.h> |
Souptick Joarder | 50a7ca3 | 2018-08-17 15:44:47 -0700 | [diff] [blame] | 19 | #include <linux/mm_types.h> |
Jacob Pan | 59a6233 | 2020-01-02 08:18:08 +0800 | [diff] [blame] | 20 | #include <linux/ioasid.h> |
Ashok Raj | 9d8c3af | 2017-08-08 13:29:27 -0700 | [diff] [blame] | 21 | #include <asm/page.h> |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 22 | |
Lu Baolu | af39507 | 2018-07-14 15:46:56 +0800 | [diff] [blame] | 23 | #include "intel-pasid.h" |
| 24 | |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 25 | static irqreturn_t prq_event_thread(int irq, void *d); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 26 | |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 27 | #define PRQ_ORDER 0 |
| 28 | |
| 29 | int intel_svm_enable_prq(struct intel_iommu *iommu) |
| 30 | { |
| 31 | struct page *pages; |
| 32 | int irq, ret; |
| 33 | |
| 34 | pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, PRQ_ORDER); |
| 35 | if (!pages) { |
| 36 | pr_warn("IOMMU: %s: Failed to allocate page request queue\n", |
| 37 | iommu->name); |
| 38 | return -ENOMEM; |
| 39 | } |
| 40 | iommu->prq = page_address(pages); |
| 41 | |
| 42 | irq = dmar_alloc_hwirq(DMAR_UNITS_SUPPORTED + iommu->seq_id, iommu->node, iommu); |
| 43 | if (irq <= 0) { |
| 44 | pr_err("IOMMU: %s: Failed to create IRQ vector for page request queue\n", |
| 45 | iommu->name); |
| 46 | ret = -EINVAL; |
| 47 | err: |
| 48 | free_pages((unsigned long)iommu->prq, PRQ_ORDER); |
| 49 | iommu->prq = NULL; |
| 50 | return ret; |
| 51 | } |
| 52 | iommu->pr_irq = irq; |
| 53 | |
| 54 | snprintf(iommu->prq_name, sizeof(iommu->prq_name), "dmar%d-prq", iommu->seq_id); |
| 55 | |
| 56 | ret = request_threaded_irq(irq, NULL, prq_event_thread, IRQF_ONESHOT, |
| 57 | iommu->prq_name, iommu); |
| 58 | if (ret) { |
| 59 | pr_err("IOMMU: %s: Failed to request IRQ for page request queue\n", |
| 60 | iommu->name); |
| 61 | dmar_free_hwirq(irq); |
Jerry Snitselaar | 72d5481 | 2017-12-20 09:48:56 -0700 | [diff] [blame] | 62 | iommu->pr_irq = 0; |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 63 | goto err; |
| 64 | } |
| 65 | dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL); |
| 66 | dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL); |
| 67 | dmar_writeq(iommu->reg + DMAR_PQA_REG, virt_to_phys(iommu->prq) | PRQ_ORDER); |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | int intel_svm_finish_prq(struct intel_iommu *iommu) |
| 73 | { |
| 74 | dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL); |
| 75 | dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL); |
| 76 | dmar_writeq(iommu->reg + DMAR_PQA_REG, 0ULL); |
| 77 | |
Jerry Snitselaar | 72d5481 | 2017-12-20 09:48:56 -0700 | [diff] [blame] | 78 | if (iommu->pr_irq) { |
| 79 | free_irq(iommu->pr_irq, iommu); |
| 80 | dmar_free_hwirq(iommu->pr_irq); |
| 81 | iommu->pr_irq = 0; |
| 82 | } |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 83 | |
| 84 | free_pages((unsigned long)iommu->prq, PRQ_ORDER); |
| 85 | iommu->prq = NULL; |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
Jacob Pan | ff3dc65 | 2020-01-02 08:18:03 +0800 | [diff] [blame] | 90 | static inline bool intel_svm_capable(struct intel_iommu *iommu) |
| 91 | { |
| 92 | return iommu->flags & VTD_FLAG_SVM_CAPABLE; |
| 93 | } |
| 94 | |
| 95 | void intel_svm_check(struct intel_iommu *iommu) |
| 96 | { |
| 97 | if (!pasid_supported(iommu)) |
| 98 | return; |
| 99 | |
| 100 | if (cpu_feature_enabled(X86_FEATURE_GBPAGES) && |
| 101 | !cap_fl1gp_support(iommu->cap)) { |
| 102 | pr_err("%s SVM disabled, incompatible 1GB page capability\n", |
| 103 | iommu->name); |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | if (cpu_feature_enabled(X86_FEATURE_LA57) && |
| 108 | !cap_5lp_support(iommu->cap)) { |
| 109 | pr_err("%s SVM disabled, incompatible paging mode\n", |
| 110 | iommu->name); |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | iommu->flags |= VTD_FLAG_SVM_CAPABLE; |
| 115 | } |
| 116 | |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 117 | static void intel_flush_svm_range_dev (struct intel_svm *svm, struct intel_svm_dev *sdev, |
Jacob Pan | 8744daf | 2019-08-26 08:53:29 -0700 | [diff] [blame] | 118 | unsigned long address, unsigned long pages, int ih) |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 119 | { |
| 120 | struct qi_desc desc; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 121 | |
Lu Baolu | f81b846 | 2019-11-20 14:10:16 +0800 | [diff] [blame] | 122 | if (pages == -1) { |
Jacob Pan | 8744daf | 2019-08-26 08:53:29 -0700 | [diff] [blame] | 123 | desc.qw0 = QI_EIOTLB_PASID(svm->pasid) | |
| 124 | QI_EIOTLB_DID(sdev->did) | |
| 125 | QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) | |
| 126 | QI_EIOTLB_TYPE; |
Lu Baolu | 5d308fc | 2018-12-10 09:58:58 +0800 | [diff] [blame] | 127 | desc.qw1 = 0; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 128 | } else { |
David Woodhouse | 5d52f48 | 2015-10-20 15:52:13 +0100 | [diff] [blame] | 129 | int mask = ilog2(__roundup_pow_of_two(pages)); |
| 130 | |
Lu Baolu | 5d308fc | 2018-12-10 09:58:58 +0800 | [diff] [blame] | 131 | desc.qw0 = QI_EIOTLB_PASID(svm->pasid) | |
| 132 | QI_EIOTLB_DID(sdev->did) | |
| 133 | QI_EIOTLB_GRAN(QI_GRAN_PSI_PASID) | |
| 134 | QI_EIOTLB_TYPE; |
| 135 | desc.qw1 = QI_EIOTLB_ADDR(address) | |
Lu Baolu | 5d308fc | 2018-12-10 09:58:58 +0800 | [diff] [blame] | 136 | QI_EIOTLB_IH(ih) | |
| 137 | QI_EIOTLB_AM(mask); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 138 | } |
Lu Baolu | 5d308fc | 2018-12-10 09:58:58 +0800 | [diff] [blame] | 139 | desc.qw2 = 0; |
| 140 | desc.qw3 = 0; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 141 | qi_submit_sync(&desc, svm->iommu); |
| 142 | |
| 143 | if (sdev->dev_iotlb) { |
Lu Baolu | 5d308fc | 2018-12-10 09:58:58 +0800 | [diff] [blame] | 144 | desc.qw0 = QI_DEV_EIOTLB_PASID(svm->pasid) | |
| 145 | QI_DEV_EIOTLB_SID(sdev->sid) | |
| 146 | QI_DEV_EIOTLB_QDEP(sdev->qdep) | |
| 147 | QI_DEIOTLB_TYPE; |
David Woodhouse | 5d52f48 | 2015-10-20 15:52:13 +0100 | [diff] [blame] | 148 | if (pages == -1) { |
Lu Baolu | 5d308fc | 2018-12-10 09:58:58 +0800 | [diff] [blame] | 149 | desc.qw1 = QI_DEV_EIOTLB_ADDR(-1ULL >> 1) | |
| 150 | QI_DEV_EIOTLB_SIZE; |
David Woodhouse | 5d52f48 | 2015-10-20 15:52:13 +0100 | [diff] [blame] | 151 | } else if (pages > 1) { |
| 152 | /* The least significant zero bit indicates the size. So, |
| 153 | * for example, an "address" value of 0x12345f000 will |
| 154 | * flush from 0x123440000 to 0x12347ffff (256KiB). */ |
| 155 | unsigned long last = address + ((unsigned long)(pages - 1) << VTD_PAGE_SHIFT); |
Ingo Molnar | ed7158b | 2018-02-22 10:54:55 +0100 | [diff] [blame] | 156 | unsigned long mask = __rounddown_pow_of_two(address ^ last); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 157 | |
Lu Baolu | 5d308fc | 2018-12-10 09:58:58 +0800 | [diff] [blame] | 158 | desc.qw1 = QI_DEV_EIOTLB_ADDR((address & ~mask) | |
| 159 | (mask - 1)) | QI_DEV_EIOTLB_SIZE; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 160 | } else { |
Lu Baolu | 5d308fc | 2018-12-10 09:58:58 +0800 | [diff] [blame] | 161 | desc.qw1 = QI_DEV_EIOTLB_ADDR(address); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 162 | } |
Lu Baolu | 5d308fc | 2018-12-10 09:58:58 +0800 | [diff] [blame] | 163 | desc.qw2 = 0; |
| 164 | desc.qw3 = 0; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 165 | qi_submit_sync(&desc, svm->iommu); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | static void intel_flush_svm_range(struct intel_svm *svm, unsigned long address, |
Jacob Pan | 8744daf | 2019-08-26 08:53:29 -0700 | [diff] [blame] | 170 | unsigned long pages, int ih) |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 171 | { |
| 172 | struct intel_svm_dev *sdev; |
| 173 | |
| 174 | rcu_read_lock(); |
| 175 | list_for_each_entry_rcu(sdev, &svm->devs, list) |
Jacob Pan | 8744daf | 2019-08-26 08:53:29 -0700 | [diff] [blame] | 176 | intel_flush_svm_range_dev(svm, sdev, address, pages, ih); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 177 | rcu_read_unlock(); |
| 178 | } |
| 179 | |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 180 | /* Pages have been freed at this point */ |
| 181 | static void intel_invalidate_range(struct mmu_notifier *mn, |
| 182 | struct mm_struct *mm, |
| 183 | unsigned long start, unsigned long end) |
| 184 | { |
| 185 | struct intel_svm *svm = container_of(mn, struct intel_svm, notifier); |
| 186 | |
| 187 | intel_flush_svm_range(svm, start, |
Jacob Pan | 8744daf | 2019-08-26 08:53:29 -0700 | [diff] [blame] | 188 | (end - start + PAGE_SIZE - 1) >> VTD_PAGE_SHIFT, 0); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 189 | } |
| 190 | |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 191 | static void intel_mm_release(struct mmu_notifier *mn, struct mm_struct *mm) |
| 192 | { |
| 193 | struct intel_svm *svm = container_of(mn, struct intel_svm, notifier); |
David Woodhouse | e57e58b | 2016-01-12 19:18:06 +0000 | [diff] [blame] | 194 | struct intel_svm_dev *sdev; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 195 | |
David Woodhouse | e57e58b | 2016-01-12 19:18:06 +0000 | [diff] [blame] | 196 | /* This might end up being called from exit_mmap(), *before* the page |
| 197 | * tables are cleared. And __mmu_notifier_release() will delete us from |
| 198 | * the list of notifiers so that our invalidate_range() callback doesn't |
| 199 | * get called when the page tables are cleared. So we need to protect |
| 200 | * against hardware accessing those page tables. |
| 201 | * |
| 202 | * We do it by clearing the entry in the PASID table and then flushing |
| 203 | * the IOTLB and the PASID table caches. This might upset hardware; |
| 204 | * perhaps we'll want to point the PASID to a dummy PGD (like the zero |
| 205 | * page) so that we end up taking a fault that the hardware really |
| 206 | * *has* to handle gracefully without affecting other processes. |
| 207 | */ |
David Woodhouse | e57e58b | 2016-01-12 19:18:06 +0000 | [diff] [blame] | 208 | rcu_read_lock(); |
| 209 | list_for_each_entry_rcu(sdev, &svm->devs, list) { |
Lu Baolu | 1c4f88b | 2018-12-10 09:59:05 +0800 | [diff] [blame] | 210 | intel_pasid_tear_down_entry(svm->iommu, sdev->dev, svm->pasid); |
Jacob Pan | 8744daf | 2019-08-26 08:53:29 -0700 | [diff] [blame] | 211 | intel_flush_svm_range_dev(svm, sdev, 0, -1, 0); |
David Woodhouse | e57e58b | 2016-01-12 19:18:06 +0000 | [diff] [blame] | 212 | } |
| 213 | rcu_read_unlock(); |
| 214 | |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static const struct mmu_notifier_ops intel_mmuops = { |
| 218 | .release = intel_mm_release, |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 219 | .invalidate_range = intel_invalidate_range, |
| 220 | }; |
| 221 | |
| 222 | static DEFINE_MUTEX(pasid_mutex); |
Lu Baolu | 51261aa | 2018-07-14 15:46:55 +0800 | [diff] [blame] | 223 | static LIST_HEAD(global_svm_list); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 224 | |
Jacob Pan | 034d473 | 2020-01-02 08:18:10 +0800 | [diff] [blame] | 225 | #define for_each_svm_dev(sdev, svm, d) \ |
| 226 | list_for_each_entry((sdev), &(svm)->devs, list) \ |
| 227 | if ((d) != (sdev)->dev) {} else |
| 228 | |
Jacob Pan | 56722a4 | 2020-05-16 14:20:47 +0800 | [diff] [blame^] | 229 | int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev, |
| 230 | struct iommu_gpasid_bind_data *data) |
| 231 | { |
| 232 | struct intel_iommu *iommu = intel_svm_device_to_iommu(dev); |
| 233 | struct dmar_domain *dmar_domain; |
| 234 | struct intel_svm_dev *sdev; |
| 235 | struct intel_svm *svm; |
| 236 | int ret = 0; |
| 237 | |
| 238 | if (WARN_ON(!iommu) || !data) |
| 239 | return -EINVAL; |
| 240 | |
| 241 | if (data->version != IOMMU_GPASID_BIND_VERSION_1 || |
| 242 | data->format != IOMMU_PASID_FORMAT_INTEL_VTD) |
| 243 | return -EINVAL; |
| 244 | |
| 245 | if (!dev_is_pci(dev)) |
| 246 | return -ENOTSUPP; |
| 247 | |
| 248 | /* VT-d supports devices with full 20 bit PASIDs only */ |
| 249 | if (pci_max_pasids(to_pci_dev(dev)) != PASID_MAX) |
| 250 | return -EINVAL; |
| 251 | |
| 252 | /* |
| 253 | * We only check host PASID range, we have no knowledge to check |
| 254 | * guest PASID range. |
| 255 | */ |
| 256 | if (data->hpasid <= 0 || data->hpasid >= PASID_MAX) |
| 257 | return -EINVAL; |
| 258 | |
| 259 | dmar_domain = to_dmar_domain(domain); |
| 260 | |
| 261 | mutex_lock(&pasid_mutex); |
| 262 | svm = ioasid_find(NULL, data->hpasid, NULL); |
| 263 | if (IS_ERR(svm)) { |
| 264 | ret = PTR_ERR(svm); |
| 265 | goto out; |
| 266 | } |
| 267 | |
| 268 | if (svm) { |
| 269 | /* |
| 270 | * If we found svm for the PASID, there must be at |
| 271 | * least one device bond, otherwise svm should be freed. |
| 272 | */ |
| 273 | if (WARN_ON(list_empty(&svm->devs))) { |
| 274 | ret = -EINVAL; |
| 275 | goto out; |
| 276 | } |
| 277 | |
| 278 | for_each_svm_dev(sdev, svm, dev) { |
| 279 | /* |
| 280 | * For devices with aux domains, we should allow |
| 281 | * multiple bind calls with the same PASID and pdev. |
| 282 | */ |
| 283 | if (iommu_dev_feature_enabled(dev, |
| 284 | IOMMU_DEV_FEAT_AUX)) { |
| 285 | sdev->users++; |
| 286 | } else { |
| 287 | dev_warn_ratelimited(dev, |
| 288 | "Already bound with PASID %u\n", |
| 289 | svm->pasid); |
| 290 | ret = -EBUSY; |
| 291 | } |
| 292 | goto out; |
| 293 | } |
| 294 | } else { |
| 295 | /* We come here when PASID has never been bond to a device. */ |
| 296 | svm = kzalloc(sizeof(*svm), GFP_KERNEL); |
| 297 | if (!svm) { |
| 298 | ret = -ENOMEM; |
| 299 | goto out; |
| 300 | } |
| 301 | /* REVISIT: upper layer/VFIO can track host process that bind |
| 302 | * the PASID. ioasid_set = mm might be sufficient for vfio to |
| 303 | * check pasid VMM ownership. We can drop the following line |
| 304 | * once VFIO and IOASID set check is in place. |
| 305 | */ |
| 306 | svm->mm = get_task_mm(current); |
| 307 | svm->pasid = data->hpasid; |
| 308 | if (data->flags & IOMMU_SVA_GPASID_VAL) { |
| 309 | svm->gpasid = data->gpasid; |
| 310 | svm->flags |= SVM_FLAG_GUEST_PASID; |
| 311 | } |
| 312 | ioasid_set_data(data->hpasid, svm); |
| 313 | INIT_LIST_HEAD_RCU(&svm->devs); |
| 314 | mmput(svm->mm); |
| 315 | } |
| 316 | sdev = kzalloc(sizeof(*sdev), GFP_KERNEL); |
| 317 | if (!sdev) { |
| 318 | ret = -ENOMEM; |
| 319 | goto out; |
| 320 | } |
| 321 | sdev->dev = dev; |
| 322 | |
| 323 | /* Only count users if device has aux domains */ |
| 324 | if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX)) |
| 325 | sdev->users = 1; |
| 326 | |
| 327 | /* Set up device context entry for PASID if not enabled already */ |
| 328 | ret = intel_iommu_enable_pasid(iommu, sdev->dev); |
| 329 | if (ret) { |
| 330 | dev_err_ratelimited(dev, "Failed to enable PASID capability\n"); |
| 331 | kfree(sdev); |
| 332 | goto out; |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * PASID table is per device for better security. Therefore, for |
| 337 | * each bind of a new device even with an existing PASID, we need to |
| 338 | * call the nested mode setup function here. |
| 339 | */ |
| 340 | spin_lock(&iommu->lock); |
| 341 | ret = intel_pasid_setup_nested(iommu, dev, (pgd_t *)data->gpgd, |
| 342 | data->hpasid, &data->vtd, dmar_domain, |
| 343 | data->addr_width); |
| 344 | spin_unlock(&iommu->lock); |
| 345 | if (ret) { |
| 346 | dev_err_ratelimited(dev, "Failed to set up PASID %llu in nested mode, Err %d\n", |
| 347 | data->hpasid, ret); |
| 348 | /* |
| 349 | * PASID entry should be in cleared state if nested mode |
| 350 | * set up failed. So we only need to clear IOASID tracking |
| 351 | * data such that free call will succeed. |
| 352 | */ |
| 353 | kfree(sdev); |
| 354 | goto out; |
| 355 | } |
| 356 | |
| 357 | svm->flags |= SVM_FLAG_GUEST_MODE; |
| 358 | |
| 359 | init_rcu_head(&sdev->rcu); |
| 360 | list_add_rcu(&sdev->list, &svm->devs); |
| 361 | out: |
| 362 | if (!IS_ERR_OR_NULL(svm) && list_empty(&svm->devs)) { |
| 363 | ioasid_set_data(data->hpasid, NULL); |
| 364 | kfree(svm); |
| 365 | } |
| 366 | |
| 367 | mutex_unlock(&pasid_mutex); |
| 368 | return ret; |
| 369 | } |
| 370 | |
| 371 | int intel_svm_unbind_gpasid(struct device *dev, int pasid) |
| 372 | { |
| 373 | struct intel_iommu *iommu = intel_svm_device_to_iommu(dev); |
| 374 | struct intel_svm_dev *sdev; |
| 375 | struct intel_svm *svm; |
| 376 | int ret = -EINVAL; |
| 377 | |
| 378 | if (WARN_ON(!iommu)) |
| 379 | return -EINVAL; |
| 380 | |
| 381 | mutex_lock(&pasid_mutex); |
| 382 | svm = ioasid_find(NULL, pasid, NULL); |
| 383 | if (!svm) { |
| 384 | ret = -EINVAL; |
| 385 | goto out; |
| 386 | } |
| 387 | |
| 388 | if (IS_ERR(svm)) { |
| 389 | ret = PTR_ERR(svm); |
| 390 | goto out; |
| 391 | } |
| 392 | |
| 393 | for_each_svm_dev(sdev, svm, dev) { |
| 394 | ret = 0; |
| 395 | if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX)) |
| 396 | sdev->users--; |
| 397 | if (!sdev->users) { |
| 398 | list_del_rcu(&sdev->list); |
| 399 | intel_pasid_tear_down_entry(iommu, dev, svm->pasid); |
| 400 | intel_flush_svm_range_dev(svm, sdev, 0, -1, 0); |
| 401 | /* TODO: Drain in flight PRQ for the PASID since it |
| 402 | * may get reused soon, we don't want to |
| 403 | * confuse with its previous life. |
| 404 | * intel_svm_drain_prq(dev, pasid); |
| 405 | */ |
| 406 | kfree_rcu(sdev, rcu); |
| 407 | |
| 408 | if (list_empty(&svm->devs)) { |
| 409 | /* |
| 410 | * We do not free the IOASID here in that |
| 411 | * IOMMU driver did not allocate it. |
| 412 | * Unlike native SVM, IOASID for guest use was |
| 413 | * allocated prior to the bind call. |
| 414 | * In any case, if the free call comes before |
| 415 | * the unbind, IOMMU driver will get notified |
| 416 | * and perform cleanup. |
| 417 | */ |
| 418 | ioasid_set_data(pasid, NULL); |
| 419 | kfree(svm); |
| 420 | } |
| 421 | } |
| 422 | break; |
| 423 | } |
| 424 | out: |
| 425 | mutex_unlock(&pasid_mutex); |
| 426 | return ret; |
| 427 | } |
| 428 | |
David Woodhouse | 0204a49 | 2015-10-13 17:18:10 +0100 | [diff] [blame] | 429 | 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] | 430 | { |
| 431 | struct intel_iommu *iommu = intel_svm_device_to_iommu(dev); |
Lu Baolu | d7cbc0f | 2019-03-25 09:30:29 +0800 | [diff] [blame] | 432 | struct device_domain_info *info; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 433 | struct intel_svm_dev *sdev; |
| 434 | struct intel_svm *svm = NULL; |
David Woodhouse | 5cec753 | 2015-10-15 15:52:15 +0100 | [diff] [blame] | 435 | struct mm_struct *mm = NULL; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 436 | int pasid_max; |
| 437 | int ret; |
| 438 | |
Lu Baolu | c56cba5 | 2019-03-01 11:23:12 +0800 | [diff] [blame] | 439 | if (!iommu || dmar_disabled) |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 440 | return -EINVAL; |
| 441 | |
Jacob Pan | 6eba09a | 2020-01-02 08:18:05 +0800 | [diff] [blame] | 442 | if (!intel_svm_capable(iommu)) |
| 443 | return -ENOTSUPP; |
| 444 | |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 445 | if (dev_is_pci(dev)) { |
| 446 | pasid_max = pci_max_pasids(to_pci_dev(dev)); |
| 447 | if (pasid_max < 0) |
| 448 | return -EINVAL; |
| 449 | } else |
| 450 | pasid_max = 1 << 20; |
| 451 | |
Lu Baolu | bb37f7d | 2018-05-04 13:08:19 +0800 | [diff] [blame] | 452 | if (flags & SVM_FLAG_SUPERVISOR_MODE) { |
David Woodhouse | 5cec753 | 2015-10-15 15:52:15 +0100 | [diff] [blame] | 453 | if (!ecap_srs(iommu->ecap)) |
| 454 | return -EINVAL; |
| 455 | } else if (pasid) { |
| 456 | mm = get_task_mm(current); |
| 457 | BUG_ON(!mm); |
| 458 | } |
| 459 | |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 460 | mutex_lock(&pasid_mutex); |
David Woodhouse | 569e4f7 | 2015-10-15 13:59:14 +0100 | [diff] [blame] | 461 | if (pasid && !(flags & SVM_FLAG_PRIVATE_PASID)) { |
Lu Baolu | 51261aa | 2018-07-14 15:46:55 +0800 | [diff] [blame] | 462 | struct intel_svm *t; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 463 | |
Lu Baolu | 51261aa | 2018-07-14 15:46:55 +0800 | [diff] [blame] | 464 | list_for_each_entry(t, &global_svm_list, list) { |
| 465 | if (t->mm != mm || (t->flags & SVM_FLAG_PRIVATE_PASID)) |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 466 | continue; |
| 467 | |
Lu Baolu | 51261aa | 2018-07-14 15:46:55 +0800 | [diff] [blame] | 468 | svm = t; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 469 | if (svm->pasid >= pasid_max) { |
| 470 | dev_warn(dev, |
| 471 | "Limited PASID width. Cannot use existing PASID %d\n", |
| 472 | svm->pasid); |
| 473 | ret = -ENOSPC; |
| 474 | goto out; |
| 475 | } |
| 476 | |
Jacob Pan | 034d473 | 2020-01-02 08:18:10 +0800 | [diff] [blame] | 477 | /* Find the matching device in svm list */ |
| 478 | for_each_svm_dev(sdev, svm, dev) { |
| 479 | if (sdev->ops != ops) { |
| 480 | ret = -EBUSY; |
| 481 | goto out; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 482 | } |
Jacob Pan | 034d473 | 2020-01-02 08:18:10 +0800 | [diff] [blame] | 483 | sdev->users++; |
| 484 | goto success; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | break; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | sdev = kzalloc(sizeof(*sdev), GFP_KERNEL); |
| 492 | if (!sdev) { |
| 493 | ret = -ENOMEM; |
| 494 | goto out; |
| 495 | } |
| 496 | sdev->dev = dev; |
| 497 | |
Lu Baolu | d7cbc0f | 2019-03-25 09:30:29 +0800 | [diff] [blame] | 498 | ret = intel_iommu_enable_pasid(iommu, dev); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 499 | if (ret || !pasid) { |
| 500 | /* If they don't actually want to assign a PASID, this is |
| 501 | * just an enabling check/preparation. */ |
| 502 | kfree(sdev); |
| 503 | goto out; |
| 504 | } |
Lu Baolu | d7cbc0f | 2019-03-25 09:30:29 +0800 | [diff] [blame] | 505 | |
| 506 | info = dev->archdata.iommu; |
| 507 | if (!info || !info->pasid_supported) { |
| 508 | kfree(sdev); |
| 509 | goto out; |
| 510 | } |
| 511 | |
| 512 | sdev->did = FLPT_DEFAULT_DID; |
| 513 | sdev->sid = PCI_DEVID(info->bus, info->devfn); |
| 514 | if (info->ats_enabled) { |
| 515 | sdev->dev_iotlb = 1; |
| 516 | sdev->qdep = info->ats_qdep; |
| 517 | if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS) |
| 518 | sdev->qdep = 0; |
| 519 | } |
| 520 | |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 521 | /* Finish the setup now we know we're keeping it */ |
| 522 | sdev->users = 1; |
David Woodhouse | 0204a49 | 2015-10-13 17:18:10 +0100 | [diff] [blame] | 523 | sdev->ops = ops; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 524 | init_rcu_head(&sdev->rcu); |
| 525 | |
| 526 | if (!svm) { |
| 527 | svm = kzalloc(sizeof(*svm), GFP_KERNEL); |
| 528 | if (!svm) { |
| 529 | ret = -ENOMEM; |
| 530 | kfree(sdev); |
| 531 | goto out; |
| 532 | } |
| 533 | svm->iommu = iommu; |
| 534 | |
Lu Baolu | 4774cc5 | 2018-07-14 15:47:01 +0800 | [diff] [blame] | 535 | if (pasid_max > intel_pasid_max_id) |
| 536 | pasid_max = intel_pasid_max_id; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 537 | |
Jacob Pan | 59a6233 | 2020-01-02 08:18:08 +0800 | [diff] [blame] | 538 | /* Do not use PASID 0, reserved for RID to PASID */ |
| 539 | svm->pasid = ioasid_alloc(NULL, PASID_MIN, |
| 540 | pasid_max - 1, svm); |
| 541 | if (svm->pasid == INVALID_IOASID) { |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 542 | kfree(svm); |
Lu Baolu | bbe4b3a | 2018-02-24 13:42:27 +0800 | [diff] [blame] | 543 | kfree(sdev); |
Jacob Pan | 59a6233 | 2020-01-02 08:18:08 +0800 | [diff] [blame] | 544 | ret = -ENOSPC; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 545 | goto out; |
| 546 | } |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 547 | svm->notifier.ops = &intel_mmuops; |
David Woodhouse | 5cec753 | 2015-10-15 15:52:15 +0100 | [diff] [blame] | 548 | svm->mm = mm; |
David Woodhouse | 569e4f7 | 2015-10-15 13:59:14 +0100 | [diff] [blame] | 549 | svm->flags = flags; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 550 | INIT_LIST_HEAD_RCU(&svm->devs); |
Lu Baolu | 51261aa | 2018-07-14 15:46:55 +0800 | [diff] [blame] | 551 | INIT_LIST_HEAD(&svm->list); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 552 | ret = -ENOMEM; |
David Woodhouse | 5cec753 | 2015-10-15 15:52:15 +0100 | [diff] [blame] | 553 | if (mm) { |
| 554 | ret = mmu_notifier_register(&svm->notifier, mm); |
| 555 | if (ret) { |
Jacob Pan | 59a6233 | 2020-01-02 08:18:08 +0800 | [diff] [blame] | 556 | ioasid_free(svm->pasid); |
David Woodhouse | 5cec753 | 2015-10-15 15:52:15 +0100 | [diff] [blame] | 557 | kfree(svm); |
| 558 | kfree(sdev); |
| 559 | goto out; |
| 560 | } |
Lu Baolu | 1c4f88b | 2018-12-10 09:59:05 +0800 | [diff] [blame] | 561 | } |
Sohil Mehta | 2f13eb7 | 2017-12-20 11:59:27 -0800 | [diff] [blame] | 562 | |
Lu Baolu | 1c4f88b | 2018-12-10 09:59:05 +0800 | [diff] [blame] | 563 | spin_lock(&iommu->lock); |
| 564 | ret = intel_pasid_setup_first_level(iommu, dev, |
| 565 | mm ? mm->pgd : init_mm.pgd, |
| 566 | svm->pasid, FLPT_DEFAULT_DID, |
Lu Baolu | 87208f2 | 2020-01-02 08:18:16 +0800 | [diff] [blame] | 567 | (mm ? 0 : PASID_FLAG_SUPERVISOR_MODE) | |
| 568 | (cpu_feature_enabled(X86_FEATURE_LA57) ? |
| 569 | PASID_FLAG_FL5LP : 0)); |
Lu Baolu | 1c4f88b | 2018-12-10 09:59:05 +0800 | [diff] [blame] | 570 | spin_unlock(&iommu->lock); |
| 571 | if (ret) { |
| 572 | if (mm) |
| 573 | mmu_notifier_unregister(&svm->notifier, mm); |
Jacob Pan | 59a6233 | 2020-01-02 08:18:08 +0800 | [diff] [blame] | 574 | ioasid_free(svm->pasid); |
Lu Baolu | 1c4f88b | 2018-12-10 09:59:05 +0800 | [diff] [blame] | 575 | kfree(svm); |
| 576 | kfree(sdev); |
| 577 | goto out; |
| 578 | } |
Lu Baolu | 51261aa | 2018-07-14 15:46:55 +0800 | [diff] [blame] | 579 | |
| 580 | list_add_tail(&svm->list, &global_svm_list); |
Jacob Pan | d7af4d9 | 2019-05-08 12:22:46 -0700 | [diff] [blame] | 581 | } else { |
| 582 | /* |
| 583 | * Binding a new device with existing PASID, need to setup |
| 584 | * the PASID entry. |
| 585 | */ |
| 586 | spin_lock(&iommu->lock); |
| 587 | ret = intel_pasid_setup_first_level(iommu, dev, |
| 588 | mm ? mm->pgd : init_mm.pgd, |
| 589 | svm->pasid, FLPT_DEFAULT_DID, |
Lu Baolu | 87208f2 | 2020-01-02 08:18:16 +0800 | [diff] [blame] | 590 | (mm ? 0 : PASID_FLAG_SUPERVISOR_MODE) | |
| 591 | (cpu_feature_enabled(X86_FEATURE_LA57) ? |
| 592 | PASID_FLAG_FL5LP : 0)); |
Jacob Pan | d7af4d9 | 2019-05-08 12:22:46 -0700 | [diff] [blame] | 593 | spin_unlock(&iommu->lock); |
| 594 | if (ret) { |
| 595 | kfree(sdev); |
| 596 | goto out; |
| 597 | } |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 598 | } |
| 599 | list_add_rcu(&sdev->list, &svm->devs); |
| 600 | |
| 601 | success: |
| 602 | *pasid = svm->pasid; |
| 603 | ret = 0; |
| 604 | out: |
| 605 | mutex_unlock(&pasid_mutex); |
David Woodhouse | 5cec753 | 2015-10-15 15:52:15 +0100 | [diff] [blame] | 606 | if (mm) |
| 607 | mmput(mm); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 608 | return ret; |
| 609 | } |
| 610 | EXPORT_SYMBOL_GPL(intel_svm_bind_mm); |
| 611 | |
| 612 | int intel_svm_unbind_mm(struct device *dev, int pasid) |
| 613 | { |
| 614 | struct intel_svm_dev *sdev; |
| 615 | struct intel_iommu *iommu; |
| 616 | struct intel_svm *svm; |
| 617 | int ret = -EINVAL; |
| 618 | |
| 619 | mutex_lock(&pasid_mutex); |
| 620 | iommu = intel_svm_device_to_iommu(dev); |
Lu Baolu | 4774cc5 | 2018-07-14 15:47:01 +0800 | [diff] [blame] | 621 | if (!iommu) |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 622 | goto out; |
| 623 | |
Jacob Pan | 59a6233 | 2020-01-02 08:18:08 +0800 | [diff] [blame] | 624 | svm = ioasid_find(NULL, pasid, NULL); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 625 | if (!svm) |
| 626 | goto out; |
| 627 | |
Jacob Pan | 59a6233 | 2020-01-02 08:18:08 +0800 | [diff] [blame] | 628 | if (IS_ERR(svm)) { |
| 629 | ret = PTR_ERR(svm); |
| 630 | goto out; |
| 631 | } |
| 632 | |
Jacob Pan | 034d473 | 2020-01-02 08:18:10 +0800 | [diff] [blame] | 633 | for_each_svm_dev(sdev, svm, dev) { |
| 634 | ret = 0; |
| 635 | sdev->users--; |
| 636 | if (!sdev->users) { |
| 637 | list_del_rcu(&sdev->list); |
| 638 | /* Flush the PASID cache and IOTLB for this device. |
| 639 | * Note that we do depend on the hardware *not* using |
| 640 | * the PASID any more. Just as we depend on other |
| 641 | * devices never using PASIDs that they have no right |
| 642 | * to use. We have a *shared* PASID table, because it's |
| 643 | * large and has to be physically contiguous. So it's |
| 644 | * hard to be as defensive as we might like. */ |
| 645 | intel_pasid_tear_down_entry(iommu, dev, svm->pasid); |
| 646 | intel_flush_svm_range_dev(svm, sdev, 0, -1, 0); |
| 647 | kfree_rcu(sdev, rcu); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 648 | |
Jacob Pan | 034d473 | 2020-01-02 08:18:10 +0800 | [diff] [blame] | 649 | if (list_empty(&svm->devs)) { |
| 650 | ioasid_free(svm->pasid); |
| 651 | if (svm->mm) |
| 652 | mmu_notifier_unregister(&svm->notifier, svm->mm); |
| 653 | list_del(&svm->list); |
| 654 | /* We mandate that no page faults may be outstanding |
| 655 | * for the PASID when intel_svm_unbind_mm() is called. |
| 656 | * If that is not obeyed, subtle errors will happen. |
| 657 | * Let's make them less subtle... */ |
| 658 | memset(svm, 0x6b, sizeof(*svm)); |
| 659 | kfree(svm); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 660 | } |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 661 | } |
Jacob Pan | 034d473 | 2020-01-02 08:18:10 +0800 | [diff] [blame] | 662 | break; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 663 | } |
| 664 | out: |
| 665 | mutex_unlock(&pasid_mutex); |
| 666 | |
| 667 | return ret; |
| 668 | } |
| 669 | EXPORT_SYMBOL_GPL(intel_svm_unbind_mm); |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 670 | |
CQ Tang | 15060ab | 2017-05-10 11:39:03 -0700 | [diff] [blame] | 671 | int intel_svm_is_pasid_valid(struct device *dev, int pasid) |
| 672 | { |
| 673 | struct intel_iommu *iommu; |
| 674 | struct intel_svm *svm; |
| 675 | int ret = -EINVAL; |
| 676 | |
| 677 | mutex_lock(&pasid_mutex); |
| 678 | iommu = intel_svm_device_to_iommu(dev); |
Lu Baolu | 4774cc5 | 2018-07-14 15:47:01 +0800 | [diff] [blame] | 679 | if (!iommu) |
CQ Tang | 15060ab | 2017-05-10 11:39:03 -0700 | [diff] [blame] | 680 | goto out; |
| 681 | |
Jacob Pan | 59a6233 | 2020-01-02 08:18:08 +0800 | [diff] [blame] | 682 | svm = ioasid_find(NULL, pasid, NULL); |
CQ Tang | 15060ab | 2017-05-10 11:39:03 -0700 | [diff] [blame] | 683 | if (!svm) |
| 684 | goto out; |
| 685 | |
Jacob Pan | 59a6233 | 2020-01-02 08:18:08 +0800 | [diff] [blame] | 686 | if (IS_ERR(svm)) { |
| 687 | ret = PTR_ERR(svm); |
| 688 | goto out; |
| 689 | } |
CQ Tang | 15060ab | 2017-05-10 11:39:03 -0700 | [diff] [blame] | 690 | /* init_mm is used in this case */ |
| 691 | if (!svm->mm) |
| 692 | ret = 1; |
| 693 | else if (atomic_read(&svm->mm->mm_users) > 0) |
| 694 | ret = 1; |
| 695 | else |
| 696 | ret = 0; |
| 697 | |
| 698 | out: |
| 699 | mutex_unlock(&pasid_mutex); |
| 700 | |
| 701 | return ret; |
| 702 | } |
| 703 | EXPORT_SYMBOL_GPL(intel_svm_is_pasid_valid); |
| 704 | |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 705 | /* Page request queue descriptor */ |
| 706 | struct page_req_dsc { |
Jacob Pan | 5b438f4 | 2019-01-11 13:04:57 +0800 | [diff] [blame] | 707 | union { |
| 708 | struct { |
| 709 | u64 type:8; |
| 710 | u64 pasid_present:1; |
| 711 | u64 priv_data_present:1; |
| 712 | u64 rsvd:6; |
| 713 | u64 rid:16; |
| 714 | u64 pasid:20; |
| 715 | u64 exe_req:1; |
| 716 | u64 pm_req:1; |
| 717 | u64 rsvd2:10; |
| 718 | }; |
| 719 | u64 qw_0; |
| 720 | }; |
| 721 | union { |
| 722 | struct { |
| 723 | u64 rd_req:1; |
| 724 | u64 wr_req:1; |
| 725 | u64 lpig:1; |
| 726 | u64 prg_index:9; |
| 727 | u64 addr:52; |
| 728 | }; |
| 729 | u64 qw_1; |
| 730 | }; |
| 731 | u64 priv_data[2]; |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 732 | }; |
| 733 | |
Jacob Pan | 52355fb | 2020-03-17 09:10:18 +0800 | [diff] [blame] | 734 | #define PRQ_RING_MASK ((0x1000 << PRQ_ORDER) - 0x20) |
Joerg Roedel | 7f8312a | 2015-11-17 16:11:39 +0100 | [diff] [blame] | 735 | |
| 736 | static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req) |
| 737 | { |
| 738 | unsigned long requested = 0; |
| 739 | |
| 740 | if (req->exe_req) |
| 741 | requested |= VM_EXEC; |
| 742 | |
| 743 | if (req->rd_req) |
| 744 | requested |= VM_READ; |
| 745 | |
| 746 | if (req->wr_req) |
| 747 | requested |= VM_WRITE; |
| 748 | |
| 749 | return (requested & ~vma->vm_flags) != 0; |
| 750 | } |
| 751 | |
Ashok Raj | 9d8c3af | 2017-08-08 13:29:27 -0700 | [diff] [blame] | 752 | static bool is_canonical_address(u64 addr) |
| 753 | { |
| 754 | int shift = 64 - (__VIRTUAL_MASK_SHIFT + 1); |
| 755 | long saddr = (long) addr; |
| 756 | |
| 757 | return (((saddr << shift) >> shift) == saddr); |
| 758 | } |
| 759 | |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 760 | static irqreturn_t prq_event_thread(int irq, void *d) |
| 761 | { |
| 762 | struct intel_iommu *iommu = d; |
| 763 | struct intel_svm *svm = NULL; |
| 764 | int head, tail, handled = 0; |
| 765 | |
David Woodhouse | 4692400 | 2016-02-15 12:42:38 +0000 | [diff] [blame] | 766 | /* Clear PPR bit before reading head/tail registers, to |
| 767 | * ensure that we get a new interrupt if needed. */ |
| 768 | writel(DMA_PRS_PPR, iommu->reg + DMAR_PRS_REG); |
| 769 | |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 770 | tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK; |
| 771 | head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK; |
| 772 | while (head != tail) { |
David Woodhouse | 0204a49 | 2015-10-13 17:18:10 +0100 | [diff] [blame] | 773 | struct intel_svm_dev *sdev; |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 774 | struct vm_area_struct *vma; |
| 775 | struct page_req_dsc *req; |
| 776 | struct qi_desc resp; |
Souptick Joarder | 50a7ca3 | 2018-08-17 15:44:47 -0700 | [diff] [blame] | 777 | int result; |
| 778 | vm_fault_t ret; |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 779 | u64 address; |
| 780 | |
| 781 | handled = 1; |
| 782 | |
| 783 | req = &iommu->prq[head / sizeof(*req)]; |
| 784 | |
| 785 | result = QI_RESP_FAILURE; |
David Woodhouse | 7f92a2e | 2015-10-16 17:22:31 +0100 | [diff] [blame] | 786 | address = (u64)req->addr << VTD_PAGE_SHIFT; |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 787 | if (!req->pasid_present) { |
| 788 | pr_err("%s: Page request without PASID: %08llx %08llx\n", |
| 789 | iommu->name, ((unsigned long long *)req)[0], |
| 790 | ((unsigned long long *)req)[1]); |
Lu Baolu | 19ed3e2 | 2018-11-05 10:18:58 +0800 | [diff] [blame] | 791 | goto no_pasid; |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | if (!svm || svm->pasid != req->pasid) { |
| 795 | rcu_read_lock(); |
Jacob Pan | 59a6233 | 2020-01-02 08:18:08 +0800 | [diff] [blame] | 796 | svm = ioasid_find(NULL, req->pasid, NULL); |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 797 | /* It *can't* go away, because the driver is not permitted |
| 798 | * to unbind the mm while any page faults are outstanding. |
| 799 | * So we only need RCU to protect the internal idr code. */ |
| 800 | rcu_read_unlock(); |
Jacob Pan | 59a6233 | 2020-01-02 08:18:08 +0800 | [diff] [blame] | 801 | if (IS_ERR_OR_NULL(svm)) { |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 802 | pr_err("%s: Page request for invalid PASID %d: %08llx %08llx\n", |
| 803 | iommu->name, req->pasid, ((unsigned long long *)req)[0], |
| 804 | ((unsigned long long *)req)[1]); |
David Woodhouse | 26322ab | 2015-10-15 21:12:56 +0100 | [diff] [blame] | 805 | goto no_pasid; |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | |
| 809 | result = QI_RESP_INVALID; |
David Woodhouse | 5cec753 | 2015-10-15 15:52:15 +0100 | [diff] [blame] | 810 | /* Since we're using init_mm.pgd directly, we should never take |
| 811 | * any faults on kernel addresses. */ |
| 812 | if (!svm->mm) |
| 813 | goto bad_req; |
Ashok Raj | 9d8c3af | 2017-08-08 13:29:27 -0700 | [diff] [blame] | 814 | |
| 815 | /* If address is not canonical, return invalid response */ |
| 816 | if (!is_canonical_address(address)) |
| 817 | goto bad_req; |
| 818 | |
Jacob Pan | 902baf6 | 2020-03-19 21:32:30 -0700 | [diff] [blame] | 819 | /* If the mm is already defunct, don't handle faults. */ |
| 820 | if (!mmget_not_zero(svm->mm)) |
| 821 | goto bad_req; |
| 822 | |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 823 | down_read(&svm->mm->mmap_sem); |
| 824 | vma = find_extend_vma(svm->mm, address); |
| 825 | if (!vma || address < vma->vm_start) |
| 826 | goto invalid; |
| 827 | |
Joerg Roedel | 7f8312a | 2015-11-17 16:11:39 +0100 | [diff] [blame] | 828 | if (access_error(vma, req)) |
| 829 | goto invalid; |
| 830 | |
Kirill A. Shutemov | dcddffd | 2016-07-26 15:25:18 -0700 | [diff] [blame] | 831 | ret = handle_mm_fault(vma, address, |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 832 | req->wr_req ? FAULT_FLAG_WRITE : 0); |
| 833 | if (ret & VM_FAULT_ERROR) |
| 834 | goto invalid; |
| 835 | |
| 836 | result = QI_RESP_SUCCESS; |
| 837 | invalid: |
| 838 | up_read(&svm->mm->mmap_sem); |
David Woodhouse | e57e58b | 2016-01-12 19:18:06 +0000 | [diff] [blame] | 839 | mmput(svm->mm); |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 840 | bad_req: |
| 841 | /* Accounting for major/minor faults? */ |
David Woodhouse | 0204a49 | 2015-10-13 17:18:10 +0100 | [diff] [blame] | 842 | rcu_read_lock(); |
| 843 | list_for_each_entry_rcu(sdev, &svm->devs, list) { |
Jacob Pan | 5b438f4 | 2019-01-11 13:04:57 +0800 | [diff] [blame] | 844 | if (sdev->sid == req->rid) |
David Woodhouse | 0204a49 | 2015-10-13 17:18:10 +0100 | [diff] [blame] | 845 | break; |
| 846 | } |
| 847 | /* Other devices can go away, but the drivers are not permitted |
| 848 | * to unbind while any page faults might be in flight. So it's |
| 849 | * OK to drop the 'lock' here now we have it. */ |
| 850 | rcu_read_unlock(); |
| 851 | |
| 852 | if (WARN_ON(&sdev->list == &svm->devs)) |
| 853 | sdev = NULL; |
| 854 | |
| 855 | if (sdev && sdev->ops && sdev->ops->fault_cb) { |
| 856 | int rwxp = (req->rd_req << 3) | (req->wr_req << 2) | |
Jacob Pan | 5b438f4 | 2019-01-11 13:04:57 +0800 | [diff] [blame] | 857 | (req->exe_req << 1) | (req->pm_req); |
| 858 | sdev->ops->fault_cb(sdev->dev, req->pasid, req->addr, |
| 859 | req->priv_data, rwxp, result); |
David Woodhouse | 0204a49 | 2015-10-13 17:18:10 +0100 | [diff] [blame] | 860 | } |
David Woodhouse | 26322ab | 2015-10-15 21:12:56 +0100 | [diff] [blame] | 861 | /* We get here in the error case where the PASID lookup failed, |
| 862 | and these can be NULL. Do not use them below this point! */ |
| 863 | sdev = NULL; |
| 864 | svm = NULL; |
| 865 | no_pasid: |
Jacob Pan | 5b438f4 | 2019-01-11 13:04:57 +0800 | [diff] [blame] | 866 | if (req->lpig || req->priv_data_present) { |
| 867 | /* |
| 868 | * Per VT-d spec. v3.0 ch7.7, system software must |
| 869 | * respond with page group response if private data |
| 870 | * is present (PDP) or last page in group (LPIG) bit |
| 871 | * is set. This is an additional VT-d feature beyond |
| 872 | * PCI ATS spec. |
| 873 | */ |
Lu Baolu | 5d308fc | 2018-12-10 09:58:58 +0800 | [diff] [blame] | 874 | resp.qw0 = QI_PGRP_PASID(req->pasid) | |
Jacob Pan | 5b438f4 | 2019-01-11 13:04:57 +0800 | [diff] [blame] | 875 | QI_PGRP_DID(req->rid) | |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 876 | QI_PGRP_PASID_P(req->pasid_present) | |
Jacob Pan | 5b438f4 | 2019-01-11 13:04:57 +0800 | [diff] [blame] | 877 | QI_PGRP_PDP(req->pasid_present) | |
| 878 | QI_PGRP_RESP_CODE(result) | |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 879 | QI_PGRP_RESP_TYPE; |
Lu Baolu | 5d308fc | 2018-12-10 09:58:58 +0800 | [diff] [blame] | 880 | resp.qw1 = QI_PGRP_IDX(req->prg_index) | |
Jacob Pan | 5b438f4 | 2019-01-11 13:04:57 +0800 | [diff] [blame] | 881 | QI_PGRP_LPIG(req->lpig); |
| 882 | |
| 883 | if (req->priv_data_present) |
| 884 | memcpy(&resp.qw2, req->priv_data, |
| 885 | sizeof(req->priv_data)); |
Jacob Pan | 5f75585 | 2020-01-02 08:18:09 +0800 | [diff] [blame] | 886 | resp.qw2 = 0; |
| 887 | resp.qw3 = 0; |
| 888 | qi_submit_sync(&resp, iommu); |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 889 | } |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 890 | head = (head + sizeof(*req)) & PRQ_RING_MASK; |
| 891 | } |
| 892 | |
| 893 | dmar_writeq(iommu->reg + DMAR_PQH_REG, tail); |
| 894 | |
| 895 | return IRQ_RETVAL(handled); |
| 896 | } |