| b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 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 |
| 5 | |
| 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. |
| 10 | |
| 11 | Also current patch limits userspace to write complete |
| 12 | 64byte command once and read 64byte response by one ioctl. |
| 13 | |
| 14 | Signed-off-by: Bharat Bhushan <Bharat.Bhushan@nxp.com> |
| 15 | Signed-off-by: Diana Craciun <diana.craciun@nxp.com> |
| 16 | --- |
| 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(-) |
| 20 | |
| 21 | --- a/drivers/vfio/fsl-mc/vfio_fsl_mc.c |
| 22 | +++ b/drivers/vfio/fsl-mc/vfio_fsl_mc.c |
| 23 | @@ -12,6 +12,7 @@ |
| 24 | #include <linux/types.h> |
| 25 | #include <linux/vfio.h> |
| 26 | #include <linux/fsl/mc.h> |
| 27 | +#include <linux/delay.h> |
| 28 | |
| 29 | #include "vfio_fsl_mc_private.h" |
| 30 | |
| 31 | @@ -112,6 +113,11 @@ static int vfio_fsl_mc_regions_init(stru |
| 32 | } |
| 33 | static void vfio_fsl_mc_regions_cleanup(struct vfio_fsl_mc_device *vdev) |
| 34 | { |
| 35 | + int i; |
| 36 | + |
| 37 | + for (i = 0; i < vdev->num_regions; i++) |
| 38 | + iounmap(vdev->regions[i].ioaddr); |
| 39 | + |
| 40 | vdev->num_regions = 0; |
| 41 | kfree(vdev->regions); |
| 42 | } |
| 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) |
| 46 | { |
| 47 | - return -EINVAL; |
| 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; |
| 52 | + uint64_t data[8]; |
| 53 | + int i; |
| 54 | + |
| 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")) |
| 58 | + return -EINVAL; |
| 59 | + |
| 60 | + if (index >= vdev->num_regions) |
| 61 | + return -EINVAL; |
| 62 | + |
| 63 | + region = &vdev->regions[index]; |
| 64 | + |
| 65 | + if (!(region->flags & VFIO_REGION_INFO_FLAG_READ)) |
| 66 | + return -EINVAL; |
| 67 | + |
| 68 | + |
| 69 | + if (!region->ioaddr) { |
| 70 | + region->ioaddr = ioremap_nocache(region->addr, region->size); |
| 71 | + if (!region->ioaddr) |
| 72 | + return -ENOMEM; |
| 73 | + } |
| 74 | + |
| 75 | + if (count != 64 || off != 0) |
| 76 | + return -EINVAL; |
| 77 | + |
| 78 | + for (i = 7; i >= 0; i--) |
| 79 | + data[i] = readq(region->ioaddr + i * sizeof(uint64_t)); |
| 80 | + |
| 81 | + if (copy_to_user(buf, data, 64)) |
| 82 | + return -EFAULT; |
| 83 | + |
| 84 | + return count; |
| 85 | } |
| 86 | |
| 87 | +#define MC_CMD_COMPLETION_TIMEOUT_MS 5000 |
| 88 | +#define MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS 500 |
| 89 | + |
| 90 | +static int vfio_fsl_mc_send_command(void __iomem *ioaddr, uint64_t *cmd_data) |
| 91 | +{ |
| 92 | + int i; |
| 93 | + enum mc_cmd_status status; |
| 94 | + unsigned long timeout_usecs = MC_CMD_COMPLETION_TIMEOUT_MS * 1000; |
| 95 | + |
| 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)); |
| 99 | + |
| 100 | + /* Write command header in the end */ |
| 101 | + writeq(cmd_data[0], ioaddr); |
| 102 | + |
| 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. |
| 106 | + */ |
| 107 | + for (;;) { |
| 108 | + u64 header; |
| 109 | + struct mc_cmd_header *resp_hdr; |
| 110 | + |
| 111 | + header = cpu_to_le64(readq_relaxed(ioaddr)); |
| 112 | + |
| 113 | + resp_hdr = (struct mc_cmd_header *)&header; |
| 114 | + status = (enum mc_cmd_status)resp_hdr->status; |
| 115 | + if (status != MC_CMD_STATUS_READY) |
| 116 | + break; |
| 117 | + |
| 118 | + udelay(MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS); |
| 119 | + timeout_usecs -= MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS; |
| 120 | + if (timeout_usecs == 0) |
| 121 | + return -ETIMEDOUT; |
| 122 | + } |
| 123 | + |
| 124 | + return 0; |
| 125 | +} |
| 126 | + |
| 127 | + |
| 128 | static ssize_t vfio_fsl_mc_write(void *device_data, const char __user *buf, |
| 129 | size_t count, loff_t *ppos) |
| 130 | { |
| 131 | - return -EINVAL; |
| 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; |
| 136 | + uint64_t data[8]; |
| 137 | + int ret; |
| 138 | + |
| 139 | + |
| 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")) |
| 143 | + return -EINVAL; |
| 144 | + |
| 145 | + if (index >= vdev->num_regions) |
| 146 | + return -EINVAL; |
| 147 | + |
| 148 | + region = &vdev->regions[index]; |
| 149 | + |
| 150 | + if (!(region->flags & VFIO_REGION_INFO_FLAG_WRITE)) |
| 151 | + return -EINVAL; |
| 152 | + |
| 153 | + if (!region->ioaddr) { |
| 154 | + region->ioaddr = ioremap_nocache(region->addr, region->size); |
| 155 | + if (!region->ioaddr) |
| 156 | + return -ENOMEM; |
| 157 | + } |
| 158 | + |
| 159 | + if (count != 64 || off != 0) |
| 160 | + return -EINVAL; |
| 161 | + |
| 162 | + if (copy_from_user(&data, buf, 64)) |
| 163 | + return -EFAULT; |
| 164 | + |
| 165 | + ret = vfio_fsl_mc_send_command(region->ioaddr, data); |
| 166 | + if (ret) |
| 167 | + return ret; |
| 168 | + |
| 169 | + return count; |
| 170 | + |
| 171 | } |
| 172 | |
| 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 { |
| 177 | u32 type; |
| 178 | u64 addr; |
| 179 | resource_size_t size; |
| 180 | + void __iomem *ioaddr; |
| 181 | }; |
| 182 | |
| 183 | struct vfio_fsl_mc_device { |