From patchwork Fri Apr 29 12:13:21 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiao Feng Ren X-Patchwork-Id: 8982411 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id CC7D2BF29F for ; Fri, 29 Apr 2016 13:36:20 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id DB1F3201DD for ; Fri, 29 Apr 2016 13:36:16 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C1BFF200E8 for ; Fri, 29 Apr 2016 13:36:15 +0000 (UTC) Received: from localhost ([::1]:54462 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aw8aZ-0001ID-JU for patchwork-qemu-devel@patchwork.kernel.org; Fri, 29 Apr 2016 09:36:11 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40374) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aw7Je-0003Xz-Uc for qemu-devel@nongnu.org; Fri, 29 Apr 2016 08:14:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aw7JS-0003Qh-NK for qemu-devel@nongnu.org; Fri, 29 Apr 2016 08:14:33 -0400 Received: from e17.ny.us.ibm.com ([129.33.205.207]:36358) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aw7JS-0003HO-H8 for qemu-devel@nongnu.org; Fri, 29 Apr 2016 08:14:26 -0400 Received: from localhost by e17.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 29 Apr 2016 08:13:49 -0400 Received: from d01dlp03.pok.ibm.com (9.56.250.168) by e17.ny.us.ibm.com (146.89.104.204) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Fri, 29 Apr 2016 08:13:46 -0400 X-IBM-Helo: d01dlp03.pok.ibm.com X-IBM-MailFrom: renxiaof@linux.vnet.ibm.com X-IBM-RcptTo: qemu-devel@nongnu.org; alex.williamson@redhat.com; agraf@suse.com; kvm@vger.kernel.org; linux-s390@vger.kernel.org Received: from b01cxnp22033.gho.pok.ibm.com (b01cxnp22033.gho.pok.ibm.com [9.57.198.23]) by d01dlp03.pok.ibm.com (Postfix) with ESMTP id 06D96C90042; Fri, 29 Apr 2016 08:13:40 -0400 (EDT) Received: from b01ledav002.gho.pok.ibm.com (b01ledav002.gho.pok.ibm.com [9.57.199.107]) by b01cxnp22033.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id u3TCDjiZ30081102; Fri, 29 Apr 2016 12:13:45 GMT Received: from b01ledav002.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id EE841124035; Fri, 29 Apr 2016 13:12:12 -0400 (EDT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by b01ledav002.gho.pok.ibm.com (Postfix) with ESMTP id DD89E12403D; Fri, 29 Apr 2016 13:12:11 -0400 (EDT) From: Xiao Feng Ren To: kvm@vger.kernel.org, linux-s390@vger.kernel.org, qemu-devel@nongnu.org Date: Fri, 29 Apr 2016 14:13:21 +0200 Message-Id: <1461932003-23830-8-git-send-email-renxiaof@linux.vnet.ibm.com> X-Mailer: git-send-email 2.6.6 In-Reply-To: <1461932003-23830-1-git-send-email-renxiaof@linux.vnet.ibm.com> References: <1461932003-23830-1-git-send-email-renxiaof@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16042912-0041-0000-0000-0000040497B9 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 129.33.205.207 X-Mailman-Approved-At: Fri, 29 Apr 2016 09:31:35 -0400 Subject: [Qemu-devel] [PATCH RFC 7/9] vfio/ccw: vfio based ccw passthrough driver X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: borntraeger@de.ibm.com, alex.williamson@redhat.com, renxiaof@linux.vnet.ibm.com, cornelia.huck@de.ibm.com, bjsdjshi@linux.vnet.ibm.com, agraf@suse.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, 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 We use the No-IOMMU mode of VFIO to realize the ccw device passthrough, implement a vfio based ccw passthrough driver called "vfio-ccw". Support qemu parameters in the style of: "-device vfio-ccw,id=xx,hostid=xx(,guestid=xx)" Signed-off-by: Xiao Feng Ren --- default-configs/s390x-softmmu.mak | 1 + hw/vfio/Makefile.objs | 1 + hw/vfio/ccw.c | 188 ++++++++++++++++++++++++++++++++++++++ include/hw/vfio/vfio-common.h | 1 + 4 files changed, 191 insertions(+) create mode 100644 hw/vfio/ccw.c diff --git a/default-configs/s390x-softmmu.mak b/default-configs/s390x-softmmu.mak index 36e15de..5576b0a 100644 --- a/default-configs/s390x-softmmu.mak +++ b/default-configs/s390x-softmmu.mak @@ -4,4 +4,5 @@ CONFIG_VIRTIO=y CONFIG_SCLPCONSOLE=y CONFIG_S390_FLIC=y CONFIG_S390_FLIC_KVM=$(CONFIG_KVM) +CONFIG_VFIO_CCW=$(CONFIG_LINUX) CONFIG_WDT_DIAG288=y diff --git a/hw/vfio/Makefile.objs b/hw/vfio/Makefile.objs index ceddbb8..2daf4ae 100644 --- a/hw/vfio/Makefile.objs +++ b/hw/vfio/Makefile.objs @@ -1,6 +1,7 @@ ifeq ($(CONFIG_LINUX), y) obj-$(CONFIG_SOFTMMU) += common.o obj-$(CONFIG_PCI) += pci.o pci-quirks.o +obj-$(CONFIG_VFIO_CCW) += ccw.o obj-$(CONFIG_SOFTMMU) += platform.o obj-$(CONFIG_SOFTMMU) += calxeda-xgmac.o obj-$(CONFIG_SOFTMMU) += amd-xgbe.o diff --git a/hw/vfio/ccw.c b/hw/vfio/ccw.c new file mode 100644 index 0000000..501a0e8 --- /dev/null +++ b/hw/vfio/ccw.c @@ -0,0 +1,188 @@ +/* + * vfio based ccw device assignment support + * + * Copyright 2016 IBM Corp. + * Author(s): Dong Jia Shi + * Xiao Feng Ren + * Pierre Morel + * + * This work is licensed under the terms of the GNU GPL, version 2 or(at + * your option) any version. See the COPYING file in the top-level + * directory. + */ + +#include +#include + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "hw/sysbus.h" +#include "hw/vfio/vfio.h" +#include "hw/vfio/vfio-common.h" +#include "hw/s390x/s390-ccw.h" + +typedef struct VFIOCCWDevice { + S390CCWDevice cdev; + VFIODevice vdev; +} VFIOCCWDevice; + +static void vfio_ccw_compute_needs_reset(VFIODevice *vdev) +{ + vdev->needs_reset = true; +} + +static int vfio_ccw_hot_reset_multi(VFIODevice *vdev) +{ + VFIOCCWDevice *vcdev = container_of(vdev, VFIOCCWDevice, vdev); + + return ioctl(vcdev->vdev.fd, VFIO_DEVICE_CCW_HOT_RESET); +} + +/* + * vfio_eoi would be called by vfio_region_read and vfio_region_write + * which are not used by us for now. + * Let's leave this empty in case we need to realize it later. + */ +static void vfio_ccw_eoi(VFIODevice *vdev) +{ + /* Do nothing. */ +} + +static VFIODeviceOps vfio_ccw_ops = { + .vfio_compute_needs_reset = vfio_ccw_compute_needs_reset, + .vfio_hot_reset_multi = vfio_ccw_hot_reset_multi, + .vfio_eoi = vfio_ccw_eoi, +}; + +static void vfio_put_device(VFIOCCWDevice *vcdev) +{ + g_free(vcdev->vdev.name); + vfio_put_base_device(&vcdev->vdev); +} + +static void vfio_ccw_realize(DeviceState *dev, Error **errp) +{ + VFIODevice *vbasedev; + VFIOGroup *group; + S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent, dev); + VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev); + S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev); + char *path = NULL; + char *tmp_path = NULL; + char *iommu_group_path = NULL; + struct stat st; + int groupid, ret; + GError *gerror; + + /* Call the class init function for ccw device. */ + if (cdc->realize) { + cdc->realize(cdev, errp); + if (*errp) { + return; + } + } + + /* Check that host device exists. */ + path = g_strdup_printf("/sys/bus/ccw/devices/%x.%x.%04x", + cdev->hcssid, cdev->hssid, cdev->hdevno); + if (stat(path, &st) < 0) { + error_setg(errp, "vfio: no such host device %s", path); + goto out_err; + } + + vcdev->vdev.ops = &vfio_ccw_ops; + vcdev->vdev.type = VFIO_DEVICE_TYPE_CCW; + vcdev->vdev.name = g_strdup_printf("%02x.%02x.%04x", cdev->hcssid, + cdev->hssid, cdev->hdevno); + + tmp_path = g_strconcat(path, "/iommu_group", NULL); + + iommu_group_path = g_file_read_link(tmp_path, &gerror); + if (!iommu_group_path) { + error_setg(errp, "vfio: error no iommu_group for device"); + goto out_err; + } + + if (sscanf(basename(iommu_group_path), "%d", &groupid) != 1) { + error_setg(errp, "vfio: error reading %s:%m", iommu_group_path); + goto out_err; + } + + group = vfio_get_group(groupid, NULL); + if (!group) { + error_setg(errp, "vfio: failed to get the group %d", groupid); + goto out_err; + } + + QLIST_FOREACH(vbasedev, &group->device_list, next) { + if (strcmp(vbasedev->name, vcdev->vdev.name) == 0) { + error_setg(errp, "vfio: device %s has already been attached", + basename(path)); + vfio_put_group(group); + goto out_err; + } + } + + ret = vfio_get_device(group, basename(path) , &vcdev->vdev); + if (ret) { + error_setg(errp, "vfio: failed to get device %s", basename(path)); + vfio_put_group(group); + goto out_err; + } + +out_err: + g_free(path); + g_free(tmp_path); + g_free(iommu_group_path); +} + +static void vfio_ccw_unrealize(DeviceState *dev, Error **errp) +{ + S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent, dev); + VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev); + S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev); + VFIOGroup *group = vcdev->vdev.group; + + if (cdc->unrealize) { + cdc->unrealize(cdev, errp); + } + + vfio_put_device(vcdev); + vfio_put_group(group); +} + +static Property vfio_ccw_properties[] = { + DEFINE_PROP_STRING("hostid", VFIOCCWDevice, cdev.hostid), + DEFINE_PROP_STRING("guestid", VFIOCCWDevice, cdev.guestid), + DEFINE_PROP_END_OF_LIST(), +}; + +static const VMStateDescription vfio_ccw_vmstate = { + .name = "vfio-ccw", + .unmigratable = 1, +}; + +static void vfio_ccw_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + + dc->props = vfio_ccw_properties; + dc->vmsd = &vfio_ccw_vmstate; + dc->desc = "VFIO-based CCW device assignment"; + dc->realize = vfio_ccw_realize; + dc->unrealize = vfio_ccw_unrealize; +} + +static const TypeInfo vfio_ccw_info = { + .name = "vfio-ccw", + .parent = TYPE_S390_CCW, + .instance_size = sizeof(VFIOCCWDevice), + .class_init = vfio_ccw_class_init, +}; + +static void register_vfio_ccw_type(void) +{ + type_register_static(&vfio_ccw_info); +} + +type_init(register_vfio_ccw_type) diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index 85c2a74..b751506 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -41,6 +41,7 @@ enum { VFIO_DEVICE_TYPE_PCI = 0, VFIO_DEVICE_TYPE_PLATFORM = 1, + VFIO_DEVICE_TYPE_CCW = 2, }; typedef struct VFIOMmap {