Skip to content

Commit cb1efcf

Browse files
committed
iommu: Add IOMMU index argument to notifier APIs
Add support for multiple IOMMU indexes to the IOMMU notifier APIs. When initializing a notifier with iommu_notifier_init(), the caller must pass the IOMMU index that it is interested in. When a change happens, the IOMMU implementation must pass memory_region_notify_iommu() the IOMMU index that has changed and that notifiers must be called for. IOMMUs which support only a single index don't need to change. Callers which only really support working with IOMMUs with a single index can use the result of passing MEMTXATTRS_UNSPECIFIED to memory_region_iommu_attrs_to_index(). Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Reviewed-by: Alex Bennée <alex.bennee@linaro.org> Message-id: 20180604152941.20374-3-peter.maydell@linaro.org
1 parent 21f4020 commit cb1efcf

File tree

7 files changed

+30
-10
lines changed

7 files changed

+30
-10
lines changed

hw/i386/intel_iommu.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
10231023
static int vtd_sync_shadow_page_hook(IOMMUTLBEntry *entry,
10241024
void *private)
10251025
{
1026-
memory_region_notify_iommu((IOMMUMemoryRegion *)private, *entry);
1026+
memory_region_notify_iommu((IOMMUMemoryRegion *)private, 0, *entry);
10271027
return 0;
10281028
}
10291029

@@ -1581,7 +1581,7 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
15811581
.addr_mask = size - 1,
15821582
.perm = IOMMU_NONE,
15831583
};
1584-
memory_region_notify_iommu(&vtd_as->iommu, entry);
1584+
memory_region_notify_iommu(&vtd_as->iommu, 0, entry);
15851585
}
15861586
}
15871587
}
@@ -2015,7 +2015,7 @@ static bool vtd_process_device_iotlb_desc(IntelIOMMUState *s,
20152015
entry.iova = addr;
20162016
entry.perm = IOMMU_NONE;
20172017
entry.translated_addr = 0;
2018-
memory_region_notify_iommu(&vtd_dev_as->iommu, entry);
2018+
memory_region_notify_iommu(&vtd_dev_as->iommu, 0, entry);
20192019

20202020
done:
20212021
return true;

hw/ppc/spapr_iommu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ static target_ulong put_tce_emu(sPAPRTCETable *tcet, target_ulong ioba,
428428
entry.translated_addr = tce & page_mask;
429429
entry.addr_mask = ~page_mask;
430430
entry.perm = spapr_tce_iommu_access_flags(tce);
431-
memory_region_notify_iommu(&tcet->iommu, entry);
431+
memory_region_notify_iommu(&tcet->iommu, 0, entry);
432432

433433
return H_SUCCESS;
434434
}

hw/s390x/s390-pci-inst.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ static void s390_pci_update_iotlb(S390PCIIOMMU *iommu, S390IOTLBEntry *entry)
589589
}
590590

591591
notify.perm = IOMMU_NONE;
592-
memory_region_notify_iommu(&iommu->iommu_mr, notify);
592+
memory_region_notify_iommu(&iommu->iommu_mr, 0, notify);
593593
notify.perm = entry->perm;
594594
}
595595

@@ -601,7 +601,7 @@ static void s390_pci_update_iotlb(S390PCIIOMMU *iommu, S390IOTLBEntry *entry)
601601
g_hash_table_replace(iommu->iotlb, &cache->iova, cache);
602602
}
603603

604-
memory_region_notify_iommu(&iommu->iommu_mr, notify);
604+
memory_region_notify_iommu(&iommu->iommu_mr, 0, notify);
605605
}
606606

607607
int rpcit_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2, uintptr_t ra)

