@@ -87,6 +87,27 @@ int amd_iommu_max_glx_val = -1;
8787
8888static struct dma_map_ops amd_iommu_dma_ops ;
8989
90+ /*
91+ * This struct contains device specific data for the IOMMU
92+ */
93+ struct iommu_dev_data {
94+ struct list_head list ; /* For domain->dev_list */
95+ struct list_head dev_data_list ; /* For global dev_data_list */
96+ struct list_head alias_list ; /* Link alias-groups together */
97+ struct iommu_dev_data * alias_data ;/* The alias dev_data */
98+ struct protection_domain * domain ; /* Domain the device is bound to */
99+ u16 devid ; /* PCI Device ID */
100+ bool iommu_v2 ; /* Device can make use of IOMMUv2 */
101+ bool passthrough ; /* Default for device is pt_domain */
102+ struct {
103+ bool enabled ;
104+ int qdep ;
105+ } ats ; /* ATS state */
106+ bool pri_tlp ; /* PASID TLB required for
107+ PPR completions */
108+ u32 errata ; /* Bitmap for errata to apply */
109+ };
110+
90111/*
91112 * general struct to manage commands send to an IOMMU
92113 */
@@ -114,8 +135,9 @@ static struct iommu_dev_data *alloc_dev_data(u16 devid)
114135 if (!dev_data )
115136 return NULL ;
116137
138+ INIT_LIST_HEAD (& dev_data -> alias_list );
139+
117140 dev_data -> devid = devid ;
118- atomic_set (& dev_data -> bind , 0 );
119141
120142 spin_lock_irqsave (& dev_data_list_lock , flags );
121143 list_add_tail (& dev_data -> dev_data_list , & dev_data_list );
@@ -260,17 +282,13 @@ static bool check_device(struct device *dev)
260282 return true;
261283}
262284
263- static int init_iommu_group (struct device * dev )
285+ static void init_iommu_group (struct device * dev )
264286{
265287 struct iommu_group * group ;
266288
267289 group = iommu_group_get_for_dev (dev );
268-
269- if (IS_ERR (group ))
270- return PTR_ERR (group );
271-
272- iommu_group_put (group );
273- return 0 ;
290+ if (!IS_ERR (group ))
291+ iommu_group_put (group );
274292}
275293
276294static int __last_alias (struct pci_dev * pdev , u16 alias , void * data )
@@ -340,7 +358,6 @@ static int iommu_init_device(struct device *dev)
340358 struct pci_dev * pdev = to_pci_dev (dev );
341359 struct iommu_dev_data * dev_data ;
342360 u16 alias ;
343- int ret ;
344361
345362 if (dev -> archdata .iommu )
346363 return 0 ;
@@ -362,12 +379,9 @@ static int iommu_init_device(struct device *dev)
362379 return - ENOTSUPP ;
363380 }
364381 dev_data -> alias_data = alias_data ;
365- }
366382
367- ret = init_iommu_group (dev );
368- if (ret ) {
369- free_dev_data (dev_data );
370- return ret ;
383+ /* Add device to the alias_list */
384+ list_add (& dev_data -> alias_list , & alias_data -> alias_list );
371385 }
372386
373387 if (pci_iommuv2_capable (pdev )) {
@@ -455,6 +469,15 @@ int __init amd_iommu_init_devices(void)
455469 goto out_free ;
456470 }
457471
472+ /*
473+ * Initialize IOMMU groups only after iommu_init_device() has
474+ * had a chance to populate any IVRS defined aliases.
475+ */
476+ for_each_pci_dev (pdev ) {
477+ if (check_device (& pdev -> dev ))
478+ init_iommu_group (& pdev -> dev );
479+ }
480+
458481 return 0 ;
459482
460483out_free :
@@ -1368,6 +1391,9 @@ static int iommu_map_page(struct protection_domain *dom,
13681391 count = PAGE_SIZE_PTE_COUNT (page_size );
13691392 pte = alloc_pte (dom , bus_addr , page_size , NULL , GFP_KERNEL );
13701393
1394+ if (!pte )
1395+ return - ENOMEM ;
1396+
13711397 for (i = 0 ; i < count ; ++ i )
13721398 if (IOMMU_PTE_PRESENT (pte [i ]))
13731399 return - EBUSY ;
@@ -2122,35 +2148,29 @@ static void do_detach(struct iommu_dev_data *dev_data)
21222148static int __attach_device (struct iommu_dev_data * dev_data ,
21232149 struct protection_domain * domain )
21242150{
2151+ struct iommu_dev_data * head , * entry ;
21252152 int ret ;
21262153
21272154 /* lock domain */
21282155 spin_lock (& domain -> lock );
21292156
2130- if (dev_data -> alias_data != NULL ) {
2131- struct iommu_dev_data * alias_data = dev_data -> alias_data ;
2157+ head = dev_data ;
21322158
2133- /* Some sanity checks */
2134- ret = - EBUSY ;
2135- if (alias_data -> domain != NULL &&
2136- alias_data -> domain != domain )
2137- goto out_unlock ;
2159+ if (head -> alias_data != NULL )
2160+ head = head -> alias_data ;
21382161
2139- if (dev_data -> domain != NULL &&
2140- dev_data -> domain != domain )
2141- goto out_unlock ;
2162+ /* Now we have the root of the alias group, if any */
21422163
2143- /* Do real assignment */
2144- if (alias_data -> domain == NULL )
2145- do_attach (alias_data , domain );
2146-
2147- atomic_inc (& alias_data -> bind );
2148- }
2164+ ret = - EBUSY ;
2165+ if (head -> domain != NULL )
2166+ goto out_unlock ;
21492167
2150- if ( dev_data -> domain == NULL )
2151- do_attach (dev_data , domain );
2168+ /* Attach alias group root */
2169+ do_attach (head , domain );
21522170
2153- atomic_inc (& dev_data -> bind );
2171+ /* Attach other devices in the alias group */
2172+ list_for_each_entry (entry , & head -> alias_list , alias_list )
2173+ do_attach (entry , domain );
21542174
21552175 ret = 0 ;
21562176
@@ -2298,6 +2318,7 @@ static int attach_device(struct device *dev,
22982318 */
22992319static void __detach_device (struct iommu_dev_data * dev_data )
23002320{
2321+ struct iommu_dev_data * head , * entry ;
23012322 struct protection_domain * domain ;
23022323 unsigned long flags ;
23032324
@@ -2307,15 +2328,14 @@ static void __detach_device(struct iommu_dev_data *dev_data)
23072328
23082329 spin_lock_irqsave (& domain -> lock , flags );
23092330
2310- if (dev_data -> alias_data != NULL ) {
2311- struct iommu_dev_data * alias_data = dev_data -> alias_data ;
2331+ head = dev_data ;
2332+ if (head -> alias_data != NULL )
2333+ head = head -> alias_data ;
23122334
2313- if (atomic_dec_and_test (& alias_data -> bind ))
2314- do_detach (alias_data );
2315- }
2335+ list_for_each_entry (entry , & head -> alias_list , alias_list )
2336+ do_detach (entry );
23162337
2317- if (atomic_dec_and_test (& dev_data -> bind ))
2318- do_detach (dev_data );
2338+ do_detach (head );
23192339
23202340 spin_unlock_irqrestore (& domain -> lock , flags );
23212341
@@ -2415,6 +2435,7 @@ static int device_change_notifier(struct notifier_block *nb,
24152435 case BUS_NOTIFY_ADD_DEVICE :
24162436
24172437 iommu_init_device (dev );
2438+ init_iommu_group (dev );
24182439
24192440 /*
24202441 * dev_data is still NULL and
@@ -3158,7 +3179,6 @@ static void cleanup_domain(struct protection_domain *domain)
31583179 entry = list_first_entry (& domain -> dev_list ,
31593180 struct iommu_dev_data , list );
31603181 __detach_device (entry );
3161- atomic_set (& entry -> bind , 0 );
31623182 }
31633183
31643184 write_unlock_irqrestore (& amd_iommu_devtable_lock , flags );
@@ -3384,28 +3404,27 @@ static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom,
33843404 return paddr ;
33853405}
33863406
3387- static int amd_iommu_domain_has_cap (struct iommu_domain * domain ,
3388- unsigned long cap )
3407+ static bool amd_iommu_capable (enum iommu_cap cap )
33893408{
33903409 switch (cap ) {
33913410 case IOMMU_CAP_CACHE_COHERENCY :
3392- return 1 ;
3411+ return true ;
33933412 case IOMMU_CAP_INTR_REMAP :
3394- return irq_remapping_enabled ;
3413+ return ( irq_remapping_enabled == 1 ) ;
33953414 }
33963415
3397- return 0 ;
3416+ return false ;
33983417}
33993418
34003419static const struct iommu_ops amd_iommu_ops = {
3420+ .capable = amd_iommu_capable ,
34013421 .domain_init = amd_iommu_domain_init ,
34023422 .domain_destroy = amd_iommu_domain_destroy ,
34033423 .attach_dev = amd_iommu_attach_device ,
34043424 .detach_dev = amd_iommu_detach_device ,
34053425 .map = amd_iommu_map ,
34063426 .unmap = amd_iommu_unmap ,
34073427 .iova_to_phys = amd_iommu_iova_to_phys ,
3408- .domain_has_cap = amd_iommu_domain_has_cap ,
34093428 .pgsize_bitmap = AMD_IOMMU_PGSIZES ,
34103429};
34113430
@@ -4235,7 +4254,7 @@ static int msi_setup_irq(struct pci_dev *pdev, unsigned int irq,
42354254 return 0 ;
42364255}
42374256
4238- static int setup_hpet_msi (unsigned int irq , unsigned int id )
4257+ static int alloc_hpet_msi (unsigned int irq , unsigned int id )
42394258{
42404259 struct irq_2_irte * irte_info ;
42414260 struct irq_cfg * cfg ;
@@ -4274,6 +4293,6 @@ struct irq_remap_ops amd_iommu_irq_ops = {
42744293 .compose_msi_msg = compose_msi_msg ,
42754294 .msi_alloc_irq = msi_alloc_irq ,
42764295 .msi_setup_irq = msi_setup_irq ,
4277- .setup_hpet_msi = setup_hpet_msi ,
4296+ .alloc_hpet_msi = alloc_hpet_msi ,
42784297};
42794298#endif
0 commit comments