blob: 73a8c06c1ea8d202503ef3040f271a9f2765f8c5 [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"
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
44static unsigned int ethosu_inference_poll(struct file *file,
45 poll_table *wait);
46
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 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +020079 default: {
80 return "Unknown";
81 }
82 }
83}
84
85static int ethosu_inference_send(struct ethosu_inference *inf)
86{
87 int ret;
88
Kristofer Jonsson116a6352020-08-20 17:25:23 +020089 inf->status = ETHOSU_UAPI_STATUS_ERROR;
90
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020091 ret = ethosu_mailbox_inference(&inf->edev->mailbox, inf,
92 inf->ifm_count, inf->ifm,
93 inf->ofm_count, inf->ofm,
Per Åstrandf7e407a2020-10-23 21:25:05 +020094 inf->net->buf,
Kristofer Jonsson35de9e62022-03-08 13:25:45 +010095 inf->net->index,
Per Åstrandf7e407a2020-10-23 21:25:05 +020096 inf->pmu_event_config,
97 ETHOSU_PMU_EVENT_MAX,
98 inf->pmu_cycle_counter_enable);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020099 if (ret)
100 return ret;
101
Davide Grohmann82d22582022-04-25 12:52:38 +0200102 inf->status = ETHOSU_UAPI_STATUS_RUNNING;
103
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200104 ethosu_inference_get(inf);
105
106 return 0;
107}
108
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100109static void ethosu_inference_fail(struct ethosu_mailbox_msg *msg)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200110{
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100111 struct ethosu_inference *inf =
112 container_of(msg, typeof(*inf), msg);
113 int ret;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200114
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100115 /* Decrement reference count if inference was pending reponse */
116 if (!inf->done) {
117 ret = ethosu_inference_put(inf);
118 if (ret)
119 return;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200120 }
121
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100122 /* Fail inference and wake up any waiting process */
123 inf->status = ETHOSU_UAPI_STATUS_ERROR;
124 inf->done = true;
125 wake_up_interruptible(&inf->waitq);
126}
127
128static int ethosu_inference_resend(struct ethosu_mailbox_msg *msg)
129{
130 struct ethosu_inference *inf =
131 container_of(msg, typeof(*inf), msg);
132 int ret;
133
134 /* Don't resend request if response has already been received */
135 if (inf->done)
136 return 0;
137
138 /* Decrement reference count for pending request */
139 ret = ethosu_inference_put(inf);
140 if (ret)
141 return 0;
142
143 /* Resend request */
144 ret = ethosu_inference_send(inf);
145 if (ret)
146 return ret;
147
148 return 0;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200149}
150
151static bool ethosu_inference_verify(struct file *file)
152{
153 return file->f_op == &ethosu_inference_fops;
154}
155
156static void ethosu_inference_kref_destroy(struct kref *kref)
157{
158 struct ethosu_inference *inf =
159 container_of(kref, struct ethosu_inference, kref);
160
161 dev_info(inf->edev->dev,
162 "Inference destroy. handle=0x%pK, status=%d\n",
163 inf, inf->status);
164
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100165 list_del(&inf->msg.list);
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200166
167 while (inf->ifm_count-- > 0)
168 ethosu_buffer_put(inf->ifm[inf->ifm_count]);
169
170 while (inf->ofm_count-- > 0)
171 ethosu_buffer_put(inf->ofm[inf->ofm_count]);
172
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200173 ethosu_network_put(inf->net);
174 devm_kfree(inf->edev->dev, inf);
175}
176
177static int ethosu_inference_release(struct inode *inode,
178 struct file *file)
179{
180 struct ethosu_inference *inf = file->private_data;
181
182 dev_info(inf->edev->dev,
183 "Inference release. handle=0x%pK, status=%d\n",
184 inf, inf->status);
185
186 ethosu_inference_put(inf);
187
188 return 0;
189}
190
191static unsigned int ethosu_inference_poll(struct file *file,
192 poll_table *wait)
193{
194 struct ethosu_inference *inf = file->private_data;
195 int ret = 0;
196
197 poll_wait(file, &inf->waitq, wait);
198
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100199 if (inf->done)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200200 ret |= POLLIN;
201
202 return ret;
203}
204
205static long ethosu_inference_ioctl(struct file *file,
206 unsigned int cmd,
207 unsigned long arg)
208{
209 struct ethosu_inference *inf = file->private_data;
Per Åstrandf7e407a2020-10-23 21:25:05 +0200210 void __user *udata = (void __user *)arg;
211 int ret;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200212
213 ret = mutex_lock_interruptible(&inf->edev->mutex);
214 if (ret)
215 return ret;
216
217 dev_info(inf->edev->dev, "Ioctl: cmd=%u, arg=%lu\n", cmd, arg);
218
219 switch (cmd) {
220 case ETHOSU_IOCTL_INFERENCE_STATUS: {
Per Åstrandf7e407a2020-10-23 21:25:05 +0200221 struct ethosu_uapi_result_status uapi;
222 int i;
223
224 uapi.status = inf->status;
225
226 for (i = 0; i < ETHOSU_PMU_EVENT_MAX; i++) {
227 uapi.pmu_config.events[i] =
228 inf->pmu_event_config[i];
229 uapi.pmu_count.events[i] =
230 inf->pmu_event_count[i];
231 }
232
233 uapi.pmu_config.cycle_count = inf->pmu_cycle_counter_enable;
234 uapi.pmu_count.cycle_count = inf->pmu_cycle_counter_count;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200235
236 dev_info(inf->edev->dev,
237 "Ioctl: Inference status. status=%s (%d)\n",
Per Åstrandf7e407a2020-10-23 21:25:05 +0200238 status_to_string(uapi.status), uapi.status);
239
240 ret = copy_to_user(udata, &uapi, sizeof(uapi)) ? -EFAULT : 0;
241
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200242 break;
243 }
244 default: {
245 dev_err(inf->edev->dev, "Invalid ioctl. cmd=%u, arg=%lu",
246 cmd, arg);
247 break;
248 }
249 }
250
251 mutex_unlock(&inf->edev->mutex);
252
253 return ret;
254}
255
256int ethosu_inference_create(struct ethosu_device *edev,
257 struct ethosu_network *net,
258 struct ethosu_uapi_inference_create *uapi)
259{
260 struct ethosu_inference *inf;
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200261 uint32_t i;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200262 int fd;
263 int ret = -ENOMEM;
264
265 inf = devm_kzalloc(edev->dev, sizeof(*inf), GFP_KERNEL);
266 if (!inf)
267 return -ENOMEM;
268
269 inf->edev = edev;
270 inf->net = net;
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100271 inf->done = false;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200272 inf->status = ETHOSU_UAPI_STATUS_ERROR;
273 kref_init(&inf->kref);
274 init_waitqueue_head(&inf->waitq);
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100275 inf->msg.fail = ethosu_inference_fail;
276 inf->msg.resend = ethosu_inference_resend;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200277
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200278 /* Get pointer to IFM buffers */
279 for (i = 0; i < uapi->ifm_count; i++) {
280 inf->ifm[i] = ethosu_buffer_get_from_fd(uapi->ifm_fd[i]);
281 if (IS_ERR(inf->ifm[i])) {
282 ret = PTR_ERR(inf->ifm[i]);
283 goto put_ifm;
284 }
285
286 inf->ifm_count++;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200287 }
288
289 /* Get pointer to OFM buffer */
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200290 for (i = 0; i < uapi->ofm_count; i++) {
291 inf->ofm[i] = ethosu_buffer_get_from_fd(uapi->ofm_fd[i]);
292 if (IS_ERR(inf->ofm[i])) {
293 ret = PTR_ERR(inf->ofm[i]);
294 goto put_ofm;
295 }
296
297 inf->ofm_count++;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200298 }
299
Per Åstrandf7e407a2020-10-23 21:25:05 +0200300 /* Configure PMU and cycle counter */
301 dev_info(inf->edev->dev,
302 "Configuring events for PMU. events=[%u, %u, %u, %u]\n",
303 uapi->pmu_config.events[0], uapi->pmu_config.events[1],
304 uapi->pmu_config.events[2], uapi->pmu_config.events[3]);
305
306 /* Configure events and reset count for all events */
307 for (i = 0; i < ETHOSU_PMU_EVENT_MAX; i++) {
308 inf->pmu_event_config[i] = uapi->pmu_config.events[i];
309 inf->pmu_event_count[i] = 0;
310 }
311
312 if (uapi->pmu_config.cycle_count)
313 dev_info(inf->edev->dev, "Enabling cycle counter\n");
314
315 /* Configure cycle counter and reset any previous count */
316 inf->pmu_cycle_counter_enable = uapi->pmu_config.cycle_count;
317 inf->pmu_cycle_counter_count = 0;
318
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200319 /* Increment network reference count */
320 ethosu_network_get(net);
321
322 /* Create file descriptor */
323 ret = fd = anon_inode_getfd("ethosu-inference", &ethosu_inference_fops,
324 inf, O_RDWR | O_CLOEXEC);
325 if (ret < 0)
326 goto put_net;
327
328 /* Store pointer to file structure */
329 inf->file = fget(ret);
330 fput(inf->file);
331
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100332 /* Add inference to pending list */
333 list_add(&inf->msg.list, &edev->mailbox.pending_list);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200334
335 /* Send inference request to Arm Ethos-U subsystem */
336 (void)ethosu_inference_send(inf);
337
338 dev_info(edev->dev, "Inference create. handle=0x%pK, fd=%d",
339 inf, fd);
340
341 return fd;
342
343put_net:
344 ethosu_network_put(inf->net);
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200345
346put_ofm:
347 while (inf->ofm_count-- > 0)
348 ethosu_buffer_put(inf->ofm[inf->ofm_count]);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200349
350put_ifm:
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200351 while (inf->ifm_count-- > 0)
352 ethosu_buffer_put(inf->ifm[inf->ifm_count]);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200353
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200354 devm_kfree(edev->dev, inf);
355
356 return ret;
357}
358
359struct ethosu_inference *ethosu_inference_get_from_fd(int fd)
360{
361 struct ethosu_inference *inf;
362 struct file *file;
363
364 file = fget(fd);
365 if (!file)
366 return ERR_PTR(-EINVAL);
367
368 if (!ethosu_inference_verify(file)) {
369 fput(file);
370
371 return ERR_PTR(-EINVAL);
372 }
373
374 inf = file->private_data;
375 ethosu_inference_get(inf);
376 fput(file);
377
378 return inf;
379}
380
381void ethosu_inference_get(struct ethosu_inference *inf)
382{
383 kref_get(&inf->kref);
384}
385
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100386int ethosu_inference_put(struct ethosu_inference *inf)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200387{
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100388 return kref_put(&inf->kref, &ethosu_inference_kref_destroy);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200389}
390
391void ethosu_inference_rsp(struct ethosu_device *edev,
392 struct ethosu_core_inference_rsp *rsp)
393{
394 struct ethosu_inference *inf =
395 (struct ethosu_inference *)rsp->user_arg;
396 int ret;
Per Åstrandf7e407a2020-10-23 21:25:05 +0200397 int i;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200398
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100399 ret = ethosu_mailbox_find(&edev->mailbox, &inf->msg);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200400 if (ret) {
401 dev_warn(edev->dev,
402 "Handle not found in inference list. handle=0x%p\n",
403 rsp);
404
405 return;
406 }
407
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200408 if (rsp->status == ETHOSU_CORE_STATUS_OK &&
409 inf->ofm_count <= ETHOSU_CORE_BUFFER_MAX) {
410 uint32_t i;
411
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200412 inf->status = ETHOSU_UAPI_STATUS_OK;
413
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200414 for (i = 0; i < inf->ofm_count; i++) {
415 struct ethosu_buffer *ofm = inf->ofm[i];
416
417 ret = ethosu_buffer_resize(
418 ofm, ofm->size + rsp->ofm_size[i],
419 ofm->offset);
420 if (ret)
421 inf->status = ETHOSU_UAPI_STATUS_ERROR;
422 }
Davide Grohmann82d22582022-04-25 12:52:38 +0200423 } else if (rsp->status == ETHOSU_CORE_STATUS_REJECTED) {
424 inf->status = ETHOSU_UAPI_STATUS_REJECTED;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200425 } else {
426 inf->status = ETHOSU_UAPI_STATUS_ERROR;
427 }
428
Davide Grohmann82d22582022-04-25 12:52:38 +0200429 if (inf->status == ETHOSU_UAPI_STATUS_OK) {
430 for (i = 0; i < ETHOSU_CORE_PMU_MAX; i++) {
431 inf->pmu_event_config[i] = rsp->pmu_event_config[i];
432 inf->pmu_event_count[i] = rsp->pmu_event_count[i];
433 }
434
435 inf->pmu_cycle_counter_enable = rsp->pmu_cycle_counter_enable;
436 inf->pmu_cycle_counter_count = rsp->pmu_cycle_counter_count;
437
438 dev_info(edev->dev,
439 "PMU events. config=[%u, %u, %u, %u], count=[%u, %u, %u, %u]\n",
440 inf->pmu_event_config[0], inf->pmu_event_config[1],
441 inf->pmu_event_config[2], inf->pmu_event_config[3],
442 inf->pmu_event_count[0], inf->pmu_event_count[1],
443 inf->pmu_event_count[2], inf->pmu_event_count[3]);
444
445 dev_info(edev->dev,
446 "PMU cycle counter. enable=%u, count=%llu\n",
447 inf->pmu_cycle_counter_enable,
448 inf->pmu_cycle_counter_count);
Per Åstrandf7e407a2020-10-23 21:25:05 +0200449 }
450
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100451 inf->done = true;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200452 wake_up_interruptible(&inf->waitq);
453
454 ethosu_inference_put(inf);
455}