blob: 5b105d8e79c5592acb91254372a4b0ade23499d0 [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
137int ethosu_mailbox_version_request(struct ethosu_mailbox *mbox)
138{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100139 struct ethosu_core_rpmsg rpmsg = {
140 .header = {
141 .magic = ETHOSU_CORE_MSG_MAGIC,
142 .type = ETHOSU_CORE_MSG_VERSION_REQ,
143 }
144 };
145
146 return rpmsg_send(mbox->ept, &rpmsg, sizeof(rpmsg.header));
Jonny Svärd7c24c772021-01-14 19:53:17 +0100147}
148
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200149int ethosu_mailbox_capabilities_request(struct ethosu_mailbox *mbox,
Davide Grohmann32660f92022-04-27 16:49:07 +0200150 struct ethosu_mailbox_msg *msg)
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200151{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100152 struct ethosu_core_rpmsg rpmsg = {
153 .header = {
154 .magic = ETHOSU_CORE_MSG_MAGIC,
155 .type = ETHOSU_CORE_MSG_CAPABILITIES_REQ,
156 .msg_id = msg->id
157 }
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200158 };
159
Mikael Olsson09965b02023-06-13 12:17:04 +0200160 msg->type = rpmsg.header.type;
161
Kristofer Jonssond779a082023-01-04 17:09:47 +0100162 return rpmsg_send(mbox->ept, &rpmsg, sizeof(rpmsg.header));
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200163}
164
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200165int ethosu_mailbox_inference(struct ethosu_mailbox *mbox,
Davide Grohmann32660f92022-04-27 16:49:07 +0200166 struct ethosu_mailbox_msg *msg,
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200167 uint32_t ifm_count,
168 struct ethosu_buffer **ifm,
169 uint32_t ofm_count,
170 struct ethosu_buffer **ofm,
Per Åstrandf7e407a2020-10-23 21:25:05 +0200171 struct ethosu_buffer *network,
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100172 uint32_t network_index,
Per Åstrandf7e407a2020-10-23 21:25:05 +0200173 uint8_t *pmu_event_config,
174 uint8_t pmu_event_config_count,
175 uint8_t pmu_cycle_counter_enable)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200176{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100177 struct ethosu_core_rpmsg rpmsg = {
178 .header = {
179 .magic = ETHOSU_CORE_MSG_MAGIC,
180 .type = ETHOSU_CORE_MSG_INFERENCE_REQ,
181 .msg_id = msg->id
182 }
183 };
184 struct ethosu_core_msg_inference_req *inf_req = &rpmsg.inf_req;
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200185 uint32_t i;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200186
Mikael Olsson09965b02023-06-13 12:17:04 +0200187 msg->type = rpmsg.header.type;
188
Per Åstrandf7e407a2020-10-23 21:25:05 +0200189 /* Verify that the uapi and core has the same number of pmus */
190 if (pmu_event_config_count != ETHOSU_CORE_PMU_MAX) {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100191 dev_err(mbox->dev, "PMU count misconfigured.");
Per Åstrandf7e407a2020-10-23 21:25:05 +0200192
193 return -EINVAL;
194 }
195
Kristofer Jonssond779a082023-01-04 17:09:47 +0100196 inf_req->ifm_count = ifm_count;
197 inf_req->ofm_count = ofm_count;
198 inf_req->pmu_cycle_counter_enable = pmu_cycle_counter_enable;
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200199
200 for (i = 0; i < ifm_count; i++)
Kristofer Jonssond779a082023-01-04 17:09:47 +0100201 ethosu_core_set_size(ifm[i], &inf_req->ifm[i]);
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200202
203 for (i = 0; i < ofm_count; i++)
Kristofer Jonssond779a082023-01-04 17:09:47 +0100204 ethosu_core_set_capacity(ofm[i], &inf_req->ofm[i]);
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200205
Per Åstrandf7e407a2020-10-23 21:25:05 +0200206 for (i = 0; i < ETHOSU_CORE_PMU_MAX; i++)
Kristofer Jonssond779a082023-01-04 17:09:47 +0100207 inf_req->pmu_event_config[i] = pmu_event_config[i];
Per Åstrandf7e407a2020-10-23 21:25:05 +0200208
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100209 if (network != NULL) {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100210 inf_req->network.type = ETHOSU_CORE_NETWORK_BUFFER;
211 ethosu_core_set_size(network, &inf_req->network.buffer);
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100212 } else {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100213 inf_req->network.type = ETHOSU_CORE_NETWORK_INDEX;
214 inf_req->network.index = network_index;
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100215 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200216
Kristofer Jonssond779a082023-01-04 17:09:47 +0100217 return rpmsg_send(mbox->ept, &rpmsg,
218 sizeof(rpmsg.header) + sizeof(rpmsg.inf_req));
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200219}
220
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100221int ethosu_mailbox_network_info_request(struct ethosu_mailbox *mbox,
Davide Grohmann32660f92022-04-27 16:49:07 +0200222 struct ethosu_mailbox_msg *msg,
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100223 struct ethosu_buffer *network,
224 uint32_t network_index)
225{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100226 struct ethosu_core_rpmsg rpmsg = {
227 .header = {
228 .magic = ETHOSU_CORE_MSG_MAGIC,
229 .type = ETHOSU_CORE_MSG_NETWORK_INFO_REQ,
230 .msg_id = msg->id
231 }
232 };
233 struct ethosu_core_msg_network_info_req *info_req = &rpmsg.net_info_req;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100234
Mikael Olsson09965b02023-06-13 12:17:04 +0200235 msg->type = rpmsg.header.type;
236
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100237 if (network != NULL) {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100238 info_req->network.type = ETHOSU_CORE_NETWORK_BUFFER;
239 ethosu_core_set_size(network, &info_req->network.buffer);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100240 } else {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100241 info_req->network.type = ETHOSU_CORE_NETWORK_INDEX;
242 info_req->network.index = network_index;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100243 }
244
Kristofer Jonssond779a082023-01-04 17:09:47 +0100245 return rpmsg_send(mbox->ept, &rpmsg,
246 sizeof(rpmsg.header) + sizeof(rpmsg.net_info_req));
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100247}
248
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100249int ethosu_mailbox_cancel_inference(struct ethosu_mailbox *mbox,
Davide Grohmann32660f92022-04-27 16:49:07 +0200250 struct ethosu_mailbox_msg *msg,
251 int inference_handle)
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100252{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100253 struct ethosu_core_rpmsg rpmsg = {
254 .header = {
255 .magic = ETHOSU_CORE_MSG_MAGIC,
256 .type =
257 ETHOSU_CORE_MSG_CANCEL_INFERENCE_REQ,
258 .msg_id = msg->id
259 },
260 .cancel_req = {
261 .inference_handle = inference_handle
262 }
263 };
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100264
Mikael Olsson09965b02023-06-13 12:17:04 +0200265 msg->type = rpmsg.header.type;
266
Kristofer Jonssond779a082023-01-04 17:09:47 +0100267 return rpmsg_send(mbox->ept, &rpmsg,
268 sizeof(rpmsg.header) + sizeof(rpmsg.cancel_req));
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200269}
270
271int ethosu_mailbox_init(struct ethosu_mailbox *mbox,
272 struct device *dev,
Kristofer Jonssond779a082023-01-04 17:09:47 +0100273 struct rpmsg_endpoint *ept)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200274{
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200275 mbox->dev = dev;
Kristofer Jonssond779a082023-01-04 17:09:47 +0100276 mbox->ept = ept;
Davide Grohmann32660f92022-04-27 16:49:07 +0200277 idr_init(&mbox->msg_idr);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200278
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200279 return 0;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200280}
281
282void ethosu_mailbox_deinit(struct ethosu_mailbox *mbox)
Kristofer Jonssond779a082023-01-04 17:09:47 +0100283{}