Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 1 | /* |
Kristofer Jonsson | b42bc0b | 2023-01-04 17:09:28 +0100 | [diff] [blame] | 2 | * Copyright 2020-2023 Arm Limited and/or its affiliates |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 3 | * |
| 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 Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 28 | #include "ethosu_core_rpmsg.h" |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 29 | #include "ethosu_device.h" |
| 30 | |
Kristofer Jonsson | 442fefb | 2022-03-17 17:15:52 +0100 | [diff] [blame] | 31 | #include <linux/jiffies.h> |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 32 | #include <linux/resource.h> |
| 33 | #include <linux/uio.h> |
| 34 | |
| 35 | /**************************************************************************** |
Kristofer Jonsson | 442fefb | 2022-03-17 17:15:52 +0100 | [diff] [blame] | 36 | * 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 Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 48 | * Functions |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | static 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 | |
| 58 | static 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 Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 65 | int ethosu_mailbox_register(struct ethosu_mailbox *mbox, |
| 66 | struct ethosu_mailbox_msg *msg) |
Kristofer Jonsson | 442fefb | 2022-03-17 17:15:52 +0100 | [diff] [blame] | 67 | { |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 68 | msg->id = idr_alloc_cyclic(&mbox->msg_idr, msg, 0, INT_MAX, GFP_KERNEL); |
| 69 | if (msg->id < 0) |
| 70 | return msg->id; |
Kristofer Jonsson | 442fefb | 2022-03-17 17:15:52 +0100 | [diff] [blame] | 71 | |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 72 | return 0; |
| 73 | } |
Kristofer Jonsson | 442fefb | 2022-03-17 17:15:52 +0100 | [diff] [blame] | 74 | |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 75 | void ethosu_mailbox_deregister(struct ethosu_mailbox *mbox, |
| 76 | struct ethosu_mailbox_msg *msg) |
| 77 | { |
| 78 | idr_remove(&mbox->msg_idr, msg->id); |
| 79 | } |
| 80 | |
| 81 | struct 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 Jonsson | 442fefb | 2022-03-17 17:15:52 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void ethosu_mailbox_fail(struct ethosu_mailbox *mbox) |
| 94 | { |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 95 | struct ethosu_mailbox_msg *cur; |
| 96 | int id; |
Kristofer Jonsson | 442fefb | 2022-03-17 17:15:52 +0100 | [diff] [blame] | 97 | |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 98 | idr_for_each_entry(&mbox->msg_idr, cur, id) { |
Kristofer Jonsson | 442fefb | 2022-03-17 17:15:52 +0100 | [diff] [blame] | 99 | cur->fail(cur); |
| 100 | } |
| 101 | } |
| 102 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 103 | int ethosu_mailbox_ping(struct ethosu_mailbox *mbox) |
| 104 | { |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 105 | 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 Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Jonny Svärd | 7c24c77 | 2021-01-14 19:53:17 +0100 | [diff] [blame] | 115 | int ethosu_mailbox_pong(struct ethosu_mailbox *mbox) |
| 116 | { |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 117 | 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ärd | 7c24c77 | 2021-01-14 19:53:17 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | int ethosu_mailbox_version_request(struct ethosu_mailbox *mbox) |
| 128 | { |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 129 | 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ärd | 7c24c77 | 2021-01-14 19:53:17 +0100 | [diff] [blame] | 137 | } |
| 138 | |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 139 | int ethosu_mailbox_capabilities_request(struct ethosu_mailbox *mbox, |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 140 | struct ethosu_mailbox_msg *msg) |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 141 | { |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 142 | 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 Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 148 | }; |
| 149 | |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 150 | return rpmsg_send(mbox->ept, &rpmsg, sizeof(rpmsg.header)); |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 151 | } |
| 152 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 153 | int ethosu_mailbox_inference(struct ethosu_mailbox *mbox, |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 154 | struct ethosu_mailbox_msg *msg, |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 155 | uint32_t ifm_count, |
| 156 | struct ethosu_buffer **ifm, |
| 157 | uint32_t ofm_count, |
| 158 | struct ethosu_buffer **ofm, |
Per Åstrand | f7e407a | 2020-10-23 21:25:05 +0200 | [diff] [blame] | 159 | struct ethosu_buffer *network, |
Kristofer Jonsson | 35de9e6 | 2022-03-08 13:25:45 +0100 | [diff] [blame] | 160 | uint32_t network_index, |
Per Åstrand | f7e407a | 2020-10-23 21:25:05 +0200 | [diff] [blame] | 161 | uint8_t *pmu_event_config, |
| 162 | uint8_t pmu_event_config_count, |
| 163 | uint8_t pmu_cycle_counter_enable) |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 164 | { |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 165 | 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 Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 173 | uint32_t i; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 174 | |
Per Åstrand | f7e407a | 2020-10-23 21:25:05 +0200 | [diff] [blame] | 175 | /* Verify that the uapi and core has the same number of pmus */ |
| 176 | if (pmu_event_config_count != ETHOSU_CORE_PMU_MAX) { |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 177 | dev_err(mbox->dev, "PMU count misconfigured."); |
Per Åstrand | f7e407a | 2020-10-23 21:25:05 +0200 | [diff] [blame] | 178 | |
| 179 | return -EINVAL; |
| 180 | } |
| 181 | |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 182 | 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 Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 185 | |
| 186 | for (i = 0; i < ifm_count; i++) |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 187 | ethosu_core_set_size(ifm[i], &inf_req->ifm[i]); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 188 | |
| 189 | for (i = 0; i < ofm_count; i++) |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 190 | ethosu_core_set_capacity(ofm[i], &inf_req->ofm[i]); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 191 | |
Per Åstrand | f7e407a | 2020-10-23 21:25:05 +0200 | [diff] [blame] | 192 | for (i = 0; i < ETHOSU_CORE_PMU_MAX; i++) |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 193 | inf_req->pmu_event_config[i] = pmu_event_config[i]; |
Per Åstrand | f7e407a | 2020-10-23 21:25:05 +0200 | [diff] [blame] | 194 | |
Kristofer Jonsson | 35de9e6 | 2022-03-08 13:25:45 +0100 | [diff] [blame] | 195 | if (network != NULL) { |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 196 | inf_req->network.type = ETHOSU_CORE_NETWORK_BUFFER; |
| 197 | ethosu_core_set_size(network, &inf_req->network.buffer); |
Kristofer Jonsson | 35de9e6 | 2022-03-08 13:25:45 +0100 | [diff] [blame] | 198 | } else { |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 199 | inf_req->network.type = ETHOSU_CORE_NETWORK_INDEX; |
| 200 | inf_req->network.index = network_index; |
Kristofer Jonsson | 35de9e6 | 2022-03-08 13:25:45 +0100 | [diff] [blame] | 201 | } |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 202 | |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 203 | return rpmsg_send(mbox->ept, &rpmsg, |
| 204 | sizeof(rpmsg.header) + sizeof(rpmsg.inf_req)); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 205 | } |
| 206 | |
Kristofer Jonsson | 3c6a260 | 2022-03-10 11:17:29 +0100 | [diff] [blame] | 207 | int ethosu_mailbox_network_info_request(struct ethosu_mailbox *mbox, |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 208 | struct ethosu_mailbox_msg *msg, |
Kristofer Jonsson | 3c6a260 | 2022-03-10 11:17:29 +0100 | [diff] [blame] | 209 | struct ethosu_buffer *network, |
| 210 | uint32_t network_index) |
| 211 | { |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 212 | 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 Jonsson | 3c6a260 | 2022-03-10 11:17:29 +0100 | [diff] [blame] | 220 | |
| 221 | if (network != NULL) { |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 222 | info_req->network.type = ETHOSU_CORE_NETWORK_BUFFER; |
| 223 | ethosu_core_set_size(network, &info_req->network.buffer); |
Kristofer Jonsson | 3c6a260 | 2022-03-10 11:17:29 +0100 | [diff] [blame] | 224 | } else { |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 225 | info_req->network.type = ETHOSU_CORE_NETWORK_INDEX; |
| 226 | info_req->network.index = network_index; |
Kristofer Jonsson | 3c6a260 | 2022-03-10 11:17:29 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 229 | return rpmsg_send(mbox->ept, &rpmsg, |
| 230 | sizeof(rpmsg.header) + sizeof(rpmsg.net_info_req)); |
Kristofer Jonsson | 3c6a260 | 2022-03-10 11:17:29 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 233 | int ethosu_mailbox_cancel_inference(struct ethosu_mailbox *mbox, |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 234 | struct ethosu_mailbox_msg *msg, |
| 235 | int inference_handle) |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 236 | { |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 237 | 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 Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 248 | |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 249 | return rpmsg_send(mbox->ept, &rpmsg, |
| 250 | sizeof(rpmsg.header) + sizeof(rpmsg.cancel_req)); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | int ethosu_mailbox_init(struct ethosu_mailbox *mbox, |
| 254 | struct device *dev, |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 255 | struct rpmsg_endpoint *ept) |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 256 | { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 257 | mbox->dev = dev; |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 258 | mbox->ept = ept; |
Davide Grohmann | 32660f9 | 2022-04-27 16:49:07 +0200 | [diff] [blame] | 259 | idr_init(&mbox->msg_idr); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 260 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 261 | return 0; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | void ethosu_mailbox_deinit(struct ethosu_mailbox *mbox) |
Kristofer Jonsson | d779a08 | 2023-01-04 17:09:47 +0100 | [diff] [blame] | 265 | {} |