Skip to contentSkip to conversation

vfio: platform: add device properties skeleton and user API

RFC PATCH New Baptiste Reynal 1 participant 3 messages Sep 30, 2015

Subsystems (5)
KVM KVM arm64 ABI/API VFIO driver +1 more subsystems
lore
Share & tools
lore
#0 lore raw

RFC PATCH v5 1/3 vfio: platform: add device properties skeleton and user API

From: Antonios Motakis <[email protected]>

This patch introduces an API that allows to return device properties (OF or ACPI) of a device bound to the vfio-platform/vfio-amba driver and the skeleton of the implementation for VFIO_PLATFORM. Information about any device node bound by VFIO_PLATFORM should be queried via the introduced ioctl VFIO_DEVICE_GET_DEV_PROPERTY.

The user needs to know the name and the data type of the property he is accessing.

---
v4 -> v5:
- add a limit size to namesz (128)
- remove flags propagation
- replace E2BIG with ENOSPC when the buffer size is too small
- remove useless affectation

v3 -> v4:
- added flags placeholder in vfio_dev_properties
- ioctl returns -E2BIG if the buffer is too small
- details VFIO_DEVICE_GET_DEV_PROPERTY documentation

---
 drivers/vfio/platform/Makefile                |  3 +-
 drivers/vfio/platform/properties.c            | 77 +++++++++++++++++++++++++++
 drivers/vfio/platform/vfio_platform_common.c  | 34 ++++++++++++
 drivers/vfio/platform/vfio_platform_private.h |  9 ++++
 include/uapi/linux/vfio.h                     | 31 +++++++++++
 5 files changed, 153 insertions(+), 1 deletion(-)
 create mode 100644 drivers/vfio/platform/properties.c
drivers/vfio/platform/Makefile +2 -1
index 9ce8afe..37cf5ed 100644
@@ -1,5 +1,6 @@
 
-vfio-platform-y := vfio_platform.o vfio_platform_common.o vfio_platform_irq.o
+vfio-platform-y := vfio_platform.o vfio_platform_common.o vfio_platform_irq.o \
+		   properties.o
 
 obj-$(CONFIG_VFIO_PLATFORM) += vfio-platform.o
 obj-$(CONFIG_VFIO_PLATFORM) += reset/
drivers/vfio/platform/properties.c new file +77 -0

77 lines not on this page · raw message

drivers/vfio/platform/vfio_platform_common.c +34 -0
index e43efb5..cb1bbb4 100644
@@ -20,6 +20,7 @@
 #include <linux/types.h>
 #include <linux/uaccess.h>
 #include <linux/vfio.h>
+#include <linux/property.h>
 
 #include "vfio_platform_private.h"
 
@@ -302,6 +303,34 @@ static long vfio_platform_ioctl(void *device_data,
 			return vdev->reset(vdev);
 		else
 			return -EINVAL;
+	} else if (cmd == VFIO_DEVICE_GET_DEV_PROPERTY) {
+		struct vfio_dev_property info;
+		void __user *datap;
+		unsigned long datasz;
+		int ret;
+
+		if (!vdev->dev)
+			return -EINVAL;
+
+		minsz = offsetofend(struct vfio_dev_property, length);
+
+		if (copy_from_user(&info, (void __user *)arg, minsz))
+			return -EFAULT;
+
+		if (info.argsz < minsz)
+			return -EINVAL;
+
+		datap = (void __user *) arg + minsz;
+		datasz = info.argsz - minsz;
+
+		ret = vfio_platform_dev_properties(vdev->dev,
+				info.type, &info.length, datap, datasz);
+
+		if (copy_to_user((void __user *)arg, &info, minsz))
+			ret = -EFAULT;
+
+		return ret;
+
 	}
 
 	return -ENOTTY;
