@@ -93,9 +93,6 @@ 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 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 );
99
96
static struct iommu_domain * __iommu_domain_alloc (const struct bus_type * bus ,
100
97
unsigned type );
101
98
static int __iommu_attach_device (struct iommu_domain * domain ,
@@ -126,7 +123,9 @@ static void __iommu_group_set_domain_nofail(struct iommu_group *group,
126
123
group , new_domain , IOMMU_SET_DOMAIN_MUST_SUCCEED ));
127
124
}
128
125
129
- static int iommu_create_device_direct_mappings (struct iommu_group * group ,
126
+ static int iommu_setup_default_domain (struct iommu_group * group ,
127
+ int target_type );
128
+ static int iommu_create_device_direct_mappings (struct iommu_domain * domain ,
130
129
struct device * dev );
131
130
static struct iommu_group * iommu_group_get_for_dev (struct device * dev );
132
131
static ssize_t iommu_group_store_type (struct iommu_group * group ,
@@ -424,33 +423,18 @@ int iommu_probe_device(struct device *dev)
424
423
425
424
mutex_lock (& group -> mutex );
426
425
427
- iommu_create_device_direct_mappings (group , dev );
426
+ if (group -> default_domain )
427
+ iommu_create_device_direct_mappings (group -> default_domain , dev );
428
428
429
429
if (group -> domain ) {
430
430
ret = __iommu_device_set_domain (group , dev , group -> domain , 0 );
431
+ if (ret )
432
+ goto err_unlock ;
431
433
} else if (!group -> default_domain ) {
432
- /*
433
- * Try to allocate a default domain - needs support from the
434
- * IOMMU driver. There are still some drivers which don't
435
- * support default domains, so the return value is not yet
436
- * checked.
437
- */
438
- group -> default_domain = iommu_group_alloc_default_domain (
439
- group , iommu_get_def_domain_type (dev ));
440
- if (group -> default_domain ) {
441
- iommu_create_device_direct_mappings (group , dev );
442
- ret = __iommu_group_set_domain (group ,
443
- group -> default_domain );
444
- }
445
-
446
- /*
447
- * We assume that the iommu driver starts up the device in
448
- * 'set_platform_dma_ops' mode if it does not support default
449
- * domains.
450
- */
434
+ ret = iommu_setup_default_domain (group , 0 );
435
+ if (ret )
436
+ goto err_unlock ;
451
437
}
452
- if (ret )
453
- goto err_unlock ;
454
438
455
439
mutex_unlock (& group -> mutex );
456
440
iommu_group_put (group );
@@ -967,16 +951,15 @@ int iommu_group_set_name(struct iommu_group *group, const char *name)
967
951
}
968
952
EXPORT_SYMBOL_GPL (iommu_group_set_name );
969
953
970
- static int iommu_create_device_direct_mappings (struct iommu_group * group ,
954
+ static int iommu_create_device_direct_mappings (struct iommu_domain * domain ,
971
955
struct device * dev )
972
956
{
973
- struct iommu_domain * domain = group -> default_domain ;
974
957
struct iommu_resv_region * entry ;
975
958
struct list_head mappings ;
976
959
unsigned long pg_size ;
977
960
int ret = 0 ;
978
961
979
- if (!domain || ! iommu_is_dma_domain (domain ))
962
+ if (!iommu_is_dma_domain (domain ))
980
963
return 0 ;
981
964
982
965
BUG_ON (!domain -> pgsize_bitmap );
@@ -1647,6 +1630,15 @@ static int iommu_get_def_domain_type(struct device *dev)
1647
1630
return 0 ;
1648
1631
}
1649
1632
1633
+ static struct iommu_domain *
1634
+ __iommu_group_alloc_default_domain (const struct bus_type * bus ,
1635
+ struct iommu_group * group , int req_type )
1636
+ {
1637
+ if (group -> default_domain && group -> default_domain -> type == req_type )
1638
+ return group -> default_domain ;
1639
+ return __iommu_domain_alloc (bus , req_type );
1640
+ }
1641
+
1650
1642
/*
1651
1643
* req_type of 0 means "auto" which means to select a domain based on
1652
1644
* iommu_def_domain_type or what the driver actually supports.
@@ -1662,17 +1654,17 @@ iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)
1662
1654
lockdep_assert_held (& group -> mutex );
1663
1655
1664
1656
if (req_type )
1665
- return __iommu_domain_alloc (bus , req_type );
1657
+ return __iommu_group_alloc_default_domain (bus , group , req_type );
1666
1658
1667
1659
/* The driver gave no guidance on what type to use, try the default */
1668
- dom = __iommu_domain_alloc (bus , iommu_def_domain_type );
1660
+ dom = __iommu_group_alloc_default_domain (bus , group , iommu_def_domain_type );
1669
1661
if (dom )
1670
1662
return dom ;
1671
1663
1672
1664
/* Otherwise IDENTITY and DMA_FQ defaults will try DMA */
1673
1665
if (iommu_def_domain_type == IOMMU_DOMAIN_DMA )
1674
1666
return NULL ;
1675
- dom = __iommu_domain_alloc (bus , IOMMU_DOMAIN_DMA );
1667
+ dom = __iommu_group_alloc_default_domain (bus , group , IOMMU_DOMAIN_DMA );
1676
1668
if (!dom )
1677
1669
return NULL ;
1678
1670
@@ -1815,21 +1807,6 @@ static void __iommu_group_dma_finalize(struct iommu_group *group)
1815
1807
iommu_group_do_probe_finalize );
1816
1808
}
1817
1809
1818
- static int iommu_do_create_direct_mappings (struct device * dev , void * data )
1819
- {
1820
- struct iommu_group * group = data ;
1821
-
1822
- iommu_create_device_direct_mappings (group , dev );
1823
-
1824
- return 0 ;
1825
- }
1826
-
1827
- static int iommu_group_create_direct_mappings (struct iommu_group * group )
1828
- {
1829
- return __iommu_group_for_each_dev (group , group ,
1830
- iommu_do_create_direct_mappings );
1831
- }
1832
-
1833
1810
int bus_iommu_probe (const struct bus_type * bus )
1834
1811
{
1835
1812
struct iommu_group * group , * next ;
@@ -1851,27 +1828,16 @@ int bus_iommu_probe(const struct bus_type *bus)
1851
1828
/* Remove item from the list */
1852
1829
list_del_init (& group -> entry );
1853
1830
1854
- /* Try to allocate default domain */
1855
- group -> default_domain = iommu_group_alloc_default_domain (
1856
- group , iommu_get_default_domain_type (group , 0 ));
1857
- if (!group -> default_domain ) {
1831
+ ret = iommu_setup_default_domain (group , 0 );
1832
+ if (ret ) {
1858
1833
mutex_unlock (& group -> mutex );
1859
- continue ;
1834
+ return ret ;
1860
1835
}
1861
-
1862
- iommu_group_create_direct_mappings (group );
1863
-
1864
- ret = __iommu_group_set_domain (group , group -> default_domain );
1865
-
1866
1836
mutex_unlock (& group -> mutex );
1867
-
1868
- if (ret )
1869
- break ;
1870
-
1871
1837
__iommu_group_dma_finalize (group );
1872
1838
}
1873
1839
1874
- return ret ;
1840
+ return 0 ;
1875
1841
}
1876
1842
1877
1843
bool iommu_present (const struct bus_type * bus )
@@ -2860,68 +2826,83 @@ int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
2860
2826
}
2861
2827
EXPORT_SYMBOL_GPL (iommu_dev_disable_feature );
2862
2828
2863
- /*
2864
- * Changes the default domain of an iommu group
2865
- *
2866
- * @group: The group for which the default domain should be changed
2867
- * @dev: The first device in the group
2868
- * @type: The type of the new default domain that gets associated with the group
2869
- *
2870
- * Returns 0 on success and error code on failure
2829
+ /**
2830
+ * iommu_setup_default_domain - Set the default_domain for the group
2831
+ * @group: Group to change
2832
+ * @target_type: Domain type to set as the default_domain
2871
2833
*
2872
- * Note:
2873
- * 1. Presently, this function is called only when user requests to change the
2874
- * group's default domain type through /sys/kernel/iommu_groups/<grp_id>/type
2875
- * Please take a closer look if intended to use for other purposes .
2834
+ * Allocate a default domain and set it as the current domain on the group. If
2835
+ * the group already has a default domain it will be changed to the target_type.
2836
+ * When target_type is 0 the default domain is selected based on driver and
2837
+ * system preferences .
2876
2838
*/
2877
- static int iommu_change_dev_def_domain (struct iommu_group * group ,
2878
- struct device * dev , int type )
2839
+ static int iommu_setup_default_domain (struct iommu_group * group ,
2840
+ int target_type )
2879
2841
{
2880
- struct iommu_domain * prev_dom ;
2842
+ struct iommu_domain * old_dom = group -> default_domain ;
2843
+ struct group_device * gdev ;
2844
+ struct iommu_domain * dom ;
2845
+ int req_type ;
2881
2846
int ret ;
2882
2847
2883
2848
lockdep_assert_held (& group -> mutex );
2884
2849
2885
- prev_dom = group -> default_domain ;
2886
- type = iommu_get_default_domain_type (group , type );
2887
- if (type < 0 )
2850
+ req_type = iommu_get_default_domain_type (group , target_type );
2851
+ if (req_type < 0 )
2888
2852
return - EINVAL ;
2889
2853
2890
2854
/*
2891
- * Switch to a new domain only if the requested domain type is different
2892
- * from the existing default domain type
2855
+ * There are still some drivers which don't support default domains, so
2856
+ * we ignore the failure and leave group->default_domain NULL.
2857
+ *
2858
+ * We assume that the iommu driver starts up the device in
2859
+ * 'set_platform_dma_ops' mode if it does not support default domains.
2893
2860
*/
2894
- if (prev_dom -> type == type )
2861
+ dom = iommu_group_alloc_default_domain (group , req_type );
2862
+ if (!dom ) {
2863
+ /* Once in default_domain mode we never leave */
2864
+ if (group -> default_domain )
2865
+ return - ENODEV ;
2866
+ group -> default_domain = NULL ;
2895
2867
return 0 ;
2896
-
2897
- group -> default_domain = NULL ;
2898
- group -> domain = NULL ;
2899
-
2900
- /* Sets group->default_domain to the newly allocated domain */
2901
- group -> default_domain = iommu_group_alloc_default_domain (group , type );
2902
- if (!group -> default_domain ) {
2903
- ret = - EINVAL ;
2904
- goto restore_old_domain ;
2905
2868
}
2906
2869
2907
- group -> domain = prev_dom ;
2908
- ret = iommu_create_device_direct_mappings (group , dev );
2909
- if (ret )
2910
- goto free_new_domain ;
2911
-
2912
- ret = __iommu_group_set_domain (group , group -> default_domain );
2913
- if (ret )
2914
- goto free_new_domain ;
2915
-
2916
- iommu_domain_free (prev_dom );
2870
+ if (group -> default_domain == dom )
2871
+ return 0 ;
2917
2872
2918
- return 0 ;
2873
+ /*
2874
+ * IOMMU_RESV_DIRECT and IOMMU_RESV_DIRECT_RELAXABLE regions must be
2875
+ * mapped before their device is attached, in order to guarantee
2876
+ * continuity with any FW activity
2877
+ */
2878
+ for_each_group_device (group , gdev )
2879
+ iommu_create_device_direct_mappings (dom , gdev -> dev );
2919
2880
2920
- free_new_domain :
2921
- iommu_domain_free (group -> default_domain );
2922
- restore_old_domain :
2923
- group -> default_domain = prev_dom ;
2881
+ /* We must set default_domain early for __iommu_device_set_domain */
2882
+ group -> default_domain = dom ;
2883
+ if (!group -> domain ) {
2884
+ /*
2885
+ * Drivers are not allowed to fail the first domain attach.
2886
+ * The only way to recover from this is to fail attaching the
2887
+ * iommu driver and call ops->release_device. Put the domain
2888
+ * in group->default_domain so it is freed after.
2889
+ */
2890
+ ret = __iommu_group_set_domain_internal (
2891
+ group , dom , IOMMU_SET_DOMAIN_MUST_SUCCEED );
2892
+ if (WARN_ON (ret ))
2893
+ goto out_free ;
2894
+ } else {
2895
+ ret = __iommu_group_set_domain (group , dom );
2896
+ if (ret ) {
2897
+ iommu_domain_free (dom );
2898
+ group -> default_domain = old_dom ;
2899
+ return ret ;
2900
+ }
2901
+ }
2924
2902
2903
+ out_free :
2904
+ if (old_dom )
2905
+ iommu_domain_free (old_dom );
2925
2906
return ret ;
2926
2907
}
2927
2908
@@ -2937,8 +2918,6 @@ static int iommu_change_dev_def_domain(struct iommu_group *group,
2937
2918
static ssize_t iommu_group_store_type (struct iommu_group * group ,
2938
2919
const char * buf , size_t count )
2939
2920
{
2940
- struct group_device * grp_dev ;
2941
- struct device * dev ;
2942
2921
int ret , req_type ;
2943
2922
2944
2923
if (!capable (CAP_SYS_ADMIN ) || !capable (CAP_SYS_RAWIO ))
@@ -2976,10 +2955,7 @@ static ssize_t iommu_group_store_type(struct iommu_group *group,
2976
2955
return - EPERM ;
2977
2956
}
2978
2957
2979
- grp_dev = list_first_entry (& group -> devices , struct group_device , list );
2980
- dev = grp_dev -> dev ;
2981
-
2982
- ret = iommu_change_dev_def_domain (group , dev , req_type );
2958
+ ret = iommu_setup_default_domain (group , req_type );
2983
2959
2984
2960
/*
2985
2961
* Release the mutex here because ops->probe_finalize() call-back of
0 commit comments