[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-ppc] [PATCH qemu v17 10/12] vfio/spapr: Create DMA window dyna
From: |
Alex Williamson |
Subject: |
Re: [Qemu-ppc] [PATCH qemu v17 10/12] vfio/spapr: Create DMA window dynamically (SPAPR IOMMU v2) |
Date: |
Fri, 3 Jun 2016 10:50:15 -0600 |
On Wed, 1 Jun 2016 18:57:41 +1000
Alexey Kardashevskiy <address@hidden> wrote:
> New VFIO_SPAPR_TCE_v2_IOMMU type supports dynamic DMA window management.
> This adds ability to VFIO common code to dynamically allocate/remove
> DMA windows in the host kernel when new VFIO container is added/removed.
>
> This adds VFIO_IOMMU_SPAPR_TCE_CREATE ioctl to vfio_listener_region_add
> and adds just created IOMMU into the host IOMMU list; the opposite
> action is taken in vfio_listener_region_del.
>
> When creating a new window, this uses heuristic to decide on the TCE table
> levels number.
>
> This should cause no guest visible change in behavior.
>
> Signed-off-by: Alexey Kardashevskiy <address@hidden>
> ---
> Changes:
> v17:
> * moved spapr window create/remove helpers to separate file
> * added hw_error() if vfio_host_win_del() failed
>
> v16:
> * used memory_region_iommu_get_page_sizes() in vfio_listener_region_add()
> * enforced no intersections between windows
>
> v14:
> * new to the series
> ---
> hw/vfio/common.c | 76
> +++++++++++++++++++++++++++++++++++++------
> hw/vfio/spapr.c | 70 +++++++++++++++++++++++++++++++++++++++
> include/hw/vfio/vfio-common.h | 6 ++++
> trace-events | 2 ++
> 4 files changed, 144 insertions(+), 10 deletions(-)
>
> diff --git a/hw/vfio/common.c b/hw/vfio/common.c
> index 52b08fd..7f55c26 100644
> --- a/hw/vfio/common.c
> +++ b/hw/vfio/common.c
> @@ -275,6 +275,18 @@ static void vfio_host_win_add(VFIOContainer *container,
> QLIST_INSERT_HEAD(&container->hostwin_list, hostwin, hostwin_next);
> }
>
> +static int vfio_host_win_del(VFIOContainer *container, hwaddr min_iova)
> +{
> + VFIOHostDMAWindow *hostwin = vfio_host_win_lookup(container, min_iova,
> 1);
> +
+1 David's comment for exact match.
> + if (!hostwin) {
> + return -1;
> + }
> + QLIST_REMOVE(hostwin, hostwin_next);
> +
> + return 0;
> +}
> +
> static bool vfio_listener_skipped_section(MemoryRegionSection *section)
> {
> return (!memory_region_is_ram(section->mr) &&
> @@ -388,6 +400,30 @@ static void vfio_listener_region_add(MemoryListener
> *listener,
> }
> end = int128_get64(int128_sub(llend, int128_one()));
>
> + if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) {
> + VFIOHostDMAWindow *hostwin;
> + hwaddr pgsize = 0;
> +
> + /* For now intersections are not allowed, we may relax this later */
> + QLIST_FOREACH(hostwin, &container->hostwin_list, hostwin_next) {
> + if (ranges_overlap(hostwin->min_iova,
> + hostwin->max_iova - hostwin->min_iova + 1,
> + section->offset_within_address_space,
> + int128_get64(section->size))) {
> + goto fail;
> + }
> + }
> +
> + ret = vfio_spapr_create_window(container, section, &pgsize);
> + if (ret) {
> + goto fail;
> + }
> +
> + vfio_host_win_add(container, section->offset_within_address_space,
> + section->offset_within_address_space +
> + int128_get64(section->size) - 1, pgsize);
> + }
> +
> if (!vfio_host_win_lookup(container, iova, end)) {
> error_report("vfio: IOMMU container %p can't map guest IOVA region"
> " 0x%"HWADDR_PRIx"..0x%"HWADDR_PRIx,
> @@ -523,6 +559,18 @@ static void vfio_listener_region_del(MemoryListener
> *listener,
> "0x%"HWADDR_PRIx") = %d (%m)",
> container, iova, int128_get64(llsize), ret);
> }
> +
> + if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) {
> + vfio_spapr_remove_window(container,
> + section->offset_within_address_space);
> + if (vfio_host_win_del(container,
> + section->offset_within_address_space) < 0) {
> + hw_error("%s: Cannot delete missing window at %"HWADDR_PRIx,
> + __func__, section->offset_within_address_space);
> + }
> +
> + trace_vfio_spapr_remove_window(section->offset_within_address_space);
Trace within the function like you do on the create_window side.
> + }
> }
>
> static const MemoryListener vfio_memory_listener = {
> @@ -960,11 +1008,6 @@ static int vfio_connect_container(VFIOGroup *group,
> AddressSpace *as)
> }
> }
>
> - /*
> - * This only considers the host IOMMU's 32-bit window. At
> - * some point we need to add support for the optional 64-bit
> - * window and dynamic windows
> - */
> info.argsz = sizeof(info);
> ret = ioctl(fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
> if (ret) {
> @@ -973,11 +1016,24 @@ static int vfio_connect_container(VFIOGroup *group,
> AddressSpace *as)
> goto listener_release_exit;
> }
>
> - /* The default table uses 4K pages */
> - vfio_host_win_add(container, info.dma32_window_start,
> - info.dma32_window_start +
> - info.dma32_window_size - 1,
> - 0x1000);
> + if (v2) {
> + /*
> + * 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) {
> + goto free_container_exit;
> + }
> + } else {
> + /* The default table uses 4K pages */
> + vfio_host_win_add(container, info.dma32_window_start,
> + info.dma32_window_start +
> + info.dma32_window_size - 1,
> + 0x1000);
> + }
> } else {
> error_report("vfio: No available IOMMU models");
> ret = -EINVAL;
> diff --git a/hw/vfio/spapr.c b/hw/vfio/spapr.c
> index f339472..0c784c4 100644
> --- a/hw/vfio/spapr.c
> +++ b/hw/vfio/spapr.c
> @@ -135,3 +135,73 @@ const MemoryListener vfio_prereg_listener = {
> .region_add = vfio_prereg_listener_region_add,
> .region_del = vfio_prereg_listener_region_del,
> };
> +
> +int vfio_spapr_create_window(VFIOContainer *container,
> + MemoryRegionSection *section,
> + hwaddr *pgsize)
> +{
> + int ret;
> + unsigned pagesizes = memory_region_iommu_get_page_sizes(section->mr);
> + unsigned pagesize = (hwaddr)1 << ctz64(pagesizes);
> + unsigned entries, pages;
> + struct vfio_iommu_spapr_tce_create create = { .argsz = sizeof(create) };
> +
> + /*
> + * 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);
> + /*
> + * SPAPR host supports multilevel TCE tables, there is some
> + * heuristic to decide how many levels we want for our table:
> + * 0..64 = 1; 65..4096 = 2; 4097..262144 = 3; 262145.. = 4
> + */
> + entries = create.window_size >> create.page_shift;
> + pages = MAX((entries * sizeof(uint64_t)) / getpagesize(), 1);
> + pages = MAX(pow2ceil(pages) - 1, 1); /* Round up */
> + create.levels = ctz64(pages) / 6 + 1;
> +
> + ret = ioctl(container->fd, VFIO_IOMMU_SPAPR_TCE_CREATE, &create);
> + 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,
> + create.start_addr);
> + ioctl(container->fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
> + return -EINVAL;
> + }
> + trace_vfio_spapr_create_window(create.page_shift,
> + create.window_size,
> + create.start_addr);
> + *pgsize = pagesize;
> +
> + return 0;
> +}
> +
> +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,
> + remove.start_addr);
> + return -errno;
> + }
> +
> + return 0;
> +}
> diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h
> index c76ddc4..7e80382 100644
> --- a/include/hw/vfio/vfio-common.h
> +++ b/include/hw/vfio/vfio-common.h
> @@ -167,4 +167,10 @@ int vfio_get_dev_region_info(VFIODevice *vbasedev,
> uint32_t type,
> #endif
> extern const MemoryListener vfio_prereg_listener;
>
> +int vfio_spapr_create_window(VFIOContainer *container,
> + MemoryRegionSection *section,
> + hwaddr *pgsize);
> +int vfio_spapr_remove_window(VFIOContainer *container,
> + hwaddr offset_within_address_space);
> +
> #endif /* !HW_VFIO_VFIO_COMMON_H */
> diff --git a/trace-events b/trace-events
> index ddb8676..ec32c20 100644
> --- a/trace-events
> +++ b/trace-events
> @@ -1768,6 +1768,8 @@ vfio_region_sparse_mmap_entry(int i, unsigned long
> start, unsigned long end) "sp
> vfio_get_dev_region(const char *name, int index, uint32_t type, uint32_t
> subtype) "%s index %d, %08x/%0x8"
> vfio_ram_register(uint64_t va, uint64_t size, int ret) "va=%"PRIx64"
> size=%"PRIx64" ret=%d"
> vfio_ram_unregister(uint64_t va, uint64_t size, int ret) "va=%"PRIx64"
> size=%"PRIx64" ret=%d"
> +vfio_spapr_create_window(int ps, uint64_t ws, uint64_t off) "pageshift=0x%x
> winsize=0x%"PRIx64" offset=0x%"PRIx64
> +vfio_spapr_remove_window(uint64_t off) "offset=%"PRIx64
Also not in common.c
>
> # hw/vfio/platform.c
> vfio_platform_base_device_init(char *name, int groupid) "%s belongs to group
> #%d"