Linux Audio

Check our new training course

Loading...
v5.9
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright © 2015 Intel Corporation.
   4 *
   5 * Authors: David Woodhouse <dwmw2@infradead.org>
   6 */
   7
   8#include <linux/intel-iommu.h>
   9#include <linux/mmu_notifier.h>
  10#include <linux/sched.h>
  11#include <linux/sched/mm.h>
  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>
  17#include <linux/dmar.h>
  18#include <linux/interrupt.h>
  19#include <linux/mm_types.h>
 
  20#include <linux/ioasid.h>
  21#include <asm/page.h>
 
  22
 
  23#include "pasid.h"
 
 
 
  24
  25static irqreturn_t prq_event_thread(int irq, void *d);
  26static void intel_svm_drain_prq(struct device *dev, int pasid);
 
 
 
 
 
 
 
 
  27
  28#define PRQ_ORDER 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  29
  30int intel_svm_enable_prq(struct intel_iommu *iommu)
  31{
 
  32	struct page *pages;
  33	int irq, ret;
  34
  35	pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, PRQ_ORDER);
  36	if (!pages) {
  37		pr_warn("IOMMU: %s: Failed to allocate page request queue\n",
  38			iommu->name);
  39		return -ENOMEM;
  40	}
  41	iommu->prq = page_address(pages);
  42
  43	irq = dmar_alloc_hwirq(DMAR_UNITS_SUPPORTED + iommu->seq_id, iommu->node, iommu);
  44	if (irq <= 0) {
  45		pr_err("IOMMU: %s: Failed to create IRQ vector for page request queue\n",
  46		       iommu->name);
  47		ret = -EINVAL;
  48	err:
  49		free_pages((unsigned long)iommu->prq, PRQ_ORDER);
  50		iommu->prq = NULL;
  51		return ret;
  52	}
  53	iommu->pr_irq = irq;
  54
 
 
 
 
 
 
 
 
 
 
  55	snprintf(iommu->prq_name, sizeof(iommu->prq_name), "dmar%d-prq", iommu->seq_id);
  56
  57	ret = request_threaded_irq(irq, NULL, prq_event_thread, IRQF_ONESHOT,
  58				   iommu->prq_name, iommu);
  59	if (ret) {
  60		pr_err("IOMMU: %s: Failed to request IRQ for page request queue\n",
  61		       iommu->name);
  62		dmar_free_hwirq(irq);
  63		iommu->pr_irq = 0;
  64		goto err;
  65	}
  66	dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
  67	dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
  68	dmar_writeq(iommu->reg + DMAR_PQA_REG, virt_to_phys(iommu->prq) | PRQ_ORDER);
  69
  70	init_completion(&iommu->prq_complete);
  71
  72	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
  73}
  74
  75int intel_svm_finish_prq(struct intel_iommu *iommu)
  76{
  77	dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
  78	dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
  79	dmar_writeq(iommu->reg + DMAR_PQA_REG, 0ULL);
  80
  81	if (iommu->pr_irq) {
  82		free_irq(iommu->pr_irq, iommu);
  83		dmar_free_hwirq(iommu->pr_irq);
  84		iommu->pr_irq = 0;
  85	}
  86
 
 
 
 
 
  87	free_pages((unsigned long)iommu->prq, PRQ_ORDER);
  88	iommu->prq = NULL;
  89
  90	return 0;
  91}
  92
  93static inline bool intel_svm_capable(struct intel_iommu *iommu)
  94{
  95	return iommu->flags & VTD_FLAG_SVM_CAPABLE;
  96}
  97
  98void intel_svm_check(struct intel_iommu *iommu)
  99{
 100	if (!pasid_supported(iommu))
 101		return;
 102
 103	if (cpu_feature_enabled(X86_FEATURE_GBPAGES) &&
 104	    !cap_fl1gp_support(iommu->cap)) {
 105		pr_err("%s SVM disabled, incompatible 1GB page capability\n",
 106		       iommu->name);
 107		return;
 108	}
 109
 110	if (cpu_feature_enabled(X86_FEATURE_LA57) &&
 111	    !cap_5lp_support(iommu->cap)) {
 112		pr_err("%s SVM disabled, incompatible paging mode\n",
 113		       iommu->name);
 114		return;
 115	}
 116
 117	iommu->flags |= VTD_FLAG_SVM_CAPABLE;
 118}
 119
 120static void intel_flush_svm_range_dev (struct intel_svm *svm, struct intel_svm_dev *sdev,
 121				unsigned long address, unsigned long pages, int ih)
 
 
 122{
 123	struct qi_desc desc;
 124
 125	if (pages == -1) {
 126		desc.qw0 = QI_EIOTLB_PASID(svm->pasid) |
 127			QI_EIOTLB_DID(sdev->did) |
 128			QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) |
 129			QI_EIOTLB_TYPE;
 130		desc.qw1 = 0;
 131	} else {
 132		int mask = ilog2(__roundup_pow_of_two(pages));
 133
 134		desc.qw0 = QI_EIOTLB_PASID(svm->pasid) |
 135				QI_EIOTLB_DID(sdev->did) |
 136				QI_EIOTLB_GRAN(QI_GRAN_PSI_PASID) |
 137				QI_EIOTLB_TYPE;
 138		desc.qw1 = QI_EIOTLB_ADDR(address) |
 139				QI_EIOTLB_IH(ih) |
 140				QI_EIOTLB_AM(mask);
 141	}
 142	desc.qw2 = 0;
 143	desc.qw3 = 0;
 144	qi_submit_sync(svm->iommu, &desc, 1, 0);
 145
 146	if (sdev->dev_iotlb) {
 147		desc.qw0 = QI_DEV_EIOTLB_PASID(svm->pasid) |
 148				QI_DEV_EIOTLB_SID(sdev->sid) |
 149				QI_DEV_EIOTLB_QDEP(sdev->qdep) |
 150				QI_DEIOTLB_TYPE;
 151		if (pages == -1) {
 152			desc.qw1 = QI_DEV_EIOTLB_ADDR(-1ULL >> 1) |
 153					QI_DEV_EIOTLB_SIZE;
 154		} else if (pages > 1) {
 155			/* The least significant zero bit indicates the size. So,
 156			 * for example, an "address" value of 0x12345f000 will
 157			 * flush from 0x123440000 to 0x12347ffff (256KiB). */
 158			unsigned long last = address + ((unsigned long)(pages - 1) << VTD_PAGE_SHIFT);
 159			unsigned long mask = __rounddown_pow_of_two(address ^ last);
 160
 161			desc.qw1 = QI_DEV_EIOTLB_ADDR((address & ~mask) |
 162					(mask - 1)) | QI_DEV_EIOTLB_SIZE;
 163		} else {
 164			desc.qw1 = QI_DEV_EIOTLB_ADDR(address);
 165		}
 166		desc.qw2 = 0;
 167		desc.qw3 = 0;
 168		qi_submit_sync(svm->iommu, &desc, 1, 0);
 169	}
 170}
 171
 172static void intel_flush_svm_range(struct intel_svm *svm, unsigned long address,
 173				unsigned long pages, int ih)
 174{
 175	struct intel_svm_dev *sdev;
 176
 177	rcu_read_lock();
 178	list_for_each_entry_rcu(sdev, &svm->devs, list)
 179		intel_flush_svm_range_dev(svm, sdev, address, pages, ih);
 180	rcu_read_unlock();
 181}
 182
 183/* Pages have been freed at this point */
 184static void intel_invalidate_range(struct mmu_notifier *mn,
 185				   struct mm_struct *mm,
 186				   unsigned long start, unsigned long end)
 187{
 188	struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
 189
 190	intel_flush_svm_range(svm, start,
 191			      (end - start + PAGE_SIZE - 1) >> VTD_PAGE_SHIFT, 0);
 192}
 193
 194static void intel_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)
 195{
 196	struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
 197	struct intel_svm_dev *sdev;
 198
 199	/* This might end up being called from exit_mmap(), *before* the page
 200	 * tables are cleared. And __mmu_notifier_release() will delete us from
 201	 * the list of notifiers so that our invalidate_range() callback doesn't
 202	 * get called when the page tables are cleared. So we need to protect
 203	 * against hardware accessing those page tables.
 204	 *
 205	 * We do it by clearing the entry in the PASID table and then flushing
 206	 * the IOTLB and the PASID table caches. This might upset hardware;
 207	 * perhaps we'll want to point the PASID to a dummy PGD (like the zero
 208	 * page) so that we end up taking a fault that the hardware really
 209	 * *has* to handle gracefully without affecting other processes.
 210	 */
 211	rcu_read_lock();
 212	list_for_each_entry_rcu(sdev, &svm->devs, list)
 213		intel_pasid_tear_down_entry(svm->iommu, sdev->dev,
 214					    svm->pasid, true);
 215	rcu_read_unlock();
 216
 217}
 218
 219static const struct mmu_notifier_ops intel_mmuops = {
 220	.release = intel_mm_release,
 221	.invalidate_range = intel_invalidate_range,
 222};
 223
 224static DEFINE_MUTEX(pasid_mutex);
 225static LIST_HEAD(global_svm_list);
 226
 227#define for_each_svm_dev(sdev, svm, d)			\
 228	list_for_each_entry((sdev), &(svm)->devs, list)	\
 229		if ((d) != (sdev)->dev) {} else
 230
 231static int pasid_to_svm_sdev(struct device *dev, unsigned int pasid,
 232			     struct intel_svm **rsvm,
 233			     struct intel_svm_dev **rsdev)
 234{
 235	struct intel_svm_dev *d, *sdev = NULL;
 236	struct intel_svm *svm;
 237
 238	/* The caller should hold the pasid_mutex lock */
 239	if (WARN_ON(!mutex_is_locked(&pasid_mutex)))
 240		return -EINVAL;
 241
 242	if (pasid == INVALID_IOASID || pasid >= PASID_MAX)
 243		return -EINVAL;
 244
 245	svm = ioasid_find(NULL, pasid, NULL);
 246	if (IS_ERR(svm))
 247		return PTR_ERR(svm);
 248
 249	if (!svm)
 250		goto out;
 251
 252	/*
 253	 * If we found svm for the PASID, there must be at least one device
 254	 * bond.
 255	 */
 256	if (WARN_ON(list_empty(&svm->devs)))
 257		return -EINVAL;
 258
 259	rcu_read_lock();
 260	list_for_each_entry_rcu(d, &svm->devs, list) {
 261		if (d->dev == dev) {
 262			sdev = d;
 263			break;
 264		}
 265	}
 266	rcu_read_unlock();
 267
 268out:
 269	*rsvm = svm;
 270	*rsdev = sdev;
 271
 272	return 0;
 273}
 274
 275int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,
 276			  struct iommu_gpasid_bind_data *data)
 
 277{
 278	struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL);
 279	struct intel_svm_dev *sdev = NULL;
 280	struct dmar_domain *dmar_domain;
 281	struct intel_svm *svm = NULL;
 282	int ret = 0;
 283
 284	if (WARN_ON(!iommu) || !data)
 285		return -EINVAL;
 286
 287	if (data->version != IOMMU_GPASID_BIND_VERSION_1 ||
 288	    data->format != IOMMU_PASID_FORMAT_INTEL_VTD)
 289		return -EINVAL;
 290
 291	if (!dev_is_pci(dev))
 292		return -ENOTSUPP;
 293
 294	/* VT-d supports devices with full 20 bit PASIDs only */
 295	if (pci_max_pasids(to_pci_dev(dev)) != PASID_MAX)
 296		return -EINVAL;
 297
 298	/*
 299	 * We only check host PASID range, we have no knowledge to check
 300	 * guest PASID range.
 301	 */
 302	if (data->hpasid <= 0 || data->hpasid >= PASID_MAX)
 303		return -EINVAL;
 304
 305	dmar_domain = to_dmar_domain(domain);
 306
 307	mutex_lock(&pasid_mutex);
 308	ret = pasid_to_svm_sdev(dev, data->hpasid, &svm, &sdev);
 309	if (ret)
 310		goto out;
 311
 312	if (sdev) {
 313		/*
 314		 * Do not allow multiple bindings of the same device-PASID since
 315		 * there is only one SL page tables per PASID. We may revisit
 316		 * once sharing PGD across domains are supported.
 317		 */
 318		dev_warn_ratelimited(dev, "Already bound with PASID %u\n",
 319				     svm->pasid);
 320		ret = -EBUSY;
 321		goto out;
 322	}
 323
 324	if (!svm) {
 325		/* We come here when PASID has never been bond to a device. */
 326		svm = kzalloc(sizeof(*svm), GFP_KERNEL);
 327		if (!svm) {
 328			ret = -ENOMEM;
 329			goto out;
 330		}
 331		/* REVISIT: upper layer/VFIO can track host process that bind
 332		 * the PASID. ioasid_set = mm might be sufficient for vfio to
 333		 * check pasid VMM ownership. We can drop the following line
 334		 * once VFIO and IOASID set check is in place.
 335		 */
 336		svm->mm = get_task_mm(current);
 337		svm->pasid = data->hpasid;
 338		if (data->flags & IOMMU_SVA_GPASID_VAL) {
 339			svm->gpasid = data->gpasid;
 340			svm->flags |= SVM_FLAG_GUEST_PASID;
 341		}
 342		ioasid_set_data(data->hpasid, svm);
 343		INIT_LIST_HEAD_RCU(&svm->devs);
 344		mmput(svm->mm);
 345	}
 346	sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
 347	if (!sdev) {
 348		ret = -ENOMEM;
 349		goto out;
 350	}
 351	sdev->dev = dev;
 352
 353	/* Only count users if device has aux domains */
 354	if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX))
 355		sdev->users = 1;
 356
 357	/* Set up device context entry for PASID if not enabled already */
 358	ret = intel_iommu_enable_pasid(iommu, sdev->dev);
 359	if (ret) {
 360		dev_err_ratelimited(dev, "Failed to enable PASID capability\n");
 361		kfree(sdev);
 362		goto out;
 363	}
 364
 365	/*
 366	 * PASID table is per device for better security. Therefore, for
 367	 * each bind of a new device even with an existing PASID, we need to
 368	 * call the nested mode setup function here.
 369	 */
 370	spin_lock(&iommu->lock);
 371	ret = intel_pasid_setup_nested(iommu, dev,
 372				       (pgd_t *)(uintptr_t)data->gpgd,
 373				       data->hpasid, &data->vtd, dmar_domain,
 374				       data->addr_width);
 375	spin_unlock(&iommu->lock);
 376	if (ret) {
 377		dev_err_ratelimited(dev, "Failed to set up PASID %llu in nested mode, Err %d\n",
 378				    data->hpasid, ret);
 379		/*
 380		 * PASID entry should be in cleared state if nested mode
 381		 * set up failed. So we only need to clear IOASID tracking
 382		 * data such that free call will succeed.
 383		 */
 384		kfree(sdev);
 385		goto out;
 386	}
 387
 388	svm->flags |= SVM_FLAG_GUEST_MODE;
 389
 390	init_rcu_head(&sdev->rcu);
 391	list_add_rcu(&sdev->list, &svm->devs);
 392 out:
 393	if (!IS_ERR_OR_NULL(svm) && list_empty(&svm->devs)) {
 394		ioasid_set_data(data->hpasid, NULL);
 395		kfree(svm);
 396	}
 397
 398	mutex_unlock(&pasid_mutex);
 399	return ret;
 400}
 401
 402int intel_svm_unbind_gpasid(struct device *dev, int pasid)
 403{
 404	struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL);
 405	struct intel_svm_dev *sdev;
 406	struct intel_svm *svm;
 407	int ret;
 408
 409	if (WARN_ON(!iommu))
 410		return -EINVAL;
 411
 412	mutex_lock(&pasid_mutex);
 413	ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev);
 414	if (ret)
 415		goto out;
 416
 417	if (sdev) {
 418		if (iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX))
 419			sdev->users--;
 420		if (!sdev->users) {
 421			list_del_rcu(&sdev->list);
 422			intel_pasid_tear_down_entry(iommu, dev,
 423						    svm->pasid, false);
 424			intel_svm_drain_prq(dev, svm->pasid);
 425			kfree_rcu(sdev, rcu);
 426
 427			if (list_empty(&svm->devs)) {
 428				/*
 429				 * We do not free the IOASID here in that
 430				 * IOMMU driver did not allocate it.
 431				 * Unlike native SVM, IOASID for guest use was
 432				 * allocated prior to the bind call.
 433				 * In any case, if the free call comes before
 434				 * the unbind, IOMMU driver will get notified
 435				 * and perform cleanup.
 436				 */
 437				ioasid_set_data(pasid, NULL);
 438				kfree(svm);
 439			}
 440		}
 441	}
 442out:
 443	mutex_unlock(&pasid_mutex);
 444	return ret;
 445}
 446
 447/* Caller must hold pasid_mutex, mm reference */
 448static int
 449intel_svm_bind_mm(struct device *dev, int flags, struct svm_dev_ops *ops,
 450		  struct mm_struct *mm, struct intel_svm_dev **sd)
 451{
 452	struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL);
 453	struct device_domain_info *info;
 454	struct intel_svm_dev *sdev;
 455	struct intel_svm *svm = NULL;
 456	int pasid_max;
 457	int ret;
 458
 459	if (!iommu || dmar_disabled)
 460		return -EINVAL;
 461
 462	if (!intel_svm_capable(iommu))
 463		return -ENOTSUPP;
 464
 465	if (dev_is_pci(dev)) {
 466		pasid_max = pci_max_pasids(to_pci_dev(dev));
 467		if (pasid_max < 0)
 468			return -EINVAL;
 469	} else
 470		pasid_max = 1 << 20;
 471
 472	/* Bind supervisor PASID shuld have mm = NULL */
 473	if (flags & SVM_FLAG_SUPERVISOR_MODE) {
 474		if (!ecap_srs(iommu->ecap) || mm) {
 475			pr_err("Supervisor PASID with user provided mm.\n");
 476			return -EINVAL;
 477		}
 478	}
 479
 480	if (!(flags & SVM_FLAG_PRIVATE_PASID)) {
 481		struct intel_svm *t;
 482
 483		list_for_each_entry(t, &global_svm_list, list) {
 484			if (t->mm != mm || (t->flags & SVM_FLAG_PRIVATE_PASID))
 485				continue;
 486
 487			svm = t;
 488			if (svm->pasid >= pasid_max) {
 489				dev_warn(dev,
 490					 "Limited PASID width. Cannot use existing PASID %d\n",
 491					 svm->pasid);
 492				ret = -ENOSPC;
 493				goto out;
 494			}
 495
 496			/* Find the matching device in svm list */
 497			for_each_svm_dev(sdev, svm, dev) {
 498				if (sdev->ops != ops) {
 499					ret = -EBUSY;
 500					goto out;
 501				}
 502				sdev->users++;
 503				goto success;
 504			}
 505
 506			break;
 507		}
 508	}
 509
 510	sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
 511	if (!sdev) {
 512		ret = -ENOMEM;
 513		goto out;
 514	}
 515	sdev->dev = dev;
 516
 517	ret = intel_iommu_enable_pasid(iommu, dev);
 518	if (ret) {
 519		kfree(sdev);
 520		goto out;
 521	}
 522
 523	info = get_domain_info(dev);
 
 524	sdev->did = FLPT_DEFAULT_DID;
 525	sdev->sid = PCI_DEVID(info->bus, info->devfn);
 
 
 
 
 526	if (info->ats_enabled) {
 527		sdev->dev_iotlb = 1;
 528		sdev->qdep = info->ats_qdep;
 529		if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS)
 530			sdev->qdep = 0;
 531	}
 532
 533	/* Finish the setup now we know we're keeping it */
 534	sdev->users = 1;
 535	sdev->ops = ops;
 536	init_rcu_head(&sdev->rcu);
 537
 538	if (!svm) {
 539		svm = kzalloc(sizeof(*svm), GFP_KERNEL);
 540		if (!svm) {
 541			ret = -ENOMEM;
 542			kfree(sdev);
 543			goto out;
 544		}
 545		svm->iommu = iommu;
 546
 547		if (pasid_max > intel_pasid_max_id)
 548			pasid_max = intel_pasid_max_id;
 549
 550		/* Do not use PASID 0, reserved for RID to PASID */
 551		svm->pasid = ioasid_alloc(NULL, PASID_MIN,
 552					  pasid_max - 1, svm);
 553		if (svm->pasid == INVALID_IOASID) {
 554			kfree(svm);
 555			kfree(sdev);
 556			ret = -ENOSPC;
 557			goto out;
 558		}
 559		svm->notifier.ops = &intel_mmuops;
 560		svm->mm = mm;
 561		svm->flags = flags;
 562		INIT_LIST_HEAD_RCU(&svm->devs);
 563		INIT_LIST_HEAD(&svm->list);
 564		ret = -ENOMEM;
 565		if (mm) {
 566			ret = mmu_notifier_register(&svm->notifier, mm);
 567			if (ret) {
 568				ioasid_free(svm->pasid);
 569				kfree(svm);
 570				kfree(sdev);
 571				goto out;
 572			}
 573		}
 574
 575		spin_lock(&iommu->lock);
 576		ret = intel_pasid_setup_first_level(iommu, dev,
 577				mm ? mm->pgd : init_mm.pgd,
 578				svm->pasid, FLPT_DEFAULT_DID,
 579				(mm ? 0 : PASID_FLAG_SUPERVISOR_MODE) |
 580				(cpu_feature_enabled(X86_FEATURE_LA57) ?
 581				 PASID_FLAG_FL5LP : 0));
 582		spin_unlock(&iommu->lock);
 583		if (ret) {
 584			if (mm)
 585				mmu_notifier_unregister(&svm->notifier, mm);
 586			ioasid_free(svm->pasid);
 587			kfree(svm);
 588			kfree(sdev);
 589			goto out;
 590		}
 591
 592		list_add_tail(&svm->list, &global_svm_list);
 593	} else {
 594		/*
 595		 * Binding a new device with existing PASID, need to setup
 596		 * the PASID entry.
 597		 */
 598		spin_lock(&iommu->lock);
 599		ret = intel_pasid_setup_first_level(iommu, dev,
 600						mm ? mm->pgd : init_mm.pgd,
 601						svm->pasid, FLPT_DEFAULT_DID,
 602						(mm ? 0 : PASID_FLAG_SUPERVISOR_MODE) |
 603						(cpu_feature_enabled(X86_FEATURE_LA57) ?
 604						PASID_FLAG_FL5LP : 0));
 605		spin_unlock(&iommu->lock);
 606		if (ret) {
 607			kfree(sdev);
 608			goto out;
 609		}
 610	}
 611	list_add_rcu(&sdev->list, &svm->devs);
 612success:
 613	sdev->pasid = svm->pasid;
 614	sdev->sva.dev = dev;
 615	if (sd)
 616		*sd = sdev;
 617	ret = 0;
 618out:
 619	return ret;
 
 
 
 
 
 620}
 621
 622/* Caller must hold pasid_mutex */
 623static int intel_svm_unbind_mm(struct device *dev, int pasid)
 624{
 625	struct intel_svm_dev *sdev;
 626	struct intel_iommu *iommu;
 627	struct intel_svm *svm;
 
 628	int ret = -EINVAL;
 629
 630	iommu = device_to_iommu(dev, NULL, NULL);
 631	if (!iommu)
 632		goto out;
 633
 634	ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev);
 635	if (ret)
 636		goto out;
 
 637
 638	if (sdev) {
 639		sdev->users--;
 640		if (!sdev->users) {
 641			list_del_rcu(&sdev->list);
 642			/* Flush the PASID cache and IOTLB for this device.
 643			 * Note that we do depend on the hardware *not* using
 644			 * the PASID any more. Just as we depend on other
 645			 * devices never using PASIDs that they have no right
 646			 * to use. We have a *shared* PASID table, because it's
 647			 * large and has to be physically contiguous. So it's
 648			 * hard to be as defensive as we might like. */
 649			intel_pasid_tear_down_entry(iommu, dev,
 650						    svm->pasid, false);
 651			intel_svm_drain_prq(dev, svm->pasid);
 652			kfree_rcu(sdev, rcu);
 653
 654			if (list_empty(&svm->devs)) {
 655				ioasid_free(svm->pasid);
 656				if (svm->mm)
 657					mmu_notifier_unregister(&svm->notifier, svm->mm);
 658				list_del(&svm->list);
 659				/* We mandate that no page faults may be outstanding
 660				 * for the PASID when intel_svm_unbind_mm() is called.
 661				 * If that is not obeyed, subtle errors will happen.
 662				 * Let's make them less subtle... */
 663				memset(svm, 0x6b, sizeof(*svm));
 664				kfree(svm);
 665			}
 666		}
 667	}
 668out:
 669	return ret;
 670}
 671
 672/* Page request queue descriptor */
 673struct page_req_dsc {
 674	union {
 675		struct {
 676			u64 type:8;
 677			u64 pasid_present:1;
 678			u64 priv_data_present:1;
 679			u64 rsvd:6;
 680			u64 rid:16;
 681			u64 pasid:20;
 682			u64 exe_req:1;
 683			u64 pm_req:1;
 684			u64 rsvd2:10;
 685		};
 686		u64 qw_0;
 687	};
 688	union {
 689		struct {
 690			u64 rd_req:1;
 691			u64 wr_req:1;
 692			u64 lpig:1;
 693			u64 prg_index:9;
 694			u64 addr:52;
 695		};
 696		u64 qw_1;
 697	};
 698	u64 priv_data[2];
 699};
 700
 701#define PRQ_RING_MASK	((0x1000 << PRQ_ORDER) - 0x20)
 702
 703static bool access_error(struct vm_area_struct *vma, struct page_req_dsc *req)
 704{
 705	unsigned long requested = 0;
 706
 707	if (req->exe_req)
 708		requested |= VM_EXEC;
 709
 710	if (req->rd_req)
 711		requested |= VM_READ;
 712
 713	if (req->wr_req)
 714		requested |= VM_WRITE;
 715
 716	return (requested & ~vma->vm_flags) != 0;
 717}
 718
 719static bool is_canonical_address(u64 addr)
 720{
 721	int shift = 64 - (__VIRTUAL_MASK_SHIFT + 1);
 722	long saddr = (long) addr;
 723
 724	return (((saddr << shift) >> shift) == saddr);
 725}
 726
 727/**
 728 * intel_svm_drain_prq - Drain page requests and responses for a pasid
 729 * @dev: target device
 730 * @pasid: pasid for draining
 731 *
 732 * Drain all pending page requests and responses related to @pasid in both
 733 * software and hardware. This is supposed to be called after the device
 734 * driver has stopped DMA, the pasid entry has been cleared, and both IOTLB
 735 * and DevTLB have been invalidated.
 736 *
 737 * It waits until all pending page requests for @pasid in the page fault
 738 * queue are completed by the prq handling thread. Then follow the steps
 739 * described in VT-d spec CH7.10 to drain all page requests and page
 740 * responses pending in the hardware.
 741 */
 742static void intel_svm_drain_prq(struct device *dev, int pasid)
 743{
 744	struct device_domain_info *info;
 745	struct dmar_domain *domain;
 746	struct intel_iommu *iommu;
 747	struct qi_desc desc[3];
 748	struct pci_dev *pdev;
 749	int head, tail;
 750	u16 sid, did;
 751	int qdep;
 752
 753	info = get_domain_info(dev);
 754	if (WARN_ON(!info || !dev_is_pci(dev)))
 755		return;
 756
 757	if (!info->pri_enabled)
 758		return;
 759
 760	iommu = info->iommu;
 761	domain = info->domain;
 762	pdev = to_pci_dev(dev);
 763	sid = PCI_DEVID(info->bus, info->devfn);
 764	did = domain->iommu_did[iommu->seq_id];
 765	qdep = pci_ats_queue_depth(pdev);
 766
 767	/*
 768	 * Check and wait until all pending page requests in the queue are
 769	 * handled by the prq handling thread.
 770	 */
 771prq_retry:
 772	reinit_completion(&iommu->prq_complete);
 773	tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
 774	head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
 775	while (head != tail) {
 776		struct page_req_dsc *req;
 777
 778		req = &iommu->prq[head / sizeof(*req)];
 779		if (!req->pasid_present || req->pasid != pasid) {
 780			head = (head + sizeof(*req)) & PRQ_RING_MASK;
 781			continue;
 782		}
 783
 784		wait_for_completion(&iommu->prq_complete);
 785		goto prq_retry;
 786	}
 787
 788	/*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 789	 * Perform steps described in VT-d spec CH7.10 to drain page
 790	 * requests and responses in hardware.
 791	 */
 792	memset(desc, 0, sizeof(desc));
 793	desc[0].qw0 = QI_IWD_STATUS_DATA(QI_DONE) |
 794			QI_IWD_FENCE |
 795			QI_IWD_TYPE;
 796	desc[1].qw0 = QI_EIOTLB_PASID(pasid) |
 797			QI_EIOTLB_DID(did) |
 798			QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) |
 799			QI_EIOTLB_TYPE;
 800	desc[2].qw0 = QI_DEV_EIOTLB_PASID(pasid) |
 801			QI_DEV_EIOTLB_SID(sid) |
 802			QI_DEV_EIOTLB_QDEP(qdep) |
 803			QI_DEIOTLB_TYPE |
 804			QI_DEV_IOTLB_PFSID(info->pfsid);
 805qi_retry:
 806	reinit_completion(&iommu->prq_complete);
 807	qi_submit_sync(iommu, desc, 3, QI_OPT_WAIT_DRAIN);
 808	if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO) {
 809		wait_for_completion(&iommu->prq_complete);
 810		goto qi_retry;
 811	}
 812}
 813
 814static int prq_to_iommu_prot(struct page_req_dsc *req)
 815{
 816	int prot = 0;
 817
 818	if (req->rd_req)
 819		prot |= IOMMU_FAULT_PERM_READ;
 820	if (req->wr_req)
 821		prot |= IOMMU_FAULT_PERM_WRITE;
 822	if (req->exe_req)
 823		prot |= IOMMU_FAULT_PERM_EXEC;
 824	if (req->pm_req)
 825		prot |= IOMMU_FAULT_PERM_PRIV;
 826
 827	return prot;
 828}
 829
 830static int
 831intel_svm_prq_report(struct device *dev, struct page_req_dsc *desc)
 832{
 833	struct iommu_fault_event event;
 834
 835	if (!dev || !dev_is_pci(dev))
 836		return -ENODEV;
 837
 838	/* Fill in event data for device specific processing */
 839	memset(&event, 0, sizeof(struct iommu_fault_event));
 840	event.fault.type = IOMMU_FAULT_PAGE_REQ;
 841	event.fault.prm.addr = desc->addr;
 842	event.fault.prm.pasid = desc->pasid;
 843	event.fault.prm.grpid = desc->prg_index;
 844	event.fault.prm.perm = prq_to_iommu_prot(desc);
 845
 846	if (desc->lpig)
 847		event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE;
 848	if (desc->pasid_present) {
 849		event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
 850		event.fault.prm.flags |= IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID;
 851	}
 852	if (desc->priv_data_present) {
 853		/*
 854		 * Set last page in group bit if private data is present,
 855		 * page response is required as it does for LPIG.
 856		 * iommu_report_device_fault() doesn't understand this vendor
 857		 * specific requirement thus we set last_page as a workaround.
 858		 */
 859		event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE;
 860		event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA;
 861		memcpy(event.fault.prm.private_data, desc->priv_data,
 862		       sizeof(desc->priv_data));
 
 
 
 
 
 
 863	}
 864
 865	return iommu_report_device_fault(dev, &event);
 866}
 867
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 868static irqreturn_t prq_event_thread(int irq, void *d)
 869{
 870	struct intel_svm_dev *sdev = NULL;
 871	struct intel_iommu *iommu = d;
 872	struct intel_svm *svm = NULL;
 873	int head, tail, handled = 0;
 
 
 874
 875	/* Clear PPR bit before reading head/tail registers, to
 876	 * ensure that we get a new interrupt if needed. */
 
 
 877	writel(DMA_PRS_PPR, iommu->reg + DMAR_PRS_REG);
 878
 879	tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
 880	head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
 
 881	while (head != tail) {
 882		struct vm_area_struct *vma;
 883		struct page_req_dsc *req;
 884		struct qi_desc resp;
 885		int result;
 886		vm_fault_t ret;
 887		u64 address;
 888
 889		handled = 1;
 890
 891		req = &iommu->prq[head / sizeof(*req)];
 892
 893		result = QI_RESP_FAILURE;
 894		address = (u64)req->addr << VTD_PAGE_SHIFT;
 895		if (!req->pasid_present) {
 896			pr_err("%s: Page request without PASID: %08llx %08llx\n",
 897			       iommu->name, ((unsigned long long *)req)[0],
 898			       ((unsigned long long *)req)[1]);
 899			goto no_pasid;
 900		}
 901
 902		if (!svm || svm->pasid != req->pasid) {
 903			rcu_read_lock();
 904			svm = ioasid_find(NULL, req->pasid, NULL);
 905			/* It *can't* go away, because the driver is not permitted
 906			 * to unbind the mm while any page faults are outstanding.
 907			 * So we only need RCU to protect the internal idr code. */
 908			rcu_read_unlock();
 909			if (IS_ERR_OR_NULL(svm)) {
 910				pr_err("%s: Page request for invalid PASID %d: %08llx %08llx\n",
 911				       iommu->name, req->pasid, ((unsigned long long *)req)[0],
 912				       ((unsigned long long *)req)[1]);
 913				goto no_pasid;
 914			}
 915		}
 916
 917		if (!sdev || sdev->sid != req->rid) {
 918			struct intel_svm_dev *t;
 919
 920			sdev = NULL;
 921			rcu_read_lock();
 922			list_for_each_entry_rcu(t, &svm->devs, list) {
 923				if (t->sid == req->rid) {
 924					sdev = t;
 925					break;
 926				}
 927			}
 928			rcu_read_unlock();
 929		}
 930
 931		result = QI_RESP_INVALID;
 932		/* Since we're using init_mm.pgd directly, we should never take
 933		 * any faults on kernel addresses. */
 934		if (!svm->mm)
 935			goto bad_req;
 
 936
 937		/* If address is not canonical, return invalid response */
 938		if (!is_canonical_address(address))
 
 939			goto bad_req;
 
 940
 
 
 
 
 
 
 
 941		/*
 942		 * If prq is to be handled outside iommu driver via receiver of
 943		 * the fault notifiers, we skip the page response here.
 944		 */
 945		if (svm->flags & SVM_FLAG_GUEST_MODE) {
 946			if (sdev && !intel_svm_prq_report(sdev->dev, req))
 947				goto prq_advance;
 948			else
 949				goto bad_req;
 950		}
 951
 952		/* If the mm is already defunct, don't handle faults. */
 953		if (!mmget_not_zero(svm->mm))
 954			goto bad_req;
 955
 956		mmap_read_lock(svm->mm);
 957		vma = find_extend_vma(svm->mm, address);
 958		if (!vma || address < vma->vm_start)
 959			goto invalid;
 960
 961		if (access_error(vma, req))
 962			goto invalid;
 963
 964		ret = handle_mm_fault(vma, address,
 965				      req->wr_req ? FAULT_FLAG_WRITE : 0,
 966				      NULL);
 967		if (ret & VM_FAULT_ERROR)
 968			goto invalid;
 969
 970		result = QI_RESP_SUCCESS;
 971invalid:
 972		mmap_read_unlock(svm->mm);
 973		mmput(svm->mm);
 974bad_req:
 975		WARN_ON(!sdev);
 976		if (sdev && sdev->ops && sdev->ops->fault_cb) {
 977			int rwxp = (req->rd_req << 3) | (req->wr_req << 2) |
 978				(req->exe_req << 1) | (req->pm_req);
 979			sdev->ops->fault_cb(sdev->dev, req->pasid, req->addr,
 980					    req->priv_data, rwxp, result);
 981		}
 982		/* We get here in the error case where the PASID lookup failed,
 983		   and these can be NULL. Do not use them below this point! */
 984		sdev = NULL;
 985		svm = NULL;
 986no_pasid:
 987		if (req->lpig || req->priv_data_present) {
 988			/*
 989			 * Per VT-d spec. v3.0 ch7.7, system software must
 990			 * respond with page group response if private data
 991			 * is present (PDP) or last page in group (LPIG) bit
 992			 * is set. This is an additional VT-d feature beyond
 993			 * PCI ATS spec.
 994			 */
 995			resp.qw0 = QI_PGRP_PASID(req->pasid) |
 996				QI_PGRP_DID(req->rid) |
 997				QI_PGRP_PASID_P(req->pasid_present) |
 998				QI_PGRP_PDP(req->pasid_present) |
 999				QI_PGRP_RESP_CODE(result) |
1000				QI_PGRP_RESP_TYPE;
1001			resp.qw1 = QI_PGRP_IDX(req->prg_index) |
1002				QI_PGRP_LPIG(req->lpig);
1003
1004			if (req->priv_data_present)
1005				memcpy(&resp.qw2, req->priv_data,
1006				       sizeof(req->priv_data));
1007			resp.qw2 = 0;
1008			resp.qw3 = 0;
1009			qi_submit_sync(iommu, &resp, 1, 0);
1010		}
1011prq_advance:
1012		head = (head + sizeof(*req)) & PRQ_RING_MASK;
1013	}
1014
1015	dmar_writeq(iommu->reg + DMAR_PQH_REG, tail);
1016
1017	/*
1018	 * Clear the page request overflow bit and wake up all threads that
1019	 * are waiting for the completion of this handling.
1020	 */
1021	if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO)
1022		writel(DMA_PRS_PRO, iommu->reg + DMAR_PRS_REG);
 
 
 
 
 
 
 
 
 
 
1023
1024	if (!completion_done(&iommu->prq_complete))
1025		complete(&iommu->prq_complete);
1026
1027	return IRQ_RETVAL(handled);
1028}
1029
1030#define to_intel_svm_dev(handle) container_of(handle, struct intel_svm_dev, sva)
1031struct iommu_sva *
1032intel_svm_bind(struct device *dev, struct mm_struct *mm, void *drvdata)
1033{
1034	struct iommu_sva *sva = ERR_PTR(-EINVAL);
1035	struct intel_svm_dev *sdev = NULL;
1036	int flags = 0;
1037	int ret;
1038
1039	/*
1040	 * TODO: Consolidate with generic iommu-sva bind after it is merged.
1041	 * It will require shared SVM data structures, i.e. combine io_mm
1042	 * and intel_svm etc.
1043	 */
1044	if (drvdata)
1045		flags = *(int *)drvdata;
1046	mutex_lock(&pasid_mutex);
1047	ret = intel_svm_bind_mm(dev, flags, NULL, mm, &sdev);
1048	if (ret)
1049		sva = ERR_PTR(ret);
1050	else if (sdev)
1051		sva = &sdev->sva;
1052	else
1053		WARN(!sdev, "SVM bind succeeded with no sdev!\n");
1054
1055	mutex_unlock(&pasid_mutex);
1056
1057	return sva;
1058}
1059
1060void intel_svm_unbind(struct iommu_sva *sva)
1061{
1062	struct intel_svm_dev *sdev;
1063
1064	mutex_lock(&pasid_mutex);
1065	sdev = to_intel_svm_dev(sva);
1066	intel_svm_unbind_mm(sdev->dev, sdev->pasid);
1067	mutex_unlock(&pasid_mutex);
1068}
1069
1070int intel_svm_get_pasid(struct iommu_sva *sva)
1071{
1072	struct intel_svm_dev *sdev;
1073	int pasid;
1074
1075	mutex_lock(&pasid_mutex);
1076	sdev = to_intel_svm_dev(sva);
1077	pasid = sdev->pasid;
1078	mutex_unlock(&pasid_mutex);
1079
1080	return pasid;
1081}
1082
1083int intel_svm_page_response(struct device *dev,
1084			    struct iommu_fault_event *evt,
1085			    struct iommu_page_response *msg)
1086{
1087	struct iommu_fault_page_request *prm;
1088	struct intel_svm_dev *sdev = NULL;
1089	struct intel_svm *svm = NULL;
1090	struct intel_iommu *iommu;
1091	bool private_present;
1092	bool pasid_present;
1093	bool last_page;
1094	u8 bus, devfn;
1095	int ret = 0;
1096	u16 sid;
1097
1098	if (!dev || !dev_is_pci(dev))
1099		return -ENODEV;
1100
1101	iommu = device_to_iommu(dev, &bus, &devfn);
1102	if (!iommu)
1103		return -ENODEV;
1104
1105	if (!msg || !evt)
1106		return -EINVAL;
1107
1108	mutex_lock(&pasid_mutex);
1109
1110	prm = &evt->fault.prm;
1111	sid = PCI_DEVID(bus, devfn);
1112	pasid_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
1113	private_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA;
1114	last_page = prm->flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE;
1115
1116	if (!pasid_present) {
1117		ret = -EINVAL;
1118		goto out;
1119	}
1120
1121	if (prm->pasid == 0 || prm->pasid >= PASID_MAX) {
1122		ret = -EINVAL;
1123		goto out;
1124	}
1125
1126	ret = pasid_to_svm_sdev(dev, prm->pasid, &svm, &sdev);
1127	if (ret || !sdev) {
1128		ret = -ENODEV;
1129		goto out;
1130	}
1131
1132	/*
1133	 * For responses from userspace, need to make sure that the
1134	 * pasid has been bound to its mm.
1135	 */
1136	if (svm->flags & SVM_FLAG_GUEST_MODE) {
1137		struct mm_struct *mm;
1138
1139		mm = get_task_mm(current);
1140		if (!mm) {
1141			ret = -EINVAL;
1142			goto out;
1143		}
1144
1145		if (mm != svm->mm) {
1146			ret = -ENODEV;
1147			mmput(mm);
1148			goto out;
1149		}
1150
1151		mmput(mm);
1152	}
1153
1154	/*
1155	 * Per VT-d spec. v3.0 ch7.7, system software must respond
1156	 * with page group response if private data is present (PDP)
1157	 * or last page in group (LPIG) bit is set. This is an
1158	 * additional VT-d requirement beyond PCI ATS spec.
1159	 */
1160	if (last_page || private_present) {
1161		struct qi_desc desc;
1162
1163		desc.qw0 = QI_PGRP_PASID(prm->pasid) | QI_PGRP_DID(sid) |
1164				QI_PGRP_PASID_P(pasid_present) |
1165				QI_PGRP_PDP(private_present) |
1166				QI_PGRP_RESP_CODE(msg->code) |
1167				QI_PGRP_RESP_TYPE;
1168		desc.qw1 = QI_PGRP_IDX(prm->grpid) | QI_PGRP_LPIG(last_page);
1169		desc.qw2 = 0;
1170		desc.qw3 = 0;
1171		if (private_present)
1172			memcpy(&desc.qw2, prm->private_data,
1173			       sizeof(prm->private_data));
 
 
 
 
 
1174
1175		qi_submit_sync(iommu, &desc, 1, 0);
1176	}
1177out:
 
 
 
 
 
 
 
1178	mutex_unlock(&pasid_mutex);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1179	return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1180}
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright © 2015 Intel Corporation.
  4 *
  5 * Authors: David Woodhouse <dwmw2@infradead.org>
  6 */
  7
 
  8#include <linux/mmu_notifier.h>
  9#include <linux/sched.h>
 10#include <linux/sched/mm.h>
 11#include <linux/slab.h>
 12#include <linux/intel-svm.h>
 13#include <linux/rculist.h>
 14#include <linux/pci.h>
 15#include <linux/pci-ats.h>
 16#include <linux/dmar.h>
 17#include <linux/interrupt.h>
 18#include <linux/mm_types.h>
 19#include <linux/xarray.h>
 20#include <linux/ioasid.h>
 21#include <asm/page.h>
 22#include <asm/fpu/api.h>
 23
 24#include "iommu.h"
 25#include "pasid.h"
 26#include "perf.h"
 27#include "../iommu-sva.h"
 28#include "trace.h"
 29
 30static irqreturn_t prq_event_thread(int irq, void *d);
 31static void intel_svm_drain_prq(struct device *dev, u32 pasid);
 32#define to_intel_svm_dev(handle) container_of(handle, struct intel_svm_dev, sva)
 33
 34static DEFINE_XARRAY_ALLOC(pasid_private_array);
 35static int pasid_private_add(ioasid_t pasid, void *priv)
 36{
 37	return xa_alloc(&pasid_private_array, &pasid, priv,
 38			XA_LIMIT(pasid, pasid), GFP_ATOMIC);
 39}
 40
 41static void pasid_private_remove(ioasid_t pasid)
 42{
 43	xa_erase(&pasid_private_array, pasid);
 44}
 45
 46static void *pasid_private_find(ioasid_t pasid)
 47{
 48	return xa_load(&pasid_private_array, pasid);
 49}
 50
 51static struct intel_svm_dev *
 52svm_lookup_device_by_dev(struct intel_svm *svm, struct device *dev)
 53{
 54	struct intel_svm_dev *sdev = NULL, *t;
 55
 56	rcu_read_lock();
 57	list_for_each_entry_rcu(t, &svm->devs, list) {
 58		if (t->dev == dev) {
 59			sdev = t;
 60			break;
 61		}
 62	}
 63	rcu_read_unlock();
 64
 65	return sdev;
 66}
 67
 68int intel_svm_enable_prq(struct intel_iommu *iommu)
 69{
 70	struct iopf_queue *iopfq;
 71	struct page *pages;
 72	int irq, ret;
 73
 74	pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, PRQ_ORDER);
 75	if (!pages) {
 76		pr_warn("IOMMU: %s: Failed to allocate page request queue\n",
 77			iommu->name);
 78		return -ENOMEM;
 79	}
 80	iommu->prq = page_address(pages);
 81
 82	irq = dmar_alloc_hwirq(DMAR_UNITS_SUPPORTED + iommu->seq_id, iommu->node, iommu);
 83	if (irq <= 0) {
 84		pr_err("IOMMU: %s: Failed to create IRQ vector for page request queue\n",
 85		       iommu->name);
 86		ret = -EINVAL;
 87		goto free_prq;
 
 
 
 88	}
 89	iommu->pr_irq = irq;
 90
 91	snprintf(iommu->iopfq_name, sizeof(iommu->iopfq_name),
 92		 "dmar%d-iopfq", iommu->seq_id);
 93	iopfq = iopf_queue_alloc(iommu->iopfq_name);
 94	if (!iopfq) {
 95		pr_err("IOMMU: %s: Failed to allocate iopf queue\n", iommu->name);
 96		ret = -ENOMEM;
 97		goto free_hwirq;
 98	}
 99	iommu->iopf_queue = iopfq;
