xref: /rk3399_ARM-atf/include/lib/pmf/pmf_helpers.h (revision 09d40e0e08283a249e7dce0e106c07c5141f9b7e)
1 /*
2  * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef PMF_HELPERS_H
8 #define PMF_HELPERS_H
9 
10 #include <assert.h>
11 #include <stddef.h>
12 #include <stdint.h>
13 
14 #include <arch_helpers.h>
15 #include <common/bl_common.h>
16 #include <plat/common/platform.h>
17 
18 /*
19  * Prototype for PMF service functions.
20  */
21 typedef int (*pmf_svc_init_t)(void);
22 typedef unsigned long long (*pmf_svc_get_ts_t)(unsigned int tid,
23 		 u_register_t mpidr,
24 		 unsigned int flags);
25 
26 /*
27  * This is the definition of PMF service desc.
28  */
29 typedef struct pmf_svc_desc {
30 	/* Structure version information */
31 	param_header_t h;
32 
33 	/* Name of the PMF service */
34 	const char *name;
35 
36 	/* PMF service config: Implementer id, Service id and total id*/
37 	unsigned int svc_config;
38 
39 	/* PMF service initialization handler */
40 	pmf_svc_init_t init;
41 
42 	/* PMF service time-stamp retrieval handler */
43 	pmf_svc_get_ts_t get_ts;
44 } pmf_svc_desc_t;
45 
46 /*
47  * Convenience macro to allocate memory for a PMF service.
48  *
49  * The extern declaration is there to satisfy MISRA C-2012 rule 8.4.
50  */
51 #define PMF_ALLOCATE_TIMESTAMP_MEMORY(_name, _total_id)		\
52 	extern unsigned long long pmf_ts_mem_ ## _name[_total_id];	\
53 	unsigned long long pmf_ts_mem_ ## _name[_total_id]	\
54 	__aligned(CACHE_WRITEBACK_GRANULE)			\
55 	__section("pmf_timestamp_array")			\
56 	__used;
57 
58 /*
59  * Convenience macro to validate tid index for the given TS array.
60  */
61 #define PMF_VALIDATE_TID(_name, _tid)	\
62 	assert((_tid & PMF_TID_MASK) < (ARRAY_SIZE(pmf_ts_mem_ ## _name)))
63 
64 /*
65  * Convenience macros for capturing time-stamp.
66  *
67  * The extern declaration is there to satisfy MISRA C-2012 rule 8.4.
68  */
69 #define PMF_DEFINE_CAPTURE_TIMESTAMP(_name, _flags)			\
70 	void pmf_capture_timestamp_ ## _name(				\
71 			unsigned int tid,				\
72 			unsigned long long ts);				\
73 	void pmf_capture_timestamp_ ## _name(				\
74 			unsigned int tid,				\
75 			unsigned long long ts)				\
76 	{								\
77 		CASSERT(_flags, select_proper_config);			\
78 		PMF_VALIDATE_TID(_name, tid);				\
79 		uintptr_t base_addr = (uintptr_t) pmf_ts_mem_ ## _name;	\
80 		if (((_flags) & PMF_STORE_ENABLE) != 0)			\
81 			__pmf_store_timestamp(base_addr, tid, ts);	\
82 		if (((_flags) & PMF_DUMP_ENABLE) != 0)			\
83 			__pmf_dump_timestamp(tid, ts);			\
84 	}								\
85 	void pmf_capture_timestamp_with_cache_maint_ ## _name(		\
86 			unsigned int tid,				\
87 			unsigned long long ts);				\
88 	void pmf_capture_timestamp_with_cache_maint_ ## _name(		\
89 			unsigned int tid,				\
90 			unsigned long long ts)				\
91 	{								\
92 		CASSERT(_flags, select_proper_config);			\
93 		PMF_VALIDATE_TID(_name, tid);				\
94 		uintptr_t base_addr = (uintptr_t) pmf_ts_mem_ ## _name;	\
95 		if (((_flags) & PMF_STORE_ENABLE) != 0)			\
96 			__pmf_store_timestamp_with_cache_maint(base_addr, tid, ts);\
97 		if (((_flags) & PMF_DUMP_ENABLE) != 0)			\
98 			__pmf_dump_timestamp(tid, ts);			\
99 	}
100 
101 /*
102  * Convenience macros for retrieving time-stamp.
103  *
104  * The extern declaration is there to satisfy MISRA C-2012 rule 8.4.
105  */
106 #define PMF_DEFINE_GET_TIMESTAMP(_name)					\
107 	unsigned long long pmf_get_timestamp_by_index_ ## _name(	\
108 		unsigned int tid, unsigned int cpuid, unsigned int flags);\
109 	unsigned long long pmf_get_timestamp_by_index_ ## _name(	\
110 		unsigned int tid, unsigned int cpuid, unsigned int flags)\
111 	{								\
112 		PMF_VALIDATE_TID(_name, tid);				\
113 		uintptr_t base_addr = (uintptr_t) pmf_ts_mem_ ## _name;	\
114 		return __pmf_get_timestamp(base_addr, tid, cpuid, flags);\
115 	}								\
116 	unsigned long long pmf_get_timestamp_by_mpidr_ ## _name(	\
117 		unsigned int tid, u_register_t mpidr, unsigned int flags);\
118 	unsigned long long pmf_get_timestamp_by_mpidr_ ## _name(	\
119 		unsigned int tid, u_register_t mpidr, unsigned int flags)\
120 	{								\
121 		PMF_VALIDATE_TID(_name, tid);				\
122 		uintptr_t base_addr = (uintptr_t) pmf_ts_mem_ ## _name;	\
123 		return __pmf_get_timestamp(base_addr, tid,		\
124 			plat_core_pos_by_mpidr(mpidr), flags);		\
125 	}
126 
127 /*
128  * Convenience macro to register a PMF service.
129  * This is needed for services that require SMC handling.
130  */
131 #define PMF_DEFINE_SERVICE_DESC(_name, _implid, _svcid, _totalid,	\
132 		_init, _getts_by_mpidr) 				\
133 	static const pmf_svc_desc_t __pmf_desc_ ## _name 		\
134 	__section("pmf_svc_descs") __used = {		 		\
135 		.h.type = PARAM_EP, 					\
136 		.h.version = VERSION_1, 				\
137 		.h.size = sizeof(pmf_svc_desc_t),			\
138 		.h.attr = 0,						\
139 		.name = #_name, 					\
140 		.svc_config = ((((_implid) << PMF_IMPL_ID_SHIFT) &	\
141 						PMF_IMPL_ID_MASK) |	\
142 				(((_svcid) << PMF_SVC_ID_SHIFT) &	\
143 						PMF_SVC_ID_MASK) |	\
144 				(((_totalid) << PMF_TID_SHIFT) &	\
145 						PMF_TID_MASK)),		\
146 		.init = _init,						\
147 		.get_ts = _getts_by_mpidr				\
148 	};
149 
150 /* PMF internal functions */
151 void __pmf_dump_timestamp(unsigned int tid, unsigned long long ts);
152 void __pmf_store_timestamp(uintptr_t base_addr,
153 		unsigned int tid,
154 		unsigned long long ts);
155 void __pmf_store_timestamp_with_cache_maint(uintptr_t base_addr,
156 		unsigned int tid,
157 		unsigned long long ts);
158 unsigned long long __pmf_get_timestamp(uintptr_t base_addr,
159 		unsigned int tid,
160 		unsigned int cpuid,
161 		unsigned int flags);
162 #endif /* PMF_HELPERS_H */
163