1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * System Control and Management Interface (SCMI) Message Protocol
4 * driver common header file containing some definitions, structures
5 * and function prototypes used in all the different SCMI protocols.
6 *
7 * Copyright (C) 2018-2021 ARM Ltd.
8 */
9 #ifndef _SCMI_COMMON_H
10 #define _SCMI_COMMON_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 #include "notify.h"
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 #define MAX_PROTOCOLS_IMP 16
34 #define MAX_OPPS 16
35
36 enum scmi_common_cmd {
37 PROTOCOL_VERSION = 0x0,
38 PROTOCOL_ATTRIBUTES = 0x1,
39 PROTOCOL_MESSAGE_ATTRIBUTES = 0x2,
40 };
41
42 /**
43 * struct scmi_msg_resp_prot_version - Response for a message
44 *
45 * @minor_version: Minor version of the ABI that firmware supports
46 * @major_version: Major version of the ABI that firmware supports
47 *
48 * In general, ABI version changes follow the rule that minor version increments
49 * are backward compatible. Major revision changes in ABI may not be
50 * backward compatible.
51 *
52 * Response to a generic message with message type SCMI_MSG_VERSION
53 */
54 struct scmi_msg_resp_prot_version {
55 __le16 minor_version;
56 __le16 major_version;
57 };
58
59 #define MSG_ID_MASK GENMASK(7, 0)
60 #define MSG_XTRACT_ID(hdr) FIELD_GET(MSG_ID_MASK, (hdr))
61 #define MSG_TYPE_MASK GENMASK(9, 8)
62 #define MSG_XTRACT_TYPE(hdr) FIELD_GET(MSG_TYPE_MASK, (hdr))
63 #define MSG_TYPE_COMMAND 0
64 #define MSG_TYPE_DELAYED_RESP 2
65 #define MSG_TYPE_NOTIFICATION 3
66 #define MSG_PROTOCOL_ID_MASK GENMASK(17, 10)
67 #define MSG_XTRACT_PROT_ID(hdr) FIELD_GET(MSG_PROTOCOL_ID_MASK, (hdr))
68 #define MSG_TOKEN_ID_MASK GENMASK(27, 18)
69 #define MSG_XTRACT_TOKEN(hdr) FIELD_GET(MSG_TOKEN_ID_MASK, (hdr))
70 #define MSG_TOKEN_MAX (MSG_XTRACT_TOKEN(MSG_TOKEN_ID_MASK) + 1)
71
72 /*
73 * Size of @pending_xfers hashtable included in @scmi_xfers_info; ideally, in
74 * order to minimize space and collisions, this should equal max_msg, i.e. the
75 * maximum number of in-flight messages on a specific platform, but such value
76 * is only available at runtime while kernel hashtables are statically sized:
77 * pick instead as a fixed static size the maximum number of entries that can
78 * fit the whole table into one 4k page.
79 */
80 #define SCMI_PENDING_XFERS_HT_ORDER_SZ 9
81
82 /**
83 * struct scmi_msg_hdr - Message(Tx/Rx) header
84 *
85 * @id: The identifier of the message being sent
86 * @protocol_id: The identifier of the protocol used to send @id message
87 * @type: The SCMI type for this message
88 * @seq: The token to identify the message. When a message returns, the
89 * platform returns the whole message header unmodified including the
90 * token
91 * @status: Status of the transfer once it's complete
92 * @poll_completion: Indicate if the transfer needs to be polled for
93 * completion or interrupt mode is used
94 */
95 struct scmi_msg_hdr {
96 u8 id;
97 u8 protocol_id;
98 u8 type;
99 u16 seq;
100 u32 status;
101 bool poll_completion;
102 };
103
104 /**
105 * pack_scmi_header() - packs and returns 32-bit header
106 *
107 * @hdr: pointer to header containing all the information on message id,
108 * protocol id, sequence id and type.
109 *
110 * Return: 32-bit packed message header to be sent to the platform.
111 */
pack_scmi_header(struct scmi_msg_hdr * hdr)112 static inline u32 pack_scmi_header(struct scmi_msg_hdr *hdr)
113 {
114 return FIELD_PREP(MSG_ID_MASK, hdr->id) |
115 FIELD_PREP(MSG_TYPE_MASK, hdr->type) |
116 FIELD_PREP(MSG_TOKEN_ID_MASK, hdr->seq) |
117 FIELD_PREP(MSG_PROTOCOL_ID_MASK, hdr->protocol_id);
118 }
119
120 /**
121 * unpack_scmi_header() - unpacks and records message and protocol id
122 *
123 * @msg_hdr: 32-bit packed message header sent from the platform
124 * @hdr: pointer to header to fetch message and protocol id.
125 */
unpack_scmi_header(u32 msg_hdr,struct scmi_msg_hdr * hdr)126 static inline void unpack_scmi_header(u32 msg_hdr, struct scmi_msg_hdr *hdr)
127 {
128 hdr->id = MSG_XTRACT_ID(msg_hdr);
129 hdr->protocol_id = MSG_XTRACT_PROT_ID(msg_hdr);
130 hdr->type = MSG_XTRACT_TYPE(msg_hdr);
131 }
132
133 /**
134 * struct scmi_msg - Message(Tx/Rx) structure
135 *
136 * @buf: Buffer pointer
137 * @len: Length of data in the Buffer
138 */
139 struct scmi_msg {
140 void *buf;
141 size_t len;
142 };
143
144 /**
145 * struct scmi_xfer - Structure representing a message flow
146 *
147 * @transfer_id: Unique ID for debug & profiling purpose
148 * @hdr: Transmit message header
149 * @tx: Transmit message
150 * @rx: Receive message, the buffer should be pre-allocated to store
151 * message. If request-ACK protocol is used, we can reuse the same
152 * buffer for the rx path as we use for the tx path.
153 * @done: command message transmit completion event
154 * @async_done: pointer to delayed response message received event completion
155 * @pending: True for xfers added to @pending_xfers hashtable
156 * @node: An hlist_node reference used to store this xfer, alternatively, on
157 * the free list @free_xfers or in the @pending_xfers hashtable
158 * @users: A refcount to track the active users for this xfer.
159 * This is meant to protect against the possibility that, when a command
160 * transaction times out concurrently with the reception of a valid
161 * response message, the xfer could be finally put on the TX path, and
162 * so vanish, while on the RX path scmi_rx_callback() is still
163 * processing it: in such a case this refcounting will ensure that, even
164 * though the timed-out transaction will anyway cause the command
165 * request to be reported as failed by time-out, the underlying xfer
166 * cannot be discarded and possibly reused until the last one user on
167 * the RX path has released it.
168 * @busy: An atomic flag to ensure exclusive write access to this xfer
169 * @state: The current state of this transfer, with states transitions deemed
170 * valid being:
171 * - SCMI_XFER_SENT_OK -> SCMI_XFER_RESP_OK [ -> SCMI_XFER_DRESP_OK ]
172 * - SCMI_XFER_SENT_OK -> SCMI_XFER_DRESP_OK
173 * (Missing synchronous response is assumed OK and ignored)
174 * @lock: A spinlock to protect state and busy fields.
175 * @priv: A pointer for transport private usage.
176 */
177 struct scmi_xfer {
178 int transfer_id;
179 struct scmi_msg_hdr hdr;
180 struct scmi_msg tx;
181 struct scmi_msg rx;
182 struct completion done;
183 struct completion *async_done;
184 bool pending;
185 struct hlist_node node;
186 refcount_t users;
187 #define SCMI_XFER_FREE 0
188 #define SCMI_XFER_BUSY 1
189 atomic_t busy;
190 #define SCMI_XFER_SENT_OK 0
191 #define SCMI_XFER_RESP_OK 1
192 #define SCMI_XFER_DRESP_OK 2
193 int state;
194 /* A lock to protect state and busy fields */
195 spinlock_t lock;
196 void *priv;
197 };
198
199 /*
200 * An helper macro to lookup an xfer from the @pending_xfers hashtable
201 * using the message sequence number token as a key.
202 */
203 #define XFER_FIND(__ht, __k) \
204 ({ \
205 typeof(__k) k_ = __k; \
206 struct scmi_xfer *xfer_ = NULL; \
207 \
208 hash_for_each_possible((__ht), xfer_, node, k_) \
209 if (xfer_->hdr.seq == k_) \
210 break; \
211 xfer_; \
212 })
213
214 struct scmi_xfer_ops;
215
216 /**
217 * struct scmi_protocol_handle - Reference to an initialized protocol instance
218 *
219 * @dev: A reference to the associated SCMI instance device (handle->dev).
220 * @xops: A reference to a struct holding refs to the core xfer operations that
221 * can be used by the protocol implementation to generate SCMI messages.
222 * @set_priv: A method to set protocol private data for this instance.
223 * @get_priv: A method to get protocol private data previously set.
224 *
225 * This structure represents a protocol initialized against specific SCMI
226 * instance and it will be used as follows:
227 * - as a parameter fed from the core to the protocol initialization code so
228 * that it can access the core xfer operations to build and generate SCMI
229 * messages exclusively for the specific underlying protocol instance.
230 * - as an opaque handle fed by an SCMI driver user when it tries to access
231 * this protocol through its own protocol operations.
232 * In this case this handle will be returned as an opaque object together
233 * with the related protocol operations when the SCMI driver tries to access
234 * the protocol.
235 */
236 struct scmi_protocol_handle {
237 struct device *dev;
238 const struct scmi_xfer_ops *xops;
239 int (*set_priv)(const struct scmi_protocol_handle *ph, void *priv);
240 void *(*get_priv)(const struct scmi_protocol_handle *ph);
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 struct scmi_revision_info *
273 scmi_revision_area_get(const struct scmi_protocol_handle *ph);
274 int scmi_handle_put(const struct scmi_handle *handle);
275 void scmi_device_link_add(struct device *consumer, struct device *supplier);
276 struct scmi_handle *scmi_handle_get(struct device *dev);
277 void scmi_set_handle(struct scmi_device *scmi_dev);
278 void scmi_setup_protocol_implemented(const struct scmi_protocol_handle *ph,
279 u8 *prot_imp);
280
281 typedef int (*scmi_prot_init_ph_fn_t)(const struct scmi_protocol_handle *);
282
283 /**
284 * struct scmi_protocol - Protocol descriptor
285 * @id: Protocol ID.
286 * @owner: Module reference if any.
287 * @instance_init: Mandatory protocol initialization function.
288 * @instance_deinit: Optional protocol de-initialization function.
289 * @ops: Optional reference to the operations provided by the protocol and
290 * exposed in scmi_protocol.h.
291 * @events: An optional reference to the events supported by this protocol.
292 */
293 struct scmi_protocol {
294 const u8 id;
295 struct module *owner;
296 const scmi_prot_init_ph_fn_t instance_init;
297 const scmi_prot_init_ph_fn_t instance_deinit;
298 const void *ops;
299 const struct scmi_protocol_events *events;
300 };
301
302 int __init scmi_bus_init(void);
303 void __exit scmi_bus_exit(void);
304
305 #define DECLARE_SCMI_REGISTER_UNREGISTER(func) \
306 int __init scmi_##func##_register(void); \
307 void __exit scmi_##func##_unregister(void)
308 DECLARE_SCMI_REGISTER_UNREGISTER(base);
309 DECLARE_SCMI_REGISTER_UNREGISTER(clock);
310 DECLARE_SCMI_REGISTER_UNREGISTER(perf);
311 DECLARE_SCMI_REGISTER_UNREGISTER(power);
312 DECLARE_SCMI_REGISTER_UNREGISTER(reset);
313 DECLARE_SCMI_REGISTER_UNREGISTER(sensors);
314 DECLARE_SCMI_REGISTER_UNREGISTER(voltage);
315 DECLARE_SCMI_REGISTER_UNREGISTER(system);
316
317 #define DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(name, proto) \
318 static const struct scmi_protocol *__this_proto = &(proto); \
319 \
320 int __init scmi_##name##_register(void) \
321 { \
322 return scmi_protocol_register(__this_proto); \
323 } \
324 \
325 void __exit scmi_##name##_unregister(void) \
326 { \
327 scmi_protocol_unregister(__this_proto); \
328 }
329
330 const struct scmi_protocol *scmi_protocol_get(int protocol_id);
331 void scmi_protocol_put(int protocol_id);
332
333 int scmi_protocol_acquire(const struct scmi_handle *handle, u8 protocol_id);
334 void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id);
335
336 /* SCMI Transport */
337 /**
338 * struct scmi_chan_info - Structure representing a SCMI channel information
339 *
340 * @dev: Reference to device in the SCMI hierarchy corresponding to this
341 * channel
342 * @handle: Pointer to SCMI entity handle
343 * @transport_info: Transport layer related information
344 */
345 struct scmi_chan_info {
346 struct device *dev;
347 struct scmi_handle *handle;
348 void *transport_info;
349 };
350
351 /**
352 * struct scmi_transport_ops - Structure representing a SCMI transport ops
353 *
354 * @link_supplier: Optional callback to add link to a supplier device
355 * @chan_available: Callback to check if channel is available or not
356 * @chan_setup: Callback to allocate and setup a channel
357 * @chan_free: Callback to free a channel
358 * @get_max_msg: Optional callback to provide max_msg dynamically
359 * Returns the maximum number of messages for the channel type
360 * (tx or rx) that can be pending simultaneously in the system
361 * @send_message: Callback to send a message
362 * @mark_txdone: Callback to mark tx as done
363 * @fetch_response: Callback to fetch response
364 * @fetch_notification: Callback to fetch notification
365 * @clear_channel: Callback to clear a channel
366 * @poll_done: Callback to poll transfer status
367 */
368 struct scmi_transport_ops {
369 int (*link_supplier)(struct device *dev);
370 bool (*chan_available)(struct device *dev, int idx);
371 int (*chan_setup)(struct scmi_chan_info *cinfo, struct device *dev,
372 bool tx);
373 int (*chan_free)(int id, void *p, void *data);
374 unsigned int (*get_max_msg)(struct scmi_chan_info *base_cinfo);
375 int (*send_message)(struct scmi_chan_info *cinfo,
376 struct scmi_xfer *xfer);
377 void (*mark_txdone)(struct scmi_chan_info *cinfo, int ret);
378 void (*fetch_response)(struct scmi_chan_info *cinfo,
379 struct scmi_xfer *xfer);
380 void (*fetch_notification)(struct scmi_chan_info *cinfo,
381 size_t max_len, struct scmi_xfer *xfer);
382 void (*clear_channel)(struct scmi_chan_info *cinfo);
383 bool (*poll_done)(struct scmi_chan_info *cinfo, struct scmi_xfer *xfer);
384 };
385
386 int scmi_protocol_device_request(const struct scmi_device_id *id_table);
387 void scmi_protocol_device_unrequest(const struct scmi_device_id *id_table);
388 struct scmi_device *scmi_child_dev_find(struct device *parent,
389 int prot_id, const char *name);
390
391 /**
392 * struct scmi_desc - Description of SoC integration
393 *
394 * @transport_init: An optional function that a transport can provide to
395 * initialize some transport-specific setup during SCMI core
396 * initialization, so ahead of SCMI core probing.
397 * @transport_exit: An optional function that a transport can provide to
398 * de-initialize some transport-specific setup during SCMI core
399 * de-initialization, so after SCMI core removal.
400 * @ops: Pointer to the transport specific ops structure
401 * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
402 * @max_msg: Maximum number of messages for a channel type (tx or rx) that can
403 * be pending simultaneously in the system. May be overridden by the
404 * get_max_msg op.
405 * @max_msg_size: Maximum size of data per message that can be handled.
406 */
407 struct scmi_desc {
408 int (*transport_init)(void);
409 void (*transport_exit)(void);
410 const struct scmi_transport_ops *ops;
411 int max_rx_timeout_ms;
412 int max_msg;
413 int max_msg_size;
414 };
415
416 #ifdef CONFIG_ARM_SCMI_TRANSPORT_MAILBOX
417 extern const struct scmi_desc scmi_mailbox_desc;
418 #endif
419 #ifdef CONFIG_ARM_SCMI_TRANSPORT_SMC
420 extern const struct scmi_desc scmi_smc_desc;
421 #endif
422 #ifdef CONFIG_ARM_SCMI_TRANSPORT_VIRTIO
423 extern const struct scmi_desc scmi_virtio_desc;
424 #endif
425
426 void scmi_rx_callback(struct scmi_chan_info *cinfo, u32 msg_hdr, void *priv);
427 void scmi_free_channel(struct scmi_chan_info *cinfo, struct idr *idr, int id);
428
429 /* shmem related declarations */
430 struct scmi_shared_mem;
431
432 void shmem_tx_prepare(struct scmi_shared_mem __iomem *shmem,
433 struct scmi_xfer *xfer);
434 u32 shmem_read_header(struct scmi_shared_mem __iomem *shmem);
435 void shmem_fetch_response(struct scmi_shared_mem __iomem *shmem,
436 struct scmi_xfer *xfer);
437 void shmem_fetch_notification(struct scmi_shared_mem __iomem *shmem,
438 size_t max_len, struct scmi_xfer *xfer);
439 void shmem_clear_channel(struct scmi_shared_mem __iomem *shmem);
440 bool shmem_poll_done(struct scmi_shared_mem __iomem *shmem,
441 struct scmi_xfer *xfer);
442 bool shmem_channel_free(struct scmi_shared_mem __iomem *shmem);
443
444 /* declarations for message passing transports */
445 struct scmi_msg_payld;
446
447 /* Maximum overhead of message w.r.t. struct scmi_desc.max_msg_size */
448 #define SCMI_MSG_MAX_PROT_OVERHEAD (2 * sizeof(__le32))
449
450 size_t msg_response_size(struct scmi_xfer *xfer);
451 size_t msg_command_size(struct scmi_xfer *xfer);
452 void msg_tx_prepare(struct scmi_msg_payld *msg, struct scmi_xfer *xfer);
453 u32 msg_read_header(struct scmi_msg_payld *msg);
454 void msg_fetch_response(struct scmi_msg_payld *msg, size_t len,
455 struct scmi_xfer *xfer);
456 void msg_fetch_notification(struct scmi_msg_payld *msg, size_t len,
457 size_t max_len, struct scmi_xfer *xfer);
458
459 void scmi_notification_instance_data_set(const struct scmi_handle *handle,
460 void *priv);
461 void *scmi_notification_instance_data_get(const struct scmi_handle *handle);
462 #endif /* _SCMI_COMMON_H */
463