Merge tag 'pm-6.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[linux-block.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-2021 ARM Ltd.
6  */
7
8 #ifndef _LINUX_SCMI_PROTOCOL_H
9 #define _LINUX_SCMI_PROTOCOL_H
10
11 #include <linux/bitfield.h>
12 #include <linux/device.h>
13 #include <linux/notifier.h>
14 #include <linux/types.h>
15
16 #define SCMI_MAX_STR_SIZE               64
17 #define SCMI_SHORT_NAME_MAX_SIZE        16
18 #define SCMI_MAX_NUM_RATES              16
19
20 /**
21  * struct scmi_revision_info - version information structure
22  *
23  * @major_ver: Major ABI version. Change here implies risk of backward
24  *      compatibility break.
25  * @minor_ver: Minor ABI version. Change here implies new feature addition,
26  *      or compatible change in ABI.
27  * @num_protocols: Number of protocols that are implemented, excluding the
28  *      base protocol.
29  * @num_agents: Number of agents in the system.
30  * @impl_ver: A vendor-specific implementation version.
31  * @vendor_id: A vendor identifier(Null terminated ASCII string)
32  * @sub_vendor_id: A sub-vendor identifier(Null terminated ASCII string)
33  */
34 struct scmi_revision_info {
35         u16 major_ver;
36         u16 minor_ver;
37         u8 num_protocols;
38         u8 num_agents;
39         u32 impl_ver;
40         char vendor_id[SCMI_SHORT_NAME_MAX_SIZE];
41         char sub_vendor_id[SCMI_SHORT_NAME_MAX_SIZE];
42 };
43
44 struct scmi_clock_info {
45         char name[SCMI_MAX_STR_SIZE];
46         unsigned int enable_latency;
47         bool rate_discrete;
48         bool rate_changed_notifications;
49         bool rate_change_requested_notifications;
50         bool state_ctrl_forbidden;
51         bool rate_ctrl_forbidden;
52         bool parent_ctrl_forbidden;
53         bool extended_config;
54         union {
55                 struct {
56                         int num_rates;
57                         u64 rates[SCMI_MAX_NUM_RATES];
58                 } list;
59                 struct {
60                         u64 min_rate;
61                         u64 max_rate;
62                         u64 step_size;
63                 } range;
64         };
65         int num_parents;
66         u32 *parents;
67 };
68
69 enum scmi_power_scale {
70         SCMI_POWER_BOGOWATTS,
71         SCMI_POWER_MILLIWATTS,
72         SCMI_POWER_MICROWATTS
73 };
74
75 struct scmi_handle;
76 struct scmi_device;
77 struct scmi_protocol_handle;
78
79 enum scmi_clock_oem_config {
80         SCMI_CLOCK_CFG_DUTY_CYCLE = 0x1,
81         SCMI_CLOCK_CFG_PHASE,
82         SCMI_CLOCK_CFG_OEM_START = 0x80,
83         SCMI_CLOCK_CFG_OEM_END = 0xFF,
84 };
85
86 /**
87  * struct scmi_clk_proto_ops - represents the various operations provided
88  *      by SCMI Clock Protocol
89  *
90  * @count_get: get the count of clocks provided by SCMI
91  * @info_get: get the information of the specified clock
92  * @rate_get: request the current clock rate of a clock
93  * @rate_set: set the clock rate of a clock
94  * @enable: enables the specified clock
95  * @disable: disables the specified clock
96  * @state_get: get the status of the specified clock
97  * @config_oem_get: get the value of an OEM specific clock config
98  * @config_oem_set: set the value of an OEM specific clock config
99  * @parent_get: get the parent id of a clk
100  * @parent_set: set the parent of a clock
101  */
102 struct scmi_clk_proto_ops {
103         int (*count_get)(const struct scmi_protocol_handle *ph);
104
105         const struct scmi_clock_info __must_check *(*info_get)
106                 (const struct scmi_protocol_handle *ph, u32 clk_id);
107         int (*rate_get)(const struct scmi_protocol_handle *ph, u32 clk_id,
108                         u64 *rate);
109         int (*rate_set)(const struct scmi_protocol_handle *ph, u32 clk_id,
110                         u64 rate);
111         int (*enable)(const struct scmi_protocol_handle *ph, u32 clk_id,
112                       bool atomic);
113         int (*disable)(const struct scmi_protocol_handle *ph, u32 clk_id,
114                        bool atomic);
115         int (*state_get)(const struct scmi_protocol_handle *ph, u32 clk_id,
116                          bool *enabled, bool atomic);
117         int (*config_oem_get)(const struct scmi_protocol_handle *ph, u32 clk_id,
118                               enum scmi_clock_oem_config oem_type,
119                               u32 *oem_val, u32 *attributes, bool atomic);
120         int (*config_oem_set)(const struct scmi_protocol_handle *ph, u32 clk_id,
121                               enum scmi_clock_oem_config oem_type,
122                               u32 oem_val, bool atomic);
123         int (*parent_get)(const struct scmi_protocol_handle *ph, u32 clk_id, u32 *parent_id);
124         int (*parent_set)(const struct scmi_protocol_handle *ph, u32 clk_id, u32 parent_id);
125 };
126
127 struct scmi_perf_domain_info {
128         char name[SCMI_MAX_STR_SIZE];
129         bool set_perf;
130 };
131
132 /**
133  * struct scmi_perf_proto_ops - represents the various operations provided
134  *      by SCMI Performance Protocol
135  *
136  * @num_domains_get: gets the number of supported performance domains
137  * @info_get: get the information of a performance domain
138  * @limits_set: sets limits on the performance level of a domain
139  * @limits_get: gets limits on the performance level of a domain
140  * @level_set: sets the performance level of a domain
141  * @level_get: gets the performance level of a domain
142  * @transition_latency_get: gets the DVFS transition latency for a given device
143  * @rate_limit_get: gets the minimum time (us) required between successive
144  *      requests
145  * @device_opps_add: adds all the OPPs for a given device
146  * @freq_set: sets the frequency for a given device using sustained frequency
147  *      to sustained performance level mapping
148  * @freq_get: gets the frequency for a given device using sustained frequency
149  *      to sustained performance level mapping
150  * @est_power_get: gets the estimated power cost for a given performance domain
151  *      at a given frequency
152  * @fast_switch_possible: indicates if fast DVFS switching is possible or not
153  *      for a given device
154  * @fast_switch_rate_limit: gets the minimum time (us) required between
155  *      successive fast_switching requests
156  * @power_scale_mw_get: indicates if the power values provided are in milliWatts
157  *      or in some other (abstract) scale
158  */
159 struct scmi_perf_proto_ops {
160         int (*num_domains_get)(const struct scmi_protocol_handle *ph);
161         const struct scmi_perf_domain_info __must_check *(*info_get)
162                 (const struct scmi_protocol_handle *ph, u32 domain);
163         int (*limits_set)(const struct scmi_protocol_handle *ph, u32 domain,
164                           u32 max_perf, u32 min_perf);
165         int (*limits_get)(const struct scmi_protocol_handle *ph, u32 domain,
166                           u32 *max_perf, u32 *min_perf);
167         int (*level_set)(const struct scmi_protocol_handle *ph, u32 domain,
168                          u32 level, bool poll);
169         int (*level_get)(const struct scmi_protocol_handle *ph, u32 domain,
170                          u32 *level, bool poll);
171         int (*transition_latency_get)(const struct scmi_protocol_handle *ph,
172                                       u32 domain);
173         int (*rate_limit_get)(const struct scmi_protocol_handle *ph,
174                               u32 domain, u32 *rate_limit);
175         int (*device_opps_add)(const struct scmi_protocol_handle *ph,
176                                struct device *dev, u32 domain);
177         int (*freq_set)(const struct scmi_protocol_handle *ph, u32 domain,
178                         unsigned long rate, bool poll);
179         int (*freq_get)(const struct scmi_protocol_handle *ph, u32 domain,
180                         unsigned long *rate, bool poll);
181         int (*est_power_get)(const struct scmi_protocol_handle *ph, u32 domain,
182                              unsigned long *rate, unsigned long *power);
183         bool (*fast_switch_possible)(const struct scmi_protocol_handle *ph,
184                                      u32 domain);
185         int (*fast_switch_rate_limit)(const struct scmi_protocol_handle *ph,
186                                       u32 domain, u32 *rate_limit);
187         enum scmi_power_scale (*power_scale_get)(const struct scmi_protocol_handle *ph);
188 };
189
190 /**
191  * struct scmi_power_proto_ops - represents the various operations provided
192  *      by SCMI Power Protocol
193  *
194  * @num_domains_get: get the count of power domains provided by SCMI
195  * @name_get: gets the name of a power domain
196  * @state_set: sets the power state of a power domain
197  * @state_get: gets the power state of a power domain
198  */
199 struct scmi_power_proto_ops {
200         int (*num_domains_get)(const struct scmi_protocol_handle *ph);
201         const char *(*name_get)(const struct scmi_protocol_handle *ph,
202                                 u32 domain);
203 #define SCMI_POWER_STATE_TYPE_SHIFT     30
204 #define SCMI_POWER_STATE_ID_MASK        (BIT(28) - 1)
205 #define SCMI_POWER_STATE_PARAM(type, id) \
206         ((((type) & BIT(0)) << SCMI_POWER_STATE_TYPE_SHIFT) | \
207                 ((id) & SCMI_POWER_STATE_ID_MASK))
208 #define SCMI_POWER_STATE_GENERIC_ON     SCMI_POWER_STATE_PARAM(0, 0)
209 #define SCMI_POWER_STATE_GENERIC_OFF    SCMI_POWER_STATE_PARAM(1, 0)
210         int (*state_set)(const struct scmi_protocol_handle *ph, u32 domain,
211                          u32 state);
212         int (*state_get)(const struct scmi_protocol_handle *ph, u32 domain,
213                          u32 *state);
214 };
215
216 /**
217  * struct scmi_sensor_reading  - represent a timestamped read
218  *
219  * Used by @reading_get_timestamped method.
220  *
221  * @value: The signed value sensor read.
222  * @timestamp: An unsigned timestamp for the sensor read, as provided by
223  *             SCMI platform. Set to zero when not available.
224  */
225 struct scmi_sensor_reading {
226         long long value;
227         unsigned long long timestamp;
228 };
229
230 /**
231  * struct scmi_range_attrs  - specifies a sensor or axis values' range
232  * @min_range: The minimum value which can be represented by the sensor/axis.
233  * @max_range: The maximum value which can be represented by the sensor/axis.
234  */
235 struct scmi_range_attrs {
236         long long min_range;
237         long long max_range;
238 };
239
240 /**
241  * struct scmi_sensor_axis_info  - describes one sensor axes
242  * @id: The axes ID.
243  * @type: Axes type. Chosen amongst one of @enum scmi_sensor_class.
244  * @scale: Power-of-10 multiplier applied to the axis unit.
245  * @name: NULL-terminated string representing axes name as advertised by
246  *        SCMI platform.
247  * @extended_attrs: Flag to indicate the presence of additional extended
248  *                  attributes for this axes.
249  * @resolution: Extended attribute representing the resolution of the axes.
250  *              Set to 0 if not reported by this axes.
251  * @exponent: Extended attribute representing the power-of-10 multiplier that
252  *            is applied to the resolution field. Set to 0 if not reported by
253  *            this axes.
254  * @attrs: Extended attributes representing minimum and maximum values
255  *         measurable by this axes. Set to 0 if not reported by this sensor.
256  */
257 struct scmi_sensor_axis_info {
258         unsigned int id;
259         unsigned int type;
260         int scale;
261         char name[SCMI_MAX_STR_SIZE];
262         bool extended_attrs;
263         unsigned int resolution;
264         int exponent;
265         struct scmi_range_attrs attrs;
266 };
267
268 /**
269  * struct scmi_sensor_intervals_info  - describes number and type of available
270  *      update intervals
271  * @segmented: Flag for segmented intervals' representation. When True there
272  *             will be exactly 3 intervals in @desc, with each entry
273  *             representing a member of a segment in this order:
274  *             {lowest update interval, highest update interval, step size}
275  * @count: Number of intervals described in @desc.
276  * @desc: Array of @count interval descriptor bitmask represented as detailed in
277  *        the SCMI specification: it can be accessed using the accompanying
278  *        macros.
279  * @prealloc_pool: A minimal preallocated pool of desc entries used to avoid
280  *                 lesser-than-64-bytes dynamic allocation for small @count
281  *                 values.
282  */
283 struct scmi_sensor_intervals_info {
284         bool segmented;
285         unsigned int count;
286 #define SCMI_SENS_INTVL_SEGMENT_LOW     0
287 #define SCMI_SENS_INTVL_SEGMENT_HIGH    1
288 #define SCMI_SENS_INTVL_SEGMENT_STEP    2
289         unsigned int *desc;
290 #define SCMI_SENS_INTVL_GET_SECS(x)             FIELD_GET(GENMASK(20, 5), (x))
291 #define SCMI_SENS_INTVL_GET_EXP(x)                                      \
292         ({                                                              \
293                 int __signed_exp = FIELD_GET(GENMASK(4, 0), (x));       \
294                                                                         \
295                 if (__signed_exp & BIT(4))                              \
296                         __signed_exp |= GENMASK(31, 5);                 \
297                 __signed_exp;                                           \
298         })
299 #define SCMI_MAX_PREALLOC_POOL                  16
300         unsigned int prealloc_pool[SCMI_MAX_PREALLOC_POOL];
301 };
302
303 /**
304  * struct scmi_sensor_info - represents information related to one of the
305  * available sensors.
306  * @id: Sensor ID.
307  * @type: Sensor type. Chosen amongst one of @enum scmi_sensor_class.
308  * @scale: Power-of-10 multiplier applied to the sensor unit.
309  * @num_trip_points: Number of maximum configurable trip points.
310  * @async: Flag for asynchronous read support.
311  * @update: Flag for continuouos update notification support.
312  * @timestamped: Flag for timestamped read support.
313  * @tstamp_scale: Power-of-10 multiplier applied to the sensor timestamps to
314  *                represent it in seconds.
315  * @num_axis: Number of supported axis if any. Reported as 0 for scalar sensors.
316  * @axis: Pointer to an array of @num_axis descriptors.
317  * @intervals: Descriptor of available update intervals.
318  * @sensor_config: A bitmask reporting the current sensor configuration as
319  *                 detailed in the SCMI specification: it can accessed and
320  *                 modified through the accompanying macros.
321  * @name: NULL-terminated string representing sensor name as advertised by
322  *        SCMI platform.
323  * @extended_scalar_attrs: Flag to indicate the presence of additional extended
324  *                         attributes for this sensor.
325  * @sensor_power: Extended attribute representing the average power
326  *                consumed by the sensor in microwatts (uW) when it is active.
327  *                Reported here only for scalar sensors.
328  *                Set to 0 if not reported by this sensor.
329  * @resolution: Extended attribute representing the resolution of the sensor.
330  *              Reported here only for scalar sensors.
331  *              Set to 0 if not reported by this sensor.
332  * @exponent: Extended attribute representing the power-of-10 multiplier that is
333  *            applied to the resolution field.
334  *            Reported here only for scalar sensors.
335  *            Set to 0 if not reported by this sensor.
336  * @scalar_attrs: Extended attributes representing minimum and maximum
337  *                measurable values by this sensor.
338  *                Reported here only for scalar sensors.
339  *                Set to 0 if not reported by this sensor.
340  */
341 struct scmi_sensor_info {
342         unsigned int id;
343         unsigned int type;
344         int scale;
345         unsigned int num_trip_points;
346         bool async;
347         bool update;
348         bool timestamped;
349         int tstamp_scale;
350         unsigned int num_axis;
351         struct scmi_sensor_axis_info *axis;
352         struct scmi_sensor_intervals_info intervals;
353         unsigned int sensor_config;
354 #define SCMI_SENS_CFG_UPDATE_SECS_MASK          GENMASK(31, 16)
355 #define SCMI_SENS_CFG_GET_UPDATE_SECS(x)                                \
356         FIELD_GET(SCMI_SENS_CFG_UPDATE_SECS_MASK, (x))
357
358 #define SCMI_SENS_CFG_UPDATE_EXP_MASK           GENMASK(15, 11)
359 #define SCMI_SENS_CFG_GET_UPDATE_EXP(x)                                 \
360         ({                                                              \
361                 int __signed_exp =                                      \
362                         FIELD_GET(SCMI_SENS_CFG_UPDATE_EXP_MASK, (x));  \
363                                                                         \
364                 if (__signed_exp & BIT(4))                              \
365                         __signed_exp |= GENMASK(31, 5);                 \
366                 __signed_exp;                                           \
367         })
368
369 #define SCMI_SENS_CFG_ROUND_MASK                GENMASK(10, 9)
370 #define SCMI_SENS_CFG_ROUND_AUTO                2
371 #define SCMI_SENS_CFG_ROUND_UP                  1
372 #define SCMI_SENS_CFG_ROUND_DOWN                0
373
374 #define SCMI_SENS_CFG_TSTAMP_ENABLED_MASK       BIT(1)
375 #define SCMI_SENS_CFG_TSTAMP_ENABLE             1
376 #define SCMI_SENS_CFG_TSTAMP_DISABLE            0
377 #define SCMI_SENS_CFG_IS_TSTAMP_ENABLED(x)                              \
378         FIELD_GET(SCMI_SENS_CFG_TSTAMP_ENABLED_MASK, (x))
379
380 #define SCMI_SENS_CFG_SENSOR_ENABLED_MASK       BIT(0)
381 #define SCMI_SENS_CFG_SENSOR_ENABLE             1
382 #define SCMI_SENS_CFG_SENSOR_DISABLE            0
383         char name[SCMI_MAX_STR_SIZE];
384 #define SCMI_SENS_CFG_IS_ENABLED(x)             FIELD_GET(BIT(0), (x))
385         bool extended_scalar_attrs;
386         unsigned int sensor_power;
387         unsigned int resolution;
388         int exponent;
389         struct scmi_range_attrs scalar_attrs;
390 };
391
392 /*
393  * Partial list from Distributed Management Task Force (DMTF) specification:
394  * DSP0249 (Platform Level Data Model specification)
395  */
396 enum scmi_sensor_class {
397         NONE = 0x0,
398         UNSPEC = 0x1,
399         TEMPERATURE_C = 0x2,
400         TEMPERATURE_F = 0x3,
401         TEMPERATURE_K = 0x4,
402         VOLTAGE = 0x5,
403         CURRENT = 0x6,
404         POWER = 0x7,
405         ENERGY = 0x8,
406         CHARGE = 0x9,
407         VOLTAMPERE = 0xA,
408         NITS = 0xB,
409         LUMENS = 0xC,
410         LUX = 0xD,
411         CANDELAS = 0xE,
412         KPA = 0xF,
413         PSI = 0x10,
414         NEWTON = 0x11,
415         CFM = 0x12,
416         RPM = 0x13,
417         HERTZ = 0x14,
418         SECS = 0x15,
419         MINS = 0x16,
420         HOURS = 0x17,
421         DAYS = 0x18,
422         WEEKS = 0x19,
423         MILS = 0x1A,
424         INCHES = 0x1B,
425         FEET = 0x1C,
426         CUBIC_INCHES = 0x1D,
427         CUBIC_FEET = 0x1E,
428         METERS = 0x1F,
429         CUBIC_CM = 0x20,
430         CUBIC_METERS = 0x21,
431         LITERS = 0x22,
432         FLUID_OUNCES = 0x23,
433         RADIANS = 0x24,
434         STERADIANS = 0x25,
435         REVOLUTIONS = 0x26,
436         CYCLES = 0x27,
437         GRAVITIES = 0x28,
438         OUNCES = 0x29,
439         POUNDS = 0x2A,
440         FOOT_POUNDS = 0x2B,
441         OUNCE_INCHES = 0x2C,
442         GAUSS = 0x2D,
443         GILBERTS = 0x2E,
444         HENRIES = 0x2F,
445         FARADS = 0x30,
446         OHMS = 0x31,
447         SIEMENS = 0x32,
448         MOLES = 0x33,
449         BECQUERELS = 0x34,
450         PPM = 0x35,
451         DECIBELS = 0x36,
452         DBA = 0x37,
453         DBC = 0x38,
454         GRAYS = 0x39,
455         SIEVERTS = 0x3A,
456         COLOR_TEMP_K = 0x3B,
457         BITS = 0x3C,
458         BYTES = 0x3D,
459         WORDS = 0x3E,
460         DWORDS = 0x3F,
461         QWORDS = 0x40,
462         PERCENTAGE = 0x41,
463         PASCALS = 0x42,
464         COUNTS = 0x43,
465         GRAMS = 0x44,
466         NEWTON_METERS = 0x45,
467         HITS = 0x46,
468         MISSES = 0x47,
469         RETRIES = 0x48,
470         OVERRUNS = 0x49,
471         UNDERRUNS = 0x4A,
472         COLLISIONS = 0x4B,
473         PACKETS = 0x4C,
474         MESSAGES = 0x4D,
475         CHARS = 0x4E,
476         ERRORS = 0x4F,
477         CORRECTED_ERRS = 0x50,
478         UNCORRECTABLE_ERRS = 0x51,
479         SQ_MILS = 0x52,
480         SQ_INCHES = 0x53,
481         SQ_FEET = 0x54,
482         SQ_CM = 0x55,
483         SQ_METERS = 0x56,
484         RADIANS_SEC = 0x57,
485         BPM = 0x58,
486         METERS_SEC_SQUARED = 0x59,
487         METERS_SEC = 0x5A,
488         CUBIC_METERS_SEC = 0x5B,
489         MM_MERCURY = 0x5C,
490         RADIANS_SEC_SQUARED = 0x5D,
491         OEM_UNIT = 0xFF
492 };
493
494 /**
495  * struct scmi_sensor_proto_ops - represents the various operations provided
496  *      by SCMI Sensor Protocol
497  *
498  * @count_get: get the count of sensors provided by SCMI
499  * @info_get: get the information of the specified sensor
500  * @trip_point_config: selects and configures a trip-point of interest
501  * @reading_get: gets the current value of the sensor
502  * @reading_get_timestamped: gets the current value and timestamp, when
503  *                           available, of the sensor. (as of v3.0 spec)
504  *                           Supports multi-axis sensors for sensors which
505  *                           supports it and if the @reading array size of
506  *                           @count entry equals the sensor num_axis
507  * @config_get: Get sensor current configuration
508  * @config_set: Set sensor current configuration
509  */
510 struct scmi_sensor_proto_ops {
511         int (*count_get)(const struct scmi_protocol_handle *ph);
512         const struct scmi_sensor_info __must_check *(*info_get)
513                 (const struct scmi_protocol_handle *ph, u32 sensor_id);
514         int (*trip_point_config)(const struct scmi_protocol_handle *ph,
515                                  u32 sensor_id, u8 trip_id, u64 trip_value);
516         int (*reading_get)(const struct scmi_protocol_handle *ph, u32 sensor_id,
517                            u64 *value);
518         int (*reading_get_timestamped)(const struct scmi_protocol_handle *ph,
519                                        u32 sensor_id, u8 count,
520                                        struct scmi_sensor_reading *readings);
521         int (*config_get)(const struct scmi_protocol_handle *ph,
522                           u32 sensor_id, u32 *sensor_config);
523         int (*config_set)(const struct scmi_protocol_handle *ph,
524                           u32 sensor_id, u32 sensor_config);
525 };
526
527 /**
528  * struct scmi_reset_proto_ops - represents the various operations provided
529  *      by SCMI Reset Protocol
530  *
531  * @num_domains_get: get the count of reset domains provided by SCMI
532  * @name_get: gets the name of a reset domain
533  * @latency_get: gets the reset latency for the specified reset domain
534  * @reset: resets the specified reset domain
535  * @assert: explicitly assert reset signal of the specified reset domain
536  * @deassert: explicitly deassert reset signal of the specified reset domain
537  */
538 struct scmi_reset_proto_ops {
539         int (*num_domains_get)(const struct scmi_protocol_handle *ph);
540         const char *(*name_get)(const struct scmi_protocol_handle *ph,
541                                 u32 domain);
542         int (*latency_get)(const struct scmi_protocol_handle *ph, u32 domain);
543         int (*reset)(const struct scmi_protocol_handle *ph, u32 domain);
544         int (*assert)(const struct scmi_protocol_handle *ph, u32 domain);
545         int (*deassert)(const struct scmi_protocol_handle *ph, u32 domain);
546 };
547
548 enum scmi_voltage_level_mode {
549         SCMI_VOLTAGE_LEVEL_SET_AUTO,
550         SCMI_VOLTAGE_LEVEL_SET_SYNC,
551 };
552
553 /**
554  * struct scmi_voltage_info - describe one available SCMI Voltage Domain
555  *
556  * @id: the domain ID as advertised by the platform
557  * @segmented: defines the layout of the entries of array @levels_uv.
558  *             - when True the entries are to be interpreted as triplets,
559  *               each defining a segment representing a range of equally
560  *               space voltages: <lowest_volts>, <highest_volt>, <step_uV>
561  *             - when False the entries simply represent a single discrete
562  *               supported voltage level
563  * @negative_volts_allowed: True if any of the entries of @levels_uv represent
564  *                          a negative voltage.
565  * @async_level_set: True when the voltage domain supports asynchronous level
566  *                   set commands.
567  * @name: name assigned to the Voltage Domain by platform
568  * @num_levels: number of total entries in @levels_uv.
569  * @levels_uv: array of entries describing the available voltage levels for
570  *             this domain.
571  */
572 struct scmi_voltage_info {
573         unsigned int id;
574         bool segmented;
575         bool negative_volts_allowed;
576         bool async_level_set;
577         char name[SCMI_MAX_STR_SIZE];
578         unsigned int num_levels;
579 #define SCMI_VOLTAGE_SEGMENT_LOW        0
580 #define SCMI_VOLTAGE_SEGMENT_HIGH       1
581 #define SCMI_VOLTAGE_SEGMENT_STEP       2
582         int *levels_uv;
583 };
584
585 /**
586  * struct scmi_voltage_proto_ops - represents the various operations provided
587  * by SCMI Voltage Protocol
588  *
589  * @num_domains_get: get the count of voltage domains provided by SCMI
590  * @info_get: get the information of the specified domain
591  * @config_set: set the config for the specified domain
592  * @config_get: get the config of the specified domain
593  * @level_set: set the voltage level for the specified domain
594  * @level_get: get the voltage level of the specified domain
595  */
596 struct scmi_voltage_proto_ops {
597         int (*num_domains_get)(const struct scmi_protocol_handle *ph);
598         const struct scmi_voltage_info __must_check *(*info_get)
599                 (const struct scmi_protocol_handle *ph, u32 domain_id);
600         int (*config_set)(const struct scmi_protocol_handle *ph, u32 domain_id,
601                           u32 config);
602 #define SCMI_VOLTAGE_ARCH_STATE_OFF             0x0
603 #define SCMI_VOLTAGE_ARCH_STATE_ON              0x7
604         int (*config_get)(const struct scmi_protocol_handle *ph, u32 domain_id,
605                           u32 *config);
606         int (*level_set)(const struct scmi_protocol_handle *ph, u32 domain_id,
607                          enum scmi_voltage_level_mode mode, s32 volt_uV);
608         int (*level_get)(const struct scmi_protocol_handle *ph, u32 domain_id,
609                          s32 *volt_uV);
610 };
611
612 /**
613  * struct scmi_powercap_info  - Describe one available Powercap domain
614  *
615  * @id: Domain ID as advertised by the platform.
616  * @notify_powercap_cap_change: CAP change notification support.
617  * @notify_powercap_measurement_change: MEASUREMENTS change notifications
618  *                                     support.
619  * @async_powercap_cap_set: Asynchronous CAP set support.
620  * @powercap_cap_config: CAP configuration support.
621  * @powercap_monitoring: Monitoring (measurements) support.
622  * @powercap_pai_config: PAI configuration support.
623  * @powercap_scale_mw: Domain reports power data in milliwatt units.
624  * @powercap_scale_uw: Domain reports power data in microwatt units.
625  *                     Note that, when both @powercap_scale_mw and
626  *                     @powercap_scale_uw are set to false, the domain
627  *                     reports power data on an abstract linear scale.
628  * @name: name assigned to the Powercap Domain by platform.
629  * @min_pai: Minimum configurable PAI.
630  * @max_pai: Maximum configurable PAI.
631  * @pai_step: Step size between two consecutive PAI values.
632  * @min_power_cap: Minimum configurable CAP.
633  * @max_power_cap: Maximum configurable CAP.
634  * @power_cap_step: Step size between two consecutive CAP values.
635  * @sustainable_power: Maximum sustainable power consumption for this domain
636  *                     under normal conditions.
637  * @accuracy: The accuracy with which the power is measured and reported in
638  *            integral multiples of 0.001 percent.
639  * @parent_id: Identifier of the containing parent power capping domain, or the
640  *             value 0xFFFFFFFF if this powercap domain is a root domain not
641  *             contained in any other domain.
642  */
643 struct scmi_powercap_info {
644         unsigned int id;
645         bool notify_powercap_cap_change;
646         bool notify_powercap_measurement_change;
647         bool async_powercap_cap_set;
648         bool powercap_cap_config;
649         bool powercap_monitoring;
650         bool powercap_pai_config;
651         bool powercap_scale_mw;
652         bool powercap_scale_uw;
653         bool fastchannels;
654         char name[SCMI_MAX_STR_SIZE];
655         unsigned int min_pai;
656         unsigned int max_pai;
657         unsigned int pai_step;
658         unsigned int min_power_cap;
659         unsigned int max_power_cap;
660         unsigned int power_cap_step;
661         unsigned int sustainable_power;
662         unsigned int accuracy;
663 #define SCMI_POWERCAP_ROOT_ZONE_ID     0xFFFFFFFFUL
664         unsigned int parent_id;
665         struct scmi_fc_info *fc_info;
666 };
667
668 /**
669  * struct scmi_powercap_proto_ops - represents the various operations provided
670  * by SCMI Powercap Protocol
671  *
672  * @num_domains_get: get the count of powercap domains provided by SCMI.
673  * @info_get: get the information for the specified domain.
674  * @cap_get: get the current CAP value for the specified domain.
675  *           On SCMI platforms supporting powercap zone disabling, this could
676  *           report a zero value for a zone where powercapping is disabled.
677  * @cap_set: set the CAP value for the specified domain to the provided value;
678  *           if the domain supports setting the CAP with an asynchronous command
679  *           this request will finally trigger an asynchronous transfer, but, if
680  *           @ignore_dresp here is set to true, this call will anyway return
681  *           immediately without waiting for the related delayed response.
682  *           Note that the powercap requested value must NOT be zero, even if
683  *           the platform supports disabling a powercap by setting its cap to
684  *           zero (since SCMI v3.2): there are dedicated operations that should
685  *           be used for that. (@cap_enable_set/get)
686  * @cap_enable_set: enable or disable the powercapping on the specified domain,
687  *                  if supported by the SCMI platform implementation.
688  *                  Note that, by the SCMI specification, the platform can
689  *                  silently ignore our disable request and decide to enforce
690  *                  anyway some other powercap value requested by another agent
691  *                  on the system: for this reason @cap_get and @cap_enable_get
692  *                  will always report the final platform view of the powercaps.
693  * @cap_enable_get: get the current CAP enable status for the specified domain.
694  * @pai_get: get the current PAI value for the specified domain.
695  * @pai_set: set the PAI value for the specified domain to the provided value.
696  * @measurements_get: retrieve the current average power measurements for the
697  *                    specified domain and the related PAI upon which is
698  *                    calculated.
699  * @measurements_threshold_set: set the desired low and high power thresholds
700  *                              to be used when registering for notification
701  *                              of type POWERCAP_MEASUREMENTS_NOTIFY with this
702  *                              powercap domain.
703  *                              Note that this must be called at least once
704  *                              before registering any callback with the usual
705  *                              @scmi_notify_ops; moreover, in case this method
706  *                              is called with measurement notifications already
707  *                              enabled it will also trigger, transparently, a
708  *                              proper update of the power thresholds configured
709  *                              in the SCMI backend server.
710  * @measurements_threshold_get: get the currently configured low and high power
711  *                              thresholds used when registering callbacks for
712  *                              notification POWERCAP_MEASUREMENTS_NOTIFY.
713  */
714 struct scmi_powercap_proto_ops {
715         int (*num_domains_get)(const struct scmi_protocol_handle *ph);
716         const struct scmi_powercap_info __must_check *(*info_get)
717                 (const struct scmi_protocol_handle *ph, u32 domain_id);
718         int (*cap_get)(const struct scmi_protocol_handle *ph, u32 domain_id,
719                        u32 *power_cap);
720         int (*cap_set)(const struct scmi_protocol_handle *ph, u32 domain_id,
721                        u32 power_cap, bool ignore_dresp);
722         int (*cap_enable_set)(const struct scmi_protocol_handle *ph,
723                               u32 domain_id, bool enable);
724         int (*cap_enable_get)(const struct scmi_protocol_handle *ph,
725                               u32 domain_id, bool *enable);
726         int (*pai_get)(const struct scmi_protocol_handle *ph, u32 domain_id,
727                        u32 *pai);
728         int (*pai_set)(const struct scmi_protocol_handle *ph, u32 domain_id,
729                        u32 pai);
730         int (*measurements_get)(const struct scmi_protocol_handle *ph,
731                                 u32 domain_id, u32 *average_power, u32 *pai);
732         int (*measurements_threshold_set)(const struct scmi_protocol_handle *ph,
733                                           u32 domain_id, u32 power_thresh_low,
734                                           u32 power_thresh_high);
735         int (*measurements_threshold_get)(const struct scmi_protocol_handle *ph,
736                                           u32 domain_id, u32 *power_thresh_low,
737                                           u32 *power_thresh_high);
738 };
739
740 /**
741  * struct scmi_notify_ops  - represents notifications' operations provided by
742  * SCMI core
743  * @devm_event_notifier_register: Managed registration of a notifier_block for
744  *                                the requested event
745  * @devm_event_notifier_unregister: Managed unregistration of a notifier_block
746  *                                  for the requested event
747  * @event_notifier_register: Register a notifier_block for the requested event
748  * @event_notifier_unregister: Unregister a notifier_block for the requested
749  *                             event
750  *
751  * A user can register/unregister its own notifier_block against the wanted
752  * platform instance regarding the desired event identified by the
753  * tuple: (proto_id, evt_id, src_id) using the provided register/unregister
754  * interface where:
755  *
756  * @sdev: The scmi_device to use when calling the devres managed ops devm_
757  * @handle: The handle identifying the platform instance to use, when not
758  *          calling the managed ops devm_
759  * @proto_id: The protocol ID as in SCMI Specification
760  * @evt_id: The message ID of the desired event as in SCMI Specification
761  * @src_id: A pointer to the desired source ID if different sources are
762  *          possible for the protocol (like domain_id, sensor_id...etc)
763  *
764  * @src_id can be provided as NULL if it simply does NOT make sense for
765  * the protocol at hand, OR if the user is explicitly interested in
766  * receiving notifications from ANY existent source associated to the
767  * specified proto_id / evt_id.
768  *
769  * Received notifications are finally delivered to the registered users,
770  * invoking the callback provided with the notifier_block *nb as follows:
771  *
772  *      int user_cb(nb, evt_id, report)
773  *
774  * with:
775  *
776  * @nb: The notifier block provided by the user
777  * @evt_id: The message ID of the delivered event
778  * @report: A custom struct describing the specific event delivered
779  */
780 struct scmi_notify_ops {
781         int (*devm_event_notifier_register)(struct scmi_device *sdev,
782                                             u8 proto_id, u8 evt_id,
783                                             const u32 *src_id,
784                                             struct notifier_block *nb);
785         int (*devm_event_notifier_unregister)(struct scmi_device *sdev,
786                                               u8 proto_id, u8 evt_id,
787                                               const u32 *src_id,
788                                               struct notifier_block *nb);
789         int (*event_notifier_register)(const struct scmi_handle *handle,
790                                        u8 proto_id, u8 evt_id,
791                                        const u32 *src_id,
792                                        struct notifier_block *nb);
793         int (*event_notifier_unregister)(const struct scmi_handle *handle,
794                                          u8 proto_id, u8 evt_id,
795                                          const u32 *src_id,
796                                          struct notifier_block *nb);
797 };
798
799 /**
800  * struct scmi_handle - Handle returned to ARM SCMI clients for usage.
801  *
802  * @dev: pointer to the SCMI device
803  * @version: pointer to the structure containing SCMI version information
804  * @devm_protocol_acquire: devres managed method to get hold of a protocol,
805  *                         causing its initialization and related resource
806  *                         accounting
807  * @devm_protocol_get: devres managed method to acquire a protocol and get specific
808  *                     operations and a dedicated protocol handler
809  * @devm_protocol_put: devres managed method to release a protocol
810  * @is_transport_atomic: method to check if the underlying transport for this
811  *                       instance handle is configured to support atomic
812  *                       transactions for commands.
813  *                       Some users of the SCMI stack in the upper layers could
814  *                       be interested to know if they can assume SCMI
815  *                       command transactions associated to this handle will
816  *                       never sleep and act accordingly.
817  *                       An optional atomic threshold value could be returned
818  *                       where configured.
819  * @notify_ops: pointer to set of notifications related operations
820  */
821 struct scmi_handle {
822         struct device *dev;
823         struct scmi_revision_info *version;
824
825         int __must_check (*devm_protocol_acquire)(struct scmi_device *sdev,
826                                                   u8 proto);
827         const void __must_check *
828                 (*devm_protocol_get)(struct scmi_device *sdev, u8 proto,
829                                      struct scmi_protocol_handle **ph);
830         void (*devm_protocol_put)(struct scmi_device *sdev, u8 proto);
831         bool (*is_transport_atomic)(const struct scmi_handle *handle,
832                                     unsigned int *atomic_threshold);
833
834         const struct scmi_notify_ops *notify_ops;
835 };
836
837 enum scmi_std_protocol {
838         SCMI_PROTOCOL_BASE = 0x10,
839         SCMI_PROTOCOL_POWER = 0x11,
840         SCMI_PROTOCOL_SYSTEM = 0x12,
841         SCMI_PROTOCOL_PERF = 0x13,
842         SCMI_PROTOCOL_CLOCK = 0x14,
843         SCMI_PROTOCOL_SENSOR = 0x15,
844         SCMI_PROTOCOL_RESET = 0x16,
845         SCMI_PROTOCOL_VOLTAGE = 0x17,
846         SCMI_PROTOCOL_POWERCAP = 0x18,
847 };
848
849 enum scmi_system_events {
850         SCMI_SYSTEM_SHUTDOWN,
851         SCMI_SYSTEM_COLDRESET,
852         SCMI_SYSTEM_WARMRESET,
853         SCMI_SYSTEM_POWERUP,
854         SCMI_SYSTEM_SUSPEND,
855         SCMI_SYSTEM_MAX
856 };
857
858 struct scmi_device {
859         u32 id;
860         u8 protocol_id;
861         const char *name;
862         struct device dev;
863         struct scmi_handle *handle;
864 };
865
866 #define to_scmi_dev(d) container_of(d, struct scmi_device, dev)
867
868 struct scmi_device_id {
869         u8 protocol_id;
870         const char *name;
871 };
872
873 struct scmi_driver {
874         const char *name;
875         int (*probe)(struct scmi_device *sdev);
876         void (*remove)(struct scmi_device *sdev);
877         const struct scmi_device_id *id_table;
878
879         struct device_driver driver;
880 };
881
882 #define to_scmi_driver(d) container_of(d, struct scmi_driver, driver)
883
884 #if IS_REACHABLE(CONFIG_ARM_SCMI_PROTOCOL)
885 int scmi_driver_register(struct scmi_driver *driver,
886                          struct module *owner, const char *mod_name);
887 void scmi_driver_unregister(struct scmi_driver *driver);
888 #else
889 static inline int
890 scmi_driver_register(struct scmi_driver *driver, struct module *owner,
891                      const char *mod_name)
892 {
893         return -EINVAL;
894 }
895
896 static inline void scmi_driver_unregister(struct scmi_driver *driver) {}
897 #endif /* CONFIG_ARM_SCMI_PROTOCOL */
898
899 #define scmi_register(driver) \
900         scmi_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
901 #define scmi_unregister(driver) \
902         scmi_driver_unregister(driver)
903
904 /**
905  * module_scmi_driver() - Helper macro for registering a scmi driver
906  * @__scmi_driver: scmi_driver structure
907  *
908  * Helper macro for scmi drivers to set up proper module init / exit
909  * functions.  Replaces module_init() and module_exit() and keeps people from
910  * printing pointless things to the kernel log when their driver is loaded.
911  */
912 #define module_scmi_driver(__scmi_driver)       \
913         module_driver(__scmi_driver, scmi_register, scmi_unregister)
914
915 /**
916  * module_scmi_protocol() - Helper macro for registering a scmi protocol
917  * @__scmi_protocol: scmi_protocol structure
918  *
919  * Helper macro for scmi drivers to set up proper module init / exit
920  * functions.  Replaces module_init() and module_exit() and keeps people from
921  * printing pointless things to the kernel log when their driver is loaded.
922  */
923 #define module_scmi_protocol(__scmi_protocol)   \
924         module_driver(__scmi_protocol,          \
925                       scmi_protocol_register, scmi_protocol_unregister)
926
927 struct scmi_protocol;
928 int scmi_protocol_register(const struct scmi_protocol *proto);
929 void scmi_protocol_unregister(const struct scmi_protocol *proto);
930
931 /* SCMI Notification API - Custom Event Reports */
932 enum scmi_notification_events {
933         SCMI_EVENT_POWER_STATE_CHANGED = 0x0,
934         SCMI_EVENT_CLOCK_RATE_CHANGED = 0x0,
935         SCMI_EVENT_CLOCK_RATE_CHANGE_REQUESTED = 0x1,
936         SCMI_EVENT_PERFORMANCE_LIMITS_CHANGED = 0x0,
937         SCMI_EVENT_PERFORMANCE_LEVEL_CHANGED = 0x1,
938         SCMI_EVENT_SENSOR_TRIP_POINT_EVENT = 0x0,
939         SCMI_EVENT_SENSOR_UPDATE = 0x1,
940         SCMI_EVENT_RESET_ISSUED = 0x0,
941         SCMI_EVENT_BASE_ERROR_EVENT = 0x0,
942         SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER = 0x0,
943         SCMI_EVENT_POWERCAP_CAP_CHANGED = 0x0,
944         SCMI_EVENT_POWERCAP_MEASUREMENTS_CHANGED = 0x1,
945 };
946
947 struct scmi_power_state_changed_report {
948         ktime_t         timestamp;
949         unsigned int    agent_id;
950         unsigned int    domain_id;
951         unsigned int    power_state;
952 };
953
954 struct scmi_clock_rate_notif_report {
955         ktime_t                 timestamp;
956         unsigned int            agent_id;
957         unsigned int            clock_id;
958         unsigned long long      rate;
959 };
960
961 struct scmi_system_power_state_notifier_report {
962         ktime_t         timestamp;
963         unsigned int    agent_id;
964 #define SCMI_SYSPOWER_IS_REQUEST_GRACEFUL(flags)        ((flags) & BIT(0))
965         unsigned int    flags;
966         unsigned int    system_state;
967         unsigned int    timeout;
968 };
969
970 struct scmi_perf_limits_report {
971         ktime_t         timestamp;
972         unsigned int    agent_id;
973         unsigned int    domain_id;
974         unsigned int    range_max;
975         unsigned int    range_min;
976         unsigned long   range_max_freq;
977         unsigned long   range_min_freq;
978 };
979
980 struct scmi_perf_level_report {
981         ktime_t         timestamp;
982         unsigned int    agent_id;
983         unsigned int    domain_id;
984         unsigned int    performance_level;
985         unsigned long   performance_level_freq;
986 };
987
988 struct scmi_sensor_trip_point_report {
989         ktime_t         timestamp;
990         unsigned int    agent_id;
991         unsigned int    sensor_id;
992         unsigned int    trip_point_desc;
993 };
994
995 struct scmi_sensor_update_report {
996         ktime_t                         timestamp;
997         unsigned int                    agent_id;
998         unsigned int                    sensor_id;
999         unsigned int                    readings_count;
1000         struct scmi_sensor_reading      readings[];
1001 };
1002
1003 struct scmi_reset_issued_report {
1004         ktime_t         timestamp;
1005         unsigned int    agent_id;
1006         unsigned int    domain_id;
1007         unsigned int    reset_state;
1008 };
1009
1010 struct scmi_base_error_report {
1011         ktime_t                 timestamp;
1012         unsigned int            agent_id;
1013         bool                    fatal;
1014         unsigned int            cmd_count;
1015         unsigned long long      reports[];
1016 };
1017
1018 struct scmi_powercap_cap_changed_report {
1019         ktime_t         timestamp;
1020         unsigned int    agent_id;
1021         unsigned int    domain_id;
1022         unsigned int    power_cap;
1023         unsigned int    pai;
1024 };
1025
1026 struct scmi_powercap_meas_changed_report {
1027         ktime_t         timestamp;
1028         unsigned int    agent_id;
1029         unsigned int    domain_id;
1030         unsigned int    power;
1031 };
1032 #endif /* _LINUX_SCMI_PROTOCOL_H */