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