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 | */ |
Cristian Marussi | 1fc2dd1 | 2020-07-01 16:53:40 +0100 | [diff] [blame] | 9 | #ifndef _SCMI_COMMON_H |
| 10 | #define _SCMI_COMMON_H |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 11 | |
Sudeep Holla | 354b2e3 | 2018-05-09 17:52:06 +0100 | [diff] [blame] | 12 | #include <linux/bitfield.h> |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 13 | #include <linux/completion.h> |
Sudeep Holla | b6f20ff | 2017-06-06 11:16:15 +0100 | [diff] [blame] | 14 | #include <linux/device.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/kernel.h> |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 17 | #include <linux/scmi_protocol.h> |
| 18 | #include <linux/types.h> |
| 19 | |
Sudeep Holla | aa90ac4 | 2019-08-07 13:46:27 +0100 | [diff] [blame] | 20 | #include <asm/unaligned.h> |
| 21 | |
Sudeep Holla | 354b2e3 | 2018-05-09 17:52:06 +0100 | [diff] [blame] | 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))) |
Sudeep Holla | b6f20ff | 2017-06-06 11:16:15 +0100 | [diff] [blame] | 26 | #define MAX_PROTOCOLS_IMP 16 |
Sudeep Holla | a9e3fbf | 2017-06-06 11:22:51 +0100 | [diff] [blame] | 27 | #define MAX_OPPS 16 |
Sudeep Holla | b6f20ff | 2017-06-06 11:16:15 +0100 | [diff] [blame] | 28 | |
| 29 | enum scmi_common_cmd { |
| 30 | PROTOCOL_VERSION = 0x0, |
| 31 | PROTOCOL_ATTRIBUTES = 0x1, |
| 32 | PROTOCOL_MESSAGE_ATTRIBUTES = 0x2, |
| 33 | }; |
| 34 | |
| 35 | /** |
| 36 | * struct scmi_msg_resp_prot_version - Response for a message |
| 37 | * |
Sudeep Holla | b6f20ff | 2017-06-06 11:16:15 +0100 | [diff] [blame] | 38 | * @minor_version: Minor version of the ABI that firmware supports |
Viresh Kumar | 71af05a | 2020-01-31 10:58:11 +0530 | [diff] [blame] | 39 | * @major_version: Major version of the ABI that firmware supports |
Sudeep Holla | b6f20ff | 2017-06-06 11:16:15 +0100 | [diff] [blame] | 40 | * |
| 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. |
| 44 | * |
| 45 | * Response to a generic message with message type SCMI_MSG_VERSION |
| 46 | */ |
| 47 | struct scmi_msg_resp_prot_version { |
| 48 | __le16 minor_version; |
| 49 | __le16 major_version; |
| 50 | }; |
| 51 | |
Viresh Kumar | c4eb836 | 2020-01-31 10:58:12 +0530 | [diff] [blame] | 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) |
| 64 | |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 65 | /** |
| 66 | * struct scmi_msg_hdr - Message(Tx/Rx) header |
| 67 | * |
Sudeep Holla | 5b65af8 | 2019-07-08 09:40:40 +0100 | [diff] [blame] | 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 |
| 72 | * token |
Sudeep Holla | 1baf47c | 2018-05-09 17:52:06 +0100 | [diff] [blame] | 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 |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 76 | */ |
| 77 | struct scmi_msg_hdr { |
| 78 | u8 id; |
| 79 | u8 protocol_id; |
| 80 | u16 seq; |
| 81 | u32 status; |
| 82 | bool poll_completion; |
| 83 | }; |
| 84 | |
| 85 | /** |
Viresh Kumar | c4eb836 | 2020-01-31 10:58:12 +0530 | [diff] [blame] | 86 | * pack_scmi_header() - packs and returns 32-bit header |
| 87 | * |
| 88 | * @hdr: pointer to header containing all the information on message id, |
| 89 | * protocol id and sequence id. |
| 90 | * |
| 91 | * Return: 32-bit packed message header to be sent to the platform. |
| 92 | */ |
| 93 | static inline u32 pack_scmi_header(struct scmi_msg_hdr *hdr) |
| 94 | { |
| 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); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * unpack_scmi_header() - unpacks and records message and protocol id |
| 102 | * |
| 103 | * @msg_hdr: 32-bit packed message header sent from the platform |
| 104 | * @hdr: pointer to header to fetch message and protocol id. |
| 105 | */ |
| 106 | static inline void unpack_scmi_header(u32 msg_hdr, struct scmi_msg_hdr *hdr) |
| 107 | { |
| 108 | hdr->id = MSG_XTRACT_ID(msg_hdr); |
| 109 | hdr->protocol_id = MSG_XTRACT_PROT_ID(msg_hdr); |
| 110 | } |
| 111 | |
| 112 | /** |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 113 | * struct scmi_msg - Message(Tx/Rx) structure |
| 114 | * |
| 115 | * @buf: Buffer pointer |
| 116 | * @len: Length of data in the Buffer |
| 117 | */ |
| 118 | struct scmi_msg { |
| 119 | void *buf; |
| 120 | size_t len; |
| 121 | }; |
| 122 | |
| 123 | /** |
| 124 | * struct scmi_xfer - Structure representing a message flow |
| 125 | * |
Lukasz Luba | 729d353 | 2019-12-17 13:43:45 +0000 | [diff] [blame] | 126 | * @transfer_id: Unique ID for debug & profiling purpose |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 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. |
Sudeep Holla | 58ecdf0 | 2019-07-08 09:40:54 +0100 | [diff] [blame] | 132 | * @done: command message transmit completion event |
Viresh Kumar | 71af05a | 2020-01-31 10:58:11 +0530 | [diff] [blame] | 133 | * @async_done: pointer to delayed response message received event completion |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 134 | */ |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 135 | struct scmi_xfer { |
Lukasz Luba | 729d353 | 2019-12-17 13:43:45 +0000 | [diff] [blame] | 136 | int transfer_id; |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 137 | struct scmi_msg_hdr hdr; |
| 138 | struct scmi_msg tx; |
| 139 | struct scmi_msg rx; |
| 140 | struct completion done; |
Sudeep Holla | 58ecdf0 | 2019-07-08 09:40:54 +0100 | [diff] [blame] | 141 | struct completion *async_done; |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 142 | }; |
| 143 | |
Sudeep Holla | 14e297b | 2018-05-09 17:52:06 +0100 | [diff] [blame] | 144 | 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] | 145 | 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] | 146 | int scmi_do_xfer_with_response(const struct scmi_handle *h, |
| 147 | struct scmi_xfer *xfer); |
Sudeep Holla | 14e297b | 2018-05-09 17:52:06 +0100 | [diff] [blame] | 148 | 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] | 149 | size_t tx_size, size_t rx_size, struct scmi_xfer **p); |
Sudeep Holla | 9724722 | 2020-10-12 14:26:24 +0100 | [diff] [blame] | 150 | void scmi_reset_rx_to_maxsz(const struct scmi_handle *handle, |
| 151 | struct scmi_xfer *xfer); |
Sudeep Holla | aa4f886 | 2017-03-28 11:36:07 +0100 | [diff] [blame] | 152 | int scmi_handle_put(const struct scmi_handle *handle); |
| 153 | struct scmi_handle *scmi_handle_get(struct device *dev); |
Sudeep Holla | 933c504 | 2017-10-30 18:33:30 +0000 | [diff] [blame] | 154 | void scmi_set_handle(struct scmi_device *scmi_dev); |
Sudeep Holla | b6f20ff | 2017-06-06 11:16:15 +0100 | [diff] [blame] | 155 | int scmi_version_get(const struct scmi_handle *h, u8 protocol, u32 *version); |
| 156 | void scmi_setup_protocol_implemented(const struct scmi_handle *handle, |
| 157 | u8 *prot_imp); |
| 158 | |
| 159 | int scmi_base_protocol_init(struct scmi_handle *h); |
Viresh Kumar | 5c8a47a | 2020-01-31 10:58:13 +0530 | [diff] [blame] | 160 | |
Sudeep Holla | 5a2f0a0 | 2020-09-07 12:00:04 +0100 | [diff] [blame] | 161 | int __init scmi_bus_init(void); |
| 162 | void __exit scmi_bus_exit(void); |
| 163 | |
Sudeep Holla | 1eaf18e | 2020-09-07 12:06:01 +0100 | [diff] [blame] | 164 | #define DECLARE_SCMI_REGISTER_UNREGISTER(func) \ |
| 165 | int __init scmi_##func##_register(void); \ |
| 166 | void __exit scmi_##func##_unregister(void) |
| 167 | DECLARE_SCMI_REGISTER_UNREGISTER(clock); |
| 168 | DECLARE_SCMI_REGISTER_UNREGISTER(perf); |
| 169 | DECLARE_SCMI_REGISTER_UNREGISTER(power); |
| 170 | DECLARE_SCMI_REGISTER_UNREGISTER(reset); |
| 171 | DECLARE_SCMI_REGISTER_UNREGISTER(sensors); |
| 172 | DECLARE_SCMI_REGISTER_UNREGISTER(system); |
| 173 | |
| 174 | #define DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(id, name) \ |
| 175 | int __init scmi_##name##_register(void) \ |
| 176 | { \ |
| 177 | return scmi_protocol_register((id), &scmi_##name##_protocol_init); \ |
| 178 | } \ |
| 179 | \ |
| 180 | void __exit scmi_##name##_unregister(void) \ |
| 181 | { \ |
| 182 | scmi_protocol_unregister((id)); \ |
| 183 | } |
| 184 | |
Viresh Kumar | 5c8a47a | 2020-01-31 10:58:13 +0530 | [diff] [blame] | 185 | /* SCMI Transport */ |
| 186 | /** |
| 187 | * struct scmi_chan_info - Structure representing a SCMI channel information |
| 188 | * |
| 189 | * @dev: Reference to device in the SCMI hierarchy corresponding to this |
| 190 | * channel |
| 191 | * @handle: Pointer to SCMI entity handle |
| 192 | * @transport_info: Transport layer related information |
| 193 | */ |
| 194 | struct scmi_chan_info { |
| 195 | struct device *dev; |
| 196 | struct scmi_handle *handle; |
| 197 | void *transport_info; |
| 198 | }; |
| 199 | |
| 200 | /** |
| 201 | * struct scmi_transport_ops - Structure representing a SCMI transport ops |
| 202 | * |
| 203 | * @chan_available: Callback to check if channel is available or not |
| 204 | * @chan_setup: Callback to allocate and setup a channel |
| 205 | * @chan_free: Callback to free a channel |
| 206 | * @send_message: Callback to send a message |
| 207 | * @mark_txdone: Callback to mark tx as done |
| 208 | * @fetch_response: Callback to fetch response |
Cristian Marussi | d5141f3 | 2020-03-27 14:34:28 +0000 | [diff] [blame] | 209 | * @fetch_notification: Callback to fetch notification |
Cristian Marussi | 87dff4e | 2020-04-20 16:23:12 +0100 | [diff] [blame] | 210 | * @clear_channel: Callback to clear a channel |
Viresh Kumar | 5c8a47a | 2020-01-31 10:58:13 +0530 | [diff] [blame] | 211 | * @poll_done: Callback to poll transfer status |
| 212 | */ |
| 213 | struct scmi_transport_ops { |
| 214 | bool (*chan_available)(struct device *dev, int idx); |
| 215 | int (*chan_setup)(struct scmi_chan_info *cinfo, struct device *dev, |
| 216 | bool tx); |
| 217 | int (*chan_free)(int id, void *p, void *data); |
| 218 | int (*send_message)(struct scmi_chan_info *cinfo, |
| 219 | struct scmi_xfer *xfer); |
| 220 | void (*mark_txdone)(struct scmi_chan_info *cinfo, int ret); |
| 221 | void (*fetch_response)(struct scmi_chan_info *cinfo, |
| 222 | struct scmi_xfer *xfer); |
Cristian Marussi | d5141f3 | 2020-03-27 14:34:28 +0000 | [diff] [blame] | 223 | void (*fetch_notification)(struct scmi_chan_info *cinfo, |
| 224 | size_t max_len, struct scmi_xfer *xfer); |
Cristian Marussi | 87dff4e | 2020-04-20 16:23:12 +0100 | [diff] [blame] | 225 | void (*clear_channel)(struct scmi_chan_info *cinfo); |
Viresh Kumar | 5c8a47a | 2020-01-31 10:58:13 +0530 | [diff] [blame] | 226 | bool (*poll_done)(struct scmi_chan_info *cinfo, struct scmi_xfer *xfer); |
| 227 | }; |
| 228 | |
| 229 | /** |
| 230 | * struct scmi_desc - Description of SoC integration |
| 231 | * |
| 232 | * @ops: Pointer to the transport specific ops structure |
| 233 | * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds) |
| 234 | * @max_msg: Maximum number of messages that can be pending |
| 235 | * simultaneously in the system |
| 236 | * @max_msg_size: Maximum size of data per message that can be handled. |
| 237 | */ |
| 238 | struct scmi_desc { |
Rikard Falkeborn | 3de7b83 | 2020-09-07 01:04:52 +0200 | [diff] [blame] | 239 | const struct scmi_transport_ops *ops; |
Viresh Kumar | 5c8a47a | 2020-01-31 10:58:13 +0530 | [diff] [blame] | 240 | int max_rx_timeout_ms; |
| 241 | int max_msg; |
| 242 | int max_msg_size; |
| 243 | }; |
| 244 | |
| 245 | extern const struct scmi_desc scmi_mailbox_desc; |
Etienne Carriere | b4009ea | 2021-05-21 15:40:51 +0200 | [diff] [blame] | 246 | #ifdef CONFIG_HAVE_ARM_SMCCC_DISCOVERY |
Peng Fan | 1dc6558 | 2020-03-08 21:24:39 +0800 | [diff] [blame] | 247 | extern const struct scmi_desc scmi_smc_desc; |
| 248 | #endif |
Viresh Kumar | 5c8a47a | 2020-01-31 10:58:13 +0530 | [diff] [blame] | 249 | |
| 250 | void scmi_rx_callback(struct scmi_chan_info *cinfo, u32 msg_hdr); |
| 251 | void scmi_free_channel(struct scmi_chan_info *cinfo, struct idr *idr, int id); |
| 252 | |
| 253 | /* shmem related declarations */ |
| 254 | struct scmi_shared_mem; |
| 255 | |
| 256 | void shmem_tx_prepare(struct scmi_shared_mem __iomem *shmem, |
| 257 | struct scmi_xfer *xfer); |
| 258 | u32 shmem_read_header(struct scmi_shared_mem __iomem *shmem); |
| 259 | void shmem_fetch_response(struct scmi_shared_mem __iomem *shmem, |
| 260 | struct scmi_xfer *xfer); |
Cristian Marussi | d5141f3 | 2020-03-27 14:34:28 +0000 | [diff] [blame] | 261 | void shmem_fetch_notification(struct scmi_shared_mem __iomem *shmem, |
| 262 | size_t max_len, struct scmi_xfer *xfer); |
Cristian Marussi | 87dff4e | 2020-04-20 16:23:12 +0100 | [diff] [blame] | 263 | void shmem_clear_channel(struct scmi_shared_mem __iomem *shmem); |
Viresh Kumar | 5c8a47a | 2020-01-31 10:58:13 +0530 | [diff] [blame] | 264 | bool shmem_poll_done(struct scmi_shared_mem __iomem *shmem, |
| 265 | struct scmi_xfer *xfer); |
Cristian Marussi | 1fc2dd1 | 2020-07-01 16:53:40 +0100 | [diff] [blame] | 266 | |
| 267 | #endif /* _SCMI_COMMON_H */ |