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