1 /*
2  * Arm SCP/MCP Software
3  * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Description:
8  *      SCMI power capping and monitoring protocol completer support.
9  */
10 
11 #ifndef INTERNAL_SCMI_POWER_CAPPING_PROTOCOL_H
12 #define INTERNAL_SCMI_POWER_CAPPING_PROTOCOL_H
13 
14 #include "fwk_event.h"
15 #include "fwk_id.h"
16 #include "internal/scmi_power_capping.h"
17 
18 #include <stdint.h>
19 
20 /*!
21  * \addtogroup GroupModules Modules
22  * \{
23  */
24 
25 /*!
26  * \defgroup GroupSCMI_POWER_CAPPING_PROTOCOL SCMI
27  *     power capping and monitoring Protocol
28  * \{
29  */
30 #define SCMI_PROTOCOL_VERSION_POWER_CAPPING UINT32_C(0x10000)
31 
32 #define SCMI_POWER_CAPPING_NO_PARENT UINT32_C(0xFFFFFFFF)
33 
34 #define SCMI_POWER_CAPPING_FCH_NOT_AVAIL UINT32_C(0)
35 #define SCMI_POWER_CAPPING_FCH_AVAIL     UINT32_C(1)
36 
37 #define SCMI_POWER_CAPPING_CAP_PAI_CHANGE_NOTIF_SUP_POS     31
38 #define SCMI_POWER_CAPPING_POWER_MEASUREMENTS_NOTIF_SUP_POS 30
39 #define SCMI_POWER_CAPPING_ASYNC_SUP_POS                    29
40 #define SCMI_POWER_CAPPING_EXTENDED_DOM_NAME_SUP_POS        28
41 #define SCMI_POWER_CAPPING_CONF_SUP_POS                     27
42 #define SCMI_POWER_CAPPING_MONITOR_SUP_POS                  26
43 #define SCMI_POWER_CAPPING_PAI_CONF_SUP_POS                 25
44 #define SCMI_POWER_CAPPING_POWER_UNIT_POS                   24
45 #define SCMI_POWER_CAPPING_FAST_CHANNEL_SUP_POS             22
46 
47 #define SCMI_POWER_CAPPING_AGENT_ID_PLATFORM 0u
48 
49 enum scmi_power_capping_notification_id {
50     SCMI_POWER_CAPPING_CAP_CHANGED,
51     SCMI_POWER_CAPPING_MEASUREMENTS_CHANGED,
52 };
53 
54 #define SET_PCAP_CONF_SUP(PCAP_CONF_SUP) \
55     (PCAP_CONF_SUP << SCMI_POWER_CAPPING_CONF_SUP_POS)
56 
57 #define POWER_UNIT_MASK (UINT32_C(0x3))
58 
59 #define SET_PAI_CONF_SUP(PAI_CONF_SUP) \
60     (PAI_CONF_SUP << SCMI_POWER_CAPPING_PAI_CONF_SUP_POS)
61 
62 #define SET_POWER_UNIT(POWER_UNIT) \
63     (((POWER_UNIT) & (POWER_UNIT_MASK)) << SCMI_POWER_CAPPING_POWER_UNIT_POS)
64 
65 #define SCMI_POWER_CAPPING_DOMAIN_ATTRIBUTES( \
66     PCAP_CONF_SUP, PAI_CONF_SUP, POWER_UNIT) \
67     (SET_PCAP_CONF_SUP(PCAP_CONF_SUP) | SET_PAI_CONF_SUP(PAI_CONF_SUP) | \
68      SET_POWER_UNIT(POWER_UNIT))
69 
70 #define SCMI_POWER_CAPPING_DOMAIN_FCH_SUPPORT(FAST_CHNL_SUP) \
71     ((FAST_CHNL_SUP) << SCMI_POWER_CAPPING_FAST_CHANNEL_SUP_POS)
72 
73 #define SCMI_POWER_CAPPING_DOMAIN_CAP_PAI_CHANGE_NOTIF_SUPPORT( \
74     CAP_PAI_CHANGE_NOTIF_SUP) \
75     ((CAP_PAI_CHANGE_NOTIF_SUP) \
76      << SCMI_POWER_CAPPING_CAP_PAI_CHANGE_NOTIF_SUP_POS)
77 
78 #define SCMI_POWER_CAPPING_DOMAIN_MEASUREMENTS_NOTIF_SUPPORT( \
79     MEASUREMENTS_CHANGE_NOTIF_SUP) \
80     ((MEASUREMENTS_CHANGE_NOTIF_SUP) \
81      << SCMI_POWER_CAPPING_POWER_MEASUREMENTS_NOTIF_SUP_POS)
82 
83 #define SCMI_POWER_CAPPING_IGN_DEL_RES_FLAG_POS 0
84 #define SCMI_POWER_CAPPING_IGN_DEL_RES_FLAG_MASK \
85     (1 << SCMI_POWER_CAPPING_IGN_DEL_RES_FLAG_POS)
86 
87 #define SCMI_POWER_CAPPING_ASYNC_FLAG_POS 1
88 #define SCMI_POWER_CAPPING_ASYNC_FLAG_MASK \
89     (1 << SCMI_POWER_CAPPING_ASYNC_FLAG_POS)
90 
91 #define SCMI_POWER_CAPPING_INVALID_MASK \
92     (~(SCMI_POWER_CAPPING_IGN_DEL_RES_FLAG_MASK | \
93        SCMI_POWER_CAPPING_ASYNC_FLAG_MASK))
94 
95 #define SCMI_POWER_CAPPING_DISABLE_CAP_VALUE ((uint32_t)0)
96 
97 #define SCMI_POWER_CAPPING_PAI_RESERVED_FLAG 0u
98 /*
99  * Power capping Domain attributes
100  */
101 #define SCMI_POWER_CAPPING_DOMAIN_NAME_LEN 16
102 
103 struct scmi_power_capping_domain_attributes_a2p {
104     uint32_t domain_id;
105 };
106 
107 struct scmi_power_capping_domain_attributes_p2a {
108     int32_t status;
109     uint32_t attributes;
110     uint8_t name[SCMI_POWER_CAPPING_DOMAIN_NAME_LEN];
111     uint32_t min_pai;
112     uint32_t max_pai;
113     uint32_t pai_step;
114     uint32_t min_power_cap;
115     uint32_t max_power_cap;
116     uint32_t power_cap_step;
117     uint32_t max_sustainable_power;
118     uint32_t accuracy;
119     uint32_t parent_id;
120 };
121 
122 /*
123  * Power capping get
124  */
125 struct scmi_power_capping_cap_get_a2p {
126     uint32_t domain_id;
127 };
128 
129 struct scmi_power_capping_cap_get_p2a {
130     int32_t status;
131     uint32_t power_cap;
132 };
133 
134 /*
135  * Power capping set
136  */
137 struct scmi_power_capping_cap_set_a2p {
138     uint32_t domain_id;
139     uint32_t flags;
140     uint32_t power_cap;
141 };
142 
143 struct scmi_power_capping_cap_set_p2a {
144     int32_t status;
145 };
146 
147 struct scmi_power_capping_cap_set_complete_p2a {
148     int32_t status;
149     uint32_t domain_id;
150     uint32_t power_cap;
151 };
152 
153 /*
154  * PAI get
155  */
156 struct scmi_power_capping_pai_get_a2p {
157     uint32_t domain_id;
158 };
159 
160 struct scmi_power_capping_pai_get_p2a {
161     int32_t status;
162     uint32_t pai;
163 };
164 
165 /*
166  * PAI set
167  */
168 struct scmi_power_capping_pai_set_a2p {
169     uint32_t domain_id;
170     uint32_t flags;
171     uint32_t pai;
172 };
173 
174 struct scmi_power_capping_pai_set_p2a {
175     int32_t status;
176 };
177 
178 /*
179  * Measurements get
180  */
181 struct scmi_power_capping_measurements_get_a2p {
182     uint32_t domain_id;
183 };
184 
185 struct scmi_power_capping_measurements_get_p2a {
186     int32_t status;
187     uint32_t power;
188     uint32_t pai;
189 };
190 
191 /*
192  * Describe fast channels
193  */
194 struct scmi_power_capping_describe_fc_a2p {
195     uint32_t domain_id;
196     uint32_t message_id;
197 };
198 
199 struct scmi_power_capping_describe_fc_p2a {
200     int32_t status;
201     uint32_t attributes;
202     uint32_t rate_limit;
203     uint32_t chan_addr_low;
204     uint32_t chan_addr_high;
205     uint32_t chan_size;
206     uint32_t doorbell_addr_low;
207     uint32_t doorbell_addr_high;
208     uint32_t doorbell_set_mask_low;
209     uint32_t doorbell_set_mask_high;
210     uint32_t doorbell_preserve_mask_low;
211     uint32_t doorbell_preserve_mask_high;
212 };
213 
214 /*
215  * Cap notify
216  */
217 struct scmi_power_capping_cap_notify_a2p {
218     uint32_t domain_id;
219     uint32_t notify_enable;
220 };
221 
222 struct scmi_power_capping_cap_notify_p2a {
223     int32_t status;
224 };
225 
226 /*
227  * Cap changed
228  */
229 struct scmi_power_capping_cap_changed_p2a {
230     uint32_t agent_id;
231     uint32_t domain_id;
232     uint32_t cap;
233     uint32_t pai;
234 };
235 
236 /*
237  * Power measurement notify
238  */
239 struct scmi_power_capping_measurements_notify_a2p {
240     uint32_t domain_id;
241     uint32_t notify_enable;
242     uint32_t threshold_low;
243     uint32_t threshold_high;
244 };
245 
246 struct scmi_power_capping_measurements_notify_p2a {
247     int32_t status;
248 };
249 
250 /*
251  * Power measurement changed
252  */
253 struct scmi_power_capping_measurements_changed_p2a {
254     uint32_t agent_id;
255     uint32_t domain_id;
256     uint32_t power;
257 };
258 
259 /*
260  * Framework interface.
261  */
262 void pcapping_protocol_init(struct mod_scmi_power_capping_context *ctx);
263 
264 int pcapping_protocol_domain_init(
265     uint32_t domain_idx,
266     const struct mod_scmi_power_capping_domain_config *config);
267 
268 int pcapping_protocol_bind(void);
269 
270 int pcapping_protocol_start(fwk_id_t id);
271 
272 int pcapping_protocol_process_fwk_notification(const struct fwk_event *event);
273 
274 int pcapping_protocol_process_bind_request(fwk_id_t api_id, const void **api);
275 
276 void pcapping_protocol_set_power_apis(
277     struct mod_scmi_power_capping_power_apis *power_management_apis);
278 
279 #ifdef BUILD_HAS_SCMI_NOTIFICATIONS
280 int pcapping_protocol_process_cap_pai_notify_event(
281     const struct fwk_event *event);
282 
283 int pcapping_protocol_process_measurements_notify_event(
284     const struct fwk_event *event);
285 #endif
286 
287 /*!
288  * \}
289  */
290 
291 /*!
292  * \}
293  */
294 
295 #endif /* INTERNAL_SCMI_POWER_CAPPING_PROTOCOL_H */
296