2019-05-27 14:55:21 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2015-03-17 04:08:42 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 - Virtual Open Systems
|
|
|
|
* Author: Antonios Motakis <a.motakis@virtualopensystems.com>
|
|
|
|
*/
|
|
|
|
|
2019-03-30 22:41:35 +08:00
|
|
|
#define dev_fmt(fmt) "VFIO: " fmt
|
|
|
|
|
2015-03-17 04:08:42 +08:00
|
|
|
#include <linux/device.h>
|
2016-07-19 21:01:44 +08:00
|
|
|
#include <linux/acpi.h>
|
2015-03-17 04:08:42 +08:00
|
|
|
#include <linux/iommu.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/mutex.h>
|
2018-05-30 01:15:28 +08:00
|
|
|
#include <linux/pm_runtime.h>
|
2015-03-17 04:08:42 +08:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/types.h>
|
2015-03-17 04:08:46 +08:00
|
|
|
#include <linux/uaccess.h>
|
2015-03-17 04:08:42 +08:00
|
|
|
#include <linux/vfio.h>
|
|
|
|
|
|
|
|
#include "vfio_platform_private.h"
|
|
|
|
|
2015-11-04 02:12:12 +08:00
|
|
|
#define DRIVER_VERSION "0.10"
|
|
|
|
#define DRIVER_AUTHOR "Antonios Motakis <a.motakis@virtualopensystems.com>"
|
|
|
|
#define DRIVER_DESC "VFIO platform base module"
|
|
|
|
|
2016-07-19 21:01:46 +08:00
|
|
|
#define VFIO_PLATFORM_IS_ACPI(vdev) ((vdev)->acpihid != NULL)
|
|
|
|
|
2015-11-04 02:12:13 +08:00
|
|
|
static LIST_HEAD(reset_list);
|
2015-03-17 04:08:46 +08:00
|
|
|
static DEFINE_MUTEX(driver_lock);
|
|
|
|
|
2015-11-04 02:12:17 +08:00
|
|
|
static vfio_platform_reset_fn_t vfio_platform_lookup_reset(const char *compat,
|
|
|
|
struct module **module)
|
2015-06-15 17:09:44 +08:00
|
|
|
{
|
2015-11-04 02:12:17 +08:00
|
|
|
struct vfio_platform_reset_node *iter;
|
|
|
|
vfio_platform_reset_fn_t reset_fn = NULL;
|
2015-06-15 17:09:44 +08:00
|
|
|
|
2015-11-04 02:12:17 +08:00
|
|
|
mutex_lock(&driver_lock);
|
|
|
|
list_for_each_entry(iter, &reset_list, link) {
|
|
|
|
if (!strcmp(iter->compat, compat) &&
|
|
|
|
try_module_get(iter->owner)) {
|
|
|
|
*module = iter->owner;
|
2016-07-19 21:01:41 +08:00
|
|
|
reset_fn = iter->of_reset;
|
2015-11-04 02:12:17 +08:00
|
|
|
break;
|
2015-06-15 17:09:44 +08:00
|
|
|
}
|
|
|
|
}
|
2015-11-04 02:12:17 +08:00
|
|
|
mutex_unlock(&driver_lock);
|
|
|
|
return reset_fn;
|
|
|
|
}
|
|
|
|
|
2016-07-19 21:01:44 +08:00
|
|
|
static int vfio_platform_acpi_probe(struct vfio_platform_device *vdev,
|
|
|
|
struct device *dev)
|
|
|
|
{
|
|
|
|
struct acpi_device *adev;
|
|
|
|
|
|
|
|
if (acpi_disabled)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
adev = ACPI_COMPANION(dev);
|
|
|
|
if (!adev) {
|
2019-03-30 22:41:35 +08:00
|
|
|
dev_err(dev, "ACPI companion device not found for %s\n",
|
2016-07-19 21:01:44 +08:00
|
|
|
vdev->name);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_ACPI
|
|
|
|
vdev->acpihid = acpi_device_hid(adev);
|
|
|
|
#endif
|
|
|
|
return WARN_ON(!vdev->acpihid) ? -EINVAL : 0;
|
|
|
|
}
|
|
|
|
|
2016-09-01 19:15:35 +08:00
|
|
|
static int vfio_platform_acpi_call_reset(struct vfio_platform_device *vdev,
|
2016-07-19 21:01:46 +08:00
|
|
|
const char **extra_dbg)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_ACPI
|
|
|
|
struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
|
|
|
|
struct device *dev = vdev->device;
|
|
|
|
acpi_handle handle = ACPI_HANDLE(dev);
|
|
|
|
acpi_status acpi_ret;
|
|
|
|
|
|
|
|
acpi_ret = acpi_evaluate_object(handle, "_RST", NULL, &buffer);
|
|
|
|
if (ACPI_FAILURE(acpi_ret)) {
|
|
|
|
if (extra_dbg)
|
|
|
|
*extra_dbg = acpi_format_exception(acpi_ret);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
return -ENOENT;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-09-01 19:15:35 +08:00
|
|
|
static bool vfio_platform_acpi_has_reset(struct vfio_platform_device *vdev)
|
2016-07-19 21:01:46 +08:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_ACPI
|
|
|
|
struct device *dev = vdev->device;
|
|
|
|
acpi_handle handle = ACPI_HANDLE(dev);
|
|
|
|
|
|
|
|
return acpi_has_method(handle, "_RST");
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-07-19 21:01:43 +08:00
|
|
|
static bool vfio_platform_has_reset(struct vfio_platform_device *vdev)
|
|
|
|
{
|
2016-07-19 21:01:46 +08:00
|
|
|
if (VFIO_PLATFORM_IS_ACPI(vdev))
|
|
|
|
return vfio_platform_acpi_has_reset(vdev);
|
|
|
|
|
2016-07-19 21:01:43 +08:00
|
|
|
return vdev->of_reset ? true : false;
|
|
|
|
}
|
|
|
|
|
2016-07-19 21:01:47 +08:00
|
|
|
static int vfio_platform_get_reset(struct vfio_platform_device *vdev)
|
2015-11-04 02:12:17 +08:00
|
|
|
{
|
2016-07-19 21:01:46 +08:00
|
|
|
if (VFIO_PLATFORM_IS_ACPI(vdev))
|
2016-07-19 21:01:47 +08:00
|
|
|
return vfio_platform_acpi_has_reset(vdev) ? 0 : -ENOENT;
|
2016-07-19 21:01:46 +08:00
|
|
|
|
2016-07-19 21:01:41 +08:00
|
|
|
vdev->of_reset = vfio_platform_lookup_reset(vdev->compat,
|
|
|
|
&vdev->reset_module);
|
|
|
|
if (!vdev->of_reset) {
|
2015-11-20 08:17:24 +08:00
|
|
|
request_module("vfio-reset:%s", vdev->compat);
|
2016-07-19 21:01:41 +08:00
|
|
|
vdev->of_reset = vfio_platform_lookup_reset(vdev->compat,
|
|
|
|
&vdev->reset_module);
|
2015-11-04 02:12:17 +08:00
|
|
|
}
|
2016-07-19 21:01:47 +08:00
|
|
|
|
|
|
|
return vdev->of_reset ? 0 : -ENOENT;
|
2015-06-15 17:09:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void vfio_platform_put_reset(struct vfio_platform_device *vdev)
|
|
|
|
{
|
2016-07-19 21:01:46 +08:00
|
|
|
if (VFIO_PLATFORM_IS_ACPI(vdev))
|
|
|
|
return;
|
|
|
|
|
2016-07-19 21:01:41 +08:00
|
|
|
if (vdev->of_reset)
|
2015-11-04 02:12:17 +08:00
|
|
|
module_put(vdev->reset_module);
|
2015-06-15 17:09:44 +08:00
|
|
|
}
|
|
|
|
|
2015-03-17 04:08:46 +08:00
|
|
|
static int vfio_platform_regions_init(struct vfio_platform_device *vdev)
|
|
|
|
{
|
|
|
|
int cnt = 0, i;
|
|
|
|
|
|
|
|
while (vdev->get_resource(vdev, cnt))
|
|
|
|
cnt++;
|
|
|
|
|
|
|
|
vdev->regions = kcalloc(cnt, sizeof(struct vfio_platform_region),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!vdev->regions)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
for (i = 0; i < cnt; i++) {
|
|
|
|
struct resource *res =
|
|
|
|
vdev->get_resource(vdev, i);
|
|
|
|
|
|
|
|
if (!res)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
vdev->regions[i].addr = res->start;
|
|
|
|
vdev->regions[i].size = resource_size(res);
|
|
|
|
vdev->regions[i].flags = 0;
|
|
|
|
|
|
|
|
switch (resource_type(res)) {
|
|
|
|
case IORESOURCE_MEM:
|
|
|
|
vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_MMIO;
|
2015-03-17 04:08:47 +08:00
|
|
|
vdev->regions[i].flags |= VFIO_REGION_INFO_FLAG_READ;
|
|
|
|
if (!(res->flags & IORESOURCE_READONLY))
|
|
|
|
vdev->regions[i].flags |=
|
|
|
|
VFIO_REGION_INFO_FLAG_WRITE;
|
2015-03-17 04:08:48 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Only regions addressed with PAGE granularity may be
|
|
|
|
* MMAPed securely.
|
|
|
|
*/
|
|
|
|
if (!(vdev->regions[i].addr & ~PAGE_MASK) &&
|
|
|
|
!(vdev->regions[i].size & ~PAGE_MASK))
|
|
|
|
vdev->regions[i].flags |=
|
|
|
|
VFIO_REGION_INFO_FLAG_MMAP;
|
|
|
|
|
2015-03-17 04:08:46 +08:00
|
|
|
break;
|
|
|
|
case IORESOURCE_IO:
|
|
|
|
vdev->regions[i].type = VFIO_PLATFORM_REGION_TYPE_PIO;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vdev->num_regions = cnt;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
err:
|
|
|
|
kfree(vdev->regions);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vfio_platform_regions_cleanup(struct vfio_platform_device *vdev)
|
|
|
|
{
|
2015-03-17 04:08:47 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < vdev->num_regions; i++)
|
|
|
|
iounmap(vdev->regions[i].ioaddr);
|
|
|
|
|
2015-03-17 04:08:46 +08:00
|
|
|
vdev->num_regions = 0;
|
|
|
|
kfree(vdev->regions);
|
|
|
|
}
|
|
|
|
|
2016-07-19 21:01:45 +08:00
|
|
|
static int vfio_platform_call_reset(struct vfio_platform_device *vdev,
|
|
|
|
const char **extra_dbg)
|
2016-07-19 21:01:42 +08:00
|
|
|
{
|
2016-07-19 21:01:46 +08:00
|
|
|
if (VFIO_PLATFORM_IS_ACPI(vdev)) {
|
|
|
|
dev_info(vdev->device, "reset\n");
|
|
|
|
return vfio_platform_acpi_call_reset(vdev, extra_dbg);
|
|
|
|
} else if (vdev->of_reset) {
|
2016-07-19 21:01:42 +08:00
|
|
|
dev_info(vdev->device, "reset\n");
|
|
|
|
return vdev->of_reset(vdev);
|
|
|
|
}
|
|
|
|
|
|
|
|
dev_warn(vdev->device, "no reset function found!\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-03-17 04:08:42 +08:00
|
|
|
static void vfio_platform_release(void *device_data)
|
|
|
|
{
|
2015-03-17 04:08:46 +08:00
|
|
|
struct vfio_platform_device *vdev = device_data;
|
|
|
|
|
|
|
|
mutex_lock(&driver_lock);
|
|
|
|
|
|
|
|
if (!(--vdev->refcnt)) {
|
2016-07-19 21:01:49 +08:00
|
|
|
const char *extra_dbg = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = vfio_platform_call_reset(vdev, &extra_dbg);
|
|
|
|
if (ret && vdev->reset_required) {
|
|
|
|
dev_warn(vdev->device, "reset driver is required and reset call failed in release (%d) %s\n",
|
|
|
|
ret, extra_dbg ? extra_dbg : "");
|
|
|
|
WARN_ON(1);
|
|
|
|
}
|
2018-05-30 01:15:28 +08:00
|
|
|
pm_runtime_put(vdev->device);
|
2015-03-17 04:08:46 +08:00
|
|
|
vfio_platform_regions_cleanup(vdev);
|
2015-03-17 04:08:48 +08:00
|
|
|
vfio_platform_irq_cleanup(vdev);
|
2015-03-17 04:08:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&driver_lock);
|
|
|
|
|
2015-11-04 02:12:12 +08:00
|
|
|
module_put(vdev->parent_module);
|
2015-03-17 04:08:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int vfio_platform_open(void *device_data)
|
|
|
|
{
|
2015-03-17 04:08:46 +08:00
|
|
|
struct vfio_platform_device *vdev = device_data;
|
|
|
|
int ret;
|
|
|
|
|
2015-11-04 02:12:12 +08:00
|
|
|
if (!try_module_get(vdev->parent_module))
|
2015-03-17 04:08:42 +08:00
|
|
|
return -ENODEV;
|
|
|
|
|
2015-03-17 04:08:46 +08:00
|
|
|
mutex_lock(&driver_lock);
|
|
|
|
|
|
|
|
if (!vdev->refcnt) {
|
2016-07-19 21:01:48 +08:00
|
|
|
const char *extra_dbg = NULL;
|
|
|
|
|
2015-03-17 04:08:46 +08:00
|
|
|
ret = vfio_platform_regions_init(vdev);
|
|
|
|
if (ret)
|
|
|
|
goto err_reg;
|
2015-03-17 04:08:48 +08:00
|
|
|
|
|
|
|
ret = vfio_platform_irq_init(vdev);
|
|
|
|
if (ret)
|
|
|
|
goto err_irq;
|
2015-06-15 17:09:43 +08:00
|
|
|
|
2018-05-30 01:15:28 +08:00
|
|
|
ret = pm_runtime_get_sync(vdev->device);
|
|
|
|
if (ret < 0)
|
|
|
|
goto err_pm;
|
|
|
|
|
2016-07-19 21:01:48 +08:00
|
|
|
ret = vfio_platform_call_reset(vdev, &extra_dbg);
|
|
|
|
if (ret && vdev->reset_required) {
|
|
|
|
dev_warn(vdev->device, "reset driver is required and reset call failed in open (%d) %s\n",
|
|
|
|
ret, extra_dbg ? extra_dbg : "");
|
|
|
|
goto err_rst;
|
|
|
|
}
|
2015-03-17 04:08:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
vdev->refcnt++;
|
|
|
|
|
|
|
|
mutex_unlock(&driver_lock);
|
2015-03-17 04:08:42 +08:00
|
|
|
return 0;
|
2015-03-17 04:08:46 +08:00
|
|
|
|
2016-07-19 21:01:48 +08:00
|
|
|
err_rst:
|
2018-05-30 01:15:28 +08:00
|
|
|
pm_runtime_put(vdev->device);
|
|
|
|
err_pm:
|
2016-07-19 21:01:48 +08:00
|
|
|
vfio_platform_irq_cleanup(vdev);
|
2015-03-17 04:08:48 +08:00
|
|
|
err_irq:
|
|
|
|
vfio_platform_regions_cleanup(vdev);
|
2015-03-17 04:08:46 +08:00
|
|
|
err_reg:
|
|
|
|
mutex_unlock(&driver_lock);
|
|
|
|
module_put(THIS_MODULE);
|
|
|
|
return ret;
|
2015-03-17 04:08:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static long vfio_platform_ioctl(void *device_data,
|
|
|
|
unsigned int cmd, unsigned long arg)
|
|
|
|
{
|
2015-03-17 04:08:46 +08:00
|
|
|
struct vfio_platform_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;
|
|
|
|
|
2016-07-19 21:01:43 +08:00
|
|
|
if (vfio_platform_has_reset(vdev))
|
2015-06-15 17:09:43 +08:00
|
|
|
vdev->flags |= VFIO_DEVICE_FLAGS_RESET;
|
2015-03-17 04:08:46 +08:00
|
|
|
info.flags = vdev->flags;
|
2015-03-17 04:08:46 +08:00
|
|
|
info.num_regions = vdev->num_regions;
|
2015-03-17 04:08:48 +08:00
|
|
|
info.num_irqs = vdev->num_irqs;
|
2015-03-17 04:08:46 +08:00
|
|
|
|
2016-02-28 22:31:39 +08:00
|
|
|
return copy_to_user((void __user *)arg, &info, minsz) ?
|
|
|
|
-EFAULT : 0;
|
2015-03-17 04:08:42 +08:00
|
|
|
|
2015-03-17 04:08:46 +08:00
|
|
|
} else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
|
|
|
|
struct vfio_region_info info;
|
|
|
|
|
|
|
|
minsz = offsetofend(struct vfio_region_info, offset);
|
|
|
|
|
|
|
|
if (copy_from_user(&info, (void __user *)arg, minsz))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (info.argsz < minsz)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (info.index >= vdev->num_regions)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* map offset to the physical address */
|
|
|
|
info.offset = VFIO_PLATFORM_INDEX_TO_OFFSET(info.index);
|
|
|
|
info.size = vdev->regions[info.index].size;
|
|
|
|
info.flags = vdev->regions[info.index].flags;
|
|
|
|
|
2016-02-28 22:31:39 +08:00
|
|
|
return copy_to_user((void __user *)arg, &info, minsz) ?
|
|
|
|
-EFAULT : 0;
|
2015-03-17 04:08:42 +08:00
|
|
|
|
2015-03-17 04:08:48 +08:00
|
|
|
} else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
|
|
|
|
struct vfio_irq_info info;
|
|
|
|
|
|
|
|
minsz = offsetofend(struct vfio_irq_info, count);
|
|
|
|
|
|
|
|
if (copy_from_user(&info, (void __user *)arg, minsz))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (info.argsz < minsz)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (info.index >= vdev->num_irqs)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
info.flags = vdev->irqs[info.index].flags;
|
|
|
|
info.count = vdev->irqs[info.index].count;
|
|
|
|
|
2016-02-28 22:31:39 +08:00
|
|
|
return copy_to_user((void __user *)arg, &info, minsz) ?
|
|
|
|
-EFAULT : 0;
|
2015-03-17 04:08:42 +08:00
|
|
|
|
2015-03-17 04:08:49 +08:00
|
|
|
} else if (cmd == VFIO_DEVICE_SET_IRQS) {
|
|
|
|
struct vfio_irq_set hdr;
|
|
|
|
u8 *data = NULL;
|
|
|
|
int ret = 0;
|
2016-11-17 04:46:29 +08:00
|
|
|
size_t data_size = 0;
|
2015-03-17 04:08:49 +08:00
|
|
|
|
|
|
|
minsz = offsetofend(struct vfio_irq_set, count);
|
|
|
|
|
|
|
|
if (copy_from_user(&hdr, (void __user *)arg, minsz))
|
|
|
|
return -EFAULT;
|
|
|
|
|
2016-11-17 04:46:29 +08:00
|
|
|
ret = vfio_set_irqs_validate_and_prepare(&hdr, vdev->num_irqs,
|
|
|
|
vdev->num_irqs, &data_size);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2015-03-17 04:08:49 +08:00
|
|
|
|
2016-11-17 04:46:29 +08:00
|
|
|
if (data_size) {
|
|
|
|
data = memdup_user((void __user *)(arg + minsz),
|
|
|
|
data_size);
|
2015-03-17 04:08:49 +08:00
|
|
|
if (IS_ERR(data))
|
|
|
|
return PTR_ERR(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_lock(&vdev->igate);
|
|
|
|
|
|
|
|
ret = vfio_platform_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
|
|
|
|
hdr.start, hdr.count, data);
|
|
|
|
mutex_unlock(&vdev->igate);
|
|
|
|
kfree(data);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
2015-06-15 17:09:43 +08:00
|
|
|
} else if (cmd == VFIO_DEVICE_RESET) {
|
2016-07-19 21:01:45 +08:00
|
|
|
return vfio_platform_call_reset(vdev, NULL);
|
2015-06-15 17:09:43 +08:00
|
|
|
}
|
2015-03-17 04:08:42 +08:00
|
|
|
|
|
|
|
return -ENOTTY;
|
|
|
|
}
|
|
|
|
|
2015-10-30 00:50:43 +08:00
|
|
|
static ssize_t vfio_platform_read_mmio(struct vfio_platform_region *reg,
|
2015-03-17 04:08:47 +08:00
|
|
|
char __user *buf, size_t count,
|
|
|
|
loff_t off)
|
|
|
|
{
|
|
|
|
unsigned int done = 0;
|
|
|
|
|
2015-10-30 00:50:43 +08:00
|
|
|
if (!reg->ioaddr) {
|
|
|
|
reg->ioaddr =
|
|
|
|
ioremap_nocache(reg->addr, reg->size);
|
2015-03-17 04:08:47 +08:00
|
|
|
|
2015-10-30 00:50:43 +08:00
|
|
|
if (!reg->ioaddr)
|
2015-03-17 04:08:47 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (count) {
|
|
|
|
size_t filled;
|
|
|
|
|
|
|
|
if (count >= 4 && !(off % 4)) {
|
|
|
|
u32 val;
|
|
|
|
|
2015-10-30 00:50:43 +08:00
|
|
|
val = ioread32(reg->ioaddr + off);
|
2015-03-17 04:08:47 +08:00
|
|
|
if (copy_to_user(buf, &val, 4))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
filled = 4;
|
|
|
|
} else if (count >= 2 && !(off % 2)) {
|
|
|
|
u16 val;
|
|
|
|
|
2015-10-30 00:50:43 +08:00
|
|
|
val = ioread16(reg->ioaddr + off);
|
2015-03-17 04:08:47 +08:00
|
|
|
if (copy_to_user(buf, &val, 2))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
filled = 2;
|
|
|
|
} else {
|
|
|
|
u8 val;
|
|
|
|
|
2015-10-30 00:50:43 +08:00
|
|
|
val = ioread8(reg->ioaddr + off);
|
2015-03-17 04:08:47 +08:00
|
|
|
if (copy_to_user(buf, &val, 1))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
filled = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
count -= filled;
|
|
|
|
done += filled;
|
|
|
|
off += filled;
|
|
|
|
buf += filled;
|
|
|
|
}
|
|
|
|
|
|
|
|
return done;
|
|
|
|
err:
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
2015-03-17 04:08:42 +08:00
|
|
|
static ssize_t vfio_platform_read(void *device_data, char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
2015-03-17 04:08:47 +08:00
|
|
|
struct vfio_platform_device *vdev = device_data;
|
|
|
|
unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
|
|
|
|
loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
|
|
|
|
|
|
|
|
if (index >= vdev->num_regions)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
|
2015-10-30 00:50:43 +08:00
|
|
|
return vfio_platform_read_mmio(&vdev->regions[index],
|
2015-03-17 04:08:47 +08:00
|
|
|
buf, count, off);
|
|
|
|
else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
|
|
|
|
return -EINVAL; /* not implemented */
|
|
|
|
|
2015-03-17 04:08:42 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-10-30 00:50:43 +08:00
|
|
|
static ssize_t vfio_platform_write_mmio(struct vfio_platform_region *reg,
|
2015-03-17 04:08:47 +08:00
|
|
|
const char __user *buf, size_t count,
|
|
|
|
loff_t off)
|
|
|
|
{
|
|
|
|
unsigned int done = 0;
|
|
|
|
|
2015-10-30 00:50:43 +08:00
|
|
|
if (!reg->ioaddr) {
|
|
|
|
reg->ioaddr =
|
|
|
|
ioremap_nocache(reg->addr, reg->size);
|
2015-03-17 04:08:47 +08:00
|
|
|
|
2015-10-30 00:50:43 +08:00
|
|
|
if (!reg->ioaddr)
|
2015-03-17 04:08:47 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (count) {
|
|
|
|
size_t filled;
|
|
|
|
|
|
|
|
if (count >= 4 && !(off % 4)) {
|
|
|
|
u32 val;
|
|
|
|
|
|
|
|
if (copy_from_user(&val, buf, 4))
|
|
|
|
goto err;
|
2015-10-30 00:50:43 +08:00
|
|
|
iowrite32(val, reg->ioaddr + off);
|
2015-03-17 04:08:47 +08:00
|
|
|
|
|
|
|
filled = 4;
|
|
|
|
} else if (count >= 2 && !(off % 2)) {
|
|
|
|
u16 val;
|
|
|
|
|
|
|
|
if (copy_from_user(&val, buf, 2))
|
|
|
|
goto err;
|
2015-10-30 00:50:43 +08:00
|
|
|
iowrite16(val, reg->ioaddr + off);
|
2015-03-17 04:08:47 +08:00
|
|
|
|
|
|
|
filled = 2;
|
|
|
|
} else {
|
|
|
|
u8 val;
|
|
|
|
|
|
|
|
if (copy_from_user(&val, buf, 1))
|
|
|
|
goto err;
|
2015-10-30 00:50:43 +08:00
|
|
|
iowrite8(val, reg->ioaddr + off);
|
2015-03-17 04:08:47 +08:00
|
|
|
|
|
|
|
filled = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
count -= filled;
|
|
|
|
done += filled;
|
|
|
|
off += filled;
|
|
|
|
buf += filled;
|
|
|
|
}
|
|
|
|
|
|
|
|
return done;
|
|
|
|
err:
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
2015-03-17 04:08:42 +08:00
|
|
|
static ssize_t vfio_platform_write(void *device_data, const char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
2015-03-17 04:08:47 +08:00
|
|
|
struct vfio_platform_device *vdev = device_data;
|
|
|
|
unsigned int index = VFIO_PLATFORM_OFFSET_TO_INDEX(*ppos);
|
|
|
|
loff_t off = *ppos & VFIO_PLATFORM_OFFSET_MASK;
|
|
|
|
|
|
|
|
if (index >= vdev->num_regions)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
|
2015-10-30 00:50:43 +08:00
|
|
|
return vfio_platform_write_mmio(&vdev->regions[index],
|
2015-03-17 04:08:47 +08:00
|
|
|
buf, count, off);
|
|
|
|
else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
|
|
|
|
return -EINVAL; /* not implemented */
|
|
|
|
|
2015-03-17 04:08:42 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-03-17 04:08:48 +08:00
|
|
|
static int vfio_platform_mmap_mmio(struct vfio_platform_region region,
|
|
|
|
struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
u64 req_len, pgoff, req_start;
|
|
|
|
|
|
|
|
req_len = vma->vm_end - vma->vm_start;
|
|
|
|
pgoff = vma->vm_pgoff &
|
|
|
|
((1U << (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
|
|
|
|
req_start = pgoff << PAGE_SHIFT;
|
|
|
|
|
|
|
|
if (region.size < PAGE_SIZE || req_start + req_len > region.size)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
|
|
|
|
vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
|
|
|
|
|
|
|
|
return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
|
|
|
|
req_len, vma->vm_page_prot);
|
|
|
|
}
|
|
|
|
|
2015-03-17 04:08:42 +08:00
|
|
|
static int vfio_platform_mmap(void *device_data, struct vm_area_struct *vma)
|
|
|
|
{
|
2015-03-17 04:08:48 +08:00
|
|
|
struct vfio_platform_device *vdev = device_data;
|
|
|
|
unsigned int index;
|
|
|
|
|
|
|
|
index = vma->vm_pgoff >> (VFIO_PLATFORM_OFFSET_SHIFT - PAGE_SHIFT);
|
|
|
|
|
|
|
|
if (vma->vm_end < vma->vm_start)
|
|
|
|
return -EINVAL;
|
|
|
|
if (!(vma->vm_flags & VM_SHARED))
|
|
|
|
return -EINVAL;
|
|
|
|
if (index >= vdev->num_regions)
|
|
|
|
return -EINVAL;
|
|
|
|
if (vma->vm_start & ~PAGE_MASK)
|
|
|
|
return -EINVAL;
|
|
|
|
if (vma->vm_end & ~PAGE_MASK)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_MMAP))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_READ)
|
|
|
|
&& (vma->vm_flags & VM_READ))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
|
|
|
|
&& (vma->vm_flags & VM_WRITE))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
vma->vm_private_data = vdev;
|
|
|
|
|
|
|
|
if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_MMIO)
|
|
|
|
return vfio_platform_mmap_mmio(vdev->regions[index], vma);
|
|
|
|
|
|
|
|
else if (vdev->regions[index].type & VFIO_PLATFORM_REGION_TYPE_PIO)
|
|
|
|
return -EINVAL; /* not implemented */
|
|
|
|
|
2015-03-17 04:08:42 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct vfio_device_ops vfio_platform_ops = {
|
|
|
|
.name = "vfio-platform",
|
|
|
|
.open = vfio_platform_open,
|
|
|
|
.release = vfio_platform_release,
|
|
|
|
.ioctl = vfio_platform_ioctl,
|
|
|
|
.read = vfio_platform_read,
|
|
|
|
.write = vfio_platform_write,
|
|
|
|
.mmap = vfio_platform_mmap,
|
|
|
|
};
|
|
|
|
|
2016-09-01 19:15:35 +08:00
|
|
|
static int vfio_platform_of_probe(struct vfio_platform_device *vdev,
|
2016-07-19 21:01:44 +08:00
|
|
|
struct device *dev)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = device_property_read_string(dev, "compatible",
|
|
|
|
&vdev->compat);
|
|
|
|
if (ret)
|
2019-03-30 22:41:35 +08:00
|
|
|
dev_err(dev, "Cannot retrieve compat for %s\n", vdev->name);
|
2016-07-19 21:01:44 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There can be two kernel build combinations. One build where
|
|
|
|
* ACPI is not selected in Kconfig and another one with the ACPI Kconfig.
|
|
|
|
*
|
|
|
|
* In the first case, vfio_platform_acpi_probe will return since
|
|
|
|
* acpi_disabled is 1. DT user will not see any kind of messages from
|
|
|
|
* ACPI.
|
|
|
|
*
|
|
|
|
* In the second case, both DT and ACPI is compiled in but the system is
|
|
|
|
* booting with any of these combinations.
|
|
|
|
*
|
|
|
|
* If the firmware is DT type, then acpi_disabled is 1. The ACPI probe routine
|
|
|
|
* terminates immediately without any messages.
|
|
|
|
*
|
|
|
|
* If the firmware is ACPI type, then acpi_disabled is 0. All other checks are
|
|
|
|
* valid checks. We cannot claim that this system is DT.
|
|
|
|
*/
|
2015-03-17 04:08:42 +08:00
|
|
|
int vfio_platform_probe_common(struct vfio_platform_device *vdev,
|
|
|
|
struct device *dev)
|
|
|
|
{
|
|
|
|
struct iommu_group *group;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!vdev)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2016-07-19 21:01:44 +08:00
|
|
|
ret = vfio_platform_acpi_probe(vdev, dev);
|
|
|
|
if (ret)
|
|
|
|
ret = vfio_platform_of_probe(vdev, dev);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2015-11-04 02:12:16 +08:00
|
|
|
|
2015-11-04 02:12:18 +08:00
|
|
|
vdev->device = dev;
|
|
|
|
|
2016-07-19 21:01:47 +08:00
|
|
|
ret = vfio_platform_get_reset(vdev);
|
|
|
|
if (ret && vdev->reset_required) {
|
2019-03-30 22:41:35 +08:00
|
|
|
dev_err(dev, "No reset function found for device %s\n",
|
|
|
|
vdev->name);
|
2016-07-19 21:01:47 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-05-23 17:47:30 +08:00
|
|
|
group = vfio_iommu_group_get(dev);
|
2015-03-17 04:08:42 +08:00
|
|
|
if (!group) {
|
2019-03-30 22:41:35 +08:00
|
|
|
dev_err(dev, "No IOMMU group for device %s\n", vdev->name);
|
2018-04-11 17:15:48 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto put_reset;
|
2015-03-17 04:08:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = vfio_add_group_dev(dev, &vfio_platform_ops, vdev);
|
2018-04-11 17:15:48 +08:00
|
|
|
if (ret)
|
|
|
|
goto put_iommu;
|
2015-03-17 04:08:42 +08:00
|
|
|
|
2015-03-17 04:08:49 +08:00
|
|
|
mutex_init(&vdev->igate);
|
|
|
|
|
2018-05-30 01:15:28 +08:00
|
|
|
pm_runtime_enable(vdev->device);
|
2015-03-17 04:08:42 +08:00
|
|
|
return 0;
|
2018-04-11 17:15:48 +08:00
|
|
|
|
|
|
|
put_iommu:
|
|
|
|
vfio_iommu_group_put(group, dev);
|
|
|
|
put_reset:
|
|
|
|
vfio_platform_put_reset(vdev);
|
|
|
|
return ret;
|
2015-03-17 04:08:42 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vfio_platform_probe_common);
|
|
|
|
|
|
|
|
struct vfio_platform_device *vfio_platform_remove_common(struct device *dev)
|
|
|
|
{
|
|
|
|
struct vfio_platform_device *vdev;
|
|
|
|
|
|
|
|
vdev = vfio_del_group_dev(dev);
|
2015-06-15 17:09:44 +08:00
|
|
|
|
|
|
|
if (vdev) {
|
2018-05-30 01:15:28 +08:00
|
|
|
pm_runtime_disable(vdev->device);
|
2015-06-15 17:09:44 +08:00
|
|
|
vfio_platform_put_reset(vdev);
|
2016-05-23 17:47:30 +08:00
|
|
|
vfio_iommu_group_put(dev->iommu_group, dev);
|
2015-06-15 17:09:44 +08:00
|
|
|
}
|
2015-03-17 04:08:42 +08:00
|
|
|
|
|
|
|
return vdev;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vfio_platform_remove_common);
|
2015-11-04 02:12:12 +08:00
|
|
|
|
2015-11-04 02:12:13 +08:00
|
|
|
void __vfio_platform_register_reset(struct vfio_platform_reset_node *node)
|
|
|
|
{
|
|
|
|
mutex_lock(&driver_lock);
|
|
|
|
list_add(&node->link, &reset_list);
|
|
|
|
mutex_unlock(&driver_lock);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(__vfio_platform_register_reset);
|
|
|
|
|
|
|
|
void vfio_platform_unregister_reset(const char *compat,
|
|
|
|
vfio_platform_reset_fn_t fn)
|
|
|
|
{
|
|
|
|
struct vfio_platform_reset_node *iter, *temp;
|
|
|
|
|
|
|
|
mutex_lock(&driver_lock);
|
|
|
|
list_for_each_entry_safe(iter, temp, &reset_list, link) {
|
2016-07-19 21:01:41 +08:00
|
|
|
if (!strcmp(iter->compat, compat) && (iter->of_reset == fn)) {
|
2015-11-04 02:12:13 +08:00
|
|
|
list_del(&iter->link);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&driver_lock);
|
|
|
|
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vfio_platform_unregister_reset);
|
|
|
|
|
2015-11-04 02:12:12 +08:00
|
|
|
MODULE_VERSION(DRIVER_VERSION);
|
|
|
|
MODULE_LICENSE("GPL v2");
|
|
|
|
MODULE_AUTHOR(DRIVER_AUTHOR);
|
|
|
|
MODULE_DESCRIPTION(DRIVER_DESC);
|