@@ -553,6 +582,11 @@ int vfio_platform_probe_common(struct vfio_platform_device *vdev,
 
 	vfio_platform_get_reset(vdev, dev);
 
+	/* add device properties flag */
+	if (device_property_present(dev, "name")) {
+		vdev->flags |= VFIO_DEVICE_FLAGS_DEV_PROPERTIES;
+	}
+
 	mutex_init(&vdev->igate);
 
 	return 0;
drivers/vfio/platform/vfio_platform_private.h +9 -0
index 1c9b3d5..55a9d32 100644
@@ -21,6 +21,8 @@
 #define VFIO_PLATFORM_OFFSET_SHIFT   40
 #define VFIO_PLATFORM_OFFSET_MASK (((u64)(1) << VFIO_PLATFORM_OFFSET_SHIFT) - 1)
 
+#define VFIO_PLATFORM_NAMESZ_LIMIT 128
+
 #define VFIO_PLATFORM_OFFSET_TO_INDEX(off)	\
 	(off >> VFIO_PLATFORM_OFFSET_SHIFT)
 
@@ -56,6 +58,7 @@ struct vfio_platform_device {
 	u32				num_irqs;
 	int				refcnt;
 	struct mutex			igate;
+	struct device			*dev;
 
 	/*
 	 * These fields should be filled by the bus specific binder
@@ -89,4 +92,10 @@ extern int vfio_platform_set_irqs_ioctl(struct vfio_platform_device *vdev,
 					unsigned start, unsigned count,
 					void *data);
 
+/* device properties support in properties.c */
+extern int vfio_platform_dev_properties(struct device *dev,
+					uint32_t type, unsigned *lenp,
+					void __user *datap,
+					unsigned long datasz);
+
 #endif /* VFIO_PLATFORM_PRIVATE_H */
include/uapi/linux/vfio.h +31 -0
index 9fd7b5d..111cb0c 100644
@@ -164,12 +164,43 @@ struct vfio_device_info {
 #define VFIO_DEVICE_FLAGS_PCI	(1 << 1)	/* vfio-pci device */
 #define VFIO_DEVICE_FLAGS_PLATFORM (1 << 2)	/* vfio-platform device */
 #define VFIO_DEVICE_FLAGS_AMBA  (1 << 3)	/* vfio-amba device */
+#define VFIO_DEVICE_FLAGS_DEV_PROPERTIES (1 << 4) /* Device properties */
 	__u32	num_regions;	/* Max region index + 1 */
 	__u32	num_irqs;	/* Max IRQ index + 1 */
 };
 #define VFIO_DEVICE_GET_INFO		_IO(VFIO_TYPE, VFIO_BASE + 7)
 
 /**
+ * VFIO_DEVICE_GET_DEV_PROPERTY - _IOR(VFIO_TYPE, VFIO_BASE + 17,
+ *						struct vfio_devtree_info)
+ *
+ * Retrieve a device property, e.g. from a HW description (device tree or ACPI)
+ * if available.
+ * Caller will initialize data[] with a single string with the requested
+ * devicetree property name, and type depending on whether an array of strings
+ * or an array of u32 values is expected. On success, data[] will be filled
+ * with the requested information, either as an array of u32, or with a list
+ * of strings separated by the NULL terminating character.
+ * Return: 0 on success, -errno on failure.
+ * Errors:
+ *   ENOSPC: the property is too big to fit in the data[] buffer (the required
+ *          size is then written into the length field).
+ */
+struct vfio_dev_property {
+	__u32	argsz;
+	__u32	flags;	/* Placeholder for future extension */
+	__u32	type;
+#define VFIO_DEV_PROPERTY_TYPE_STRINGS	0
+#define VFIO_DEV_PROPERTY_TYPE_U8	1
+#define VFIO_DEV_PROPERTY_TYPE_U16	2
+#define VFIO_DEV_PROPERTY_TYPE_U32	3
+#define VFIO_DEV_PROPERTY_TYPE_U64	4
+	__u32	length;	/* Size of data[] */
+	__u8	data[];
+};
+#define VFIO_DEVICE_GET_DEV_PROPERTY	_IO(VFIO_TYPE, VFIO_BASE + 17)
+
+/**
  * VFIO_DEVICE_GET_REGION_INFO - _IOWR(VFIO_TYPE, VFIO_BASE + 8,
  *				       struct vfio_region_info)
  *
Signature · 1 line
2.6.0
#1 lore raw

RFC PATCH v5 2/3 vfio: platform: access device property as a list of strings

From: Antonios Motakis <[email protected]>

Certain device properties (e.g. the device node name, the compatible string), are available as a list of strings (separated by the null terminating character). Let the VFIO user query this type of properties.

---
v4 -> v5:
- return ENOSPC when the buffer size is too small
- remove strlen call

v3 -> v4:
- The list length is computed before strings copy. If the entire list doesn't fit, no strings are copied to the user.

---
 drivers/vfio/platform/properties.c | 43 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 42 insertions(+), 1 deletion(-)
drivers/vfio/platform/properties.c +42 -1
index 48c90c5..212755f 100644
@@ -22,7 +22,48 @@ static int dev_property_get_strings(struct device *dev,
 				    char *name, unsigned *lenp,
 				    void __user *datap, unsigned long datasz)
 {
-	return -EINVAL;
+	const char **val;
+	int n, i, ret;
+
+	if (lenp == NULL)
+		return -EFAULT;
+
+	*lenp = 0;
+
+	n = device_property_read_string_array(dev, name, NULL, 0);
+	if (n < 0)
+		return n;
+
+	val = kcalloc(n, sizeof(char *), GFP_KERNEL);
+	if (!val)
+		return -ENOMEM;
+
+	ret = device_property_read_string_array(dev, name, val, n);
+	if (ret < 0)
+		goto out;
+
+	for (i = 0; i < n; i++)
+		*lenp += strlen(val[i]) + 1;
+
+	if (datasz < *lenp) {
+		ret = -ENOSPC;
+		goto out;
+	}
+
+	for (i = 0; i < n; i++) {
+		size_t len = strlen(val[i]) + 1;
+
+		if (copy_to_user(datap, val[i], len)) {
+			ret = -EFAULT;
+			goto out;
+		}
+
+		datap += len;
+	}
+
+out:
+	kfree(val);
+	return ret;
 }
 
 static int dev_property_get_uint(struct device *dev,
Signature · 1 line
2.6.0
#2 lore raw

RFC PATCH v5 3/3 vfio: platform: return device properties as arrays of unsigned integers

In reply to Baptiste Reynal

From: Antonios Motakis <a.motakis-lrHrjnjw1UfHK3s98zE1ajGjJy/[email protected]>

Certain properties of a device are accessible as an array of unsigned integers, either u64, u32, u16, or u8. Let the VFIO user query this type of device properties.

---
v4 -> v5:
- fix return error when the buffer size is too small

---
 drivers/vfio/platform/properties.c | 62 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 61 insertions(+), 1 deletion(-)
drivers/vfio/platform/properties.c +61 -1
index 212755f..a4b6955 100644
@@ -70,7 +70,67 @@ static int dev_property_get_uint(struct device *dev,
 				 char *name, uint32_t type, unsigned *lenp,
 				 void __user *datap, unsigned long datasz)
 {
-	return -EINVAL;
+	int ret, n;
+	u8 *out;
+	size_t sz;
+	int (*func)(const struct device *, const char *, void *, size_t)
+		= NULL;
+
+	switch (type) {
+	case VFIO_DEV_PROPERTY_TYPE_U64:
+		sz = sizeof(u64);
+		func = (int (*)(const struct device *,
+				const char *, void *, size_t))
+			device_property_read_u64_array;
+		break;
+	case VFIO_DEV_PROPERTY_TYPE_U32:
+		sz = sizeof(u32);
+		func = (int (*)(const struct device *,
+				const char *, void *, size_t))
+			device_property_read_u32_array;
+		break;
+	case VFIO_DEV_PROPERTY_TYPE_U16:
+		sz = sizeof(u16);
+		func = (int (*)(const struct device *,
+				const char *, void *, size_t))
+			device_property_read_u16_array;
+		break;
+	case VFIO_DEV_PROPERTY_TYPE_U8:
+		sz = sizeof(u8);
+		func = (int (*)(const struct device *,
+				const char *, void *, size_t))
+			device_property_read_u8_array;
+		break;
+
+	default:
+		return -EINVAL;
+	}
+
+	/* get size of array */
+	n = func(dev, name, NULL, 0);
+	if (n < 0)
+		return n;
+
+	if (lenp)
+		*lenp = n * sz;
+
+	if (n * sz > datasz)
+		return -ENOSPC;
+
+	out = kcalloc(n, sz, GFP_KERNEL);
+	if (!out)
+		return -ENOMEM;
+
+	ret = func(dev, name, out, n);
+	if (ret)
+		goto out;
+
+	if (copy_to_user(datap, out, n * sz))
+		ret = -EFAULT;
+
+out:
+	kfree(out);
+	return ret;
 }
 
 int vfio_platform_dev_properties(struct device *dev,
Signature · 1 line
2.6.0