Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 1 | /* |
Kristofer Jonsson | 3c6a260 | 2022-03-10 11:17:29 +0100 | [diff] [blame] | 2 | * Copyright (c) 2020,2022 Arm Limited. |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 3 | * |
| 4 | * This program is free software and is provided to you under the terms of the |
| 5 | * GNU General Public License version 2 as published by the Free Software |
| 6 | * Foundation, and any use by you of this program is subject to the terms |
| 7 | * of such GNU licence. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, you can access it online at |
| 16 | * http://www.gnu.org/licenses/gpl-2.0.html. |
| 17 | * |
| 18 | * SPDX-License-Identifier: GPL-2.0-only |
| 19 | */ |
| 20 | |
| 21 | /**************************************************************************** |
| 22 | * Includes |
| 23 | ****************************************************************************/ |
| 24 | |
| 25 | #include "ethosu_network.h" |
| 26 | |
| 27 | #include "ethosu_buffer.h" |
| 28 | #include "ethosu_device.h" |
| 29 | #include "ethosu_inference.h" |
Kristofer Jonsson | 3c6a260 | 2022-03-10 11:17:29 +0100 | [diff] [blame] | 30 | #include "ethosu_network_info.h" |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 31 | #include "uapi/ethosu.h" |
| 32 | |
| 33 | #include <linux/anon_inodes.h> |
| 34 | #include <linux/file.h> |
| 35 | #include <linux/fs.h> |
| 36 | #include <linux/uaccess.h> |
| 37 | |
| 38 | /**************************************************************************** |
| 39 | * Variables |
| 40 | ****************************************************************************/ |
| 41 | |
| 42 | static int ethosu_network_release(struct inode *inode, |
| 43 | struct file *file); |
| 44 | |
| 45 | static long ethosu_network_ioctl(struct file *file, |
| 46 | unsigned int cmd, |
| 47 | unsigned long arg); |
| 48 | |
| 49 | static const struct file_operations ethosu_network_fops = { |
| 50 | .release = ðosu_network_release, |
| 51 | .unlocked_ioctl = ðosu_network_ioctl, |
| 52 | #ifdef CONFIG_COMPAT |
| 53 | .compat_ioctl = ðosu_network_ioctl, |
| 54 | #endif |
| 55 | }; |
| 56 | |
| 57 | /**************************************************************************** |
| 58 | * Functions |
| 59 | ****************************************************************************/ |
| 60 | |
| 61 | static bool ethosu_network_verify(struct file *file) |
| 62 | { |
| 63 | return file->f_op == ðosu_network_fops; |
| 64 | } |
| 65 | |
| 66 | static void ethosu_network_destroy(struct kref *kref) |
| 67 | { |
| 68 | struct ethosu_network *net = |
| 69 | container_of(kref, struct ethosu_network, kref); |
| 70 | |
| 71 | dev_info(net->edev->dev, "Network destroy. handle=0x%pK\n", net); |
| 72 | |
Kristofer Jonsson | 35de9e6 | 2022-03-08 13:25:45 +0100 | [diff] [blame] | 73 | if (net->buf != NULL) |
| 74 | ethosu_buffer_put(net->buf); |
| 75 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 76 | devm_kfree(net->edev->dev, net); |
| 77 | } |
| 78 | |
| 79 | static int ethosu_network_release(struct inode *inode, |
| 80 | struct file *file) |
| 81 | { |
| 82 | struct ethosu_network *net = file->private_data; |
| 83 | |
| 84 | dev_info(net->edev->dev, "Network release. handle=0x%pK\n", net); |
| 85 | |
| 86 | ethosu_network_put(net); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
Kristofer Jonsson | 3c6a260 | 2022-03-10 11:17:29 +0100 | [diff] [blame] | 91 | static int ethosu_network_info_request(struct ethosu_network *net, |
| 92 | struct ethosu_uapi_network_info *uapi) |
| 93 | { |
| 94 | struct ethosu_network_info *info; |
| 95 | int ret; |
| 96 | |
| 97 | /* Create network info request */ |
| 98 | info = ethosu_network_info_create(net->edev, net, uapi); |
| 99 | if (IS_ERR(info)) |
| 100 | return PTR_ERR(info); |
| 101 | |
| 102 | /* Unlock the device mutex and wait for completion */ |
| 103 | mutex_unlock(&net->edev->mutex); |
| 104 | ret = ethosu_network_info_wait(info, 3000); |
| 105 | mutex_lock(&net->edev->mutex); |
| 106 | |
Kristofer Jonsson | 442fefb | 2022-03-17 17:15:52 +0100 | [diff] [blame] | 107 | if (ret) |
| 108 | info->msg.fail(&info->msg); |
| 109 | |
Kristofer Jonsson | 3c6a260 | 2022-03-10 11:17:29 +0100 | [diff] [blame] | 110 | ethosu_network_info_put(info); |
| 111 | |
| 112 | return ret; |
| 113 | } |
| 114 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 115 | static long ethosu_network_ioctl(struct file *file, |
| 116 | unsigned int cmd, |
| 117 | unsigned long arg) |
| 118 | { |
| 119 | struct ethosu_network *net = file->private_data; |
| 120 | void __user *udata = (void __user *)arg; |
| 121 | int ret = -EINVAL; |
| 122 | |
| 123 | ret = mutex_lock_interruptible(&net->edev->mutex); |
| 124 | if (ret) |
| 125 | return ret; |
| 126 | |
Kristofer Jonsson | 3c6a260 | 2022-03-10 11:17:29 +0100 | [diff] [blame] | 127 | dev_info(net->edev->dev, "Ioctl: cmd=0x%x, arg=0x%lx\n", cmd, arg); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 128 | |
| 129 | switch (cmd) { |
Kristofer Jonsson | 3c6a260 | 2022-03-10 11:17:29 +0100 | [diff] [blame] | 130 | case ETHOSU_IOCTL_NETWORK_INFO: { |
| 131 | struct ethosu_uapi_network_info uapi; |
| 132 | |
| 133 | if (copy_from_user(&uapi, udata, sizeof(uapi))) |
| 134 | break; |
| 135 | |
| 136 | dev_info(net->edev->dev, |
| 137 | "Ioctl: Network info. handle=%p\n", |
| 138 | net); |
| 139 | |
| 140 | ret = ethosu_network_info_request(net, &uapi); |
| 141 | if (ret) |
| 142 | break; |
| 143 | |
| 144 | ret = copy_to_user(udata, &uapi, sizeof(uapi)) ? -EFAULT : 0; |
| 145 | break; |
| 146 | } |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 147 | case ETHOSU_IOCTL_INFERENCE_CREATE: { |
| 148 | struct ethosu_uapi_inference_create uapi; |
| 149 | |
| 150 | if (copy_from_user(&uapi, udata, sizeof(uapi))) |
| 151 | break; |
| 152 | |
| 153 | dev_info(net->edev->dev, |
| 154 | "Ioctl: Inference. ifm_fd=%u, ofm_fd=%u\n", |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 155 | uapi.ifm_fd[0], uapi.ofm_fd[0]); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 156 | |
| 157 | ret = ethosu_inference_create(net->edev, net, &uapi); |
| 158 | break; |
| 159 | } |
| 160 | default: { |
| 161 | dev_err(net->edev->dev, "Invalid ioctl. cmd=%u, arg=%lu", |
| 162 | cmd, arg); |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | mutex_unlock(&net->edev->mutex); |
| 168 | |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | int ethosu_network_create(struct ethosu_device *edev, |
| 173 | struct ethosu_uapi_network_create *uapi) |
| 174 | { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 175 | struct ethosu_network *net; |
| 176 | int ret = -ENOMEM; |
| 177 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 178 | net = devm_kzalloc(edev->dev, sizeof(*net), GFP_KERNEL); |
Kristofer Jonsson | 35de9e6 | 2022-03-08 13:25:45 +0100 | [diff] [blame] | 179 | if (!net) |
| 180 | return -ENOMEM; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 181 | |
| 182 | net->edev = edev; |
Kristofer Jonsson | 35de9e6 | 2022-03-08 13:25:45 +0100 | [diff] [blame] | 183 | net->buf = NULL; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 184 | kref_init(&net->kref); |
| 185 | |
Kristofer Jonsson | 35de9e6 | 2022-03-08 13:25:45 +0100 | [diff] [blame] | 186 | if (uapi->type == ETHOSU_UAPI_NETWORK_BUFFER) { |
| 187 | net->buf = ethosu_buffer_get_from_fd(uapi->fd); |
| 188 | if (IS_ERR(net->buf)) { |
| 189 | ret = PTR_ERR(net->buf); |
| 190 | goto free_net; |
| 191 | } |
| 192 | } else { |
| 193 | net->index = uapi->index; |
| 194 | } |
| 195 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 196 | ret = anon_inode_getfd("ethosu-network", ðosu_network_fops, net, |
| 197 | O_RDWR | O_CLOEXEC); |
| 198 | if (ret < 0) |
Kristofer Jonsson | 35de9e6 | 2022-03-08 13:25:45 +0100 | [diff] [blame] | 199 | goto put_buf; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 200 | |
| 201 | net->file = fget(ret); |
| 202 | fput(net->file); |
| 203 | |
| 204 | dev_info(edev->dev, "Network create. handle=0x%pK", |
| 205 | net); |
| 206 | |
| 207 | return ret; |
| 208 | |
Kristofer Jonsson | 35de9e6 | 2022-03-08 13:25:45 +0100 | [diff] [blame] | 209 | put_buf: |
| 210 | if (net->buf != NULL) |
| 211 | ethosu_buffer_put(net->buf); |
| 212 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 213 | free_net: |
| 214 | devm_kfree(edev->dev, net); |
| 215 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 216 | return ret; |
| 217 | } |
| 218 | |
| 219 | struct ethosu_network *ethosu_network_get_from_fd(int fd) |
| 220 | { |
| 221 | struct ethosu_network *net; |
| 222 | struct file *file; |
| 223 | |
| 224 | file = fget(fd); |
| 225 | if (!file) |
| 226 | return ERR_PTR(-EINVAL); |
| 227 | |
| 228 | if (!ethosu_network_verify(file)) { |
| 229 | fput(file); |
| 230 | |
| 231 | return ERR_PTR(-EINVAL); |
| 232 | } |
| 233 | |
| 234 | net = file->private_data; |
| 235 | ethosu_network_get(net); |
| 236 | fput(file); |
| 237 | |
| 238 | return net; |
| 239 | } |
| 240 | |
| 241 | void ethosu_network_get(struct ethosu_network *net) |
| 242 | { |
| 243 | kref_get(&net->kref); |
| 244 | } |
| 245 | |
Kristofer Jonsson | 442fefb | 2022-03-17 17:15:52 +0100 | [diff] [blame] | 246 | int ethosu_network_put(struct ethosu_network *net) |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 247 | { |
Kristofer Jonsson | 442fefb | 2022-03-17 17:15:52 +0100 | [diff] [blame] | 248 | return kref_put(&net->kref, ethosu_network_destroy); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 249 | } |