2016-07-04 05:33:04 +02:00
|
|
|
/*
|
|
|
|
* DMA memory preregistration
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Alexey Kardashevskiy <aik@ozlabs.ru>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2. See
|
|
|
|
* the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <linux/vfio.h>
|
2023-11-02 08:12:23 +01:00
|
|
|
#ifdef CONFIG_KVM
|
|
|
|
#include <linux/kvm.h>
|
|
|
|
#endif
|
|
|
|
#include "sysemu/kvm.h"
|
2023-11-02 08:12:24 +01:00
|
|
|
#include "exec/address-spaces.h"
|
2016-07-04 05:33:04 +02:00
|
|
|
|
|
|
|
#include "hw/vfio/vfio-common.h"
|
|
|
|
#include "hw/hw.h"
|
2018-06-20 11:10:12 +02:00
|
|
|
#include "exec/ram_addr.h"
|
2016-07-04 05:33:04 +02:00
|
|
|
#include "qemu/error-report.h"
|
2019-09-24 10:25:16 +02:00
|
|
|
#include "qapi/error.h"
|
2016-07-04 05:33:04 +02:00
|
|
|
#include "trace.h"
|
|
|
|
|
2023-11-02 08:12:42 +01:00
|
|
|
typedef struct VFIOSpaprContainer {
|
|
|
|
VFIOContainer container;
|
2023-11-02 08:12:44 +01:00
|
|
|
MemoryListener prereg_listener;
|
2023-11-02 08:12:45 +01:00
|
|
|
QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list;
|
2023-11-02 08:12:42 +01:00
|
|
|
} VFIOSpaprContainer;
|
|
|
|
|
2016-07-04 05:33:04 +02:00
|
|
|
static bool vfio_prereg_listener_skipped_section(MemoryRegionSection *section)
|
|
|
|
{
|
|
|
|
if (memory_region_is_iommu(section->mr)) {
|
|
|
|
hw_error("Cannot possibly preregister IOMMU memory");
|
|
|
|
}
|
|
|
|
|
|
|
|
return !memory_region_is_ram(section->mr) ||
|
2016-10-31 16:53:03 +01:00
|
|
|
memory_region_is_ram_device(section->mr);
|
2016-07-04 05:33:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *vfio_prereg_gpa_to_vaddr(MemoryRegionSection *section, hwaddr gpa)
|
|
|
|
{
|
|
|
|
return memory_region_get_ram_ptr(section->mr) +
|
|
|
|
section->offset_within_region +
|
|
|
|
(gpa - section->offset_within_address_space);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vfio_prereg_listener_region_add(MemoryListener *listener,
|
|
|
|
MemoryRegionSection *section)
|
|
|
|
{
|
2023-11-02 08:12:44 +01:00
|
|
|
VFIOSpaprContainer *scontainer = container_of(listener, VFIOSpaprContainer,
|
|
|
|
prereg_listener);
|
|
|
|
VFIOContainer *container = &scontainer->container;
|
2023-11-02 08:12:38 +01:00
|
|
|
VFIOContainerBase *bcontainer = &container->bcontainer;
|
2016-07-04 05:33:04 +02:00
|
|
|
const hwaddr gpa = section->offset_within_address_space;
|
|
|
|
hwaddr end;
|
|
|
|
int ret;
|
2022-03-23 16:57:22 +01:00
|
|
|
hwaddr page_mask = qemu_real_host_page_mask();
|
2016-07-04 05:33:04 +02:00
|
|
|
struct vfio_iommu_spapr_register_memory reg = {
|
|
|
|
.argsz = sizeof(reg),
|
|
|
|
.flags = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (vfio_prereg_listener_skipped_section(section)) {
|
|
|
|
trace_vfio_prereg_listener_region_add_skip(
|
|
|
|
section->offset_within_address_space,
|
|
|
|
section->offset_within_address_space +
|
|
|
|
int128_get64(int128_sub(section->size, int128_one())));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely((section->offset_within_address_space & ~page_mask) ||
|
|
|
|
(section->offset_within_region & ~page_mask) ||
|
|
|
|
(int128_get64(section->size) & ~page_mask))) {
|
|
|
|
error_report("%s received unaligned region", __func__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
end = section->offset_within_address_space + int128_get64(section->size);
|
|
|
|
if (gpa >= end) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memory_region_ref(section->mr);
|
|
|
|
|
|
|
|
reg.vaddr = (uintptr_t) vfio_prereg_gpa_to_vaddr(section, gpa);
|
|
|
|
reg.size = end - gpa;
|
|
|
|
|
|
|
|
ret = ioctl(container->fd, VFIO_IOMMU_SPAPR_REGISTER_MEMORY, ®);
|
|
|
|
trace_vfio_prereg_register(reg.vaddr, reg.size, ret ? -errno : 0);
|
|
|
|
if (ret) {
|
|
|
|
/*
|
|
|
|
* On the initfn path, store the first error in the container so we
|
|
|
|
* can gracefully fail. Runtime, there's not much we can do other
|
|
|
|
* than throw a hardware error.
|
|
|
|
*/
|
2023-11-02 08:12:38 +01:00
|
|
|
if (!bcontainer->initialized) {
|
|
|
|
if (!bcontainer->error) {
|
|
|
|
error_setg_errno(&bcontainer->error, -ret,
|
2019-09-24 10:25:16 +02:00
|
|
|
"Memory registering failed");
|
2016-07-04 05:33:04 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hw_error("vfio: Memory registering failed, unable to continue");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vfio_prereg_listener_region_del(MemoryListener *listener,
|
|
|
|
MemoryRegionSection *section)
|
|
|
|
{
|
2023-11-02 08:12:44 +01:00
|
|
|
VFIOSpaprContainer *scontainer = container_of(listener, VFIOSpaprContainer,
|
|
|
|
prereg_listener);
|
|
|
|
VFIOContainer *container = &scontainer->container;
|
2016-07-04 05:33:04 +02:00
|
|
|
const hwaddr gpa = section->offset_within_address_space;
|
|
|
|
hwaddr end;
|
|
|
|
int ret;
|
2022-03-23 16:57:22 +01:00
|
|
|
hwaddr page_mask = qemu_real_host_page_mask();
|
2016-07-04 05:33:04 +02:00
|
|
|
struct vfio_iommu_spapr_register_memory reg = {
|
|
|
|
.argsz = sizeof(reg),
|
|
|
|
.flags = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (vfio_prereg_listener_skipped_section(section)) {
|
|
|
|
trace_vfio_prereg_listener_region_del_skip(
|
|
|
|
section->offset_within_address_space,
|
|
|
|
section->offset_within_address_space +
|
|
|
|
int128_get64(int128_sub(section->size, int128_one())));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely((section->offset_within_address_space & ~page_mask) ||
|
|
|
|
(section->offset_within_region & ~page_mask) ||
|
|
|
|
(int128_get64(section->size) & ~page_mask))) {
|
|
|
|
error_report("%s received unaligned region", __func__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
end = section->offset_within_address_space + int128_get64(section->size);
|
|
|
|
if (gpa >= end) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
reg.vaddr = (uintptr_t) vfio_prereg_gpa_to_vaddr(section, gpa);
|
|
|
|
reg.size = end - gpa;
|
|
|
|
|
|
|
|
ret = ioctl(container->fd, VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY, ®);
|
|
|
|
trace_vfio_prereg_unregister(reg.vaddr, reg.size, ret ? -errno : 0);
|
|
|
|
}
|
|
|
|
|
2023-11-02 08:12:24 +01:00
|
|
|
static const MemoryListener vfio_prereg_listener = {
|
2021-08-17 03:35:52 +02:00
|
|
|
.name = "vfio-pre-reg",
|
2016-07-04 05:33:04 +02:00
|
|
|
.region_add = vfio_prereg_listener_region_add,
|
|
|
|
.region_del = vfio_prereg_listener_region_del,
|
|
|
|
};
|
2016-07-04 05:33:06 +02:00
|
|
|
|
2023-11-02 08:12:45 +01:00
|
|
|
static void vfio_host_win_add(VFIOSpaprContainer *scontainer, hwaddr min_iova,
|
2023-11-02 08:12:26 +01:00
|
|
|
hwaddr max_iova, uint64_t iova_pgsizes)
|
|
|
|
{
|
|
|
|
VFIOHostDMAWindow *hostwin;
|
|
|
|
|
2023-11-02 08:12:45 +01:00
|
|
|
QLIST_FOREACH(hostwin, &scontainer->hostwin_list, hostwin_next) {
|
2023-11-02 08:12:26 +01:00
|
|
|
if (ranges_overlap(hostwin->min_iova,
|
|
|
|
hostwin->max_iova - hostwin->min_iova + 1,
|
|
|
|
min_iova,
|
|
|
|
max_iova - min_iova + 1)) {
|
|
|
|
hw_error("%s: Overlapped IOMMU are not enabled", __func__);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hostwin = g_malloc0(sizeof(*hostwin));
|
|
|
|
|
|
|
|
hostwin->min_iova = min_iova;
|
|
|
|
hostwin->max_iova = max_iova;
|
|
|
|
hostwin->iova_pgsizes = iova_pgsizes;
|
2023-11-02 08:12:45 +01:00
|
|
|
QLIST_INSERT_HEAD(&scontainer->hostwin_list, hostwin, hostwin_next);
|
2023-11-02 08:12:26 +01:00
|
|
|
}
|
|
|
|
|
2023-11-02 08:12:45 +01:00
|
|
|
static int vfio_host_win_del(VFIOSpaprContainer *scontainer,
|
2023-11-02 08:12:26 +01:00
|
|
|
hwaddr min_iova, hwaddr max_iova)
|
|
|
|
{
|
|
|
|
VFIOHostDMAWindow *hostwin;
|
|
|
|
|
2023-11-02 08:12:45 +01:00
|
|
|
QLIST_FOREACH(hostwin, &scontainer->hostwin_list, hostwin_next) {
|
2023-11-02 08:12:26 +01:00
|
|
|
if (hostwin->min_iova == min_iova && hostwin->max_iova == max_iova) {
|
|
|
|
QLIST_REMOVE(hostwin, hostwin_next);
|
|
|
|
g_free(hostwin);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-11-02 08:12:45 +01:00
|
|
|
static VFIOHostDMAWindow *vfio_find_hostwin(VFIOSpaprContainer *container,
|
2023-11-02 08:12:26 +01:00
|
|
|
hwaddr iova, hwaddr end)
|
|
|
|
{
|
|
|
|
VFIOHostDMAWindow *hostwin;
|
|
|
|
bool hostwin_found = false;
|
|
|
|
|
|
|
|
QLIST_FOREACH(hostwin, &container->hostwin_list, hostwin_next) {
|
|
|
|
if (hostwin->min_iova <= iova && end <= hostwin->max_iova) {
|
|
|
|
hostwin_found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hostwin_found ? hostwin : NULL;
|
|
|
|
}
|
|
|
|
|
2023-11-02 08:12:25 +01:00
|
|
|
static int vfio_spapr_remove_window(VFIOContainer *container,
|
|
|
|
hwaddr offset_within_address_space)
|
|
|
|
{
|
|
|
|
struct vfio_iommu_spapr_tce_remove remove = {
|
|
|
|
.argsz = sizeof(remove),
|
|
|
|
.start_addr = offset_within_address_space,
|
|
|
|
};
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ioctl(container->fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
|
|
|
|
if (ret) {
|
|
|
|
error_report("Failed to remove window at %"PRIx64,
|
|
|
|
(uint64_t)remove.start_addr);
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace_vfio_spapr_remove_window(offset_within_address_space);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vfio_spapr_create_window(VFIOContainer *container,
|
|
|
|
MemoryRegionSection *section,
|
|
|
|
hwaddr *pgsize)
|
2016-07-04 05:33:06 +02:00
|
|
|
{
|
2019-02-27 09:51:45 +01:00
|
|
|
int ret = 0;
|
2023-11-02 08:12:36 +01:00
|
|
|
VFIOContainerBase *bcontainer = &container->bcontainer;
|
2017-07-11 05:56:19 +02:00
|
|
|
IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr);
|
2020-03-24 07:39:12 +01:00
|
|
|
uint64_t pagesize = memory_region_iommu_get_min_page_size(iommu_mr), pgmask;
|
2019-02-27 09:51:45 +01:00
|
|
|
unsigned entries, bits_total, bits_per_level, max_levels;
|
2016-07-04 05:33:06 +02:00
|
|
|
struct vfio_iommu_spapr_tce_create create = { .argsz = sizeof(create) };
|
2019-04-17 13:31:43 +02:00
|
|
|
long rampagesize = qemu_minrampagesize();
|
2018-06-20 11:10:12 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The host might not support the guest supported IOMMU page size,
|
|
|
|
* so we will use smaller physical IOMMU pages to back them.
|
|
|
|
*/
|
2019-02-27 09:51:46 +01:00
|
|
|
if (pagesize > rampagesize) {
|
|
|
|
pagesize = rampagesize;
|
2018-06-20 11:10:12 +02:00
|
|
|
}
|
2023-11-02 08:12:36 +01:00
|
|
|
pgmask = bcontainer->pgsizes & (pagesize | (pagesize - 1));
|
2020-03-24 07:39:12 +01:00
|
|
|
pagesize = pgmask ? (1ULL << (63 - clz64(pgmask))) : 0;
|
2018-06-20 11:10:12 +02:00
|
|
|
if (!pagesize) {
|
|
|
|
error_report("Host doesn't support page size 0x%"PRIx64
|
|
|
|
", the supported mask is 0x%lx",
|
|
|
|
memory_region_iommu_get_min_page_size(iommu_mr),
|
2023-11-02 08:12:36 +01:00
|
|
|
bcontainer->pgsizes);
|
2018-06-20 11:10:12 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2016-07-04 05:33:06 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* FIXME: For VFIO iommu types which have KVM acceleration to
|
|
|
|
* avoid bouncing all map/unmaps through qemu this way, this
|
|
|
|
* would be the right place to wire that up (tell the KVM
|
|
|
|
* device emulation the VFIO iommu handles to use).
|
|
|
|
*/
|
|
|
|
create.window_size = int128_get64(section->size);
|
|
|
|
create.page_shift = ctz64(pagesize);
|
|
|
|
/*
|
2019-02-27 09:51:45 +01:00
|
|
|
* SPAPR host supports multilevel TCE tables. We try to guess optimal
|
|
|
|
* levels number and if this fails (for example due to the host memory
|
|
|
|
* fragmentation), we increase levels. The DMA address structure is:
|
|
|
|
* rrrrrrrr rxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx iiiiiiii
|
|
|
|
* where:
|
|
|
|
* r = reserved (bits >= 55 are reserved in the existing hardware)
|
|
|
|
* i = IOMMU page offset (64K in this example)
|
|
|
|
* x = bits to index a TCE which can be split to equal chunks to index
|
|
|
|
* within the level.
|
|
|
|
* The aim is to split "x" to smaller possible number of levels.
|
2016-07-04 05:33:06 +02:00
|
|
|
*/
|
|
|
|
entries = create.window_size >> create.page_shift;
|
2019-02-27 09:51:45 +01:00
|
|
|
/* bits_total is number of "x" needed */
|
|
|
|
bits_total = ctz64(entries * sizeof(uint64_t));
|
|
|
|
/*
|
|
|
|
* bits_per_level is a safe guess of how much we can allocate per level:
|
|
|
|
* 8 is the current minimum for CONFIG_FORCE_MAX_ZONEORDER and MAX_ORDER
|
|
|
|
* is usually bigger than that.
|
2019-10-13 04:11:45 +02:00
|
|
|
* Below we look at qemu_real_host_page_size as TCEs are allocated from
|
|
|
|
* system pages.
|
2019-02-27 09:51:45 +01:00
|
|
|
*/
|
2022-03-23 16:57:22 +01:00
|
|
|
bits_per_level = ctz64(qemu_real_host_page_size()) + 8;
|
2019-02-27 09:51:45 +01:00
|
|
|
create.levels = bits_total / bits_per_level;
|
|
|
|
if (bits_total % bits_per_level) {
|
|
|
|
++create.levels;
|
|
|
|
}
|
2022-03-23 16:57:22 +01:00
|
|
|
max_levels = (64 - create.page_shift) / ctz64(qemu_real_host_page_size());
|
2019-02-27 09:51:45 +01:00
|
|
|
for ( ; create.levels <= max_levels; ++create.levels) {
|
|
|
|
ret = ioctl(container->fd, VFIO_IOMMU_SPAPR_TCE_CREATE, &create);
|
|
|
|
if (!ret) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-07-04 05:33:06 +02:00
|
|
|
if (ret) {
|
|
|
|
error_report("Failed to create a window, ret = %d (%m)", ret);
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (create.start_addr != section->offset_within_address_space) {
|
|
|
|
vfio_spapr_remove_window(container, create.start_addr);
|
|
|
|
|
|
|
|
error_report("Host doesn't support DMA window at %"HWADDR_PRIx", must be %"PRIx64,
|
|
|
|
section->offset_within_address_space,
|
|
|
|
(uint64_t)create.start_addr);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
trace_vfio_spapr_create_window(create.page_shift,
|
2019-02-27 09:51:45 +01:00
|
|
|
create.levels,
|
2016-07-04 05:33:06 +02:00
|
|
|
create.window_size,
|
|
|
|
create.start_addr);
|
|
|
|
*pgsize = pagesize;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-11-02 08:12:43 +01:00
|
|
|
static int
|
|
|
|
vfio_spapr_container_add_section_window(VFIOContainerBase *bcontainer,
|
|
|
|
MemoryRegionSection *section,
|
|
|
|
Error **errp)
|
2023-11-02 08:12:23 +01:00
|
|
|
{
|
2023-11-02 08:12:43 +01:00
|
|
|
VFIOContainer *container = container_of(bcontainer, VFIOContainer,
|
|
|
|
bcontainer);
|
2023-11-02 08:12:45 +01:00
|
|
|
VFIOSpaprContainer *scontainer = container_of(container, VFIOSpaprContainer,
|
|
|
|
container);
|
2023-11-02 08:12:23 +01:00
|
|
|
VFIOHostDMAWindow *hostwin;
|
|
|
|
hwaddr pgsize = 0;
|
|
|
|
int ret;
|
|
|
|
|
2023-11-02 08:12:26 +01:00
|
|
|
/*
|
|
|
|
* VFIO_SPAPR_TCE_IOMMU supports a single host window between
|
|
|
|
* [dma32_window_start, dma32_window_size), we need to ensure
|
|
|
|
* the section fall in this range.
|
|
|
|
*/
|
|
|
|
if (container->iommu_type == VFIO_SPAPR_TCE_IOMMU) {
|
|
|
|
hwaddr iova, end;
|
|
|
|
|
|
|
|
iova = section->offset_within_address_space;
|
|
|
|
end = iova + int128_get64(section->size) - 1;
|
|
|
|
|
2023-11-02 08:12:45 +01:00
|
|
|
if (!vfio_find_hostwin(scontainer, iova, end)) {
|
2023-11-02 08:12:26 +01:00
|
|
|
error_setg(errp, "Container %p can't map guest IOVA region"
|
|
|
|
" 0x%"HWADDR_PRIx"..0x%"HWADDR_PRIx, container,
|
|
|
|
iova, end);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-11-02 08:12:23 +01:00
|
|
|
if (container->iommu_type != VFIO_SPAPR_TCE_v2_IOMMU) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For now intersections are not allowed, we may relax this later */
|
2023-11-02 08:12:45 +01:00
|
|
|
QLIST_FOREACH(hostwin, &scontainer->hostwin_list, hostwin_next) {
|
2023-11-02 08:12:23 +01:00
|
|
|
if (ranges_overlap(hostwin->min_iova,
|
|
|
|
hostwin->max_iova - hostwin->min_iova + 1,
|
|
|
|
section->offset_within_address_space,
|
|
|
|
int128_get64(section->size))) {
|
|
|
|
error_setg(errp,
|
|
|
|
"region [0x%"PRIx64",0x%"PRIx64"] overlaps with existing"
|
|
|
|
"host DMA window [0x%"PRIx64",0x%"PRIx64"]",
|
|
|
|
section->offset_within_address_space,
|
|
|
|
section->offset_within_address_space +
|
|
|
|
int128_get64(section->size) - 1,
|
|
|
|
hostwin->min_iova, hostwin->max_iova);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = vfio_spapr_create_window(container, section, &pgsize);
|
|
|
|
if (ret) {
|
|
|
|
error_setg_errno(errp, -ret, "Failed to create SPAPR window");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-11-02 08:12:45 +01:00
|
|
|
vfio_host_win_add(scontainer, section->offset_within_address_space,
|
2023-11-02 08:12:23 +01:00
|
|
|
section->offset_within_address_space +
|
|
|
|
int128_get64(section->size) - 1, pgsize);
|
|
|
|
#ifdef CONFIG_KVM
|
|
|
|
if (kvm_enabled()) {
|
|
|
|
VFIOGroup *group;
|
|
|
|
IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr);
|
|
|
|
struct kvm_vfio_spapr_tce param;
|
|
|
|
struct kvm_device_attr attr = {
|
|
|
|
.group = KVM_DEV_VFIO_GROUP,
|
|
|
|
.attr = KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE,
|
|
|
|
.addr = (uint64_t)(unsigned long)¶m,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!memory_region_iommu_get_attr(iommu_mr, IOMMU_ATTR_SPAPR_TCE_FD,
|
|
|
|
¶m.tablefd)) {
|
|
|
|
QLIST_FOREACH(group, &container->group_list, container_next) {
|
|
|
|
param.groupfd = group->fd;
|
|
|
|
if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {
|
|
|
|
error_setg_errno(errp, errno,
|
|
|
|
"vfio: failed GROUP_SET_SPAPR_TCE for "
|
|
|
|
"KVM VFIO device %d and group fd %d",
|
|
|
|
param.tablefd, param.groupfd);
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
trace_vfio_spapr_group_attach(param.groupfd, param.tablefd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-11-02 08:12:43 +01:00
|
|
|
static void
|
|
|
|
vfio_spapr_container_del_section_window(VFIOContainerBase *bcontainer,
|
|
|
|
MemoryRegionSection *section)
|
2023-11-02 08:12:23 +01:00
|
|
|
{
|
2023-11-02 08:12:43 +01:00
|
|
|
VFIOContainer *container = container_of(bcontainer, VFIOContainer,
|
|
|
|
bcontainer);
|
2023-11-02 08:12:45 +01:00
|
|
|
VFIOSpaprContainer *scontainer = container_of(container, VFIOSpaprContainer,
|
|
|
|
container);
|
2023-11-02 08:12:43 +01:00
|
|
|
|
2023-11-02 08:12:23 +01:00
|
|
|
if (container->iommu_type != VFIO_SPAPR_TCE_v2_IOMMU) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
vfio_spapr_remove_window(container,
|
|
|
|
section->offset_within_address_space);
|
2023-11-02 08:12:45 +01:00
|
|
|
if (vfio_host_win_del(scontainer,
|
2023-11-02 08:12:23 +01:00
|
|
|
section->offset_within_address_space,
|
|
|
|
section->offset_within_address_space +
|
|
|
|
int128_get64(section->size) - 1) < 0) {
|
|
|
|
hw_error("%s: Cannot delete missing window at %"HWADDR_PRIx,
|
|
|
|
__func__, section->offset_within_address_space);
|
|
|
|
}
|
|
|
|
}
|
2023-11-02 08:12:24 +01:00
|
|
|
|
2023-12-19 07:58:16 +01:00
|
|
|
static void vfio_spapr_container_release(VFIOContainerBase *bcontainer)
|
|
|
|
{
|
|
|
|
VFIOContainer *container = container_of(bcontainer, VFIOContainer,
|
|
|
|
bcontainer);
|
|
|
|
VFIOSpaprContainer *scontainer = container_of(container, VFIOSpaprContainer,
|
|
|
|
container);
|
|
|
|
VFIOHostDMAWindow *hostwin, *next;
|
|
|
|
|
|
|
|
if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) {
|
|
|
|
memory_listener_unregister(&scontainer->prereg_listener);
|
|
|
|
}
|
|
|
|
QLIST_FOREACH_SAFE(hostwin, &scontainer->hostwin_list, hostwin_next,
|
|
|
|
next) {
|
|
|
|
QLIST_REMOVE(hostwin, hostwin_next);
|
|
|
|
g_free(hostwin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-19 07:58:22 +01:00
|
|
|
static int vfio_spapr_container_setup(VFIOContainerBase *bcontainer,
|
|
|
|
Error **errp)
|
2023-11-02 08:12:24 +01:00
|
|
|
{
|
2023-12-19 07:58:22 +01:00
|
|
|
VFIOContainer *container = container_of(bcontainer, VFIOContainer,
|
|
|
|
bcontainer);
|
2023-11-02 08:12:44 +01:00
|
|
|
VFIOSpaprContainer *scontainer = container_of(container, VFIOSpaprContainer,
|
|
|
|
container);
|
2023-11-02 08:12:24 +01:00
|
|
|
struct vfio_iommu_spapr_tce_info info;
|
|
|
|
bool v2 = container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU;
|
|
|
|
int ret, fd = container->fd;
|
|
|
|
|
2023-11-02 08:12:45 +01:00
|
|
|
QLIST_INIT(&scontainer->hostwin_list);
|
2023-11-02 08:12:26 +01:00
|
|
|
|
2023-11-02 08:12:24 +01:00
|
|
|
/*
|
|
|
|
* The host kernel code implementing VFIO_IOMMU_DISABLE is called
|
|
|
|
* when container fd is closed so we do not call it explicitly
|
|
|
|
* in this file.
|
|
|
|
*/
|
|
|
|
if (!v2) {
|
|
|
|
ret = ioctl(fd, VFIO_IOMMU_ENABLE);
|
|
|
|
if (ret) {
|
|
|
|
error_setg_errno(errp, errno, "failed to enable container");
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
} else {
|
2023-11-02 08:12:44 +01:00
|
|
|
scontainer->prereg_listener = vfio_prereg_listener;
|
2023-11-02 08:12:24 +01:00
|
|
|
|
2023-11-02 08:12:44 +01:00
|
|
|
memory_listener_register(&scontainer->prereg_listener,
|
2023-11-02 08:12:24 +01:00
|
|
|
&address_space_memory);
|
2023-11-02 08:12:38 +01:00
|
|
|
if (bcontainer->error) {
|
2023-11-02 08:12:24 +01:00
|
|
|
ret = -1;
|
2023-11-02 08:12:38 +01:00
|
|
|
error_propagate_prepend(errp, bcontainer->error,
|
2023-11-02 08:12:24 +01:00
|
|
|
"RAM memory listener initialization failed: ");
|
|
|
|
goto listener_unregister_exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
info.argsz = sizeof(info);
|
|
|
|
ret = ioctl(fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
|
|
|
|
if (ret) {
|
|
|
|
error_setg_errno(errp, errno,
|
|
|
|
"VFIO_IOMMU_SPAPR_TCE_GET_INFO failed");
|
|
|
|
ret = -errno;
|
|
|
|
goto listener_unregister_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v2) {
|
2023-11-02 08:12:36 +01:00
|
|
|
bcontainer->pgsizes = info.ddw.pgsizes;
|
2023-11-02 08:12:24 +01:00
|
|
|
/*
|
|
|
|
* There is a default window in just created container.
|
|
|
|
* To make region_add/del simpler, we better remove this
|
|
|
|
* window now and let those iommu_listener callbacks
|
|
|
|
* create/remove them when needed.
|
|
|
|
*/
|
|
|
|
ret = vfio_spapr_remove_window(container, info.dma32_window_start);
|
|
|
|
if (ret) {
|
|
|
|
error_setg_errno(errp, -ret,
|
|
|
|
"failed to remove existing window");
|
|
|
|
goto listener_unregister_exit;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* The default table uses 4K pages */
|
2023-11-02 08:12:36 +01:00
|
|
|
bcontainer->pgsizes = 0x1000;
|
2023-11-02 08:12:45 +01:00
|
|
|
vfio_host_win_add(scontainer, info.dma32_window_start,
|
2023-11-02 08:12:24 +01:00
|
|
|
info.dma32_window_start +
|
|
|
|
info.dma32_window_size - 1,
|
|
|
|
0x1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
listener_unregister_exit:
|
|
|
|
if (v2) {
|
2023-11-02 08:12:44 +01:00
|
|
|
memory_listener_unregister(&scontainer->prereg_listener);
|
2023-11-02 08:12:24 +01:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2023-12-19 07:58:22 +01:00
|
|
|
|
|
|
|
static void vfio_iommu_spapr_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass);
|
|
|
|
|
|
|
|
vioc->add_window = vfio_spapr_container_add_section_window;
|
|
|
|
vioc->del_window = vfio_spapr_container_del_section_window;
|
|
|
|
vioc->release = vfio_spapr_container_release;
|
|
|
|
vioc->setup = vfio_spapr_container_setup;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const TypeInfo types[] = {
|
|
|
|
{
|
|
|
|
.name = TYPE_VFIO_IOMMU_SPAPR,
|
|
|
|
.parent = TYPE_VFIO_IOMMU_LEGACY,
|
|
|
|
.class_init = vfio_iommu_spapr_class_init,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
DEFINE_TYPES(types)
|