Skip to content

Commit dd6692f

Browse files
LuBaolujoergroedel
authored andcommitted
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 <[email protected]> Reviewed-by: Kevin Tian <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Joerg Roedel <[email protected]>
1 parent d315e9e commit dd6692f

File tree

3 files changed

+21
-45
lines changed

3 files changed

+21
-45
lines changed

drivers/iommu/intel/iommu.c

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -778,16 +778,16 @@ is_downstream_to_pci_bridge(struct device *dev, struct device *bridge)
778778
return false;
779779
}
780780

781-
static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn)
781+
struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn)
782782
{
783783
struct dmar_drhd_unit *drhd = NULL;
784+
struct pci_dev *pdev = NULL;
784785
struct intel_iommu *iommu;
785786
struct device *tmp;
786-
struct pci_dev *pdev = NULL;
787787
u16 segment = 0;
788788
int i;
789789

790-
if (iommu_dummy(dev))
790+
if (!dev || iommu_dummy(dev))
791791
return NULL;
792792

793793
if (dev_is_pci(dev)) {
@@ -818,8 +818,10 @@ static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devf
818818
if (pdev && pdev->is_virtfn)
819819
goto got_pdev;
820820

821-
*bus = drhd->devices[i].bus;
822-
*devfn = drhd->devices[i].devfn;
821+
if (bus && devfn) {
822+
*bus = drhd->devices[i].bus;
823+
*devfn = drhd->devices[i].devfn;
824+
}
823825
goto out;
824826
}
825827

@@ -829,8 +831,10 @@ static struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devf
829831

830832
if (pdev && drhd->include_all) {
831833
got_pdev:
832-
*bus = pdev->bus->number;
833-
*devfn = pdev->devfn;
834+
if (bus && devfn) {
835+
*bus = pdev->bus->number;
836+
*devfn = pdev->devfn;
837+
}
834838
goto out;
835839
}
836840
}
@@ -5146,11 +5150,10 @@ static int aux_domain_add_dev(struct dmar_domain *domain,
51465150
struct device *dev)
51475151
{
51485152
int ret;
5149-
u8 bus, devfn;
51505153
unsigned long flags;
51515154
struct intel_iommu *iommu;
51525155

5153-
iommu = device_to_iommu(dev, &bus, &devfn);
5156+
iommu = device_to_iommu(dev, NULL, NULL);
51545157
if (!iommu)
51555158
return -ENODEV;
51565159

@@ -5236,9 +5239,8 @@ static int prepare_domain_attach_device(struct iommu_domain *domain,
52365239
struct dmar_domain *dmar_domain = to_dmar_domain(domain);
52375240
struct intel_iommu *iommu;
52385241
int addr_width;
5239-
u8 bus, devfn;
52405242

5241-
iommu = device_to_iommu(dev, &bus, &devfn);
5243+
iommu = device_to_iommu(dev, NULL, NULL);
52425244
if (!iommu)
52435245
return -ENODEV;
52445246

@@ -5668,9 +5670,8 @@ static bool intel_iommu_capable(enum iommu_cap cap)
56685670
static struct iommu_device *intel_iommu_probe_device(struct device *dev)
56695671
{
56705672
struct intel_iommu *iommu;
5671-
u8 bus, devfn;
56725673

5673-
iommu = device_to_iommu(dev, &bus, &devfn);
5674+
iommu = device_to_iommu(dev, NULL, NULL);
56745675
if (!iommu)
56755676
return ERR_PTR(-ENODEV);
56765677

@@ -5683,9 +5684,8 @@ static struct iommu_device *intel_iommu_probe_device(struct device *dev)
56835684
static void intel_iommu_release_device(struct device *dev)
56845685
{
56855686
struct intel_iommu *iommu;
5686-
u8 bus, devfn;
56875687

5688-
iommu = device_to_iommu(dev, &bus, &devfn);
5688+
iommu = device_to_iommu(dev, NULL, NULL);
56895689
if (!iommu)
56905690
return;
56915691

@@ -5835,37 +5835,14 @@ static struct iommu_group *intel_iommu_device_group(struct device *dev)
58355835
return generic_device_group(dev);
58365836
}
58375837

5838-
#ifdef CONFIG_INTEL_IOMMU_SVM
5839-
struct intel_iommu *intel_svm_device_to_iommu(struct device *dev)
5840-
{
5841-
struct intel_iommu *iommu;
5842-
u8 bus, devfn;
5843-
5844-
if (iommu_dummy(dev)) {
5845-
dev_warn(dev,
5846-
"No IOMMU translation for device; cannot enable SVM\n");
5847-
return NULL;
5848-
}
5849-
5850-
iommu = device_to_iommu(dev, &bus, &devfn);
5851-
if ((!iommu)) {
5852-
dev_err(dev, "No IOMMU for device; cannot enable SVM\n");
5853-
return NULL;
5854-
}
5855-
5856-
return iommu;
5857-
}
5858-
#endif /* CONFIG_INTEL_IOMMU_SVM */
5859-
58605838
static int intel_iommu_enable_auxd(struct device *dev)
58615839
{
58625840
struct device_domain_info *info;
58635841
struct intel_iommu *iommu;
58645842
unsigned long flags;
5865-
u8 bus, devfn;
58665843
int ret;
58675844

5868-
iommu = device_to_iommu(dev, &bus, &devfn);
5845+
iommu = device_to_iommu(dev, NULL, NULL);
58695846
if (!iommu || dmar_disabled)
58705847
return -EINVAL;
58715848

drivers/iommu/intel/svm.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ static LIST_HEAD(global_svm_list);
231231
int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,
232232
struct iommu_gpasid_bind_data *data)
233233
{
234-
struct intel_iommu *iommu = intel_svm_device_to_iommu(dev);
234+
struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL);
235235
struct dmar_domain *dmar_domain;
236236
struct intel_svm_dev *sdev;
237237
struct intel_svm *svm;
@@ -369,7 +369,7 @@ int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev,
369369

370370
int intel_svm_unbind_gpasid(struct device *dev, int pasid)
371371
{
372-
struct intel_iommu *iommu = intel_svm_device_to_iommu(dev);
372+
struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL);
373373
struct intel_svm_dev *sdev;
374374
struct intel_svm *svm;
375375
int ret = -EINVAL;
@@ -426,7 +426,7 @@ static int
426426
intel_svm_bind_mm(struct device *dev, int flags, struct svm_dev_ops *ops,
427427
struct mm_struct *mm, struct intel_svm_dev **sd)
428428
{
429-
struct intel_iommu *iommu = intel_svm_device_to_iommu(dev);
429+
struct intel_iommu *iommu = device_to_iommu(dev, NULL, NULL);
430430
struct device_domain_info *info;
431431
struct intel_svm_dev *sdev;
432432
struct intel_svm *svm = NULL;
@@ -604,7 +604,7 @@ static int intel_svm_unbind_mm(struct device *dev, int pasid)
604604
struct intel_svm *svm;
605605
int ret = -EINVAL;
606606

607-
iommu = intel_svm_device_to_iommu(dev);
607+
iommu = device_to_iommu(dev, NULL, NULL);
608608
if (!iommu)
609609
goto out;
610610

include/linux/intel-iommu.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,7 @@ void iommu_flush_write_buffer(struct intel_iommu *iommu);
727727
int intel_iommu_enable_pasid(struct intel_iommu *iommu, struct device *dev);
728728
struct dmar_domain *find_domain(struct device *dev);
729729
struct device_domain_info *get_domain_info(struct device *dev);
730+
struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn);
730731

731732
#ifdef CONFIG_INTEL_IOMMU_SVM
732733
extern void intel_svm_check(struct intel_iommu *iommu);
@@ -765,8 +766,6 @@ struct intel_svm {
765766
struct list_head devs;
766767
struct list_head list;
767768
};
768-
769-
extern struct intel_iommu *intel_svm_device_to_iommu(struct device *dev);
770769
#else
771770
static inline void intel_svm_check(struct intel_iommu *iommu) {}
772771
#endif

0 commit comments

Comments
 (0)