From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48888) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bFXHt-0002Sw-8t for qemu-devel@nongnu.org; Tue, 21 Jun 2016 21:49:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bFXHq-0004K3-DB for qemu-devel@nongnu.org; Tue, 21 Jun 2016 21:49:04 -0400 Date: Wed, 22 Jun 2016 11:29:07 +1000 From: David Gibson Message-ID: <20160622012907.GH17957@voom.fritz.box> References: <1466471645-5396-1-git-send-email-aik@ozlabs.ru> <1466471645-5396-5-git-send-email-aik@ozlabs.ru> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="4C6bbPZ6c/S1npyF" Content-Disposition: inline In-Reply-To: <1466471645-5396-5-git-send-email-aik@ozlabs.ru> Subject: Re: [Qemu-devel] [PATCH qemu v18 4/5] vfio/spapr: Create DMA window dynamically (SPAPR IOMMU v2) List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Alexey Kardashevskiy Cc: qemu-devel@nongnu.org, qemu-ppc@nongnu.org, Alex Williamson --4C6bbPZ6c/S1npyF Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Jun 21, 2016 at 11:14:04AM +1000, Alexey Kardashevskiy 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. >=20 > This adds a helper to vfio_listener_region_add which makes > VFIO_IOMMU_SPAPR_TCE_CREATE ioctl and adds just created IOMMU into > the host IOMMU list; the opposite action is taken in > vfio_listener_region_del. >=20 > When creating a new window, this uses heuristic to decide on the TCE table > levels number. >=20 > This should cause no guest visible change in behavior. >=20 > Signed-off-by: Alexey Kardashevskiy Reviewed-by: David Gibson > --- > Changes: > v18: > * moved trace definitions under hw/vfio/spapr.c section > * moved trace_vfio_spapr_remove_window to vfio_spapr_remove_window() > * vfio_host_win_del() now checks for exact window size > * one ctz() less in vfio_spapr_create_window() >=20 > v17: > * moved spapr window create/remove helpers to separate file > * added hw_error() if vfio_host_win_del() failed >=20 > v16: > * used memory_region_iommu_get_page_sizes() in vfio_listener_region_add() > * enforced no intersections between windows >=20 > v14: > * new to the series > --- > hw/vfio/common.c | 79 +++++++++++++++++++++++++++++++++++++= ------ > hw/vfio/spapr.c | 71 ++++++++++++++++++++++++++++++++++++++ > include/hw/vfio/vfio-common.h | 6 ++++ > trace-events | 2 ++ > 4 files changed, 148 insertions(+), 10 deletions(-) >=20 > diff --git a/hw/vfio/common.c b/hw/vfio/common.c > index b53a1db..8e3466c 100644 > --- a/hw/vfio/common.c > +++ b/hw/vfio/common.c > @@ -265,6 +265,21 @@ static void vfio_host_win_add(VFIOContainer *contain= er, > QLIST_INSERT_HEAD(&container->hostwin_list, hostwin, hostwin_next); > } > =20 > +static int vfio_host_win_del(VFIOContainer *container, hwaddr min_iova, > + hwaddr max_iova) > +{ > + VFIOHostDMAWindow *hostwin; > + > + QLIST_FOREACH(hostwin, &container->hostwin_list, hostwin_next) { > + if (hostwin->min_iova =3D=3D min_iova && hostwin->max_iova =3D= =3D max_iova) { > + QLIST_REMOVE(hostwin, hostwin_next); > + return 0; > + } > + } > + > + return -1; > +} > + > static bool vfio_listener_skipped_section(MemoryRegionSection *section) > { > return (!memory_region_is_ram(section->mr) && > @@ -380,6 +395,30 @@ static void vfio_listener_region_add(MemoryListener = *listener, > } > end =3D int128_get64(int128_sub(llend, int128_one())); > =20 > + if (container->iommu_type =3D=3D VFIO_SPAPR_TCE_v2_IOMMU) { > + VFIOHostDMAWindow *hostwin; > + hwaddr pgsize =3D 0; > + > + /* For now intersections are not allowed, we may relax this late= r */ > + 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 =3D vfio_spapr_create_window(container, section, &pgsize); > + if (ret) { > + goto fail; > + } > + > + vfio_host_win_add(container, section->offset_within_address_spac= e, > + section->offset_within_address_space + > + int128_get64(section->size) - 1, pgsize); > + } > + > hostwin_found =3D false; > QLIST_FOREACH(hostwin, &container->hostwin_list, hostwin_next) { > if (hostwin->min_iova <=3D iova && end <=3D hostwin->max_iova) { > @@ -522,6 +561,18 @@ static void vfio_listener_region_del(MemoryListener = *listener, > "0x%"HWADDR_PRIx") =3D %d (%m)", > container, iova, int128_get64(llsize), ret); > } > + > + if (container->iommu_type =3D=3D 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, > + 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); > + } > + } > } > =20 > static const MemoryListener vfio_memory_listener =3D { > @@ -960,11 +1011,6 @@ static int vfio_connect_container(VFIOGroup *group,= AddressSpace *as) > } > } > =20 > - /* > - * 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 =3D sizeof(info); > ret =3D ioctl(fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info); > if (ret) { > @@ -976,11 +1022,24 @@ static int vfio_connect_container(VFIOGroup *group= , AddressSpace *as) > goto listener_release_exit; > } > =20 > - /* 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 =3D vfio_spapr_remove_window(container, info.dma32_windo= w_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 =3D -EINVAL; > diff --git a/hw/vfio/spapr.c b/hw/vfio/spapr.c > index 5c29bec..852da0b 100644 > --- a/hw/vfio/spapr.c > +++ b/hw/vfio/spapr.c > @@ -137,3 +137,74 @@ const MemoryListener vfio_prereg_listener =3D { > .region_add =3D vfio_prereg_listener_region_add, > .region_del =3D vfio_prereg_listener_region_del, > }; > + > +int vfio_spapr_create_window(VFIOContainer *container, > + MemoryRegionSection *section, > + hwaddr *pgsize) > +{ > + int ret; > + unsigned pagesize =3D memory_region_iommu_get_min_page_size(section-= >mr); > + unsigned entries, pages; > + struct vfio_iommu_spapr_tce_create create =3D { .argsz =3D sizeof(cr= eate) }; > + > + /* > + * 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 =3D int128_get64(section->size); > + create.page_shift =3D ctz64(pagesize); > + /* > + * SPAPR host supports multilevel TCE tables, there is some > + * heuristic to decide how many levels we want for our table: > + * 0..64 =3D 1; 65..4096 =3D 2; 4097..262144 =3D 3; 262145.. =3D 4 > + */ > + entries =3D create.window_size >> create.page_shift; > + pages =3D MAX((entries * sizeof(uint64_t)) / getpagesize(), 1); > + pages =3D MAX(pow2ceil(pages) - 1, 1); /* Round up */ > + create.levels =3D ctz64(pages) / 6 + 1; > + > + ret =3D ioctl(container->fd, VFIO_IOMMU_SPAPR_TCE_CREATE, &create); > + if (ret) { > + error_report("Failed to create a window, ret =3D %d (%m)", ret); > + return -errno; > + } > + > + if (create.start_addr !=3D 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 =3D pagesize; > + > + return 0; > +} > + > +int vfio_spapr_remove_window(VFIOContainer *container, > + hwaddr offset_within_address_space) > +{ > + struct vfio_iommu_spapr_tce_remove remove =3D { > + .argsz =3D sizeof(remove), > + .start_addr =3D offset_within_address_space, > + }; > + int ret; > + > + ret =3D ioctl(container->fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove); > + if (ret) { > + error_report("Failed to remove window at %"PRIx64, > + remove.start_addr); > + return -errno; > + } > + > + trace_vfio_spapr_remove_window(offset_within_address_space); > + > + return 0; > +} > diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h > index b1f3e92..07f7188 100644 > --- a/include/hw/vfio/vfio-common.h > +++ b/include/hw/vfio/vfio-common.h > @@ -168,4 +168,10 @@ int vfio_get_dev_region_info(VFIODevice *vbasedev, u= int32_t type, > #endif > extern const MemoryListener vfio_prereg_listener; > =20 > +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 0b1583f..7e94d92 100644 > --- a/trace-events > +++ b/trace-events > @@ -1775,6 +1775,8 @@ vfio_prereg_listener_region_add_skip(uint64_t start= , uint64_t end) "SKIPPING reg > vfio_prereg_listener_region_del_skip(uint64_t start, uint64_t end) "SKIP= PING region_del %"PRIx64" - %"PRIx64 > vfio_prereg_register(uint64_t va, uint64_t size, int ret) "va=3D%"PRIx64= " size=3D%"PRIx64" ret=3D%d" > vfio_prereg_unregister(uint64_t va, uint64_t size, int ret) "va=3D%"PRIx= 64" size=3D%"PRIx64" ret=3D%d" > +vfio_spapr_create_window(int ps, uint64_t ws, uint64_t off) "pageshift= =3D0x%x winsize=3D0x%"PRIx64" offset=3D0x%"PRIx64 > +vfio_spapr_remove_window(uint64_t off) "offset=3D%"PRIx64 > =20 > # hw/vfio/platform.c > vfio_platform_base_device_init(char *name, int groupid) "%s belongs to g= roup #%d" --=20 David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson --4C6bbPZ6c/S1npyF Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBAgAGBQJXaenjAAoJEGw4ysog2bOSi5YQAIpf2sFDI0jfYdThc6lkRoJS EfVsiTnhyHsWft76Zp1GogSE1m6lXD542ejAuQ1uxBNHMm5gHDJwZ6YZAsiGeh1Y xsuaXIu4nCrKhK+RkVAYSQWrlVROgxlG0A9tw/b0r0e7QqmOuy1V8N75c2LjbuJK FyrN8BlEaqKtITQCz3K1+vn2mTgiY+m0OAb9Avoh7sq4ZAUh9knwyORxJP2tYh6o 6exowomze9PnslCW4YDNOOIoimR33ePZpmEFYTl3Ejm38zU9+uCPIPxlPIgop9Fy XqwuMbP1/GfawDgtGAKat+ciqf9pC3YwvQx6NzuN0lscEafl0/kkoXeOkgU+E9oz yZimMX+bq+0gKhjP20EgN2i0K21eypeNPYTe0sE5Qrxr1Ga2cA3nZAzuBeFNbaSd kMJFGhXyWL7KJ4H+Vm3vQevpblxuG82xECXgfyylzjtEHp/pcIHXLlLDT+F0nd1s Di2FHDjTJCbG+6BYm1ceuam2y//mlrXwv8v34V3HZp3+vSqfs5kRcSAwDj7YEAs+ BnhNotMq+h8LickuBMKn+6FgXXG9vEk7jHVkcoDzp/XorW1nPn/XuEYSCBpZLg8g toAxjPezXn6cKm86YGaxxsvRyENDTV6v3CrCWubNHlQCTTvLtuDhSK71bE9VCs+Z THgBQUmUMZe0JutjVZWA =624c -----END PGP SIGNATURE----- --4C6bbPZ6c/S1npyF--