Skip to content

Commit 1af9576

Browse files
hegdevasantjoergroedel
authored andcommitted
iommu/amd: Initial SVA support for AMD IOMMU
This includes : - Add data structure to track per protection domain dev/pasid binding details protection_domain->dev_data_list will track attached list of dev_data/PASIDs. - Move 'to_pdomain()' to header file - Add iommu_sva_set_dev_pasid(). It will check whether PASID is supported or not. Also adds PASID to SVA protection domain list as well as to device GCR3 table. - Add iommu_ops.remove_dev_pasid support. It will unbind PASID from device. Also remove pasid data from protection domain device list. - Add IOMMU_SVA as dependency to AMD_IOMMU driver For a given PASID, iommu_set_dev_pasid() will bind all devices to same SVA protection domain (1 PASID : 1 SVA protection domain : N devices). This protection domain is different from device protection domain (one that's mapped in attach_device() path). IOMMU uses domain ID for caching, invalidation, etc. In SVA mode it will use per-device-domain-ID. Hence in invalidation path we retrieve domain ID from gcr3_info_table structure and use that for invalidation. Co-developed-by: Wei Huang <wei.huang2@amd.com> Signed-off-by: Wei Huang <wei.huang2@amd.com> Co-developed-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com> Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com> Signed-off-by: Vasant Hegde <vasant.hegde@amd.com> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Link: https://lore.kernel.org/r/20240418103400.6229-14-vasant.hegde@amd.com Signed-off-by: Joerg Roedel <jroedel@suse.de>
1 parent c4cb231 commit 1af9576

File tree

6 files changed

+156
-6
lines changed

6 files changed

+156
-6
lines changed

drivers/iommu/amd/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ config AMD_IOMMU
1010
select IOMMU_API
1111
select IOMMU_IOVA
1212
select IOMMU_IO_PGTABLE
13+
select IOMMU_SVA
1314
select IOMMU_IOPF
1415
select IOMMUFD_DRIVER if IOMMUFD
1516
depends on X86_64 && PCI && ACPI && HAVE_CMPXCHG_DOUBLE

drivers/iommu/amd/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# SPDX-License-Identifier: GPL-2.0-only
2-
obj-$(CONFIG_AMD_IOMMU) += iommu.o init.o quirks.o io_pgtable.o io_pgtable_v2.o ppr.o
2+
obj-$(CONFIG_AMD_IOMMU) += iommu.o init.o quirks.o io_pgtable.o io_pgtable_v2.o ppr.o pasid.o
33
obj-$(CONFIG_AMD_IOMMU_DEBUGFS) += debugfs.o

drivers/iommu/amd/amd_iommu.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ extern int amd_iommu_guest_ir;
4444
extern enum io_pgtable_fmt amd_iommu_pgtable;
4545
extern int amd_iommu_gpt_level;
4646

47+
/* Protection domain ops */
48+
int iommu_sva_set_dev_pasid(struct iommu_domain *domain,
49+
struct device *dev, ioasid_t pasid);
50+
void amd_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid);
51+
52+
/* SVA/PASID */
4753
bool amd_iommu_pasid_supported(void);
4854

4955
/* IOPF */
@@ -174,6 +180,11 @@ static inline struct amd_iommu *get_amd_iommu_from_dev_data(struct iommu_dev_dat
174180
return iommu_get_iommu_dev(dev_data->dev, struct amd_iommu, iommu);
175181
}
176182

183+
static inline struct protection_domain *to_pdomain(struct iommu_domain *dom)
184+
{
185+
return container_of(dom, struct protection_domain, domain);
186+
}
187+
177188
bool translation_pre_enabled(struct amd_iommu *iommu);
178189
bool amd_iommu_is_attach_deferred(struct device *dev);
179190
int __init add_special_device(u8 type, u8 id, u32 *devid, bool cmd_line);

drivers/iommu/amd/amd_iommu_types.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
#ifndef _ASM_X86_AMD_IOMMU_TYPES_H
99
#define _ASM_X86_AMD_IOMMU_TYPES_H
1010

