Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
Etienne Carriere | 593eac9 | 2022-05-31 18:09:16 +0200 | [diff] [blame] | 3 | * Copyright (C) 2020-2022 Linaro Limited. |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #define LOG_CATEGORY UCLASS_SCMI_AGENT |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <errno.h> |
| 11 | #include <scmi_agent.h> |
| 12 | #include <scmi_agent-uclass.h> |
| 13 | #include <string.h> |
| 14 | #include <tee.h> |
| 15 | #include <asm/types.h> |
| 16 | #include <dm/device_compat.h> |
| 17 | #include <dm/devres.h> |
| 18 | #include <linux/arm-smccc.h> |
| 19 | #include <linux/bug.h> |
| 20 | #include <linux/compat.h> |
| 21 | |
| 22 | #include "smt.h" |
| 23 | |
| 24 | #define SCMI_SHM_SIZE 128 |
| 25 | |
| 26 | /** |
| 27 | * struct scmi_optee_channel - Description of an SCMI OP-TEE transport |
| 28 | * @channel_id: Channel identifier |
| 29 | * @smt: Shared memory buffer with synchronisation protocol |
| 30 | * @dyn_shm: True if using dynamically allocated shared memory |
| 31 | */ |
| 32 | struct scmi_optee_channel { |
| 33 | unsigned int channel_id; |
| 34 | struct scmi_smt smt; |
| 35 | bool dyn_shm; |
| 36 | }; |
| 37 | |
| 38 | /** |
Etienne Carriere | 965d606 | 2022-05-31 18:09:24 +0200 | [diff] [blame] | 39 | * struct scmi_channel - Channel instance referenced in SCMI drivers |
| 40 | * @ref: Reference to local channel instance |
| 41 | **/ |
| 42 | struct scmi_channel { |
| 43 | struct scmi_optee_channel ref; |
| 44 | }; |
| 45 | |
| 46 | /** |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 47 | * struct channel_session - Aggreates SCMI service session context references |
| 48 | * @tee: OP-TEE device to invoke |
| 49 | * @tee_session: OP-TEE session identifier |
| 50 | * @tee_shm: Dynamically allocated OP-TEE shared memory, or NULL |
| 51 | * @channel_hdl: Channel handle provided by OP-TEE SCMI service |
| 52 | */ |
| 53 | struct channel_session { |
| 54 | struct udevice *tee; |
| 55 | u32 tee_session; |
| 56 | struct tee_shm *tee_shm; |
| 57 | u32 channel_hdl; |
| 58 | }; |
| 59 | |
| 60 | #define TA_SCMI_UUID { 0xa8cfe406, 0xd4f5, 0x4a2e, \ |
| 61 | { 0x9f, 0x8d, 0xa2, 0x5d, 0xc7, 0x54, 0xc0, 0x99 } } |
| 62 | |
| 63 | enum optee_smci_pta_cmd { |
| 64 | /* |
| 65 | * PTA_SCMI_CMD_CAPABILITIES - Get channel capabilities |
| 66 | * |
| 67 | * [out] value[0].a: Capability bit mask (enum pta_scmi_caps) |
| 68 | * [out] value[0].b: Extended capabilities or 0 |
| 69 | */ |
| 70 | PTA_SCMI_CMD_CAPABILITIES = 0, |
| 71 | |
| 72 | /* |
| 73 | * PTA_SCMI_CMD_PROCESS_SMT_CHANNEL - Process SCMI message in SMT buffer |
| 74 | * |
| 75 | * [in] value[0].a: Channel handle |
| 76 | * |
| 77 | * Shared memory used for SCMI message/response exhange is expected |
| 78 | * already identified and bound to channel handle in both SCMI agent |
| 79 | * and SCMI server (OP-TEE) parts. |
| 80 | * The memory uses SMT header to carry SCMI meta-data (protocol ID and |
| 81 | * protocol message ID). |
| 82 | */ |
| 83 | PTA_SCMI_CMD_PROCESS_SMT_CHANNEL = 1, |
| 84 | |
| 85 | /* |
| 86 | * PTA_SCMI_CMD_PROCESS_SMT_CHANNEL_MESSAGE - Process SMT/SCMI message |
| 87 | * |
| 88 | * [in] value[0].a: Channel handle |
| 89 | * [in/out] memref[1]: Message/response buffer (SMT and SCMI payload) |
| 90 | * |
| 91 | * Shared memory used for SCMI message/response is a SMT buffer |
| 92 | * referenced by param[1]. It shall be 128 bytes large to fit response |
| 93 | * payload whatever message playload size. |
| 94 | * The memory uses SMT header to carry SCMI meta-data (protocol ID and |
| 95 | * protocol message ID). |
| 96 | */ |
| 97 | PTA_SCMI_CMD_PROCESS_SMT_CHANNEL_MESSAGE = 2, |
| 98 | |
| 99 | /* |
| 100 | * PTA_SCMI_CMD_GET_CHANNEL - Get channel handle |
| 101 | * |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 102 | * [in] value[0].a: Channel identifier |
| 103 | * [out] value[0].a: Returned channel handle |
| 104 | * [in] value[0].b: Requested capabilities mask (enum pta_scmi_caps) |
| 105 | */ |
| 106 | PTA_SCMI_CMD_GET_CHANNEL = 3, |
Etienne Carriere | 593eac9 | 2022-05-31 18:09:16 +0200 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | * PTA_SCMI_CMD_PROCESS_MSG_CHANNEL - Process SCMI message in MSG |
| 110 | * buffers pointed by memref parameters |
| 111 | * |
| 112 | * [in] value[0].a: Channel handle |
| 113 | * [in] memref[1]: Message buffer (MSG header and SCMI payload) |
| 114 | * [out] memref[2]: Response buffer (MSG header and SCMI payload) |
| 115 | * |
| 116 | * Shared memories used for SCMI message/response are MSG buffers |
| 117 | * referenced by param[1] and param[2]. MSG transport protocol |
| 118 | * uses a 32bit header to carry SCMI meta-data (protocol ID and |
| 119 | * protocol message ID) followed by the effective SCMI message |
| 120 | * payload. |
| 121 | */ |
| 122 | PTA_SCMI_CMD_PROCESS_MSG_CHANNEL = 4, |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | /* |
| 126 | * OP-TEE SCMI service capabilities bit flags (32bit) |
| 127 | * |
| 128 | * PTA_SCMI_CAPS_SMT_HEADER |
| 129 | * When set, OP-TEE supports command using SMT header protocol (SCMI shmem) in |
| 130 | * shared memory buffers to carry SCMI protocol synchronisation information. |
Etienne Carriere | 593eac9 | 2022-05-31 18:09:16 +0200 | [diff] [blame] | 131 | * |
| 132 | * PTA_SCMI_CAPS_MSG_HEADER |
| 133 | * When set, OP-TEE supports command using MSG header protocol in an OP-TEE |
| 134 | * shared memory to carry SCMI protocol synchronisation information and SCMI |
| 135 | * message payload. |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 136 | */ |
| 137 | #define PTA_SCMI_CAPS_NONE 0 |
| 138 | #define PTA_SCMI_CAPS_SMT_HEADER BIT(0) |
Etienne Carriere | 593eac9 | 2022-05-31 18:09:16 +0200 | [diff] [blame] | 139 | #define PTA_SCMI_CAPS_MSG_HEADER BIT(1) |
| 140 | #define PTA_SCMI_CAPS_MASK (PTA_SCMI_CAPS_SMT_HEADER | \ |
| 141 | PTA_SCMI_CAPS_MSG_HEADER) |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 142 | |
Etienne Carriere | 965d606 | 2022-05-31 18:09:24 +0200 | [diff] [blame] | 143 | static int open_channel(struct udevice *dev, struct scmi_optee_channel *chan, |
| 144 | struct channel_session *sess) |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 145 | { |
| 146 | const struct tee_optee_ta_uuid uuid = TA_SCMI_UUID; |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 147 | struct tee_open_session_arg sess_arg = { }; |
| 148 | struct tee_invoke_arg cmd_arg = { }; |
| 149 | struct tee_param param[1] = { }; |
| 150 | int ret; |
| 151 | |
| 152 | memset(sess, 0, sizeof(sess)); |
| 153 | |
| 154 | sess->tee = tee_find_device(NULL, NULL, NULL, NULL); |
| 155 | if (!sess->tee) |
| 156 | return -ENODEV; |
| 157 | |
| 158 | sess_arg.clnt_login = TEE_LOGIN_REE_KERNEL; |
| 159 | tee_optee_ta_uuid_to_octets(sess_arg.uuid, &uuid); |
| 160 | |
| 161 | ret = tee_open_session(sess->tee, &sess_arg, 0, NULL); |
| 162 | if (ret) { |
| 163 | dev_err(dev, "can't open session: %d\n", ret); |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | cmd_arg.func = PTA_SCMI_CMD_GET_CHANNEL; |
| 168 | cmd_arg.session = sess_arg.session; |
| 169 | |
| 170 | param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INOUT; |
| 171 | param[0].u.value.a = chan->channel_id; |
Etienne Carriere | 593eac9 | 2022-05-31 18:09:16 +0200 | [diff] [blame] | 172 | if (chan->dyn_shm) |
| 173 | param[0].u.value.b = PTA_SCMI_CAPS_MSG_HEADER; |
| 174 | else |
| 175 | param[0].u.value.b = PTA_SCMI_CAPS_SMT_HEADER; |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 176 | |
| 177 | ret = tee_invoke_func(sess->tee, &cmd_arg, ARRAY_SIZE(param), param); |
| 178 | if (ret || cmd_arg.ret) { |
| 179 | dev_err(dev, "Invoke failed: %d, 0x%x\n", ret, cmd_arg.ret); |
| 180 | if (!ret) |
| 181 | ret = -EPROTO; |
| 182 | |
| 183 | tee_close_session(sess->tee, sess_arg.session); |
| 184 | return ret; |
| 185 | } |
| 186 | |
| 187 | sess->tee_session = sess_arg.session; |
| 188 | sess->channel_hdl = param[0].u.value.a; |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static void close_channel(struct channel_session *sess) |
| 194 | { |
| 195 | tee_close_session(sess->tee, sess->tee_session); |
| 196 | } |
| 197 | |
Etienne Carriere | 965d606 | 2022-05-31 18:09:24 +0200 | [diff] [blame] | 198 | static int invoke_cmd(struct udevice *dev, struct scmi_optee_channel *chan, |
| 199 | struct channel_session *sess, struct scmi_msg *msg) |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 200 | { |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 201 | struct tee_invoke_arg arg = { }; |
Etienne Carriere | 593eac9 | 2022-05-31 18:09:16 +0200 | [diff] [blame] | 202 | struct tee_param param[3] = { }; |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 203 | int ret; |
| 204 | |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 205 | arg.session = sess->tee_session; |
| 206 | param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT; |
| 207 | param[0].u.value.a = sess->channel_hdl; |
| 208 | |
Etienne Carriere | 593eac9 | 2022-05-31 18:09:16 +0200 | [diff] [blame] | 209 | if (sess->tee_shm) { |
| 210 | size_t in_size; |
| 211 | |
| 212 | ret = scmi_msg_to_smt_msg(dev, &chan->smt, msg, &in_size); |
| 213 | if (ret < 0) |
| 214 | return ret; |
| 215 | |
| 216 | arg.func = PTA_SCMI_CMD_PROCESS_MSG_CHANNEL; |
| 217 | param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT; |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 218 | param[1].u.memref.shm = sess->tee_shm; |
Etienne Carriere | 593eac9 | 2022-05-31 18:09:16 +0200 | [diff] [blame] | 219 | param[1].u.memref.size = in_size; |
| 220 | param[2].attr = TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT; |
| 221 | param[2].u.memref.shm = sess->tee_shm; |
| 222 | param[2].u.memref.size = sess->tee_shm->size; |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 223 | } else { |
| 224 | arg.func = PTA_SCMI_CMD_PROCESS_SMT_CHANNEL; |
Etienne Carriere | 593eac9 | 2022-05-31 18:09:16 +0200 | [diff] [blame] | 225 | scmi_write_msg_to_smt(dev, &chan->smt, msg); |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | ret = tee_invoke_func(sess->tee, &arg, ARRAY_SIZE(param), param); |
| 229 | if (ret || arg.ret) { |
| 230 | if (!ret) |
| 231 | ret = -EPROTO; |
Etienne Carriere | 593eac9 | 2022-05-31 18:09:16 +0200 | [diff] [blame] | 232 | |
| 233 | return ret; |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Etienne Carriere | 593eac9 | 2022-05-31 18:09:16 +0200 | [diff] [blame] | 236 | if (sess->tee_shm) { |
| 237 | ret = scmi_msg_from_smt_msg(dev, &chan->smt, msg, |
| 238 | param[2].u.memref.size); |
| 239 | } else { |
| 240 | ret = scmi_read_resp_from_smt(dev, &chan->smt, msg); |
| 241 | scmi_clear_smt_channel(&chan->smt); |
| 242 | } |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 243 | |
| 244 | return ret; |
| 245 | } |
| 246 | |
Etienne Carriere | 965d606 | 2022-05-31 18:09:24 +0200 | [diff] [blame] | 247 | static int prepare_shm(struct udevice *dev, struct scmi_optee_channel *chan, |
| 248 | struct channel_session *sess) |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 249 | { |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 250 | int ret; |
| 251 | |
| 252 | /* Static shm is already prepared by the firmware: nothing to do */ |
| 253 | if (!chan->dyn_shm) |
| 254 | return 0; |
| 255 | |
| 256 | chan->smt.size = SCMI_SHM_SIZE; |
| 257 | |
| 258 | ret = tee_shm_alloc(sess->tee, chan->smt.size, 0, &sess->tee_shm); |
| 259 | if (ret) { |
| 260 | dev_err(dev, "Failed to allocated shmem: %d\n", ret); |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | chan->smt.buf = sess->tee_shm->addr; |
| 265 | |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | static void release_shm(struct udevice *dev, struct channel_session *sess) |
| 270 | { |
| 271 | struct scmi_optee_channel *chan = dev_get_plat(dev); |
| 272 | |
| 273 | if (chan->dyn_shm) |
| 274 | tee_shm_free(sess->tee_shm); |
| 275 | } |
| 276 | |
Etienne Carriere | 85dc582 | 2022-05-31 18:09:19 +0200 | [diff] [blame] | 277 | static int scmi_optee_process_msg(struct udevice *dev, |
| 278 | struct scmi_channel *channel, |
| 279 | struct scmi_msg *msg) |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 280 | { |
Etienne Carriere | c08decd | 2022-05-31 18:09:29 +0200 | [diff] [blame] | 281 | struct scmi_optee_channel *chan = &channel->ref; |
Etienne Carriere | 593eac9 | 2022-05-31 18:09:16 +0200 | [diff] [blame] | 282 | struct channel_session sess = { }; |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 283 | int ret; |
| 284 | |
Etienne Carriere | 965d606 | 2022-05-31 18:09:24 +0200 | [diff] [blame] | 285 | ret = open_channel(dev, chan, &sess); |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 286 | if (ret) |
| 287 | return ret; |
| 288 | |
Etienne Carriere | 965d606 | 2022-05-31 18:09:24 +0200 | [diff] [blame] | 289 | ret = prepare_shm(dev, chan, &sess); |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 290 | if (ret) |
| 291 | goto out; |
| 292 | |
Etienne Carriere | 965d606 | 2022-05-31 18:09:24 +0200 | [diff] [blame] | 293 | ret = invoke_cmd(dev, chan, &sess, msg); |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 294 | |
| 295 | release_shm(dev, &sess); |
| 296 | |
| 297 | out: |
| 298 | close_channel(&sess); |
| 299 | |
| 300 | return ret; |
| 301 | } |
| 302 | |
Etienne Carriere | 965d606 | 2022-05-31 18:09:24 +0200 | [diff] [blame] | 303 | static int setup_channel(struct udevice *dev, struct scmi_optee_channel *chan) |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 304 | { |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 305 | int ret; |
| 306 | |
| 307 | if (dev_read_u32(dev, "linaro,optee-channel-id", &chan->channel_id)) { |
| 308 | dev_err(dev, "Missing property linaro,optee-channel-id\n"); |
| 309 | return -EINVAL; |
| 310 | } |
| 311 | |
| 312 | if (dev_read_prop(dev, "shmem", NULL)) { |
| 313 | ret = scmi_dt_get_smt_buffer(dev, &chan->smt); |
| 314 | if (ret) { |
| 315 | dev_err(dev, "Failed to get smt resources: %d\n", ret); |
| 316 | return ret; |
| 317 | } |
| 318 | chan->dyn_shm = false; |
| 319 | } else { |
| 320 | chan->dyn_shm = true; |
| 321 | } |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
Etienne Carriere | 965d606 | 2022-05-31 18:09:24 +0200 | [diff] [blame] | 326 | static int scmi_optee_get_channel(struct udevice *dev, |
| 327 | struct scmi_channel **channel) |
| 328 | { |
Patrick Delaunay | eebb967 | 2022-09-30 09:36:38 +0200 | [diff] [blame] | 329 | struct scmi_optee_channel *base_chan = dev_get_plat(dev); |
Etienne Carriere | 965d606 | 2022-05-31 18:09:24 +0200 | [diff] [blame] | 330 | struct scmi_optee_channel *chan; |
| 331 | u32 channel_id; |
| 332 | int ret; |
| 333 | |
| 334 | if (dev_read_u32(dev, "linaro,optee-channel-id", &channel_id)) { |
| 335 | /* Uses agent base channel */ |
| 336 | *channel = container_of(base_chan, struct scmi_channel, ref); |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | /* Setup a dedicated channel */ |
| 342 | chan = calloc(1, sizeof(*chan)); |
| 343 | if (!chan) |
| 344 | return -ENOMEM; |
| 345 | |
| 346 | ret = setup_channel(dev, chan); |
| 347 | if (ret) { |
| 348 | free(chan); |
| 349 | return ret; |
| 350 | } |
| 351 | |
| 352 | *channel = container_of(chan, struct scmi_channel, ref); |
| 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | static int scmi_optee_of_to_plat(struct udevice *dev) |
| 358 | { |
| 359 | struct scmi_optee_channel *chan = dev_get_plat(dev); |
| 360 | |
| 361 | return setup_channel(dev, chan); |
| 362 | } |
| 363 | |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 364 | static int scmi_optee_probe(struct udevice *dev) |
| 365 | { |
Etienne Carriere | 965d606 | 2022-05-31 18:09:24 +0200 | [diff] [blame] | 366 | struct scmi_optee_channel *chan = dev_get_plat(dev); |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 367 | struct channel_session sess; |
| 368 | int ret; |
| 369 | |
| 370 | /* Check OP-TEE service acknowledges the SCMI channel */ |
Etienne Carriere | 965d606 | 2022-05-31 18:09:24 +0200 | [diff] [blame] | 371 | ret = open_channel(dev, chan, &sess); |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 372 | if (!ret) |
| 373 | close_channel(&sess); |
| 374 | |
| 375 | return ret; |
| 376 | } |
| 377 | |
| 378 | static const struct udevice_id scmi_optee_ids[] = { |
| 379 | { .compatible = "linaro,scmi-optee" }, |
| 380 | { } |
| 381 | }; |
| 382 | |
| 383 | static const struct scmi_agent_ops scmi_optee_ops = { |
Etienne Carriere | 965d606 | 2022-05-31 18:09:24 +0200 | [diff] [blame] | 384 | .of_get_channel = scmi_optee_get_channel, |
Etienne Carriere | 48108f3 | 2021-11-09 17:08:24 +0100 | [diff] [blame] | 385 | .process_msg = scmi_optee_process_msg, |
| 386 | }; |
| 387 | |
| 388 | U_BOOT_DRIVER(scmi_optee) = { |
| 389 | .name = "scmi-over-optee", |
| 390 | .id = UCLASS_SCMI_AGENT, |
| 391 | .of_match = scmi_optee_ids, |
| 392 | .plat_auto = sizeof(struct scmi_optee_channel), |
| 393 | .of_to_plat = scmi_optee_of_to_plat, |
| 394 | .probe = scmi_optee_probe, |
| 395 | .flags = DM_FLAG_OS_PREPARE, |
| 396 | .ops = &scmi_optee_ops, |
| 397 | }; |