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