1/* SPDX-License-Identifier: GPL-2.0-only */ 2/* 3 * Copyright © 2015 Intel Corporation. 4 * 5 * Authors: David Woodhouse <David.Woodhouse@intel.com> 6 */ 7 8#ifndef __INTEL_SVM_H__ 9#define __INTEL_SVM_H__ 10 11struct device; 12 13struct svm_dev_ops { 14 void (*fault_cb)(struct device *dev, int pasid, u64 address, 15 void *private, int rwxp, int response); 16}; 17 18/* Values for rxwp in fault_cb callback */ 19#define SVM_REQ_READ (1<<3) 20#define SVM_REQ_WRITE (1<<2) 21#define SVM_REQ_EXEC (1<<1) 22#define SVM_REQ_PRIV (1<<0) 23 24 25/* 26 * The SVM_FLAG_PRIVATE_PASID flag requests a PASID which is *not* the "main" 27 * PASID for the current process. Even if a PASID already exists, a new one 28 * will be allocated. And the PASID allocated with SVM_FLAG_PRIVATE_PASID 29 * will not be given to subsequent callers. This facility allows a driver to 30 * disambiguate between multiple device contexts which access the same MM, 31 * if there is no other way to do so. It should be used sparingly, if at all. 32 */ 33#define SVM_FLAG_PRIVATE_PASID (1<<0) 34 35/* 36 * The SVM_FLAG_SUPERVISOR_MODE flag requests a PASID which can be used only 37 * for access to kernel addresses. No IOTLB flushes are automatically done 38 * for kernel mappings; it is valid only for access to the kernel's static 39 * 1:1 mapping of physical memory — not to vmalloc or even module mappings. 40 * A future API addition may permit the use of such ranges, by means of an 41 * explicit IOTLB flush call (akin to the DMA API's unmap method). 42 * 43 * It is unlikely that we will ever hook into flush_tlb_kernel_range() to 44 * do such IOTLB flushes automatically. 45 */ 46#define SVM_FLAG_SUPERVISOR_MODE (1<<1) 47 48#ifdef CONFIG_INTEL_IOMMU_SVM 49 50/** 51 * intel_svm_bind_mm() - Bind the current process to a PASID 52 * @dev: Device to be granted acccess 53 * @pasid: Address for allocated PASID 54 * @flags: Flags. Later for requesting supervisor mode, etc. 55 * @ops: Callbacks to device driver 56 * 57 * This function attempts to enable PASID support for the given device. 58 * If the @pasid argument is non-%NULL, a PASID is allocated for access 59 * to the MM of the current process. 60 * 61 * By using a %NULL value for the @pasid argument, this function can 62 * be used to simply validate that PASID support is available for the 63 * given device — i.e. that it is behind an IOMMU which has the 64 * requisite support, and is enabled. 65 * 66 * Page faults are handled transparently by the IOMMU code, and there 67 * should be no need for the device driver to be involved. If a page 68 * fault cannot be handled (i.e. is an invalid address rather than 69 * just needs paging in), then the page request will be completed by 70 * the core IOMMU code with appropriate status, and the device itself 71 * can then report the resulting fault to its driver via whatever 72 * mechanism is appropriate. 73 * 74 * Multiple calls from the same process may result in the same PASID 75 * being re-used. A reference count is kept. 76 */ 77extern int intel_svm_bind_mm(struct device *dev, int *pasid, int flags, 78 struct svm_dev_ops *ops); 79 80/** 81 * intel_svm_unbind_mm() - Unbind a specified PASID 82 * @dev: Device for which PASID was allocated 83 * @pasid: PASID value to be unbound 84 * 85 * This function allows a PASID to be retired when the device no 86 * longer requires access to the address space of a given process. 87 * 88 * If the use count for the PASID in question reaches zero, the 89 * PASID is revoked and may no longer be used by hardware. 90 * 91 * Device drivers are required to ensure that no access (including 92 * page requests) is currently outstanding for the PASID in question, 93 * before calling this function. 94 */ 95extern int intel_svm_unbind_mm(struct device *dev, int pasid); 96 97/** 98 * intel_svm_is_pasid_valid() - check if pasid is valid 99 * @dev: Device for which PASID was allocated 100 * @pasid: PASID value to be checked 101 * 102 * This function checks if the specified pasid is still valid. A 103 * valid pasid means the backing mm is still having a valid user. 104 * For kernel callers init_mm is always valid. for other mm, if mm->mm_users 105 * is non-zero, it is valid. 106 * 107 * returns -EINVAL if invalid pasid, 0 if pasid ref count is invalid 108 * 1 if pasid is valid. 109 */ 110extern int intel_svm_is_pasid_valid(struct device *dev, int pasid); 111 112#else /* CONFIG_INTEL_IOMMU_SVM */ 113 114static inline int intel_svm_bind_mm(struct device *dev, int *pasid, 115 int flags, struct svm_dev_ops *ops) 116{ 117 return -ENOSYS; 118} 119 120static inline int intel_svm_unbind_mm(struct device *dev, int pasid) 121{ 122 BUG(); 123} 124 125static int intel_svm_is_pasid_valid(struct device *dev, int pasid) 126{ 127 return -EINVAL; 128} 129#endif /* CONFIG_INTEL_IOMMU_SVM */ 130 131#define intel_svm_available(dev) (!intel_svm_bind_mm((dev), NULL, 0, NULL)) 132 133#endif /* __INTEL_SVM_H__ */ 134