xref: /optee_os/lib/libutee/arch/arm/gprof/gprof_pta.c (revision 817466cb476de705a8e3dabe1ef165fe27a18c2f)
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 
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, 0, 0, NULL, &sess, NULL);
21 		if (res != TEE_SUCCESS)
22 			return res;
23 	}
24 	res = TEE_InvokeTACommand(sess, 0, cmd_id, param_types, params, NULL);
25 	return res;
26 }
27 
28 TEE_Result __pta_gprof_send(void *buf, size_t len, uint32_t *id)
29 {
30 	TEE_Param params[TEE_NUM_PARAMS];
31 	uint32_t param_types;
32 	TEE_Result res;
33 
34 	param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INOUT,
35 				      TEE_PARAM_TYPE_MEMREF_INPUT,
36 				      TEE_PARAM_TYPE_NONE,
37 				      TEE_PARAM_TYPE_NONE);
38 	memset(params, 0, sizeof(params));
39 	params[0].value.a = *id;
40 	params[1].memref.buffer = buf;
41 	params[1].memref.size = len;
42 	res = invoke_gprof_pta(PTA_GPROF_SEND, param_types, params);
43 	if (res == TEE_SUCCESS)
44 		*id = params[0].value.a;
45 	return res;
46 }
47 
48 TEE_Result __pta_gprof_pc_sampling_start(void *buf, size_t len, size_t offset,
49 					 size_t scale)
50 {
51 	TEE_Param params[TEE_NUM_PARAMS];
52 	uint32_t param_types;
53 
54 	param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
55 				      TEE_PARAM_TYPE_VALUE_INPUT,
56 				      TEE_PARAM_TYPE_NONE,
57 				      TEE_PARAM_TYPE_NONE);
58 	memset(params, 0, sizeof(params));
59 	params[0].memref.buffer = buf;
60 	params[0].memref.size = len;
61 	params[1].value.a = offset;
62 	params[1].value.b = scale;
63 	return invoke_gprof_pta(PTA_GPROF_START_PC_SAMPLING, param_types,
64 				params);
65 }
66 
67 TEE_Result __pta_gprof_pc_sampling_stop(uint32_t *rate)
68 {
69 	TEE_Param params[TEE_NUM_PARAMS];
70 	uint32_t param_types;
71 	TEE_Result res;
72 
73 	param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_OUTPUT,
74 				      TEE_PARAM_TYPE_NONE,
75 				      TEE_PARAM_TYPE_NONE,
76 				      TEE_PARAM_TYPE_NONE);
77 	memset(params, 0, sizeof(params));
78 	res = invoke_gprof_pta(PTA_GPROF_STOP_PC_SAMPLING, param_types,
79 				params);
80 	if (res != TEE_SUCCESS)
81 		return res;
82 	if (rate)
83 		*rate = params[0].value.a;
84 	return res;
85 }
86 
87 void __pta_gprof_fini(void)
88 {
89 	if (sess)
90 		TEE_CloseTASession(sess);
91 }
92