blob: 3c18bbdcd6d17c00ad405a43f6099c19be012138 [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 }
73 default: {
74 return "Unknown";
75 }
76 }
77}
78
79static int ethosu_inference_send(struct ethosu_inference *inf)
80{
81 int ret;
82
Kristofer Jonsson116a6352020-08-20 17:25:23 +020083 inf->status = ETHOSU_UAPI_STATUS_ERROR;
84
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020085 ret = ethosu_mailbox_inference(&inf->edev->mailbox, inf,
86 inf->ifm_count, inf->ifm,
87 inf->ofm_count, inf->ofm,
Per Åstrandf7e407a2020-10-23 21:25:05 +020088 inf->net->buf,
Kristofer Jonsson35de9e62022-03-08 13:25:45 +010089 inf->net->index,
Per Åstrandf7e407a2020-10-23 21:25:05 +020090 inf->pmu_event_config,
91 ETHOSU_PMU_EVENT_MAX,
92 inf->pmu_cycle_counter_enable);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020093 if (ret)
94 return ret;
95
Kristofer Jonsson116a6352020-08-20 17:25:23 +020096 ethosu_inference_get(inf);
97
98 return 0;
99}
100
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100101static void ethosu_inference_fail(struct ethosu_mailbox_msg *msg)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200102{
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100103 struct ethosu_inference *inf =
104 container_of(msg, typeof(*inf), msg);
105 int ret;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200106
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100107 /* Decrement reference count if inference was pending reponse */
108 if (!inf->done) {
109 ret = ethosu_inference_put(inf);
110 if (ret)
111 return;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200112 }
113
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100114 /* Fail inference and wake up any waiting process */
115 inf->status = ETHOSU_UAPI_STATUS_ERROR;
116 inf->done = true;
117 wake_up_interruptible(&inf->waitq);
118}
119
120static int ethosu_inference_resend(struct ethosu_mailbox_msg *msg)
121{
122 struct ethosu_inference *inf =
123 container_of(msg, typeof(*inf), msg);
124 int ret;
125
126 /* Don't resend request if response has already been received */
127 if (inf->done)
128 return 0;
129
130 /* Decrement reference count for pending request */
131 ret = ethosu_inference_put(inf);
132 if (ret)
133 return 0;
134
135 /* Resend request */
136 ret = ethosu_inference_send(inf);
137 if (ret)
138 return ret;
139
140 return 0;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200141}
142
143static bool ethosu_inference_verify(struct file *file)
144{
145 return file->f_op == &ethosu_inference_fops;
146}
147
148static void ethosu_inference_kref_destroy(struct kref *kref)
149{
150 struct ethosu_inference *inf =
151 container_of(kref, struct ethosu_inference, kref);
152
153 dev_info(inf->edev->dev,
154 "Inference destroy. handle=0x%pK, status=%d\n",
155 inf, inf->status);
156
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100157 list_del(&inf->msg.list);
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);
166 devm_kfree(inf->edev->dev, inf);
167}
168
169static int ethosu_inference_release(struct inode *inode,
170 struct file *file)
171{
172 struct ethosu_inference *inf = file->private_data;
173
174 dev_info(inf->edev->dev,
175 "Inference release. handle=0x%pK, status=%d\n",
176 inf, inf->status);
177
178 ethosu_inference_put(inf);
179
180 return 0;
181}
182
183static unsigned int ethosu_inference_poll(struct file *file,
184 poll_table *wait)
185{
186 struct ethosu_inference *inf = file->private_data;
187 int ret = 0;
188
189 poll_wait(file, &inf->waitq, wait);
190
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100191 if (inf->done)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200192 ret |= POLLIN;
193
194 return ret;
195}
196
197static long ethosu_inference_ioctl(struct file *file,
198 unsigned int cmd,
199 unsigned long arg)
200{
201 struct ethosu_inference *inf = file->private_data;
Per Åstrandf7e407a2020-10-23 21:25:05 +0200202 void __user *udata = (void __user *)arg;
203 int ret;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200204
205 ret = mutex_lock_interruptible(&inf->edev->mutex);
206 if (ret)
207 return ret;
208
209 dev_info(inf->edev->dev, "Ioctl: cmd=%u, arg=%lu\n", cmd, arg);
210
211 switch (cmd) {
212 case ETHOSU_IOCTL_INFERENCE_STATUS: {
Per Åstrandf7e407a2020-10-23 21:25:05 +0200213 struct ethosu_uapi_result_status uapi;
214 int i;
215
216 uapi.status = inf->status;
217
218 for (i = 0; i < ETHOSU_PMU_EVENT_MAX; i++) {
219 uapi.pmu_config.events[i] =
220 inf->pmu_event_config[i];
221 uapi.pmu_count.events[i] =
222 inf->pmu_event_count[i];
223 }
224
225 uapi.pmu_config.cycle_count = inf->pmu_cycle_counter_enable;
226 uapi.pmu_count.cycle_count = inf->pmu_cycle_counter_count;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200227
228 dev_info(inf->edev->dev,
229 "Ioctl: Inference status. status=%s (%d)\n",
Per Åstrandf7e407a2020-10-23 21:25:05 +0200230 status_to_string(uapi.status), uapi.status);
231
232 ret = copy_to_user(udata, &uapi, sizeof(uapi)) ? -EFAULT : 0;
233
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200234 break;
235 }
236 default: {
237 dev_err(inf->edev->dev, "Invalid ioctl. cmd=%u, arg=%lu",
238 cmd, arg);
239 break;
240 }
241 }
242
243 mutex_unlock(&inf->edev->mutex);
244
245 return ret;
246}
247
248int ethosu_inference_create(struct ethosu_device *edev,
249 struct ethosu_network *net,
250 struct ethosu_uapi_inference_create *uapi)
251{
252 struct ethosu_inference *inf;
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200253 uint32_t i;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200254 int fd;
255 int ret = -ENOMEM;
256
257 inf = devm_kzalloc(edev->dev, sizeof(*inf), GFP_KERNEL);
258 if (!inf)
259 return -ENOMEM;
260
261 inf->edev = edev;
262 inf->net = net;
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100263 inf->done = false;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200264 inf->status = ETHOSU_UAPI_STATUS_ERROR;
265 kref_init(&inf->kref);
266 init_waitqueue_head(&inf->waitq);
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100267 inf->msg.fail = ethosu_inference_fail;
268 inf->msg.resend = ethosu_inference_resend;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200269
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200270 /* Get pointer to IFM buffers */
271 for (i = 0; i < uapi->ifm_count; i++) {
272 inf->ifm[i] = ethosu_buffer_get_from_fd(uapi->ifm_fd[i]);
273 if (IS_ERR(inf->ifm[i])) {
274 ret = PTR_ERR(inf->ifm[i]);
275 goto put_ifm;
276 }
277
278 inf->ifm_count++;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200279 }
280
281 /* Get pointer to OFM buffer */
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200282 for (i = 0; i < uapi->ofm_count; i++) {
283 inf->ofm[i] = ethosu_buffer_get_from_fd(uapi->ofm_fd[i]);
284 if (IS_ERR(inf->ofm[i])) {
285 ret = PTR_ERR(inf->ofm[i]);
286 goto put_ofm;
287 }
288
289 inf->ofm_count++;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200290 }
291
Per Åstrandf7e407a2020-10-23 21:25:05 +0200292 /* Configure PMU and cycle counter */
293 dev_info(inf->edev->dev,
294 "Configuring events for PMU. events=[%u, %u, %u, %u]\n",
295 uapi->pmu_config.events[0], uapi->pmu_config.events[1],
296 uapi->pmu_config.events[2], uapi->pmu_config.events[3]);
297
298 /* Configure events and reset count for all events */
299 for (i = 0; i < ETHOSU_PMU_EVENT_MAX; i++) {
300 inf->pmu_event_config[i] = uapi->pmu_config.events[i];
301 inf->pmu_event_count[i] = 0;
302 }
303
304 if (uapi->pmu_config.cycle_count)
305 dev_info(inf->edev->dev, "Enabling cycle counter\n");
306
307 /* Configure cycle counter and reset any previous count */
308 inf->pmu_cycle_counter_enable = uapi->pmu_config.cycle_count;
309 inf->pmu_cycle_counter_count = 0;
310
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200311 /* Increment network reference count */
312 ethosu_network_get(net);
313
314 /* Create file descriptor */
315 ret = fd = anon_inode_getfd("ethosu-inference", &ethosu_inference_fops,
316 inf, O_RDWR | O_CLOEXEC);
317 if (ret < 0)
318 goto put_net;
319
320 /* Store pointer to file structure */
321 inf->file = fget(ret);
322 fput(inf->file);
323
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100324 /* Add inference to pending list */
325 list_add(&inf->msg.list, &edev->mailbox.pending_list);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200326
327 /* Send inference request to Arm Ethos-U subsystem */
328 (void)ethosu_inference_send(inf);
329
330 dev_info(edev->dev, "Inference create. handle=0x%pK, fd=%d",
331 inf, fd);
332
333 return fd;
334
335put_net:
336 ethosu_network_put(inf->net);
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200337
338put_ofm:
339 while (inf->ofm_count-- > 0)
340 ethosu_buffer_put(inf->ofm[inf->ofm_count]);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200341
342put_ifm:
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200343 while (inf->ifm_count-- > 0)
344 ethosu_buffer_put(inf->ifm[inf->ifm_count]);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200345
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200346 devm_kfree(edev->dev, inf);
347
348 return ret;
349}
350
351struct ethosu_inference *ethosu_inference_get_from_fd(int fd)
352{
353 struct ethosu_inference *inf;
354 struct file *file;
355
356 file = fget(fd);
357 if (!file)
358 return ERR_PTR(-EINVAL);
359
360 if (!ethosu_inference_verify(file)) {
361 fput(file);
362
363 return ERR_PTR(-EINVAL);
364 }
365
366 inf = file->private_data;
367 ethosu_inference_get(inf);
368 fput(file);
369
370 return inf;
371}
372
373void ethosu_inference_get(struct ethosu_inference *inf)
374{
375 kref_get(&inf->kref);
376}
377
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100378int ethosu_inference_put(struct ethosu_inference *inf)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200379{
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100380 return kref_put(&inf->kref, &ethosu_inference_kref_destroy);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200381}
382
383void ethosu_inference_rsp(struct ethosu_device *edev,
384 struct ethosu_core_inference_rsp *rsp)
385{
386 struct ethosu_inference *inf =
387 (struct ethosu_inference *)rsp->user_arg;
388 int ret;
Per Åstrandf7e407a2020-10-23 21:25:05 +0200389 int i;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200390
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100391 ret = ethosu_mailbox_find(&edev->mailbox, &inf->msg);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200392 if (ret) {
393 dev_warn(edev->dev,
394 "Handle not found in inference list. handle=0x%p\n",
395 rsp);
396
397 return;
398 }
399
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200400 if (rsp->status == ETHOSU_CORE_STATUS_OK &&
401 inf->ofm_count <= ETHOSU_CORE_BUFFER_MAX) {
402 uint32_t i;
403
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200404 inf->status = ETHOSU_UAPI_STATUS_OK;
405
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200406 for (i = 0; i < inf->ofm_count; i++) {
407 struct ethosu_buffer *ofm = inf->ofm[i];
408
409 ret = ethosu_buffer_resize(
410 ofm, ofm->size + rsp->ofm_size[i],
411 ofm->offset);
412 if (ret)
413 inf->status = ETHOSU_UAPI_STATUS_ERROR;
414 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200415 } else {
416 inf->status = ETHOSU_UAPI_STATUS_ERROR;
417 }
418
Per Åstrandf7e407a2020-10-23 21:25:05 +0200419 for (i = 0; i < ETHOSU_CORE_PMU_MAX; i++) {
420 inf->pmu_event_config[i] = rsp->pmu_event_config[i];
421 inf->pmu_event_count[i] = rsp->pmu_event_count[i];
422 }
423
424 inf->pmu_cycle_counter_enable = rsp->pmu_cycle_counter_enable;
425 inf->pmu_cycle_counter_count = rsp->pmu_cycle_counter_count;
426
427 dev_info(edev->dev,
Jonny Svärd9d8d92c2020-12-10 11:33:19 +0100428 "PMU events. config=[%u, %u, %u, %u], count=[%u, %u, %u, %u]\n",
429 inf->pmu_event_config[0], inf->pmu_event_config[1],
430 inf->pmu_event_config[2], inf->pmu_event_config[3],
431 inf->pmu_event_count[0], inf->pmu_event_count[1],
432 inf->pmu_event_count[2], inf->pmu_event_count[3]);
Per Åstrandf7e407a2020-10-23 21:25:05 +0200433
434 dev_info(edev->dev,
435 "PMU cycle counter. enable=%u, count=%llu\n",
436 inf->pmu_cycle_counter_enable,
437 inf->pmu_cycle_counter_count);
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100438
439 inf->done = true;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200440 wake_up_interruptible(&inf->waitq);
441
442 ethosu_inference_put(inf);
443}