blob: b721fb764b2d6137094c2f08cc9a467a18ec078f [file] [log] [blame]
Kristofer Jonsson116a6352020-08-20 17:25:23 +02001/*
Mikael Olssond4ad9e52024-02-07 11:22:26 +01002 * SPDX-FileCopyrightText: Copyright 2020, 2022-2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
Ledion Dajaedd25502023-10-17 09:15:32 +02003 * SPDX-License-Identifier: GPL-2.0-only
Kristofer Jonsson116a6352020-08-20 17:25:23 +02004 *
5 * This program is free software and is provided to you under the terms of the
6 * GNU General Public License version 2 as published by the Free Software
7 * Foundation, and any use by you of this program is subject to the terms
8 * of such GNU licence.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, you can access it online at
17 * http://www.gnu.org/licenses/gpl-2.0.html.
Kristofer Jonsson116a6352020-08-20 17:25:23 +020018 */
19
20/****************************************************************************
21 * Includes
22 ****************************************************************************/
23
Mikael Olssond4ad9e52024-02-07 11:22:26 +010024#include <rpmsg/ethosu_rpmsg_inference.h>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020025
Mikael Olssond4ad9e52024-02-07 11:22:26 +010026#include <common/ethosu_buffer.h>
27#include <common/ethosu_device.h>
28#include <rpmsg/ethosu_rpmsg.h>
29#include <rpmsg/ethosu_rpmsg_cancel_inference.h>
30#include <rpmsg/ethosu_rpmsg_network.h>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020031
32#include <linux/anon_inodes.h>
33#include <linux/file.h>
34#include <linux/fs.h>
35#include <linux/poll.h>
36
37/****************************************************************************
38 * Variables
39 ****************************************************************************/
40
41static int ethosu_inference_release(struct inode *inode,
42 struct file *file);
43
Mikael Olssonb6824312023-11-14 10:43:04 +010044static __poll_t ethosu_inference_poll(struct file *file,
45 poll_table *wait);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020046
47static long ethosu_inference_ioctl(struct file *file,
48 unsigned int cmd,
49 unsigned long arg);
50
51static const struct file_operations ethosu_inference_fops = {
52 .release = &ethosu_inference_release,
53 .poll = &ethosu_inference_poll,
54 .unlocked_ioctl = &ethosu_inference_ioctl,
55#ifdef CONFIG_COMPAT
56 .compat_ioctl = &ethosu_inference_ioctl,
57#endif
58};
59
60/****************************************************************************
61 * Functions
62 ****************************************************************************/
63
64static const char *status_to_string(const enum ethosu_uapi_status status)
65{
66 switch (status) {
67 case ETHOSU_UAPI_STATUS_OK: {
68 return "Ok";
69 }
70 case ETHOSU_UAPI_STATUS_ERROR: {
71 return "Error";
72 }
Davide Grohmann82d22582022-04-25 12:52:38 +020073 case ETHOSU_UAPI_STATUS_RUNNING: {
74 return "Running";
75 }
76 case ETHOSU_UAPI_STATUS_REJECTED: {
77 return "Rejected";
78 }
Davide Grohmann7e8f5082022-03-23 12:48:45 +010079 case ETHOSU_UAPI_STATUS_ABORTED: {
80 return "Aborted";
81 }
82 case ETHOSU_UAPI_STATUS_ABORTING: {
83 return "Aborting";
84 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +020085 default: {
86 return "Unknown";
87 }
88 }
89}
90
91static int ethosu_inference_send(struct ethosu_inference *inf)
92{
Kristofer Jonssonec477042023-01-20 13:38:13 +010093 struct device *dev = inf->dev;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020094 int ret;
95
Kristofer Jonsson116a6352020-08-20 17:25:23 +020096 inf->status = ETHOSU_UAPI_STATUS_ERROR;
97
Kristofer Jonssonec477042023-01-20 13:38:13 +010098 ret = ethosu_mailbox_inference(inf->mailbox, &inf->msg,
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020099 inf->ifm_count, inf->ifm,
100 inf->ofm_count, inf->ofm,
Mikael Olssonc081e592023-10-30 11:10:56 +0100101 inf->net, inf->pmu_event_config,
Per Åstrandf7e407a2020-10-23 21:25:05 +0200102 ETHOSU_PMU_EVENT_MAX,
103 inf->pmu_cycle_counter_enable);
Kristofer Jonsson2d69b8b2022-06-30 15:34:52 +0200104 if (ret) {
Kristofer Jonssonec477042023-01-20 13:38:13 +0100105 dev_warn(dev,
Kristofer Jonsson2d69b8b2022-06-30 15:34:52 +0200106 "Failed to send inference request. inf=0x%pK, ret=%d",
107 inf, ret);
108
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200109 return ret;
Kristofer Jonsson2d69b8b2022-06-30 15:34:52 +0200110 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200111
Davide Grohmann82d22582022-04-25 12:52:38 +0200112 inf->status = ETHOSU_UAPI_STATUS_RUNNING;
113
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200114 ethosu_inference_get(inf);
115
116 return 0;
117}
118
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100119static void ethosu_inference_fail(struct ethosu_mailbox_msg *msg)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200120{
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100121 struct ethosu_inference *inf =
122 container_of(msg, typeof(*inf), msg);
123 int ret;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200124
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100125 if (inf->done)
126 return;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200127
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100128 /* Decrement reference count if inference was pending reponse */
129 ret = ethosu_inference_put(inf);
130 if (ret)
131 return;
132
133 /* Set status accordingly to the inference state */
134 inf->status = inf->status == ETHOSU_UAPI_STATUS_ABORTING ?
135 ETHOSU_UAPI_STATUS_ABORTED :
136 ETHOSU_UAPI_STATUS_ERROR;
137 /* Mark it done and wake up the waiting process */
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100138 inf->done = true;
139 wake_up_interruptible(&inf->waitq);
140}
141
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200142static bool ethosu_inference_verify(struct file *file)
143{
144 return file->f_op == &ethosu_inference_fops;
145}
146
147static void ethosu_inference_kref_destroy(struct kref *kref)
148{
149 struct ethosu_inference *inf =
150 container_of(kref, struct ethosu_inference, kref);
Kristofer Jonssonec477042023-01-20 13:38:13 +0100151 struct device *dev = inf->dev;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200152
Ledion Dajaedd25502023-10-17 09:15:32 +0200153 dev_dbg(dev,
154 "Inference destroy. inf=0x%pK, status=%d, ifm_count=%u, ofm_count=%u",
155 inf, inf->status, inf->ifm_count, inf->ofm_count);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200156
Kristofer Jonssonec477042023-01-20 13:38:13 +0100157 ethosu_mailbox_deregister(inf->mailbox, &inf->msg);
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200158
159 while (inf->ifm_count-- > 0)
160 ethosu_buffer_put(inf->ifm[inf->ifm_count]);
161
162 while (inf->ofm_count-- > 0)
163 ethosu_buffer_put(inf->ofm[inf->ofm_count]);
164
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200165 ethosu_network_put(inf->net);
Mikael Olsson1182f382023-08-10 13:25:44 +0200166 memset(inf, 0, sizeof(*inf));
Kristofer Jonssonec477042023-01-20 13:38:13 +0100167 devm_kfree(dev, inf);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200168}
169
170static int ethosu_inference_release(struct inode *inode,
171 struct file *file)
172{
173 struct ethosu_inference *inf = file->private_data;
Kristofer Jonssonec477042023-01-20 13:38:13 +0100174 struct device *dev = inf->dev;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200175
Ledion Dajaedd25502023-10-17 09:15:32 +0200176 dev_dbg(dev,
177 "Inference release. file=0x%pK, inf=0x%pK",
178 file, inf);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200179
Mikael Olsson529cfad2023-06-14 17:14:14 +0200180 device_lock(dev);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200181 ethosu_inference_put(inf);
Mikael Olsson529cfad2023-06-14 17:14:14 +0200182 device_unlock(dev);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200183
184 return 0;
185}
186
Mikael Olssonb6824312023-11-14 10:43:04 +0100187static __poll_t ethosu_inference_poll(struct file *file,
188 poll_table *wait)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200189{
190 struct ethosu_inference *inf = file->private_data;
Mikael Olssonb6824312023-11-14 10:43:04 +0100191 __poll_t ret = 0;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200192
193 poll_wait(file, &inf->waitq, wait);
194
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100195 if (inf->done)
Mikael Olssonb6824312023-11-14 10:43:04 +0100196 ret |= EPOLLIN;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200197
198 return ret;
199}
200
201static long ethosu_inference_ioctl(struct file *file,
202 unsigned int cmd,
203 unsigned long arg)
204{
205 struct ethosu_inference *inf = file->private_data;
Kristofer Jonssonec477042023-01-20 13:38:13 +0100206 struct device *dev = inf->dev;
Per Åstrandf7e407a2020-10-23 21:25:05 +0200207 void __user *udata = (void __user *)arg;
208 int ret;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200209
Kristofer Jonssonec477042023-01-20 13:38:13 +0100210 ret = device_lock_interruptible(dev);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200211 if (ret)
212 return ret;
213
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200214 switch (cmd) {
215 case ETHOSU_IOCTL_INFERENCE_STATUS: {
Mikael Olssonec902232023-08-24 13:28:19 +0200216 struct ethosu_uapi_result_status uapi = { 0 };
Per Åstrandf7e407a2020-10-23 21:25:05 +0200217 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 Jonsson116a6352020-08-20 17:25:23 +0200230
Ledion Dajaedd25502023-10-17 09:15:32 +0200231 dev_dbg(dev,
232 "Inference ioctl: Inference status. status=%s (%d)\n",
233 status_to_string(uapi.status), uapi.status);
Per Åstrandf7e407a2020-10-23 21:25:05 +0200234
235 ret = copy_to_user(udata, &uapi, sizeof(uapi)) ? -EFAULT : 0;
236
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200237 break;
238 }
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100239 case ETHOSU_IOCTL_INFERENCE_CANCEL: {
Mikael Olssonec902232023-08-24 13:28:19 +0200240 struct ethosu_uapi_cancel_inference_status uapi = { 0 };
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100241
Ledion Dajaedd25502023-10-17 09:15:32 +0200242 dev_dbg(dev,
243 "Inference ioctl: Cancel Inference. Handle=%p\n",
244 inf);
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100245
Kristofer Jonssonec477042023-01-20 13:38:13 +0100246 ret = ethosu_cancel_inference_request(dev, inf->mailbox, inf,
247 &uapi);
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100248 if (ret)
249 break;
250
251 ret = copy_to_user(udata, &uapi, sizeof(uapi)) ? -EFAULT : 0;
252
253 break;
254 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200255 default: {
Kristofer Jonssonec477042023-01-20 13:38:13 +0100256 dev_err(dev, "Invalid ioctl. cmd=%u, arg=%lu\n",
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200257 cmd, arg);
Mikael Olsson891156d2023-08-24 13:20:31 +0200258 ret = -ENOIOCTLCMD;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200259 break;
260 }
261 }
262
Kristofer Jonssonec477042023-01-20 13:38:13 +0100263 device_unlock(dev);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200264
265 return ret;
266}
267
Kristofer Jonssonec477042023-01-20 13:38:13 +0100268int ethosu_inference_create(struct device *dev,
269 struct ethosu_mailbox *mailbox,
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200270 struct ethosu_network *net,
271 struct ethosu_uapi_inference_create *uapi)
272{
273 struct ethosu_inference *inf;
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200274 uint32_t i;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200275 int fd;
276 int ret = -ENOMEM;
277
Kristofer Jonsson2d69b8b2022-06-30 15:34:52 +0200278 if (uapi->ifm_count > ETHOSU_FD_MAX ||
279 uapi->ofm_count > ETHOSU_FD_MAX) {
Kristofer Jonssonec477042023-01-20 13:38:13 +0100280 dev_warn(dev,
Kristofer Jonsson2d69b8b2022-06-30 15:34:52 +0200281 "Too many IFM and/or OFM buffers for inference. ifm_count=%u, ofm_count=%u",
282 uapi->ifm_count, uapi->ofm_count);
283
284 return -EFAULT;
285 }
286
Kristofer Jonssonec477042023-01-20 13:38:13 +0100287 inf = devm_kzalloc(dev, sizeof(*inf), GFP_KERNEL);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200288 if (!inf)
289 return -ENOMEM;
290
Kristofer Jonssonec477042023-01-20 13:38:13 +0100291 inf->dev = dev;
292 inf->mailbox = mailbox;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200293 inf->net = net;
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100294 inf->done = false;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200295 inf->status = ETHOSU_UAPI_STATUS_ERROR;
296 kref_init(&inf->kref);
297 init_waitqueue_head(&inf->waitq);
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100298 inf->msg.fail = ethosu_inference_fail;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200299
Davide Grohmann32660f92022-04-27 16:49:07 +0200300 /* Add inference to pending list */
Kristofer Jonssonec477042023-01-20 13:38:13 +0100301 ret = ethosu_mailbox_register(mailbox, &inf->msg);
Davide Grohmann32660f92022-04-27 16:49:07 +0200302 if (ret < 0)
303 goto kfree;
304
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200305 /* Get pointer to IFM buffers */
306 for (i = 0; i < uapi->ifm_count; i++) {
307 inf->ifm[i] = ethosu_buffer_get_from_fd(uapi->ifm_fd[i]);
308 if (IS_ERR(inf->ifm[i])) {
309 ret = PTR_ERR(inf->ifm[i]);
310 goto put_ifm;
311 }
312
313 inf->ifm_count++;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200314 }
315
316 /* Get pointer to OFM buffer */
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200317 for (i = 0; i < uapi->ofm_count; i++) {
318 inf->ofm[i] = ethosu_buffer_get_from_fd(uapi->ofm_fd[i]);
319 if (IS_ERR(inf->ofm[i])) {
320 ret = PTR_ERR(inf->ofm[i]);
321 goto put_ofm;
322 }
323
324 inf->ofm_count++;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200325 }
326
Per Åstrandf7e407a2020-10-23 21:25:05 +0200327 /* Configure PMU and cycle counter */
Ledion Dajaedd25502023-10-17 09:15:32 +0200328 dev_dbg(dev,
329 "Configuring events for PMU. events=[%u, %u, %u, %u]\n",
330 uapi->pmu_config.events[0], uapi->pmu_config.events[1],
331 uapi->pmu_config.events[2], uapi->pmu_config.events[3]);
Per Åstrandf7e407a2020-10-23 21:25:05 +0200332
333 /* Configure events and reset count for all events */
334 for (i = 0; i < ETHOSU_PMU_EVENT_MAX; i++) {
335 inf->pmu_event_config[i] = uapi->pmu_config.events[i];
336 inf->pmu_event_count[i] = 0;
337 }
338
Per Åstrandf7e407a2020-10-23 21:25:05 +0200339 /* Configure cycle counter and reset any previous count */
340 inf->pmu_cycle_counter_enable = uapi->pmu_config.cycle_count;
341 inf->pmu_cycle_counter_count = 0;
342
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200343 /* Increment network reference count */
344 ethosu_network_get(net);
345
Kristofer Jonsson2d69b8b2022-06-30 15:34:52 +0200346 /* Send inference request to Arm Ethos-U subsystem */
347 ret = ethosu_inference_send(inf);
348 if (ret)
349 goto put_net;
350
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200351 /* Create file descriptor */
352 ret = fd = anon_inode_getfd("ethosu-inference", &ethosu_inference_fops,
353 inf, O_RDWR | O_CLOEXEC);
354 if (ret < 0)
355 goto put_net;
356
357 /* Store pointer to file structure */
358 inf->file = fget(ret);
359 fput(inf->file);
360
Ledion Dajaedd25502023-10-17 09:15:32 +0200361 dev_dbg(dev,
362 "Inference create. file=0x%pK, fd=%d, inf=0x%p, net=0x%pK, msg.id=0x%x",
363 inf->file, fd, inf, inf->net, inf->msg.id);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200364
365 return fd;
366
367put_net:
368 ethosu_network_put(inf->net);
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200369
370put_ofm:
371 while (inf->ofm_count-- > 0)
372 ethosu_buffer_put(inf->ofm[inf->ofm_count]);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200373
374put_ifm:
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200375 while (inf->ifm_count-- > 0)
376 ethosu_buffer_put(inf->ifm[inf->ifm_count]);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200377
Mikael Olsson27fe8e02023-08-23 10:43:08 +0200378 ethosu_mailbox_deregister(mailbox, &inf->msg);
379
Davide Grohmann32660f92022-04-27 16:49:07 +0200380kfree:
Mikael Olsson1182f382023-08-10 13:25:44 +0200381 memset(inf, 0, sizeof(*inf));
Kristofer Jonssonec477042023-01-20 13:38:13 +0100382 devm_kfree(dev, inf);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200383
384 return ret;
385}
386
387struct ethosu_inference *ethosu_inference_get_from_fd(int fd)
388{
389 struct ethosu_inference *inf;
390 struct file *file;
391
392 file = fget(fd);
393 if (!file)
394 return ERR_PTR(-EINVAL);
395
396 if (!ethosu_inference_verify(file)) {
397 fput(file);
398
399 return ERR_PTR(-EINVAL);
400 }
401
402 inf = file->private_data;
403 ethosu_inference_get(inf);
404 fput(file);
405
406 return inf;
407}
408
409void ethosu_inference_get(struct ethosu_inference *inf)
410{
411 kref_get(&inf->kref);
412}
413
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100414int ethosu_inference_put(struct ethosu_inference *inf)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200415{
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100416 return kref_put(&inf->kref, &ethosu_inference_kref_destroy);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200417}
418
Kristofer Jonssonec477042023-01-20 13:38:13 +0100419void ethosu_inference_rsp(struct ethosu_mailbox *mailbox,
Kristofer Jonssond779a082023-01-04 17:09:47 +0100420 int msg_id,
421 struct ethosu_core_msg_inference_rsp *rsp)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200422{
Kristofer Jonssonec477042023-01-20 13:38:13 +0100423 struct device *dev = mailbox->dev;
Davide Grohmann32660f92022-04-27 16:49:07 +0200424 struct ethosu_mailbox_msg *msg;
425 struct ethosu_inference *inf;
Per Åstrandf7e407a2020-10-23 21:25:05 +0200426 int i;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200427
Mikael Olsson09965b02023-06-13 12:17:04 +0200428 msg = ethosu_mailbox_find(mailbox, msg_id,
429 ETHOSU_CORE_MSG_INFERENCE_REQ);
Davide Grohmann32660f92022-04-27 16:49:07 +0200430 if (IS_ERR(msg)) {
Kristofer Jonssonec477042023-01-20 13:38:13 +0100431 dev_warn(dev,
Mikael Olsson09965b02023-06-13 12:17:04 +0200432 "Id for inference msg not found. Id=0x%x: %ld\n",
433 msg_id, PTR_ERR(msg));
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200434
435 return;
436 }
437
Davide Grohmann32660f92022-04-27 16:49:07 +0200438 inf = container_of(msg, typeof(*inf), msg);
439
Mikael Olsson6d5e2d22024-01-16 11:19:09 +0100440 /*
441 * Don't handle the response if the inference is aborted or
442 * in the process of being aborted
443 */
444 if (inf->status == ETHOSU_UAPI_STATUS_ABORTED ||
445 inf->status == ETHOSU_UAPI_STATUS_ABORTING) {
446 inf->status = ETHOSU_UAPI_STATUS_ABORTED;
447 goto done;
448 }
449
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200450 if (rsp->status == ETHOSU_CORE_STATUS_OK &&
Mikael Olsson07545152023-10-17 13:05:38 +0200451 inf->ofm_count <= ETHOSU_CORE_BUFFER_MAX)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200452 inf->status = ETHOSU_UAPI_STATUS_OK;
Mikael Olsson07545152023-10-17 13:05:38 +0200453 else if (rsp->status == ETHOSU_CORE_STATUS_REJECTED)
Davide Grohmann82d22582022-04-25 12:52:38 +0200454 inf->status = ETHOSU_UAPI_STATUS_REJECTED;
Mikael Olsson07545152023-10-17 13:05:38 +0200455 else if (rsp->status == ETHOSU_CORE_STATUS_ABORTED)
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100456 inf->status = ETHOSU_UAPI_STATUS_ABORTED;
Mikael Olsson07545152023-10-17 13:05:38 +0200457 else
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200458 inf->status = ETHOSU_UAPI_STATUS_ERROR;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200459
Davide Grohmann82d22582022-04-25 12:52:38 +0200460 if (inf->status == ETHOSU_UAPI_STATUS_OK) {
461 for (i = 0; i < ETHOSU_CORE_PMU_MAX; i++) {
462 inf->pmu_event_config[i] = rsp->pmu_event_config[i];
463 inf->pmu_event_count[i] = rsp->pmu_event_count[i];
464 }
465
466 inf->pmu_cycle_counter_enable = rsp->pmu_cycle_counter_enable;
467 inf->pmu_cycle_counter_count = rsp->pmu_cycle_counter_count;
468
Ledion Dajaedd25502023-10-17 09:15:32 +0200469 dev_dbg(dev,
Mikael Olssone87446c2023-12-15 17:17:06 +0100470 "PMU events. config=[%u, %u, %u, %u], count=[%llu, %llu, %llu, %llu]\n",
Ledion Dajaedd25502023-10-17 09:15:32 +0200471 inf->pmu_event_config[0], inf->pmu_event_config[1],
472 inf->pmu_event_config[2], inf->pmu_event_config[3],
473 inf->pmu_event_count[0], inf->pmu_event_count[1],
474 inf->pmu_event_count[2], inf->pmu_event_count[3]);
Davide Grohmann82d22582022-04-25 12:52:38 +0200475
Ledion Dajaedd25502023-10-17 09:15:32 +0200476 if (inf->pmu_cycle_counter_enable)
477 dev_dbg(dev,
478 "PMU cycle counter: count=%llu\n",
479 inf->pmu_cycle_counter_count);
Per Åstrandf7e407a2020-10-23 21:25:05 +0200480 }
481
Mikael Olsson6d5e2d22024-01-16 11:19:09 +0100482done:
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100483 inf->done = true;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200484 wake_up_interruptible(&inf->waitq);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200485 ethosu_inference_put(inf);
486}