1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * System Control and Management Interface (SCMI) Message Protocol 4 * protocols common header file containing some definitions, structures 5 * and function prototypes used in all the different SCMI protocols. 6 * 7 * Copyright (C) 2022 ARM Ltd. 8 */ 9 #ifndef _SCMI_PROTOCOLS_H 10 #define _SCMI_PROTOCOLS_H 11 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/hashtable.h> 18 #include <linux/list.h> 19 #include <linux/module.h> 20 #include <linux/refcount.h> 21 #include <linux/scmi_protocol.h> 22 #include <linux/spinlock.h> 23 #include <linux/types.h> 24 25 #include <linux/unaligned.h> 26 27 #define PROTOCOL_REV_MINOR_MASK GENMASK(15, 0) 28 #define PROTOCOL_REV_MAJOR_MASK GENMASK(31, 16) 29 #define PROTOCOL_REV_MAJOR(x) ((u16)(FIELD_GET(PROTOCOL_REV_MAJOR_MASK, (x)))) 30 #define PROTOCOL_REV_MINOR(x) ((u16)(FIELD_GET(PROTOCOL_REV_MINOR_MASK, (x)))) 31 32 #define SCMI_PROTOCOL_VENDOR_BASE 0x80 33 34 #define MSG_SUPPORTS_FASTCHANNEL(x) ((x) & BIT(0)) 35 36 enum scmi_common_cmd { 37 PROTOCOL_VERSION = 0x0, 38 PROTOCOL_ATTRIBUTES = 0x1, 39 PROTOCOL_MESSAGE_ATTRIBUTES = 0x2, 40 NEGOTIATE_PROTOCOL_VERSION = 0x10, 41 }; 42 43 /** 44 * struct scmi_msg_resp_prot_version - Response for a message 45 * 46 * @minor_version: Minor version of the ABI that firmware supports 47 * @major_version: Major version of the ABI that firmware supports 48 * 49 * In general, ABI version changes follow the rule that minor version increments 50 * are backward compatible. Major revision changes in ABI may not be 51 * backward compatible. 52 * 53 * Response to a generic message with message type SCMI_MSG_VERSION 54 */ 55 struct scmi_msg_resp_prot_version { 56 __le16 minor_version; 57 __le16 major_version; 58 }; 59 60 /** 61 * struct scmi_msg - Message(Tx/Rx) structure 62 * 63 * @buf: Buffer pointer 64 * @len: Length of data in the Buffer 65 */ 66 struct scmi_msg { 67 void *buf; 68 size_t len; 69 }; 70 71 /** 72 * struct scmi_msg_hdr - Message(Tx/Rx) header 73 * 74 * @id: The identifier of the message being sent 75 * @protocol_id: The identifier of the protocol used to send @id message 76 * @type: The SCMI type for this message 77 * @seq: The token to identify the message. When a message returns, the 78 * platform returns the whole message header unmodified including the 79 * token 80 * @status: Status of the transfer once it's complete 81 * @poll_completion: Indicate if the transfer needs to be polled for 82 * completion or interrupt mode is used 83 */ 84 struct scmi_msg_hdr { 85 u8 id; 86 u8 protocol_id; 87 u8 type; 88 u16 seq; 89 u32 status; 90 bool poll_completion; 91 }; 92 93 /** 94 * struct scmi_xfer - Structure representing a message flow 95 * 96 * @transfer_id: Unique ID for debug & profiling purpose 97 * @hdr: Transmit message header 98 * @tx: Transmit message 99 * @rx: Receive message, the buffer should be pre-allocated to store 100 * message. If request-ACK protocol is used, we can reuse the same 101 * buffer for the rx path as we use for the tx path. 102 * @done: command message transmit completion event 103 * @async_done: pointer to delayed response message received event completion 104 * @pending: True for xfers added to @pending_xfers hashtable 105 * @node: An hlist_node reference used to store this xfer, alternatively, on 106 * the free list @free_xfers or in the @pending_xfers hashtable 107 * @users: A refcount to track the active users for this xfer. 108 * This is meant to protect against the possibility that, when a command 109 * transaction times out concurrently with the reception of a valid 110 * response message, the xfer could be finally put on the TX path, and 111 * so vanish, while on the RX path scmi_rx_callback() is still 112 * processing it: in such a case this refcounting will ensure that, even 113 * though the timed-out transaction will anyway cause the command 114 * request to be reported as failed by time-out, the underlying xfer 115 * cannot be discarded and possibly reused until the last one user on 116 * the RX path has released it. 117 * @busy: An atomic flag to ensure exclusive write access to this xfer 118 * @state: The current state of this transfer, with states transitions deemed 119 * valid being: 120 * - SCMI_XFER_SENT_OK -> SCMI_XFER_RESP_OK [ -> SCMI_XFER_DRESP_OK ] 121 * - SCMI_XFER_SENT_OK -> SCMI_XFER_DRESP_OK 122 * (Missing synchronous response is assumed OK and ignored) 123 * @flags: Optional flags associated to this xfer. 124 * @lock: A spinlock to protect state and busy fields. 125 * @priv: A pointer for transport private usage. 126 */ 127 struct scmi_xfer { 128 int transfer_id; 129 struct scmi_msg_hdr hdr; 130 struct scmi_msg tx; 131 struct scmi_msg rx; 132 struct completion done; 133 struct completion *async_done; 134 bool pending; 135 struct hlist_node node; 136 refcount_t users; 137 #define SCMI_XFER_FREE 0 138 #define SCMI_XFER_BUSY 1 139 atomic_t busy; 140 #define SCMI_XFER_SENT_OK 0 141 #define SCMI_XFER_RESP_OK 1 142 #define SCMI_XFER_DRESP_OK 2 143 int state; 144 #define SCMI_XFER_FLAG_IS_RAW BIT(0) 145 #define SCMI_XFER_IS_RAW(x) ((x)->flags & SCMI_XFER_FLAG_IS_RAW) 146 #define SCMI_XFER_FLAG_CHAN_SET BIT(1) 147 #define SCMI_XFER_IS_CHAN_SET(x) \ 148 ((x)->flags & SCMI_XFER_FLAG_CHAN_SET) 149 int flags; 150 /* A lock to protect state and busy fields */ 151 spinlock_t lock; 152 void *priv; 153 }; 154 155 struct scmi_xfer_ops; 156 struct scmi_proto_helpers_ops; 157 158 /** 159 * struct scmi_protocol_handle - Reference to an initialized protocol instance 160 * 161 * @dev: A reference to the associated SCMI instance device (handle->dev). 162 * @xops: A reference to a struct holding refs to the core xfer operations that 163 * can be used by the protocol implementation to generate SCMI messages. 164 * @set_priv: A method to set protocol private data for this instance. 165 * @get_priv: A method to get protocol private data previously set. 166 * 167 * This structure represents a protocol initialized against specific SCMI 168 * instance and it will be used as follows: 169 * - as a parameter fed from the core to the protocol initialization code so 170 * that it can access the core xfer operations to build and generate SCMI 171 * messages exclusively for the specific underlying protocol instance. 172 * - as an opaque handle fed by an SCMI driver user when it tries to access 173 * this protocol through its own protocol operations. 174 * In this case this handle will be returned as an opaque object together 175 * with the related protocol operations when the SCMI driver tries to access 176 * the protocol. 177 */ 178 struct scmi_protocol_handle { 179 struct device *dev; 180 const struct scmi_xfer_ops *xops; 181 const struct scmi_proto_helpers_ops *hops; 182 int (*set_priv)(const struct scmi_protocol_handle *ph, void *priv, 183 u32 version); 184 void *(*get_priv)(const struct scmi_protocol_handle *ph); 185 }; 186 187 /** 188 * struct scmi_iterator_state - Iterator current state descriptor 189 * @desc_index: Starting index for the current mulit-part request. 190 * @num_returned: Number of returned items in the last multi-part reply. 191 * @num_remaining: Number of remaining items in the multi-part message. 192 * @max_resources: Maximum acceptable number of items, configured by the caller 193 * depending on the underlying resources that it is querying. 194 * @loop_idx: The iterator loop index in the current multi-part reply. 195 * @rx_len: Size in bytes of the currenly processed message; it can be used by 196 * the user of the iterator to verify a reply size. 197 * @priv: Optional pointer to some additional state-related private data setup 198 * by the caller during the iterations. 199 */ 200 struct scmi_iterator_state { 201 unsigned int desc_index; 202 unsigned int num_returned; 203 unsigned int num_remaining; 204 unsigned int max_resources; 205 unsigned int loop_idx; 206 size_t rx_len; 207 void *priv; 208 }; 209 210 /** 211 * struct scmi_iterator_ops - Custom iterator operations 212 * @prepare_message: An operation to provide the custom logic to fill in the 213 * SCMI command request pointed by @message. @desc_index is 214 * a reference to the next index to use in the multi-part 215 * request. 216 * @update_state: An operation to provide the custom logic to update the 217 * iterator state from the actual message response. 218 * @process_response: An operation to provide the custom logic needed to process 219 * each chunk of the multi-part message. 220 */ 221 struct scmi_iterator_ops { 222 void (*prepare_message)(void *message, unsigned int desc_index, 223 const void *priv); 224 int (*update_state)(struct scmi_iterator_state *st, 225 const void *response, void *priv); 226 int (*process_response)(const struct scmi_protocol_handle *ph, 227 const void *response, 228 struct scmi_iterator_state *st, void *priv); 229 }; 230 231 struct scmi_fc_db_info { 232 int width; 233 u64 set; 234 u64 mask; 235 void __iomem *addr; 236 }; 237 238 struct scmi_fc_info { 239 void __iomem *set_addr; 240 void __iomem *get_addr; 241 struct scmi_fc_db_info *set_db; 242 u32 rate_limit; 243 }; 244 245 /** 246 * struct scmi_proto_helpers_ops - References to common protocol helpers 247 * @extended_name_get: A common helper function to retrieve extended naming 248 * for the specified resource using the specified command. 249 * Result is returned as a NULL terminated string in the 250 * pre-allocated area pointed to by @name with maximum 251 * capacity of @len bytes. 252 * @iter_response_init: A common helper to initialize a generic iterator to 253 * parse multi-message responses: when run the iterator 254 * will take care to send the initial command request as 255 * specified by @msg_id and @tx_size and then to parse the 256 * multi-part responses using the custom operations 257 * provided in @ops. 258 * @iter_response_run: A common helper to trigger the run of a previously 259 * initialized iterator. 260 * @protocol_msg_check: A common helper to check is a specific protocol message 261 * is supported. 262 * @fastchannel_init: A common helper used to initialize FC descriptors by 263 * gathering FC descriptions from the SCMI platform server. 264 * @fastchannel_db_ring: A common helper to ring a FC doorbell. 265 * @get_max_msg_size: A common helper to get the maximum message size. 266 */ 267 struct scmi_proto_helpers_ops { 268 int (*extended_name_get)(const struct scmi_protocol_handle *ph, 269 u8 cmd_id, u32 res_id, u32 *flags, char *name, 270 size_t len); 271 void *(*iter_response_init)(const struct scmi_protocol_handle *ph, 272 struct scmi_iterator_ops *ops, 273 unsigned int max_resources, u8 msg_id, 274 size_t tx_size, void *priv); 275 int (*iter_response_run)(void *iter); 276 int (*protocol_msg_check)(const struct scmi_protocol_handle *ph, 277 u32 message_id, u32 *attributes); 278 void (*fastchannel_init)(const struct scmi_protocol_handle *ph, 279 u8 describe_id, u32 message_id, 280 u32 valid_size, u32 domain, 281 void __iomem **p_addr, 282 struct scmi_fc_db_info **p_db, 283 u32 *rate_limit); 284 void (*fastchannel_db_ring)(struct scmi_fc_db_info *db); 285 int (*get_max_msg_size)(const struct scmi_protocol_handle *ph); 286 }; 287 288 /** 289 * struct scmi_xfer_ops - References to the core SCMI xfer operations. 290 * @version_get: Get this version protocol. 291 * @xfer_get_init: Initialize one struct xfer if any xfer slot is free. 292 * @reset_rx_to_maxsz: Reset rx size to max transport size. 293 * @do_xfer: Do the SCMI transfer. 294 * @do_xfer_with_response: Do the SCMI transfer waiting for a response. 295 * @xfer_put: Free the xfer slot. 296 * 297 * Note that all this operations expect a protocol handle as first parameter; 298 * they then internally use it to infer the underlying protocol number: this 299 * way is not possible for a protocol implementation to forge messages for 300 * another protocol. 301 */ 302 struct scmi_xfer_ops { 303 int (*version_get)(const struct scmi_protocol_handle *ph, u32 *version); 304 int (*xfer_get_init)(const struct scmi_protocol_handle *ph, u8 msg_id, 305 size_t tx_size, size_t rx_size, 306 struct scmi_xfer **p); 307 void (*reset_rx_to_maxsz)(const struct scmi_protocol_handle *ph, 308 struct scmi_xfer *xfer); 309 int (*do_xfer)(const struct scmi_protocol_handle *ph, 310 struct scmi_xfer *xfer); 311 int (*do_xfer_with_response)(const struct scmi_protocol_handle *ph, 312 struct scmi_xfer *xfer); 313 void (*xfer_put)(const struct scmi_protocol_handle *ph, 314 struct scmi_xfer *xfer); 315 }; 316 317 typedef int (*scmi_prot_init_ph_fn_t)(const struct scmi_protocol_handle *); 318 319 /** 320 * struct scmi_protocol - Protocol descriptor 321 * @id: Protocol ID. 322 * @owner: Module reference if any. 323 * @instance_init: Mandatory protocol initialization function. 324 * @instance_deinit: Optional protocol de-initialization function. 325 * @ops: Optional reference to the operations provided by the protocol and 326 * exposed in scmi_protocol.h. 327 * @events: An optional reference to the events supported by this protocol. 328 * @supported_version: The highest version currently supported for this 329 * protocol by the agent. Each protocol implementation 330 * in the agent is supposed to downgrade to match the 331 * protocol version supported by the platform. 332 * @vendor_id: A firmware vendor string for vendor protocols matching. 333 * Ignored when @id identifies a standard protocol, cannot be NULL 334 * otherwise. 335 * @sub_vendor_id: A firmware sub_vendor string for vendor protocols matching. 336 * Ignored if NULL or when @id identifies a standard protocol. 337 * @impl_ver: A firmware implementation version for vendor protocols matching. 338 * Ignored if zero or if @id identifies a standard protocol. 339 * 340 * Note that vendor protocols matching at load time is performed by attempting 341 * the closest match first against the tuple (vendor, sub_vendor, impl_ver) 342 */ 343 struct scmi_protocol { 344 const u8 id; 345 struct module *owner; 346 const scmi_prot_init_ph_fn_t instance_init; 347 const scmi_prot_init_ph_fn_t instance_deinit; 348 const void *ops; 349 const struct scmi_protocol_events *events; 350 unsigned int supported_version; 351 char *vendor_id; 352 char *sub_vendor_id; 353 u32 impl_ver; 354 }; 355 356 #define DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(name, proto) \ 357 static const struct scmi_protocol *__this_proto = &(proto); \ 358 \ 359 int __init scmi_##name##_register(void) \ 360 { \ 361 return scmi_protocol_register(__this_proto); \ 362 } \ 363 \ 364 void __exit scmi_##name##_unregister(void) \ 365 { \ 366 scmi_protocol_unregister(__this_proto); \ 367 } 368 369 #define DECLARE_SCMI_REGISTER_UNREGISTER(func) \ 370 int __init scmi_##func##_register(void); \ 371 void __exit scmi_##func##_unregister(void) 372 DECLARE_SCMI_REGISTER_UNREGISTER(base); 373 DECLARE_SCMI_REGISTER_UNREGISTER(clock); 374 DECLARE_SCMI_REGISTER_UNREGISTER(perf); 375 DECLARE_SCMI_REGISTER_UNREGISTER(pinctrl); 376 DECLARE_SCMI_REGISTER_UNREGISTER(power); 377 DECLARE_SCMI_REGISTER_UNREGISTER(reset); 378 DECLARE_SCMI_REGISTER_UNREGISTER(sensors); 379 DECLARE_SCMI_REGISTER_UNREGISTER(voltage); 380 DECLARE_SCMI_REGISTER_UNREGISTER(system); 381 DECLARE_SCMI_REGISTER_UNREGISTER(powercap); 382 383 #endif /* _SCMI_PROTOCOLS_H */ 384