Skip to content

Commit 1342881

Browse files
jpemartinsjgunthorpe
authored andcommitted
iommu/amd: Add domain_alloc_user based domain allocation
Add the domain_alloc_user op implementation. To that end, refactor amd_iommu_domain_alloc() to receive a dev pointer and flags, while renaming it too, such that it becomes a common function shared with domain_alloc_user() implementation. The sole difference with domain_alloc_user() is that we initialize also other fields that iommu_domain_alloc() does. It lets it return the iommu domain correctly initialized in one function. This is in preparation to add dirty enforcement on AMD implementation of domain_alloc_user. Link: https://lore.kernel.org/r/20231024135109.73787-11-joao.m.martins@oracle.com Signed-off-by: Joao Martins <joao.m.martins@oracle.com> Reviewed-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
1 parent 6098481 commit 1342881

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

drivers/iommu/amd/iommu.c

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <asm/iommu.h>
3838
#include <asm/gart.h>
3939
#include <asm/dma.h>
40+
#include <uapi/linux/iommufd.h>
4041

4142
#include "amd_iommu.h"
4243
#include "../dma-iommu.h"
@@ -2155,28 +2156,64 @@ static inline u64 dma_max_address(void)
21552156
return ((1ULL << PM_LEVEL_SHIFT(amd_iommu_gpt_level)) - 1);
21562157
}
21572158

2158-
static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
2159+
static struct iommu_domain *do_iommu_domain_alloc(unsigned int type,
2160+
struct device *dev, u32 flags)
21592161
{
21602162
struct protection_domain *domain;
2163+
struct amd_iommu *iommu = NULL;
2164+
2165+
if (dev) {
2166+
iommu = rlookup_amd_iommu(dev);
2167+
if (!iommu)
2168+
return ERR_PTR(-ENODEV);
2169+
}
21612170

21622171
/*
21632172
* Since DTE[Mode]=0 is prohibited on SNP-enabled system,
21642173
* default to use IOMMU_DOMAIN_DMA[_FQ].
21652174
*/
21662175
if (amd_iommu_snp_en && (type == IOMMU_DOMAIN_IDENTITY))
2167-
return NULL;
2176+
return ERR_PTR(-EINVAL);
21682177

21692178
domain = protection_domain_alloc(type);
21702179
if (!domain)
2171-
return NULL;
2180+
return ERR_PTR(-ENOMEM);
21722181

21732182
domain->domain.geometry.aperture_start = 0;
21742183
domain->domain.geometry.aperture_end = dma_max_address();
21752184
domain->domain.geometry.force_aperture = true;
21762185

2186+
if (iommu) {
2187+
domain->domain.type = type;
2188+
domain->domain.pgsize_bitmap = iommu->iommu.ops->pgsize_bitmap;
2189+
domain->domain.ops = iommu->iommu.ops->default_domain_ops;
2190+
}
2191+
21772192
return &domain->domain;
21782193
}
21792194

2195+
static struct iommu_domain *amd_iommu_domain_alloc(unsigned int type)
2196+
{
2197+
struct iommu_domain *domain;
2198+
2199+
domain = do_iommu_domain_alloc(type, NULL, 0);
2200+
if (IS_ERR(domain))
2201+
return NULL;
2202+
2203+
return domain;
2204+
}
2205+
2206+
static struct iommu_domain *amd_iommu_domain_alloc_user(struct device *dev,
2207+
u32 flags)
2208+
{
2209+
unsigned int type = IOMMU_DOMAIN_UNMANAGED;
2210+
2211+
if (flags)
2212+
return ERR_PTR(-EOPNOTSUPP);
2213+
2214+
return do_iommu_domain_alloc(type, dev, flags);
2215+
}
2216+
21802217
static void amd_iommu_domain_free(struct iommu_domain *dom)
21812218
{
21822219
struct protection_domain *domain;
@@ -2464,6 +2501,7 @@ static bool amd_iommu_enforce_cache_coherency(struct iommu_domain *domain)
24642501
const struct iommu_ops amd_iommu_ops = {
24652502
.capable = amd_iommu_capable,
24662503
.domain_alloc = amd_iommu_domain_alloc,
2504+
.domain_alloc_user = amd_iommu_domain_alloc_user,
24672505
.probe_device = amd_iommu_probe_device,
24682506
.release_device = amd_iommu_release_device,
24692507
.probe_finalize = amd_iommu_probe_finalize,

0 commit comments

Comments
 (0)