blob: 3e7284b662483a64afa2ac54211a6e85ccf36267 [file] [log] [blame]
Kristofer Jonsson116a6352020-08-20 17:25:23 +02001/*
Kristofer Jonssonb42bc0b2023-01-04 17:09:28 +01002 * Copyright 2020-2023 Arm Limited and/or its affiliates
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"
Kristofer Jonssond779a082023-01-04 17:09:47 +010028#include "ethosu_core_rpmsg.h"
Kristofer Jonsson116a6352020-08-20 17:25:23 +020029#include "ethosu_device.h"
30
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010031#include <linux/jiffies.h>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020032#include <linux/resource.h>
33#include <linux/uio.h>
34
35/****************************************************************************
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010036 * Includes
37 ****************************************************************************/
38
39#ifndef fallthrough
40#if __has_attribute(__fallthrough__)
41#define fallthrough __attribute__((__fallthrough__))
42#else
43#define fallthrough do {} while (0) /* fallthrough */
44#endif
45#endif
46
47/****************************************************************************
Kristofer Jonsson116a6352020-08-20 17:25:23 +020048 * Functions
49 ****************************************************************************/
50
51static void ethosu_core_set_size(struct ethosu_buffer *buf,
52 struct ethosu_core_buffer *cbuf)
53{
54 cbuf->ptr = (uint32_t)buf->dma_addr + buf->offset;
55 cbuf->size = (uint32_t)buf->size;
56}
57
58static void ethosu_core_set_capacity(struct ethosu_buffer *buf,
59 struct ethosu_core_buffer *cbuf)
60{
61 cbuf->ptr = (uint32_t)buf->dma_addr + buf->offset + buf->size;
62 cbuf->size = (uint32_t)buf->capacity - buf->offset - buf->size;
63}
64
Davide Grohmann32660f92022-04-27 16:49:07 +020065int ethosu_mailbox_register(struct ethosu_mailbox *mbox,
66 struct ethosu_mailbox_msg *msg)
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010067{
Davide Grohmann32660f92022-04-27 16:49:07 +020068 msg->id = idr_alloc_cyclic(&mbox->msg_idr, msg, 0, INT_MAX, GFP_KERNEL);
69 if (msg->id < 0)
70 return msg->id;
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010071
Davide Grohmann32660f92022-04-27 16:49:07 +020072 return 0;
73}
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010074
Davide Grohmann32660f92022-04-27 16:49:07 +020075void ethosu_mailbox_deregister(struct ethosu_mailbox *mbox,
76 struct ethosu_mailbox_msg *msg)
77{
78 idr_remove(&mbox->msg_idr, msg->id);
79}
80
81struct ethosu_mailbox_msg *ethosu_mailbox_find(struct ethosu_mailbox *mbox,
Mikael Olsson09965b02023-06-13 12:17:04 +020082 int msg_id,
83 uint32_t msg_type)
Davide Grohmann32660f92022-04-27 16:49:07 +020084{
85 struct ethosu_mailbox_msg *ptr = (struct ethosu_mailbox_msg *)idr_find(
86 &mbox->msg_idr, msg_id);
87
88 if (ptr == NULL)
Mikael Olsson09965b02023-06-13 12:17:04 +020089 return ERR_PTR(-ENOENT);
90
91 if (ptr->type != msg_type)
Davide Grohmann32660f92022-04-27 16:49:07 +020092 return ERR_PTR(-EINVAL);
93
94 return ptr;
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010095}
96
97void ethosu_mailbox_fail(struct ethosu_mailbox *mbox)
98{
Davide Grohmann32660f92022-04-27 16:49:07 +020099 struct ethosu_mailbox_msg *cur;
100 int id;
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100101
Davide Grohmann32660f92022-04-27 16:49:07 +0200102 idr_for_each_entry(&mbox->msg_idr, cur, id) {
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100103 cur->fail(cur);
104 }
105}
106
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200107int ethosu_mailbox_ping(struct ethosu_mailbox *mbox)
108{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100109 struct ethosu_core_rpmsg rpmsg = {
110 .header = {
111 .magic = ETHOSU_CORE_MSG_MAGIC,
112 .type = ETHOSU_CORE_MSG_PING,
113 }
114 };
115
116 return rpmsg_send(mbox->ept, &rpmsg, sizeof(rpmsg.header));
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200117}
118
Jonny Svärd7c24c772021-01-14 19:53:17 +0100119int ethosu_mailbox_pong(struct ethosu_mailbox *mbox)
120{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100121 struct ethosu_core_rpmsg rpmsg = {
122 .header = {
123 .magic = ETHOSU_CORE_MSG_MAGIC,
124 .type = ETHOSU_CORE_MSG_PONG,
125 }
126 };
127
128 return rpmsg_send(mbox->ept, &rpmsg, sizeof(rpmsg.header));
Jonny Svärd7c24c772021-01-14 19:53:17 +0100129}
130
131int ethosu_mailbox_version_request(struct ethosu_mailbox *mbox)
132{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100133 struct ethosu_core_rpmsg rpmsg = {
134 .header = {
135 .magic = ETHOSU_CORE_MSG_MAGIC,
136 .type = ETHOSU_CORE_MSG_VERSION_REQ,
137 }
138 };
139
140 return rpmsg_send(mbox->ept, &rpmsg, sizeof(rpmsg.header));
Jonny Svärd7c24c772021-01-14 19:53:17 +0100141}
142
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200143int ethosu_mailbox_capabilities_request(struct ethosu_mailbox *mbox,
Davide Grohmann32660f92022-04-27 16:49:07 +0200144 struct ethosu_mailbox_msg *msg)
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200145{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100146 struct ethosu_core_rpmsg rpmsg = {
147 .header = {
148 .magic = ETHOSU_CORE_MSG_MAGIC,
149 .type = ETHOSU_CORE_MSG_CAPABILITIES_REQ,
150 .msg_id = msg->id
151 }
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200152 };
153
Mikael Olsson09965b02023-06-13 12:17:04 +0200154 msg->type = rpmsg.header.type;
155
Kristofer Jonssond779a082023-01-04 17:09:47 +0100156 return rpmsg_send(mbox->ept, &rpmsg, sizeof(rpmsg.header));
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200157}
158
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200159int ethosu_mailbox_inference(struct ethosu_mailbox *mbox,
Davide Grohmann32660f92022-04-27 16:49:07 +0200160 struct ethosu_mailbox_msg *msg,
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200161 uint32_t ifm_count,
162 struct ethosu_buffer **ifm,
163 uint32_t ofm_count,
164 struct ethosu_buffer **ofm,
Per Åstrandf7e407a2020-10-23 21:25:05 +0200165 struct ethosu_buffer *network,
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100166 uint32_t network_index,
Per Åstrandf7e407a2020-10-23 21:25:05 +0200167 uint8_t *pmu_event_config,
168 uint8_t pmu_event_config_count,
169 uint8_t pmu_cycle_counter_enable)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200170{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100171 struct ethosu_core_rpmsg rpmsg = {
172 .header = {
173 .magic = ETHOSU_CORE_MSG_MAGIC,
174 .type = ETHOSU_CORE_MSG_INFERENCE_REQ,
175 .msg_id = msg->id
176 }
177 };
178 struct ethosu_core_msg_inference_req *inf_req = &rpmsg.inf_req;
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200179 uint32_t i;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200180
Mikael Olsson09965b02023-06-13 12:17:04 +0200181 msg->type = rpmsg.header.type;
182
Per Åstrandf7e407a2020-10-23 21:25:05 +0200183 /* Verify that the uapi and core has the same number of pmus */
184 if (pmu_event_config_count != ETHOSU_CORE_PMU_MAX) {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100185 dev_err(mbox->dev, "PMU count misconfigured.");
Per Åstrandf7e407a2020-10-23 21:25:05 +0200186
187 return -EINVAL;
188 }
189
Kristofer Jonssond779a082023-01-04 17:09:47 +0100190 inf_req->ifm_count = ifm_count;
191 inf_req->ofm_count = ofm_count;
192 inf_req->pmu_cycle_counter_enable = pmu_cycle_counter_enable;
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200193
194 for (i = 0; i < ifm_count; i++)
Kristofer Jonssond779a082023-01-04 17:09:47 +0100195 ethosu_core_set_size(ifm[i], &inf_req->ifm[i]);
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200196
197 for (i = 0; i < ofm_count; i++)
Kristofer Jonssond779a082023-01-04 17:09:47 +0100198 ethosu_core_set_capacity(ofm[i], &inf_req->ofm[i]);
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200199
Per Åstrandf7e407a2020-10-23 21:25:05 +0200200 for (i = 0; i < ETHOSU_CORE_PMU_MAX; i++)
Kristofer Jonssond779a082023-01-04 17:09:47 +0100201 inf_req->pmu_event_config[i] = pmu_event_config[i];
Per Åstrandf7e407a2020-10-23 21:25:05 +0200202
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100203 if (network != NULL) {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100204 inf_req->network.type = ETHOSU_CORE_NETWORK_BUFFER;
205 ethosu_core_set_size(network, &inf_req->network.buffer);
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100206 } else {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100207 inf_req->network.type = ETHOSU_CORE_NETWORK_INDEX;
208 inf_req->network.index = network_index;
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100209 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200210
Kristofer Jonssond779a082023-01-04 17:09:47 +0100211 return rpmsg_send(mbox->ept, &rpmsg,
212 sizeof(rpmsg.header) + sizeof(rpmsg.inf_req));
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200213}
214
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100215int ethosu_mailbox_network_info_request(struct ethosu_mailbox *mbox,
Davide Grohmann32660f92022-04-27 16:49:07 +0200216 struct ethosu_mailbox_msg *msg,
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100217 struct ethosu_buffer *network,
218 uint32_t network_index)
219{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100220 struct ethosu_core_rpmsg rpmsg = {
221 .header = {
222 .magic = ETHOSU_CORE_MSG_MAGIC,
223 .type = ETHOSU_CORE_MSG_NETWORK_INFO_REQ,
224 .msg_id = msg->id
225 }
226 };
227 struct ethosu_core_msg_network_info_req *info_req = &rpmsg.net_info_req;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100228
Mikael Olsson09965b02023-06-13 12:17:04 +0200229 msg->type = rpmsg.header.type;
230
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100231 if (network != NULL) {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100232 info_req->network.type = ETHOSU_CORE_NETWORK_BUFFER;
233 ethosu_core_set_size(network, &info_req->network.buffer);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100234 } else {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100235 info_req->network.type = ETHOSU_CORE_NETWORK_INDEX;
236 info_req->network.index = network_index;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100237 }
238
Kristofer Jonssond779a082023-01-04 17:09:47 +0100239 return rpmsg_send(mbox->ept, &rpmsg,
240 sizeof(rpmsg.header) + sizeof(rpmsg.net_info_req));
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100241}
242
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100243int ethosu_mailbox_cancel_inference(struct ethosu_mailbox *mbox,
Davide Grohmann32660f92022-04-27 16:49:07 +0200244 struct ethosu_mailbox_msg *msg,
245 int inference_handle)
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100246{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100247 struct ethosu_core_rpmsg rpmsg = {
248 .header = {
249 .magic = ETHOSU_CORE_MSG_MAGIC,
250 .type =
251 ETHOSU_CORE_MSG_CANCEL_INFERENCE_REQ,
252 .msg_id = msg->id
253 },
254 .cancel_req = {
255 .inference_handle = inference_handle
256 }
257 };
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100258
Mikael Olsson09965b02023-06-13 12:17:04 +0200259 msg->type = rpmsg.header.type;
260
Kristofer Jonssond779a082023-01-04 17:09:47 +0100261 return rpmsg_send(mbox->ept, &rpmsg,
262 sizeof(rpmsg.header) + sizeof(rpmsg.cancel_req));
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200263}
264
265int ethosu_mailbox_init(struct ethosu_mailbox *mbox,
266 struct device *dev,
Kristofer Jonssond779a082023-01-04 17:09:47 +0100267 struct rpmsg_endpoint *ept)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200268{
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200269 mbox->dev = dev;
Kristofer Jonssond779a082023-01-04 17:09:47 +0100270 mbox->ept = ept;
Davide Grohmann32660f92022-04-27 16:49:07 +0200271 idr_init(&mbox->msg_idr);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200272
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200273 return 0;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200274}
275
276void ethosu_mailbox_deinit(struct ethosu_mailbox *mbox)
Kristofer Jonssond779a082023-01-04 17:09:47 +0100277{}