From patchwork Fri Sep 22 03:01:52 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "lan,Tianyu" X-Patchwork-Id: 9965607 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 378A8600C5 for ; Fri, 22 Sep 2017 09:12:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 31E9028501 for ; Fri, 22 Sep 2017 09:12:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 26A062885F; Fri, 22 Sep 2017 09:12:29 +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=-2.7 required=2.0 tests=BAYES_00, DATE_IN_PAST_06_12, 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 8461A28501 for ; Fri, 22 Sep 2017 09:12:28 +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 1dvJyQ-000465-8F; Fri, 22 Sep 2017 09:10:14 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dvJyP-00044J-2m for xen-devel@lists.xen.org; Fri, 22 Sep 2017 09:10:13 +0000 Received: from [85.158.139.211] by server-13.bemta-5.messagelabs.com id A8/F9-01754-473D4C95; Fri, 22 Sep 2017 09:10:12 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrPLMWRWlGSWpSXmKPExsVywNwkVrf48pF Ig2XTDS2WfFzM4sDocXT3b6YAxijWzLyk/IoE1oxP77IKGm0rLt86wNbAeMqgi5GLQ0hgOqPE xFtH2boYOTkkBHgljiybwQph+0vsnP+BHaKog1HizNQuFpAEm4C6xInFExlBbBEBaYlrny8zg hQxC2xmkpi7ZhZYkbCAs8T/E9fBbBYBVYn9L64zdTFycPAKuEocnBcEsUBBYsrD98wgNidQ+F ffdiYQW0jARaJ16X6mCYy8CxgZVjGqF6cWlaUW6ZrrJRVlpmeU5CZm5ugaGpjq5aYWFyemp+Y kJhXrJefnbmIEBgMDEOxgPDbZ+RCjJAeTkijv+/NHIoX4kvJTKjMSizPii0pzUosPMcpwcChJ 8IpeAsoJFqWmp1akZeYAwxImLcHBoyTCqwaS5i0uSMwtzkyHSJ1iNOY4MOHKHyaOjpt3/zAJs eTl56VKifPqgZQKgJRmlObBDYLFyyVGWSlhXkag04R4ClKLcjNLUOVfMYpzMCoJ8xqATOHJzC uB2/cK6BQmoFPKV4OdUpKIkJJqYCwSVn22bA7LVInKjyomN7ok7+08PfXNHbv8A9/4ZuaUM61 946bwm3l+177sXm2li56hyvauEw4UCHtuvrXFTzhq6x6j9wbLrc/6MQl6Wt7jclTrULEwNJ8q VhFlGL7EVn2t+vGX/dKeflOamq+ynORauWaaqWL5y4U1P3w+aV9vPupzt12sWomlOCPRUIu5q DgRAI35si+SAgAA X-Env-Sender: tianyu.lan@intel.com X-Msg-Ref: server-12.tower-206.messagelabs.com!1506071409!76473848!1 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 35581 invoked from network); 22 Sep 2017 09:10:11 -0000 Received: from mga11.intel.com (HELO mga11.intel.com) (192.55.52.93) by server-12.tower-206.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 22 Sep 2017 09:10:11 -0000 Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 22 Sep 2017 02:10:06 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.42,427,1500966000"; d="scan'208";a="315007339" Received: from sky-ws.sh.intel.com (HELO localhost) ([10.239.48.141]) by fmsmga004.fm.intel.com with ESMTP; 22 Sep 2017 02:10:04 -0700 From: Lan Tianyu To: xen-devel@lists.xen.org Date: Thu, 21 Sep 2017 23:01:52 -0400 Message-Id: <1506049330-11196-12-git-send-email-tianyu.lan@intel.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1506049330-11196-1-git-send-email-tianyu.lan@intel.com> References: <1506049330-11196-1-git-send-email-tianyu.lan@intel.com> Cc: Lan Tianyu , kevin.tian@intel.com, sstabellini@kernel.org, wei.liu2@citrix.com, George.Dunlap@eu.citrix.com, andrew.cooper3@citrix.com, ian.jackson@eu.citrix.com, tim@xen.org, jbeulich@suse.com, roger.pau@citrix.com, Chao Gao Subject: [Xen-devel] [PATCH V3 11/29] 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 From: Chao Gao This patch adds create/destroy function for the emulated VTD and adapts it to the common VIOMMU abstraction. Signed-off-by: Chao Gao Signed-off-by: Lan Tianyu --- xen/drivers/passthrough/vtd/Makefile | 7 +- xen/drivers/passthrough/vtd/iommu.h | 23 +++++- xen/drivers/passthrough/vtd/vvtd.c | 147 +++++++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+), 7 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 d7e433e..ef038c9 100644 --- a/xen/drivers/passthrough/vtd/iommu.h +++ b/xen/drivers/passthrough/vtd/iommu.h @@ -66,6 +66,12 @@ #define VER_MAJOR(v) (((v) & 0xf0) >> 4) #define VER_MINOR(v) ((v) & 0x0f) +/* Supported Adjusted Guest Address Widths */ +#define DMA_CAP_SAGAW_SHIFT 8 + /* 39-bit AGAW, 3-level page-table */ +#define DMA_CAP_SAGAW_39bit (0x2ULL << DMA_CAP_SAGAW_SHIFT) +#define DMA_CAP_ND_64K 6ULL + /* * Decoding Capability Register */ @@ -74,6 +80,7 @@ #define cap_write_drain(c) (((c) >> 54) & 1) #define cap_max_amask_val(c) (((c) >> 48) & 0x3f) #define cap_num_fault_regs(c) ((((c) >> 40) & 0xff) + 1) +#define cap_set_num_fault_regs(c) ((((c) - 1) & 0xff) << 40) #define cap_pgsel_inv(c) (((c) >> 39) & 1) #define cap_super_page_val(c) (((c) >> 34) & 0xf) @@ -85,11 +92,13 @@ #define cap_sps_1tb(c) ((c >> 37) & 1) #define cap_fault_reg_offset(c) ((((c) >> 24) & 0x3ff) * 16) +#define cap_set_fault_reg_offset(c) ((((c) / 16) & 0x3ff) << 24 ) #define cap_isoch(c) (((c) >> 23) & 1) #define cap_qos(c) (((c) >> 22) & 1) #define cap_mgaw(c) ((((c) >> 16) & 0x3f) + 1) -#define cap_sagaw(c) (((c) >> 8) & 0x1f) +#define cap_set_mgaw(c) ((((c) - 1) & 0x3f) << 16) +#define cap_sagaw(c) (((c) >> DMA_CAP_SAGAW_SHIFT) & 0x1f) #define cap_caching_mode(c) (((c) >> 7) & 1) #define cap_phmr(c) (((c) >> 6) & 1) #define cap_plmr(c) (((c) >> 5) & 1) @@ -104,10 +113,16 @@ #define ecap_niotlb_iunits(e) ((((e) >> 24) & 0xff) + 1) #define ecap_iotlb_offset(e) ((((e) >> 8) & 0x3ff) * 16) #define ecap_coherent(e) ((e >> 0) & 0x1) -#define ecap_queued_inval(e) ((e >> 1) & 0x1) +#define DMA_ECAP_QI_SHIFT 1 +#define DMA_ECAP_QI (1ULL << DMA_ECAP_QI_SHIFT) +#define ecap_queued_inval(e) ((e >> DMA_ECAP_QI_SHIFT) & 0x1) #define ecap_dev_iotlb(e) ((e >> 2) & 0x1) -#define ecap_intr_remap(e) ((e >> 3) & 0x1) -#define ecap_eim(e) ((e >> 4) & 0x1) +#define DMA_ECAP_IR_SHIFT 3 +#define DMA_ECAP_IR (1ULL << DMA_ECAP_IR_SHIFT) +#define ecap_intr_remap(e) ((e >> DMA_ECAP_IR_SHIFT) & 0x1) +#define DMA_ECAP_EIM_SHIFT 4 +#define DMA_ECAP_EIM (1ULL << DMA_ECAP_EIM_SHIFT) +#define ecap_eim(e) ((e >> DMA_ECAP_EIM_SHIFT) & 0x1) #define ecap_cache_hints(e) ((e >> 5) & 0x1) #define ecap_pass_thru(e) ((e >> 6) & 0x1) #define ecap_snp_ctl(e) ((e >> 7) & 0x1) diff --git a/xen/drivers/passthrough/vtd/vvtd.c b/xen/drivers/passthrough/vtd/vvtd.c new file mode 100644 index 0000000..c851ec7 --- /dev/null +++ b/xen/drivers/passthrough/vtd/vvtd.c @@ -0,0 +1,147 @@ +/* + * 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 +#include + +#include "iommu.h" + +/* Supported capabilities by vvtd */ +unsigned int vvtd_caps = VIOMMU_CAP_IRQ_REMAPPING; + +union hvm_hw_vvtd_regs { + uint32_t data32[256]; + uint64_t data64[128]; +}; + +struct vvtd { + /* Address range of remapping hardware register-set */ + uint64_t base_addr; + uint64_t length; + /* Point back to the owner domain */ + struct domain *domain; + union hvm_hw_vvtd_regs *regs; + struct page_info *regs_page; +}; + +static inline void vvtd_set_reg(struct vvtd *vtd, uint32_t reg, uint32_t value) +{ + vtd->regs->data32[reg/sizeof(uint32_t)] = value; +} + +static inline uint32_t vvtd_get_reg(struct vvtd *vtd, uint32_t reg) +{ + return vtd->regs->data32[reg/sizeof(uint32_t)]; +} + +static inline void vvtd_set_reg_quad(struct vvtd *vtd, uint32_t reg, + uint64_t value) +{ + vtd->regs->data64[reg/sizeof(uint64_t)] = value; +} + +static inline uint64_t vvtd_get_reg_quad(struct vvtd *vtd, uint32_t reg) +{ + return vtd->regs->data64[reg/sizeof(uint64_t)]; +} + +static void vvtd_reset(struct vvtd *vvtd, uint64_t capability) +{ + uint64_t cap = cap_set_num_fault_regs(1ULL) | + cap_set_fault_reg_offset(0x220ULL) | + cap_set_mgaw(39ULL) | DMA_CAP_SAGAW_39bit | + DMA_CAP_ND_64K; + uint64_t ecap = DMA_ECAP_IR | DMA_ECAP_EIM | DMA_ECAP_QI; + + 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; + int ret; + + if ( !is_hvm_domain(d) || (viommu->base_address & (PAGE_SIZE - 1)) || + (~vvtd_caps & viommu->caps) ) + return -EINVAL; + + ret = -ENOMEM; + vvtd = xzalloc_bytes(sizeof(struct vvtd)); + if ( !vvtd ) + return ret; + + vvtd->regs_page = alloc_domheap_page(d, MEMF_no_owner); + if ( !vvtd->regs_page ) + goto out1; + + vvtd->regs = __map_domain_page_global(vvtd->regs_page); + if ( !vvtd->regs ) + goto out2; + clear_page(vvtd->regs); + + vvtd_reset(vvtd, viommu->caps); + vvtd->base_addr = viommu->base_address; + vvtd->domain = d; + + viommu->priv = vvtd; + + return 0; + + out2: + free_domheap_page(vvtd->regs_page); + out1: + xfree(vvtd); + return ret; +} + +static int vvtd_destroy(struct viommu *viommu) +{ + struct vvtd *vvtd = viommu->priv; + + if ( vvtd ) + { + unmap_domain_page_global(vvtd->regs); + free_domheap_page(vvtd->regs_page); + xfree(vvtd); + } + return 0; +} + +struct viommu_ops vvtd_hvm_vmx_ops = { + .create = vvtd_create, + .destroy = vvtd_destroy +}; + +static int vvtd_register(void) +{ + viommu_register_type(VIOMMU_TYPE_INTEL_VTD, &vvtd_hvm_vmx_ops); + return 0; +} +__initcall(vvtd_register);