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