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