protocols.h 14 KB

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