Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) COPYRIGHT 2020 ARM Limited. All rights reserved. |
| 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_inference.h" |
| 26 | |
| 27 | #include "ethosu_buffer.h" |
| 28 | #include "ethosu_core_interface.h" |
| 29 | #include "ethosu_device.h" |
| 30 | #include "ethosu_network.h" |
| 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/poll.h> |
| 37 | |
| 38 | /**************************************************************************** |
| 39 | * Variables |
| 40 | ****************************************************************************/ |
| 41 | |
| 42 | static int ethosu_inference_release(struct inode *inode, |
| 43 | struct file *file); |
| 44 | |
| 45 | static unsigned int ethosu_inference_poll(struct file *file, |
| 46 | poll_table *wait); |
| 47 | |
| 48 | static long ethosu_inference_ioctl(struct file *file, |
| 49 | unsigned int cmd, |
| 50 | unsigned long arg); |
| 51 | |
| 52 | static const struct file_operations ethosu_inference_fops = { |
| 53 | .release = ðosu_inference_release, |
| 54 | .poll = ðosu_inference_poll, |
| 55 | .unlocked_ioctl = ðosu_inference_ioctl, |
| 56 | #ifdef CONFIG_COMPAT |
| 57 | .compat_ioctl = ðosu_inference_ioctl, |
| 58 | #endif |
| 59 | }; |
| 60 | |
| 61 | /**************************************************************************** |
| 62 | * Functions |
| 63 | ****************************************************************************/ |
| 64 | |
| 65 | static const char *status_to_string(const enum ethosu_uapi_status status) |
| 66 | { |
| 67 | switch (status) { |
| 68 | case ETHOSU_UAPI_STATUS_OK: { |
| 69 | return "Ok"; |
| 70 | } |
| 71 | case ETHOSU_UAPI_STATUS_ERROR: { |
| 72 | return "Error"; |
| 73 | } |
| 74 | default: { |
| 75 | return "Unknown"; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | static int ethosu_inference_send(struct ethosu_inference *inf) |
| 81 | { |
| 82 | int ret; |
| 83 | |
| 84 | if (inf->pending) |
| 85 | return -EINVAL; |
| 86 | |
| 87 | inf->status = ETHOSU_UAPI_STATUS_ERROR; |
| 88 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 89 | ret = ethosu_mailbox_inference(&inf->edev->mailbox, inf, |
| 90 | inf->ifm_count, inf->ifm, |
| 91 | inf->ofm_count, inf->ofm, |
Per Åstrand | f7e407a | 2020-10-23 21:25:05 +0200 | [diff] [blame] | 92 | inf->net->buf, |
| 93 | inf->pmu_event_config, |
| 94 | ETHOSU_PMU_EVENT_MAX, |
| 95 | inf->pmu_cycle_counter_enable); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 96 | if (ret) |
| 97 | return ret; |
| 98 | |
| 99 | inf->pending = true; |
| 100 | |
| 101 | ethosu_inference_get(inf); |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static int ethosu_inference_find(struct ethosu_inference *inf, |
| 107 | struct list_head *inference_list) |
| 108 | { |
| 109 | struct ethosu_inference *cur; |
| 110 | |
| 111 | list_for_each_entry(cur, inference_list, list) { |
| 112 | if (cur == inf) |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | return -EINVAL; |
| 117 | } |
| 118 | |
| 119 | static bool ethosu_inference_verify(struct file *file) |
| 120 | { |
| 121 | return file->f_op == ðosu_inference_fops; |
| 122 | } |
| 123 | |
| 124 | static void ethosu_inference_kref_destroy(struct kref *kref) |
| 125 | { |
| 126 | struct ethosu_inference *inf = |
| 127 | container_of(kref, struct ethosu_inference, kref); |
| 128 | |
| 129 | dev_info(inf->edev->dev, |
| 130 | "Inference destroy. handle=0x%pK, status=%d\n", |
| 131 | inf, inf->status); |
| 132 | |
| 133 | list_del(&inf->list); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 134 | |
| 135 | while (inf->ifm_count-- > 0) |
| 136 | ethosu_buffer_put(inf->ifm[inf->ifm_count]); |
| 137 | |
| 138 | while (inf->ofm_count-- > 0) |
| 139 | ethosu_buffer_put(inf->ofm[inf->ofm_count]); |
| 140 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 141 | ethosu_network_put(inf->net); |
| 142 | devm_kfree(inf->edev->dev, inf); |
| 143 | } |
| 144 | |
| 145 | static int ethosu_inference_release(struct inode *inode, |
| 146 | struct file *file) |
| 147 | { |
| 148 | struct ethosu_inference *inf = file->private_data; |
| 149 | |
| 150 | dev_info(inf->edev->dev, |
| 151 | "Inference release. handle=0x%pK, status=%d\n", |
| 152 | inf, inf->status); |
| 153 | |
| 154 | ethosu_inference_put(inf); |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static unsigned int ethosu_inference_poll(struct file *file, |
| 160 | poll_table *wait) |
| 161 | { |
| 162 | struct ethosu_inference *inf = file->private_data; |
| 163 | int ret = 0; |
| 164 | |
| 165 | poll_wait(file, &inf->waitq, wait); |
| 166 | |
| 167 | if (!inf->pending) |
| 168 | ret |= POLLIN; |
| 169 | |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | static long ethosu_inference_ioctl(struct file *file, |
| 174 | unsigned int cmd, |
| 175 | unsigned long arg) |
| 176 | { |
| 177 | struct ethosu_inference *inf = file->private_data; |
Per Åstrand | f7e407a | 2020-10-23 21:25:05 +0200 | [diff] [blame] | 178 | void __user *udata = (void __user *)arg; |
| 179 | int ret; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 180 | |
| 181 | ret = mutex_lock_interruptible(&inf->edev->mutex); |
| 182 | if (ret) |
| 183 | return ret; |
| 184 | |
| 185 | dev_info(inf->edev->dev, "Ioctl: cmd=%u, arg=%lu\n", cmd, arg); |
| 186 | |
| 187 | switch (cmd) { |
| 188 | case ETHOSU_IOCTL_INFERENCE_STATUS: { |
Per Åstrand | f7e407a | 2020-10-23 21:25:05 +0200 | [diff] [blame] | 189 | struct ethosu_uapi_result_status uapi; |
| 190 | int i; |
| 191 | |
| 192 | uapi.status = inf->status; |
| 193 | |
| 194 | for (i = 0; i < ETHOSU_PMU_EVENT_MAX; i++) { |
| 195 | uapi.pmu_config.events[i] = |
| 196 | inf->pmu_event_config[i]; |
| 197 | uapi.pmu_count.events[i] = |
| 198 | inf->pmu_event_count[i]; |
| 199 | } |
| 200 | |
| 201 | uapi.pmu_config.cycle_count = inf->pmu_cycle_counter_enable; |
| 202 | uapi.pmu_count.cycle_count = inf->pmu_cycle_counter_count; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 203 | |
| 204 | dev_info(inf->edev->dev, |
| 205 | "Ioctl: Inference status. status=%s (%d)\n", |
Per Åstrand | f7e407a | 2020-10-23 21:25:05 +0200 | [diff] [blame] | 206 | status_to_string(uapi.status), uapi.status); |
| 207 | |
| 208 | ret = copy_to_user(udata, &uapi, sizeof(uapi)) ? -EFAULT : 0; |
| 209 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 210 | break; |
| 211 | } |
| 212 | default: { |
| 213 | dev_err(inf->edev->dev, "Invalid ioctl. cmd=%u, arg=%lu", |
| 214 | cmd, arg); |
| 215 | break; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | mutex_unlock(&inf->edev->mutex); |
| 220 | |
| 221 | return ret; |
| 222 | } |
| 223 | |
| 224 | int ethosu_inference_create(struct ethosu_device *edev, |
| 225 | struct ethosu_network *net, |
| 226 | struct ethosu_uapi_inference_create *uapi) |
| 227 | { |
| 228 | struct ethosu_inference *inf; |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 229 | uint32_t i; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 230 | int fd; |
| 231 | int ret = -ENOMEM; |
| 232 | |
| 233 | inf = devm_kzalloc(edev->dev, sizeof(*inf), GFP_KERNEL); |
| 234 | if (!inf) |
| 235 | return -ENOMEM; |
| 236 | |
| 237 | inf->edev = edev; |
| 238 | inf->net = net; |
| 239 | inf->pending = false; |
| 240 | inf->status = ETHOSU_UAPI_STATUS_ERROR; |
| 241 | kref_init(&inf->kref); |
| 242 | init_waitqueue_head(&inf->waitq); |
| 243 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 244 | /* Get pointer to IFM buffers */ |
| 245 | for (i = 0; i < uapi->ifm_count; i++) { |
| 246 | inf->ifm[i] = ethosu_buffer_get_from_fd(uapi->ifm_fd[i]); |
| 247 | if (IS_ERR(inf->ifm[i])) { |
| 248 | ret = PTR_ERR(inf->ifm[i]); |
| 249 | goto put_ifm; |
| 250 | } |
| 251 | |
| 252 | inf->ifm_count++; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /* Get pointer to OFM buffer */ |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 256 | for (i = 0; i < uapi->ofm_count; i++) { |
| 257 | inf->ofm[i] = ethosu_buffer_get_from_fd(uapi->ofm_fd[i]); |
| 258 | if (IS_ERR(inf->ofm[i])) { |
| 259 | ret = PTR_ERR(inf->ofm[i]); |
| 260 | goto put_ofm; |
| 261 | } |
| 262 | |
| 263 | inf->ofm_count++; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 264 | } |
| 265 | |
Per Åstrand | f7e407a | 2020-10-23 21:25:05 +0200 | [diff] [blame] | 266 | /* Configure PMU and cycle counter */ |
| 267 | dev_info(inf->edev->dev, |
| 268 | "Configuring events for PMU. events=[%u, %u, %u, %u]\n", |
| 269 | uapi->pmu_config.events[0], uapi->pmu_config.events[1], |
| 270 | uapi->pmu_config.events[2], uapi->pmu_config.events[3]); |
| 271 | |
| 272 | /* Configure events and reset count for all events */ |
| 273 | for (i = 0; i < ETHOSU_PMU_EVENT_MAX; i++) { |
| 274 | inf->pmu_event_config[i] = uapi->pmu_config.events[i]; |
| 275 | inf->pmu_event_count[i] = 0; |
| 276 | } |
| 277 | |
| 278 | if (uapi->pmu_config.cycle_count) |
| 279 | dev_info(inf->edev->dev, "Enabling cycle counter\n"); |
| 280 | |
| 281 | /* Configure cycle counter and reset any previous count */ |
| 282 | inf->pmu_cycle_counter_enable = uapi->pmu_config.cycle_count; |
| 283 | inf->pmu_cycle_counter_count = 0; |
| 284 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 285 | /* Increment network reference count */ |
| 286 | ethosu_network_get(net); |
| 287 | |
| 288 | /* Create file descriptor */ |
| 289 | ret = fd = anon_inode_getfd("ethosu-inference", ðosu_inference_fops, |
| 290 | inf, O_RDWR | O_CLOEXEC); |
| 291 | if (ret < 0) |
| 292 | goto put_net; |
| 293 | |
| 294 | /* Store pointer to file structure */ |
| 295 | inf->file = fget(ret); |
| 296 | fput(inf->file); |
| 297 | |
| 298 | /* Add inference to inference list */ |
| 299 | list_add(&inf->list, &edev->inference_list); |
| 300 | |
| 301 | /* Send inference request to Arm Ethos-U subsystem */ |
| 302 | (void)ethosu_inference_send(inf); |
| 303 | |
| 304 | dev_info(edev->dev, "Inference create. handle=0x%pK, fd=%d", |
| 305 | inf, fd); |
| 306 | |
| 307 | return fd; |
| 308 | |
| 309 | put_net: |
| 310 | ethosu_network_put(inf->net); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 311 | |
| 312 | put_ofm: |
| 313 | while (inf->ofm_count-- > 0) |
| 314 | ethosu_buffer_put(inf->ofm[inf->ofm_count]); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 315 | |
| 316 | put_ifm: |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 317 | while (inf->ifm_count-- > 0) |
| 318 | ethosu_buffer_put(inf->ifm[inf->ifm_count]); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 319 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 320 | devm_kfree(edev->dev, inf); |
| 321 | |
| 322 | return ret; |
| 323 | } |
| 324 | |
| 325 | struct ethosu_inference *ethosu_inference_get_from_fd(int fd) |
| 326 | { |
| 327 | struct ethosu_inference *inf; |
| 328 | struct file *file; |
| 329 | |
| 330 | file = fget(fd); |
| 331 | if (!file) |
| 332 | return ERR_PTR(-EINVAL); |
| 333 | |
| 334 | if (!ethosu_inference_verify(file)) { |
| 335 | fput(file); |
| 336 | |
| 337 | return ERR_PTR(-EINVAL); |
| 338 | } |
| 339 | |
| 340 | inf = file->private_data; |
| 341 | ethosu_inference_get(inf); |
| 342 | fput(file); |
| 343 | |
| 344 | return inf; |
| 345 | } |
| 346 | |
| 347 | void ethosu_inference_get(struct ethosu_inference *inf) |
| 348 | { |
| 349 | kref_get(&inf->kref); |
| 350 | } |
| 351 | |
| 352 | void ethosu_inference_put(struct ethosu_inference *inf) |
| 353 | { |
| 354 | kref_put(&inf->kref, ðosu_inference_kref_destroy); |
| 355 | } |
| 356 | |
| 357 | void ethosu_inference_rsp(struct ethosu_device *edev, |
| 358 | struct ethosu_core_inference_rsp *rsp) |
| 359 | { |
| 360 | struct ethosu_inference *inf = |
| 361 | (struct ethosu_inference *)rsp->user_arg; |
| 362 | int ret; |
Per Åstrand | f7e407a | 2020-10-23 21:25:05 +0200 | [diff] [blame] | 363 | int i; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 364 | |
| 365 | ret = ethosu_inference_find(inf, &edev->inference_list); |
| 366 | if (ret) { |
| 367 | dev_warn(edev->dev, |
| 368 | "Handle not found in inference list. handle=0x%p\n", |
| 369 | rsp); |
| 370 | |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | inf->pending = false; |
| 375 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 376 | if (rsp->status == ETHOSU_CORE_STATUS_OK && |
| 377 | inf->ofm_count <= ETHOSU_CORE_BUFFER_MAX) { |
| 378 | uint32_t i; |
| 379 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 380 | inf->status = ETHOSU_UAPI_STATUS_OK; |
| 381 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 382 | for (i = 0; i < inf->ofm_count; i++) { |
| 383 | struct ethosu_buffer *ofm = inf->ofm[i]; |
| 384 | |
| 385 | ret = ethosu_buffer_resize( |
| 386 | ofm, ofm->size + rsp->ofm_size[i], |
| 387 | ofm->offset); |
| 388 | if (ret) |
| 389 | inf->status = ETHOSU_UAPI_STATUS_ERROR; |
| 390 | } |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 391 | } else { |
| 392 | inf->status = ETHOSU_UAPI_STATUS_ERROR; |
| 393 | } |
| 394 | |
Per Åstrand | f7e407a | 2020-10-23 21:25:05 +0200 | [diff] [blame] | 395 | for (i = 0; i < ETHOSU_CORE_PMU_MAX; i++) { |
| 396 | inf->pmu_event_config[i] = rsp->pmu_event_config[i]; |
| 397 | inf->pmu_event_count[i] = rsp->pmu_event_count[i]; |
| 398 | } |
| 399 | |
| 400 | inf->pmu_cycle_counter_enable = rsp->pmu_cycle_counter_enable; |
| 401 | inf->pmu_cycle_counter_count = rsp->pmu_cycle_counter_count; |
| 402 | |
| 403 | dev_info(edev->dev, |
Jonny Svärd | 9d8d92c | 2020-12-10 11:33:19 +0100 | [diff] [blame] | 404 | "PMU events. config=[%u, %u, %u, %u], count=[%u, %u, %u, %u]\n", |
| 405 | inf->pmu_event_config[0], inf->pmu_event_config[1], |
| 406 | inf->pmu_event_config[2], inf->pmu_event_config[3], |
| 407 | inf->pmu_event_count[0], inf->pmu_event_count[1], |
| 408 | inf->pmu_event_count[2], inf->pmu_event_count[3]); |
Per Åstrand | f7e407a | 2020-10-23 21:25:05 +0200 | [diff] [blame] | 409 | |
| 410 | dev_info(edev->dev, |
| 411 | "PMU cycle counter. enable=%u, count=%llu\n", |
| 412 | inf->pmu_cycle_counter_enable, |
| 413 | inf->pmu_cycle_counter_count); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 414 | wake_up_interruptible(&inf->waitq); |
| 415 | |
| 416 | ethosu_inference_put(inf); |
| 417 | } |