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 #include <linux/bitfield.h>
11 #include <linux/completion.h>
12 #include <linux/device.h>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/scmi_protocol.h>
16 #include <linux/types.h>
18 #include <asm/unaligned.h>
20 #define PROTOCOL_REV_MINOR_MASK GENMASK(15, 0)
21 #define PROTOCOL_REV_MAJOR_MASK GENMASK(31, 16)
22 #define PROTOCOL_REV_MAJOR(x) (u16)(FIELD_GET(PROTOCOL_REV_MAJOR_MASK, (x)))
23 #define PROTOCOL_REV_MINOR(x) (u16)(FIELD_GET(PROTOCOL_REV_MINOR_MASK, (x)))
24 #define MAX_PROTOCOLS_IMP 16
27 enum scmi_common_cmd
{
28 PROTOCOL_VERSION
= 0x0,
29 PROTOCOL_ATTRIBUTES
= 0x1,
30 PROTOCOL_MESSAGE_ATTRIBUTES
= 0x2,
34 * struct scmi_msg_resp_prot_version - Response for a message
36 * @minor_version: Minor version of the ABI that firmware supports
37 * @major_version: Major version of the ABI that firmware supports
39 * In general, ABI version changes follow the rule that minor version increments
40 * are backward compatible. Major revision changes in ABI may not be
41 * backward compatible.
43 * Response to a generic message with message type SCMI_MSG_VERSION
45 struct scmi_msg_resp_prot_version
{
50 #define MSG_ID_MASK GENMASK(7, 0)
51 #define MSG_XTRACT_ID(hdr) FIELD_GET(MSG_ID_MASK, (hdr))
52 #define MSG_TYPE_MASK GENMASK(9, 8)
53 #define MSG_XTRACT_TYPE(hdr) FIELD_GET(MSG_TYPE_MASK, (hdr))
54 #define MSG_TYPE_COMMAND 0
55 #define MSG_TYPE_DELAYED_RESP 2
56 #define MSG_TYPE_NOTIFICATION 3
57 #define MSG_PROTOCOL_ID_MASK GENMASK(17, 10)
58 #define MSG_XTRACT_PROT_ID(hdr) FIELD_GET(MSG_PROTOCOL_ID_MASK, (hdr))
59 #define MSG_TOKEN_ID_MASK GENMASK(27, 18)
60 #define MSG_XTRACT_TOKEN(hdr) FIELD_GET(MSG_TOKEN_ID_MASK, (hdr))
61 #define MSG_TOKEN_MAX (MSG_XTRACT_TOKEN(MSG_TOKEN_ID_MASK) + 1)
64 * struct scmi_msg_hdr - Message(Tx/Rx) header
66 * @id: The identifier of the message being sent
67 * @protocol_id: The identifier of the protocol used to send @id message
68 * @seq: The token to identify the message. When a message returns, the
69 * platform returns the whole message header unmodified including the
71 * @status: Status of the transfer once it's complete
72 * @poll_completion: Indicate if the transfer needs to be polled for
73 * completion or interrupt mode is used
84 * pack_scmi_header() - packs and returns 32-bit header
86 * @hdr: pointer to header containing all the information on message id,
87 * protocol id and sequence id.
89 * Return: 32-bit packed message header to be sent to the platform.
91 static inline u32
pack_scmi_header(struct scmi_msg_hdr
*hdr
)
93 return FIELD_PREP(MSG_ID_MASK
, hdr
->id
) |
94 FIELD_PREP(MSG_TOKEN_ID_MASK
, hdr
->seq
) |
95 FIELD_PREP(MSG_PROTOCOL_ID_MASK
, hdr
->protocol_id
);
99 * unpack_scmi_header() - unpacks and records message and protocol id
101 * @msg_hdr: 32-bit packed message header sent from the platform
102 * @hdr: pointer to header to fetch message and protocol id.
104 static inline void unpack_scmi_header(u32 msg_hdr
, struct scmi_msg_hdr
*hdr
)
106 hdr
->id
= MSG_XTRACT_ID(msg_hdr
);
107 hdr
->protocol_id
= MSG_XTRACT_PROT_ID(msg_hdr
);
111 * struct scmi_msg - Message(Tx/Rx) structure
113 * @buf: Buffer pointer
114 * @len: Length of data in the Buffer
122 * struct scmi_xfer - Structure representing a message flow
124 * @transfer_id: Unique ID for debug & profiling purpose
125 * @hdr: Transmit message header
126 * @tx: Transmit message
127 * @rx: Receive message, the buffer should be pre-allocated to store
128 * message. If request-ACK protocol is used, we can reuse the same
129 * buffer for the rx path as we use for the tx path.
130 * @done: command message transmit completion event
131 * @async_done: pointer to delayed response message received event completion
135 struct scmi_msg_hdr hdr
;
138 struct completion done
;
139 struct completion
*async_done
;
142 void scmi_xfer_put(const struct scmi_handle
*h
, struct scmi_xfer
*xfer
);
143 int scmi_do_xfer(const struct scmi_handle
*h
, struct scmi_xfer
*xfer
);
144 int scmi_do_xfer_with_response(const struct scmi_handle
*h
,
145 struct scmi_xfer
*xfer
);
146 int scmi_xfer_get_init(const struct scmi_handle
*h
, u8 msg_id
, u8 prot_id
,
147 size_t tx_size
, size_t rx_size
, struct scmi_xfer
**p
);
148 int scmi_handle_put(const struct scmi_handle
*handle
);
149 struct scmi_handle
*scmi_handle_get(struct device
*dev
);
150 void scmi_set_handle(struct scmi_device
*scmi_dev
);
151 int scmi_version_get(const struct scmi_handle
*h
, u8 protocol
, u32
*version
);
152 void scmi_setup_protocol_implemented(const struct scmi_handle
*handle
,
155 int scmi_base_protocol_init(struct scmi_handle
*h
);
159 * struct scmi_chan_info - Structure representing a SCMI channel information
161 * @dev: Reference to device in the SCMI hierarchy corresponding to this
163 * @handle: Pointer to SCMI entity handle
164 * @transport_info: Transport layer related information
166 struct scmi_chan_info
{
168 struct scmi_handle
*handle
;
169 void *transport_info
;
173 * struct scmi_transport_ops - Structure representing a SCMI transport ops
175 * @chan_available: Callback to check if channel is available or not
176 * @chan_setup: Callback to allocate and setup a channel
177 * @chan_free: Callback to free a channel
178 * @send_message: Callback to send a message
179 * @mark_txdone: Callback to mark tx as done
180 * @fetch_response: Callback to fetch response
181 * @poll_done: Callback to poll transfer status
183 struct scmi_transport_ops
{
184 bool (*chan_available
)(struct device
*dev
, int idx
);
185 int (*chan_setup
)(struct scmi_chan_info
*cinfo
, struct device
*dev
,
187 int (*chan_free
)(int id
, void *p
, void *data
);
188 int (*send_message
)(struct scmi_chan_info
*cinfo
,
189 struct scmi_xfer
*xfer
);
190 void (*mark_txdone
)(struct scmi_chan_info
*cinfo
, int ret
);
191 void (*fetch_response
)(struct scmi_chan_info
*cinfo
,
192 struct scmi_xfer
*xfer
);
193 bool (*poll_done
)(struct scmi_chan_info
*cinfo
, struct scmi_xfer
*xfer
);
197 * struct scmi_desc - Description of SoC integration
199 * @ops: Pointer to the transport specific ops structure
200 * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
201 * @max_msg: Maximum number of messages that can be pending
202 * simultaneously in the system
203 * @max_msg_size: Maximum size of data per message that can be handled.
206 struct scmi_transport_ops
*ops
;
207 int max_rx_timeout_ms
;
212 extern const struct scmi_desc scmi_mailbox_desc
;
214 void scmi_rx_callback(struct scmi_chan_info
*cinfo
, u32 msg_hdr
);
215 void scmi_free_channel(struct scmi_chan_info
*cinfo
, struct idr
*idr
, int id
);
217 /* shmem related declarations */
218 struct scmi_shared_mem
;
220 void shmem_tx_prepare(struct scmi_shared_mem __iomem
*shmem
,
221 struct scmi_xfer
*xfer
);
222 u32
shmem_read_header(struct scmi_shared_mem __iomem
*shmem
);
223 void shmem_fetch_response(struct scmi_shared_mem __iomem
*shmem
,
224 struct scmi_xfer
*xfer
);
225 bool shmem_poll_done(struct scmi_shared_mem __iomem
*shmem
,
226 struct scmi_xfer
*xfer
);