From patchwork Mon Aug 5 13:17:11 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Antonios Motakis X-Patchwork-Id: 2838683 Return-Path: X-Original-To: patchwork-linux-arm@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 9E33DBF535 for ; Mon, 5 Aug 2013 13:19:29 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 412182013A for ; Mon, 5 Aug 2013 13:19:28 +0000 (UTC) Received: from casper.infradead.org (casper.infradead.org [85.118.1.10]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C5799201B2 for ; Mon, 5 Aug 2013 13:19:26 +0000 (UTC) Received: from merlin.infradead.org ([2001:4978:20e::2]) by casper.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1V6Kgh-0003Go-Uv; Mon, 05 Aug 2013 13:19:04 +0000 Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1V6KgX-0002km-SG; Mon, 05 Aug 2013 13:18:53 +0000 Received: from mail-wi0-f179.google.com ([209.85.212.179]) by merlin.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1V6KgB-0002fh-RY for linux-arm-kernel@lists.infradead.org; Mon, 05 Aug 2013 13:18:41 +0000 Received: by mail-wi0-f179.google.com with SMTP id hr7so1505160wib.12 for ; Mon, 05 Aug 2013 06:18:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references :x-gm-message-state; bh=2b0kdq8M7Hlkdl9/IK4vlwGv4NM9U8+CuYVWBjEA0uc=; b=OgfwPfRrM1xjYtHmdvbS1CmsQzY3t42amXIdvfNRXAjWn7KKKGlVjVXvtDIJDrk/Zr vqKzAok0+eyvOIi5zHwDdWXX8MNWCS+6jSr4/VJYiQ+YK6i/BlT+N77JneNPmIwC3jF7 ySpoZBgU+YSApLzKgV4ibd0k82B/OtFdLti4A72McD2Jd7Hybs0XOhQv1GQjt40utYWS NGfOACTegzeTGnXWfKQ4oKr3nUzwRZqjObfxYJoSRequstVReBHVmeRdHfMNVR0auzPn mMopHJd5wVoBWaRza0WTEWpLEg3rp7ou3MzetX8GBDrTCLKCTQH76HhrZPVZIZr0E5+q XzAw== X-Received: by 10.194.80.69 with SMTP id p5mr1153879wjx.33.1375708689927; Mon, 05 Aug 2013 06:18:09 -0700 (PDT) Received: from localhost.localdomain (home.tvelocity.eu. [82.67.68.96]) by mx.google.com with ESMTPSA id lp4sm18736449wic.2.2013.08.05.06.18.07 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 05 Aug 2013 06:18:09 -0700 (PDT) From: Antonios Motakis To: linux-arm-kernel@lists.infradead.org, alex.williamson@redhat.com Subject: [PATCH 2/3] Initial skeleton of VFIO support for Device Tree based devices Date: Mon, 5 Aug 2013 15:17:11 +0200 Message-Id: <1375708632-26356-3-git-send-email-a.motakis@virtualopensystems.com> X-Mailer: git-send-email 1.8.1.2 In-Reply-To: <1375708632-26356-1-git-send-email-a.motakis@virtualopensystems.com> References: <1375708632-26356-1-git-send-email-a.motakis@virtualopensystems.com> X-Gm-Message-State: ALoCoQnC0W38T4uKKJdwHNX+T82E8Fuo8GtnI4KnhyWKspRK9N0zP/ztvxbUV01Q2RTNKveaB5ni X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20130805_091832_448568_C51C2A9D X-CRM114-Status: GOOD ( 23.32 ) X-Spam-Score: -2.6 (--) Cc: linux-samsung-soc@vger.kernel.org, kvm@vger.kernel.org, agraf@suse.de, B08248@freescale.com, iommu@lists.linux-foundation.org, Antonios Motakis , kvmarm@lists.cs.columbia.edu X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, 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 Platform devices in the Linux kernel are usually managed by the DT interface. This patch forms the base to support these kind of devices with VFIO. Signed-off-by: Antonios Motakis --- drivers/vfio/Kconfig | 10 +++ drivers/vfio/Makefile | 1 + drivers/vfio/vfio_dt.c | 187 ++++++++++++++++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 1 + 4 files changed, 199 insertions(+) create mode 100644 drivers/vfio/vfio_dt.c diff --git a/drivers/vfio/Kconfig b/drivers/vfio/Kconfig index 1f84eda..a77a2e4 100644 --- a/drivers/vfio/Kconfig +++ b/drivers/vfio/Kconfig @@ -13,4 +13,14 @@ menuconfig VFIO If you don't know what to do here, say N. +config VFIO_DT + tristate "VFIO support for Device Tree devices" + depends on VFIO && EVENTFD + help + Support for the VFIO Device Tree driver. This is required to make + use of platform devices present on Device Tree nodes using the VFIO + framework. + + If you don't know what to do here, say N. + source "drivers/vfio/pci/Kconfig" diff --git a/drivers/vfio/Makefile b/drivers/vfio/Makefile index 2398d4a..d599a67 100644 --- a/drivers/vfio/Makefile +++ b/drivers/vfio/Makefile @@ -1,3 +1,4 @@ obj-$(CONFIG_VFIO) += vfio.o obj-$(CONFIG_VFIO_IOMMU_TYPE1) += vfio_iommu_type1.o obj-$(CONFIG_VFIO_PCI) += pci/ +obj-$(CONFIG_VFIO_DT) += vfio_dt.o diff --git a/drivers/vfio/vfio_dt.c b/drivers/vfio/vfio_dt.c new file mode 100644 index 0000000..ad4d31d --- /dev/null +++ b/drivers/vfio/vfio_dt.c @@ -0,0 +1,187 @@ +/* + * Copyright (C) 2013 - Virtual Open Systems + * Author: Antonios Motakis + * + * This program is free software; you can redistribute it and/or modify + * it under the terms 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, write to the Free Software + * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DRIVER_VERSION "0.1" +#define DRIVER_AUTHOR "Antonios Motakis " +#define DRIVER_DESC "VFIO Device Tree devices - User Level meta-driver" + +struct vfio_dt_device { + struct platform_device *pdev; +}; + +static void vfio_dt_release(void *device_data) +{ + module_put(THIS_MODULE); +} + +static int vfio_dt_open(void *device_data) +{ + if (!try_module_get(THIS_MODULE)) + return -ENODEV; + + return 0; +} + +static long vfio_dt_ioctl(void *device_data, + unsigned int cmd, unsigned long arg) +{ + struct vfio_dt_device *vdev = device_data; + unsigned long minsz; + + if (cmd == VFIO_DEVICE_GET_INFO) { + struct vfio_device_info info; + + minsz = offsetofend(struct vfio_device_info, num_irqs); + + if (copy_from_user(&info, (void __user *)arg, minsz)) + return -EFAULT; + + if (info.argsz < minsz) + return -EINVAL; + + info.flags = VFIO_DEVICE_FLAGS_DT; + info.num_regions = 0; + info.num_irqs = 0; + + return copy_to_user((void __user *)arg, &info, minsz); + + } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) + return -EINVAL; + + else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) + return -EINVAL; + + else if (cmd == VFIO_DEVICE_SET_IRQS) + return -EINVAL; + + else if (cmd == VFIO_DEVICE_RESET) + return -EINVAL; + + return -ENOTTY; +} + +static ssize_t vfio_dt_read(void *device_data, char __user *buf, + size_t count, loff_t *ppos) +{ + return 0; +} + +static ssize_t vfio_dt_write(void *device_data, const char __user *buf, + size_t count, loff_t *ppos) +{ + return 0; +} + +static int vfio_dt_mmap(void *device_data, struct vm_area_struct *vma) +{ + return -EINVAL; +} + +static const struct vfio_device_ops vfio_dt_ops = { + .name = "vfio-dts", + .open = vfio_dt_open, + .release = vfio_dt_release, + .ioctl = vfio_dt_ioctl, + .read = vfio_dt_read, + .write = vfio_dt_write, + .mmap = vfio_dt_mmap, +}; + +static int vfio_dt_probe(struct platform_device *pdev) +{ + struct vfio_dt_device *vdev; + struct iommu_group *group; + int ret; + + group = iommu_group_get(&pdev->dev); + if (!group) { + pr_err("VFIO: No IOMMU group for device %s\n", pdev->name); + return -EINVAL; + } + + vdev = kzalloc(sizeof(*vdev), GFP_KERNEL); + if (!vdev) { + iommu_group_put(group); + return -ENOMEM; + } + + vdev->pdev = pdev; + + ret = vfio_add_group_dev(&pdev->dev, &vfio_dt_ops, vdev); + if (ret) { + iommu_group_put(group); + kfree(vdev); + } + + return ret; +} + +static int vfio_dt_remove(struct platform_device *pdev) +{ + struct vfio_dt_device *vdev; + + vdev = vfio_del_group_dev(&pdev->dev); + if (!vdev) + return -EINVAL; + + iommu_group_put(pdev->dev.iommu_group); + kfree(vdev); + + return 0; +} + +static const struct of_device_id vfio_dt_match[] = { + /* In the future, we can implement a better mechanism to bind the + * module to any device. For now add the compatible property to the + * dtb of the devices we want to use. */ + { + .compatible = "vfio-dt", + }, + {}, +}; +MODULE_DEVICE_TABLE(of, vfio_dt_match); + +static struct platform_driver vfio_dt_driver = { + .probe = vfio_dt_probe, + .remove = vfio_dt_remove, + .driver = { + .name = "vfio-dt", + .owner = THIS_MODULE, + .of_match_table = vfio_dt_match, + }, +}; + +module_platform_driver(vfio_dt_driver); + +MODULE_VERSION(DRIVER_VERSION); +MODULE_LICENSE("GPL v2"); +MODULE_AUTHOR(DRIVER_AUTHOR); +MODULE_DESCRIPTION(DRIVER_DESC); diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 284ff24..1e4bef2 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -147,6 +147,7 @@ struct vfio_device_info { __u32 flags; #define VFIO_DEVICE_FLAGS_RESET (1 << 0) /* Device supports reset */ #define VFIO_DEVICE_FLAGS_PCI (1 << 1) /* vfio-pci device */ +#define VFIO_DEVICE_FLAGS_DT (1 << 2) /* vfio-dt device */ __u32 num_regions; /* Max region index + 1 */ __u32 num_irqs; /* Max IRQ index + 1 */ };