xref: /openbmc/linux/include/linux/scmi_protocol.h (revision d09aac0e)
14752544aSSudeep Holla /* SPDX-License-Identifier: GPL-2.0-only */
2aa4f886fSSudeep Holla /*
3aa4f886fSSudeep Holla  * SCMI Message Protocol driver header
4aa4f886fSSudeep Holla  *
5aa4f886fSSudeep Holla  * Copyright (C) 2018 ARM Ltd.
6aa4f886fSSudeep Holla  */
7933c5044SSudeep Holla #include <linux/device.h>
8aa4f886fSSudeep Holla #include <linux/types.h>
9aa4f886fSSudeep Holla 
10b6f20ff8SSudeep Holla #define SCMI_MAX_STR_SIZE	16
115f6c6430SSudeep Holla #define SCMI_MAX_NUM_RATES	16
12b6f20ff8SSudeep Holla 
13b6f20ff8SSudeep Holla /**
14b6f20ff8SSudeep Holla  * struct scmi_revision_info - version information structure
15b6f20ff8SSudeep Holla  *
16b6f20ff8SSudeep Holla  * @major_ver: Major ABI version. Change here implies risk of backward
17b6f20ff8SSudeep Holla  *	compatibility break.
18b6f20ff8SSudeep Holla  * @minor_ver: Minor ABI version. Change here implies new feature addition,
19b6f20ff8SSudeep Holla  *	or compatible change in ABI.
20b6f20ff8SSudeep Holla  * @num_protocols: Number of protocols that are implemented, excluding the
21b6f20ff8SSudeep Holla  *	base protocol.
22b6f20ff8SSudeep Holla  * @num_agents: Number of agents in the system.
23b6f20ff8SSudeep Holla  * @impl_ver: A vendor-specific implementation version.
24b6f20ff8SSudeep Holla  * @vendor_id: A vendor identifier(Null terminated ASCII string)
25b6f20ff8SSudeep Holla  * @sub_vendor_id: A sub-vendor identifier(Null terminated ASCII string)
26b6f20ff8SSudeep Holla  */
27b6f20ff8SSudeep Holla struct scmi_revision_info {
28b6f20ff8SSudeep Holla 	u16 major_ver;
29b6f20ff8SSudeep Holla 	u16 minor_ver;
30b6f20ff8SSudeep Holla 	u8 num_protocols;
31b6f20ff8SSudeep Holla 	u8 num_agents;
32b6f20ff8SSudeep Holla 	u32 impl_ver;
33b6f20ff8SSudeep Holla 	char vendor_id[SCMI_MAX_STR_SIZE];
34b6f20ff8SSudeep Holla 	char sub_vendor_id[SCMI_MAX_STR_SIZE];
35b6f20ff8SSudeep Holla };
36b6f20ff8SSudeep Holla 
375f6c6430SSudeep Holla struct scmi_clock_info {
385f6c6430SSudeep Holla 	char name[SCMI_MAX_STR_SIZE];
395f6c6430SSudeep Holla 	bool rate_discrete;
405f6c6430SSudeep Holla 	union {
415f6c6430SSudeep Holla 		struct {
425f6c6430SSudeep Holla 			int num_rates;
435f6c6430SSudeep Holla 			u64 rates[SCMI_MAX_NUM_RATES];
445f6c6430SSudeep Holla 		} list;
455f6c6430SSudeep Holla 		struct {
465f6c6430SSudeep Holla 			u64 min_rate;
475f6c6430SSudeep Holla 			u64 max_rate;
485f6c6430SSudeep Holla 			u64 step_size;
495f6c6430SSudeep Holla 		} range;
505f6c6430SSudeep Holla 	};
515f6c6430SSudeep Holla };
525f6c6430SSudeep Holla 
53a9e3fbfaSSudeep Holla struct scmi_handle;
54a9e3fbfaSSudeep Holla 
55a9e3fbfaSSudeep Holla /**
565f6c6430SSudeep Holla  * struct scmi_clk_ops - represents the various operations provided
575f6c6430SSudeep Holla  *	by SCMI Clock Protocol
585f6c6430SSudeep Holla  *
595f6c6430SSudeep Holla  * @count_get: get the count of clocks provided by SCMI
605f6c6430SSudeep Holla  * @info_get: get the information of the specified clock
615f6c6430SSudeep Holla  * @rate_get: request the current clock rate of a clock
625f6c6430SSudeep Holla  * @rate_set: set the clock rate of a clock
635f6c6430SSudeep Holla  * @enable: enables the specified clock
645f6c6430SSudeep Holla  * @disable: disables the specified clock
655f6c6430SSudeep Holla  */
665f6c6430SSudeep Holla struct scmi_clk_ops {
675f6c6430SSudeep Holla 	int (*count_get)(const struct scmi_handle *handle);
685f6c6430SSudeep Holla 
695f6c6430SSudeep Holla 	const struct scmi_clock_info *(*info_get)
705f6c6430SSudeep Holla 		(const struct scmi_handle *handle, u32 clk_id);
715f6c6430SSudeep Holla 	int (*rate_get)(const struct scmi_handle *handle, u32 clk_id,
725f6c6430SSudeep Holla 			u64 *rate);
735f6c6430SSudeep Holla 	int (*rate_set)(const struct scmi_handle *handle, u32 clk_id,
745f6c6430SSudeep Holla 			u32 config, u64 rate);
755f6c6430SSudeep Holla 	int (*enable)(const struct scmi_handle *handle, u32 clk_id);
765f6c6430SSudeep Holla 	int (*disable)(const struct scmi_handle *handle, u32 clk_id);
775f6c6430SSudeep Holla };
785f6c6430SSudeep Holla 
795f6c6430SSudeep Holla /**
80a9e3fbfaSSudeep Holla  * struct scmi_perf_ops - represents the various operations provided
81a9e3fbfaSSudeep Holla  *	by SCMI Performance Protocol
82a9e3fbfaSSudeep Holla  *
83a9e3fbfaSSudeep Holla  * @limits_set: sets limits on the performance level of a domain
84a9e3fbfaSSudeep Holla  * @limits_get: gets limits on the performance level of a domain
85a9e3fbfaSSudeep Holla  * @level_set: sets the performance level of a domain
86a9e3fbfaSSudeep Holla  * @level_get: gets the performance level of a domain
87a9e3fbfaSSudeep Holla  * @device_domain_id: gets the scmi domain id for a given device
887859e08cSSudeep Holla  * @transition_latency_get: gets the DVFS transition latency for a given device
897859e08cSSudeep Holla  * @device_opps_add: adds all the OPPs for a given device
90a9e3fbfaSSudeep Holla  * @freq_set: sets the frequency for a given device using sustained frequency
91a9e3fbfaSSudeep Holla  *	to sustained performance level mapping
92a9e3fbfaSSudeep Holla  * @freq_get: gets the frequency for a given device using sustained frequency
93a9e3fbfaSSudeep Holla  *	to sustained performance level mapping
941a63fe9aSQuentin Perret  * @est_power_get: gets the estimated power cost for a given performance domain
951a63fe9aSQuentin Perret  *	at a given frequency
96a9e3fbfaSSudeep Holla  */
97a9e3fbfaSSudeep Holla struct scmi_perf_ops {
98a9e3fbfaSSudeep Holla 	int (*limits_set)(const struct scmi_handle *handle, u32 domain,
99a9e3fbfaSSudeep Holla 			  u32 max_perf, u32 min_perf);
100a9e3fbfaSSudeep Holla 	int (*limits_get)(const struct scmi_handle *handle, u32 domain,
101a9e3fbfaSSudeep Holla 			  u32 *max_perf, u32 *min_perf);
102a9e3fbfaSSudeep Holla 	int (*level_set)(const struct scmi_handle *handle, u32 domain,
1035c4ba3ccSSudeep Holla 			 u32 level, bool poll);
104a9e3fbfaSSudeep Holla 	int (*level_get)(const struct scmi_handle *handle, u32 domain,
1055c4ba3ccSSudeep Holla 			 u32 *level, bool poll);
106a9e3fbfaSSudeep Holla 	int (*device_domain_id)(struct device *dev);
1077859e08cSSudeep Holla 	int (*transition_latency_get)(const struct scmi_handle *handle,
108a9e3fbfaSSudeep Holla 				      struct device *dev);
1097859e08cSSudeep Holla 	int (*device_opps_add)(const struct scmi_handle *handle,
110a9e3fbfaSSudeep Holla 			       struct device *dev);
111a9e3fbfaSSudeep Holla 	int (*freq_set)(const struct scmi_handle *handle, u32 domain,
1125c4ba3ccSSudeep Holla 			unsigned long rate, bool poll);
113a9e3fbfaSSudeep Holla 	int (*freq_get)(const struct scmi_handle *handle, u32 domain,
1145c4ba3ccSSudeep Holla 			unsigned long *rate, bool poll);
1151a63fe9aSQuentin Perret 	int (*est_power_get)(const struct scmi_handle *handle, u32 domain,
1161a63fe9aSQuentin Perret 			     unsigned long *rate, unsigned long *power);
117a9e3fbfaSSudeep Holla };
118a9e3fbfaSSudeep Holla 
119aa4f886fSSudeep Holla /**
12076a65509SSudeep Holla  * struct scmi_power_ops - represents the various operations provided
12176a65509SSudeep Holla  *	by SCMI Power Protocol
12276a65509SSudeep Holla  *
12376a65509SSudeep Holla  * @num_domains_get: get the count of power domains provided by SCMI
12476a65509SSudeep Holla  * @name_get: gets the name of a power domain
12576a65509SSudeep Holla  * @state_set: sets the power state of a power domain
12676a65509SSudeep Holla  * @state_get: gets the power state of a power domain
12776a65509SSudeep Holla  */
12876a65509SSudeep Holla struct scmi_power_ops {
12976a65509SSudeep Holla 	int (*num_domains_get)(const struct scmi_handle *handle);
13076a65509SSudeep Holla 	char *(*name_get)(const struct scmi_handle *handle, u32 domain);
13176a65509SSudeep Holla #define SCMI_POWER_STATE_TYPE_SHIFT	30
13276a65509SSudeep Holla #define SCMI_POWER_STATE_ID_MASK	(BIT(28) - 1)
13376a65509SSudeep Holla #define SCMI_POWER_STATE_PARAM(type, id) \
13476a65509SSudeep Holla 	((((type) & BIT(0)) << SCMI_POWER_STATE_TYPE_SHIFT) | \
13576a65509SSudeep Holla 		((id) & SCMI_POWER_STATE_ID_MASK))
13676a65509SSudeep Holla #define SCMI_POWER_STATE_GENERIC_ON	SCMI_POWER_STATE_PARAM(0, 0)
13776a65509SSudeep Holla #define SCMI_POWER_STATE_GENERIC_OFF	SCMI_POWER_STATE_PARAM(1, 0)
13876a65509SSudeep Holla 	int (*state_set)(const struct scmi_handle *handle, u32 domain,
13976a65509SSudeep Holla 			 u32 state);
14076a65509SSudeep Holla 	int (*state_get)(const struct scmi_handle *handle, u32 domain,
14176a65509SSudeep Holla 			 u32 *state);
14276a65509SSudeep Holla };
14376a65509SSudeep Holla 
1445179c523SSudeep Holla struct scmi_sensor_info {
1455179c523SSudeep Holla 	u32 id;
1465179c523SSudeep Holla 	u8 type;
1470b673b64SFlorian Fainelli 	s8 scale;
148d09aac0eSSudeep Holla 	u8 num_trip_points;
149d09aac0eSSudeep Holla 	bool async;
1505179c523SSudeep Holla 	char name[SCMI_MAX_STR_SIZE];
1515179c523SSudeep Holla };
1525179c523SSudeep Holla 
1535179c523SSudeep Holla /*
1545179c523SSudeep Holla  * Partial list from Distributed Management Task Force (DMTF) specification:
1555179c523SSudeep Holla  * DSP0249 (Platform Level Data Model specification)
1565179c523SSudeep Holla  */
1575179c523SSudeep Holla enum scmi_sensor_class {
1585179c523SSudeep Holla 	NONE = 0x0,
1595179c523SSudeep Holla 	TEMPERATURE_C = 0x2,
1605179c523SSudeep Holla 	VOLTAGE = 0x5,
1615179c523SSudeep Holla 	CURRENT = 0x6,
1625179c523SSudeep Holla 	POWER = 0x7,
1635179c523SSudeep Holla 	ENERGY = 0x8,
1645179c523SSudeep Holla };
1655179c523SSudeep Holla 
1665179c523SSudeep Holla /**
1675179c523SSudeep Holla  * struct scmi_sensor_ops - represents the various operations provided
1685179c523SSudeep Holla  *	by SCMI Sensor Protocol
1695179c523SSudeep Holla  *
1705179c523SSudeep Holla  * @count_get: get the count of sensors provided by SCMI
1715179c523SSudeep Holla  * @info_get: get the information of the specified sensor
1729eefa43aSSudeep Holla  * @trip_point_notify: control notifications on cross-over events for
1735179c523SSudeep Holla  *	the trip-points
1749eefa43aSSudeep Holla  * @trip_point_config: selects and configures a trip-point of interest
1755179c523SSudeep Holla  * @reading_get: gets the current value of the sensor
1765179c523SSudeep Holla  */
1775179c523SSudeep Holla struct scmi_sensor_ops {
1785179c523SSudeep Holla 	int (*count_get)(const struct scmi_handle *handle);
1795179c523SSudeep Holla 
1805179c523SSudeep Holla 	const struct scmi_sensor_info *(*info_get)
1815179c523SSudeep Holla 		(const struct scmi_handle *handle, u32 sensor_id);
1829eefa43aSSudeep Holla 	int (*trip_point_notify)(const struct scmi_handle *handle,
1839eefa43aSSudeep Holla 				 u32 sensor_id, bool enable);
1849eefa43aSSudeep Holla 	int (*trip_point_config)(const struct scmi_handle *handle,
1859eefa43aSSudeep Holla 				 u32 sensor_id, u8 trip_id, u64 trip_value);
1865179c523SSudeep Holla 	int (*reading_get)(const struct scmi_handle *handle, u32 sensor_id,
1876a55331cSSudeep Holla 			   u64 *value);
1885179c523SSudeep Holla };
1895179c523SSudeep Holla 
19076a65509SSudeep Holla /**
191aa4f886fSSudeep Holla  * struct scmi_handle - Handle returned to ARM SCMI clients for usage.
192aa4f886fSSudeep Holla  *
193aa4f886fSSudeep Holla  * @dev: pointer to the SCMI device
194b6f20ff8SSudeep Holla  * @version: pointer to the structure containing SCMI version information
19576a65509SSudeep Holla  * @power_ops: pointer to set of power protocol operations
196a9e3fbfaSSudeep Holla  * @perf_ops: pointer to set of performance protocol operations
1975f6c6430SSudeep Holla  * @clk_ops: pointer to set of clock protocol operations
1985179c523SSudeep Holla  * @sensor_ops: pointer to set of sensor protocol operations
1991baf47c2SSudeep Holla  * @perf_priv: pointer to private data structure specific to performance
2001baf47c2SSudeep Holla  *	protocol(for internal use only)
2011baf47c2SSudeep Holla  * @clk_priv: pointer to private data structure specific to clock
2021baf47c2SSudeep Holla  *	protocol(for internal use only)
2031baf47c2SSudeep Holla  * @power_priv: pointer to private data structure specific to power
2041baf47c2SSudeep Holla  *	protocol(for internal use only)
2051baf47c2SSudeep Holla  * @sensor_priv: pointer to private data structure specific to sensors
2061baf47c2SSudeep Holla  *	protocol(for internal use only)
207aa4f886fSSudeep Holla  */
208aa4f886fSSudeep Holla struct scmi_handle {
209aa4f886fSSudeep Holla 	struct device *dev;
210b6f20ff8SSudeep Holla 	struct scmi_revision_info *version;
211a9e3fbfaSSudeep Holla 	struct scmi_perf_ops *perf_ops;
2125f6c6430SSudeep Holla 	struct scmi_clk_ops *clk_ops;
21376a65509SSudeep Holla 	struct scmi_power_ops *power_ops;
2145179c523SSudeep Holla 	struct scmi_sensor_ops *sensor_ops;
215a9e3fbfaSSudeep Holla 	/* for protocol internal use */
216a9e3fbfaSSudeep Holla 	void *perf_priv;
2175f6c6430SSudeep Holla 	void *clk_priv;
21876a65509SSudeep Holla 	void *power_priv;
2195179c523SSudeep Holla 	void *sensor_priv;
220b6f20ff8SSudeep Holla };
221b6f20ff8SSudeep Holla 
222b6f20ff8SSudeep Holla enum scmi_std_protocol {
223b6f20ff8SSudeep Holla 	SCMI_PROTOCOL_BASE = 0x10,
224b6f20ff8SSudeep Holla 	SCMI_PROTOCOL_POWER = 0x11,
225b6f20ff8SSudeep Holla 	SCMI_PROTOCOL_SYSTEM = 0x12,
226b6f20ff8SSudeep Holla 	SCMI_PROTOCOL_PERF = 0x13,
227b6f20ff8SSudeep Holla 	SCMI_PROTOCOL_CLOCK = 0x14,
228b6f20ff8SSudeep Holla 	SCMI_PROTOCOL_SENSOR = 0x15,
229aa4f886fSSudeep Holla };
230933c5044SSudeep Holla 
231933c5044SSudeep Holla struct scmi_device {
232933c5044SSudeep Holla 	u32 id;
233933c5044SSudeep Holla 	u8 protocol_id;
234933c5044SSudeep Holla 	struct device dev;
235933c5044SSudeep Holla 	struct scmi_handle *handle;
236933c5044SSudeep Holla };
237933c5044SSudeep Holla 
238933c5044SSudeep Holla #define to_scmi_dev(d) container_of(d, struct scmi_device, dev)
239933c5044SSudeep Holla 
240933c5044SSudeep Holla struct scmi_device *
241933c5044SSudeep Holla scmi_device_create(struct device_node *np, struct device *parent, int protocol);
242933c5044SSudeep Holla void scmi_device_destroy(struct scmi_device *scmi_dev);
243933c5044SSudeep Holla 
244933c5044SSudeep Holla struct scmi_device_id {
245933c5044SSudeep Holla 	u8 protocol_id;
246933c5044SSudeep Holla };
247933c5044SSudeep Holla 
248933c5044SSudeep Holla struct scmi_driver {
249933c5044SSudeep Holla 	const char *name;
250933c5044SSudeep Holla 	int (*probe)(struct scmi_device *sdev);
251933c5044SSudeep Holla 	void (*remove)(struct scmi_device *sdev);
252933c5044SSudeep Holla 	const struct scmi_device_id *id_table;
253933c5044SSudeep Holla 
254933c5044SSudeep Holla 	struct device_driver driver;
255933c5044SSudeep Holla };
256933c5044SSudeep Holla 
257933c5044SSudeep Holla #define to_scmi_driver(d) container_of(d, struct scmi_driver, driver)
258933c5044SSudeep Holla 
259933c5044SSudeep Holla #ifdef CONFIG_ARM_SCMI_PROTOCOL
260933c5044SSudeep Holla int scmi_driver_register(struct scmi_driver *driver,
261933c5044SSudeep Holla 			 struct module *owner, const char *mod_name);
262933c5044SSudeep Holla void scmi_driver_unregister(struct scmi_driver *driver);
263933c5044SSudeep Holla #else
264933c5044SSudeep Holla static inline int
265933c5044SSudeep Holla scmi_driver_register(struct scmi_driver *driver, struct module *owner,
266933c5044SSudeep Holla 		     const char *mod_name)
267933c5044SSudeep Holla {
268933c5044SSudeep Holla 	return -EINVAL;
269933c5044SSudeep Holla }
270933c5044SSudeep Holla 
271933c5044SSudeep Holla static inline void scmi_driver_unregister(struct scmi_driver *driver) {}
272933c5044SSudeep Holla #endif /* CONFIG_ARM_SCMI_PROTOCOL */
273933c5044SSudeep Holla 
274933c5044SSudeep Holla #define scmi_register(driver) \
275933c5044SSudeep Holla 	scmi_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
276933c5044SSudeep Holla #define scmi_unregister(driver) \
277933c5044SSudeep Holla 	scmi_driver_unregister(driver)
278933c5044SSudeep Holla 
279933c5044SSudeep Holla /**
280933c5044SSudeep Holla  * module_scmi_driver() - Helper macro for registering a scmi driver
281933c5044SSudeep Holla  * @__scmi_driver: scmi_driver structure
282933c5044SSudeep Holla  *
283933c5044SSudeep Holla  * Helper macro for scmi drivers to set up proper module init / exit
284933c5044SSudeep Holla  * functions.  Replaces module_init() and module_exit() and keeps people from
285933c5044SSudeep Holla  * printing pointless things to the kernel log when their driver is loaded.
286933c5044SSudeep Holla  */
287933c5044SSudeep Holla #define module_scmi_driver(__scmi_driver)	\
288933c5044SSudeep Holla 	module_driver(__scmi_driver, scmi_register, scmi_unregister)
289933c5044SSudeep Holla 
290933c5044SSudeep Holla typedef int (*scmi_prot_init_fn_t)(struct scmi_handle *);
291933c5044SSudeep Holla int scmi_protocol_register(int protocol_id, scmi_prot_init_fn_t fn);
292933c5044SSudeep Holla void scmi_protocol_unregister(int protocol_id);
293