From patchwork Fri Nov 17 06:22:14 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chao Gao X-Patchwork-Id: 10062315 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 940A9604D4 for ; Fri, 17 Nov 2017 06:27:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 83A8F2A825 for ; Fri, 17 Nov 2017 06:27:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 785462A990; Fri, 17 Nov 2017 06:27:14 +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=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 01AD52A825 for ; Fri, 17 Nov 2017 06:27:13 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eFa57-0000X5-Am; Fri, 17 Nov 2017 06:24:53 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eFa56-0000W8-47 for xen-devel@lists.xen.org; Fri, 17 Nov 2017 06:24:52 +0000 Received: from [85.158.143.35] by server-1.bemta-6.messagelabs.com id 3D/45-04165-3B08E0A5; Fri, 17 Nov 2017 06:24:51 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmplkeJIrShJLcpLzFFi42I5YG4Sq7upgS/ K4NVta4slHxezODB6HN39mymAMYo1My8pvyKBNWP3ub/MBR0WFXNbdRoYn+l0MXJxCAlMZ5TY uOYcYxcjJ4eEAK/EkWUzWCHsAIlDO5exgNhCAlUSC25dYgOx2QSUJS5+7QWzRQSkJa59vswIM ohZYCqLxMEL7WANwgLOEl1PrzGD2CwCqhK3l9wEi/MKOEksmrwSaoGCxJSH78FqOIHq9785yA SxzEni4LppTBMYeRcwMqxiVC9OLSpLLdK11EsqykzPKMlNzMzRNTQw08tNLS5OTE/NSUwq1kv Oz93ECAwGBiDYwXh3U8AhRkkOJiVRXh5zvighvqT8lMqMxOKM+KLSnNTiQ4wyHBxKErxe9UA5 waLU9NSKtMwcYFjCpCU4eJREeAtA0rzFBYm5xZnpEKlTjJYcByZc+cPE8Wzm6wZmjmlXW5uYh Vjy8vNSpcR5i0EaBEAaMkrz4MbBYucSo6yUMC8j0IFCPAWpRbmZJajyrxjFORiVhHkrQabwZO aVwG19BXQQE9BBNje4QQ4qSURISTUwynd0toqrvppot23fBq/JRmGf1X7KHtaUrtfYq3ZDqCL w8s1PBx84q1Yv3pTm9mir++UH0RUl5jNlH2t5WdzSTNjm7bO6ZFtQ6hPOjfU1OrlTa8rLPz3q 8tqok/Tzx6vwYwIWWrta+MMdUn4uEa5b9Jn75a+zeuVi33gOxbxKMn+0YGqdyO46JZbijERDL eai4kQAqiY685gCAAA= X-Env-Sender: chao.gao@intel.com X-Msg-Ref: server-12.tower-21.messagelabs.com!1510899867!82378134!8 X-Originating-IP: [192.55.52.93] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogMTkyLjU1LjUyLjkzID0+IDMyNDY2NQ==\n X-StarScan-Received: X-StarScan-Version: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 56177 invoked from network); 17 Nov 2017 06:24:50 -0000 Received: from mga11.intel.com (HELO mga11.intel.com) (192.55.52.93) by server-12.tower-21.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 17 Nov 2017 06:24:50 -0000 Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 16 Nov 2017 22:24:50 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos; i="5.44,407,1505804400"; d="scan'208"; a="1245165064" Received: from skl-4s-chao.sh.intel.com ([10.239.48.9]) by fmsmga002.fm.intel.com with ESMTP; 16 Nov 2017 22:24:47 -0800 From: Chao Gao To: xen-devel@lists.xen.org Date: Fri, 17 Nov 2017 14:22:14 +0800 Message-Id: <1510899755-40237-8-git-send-email-chao.gao@intel.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1510899755-40237-1-git-send-email-chao.gao@intel.com> References: <1510899755-40237-1-git-send-email-chao.gao@intel.com> Cc: Lan Tianyu , Kevin Tian , Stefano Stabellini , Wei Liu , Konrad Rzeszutek Wilk , George Dunlap , Ian Jackson , Tim Deegan , Jan Beulich , Andrew Cooper , Chao Gao , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Subject: [Xen-devel] [PATCH v4 07/28] x86/hvm: Introduce a emulated VTD for HVM X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP This patch adds create/destroy function for the emulated VTD and adapts it to the common VIOMMU abstraction. As the Makefile is changed here, put all files in alphabetic order by this chance. Signed-off-by: Chao Gao Signed-off-by: Lan Tianyu --- v4: - use REGISTER_VIOMMU - shrink the size of hvm_hw_vvtd_regs - make hvm_hw_vvtd_regs a field inside struct vvtd --- xen/drivers/passthrough/vtd/Makefile | 7 +- xen/drivers/passthrough/vtd/iommu.h | 9 +++ xen/drivers/passthrough/vtd/vvtd.c | 150 +++++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 xen/drivers/passthrough/vtd/vvtd.c diff --git a/xen/drivers/passthrough/vtd/Makefile b/xen/drivers/passthrough/vtd/Makefile index f302653..163c7fe 100644 --- a/xen/drivers/passthrough/vtd/Makefile +++ b/xen/drivers/passthrough/vtd/Makefile @@ -1,8 +1,9 @@ subdir-$(CONFIG_X86) += x86 -obj-y += iommu.o obj-y += dmar.o -obj-y += utils.o -obj-y += qinval.o obj-y += intremap.o +obj-y += iommu.o +obj-y += qinval.o obj-y += quirks.o +obj-y += utils.o +obj-$(CONFIG_VIOMMU) += vvtd.o diff --git a/xen/drivers/passthrough/vtd/iommu.h b/xen/drivers/passthrough/vtd/iommu.h index db80b31..f2ef3dd 100644 --- a/xen/drivers/passthrough/vtd/iommu.h +++ b/xen/drivers/passthrough/vtd/iommu.h @@ -47,6 +47,7 @@ #define DMAR_IQH_REG 0x80 /* invalidation queue head */ #define DMAR_IQT_REG 0x88 /* invalidation queue tail */ #define DMAR_IQA_REG 0x90 /* invalidation queue addr */ +#define DMAR_IECTL_REG 0xa0 /* invalidation event control register */ #define DMAR_IRTA_REG 0xb8 /* intr remap */ #define OFFSET_STRIDE (9) @@ -89,6 +90,12 @@ #define cap_afl(c) (((c) >> 3) & 1) #define cap_ndoms(c) (1 << (4 + 2 * ((c) & 0x7))) +#define cap_set_num_fault_regs(c) ((((c) - 1) & 0xff) << 40) +#define cap_set_fault_reg_offset(c) ((((c) / 16) & 0x3ff) << 24) +#define cap_set_mgaw(c) ((((c) - 1) & 0x3f) << 16) +#define cap_set_sagaw(c) (((c) & 0x1f) << 8) +#define cap_set_ndoms(c) ((c) & 0x7) + /* * Extended Capability Register */ @@ -114,6 +121,8 @@ #define ecap_niotlb_iunits(e) ((((e) >> 24) & 0xff) + 1) #define ecap_iotlb_offset(e) ((((e) >> 8) & 0x3ff) * 16) +#define ecap_set_mhmv(e) (((e) & 0xf) << 20) + /* IOTLB_REG */ #define DMA_TLB_FLUSH_GRANU_OFFSET 60 #define DMA_TLB_GLOBAL_FLUSH (((u64)1) << 60) diff --git a/xen/drivers/passthrough/vtd/vvtd.c b/xen/drivers/passthrough/vtd/vvtd.c new file mode 100644 index 0000000..9f76ccf --- /dev/null +++ b/xen/drivers/passthrough/vtd/vvtd.c @@ -0,0 +1,150 @@ +/* + * vvtd.c + * + * virtualize VTD for HVM. + * + * Copyright (C) 2017 Chao Gao, Intel Corporation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms and conditions of the GNU General Public + * License, version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; If not, see . + */ + +#include +#include +#include +#include +#include +#include + +#include "iommu.h" + +/* Supported capabilities by vvtd */ +#define VVTD_MAX_CAPS VIOMMU_CAP_IRQ_REMAPPING + +#define VVTD_FRCD_NUM 1ULL +#define VVTD_FRCD_START (DMAR_IRTA_REG + 8) +#define VVTD_FRCD_END (VVTD_FRCD_START + VVTD_FRCD_NUM * 16) +#define VVTD_MAX_OFFSET VVTD_FRCD_END + +struct hvm_hw_vvtd { + uint32_t regs[VVTD_MAX_OFFSET/sizeof(uint32_t)]; +}; + +struct vvtd { + /* Base address of remapping hardware register-set */ + uint64_t base_addr; + /* Point back to the owner domain */ + struct domain *domain; + + struct hvm_hw_vvtd hw; +}; + +/* Setting viommu_verbose enables debugging messages of vIOMMU */ +bool __read_mostly viommu_verbose; +boolean_runtime_param("viommu_verbose", viommu_verbose); + +#ifndef NDEBUG +#define vvtd_info(fmt...) do { \ + if ( viommu_verbose ) \ + gprintk(XENLOG_INFO, ## fmt); \ +} while(0) +/* + * Use printk and '_G_' prefix because vvtd_debug() may be called + * in the context of another domain's vCPU. Don't output 'current' + * information to avoid confusion. + */ +#define vvtd_debug(fmt...) do { \ + if ( viommu_verbose && printk_ratelimit()) \ + printk(XENLOG_G_DEBUG fmt); \ +} while(0) +#else +#define vvtd_info(...) do {} while(0) +#define vvtd_debug(...) do {} while(0) +#endif + +#define VVTD_REG_POS(vvtd, offset) &(vvtd->hw.regs[offset/sizeof(uint32_t)]) + +static inline void vvtd_set_reg(struct vvtd *vvtd, uint32_t reg, uint32_t value) +{ + *VVTD_REG_POS(vvtd, reg) = value; +} + +static inline uint32_t vvtd_get_reg(const struct vvtd *vvtd, uint32_t reg) +{ + return *VVTD_REG_POS(vvtd, reg); +} + +static inline void vvtd_set_reg_quad(struct vvtd *vvtd, uint32_t reg, + uint64_t value) +{ + *(uint64_t*)VVTD_REG_POS(vvtd, reg) = value; +} + +static inline uint64_t vvtd_get_reg_quad(const struct vvtd *vvtd, uint32_t reg) +{ + return *(uint64_t*)VVTD_REG_POS(vvtd, reg); +} + +static void vvtd_reset(struct vvtd *vvtd) +{ + uint64_t cap = cap_set_num_fault_regs(VVTD_FRCD_NUM) + | cap_set_fault_reg_offset(VVTD_FRCD_START) + | cap_set_mgaw(39) /* maximum guest address width */ + | cap_set_sagaw(2) /* support 3-level page_table */ + | cap_set_ndoms(6); /* support 64K domains */ + uint64_t ecap = DMA_ECAP_QUEUED_INVAL | DMA_ECAP_INTR_REMAP | DMA_ECAP_EIM | + ecap_set_mhmv(0xf); + + vvtd_set_reg(vvtd, DMAR_VER_REG, 0x10UL); + vvtd_set_reg_quad(vvtd, DMAR_CAP_REG, cap); + vvtd_set_reg_quad(vvtd, DMAR_ECAP_REG, ecap); + vvtd_set_reg(vvtd, DMAR_FECTL_REG, 0x80000000UL); + vvtd_set_reg(vvtd, DMAR_IECTL_REG, 0x80000000UL); +} + +static int vvtd_create(struct domain *d, struct viommu *viommu) +{ + struct vvtd *vvtd; + + if ( !is_hvm_domain(d) || (viommu->base_address & (PAGE_SIZE - 1)) || + (~VVTD_MAX_CAPS & viommu->caps) ) + return -EINVAL; + + vvtd = xzalloc_bytes(sizeof(struct vvtd)); + if ( !vvtd ) + return ENOMEM; + + vvtd_reset(vvtd); + vvtd->base_addr = viommu->base_address; + vvtd->domain = d; + + viommu->priv = vvtd; + + return 0; +} + +static int vvtd_destroy(struct viommu *viommu) +{ + struct vvtd *vvtd = viommu->priv; + + if ( vvtd ) + xfree(vvtd); + + return 0; +} + +static const struct viommu_ops vvtd_hvm_vmx_ops = { + .create = vvtd_create, + .destroy = vvtd_destroy, +}; + +REGISTER_VIOMMU(vvtd_hvm_vmx_ops);