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