hw/vfio/common.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ static void vfio_listener_region_add(MemoryListener *listener,
507507
if (memory_region_is_iommu(section->mr)) {
508508
VFIOGuestIOMMU *giommu;
509509
IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr);
510+
int iommu_idx;
510511

511512
trace_vfio_listener_region_add_iommu(iova, end);
512513
/*
@@ -523,10 +524,13 @@ static void vfio_listener_region_add(MemoryListener *listener,
523524
llend = int128_add(int128_make64(section->offset_within_region),
524525
section->size);
525526
llend = int128_sub(llend, int128_one());
527+
iommu_idx = memory_region_iommu_attrs_to_index(iommu_mr,
528+
MEMTXATTRS_UNSPECIFIED);
526529
iommu_notifier_init(&giommu->n, vfio_iommu_map_notify,
527530
IOMMU_NOTIFIER_ALL,
528531
section->offset_within_region,
529-
int128_get64(llend));
532+
int128_get64(llend),
533+
iommu_idx);
530534
QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next);
531535

532536
memory_region_register_iommu_notifier(section->mr, &giommu->n);

hw/virtio/vhost.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,8 @@ static void vhost_iommu_region_add(MemoryListener *listener,
662662
iommu_listener);
663663
struct vhost_iommu *iommu;
664664
Int128 end;
665+
int iommu_idx;
666+
IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr);
665667

666668
if (!memory_region_is_iommu(section->mr)) {
667669
return;
@@ -671,10 +673,13 @@ static void vhost_iommu_region_add(MemoryListener *listener,
671673
end = int128_add(int128_make64(section->offset_within_region),
672674
section->size);
673675
end = int128_sub(end, int128_one());
676+
iommu_idx = memory_region_iommu_attrs_to_index(iommu_mr,
677+
MEMTXATTRS_UNSPECIFIED);
674678
iommu_notifier_init(&iommu->n, vhost_iommu_unmap_notify,
675679
IOMMU_NOTIFIER_UNMAP,
676680
section->offset_within_region,
677-
int128_get64(end));
681+
int128_get64(end),
682+
iommu_idx);
678683
iommu->mr = section->mr;
679684
iommu->iommu_offset = section->offset_within_address_space -
680685
section->offset_within_region;

include/exec/memory.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,21 @@ struct IOMMUNotifier {
9898
/* Notify for address space range start <= addr <= end */
9999
hwaddr start;
100100
hwaddr end;
101+
int iommu_idx;
101102
QLIST_ENTRY(IOMMUNotifier) node;
102103
};
103104
typedef struct IOMMUNotifier IOMMUNotifier;
104105

105106
static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn,
106107
IOMMUNotifierFlag flags,
107-
hwaddr start, hwaddr end)
108+
hwaddr start, hwaddr end,
109+
int iommu_idx)
108110
{
109111
n->notify = fn;
110112
n->notifier_flags = flags;
111113
n->start = start;
112114
n->end = end;
115+
n->iommu_idx = iommu_idx;
113116
}
114117

115118
/*
@@ -1008,11 +1011,13 @@ uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion *iommu_mr);
10081011
* should be notified with an UNMAP followed by a MAP.
10091012
*
10101013
* @iommu_mr: the memory region that was changed
1014+
* @iommu_idx: the IOMMU index for the translation table which has changed
10111015
* @entry: the new entry in the IOMMU translation table. The entry
10121016
* replaces all old entries for the same virtual I/O address range.
10131017
* Deleted entries have .@perm == 0.
10141018
*/
10151019
void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr,
1020+
int iommu_idx,
10161021
IOMMUTLBEntry entry);
10171022

10181023
/**

memory.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1799,6 +1799,9 @@ void memory_region_register_iommu_notifier(MemoryRegion *mr,
17991799
iommu_mr = IOMMU_MEMORY_REGION(mr);
18001800
assert(n->notifier_flags != IOMMU_NOTIFIER_NONE);
18011801
assert(n->start <= n->end);
1802+
assert(n->iommu_idx >= 0 &&
1803+
n->iommu_idx < memory_region_iommu_num_indexes(iommu_mr));
1804+
18021805
QLIST_INSERT_HEAD(&iommu_mr->iommu_notify, n, node);
18031806
memory_region_update_iommu_notify_flags(iommu_mr);
18041807
}
@@ -1891,14 +1894,17 @@ void memory_region_notify_one(IOMMUNotifier *notifier,
18911894
}
18921895

18931896
void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr,
1897+
int iommu_idx,
18941898
IOMMUTLBEntry entry)
18951899
{
18961900
IOMMUNotifier *iommu_notifier;
18971901

18981902
assert(memory_region_is_iommu(MEMORY_REGION(iommu_mr)));
18991903

19001904
IOMMU_NOTIFIER_FOREACH(iommu_notifier, iommu_mr) {
1901-
memory_region_notify_one(iommu_notifier, &entry);
1905+
if (iommu_notifier->iommu_idx == iommu_idx) {
1906+
memory_region_notify_one(iommu_notifier, &entry);
1907+
}
19021908
}
19031909
}
19041910

0 commit comments

Comments
 (0)