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_inference.h" |
| 26 | |
| 27 | #include "ethosu_buffer.h" |
| 28 | #include "ethosu_core_interface.h" |
| 29 | #include "ethosu_device.h" |
| 30 | #include "ethosu_network.h" |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame^] | 31 | #include "ethosu_cancel_inference.h" |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 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 | } |
Davide Grohmann | 82d2258 | 2022-04-25 12:52:38 +0200 | [diff] [blame] | 74 | case ETHOSU_UAPI_STATUS_RUNNING: { |
| 75 | return "Running"; |
| 76 | } |
| 77 | case ETHOSU_UAPI_STATUS_REJECTED: { |
| 78 | return "Rejected"; |
| 79 | } |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame^] | 80 | case ETHOSU_UAPI_STATUS_ABORTED: { |
| 81 | return "Aborted"; |
| 82 | } |
| 83 | case ETHOSU_UAPI_STATUS_ABORTING: { |
| 84 | return "Aborting"; |
| 85 | } |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 86 | default: { |
| 87 | return "Unknown"; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | static int ethosu_inference_send(struct ethosu_inference *inf) |
| 93 | { |
| 94 | int ret; |
| 95 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 96 | inf->status = ETHOSU_UAPI_STATUS_ERROR; |
| 97 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 98 | ret = ethosu_mailbox_inference(&inf->edev->mailbox, inf, |
| 99 | inf->ifm_count, inf->ifm, |
| 100 | inf->ofm_count, inf->ofm, |
Per Åstrand | f7e407a | 2020-10-23 21:25:05 +0200 | [diff] [blame] | 101 | inf->net->buf, |
Kristofer Jonsson | 35de9e6 | 2022-03-08 13:25:45 +0100 | [diff] [blame] | 102 | inf->net->index, |
Per Åstrand | f7e407a | 2020-10-23 21:25:05 +0200 | [diff] [blame] | 103 | inf->pmu_event_config, |
| 104 | ETHOSU_PMU_EVENT_MAX, |
| 105 | inf->pmu_cycle_counter_enable); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 106 | if (ret) |
| 107 | return ret; |
| 108 | |
Davide Grohmann | 82d2258 | 2022-04-25 12:52:38 +0200 | [diff] [blame] | 109 | inf->status = ETHOSU_UAPI_STATUS_RUNNING; |
| 110 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 111 | ethosu_inference_get(inf); |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
Kristofer Jonsson | 442fefb | 2022-03-17 17:15:52 +0100 | [diff] [blame] | 116 | static void ethosu_inference_fail(struct ethosu_mailbox_msg *msg) |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 117 | { |
Kristofer Jonsson | 442fefb | 2022-03-17 17:15:52 +0100 | [diff] [blame] | 118 | struct ethosu_inference *inf = |
| 119 | container_of(msg, typeof(*inf), msg); |
| 120 | int ret; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 121 | |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame^] | 122 | if (inf->done) |
| 123 | return; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 124 | |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame^] | 125 | /* Decrement reference count if inference was pending reponse */ |
| 126 | ret = ethosu_inference_put(inf); |
| 127 | if (ret) |
| 128 | return; |
| 129 | |
| 130 | /* Set status accordingly to the inference state */ |
| 131 | inf->status = inf->status == ETHOSU_UAPI_STATUS_ABORTING ? |
| 132 | ETHOSU_UAPI_STATUS_ABORTED : |
| 133 | ETHOSU_UAPI_STATUS_ERROR; |
| 134 | /* Mark it done and wake up the waiting process */ |
Kristofer Jonsson | 442fefb | 2022-03-17 17:15:52 +0100 | [diff] [blame] | 135 | inf->done = true; |
| 136 | wake_up_interruptible(&inf->waitq); |
| 137 | } |
| 138 | |
| 139 | static int ethosu_inference_resend(struct ethosu_mailbox_msg *msg) |
| 140 | { |
| 141 | struct ethosu_inference *inf = |
| 142 | container_of(msg, typeof(*inf), msg); |
| 143 | int ret; |
| 144 | |
| 145 | /* Don't resend request if response has already been received */ |
| 146 | if (inf->done) |
| 147 | return 0; |
| 148 | |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame^] | 149 | /* If marked as ABORTING simply fail it and return */ |
| 150 | if (inf->status == ETHOSU_UAPI_STATUS_ABORTING) { |
| 151 | ethosu_inference_fail(msg); |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
Kristofer Jonsson | 442fefb | 2022-03-17 17:15:52 +0100 | [diff] [blame] | 156 | /* Decrement reference count for pending request */ |
| 157 | ret = ethosu_inference_put(inf); |
| 158 | if (ret) |
| 159 | return 0; |
| 160 | |
| 161 | /* Resend request */ |
| 162 | ret = ethosu_inference_send(inf); |
| 163 | if (ret) |
| 164 | return ret; |
| 165 | |
| 166 | return 0; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static bool ethosu_inference_verify(struct file *file) |
| 170 | { |
| 171 | return file->f_op == ðosu_inference_fops; |
| 172 | } |
| 173 | |
| 174 | static void ethosu_inference_kref_destroy(struct kref *kref) |
| 175 | { |
| 176 | struct ethosu_inference *inf = |
| 177 | container_of(kref, struct ethosu_inference, kref); |
| 178 | |
| 179 | dev_info(inf->edev->dev, |
| 180 | "Inference destroy. handle=0x%pK, status=%d\n", |
| 181 | inf, inf->status); |
| 182 | |
Kristofer Jonsson | 442fefb | 2022-03-17 17:15:52 +0100 | [diff] [blame] | 183 | list_del(&inf->msg.list); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 184 | |
| 185 | while (inf->ifm_count-- > 0) |
| 186 | ethosu_buffer_put(inf->ifm[inf->ifm_count]); |
| 187 | |
| 188 | while (inf->ofm_count-- > 0) |
| 189 | ethosu_buffer_put(inf->ofm[inf->ofm_count]); |
| 190 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 191 | ethosu_network_put(inf->net); |
| 192 | devm_kfree(inf->edev->dev, inf); |
| 193 | } |
| 194 | |
| 195 | static int ethosu_inference_release(struct inode *inode, |
| 196 | struct file *file) |
| 197 | { |
| 198 | struct ethosu_inference *inf = file->private_data; |
| 199 | |
| 200 | dev_info(inf->edev->dev, |
| 201 | "Inference release. handle=0x%pK, status=%d\n", |
| 202 | inf, inf->status); |
| 203 | |
| 204 | ethosu_inference_put(inf); |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static unsigned int ethosu_inference_poll(struct file *file, |
| 210 | poll_table *wait) |
| 211 | { |
| 212 | struct ethosu_inference *inf = file->private_data; |
| 213 | int ret = 0; |
| 214 | |
| 215 | poll_wait(file, &inf->waitq, wait); |
| 216 | |
Kristofer Jonsson | 442fefb | 2022-03-17 17:15:52 +0100 | [diff] [blame] | 217 | if (inf->done) |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 218 | ret |= POLLIN; |
| 219 | |
| 220 | return ret; |
| 221 | } |
| 222 | |
| 223 | static long ethosu_inference_ioctl(struct file *file, |
| 224 | unsigned int cmd, |
| 225 | unsigned long arg) |
| 226 | { |
| 227 | struct ethosu_inference *inf = file->private_data; |
Per Åstrand | f7e407a | 2020-10-23 21:25:05 +0200 | [diff] [blame] | 228 | void __user *udata = (void __user *)arg; |
| 229 | int ret; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 230 | |
| 231 | ret = mutex_lock_interruptible(&inf->edev->mutex); |
| 232 | if (ret) |
| 233 | return ret; |
| 234 | |
| 235 | dev_info(inf->edev->dev, "Ioctl: cmd=%u, arg=%lu\n", cmd, arg); |
| 236 | |
| 237 | switch (cmd) { |
| 238 | case ETHOSU_IOCTL_INFERENCE_STATUS: { |
Per Åstrand | f7e407a | 2020-10-23 21:25:05 +0200 | [diff] [blame] | 239 | struct ethosu_uapi_result_status uapi; |
| 240 | int i; |
| 241 | |
| 242 | uapi.status = inf->status; |
| 243 | |
| 244 | for (i = 0; i < ETHOSU_PMU_EVENT_MAX; i++) { |
| 245 | uapi.pmu_config.events[i] = |
| 246 | inf->pmu_event_config[i]; |
| 247 | uapi.pmu_count.events[i] = |
| 248 | inf->pmu_event_count[i]; |
| 249 | } |
| 250 | |
| 251 | uapi.pmu_config.cycle_count = inf->pmu_cycle_counter_enable; |
| 252 | uapi.pmu_count.cycle_count = inf->pmu_cycle_counter_count; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 253 | |
| 254 | dev_info(inf->edev->dev, |
| 255 | "Ioctl: Inference status. status=%s (%d)\n", |
Per Åstrand | f7e407a | 2020-10-23 21:25:05 +0200 | [diff] [blame] | 256 | status_to_string(uapi.status), uapi.status); |
| 257 | |
| 258 | ret = copy_to_user(udata, &uapi, sizeof(uapi)) ? -EFAULT : 0; |
| 259 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 260 | break; |
| 261 | } |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame^] | 262 | case ETHOSU_IOCTL_INFERENCE_CANCEL: { |
| 263 | struct ethosu_uapi_cancel_inference_status uapi; |
| 264 | |
| 265 | dev_info(inf->edev->dev, "Ioctl: Cancel Inference. Handle=%p\n", |
| 266 | inf); |
| 267 | |
| 268 | ret = ethosu_cancel_inference_request(inf, &uapi); |
| 269 | if (ret) |
| 270 | break; |
| 271 | |
| 272 | ret = copy_to_user(udata, &uapi, sizeof(uapi)) ? -EFAULT : 0; |
| 273 | |
| 274 | break; |
| 275 | } |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 276 | default: { |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame^] | 277 | dev_err(inf->edev->dev, "Invalid ioctl. cmd=%u, arg=%lu\n", |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 278 | cmd, arg); |
| 279 | break; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | mutex_unlock(&inf->edev->mutex); |
| 284 | |
| 285 | return ret; |
| 286 | } |
| 287 | |
| 288 | int ethosu_inference_create(struct ethosu_device *edev, |
| 289 | struct ethosu_network *net, |
| 290 | struct ethosu_uapi_inference_create *uapi) |
| 291 | { |
| 292 | struct ethosu_inference *inf; |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 293 | uint32_t i; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 294 | int fd; |
| 295 | int ret = -ENOMEM; |
| 296 | |
| 297 | inf = devm_kzalloc(edev->dev, sizeof(*inf), GFP_KERNEL); |
| 298 | if (!inf) |
| 299 | return -ENOMEM; |
| 300 | |
| 301 | inf->edev = edev; |
| 302 | inf->net = net; |
Kristofer Jonsson | 442fefb | 2022-03-17 17:15:52 +0100 | [diff] [blame] | 303 | inf->done = false; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 304 | inf->status = ETHOSU_UAPI_STATUS_ERROR; |
| 305 | kref_init(&inf->kref); |
| 306 | init_waitqueue_head(&inf->waitq); |
Kristofer Jonsson | 442fefb | 2022-03-17 17:15:52 +0100 | [diff] [blame] | 307 | inf->msg.fail = ethosu_inference_fail; |
| 308 | inf->msg.resend = ethosu_inference_resend; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 309 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 310 | /* Get pointer to IFM buffers */ |
| 311 | for (i = 0; i < uapi->ifm_count; i++) { |
| 312 | inf->ifm[i] = ethosu_buffer_get_from_fd(uapi->ifm_fd[i]); |
| 313 | if (IS_ERR(inf->ifm[i])) { |
| 314 | ret = PTR_ERR(inf->ifm[i]); |
| 315 | goto put_ifm; |
| 316 | } |
| 317 | |
| 318 | inf->ifm_count++; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /* Get pointer to OFM buffer */ |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 322 | for (i = 0; i < uapi->ofm_count; i++) { |
| 323 | inf->ofm[i] = ethosu_buffer_get_from_fd(uapi->ofm_fd[i]); |
| 324 | if (IS_ERR(inf->ofm[i])) { |
| 325 | ret = PTR_ERR(inf->ofm[i]); |
| 326 | goto put_ofm; |
| 327 | } |
| 328 | |
| 329 | inf->ofm_count++; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 330 | } |
| 331 | |
Per Åstrand | f7e407a | 2020-10-23 21:25:05 +0200 | [diff] [blame] | 332 | /* Configure PMU and cycle counter */ |
| 333 | dev_info(inf->edev->dev, |
| 334 | "Configuring events for PMU. events=[%u, %u, %u, %u]\n", |
| 335 | uapi->pmu_config.events[0], uapi->pmu_config.events[1], |
| 336 | uapi->pmu_config.events[2], uapi->pmu_config.events[3]); |
| 337 | |
| 338 | /* Configure events and reset count for all events */ |
| 339 | for (i = 0; i < ETHOSU_PMU_EVENT_MAX; i++) { |
| 340 | inf->pmu_event_config[i] = uapi->pmu_config.events[i]; |
| 341 | inf->pmu_event_count[i] = 0; |
| 342 | } |
| 343 | |
| 344 | if (uapi->pmu_config.cycle_count) |
| 345 | dev_info(inf->edev->dev, "Enabling cycle counter\n"); |
| 346 | |
| 347 | /* Configure cycle counter and reset any previous count */ |
| 348 | inf->pmu_cycle_counter_enable = uapi->pmu_config.cycle_count; |
| 349 | inf->pmu_cycle_counter_count = 0; |
| 350 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 351 | /* Increment network reference count */ |
| 352 | ethosu_network_get(net); |
| 353 | |
| 354 | /* Create file descriptor */ |
| 355 | ret = fd = anon_inode_getfd("ethosu-inference", ðosu_inference_fops, |
| 356 | inf, O_RDWR | O_CLOEXEC); |
| 357 | if (ret < 0) |
| 358 | goto put_net; |
| 359 | |
| 360 | /* Store pointer to file structure */ |
| 361 | inf->file = fget(ret); |
| 362 | fput(inf->file); |
| 363 | |
Kristofer Jonsson | 442fefb | 2022-03-17 17:15:52 +0100 | [diff] [blame] | 364 | /* Add inference to pending list */ |
| 365 | list_add(&inf->msg.list, &edev->mailbox.pending_list); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 366 | |
| 367 | /* Send inference request to Arm Ethos-U subsystem */ |
| 368 | (void)ethosu_inference_send(inf); |
| 369 | |
| 370 | dev_info(edev->dev, "Inference create. handle=0x%pK, fd=%d", |
| 371 | inf, fd); |
| 372 | |
| 373 | return fd; |
| 374 | |
| 375 | put_net: |
| 376 | ethosu_network_put(inf->net); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 377 | |
| 378 | put_ofm: |
| 379 | while (inf->ofm_count-- > 0) |
| 380 | ethosu_buffer_put(inf->ofm[inf->ofm_count]); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 381 | |
| 382 | put_ifm: |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 383 | while (inf->ifm_count-- > 0) |
| 384 | ethosu_buffer_put(inf->ifm[inf->ifm_count]); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 385 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 386 | devm_kfree(edev->dev, inf); |
| 387 | |
| 388 | return ret; |
| 389 | } |
| 390 | |
| 391 | struct ethosu_inference *ethosu_inference_get_from_fd(int fd) |
| 392 | { |
| 393 | struct ethosu_inference *inf; |
| 394 | struct file *file; |
| 395 | |
| 396 | file = fget(fd); |
| 397 | if (!file) |
| 398 | return ERR_PTR(-EINVAL); |
| 399 | |
| 400 | if (!ethosu_inference_verify(file)) { |
| 401 | fput(file); |
| 402 | |
| 403 | return ERR_PTR(-EINVAL); |
| 404 | } |
| 405 | |
| 406 | inf = file->private_data; |
| 407 | ethosu_inference_get(inf); |
| 408 | fput(file); |
| 409 | |
| 410 | return inf; |
| 411 | } |
| 412 | |
| 413 | void ethosu_inference_get(struct ethosu_inference *inf) |
| 414 | { |
| 415 | kref_get(&inf->kref); |
| 416 | } |
| 417 | |
Kristofer Jonsson | 442fefb | 2022-03-17 17:15:52 +0100 | [diff] [blame] | 418 | int ethosu_inference_put(struct ethosu_inference *inf) |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 419 | { |
Kristofer Jonsson | 442fefb | 2022-03-17 17:15:52 +0100 | [diff] [blame] | 420 | return kref_put(&inf->kref, ðosu_inference_kref_destroy); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | void ethosu_inference_rsp(struct ethosu_device *edev, |
| 424 | struct ethosu_core_inference_rsp *rsp) |
| 425 | { |
| 426 | struct ethosu_inference *inf = |
| 427 | (struct ethosu_inference *)rsp->user_arg; |
| 428 | int ret; |
Per Åstrand | f7e407a | 2020-10-23 21:25:05 +0200 | [diff] [blame] | 429 | int i; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 430 | |
Kristofer Jonsson | 442fefb | 2022-03-17 17:15:52 +0100 | [diff] [blame] | 431 | ret = ethosu_mailbox_find(&edev->mailbox, &inf->msg); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 432 | if (ret) { |
| 433 | dev_warn(edev->dev, |
| 434 | "Handle not found in inference list. handle=0x%p\n", |
| 435 | rsp); |
| 436 | |
| 437 | return; |
| 438 | } |
| 439 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 440 | if (rsp->status == ETHOSU_CORE_STATUS_OK && |
| 441 | inf->ofm_count <= ETHOSU_CORE_BUFFER_MAX) { |
| 442 | uint32_t i; |
| 443 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 444 | inf->status = ETHOSU_UAPI_STATUS_OK; |
| 445 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 446 | for (i = 0; i < inf->ofm_count; i++) { |
| 447 | struct ethosu_buffer *ofm = inf->ofm[i]; |
| 448 | |
| 449 | ret = ethosu_buffer_resize( |
| 450 | ofm, ofm->size + rsp->ofm_size[i], |
| 451 | ofm->offset); |
| 452 | if (ret) |
| 453 | inf->status = ETHOSU_UAPI_STATUS_ERROR; |
| 454 | } |
Davide Grohmann | 82d2258 | 2022-04-25 12:52:38 +0200 | [diff] [blame] | 455 | } else if (rsp->status == ETHOSU_CORE_STATUS_REJECTED) { |
| 456 | inf->status = ETHOSU_UAPI_STATUS_REJECTED; |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame^] | 457 | } else if (rsp->status == ETHOSU_CORE_STATUS_ABORTED) { |
| 458 | inf->status = ETHOSU_UAPI_STATUS_ABORTED; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 459 | } else { |
| 460 | inf->status = ETHOSU_UAPI_STATUS_ERROR; |
| 461 | } |
| 462 | |
Davide Grohmann | 82d2258 | 2022-04-25 12:52:38 +0200 | [diff] [blame] | 463 | if (inf->status == ETHOSU_UAPI_STATUS_OK) { |
| 464 | for (i = 0; i < ETHOSU_CORE_PMU_MAX; i++) { |
| 465 | inf->pmu_event_config[i] = rsp->pmu_event_config[i]; |
| 466 | inf->pmu_event_count[i] = rsp->pmu_event_count[i]; |
| 467 | } |
| 468 | |
| 469 | inf->pmu_cycle_counter_enable = rsp->pmu_cycle_counter_enable; |
| 470 | inf->pmu_cycle_counter_count = rsp->pmu_cycle_counter_count; |
| 471 | |
| 472 | dev_info(edev->dev, |
| 473 | "PMU events. config=[%u, %u, %u, %u], count=[%u, %u, %u, %u]\n", |
| 474 | inf->pmu_event_config[0], inf->pmu_event_config[1], |
| 475 | inf->pmu_event_config[2], inf->pmu_event_config[3], |
| 476 | inf->pmu_event_count[0], inf->pmu_event_count[1], |
| 477 | inf->pmu_event_count[2], inf->pmu_event_count[3]); |
| 478 | |
| 479 | dev_info(edev->dev, |
| 480 | "PMU cycle counter. enable=%u, count=%llu\n", |
| 481 | inf->pmu_cycle_counter_enable, |
| 482 | inf->pmu_cycle_counter_count); |
Per Åstrand | f7e407a | 2020-10-23 21:25:05 +0200 | [diff] [blame] | 483 | } |
| 484 | |
Kristofer Jonsson | 442fefb | 2022-03-17 17:15:52 +0100 | [diff] [blame] | 485 | inf->done = true; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 486 | wake_up_interruptible(&inf->waitq); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 487 | ethosu_inference_put(inf); |
| 488 | } |