Nishad Kamdar | d252768 | 2019-06-13 19:30:03 +0530 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 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 | |
Sudeep Holla | 354b2e3 | 2018-05-09 17:52:06 +0100 | [diff] [blame] | 10 | #include <linux/bitfield.h> |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 11 | #include <linux/completion.h> |
Sudeep Holla | b6f20ff | 2017-06-06 11:16:15 +0100 | [diff] [blame] | 12 | #include <linux/device.h> |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/kernel.h> |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 15 | #include <linux/scmi_protocol.h> |
| 16 | #include <linux/types.h> |
| 17 | |
Sudeep Holla | aa90ac4 | 2019-08-07 13:46:27 +0100 | [diff] [blame] | 18 | #include <asm/unaligned.h> |
| 19 | |
Sudeep Holla | 354b2e3 | 2018-05-09 17:52:06 +0100 | [diff] [blame] | 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))) |
Sudeep Holla | b6f20ff | 2017-06-06 11:16:15 +0100 | [diff] [blame] | 24 | #define MAX_PROTOCOLS_IMP 16 |
Sudeep Holla | a9e3fbf | 2017-06-06 11:22:51 +0100 | [diff] [blame] | 25 | #define MAX_OPPS 16 |
Sudeep Holla | b6f20ff | 2017-06-06 11:16:15 +0100 | [diff] [blame] | 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 | * |
Sudeep Holla | b6f20ff | 2017-06-06 11:16:15 +0100 | [diff] [blame] | 36 | * @minor_version: Minor version of the ABI that firmware supports |
Viresh Kumar | 71af05a | 2020-01-31 10:58:11 +0530 | [diff] [blame] | 37 | * @major_version: Major version of the ABI that firmware supports |
Sudeep Holla | b6f20ff | 2017-06-06 11:16:15 +0100 | [diff] [blame] | 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 | |
Viresh Kumar | c4eb836 | 2020-01-31 10:58:12 +0530 | [diff] [blame] | 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 | |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 63 | /** |
| 64 | * struct scmi_msg_hdr - Message(Tx/Rx) header |
| 65 | * |
Sudeep Holla | 5b65af8 | 2019-07-08 09:40:40 +0100 | [diff] [blame] | 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 |
Sudeep Holla | 1baf47c | 2018-05-09 17:52:06 +0100 | [diff] [blame] | 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 |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 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 | /** |
Viresh Kumar | c4eb836 | 2020-01-31 10:58:12 +0530 | [diff] [blame] | 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 | /** |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 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 | * |
Lukasz Luba | 729d353 | 2019-12-17 13:43:45 +0000 | [diff] [blame] | 124 | * @transfer_id: Unique ID for debug & profiling purpose |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 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. |
Sudeep Holla | 58ecdf0 | 2019-07-08 09:40:54 +0100 | [diff] [blame] | 130 | * @done: command message transmit completion event |
Viresh Kumar | 71af05a | 2020-01-31 10:58:11 +0530 | [diff] [blame] | 131 | * @async_done: pointer to delayed response message received event completion |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 132 | */ |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 133 | struct scmi_xfer { |
Lukasz Luba | 729d353 | 2019-12-17 13:43:45 +0000 | [diff] [blame] | 134 | int transfer_id; |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 135 | struct scmi_msg_hdr hdr; |
| 136 | struct scmi_msg tx; |
| 137 | struct scmi_msg rx; |
| 138 | struct completion done; |
Sudeep Holla | 58ecdf0 | 2019-07-08 09:40:54 +0100 | [diff] [blame] | 139 | struct completion *async_done; |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 140 | }; |
| 141 | |
Sudeep Holla | 14e297b | 2018-05-09 17:52:06 +0100 | [diff] [blame] | 142 | void scmi_xfer_put(const struct scmi_handle *h, struct scmi_xfer *xfer); |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 143 | int scmi_do_xfer(const struct scmi_handle *h, struct scmi_xfer *xfer); |
Sudeep Holla | 58ecdf0 | 2019-07-08 09:40:54 +0100 | [diff] [blame] | 144 | int scmi_do_xfer_with_response(const struct scmi_handle *h, |
| 145 | struct scmi_xfer *xfer); |
Sudeep Holla | 14e297b | 2018-05-09 17:52:06 +0100 | [diff] [blame] | 146 | int scmi_xfer_get_init(const struct scmi_handle *h, u8 msg_id, u8 prot_id, |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 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); |
Sudeep Holla | 933c504 | 2017-10-30 18:33:30 +0000 | [diff] [blame] | 150 | void scmi_set_handle(struct scmi_device *scmi_dev); |
Sudeep Holla | b6f20ff | 2017-06-06 11:16:15 +0100 | [diff] [blame] | 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); |
Viresh Kumar | 5c8a47a | 2020-01-31 10:58:13 +0530 | [diff] [blame] | 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 |
Cristian Marussi | d5141f3 | 2020-03-27 14:34:28 +0000 | [diff] [blame] | 181 | * @fetch_notification: Callback to fetch notification |
Cristian Marussi | 87dff4e | 2020-04-20 16:23:12 +0100 | [diff] [blame^] | 182 | * @clear_channel: Callback to clear a channel |
Viresh Kumar | 5c8a47a | 2020-01-31 10:58:13 +0530 | [diff] [blame] | 183 | * @poll_done: Callback to poll transfer status |
| 184 | */ |
| 185 | struct scmi_transport_ops { |
| 186 | bool (*chan_available)(struct device *dev, int idx); |
| 187 | int (*chan_setup)(struct scmi_chan_info *cinfo, struct device *dev, |
| 188 | bool tx); |
| 189 | int (*chan_free)(int id, void *p, void *data); |
| 190 | int (*send_message)(struct scmi_chan_info *cinfo, |
| 191 | struct scmi_xfer *xfer); |
| 192 | void (*mark_txdone)(struct scmi_chan_info *cinfo, int ret); |
| 193 | void (*fetch_response)(struct scmi_chan_info *cinfo, |
| 194 | struct scmi_xfer *xfer); |
Cristian Marussi | d5141f3 | 2020-03-27 14:34:28 +0000 | [diff] [blame] | 195 | void (*fetch_notification)(struct scmi_chan_info *cinfo, |
| 196 | size_t max_len, struct scmi_xfer *xfer); |
Cristian Marussi | 87dff4e | 2020-04-20 16:23:12 +0100 | [diff] [blame^] | 197 | void (*clear_channel)(struct scmi_chan_info *cinfo); |
Viresh Kumar | 5c8a47a | 2020-01-31 10:58:13 +0530 | [diff] [blame] | 198 | bool (*poll_done)(struct scmi_chan_info *cinfo, struct scmi_xfer *xfer); |
| 199 | }; |
| 200 | |
| 201 | /** |
| 202 | * struct scmi_desc - Description of SoC integration |
| 203 | * |
| 204 | * @ops: Pointer to the transport specific ops structure |
| 205 | * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds) |
| 206 | * @max_msg: Maximum number of messages that can be pending |
| 207 | * simultaneously in the system |
| 208 | * @max_msg_size: Maximum size of data per message that can be handled. |
| 209 | */ |
| 210 | struct scmi_desc { |
| 211 | struct scmi_transport_ops *ops; |
| 212 | int max_rx_timeout_ms; |
| 213 | int max_msg; |
| 214 | int max_msg_size; |
| 215 | }; |
| 216 | |
| 217 | extern const struct scmi_desc scmi_mailbox_desc; |
Peng Fan | 1dc6558 | 2020-03-08 21:24:39 +0800 | [diff] [blame] | 218 | #ifdef CONFIG_HAVE_ARM_SMCCC |
| 219 | extern const struct scmi_desc scmi_smc_desc; |
| 220 | #endif |
Viresh Kumar | 5c8a47a | 2020-01-31 10:58:13 +0530 | [diff] [blame] | 221 | |
| 222 | void scmi_rx_callback(struct scmi_chan_info *cinfo, u32 msg_hdr); |
| 223 | void scmi_free_channel(struct scmi_chan_info *cinfo, struct idr *idr, int id); |
| 224 | |
| 225 | /* shmem related declarations */ |
| 226 | struct scmi_shared_mem; |
| 227 | |
| 228 | void shmem_tx_prepare(struct scmi_shared_mem __iomem *shmem, |
| 229 | struct scmi_xfer *xfer); |
| 230 | u32 shmem_read_header(struct scmi_shared_mem __iomem *shmem); |
| 231 | void shmem_fetch_response(struct scmi_shared_mem __iomem *shmem, |
| 232 | struct scmi_xfer *xfer); |
Cristian Marussi | d5141f3 | 2020-03-27 14:34:28 +0000 | [diff] [blame] | 233 | void shmem_fetch_notification(struct scmi_shared_mem __iomem *shmem, |
| 234 | size_t max_len, struct scmi_xfer *xfer); |
Cristian Marussi | 87dff4e | 2020-04-20 16:23:12 +0100 | [diff] [blame^] | 235 | void shmem_clear_channel(struct scmi_shared_mem __iomem *shmem); |
Viresh Kumar | 5c8a47a | 2020-01-31 10:58:13 +0530 | [diff] [blame] | 236 | bool shmem_poll_done(struct scmi_shared_mem __iomem *shmem, |
| 237 | struct scmi_xfer *xfer); |