From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36142) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bAWZs-0001KP-BS for qemu-devel@nongnu.org; Wed, 08 Jun 2016 02:02:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bAWZn-0003TR-6o for qemu-devel@nongnu.org; Wed, 08 Jun 2016 02:02:55 -0400 Date: Wed, 8 Jun 2016 15:56:02 +1000 From: David Gibson Message-ID: <20160608055602.GW9226@voom.fritz.box> References: <1464771463-37214-1-git-send-email-aik@ozlabs.ru> <201606011012.u51A9A6i023070@mx0a-001b2d01.pphosted.com> <20160603073723.GV1087@voom.fritz.box> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="RMorpr61ug7CvG36" Content-Disposition: inline In-Reply-To: Subject: Re: [Qemu-devel] [PATCH qemu v17 10/12] 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, Alexander Graf , Alex Williamson --RMorpr61ug7CvG36 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Jun 06, 2016 at 04:45:50PM +1000, Alexey Kardashevskiy wrote: > On 03/06/16 17:37, David Gibson wrote: > > On Wed, Jun 01, 2016 at 06:57:41PM +1000, Alexey Kardashevskiy wrote: > >> New VFIO_SPAPR_TCE_v2_IOMMU type supports dynamic DMA window managemen= t. > >> This adds ability to VFIO common code to dynamically allocate/remove > >> DMA windows in the host kernel when new VFIO container is added/remove= d. > >> > >> 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 t= able > >> levels number. > >> > >> This should cause no guest visible change in behavior. > >> > >> Signed-off-by: Alexey Kardashevskiy > >> --- > >> 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_ad= d() > >> * 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 *cont= ainer, > >> QLIST_INSERT_HEAD(&container->hostwin_list, hostwin, hostwin_next= ); > >> } > >> =20 > >> +static int vfio_host_win_del(VFIOContainer *container, hwaddr min_iov= a) > >> +{ > >> + VFIOHostDMAWindow *hostwin =3D vfio_host_win_lookup(container, mi= n_iova, 1); > >=20 > > Hrm.. and for this case I think you want exact match, rather than > > looking for range inclusion. >=20 > I suppose so, I'll change this. >=20 >=20 > >> + > >> + if (!hostwin) { > >> + return -1; > >> + } > >> + QLIST_REMOVE(hostwin, hostwin_next); > >> + > >> + return 0; > >> +} > >> + > >> static bool vfio_listener_skipped_section(MemoryRegionSection *sectio= n) > >> { > >> return (!memory_region_is_ram(section->mr) && > >> @@ -388,6 +400,30 @@ static void vfio_listener_region_add(MemoryListen= er *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 l= ater */ > >> + 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_s= pace, > >> + 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 r= egion" > >> " 0x%"HWADDR_PRIx"..0x%"HWADDR_PRIx, > >> @@ -523,6 +559,18 @@ static void vfio_listener_region_del(MemoryListen= er *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= ); > >=20 > > Should check for error here. >=20 >=20 > And do what here? vfio_spapr_remove_window() calls error_report() already > and I still want to remove the host window here. Hmm.. yes, I guess so. Probably best to have a comment here saying why it's safe to ignore an error you should usually test for. > >=20 > >> + if (vfio_host_win_del(container, > >> + section->offset_within_address_space) <= 0) { > >> + hw_error("%s: Cannot delete missing window at %"HWADDR_PR= Ix, > >> + __func__, section->offset_within_address_space); > >=20 > > Personally I think assert() would be better here, but Alex doesn't > > like them so I'm ok with this. > >=20 > >> + } > >> + > >> + trace_vfio_spapr_remove_window(section->offset_within_address= _space); > >> + } > >> } > >> =20 > >> static const MemoryListener vfio_memory_listener =3D { > >> @@ -960,11 +1008,6 @@ static int vfio_connect_container(VFIOGroup *gro= up, 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) { > >> @@ -973,11 +1016,24 @@ static int vfio_connect_container(VFIOGroup *gr= oup, 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_wi= ndow_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 f339472..0c784c4 100644 > >> --- a/hw/vfio/spapr.c > >> +++ b/hw/vfio/spapr.c > >> @@ -135,3 +135,73 @@ 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 pagesizes =3D memory_region_iommu_get_page_sizes(section= ->mr); > >> + unsigned pagesize =3D (hwaddr)1 << ctz64(pagesizes); > >> + unsigned entries, pages; > >> + struct vfio_iommu_spapr_tce_create create =3D { .argsz =3D 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 =3D int128_get64(section->size); > >> + create.page_shift =3D ctz64(pagesize); > >=20 > > Doing a ctz on a value which is defined as 1 << n seems a bit > > perverse. >=20 > Well, this way it felt more obvious that pagesize is a single page size, > not a mask. Not sure if memory_region_iommu_get_page_sizes() returning a > mask (rather than a page size) is a good idea after all... >=20 > I'll make it: >=20 > create.page_shift =3D ctz64(pagesizes); >=20 > and (below): >=20 > *pgsize =3D 1ULL << create.page_shift; Yes, I think that's better. > and remove pagesize. >=20 > >> + /* > >> + * 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)", re= t); > >> + 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_PRI= x", 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; > >> + } > >> + > >> + return 0; > >> +} > >> diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-comm= on.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; > >> =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 ddb8676..ec32c20 100644 > >> --- a/trace-events > >> +++ b/trace-events > >> @@ -1768,6 +1768,8 @@ vfio_region_sparse_mmap_entry(int i, unsigned lo= ng start, unsigned long end) "sp > >> vfio_get_dev_region(const char *name, int index, uint32_t type, uint3= 2_t subtype) "%s index %d, %08x/%0x8" > >> vfio_ram_register(uint64_t va, uint64_t size, int ret) "va=3D%"PRIx64= " size=3D%"PRIx64" ret=3D%d" > >> vfio_ram_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) "pageshif= t=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 t= o group #%d" > >=20 >=20 >=20 --=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 --RMorpr61ug7CvG36 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBAgAGBQJXV7NyAAoJEGw4ysog2bOSMp0QAL6dq0GNM5z8tp4eI5yZQsiJ 6CEUdzGtp644iBXL5Lhf8S+Zl+3Km/f8lZgqb3B8UDcH8+173K0S5Ss1239bkFRZ n4Ux6fpA73SfzJ6SZqplX8RpLxLCJHRvZ5NzmxJt5qLaRVi0KDUEjh+1XcCJbT0h NaDbNgC4ZzFfXPr6nk7jbpHfF5ZOmTelX6fzpWe/XW+bC9NmAzene4x2b6HNdkcu Qqm2MMHeKRCZEy8xAPlmY8DhFh4csHLRIfwFicn9NUOsA+ayQkpRIh75KxGLCmfQ 81/1gm21Gi4XKoaK+XQhrIsWTNATrD6TDjPztti5f08Exs8rVazxbHNwzr+8+C84 u8w1V/erb+Ihcbwlla61KZSLQKM+xqtzPJJ3bzkcWQSijcnqgvHr7lZ7BmPvRQsn BSsVzqNRpEwh2KzUzT5op4FIAMK7T9h23UnavmVRToawmHfPM70DeAYa8StkdRsn AfKNnKPmmGildeMr7xlmPMY2XMZQmAocXsHzKh69kdEfASAg6H5jUinfSN27FX7Z N4LK+aF2nUPPNl5Wde8NjPAaJ+wHn1E+k2RM8a1BUX8sNbtXWo7uM3H6VK0JfX9r wnpXEeB8g5ZWfR+d2ZXOy5KZdqAynMbjKCcA30LS+ICD9vtpDPO4T/1cPEA9g2Ky v5aVWhgBb3M5E58JooXG =I9O3 -----END PGP SIGNATURE----- --RMorpr61ug7CvG36--