From patchwork Wed May 4 06:52:29 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexey Kardashevskiy X-Patchwork-Id: 9010971 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 2F958BF29F for ; Wed, 4 May 2016 07:01:20 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 4F3682037F for ; Wed, 4 May 2016 07:01:19 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 3D1B2202E9 for ; Wed, 4 May 2016 07:01:18 +0000 (UTC) Received: from localhost ([::1]:46017 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1axqo6-00045X-FO for patchwork-qemu-devel@patchwork.kernel.org; Wed, 04 May 2016 03:01:14 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60878) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1axqjf-00047I-Eb for qemu-devel@nongnu.org; Wed, 04 May 2016 02:56:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1axqjN-0006aK-3R for qemu-devel@nongnu.org; Wed, 04 May 2016 02:56:33 -0400 Received: from e23smtp04.au.ibm.com ([202.81.31.146]:41447) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1axqjM-0006XI-4P for qemu-devel@nongnu.org; Wed, 04 May 2016 02:56:21 -0400 Received: from localhost by e23smtp04.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 4 May 2016 16:56:01 +1000 Received: from d23dlp01.au.ibm.com (202.81.31.203) by e23smtp04.au.ibm.com (202.81.31.210) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 4 May 2016 16:55:58 +1000 X-IBM-Helo: d23dlp01.au.ibm.com X-IBM-MailFrom: aik@ozlabs.ru X-IBM-RcptTo: qemu-devel@nongnu.org;qemu-ppc@nongnu.org Received: from d23relay09.au.ibm.com (d23relay09.au.ibm.com [9.185.63.181]) by d23dlp01.au.ibm.com (Postfix) with ESMTP id C8CEB2CE805F; Wed, 4 May 2016 16:55:22 +1000 (EST) Received: from d23av01.au.ibm.com (d23av01.au.ibm.com [9.190.234.96]) by d23relay09.au.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id u446sfuP46596150; Wed, 4 May 2016 16:55:17 +1000 Received: from d23av01.au.ibm.com (localhost [127.0.0.1]) by d23av01.au.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id u446sHBg010334; Wed, 4 May 2016 16:54:17 +1000 Received: from ozlabs.au.ibm.com (ozlabs.au.ibm.com [9.192.253.14]) by d23av01.au.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id u446sGZt009763; Wed, 4 May 2016 16:54:16 +1000 Received: from bran.ozlabs.ibm.com (haven.au.ibm.com [9.192.254.114]) by ozlabs.au.ibm.com (Postfix) with ESMTP id 4A2FDA03BE; Wed, 4 May 2016 16:52:36 +1000 (AEST) Received: from vpl2.ozlabs.ibm.com (vpl2.ozlabs.ibm.com [10.61.141.27]) by bran.ozlabs.ibm.com (Postfix) with ESMTP id 4EF54E3A49; Wed, 4 May 2016 16:52:36 +1000 (AEST) From: Alexey Kardashevskiy To: qemu-devel@nongnu.org Date: Wed, 4 May 2016 16:52:29 +1000 Message-Id: <1462344751-28281-18-git-send-email-aik@ozlabs.ru> X-Mailer: git-send-email 2.5.0.rc3 In-Reply-To: <1462344751-28281-1-git-send-email-aik@ozlabs.ru> References: <1462344751-28281-1-git-send-email-aik@ozlabs.ru> X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16050406-0013-0000-0000-000005042729 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 202.81.31.146 Subject: [Qemu-devel] [PATCH qemu v16 17/19] spapr_iommu, vfio, memory: Notify IOMMU about starting/stopping being used by VFIO X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Alexey Kardashevskiy , Alexander Graf , Alex Williamson , qemu-ppc@nongnu.org, Paolo Bonzini , David Gibson Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The sPAPR TCE tables manage 2 copies when VFIO is using an IOMMU - a guest view of the table and a hardware TCE table. If there is no VFIO presense in the address space, then just the guest view is used, if this is the case, it is allocated in the KVM. However since there is no support yet for VFIO in KVM TCE hypercalls, when we start using VFIO, we need to move the guest view from KVM to the userspace; and we need to do this for every IOMMU on a bus with VFIO devices. This adds vfio_start/vfio_stop callbacks in MemoryRegionIOMMUOps to notify IOMMU about changing environment so it can reallocate the table to/from KVM or (when available) hook the IOMMU groups with the logical bus (LIOBN) in the KVM. This removes explicit spapr_tce_set_need_vfio() call from PCI hotplug path as the new callbacks do this better - they notify IOMMU at the exact moment when the configuration is changed, and this also includes the case of PCI hot unplug. This postpones vfio_stop() till the end of region_del() as vfio_dma_unmap() has to execute before VFIO support is disabled. As there can be multiple containers attached to the same PHB/LIOBN, this adds a wrapper with a use counter for every IOMMU MR and stores them in a list in the VFIOAddressSpace. Signed-off-by: Alexey Kardashevskiy --- Changes: v16: * added a use counter in VFIOAddressSpace->VFIOIOMMUMR v15: * s/need_vfio/vfio-Users/g --- hw/ppc/spapr_iommu.c | 12 ++++++++++++ hw/ppc/spapr_pci.c | 6 ------ hw/vfio/common.c | 45 ++++++++++++++++++++++++++++++++++++++++++- include/exec/memory.h | 4 ++++ include/hw/vfio/vfio-common.h | 7 +++++++ 5 files changed, 67 insertions(+), 7 deletions(-) diff --git a/hw/ppc/spapr_iommu.c b/hw/ppc/spapr_iommu.c index c945dba..7af2700 100644 --- a/hw/ppc/spapr_iommu.c +++ b/hw/ppc/spapr_iommu.c @@ -155,6 +155,16 @@ static uint64_t spapr_tce_get_page_sizes(MemoryRegion *iommu) return 1ULL << tcet->page_shift; } +static void spapr_tce_vfio_start(MemoryRegion *iommu) +{ + spapr_tce_set_need_vfio(container_of(iommu, sPAPRTCETable, iommu), true); +} + +static void spapr_tce_vfio_stop(MemoryRegion *iommu) +{ + spapr_tce_set_need_vfio(container_of(iommu, sPAPRTCETable, iommu), false); +} + static void spapr_tce_table_do_enable(sPAPRTCETable *tcet); static void spapr_tce_table_do_disable(sPAPRTCETable *tcet); @@ -239,6 +249,8 @@ static const VMStateDescription vmstate_spapr_tce_table = { static MemoryRegionIOMMUOps spapr_iommu_ops = { .translate = spapr_tce_translate_iommu, .get_page_sizes = spapr_tce_get_page_sizes, + .vfio_start = spapr_tce_vfio_start, + .vfio_stop = spapr_tce_vfio_stop, }; static int spapr_tce_table_realize(DeviceState *dev) diff --git a/hw/ppc/spapr_pci.c b/hw/ppc/spapr_pci.c index 5b9ccff..51e7d56 100644 --- a/hw/ppc/spapr_pci.c +++ b/hw/ppc/spapr_pci.c @@ -1086,12 +1086,6 @@ static void spapr_phb_add_pci_device(sPAPRDRConnector *drc, void *fdt = NULL; int fdt_start_offset = 0, fdt_size; - if (object_dynamic_cast(OBJECT(pdev), "vfio-pci")) { - sPAPRTCETable *tcet = spapr_tce_find_by_liobn(phb->dma_liobn); - - spapr_tce_set_need_vfio(tcet, true); - } - if (dev->hotplugged) { fdt = create_device_tree(&fdt_size); fdt_start_offset = spapr_create_pci_child_dt(phb, pdev, fdt, 0); diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 3f2fb23..03daf88 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -421,6 +421,26 @@ static void vfio_listener_region_add(MemoryListener *listener, QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next); memory_region_register_iommu_notifier(giommu->iommu, &giommu->n); + + if (section->mr->iommu_ops && section->mr->iommu_ops->vfio_start) { + VFIOIOMMUMR *iommumr; + bool found = false; + + QLIST_FOREACH(iommumr, &container->space->iommumrs, iommumr_next) { + if (iommumr->iommu == section->mr) { + found = true; + break; + } + } + if (!found) { + iommumr = g_malloc0(sizeof(*iommumr)); + iommumr->iommu = section->mr; + section->mr->iommu_ops->vfio_start(section->mr); + QLIST_INSERT_HEAD(&container->space->iommumrs, iommumr, + iommumr_next); + } + ++iommumr->users; + } memory_region_iommu_replay(giommu->iommu, &giommu->n, false); @@ -470,6 +490,7 @@ static void vfio_listener_region_del(MemoryListener *listener, hwaddr iova, end; Int128 llend, llsize; int ret; + MemoryRegion *iommu = NULL; if (vfio_listener_skipped_section(section)) { trace_vfio_listener_region_del_skip( @@ -490,13 +511,30 @@ static void vfio_listener_region_del(MemoryListener *listener, QLIST_FOREACH(giommu, &container->giommu_list, giommu_next) { if (giommu->iommu == section->mr) { + VFIOIOMMUMR *iommumr; + memory_region_unregister_iommu_notifier(&giommu->n); + + QLIST_FOREACH(iommumr, &container->space->iommumrs, + iommumr_next) { + if (iommumr->iommu != section->mr) { + continue; + } + --iommumr->users; + if (iommumr->users) { + break; + } + QLIST_REMOVE(iommumr, iommumr_next); + g_free(iommumr); + iommu = giommu->iommu; + break; + } + QLIST_REMOVE(giommu, giommu_next); g_free(giommu); break; } } - /* * FIXME: We assume the one big unmap below is adequate to * remove any individual page mappings in the IOMMU which @@ -527,6 +565,10 @@ static void vfio_listener_region_del(MemoryListener *listener, "0x%"HWADDR_PRIx") = %d (%m)", container, iova, int128_get64(llsize), ret); } + + if (iommu && iommu->iommu_ops && iommu->iommu_ops->vfio_stop) { + iommu->iommu_ops->vfio_stop(section->mr); + } } static const MemoryListener vfio_memory_listener = { @@ -787,6 +829,7 @@ static VFIOAddressSpace *vfio_get_address_space(AddressSpace *as) space = g_malloc0(sizeof(*space)); space->as = as; QLIST_INIT(&space->containers); + QLIST_INIT(&space->iommumrs); QLIST_INSERT_HEAD(&vfio_address_spaces, space, list); diff --git a/include/exec/memory.h b/include/exec/memory.h index a3a1703..52d2c70 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -151,6 +151,10 @@ struct MemoryRegionIOMMUOps { IOMMUTLBEntry (*translate)(MemoryRegion *iommu, hwaddr addr, bool is_write); /* Returns supported page sizes */ uint64_t (*get_page_sizes)(MemoryRegion *iommu); + /* Called when VFIO starts using this */ + void (*vfio_start)(MemoryRegion *iommu); + /* Called when VFIO stops using this */ + void (*vfio_stop)(MemoryRegion *iommu); }; typedef struct CoalescedMemoryRange CoalescedMemoryRange; diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index 808263b..a9e6e33 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -64,9 +64,16 @@ typedef struct VFIORegion { typedef struct VFIOAddressSpace { AddressSpace *as; QLIST_HEAD(, VFIOContainer) containers; + QLIST_HEAD(, VFIOIOMMUMR) iommumrs; QLIST_ENTRY(VFIOAddressSpace) list; } VFIOAddressSpace; +typedef struct VFIOIOMMUMR { + MemoryRegion *iommu; + int users; + QLIST_ENTRY(VFIOIOMMUMR) iommumr_next; +} VFIOIOMMUMR; + struct VFIOGroup; typedef struct VFIOContainer {