1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2016, Linaro Limited
4  */
5 
6 #include <pta_gprof.h>
7 #include <string.h>
8 #include <tee_api.h>
9 #include "gprof_pta.h"
10 
11 static TEE_TASessionHandle sess = TEE_HANDLE_NULL;
12 
invoke_gprof_pta(uint32_t cmd_id,uint32_t param_types,TEE_Param params[TEE_NUM_PARAMS])13 static TEE_Result invoke_gprof_pta(uint32_t cmd_id, uint32_t param_types,
14 				   TEE_Param params[TEE_NUM_PARAMS])
15 {
16 	static const TEE_UUID core_uuid = PTA_GPROF_UUID;
17 	TEE_Result res;
18 
19 	if (!sess) {
20 		res = TEE_OpenTASession(&core_uuid, TEE_TIMEOUT_INFINITE,
21 					0, NULL, &sess, NULL);
22 		if (res != TEE_SUCCESS)
23 			return res;
24 	}
25 	res = TEE_InvokeTACommand(sess, TEE_TIMEOUT_INFINITE, cmd_id,
26 				  param_types, params, NULL);
27 	return res;
28 }
29 
__pta_gprof_send(void * buf,size_t len,uint32_t * id)30 TEE_Result __pta_gprof_send(void *buf, size_t len, uint32_t *id)
31 {
32 	TEE_Param params[TEE_NUM_PARAMS];
33 	uint32_t param_types;
34 	TEE_Result res;
35 
36 	param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INOUT,
37 				      TEE_PARAM_TYPE_MEMREF_INPUT,
38 				      TEE_PARAM_TYPE_NONE,
39 				      TEE_PARAM_TYPE_NONE);
40 	memset(params, 0, sizeof(params));
41 	params[0].value.a = *id;
42 	params[1].memref.buffer = buf;
43 	params[1].memref.size = len;
44 	res = invoke_gprof_pta(PTA_GPROF_SEND, param_types, params);
45 	if (res == TEE_SUCCESS)
46 		*id = params[0].value.a;
47 	return res;
48 }
49 
__pta_gprof_pc_sampling_start(void * buf,size_t len,size_t offset,size_t scale)50 TEE_Result __pta_gprof_pc_sampling_start(void *buf, size_t len, size_t offset,
51 					 size_t scale)
52 {
53 	TEE_Param params[TEE_NUM_PARAMS];
54 	uint32_t param_types;
55 
56 	param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
57 				      TEE_PARAM_TYPE_VALUE_INPUT,
58 				      TEE_PARAM_TYPE_NONE,
59 				      TEE_PARAM_TYPE_NONE);
60 	memset(params, 0, sizeof(params));
61 	params[0].memref.buffer = buf;
62 	params[0].memref.size = len;
63 	params[1].value.a = offset;
64 	params[1].value.b = scale;
65 	return invoke_gprof_pta(PTA_GPROF_START_PC_SAMPLING, param_types,
66 				params);
67 }
68 
__pta_gprof_pc_sampling_stop(uint32_t * rate)69 TEE_Result __pta_gprof_pc_sampling_stop(uint32_t *rate)
70 {
71 	TEE_Param params[TEE_NUM_PARAMS];
72 	uint32_t param_types;
73 	TEE_Result res;
74 
75 	param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_OUTPUT,
76 				      TEE_PARAM_TYPE_NONE,
77 				      TEE_PARAM_TYPE_NONE,
78 				      TEE_PARAM_TYPE_NONE);
79 	memset(params, 0, sizeof(params));
80 	res = invoke_gprof_pta(PTA_GPROF_STOP_PC_SAMPLING, param_types,
81 				params);
82 	if (res != TEE_SUCCESS)
83 		return res;
84 	if (rate)
85 		*rate = params[0].value.a;
86 	return res;
87 }
88 
__pta_gprof_fini(void)89 void __pta_gprof_fini(void)
90 {
91 	if (sess)
92 		TEE_CloseTASession(sess);
93 }
94