From patchwork Fri Sep 22 03:01:43 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "lan,Tianyu" X-Patchwork-Id: 9965579 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 D59526057C for ; Fri, 22 Sep 2017 09:12:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CF66F28501 for ; Fri, 22 Sep 2017 09:12:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C41A028757; Fri, 22 Sep 2017 09:12:11 +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 0168A28517 for ; Fri, 22 Sep 2017 09:12:11 +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 1dvJxo-0003BV-MS; Fri, 22 Sep 2017 09:09:36 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dvJxn-0003Ac-Ie for xen-devel@lists.xen.org; Fri, 22 Sep 2017 09:09:35 +0000 Received: from [85.158.139.211] by server-4.bemta-5.messagelabs.com id E5/73-09852-E43D4C95; Fri, 22 Sep 2017 09:09:34 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmpkkeJIrShJLcpLzFFi42Jpa+uQ0PW7fCT S4OwCAYslHxezODB6HN39mymAMYo1My8pvyKBNePahItsBfcdK3ad+8TawLjDpIuRi0NIYBqj xNqJj9m7GDk5JAR4JY4sm8EKYQdIrPg6hQ2iqINR4tnVA0wgCTYBdYkTiycygtgiAtIS1z5fZ gQpYhZYwSSx8vESsISwQLzEx0Vb2EBsFgFViVW37zF3MXJw8Aq4SKzr1oFYoCAx5eF7ZhCbU8 BV4lffdrD5QkAlrUv3M01g5F3AyLCKUaM4tagstUjXyEwvqSgzPaMkNzEzR9fQwFQvN7W4ODE 9NScxqVgvOT93EyMwIOoZGBh3MN6e7HeIUZKDSUmU9/35I5FCfEn5KZUZicUZ8UWlOanFhxhl ODiUJHhFLwHlBItS01Mr0jJzgKEJk5bg4FES4X11ESjNW1yQmFucmQ6ROsVozHFgwpU/TBwdN +/+YRJiycvPS5US59UDmSQAUppRmgc3CBYzlxhlpYR5GRkYGIR4ClKLcjNLUOVfMYpzMCoJ8x qATOHJzCuB2/cK6BQmoFPKV4OdUpKIkJJqYFz41HlLQc5y23n26c3ip+2PtGQzM7X9lX6e7bV fwN7wz/401Z0B+4WtZB+scz3tkBnQ8/Karz+nn3yXTpH93dri3qkbr/Wo5t3iufBfXzTVg4O1 al/VzMvfDbcknO275pwZ2fPcfqvM/sZPJ97M4HmSd2+BTUHaEhnXH1k+b91+p9R9TLc9psRSn JFoqMVcVJwIAGL5ib+UAgAA X-Env-Sender: tianyu.lan@intel.com X-Msg-Ref: server-2.tower-206.messagelabs.com!1506071370!92157135!2 X-Originating-IP: [134.134.136.24] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogMTM0LjEzNC4xMzYuMjQgPT4gMzkwOTcx\n X-StarScan-Received: X-StarScan-Version: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 7123 invoked from network); 22 Sep 2017 09:09:33 -0000 Received: from mga09.intel.com (HELO mga09.intel.com) (134.134.136.24) by server-2.tower-206.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 22 Sep 2017 09:09:33 -0000 Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 22 Sep 2017 02:09:33 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos; i="5.42,427,1500966000"; d="scan'208"; a="1017290687" Received: from sky-ws.sh.intel.com (HELO localhost) ([10.239.48.141]) by orsmga003.jf.intel.com with ESMTP; 22 Sep 2017 02:09:30 -0700 From: Lan Tianyu To: xen-devel@lists.xen.org Date: Thu, 21 Sep 2017 23:01:43 -0400 Message-Id: <1506049330-11196-3-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@intel.com Subject: [Xen-devel] [PATCH V3 2/29] VIOMMU: Add vIOMMU helper functions to create, destroy vIOMMU instance 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 is to introduce an abstract layer for arch vIOMMU implementation to deal with requests from dom0. Arch vIOMMU code needs to provide callback to do create and destroy operation. Signed-off-by: Lan Tianyu --- docs/misc/xen-command-line.markdown | 7 ++ xen/arch/x86/Kconfig | 1 + xen/common/Kconfig | 3 + xen/common/Makefile | 1 + xen/common/domain.c | 4 + xen/common/viommu.c | 144 ++++++++++++++++++++++++++++++++++++ xen/include/xen/sched.h | 8 ++ xen/include/xen/viommu.h | 63 ++++++++++++++++ 8 files changed, 231 insertions(+) create mode 100644 xen/common/viommu.c create mode 100644 xen/include/xen/viommu.h diff --git a/docs/misc/xen-command-line.markdown b/docs/misc/xen-command-line.markdown index 9797c8d..dfd1db5 100644 --- a/docs/misc/xen-command-line.markdown +++ b/docs/misc/xen-command-line.markdown @@ -1825,3 +1825,10 @@ mode. > Default: `true` Permit use of the `xsave/xrstor` instructions. + +### viommu +> `= ` + +> Default: `false` + +Permit use of viommu interface to create and destroy viommu device model. diff --git a/xen/arch/x86/Kconfig b/xen/arch/x86/Kconfig index 30c2769..1f1de96 100644 --- a/xen/arch/x86/Kconfig +++ b/xen/arch/x86/Kconfig @@ -23,6 +23,7 @@ config X86 select HAS_PDX select NUMA select VGA + select VIOMMU config ARCH_DEFCONFIG string diff --git a/xen/common/Kconfig b/xen/common/Kconfig index dc8e876..2ad2c8d 100644 --- a/xen/common/Kconfig +++ b/xen/common/Kconfig @@ -49,6 +49,9 @@ config HAS_CHECKPOLICY string option env="XEN_HAS_CHECKPOLICY" +config VIOMMU + bool + config KEXEC bool "kexec support" default y diff --git a/xen/common/Makefile b/xen/common/Makefile index 39e2614..da32f71 100644 --- a/xen/common/Makefile +++ b/xen/common/Makefile @@ -56,6 +56,7 @@ obj-y += time.o obj-y += timer.o obj-y += trace.o obj-y += version.o +obj-$(CONFIG_VIOMMU) += viommu.o obj-y += virtual_region.o obj-y += vm_event.o obj-y += vmap.o diff --git a/xen/common/domain.c b/xen/common/domain.c index 5aebcf2..cdb1c9d 100644 --- a/xen/common/domain.c +++ b/xen/common/domain.c @@ -814,6 +814,10 @@ static void complete_domain_destroy(struct rcu_head *head) sched_destroy_domain(d); +#ifdef CONFIG_VIOMMU + viommu_destroy_domain(d); +#endif + /* Free page used by xen oprofile buffer. */ #ifdef CONFIG_XENOPROF free_xenoprof_pages(d); diff --git a/xen/common/viommu.c b/xen/common/viommu.c new file mode 100644 index 0000000..64d91e6 --- /dev/null +++ b/xen/common/viommu.c @@ -0,0 +1,144 @@ +/* + * common/viommu.c + * + * Copyright (c) 2017 Intel Corporation + * Author: Lan Tianyu + * + * 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 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 + +bool __read_mostly opt_viommu; +boolean_param("viommu", opt_viommu); + +static DEFINE_SPINLOCK(type_list_lock); +static LIST_HEAD(type_list); + +struct viommu_type { + uint64_t type; + struct viommu_ops *ops; + struct list_head node; +}; + +int viommu_destroy_domain(struct domain *d) +{ + int ret; + + if ( !d->viommu ) + return -EINVAL; + + ret = d->viommu->ops->destroy(d->viommu); + if ( ret < 0 ) + return ret; + + xfree(d->viommu); + d->viommu = NULL; + return 0; +} + +static struct viommu_type *viommu_get_type(uint64_t type) +{ + struct viommu_type *viommu_type = NULL; + + spin_lock(&type_list_lock); + list_for_each_entry( viommu_type, &type_list, node ) + { + if ( viommu_type->type == type ) + { + spin_unlock(&type_list_lock); + return viommu_type; + } + } + spin_unlock(&type_list_lock); + + return NULL; +} + +int viommu_register_type(uint64_t type, struct viommu_ops *ops) +{ + struct viommu_type *viommu_type = NULL; + + if ( !viommu_enabled() ) + return -ENODEV; + + if ( viommu_get_type(type) ) + return -EEXIST; + + viommu_type = xzalloc(struct viommu_type); + if ( !viommu_type ) + return -ENOMEM; + + viommu_type->type = type; + viommu_type->ops = ops; + + spin_lock(&type_list_lock); + list_add_tail(&viommu_type->node, &type_list); + spin_unlock(&type_list_lock); + + return 0; +} + +static int viommu_create(struct domain *d, uint64_t type, + uint64_t base_address, uint64_t caps, + uint32_t *viommu_id) +{ + struct viommu *viommu; + struct viommu_type *viommu_type = NULL; + int rc; + + /* Only support one vIOMMU per domain. */ + if ( d->viommu ) + return -E2BIG; + + viommu_type = viommu_get_type(type); + if ( !viommu_type ) + return -EINVAL; + + if ( !viommu_type->ops || !viommu_type->ops->create ) + return -EINVAL; + + viommu = xzalloc(struct viommu); + if ( !viommu ) + return -ENOMEM; + + viommu->base_address = base_address; + viommu->caps = caps; + viommu->ops = viommu_type->ops; + + rc = viommu->ops->create(d, viommu); + if ( rc < 0 ) + { + xfree(viommu); + return rc; + } + + d->viommu = viommu; + + /* Only support one vIOMMU per domain. */ + *viommu_id = 0; + return 0; +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h index 5b8f8c6..750f235 100644 --- a/xen/include/xen/sched.h +++ b/xen/include/xen/sched.h @@ -33,6 +33,10 @@ DEFINE_XEN_GUEST_HANDLE(vcpu_runstate_info_compat_t); #endif +#ifdef CONFIG_VIOMMU +#include +#endif + /* * Stats * @@ -479,6 +483,10 @@ struct domain rwlock_t vnuma_rwlock; struct vnuma_info *vnuma; +#ifdef CONFIG_VIOMMU + struct viommu *viommu; +#endif + /* Common monitor options */ struct { unsigned int guest_request_enabled : 1; diff --git a/xen/include/xen/viommu.h b/xen/include/xen/viommu.h new file mode 100644 index 0000000..636a2a3 --- /dev/null +++ b/xen/include/xen/viommu.h @@ -0,0 +1,63 @@ +/* + * include/xen/viommu.h + * + * Copyright (c) 2017, Intel Corporation + * Author: Lan Tianyu + * + * 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 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 . + * + */ +#ifndef __XEN_VIOMMU_H__ +#define __XEN_VIOMMU_H__ + +struct viommu; + +struct viommu_ops { + int (*create)(struct domain *d, struct viommu *viommu); + int (*destroy)(struct viommu *viommu); +}; + +struct viommu { + uint64_t base_address; + uint64_t caps; + const struct viommu_ops *ops; + void *priv; +}; + +#ifdef CONFIG_VIOMMU +extern bool opt_viommu; +static inline bool viommu_enabled(void) +{ + return opt_viommu; +} + +int viommu_register_type(uint64_t type, struct viommu_ops *ops); +int viommu_destroy_domain(struct domain *d); +#else +static inline int viommu_register_type(uint64_t type, struct viommu_ops *ops) +{ + return -EINVAL; +} +#endif + +#endif /* __XEN_VIOMMU_H__ */ + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */