From patchwork Sat Oct 15 10:12:06 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: 9377759 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 B1BC2600CA for ; Sat, 15 Oct 2016 10:13:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A25042933E for ; Sat, 15 Oct 2016 10:13:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 967612934C; Sat, 15 Oct 2016 10:13: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.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, 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 DDA972933E for ; Sat, 15 Oct 2016 10:13:11 +0000 (UTC) Received: from localhost ([::1]:51193 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bvLxn-0003f7-0z for patchwork-qemu-devel@patchwork.kernel.org; Sat, 15 Oct 2016 06:13:11 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36366) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bvLxJ-0003cE-4l for qemu-devel@nongnu.org; Sat, 15 Oct 2016 06:12:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bvLxH-0002k5-9K for qemu-devel@nongnu.org; Sat, 15 Oct 2016 06:12:40 -0400 Received: from mail-wm0-x241.google.com ([2a00:1450:400c:c09::241]:33535) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1bvLxG-0002jH-W2 for qemu-devel@nongnu.org; Sat, 15 Oct 2016 06:12:39 -0400 Received: by mail-wm0-x241.google.com with SMTP id 191so1971097wmr.0 for ; Sat, 15 Oct 2016 03:12:38 -0700 (PDT) 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=6SFkZGsbOC2qJE1Sijht69kYiLIzYCVpOURbrXgUKgE=; b=au3FdO2ry0rx/RakbL4TD9q+YJHSDK8JQ+KVzf1QHVx2acHQiEaxYB7Ws7YdwNNedf KORcPffYmaRVBAxv/dnAjHOUb80wfa/PZs3M7ewa2IocKWKRE7uCjzapS1hbl7ipz+Sq F5yuAMadcOwBFW1SKiOFqpEudvLjwUOdmOeRTn/pJEwYj/ZXmuCgfAY5VYOTrdt64lIN S7cX11cbFNb5NrRLmmLM7hPX8Q8+K/NaLxgz6iqebVpepzWIY9hpf62Eaq3T1cVHvYhz 0BL4qrQT32zdvVlfei/56Mc59ScHaGsbiY4wHb4rrZo8ZdIRF3TTVGDk4uOn1CCJm2bt IARQ== 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=6SFkZGsbOC2qJE1Sijht69kYiLIzYCVpOURbrXgUKgE=; b=K57lsEW3m0kI8B41t98SHVqfgqFFA2RBSYRa5+X0iEKse/NqQ8OicKUhj2Ij1xF8tK /3bTDiK/9qLL3DMMlIacLg9NVkcQP+8YjjGjdnLp0Gm1xdfdsRyvZc84XgFYxuiBc1BL Wwh2T8MIauZ+Xq/8TBeowRMOpP/47qhTIhMzJNBaK20r/m0w1zoYHrXGK1i5368I+MUq 7+9egX3USBy03RLzdtDOzs1Phrneyn0XzejcvAN0nsq6+/vmy3It3clgYBdsflBxDpfb 52BGxcQz6sBj/Nxi6Lc1JLx/aThMOznjQuExBzWLhMxcbxYHwIL0Sp/wKzOer0Swt+po Ki0g== X-Gm-Message-State: AA6/9RmxfITkUCSVse1q6x4fz8mVsmF0A4TYAtscYvJzA4ZsMjWollNceESguGlC3LISAg== X-Received: by 10.194.68.106 with SMTP id v10mr5247265wjt.149.1476526357820; Sat, 15 Oct 2016 03:12:37 -0700 (PDT) Received: from tapuz2.cslcs.technion.ac.il (tapuz2.cslcs.technion.ac.il. [132.68.206.212]) by smtp.gmail.com with ESMTPSA id i15sm2757136wmf.8.2016.10.15.03.12.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 15 Oct 2016 03:12:37 -0700 (PDT) From: "Aviv B.D" To: qemu-devel@nongnu.org Date: Sat, 15 Oct 2016 13:12:06 +0300 Message-Id: <1476526326-32363-4-git-send-email-bd.aviv@gmail.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1476526326-32363-1-git-send-email-bd.aviv@gmail.com> References: <1476526326-32363-1-git-send-email-bd.aviv@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::241 Subject: [Qemu-devel] [PATCH v4 3/3] 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: Jan Kiszka , Alex Williamson , Aviv Ben-David , Peter Xu , "Michael S. Tsirkin" 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" * register notifiers when vtd_iommu_notify_flag_changed is called. * Notify on IOTLB page invalidation about unmap and map operations. Adds a list of registered vtd_as's to intel iommu state to save iterations over each PCI device in a search of the corrosponding domain. Signed-off-by: Aviv Ben-David --- hw/i386/intel_iommu.c | 102 ++++++++++++++++++++++++++++++++++++++--- hw/i386/intel_iommu_internal.h | 2 + include/hw/i386/intel_iommu.h | 8 ++++ 3 files changed, 105 insertions(+), 7 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 9ffa45f..0560e3c 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -51,6 +51,9 @@ static int vtd_dbgflags = VTD_DBGBIT(GENERAL) | VTD_DBGBIT(CSR); #define VTD_DPRINTF(what, fmt, ...) do {} while (0) #endif +static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num, + uint8_t devfn, VTDContextEntry *ce); + static void vtd_define_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val, uint64_t wmask, uint64_t w1cmask) { @@ -142,6 +145,23 @@ static uint64_t vtd_set_clear_mask_quad(IntelIOMMUState *s, hwaddr addr, return new_val; } +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; +} + /* GHashTable functions */ static gboolean vtd_uint64_equal(gconstpointer v1, gconstpointer v2) { @@ -682,9 +702,6 @@ static int vtd_gpa_to_slpte(VTDContextEntry *ce, uint64_t gpa, IOMMUAccessFlags *reads = (*reads) && (slpte & VTD_SL_R); *writes = (*writes) && (slpte & VTD_SL_W); if (!(slpte & access_right_check)) { - VTD_DPRINTF(GENERAL, "error: lack of %s permission for " - "gpa 0x%"PRIx64 " slpte 0x%"PRIx64, - (flags == IOMMU_WO ? "write" : "read"), gpa, slpte); return (flags == IOMMU_RW || flags == IOMMU_WO) ? -VTD_FR_WRITE : -VTD_FR_READ; } if (vtd_slpte_nonzero_rsvd(slpte, level)) { @@ -732,9 +749,6 @@ static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num, } if (!vtd_context_entry_present(ce)) { - VTD_DPRINTF(GENERAL, - "error: context-entry #%"PRIu8 "(bus #%"PRIu8 ") " - "is not present", devfn, bus_num); return -VTD_FR_CONTEXT_ENTRY_P; } else if ((ce->hi & VTD_CONTEXT_ENTRY_RSVD_HI) || (ce->lo & VTD_CONTEXT_ENTRY_RSVD_LO)) { @@ -1062,6 +1076,55 @@ 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); + //int i=0; + if (!ret && domain_id == vfio_domain_id){ + IOMMUTLBEntry entry; + + /* notify unmap */ + if (node->notifier_flag & IOMMU_NOTIFIER_UNMAP){ + VTD_DPRINTF(GENERAL, "Remove addr 0x%"PRIx64 " mask %d", addr, am); + 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_4K + am); + entry.perm = IOMMU_NONE; + memory_region_notify_iommu(&node->vtd_as->iommu, entry); + } + + /* notify map */ + if (node->notifier_flag & IOMMU_NOTIFIER_MAP){ + hwaddr original_addr = addr; + VTD_DPRINTF(GENERAL, "add addr 0x%"PRIx64 " mask %d", addr, am); + while (addr < original_addr + (1<iommu_ops.translate( + &node->vtd_as->iommu, + addr, + IOMMU_NO_FAIL); + if (entry.perm != IOMMU_NONE){ + addr += entry.addr_mask + 1; + memory_region_notify_iommu(&node->vtd_as->iommu, entry); + } else { + addr += VTD_PAGE_SIZE; + } + } + } + } + } +} + + static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id, hwaddr addr, uint8_t am) { @@ -1072,6 +1135,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 @@ -1997,8 +2062,10 @@ 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; - if (new & IOMMU_NOTIFIER_MAP) { + if (!s->cache_mode_enabled && 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", @@ -2006,6 +2073,27 @@ static void vtd_iommu_notify_flag_changed(MemoryRegion *iommu, PCI_FUNC(vtd_as->devfn)); 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(node, &s->notifiers_list, next){ + if (node->vtd_as == vtd_as){ + if (new == IOMMU_NOTIFIER_NONE){ + QLIST_REMOVE(node, next); + } 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 35d9f3a..96df4ae 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 7a94f16..3083608 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 { @@ -248,6 +249,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; @@ -285,6 +292,7 @@ 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 */ + QLIST_HEAD(, IntelIOMMUNotifierNode) notifiers_list; /* list of registered notifiers */ /* interrupt remapping */ bool intr_enabled; /* Whether guest enabled IR */