@@ -93,8 +93,9 @@ static const char * const iommu_group_resv_type_string[] = {
93
93
static int iommu_bus_notifier (struct notifier_block * nb ,
94
94
unsigned long action , void * data );
95
95
static void iommu_release_device (struct device * dev );
96
- static int iommu_alloc_default_domain (struct iommu_group * group ,
97
- struct device * dev );
96
+ static struct iommu_domain *
97
+ iommu_group_alloc_default_domain (struct iommu_group * group , int req_type );
98
+ static int iommu_get_def_domain_type (struct device * dev );
98
99
static struct iommu_domain * __iommu_domain_alloc (const struct bus_type * bus ,
99
100
unsigned type );
100
101
static int __iommu_attach_device (struct iommu_domain * domain ,
@@ -434,7 +435,8 @@ int iommu_probe_device(struct device *dev)
434
435
* support default domains, so the return value is not yet
435
436
* checked.
436
437
*/
437
- iommu_alloc_default_domain (group , dev );
438
+ group -> default_domain = iommu_group_alloc_default_domain (
439
+ group , iommu_get_def_domain_type (dev ));
438
440
if (group -> default_domain ) {
439
441
iommu_create_device_direct_mappings (group , dev );
440
442
ret = __iommu_group_set_domain (group ,
@@ -1645,35 +1647,38 @@ static int iommu_get_def_domain_type(struct device *dev)
1645
1647
return 0 ;
1646
1648
}
1647
1649
1648
- static int iommu_group_alloc_default_domain (const struct bus_type * bus ,
1649
- struct iommu_group * group ,
1650
- unsigned int type )
1650
+ /*
1651
+ * req_type of 0 means "auto" which means to select a domain based on
1652
+ * iommu_def_domain_type or what the driver actually supports.
1653
+ */
1654
+ static struct iommu_domain *
1655
+ iommu_group_alloc_default_domain (struct iommu_group * group , int req_type )
1651
1656
{
1657
+ const struct bus_type * bus =
1658
+ list_first_entry (& group -> devices , struct group_device , list )
1659
+ -> dev -> bus ;
1652
1660
struct iommu_domain * dom ;
1653
1661
1654
- dom = __iommu_domain_alloc (bus , type );
1655
- if (!dom && type != IOMMU_DOMAIN_DMA ) {
1656
- dom = __iommu_domain_alloc (bus , IOMMU_DOMAIN_DMA );
1657
- if (dom )
1658
- pr_warn ("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA" ,
1659
- type , group -> name );
1660
- }
1661
-
1662
- if (!dom )
1663
- return - ENOMEM ;
1662
+ lockdep_assert_held (& group -> mutex );
1664
1663
1665
- group -> default_domain = dom ;
1666
- return 0 ;
1667
- }
1664
+ if (req_type )
1665
+ return __iommu_domain_alloc (bus , req_type );
1668
1666
1669
- static int iommu_alloc_default_domain ( struct iommu_group * group ,
1670
- struct device * dev )
1671
- {
1672
- unsigned int type ;
1667
+ /* The driver gave no guidance on what type to use, try the default */
1668
+ dom = __iommu_domain_alloc ( bus , iommu_def_domain_type );
1669
+ if ( dom )
1670
+ return dom ;
1673
1671
1674
- type = iommu_get_def_domain_type (dev ) ? : iommu_def_domain_type ;
1672
+ /* Otherwise IDENTITY and DMA_FQ defaults will try DMA */
1673
+ if (iommu_def_domain_type == IOMMU_DOMAIN_DMA )
1674
+ return NULL ;
1675
+ dom = __iommu_domain_alloc (bus , IOMMU_DOMAIN_DMA );
1676
+ if (!dom )
1677
+ return NULL ;
1675
1678
1676
- return iommu_group_alloc_default_domain (dev -> bus , group , type );
1679
+ pr_warn ("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA" ,
1680
+ iommu_def_domain_type , group -> name );
1681
+ return dom ;
1677
1682
}
1678
1683
1679
1684
/**
@@ -1785,15 +1790,12 @@ static int iommu_get_default_domain_type(struct iommu_group *group,
1785
1790
"Device needs domain type %s, but device %s in the same iommu group requires type %s - using default\n" ,
1786
1791
iommu_domain_type_str (type ), dev_name (last_dev ),
1787
1792
iommu_domain_type_str (best_type ));
1788
- return iommu_def_domain_type ;
1793
+ return 0 ;
1789
1794
}
1790
1795
if (!best_type )
1791
1796
best_type = type ;
1792
1797
last_dev = gdev -> dev ;
1793
1798
}
1794
-
1795
- if (!best_type )
1796
- return iommu_def_domain_type ;
1797
1799
return best_type ;
1798
1800
}
1799
1801
@@ -1850,9 +1852,8 @@ int bus_iommu_probe(const struct bus_type *bus)
1850
1852
list_del_init (& group -> entry );
1851
1853
1852
1854
/* Try to allocate default domain */
1853
- iommu_group_alloc_default_domain (
1854
- bus , group , iommu_get_default_domain_type (group , 0 ));
1855
-
1855
+ group -> default_domain = iommu_group_alloc_default_domain (
1856
+ group , iommu_get_default_domain_type (group , 0 ));
1856
1857
if (!group -> default_domain ) {
1857
1858
mutex_unlock (& group -> mutex );
1858
1859
continue ;
@@ -2897,9 +2898,11 @@ static int iommu_change_dev_def_domain(struct iommu_group *group,
2897
2898
group -> domain = NULL ;
2898
2899
2899
2900
/* Sets group->default_domain to the newly allocated domain */
2900
- ret = iommu_group_alloc_default_domain (dev -> bus , group , type );
2901
- if (ret )
2901
+ group -> default_domain = iommu_group_alloc_default_domain (group , type );
2902
+ if (!group -> default_domain ) {
2903
+ ret = - EINVAL ;
2902
2904
goto restore_old_domain ;
2905
+ }
2903
2906
2904
2907
group -> domain = prev_dom ;
2905
2908
ret = iommu_create_device_direct_mappings (group , dev );
0 commit comments