xref: /rk3399_ARM-atf/include/lib/pmf/pmf_helpers.h (revision 5695cfe77f8e4fa41261a0a6f99dabcbfdc4f2cb)
1 /*
2  * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of ARM nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific
16  * prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef __PMF_HELPERS_H__
32 #define __PMF_HELPERS_H__
33 
34 #include <arch_helpers.h>
35 #include <assert.h>
36 #include <platform.h>
37 #include <pmf.h>
38 #include <stdint.h>
39 #include <stddef.h>
40 
41 /*
42  * Prototype for PMF service functions.
43  */
44 typedef int (*pmf_svc_init_t)(void);
45 typedef unsigned long long (*pmf_svc_get_ts_t)(unsigned int tid,
46 		 u_register_t mpidr,
47 		 unsigned int flags);
48 
49 /*
50  * This is the definition of PMF service desc.
51  */
52 typedef struct pmf_svc_desc {
53 	/* Structure version information */
54 	param_header_t h;
55 
56 	/* Name of the PMF service */
57 	const char *name;
58 
59 	/* PMF service config: Implementer id, Service id and total id*/
60 	unsigned int svc_config;
61 
62 	/* PMF service initialization handler */
63 	pmf_svc_init_t init;
64 
65 	/* PMF service time-stamp retrieval handler */
66 	pmf_svc_get_ts_t get_ts;
67 } pmf_svc_desc_t;
68 
69 /*
70  * Convenience macro to allocate memory for a PMF service.
71  */
72 #define PMF_ALLOCATE_TIMESTAMP_MEMORY(_name, _total_id)		\
73 	unsigned long long pmf_ts_mem_ ## _name[_total_id]	\
74 	__section("pmf_timestamp_array") __used;
75 
76 /*
77  * Convenience macro to validate tid index for the given TS array.
78  */
79 #define PMF_VALIDATE_TID(_name, _tid)	\
80 	assert((_tid & PMF_TID_MASK) < (ARRAY_SIZE(pmf_ts_mem_ ## _name)))
81 
82 /*
83  * Convenience macros for capturing time-stamp.
84  */
85 #define PMF_DEFINE_CAPTURE_TIMESTAMP(_name, _flags)			\
86 	void pmf_capture_timestamp_ ## _name(				\
87 			unsigned int tid,				\
88 			unsigned long long ts)				\
89 	{								\
90 		CASSERT(_flags, select_proper_config);			\
91 		PMF_VALIDATE_TID(_name, tid);				\
92 		uintptr_t base_addr = (uintptr_t) pmf_ts_mem_ ## _name;	\
93 		if ((_flags) & PMF_STORE_ENABLE)			\
94 			__pmf_store_timestamp(base_addr, tid, ts);	\
95 		if ((_flags) & PMF_DUMP_ENABLE)				\
96 			__pmf_dump_timestamp(tid, ts);			\
97 	}								\
98 	void pmf_capture_timestamp_with_cache_maint_ ## _name(		\
99 			unsigned int tid,				\
100 			unsigned long long ts)				\
101 	{								\
102 		CASSERT(_flags, select_proper_config);			\
103 		PMF_VALIDATE_TID(_name, tid);				\
104 		uintptr_t base_addr = (uintptr_t) pmf_ts_mem_ ## _name;	\
105 		if ((_flags) & PMF_STORE_ENABLE)			\
106 			__pmf_store_timestamp_with_cache_maint(base_addr, tid, ts);\
107 		if ((_flags) & PMF_DUMP_ENABLE)				\
108 			__pmf_dump_timestamp(tid, ts);			\
109 	}
110 
111 /*
112  * Convenience macros for retrieving time-stamp.
113  */
114 #define PMF_DEFINE_GET_TIMESTAMP(_name)					\
115 	unsigned long long pmf_get_timestamp_by_index_ ## _name(	\
116 		unsigned int tid, unsigned int cpuid, unsigned int flags)\
117 	{								\
118 		PMF_VALIDATE_TID(_name, tid);				\
119 		uintptr_t base_addr = (uintptr_t) pmf_ts_mem_ ## _name;	\
120 		return __pmf_get_timestamp(base_addr, tid, cpuid, flags);\
121 	}								\
122 	unsigned long long pmf_get_timestamp_by_mpidr_ ## _name(	\
123 		unsigned int tid, u_register_t mpidr, unsigned int flags)\
124 	{								\
125 		PMF_VALIDATE_TID(_name, tid);				\
126 		uintptr_t base_addr = (uintptr_t) pmf_ts_mem_ ## _name;	\
127 		return __pmf_get_timestamp(base_addr, tid,		\
128 			plat_core_pos_by_mpidr(mpidr), flags);		\
129 	}
130 
131 /*
132  * Convenience macro to register a PMF service.
133  * This is needed for services that require SMC handling.
134  */
135 #define PMF_DEFINE_SERVICE_DESC(_name, _implid, _svcid, _totalid,	\
136 		_init, _getts_by_mpidr) 				\
137 	static const pmf_svc_desc_t __pmf_desc_ ## _name 		\
138 	__section("pmf_svc_descs") __used = {		 		\
139 		.h.type = PARAM_EP, 					\
140 		.h.version = VERSION_1, 				\
141 		.h.size = sizeof(pmf_svc_desc_t),			\
142 		.h.attr = 0,						\
143 		.name = #_name, 					\
144 		.svc_config = ((((_implid) << PMF_IMPL_ID_SHIFT) &	\
145 						PMF_IMPL_ID_MASK) |	\
146 				(((_svcid) << PMF_SVC_ID_SHIFT) &	\
147 						PMF_SVC_ID_MASK) |	\
148 				(((_totalid) << PMF_TID_SHIFT) &	\
149 						PMF_TID_MASK)),		\
150 		.init = _init,						\
151 		.get_ts = _getts_by_mpidr				\
152 	};
153 
154 /* PMF internal functions */
155 void __pmf_dump_timestamp(unsigned int tid, unsigned long long ts);
156 void __pmf_store_timestamp(uintptr_t base_addr,
157 		unsigned int tid,
158 		unsigned long long ts);
159 void __pmf_store_timestamp_with_cache_maint(uintptr_t base_addr,
160 		unsigned int tid,
161 		unsigned long long ts);
162 unsigned long long __pmf_get_timestamp(uintptr_t base_addr,
163 		unsigned int tid,
164 		unsigned int cpuid,
165 		unsigned int flags);
166 #endif /* __PMF_HELPERS_H__ */
167