1 // SPDX-License-Identifier: MIT
3 * Copyright 2019 Advanced Micro Devices, Inc.
6 #include <linux/errno.h>
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/string.h>
11 #include <linux/device.h>
12 #include <linux/tee_core.h>
13 #include <linux/types.h>
15 #include <linux/uaccess.h>
16 #include <linux/firmware.h>
17 #include "amdtee_private.h"
18 #include <linux/psp-tee.h>
20 static struct amdtee_driver_data
*drv_data
;
21 static DEFINE_MUTEX(session_list_mutex
);
23 static void amdtee_get_version(struct tee_device
*teedev
,
24 struct tee_ioctl_version_data
*vers
)
26 struct tee_ioctl_version_data v
= {
27 .impl_id
= TEE_IMPL_ID_AMDTEE
,
29 .gen_caps
= TEE_GEN_CAP_GP
,
34 static int amdtee_open(struct tee_context
*ctx
)
36 struct amdtee_context_data
*ctxdata
;
38 ctxdata
= kzalloc(sizeof(*ctxdata
), GFP_KERNEL
);
42 INIT_LIST_HEAD(&ctxdata
->sess_list
);
43 INIT_LIST_HEAD(&ctxdata
->shm_list
);
44 mutex_init(&ctxdata
->shm_mutex
);
50 static void release_session(struct amdtee_session
*sess
)
54 /* Close any open session */
55 for (i
= 0; i
< TEE_NUM_SESSIONS
; ++i
) {
56 /* Check if session entry 'i' is valid */
57 if (!test_bit(i
, sess
->sess_mask
))
60 handle_close_session(sess
->ta_handle
, sess
->session_info
[i
]);
61 handle_unload_ta(sess
->ta_handle
);
67 static void amdtee_release(struct tee_context
*ctx
)
69 struct amdtee_context_data
*ctxdata
= ctx
->data
;
75 struct amdtee_session
*sess
;
77 sess
= list_first_entry_or_null(&ctxdata
->sess_list
,
78 struct amdtee_session
,
84 list_del(&sess
->list_node
);
85 release_session(sess
);
87 mutex_destroy(&ctxdata
->shm_mutex
);
94 * alloc_session() - Allocate a session structure
95 * @ctxdata: TEE Context data structure
96 * @session: Session ID for which 'struct amdtee_session' structure is to be
99 * Scans the TEE context's session list to check if TA is already loaded in to
100 * TEE. If yes, returns the 'session' structure for that TA. Else allocates,
101 * initializes a new 'session' structure and adds it to context's session list.
103 * The caller must hold a mutex.
106 * 'struct amdtee_session *' on success and NULL on failure.
108 static struct amdtee_session
*alloc_session(struct amdtee_context_data
*ctxdata
,
111 struct amdtee_session
*sess
;
112 u32 ta_handle
= get_ta_handle(session
);
114 /* Scan session list to check if TA is already loaded in to TEE */
115 list_for_each_entry(sess
, &ctxdata
->sess_list
, list_node
)
116 if (sess
->ta_handle
== ta_handle
) {
117 kref_get(&sess
->refcount
);
121 /* Allocate a new session and add to list */
122 sess
= kzalloc(sizeof(*sess
), GFP_KERNEL
);
124 sess
->ta_handle
= ta_handle
;
125 kref_init(&sess
->refcount
);
126 spin_lock_init(&sess
->lock
);
127 list_add(&sess
->list_node
, &ctxdata
->sess_list
);
133 /* Requires mutex to be held */
134 static struct amdtee_session
*find_session(struct amdtee_context_data
*ctxdata
,
137 u32 ta_handle
= get_ta_handle(session
);
138 u32 index
= get_session_index(session
);
139 struct amdtee_session
*sess
;
141 if (index
>= TEE_NUM_SESSIONS
)
144 list_for_each_entry(sess
, &ctxdata
->sess_list
, list_node
)
145 if (ta_handle
== sess
->ta_handle
&&
146 test_bit(index
, sess
->sess_mask
))
152 u32
get_buffer_id(struct tee_shm
*shm
)
154 struct amdtee_context_data
*ctxdata
= shm
->ctx
->data
;
155 struct amdtee_shm_data
*shmdata
;
158 mutex_lock(&ctxdata
->shm_mutex
);
159 list_for_each_entry(shmdata
, &ctxdata
->shm_list
, shm_node
)
160 if (shmdata
->kaddr
== shm
->kaddr
) {
161 buf_id
= shmdata
->buf_id
;
164 mutex_unlock(&ctxdata
->shm_mutex
);
169 static DEFINE_MUTEX(drv_mutex
);
170 static int copy_ta_binary(struct tee_context
*ctx
, void *ptr
, void **ta
,
173 const struct firmware
*fw
;
174 char fw_name
[TA_PATH_MAX
];
183 n
= snprintf(fw_name
, TA_PATH_MAX
,
184 "%s/%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x.bin",
185 TA_LOAD_PATH
, uuid
->lo
, uuid
->mid
, uuid
->hi_ver
,
186 uuid
->seq_n
[0], uuid
->seq_n
[1],
187 uuid
->seq_n
[2], uuid
->seq_n
[3],
188 uuid
->seq_n
[4], uuid
->seq_n
[5],
189 uuid
->seq_n
[6], uuid
->seq_n
[7]);
190 if (n
< 0 || n
>= TA_PATH_MAX
) {
191 pr_err("failed to get firmware name\n");
195 mutex_lock(&drv_mutex
);
196 n
= request_firmware(&fw
, fw_name
, &ctx
->teedev
->dev
);
198 pr_err("failed to load firmware %s\n", fw_name
);
203 *ta_size
= roundup(fw
->size
, PAGE_SIZE
);
204 *ta
= (void *)__get_free_pages(GFP_KERNEL
, get_order(*ta_size
));
206 pr_err("%s: get_free_pages failed\n", __func__
);
211 memcpy(*ta
, fw
->data
, fw
->size
);
213 release_firmware(fw
);
215 mutex_unlock(&drv_mutex
);
219 /* mutex must be held by caller */
220 static void destroy_session(struct kref
*ref
)
222 struct amdtee_session
*sess
= container_of(ref
, struct amdtee_session
,
225 list_del(&sess
->list_node
);
226 mutex_unlock(&session_list_mutex
);
230 int amdtee_open_session(struct tee_context
*ctx
,
231 struct tee_ioctl_open_session_arg
*arg
,
232 struct tee_param
*param
)
234 struct amdtee_context_data
*ctxdata
= ctx
->data
;
235 struct amdtee_session
*sess
= NULL
;
236 u32 session_info
, ta_handle
;
241 if (arg
->clnt_login
!= TEE_IOCTL_LOGIN_PUBLIC
) {
242 pr_err("unsupported client login method\n");
246 rc
= copy_ta_binary(ctx
, &arg
->uuid
[0], &ta
, &ta_size
);
248 pr_err("failed to copy TA binary\n");
252 /* Load the TA binary into TEE environment */
253 handle_load_ta(ta
, ta_size
, arg
);
254 if (arg
->ret
!= TEEC_SUCCESS
)
257 ta_handle
= get_ta_handle(arg
->session
);
259 mutex_lock(&session_list_mutex
);
260 sess
= alloc_session(ctxdata
, arg
->session
);
261 mutex_unlock(&session_list_mutex
);
264 handle_unload_ta(ta_handle
);
269 /* Open session with loaded TA */
270 handle_open_session(arg
, &session_info
, param
);
271 if (arg
->ret
!= TEEC_SUCCESS
) {
272 pr_err("open_session failed %d\n", arg
->ret
);
273 handle_unload_ta(ta_handle
);
274 kref_put_mutex(&sess
->refcount
, destroy_session
,
275 &session_list_mutex
);
279 /* Find an empty session index for the given TA */
280 spin_lock(&sess
->lock
);
281 i
= find_first_zero_bit(sess
->sess_mask
, TEE_NUM_SESSIONS
);
282 if (i
< TEE_NUM_SESSIONS
) {
283 sess
->session_info
[i
] = session_info
;
284 set_session_id(ta_handle
, i
, &arg
->session
);
285 set_bit(i
, sess
->sess_mask
);
287 spin_unlock(&sess
->lock
);
289 if (i
>= TEE_NUM_SESSIONS
) {
290 pr_err("reached maximum session count %d\n", TEE_NUM_SESSIONS
);
291 handle_close_session(ta_handle
, session_info
);
292 handle_unload_ta(ta_handle
);
293 kref_put_mutex(&sess
->refcount
, destroy_session
,
294 &session_list_mutex
);
300 free_pages((u64
)ta
, get_order(ta_size
));
304 int amdtee_close_session(struct tee_context
*ctx
, u32 session
)
306 struct amdtee_context_data
*ctxdata
= ctx
->data
;
307 u32 i
, ta_handle
, session_info
;
308 struct amdtee_session
*sess
;
310 pr_debug("%s: sid = 0x%x\n", __func__
, session
);
313 * Check that the session is valid and clear the session
316 mutex_lock(&session_list_mutex
);
317 sess
= find_session(ctxdata
, session
);
319 ta_handle
= get_ta_handle(session
);
320 i
= get_session_index(session
);
321 session_info
= sess
->session_info
[i
];
322 spin_lock(&sess
->lock
);
323 clear_bit(i
, sess
->sess_mask
);
324 spin_unlock(&sess
->lock
);
326 mutex_unlock(&session_list_mutex
);
331 /* Close the session */
332 handle_close_session(ta_handle
, session_info
);
333 handle_unload_ta(ta_handle
);
335 kref_put_mutex(&sess
->refcount
, destroy_session
, &session_list_mutex
);
340 int amdtee_map_shmem(struct tee_shm
*shm
)
342 struct amdtee_context_data
*ctxdata
;
343 struct amdtee_shm_data
*shmnode
;
344 struct shmem_desc shmem
;
351 shmnode
= kmalloc(sizeof(*shmnode
), GFP_KERNEL
);
356 shmem
.kaddr
= shm
->kaddr
;
357 shmem
.size
= shm
->size
;
360 * Send a MAP command to TEE and get the corresponding
363 rc
= handle_map_shmem(count
, &shmem
, &buf_id
);
365 pr_err("map_shmem failed: ret = %d\n", rc
);
370 shmnode
->kaddr
= shm
->kaddr
;
371 shmnode
->buf_id
= buf_id
;
372 ctxdata
= shm
->ctx
->data
;
373 mutex_lock(&ctxdata
->shm_mutex
);
374 list_add(&shmnode
->shm_node
, &ctxdata
->shm_list
);
375 mutex_unlock(&ctxdata
->shm_mutex
);
377 pr_debug("buf_id :[%x] kaddr[%p]\n", shmnode
->buf_id
, shmnode
->kaddr
);
382 void amdtee_unmap_shmem(struct tee_shm
*shm
)
384 struct amdtee_context_data
*ctxdata
;
385 struct amdtee_shm_data
*shmnode
;
391 buf_id
= get_buffer_id(shm
);
392 /* Unmap the shared memory from TEE */
393 handle_unmap_shmem(buf_id
);
395 ctxdata
= shm
->ctx
->data
;
396 mutex_lock(&ctxdata
->shm_mutex
);
397 list_for_each_entry(shmnode
, &ctxdata
->shm_list
, shm_node
)
398 if (buf_id
== shmnode
->buf_id
) {
399 list_del(&shmnode
->shm_node
);
403 mutex_unlock(&ctxdata
->shm_mutex
);
406 int amdtee_invoke_func(struct tee_context
*ctx
,
407 struct tee_ioctl_invoke_arg
*arg
,
408 struct tee_param
*param
)
410 struct amdtee_context_data
*ctxdata
= ctx
->data
;
411 struct amdtee_session
*sess
;
414 /* Check that the session is valid */
415 mutex_lock(&session_list_mutex
);
416 sess
= find_session(ctxdata
, arg
->session
);
418 i
= get_session_index(arg
->session
);
419 session_info
= sess
->session_info
[i
];
421 mutex_unlock(&session_list_mutex
);
426 handle_invoke_cmd(arg
, session_info
, param
);
431 int amdtee_cancel_req(struct tee_context
*ctx
, u32 cancel_id
, u32 session
)
436 static const struct tee_driver_ops amdtee_ops
= {
437 .get_version
= amdtee_get_version
,
439 .release
= amdtee_release
,
440 .open_session
= amdtee_open_session
,
441 .close_session
= amdtee_close_session
,
442 .invoke_func
= amdtee_invoke_func
,
443 .cancel_req
= amdtee_cancel_req
,
446 static const struct tee_desc amdtee_desc
= {
447 .name
= DRIVER_NAME
"-clnt",
449 .owner
= THIS_MODULE
,
452 static int __init
amdtee_driver_init(void)
454 struct tee_device
*teedev
;
455 struct tee_shm_pool
*pool
;
456 struct amdtee
*amdtee
;
459 rc
= psp_check_tee_status();
461 pr_err("amd-tee driver: tee not present\n");
465 drv_data
= kzalloc(sizeof(*drv_data
), GFP_KERNEL
);
469 amdtee
= kzalloc(sizeof(*amdtee
), GFP_KERNEL
);
472 goto err_kfree_drv_data
;
475 pool
= amdtee_config_shm();
477 pr_err("shared pool configuration error\n");
479 goto err_kfree_amdtee
;
482 teedev
= tee_device_alloc(&amdtee_desc
, NULL
, pool
, amdtee
);
483 if (IS_ERR(teedev
)) {
484 rc
= PTR_ERR(teedev
);
487 amdtee
->teedev
= teedev
;
489 rc
= tee_device_register(amdtee
->teedev
);
491 goto err_device_unregister
;
495 drv_data
->amdtee
= amdtee
;
497 pr_info("amd-tee driver initialization successful\n");
500 err_device_unregister
:
501 tee_device_unregister(amdtee
->teedev
);
504 tee_shm_pool_free(pool
);
513 pr_err("amd-tee driver initialization failed\n");
516 module_init(amdtee_driver_init
);
518 static void __exit
amdtee_driver_exit(void)
520 struct amdtee
*amdtee
;
522 if (!drv_data
|| !drv_data
->amdtee
)
525 amdtee
= drv_data
->amdtee
;
527 tee_device_unregister(amdtee
->teedev
);
528 tee_shm_pool_free(amdtee
->pool
);
530 module_exit(amdtee_driver_exit
);
532 MODULE_AUTHOR(DRIVER_AUTHOR
);
533 MODULE_DESCRIPTION("AMD-TEE driver");
534 MODULE_VERSION("1.0");
535 MODULE_LICENSE("Dual MIT/GPL");