blob: 5343e56870464a050a72b53f39641ffbb1be7e03 [file] [log] [blame]
Kristofer Jonsson116a6352020-08-20 17:25:23 +02001/*
Kristofer Jonsson35de9e62022-03-08 13:25:45 +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_mailbox.h"
26
27#include "ethosu_buffer.h"
28#include "ethosu_core_interface.h"
29#include "ethosu_device.h"
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010030#include "ethosu_watchdog.h"
Kristofer Jonsson116a6352020-08-20 17:25:23 +020031
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010032#include <linux/jiffies.h>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020033#include <linux/resource.h>
34#include <linux/uio.h>
35
36/****************************************************************************
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010037 * Includes
38 ****************************************************************************/
39
40#ifndef fallthrough
41#if __has_attribute(__fallthrough__)
42#define fallthrough __attribute__((__fallthrough__))
43#else
44#define fallthrough do {} while (0) /* fallthrough */
45#endif
46#endif
47
48/****************************************************************************
Kristofer Jonsson116a6352020-08-20 17:25:23 +020049 * Functions
50 ****************************************************************************/
51
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +010052static void ethosu_wd_inc(struct ethosu_mailbox *mbox,
53 enum ethosu_core_msg_type type)
54{
55 switch (type) {
56 case ETHOSU_CORE_MSG_PING:
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010057 mbox->ping_count++;
58 fallthrough;
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +010059 case ETHOSU_CORE_MSG_INFERENCE_REQ:
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +010060 ethosu_watchdog_inc(mbox->wdog);
61 break;
62 default:
63 break;
64 }
65}
66
67static void ethosu_wd_dec(struct ethosu_mailbox *mbox,
68 enum ethosu_core_msg_type type)
69{
70 switch (type) {
71 case ETHOSU_CORE_MSG_PONG:
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010072 mbox->ping_count--;
73 fallthrough;
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +010074 case ETHOSU_CORE_MSG_INFERENCE_RSP:
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +010075 ethosu_watchdog_dec(mbox->wdog);
76 break;
77 default:
78 break;
79 }
80}
81
Kristofer Jonsson116a6352020-08-20 17:25:23 +020082static void ethosu_core_set_size(struct ethosu_buffer *buf,
83 struct ethosu_core_buffer *cbuf)
84{
85 cbuf->ptr = (uint32_t)buf->dma_addr + buf->offset;
86 cbuf->size = (uint32_t)buf->size;
87}
88
89static void ethosu_core_set_capacity(struct ethosu_buffer *buf,
90 struct ethosu_core_buffer *cbuf)
91{
92 cbuf->ptr = (uint32_t)buf->dma_addr + buf->offset + buf->size;
93 cbuf->size = (uint32_t)buf->capacity - buf->offset - buf->size;
94}
95
96static size_t ethosu_queue_available(struct ethosu_core_queue *queue)
97{
98 size_t size = queue->header.write - queue->header.read;
99
100 if (queue->header.read > queue->header.write)
101 size += queue->header.size;
102
103 return size;
104}
105
106static size_t ethosu_queue_capacity(struct ethosu_core_queue *queue)
107{
108 return queue->header.size - ethosu_queue_available(queue);
109}
110
111static int ethosu_queue_write(struct ethosu_mailbox *mbox,
112 const struct kvec *vec,
113 size_t length)
114{
115 struct ethosu_core_queue *queue = mbox->in_queue;
116 uint8_t *dst = &queue->data[0];
117 uint32_t wpos = queue->header.write;
118 size_t total_size;
119 size_t i;
120 int ret;
121
122 for (i = 0, total_size = 0; i < length; i++)
123 total_size += vec[i].iov_len;
124
125 if (total_size > ethosu_queue_capacity(queue))
126 return -EINVAL;
127
128 for (i = 0; i < length; i++) {
129 const uint8_t *src = vec[i].iov_base;
130 const uint8_t *end = src + vec[i].iov_len;
131
132 while (src < end) {
133 dst[wpos] = *src++;
134 wpos = (wpos + 1) % queue->header.size;
135 }
136 }
137
138 queue->header.write = wpos;
139
140 ret = mbox_send_message(mbox->tx, queue);
141 if (ret < 0)
142 return ret;
143
144 return 0;
145}
146
147static int ethosu_queue_write_msg(struct ethosu_mailbox *mbox,
148 uint32_t type,
149 void *data,
150 size_t length)
151{
Jonny Svärd7c24c772021-01-14 19:53:17 +0100152 struct ethosu_core_msg msg = {
153 .magic = ETHOSU_CORE_MSG_MAGIC,
154 .type = type, .length= length
155 };
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200156 const struct kvec vec[2] = {
157 { &msg, sizeof(msg) },
158 { data, length }
159 };
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +0100160 int ret;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200161
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +0100162 ret = ethosu_queue_write(mbox, vec, 2);
163 if (ret)
164 return ret;
165
166 ethosu_wd_inc(mbox, type);
167
168 return 0;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200169}
170
171static int ethosu_queue_read(struct ethosu_mailbox *mbox,
172 void *data,
173 size_t length)
174{
175 struct ethosu_core_queue *queue = mbox->out_queue;
176 uint8_t *src = &queue->data[0];
177 uint8_t *dst = (uint8_t *)data;
178 const uint8_t *end = dst + length;
179 uint32_t rpos = queue->header.read;
Jonny Svärd7c24c772021-01-14 19:53:17 +0100180 size_t queue_avail = ethosu_queue_available(queue);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200181
Davide Grohmann1c26baa2021-06-15 13:21:15 +0200182 if (length == 0)
183 return 0;
184 else if (queue_avail == 0)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200185 return -ENOMSG;
Jonny Svärd7c24c772021-01-14 19:53:17 +0100186 else if (length > queue_avail)
187 return -EBADMSG;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200188
189 while (dst < end) {
190 *dst++ = src[rpos];
191 rpos = (rpos + 1) % queue->header.size;
192 }
193
194 queue->header.read = rpos;
195
196 return 0;
197}
198
Jonny Svärd7c24c772021-01-14 19:53:17 +0100199void ethosu_mailbox_reset(struct ethosu_mailbox *mbox)
200{
201 mbox->out_queue->header.read = mbox->out_queue->header.write;
202}
203
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100204void ethosu_mailbox_wait_prepare(struct ethosu_mailbox *mbox)
205{
206 mbox->in_queue->header.size = 0;
207 mbox->in_queue->header.read = 0xffffff;
208 mbox->in_queue->header.write = 0xffffff;
209}
210
211int ethosu_mailbox_wait_firmware(struct ethosu_mailbox *mbox)
212{
213 const unsigned long timeout = 1000;
214 const unsigned long end = jiffies + msecs_to_jiffies(timeout);
215 volatile struct ethosu_core_queue_header *hdr =
216 &mbox->in_queue->header;
217 int ret = -ETIMEDOUT;
218
219 /* Spin wait on mailbox initialization */
220 while ((end - jiffies) < timeout)
221 if (hdr->size != 0 &&
222 hdr->read != 0xffffff &&
223 hdr->write != 0xffffff) {
224 ret = 0;
225 break;
226 }
227
228 dev_info(mbox->dev, "mbox: Wait. ret=%d, size=%u, read=%u, write=%u",
229 ret, hdr->size, hdr->read, hdr->write);
230
231 return ret;
232}
233
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200234int ethosu_mailbox_read(struct ethosu_mailbox *mbox,
235 struct ethosu_core_msg *header,
236 void *data,
237 size_t length)
238{
239 int ret;
240
Jonny Svärd7c24c772021-01-14 19:53:17 +0100241 /* Read message header magic */
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200242 ret = ethosu_queue_read(mbox, header, sizeof(*header));
Jonny Svärd7c24c772021-01-14 19:53:17 +0100243 if (ret) {
244 if (ret != -ENOMSG)
245 dev_warn(mbox->dev,
246 "Msg: Failed to read message header\n");
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200247
Jonny Svärd7c24c772021-01-14 19:53:17 +0100248 return ret;
249 }
250
251 if (header->magic != ETHOSU_CORE_MSG_MAGIC) {
252 dev_warn(mbox->dev,
253 "Msg: Invalid magic. Got: %08X but expected %08X\n",
254 header->magic, ETHOSU_CORE_MSG_MAGIC);
255
256 return -EINVAL;
257 }
258
259 dev_info(mbox->dev,
260 "mbox: Read msg header. magic=%08X, type=%u, length=%u",
261 header->magic, header->type, header->length);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200262
263 /* Check that payload is not larger than allocated buffer */
Jonny Svärd7c24c772021-01-14 19:53:17 +0100264 if (header->length > length) {
265 dev_warn(mbox->dev,
266 "Msg: Buffer size (%zu) too small for message (%u)\n",
267 sizeof(data), header->length);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200268
Jonny Svärd7c24c772021-01-14 19:53:17 +0100269 return -ENOMEM;
270 }
271
272 /* Read payload data */
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200273 ret = ethosu_queue_read(mbox, data, header->length);
Jonny Svärd7c24c772021-01-14 19:53:17 +0100274 if (ret) {
275 dev_warn(mbox->dev, "Msg: Failed to read payload data\n");
276
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200277 return -EBADMSG;
Jonny Svärd7c24c772021-01-14 19:53:17 +0100278 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200279
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +0100280 ethosu_wd_dec(mbox, header->type);
281
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200282 return 0;
283}
284
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100285int ethosu_mailbox_find(struct ethosu_mailbox *mbox,
286 struct ethosu_mailbox_msg *msg)
287{
288 struct ethosu_mailbox_msg *cur;
289
290 list_for_each_entry(cur, &mbox->pending_list, list) {
291 if (cur == msg)
292 return 0;
293 }
294
295 return -EINVAL;
296}
297
298void ethosu_mailbox_fail(struct ethosu_mailbox *mbox)
299{
300 struct ethosu_mailbox_msg *cur, *cur_tmp;
301
302 list_for_each_entry_safe(cur, cur_tmp, &mbox->pending_list, list) {
303 cur->fail(cur);
304 }
305}
306
307int ethosu_mailbox_resend(struct ethosu_mailbox *mbox)
308{
309 struct ethosu_mailbox_msg *cur, *cur_tmp;
310 int ret;
311
312 list_for_each_entry_safe(cur, cur_tmp, &mbox->pending_list, list) {
313 ret = cur->resend(cur);
314 if (ret) {
315 cur->fail(cur);
316
317 return ret;
318 }
319 }
320
321 return 0;
322}
323
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200324int ethosu_mailbox_ping(struct ethosu_mailbox *mbox)
325{
326 return ethosu_queue_write_msg(mbox, ETHOSU_CORE_MSG_PING, NULL, 0);
327}
328
Jonny Svärd7c24c772021-01-14 19:53:17 +0100329int ethosu_mailbox_pong(struct ethosu_mailbox *mbox)
330{
331 return ethosu_queue_write_msg(mbox, ETHOSU_CORE_MSG_PONG, NULL, 0);
332}
333
334int ethosu_mailbox_version_request(struct ethosu_mailbox *mbox)
335{
336 return ethosu_queue_write_msg(mbox, ETHOSU_CORE_MSG_VERSION_REQ, NULL,
337 0);
338}
339
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200340int ethosu_mailbox_capabilities_request(struct ethosu_mailbox *mbox,
341 void *user_arg)
342{
343 struct ethosu_core_capabilities_req req = {
344 .user_arg = (ptrdiff_t)user_arg
345 };
346
347 return ethosu_queue_write_msg(mbox, ETHOSU_CORE_MSG_CAPABILITIES_REQ,
348 &req,
349 sizeof(req));
350}
351
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200352int ethosu_mailbox_inference(struct ethosu_mailbox *mbox,
353 void *user_arg,
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200354 uint32_t ifm_count,
355 struct ethosu_buffer **ifm,
356 uint32_t ofm_count,
357 struct ethosu_buffer **ofm,
Per Åstrandf7e407a2020-10-23 21:25:05 +0200358 struct ethosu_buffer *network,
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100359 uint32_t network_index,
Per Åstrandf7e407a2020-10-23 21:25:05 +0200360 uint8_t *pmu_event_config,
361 uint8_t pmu_event_config_count,
362 uint8_t pmu_cycle_counter_enable)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200363{
364 struct ethosu_core_inference_req inf;
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200365 uint32_t i;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200366
Per Åstrandf7e407a2020-10-23 21:25:05 +0200367 /* Verify that the uapi and core has the same number of pmus */
368 if (pmu_event_config_count != ETHOSU_CORE_PMU_MAX) {
369 dev_err(mbox->dev, "PMU count misconfigured.\n");
370
371 return -EINVAL;
372 }
373
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200374 inf.user_arg = (ptrdiff_t)user_arg;
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200375 inf.ifm_count = ifm_count;
376 inf.ofm_count = ofm_count;
Per Åstrandf7e407a2020-10-23 21:25:05 +0200377 inf.pmu_cycle_counter_enable = pmu_cycle_counter_enable;
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200378
379 for (i = 0; i < ifm_count; i++)
380 ethosu_core_set_size(ifm[i], &inf.ifm[i]);
381
382 for (i = 0; i < ofm_count; i++)
383 ethosu_core_set_capacity(ofm[i], &inf.ofm[i]);
384
Per Åstrandf7e407a2020-10-23 21:25:05 +0200385 for (i = 0; i < ETHOSU_CORE_PMU_MAX; i++)
386 inf.pmu_event_config[i] = pmu_event_config[i];
387
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100388 if (network != NULL) {
389 inf.network.type = ETHOSU_CORE_NETWORK_BUFFER;
390 ethosu_core_set_size(network, &inf.network.buffer);
391 } else {
392 inf.network.type = ETHOSU_CORE_NETWORK_INDEX;
393 inf.network.index = network_index;
394 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200395
396 return ethosu_queue_write_msg(mbox, ETHOSU_CORE_MSG_INFERENCE_REQ,
397 &inf, sizeof(inf));
398}
399
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100400int ethosu_mailbox_network_info_request(struct ethosu_mailbox *mbox,
401 void *user_arg,
402 struct ethosu_buffer *network,
403 uint32_t network_index)
404{
405 struct ethosu_core_network_info_req info;
406
407 info.user_arg = (ptrdiff_t)user_arg;
408
409 if (network != NULL) {
410 info.network.type = ETHOSU_CORE_NETWORK_BUFFER;
411 ethosu_core_set_size(network, &info.network.buffer);
412 } else {
413 info.network.type = ETHOSU_CORE_NETWORK_INDEX;
414 info.network.index = network_index;
415 }
416
417 return ethosu_queue_write_msg(mbox, ETHOSU_CORE_MSG_NETWORK_INFO_REQ,
418 &info, sizeof(info));
419}
420
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100421int ethosu_mailbox_cancel_inference(struct ethosu_mailbox *mbox,
422 void *user_arg,
423 void *inference_handle)
424{
425 struct ethosu_core_cancel_inference_req req;
426
427 req.user_arg = (ptrdiff_t)user_arg;
428 req.inference_handle = (ptrdiff_t)inference_handle;
429
430 return ethosu_queue_write_msg(mbox,
431 ETHOSU_CORE_MSG_CANCEL_INFERENCE_REQ,
432 &req, sizeof(req));
433}
434
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200435static void ethosu_mailbox_rx_work(struct work_struct *work)
436{
437 struct ethosu_mailbox *mbox = container_of(work, typeof(*mbox), work);
438
439 mbox->callback(mbox->user_arg);
440}
441
442static void ethosu_mailbox_rx_callback(struct mbox_client *client,
443 void *message)
444{
445 struct ethosu_mailbox *mbox =
446 container_of(client, typeof(*mbox), client);
447
448 dev_info(mbox->dev, "mbox: Received message.\n");
449
450 queue_work(mbox->wq, &mbox->work);
451}
452
453static void ethosu_mailbox_tx_done(struct mbox_client *client,
454 void *message,
455 int r)
456{
457 if (r)
458 dev_warn(client->dev, "mbox: Failed sending message (%d)\n", r);
459 else
460 dev_info(client->dev, "mbox: Message sent\n");
461}
462
463int ethosu_mailbox_init(struct ethosu_mailbox *mbox,
464 struct device *dev,
465 struct resource *in_queue,
466 struct resource *out_queue,
467 ethosu_mailbox_cb callback,
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +0100468 void *user_arg,
469 struct ethosu_watchdog *wdog)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200470{
471 int ret;
472
473 mbox->dev = dev;
474 mbox->callback = callback;
475 mbox->user_arg = user_arg;
Kristofer Jonssonf5b98c92022-03-14 16:09:12 +0100476 mbox->wdog = wdog;
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100477 mbox->ping_count = 0;
478 INIT_LIST_HEAD(&mbox->pending_list);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200479
480 mbox->client.dev = dev;
481 mbox->client.rx_callback = ethosu_mailbox_rx_callback;
482 mbox->client.tx_prepare = NULL; /* preparation of data is handled
483 * through the
484 * queue functions */
485 mbox->client.tx_done = ethosu_mailbox_tx_done;
486 mbox->client.tx_block = true;
487 mbox->client.knows_txdone = false;
488 mbox->client.tx_tout = 500;
489
490 mbox->in_queue = devm_ioremap_resource(mbox->dev, in_queue);
491 if (IS_ERR(mbox->in_queue))
492 return PTR_ERR(mbox->in_queue);
493
494 mbox->out_queue = devm_ioremap_resource(mbox->dev, out_queue);
495 if (IS_ERR(mbox->out_queue)) {
496 ret = PTR_ERR(mbox->out_queue);
497 goto unmap_in_queue;
498 }
499
500 mbox->wq = create_singlethread_workqueue("ethosu_workqueue");
501 if (!mbox->wq) {
502 dev_err(mbox->dev, "Failed to create work queue\n");
503 ret = -EINVAL;
504 goto unmap_out_queue;
505 }
506
507 INIT_WORK(&mbox->work, ethosu_mailbox_rx_work);
508
509 mbox->tx = mbox_request_channel_byname(&mbox->client, "tx");
510 if (IS_ERR(mbox->tx)) {
511 dev_warn(mbox->dev, "mbox: Failed to request tx channel\n");
512 ret = PTR_ERR(mbox->tx);
513 goto workqueue_destroy;
514 }
515
516 mbox->rx = mbox_request_channel_byname(&mbox->client, "rx");
517 if (IS_ERR(mbox->rx)) {
518 dev_info(dev, "mbox: Using same channel for RX and TX\n");
519 mbox->rx = mbox->tx;
520 }
521
522 return 0;
523
524workqueue_destroy:
525 destroy_workqueue(mbox->wq);
526
527unmap_out_queue:
528 devm_iounmap(mbox->dev, mbox->out_queue);
529
530unmap_in_queue:
531 devm_iounmap(mbox->dev, mbox->in_queue);
532
533 return ret;
534}
535
536void ethosu_mailbox_deinit(struct ethosu_mailbox *mbox)
537{
538 if (mbox->rx != mbox->tx)
539 mbox_free_channel(mbox->rx);
540
541 mbox_free_channel(mbox->tx);
542 destroy_workqueue(mbox->wq);
543 devm_iounmap(mbox->dev, mbox->out_queue);
544 devm_iounmap(mbox->dev, mbox->in_queue);
545}