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 ARM Ltd. 8 */ 9 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> 17 18 #include <asm/unaligned.h> 19 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 25 #define MAX_OPPS 16 26 27 enum scmi_common_cmd { 28 PROTOCOL_VERSION = 0x0, 29 PROTOCOL_ATTRIBUTES = 0x1, 30 PROTOCOL_MESSAGE_ATTRIBUTES = 0x2, 31 }; 32 33 /** 34 * struct scmi_msg_resp_prot_version - Response for a message 35 * 36 * @minor_version: Minor version of the ABI that firmware supports 37 * @major_version: Major version of the ABI that firmware supports 38 * 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. 42 * 43 * Response to a generic message with message type SCMI_MSG_VERSION 44 */ 45 struct scmi_msg_resp_prot_version { 46 __le16 minor_version; 47 __le16 major_version; 48 }; 49 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) 62 63 /** 64 * struct scmi_msg_hdr - Message(Tx/Rx) header 65 * 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 70 * token 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 74 */ 75 struct scmi_msg_hdr { 76 u8 id; 77 u8 protocol_id; 78 u16 seq; 79 u32 status; 80 bool poll_completion; 81 }; 82 83 /** 84 * pack_scmi_header() - packs and returns 32-bit header 85 * 86 * @hdr: pointer to header containing all the information on message id, 87 * protocol id and sequence id. 88 * 89 * Return: 32-bit packed message header to be sent to the platform. 90 */ 91 static inline u32 pack_scmi_header(struct scmi_msg_hdr *hdr) 92 { 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); 96 } 97 98 /** 99 * unpack_scmi_header() - unpacks and records message and protocol id 100 * 101 * @msg_hdr: 32-bit packed message header sent from the platform 102 * @hdr: pointer to header to fetch message and protocol id. 103 */ 104 static inline void unpack_scmi_header(u32 msg_hdr, struct scmi_msg_hdr *hdr) 105 { 106 hdr->id = MSG_XTRACT_ID(msg_hdr); 107 hdr->protocol_id = MSG_XTRACT_PROT_ID(msg_hdr); 108 } 109 110 /** 111 * struct scmi_msg - Message(Tx/Rx) structure 112 * 113 * @buf: Buffer pointer 114 * @len: Length of data in the Buffer 115 */ 116 struct scmi_msg { 117 void *buf; 118 size_t len; 119 }; 120 121 /** 122 * struct scmi_xfer - Structure representing a message flow 123 * 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 132 */ 133 struct scmi_xfer { 134 int transfer_id; 135 struct scmi_msg_hdr hdr; 136 struct scmi_msg tx; 137 struct scmi_msg rx; 138 struct completion done; 139 struct completion *async_done; 140 }; 141 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, 153 u8 *prot_imp); 154 155 int scmi_base_protocol_init(struct scmi_handle *h); 156 157 /* SCMI Transport */ 158 /** 159 * struct scmi_chan_info - Structure representing a SCMI channel information 160 * 161 * @dev: Reference to device in the SCMI hierarchy corresponding to this 162 * channel 163 * @handle: Pointer to SCMI entity handle 164 * @transport_info: Transport layer related information 165 */ 166 struct scmi_chan_info { 167 struct device *dev; 168 struct scmi_handle *handle; 169 void *transport_info; 170 }; 171 172 /** 173 * struct scmi_transport_ops - Structure representing a SCMI transport ops 174 * 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 182 */ 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, 186 bool tx); 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); 194 }; 195 196 /** 197 * struct scmi_desc - Description of SoC integration 198 * 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. 204 */ 205 struct scmi_desc { 206 struct scmi_transport_ops *ops; 207 int max_rx_timeout_ms; 208 int max_msg; 209 int max_msg_size; 210 }; 211 212 extern const struct scmi_desc scmi_mailbox_desc; 213 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); 216 217 /* shmem related declarations */ 218 struct scmi_shared_mem; 219 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); 227