xref: /optee_os/lib/libutee/arch/arm/gprof/gprof_pta.c (revision 8e81e2f5366a971afdd2ac47fb8529d1def5feb0)
1 /*
2  * Copyright (c) 2016, Linaro Limited
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <pta_gprof.h>
29 #include <string.h>
30 #include <tee_api.h>
31 #include "gprof_pta.h"
32 
33 static TEE_TASessionHandle sess = TEE_HANDLE_NULL;
34 
35 static TEE_Result invoke_gprof_pta(uint32_t cmd_id, uint32_t param_types,
36 				   TEE_Param params[TEE_NUM_PARAMS])
37 {
38 	static const TEE_UUID core_uuid = PTA_GPROF_UUID;
39 	TEE_Result res;
40 
41 	if (!sess) {
42 		res = TEE_OpenTASession(&core_uuid, 0, 0, NULL, &sess, NULL);
43 		if (res != TEE_SUCCESS)
44 			return res;
45 	}
46 	res = TEE_InvokeTACommand(sess, 0, cmd_id, param_types, params, NULL);
47 	return res;
48 }
49 
50 TEE_Result __pta_gprof_send(void *buf, size_t len, uint32_t *id)
51 {
52 	TEE_Param params[TEE_NUM_PARAMS];
53 	uint32_t param_types;
54 	TEE_Result res;
55 
56 	param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INOUT,
57 				      TEE_PARAM_TYPE_MEMREF_INPUT,
58 				      TEE_PARAM_TYPE_NONE,
59 				      TEE_PARAM_TYPE_NONE);
60 	memset(params, 0, sizeof(params));
61 	params[0].value.a = *id;
62 	params[1].memref.buffer = buf;
63 	params[1].memref.size = len;
64 	res = invoke_gprof_pta(PTA_GPROF_SEND, param_types, params);
65 	if (res == TEE_SUCCESS)
66 		*id = params[0].value.a;
67 	return res;
68 }
69 
70 TEE_Result __pta_gprof_pc_sampling_start(void *buf, size_t len, size_t offset,
71 					 size_t scale)
72 {
73 	TEE_Param params[TEE_NUM_PARAMS];
74 	uint32_t param_types;
75 
76 	param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
77 				      TEE_PARAM_TYPE_VALUE_INPUT,
78 				      TEE_PARAM_TYPE_NONE,
79 				      TEE_PARAM_TYPE_NONE);
80 	memset(params, 0, sizeof(params));
81 	params[0].memref.buffer = buf;
82 	params[0].memref.size = len;
83 	params[1].value.a = offset;
84 	params[1].value.b = scale;
85 	return invoke_gprof_pta(PTA_GPROF_START_PC_SAMPLING, param_types,
86 				params);
87 }
88 
89 TEE_Result __pta_gprof_pc_sampling_stop(uint32_t *rate)
90 {
91 	TEE_Param params[TEE_NUM_PARAMS];
92 	uint32_t param_types;
93 	TEE_Result res;
94 
95 	param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_OUTPUT,
96 				      TEE_PARAM_TYPE_NONE,
97 				      TEE_PARAM_TYPE_NONE,
98 				      TEE_PARAM_TYPE_NONE);
99 	memset(params, 0, sizeof(params));
100 	res = invoke_gprof_pta(PTA_GPROF_STOP_PC_SAMPLING, param_types,
101 				params);
102 	if (res != TEE_SUCCESS)
103 		return res;
104 	if (rate)
105 		*rate = params[0].value.a;
106 	return res;
107 }
108 
109 void __pta_gprof_fini(void)
110 {
111 	if (sess)
112 		TEE_CloseTASession(sess);
113 }
114