100
101	snprintf(iommu->prq_name, sizeof(iommu->prq_name), "dmar%d-prq", iommu->seq_id);
102
103	ret = request_threaded_irq(irq, NULL, prq_event_thread, IRQF_ONESHOT,
104				   iommu->prq_name, iommu);
105	if (ret) {
106		pr_err("IOMMU: %s: Failed to request IRQ for page request queue\n",
107		       iommu->name);
108		goto free_iopfq;
 
 
109	}
110	dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
111	dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
112	dmar_writeq(iommu->reg + DMAR_PQA_REG, virt_to_phys(iommu->prq) | PRQ_ORDER);
113
114	init_completion(&iommu->prq_complete);
115
116	return 0;
117
118free_iopfq:
119	iopf_queue_free(iommu->iopf_queue);
120	iommu->iopf_queue = NULL;
121free_hwirq:
122	dmar_free_hwirq(irq);
123	iommu->pr_irq = 0;
124free_prq:
125	free_pages((unsigned long)iommu->prq, PRQ_ORDER);
126	iommu->prq = NULL;
127
128	return ret;
129}
130
131int intel_svm_finish_prq(struct intel_iommu *iommu)
132{
133	dmar_writeq(iommu->reg + DMAR_PQH_REG, 0ULL);
134	dmar_writeq(iommu->reg + DMAR_PQT_REG, 0ULL);
135	dmar_writeq(iommu->reg + DMAR_PQA_REG, 0ULL);
136
137	if (iommu->pr_irq) {
138		free_irq(iommu->pr_irq, iommu);
139		dmar_free_hwirq(iommu->pr_irq);
140		iommu->pr_irq = 0;
141	}
142
143	if (iommu->iopf_queue) {
144		iopf_queue_free(iommu->iopf_queue);
145		iommu->iopf_queue = NULL;
146	}
147
148	free_pages((unsigned long)iommu->prq, PRQ_ORDER);
149	iommu->prq = NULL;
150
151	return 0;
152}
153
 
 
 
 
 
