blob: 4c64f17877387fbc52000dfaaef19726ad6d2a26 [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,
82 int msg_id)
83{
84 struct ethosu_mailbox_msg *ptr = (struct ethosu_mailbox_msg *)idr_find(
85 &mbox->msg_idr, msg_id);
86
87 if (ptr == NULL)
88 return ERR_PTR(-EINVAL);
89
90 return ptr;
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010091}
92
93void ethosu_mailbox_fail(struct ethosu_mailbox *mbox)
94{
Davide Grohmann32660f92022-04-27 16:49:07 +020095 struct ethosu_mailbox_msg *cur;
96 int id;
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010097
Davide Grohmann32660f92022-04-27 16:49:07 +020098 idr_for_each_entry(&mbox->msg_idr, cur, id) {
Kristofer Jonsson442fefb2022-03-17 17:15:52 +010099 cur->fail(cur);
100 }
101}
102
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200103int ethosu_mailbox_ping(struct ethosu_mailbox *mbox)
104{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100105 struct ethosu_core_rpmsg rpmsg = {
106 .header = {
107 .magic = ETHOSU_CORE_MSG_MAGIC,
108 .type = ETHOSU_CORE_MSG_PING,
109 }
110 };
111
112 return rpmsg_send(mbox->ept, &rpmsg, sizeof(rpmsg.header));
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200113}
114
Jonny Svärd7c24c772021-01-14 19:53:17 +0100115int ethosu_mailbox_pong(struct ethosu_mailbox *mbox)
116{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100117 struct ethosu_core_rpmsg rpmsg = {
118 .header = {
119 .magic = ETHOSU_CORE_MSG_MAGIC,
120 .type = ETHOSU_CORE_MSG_PONG,
121 }
122 };
123
124 return rpmsg_send(mbox->ept, &rpmsg, sizeof(rpmsg.header));
Jonny Svärd7c24c772021-01-14 19:53:17 +0100125}
126
127int ethosu_mailbox_version_request(struct ethosu_mailbox *mbox)
128{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100129 struct ethosu_core_rpmsg rpmsg = {
130 .header = {
131 .magic = ETHOSU_CORE_MSG_MAGIC,
132 .type = ETHOSU_CORE_MSG_VERSION_REQ,
133 }
134 };
135
136 return rpmsg_send(mbox->ept, &rpmsg, sizeof(rpmsg.header));
Jonny Svärd7c24c772021-01-14 19:53:17 +0100137}
138
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200139int ethosu_mailbox_capabilities_request(struct ethosu_mailbox *mbox,
Davide Grohmann32660f92022-04-27 16:49:07 +0200140 struct ethosu_mailbox_msg *msg)
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200141{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100142 struct ethosu_core_rpmsg rpmsg = {
143 .header = {
144 .magic = ETHOSU_CORE_MSG_MAGIC,
145 .type = ETHOSU_CORE_MSG_CAPABILITIES_REQ,
146 .msg_id = msg->id
147 }
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200148 };
149
Kristofer Jonssond779a082023-01-04 17:09:47 +0100150 return rpmsg_send(mbox->ept, &rpmsg, sizeof(rpmsg.header));
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200151}
152
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200153int ethosu_mailbox_inference(struct ethosu_mailbox *mbox,
Davide Grohmann32660f92022-04-27 16:49:07 +0200154 struct ethosu_mailbox_msg *msg,
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200155 uint32_t ifm_count,
156 struct ethosu_buffer **ifm,
157 uint32_t ofm_count,
158 struct ethosu_buffer **ofm,
Per Åstrandf7e407a2020-10-23 21:25:05 +0200159 struct ethosu_buffer *network,
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100160 uint32_t network_index,
Per Åstrandf7e407a2020-10-23 21:25:05 +0200161 uint8_t *pmu_event_config,
162 uint8_t pmu_event_config_count,
163 uint8_t pmu_cycle_counter_enable)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200164{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100165 struct ethosu_core_rpmsg rpmsg = {
166 .header = {
167 .magic = ETHOSU_CORE_MSG_MAGIC,
168 .type = ETHOSU_CORE_MSG_INFERENCE_REQ,
169 .msg_id = msg->id
170 }
171 };
172 struct ethosu_core_msg_inference_req *inf_req = &rpmsg.inf_req;
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200173 uint32_t i;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200174
Per Åstrandf7e407a2020-10-23 21:25:05 +0200175 /* Verify that the uapi and core has the same number of pmus */
176 if (pmu_event_config_count != ETHOSU_CORE_PMU_MAX) {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100177 dev_err(mbox->dev, "PMU count misconfigured.");
Per Åstrandf7e407a2020-10-23 21:25:05 +0200178
179 return -EINVAL;
180 }
181
Kristofer Jonssond779a082023-01-04 17:09:47 +0100182 inf_req->ifm_count = ifm_count;
183 inf_req->ofm_count = ofm_count;
184 inf_req->pmu_cycle_counter_enable = pmu_cycle_counter_enable;
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200185
186 for (i = 0; i < ifm_count; i++)
Kristofer Jonssond779a082023-01-04 17:09:47 +0100187 ethosu_core_set_size(ifm[i], &inf_req->ifm[i]);
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200188
189 for (i = 0; i < ofm_count; i++)
Kristofer Jonssond779a082023-01-04 17:09:47 +0100190 ethosu_core_set_capacity(ofm[i], &inf_req->ofm[i]);
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200191
Per Åstrandf7e407a2020-10-23 21:25:05 +0200192 for (i = 0; i < ETHOSU_CORE_PMU_MAX; i++)
Kristofer Jonssond779a082023-01-04 17:09:47 +0100193 inf_req->pmu_event_config[i] = pmu_event_config[i];
Per Åstrandf7e407a2020-10-23 21:25:05 +0200194
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100195 if (network != NULL) {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100196 inf_req->network.type = ETHOSU_CORE_NETWORK_BUFFER;
197 ethosu_core_set_size(network, &inf_req->network.buffer);
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100198 } else {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100199 inf_req->network.type = ETHOSU_CORE_NETWORK_INDEX;
200 inf_req->network.index = network_index;
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100201 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200202
Kristofer Jonssond779a082023-01-04 17:09:47 +0100203 return rpmsg_send(mbox->ept, &rpmsg,
204 sizeof(rpmsg.header) + sizeof(rpmsg.inf_req));
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200205}
206
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100207int ethosu_mailbox_network_info_request(struct ethosu_mailbox *mbox,
Davide Grohmann32660f92022-04-27 16:49:07 +0200208 struct ethosu_mailbox_msg *msg,
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100209 struct ethosu_buffer *network,
210 uint32_t network_index)
211{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100212 struct ethosu_core_rpmsg rpmsg = {
213 .header = {
214 .magic = ETHOSU_CORE_MSG_MAGIC,
215 .type = ETHOSU_CORE_MSG_NETWORK_INFO_REQ,
216 .msg_id = msg->id
217 }
218 };
219 struct ethosu_core_msg_network_info_req *info_req = &rpmsg.net_info_req;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100220
221 if (network != NULL) {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100222 info_req->network.type = ETHOSU_CORE_NETWORK_BUFFER;
223 ethosu_core_set_size(network, &info_req->network.buffer);
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100224 } else {
Kristofer Jonssond779a082023-01-04 17:09:47 +0100225 info_req->network.type = ETHOSU_CORE_NETWORK_INDEX;
226 info_req->network.index = network_index;
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100227 }
228
Kristofer Jonssond779a082023-01-04 17:09:47 +0100229 return rpmsg_send(mbox->ept, &rpmsg,
230 sizeof(rpmsg.header) + sizeof(rpmsg.net_info_req));
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100231}
232
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100233int ethosu_mailbox_cancel_inference(struct ethosu_mailbox *mbox,
Davide Grohmann32660f92022-04-27 16:49:07 +0200234 struct ethosu_mailbox_msg *msg,
235 int inference_handle)
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100236{
Kristofer Jonssond779a082023-01-04 17:09:47 +0100237 struct ethosu_core_rpmsg rpmsg = {
238 .header = {
239 .magic = ETHOSU_CORE_MSG_MAGIC,
240 .type =
241 ETHOSU_CORE_MSG_CANCEL_INFERENCE_REQ,
242 .msg_id = msg->id
243 },
244 .cancel_req = {
245 .inference_handle = inference_handle
246 }
247 };
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100248
Kristofer Jonssond779a082023-01-04 17:09:47 +0100249 return rpmsg_send(mbox->ept, &rpmsg,
250 sizeof(rpmsg.header) + sizeof(rpmsg.cancel_req));
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200251}
252
253int ethosu_mailbox_init(struct ethosu_mailbox *mbox,
254 struct device *dev,
Kristofer Jonssond779a082023-01-04 17:09:47 +0100255 struct rpmsg_endpoint *ept)
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200256{
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200257 mbox->dev = dev;
Kristofer Jonssond779a082023-01-04 17:09:47 +0100258 mbox->ept = ept;
Davide Grohmann32660f92022-04-27 16:49:07 +0200259 idr_init(&mbox->msg_idr);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200260
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200261 return 0;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200262}
263
264void ethosu_mailbox_deinit(struct ethosu_mailbox *mbox)
Kristofer Jonssond779a082023-01-04 17:09:47 +0100265{}