Merge tag 'mtd/for-5.11' of git://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux
[linux-2.6-microblaze.git] / include / linux / scmi_protocol.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * SCMI Message Protocol driver header
4  *
5  * Copyright (C) 2018 ARM Ltd.
6  */
7
8 #ifndef _LINUX_SCMI_PROTOCOL_H
9 #define _LINUX_SCMI_PROTOCOL_H
10
11 #include <linux/device.h>
12 #include <linux/notifier.h>
13 #include <linux/types.h>
14
15 #define SCMI_MAX_STR_SIZE       16
16 #define SCMI_MAX_NUM_RATES      16
17
18 /**
19  * struct scmi_revision_info - version information structure
20  *
21  * @major_ver: Major ABI version. Change here implies risk of backward
22  *      compatibility break.
23  * @minor_ver: Minor ABI version. Change here implies new feature addition,
24  *      or compatible change in ABI.
25  * @num_protocols: Number of protocols that are implemented, excluding the
26  *      base protocol.
27  * @num_agents: Number of agents in the system.
28  * @impl_ver: A vendor-specific implementation version.
29  * @vendor_id: A vendor identifier(Null terminated ASCII string)
30  * @sub_vendor_id: A sub-vendor identifier(Null terminated ASCII string)
31  */
32 struct scmi_revision_info {
33         u16 major_ver;
34         u16 minor_ver;
35         u8 num_protocols;
36         u8 num_agents;
37         u32 impl_ver;
38         char vendor_id[SCMI_MAX_STR_SIZE];
39         char sub_vendor_id[SCMI_MAX_STR_SIZE];
40 };
41
42 struct scmi_clock_info {
43         char name[SCMI_MAX_STR_SIZE];
44         bool rate_discrete;
45         union {
46                 struct {
47                         int num_rates;
48                         u64 rates[SCMI_MAX_NUM_RATES];
49                 } list;
50                 struct {
51                         u64 min_rate;
52                         u64 max_rate;
53                         u64 step_size;
54                 } range;
55         };
56 };
57
58 struct scmi_handle;
59
60 /**
61  * struct scmi_clk_ops - represents the various operations provided
62  *      by SCMI Clock Protocol
63  *
64  * @count_get: get the count of clocks provided by SCMI
65  * @info_get: get the information of the specified clock
66  * @rate_get: request the current clock rate of a clock
67  * @rate_set: set the clock rate of a clock
68  * @enable: enables the specified clock
69  * @disable: disables the specified clock
70  */
71 struct scmi_clk_ops {
72         int (*count_get)(const struct scmi_handle *handle);
73
74         const struct scmi_clock_info *(*info_get)
75                 (const struct scmi_handle *handle, u32 clk_id);
76         int (*rate_get)(const struct scmi_handle *handle, u32 clk_id,
77                         u64 *rate);
78         int (*rate_set)(const struct scmi_handle *handle, u32 clk_id,
79                         u64 rate);
80         int (*enable)(const struct scmi_handle *handle, u32 clk_id);
81         int (*disable)(const struct scmi_handle *handle, u32 clk_id);
82 };
83
84 /**
85  * struct scmi_perf_ops - represents the various operations provided
86  *      by SCMI Performance Protocol
87  *
88  * @limits_set: sets limits on the performance level of a domain
89  * @limits_get: gets limits on the performance level of a domain
90  * @level_set: sets the performance level of a domain
91  * @level_get: gets the performance level of a domain
92  * @device_domain_id: gets the scmi domain id for a given device
93  * @transition_latency_get: gets the DVFS transition latency for a given device
94  * @device_opps_add: adds all the OPPs for a given device
95  * @freq_set: sets the frequency for a given device using sustained frequency
96  *      to sustained performance level mapping
97  * @freq_get: gets the frequency for a given device using sustained frequency
98  *      to sustained performance level mapping
99  * @est_power_get: gets the estimated power cost for a given performance domain
100  *      at a given frequency
101  */
102 struct scmi_perf_ops {
103         int (*limits_set)(const struct scmi_handle *handle, u32 domain,
104                           u32 max_perf, u32 min_perf);
105         int (*limits_get)(const struct scmi_handle *handle, u32 domain,
106                           u32 *max_perf, u32 *min_perf);
107         int (*level_set)(const struct scmi_handle *handle, u32 domain,
108                          u32 level, bool poll);
109         int (*level_get)(const struct scmi_handle *handle, u32 domain,
110                          u32 *level, bool poll);
111         int (*device_domain_id)(struct device *dev);
112         int (*transition_latency_get)(const struct scmi_handle *handle,
113                                       struct device *dev);
114         int (*device_opps_add)(const struct scmi_handle *handle,
115                                struct device *dev);
116         int (*freq_set)(const struct scmi_handle *handle, u32 domain,
117                         unsigned long rate, bool poll);
118         int (*freq_get)(const struct scmi_handle *handle, u32 domain,
119                         unsigned long *rate, bool poll);
120         int (*est_power_get)(const struct scmi_handle *handle, u32 domain,
121                              unsigned long *rate, unsigned long *power);
122         bool (*fast_switch_possible)(const struct scmi_handle *handle,
123                                      struct device *dev);
124         bool (*power_scale_mw_get)(const struct scmi_handle *handle);
125 };
126
127 /**
128  * struct scmi_power_ops - represents the various operations provided
129  *      by SCMI Power Protocol
130  *
131  * @num_domains_get: get the count of power domains provided by SCMI
132  * @name_get: gets the name of a power domain
133  * @state_set: sets the power state of a power domain
134  * @state_get: gets the power state of a power domain
135  */
136 struct scmi_power_ops {
137         int (*num_domains_get)(const struct scmi_handle *handle);
138         char *(*name_get)(const struct scmi_handle *handle, u32 domain);
139 #define SCMI_POWER_STATE_TYPE_SHIFT     30
140 #define SCMI_POWER_STATE_ID_MASK        (BIT(28) - 1)
141 #define SCMI_POWER_STATE_PARAM(type, id) \
142         ((((type) & BIT(0)) << SCMI_POWER_STATE_TYPE_SHIFT) | \
143                 ((id) & SCMI_POWER_STATE_ID_MASK))
144 #define SCMI_POWER_STATE_GENERIC_ON     SCMI_POWER_STATE_PARAM(0, 0)
145 #define SCMI_POWER_STATE_GENERIC_OFF    SCMI_POWER_STATE_PARAM(1, 0)
146         int (*state_set)(const struct scmi_handle *handle, u32 domain,
147                          u32 state);
148         int (*state_get)(const struct scmi_handle *handle, u32 domain,
149                          u32 *state);
150 };
151
152 struct scmi_sensor_info {
153         u32 id;
154         u8 type;
155         s8 scale;
156         u8 num_trip_points;
157         bool async;
158         char name[SCMI_MAX_STR_SIZE];
159 };
160
161 /*
162  * Partial list from Distributed Management Task Force (DMTF) specification:
163  * DSP0249 (Platform Level Data Model specification)
164  */
165 enum scmi_sensor_class {
166         NONE = 0x0,
167         TEMPERATURE_C = 0x2,
168         VOLTAGE = 0x5,
169         CURRENT = 0x6,
170         POWER = 0x7,
171         ENERGY = 0x8,
172 };
173
174 /**
175  * struct scmi_sensor_ops - represents the various operations provided
176  *      by SCMI Sensor Protocol
177  *
178  * @count_get: get the count of sensors provided by SCMI
179  * @info_get: get the information of the specified sensor
180  * @trip_point_config: selects and configures a trip-point of interest
181  * @reading_get: gets the current value of the sensor
182  */
183 struct scmi_sensor_ops {
184         int (*count_get)(const struct scmi_handle *handle);
185         const struct scmi_sensor_info *(*info_get)
186                 (const struct scmi_handle *handle, u32 sensor_id);
187         int (*trip_point_config)(const struct scmi_handle *handle,
188                                  u32 sensor_id, u8 trip_id, u64 trip_value);
189         int (*reading_get)(const struct scmi_handle *handle, u32 sensor_id,
190                            u64 *value);
191 };
192
193 /**
194  * struct scmi_reset_ops - represents the various operations provided
195  *      by SCMI Reset Protocol
196  *
197  * @num_domains_get: get the count of reset domains provided by SCMI
198  * @name_get: gets the name of a reset domain
199  * @latency_get: gets the reset latency for the specified reset domain
200  * @reset: resets the specified reset domain
201  * @assert: explicitly assert reset signal of the specified reset domain
202  * @deassert: explicitly deassert reset signal of the specified reset domain
203  */
204 struct scmi_reset_ops {
205         int (*num_domains_get)(const struct scmi_handle *handle);
206         char *(*name_get)(const struct scmi_handle *handle, u32 domain);
207         int (*latency_get)(const struct scmi_handle *handle, u32 domain);
208         int (*reset)(const struct scmi_handle *handle, u32 domain);
209         int (*assert)(const struct scmi_handle *handle, u32 domain);
210         int (*deassert)(const struct scmi_handle *handle, u32 domain);
211 };
212
213 /**
214  * struct scmi_voltage_info - describe one available SCMI Voltage Domain
215  *
216  * @id: the domain ID as advertised by the platform
217  * @segmented: defines the layout of the entries of array @levels_uv.
218  *             - when True the entries are to be interpreted as triplets,
219  *               each defining a segment representing a range of equally
220  *               space voltages: <lowest_volts>, <highest_volt>, <step_uV>
221  *             - when False the entries simply represent a single discrete
222  *               supported voltage level
223  * @negative_volts_allowed: True if any of the entries of @levels_uv represent
224  *                          a negative voltage.
225  * @attributes: represents Voltage Domain advertised attributes
226  * @name: name assigned to the Voltage Domain by platform
227  * @num_levels: number of total entries in @levels_uv.
228  * @levels_uv: array of entries describing the available voltage levels for
229  *             this domain.
230  */
231 struct scmi_voltage_info {
232         unsigned int id;
233         bool segmented;
234         bool negative_volts_allowed;
235         unsigned int attributes;
236         char name[SCMI_MAX_STR_SIZE];
237         unsigned int num_levels;
238 #define SCMI_VOLTAGE_SEGMENT_LOW        0
239 #define SCMI_VOLTAGE_SEGMENT_HIGH       1
240 #define SCMI_VOLTAGE_SEGMENT_STEP       2
241         int *levels_uv;
242 };
243
244 /**
245  * struct scmi_voltage_ops - represents the various operations provided
246  * by SCMI Voltage Protocol
247  *
248  * @num_domains_get: get the count of voltage domains provided by SCMI
249  * @info_get: get the information of the specified domain
250  * @config_set: set the config for the specified domain
251  * @config_get: get the config of the specified domain
252  * @level_set: set the voltage level for the specified domain
253  * @level_get: get the voltage level of the specified domain
254  */
255 struct scmi_voltage_ops {
256         int (*num_domains_get)(const struct scmi_handle *handle);
257         const struct scmi_voltage_info __must_check *(*info_get)
258                 (const struct scmi_handle *handle, u32 domain_id);
259         int (*config_set)(const struct scmi_handle *handle, u32 domain_id,
260                           u32 config);
261 #define SCMI_VOLTAGE_ARCH_STATE_OFF             0x0
262 #define SCMI_VOLTAGE_ARCH_STATE_ON              0x7
263         int (*config_get)(const struct scmi_handle *handle, u32 domain_id,
264                           u32 *config);
265         int (*level_set)(const struct scmi_handle *handle, u32 domain_id,
266                          u32 flags, s32 volt_uV);
267         int (*level_get)(const struct scmi_handle *handle, u32 domain_id,
268                          s32 *volt_uV);
269 };
270
271 /**
272  * struct scmi_notify_ops  - represents notifications' operations provided by
273  * SCMI core
274  * @register_event_notifier: Register a notifier_block for the requested event
275  * @unregister_event_notifier: Unregister a notifier_block for the requested
276  *                             event
277  *
278  * A user can register/unregister its own notifier_block against the wanted
279  * platform instance regarding the desired event identified by the
280  * tuple: (proto_id, evt_id, src_id) using the provided register/unregister
281  * interface where:
282  *
283  * @handle: The handle identifying the platform instance to use
284  * @proto_id: The protocol ID as in SCMI Specification
285  * @evt_id: The message ID of the desired event as in SCMI Specification
286  * @src_id: A pointer to the desired source ID if different sources are
287  *          possible for the protocol (like domain_id, sensor_id...etc)
288  *
289  * @src_id can be provided as NULL if it simply does NOT make sense for
290  * the protocol at hand, OR if the user is explicitly interested in
291  * receiving notifications from ANY existent source associated to the
292  * specified proto_id / evt_id.
293  *
294  * Received notifications are finally delivered to the registered users,
295  * invoking the callback provided with the notifier_block *nb as follows:
296  *
297  *      int user_cb(nb, evt_id, report)
298  *
299  * with:
300  *
301  * @nb: The notifier block provided by the user
302  * @evt_id: The message ID of the delivered event
303  * @report: A custom struct describing the specific event delivered
304  */
305 struct scmi_notify_ops {
306         int (*register_event_notifier)(const struct scmi_handle *handle,
307                                        u8 proto_id, u8 evt_id, u32 *src_id,
308                                        struct notifier_block *nb);
309         int (*unregister_event_notifier)(const struct scmi_handle *handle,
310                                          u8 proto_id, u8 evt_id, u32 *src_id,
311                                          struct notifier_block *nb);
312 };
313
314 /**
315  * struct scmi_handle - Handle returned to ARM SCMI clients for usage.
316  *
317  * @dev: pointer to the SCMI device
318  * @version: pointer to the structure containing SCMI version information
319  * @power_ops: pointer to set of power protocol operations
320  * @perf_ops: pointer to set of performance protocol operations
321  * @clk_ops: pointer to set of clock protocol operations
322  * @sensor_ops: pointer to set of sensor protocol operations
323  * @reset_ops: pointer to set of reset protocol operations
324  * @voltage_ops: pointer to set of voltage protocol operations
325  * @notify_ops: pointer to set of notifications related operations
326  * @perf_priv: pointer to private data structure specific to performance
327  *      protocol(for internal use only)
328  * @clk_priv: pointer to private data structure specific to clock
329  *      protocol(for internal use only)
330  * @power_priv: pointer to private data structure specific to power
331  *      protocol(for internal use only)
332  * @sensor_priv: pointer to private data structure specific to sensors
333  *      protocol(for internal use only)
334  * @reset_priv: pointer to private data structure specific to reset
335  *      protocol(for internal use only)
336  * @voltage_priv: pointer to private data structure specific to voltage
337  *      protocol(for internal use only)
338  * @notify_priv: pointer to private data structure specific to notifications
339  *      (for internal use only)
340  */
341 struct scmi_handle {
342         struct device *dev;
343         struct scmi_revision_info *version;
344         const struct scmi_perf_ops *perf_ops;
345         const struct scmi_clk_ops *clk_ops;
346         const struct scmi_power_ops *power_ops;
347         const struct scmi_sensor_ops *sensor_ops;
348         const struct scmi_reset_ops *reset_ops;
349         const struct scmi_voltage_ops *voltage_ops;
350         const struct scmi_notify_ops *notify_ops;
351         /* for protocol internal use */
352         void *perf_priv;
353         void *clk_priv;
354         void *power_priv;
355         void *sensor_priv;
356         void *reset_priv;
357         void *voltage_priv;
358         void *notify_priv;
359         void *system_priv;
360 };
361
362 enum scmi_std_protocol {
363         SCMI_PROTOCOL_BASE = 0x10,
364         SCMI_PROTOCOL_POWER = 0x11,
365         SCMI_PROTOCOL_SYSTEM = 0x12,
366         SCMI_PROTOCOL_PERF = 0x13,
367         SCMI_PROTOCOL_CLOCK = 0x14,
368         SCMI_PROTOCOL_SENSOR = 0x15,
369         SCMI_PROTOCOL_RESET = 0x16,
370         SCMI_PROTOCOL_VOLTAGE = 0x17,
371 };
372
373 enum scmi_system_events {
374         SCMI_SYSTEM_SHUTDOWN,
375         SCMI_SYSTEM_COLDRESET,
376         SCMI_SYSTEM_WARMRESET,
377         SCMI_SYSTEM_POWERUP,
378         SCMI_SYSTEM_SUSPEND,
379         SCMI_SYSTEM_MAX
380 };
381
382 struct scmi_device {
383         u32 id;
384         u8 protocol_id;
385         const char *name;
386         struct device dev;
387         struct scmi_handle *handle;
388 };
389
390 #define to_scmi_dev(d) container_of(d, struct scmi_device, dev)
391
392 struct scmi_device *
393 scmi_device_create(struct device_node *np, struct device *parent, int protocol,
394                    const char *name);
395 void scmi_device_destroy(struct scmi_device *scmi_dev);
396
397 struct scmi_device_id {
398         u8 protocol_id;
399         const char *name;
400 };
401
402 struct scmi_driver {
403         const char *name;
404         int (*probe)(struct scmi_device *sdev);
405         void (*remove)(struct scmi_device *sdev);
406         const struct scmi_device_id *id_table;
407
408         struct device_driver driver;
409 };
410
411 #define to_scmi_driver(d) container_of(d, struct scmi_driver, driver)
412
413 #if IS_REACHABLE(CONFIG_ARM_SCMI_PROTOCOL)
414 int scmi_driver_register(struct scmi_driver *driver,
415                          struct module *owner, const char *mod_name);
416 void scmi_driver_unregister(struct scmi_driver *driver);
417 #else
418 static inline int
419 scmi_driver_register(struct scmi_driver *driver, struct module *owner,
420                      const char *mod_name)
421 {
422         return -EINVAL;
423 }
424
425 static inline void scmi_driver_unregister(struct scmi_driver *driver) {}
426 #endif /* CONFIG_ARM_SCMI_PROTOCOL */
427
428 #define scmi_register(driver) \
429         scmi_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
430 #define scmi_unregister(driver) \
431         scmi_driver_unregister(driver)
432
433 /**
434  * module_scmi_driver() - Helper macro for registering a scmi driver
435  * @__scmi_driver: scmi_driver structure
436  *
437  * Helper macro for scmi drivers to set up proper module init / exit
438  * functions.  Replaces module_init() and module_exit() and keeps people from
439  * printing pointless things to the kernel log when their driver is loaded.
440  */
441 #define module_scmi_driver(__scmi_driver)       \
442         module_driver(__scmi_driver, scmi_register, scmi_unregister)
443
444 typedef int (*scmi_prot_init_fn_t)(struct scmi_handle *);
445 int scmi_protocol_register(int protocol_id, scmi_prot_init_fn_t fn);
446 void scmi_protocol_unregister(int protocol_id);
447
448 /* SCMI Notification API - Custom Event Reports */
449 enum scmi_notification_events {
450         SCMI_EVENT_POWER_STATE_CHANGED = 0x0,
451         SCMI_EVENT_PERFORMANCE_LIMITS_CHANGED = 0x0,
452         SCMI_EVENT_PERFORMANCE_LEVEL_CHANGED = 0x1,
453         SCMI_EVENT_SENSOR_TRIP_POINT_EVENT = 0x0,
454         SCMI_EVENT_RESET_ISSUED = 0x0,
455         SCMI_EVENT_BASE_ERROR_EVENT = 0x0,
456         SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER = 0x0,
457 };
458
459 struct scmi_power_state_changed_report {
460         ktime_t         timestamp;
461         unsigned int    agent_id;
462         unsigned int    domain_id;
463         unsigned int    power_state;
464 };
465
466 struct scmi_system_power_state_notifier_report {
467         ktime_t         timestamp;
468         unsigned int    agent_id;
469         unsigned int    flags;
470         unsigned int    system_state;
471 };
472
473 struct scmi_perf_limits_report {
474         ktime_t         timestamp;
475         unsigned int    agent_id;
476         unsigned int    domain_id;
477         unsigned int    range_max;
478         unsigned int    range_min;
479 };
480
481 struct scmi_perf_level_report {
482         ktime_t         timestamp;
483         unsigned int    agent_id;
484         unsigned int    domain_id;
485         unsigned int    performance_level;
486 };
487
488 struct scmi_sensor_trip_point_report {
489         ktime_t         timestamp;
490         unsigned int    agent_id;
491         unsigned int    sensor_id;
492         unsigned int    trip_point_desc;
493 };
494
495 struct scmi_reset_issued_report {
496         ktime_t         timestamp;
497         unsigned int    agent_id;
498         unsigned int    domain_id;
499         unsigned int    reset_state;
500 };
501
502 struct scmi_base_error_report {
503         ktime_t                 timestamp;
504         unsigned int            agent_id;
505         bool                    fatal;
506         unsigned int            cmd_count;
507         unsigned long long      reports[];
508 };
509
510 #endif /* _LINUX_SCMI_PROTOCOL_H */