Skip to content

Commit 29a4e55

Browse files
committed
iommu: Handle translated device firmware mappings
Signed-off-by: Janne Grunau <j@jannau.net>
1 parent 4ad1907 commit 29a4e55

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

11101110
/* We need to consider overlapping regions for different devices */
11111111
list_for_each_entry(entry, &mappings, list) {
1112-
dma_addr_t start, end, addr;
1112+
dma_addr_t start, end, addr, iova;
11131113
size_t map_size = 0;
11141114

11151115
if (entry->type == IOMMU_RESV_DIRECT)
11161116
dev->iommu->require_direct = 1;
1117+
if (entry->type == IOMMU_RESV_TRANSLATED)
1118+
dev->iommu->require_translated = 1;
11171119

11181120
if ((entry->type != IOMMU_RESV_DIRECT &&
1119-
entry->type != IOMMU_RESV_DIRECT_RELAXABLE) ||
1121+
entry->type != IOMMU_RESV_DIRECT_RELAXABLE &&
1122+
entry->type != IOMMU_RESV_TRANSLATED) ||
11201123
!iommu_is_dma_domain(domain))
11211124
continue;
11221125

11231126
start = ALIGN(entry->start, pg_size);
11241127
end = ALIGN(entry->start + entry->length, pg_size);
11251128

1126-
for (addr = start; addr <= end; addr += pg_size) {
1129+
if (entry->type == IOMMU_RESV_TRANSLATED)
1130+
iova = ALIGN(entry->dva, pg_size);
1131+
else
1132+
iova = start;
1133+
1134+
for (addr = start; addr <= end; addr += pg_size, iova += pg_size) {
11271135
phys_addr_t phys_addr;
11281136

11291137
if (addr == end)
11301138
goto map_end;
11311139

1132-
phys_addr = iommu_iova_to_phys(domain, addr);
1140+
phys_addr = iommu_iova_to_phys(domain, iova);
11331141
if (!phys_addr) {
11341142
map_size += pg_size;
11351143
continue;
11361144
}
11371145

11381146
map_end:
11391147
if (map_size) {
1140-
ret = iommu_map(domain, addr - map_size,
1148+
ret = iommu_map(domain, iova - map_size,
11411149
addr - map_size, map_size,
11421150
entry->prot, GFP_KERNEL);
11431151
if (ret)
@@ -2235,6 +2243,19 @@ static int __iommu_device_set_domain(struct iommu_group *group,
22352243
"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");
22362244
return -EINVAL;
22372245
}
2246+
/*
2247+
* If the device requires IOMMU_RESV_TRANSLATED then we cannot allow
2248+
* the identy or blocking domain to be attached as it does not contain
2249+
* the required translated mapping.
2250+
*/
2251+
if (dev->iommu->require_translated &&
2252+
(new_domain->type == IOMMU_DOMAIN_IDENTITY ||
2253+
new_domain->type == IOMMU_DOMAIN_BLOCKED ||
2254+
new_domain == group->blocking_domain)) {
2255+
dev_warn(dev,
2256+
"Firmware has requested this device have a translated IOMMU mapping, rejecting configuring the device without a translated mapping. Contact your platform vendor.\n");
2257+
return -EINVAL;
2258+
}
22382259

22392260
if (dev->iommu->attach_deferred) {
22402261
if (new_domain == group->default_domain)

include/linux/iommu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,7 @@ struct iommu_fault_param {
789789
* @pci_32bit_workaround: Limit DMA allocations to 32-bit IOVAs
790790
* @require_direct: device requires IOMMU_RESV_DIRECT regions
791791
* @shadow_on_flush: IOTLB flushes are used to sync shadow tables
792+
* @require_translated: device requires IOMMU_RESV_TRANSLATED regions
792793
*
793794
* TODO: migrate other per device data pointers under iommu_dev_data, e.g.
794795
* struct iommu_group *iommu_group;
@@ -804,6 +805,7 @@ struct dev_iommu {
804805
u32 pci_32bit_workaround:1;
805806
u32 require_direct:1;
806807
u32 shadow_on_flush:1;
808+
u32 require_translated:1;
807809
};
808810

809811
int iommu_device_register(struct iommu_device *iommu,

0 commit comments

Comments
 (0)