1 /* SPDX-License-Identifier: GPL-2.0 */
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.
7 * Copyright (C) 2018 ARM Ltd.
10 #define _SCMI_COMMON_H
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/scmi_protocol.h>
18 #include <linux/types.h>
20 #include <asm/unaligned.h>
22 #define PROTOCOL_REV_MINOR_MASK GENMASK(15, 0)
23 #define PROTOCOL_REV_MAJOR_MASK GENMASK(31, 16)
24 #define PROTOCOL_REV_MAJOR(x) (u16)(FIELD_GET(PROTOCOL_REV_MAJOR_MASK, (x)))
25 #define PROTOCOL_REV_MINOR(x) (u16)(FIELD_GET(PROTOCOL_REV_MINOR_MASK, (x)))
26 #define MAX_PROTOCOLS_IMP 16
29 enum scmi_common_cmd {
30 PROTOCOL_VERSION = 0x0,
31 PROTOCOL_ATTRIBUTES = 0x1,
32 PROTOCOL_MESSAGE_ATTRIBUTES = 0x2,
36 * struct scmi_msg_resp_prot_version - Response for a message
38 * @minor_version: Minor version of the ABI that firmware supports
39 * @major_version: Major version of the ABI that firmware supports
41 * In general, ABI version changes follow the rule that minor version increments
42 * are backward compatible. Major revision changes in ABI may not be
43 * backward compatible.
45 * Response to a generic message with message type SCMI_MSG_VERSION
47 struct scmi_msg_resp_prot_version {
52 #define MSG_ID_MASK GENMASK(7, 0)
53 #define MSG_XTRACT_ID(hdr) FIELD_GET(MSG_ID_MASK, (hdr))
54 #define MSG_TYPE_MASK GENMASK(9, 8)
55 #define MSG_XTRACT_TYPE(hdr) FIELD_GET(MSG_TYPE_MASK, (hdr))
56 #define MSG_TYPE_COMMAND 0
57 #define MSG_TYPE_DELAYED_RESP 2
58 #define MSG_TYPE_NOTIFICATION 3
59 #define MSG_PROTOCOL_ID_MASK GENMASK(17, 10)
60 #define MSG_XTRACT_PROT_ID(hdr) FIELD_GET(MSG_PROTOCOL_ID_MASK, (hdr))
61 #define MSG_TOKEN_ID_MASK GENMASK(27, 18)
62 #define MSG_XTRACT_TOKEN(hdr) FIELD_GET(MSG_TOKEN_ID_MASK, (hdr))
63 #define MSG_TOKEN_MAX (MSG_XTRACT_TOKEN(MSG_TOKEN_ID_MASK) + 1)
66 * struct scmi_msg_hdr - Message(Tx/Rx) header
68 * @id: The identifier of the message being sent
69 * @protocol_id: The identifier of the protocol used to send @id message
70 * @seq: The token to identify the message. When a message returns, the
71 * platform returns the whole message header unmodified including the
73 * @status: Status of the transfer once it's complete
74 * @poll_completion: Indicate if the transfer needs to be polled for
75 * completion or interrupt mode is used
86 * pack_scmi_header() - packs and returns 32-bit header
88 * @hdr: pointer to header containing all the information on message id,
89 * protocol id and sequence id.
91 * Return: 32-bit packed message header to be sent to the platform.
93 static inline u32 pack_scmi_header(struct scmi_msg_hdr *hdr)
95 return FIELD_PREP(MSG_ID_MASK, hdr->id) |
96 FIELD_PREP(MSG_TOKEN_ID_MASK, hdr->seq) |
97 FIELD_PREP(MSG_PROTOCOL_ID_MASK, hdr->protocol_id);
101 * unpack_scmi_header() - unpacks and records message and protocol id
103 * @msg_hdr: 32-bit packed message header sent from the platform
104 * @hdr: pointer to header to fetch message and protocol id.
106 static inline void unpack_scmi_header(u32 msg_hdr, struct scmi_msg_hdr *hdr)
108 hdr->id = MSG_XTRACT_ID(msg_hdr);
109 hdr->protocol_id = MSG_XTRACT_PROT_ID(msg_hdr);
113 * struct scmi_msg - Message(Tx/Rx) structure
115 * @buf: Buffer pointer
116 * @len: Length of data in the Buffer
124 * struct scmi_xfer - Structure representing a message flow
126 * @transfer_id: Unique ID for debug & profiling purpose
127 * @hdr: Transmit message header
128 * @tx: Transmit message
129 * @rx: Receive message, the buffer should be pre-allocated to store
130 * message. If request-ACK protocol is used, we can reuse the same
131 * buffer for the rx path as we use for the tx path.
132 * @done: command message transmit completion event
133 * @async_done: pointer to delayed response message received event completion
137 struct scmi_msg_hdr hdr;
140 struct completion done;
141 struct completion *async_done;
144 void scmi_xfer_put(const struct scmi_handle *h, struct scmi_xfer *xfer);
145 int scmi_do_xfer(const struct scmi_handle *h, struct scmi_xfer *xfer);
146 int scmi_do_xfer_with_response(const struct scmi_handle *h,
147 struct scmi_xfer *xfer);
148 int scmi_xfer_get_init(const struct scmi_handle *h, u8 msg_id, u8 prot_id,
149 size_t tx_size, size_t rx_size, struct scmi_xfer **p);
150 int scmi_handle_put(const struct scmi_handle *handle);
151 struct scmi_handle *scmi_handle_get(struct device *dev);
152 void scmi_set_handle(struct scmi_device *scmi_dev);
153 int scmi_version_get(const struct scmi_handle *h, u8 protocol, u32 *version);
154 void scmi_setup_protocol_implemented(const struct scmi_handle *handle,
157 int scmi_base_protocol_init(struct scmi_handle *h);
159 int __init scmi_bus_init(void);
160 void __exit scmi_bus_exit(void);
162 #define DECLARE_SCMI_REGISTER_UNREGISTER(func) \
163 int __init scmi_##func##_register(void); \
164 void __exit scmi_##func##_unregister(void)
165 DECLARE_SCMI_REGISTER_UNREGISTER(clock);
166 DECLARE_SCMI_REGISTER_UNREGISTER(perf);
167 DECLARE_SCMI_REGISTER_UNREGISTER(power);
168 DECLARE_SCMI_REGISTER_UNREGISTER(reset);
169 DECLARE_SCMI_REGISTER_UNREGISTER(sensors);
170 DECLARE_SCMI_REGISTER_UNREGISTER(system);
172 #define DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(id, name) \
173 int __init scmi_##name##_register(void) \
175 return scmi_protocol_register((id), &scmi_##name##_protocol_init); \
178 void __exit scmi_##name##_unregister(void) \
180 scmi_protocol_unregister((id)); \
185 * struct scmi_chan_info - Structure representing a SCMI channel information
187 * @dev: Reference to device in the SCMI hierarchy corresponding to this
189 * @handle: Pointer to SCMI entity handle
190 * @transport_info: Transport layer related information
192 struct scmi_chan_info {
194 struct scmi_handle *handle;
195 void *transport_info;
199 * struct scmi_transport_ops - Structure representing a SCMI transport ops
201 * @chan_available: Callback to check if channel is available or not
202 * @chan_setup: Callback to allocate and setup a channel
203 * @chan_free: Callback to free a channel
204 * @send_message: Callback to send a message
205 * @mark_txdone: Callback to mark tx as done
206 * @fetch_response: Callback to fetch response
207 * @fetch_notification: Callback to fetch notification
208 * @clear_channel: Callback to clear a channel
209 * @poll_done: Callback to poll transfer status
211 struct scmi_transport_ops {
212 bool (*chan_available)(struct device *dev, int idx);
213 int (*chan_setup)(struct scmi_chan_info *cinfo, struct device *dev,
215 int (*chan_free)(int id, void *p, void *data);
216 int (*send_message)(struct scmi_chan_info *cinfo,
217 struct scmi_xfer *xfer);
218 void (*mark_txdone)(struct scmi_chan_info *cinfo, int ret);
219 void (*fetch_response)(struct scmi_chan_info *cinfo,
220 struct scmi_xfer *xfer);
221 void (*fetch_notification)(struct scmi_chan_info *cinfo,
222 size_t max_len, struct scmi_xfer *xfer);
223 void (*clear_channel)(struct scmi_chan_info *cinfo);
224 bool (*poll_done)(struct scmi_chan_info *cinfo, struct scmi_xfer *xfer);
228 * struct scmi_desc - Description of SoC integration
230 * @ops: Pointer to the transport specific ops structure
231 * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
232 * @max_msg: Maximum number of messages that can be pending
233 * simultaneously in the system
234 * @max_msg_size: Maximum size of data per message that can be handled.
237 const struct scmi_transport_ops *ops;
238 int max_rx_timeout_ms;
243 extern const struct scmi_desc scmi_mailbox_desc;
244 #ifdef CONFIG_HAVE_ARM_SMCCC
245 extern const struct scmi_desc scmi_smc_desc;
248 void scmi_rx_callback(struct scmi_chan_info *cinfo, u32 msg_hdr);
249 void scmi_free_channel(struct scmi_chan_info *cinfo, struct idr *idr, int id);
251 /* shmem related declarations */
252 struct scmi_shared_mem;
254 void shmem_tx_prepare(struct scmi_shared_mem __iomem *shmem,
255 struct scmi_xfer *xfer);
256 u32 shmem_read_header(struct scmi_shared_mem __iomem *shmem);
257 void shmem_fetch_response(struct scmi_shared_mem __iomem *shmem,
258 struct scmi_xfer *xfer);
259 void shmem_fetch_notification(struct scmi_shared_mem __iomem *shmem,
260 size_t max_len, struct scmi_xfer *xfer);
261 void shmem_clear_channel(struct scmi_shared_mem __iomem *shmem);
262 bool shmem_poll_done(struct scmi_shared_mem __iomem *shmem,
263 struct scmi_xfer *xfer);
265 #endif /* _SCMI_COMMON_H */