blob: 4f7f5b7be17b8d7aba2a80cdfe1df846189ea7dc [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>
Mikael Olsson529cfad2023-06-14 17:14:14 +020034#include <linux/bug.h>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020035
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
52static void ethosu_core_set_size(struct ethosu_buffer *buf,
53 struct ethosu_core_buffer *cbuf)
54{
55 cbuf->ptr = (uint32_t)buf->dma_addr + buf->offset;
56 cbuf->size = (uint32_t)buf->size;
57}
58
59static void ethosu_core_set_capacity(struct ethosu_buffer *buf,
60 struct ethosu_core_buffer *cbuf)
61{
62 cbuf->ptr = (uint32_t)buf->dma_addr + buf->offset + buf->size;
63 cbuf->size = (uint32_t)buf->capacity - buf->offset - buf->size;
64}
65
Davide Grohmann32660f92022-04-27 16:49:07 +020066int ethosu_mailbox_register(struct ethosu_mailbox *mbox,
67 struct ethosu_mailbox_msg *msg)
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010068{
Mikael Olsson529cfad2023-06-14 17:14:14 +020069 WARN_ON_ONCE(!mutex_is_locked(&mbox->dev->mutex));
Davide Grohmann32660f92022-04-27 16:49:07 +020070 msg->id = idr_alloc_cyclic(&mbox->msg_idr, msg, 0, INT_MAX, GFP_KERNEL);
71 if (msg->id < 0)
72 return msg->id;
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010073
Davide Grohmann32660f92022-04-27 16:49:07 +020074 return 0;
75}
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010076
Davide Grohmann32660f92022-04-27 16:49:07 +020077void ethosu_mailbox_deregister(struct ethosu_mailbox *mbox,
78 struct ethosu_mailbox_msg *msg)
79{
Mikael Olsson529cfad2023-06-14 17:14:14 +020080 WARN_ON_ONCE(!mutex_is_locked(&mbox->dev->mutex));
Davide Grohmann32660f92022-04-27 16:49:07 +020081 idr_remove(&mbox->msg_idr, msg->id);
82}
83
84struct ethosu_mailbox_msg *ethosu_mailbox_find(struct ethosu_mailbox *mbox,
Mikael Olsson09965b02023-06-13 12:17:04 +020085 int msg_id,
86 uint32_t msg_type)
Davide Grohmann32660f92022-04-27 16:49:07 +020087{
Mikael Olsson529cfad2023-06-14 17:14:14 +020088 struct ethosu_mailbox_msg *ptr;
89
90 WARN_ON_ONCE(!mutex_is_locked(&mbox->dev->mutex));
91 ptr = (struct ethosu_mailbox_msg *)idr_find(&mbox->msg_idr, msg_id);
Davide Grohmann32660f92022-04-27 16:49:07 +020092
93 if (ptr == NULL)
Mikael Olsson09965b02023-06-13 12:17:04 +020094 return ERR_PTR(-ENOENT);
95
96 if (ptr->type != msg_type)
Davide Grohmann32660f92022-04-27 16:49:07 +020097 return ERR_PTR(-EINVAL);
98
99 return ptr;
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100100}
101
102void ethosu_mailbox_fail(struct ethosu_mailbox *mbox)
103{
Davide Grohmann32660f92022-04-27 16:49:07 +0200104 struct ethosu_mailbox_msg *cur;
105 int id;
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100106
Mikael Olsson529cfad2023-06-14 17:14:14 +0200107 WARN_ON_ONCE(!mutex_is_locked(&mbox->dev->mutex));
Davide Grohmann32660f92022-04-27 16:49:07 +0200108 idr_for_each_entry(&mbox->msg_idr, cur, id) {
Kristofer Jonsson442fefb2022-03-17 17:15:52 +0100109 cur->fail(cur);
110 }
111}
112
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200113int ethosu_mailbox_ping(struct ethosu_mailbox *mbox)
114{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100115 struct ethosu_core_rpmsg rpmsg = {
116 .header = {
117 .magic = ETHOSU_CORE_MSG_MAGIC,
118 .type = ETHOSU_CORE_MSG_PING,
119 }
120 };
121
122 return rpmsg_send(mbox->ept, &rpmsg, sizeof(rpmsg.header));
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200123}
124
Jonny Svärd7c24c772021-01-14 19:53:17 +0100125int ethosu_mailbox_pong(struct ethosu_mailbox *mbox)
126{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100127 struct ethosu_core_rpmsg rpmsg = {
128 .header = {
129 .magic = ETHOSU_CORE_MSG_MAGIC,
130 .type = ETHOSU_CORE_MSG_PONG,
131 }
132 };
133
134 return rpmsg_send(mbox->ept, &rpmsg, sizeof(rpmsg.header));
Jonny Svärd7c24c772021-01-14 19:53:17 +0100135}
136
Mikael Olsson7c843dc2023-08-03 12:41:48 +0200137int ethosu_mailbox_version_request(struct ethosu_mailbox *mbox,
138 struct ethosu_mailbox_msg *msg)
Jonny Svärd7c24c772021-01-14 19:53:17 +0100139{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100140 struct ethosu_core_rpmsg rpmsg = {
Mikael Olsson7c843dc2023-08-03 12:41:48 +0200141 .header = {
142 .magic = ETHOSU_CORE_MSG_MAGIC,
143 .type = ETHOSU_CORE_MSG_VERSION_REQ,
144 .msg_id = msg->id
Kristofer Jonssond779a082023-01-04 17:09:47 +0100145 }
146 };
147
Mikael Olsson7c843dc2023-08-03 12:41:48 +0200148 msg->type = rpmsg.header.type;
149
Kristofer Jonssond779a082023-01-04 17:09:47 +0100150 return rpmsg_send(mbox->ept, &rpmsg, sizeof(rpmsg.header));
Jonny Svärd7c24c772021-01-14 19:53:17 +0100151}
152
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200153int ethosu_mailbox_capabilities_request(struct ethosu_mailbox *mbox,
Davide Grohmann32660f92022-04-27 16:49:07 +0200154 struct ethosu_mailbox_msg *msg)
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200155{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100156 struct ethosu_core_rpmsg rpmsg = {
157 .header = {
158 .magic = ETHOSU_CORE_MSG_MAGIC,
159 .type = ETHOSU_CORE_MSG_CAPABILITIES_REQ,
160 .msg_id = msg->id
161 }
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200162 };
163
Mikael Olsson09965b02023-06-13 12:17:04 +0200164 msg->type = rpmsg.header.type;
165
Kristofer Jonssond779a082023-01-04 17:09:47 +0100166 return rpmsg_send(mbox->ept, &rpmsg, sizeof(rpmsg.header));
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200167}
168
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200169int ethosu_mailbox_inference(struct ethosu_mailbox *mbox,
Davide Grohmann32660f92022-04-27 16:49:07 +0200170 struct ethosu_mailbox_msg *msg,
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200171 uint32_t ifm_count,
172 struct ethosu_buffer **ifm,
173 uint32_t ofm_count,
174 struct ethosu_buffer **ofm,
Per Åstrandf7e407a2020-10-23 21:25:05 +0200175 struct ethosu_buffer *network,
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100176 uint32_t network_index,
Per Åstrandf7e407a2020-10-23 21:25:05 +0200177 uint8_t *pmu_event_config,
178 uint8_t pmu_event_config_count,
179 uint8_t pmu_cycle_counter_enable)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200180{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100181 struct ethosu_core_rpmsg rpmsg = {
182 .header = {
183 .magic = ETHOSU_CORE_MSG_MAGIC,
184 .type = ETHOSU_CORE_MSG_INFERENCE_REQ,
185 .msg_id = msg->id
186 }
187 };
188 struct ethosu_core_msg_inference_req *inf_req = &rpmsg.inf_req;
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200189 uint32_t i;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200190
Mikael Olsson09965b02023-06-13 12:17:04 +0200191 msg->type = rpmsg.header.type;
192
Per Åstrandf7e407a2020-10-23 21:25:05 +0200193 /* Verify that the uapi and core has the same number of pmus */
194 if (pmu_event_config_count != ETHOSU_CORE_PMU_MAX) {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100195 dev_err(mbox->dev, "PMU count misconfigured.");
Per Åstrandf7e407a2020-10-23 21:25:05 +0200196
197 return -EINVAL;
198 }
199
Kristofer Jonssond779a082023-01-04 17:09:47 +0100200 inf_req->ifm_count = ifm_count;
201 inf_req->ofm_count = ofm_count;
202 inf_req->pmu_cycle_counter_enable = pmu_cycle_counter_enable;
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200203
204 for (i = 0; i < ifm_count; i++)
Kristofer Jonssond779a082023-01-04 17:09:47 +0100205 ethosu_core_set_size(ifm[i], &inf_req->ifm[i]);
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200206
207 for (i = 0; i < ofm_count; i++)
Kristofer Jonssond779a082023-01-04 17:09:47 +0100208 ethosu_core_set_capacity(ofm[i], &inf_req->ofm[i]);
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200209
Per Åstrandf7e407a2020-10-23 21:25:05 +0200210 for (i = 0; i < ETHOSU_CORE_PMU_MAX; i++)
Kristofer Jonssond779a082023-01-04 17:09:47 +0100211 inf_req->pmu_event_config[i] = pmu_event_config[i];
Per Åstrandf7e407a2020-10-23 21:25:05 +0200212
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100213 if (network != NULL) {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100214 inf_req->network.type = ETHOSU_CORE_NETWORK_BUFFER;
215 ethosu_core_set_size(network, &inf_req->network.buffer);
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100216 } else {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100217 inf_req->network.type = ETHOSU_CORE_NETWORK_INDEX;
218 inf_req->network.index = network_index;
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100219 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200220
Kristofer Jonssond779a082023-01-04 17:09:47 +0100221 return rpmsg_send(mbox->ept, &rpmsg,
222 sizeof(rpmsg.header) + sizeof(rpmsg.inf_req));
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200223}
224
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100225int ethosu_mailbox_network_info_request(struct ethosu_mailbox *mbox,
Davide Grohmann32660f92022-04-27 16:49:07 +0200226 struct ethosu_mailbox_msg *msg,
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100227 struct ethosu_buffer *network,
228 uint32_t network_index)
229{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100230 struct ethosu_core_rpmsg rpmsg = {
231 .header = {
232 .magic = ETHOSU_CORE_MSG_MAGIC,
233 .type = ETHOSU_CORE_MSG_NETWORK_INFO_REQ,
234 .msg_id = msg->id
235 }
236 };
237 struct ethosu_core_msg_network_info_req *info_req = &rpmsg.net_info_req;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100238
Mikael Olsson09965b02023-06-13 12:17:04 +0200239 msg->type = rpmsg.header.type;
240
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100241 if (network != NULL) {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100242 info_req->network.type = ETHOSU_CORE_NETWORK_BUFFER;
243 ethosu_core_set_size(network, &info_req->network.buffer);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100244 } else {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100245 info_req->network.type = ETHOSU_CORE_NETWORK_INDEX;
246 info_req->network.index = network_index;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100247 }
248
Kristofer Jonssond779a082023-01-04 17:09:47 +0100249 return rpmsg_send(mbox->ept, &rpmsg,
250 sizeof(rpmsg.header) + sizeof(rpmsg.net_info_req));
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100251}
252
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100253int ethosu_mailbox_cancel_inference(struct ethosu_mailbox *mbox,
Davide Grohmann32660f92022-04-27 16:49:07 +0200254 struct ethosu_mailbox_msg *msg,
255 int inference_handle)
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100256{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100257 struct ethosu_core_rpmsg rpmsg = {
258 .header = {
259 .magic = ETHOSU_CORE_MSG_MAGIC,
260 .type =
261 ETHOSU_CORE_MSG_CANCEL_INFERENCE_REQ,
262 .msg_id = msg->id
263 },
264 .cancel_req = {
265 .inference_handle = inference_handle
266 }
267 };
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100268
Mikael Olsson09965b02023-06-13 12:17:04 +0200269 msg->type = rpmsg.header.type;
270
Kristofer Jonssond779a082023-01-04 17:09:47 +0100271 return rpmsg_send(mbox->ept, &rpmsg,
272 sizeof(rpmsg.header) + sizeof(rpmsg.cancel_req));
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200273}
274
275int ethosu_mailbox_init(struct ethosu_mailbox *mbox,
276 struct device *dev,
Kristofer Jonssond779a082023-01-04 17:09:47 +0100277 struct rpmsg_endpoint *ept)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200278{
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200279 mbox->dev = dev;
Kristofer Jonssond779a082023-01-04 17:09:47 +0100280 mbox->ept = ept;
Davide Grohmann32660f92022-04-27 16:49:07 +0200281 idr_init(&mbox->msg_idr);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200282
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200283 return 0;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200284}
285
286void ethosu_mailbox_deinit(struct ethosu_mailbox *mbox)
Kristofer Jonssond779a082023-01-04 17:09:47 +0100287{}