154void intel_svm_check(struct intel_iommu *iommu)
155{
156	if (!pasid_supported(iommu))
157		return;
158
159	if (cpu_feature_enabled(X86_FEATURE_GBPAGES) &&
160	    !cap_fl1gp_support(iommu->cap)) {
161		pr_err("%s SVM disabled, incompatible 1GB page capability\n",
162		       iommu->name);
163		return;
164	}
165
166	if (cpu_feature_enabled(X86_FEATURE_LA57) &&
167	    !cap_fl5lp_support(iommu->cap)) {
168		pr_err("%s SVM disabled, incompatible paging mode\n",
169		       iommu->name);
170		return;
171	}
172
173	iommu->flags |= VTD_FLAG_SVM_CAPABLE;
174}
175
176static void __flush_svm_range_dev(struct intel_svm *svm,
177				  struct intel_svm_dev *sdev,
178				  unsigned long address,
179				  unsigned long pages, int ih)
180{
181	struct device_domain_info *info = dev_iommu_priv_get(sdev->dev);
182
183	if (WARN_ON(!pages))
184		return;
 
 
 
 
 
 
185
186	qi_flush_piotlb(sdev->iommu, sdev->did, svm->pasid, address, pages, ih);
187	if (info->ats_enabled) {
188		qi_flush_dev_iotlb_pasid(sdev->iommu, sdev->sid, info->pfsid,
189					 svm->pasid, sdev->qdep, address,
190					 order_base_2(pages));
191		quirk_extra_dev_tlb_flush(info, address, order_base_2(pages),
192					  svm->pasid, sdev->qdep);
193	}
194}
195
196static void intel_flush_svm_range_dev(struct intel_svm *svm,
197				      struct intel_svm_dev *sdev,
198				      unsigned long address,
199				      unsigned long pages, int ih)
200{
201	unsigned long shift = ilog2(__roundup_pow_of_two(pages));
202	unsigned long align = (1ULL << (VTD_PAGE_SHIFT + shift));
203	unsigned long start = ALIGN_DOWN(address, align);
204	unsigned long end = ALIGN(address + (pages << VTD_PAGE_SHIFT), align);
205
206	while (start < end) {
207		__flush_svm_range_dev(svm, sdev, start, align >> VTD_PAGE_SHIFT, ih);
208		start += align;
 
 
 
 
 
 
 
 
 
 
 
 
209	}
210}
211
212static void intel_flush_svm_range(struct intel_svm *svm, unsigned long address,
213				unsigned long pages, int ih)
214{
215	struct intel_svm_dev *sdev;
216
217	rcu_read_lock();
218	list_for_each_entry_rcu(sdev, &svm->devs, list)
219		intel_flush_svm_range_dev(svm, sdev, address, pages, ih);
220	rcu_read_unlock();
221}
222
223/* Pages have been freed at this point */
224static void intel_invalidate_range(struct mmu_notifier *mn,
225				   struct mm_struct *mm,
226				   unsigned long start, unsigned long end)
227{
228	struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
229
230	intel_flush_svm_range(svm, start,
231			      (end - start + PAGE_SIZE - 1) >> VTD_PAGE_SHIFT, 0);
232}
233
234static void intel_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)
235{
236	struct intel_svm *svm = container_of(mn, struct intel_svm, notifier);
237	struct intel_svm_dev *sdev;
238
239	/* This might end up being called from exit_mmap(), *before* the page
240	 * tables are cleared. And __mmu_notifier_release() will delete us from
241	 * the list of notifiers so that our invalidate_range() callback doesn't
242	 * get called when the page tables are cleared. So we need to protect
243	 * against hardware accessing those page tables.
244	 *
245	 * We do it by clearing the entry in the PASID table and then flushing
246	 * the IOTLB and the PASID table caches. This might upset hardware;
247	 * perhaps we'll want to point the PASID to a dummy PGD (like the zero
248	 * page) so that we end up taking a fault that the hardware really
249	 * *has* to handle gracefully without affecting other processes.
250	 */
251	rcu_read_lock();
252	list_for_each_entry_rcu(sdev, &svm->devs, list)
253		intel_pasid_tear_down_entry(sdev->iommu, sdev->dev,
254					    svm->pasid, true);
255	rcu_read_unlock();
256
257}
258
259static const struct mmu_notifier_ops intel_mmuops = {
260	.release = intel_mm_release,
261	.invalidate_range = intel_invalidate_range,
262};
263
264static DEFINE_MUTEX(pasid_mutex);
 
 
 
 
 
