xref: /openbmc/linux/include/linux/scmi_protocol.h (revision 5c4ba3cc)
1aa4f886fSSudeep Holla // SPDX-License-Identifier: GPL-2.0
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
88a9e3fbfaSSudeep Holla  * @get_transition_latency: gets the DVFS transition latency for a given device
89a9e3fbfaSSudeep Holla  * @add_opps_to_device: 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
94a9e3fbfaSSudeep Holla  */
95a9e3fbfaSSudeep Holla struct scmi_perf_ops {
96a9e3fbfaSSudeep Holla 	int (*limits_set)(const struct scmi_handle *handle, u32 domain,
97a9e3fbfaSSudeep Holla 			  u32 max_perf, u32 min_perf);
98a9e3fbfaSSudeep Holla 	int (*limits_get)(const struct scmi_handle *handle, u32 domain,
99a9e3fbfaSSudeep Holla 			  u32 *max_perf, u32 *min_perf);
100a9e3fbfaSSudeep Holla 	int (*level_set)(const struct scmi_handle *handle, u32 domain,
1015c4ba3ccSSudeep Holla 			 u32 level, bool poll);
102a9e3fbfaSSudeep Holla 	int (*level_get)(const struct scmi_handle *handle, u32 domain,
1035c4ba3ccSSudeep Holla 			 u32 *level, bool poll);
104a9e3fbfaSSudeep Holla 	int (*device_domain_id)(struct device *dev);
105a9e3fbfaSSudeep Holla 	int (*get_transition_latency)(const struct scmi_handle *handle,
106a9e3fbfaSSudeep Holla 				      struct device *dev);
107a9e3fbfaSSudeep Holla 	int (*add_opps_to_device)(const struct scmi_handle *handle,
108a9e3fbfaSSudeep Holla 				  struct device *dev);
109a9e3fbfaSSudeep Holla 	int (*freq_set)(const struct scmi_handle *handle, u32 domain,
1105c4ba3ccSSudeep Holla 			unsigned long rate, bool poll);
111a9e3fbfaSSudeep Holla 	int (*freq_get)(const struct scmi_handle *handle, u32 domain,
1125c4ba3ccSSudeep Holla 			unsigned long *rate, bool poll);
113a9e3fbfaSSudeep Holla };
114a9e3fbfaSSudeep Holla 
115aa4f886fSSudeep Holla /**
11676a65509SSudeep Holla  * struct scmi_power_ops - represents the various operations provided
11776a65509SSudeep Holla  *	by SCMI Power Protocol
11876a65509SSudeep Holla  *
11976a65509SSudeep Holla  * @num_domains_get: get the count of power domains provided by SCMI
12076a65509SSudeep Holla  * @name_get: gets the name of a power domain
12176a65509SSudeep Holla  * @state_set: sets the power state of a power domain
12276a65509SSudeep Holla  * @state_get: gets the power state of a power domain
12376a65509SSudeep Holla  */
12476a65509SSudeep Holla struct scmi_power_ops {
12576a65509SSudeep Holla 	int (*num_domains_get)(const struct scmi_handle *handle);
12676a65509SSudeep Holla 	char *(*name_get)(const struct scmi_handle *handle, u32 domain);
12776a65509SSudeep Holla #define SCMI_POWER_STATE_TYPE_SHIFT	30
12876a65509SSudeep Holla #define SCMI_POWER_STATE_ID_MASK	(BIT(28) - 1)
12976a65509SSudeep Holla #define SCMI_POWER_STATE_PARAM(type, id) \
13076a65509SSudeep Holla 	((((type) & BIT(0)) << SCMI_POWER_STATE_TYPE_SHIFT) | \
13176a65509SSudeep Holla 		((id) & SCMI_POWER_STATE_ID_MASK))
13276a65509SSudeep Holla #define SCMI_POWER_STATE_GENERIC_ON	SCMI_POWER_STATE_PARAM(0, 0)
13376a65509SSudeep Holla #define SCMI_POWER_STATE_GENERIC_OFF	SCMI_POWER_STATE_PARAM(1, 0)
13476a65509SSudeep Holla 	int (*state_set)(const struct scmi_handle *handle, u32 domain,
13576a65509SSudeep Holla 			 u32 state);
13676a65509SSudeep Holla 	int (*state_get)(const struct scmi_handle *handle, u32 domain,
13776a65509SSudeep Holla 			 u32 *state);
13876a65509SSudeep Holla };
13976a65509SSudeep Holla 
1405179c523SSudeep Holla struct scmi_sensor_info {
1415179c523SSudeep Holla 	u32 id;
1425179c523SSudeep Holla 	u8 type;
1435179c523SSudeep Holla 	char name[SCMI_MAX_STR_SIZE];
1445179c523SSudeep Holla };
1455179c523SSudeep Holla 
1465179c523SSudeep Holla /*
1475179c523SSudeep Holla  * Partial list from Distributed Management Task Force (DMTF) specification:
1485179c523SSudeep Holla  * DSP0249 (Platform Level Data Model specification)
1495179c523SSudeep Holla  */
1505179c523SSudeep Holla enum scmi_sensor_class {
1515179c523SSudeep Holla 	NONE = 0x0,
1525179c523SSudeep Holla 	TEMPERATURE_C = 0x2,
1535179c523SSudeep Holla 	VOLTAGE = 0x5,
1545179c523SSudeep Holla 	CURRENT = 0x6,
1555179c523SSudeep Holla 	POWER = 0x7,
1565179c523SSudeep Holla 	ENERGY = 0x8,
1575179c523SSudeep Holla };
1585179c523SSudeep Holla 
1595179c523SSudeep Holla /**
1605179c523SSudeep Holla  * struct scmi_sensor_ops - represents the various operations provided
1615179c523SSudeep Holla  *	by SCMI Sensor Protocol
1625179c523SSudeep Holla  *
1635179c523SSudeep Holla  * @count_get: get the count of sensors provided by SCMI
1645179c523SSudeep Holla  * @info_get: get the information of the specified sensor
1655179c523SSudeep Holla  * @configuration_set: control notifications on cross-over events for
1665179c523SSudeep Holla  *	the trip-points
1675179c523SSudeep Holla  * @trip_point_set: selects and configures a trip-point of interest
1685179c523SSudeep Holla  * @reading_get: gets the current value of the sensor
1695179c523SSudeep Holla  */
1705179c523SSudeep Holla struct scmi_sensor_ops {
1715179c523SSudeep Holla 	int (*count_get)(const struct scmi_handle *handle);
1725179c523SSudeep Holla 
1735179c523SSudeep Holla 	const struct scmi_sensor_info *(*info_get)
1745179c523SSudeep Holla 		(const struct scmi_handle *handle, u32 sensor_id);
1755179c523SSudeep Holla 	int (*configuration_set)(const struct scmi_handle *handle,
1765179c523SSudeep Holla 				 u32 sensor_id);
1775179c523SSudeep Holla 	int (*trip_point_set)(const struct scmi_handle *handle, u32 sensor_id,
1785179c523SSudeep Holla 			      u8 trip_id, u64 trip_value);
1795179c523SSudeep Holla 	int (*reading_get)(const struct scmi_handle *handle, u32 sensor_id,
1805179c523SSudeep Holla 			   bool async, u64 *value);
1815179c523SSudeep Holla };
1825179c523SSudeep Holla 
18376a65509SSudeep Holla /**
184aa4f886fSSudeep Holla  * struct scmi_handle - Handle returned to ARM SCMI clients for usage.
185aa4f886fSSudeep Holla  *
186aa4f886fSSudeep Holla  * @dev: pointer to the SCMI device
187b6f20ff8SSudeep Holla  * @version: pointer to the structure containing SCMI version information
18876a65509SSudeep Holla  * @power_ops: pointer to set of power protocol operations
189a9e3fbfaSSudeep Holla  * @perf_ops: pointer to set of performance protocol operations
1905f6c6430SSudeep Holla  * @clk_ops: pointer to set of clock protocol operations
1915179c523SSudeep Holla  * @sensor_ops: pointer to set of sensor protocol operations
192aa4f886fSSudeep Holla  */
193aa4f886fSSudeep Holla struct scmi_handle {
194aa4f886fSSudeep Holla 	struct device *dev;
195b6f20ff8SSudeep Holla 	struct scmi_revision_info *version;
196a9e3fbfaSSudeep Holla 	struct scmi_perf_ops *perf_ops;
1975f6c6430SSudeep Holla 	struct scmi_clk_ops *clk_ops;
19876a65509SSudeep Holla 	struct scmi_power_ops *power_ops;
1995179c523SSudeep Holla 	struct scmi_sensor_ops *sensor_ops;
200a9e3fbfaSSudeep Holla 	/* for protocol internal use */
201a9e3fbfaSSudeep Holla 	void *perf_priv;
2025f6c6430SSudeep Holla 	void *clk_priv;
20376a65509SSudeep Holla 	void *power_priv;
2045179c523SSudeep Holla 	void *sensor_priv;
205b6f20ff8SSudeep Holla };
206b6f20ff8SSudeep Holla 
207b6f20ff8SSudeep Holla enum scmi_std_protocol {
208b6f20ff8SSudeep Holla 	SCMI_PROTOCOL_BASE = 0x10,
209b6f20ff8SSudeep Holla 	SCMI_PROTOCOL_POWER = 0x11,
210b6f20ff8SSudeep Holla 	SCMI_PROTOCOL_SYSTEM = 0x12,
211b6f20ff8SSudeep Holla 	SCMI_PROTOCOL_PERF = 0x13,
212b6f20ff8SSudeep Holla 	SCMI_PROTOCOL_CLOCK = 0x14,
213b6f20ff8SSudeep Holla 	SCMI_PROTOCOL_SENSOR = 0x15,
214aa4f886fSSudeep Holla };
215933c5044SSudeep Holla 
216933c5044SSudeep Holla struct scmi_device {
217933c5044SSudeep Holla 	u32 id;
218933c5044SSudeep Holla 	u8 protocol_id;
219933c5044SSudeep Holla 	struct device dev;
220933c5044SSudeep Holla 	struct scmi_handle *handle;
221933c5044SSudeep Holla };
222933c5044SSudeep Holla 
223933c5044SSudeep Holla #define to_scmi_dev(d) container_of(d, struct scmi_device, dev)
224933c5044SSudeep Holla 
225933c5044SSudeep Holla struct scmi_device *
226933c5044SSudeep Holla scmi_device_create(struct device_node *np, struct device *parent, int protocol);
227933c5044SSudeep Holla void scmi_device_destroy(struct scmi_device *scmi_dev);
228933c5044SSudeep Holla 
229933c5044SSudeep Holla struct scmi_device_id {
230933c5044SSudeep Holla 	u8 protocol_id;
231933c5044SSudeep Holla };
232933c5044SSudeep Holla 
233933c5044SSudeep Holla struct scmi_driver {
234933c5044SSudeep Holla 	const char *name;
235933c5044SSudeep Holla 	int (*probe)(struct scmi_device *sdev);
236933c5044SSudeep Holla 	void (*remove)(struct scmi_device *sdev);
237933c5044SSudeep Holla 	const struct scmi_device_id *id_table;
238933c5044SSudeep Holla 
239933c5044SSudeep Holla 	struct device_driver driver;
240933c5044SSudeep Holla };
241933c5044SSudeep Holla 
242933c5044SSudeep Holla #define to_scmi_driver(d) container_of(d, struct scmi_driver, driver)
243933c5044SSudeep Holla 
244933c5044SSudeep Holla #ifdef CONFIG_ARM_SCMI_PROTOCOL
245933c5044SSudeep Holla int scmi_driver_register(struct scmi_driver *driver,
246933c5044SSudeep Holla 			 struct module *owner, const char *mod_name);
247933c5044SSudeep Holla void scmi_driver_unregister(struct scmi_driver *driver);
248933c5044SSudeep Holla #else
249933c5044SSudeep Holla static inline int
250933c5044SSudeep Holla scmi_driver_register(struct scmi_driver *driver, struct module *owner,
251933c5044SSudeep Holla 		     const char *mod_name)
252933c5044SSudeep Holla {
253933c5044SSudeep Holla 	return -EINVAL;
254933c5044SSudeep Holla }
255933c5044SSudeep Holla 
256933c5044SSudeep Holla static inline void scmi_driver_unregister(struct scmi_driver *driver) {}
257933c5044SSudeep Holla #endif /* CONFIG_ARM_SCMI_PROTOCOL */
258933c5044SSudeep Holla 
259933c5044SSudeep Holla #define scmi_register(driver) \
260933c5044SSudeep Holla 	scmi_driver_register(driver, THIS_MODULE, KBUILD_MODNAME)
261933c5044SSudeep Holla #define scmi_unregister(driver) \
262933c5044SSudeep Holla 	scmi_driver_unregister(driver)
263933c5044SSudeep Holla 
264933c5044SSudeep Holla /**
265933c5044SSudeep Holla  * module_scmi_driver() - Helper macro for registering a scmi driver
266933c5044SSudeep Holla  * @__scmi_driver: scmi_driver structure
267933c5044SSudeep Holla  *
268933c5044SSudeep Holla  * Helper macro for scmi drivers to set up proper module init / exit
269933c5044SSudeep Holla  * functions.  Replaces module_init() and module_exit() and keeps people from
270933c5044SSudeep Holla  * printing pointless things to the kernel log when their driver is loaded.
271933c5044SSudeep Holla  */
272933c5044SSudeep Holla #define module_scmi_driver(__scmi_driver)	\
273933c5044SSudeep Holla 	module_driver(__scmi_driver, scmi_register, scmi_unregister)
274933c5044SSudeep Holla 
275933c5044SSudeep Holla typedef int (*scmi_prot_init_fn_t)(struct scmi_handle *);
276933c5044SSudeep Holla int scmi_protocol_register(int protocol_id, scmi_prot_init_fn_t fn);
277933c5044SSudeep Holla void scmi_protocol_unregister(int protocol_id);
278