xref: /rk3399_ARM-atf/include/lib/pmf/pmf_helpers.h (revision f426fc0519103defb3dcf4a9d86d985d48204424)
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 	__aligned(CACHE_WRITEBACK_GRANULE)			\
75 	__section("pmf_timestamp_array")			\
76 	__used;
77 
78 /*
79  * Convenience macro to validate tid index for the given TS array.
80  */
81 #define PMF_VALIDATE_TID(_name, _tid)	\
82 	assert((_tid & PMF_TID_MASK) < (ARRAY_SIZE(pmf_ts_mem_ ## _name)))
83 
84 /*
85  * Convenience macros for capturing time-stamp.
86  */
87 #define PMF_DEFINE_CAPTURE_TIMESTAMP(_name, _flags)			\
88 	void pmf_capture_timestamp_ ## _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)			\
96 			__pmf_store_timestamp(base_addr, tid, ts);	\
97 		if ((_flags) & PMF_DUMP_ENABLE)				\
98 			__pmf_dump_timestamp(tid, ts);			\
99 	}								\
100 	void pmf_capture_timestamp_with_cache_maint_ ## _name(		\
101 			unsigned int tid,				\
102 			unsigned long long ts)				\
103 	{								\
104 		CASSERT(_flags, select_proper_config);			\
105 		PMF_VALIDATE_TID(_name, tid);				\
106 		uintptr_t base_addr = (uintptr_t) pmf_ts_mem_ ## _name;	\
107 		if ((_flags) & PMF_STORE_ENABLE)			\
108 			__pmf_store_timestamp_with_cache_maint(base_addr, tid, ts);\
109 		if ((_flags) & PMF_DUMP_ENABLE)				\
110 			__pmf_dump_timestamp(tid, ts);			\
111 	}
112 
113 /*
114  * Convenience macros for retrieving time-stamp.
115  */
116 #define PMF_DEFINE_GET_TIMESTAMP(_name)					\
117 	unsigned long long pmf_get_timestamp_by_index_ ## _name(	\
118 		unsigned int tid, unsigned int cpuid, unsigned int flags)\
119 	{								\
120 		PMF_VALIDATE_TID(_name, tid);				\
121 		uintptr_t base_addr = (uintptr_t) pmf_ts_mem_ ## _name;	\
122 		return __pmf_get_timestamp(base_addr, tid, cpuid, flags);\
123 	}								\
124 	unsigned long long pmf_get_timestamp_by_mpidr_ ## _name(	\
125 		unsigned int tid, u_register_t mpidr, unsigned int flags)\
126 	{								\
127 		PMF_VALIDATE_TID(_name, tid);				\
128 		uintptr_t base_addr = (uintptr_t) pmf_ts_mem_ ## _name;	\
129 		return __pmf_get_timestamp(base_addr, tid,		\
130 			plat_core_pos_by_mpidr(mpidr), flags);		\
131 	}
132 
133 /*
134  * Convenience macro to register a PMF service.
135  * This is needed for services that require SMC handling.
136  */
137 #define PMF_DEFINE_SERVICE_DESC(_name, _implid, _svcid, _totalid,	\
138 		_init, _getts_by_mpidr) 				\
139 	static const pmf_svc_desc_t __pmf_desc_ ## _name 		\
140 	__section("pmf_svc_descs") __used = {		 		\
141 		.h.type = PARAM_EP, 					\
142 		.h.version = VERSION_1, 				\
143 		.h.size = sizeof(pmf_svc_desc_t),			\
144 		.h.attr = 0,						\
145 		.name = #_name, 					\
146 		.svc_config = ((((_implid) << PMF_IMPL_ID_SHIFT) &	\
147 						PMF_IMPL_ID_MASK) |	\
148 				(((_svcid) << PMF_SVC_ID_SHIFT) &	\
149 						PMF_SVC_ID_MASK) |	\
150 				(((_totalid) << PMF_TID_SHIFT) &	\
151 						PMF_TID_MASK)),		\
152 		.init = _init,						\
153 		.get_ts = _getts_by_mpidr				\
154 	};
155 
156 /* PMF internal functions */
157 void __pmf_dump_timestamp(unsigned int tid, unsigned long long ts);
158 void __pmf_store_timestamp(uintptr_t base_addr,
159 		unsigned int tid,
160 		unsigned long long ts);
161 void __pmf_store_timestamp_with_cache_maint(uintptr_t base_addr,
162 		unsigned int tid,
163 		unsigned long long ts);
164 unsigned long long __pmf_get_timestamp(uintptr_t base_addr,
165 		unsigned int tid,
166 		unsigned int cpuid,
167 		unsigned int flags);
168 #endif /* __PMF_HELPERS_H__ */
169