265
266static int pasid_to_svm_sdev(struct device *dev, unsigned int pasid,
267			     struct intel_svm **rsvm,
268			     struct intel_svm_dev **rsdev)
269{
270	struct intel_svm_dev *sdev = NULL;
271	struct intel_svm *svm;
272
273	/* The caller should hold the pasid_mutex lock */
274	if (WARN_ON(!mutex_is_locked(&pasid_mutex)))
275		return -EINVAL;
276
277	if (pasid == INVALID_IOASID || pasid >= PASID_MAX)
278		return -EINVAL;
279
280	svm = pasid_private_find(pasid);
281	if (IS_ERR(svm))
282		return PTR_ERR(svm);
283
284	if (!svm)
285		goto out;
286
287	/*
288	 * If we found svm for the PASID, there must be at least one device
289	 * bond.
290	 */
291	if (WARN_ON(list_empty(&svm->devs)))
292		return -EINVAL;
293	sdev = svm_lookup_device_by_dev(svm, dev);
 
 
 
 
 
 
 
 
294
295out:
296	*rsvm = svm;
297	*rsdev = sdev;
298
299	return 0;
300}
301
302static struct iommu_sva *intel_svm_bind_mm(struct intel_iommu *iommu,
303					   struct device *dev,
304					   struct mm_struct *mm)
305{
306	struct device_domain_info *info = dev_iommu_priv_get(dev);
307	struct intel_svm_dev *sdev;
308	struct intel_svm *svm;
309	unsigned long sflags;
310	int ret = 0;
311
312	svm = pasid_private_find(mm->pasid);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
313	if (!svm) {
 
314		svm = kzalloc(sizeof(*svm), GFP_KERNEL);
315		if (!svm)
316			return ERR_PTR(-ENOMEM);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317
318		svm->pasid = mm->pasid;
319		svm->mm = mm;
320		INIT_LIST_HEAD_RCU(&svm->devs);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
321
322		svm->notifier.ops = &intel_mmuops;
323		ret = mmu_notifier_register(&svm->notifier, mm);
324		if (ret) {
325			kfree(svm);
326			return ERR_PTR(ret);
 
 
 
 
 
 
 
 
327		}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328
329		ret = pasid_private_add(svm->pasid, svm);
330		if (ret) {
331			mmu_notifier_unregister(&svm->notifier, mm);
332			kfree(svm);
333			return ERR_PTR(ret);
 
 
 
 
 
 
 
 
 
 
 
 
 
334		}
335	}
336
337	/* Find the matching device in svm list */
338	sdev = svm_lookup_device_by_dev(svm, dev);
339	if (sdev) {
340		sdev->users++;
341		goto success;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
342	}
343
344	sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
345	if (!sdev) {
346		ret = -ENOMEM;
347		goto free_svm;
 
 
 
 
 
 
 
348	}
349
350	sdev->dev = dev;
351	sdev->iommu = iommu;
352	sdev->did = FLPT_DEFAULT_DID;
353	sdev->sid = PCI_DEVID(info->bus, info->devfn);
354	sdev->users = 1;
355	sdev->pasid = svm->pasid;
356	sdev->sva.dev = dev;
357	init_rcu_head(&sdev->rcu);
358	if (info->ats_enabled) {
359		sdev->dev_iotlb = 1;
360		sdev->qdep = info->ats_qdep;
361		if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS)
362			sdev->qdep = 0;
363	}
364
365	/* Setup the pasid table: */
366	sflags = cpu_feature_enabled(X86_FEATURE_LA57) ? PASID_FLAG_FL5LP : 0;
367	ret = intel_pasid_setup_first_level(iommu, dev, mm->pgd, mm->pasid,
368					    FLPT_DEFAULT_DID, sflags);
369	if (ret)
370		goto free_sdev;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
371
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
372	list_add_rcu(&sdev->list, &svm->devs);
373success:
374	return &sdev->sva;
375
376free_sdev:
377	kfree(sdev);
378free_svm:
379	if (list_empty(&svm->devs)) {
380		mmu_notifier_unregister(&svm->notifier, mm);
381		pasid_private_remove(mm->pasid);
382		kfree(svm);
383	}
384
385	return ERR_PTR(ret);
386}
387
388/* Caller must hold pasid_mutex */
389static int intel_svm_unbind_mm(struct device *dev, u32 pasid)
390{
391	struct intel_svm_dev *sdev;
392	struct intel_iommu *iommu;
393	struct intel_svm *svm;
394	struct mm_struct *mm;
395	int ret = -EINVAL;
396
397	iommu = device_to_iommu(dev, NULL, NULL);
398	if (!iommu)
399		goto out;
400
401	ret = pasid_to_svm_sdev(dev, pasid, &svm, &sdev);
402	if (ret)
403		goto out;
404	mm = svm->mm;
405
406	if (sdev) {
407		sdev->users--;
408		if (!sdev->users) {
409			list_del_rcu(&sdev->list);
410			/* Flush the PASID cache and IOTLB for this device.
411			 * Note that we do depend on the hardware *not* using
412			 * the PASID any more. Just as we depend on other
413			 * devices never using PASIDs that they have no right
414			 * to use. We have a *shared* PASID table, because it's
415			 * large and has to be physically contiguous. So it's
416			 * hard to be as defensive as we might like. */
417			intel_pasid_tear_down_entry(iommu, dev,
418						    svm->pasid, false);
419			intel_svm_drain_prq(dev, svm->pasid);
420			kfree_rcu(sdev, rcu);
421
422			if (list_empty(&svm->devs)) {
423				if (svm->notifier.ops)
424					mmu_notifier_unregister(&svm->notifier, mm);
425				pasid_private_remove(svm->pasid);
 
426				/* We mandate that no page faults may be outstanding
427				 * for the PASID when intel_svm_unbind_mm() is called.
428				 * If that is not obeyed, subtle errors will happen.
429				 * Let's make them less subtle... */
430				memset(svm, 0x6b, sizeof(*svm));
431				kfree(svm);
432			}
433		}
434	}
435out:
436	return ret;
437}
438
439/* Page request queue descriptor */
440struct page_req_dsc {
441	union {
442		struct {
443			u64 type:8;
444			u64 pasid_present:1;
445			u64 priv_data_present:1;
446			u64 rsvd:6;
447			u64 rid:16;
448			u64 pasid:20;
449			u64 exe_req:1;
450			u64 pm_req:1;
451			u64 rsvd2:10;
452		};
453		u64 qw_0;
454	};
455	union {
456		struct {
457			u64 rd_req:1;
458			u64 wr_req:1;
459			u64 lpig:1;
460			u64 prg_index:9;
461			u64 addr:52;
462		};
463		u64 qw_1;
464	};
465	u64 priv_data[2];
466};
467
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
468static bool is_canonical_address(u64 addr)
469{
470	int shift = 64 - (__VIRTUAL_MASK_SHIFT + 1);
471	long saddr = (long) addr;
472
473	return (((saddr << shift) >> shift) == saddr);
474}
475
476/**
477 * intel_svm_drain_prq - Drain page requests and responses for a pasid
478 * @dev: target device
479 * @pasid: pasid for draining
480 *
481 * Drain all pending page requests and responses related to @pasid in both
482 * software and hardware. This is supposed to be called after the device
483 * driver has stopped DMA, the pasid entry has been cleared, and both IOTLB
484 * and DevTLB have been invalidated.
485 *
486 * It waits until all pending page requests for @pasid in the page fault
487 * queue are completed by the prq handling thread. Then follow the steps
488 * described in VT-d spec CH7.10 to drain all page requests and page
489 * responses pending in the hardware.
490 */
491static void intel_svm_drain_prq(struct device *dev, u32 pasid)
492{
493	struct device_domain_info *info;
494	struct dmar_domain *domain;
495	struct intel_iommu *iommu;
496	struct qi_desc desc[3];
497	struct pci_dev *pdev;
498	int head, tail;
499	u16 sid, did;
500	int qdep;
501
502	info = dev_iommu_priv_get(dev);
503	if (WARN_ON(!info || !dev_is_pci(dev)))
504		return;
505
506	if (!info->pri_enabled)
507		return;
508
509	iommu = info->iommu;
510	domain = info->domain;
511	pdev = to_pci_dev(dev);
512	sid = PCI_DEVID(info->bus, info->devfn);
513	did = domain_id_iommu(domain, iommu);
514	qdep = pci_ats_queue_depth(pdev);
515
516	/*
517	 * Check and wait until all pending page requests in the queue are
518	 * handled by the prq handling thread.
519	 */
520prq_retry:
521	reinit_completion(&iommu->prq_complete);
522	tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
523	head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
524	while (head != tail) {
525		struct page_req_dsc *req;
526
527		req = &iommu->prq[head / sizeof(*req)];
528		if (!req->pasid_present || req->pasid != pasid) {
529			head = (head + sizeof(*req)) & PRQ_RING_MASK;
530			continue;
531		}
532
533		wait_for_completion(&iommu->prq_complete);
534		goto prq_retry;
535	}
536
537	/*
538	 * A work in IO page fault workqueue may try to lock pasid_mutex now.
539	 * Holding pasid_mutex while waiting in iopf_queue_flush_dev() for
540	 * all works in the workqueue to finish may cause deadlock.
541	 *
542	 * It's unnecessary to hold pasid_mutex in iopf_queue_flush_dev().
543	 * Unlock it to allow the works to be handled while waiting for
544	 * them to finish.
545	 */
546	lockdep_assert_held(&pasid_mutex);
547	mutex_unlock(&pasid_mutex);
548	iopf_queue_flush_dev(dev);
549	mutex_lock(&pasid_mutex);
550
551	/*
552	 * Perform steps described in VT-d spec CH7.10 to drain page
553	 * requests and responses in hardware.
554	 */
555	memset(desc, 0, sizeof(desc));
556	desc[0].qw0 = QI_IWD_STATUS_DATA(QI_DONE) |
557			QI_IWD_FENCE |
558			QI_IWD_TYPE;
559	desc[1].qw0 = QI_EIOTLB_PASID(pasid) |
560			QI_EIOTLB_DID(did) |
561			QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) |
562			QI_EIOTLB_TYPE;
563	desc[2].qw0 = QI_DEV_EIOTLB_PASID(pasid) |
564			QI_DEV_EIOTLB_SID(sid) |
565			QI_DEV_EIOTLB_QDEP(qdep) |
566			QI_DEIOTLB_TYPE |
567			QI_DEV_IOTLB_PFSID(info->pfsid);
568qi_retry:
569	reinit_completion(&iommu->prq_complete);
570	qi_submit_sync(iommu, desc, 3, QI_OPT_WAIT_DRAIN);
571	if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO) {
572		wait_for_completion(&iommu->prq_complete);
573		goto qi_retry;
574	}
575}
576
577static int prq_to_iommu_prot(struct page_req_dsc *req)
578{
579	int prot = 0;
580
581	if (req->rd_req)
582		prot |= IOMMU_FAULT_PERM_READ;
583	if (req->wr_req)
584		prot |= IOMMU_FAULT_PERM_WRITE;
585	if (req->exe_req)
586		prot |= IOMMU_FAULT_PERM_EXEC;
587	if (req->pm_req)
588		prot |= IOMMU_FAULT_PERM_PRIV;
589
590	return prot;
591}
592
593static int intel_svm_prq_report(struct intel_iommu *iommu, struct device *dev,
594				struct page_req_dsc *desc)
595{
596	struct iommu_fault_event event;
597
598	if (!dev || !dev_is_pci(dev))
599		return -ENODEV;
600
601	/* Fill in event data for device specific processing */
602	memset(&event, 0, sizeof(struct iommu_fault_event));
603	event.fault.type = IOMMU_FAULT_PAGE_REQ;
604	event.fault.prm.addr = (u64)desc->addr << VTD_PAGE_SHIFT;
605	event.fault.prm.pasid = desc->pasid;
606	event.fault.prm.grpid = desc->prg_index;
607	event.fault.prm.perm = prq_to_iommu_prot(desc);
608
609	if (desc->lpig)
610		event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE;
611	if (desc->pasid_present) {
612		event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
613		event.fault.prm.flags |= IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID;
614	}
615	if (desc->priv_data_present) {
616		/*
617		 * Set last page in group bit if private data is present,
618		 * page response is required as it does for LPIG.
619		 * iommu_report_device_fault() doesn't understand this vendor
620		 * specific requirement thus we set last_page as a workaround.
621		 */
622		event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE;
623		event.fault.prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA;
624		event.fault.prm.private_data[0] = desc->priv_data[0];
625		event.fault.prm.private_data[1] = desc->priv_data[1];
626	} else if (dmar_latency_enabled(iommu, DMAR_LATENCY_PRQ)) {
627		/*
628		 * If the private data fields are not used by hardware, use it
629		 * to monitor the prq handle latency.
630		 */
631		event.fault.prm.private_data[0] = ktime_to_ns(ktime_get());
632	}
633
634	return iommu_report_device_fault(dev, &event);
635}
636
637static void handle_bad_prq_event(struct intel_iommu *iommu,
638				 struct page_req_dsc *req, int result)
639{
640	struct qi_desc desc;
641
642	pr_err("%s: Invalid page request: %08llx %08llx\n",
643	       iommu->name, ((unsigned long long *)req)[0],
644	       ((unsigned long long *)req)[1]);
645
646	/*
647	 * Per VT-d spec. v3.0 ch7.7, system software must
648	 * respond with page group response if private data
649	 * is present (PDP) or last page in group (LPIG) bit
650	 * is set. This is an additional VT-d feature beyond
651	 * PCI ATS spec.
652	 */
653	if (!req->lpig && !req->priv_data_present)
654		return;
655
656	desc.qw0 = QI_PGRP_PASID(req->pasid) |
657			QI_PGRP_DID(req->rid) |
658			QI_PGRP_PASID_P(req->pasid_present) |
659			QI_PGRP_PDP(req->priv_data_present) |
660			QI_PGRP_RESP_CODE(result) |
661			QI_PGRP_RESP_TYPE;
662	desc.qw1 = QI_PGRP_IDX(req->prg_index) |
663			QI_PGRP_LPIG(req->lpig);
664
665	if (req->priv_data_present) {
666		desc.qw2 = req->priv_data[0];
667		desc.qw3 = req->priv_data[1];
668	} else {
669		desc.qw2 = 0;
670		desc.qw3 = 0;
671	}
672
673	qi_submit_sync(iommu, &desc, 1, 0);
674}
675
676static irqreturn_t prq_event_thread(int irq, void *d)
677{
 
678	struct intel_iommu *iommu = d;
679	struct page_req_dsc *req;
680	int head, tail, handled;
681	struct pci_dev *pdev;
682	u64 address;
683
684	/*
685	 * Clear PPR bit before reading head/tail registers, to ensure that
686	 * we get a new interrupt if needed.
687	 */
688	writel(DMA_PRS_PPR, iommu->reg + DMAR_PRS_REG);
689
690	tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
691	head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
692	handled = (head != tail);
693	while (head != tail) {
 
 
 
 
 
 
 
 
 
694		req = &iommu->prq[head / sizeof(*req)];
 
 
695		address = (u64)req->addr << VTD_PAGE_SHIFT;
 
 
 
 
 
 
696
697		if (unlikely(!req->pasid_present)) {
698			pr_err("IOMMU: %s: Page request without PASID\n",
699			       iommu->name);
700bad_req:
701			handle_bad_prq_event(iommu, req, QI_RESP_INVALID);
702			goto prq_advance;
 
 
 
 
 
 
 
703		}
704
705		if (unlikely(!is_canonical_address(address))) {
706			pr_err("IOMMU: %s: Address is not canonical\n",
707			       iommu->name);
708			goto bad_req;
 
 
 
 
 
 
 
 
709		}
710
711		if (unlikely(req->pm_req && (req->rd_req | req->wr_req))) {
712			pr_err("IOMMU: %s: Page request in Privilege Mode\n",
713			       iommu->name);
 
714			goto bad_req;
715		}
716
717		if (unlikely(req->exe_req && req->rd_req)) {
718			pr_err("IOMMU: %s: Execution request not supported\n",
719			       iommu->name);
720			goto bad_req;
721		}
722
723		/* Drop Stop Marker message. No need for a response. */
724		if (unlikely(req->lpig && !req->rd_req && !req->wr_req))
725			goto prq_advance;
726
727		pdev = pci_get_domain_bus_and_slot(iommu->segment,
728						   PCI_BUS_NUM(req->rid),
729						   req->rid & 0xff);
730		/*
731		 * If prq is to be handled outside iommu driver via receiver of
732		 * the fault notifiers, we skip the page response here.
733		 */
734		if (!pdev)
 
 
 
 
 
 
 
 
735			goto bad_req;
736
737		if (intel_svm_prq_report(iommu, &pdev->dev, req))
738			handle_bad_prq_event(iommu, req, QI_RESP_INVALID);
739		else
740			trace_prq_report(iommu, &pdev->dev, req->qw_0, req->qw_1,
741					 req->priv_data[0], req->priv_data[1],
742					 iommu->prq_seq_number++);
743		pci_dev_put(pdev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
744prq_advance:
745		head = (head + sizeof(*req)) & PRQ_RING_MASK;
746	}
747
748	dmar_writeq(iommu->reg + DMAR_PQH_REG, tail);
749
750	/*
751	 * Clear the page request overflow bit and wake up all threads that
752	 * are waiting for the completion of this handling.
753	 */
754	if (readl(iommu->reg + DMAR_PRS_REG) & DMA_PRS_PRO) {
755		pr_info_ratelimited("IOMMU: %s: PRQ overflow detected\n",
756				    iommu->name);
757		head = dmar_readq(iommu->reg + DMAR_PQH_REG) & PRQ_RING_MASK;
758		tail = dmar_readq(iommu->reg + DMAR_PQT_REG) & PRQ_RING_MASK;
759		if (head == tail) {
760			iopf_queue_discard_partial(iommu->iopf_queue);
761			writel(DMA_PRS_PRO, iommu->reg + DMAR_PRS_REG);
762			pr_info_ratelimited("IOMMU: %s: PRQ overflow cleared",
763					    iommu->name);
764		}
765	}
766
767	if (!completion_done(&iommu->prq_complete))
768		complete(&iommu->prq_complete);
769
770	return IRQ_RETVAL(handled);
771}
772
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
773int intel_svm_page_response(struct device *dev,
774			    struct iommu_fault_event *evt,
775			    struct iommu_page_response *msg)
776{
777	struct iommu_fault_page_request *prm;
 
 
778	struct intel_iommu *iommu;
779	bool private_present;
780	bool pasid_present;
781	bool last_page;
782	u8 bus, devfn;
783	int ret = 0;
784	u16 sid;
785
786	if (!dev || !dev_is_pci(dev))
787		return -ENODEV;
788
789	iommu = device_to_iommu(dev, &bus, &devfn);
790	if (!iommu)
791		return -ENODEV;
792
793	if (!msg || !evt)
794		return -EINVAL;
795
 
 
796	prm = &evt->fault.prm;
797	sid = PCI_DEVID(bus, devfn);
798	pasid_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
799	private_present = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA;
800	last_page = prm->flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE;
801
802	if (!pasid_present) {
803		ret = -EINVAL;
804		goto out;
805	}
806
807	if (prm->pasid == 0 || prm->pasid >= PASID_MAX) {
808		ret = -EINVAL;
809		goto out;
810	}
811
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
812	/*
813	 * Per VT-d spec. v3.0 ch7.7, system software must respond
814	 * with page group response if private data is present (PDP)
815	 * or last page in group (LPIG) bit is set. This is an
816	 * additional VT-d requirement beyond PCI ATS spec.
817	 */
818	if (last_page || private_present) {
819		struct qi_desc desc;
820
821		desc.qw0 = QI_PGRP_PASID(prm->pasid) | QI_PGRP_DID(sid) |
822				QI_PGRP_PASID_P(pasid_present) |
823				QI_PGRP_PDP(private_present) |
824				QI_PGRP_RESP_CODE(msg->code) |
825				QI_PGRP_RESP_TYPE;
826		desc.qw1 = QI_PGRP_IDX(prm->grpid) | QI_PGRP_LPIG(last_page);
827		desc.qw2 = 0;
828		desc.qw3 = 0;
829
830		if (private_present) {
831			desc.qw2 = prm->private_data[0];
832			desc.qw3 = prm->private_data[1];
833		} else if (prm->private_data[0]) {
834			dmar_latency_update(iommu, DMAR_LATENCY_PRQ,
835				ktime_to_ns(ktime_get()) - prm->private_data[0]);
836		}
837
838		qi_submit_sync(iommu, &desc, 1, 0);
839	}
840out:
841	return ret;
842}
843
844void intel_svm_remove_dev_pasid(struct device *dev, ioasid_t pasid)
845{
846	mutex_lock(&pasid_mutex);
847	intel_svm_unbind_mm(dev, pasid);
848	mutex_unlock(&pasid_mutex);
849}
850
851static int intel_svm_set_dev_pasid(struct iommu_domain *domain,
852				   struct device *dev, ioasid_t pasid)
853{
854	struct device_domain_info *info = dev_iommu_priv_get(dev);
855	struct intel_iommu *iommu = info->iommu;
856	struct mm_struct *mm = domain->mm;
857	struct iommu_sva *sva;
858	int ret = 0;
859
860	mutex_lock(&pasid_mutex);
861	sva = intel_svm_bind_mm(iommu, dev, mm);
862	if (IS_ERR(sva))
863		ret = PTR_ERR(sva);
864	mutex_unlock(&pasid_mutex);
865
866	return ret;
867}
868
869static void intel_svm_domain_free(struct iommu_domain *domain)
870{
871	kfree(to_dmar_domain(domain));
872}
873
874static const struct iommu_domain_ops intel_svm_domain_ops = {
875	.set_dev_pasid		= intel_svm_set_dev_pasid,
876	.free			= intel_svm_domain_free
877};
878
879struct iommu_domain *intel_svm_domain_alloc(void)
880{
881	struct dmar_domain *domain;
882
883	domain = kzalloc(sizeof(*domain), GFP_KERNEL);
884	if (!domain)
885		return NULL;
886	domain->domain.ops = &intel_svm_domain_ops;
887
888	return &domain->domain;
889}