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