11+
#include <linux/iommu.h>
1112
#include <linux/types.h>
13+
#include <linux/mmu_notifier.h>
1214
#include <linux/mutex.h>
1315
#include <linux/msi.h>
1416
#include <linux/list.h>
@@ -511,6 +513,11 @@ extern struct kmem_cache *amd_iommu_irq_cache;
511513
list_for_each_entry((iommu), &amd_iommu_list, list)
512514
#define for_each_iommu_safe(iommu, next) \
513515
list_for_each_entry_safe((iommu), (next), &amd_iommu_list, list)
516+
/* Making iterating over protection_domain->dev_data_list easier */
517+
#define for_each_pdom_dev_data(pdom_dev_data, pdom) \
518+
list_for_each_entry(pdom_dev_data, &pdom->dev_data_list, list)
519+
#define for_each_pdom_dev_data_safe(pdom_dev_data, next, pdom) \
520+
list_for_each_entry_safe((pdom_dev_data), (next), &pdom->dev_data_list, list)
514521

515522
struct amd_iommu;
516523
struct iommu_domain;
@@ -552,6 +559,16 @@ enum protection_domain_mode {
552559
PD_MODE_V2,
553560
};
554561

562+
/* Track dev_data/PASID list for the protection domain */
563+
struct pdom_dev_data {
564+
/* Points to attached device data */
565+
struct iommu_dev_data *dev_data;
566+
/* PASID attached to the protection domain */
567+
ioasid_t pasid;
568+
/* For protection_domain->dev_data_list */
569+
struct list_head list;
570+
};
571+
555572
/*
556573
* This structure contains generic data for IOMMU protection domains
557574
* independent of their use.
@@ -568,6 +585,8 @@ struct protection_domain {
568585
bool dirty_tracking; /* dirty tracking is enabled in the domain */
569586
unsigned dev_cnt; /* devices assigned to this domain */
570587
unsigned dev_iommu[MAX_IOMMUS]; /* per-IOMMU reference count */
588+
589+
struct list_head dev_data_list; /* List of pdom_dev_data */
571590
};
572591

