iommu/vt-d: Refactor device_to_iommu() helper

It is refactored in two ways:

- Make it global so that it could be used in other files.

- Make bus/devfn optional so that callers could ignore these two returned
values when they only want to get the coresponding iommu pointer.

Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Link: https://lore.kernel.org/r/20200724014925.15523-9-baolu.lu@linux.intel.com
Signed-off-by: Joerg Roedel <jroedel@suse.de>
This commit is contained in:
Lu Baolu 2020-07-24 09:49:21 +08:00 committed by Joerg Roedel
parent d315e9e684
commit dd6692f1b8
3 changed files with 21 additions and 45 deletions

View file

@ -778,16 +778,16 @@ is_downstream_to_pci_bridge(struct device *dev, struct device *bridge)
return false; return false;
} }
static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn) struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn)
{ {
struct dmar_drhd_unit *drhd = NULL; struct dmar_drhd_unit *drhd = NULL;
struct pci_dev *pdev = NULL;
struct intel_iommu *iommu; struct intel_iommu *iommu;
struct device *tmp; struct device *tmp;
struct pci_dev *pdev = NULL;
u16 segment = 0; u16 segment = 0;
int i; int i;
if (iommu_dummy(dev)) if (!dev || iommu_dummy(dev))
return NULL; return NULL;
if (dev_is_pci(dev)) { if (dev_is_pci(dev)) {
@ -818,8 +818,10 @@ static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devf
if (pdev && pdev->is_virtfn) if (pdev && pdev->is_virtfn)
goto got_pdev; goto got_pdev;
*bus = drhd->devices[i].bus; if (bus && devfn) {
*devfn = drhd->devices[i].devfn; *bus = drhd->devices[i].bus;
*devfn = drhd->devices[i].devfn;
}
goto out; goto out;
} }
@ -829,8 +831,10 @@ static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devf
if (pdev && drhd->include_all) { if (pdev && drhd->include_all) {
got_pdev: got_pdev:
*bus = pdev->bus->number; if (bus && devfn) {
*devfn = pdev->devfn; *bus = pdev->bus->number;
*devfn = pdev->devfn;
}
goto out; goto out;
} }
} }
@ -5146,11 +5150,10 @@ static int aux_domain_add_dev(struct dmar_domain *domain,
struct device *dev) struct device *dev)
{ {
int ret; int ret;
u8 bus, devfn;
unsigned long flags; unsigned long flags;
struct intel_iommu *iommu; struct intel_iommu *iommu;
iommu = device_to_iommu(dev, &bus, &devfn); iommu = device_to_iommu(dev, NULL, NULL);
if (!iommu) if (!iommu)
return -ENODEV; return -ENODEV;
@ -5236,9 +5239,8 @@ static int prepare_domain_attach_device(struct iommu_domain *domain,
struct dmar_domain *dmar_domain = to_dmar_domain(domain); struct dmar_domain *dmar_domain = to_dmar_domain(domain);
struct intel_iommu *iommu; struct intel_iommu *iommu;
int addr_width; int addr_width;
u8 bus, devfn;
iommu = device_to_iommu(dev, &bus, &devfn); iommu = device_to_iommu(dev, NULL, NULL);
if (!iommu) if (!iommu)
return -ENODEV; return -ENODEV;
@ -5668,9 +5670,8 @@ static bool intel_iommu_capable(enum iommu_cap cap)
static struct iommu_device *intel_iommu_probe_device(struct device *dev) static struct iommu_device *intel_iommu_probe_device(struct device *dev)
{ {
struct intel_iommu *iommu; struct intel_iommu *iommu;
u8 bus, devfn;
iommu = device_to_iommu(dev, &bus, &devfn); iommu = device_to_iommu(dev, NULL, NULL);
if (!iommu) if (!iommu)
return ERR_PTR(-ENODEV); return ERR_PTR(-ENODEV);
@ -5683,9 +5684,8 @@ static struct iommu_device *intel_iommu_probe_device(struct device *dev)
static void intel_iommu_release_device(struct device *dev) static void intel_iommu_release_device(struct device *dev)
{ {
struct intel_iommu *iommu; struct intel_iommu *iommu;
u8 bus, devfn;
iommu = device_to_iommu(dev, &bus, &devfn); iommu = device_to_iommu(dev, NULL, NULL);
if (!iommu) if (!iommu)
return; return;
@ -5835,37 +5835,14 @@ static struct iommu_group *intel_iommu_device_group(struct device *dev)
return generic_device_group(dev); return generic_device_group(dev);
} }
#ifdef CONFIG_INTEL_IOMMU_SVM
struct intel_iommu *intel_svm_device_to_iommu(struct device *dev)
{
struct intel_iommu *iommu;
u8 bus, devfn;
if (iommu_dummy(dev)) {
dev_warn(dev,
"No IOMMU translation for device; cannot enable SVM\n");
return NULL;
}
iommu = device_to_iommu(dev, &bus, &devfn);
if ((!iommu)) {
dev_err(dev, "No IOMMU for device; cannot enable SVM\n");
return NULL;
}
return iommu;
}
#endif /* CONFIG_INTEL_IOMMU_SVM */
static int intel_iommu_enable_auxd(struct device *dev) static int intel_iommu_enable_auxd(struct device *dev)
{ {
struct device_domain_info *info; struct device_domain_info *info;
struct intel_iommu *iommu; struct intel_iommu *iommu;
unsigned long flags; unsigned long flags;
u8 bus, devfn;
int ret; int ret;
iommu = device_to_iommu(dev, &bus, &devfn); iommu = device_to_iommu(dev, NULL, NULL);
if (!iommu || dmar_disabled) if (!iommu || dmar_disabled)
return -EINVAL; return -EINVAL;

View file

@ -231,7 +231,7 @@ static LIST_HEAD(global_svm_list);
int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev, int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,
struct iommu_gpasid_bind_data *data) struct iommu_gpasid_bind_data *data)
{ {
struct intel_iommu *iommu = intel_svm_device_to_iommu(dev); struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL);
struct dmar_domain *dmar_domain; struct dmar_domain *dmar_domain;
struct intel_svm_dev *sdev; struct intel_svm_dev *sdev;
struct intel_svm *svm; struct intel_svm *svm;
@ -369,7 +369,7 @@ int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,
int intel_svm_unbind_gpasid(struct device *dev, int pasid) int intel_svm_unbind_gpasid(struct device *dev, int pasid)
{ {
struct intel_iommu *iommu = intel_svm_device_to_iommu(dev); struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL);
struct intel_svm_dev *sdev; struct intel_svm_dev *sdev;
struct intel_svm *svm; struct intel_svm *svm;
int ret = -EINVAL; int ret = -EINVAL;
@ -426,7 +426,7 @@ static int
intel_svm_bind_mm(struct device *dev, int flags, struct svm_dev_ops *ops, intel_svm_bind_mm(struct device *dev, int flags, struct svm_dev_ops *ops,
struct mm_struct *mm, struct intel_svm_dev **sd) struct mm_struct *mm, struct intel_svm_dev **sd)
{ {
struct intel_iommu *iommu = intel_svm_device_to_iommu(dev); struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL);
struct device_domain_info *info; struct device_domain_info *info;
struct intel_svm_dev *sdev; struct intel_svm_dev *sdev;
struct intel_svm *svm = NULL; struct intel_svm *svm = NULL;
@ -604,7 +604,7 @@ static int intel_svm_unbind_mm(struct device *dev, int pasid)
struct intel_svm *svm; struct intel_svm *svm;
int ret = -EINVAL; int ret = -EINVAL;
iommu = intel_svm_device_to_iommu(dev); iommu = device_to_iommu(dev, NULL, NULL);
if (!iommu) if (!iommu)
goto out; goto out;

View file

@ -727,6 +727,7 @@ void iommu_flush_write_buffer(struct intel_iommu *iommu);
int intel_iommu_enable_pasid(struct intel_iommu *iommu, struct device *dev); int intel_iommu_enable_pasid(struct intel_iommu *iommu, struct device *dev);
struct dmar_domain *find_domain(struct device *dev); struct dmar_domain *find_domain(struct device *dev);
struct device_domain_info *get_domain_info(struct device *dev); struct device_domain_info *get_domain_info(struct device *dev);
struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn);
#ifdef CONFIG_INTEL_IOMMU_SVM #ifdef CONFIG_INTEL_IOMMU_SVM
extern void intel_svm_check(struct intel_iommu *iommu); extern void intel_svm_check(struct intel_iommu *iommu);
@ -765,8 +766,6 @@ struct intel_svm {
struct list_head devs; struct list_head devs;
struct list_head list; struct list_head list;
}; };
extern struct intel_iommu *intel_svm_device_to_iommu(struct device *dev);
#else #else
static inline void intel_svm_check(struct intel_iommu *iommu) {} static inline void intel_svm_check(struct intel_iommu *iommu) {}
#endif #endif