blob: d2209fdee12e8713e46af6d6fde8d90add376082 [file] [log] [blame]
Kristofer Jonsson116a6352020-08-20 17:25:23 +02001/*
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +01002 * Copyright (c) 2020,2022 Arm Limited.
Kristofer Jonsson116a6352020-08-20 17:25:23 +02003 *
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 Grohmann7e8f5082022-03-23 12:48:45 +010031#include "ethosu_cancel_inference.h"
Kristofer Jonsson116a6352020-08-20 17:25:23 +020032
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
42static int ethosu_inference_release(struct inode *inode,
43 struct file *file);
44
45static unsigned int ethosu_inference_poll(struct file *file,
46 poll_table *wait);
47
48static long ethosu_inference_ioctl(struct file *file,
49 unsigned int cmd,
50 unsigned long arg);
51
52static const struct file_operations ethosu_inference_fops = {
53 .release = &ethosu_inference_release,
54 .poll = &ethosu_inference_poll,
55 .unlocked_ioctl = &ethosu_inference_ioctl,
56#ifdef CONFIG_COMPAT
57 .compat_ioctl = &ethosu_inference_ioctl,
58#endif
59};
60
61/****************************************************************************
62 * Functions
63 ****************************************************************************/
64
65static 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 Grohmann82d22582022-04-25 12:52:38 +020074 case ETHOSU_UAPI_STATUS_RUNNING: {
75 return "Running";
76 }
77 case ETHOSU_UAPI_STATUS_REJECTED: {
78 return "Rejected";
79 }
Davide Grohmann7e8f5082022-03-23 12:48:45 +010080 case ETHOSU_UAPI_STATUS_ABORTED: {
81 return "Aborted";
82 }
83 case ETHOSU_UAPI_STATUS_ABORTING: {
84 return "Aborting";
85 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +020086 default: {
87 return "Unknown";
88 }
89 }
90}
91
92static int ethosu_inference_send(struct ethosu_inference *inf)
93{
94 int ret;
95
Kristofer Jonsson116a6352020-08-20 17:25:23 +020096 inf->status = ETHOSU_UAPI_STATUS_ERROR;
97
Davide Grohmann32660f92022-04-27 16:49:07 +020098 ret = ethosu_mailbox_inference(&inf->edev->mailbox, &inf->msg,
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020099 inf->ifm_count, inf->ifm,
100 inf->ofm_count, inf->ofm,
Per Åstrandf7e407a2020-10-23 21:25:05 +0200101 inf->net->buf,
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100102 inf->net->index,
Per Åstrandf7e407a2020-10-23 21:25:05 +0200103 inf->pmu_event_config,
104 ETHOSU_PMU_EVENT_MAX,
105 inf->pmu_cycle_counter_enable);
Kristofer Jonsson2d69b8b2022-06-30 15:34:52 +0200106 if (ret) {
107 dev_warn(inf->edev->dev,
108 "Failed to send inference request. inf=0x%pK, ret=%d",
109 inf, ret);
110
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200111 return ret;
Kristofer Jonsson2d69b8b2022-06-30 15:34:52 +0200112 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200113
Davide Grohmann82d22582022-04-25 12:52:38 +0200114 inf->status = ETHOSU_UAPI_STATUS_RUNNING;
115
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200116 ethosu_inference_get(inf);
117
118 return 0;
119}
120
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100121static void ethosu_inference_fail(struct ethosu_mailbox_msg *msg)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200122{
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100123 struct ethosu_inference *inf =
124 container_of(msg, typeof(*inf), msg);
125 int ret;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200126
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100127 if (inf->done)
128 return;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200129
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100130 /* 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 Jonsson442fefb2022-03-17 17:15:52 +0100140 inf->done = true;
141 wake_up_interruptible(&inf->waitq);
142}
143
144static int ethosu_inference_resend(struct ethosu_mailbox_msg *msg)
145{
146 struct ethosu_inference *inf =
147 container_of(msg, typeof(*inf), msg);
148 int ret;
149
150 /* Don't resend request if response has already been received */
151 if (inf->done)
152 return 0;
153
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100154 /* If marked as ABORTING simply fail it and return */
155 if (inf->status == ETHOSU_UAPI_STATUS_ABORTING) {
156 ethosu_inference_fail(msg);
157
158 return 0;
159 }
160
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100161 /* Decrement reference count for pending request */
162 ret = ethosu_inference_put(inf);
163 if (ret)
164 return 0;
165
166 /* Resend request */
167 ret = ethosu_inference_send(inf);
168 if (ret)
169 return ret;
170
171 return 0;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200172}
173
174static bool ethosu_inference_verify(struct file *file)
175{
176 return file->f_op == &ethosu_inference_fops;
177}
178
179static void ethosu_inference_kref_destroy(struct kref *kref)
180{
181 struct ethosu_inference *inf =
182 container_of(kref, struct ethosu_inference, kref);
183
184 dev_info(inf->edev->dev,
Kristofer Jonsson2d69b8b2022-06-30 15:34:52 +0200185 "Inference destroy. inf=0x%pK, status=%d, ifm_count=%u, ofm_count=%u",
186 inf, inf->status, inf->ifm_count, inf->ofm_count);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200187
Davide Grohmann32660f92022-04-27 16:49:07 +0200188 ethosu_mailbox_deregister(&inf->edev->mailbox, &inf->msg);
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200189
190 while (inf->ifm_count-- > 0)
191 ethosu_buffer_put(inf->ifm[inf->ifm_count]);
192
193 while (inf->ofm_count-- > 0)
194 ethosu_buffer_put(inf->ofm[inf->ofm_count]);
195
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200196 ethosu_network_put(inf->net);
197 devm_kfree(inf->edev->dev, inf);
198}
199
200static int ethosu_inference_release(struct inode *inode,
201 struct file *file)
202{
203 struct ethosu_inference *inf = file->private_data;
204
205 dev_info(inf->edev->dev,
Kristofer Jonsson2d69b8b2022-06-30 15:34:52 +0200206 "Inference release. file=0x%pK, inf=0x%pK",
207 file, inf);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200208
209 ethosu_inference_put(inf);
210
211 return 0;
212}
213
214static unsigned int ethosu_inference_poll(struct file *file,
215 poll_table *wait)
216{
217 struct ethosu_inference *inf = file->private_data;
218 int ret = 0;
219
220 poll_wait(file, &inf->waitq, wait);
221
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100222 if (inf->done)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200223 ret |= POLLIN;
224
225 return ret;
226}
227
228static long ethosu_inference_ioctl(struct file *file,
229 unsigned int cmd,
230 unsigned long arg)
231{
232 struct ethosu_inference *inf = file->private_data;
Per Åstrandf7e407a2020-10-23 21:25:05 +0200233 void __user *udata = (void __user *)arg;
234 int ret;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200235
236 ret = mutex_lock_interruptible(&inf->edev->mutex);
237 if (ret)
238 return ret;
239
Kristofer Jonsson2d69b8b2022-06-30 15:34:52 +0200240 dev_info(inf->edev->dev, "Ioctl: cmd=%u, arg=%lu", cmd, arg);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200241
242 switch (cmd) {
243 case ETHOSU_IOCTL_INFERENCE_STATUS: {
Per Åstrandf7e407a2020-10-23 21:25:05 +0200244 struct ethosu_uapi_result_status uapi;
245 int i;
246
247 uapi.status = inf->status;
248
249 for (i = 0; i < ETHOSU_PMU_EVENT_MAX; i++) {
250 uapi.pmu_config.events[i] =
251 inf->pmu_event_config[i];
252 uapi.pmu_count.events[i] =
253 inf->pmu_event_count[i];
254 }
255
256 uapi.pmu_config.cycle_count = inf->pmu_cycle_counter_enable;
257 uapi.pmu_count.cycle_count = inf->pmu_cycle_counter_count;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200258
259 dev_info(inf->edev->dev,
260 "Ioctl: Inference status. status=%s (%d)\n",
Per Åstrandf7e407a2020-10-23 21:25:05 +0200261 status_to_string(uapi.status), uapi.status);
262
263 ret = copy_to_user(udata, &uapi, sizeof(uapi)) ? -EFAULT : 0;
264
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200265 break;
266 }
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100267 case ETHOSU_IOCTL_INFERENCE_CANCEL: {
268 struct ethosu_uapi_cancel_inference_status uapi;
269
270 dev_info(inf->edev->dev, "Ioctl: Cancel Inference. Handle=%p\n",
271 inf);
272
273 ret = ethosu_cancel_inference_request(inf, &uapi);
274 if (ret)
275 break;
276
277 ret = copy_to_user(udata, &uapi, sizeof(uapi)) ? -EFAULT : 0;
278
279 break;
280 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200281 default: {
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100282 dev_err(inf->edev->dev, "Invalid ioctl. cmd=%u, arg=%lu\n",
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200283 cmd, arg);
284 break;
285 }
286 }
287
288 mutex_unlock(&inf->edev->mutex);
289
290 return ret;
291}
292
293int ethosu_inference_create(struct ethosu_device *edev,
294 struct ethosu_network *net,
295 struct ethosu_uapi_inference_create *uapi)
296{
297 struct ethosu_inference *inf;
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200298 uint32_t i;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200299 int fd;
300 int ret = -ENOMEM;
301
Kristofer Jonsson2d69b8b2022-06-30 15:34:52 +0200302 if (uapi->ifm_count > ETHOSU_FD_MAX ||
303 uapi->ofm_count > ETHOSU_FD_MAX) {
304 dev_warn(edev->dev,
305 "Too many IFM and/or OFM buffers for inference. ifm_count=%u, ofm_count=%u",
306 uapi->ifm_count, uapi->ofm_count);
307
308 return -EFAULT;
309 }
310
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200311 inf = devm_kzalloc(edev->dev, sizeof(*inf), GFP_KERNEL);
312 if (!inf)
313 return -ENOMEM;
314
315 inf->edev = edev;
316 inf->net = net;
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100317 inf->done = false;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200318 inf->status = ETHOSU_UAPI_STATUS_ERROR;
319 kref_init(&inf->kref);
320 init_waitqueue_head(&inf->waitq);
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100321 inf->msg.fail = ethosu_inference_fail;
322 inf->msg.resend = ethosu_inference_resend;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200323
Davide Grohmann32660f92022-04-27 16:49:07 +0200324 /* Add inference to pending list */
325 ret = ethosu_mailbox_register(&edev->mailbox, &inf->msg);
326 if (ret < 0)
327 goto kfree;
328
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200329 /* Get pointer to IFM buffers */
330 for (i = 0; i < uapi->ifm_count; i++) {
331 inf->ifm[i] = ethosu_buffer_get_from_fd(uapi->ifm_fd[i]);
332 if (IS_ERR(inf->ifm[i])) {
333 ret = PTR_ERR(inf->ifm[i]);
334 goto put_ifm;
335 }
336
337 inf->ifm_count++;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200338 }
339
340 /* Get pointer to OFM buffer */
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200341 for (i = 0; i < uapi->ofm_count; i++) {
342 inf->ofm[i] = ethosu_buffer_get_from_fd(uapi->ofm_fd[i]);
343 if (IS_ERR(inf->ofm[i])) {
344 ret = PTR_ERR(inf->ofm[i]);
345 goto put_ofm;
346 }
347
348 inf->ofm_count++;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200349 }
350
Per Åstrandf7e407a2020-10-23 21:25:05 +0200351 /* Configure PMU and cycle counter */
352 dev_info(inf->edev->dev,
353 "Configuring events for PMU. events=[%u, %u, %u, %u]\n",
354 uapi->pmu_config.events[0], uapi->pmu_config.events[1],
355 uapi->pmu_config.events[2], uapi->pmu_config.events[3]);
356
357 /* Configure events and reset count for all events */
358 for (i = 0; i < ETHOSU_PMU_EVENT_MAX; i++) {
359 inf->pmu_event_config[i] = uapi->pmu_config.events[i];
360 inf->pmu_event_count[i] = 0;
361 }
362
363 if (uapi->pmu_config.cycle_count)
364 dev_info(inf->edev->dev, "Enabling cycle counter\n");
365
366 /* Configure cycle counter and reset any previous count */
367 inf->pmu_cycle_counter_enable = uapi->pmu_config.cycle_count;
368 inf->pmu_cycle_counter_count = 0;
369
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200370 /* Increment network reference count */
371 ethosu_network_get(net);
372
Kristofer Jonsson2d69b8b2022-06-30 15:34:52 +0200373 /* Send inference request to Arm Ethos-U subsystem */
374 ret = ethosu_inference_send(inf);
375 if (ret)
376 goto put_net;
377
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200378 /* Create file descriptor */
379 ret = fd = anon_inode_getfd("ethosu-inference", &ethosu_inference_fops,
380 inf, O_RDWR | O_CLOEXEC);
381 if (ret < 0)
382 goto put_net;
383
384 /* Store pointer to file structure */
385 inf->file = fget(ret);
386 fput(inf->file);
387
Davide Grohmann32660f92022-04-27 16:49:07 +0200388 dev_info(edev->dev, "Inference create. Id=%d, handle=0x%p, fd=%d",
389 inf->msg.id, inf, fd);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200390
391 return fd;
392
393put_net:
394 ethosu_network_put(inf->net);
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200395
396put_ofm:
397 while (inf->ofm_count-- > 0)
398 ethosu_buffer_put(inf->ofm[inf->ofm_count]);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200399
400put_ifm:
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200401 while (inf->ifm_count-- > 0)
402 ethosu_buffer_put(inf->ifm[inf->ifm_count]);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200403
Davide Grohmann32660f92022-04-27 16:49:07 +0200404kfree:
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200405 devm_kfree(edev->dev, inf);
406
407 return ret;
408}
409
410struct ethosu_inference *ethosu_inference_get_from_fd(int fd)
411{
412 struct ethosu_inference *inf;
413 struct file *file;
414
415 file = fget(fd);
416 if (!file)
417 return ERR_PTR(-EINVAL);
418
419 if (!ethosu_inference_verify(file)) {
420 fput(file);
421
422 return ERR_PTR(-EINVAL);
423 }
424
425 inf = file->private_data;
426 ethosu_inference_get(inf);
427 fput(file);
428
429 return inf;
430}
431
432void ethosu_inference_get(struct ethosu_inference *inf)
433{
434 kref_get(&inf->kref);
435}
436
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100437int ethosu_inference_put(struct ethosu_inference *inf)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200438{
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100439 return kref_put(&inf->kref, &ethosu_inference_kref_destroy);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200440}
441
442void ethosu_inference_rsp(struct ethosu_device *edev,
443 struct ethosu_core_inference_rsp *rsp)
444{
Davide Grohmann32660f92022-04-27 16:49:07 +0200445 int id = (int)rsp->user_arg;
446 struct ethosu_mailbox_msg *msg;
447 struct ethosu_inference *inf;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200448 int ret;
Per Åstrandf7e407a2020-10-23 21:25:05 +0200449 int i;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200450
Davide Grohmann32660f92022-04-27 16:49:07 +0200451 msg = ethosu_mailbox_find(&edev->mailbox, id);
452 if (IS_ERR(msg)) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200453 dev_warn(edev->dev,
Davide Grohmann32660f92022-04-27 16:49:07 +0200454 "Id for inference msg not found. Id=%d\n",
455 id);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200456
457 return;
458 }
459
Davide Grohmann32660f92022-04-27 16:49:07 +0200460 inf = container_of(msg, typeof(*inf), msg);
461
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200462 if (rsp->status == ETHOSU_CORE_STATUS_OK &&
463 inf->ofm_count <= ETHOSU_CORE_BUFFER_MAX) {
464 uint32_t i;
465
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200466 inf->status = ETHOSU_UAPI_STATUS_OK;
467
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200468 for (i = 0; i < inf->ofm_count; i++) {
469 struct ethosu_buffer *ofm = inf->ofm[i];
470
471 ret = ethosu_buffer_resize(
472 ofm, ofm->size + rsp->ofm_size[i],
473 ofm->offset);
474 if (ret)
475 inf->status = ETHOSU_UAPI_STATUS_ERROR;
476 }
Davide Grohmann82d22582022-04-25 12:52:38 +0200477 } else if (rsp->status == ETHOSU_CORE_STATUS_REJECTED) {
478 inf->status = ETHOSU_UAPI_STATUS_REJECTED;
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100479 } else if (rsp->status == ETHOSU_CORE_STATUS_ABORTED) {
480 inf->status = ETHOSU_UAPI_STATUS_ABORTED;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200481 } else {
482 inf->status = ETHOSU_UAPI_STATUS_ERROR;
483 }
484
Davide Grohmann82d22582022-04-25 12:52:38 +0200485 if (inf->status == ETHOSU_UAPI_STATUS_OK) {
486 for (i = 0; i < ETHOSU_CORE_PMU_MAX; i++) {
487 inf->pmu_event_config[i] = rsp->pmu_event_config[i];
488 inf->pmu_event_count[i] = rsp->pmu_event_count[i];
489 }
490
491 inf->pmu_cycle_counter_enable = rsp->pmu_cycle_counter_enable;
492 inf->pmu_cycle_counter_count = rsp->pmu_cycle_counter_count;
493
494 dev_info(edev->dev,
495 "PMU events. config=[%u, %u, %u, %u], count=[%u, %u, %u, %u]\n",
496 inf->pmu_event_config[0], inf->pmu_event_config[1],
497 inf->pmu_event_config[2], inf->pmu_event_config[3],
498 inf->pmu_event_count[0], inf->pmu_event_count[1],
499 inf->pmu_event_count[2], inf->pmu_event_count[3]);
500
501 dev_info(edev->dev,
502 "PMU cycle counter. enable=%u, count=%llu\n",
503 inf->pmu_cycle_counter_enable,
504 inf->pmu_cycle_counter_count);
Per Åstrandf7e407a2020-10-23 21:25:05 +0200505 }
506
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100507 inf->done = true;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200508 wake_up_interruptible(&inf->waitq);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200509 ethosu_inference_put(inf);
510}