1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * OP-TEE STM32MP BSEC PTA interface, used by STM32 ROMEM driver
5 * Copyright (C) 2022, STMicroelectronics - All Rights Reserved
8 #include <linux/tee_drv.h>
10 #include "stm32-bsec-optee-ta.h"
15 * [in] value[0].a OTP start offset in byte
16 * [in] value[0].b Access type (0:shadow, 1:fuse, 2:lock)
17 * [out] memref[1].buffer Output buffer to store read values
18 * [out] memref[1].size Size of OTP to be read
21 * TEE_SUCCESS - Invoke command success
22 * TEE_ERROR_BAD_PARAMETERS - Incorrect input param
23 * TEE_ERROR_ACCESS_DENIED - OTP not accessible by caller
25 #define PTA_BSEC_READ_MEM 0x0
30 * [in] value[0].a OTP start offset in byte
31 * [in] value[0].b Access type (0:shadow, 1:fuse, 2:lock)
32 * [in] memref[1].buffer Input buffer to read values
33 * [in] memref[1].size Size of OTP to be written
36 * TEE_SUCCESS - Invoke command success
37 * TEE_ERROR_BAD_PARAMETERS - Incorrect input param
38 * TEE_ERROR_ACCESS_DENIED - OTP not accessible by caller
40 #define PTA_BSEC_WRITE_MEM 0x1
42 /* value of PTA_BSEC access type = value[in] b */
43 #define SHADOW_ACCESS 0
47 /* Bitfield definition for LOCK status */
48 #define LOCK_PERM BIT(30)
50 /* OP-TEE STM32MP BSEC TA UUID */
51 static const uuid_t stm32mp_bsec_ta_uuid =
52 UUID_INIT(0x94cf71ad, 0x80e6, 0x40b5,
53 0xa7, 0xc6, 0x3d, 0xc5, 0x01, 0xeb, 0x28, 0x03);
56 * Check whether this driver supports the BSEC TA in the TEE instance
57 * represented by the params (ver/data) to this function.
59 static int stm32_bsec_optee_ta_match(struct tee_ioctl_version_data *ver,
62 /* Currently this driver only supports GP compliant, OP-TEE based TA */
63 if ((ver->impl_id == TEE_IMPL_ID_OPTEE) &&
64 (ver->gen_caps & TEE_GEN_CAP_GP))
70 /* Open a session to OP-TEE for STM32MP BSEC TA */
71 static int stm32_bsec_ta_open_session(struct tee_context *ctx, u32 *id)
73 struct tee_ioctl_open_session_arg sess_arg;
76 memset(&sess_arg, 0, sizeof(sess_arg));
77 export_uuid(sess_arg.uuid, &stm32mp_bsec_ta_uuid);
78 sess_arg.clnt_login = TEE_IOCTL_LOGIN_REE_KERNEL;
79 sess_arg.num_params = 0;
81 rc = tee_client_open_session(ctx, &sess_arg, NULL);
82 if ((rc < 0) || (sess_arg.ret != 0)) {
83 pr_err("%s: tee_client_open_session failed err:%#x, ret:%#x\n",
84 __func__, sess_arg.ret, rc);
88 *id = sess_arg.session;
94 /* close a session to OP-TEE for STM32MP BSEC TA */
95 static void stm32_bsec_ta_close_session(void *ctx, u32 id)
97 tee_client_close_session(ctx, id);
100 /* stm32_bsec_optee_ta_open() - initialize the STM32MP BSEC TA */
101 int stm32_bsec_optee_ta_open(struct tee_context **ctx)
103 struct tee_context *tee_ctx;
107 /* Open context with TEE driver */
108 tee_ctx = tee_client_open_context(NULL, stm32_bsec_optee_ta_match, NULL, NULL);
109 if (IS_ERR(tee_ctx)) {
110 rc = PTR_ERR(tee_ctx);
112 return -EPROBE_DEFER;
113 pr_err("%s: tee_client_open_context failed (%d)\n", __func__, rc);
118 /* Check STM32MP BSEC TA presence */
119 rc = stm32_bsec_ta_open_session(tee_ctx, &session_id);
121 tee_client_close_context(tee_ctx);
125 stm32_bsec_ta_close_session(tee_ctx, session_id);
132 /* stm32_bsec_optee_ta_open() - release the PTA STM32MP BSEC TA */
133 void stm32_bsec_optee_ta_close(void *ctx)
135 tee_client_close_context(ctx);
138 /* stm32_bsec_optee_ta_read() - nvmem read access using PTA client driver */
139 int stm32_bsec_optee_ta_read(struct tee_context *ctx, unsigned int offset,
140 void *buf, size_t bytes)
143 struct tee_ioctl_invoke_arg arg;
144 struct tee_param param[2];
146 u32 start, num_bytes;
150 ret = stm32_bsec_ta_open_session(ctx, &session_id);
154 memset(&arg, 0, sizeof(arg));
155 memset(¶m, 0, sizeof(param));
157 arg.func = PTA_BSEC_READ_MEM;
158 arg.session = session_id;
161 /* align access on 32bits */
162 start = ALIGN_DOWN(offset, 4);
163 num_bytes = round_up(offset + bytes - start, 4);
164 param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
165 param[0].u.value.a = start;
166 param[0].u.value.b = SHADOW_ACCESS;
168 shm = tee_shm_alloc_kernel_buf(ctx, num_bytes);
171 goto out_tee_session;
174 param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
175 param[1].u.memref.shm = shm;
176 param[1].u.memref.size = num_bytes;
178 ret = tee_client_invoke_func(ctx, &arg, param);
179 if (ret < 0 || arg.ret != 0) {
180 pr_err("TA_BSEC invoke failed TEE err:%#x, ret:%#x\n",
186 shm_buf = tee_shm_get_va(shm, 0);
187 if (IS_ERR(shm_buf)) {
188 ret = PTR_ERR(shm_buf);
189 pr_err("tee_shm_get_va failed for transmit (%d)\n", ret);
191 /* read data from 32 bits aligned buffer */
192 memcpy(buf, &shm_buf[offset % 4], bytes);
199 stm32_bsec_ta_close_session(ctx, session_id);
204 /* stm32_bsec_optee_ta_write() - nvmem write access using PTA client driver */
205 int stm32_bsec_optee_ta_write(struct tee_context *ctx, unsigned int lower,
206 unsigned int offset, void *buf, size_t bytes)
207 { struct tee_shm *shm;
208 struct tee_ioctl_invoke_arg arg;
209 struct tee_param param[2];
214 ret = stm32_bsec_ta_open_session(ctx, &session_id);
218 /* Allow only writing complete 32-bits aligned words */
219 if ((bytes % 4) || (offset % 4))
222 memset(&arg, 0, sizeof(arg));
223 memset(¶m, 0, sizeof(param));
225 arg.func = PTA_BSEC_WRITE_MEM;
226 arg.session = session_id;
229 param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
230 param[0].u.value.a = offset;
231 param[0].u.value.b = FUSE_ACCESS;
233 shm = tee_shm_alloc_kernel_buf(ctx, bytes);
236 goto out_tee_session;
239 param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
240 param[1].u.memref.shm = shm;
241 param[1].u.memref.size = bytes;
243 shm_buf = tee_shm_get_va(shm, 0);
244 if (IS_ERR(shm_buf)) {
245 ret = PTR_ERR(shm_buf);
246 pr_err("tee_shm_get_va failed for transmit (%d)\n", ret);
249 goto out_tee_session;
252 memcpy(shm_buf, buf, bytes);
254 ret = tee_client_invoke_func(ctx, &arg, param);
255 if (ret < 0 || arg.ret != 0) {
256 pr_err("TA_BSEC invoke failed TEE err:%#x, ret:%#x\n", arg.ret, ret);
260 pr_debug("Write OTPs %d to %zu, ret=%d\n", offset / 4, (offset + bytes) / 4, ret);
262 /* Lock the upper OTPs with ECC protection, word programming only */
263 if (!ret && ((offset + bytes) >= (lower * 4))) {
265 u32 *lock = (u32 *)shm_buf;
269 * don't lock the lower OTPs, no ECC protection and incremental
270 * bit programming, a second write is allowed
272 start = max_t(u32, offset, lower * 4);
273 nb_lock = (offset + bytes - start) / 4;
275 param[0].u.value.a = start;
276 param[0].u.value.b = LOCK_ACCESS;
277 param[1].u.memref.size = nb_lock * 4;
279 for (i = 0; i < nb_lock; i++)
282 ret = tee_client_invoke_func(ctx, &arg, param);
283 if (ret < 0 || arg.ret != 0) {
284 pr_err("TA_BSEC invoke failed TEE err:%#x, ret:%#x\n", arg.ret, ret);
288 pr_debug("Lock upper OTPs %d to %d, ret=%d\n",
289 start / 4, start / 4 + nb_lock, ret);
295 stm32_bsec_ta_close_session(ctx, session_id);