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