Skip to content

Commit 24a94f4

Browse files
committed
iommu: Handle translated device firmware mappings
Signed-off-by: Janne Grunau <j@jannau.net>
1 parent 39d8cd1 commit 24a94f4

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
@@ -1147,35 +1147,43 @@ static int iommu_create_device_fw_mappings(struct iommu_domain *domain,
11471147

11481148
/* We need to consider overlapping regions for different devices */
11491149
list_for_each_entry(entry, &mappings, list) {
1150-
dma_addr_t start, end, addr;
1150+
dma_addr_t start, end, addr, iova;
11511151
size_t map_size = 0;
11521152

11531153
if (entry->type == IOMMU_RESV_DIRECT)
11541154
dev->iommu->require_direct = 1;
1155+
if (entry->type == IOMMU_RESV_TRANSLATED)
1156+
dev->iommu->require_translated = 1;
11551157

11561158
if ((entry->type != IOMMU_RESV_DIRECT &&
1157-
entry->type != IOMMU_RESV_DIRECT_RELAXABLE) ||
1159+
entry->type != IOMMU_RESV_DIRECT_RELAXABLE &&
1160+
entry->type != IOMMU_RESV_TRANSLATED) ||
11581161
!iommu_is_dma_domain(domain))
11591162
continue;
11601163

11611164
start = ALIGN(entry->start, pg_size);
11621165
end = ALIGN(entry->start + entry->length, pg_size);
11631166

1164-
for (addr = start; addr <= end; addr += pg_size) {
1167+
if (entry->type == IOMMU_RESV_TRANSLATED)
1168+
iova = ALIGN(entry->dva, pg_size);
1169+
else
1170+
iova = start;
1171+
1172+
for (addr = start; addr <= end; addr += pg_size, iova += pg_size) {
11651173
phys_addr_t phys_addr;
11661174

11671175
if (addr == end)
11681176
goto map_end;
11691177

1170-
phys_addr = iommu_iova_to_phys(domain, addr);
1178+
phys_addr = iommu_iova_to_phys(domain, iova);
11711179
if (!phys_addr) {
11721180
map_size += pg_size;
11731181
continue;
11741182
}
11751183

11761184
map_end:
11771185
if (map_size) {
1178-
ret = iommu_map(domain, addr - map_size,
1186+
ret = iommu_map(domain, iova - map_size,
11791187
addr - map_size, map_size,
11801188
entry->prot, GFP_KERNEL);
11811189
if (ret)
@@ -2284,6 +2292,19 @@ static int __iommu_device_set_domain(struct iommu_group *group,
22842292
"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");
22852293
return -EINVAL;
22862294
}
2295+
/*
2296+
* If the device requires IOMMU_RESV_TRANSLATED then we cannot allow
2297+
* the identy or blocking domain to be attached as it does not contain
2298+
* the required translated mapping.
2299+
*/
2300+
if (dev->iommu->require_translated &&
2301+
(new_domain->type == IOMMU_DOMAIN_IDENTITY ||
2302+
new_domain->type == IOMMU_DOMAIN_BLOCKED ||
2303+
new_domain == group->blocking_domain)) {
2304+
dev_warn(dev,
2305+
"Firmware has requested this device have a translated IOMMU mapping, rejecting configuring the device without a translated mapping. Contact your platform vendor.\n");
2306+
return -EINVAL;
2307+
}
22872308

22882309
if (dev->iommu->attach_deferred) {
22892310
if (new_domain == group->default_domain)

include/linux/iommu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@ struct iommu_fault_param {
800800
* @pci_32bit_workaround: Limit DMA allocations to 32-bit IOVAs
801801
* @require_direct: device requires IOMMU_RESV_DIRECT regions
802802
* @shadow_on_flush: IOTLB flushes are used to sync shadow tables
803+
* @require_translated: device requires IOMMU_RESV_TRANSLATED regions
803804
*
804805
* TODO: migrate other per device data pointers under iommu_dev_data, e.g.
805806
* struct iommu_group *iommu_group;
@@ -815,6 +816,7 @@ struct dev_iommu {
815816
u32 pci_32bit_workaround:1;
816817
u32 require_direct:1;
817818
u32 shadow_on_flush:1;
819+
u32 require_translated:1;
818820
};
819821

820822
int iommu_device_register(struct iommu_device *iommu,

0 commit comments

Comments
 (0)