xref: /linux/drivers/firmware/arm_scmi/protocols.h (revision eb01fe7abbe2d0b38824d2a93fdb4cc3eaf2ccc1)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * System Control and Management Interface (SCMI) Message Protocol
4  * protocols common header file containing some definitions, structures
5  * and function prototypes used in all the different SCMI protocols.
6  *
7  * Copyright (C) 2022 ARM Ltd.
8  */
9 #ifndef _SCMI_PROTOCOLS_H
10 #define _SCMI_PROTOCOLS_H
11 
12 #include <linux/bitfield.h>
13 #include <linux/completion.h>
14 #include <linux/device.h>
15 #include <linux/errno.h>
16 #include <linux/kernel.h>
17 #include <linux/hashtable.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
20 #include <linux/refcount.h>
21 #include <linux/scmi_protocol.h>
22 #include <linux/spinlock.h>
23 #include <linux/types.h>
24 
25 #include <asm/unaligned.h>
26 
27 #define PROTOCOL_REV_MINOR_MASK	GENMASK(15, 0)
28 #define PROTOCOL_REV_MAJOR_MASK	GENMASK(31, 16)
29 #define PROTOCOL_REV_MAJOR(x)	((u16)(FIELD_GET(PROTOCOL_REV_MAJOR_MASK, (x))))
30 #define PROTOCOL_REV_MINOR(x)	((u16)(FIELD_GET(PROTOCOL_REV_MINOR_MASK, (x))))
31 
32 enum scmi_common_cmd {
33 	PROTOCOL_VERSION = 0x0,
34 	PROTOCOL_ATTRIBUTES = 0x1,
35 	PROTOCOL_MESSAGE_ATTRIBUTES = 0x2,
36 	NEGOTIATE_PROTOCOL_VERSION = 0x10,
37 };
38 
39 /**
40  * struct scmi_msg_resp_prot_version - Response for a message
41  *
42  * @minor_version: Minor version of the ABI that firmware supports
43  * @major_version: Major version of the ABI that firmware supports
44  *
45  * In general, ABI version changes follow the rule that minor version increments
46  * are backward compatible. Major revision changes in ABI may not be
47  * backward compatible.
48  *
49  * Response to a generic message with message type SCMI_MSG_VERSION
50  */
51 struct scmi_msg_resp_prot_version {
52 	__le16 minor_version;
53 	__le16 major_version;
54 };
55 
56 /**
57  * struct scmi_msg - Message(Tx/Rx) structure
58  *
59  * @buf: Buffer pointer
60  * @len: Length of data in the Buffer
61  */
62 struct scmi_msg {
63 	void *buf;
64 	size_t len;
65 };
66 
67 /**
68  * struct scmi_msg_hdr - Message(Tx/Rx) header
69  *
70  * @id: The identifier of the message being sent
71  * @protocol_id: The identifier of the protocol used to send @id message
72  * @type: The SCMI type for this message
73  * @seq: The token to identify the message. When a message returns, the
74  *	platform returns the whole message header unmodified including the
75  *	token
76  * @status: Status of the transfer once it's complete
77  * @poll_completion: Indicate if the transfer needs to be polled for
78  *	completion or interrupt mode is used
79  */
80 struct scmi_msg_hdr {
81 	u8 id;
82 	u8 protocol_id;
83 	u8 type;
84 	u16 seq;
85 	u32 status;
86 	bool poll_completion;
87 };
88 
89 /**
90  * struct scmi_xfer - Structure representing a message flow
91  *
92  * @transfer_id: Unique ID for debug & profiling purpose
93  * @hdr: Transmit message header
94  * @tx: Transmit message
95  * @rx: Receive message, the buffer should be pre-allocated to store
96  *	message. If request-ACK protocol is used, we can reuse the same
97  *	buffer for the rx path as we use for the tx path.
98  * @done: command message transmit completion event
99  * @async_done: pointer to delayed response message received event completion
100  * @pending: True for xfers added to @pending_xfers hashtable
101  * @node: An hlist_node reference used to store this xfer, alternatively, on
102  *	  the free list @free_xfers or in the @pending_xfers hashtable
103  * @users: A refcount to track the active users for this xfer.
104  *	   This is meant to protect against the possibility that, when a command
105  *	   transaction times out concurrently with the reception of a valid
106  *	   response message, the xfer could be finally put on the TX path, and
107  *	   so vanish, while on the RX path scmi_rx_callback() is still
108  *	   processing it: in such a case this refcounting will ensure that, even
109  *	   though the timed-out transaction will anyway cause the command
110  *	   request to be reported as failed by time-out, the underlying xfer
111  *	   cannot be discarded and possibly reused until the last one user on
112  *	   the RX path has released it.
113  * @busy: An atomic flag to ensure exclusive write access to this xfer
114  * @state: The current state of this transfer, with states transitions deemed
115  *	   valid being:
116  *	    - SCMI_XFER_SENT_OK -> SCMI_XFER_RESP_OK [ -> SCMI_XFER_DRESP_OK ]
117  *	    - SCMI_XFER_SENT_OK -> SCMI_XFER_DRESP_OK
118  *	      (Missing synchronous response is assumed OK and ignored)
119  * @flags: Optional flags associated to this xfer.
120  * @lock: A spinlock to protect state and busy fields.
121  * @priv: A pointer for transport private usage.
122  */
123 struct scmi_xfer {
124 	int transfer_id;
125 	struct scmi_msg_hdr hdr;
126 	struct scmi_msg tx;
127 	struct scmi_msg rx;
128 	struct completion done;
129 	struct completion *async_done;
130 	bool pending;
131 	struct hlist_node node;
132 	refcount_t users;
133 #define SCMI_XFER_FREE		0
134 #define SCMI_XFER_BUSY		1
135 	atomic_t busy;
136 #define SCMI_XFER_SENT_OK	0
137 #define SCMI_XFER_RESP_OK	1
138 #define SCMI_XFER_DRESP_OK	2
139 	int state;
140 #define SCMI_XFER_FLAG_IS_RAW	BIT(0)
141 #define SCMI_XFER_IS_RAW(x)	((x)->flags & SCMI_XFER_FLAG_IS_RAW)
142 #define SCMI_XFER_FLAG_CHAN_SET	BIT(1)
143 #define SCMI_XFER_IS_CHAN_SET(x)	\
144 	((x)->flags & SCMI_XFER_FLAG_CHAN_SET)
145 	int flags;
146 	/* A lock to protect state and busy fields */
147 	spinlock_t lock;
148 	void *priv;
149 };
150 
151 struct scmi_xfer_ops;
152 struct scmi_proto_helpers_ops;
153 
154 /**
155  * struct scmi_protocol_handle  - Reference to an initialized protocol instance
156  *
157  * @dev: A reference to the associated SCMI instance device (handle->dev).
158  * @xops: A reference to a struct holding refs to the core xfer operations that
159  *	  can be used by the protocol implementation to generate SCMI messages.
160  * @set_priv: A method to set protocol private data for this instance.
161  * @get_priv: A method to get protocol private data previously set.
162  *
163  * This structure represents a protocol initialized against specific SCMI
164  * instance and it will be used as follows:
165  * - as a parameter fed from the core to the protocol initialization code so
166  *   that it can access the core xfer operations to build and generate SCMI
167  *   messages exclusively for the specific underlying protocol instance.
168  * - as an opaque handle fed by an SCMI driver user when it tries to access
169  *   this protocol through its own protocol operations.
170  *   In this case this handle will be returned as an opaque object together
171  *   with the related protocol operations when the SCMI driver tries to access
172  *   the protocol.
173  */
174 struct scmi_protocol_handle {
175 	struct device *dev;
176 	const struct scmi_xfer_ops *xops;
177 	const struct scmi_proto_helpers_ops *hops;
178 	int (*set_priv)(const struct scmi_protocol_handle *ph, void *priv,
179 			u32 version);
180 	void *(*get_priv)(const struct scmi_protocol_handle *ph);
181 };
182 
183 /**
184  * struct scmi_iterator_state  - Iterator current state descriptor
185  * @desc_index: Starting index for the current mulit-part request.
186  * @num_returned: Number of returned items in the last multi-part reply.
187  * @num_remaining: Number of remaining items in the multi-part message.
188  * @max_resources: Maximum acceptable number of items, configured by the caller
189  *		   depending on the underlying resources that it is querying.
190  * @loop_idx: The iterator loop index in the current multi-part reply.
191  * @rx_len: Size in bytes of the currenly processed message; it can be used by
192  *	    the user of the iterator to verify a reply size.
193  * @priv: Optional pointer to some additional state-related private data setup
194  *	  by the caller during the iterations.
195  */
196 struct scmi_iterator_state {
197 	unsigned int desc_index;
198 	unsigned int num_returned;
199 	unsigned int num_remaining;
200 	unsigned int max_resources;
201 	unsigned int loop_idx;
202 	size_t rx_len;
203 	void *priv;
204 };
205 
206 /**
207  * struct scmi_iterator_ops  - Custom iterator operations
208  * @prepare_message: An operation to provide the custom logic to fill in the
209  *		     SCMI command request pointed by @message. @desc_index is
210  *		     a reference to the next index to use in the multi-part
211  *		     request.
212  * @update_state: An operation to provide the custom logic to update the
213  *		  iterator state from the actual message response.
214  * @process_response: An operation to provide the custom logic needed to process
215  *		      each chunk of the multi-part message.
216  */
217 struct scmi_iterator_ops {
218 	void (*prepare_message)(void *message, unsigned int desc_index,
219 				const void *priv);
220 	int (*update_state)(struct scmi_iterator_state *st,
221 			    const void *response, void *priv);
222 	int (*process_response)(const struct scmi_protocol_handle *ph,
223 				const void *response,
224 				struct scmi_iterator_state *st, void *priv);
225 };
226 
227 struct scmi_fc_db_info {
228 	int width;
229 	u64 set;
230 	u64 mask;
231 	void __iomem *addr;
232 };
233 
234 struct scmi_fc_info {
235 	void __iomem *set_addr;
236 	void __iomem *get_addr;
237 	struct scmi_fc_db_info *set_db;
238 	u32 rate_limit;
239 };
240 
241 /**
242  * struct scmi_proto_helpers_ops  - References to common protocol helpers
243  * @extended_name_get: A common helper function to retrieve extended naming
244  *		       for the specified resource using the specified command.
245  *		       Result is returned as a NULL terminated string in the
246  *		       pre-allocated area pointed to by @name with maximum
247  *		       capacity of @len bytes.
248  * @iter_response_init: A common helper to initialize a generic iterator to
249  *			parse multi-message responses: when run the iterator
250  *			will take care to send the initial command request as
251  *			specified by @msg_id and @tx_size and then to parse the
252  *			multi-part responses using the custom operations
253  *			provided in @ops.
254  * @iter_response_run: A common helper to trigger the run of a previously
255  *		       initialized iterator.
256  * @protocol_msg_check: A common helper to check is a specific protocol message
257  *			is supported.
258  * @fastchannel_init: A common helper used to initialize FC descriptors by
259  *		      gathering FC descriptions from the SCMI platform server.
260  * @fastchannel_db_ring: A common helper to ring a FC doorbell.
261  */
262 struct scmi_proto_helpers_ops {
263 	int (*extended_name_get)(const struct scmi_protocol_handle *ph,
264 				 u8 cmd_id, u32 res_id, u32 *flags, char *name,
265 				 size_t len);
266 	void *(*iter_response_init)(const struct scmi_protocol_handle *ph,
267 				    struct scmi_iterator_ops *ops,
268 				    unsigned int max_resources, u8 msg_id,
269 				    size_t tx_size, void *priv);
270 	int (*iter_response_run)(void *iter);
271 	int (*protocol_msg_check)(const struct scmi_protocol_handle *ph,
272 				  u32 message_id, u32 *attributes);
273 	void (*fastchannel_init)(const struct scmi_protocol_handle *ph,
274 				 u8 describe_id, u32 message_id,
275 				 u32 valid_size, u32 domain,
276 				 void __iomem **p_addr,
277 				 struct scmi_fc_db_info **p_db,
278 				 u32 *rate_limit);
279 	void (*fastchannel_db_ring)(struct scmi_fc_db_info *db);
280 };
281 
282 /**
283  * struct scmi_xfer_ops  - References to the core SCMI xfer operations.
284  * @version_get: Get this version protocol.
285  * @xfer_get_init: Initialize one struct xfer if any xfer slot is free.
286  * @reset_rx_to_maxsz: Reset rx size to max transport size.
287  * @do_xfer: Do the SCMI transfer.
288  * @do_xfer_with_response: Do the SCMI transfer waiting for a response.
289  * @xfer_put: Free the xfer slot.
290  *
291  * Note that all this operations expect a protocol handle as first parameter;
292  * they then internally use it to infer the underlying protocol number: this
293  * way is not possible for a protocol implementation to forge messages for
294  * another protocol.
295  */
296 struct scmi_xfer_ops {
297 	int (*version_get)(const struct scmi_protocol_handle *ph, u32 *version);
298 	int (*xfer_get_init)(const struct scmi_protocol_handle *ph, u8 msg_id,
299 			     size_t tx_size, size_t rx_size,
300 			     struct scmi_xfer **p);
301 	void (*reset_rx_to_maxsz)(const struct scmi_protocol_handle *ph,
302 				  struct scmi_xfer *xfer);
303 	int (*do_xfer)(const struct scmi_protocol_handle *ph,
304 		       struct scmi_xfer *xfer);
305 	int (*do_xfer_with_response)(const struct scmi_protocol_handle *ph,
306 				     struct scmi_xfer *xfer);
307 	void (*xfer_put)(const struct scmi_protocol_handle *ph,
308 			 struct scmi_xfer *xfer);
309 };
310 
311 typedef int (*scmi_prot_init_ph_fn_t)(const struct scmi_protocol_handle *);
312 
313 /**
314  * struct scmi_protocol  - Protocol descriptor
315  * @id: Protocol ID.
316  * @owner: Module reference if any.
317  * @instance_init: Mandatory protocol initialization function.
318  * @instance_deinit: Optional protocol de-initialization function.
319  * @ops: Optional reference to the operations provided by the protocol and
320  *	 exposed in scmi_protocol.h.
321  * @events: An optional reference to the events supported by this protocol.
322  * @supported_version: The highest version currently supported for this
323  *		       protocol by the agent. Each protocol implementation
324  *		       in the agent is supposed to downgrade to match the
325  *		       protocol version supported by the platform.
326  */
327 struct scmi_protocol {
328 	const u8				id;
329 	struct module				*owner;
330 	const scmi_prot_init_ph_fn_t		instance_init;
331 	const scmi_prot_init_ph_fn_t		instance_deinit;
332 	const void				*ops;
333 	const struct scmi_protocol_events	*events;
334 	unsigned int				supported_version;
335 };
336 
337 #define DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(name, proto)	\
338 static const struct scmi_protocol *__this_proto = &(proto);	\
339 								\
340 int __init scmi_##name##_register(void)				\
341 {								\
342 	return scmi_protocol_register(__this_proto);		\
343 }								\
344 								\
345 void __exit scmi_##name##_unregister(void)			\
346 {								\
347 	scmi_protocol_unregister(__this_proto);			\
348 }
349 
350 #define DECLARE_SCMI_REGISTER_UNREGISTER(func)		\
351 	int __init scmi_##func##_register(void);	\
352 	void __exit scmi_##func##_unregister(void)
353 DECLARE_SCMI_REGISTER_UNREGISTER(base);
354 DECLARE_SCMI_REGISTER_UNREGISTER(clock);
355 DECLARE_SCMI_REGISTER_UNREGISTER(perf);
356 DECLARE_SCMI_REGISTER_UNREGISTER(power);
357 DECLARE_SCMI_REGISTER_UNREGISTER(reset);
358 DECLARE_SCMI_REGISTER_UNREGISTER(sensors);
359 DECLARE_SCMI_REGISTER_UNREGISTER(voltage);
360 DECLARE_SCMI_REGISTER_UNREGISTER(system);
361 DECLARE_SCMI_REGISTER_UNREGISTER(powercap);
362 
363 #endif /* _SCMI_PROTOCOLS_H */
364