1 From c4fbb93e85b13edc174fa6fc3105341f646305e4 Mon Sep 17 00:00:00 2001
2 From: Diana Craciun <diana.craciun@nxp.com>
3 Date: Wed, 2 Oct 2019 11:14:52 +0300
4 Subject: [PATCH] vfio/fsl-mc: Add read/write support for fsl-mc devices
6 This patch adds support to read and write ioctls for
7 fsl-mc devices. Only read-write to DPRC/DPMCP devices
8 are supported while read writes on other fsl-mc devices
9 is not supported by this patch.
11 Also current patch limits userspace to write complete
12 64byte command once and read 64byte response by one ioctl.
14 Signed-off-by: Bharat Bhushan <Bharat.Bhushan@nxp.com>
15 Signed-off-by: Diana Craciun <diana.craciun@nxp.com>
17 drivers/vfio/fsl-mc/vfio_fsl_mc.c | 125 +++++++++++++++++++++++++++++-
18 drivers/vfio/fsl-mc/vfio_fsl_mc_private.h | 1 +
19 2 files changed, 124 insertions(+), 2 deletions(-)
21 --- a/drivers/vfio/fsl-mc/vfio_fsl_mc.c
22 +++ b/drivers/vfio/fsl-mc/vfio_fsl_mc.c
24 #include <linux/types.h>
25 #include <linux/vfio.h>
26 #include <linux/fsl/mc.h>
27 +#include <linux/delay.h>
29 #include "vfio_fsl_mc_private.h"
31 @@ -112,6 +113,11 @@ static int vfio_fsl_mc_regions_init(stru
33 static void vfio_fsl_mc_regions_cleanup(struct vfio_fsl_mc_device *vdev)
37 + for (i = 0; i < vdev->num_regions; i++)
38 + iounmap(vdev->regions[i].ioaddr);
40 vdev->num_regions = 0;
43 @@ -308,13 +314,128 @@ static long vfio_fsl_mc_ioctl(void *devi
44 static ssize_t vfio_fsl_mc_read(void *device_data, char __user *buf,
45 size_t count, loff_t *ppos)
48 + struct vfio_fsl_mc_device *vdev = device_data;
49 + unsigned int index = VFIO_FSL_MC_OFFSET_TO_INDEX(*ppos);
50 + loff_t off = *ppos & VFIO_FSL_MC_OFFSET_MASK;
51 + struct vfio_fsl_mc_region *region;
55 + /* Read ioctl supported only for DPRC and DPMCP device */
56 + if (strcmp(vdev->mc_dev->obj_desc.type, "dprc") &&
57 + strcmp(vdev->mc_dev->obj_desc.type, "dpmcp"))
60 + if (index >= vdev->num_regions)
63 + region = &vdev->regions[index];
65 + if (!(region->flags & VFIO_REGION_INFO_FLAG_READ))
69 + if (!region->ioaddr) {
70 + region->ioaddr = ioremap_nocache(region->addr, region->size);
71 + if (!region->ioaddr)
75 + if (count != 64 || off != 0)
78 + for (i = 7; i >= 0; i--)
79 + data[i] = readq(region->ioaddr + i * sizeof(uint64_t));
81 + if (copy_to_user(buf, data, 64))
87 +#define MC_CMD_COMPLETION_TIMEOUT_MS 5000
88 +#define MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS 500
90 +static int vfio_fsl_mc_send_command(void __iomem *ioaddr, uint64_t *cmd_data)
93 + enum mc_cmd_status status;
94 + unsigned long timeout_usecs = MC_CMD_COMPLETION_TIMEOUT_MS * 1000;
96 + /* Write at command parameter into portal */
97 + for (i = 7; i >= 1; i--)
98 + writeq_relaxed(cmd_data[i], ioaddr + i * sizeof(uint64_t));
100 + /* Write command header in the end */
101 + writeq(cmd_data[0], ioaddr);
103 + /* Wait for response before returning to user-space
104 + * This can be optimized in future to even prepare response
105 + * before returning to user-space and avoid read ioctl.
109 + struct mc_cmd_header *resp_hdr;
111 + header = cpu_to_le64(readq_relaxed(ioaddr));
113 + resp_hdr = (struct mc_cmd_header *)&header;
114 + status = (enum mc_cmd_status)resp_hdr->status;
115 + if (status != MC_CMD_STATUS_READY)
118 + udelay(MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS);
119 + timeout_usecs -= MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS;
120 + if (timeout_usecs == 0)
128 static ssize_t vfio_fsl_mc_write(void *device_data, const char __user *buf,
129 size_t count, loff_t *ppos)
132 + struct vfio_fsl_mc_device *vdev = device_data;
133 + unsigned int index = VFIO_FSL_MC_OFFSET_TO_INDEX(*ppos);
134 + loff_t off = *ppos & VFIO_FSL_MC_OFFSET_MASK;
135 + struct vfio_fsl_mc_region *region;
140 + /* Write ioctl supported only for DPRC and DPMCP device */
141 + if (strcmp(vdev->mc_dev->obj_desc.type, "dprc") &&
142 + strcmp(vdev->mc_dev->obj_desc.type, "dpmcp"))
145 + if (index >= vdev->num_regions)
148 + region = &vdev->regions[index];
150 + if (!(region->flags & VFIO_REGION_INFO_FLAG_WRITE))
153 + if (!region->ioaddr) {
154 + region->ioaddr = ioremap_nocache(region->addr, region->size);
155 + if (!region->ioaddr)
159 + if (count != 64 || off != 0)
162 + if (copy_from_user(&data, buf, 64))
165 + ret = vfio_fsl_mc_send_command(region->ioaddr, data);
173 static int vfio_fsl_mc_mmap_mmio(struct vfio_fsl_mc_region region,
174 --- a/drivers/vfio/fsl-mc/vfio_fsl_mc_private.h
175 +++ b/drivers/vfio/fsl-mc/vfio_fsl_mc_private.h
176 @@ -32,6 +32,7 @@ struct vfio_fsl_mc_region {
179 resource_size_t size;
180 + void __iomem *ioaddr;
183 struct vfio_fsl_mc_device {