From patchwork Wed Sep 7 05:32:23 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 9318193 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id D02B16077F for ; Wed, 7 Sep 2016 05:33:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C1C7029003 for ; Wed, 7 Sep 2016 05:33:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B67E529018; Wed, 7 Sep 2016 05:33:19 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 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.wl.linuxfoundation.org (Postfix) with ESMTPS id 290D929003 for ; Wed, 7 Sep 2016 05:33:19 +0000 (UTC) Received: from localhost ([::1]:38048 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bhVU6-0000DD-77 for patchwork-qemu-devel@patchwork.kernel.org; Wed, 07 Sep 2016 01:33:18 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44231) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bhVTV-0000Aa-LR for qemu-devel@nongnu.org; Wed, 07 Sep 2016 01:32:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bhVTT-0003a7-Hb for qemu-devel@nongnu.org; Wed, 07 Sep 2016 01:32:40 -0400 Received: from mx1.redhat.com ([209.132.183.28]:56930) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bhVTT-0003a3-9s for qemu-devel@nongnu.org; Wed, 07 Sep 2016 01:32:39 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DCDCB7F7A4; Wed, 7 Sep 2016 05:32:38 +0000 (UTC) Received: from pxdev.xzpeter.org.com (dhcp-14-106.nay.redhat.com [10.66.14.106]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u875WRlK010342; Wed, 7 Sep 2016 01:32:35 -0400 From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 7 Sep 2016 13:32:23 +0800 Message-Id: <1473226344-28520-3-git-send-email-peterx@redhat.com> In-Reply-To: <1473226344-28520-1-git-send-email-peterx@redhat.com> References: <1473226344-28520-1-git-send-email-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Wed, 07 Sep 2016 05:32:38 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 2/3] memory: generalize iommu_ops.notify_started to notifier_add 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: mst@redhat.com, jasowang@redhat.com, peterx@redhat.com, vkaplans@redhat.com, alex.williamson@redhat.com, wexu@redhat.com, pbonzini@redhat.com, cornelia.huck@de.ibm.com, dgibson@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Considering that we may have multiple IOMMU notifier consumers in the future, converting iommu_ops.notify_{started|stopped} into some more general form. Now we can trap all notifier registerations and deregistrations, rather than only the first ones. Power was leveraging the notifier_{started|stopped}, adding iommu_user field for counting on Power guests to achieve the same goal. Suggested-by: Paolo Bonzini Signed-off-by: Peter Xu --- hw/i386/intel_iommu.c | 4 ++-- hw/ppc/spapr_iommu.c | 18 ++++++++++++------ include/exec/memory.h | 8 ++++---- include/hw/ppc/spapr.h | 1 + memory.c | 10 ++++------ 5 files changed, 23 insertions(+), 18 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 28c31a2..c6bd8f6 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -1974,7 +1974,7 @@ static IOMMUTLBEntry vtd_iommu_translate(MemoryRegion *iommu, hwaddr addr, return ret; } -static void vtd_iommu_notify_started(MemoryRegion *iommu) +static void vtd_iommu_notifier_add(MemoryRegion *iommu, IOMMUNotifier *n) { VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu); @@ -2348,7 +2348,7 @@ static void vtd_init(IntelIOMMUState *s) memset(s->womask, 0, DMAR_REG_SIZE); s->iommu_ops.translate = vtd_iommu_translate; - s->iommu_ops.notify_started = vtd_iommu_notify_started; + s->iommu_ops.notifier_add = vtd_iommu_notifier_add; s->root = 0; s->root_extended = false; s->dmar_enabled = false; diff --git a/hw/ppc/spapr_iommu.c b/hw/ppc/spapr_iommu.c index 6bc4d4d..99c83a4 100644 --- a/hw/ppc/spapr_iommu.c +++ b/hw/ppc/spapr_iommu.c @@ -156,14 +156,20 @@ static uint64_t spapr_tce_get_min_page_size(MemoryRegion *iommu) return 1ULL << tcet->page_shift; } -static void spapr_tce_notify_started(MemoryRegion *iommu) +static void spapr_tce_notifier_add(MemoryRegion *iommu, IOMMUNotifier *n) { - spapr_tce_set_need_vfio(container_of(iommu, sPAPRTCETable, iommu), true); + sPAPRTCETable *tcet = container_of(iommu, sPAPRTCETable, iommu); + if (tcet->iommu_users++ == 0) { + spapr_tce_set_need_vfio(tcet, true); + } } -static void spapr_tce_notify_stopped(MemoryRegion *iommu) +static void spapr_tce_notifier_del(MemoryRegion *iommu, IOMMUNotifier *n) { - spapr_tce_set_need_vfio(container_of(iommu, sPAPRTCETable, iommu), false); + sPAPRTCETable *tcet = container_of(iommu, sPAPRTCETable, iommu); + if (--tcet->iommu_users == 0) { + spapr_tce_set_need_vfio(tcet, false); + } } static int spapr_tce_table_post_load(void *opaque, int version_id) @@ -246,8 +252,8 @@ static const VMStateDescription vmstate_spapr_tce_table = { static MemoryRegionIOMMUOps spapr_iommu_ops = { .translate = spapr_tce_translate_iommu, .get_min_page_size = spapr_tce_get_min_page_size, - .notify_started = spapr_tce_notify_started, - .notify_stopped = spapr_tce_notify_stopped, + .notifier_add = spapr_tce_notifier_add, + .notifier_del = spapr_tce_notifier_del, }; static int spapr_tce_table_realize(DeviceState *dev) diff --git a/include/exec/memory.h b/include/exec/memory.h index 92f14db..9efcf1b 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -175,10 +175,10 @@ struct MemoryRegionIOMMUOps { IOMMUTLBEntry (*translate)(MemoryRegion *iommu, hwaddr addr, bool is_write); /* Returns minimum supported page size */ uint64_t (*get_min_page_size)(MemoryRegion *iommu); - /* Called when the first notifier is set */ - void (*notify_started)(MemoryRegion *iommu); - /* Called when the last notifier is removed */ - void (*notify_stopped)(MemoryRegion *iommu); + /* Called when someone registers to the notify list */ + void (*notifier_add)(MemoryRegion *iommu, IOMMUNotifier *n); + /* Called when someone unregisters from the notify list */ + void (*notifier_del)(MemoryRegion *iommu, IOMMUNotifier *n); }; typedef struct CoalescedMemoryRange CoalescedMemoryRange; diff --git a/include/hw/ppc/spapr.h b/include/hw/ppc/spapr.h index caf7be9..08627ec 100644 --- a/include/hw/ppc/spapr.h +++ b/include/hw/ppc/spapr.h @@ -564,6 +564,7 @@ struct sPAPRTCETable { MemoryRegion root, iommu; struct VIOsPAPRDevice *vdev; /* for @bypass migration compatibility only */ QLIST_ENTRY(sPAPRTCETable) list; + int iommu_users; }; sPAPRTCETable *spapr_tce_find_by_liobn(target_ulong liobn); diff --git a/memory.c b/memory.c index 45a3902..d913043 100644 --- a/memory.c +++ b/memory.c @@ -1518,9 +1518,8 @@ void memory_region_register_iommu_notifier(MemoryRegion *mr, { /* We need to register for at least one bitfield */ assert(n->notifier_caps != IOMMU_NOTIFIER_NONE); - if (mr->iommu_ops->notify_started && - QLIST_EMPTY(&mr->iommu_notify)) { - mr->iommu_ops->notify_started(mr); + if (mr->iommu_ops->notifier_add) { + mr->iommu_ops->notifier_add(mr, n); } QLIST_INSERT_HEAD(&mr->iommu_notify, n, node); } @@ -1560,9 +1559,8 @@ void memory_region_unregister_iommu_notifier(MemoryRegion *mr, IOMMUNotifier *n) { QLIST_REMOVE(n, node); - if (mr->iommu_ops->notify_stopped && - QLIST_EMPTY(&mr->iommu_notify)) { - mr->iommu_ops->notify_stopped(mr); + if (mr->iommu_ops->notifier_del) { + mr->iommu_ops->notifier_del(mr, n); } }