From patchwork Mon Nov 28 15:51:53 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Aviv B.D." X-Patchwork-Id: 9449733 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 20ED6600CB for ; Mon, 28 Nov 2016 15:59:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0D04327DF9 for ; Mon, 28 Nov 2016 15:59:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F2C8E27E3E; Mon, 28 Nov 2016 15:59:41 +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.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID 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 51D1127DF9 for ; Mon, 28 Nov 2016 15:59:40 +0000 (UTC) Received: from localhost ([::1]:59625 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cBOLC-0005jJ-RR for patchwork-qemu-devel@patchwork.kernel.org; Mon, 28 Nov 2016 10:59:38 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45867) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cBOFQ-00012f-TV for qemu-devel@nongnu.org; Mon, 28 Nov 2016 10:53:44 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cBOFN-0008S4-RF for qemu-devel@nongnu.org; Mon, 28 Nov 2016 10:53:40 -0500 Received: from mail-wj0-f193.google.com ([209.85.210.193]:35562) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cBOFN-0008Ra-HT for qemu-devel@nongnu.org; Mon, 28 Nov 2016 10:53:37 -0500 Received: by mail-wj0-f193.google.com with SMTP id he10so355426wjc.2 for ; Mon, 28 Nov 2016 07:53:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=V/BowPNOlrC89C4UxiHU6yTFKlVTv94TRyBejG/Jw7I=; b=AZSbKNqgbLcGSoc0eIQf2kv30O7kiUVTPaDdM8DcvkNNCfQzCADbU6V6zaQ7bXHbg8 SZPv1y+L2s9zV6/8qycvQvU5sVeZpFqW2LUj1qa4DrgB/T6r6NNh1QZ+R4boQ1/aqCAa kAbCvY6yAohszx/pMJAQ2f9YrWeFI+e4R72wFRBV6TDXqWrAcrroWvEIscVmn4OMVKSP M/NeRJFZHCMDth9VBnbj03IQ6jwF9S5nWgMTK6+7aVpfdEdz7qljyguOeOVQOaK2L7WW ZM0vxosPDXy91l6E6xr+uMxqYHML7msNyyPupvuFLIOcyXJKslM8XCnusZzShIWYiIJC 74qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=V/BowPNOlrC89C4UxiHU6yTFKlVTv94TRyBejG/Jw7I=; b=EDUbXFFODvhMKbI1aNqy1pjcUs+4avRbwN5J+y4InM48I1+ZJAtU0rbmAOc2boqgJ/ qqBltbOsw9MUbNJuLdoB1IZ8XioGym0ed2ZOtgyLv/OuIMt1nl4vf9CZQt6oSfU1CHN6 a9lNYh+8cV/lpX7R9lzqcw2nS8O9smLdjIb1W7t54ApibJWYSs99/TWJUi3Zd2l40wAV 9/THMX7mzu+HDnuRbxSCgZ+wcRXNd5e0OOeIKJys1tv+HF/V9JQ8xK3QGSgwkpvZH1rA cyBbjrHE6wiRRPYkL6V94pDzvZe+C9sAO/TgE5qwYn+lFAdOdMa4V5QlvCtZq6KOye+i 1jnA== X-Gm-Message-State: AKaTC03sJuHXVeHTtY+uPS1s5B99IKGaRLqr5snc6BsHcjhRm8rtek835i/oGJwP89/Hww== X-Received: by 10.194.157.41 with SMTP id wj9mr18937830wjb.160.1480348356063; Mon, 28 Nov 2016 07:52:36 -0800 (PST) Received: from tapuz2.cslcs.technion.ac.il (tapuz2.cslcs.technion.ac.il. [132.68.206.212]) by smtp.gmail.com with ESMTPSA id jm6sm63173534wjb.27.2016.11.28.07.52.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 28 Nov 2016 07:52:34 -0800 (PST) From: "Aviv B.D" To: qemu-devel@nongnu.org Date: Mon, 28 Nov 2016 17:51:53 +0200 Message-Id: <1480348315-13332-4-git-send-email-bd.aviv@gmail.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1480348315-13332-1-git-send-email-bd.aviv@gmail.com> References: <1480348315-13332-1-git-send-email-bd.aviv@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.85.210.193 Subject: [Qemu-devel] [PATCH v7 3/5] IOMMU: enable intel_iommu map and unmap notifiers 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: "Michael S. Tsirkin" , Jan Kiszka , Aviv Ben-David , Peter Xu , Alex Williamson , Jason Wang Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: "Aviv Ben-David" Adds a list of registered vtd_as's to intel iommu state to save iteration over each PCI device in a search of the corrosponding domain. Signed-off-by: Aviv Ben-David --- hw/i386/intel_iommu.c | 94 ++++++++++++++++++++++++++++++++++++++---- hw/i386/intel_iommu_internal.h | 2 + include/hw/i386/intel_iommu.h | 9 ++++ 3 files changed, 98 insertions(+), 7 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 05973b9..d872969 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -679,7 +679,7 @@ static int vtd_gpa_to_slpte(VTDContextEntry *ce, uint64_t gpa, } *reads = (*reads) && (slpte & VTD_SL_R); *writes = (*writes) && (slpte & VTD_SL_W); - if (!(slpte & access_right_check)) { + if (!(slpte & access_right_check) && !(flags & IOMMU_NO_FAIL)) { VTD_DPRINTF(GENERAL, "error: lack of %s permission for " "gpa 0x%"PRIx64 " slpte 0x%"PRIx64, (flags & IOMMU_WO ? "write" : "read"), gpa, slpte); @@ -978,6 +978,23 @@ static VTDBus *vtd_find_as_from_bus_num(IntelIOMMUState *s, uint8_t bus_num) return vtd_bus; } +static int vtd_get_did_dev(IntelIOMMUState *s, uint8_t bus_num, uint8_t devfn, + uint16_t *domain_id) +{ + VTDContextEntry ce; + int ret_fr; + + assert(domain_id); + + ret_fr = vtd_dev_to_context_entry(s, bus_num, devfn, &ce); + if (ret_fr) { + return -1; + } + + *domain_id = VTD_CONTEXT_ENTRY_DID(ce.hi); + return 0; +} + /* Do a context-cache device-selective invalidation. * @func_mask: FM field after shifting */ @@ -1064,6 +1081,45 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id) &domain_id); } +static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s, + uint16_t domain_id, hwaddr addr, + uint8_t am) +{ + IntelIOMMUNotifierNode *node; + + QLIST_FOREACH(node, &(s->notifiers_list), next) { + VTDAddressSpace *vtd_as = node->vtd_as; + uint16_t vfio_domain_id; + int ret = vtd_get_did_dev(s, pci_bus_num(vtd_as->bus), vtd_as->devfn, + &vfio_domain_id); + + if (!ret && domain_id == vfio_domain_id) { + hwaddr original_addr = addr; + + while (addr < original_addr + (1 << am) * VTD_PAGE_SIZE) { + IOMMUTLBEntry entry = s->iommu_ops.translate( + &node->vtd_as->iommu, + addr, + IOMMU_NO_FAIL); + + if (entry.perm == IOMMU_NONE && + node->notifier_flag & IOMMU_NOTIFIER_UNMAP) { + entry.target_as = &address_space_memory; + entry.iova = addr & VTD_PAGE_MASK_4K; + entry.translated_addr = 0; + entry.addr_mask = ~VTD_PAGE_MASK(VTD_PAGE_SHIFT); + memory_region_notify_iommu(&node->vtd_as->iommu, entry); + addr += VTD_PAGE_SIZE; + } else if (node->notifier_flag & IOMMU_NOTIFIER_MAP) { + memory_region_notify_iommu(&node->vtd_as->iommu, entry); + addr += entry.addr_mask + 1; + } + } + } + } +} + static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id, hwaddr addr, uint8_t am) { @@ -1074,6 +1130,8 @@ static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id, info.addr = addr; info.mask = ~((1 << am) - 1); g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page, &info); + + vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am); } /* Flush IOTLB @@ -1999,15 +2057,37 @@ static void vtd_iommu_notify_flag_changed(MemoryRegion *iommu, IOMMUNotifierFlag new) { VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu); + IntelIOMMUState *s = vtd_as->iommu_state; + IntelIOMMUNotifierNode *node = NULL; + IntelIOMMUNotifierNode *next_node = NULL; - if (new & IOMMU_NOTIFIER_MAP) { - error_report("Device at bus %s addr %02x.%d requires iommu " - "notifier which is currently not supported by " - "intel-iommu emulation", - vtd_as->bus->qbus.name, PCI_SLOT(vtd_as->devfn), - PCI_FUNC(vtd_as->devfn)); + if (!s->cache_mode_enabled && new & IOMMU_NOTIFIER_MAP) { + error_report("We need to set cache_mode=1 for intel-iommu to enable " + "device assignment with IOMMU protection."); exit(1); } + + /* Add new ndoe if no mapping was exising before this call */ + if (old == IOMMU_NOTIFIER_NONE) { + node = g_malloc0(sizeof(*node)); + node->vtd_as = vtd_as; + node->notifier_flag = new; + QLIST_INSERT_HEAD(&s->notifiers_list, node, next); + return; + } + + /* update notifier node with new flags */ + QLIST_FOREACH_SAFE(node, &s->notifiers_list, next, next_node) { + if (node->vtd_as == vtd_as) { + if (new == IOMMU_NOTIFIER_NONE) { + QLIST_REMOVE(node, next); + g_free(node); + } else { + node->notifier_flag = new; + } + return; + } + } } static const VMStateDescription vtd_vmstate = { diff --git a/hw/i386/intel_iommu_internal.h b/hw/i386/intel_iommu_internal.h index 00cbe0d..c1b9cfc 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -381,6 +381,8 @@ typedef struct VTDIOTLBPageInvInfo VTDIOTLBPageInvInfo; #define VTD_PAGE_SHIFT_1G 30 #define VTD_PAGE_MASK_1G (~((1ULL << VTD_PAGE_SHIFT_1G) - 1)) +#define VTD_PAGE_MASK(shift) (~((1ULL << (shift)) - 1)) + struct VTDRootEntry { uint64_t val; uint64_t rsvd; diff --git a/include/hw/i386/intel_iommu.h b/include/hw/i386/intel_iommu.h index 749eef9..1a355c1 100644 --- a/include/hw/i386/intel_iommu.h +++ b/include/hw/i386/intel_iommu.h @@ -63,6 +63,7 @@ typedef union VTD_IR_TableEntry VTD_IR_TableEntry; typedef union VTD_IR_MSIAddress VTD_IR_MSIAddress; typedef struct VTDIrq VTDIrq; typedef struct VTD_MSIMessage VTD_MSIMessage; +typedef struct IntelIOMMUNotifierNode IntelIOMMUNotifierNode; /* Context-Entry */ struct VTDContextEntry { @@ -247,6 +248,12 @@ struct VTD_MSIMessage { /* When IR is enabled, all MSI/MSI-X data bits should be zero */ #define VTD_IR_MSI_DATA (0) +struct IntelIOMMUNotifierNode { + VTDAddressSpace *vtd_as; + IOMMUNotifierFlag notifier_flag; + QLIST_ENTRY(IntelIOMMUNotifierNode) next; +}; + /* The iommu (DMAR) device state struct */ struct IntelIOMMUState { X86IOMMUState x86_iommu; @@ -284,6 +291,8 @@ struct IntelIOMMUState { MemoryRegionIOMMUOps iommu_ops; GHashTable *vtd_as_by_busptr; /* VTDBus objects indexed by PCIBus* reference */ VTDBus *vtd_as_by_bus_num[VTD_PCI_BUS_MAX]; /* VTDBus objects indexed by bus number */ + /* list of registered notifiers */ + QLIST_HEAD(, IntelIOMMUNotifierNode) notifiers_list; /* interrupt remapping */ bool intr_enabled; /* Whether guest enabled IR */