From patchwork Thu Mar 1 10:33:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Liu, Yi L" X-Patchwork-Id: 10250973 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 D9B32602B5 for ; Thu, 1 Mar 2018 11:07:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C795728FCE for ; Thu, 1 Mar 2018 11:07:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BBFD428EF9; Thu, 1 Mar 2018 11:07:12 +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 E974428EF9 for ; Thu, 1 Mar 2018 11:07:11 +0000 (UTC) Received: from localhost ([::1]:55616 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1erM3L-0002nP-5q for patchwork-qemu-devel@patchwork.kernel.org; Thu, 01 Mar 2018 06:07:11 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53216) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1erLnS-0005gk-I8 for qemu-devel@nongnu.org; Thu, 01 Mar 2018 05:50:47 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1erLnR-0000E1-I9 for qemu-devel@nongnu.org; Thu, 01 Mar 2018 05:50:46 -0500 Received: from mga14.intel.com ([192.55.52.115]:7359) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1erLnR-00006Y-9Y for qemu-devel@nongnu.org; Thu, 01 Mar 2018 05:50:45 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 01 Mar 2018 02:50:44 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.47,408,1515484800"; d="scan'208";a="208048717" Received: from sky-dev.bj.intel.com ([10.238.144.105]) by fmsmga006.fm.intel.com with ESMTP; 01 Mar 2018 02:50:42 -0800 From: "Liu, Yi L" To: qemu-devel@nongnu.org, mst@redhat.com, david@gibson.dropbear.id.au Date: Thu, 1 Mar 2018 18:33:32 +0800 Message-Id: <1519900415-30314-10-git-send-email-yi.l.liu@linux.intel.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1519900415-30314-1-git-send-email-yi.l.liu@linux.intel.com> References: <1519900415-30314-1-git-send-email-yi.l.liu@linux.intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.115 Subject: [Qemu-devel] [PATCH v3 09/12] intel_iommu: record assigned devices in a list 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: "Liu, Yi L" , kevin.tian@intel.com, yi.l.liu@intel.com, jasowang@redhat.com, peterx@redhat.com, alex.williamson@redhat.com, pbonzini@redhat.com, eric.auger.pro@gmail.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP This patch records assigned devices in a list within Intel vIOMMU emulator. The recorded info can be used to filter out affect assigned devices when Qemu captured guest's cache invalidate request. Signed-off-by: Liu, Yi L --- hw/i386/intel_iommu.c | 31 ++++++++++++++++++++++++++----- include/hw/i386/intel_iommu.h | 11 ++++++++++- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 2fd0a6d..978f47a 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -2685,7 +2685,10 @@ static const MemoryRegionOps vtd_mem_ir_ops = { }, }; -VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn) +VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, + PCIBus *bus, + int devfn, + bool allocate) { uintptr_t key = (uintptr_t)bus; VTDBus *vtd_bus = g_hash_table_lookup(s->vtd_as_by_busptr, &key); @@ -2704,7 +2707,7 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn) vtd_dev_as = vtd_bus->dev_as[devfn]; - if (!vtd_dev_as) { + if (!vtd_dev_as && allocate) { snprintf(name, sizeof(name), "intel_iommu_devfn_%d", devfn); vtd_bus->dev_as[devfn] = vtd_dev_as = g_malloc0(sizeof(VTDAddressSpace)); @@ -3001,7 +3004,7 @@ static AddressSpace *vtd_host_dma_iommu(PCIBus *bus, void *opaque, int devfn) assert(0 <= devfn && devfn < PCI_DEVFN_MAX); - vtd_as = vtd_find_add_as(s, bus, devfn); + vtd_as = vtd_find_add_as(s, bus, devfn, true); return &vtd_as->as; } @@ -3012,16 +3015,34 @@ static int vtd_device_notify(PCIBus *bus, { IntelIOMMUState *s = opaque; VTDAddressSpace *vtd_as; + IntelIOMMUAssignedDeviceNode *node = NULL; + IntelIOMMUAssignedDeviceNode *next_node = NULL; assert(0 <= devfn && devfn < PCI_DEVFN_MAX); - vtd_as = vtd_find_add_as(s, bus, devfn); + vtd_as = vtd_find_add_as(s, bus, devfn, false); if (vtd_as == NULL) { return -1; } - /* TODO: record assigned device in IOMMU Emulator */ + if (type == PCI_NTY_DEV_ADD) { + node = g_malloc0(sizeof(*node)); + node->vtd_as = vtd_as; + QLIST_INSERT_HEAD(&s->assigned_device_list, node, next); + return 0; + } + + QLIST_FOREACH_SAFE(node, &s->assigned_device_list, next, next_node) { + if (node->vtd_as == vtd_as) { + if (type == PCI_NTY_DEV_DEL) { + QLIST_REMOVE(node, next); + g_free(node); + } + break; + } + } + return 0; } diff --git a/include/hw/i386/intel_iommu.h b/include/hw/i386/intel_iommu.h index 1df6fa9..0b6dc32 100644 --- a/include/hw/i386/intel_iommu.h +++ b/include/hw/i386/intel_iommu.h @@ -68,6 +68,7 @@ typedef union VTD_IR_MSIAddress VTD_IR_MSIAddress; typedef struct VTDIrq VTDIrq; typedef struct VTD_MSIMessage VTD_MSIMessage; typedef struct IntelIOMMUMRNotifierNode IntelIOMMUMRNotifierNode; +typedef struct IntelIOMMUAssignedDeviceNode IntelIOMMUAssignedDeviceNode; /* Context-Entry */ struct VTDContextEntry { @@ -258,6 +259,11 @@ struct IntelIOMMUMRNotifierNode { QLIST_ENTRY(IntelIOMMUMRNotifierNode) next; }; +struct IntelIOMMUAssignedDeviceNode { + VTDAddressSpace *vtd_as; + QLIST_ENTRY(IntelIOMMUAssignedDeviceNode) next; +}; + /* The iommu (DMAR) device state struct */ struct IntelIOMMUState { X86IOMMUState x86_iommu; @@ -296,6 +302,8 @@ struct IntelIOMMUState { VTDBus *vtd_as_by_bus_num[VTD_PCI_BUS_MAX]; /* VTDBus objects indexed by bus number */ /* list of registered notifiers */ QLIST_HEAD(, IntelIOMMUMRNotifierNode) notifiers_list; + /* list of assigned devices */ + QLIST_HEAD(, IntelIOMMUAssignedDeviceNode) assigned_device_list; /* interrupt remapping */ bool intr_enabled; /* Whether guest enabled IR */ @@ -310,6 +318,7 @@ struct IntelIOMMUState { /* Find the VTD Address space associated with the given bus pointer, * create a new one if none exists */ -VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn); +VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, + int devfn, bool allocate); #endif