From patchwork Fri Mar 17 11:29:15 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "lan,Tianyu" X-Patchwork-Id: 9630465 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 54A4960132 for ; Fri, 17 Mar 2017 11:38:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3DBB4284E9 for ; Fri, 17 Mar 2017 11:38:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 325AA28644; Fri, 17 Mar 2017 11:38:19 +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.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID 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 A398F284E9 for ; Fri, 17 Mar 2017 11:38:18 +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 1coqBr-0005m5-KA; Fri, 17 Mar 2017 11:37:03 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1coqBr-0005kt-4n for xen-devel@lists.xensource.com; Fri, 17 Mar 2017 11:37:03 +0000 Received: from [85.158.137.68] by server-5.bemta-3.messagelabs.com id 97/38-19998-E5ACBC85; Fri, 17 Mar 2017 11:37:02 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmpnkeJIrShJLcpLzFFi42I5YG4Sqxt36nS EwbWNTBb3prxnd2D02N63iz2AMYo1My8pvyKBNePLzTnsBfsVKy79WsDSwNgl3cXIxcEicItJ oml+LzOIIyQwjVFix/SPrF2MnBwSArwSR5bNgLL9JC5+ns4IUdTPKHH14g42kASbgLrEicUTG UFsEQFLiYXTWllAipgF2hgl+v48YgFJCAsESMw/8hysgUVAVeLTi5dgcV4BF4lVK7+wdzFyAG 1QkJgzyQYkzCngKrH+7Uo2kLAQUMnOa3kTGPkWMDKsYtQoTi0qSy3SNTLXSyrKTM8oyU3MzNE 1NDDWy00tLk5MT81JTCrWS87P3cQIDJR6BgbGHYwte/0OMUpyMCmJ8qoInogQ4kvKT6nMSCzO iC8qzUktPsQow8GhJME7ZSFQTrAoNT21Ii0zBxiyMGkJDh4lEd6pIGne4oLE3OLMdIjUKUZdj kX/dr9hEmLJy89LlRLnfQRSJABSlFGaBzcCFj+XGGWlhHkZGRgYhHgKUotyM0tQ5V8xinMwKg nzai8CmsKTmVcCt+kV0BFMQEck/jwCckRJIkJKqoFRgWWe9FxJT+dPuVumcxeFPe1a4b2j04v zTfBMPh2OvbVXLz5trCtd+9n20dqLPaVKccudzNifF19u3t0V8G/K/0trXSsXJ5u1zvh48Muh LX8Di2+97Em6llFRqCLLyKG7usWozvTvjaenTs0wqu7gWRFwfXNF25wXdfXcnMumcCi6bf3Td 3OWEktxRqKhFnNRcSIA3UC9t5oCAAA= X-Env-Sender: tianyu.lan@intel.com X-Msg-Ref: server-6.tower-31.messagelabs.com!1489750619!64427069!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.2.3; banners=-,-,- X-VirusChecked: Checked Received: (qmail 65249 invoked from network); 17 Mar 2017 11:37:01 -0000 Received: from mga11.intel.com (HELO mga11.intel.com) (192.55.52.93) by server-6.tower-31.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 17 Mar 2017 11:37:01 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=intel.com; i=@intel.com; q=dns/txt; s=intel; t=1489750621; x=1521286621; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=FJn+QHsAGjc/hwhMYdxcpzF/yDLppitdiY2JCZnxdjU=; b=apVeSQRnmjpKdYrfCqhnhU8SwsrsefOpd5Hvl0Yr/HKBwJnrt1ZmL7rA RHgWmzqAYoUyrFy7vHlRrPHYhvWU2A==; Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 17 Mar 2017 04:36:59 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.36,176,1486454400"; d="scan'208";a="76518137" Received: from lantianyu-ws.sh.intel.com (HELO localhost) ([10.239.159.159]) by orsmga005.jf.intel.com with ESMTP; 17 Mar 2017 04:36:57 -0700 From: Lan Tianyu To: xen-devel@lists.xensource.com, qemu-devel@nongnu.org Date: Fri, 17 Mar 2017 19:29:15 +0800 Message-Id: <1489750157-17401-3-git-send-email-tianyu.lan@intel.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1489750157-17401-1-git-send-email-tianyu.lan@intel.com> References: <1489750157-17401-1-git-send-email-tianyu.lan@intel.com> Cc: anthony.perard@citrix.com, Lan Tianyu , kevin.tian@intel.com, sstabellini@kernel.org, chao.gao@intel.com Subject: [Xen-devel] [RFC PATCH 2/4] Xen: add a dummy vIOMMU to create/destroy vIOMMU in Xen 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 Since adding a dynamic sysbus device is forbidden, so choose TYPE_DEVICE as parent class. Signed-off-by: Chao Gao Signed-off-by: Lan Tianyu --- hw/xen/Makefile.objs | 1 + hw/xen/xen_viommu.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 hw/xen/xen_viommu.c diff --git a/hw/xen/Makefile.objs b/hw/xen/Makefile.objs index d367094..e37d808 100644 --- a/hw/xen/Makefile.objs +++ b/hw/xen/Makefile.objs @@ -3,3 +3,4 @@ common-obj-$(CONFIG_XEN_BACKEND) += xen_backend.o xen_devconfig.o obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen-host-pci-device.o obj-$(CONFIG_XEN_PCI_PASSTHROUGH) += xen_pt.o xen_pt_config_init.o xen_pt_graphics.o xen_pt_msi.o +obj-$(CONFIG_XEN) += xen_viommu.o diff --git a/hw/xen/xen_viommu.c b/hw/xen/xen_viommu.c new file mode 100644 index 0000000..9bf9158 --- /dev/null +++ b/hw/xen/xen_viommu.c @@ -0,0 +1,116 @@ +/* + * Xen virtual IOMMU (virtual VT-D) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + + * 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 "qemu/osdep.h" +#include "qemu/error-report.h" + +#include "hw/qdev-core.h" +#include "hw/sysbus.h" +#include "hw/xen/xen.h" +#include "hw/xen/xen_backend.h" + +#define TYPE_XEN_VIOMMU_DEVICE "xen_viommu" +#define XEN_VIOMMU_DEVICE(obj) \ + OBJECT_CHECK(XenVIOMMUState, (obj), TYPE_XEN_VIOMMU_DEVICE) + +typedef struct XenVIOMMUState XenVIOMMUState; + +struct XenVIOMMUState { + DeviceState dev; + uint32_t id; + uint64_t cap; + uint64_t base_addr; +}; + +static void xen_viommu_realize(DeviceState *dev, Error **errp) +{ + int rc; + uint64_t cap; + char *dom; + char viommu_path[1024]; + XenVIOMMUState *s = XEN_VIOMMU_DEVICE(dev); + + s->id = -1; + + /* Read vIOMMU attributes from Xenstore. */ + dom = xs_get_domain_path(xenstore, xen_domid); + snprintf(viommu_path, sizeof(viommu_path), "%s/viommu", dom); + rc = xenstore_read_uint64(viommu_path, "base_addr", &s->base_addr); + if (rc) { + error_report("Can't get base address of vIOMMU"); + exit(1); + } + + rc = xenstore_read_uint64(viommu_path, "cap", &s->cap); + if (rc) { + error_report("Can't get capabilities of vIOMMU"); + exit(1); + } + + rc = xc_viommu_query_cap(xen_xc, xen_domid, &cap); + if (rc) { + exit(1); + } + + + if ((cap & s->cap) != cap) { + error_report("xen: Unsupported capability %lx", s->cap); + exit(1); + } + + rc = xc_viommu_create(xen_xc, xen_domid, s->base_addr, s->cap, &s->id); + if (rc) { + s->id = -1; + error_report("xen: failed(%d) to create viommu ", rc); + exit(1); + } +} + +static void xen_viommu_instance_finalize(Object *o) +{ + int rc; + XenVIOMMUState *s = XEN_VIOMMU_DEVICE(o); + + if (s->id != -1) { + rc = xc_viommu_destroy(xen_xc, xen_domid, s->id); + if (rc) { + error_report("xen: failed(%d) to destroy viommu ", rc); + } + } +} + +static void xen_viommu_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + dc->hotpluggable = false; + dc->realize = xen_viommu_realize; +} + +static const TypeInfo xen_viommu_info = { + .name = TYPE_XEN_VIOMMU_DEVICE, + .parent = TYPE_SYS_BUS_DEVICE, + .instance_size = sizeof(XenVIOMMUState), + .instance_finalize = xen_viommu_instance_finalize, + .class_init = xen_viommu_class_init, +}; + +static void xen_viommu_register_types(void) +{ + type_register_static(&xen_viommu_info); +} + +type_init(xen_viommu_register_types);