Skip to content

Commit c4996c0

Browse files
committed
iommu: Handle translated device firmware mappings
Signed-off-by: Janne Grunau <j@jannau.net>
1 parent 9dc5ffd commit c4996c0

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

drivers/iommu/iommu.c

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,35 +1173,43 @@ static int iommu_create_device_fw_mappings(struct iommu_domain *domain,
11731173

11741174
/* We need to consider overlapping regions for different devices */
11751175
list_for_each_entry(entry, &mappings, list) {
1176-
dma_addr_t start, end, addr;
1176+
dma_addr_t start, end, addr, iova;
11771177
size_t map_size = 0;
11781178

11791179
if (entry->type == IOMMU_RESV_DIRECT)
11801180
dev->iommu->require_direct = 1;
1181+
if (entry->type == IOMMU_RESV_TRANSLATED)
1182+
dev->iommu->require_translated = 1;
11811183

11821184
if ((entry->type != IOMMU_RESV_DIRECT &&
1183-
entry->type != IOMMU_RESV_DIRECT_RELAXABLE) ||
1185+
entry->type != IOMMU_RESV_DIRECT_RELAXABLE &&
1186+
entry->type != IOMMU_RESV_TRANSLATED) ||
11841187
!iommu_is_dma_domain(domain))
11851188
continue;
11861189

11871190
start = ALIGN(entry->start, pg_size);
11881191
end = ALIGN(entry->start + entry->length, pg_size);
11891192

1190-
for (addr = start; addr <= end; addr += pg_size) {
1193+
if (entry->type == IOMMU_RESV_TRANSLATED)
1194+
iova = ALIGN(entry->dva, pg_size);
1195+
else
1196+
iova = start;
1197+
1198+
for (addr = start; addr <= end; addr += pg_size, iova += pg_size) {
11911199
phys_addr_t phys_addr;
11921200

11931201
if (addr == end)
11941202
goto map_end;
11951203

1196-
phys_addr = iommu_iova_to_phys(domain, addr);
1204+
phys_addr = iommu_iova_to_phys(domain, iova);
11971205
if (!phys_addr) {
11981206
map_size += pg_size;
11991207
continue;
12001208
}
12011209

12021210
map_end:
12031211
if (map_size) {
1204-
ret = iommu_map(domain, addr - map_size,
1212+
ret = iommu_map(domain, iova - map_size,
12051213
addr - map_size, map_size,
12061214
entry->prot, GFP_KERNEL);
12071215
if (ret)
@@ -2303,6 +2311,19 @@ static int __iommu_device_set_domain(struct iommu_group *group,
23032311
"Firmware has requested this device have a 1:1 IOMMU mapping, rejecting configuring the device without a 1:1 mapping. Contact your platform vendor.\n");
23042312
return -EINVAL;
23052313
}
2314+
/*
2315+
* If the device requires IOMMU_RESV_TRANSLATED then we cannot allow
2316+
* the identy or blocking domain to be attached as it does not contain
2317+
* the required translated mapping.
2318+
*/
2319+
if (dev->iommu->require_translated &&
2320+
(new_domain->type == IOMMU_DOMAIN_IDENTITY ||
2321+
new_domain->type == IOMMU_DOMAIN_BLOCKED ||
2322+
new_domain == group->blocking_domain)) {
2323+
dev_warn(dev,
2324+
"Firmware has requested this device have a translated IOMMU mapping, rejecting configuring the device without a translated mapping. Contact your platform vendor.\n");
2325+
return -EINVAL;
2326+
}
23062327

23072328
if (dev->iommu->attach_deferred) {
23082329
if (new_domain == group->default_domain)

include/linux/iommu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,7 @@ struct iommu_fault_param {
844844
* @pci_32bit_workaround: Limit DMA allocations to 32-bit IOVAs
845845
* @require_direct: device requires IOMMU_RESV_DIRECT regions
846846
* @shadow_on_flush: IOTLB flushes are used to sync shadow tables
847+
* @require_translated: device requires IOMMU_RESV_TRANSLATED regions
847848
*
848849
* TODO: migrate other per device data pointers under iommu_dev_data, e.g.
849850
* struct iommu_group *iommu_group;
@@ -859,6 +860,7 @@ struct dev_iommu {
859860
u32 pci_32bit_workaround:1;
860861
u32 require_direct:1;
861862
u32 shadow_on_flush:1;
863+
u32 require_translated:1;
862864
};
863865

864866
int iommu_device_register(struct iommu_device *iommu,

0 commit comments

Comments
 (0)