From patchwork Mon May 5 01:27:57 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gavin Shan X-Patchwork-Id: 4111271 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id B88C1BFF02 for ; Mon, 5 May 2014 01:28:24 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id C64C220414 for ; Mon, 5 May 2014 01:28:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id BB29120254 for ; Mon, 5 May 2014 01:28:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754045AbaEEB2F (ORCPT ); Sun, 4 May 2014 21:28:05 -0400 Received: from e23smtp07.au.ibm.com ([202.81.31.140]:47565 "EHLO e23smtp07.au.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753843AbaEEB2A (ORCPT ); Sun, 4 May 2014 21:28:00 -0400 Received: from /spool/local by e23smtp07.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 5 May 2014 11:27:58 +1000 Received: from d23dlp01.au.ibm.com (202.81.31.203) by e23smtp07.au.ibm.com (202.81.31.204) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 5 May 2014 11:27:56 +1000 Received: from d23relay03.au.ibm.com (d23relay03.au.ibm.com [9.190.235.21]) by d23dlp01.au.ibm.com (Postfix) with ESMTP id 034302CE8056; Mon, 5 May 2014 11:27:56 +1000 (EST) Received: from d23av04.au.ibm.com (d23av04.au.ibm.com [9.190.235.139]) by d23relay03.au.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id s451Rfh410420484; Mon, 5 May 2014 11:27:41 +1000 Received: from d23av04.au.ibm.com (localhost [127.0.0.1]) by d23av04.au.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id s451RsVw011701; Mon, 5 May 2014 11:27:55 +1000 Received: from ozlabs.au.ibm.com (ozlabs.au.ibm.com [9.190.163.12]) by d23av04.au.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id s451Rs80011695; Mon, 5 May 2014 11:27:54 +1000 Received: from shangw (haven.au.ibm.com [9.190.164.82]) by ozlabs.au.ibm.com (Postfix) with ESMTP id A766AA01A0; Mon, 5 May 2014 11:27:54 +1000 (EST) Received: by shangw (Postfix, from userid 1000) id 979833E0370; Mon, 5 May 2014 11:28:16 +1000 (EST) From: Gavin Shan To: linuxppc-dev@lists.ozlabs.org, kvm@vger.kernel.org, kvm-ppc@vger.kernel.org Cc: alex.williamson@redhat.com, benh@kernel.crashing.org, aik@ozlabs.ru, qiudayu@linux.vnet.ibm.com, Gavin Shan Subject: [PATCH 08/22] kvm: Address mapping for VFIO device Date: Mon, 5 May 2014 11:27:57 +1000 Message-Id: <1399253291-3975-9-git-send-email-gwshan@linux.vnet.ibm.com> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1399253291-3975-1-git-send-email-gwshan@linux.vnet.ibm.com> References: <1399253291-3975-1-git-send-email-gwshan@linux.vnet.ibm.com> X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 14050501-0260-0000-0000-000004E1DA4F Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The address (domain/bus/slot/function) looks different from the perspective of host and guest. We have to setup the mapping for EEH and tear it down accordingly. The patch introduces additional attributes to KVM VFIO device for address mapping or unmapping. Signed-off-by: Gavin Shan --- arch/powerpc/kvm/Kconfig | 1 + arch/powerpc/kvm/Makefile | 3 +++ include/uapi/linux/kvm.h | 10 ++++++++ virt/kvm/vfio.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 73 insertions(+), 1 deletion(-) diff --git a/arch/powerpc/kvm/Kconfig b/arch/powerpc/kvm/Kconfig index 743d2d9..6764fc5 100644 --- a/arch/powerpc/kvm/Kconfig +++ b/arch/powerpc/kvm/Kconfig @@ -64,6 +64,7 @@ config KVM_BOOK3S_64 select KVM_BOOK3S_64_HANDLER select KVM select KVM_BOOK3S_PR_POSSIBLE if !KVM_BOOK3S_HV_POSSIBLE + select KVM_VFIO if VFIO ---help--- Support running unmodified book3s_64 and book3s_32 guest kernels in virtual machines on book3s_64 host processors. diff --git a/arch/powerpc/kvm/Makefile b/arch/powerpc/kvm/Makefile index ce569b6..673038d 100644 --- a/arch/powerpc/kvm/Makefile +++ b/arch/powerpc/kvm/Makefile @@ -97,6 +97,9 @@ endif kvm-book3s_64-objs-$(CONFIG_KVM_XICS) += \ book3s_xics.o +kvm-book3s_64-objs-$(CONFIG_KVM_VFIO) += \ + $(addprefix ../../../virt/kvm/, vfio.o) + kvm-book3s_64-module-objs += \ $(KVM)/kvm_main.o \ $(KVM)/eventfd.o \ diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index a8f4ee5..97b4d1e 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -932,9 +932,19 @@ struct kvm_device_attr { #define KVM_DEV_VFIO_GROUP 1 #define KVM_DEV_VFIO_GROUP_ADD 1 #define KVM_DEV_VFIO_GROUP_DEL 2 +#define KVM_DEV_VFIO_DEV 2 +#define KVM_DEV_VFIO_DEV_EEH_MAP 1 +#define KVM_DEV_VFIO_DEV_EEH_UNMAP 2 #define KVM_DEV_TYPE_ARM_VGIC_V2 5 #define KVM_DEV_TYPE_FLIC 6 +struct kvm_vfio_pci_addr { + __u32 domain; /* Host PHB domain */ + __u32 bdn; /* Host bus/dev/func */ + __u64 gbuid; /* Guet PHB BUID */ + __u32 gbdn; /* Guest bus/dev/func */ +}; + /* * ioctls for VM fds */ diff --git a/virt/kvm/vfio.c b/virt/kvm/vfio.c index ba1a93f..778015d 100644 --- a/virt/kvm/vfio.c +++ b/virt/kvm/vfio.c @@ -28,6 +28,10 @@ struct kvm_vfio { struct list_head group_list; struct mutex lock; bool noncoherent; +#ifdef CONFIG_KVM_EEH + kvm_vfio_dev_eeh_map eeh_map; + kvm_vfio_dev_eeh_unmap eeh_unmap; +#endif }; static struct vfio_group *kvm_vfio_group_get_external_user(struct file *filep) @@ -201,12 +205,53 @@ static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg) return -ENXIO; } +static int kvm_vfio_set_dev(struct kvm_device *dev, long attr, u64 arg) +{ + struct kvm_vfio *kv = dev->private; + struct kvm_vfio_pci_addr addr; + int ret = -ENXIO; + + switch (attr) { +#ifdef CONFIG_KVM_EEH + case KVM_DEV_VFIO_DEV_EEH_MAP: + if (copy_from_user(&addr, (void __user *)arg, sizeof(addr))) { + ret = -EFAULT; + break; + } + + if (kv->eeh_map) + ret = kv->eeh_map(dev->kvm, addr.domain, + addr.bdn, addr.gbuid, addr.gbdn); + else + ret = 0; + + break; + case KVM_DEV_VFIO_DEV_EEH_UNMAP: + if (copy_from_user(&addr, (void __user *)arg, sizeof(addr))) { + ret = -EFAULT; + break; + } + + if (kv->eeh_unmap) + ret = kv->eeh_unmap(dev->kvm, addr.domain, addr.bdn); + else + ret = 0; + + break; +#endif + } + + return ret; +} + static int kvm_vfio_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr) { switch (attr->group) { case KVM_DEV_VFIO_GROUP: return kvm_vfio_set_group(dev, attr->attr, attr->addr); + case KVM_DEV_VFIO_DEV: + return kvm_vfio_set_dev(dev, attr->attr, attr->addr); } return -ENXIO; @@ -224,6 +269,16 @@ static int kvm_vfio_has_attr(struct kvm_device *dev, } break; + case KVM_DEV_VFIO_DEV: + switch (attr->attr) { +#ifdef CONFIG_KVM_EEH + case KVM_DEV_VFIO_DEV_EEH_MAP: + case KVM_DEV_VFIO_DEV_EEH_UNMAP: + return 0; +#endif + } + + break; } return -ENXIO; @@ -262,7 +317,10 @@ static int kvm_vfio_create(struct kvm_device *dev, u32 type) INIT_LIST_HEAD(&kv->group_list); mutex_init(&kv->lock); - +#ifdef CONFIG_KVM_EEH + kv->eeh_map = kvm_vfio_eeh_dev_map; + kv->eeh_unmap = kvm_vfio_eeh_dev_unmap; +#endif dev->private = kv; return 0;