573592
/*

drivers/iommu/amd/iommu.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,6 @@ static struct amd_iommu *rlookup_amd_iommu(struct device *dev)
194194
return __rlookup_amd_iommu(seg, PCI_SBDF_TO_DEVID(devid));
195195
}
196196

197-
static struct protection_domain *to_pdomain(struct iommu_domain *dom)
198-
{
199-
return container_of(dom, struct protection_domain, domain);
200-
}
201-
202197
static struct iommu_dev_data *alloc_dev_data(struct amd_iommu *iommu, u16 devid)
203198
{
204199
struct iommu_dev_data *dev_data;
@@ -2345,6 +2340,7 @@ static struct protection_domain *protection_domain_alloc(unsigned int type)
23452340

23462341
spin_lock_init(&domain->lock);
23472342
INIT_LIST_HEAD(&domain->dev_list);
2343+
INIT_LIST_HEAD(&domain->dev_data_list);
23482344
domain->nid = NUMA_NO_NODE;
23492345

23502346
switch (type) {
@@ -2874,6 +2870,7 @@ const struct iommu_ops amd_iommu_ops = {
28742870
.def_domain_type = amd_iommu_def_domain_type,
28752871
.dev_enable_feat = amd_iommu_dev_enable_feature,
28762872
.dev_disable_feat = amd_iommu_dev_disable_feature,
2873+
.remove_dev_pasid = amd_iommu_remove_dev_pasid,
28772874
.page_response = amd_iommu_page_response,
28782875
.default_domain_ops = &(const struct iommu_domain_ops) {
28792876
.attach_dev = amd_iommu_attach_device,

drivers/iommu/amd/pasid.c

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Copyright (C) 2024 Advanced Micro Devices, Inc.
4+
*/
5+
6+
#define pr_fmt(fmt) "AMD-Vi: " fmt
7+
#define dev_fmt(fmt) pr_fmt(fmt)
8+
9+
#include <linux/iommu.h>
10+
#include <linux/mm_types.h>
11+
12+
#include "amd_iommu.h"
13+
14+
static inline bool is_pasid_enabled(struct iommu_dev_data *dev_data)
15+
{
16+
if (dev_data->pasid_enabled && dev_data->max_pasids &&
17+
dev_data->gcr3_info.gcr3_tbl != NULL)
18+
return true;
19+
20+
return false;
21+
}
22+
23+
static inline bool is_pasid_valid(struct iommu_dev_data *dev_data,
24+
ioasid_t pasid)
25+
{
26+
if (pasid > 0 && pasid < dev_data->max_pasids)
27+
return true;
28+
29+
return false;
30+
}
31+
32+
static void remove_dev_pasid(struct pdom_dev_data *pdom_dev_data)
33+
{
34+
/* Update GCR3 table and flush IOTLB */
35+
amd_iommu_clear_gcr3(pdom_dev_data->dev_data, pdom_dev_data->pasid);
36+
37+
list_del(&pdom_dev_data->list);
38+
kfree(pdom_dev_data);
39+
}
40+
41+
/* Clear PASID from device GCR3 table and remove pdom_dev_data from list */
42+
static void remove_pdom_dev_pasid(struct protection_domain *pdom,
43+
struct device *dev, ioasid_t pasid)
44+
{
45+
struct pdom_dev_data *pdom_dev_data;
46+
struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
47+
48+
lockdep_assert_held(&pdom->lock);
49+
50+
for_each_pdom_dev_data(pdom_dev_data, pdom) {
51+
if (pdom_dev_data->dev_data == dev_data &&
52+
pdom_dev_data->pasid == pasid) {
53+
remove_dev_pasid(pdom_dev_data);
54+
break;
55+
}
56+
}
57+
}
58+
59+
int iommu_sva_set_dev_pasid(struct iommu_domain *domain,
60+
struct device *dev, ioasid_t pasid)
61+
{
62+
struct pdom_dev_data *pdom_dev_data;
63+
struct protection_domain *sva_pdom = to_pdomain(domain);
64+
struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
65+
unsigned long flags;
66+
int ret = -EINVAL;
67+
68+
/* PASID zero is used for requests from the I/O device without PASID */
69+
if (!is_pasid_valid(dev_data, pasid))
70+
return ret;
71+
72+
/* Make sure PASID is enabled */
73+
if (!is_pasid_enabled(dev_data))
74+
return ret;
75+
76+
/* Add PASID to protection domain pasid list */
77+
pdom_dev_data = kzalloc(sizeof(*pdom_dev_data), GFP_KERNEL);
78+
if (pdom_dev_data == NULL)
79+
return ret;
80+
81+
pdom_dev_data->pasid = pasid;
82+
pdom_dev_data->dev_data = dev_data;
83+
84+
spin_lock_irqsave(&sva_pdom->lock, flags);
85+
86+
/* Setup GCR3 table */
87+
ret = amd_iommu_set_gcr3(dev_data, pasid,
88+
iommu_virt_to_phys(domain->mm->pgd));
89+
if (ret) {
90+
kfree(pdom_dev_data);
91+
goto out_unlock;
92+
}
93+
94+
list_add(&pdom_dev_data->list, &sva_pdom->dev_data_list);
95+
96+
out_unlock:
97+
spin_unlock_irqrestore(&sva_pdom->lock, flags);
98+
return ret;
99+
}
100+
101+
void amd_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid)
102+
{
103+
struct protection_domain *sva_pdom;
104+
struct iommu_domain *domain;
105+
unsigned long flags;
106+
107+
if (!is_pasid_valid(dev_iommu_priv_get(dev), pasid))
108+
return;
109+
110+
/* Get protection domain */
111+
domain = iommu_get_domain_for_dev_pasid(dev, pasid, IOMMU_DOMAIN_SVA);
112+
if (!domain)
113+
return;
114+
sva_pdom = to_pdomain(domain);
115+
116+
spin_lock_irqsave(&sva_pdom->lock, flags);
117+
118+
/* Remove PASID from dev_data_list */
119+
remove_pdom_dev_pasid(sva_pdom, dev, pasid);
120+
121+
spin_unlock_irqrestore(&sva_pdom->lock, flags);
122+
}

0 commit comments

Comments
 (0)