protocols.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 <asm/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. enum scmi_common_cmd {
  29. PROTOCOL_VERSION = 0x0,
  30. PROTOCOL_ATTRIBUTES = 0x1,
  31. PROTOCOL_MESSAGE_ATTRIBUTES = 0x2,
  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. * struct scmi_msg - Message(Tx/Rx) structure
  51. *
  52. * @buf: Buffer pointer
  53. * @len: Length of data in the Buffer
  54. */
  55. struct scmi_msg {
  56. void *buf;
  57. size_t len;
  58. };
  59. /**
  60. * struct scmi_msg_hdr - Message(Tx/Rx) header
  61. *
  62. * @id: The identifier of the message being sent
  63. * @protocol_id: The identifier of the protocol used to send @id message
  64. * @type: The SCMI type for this message
  65. * @seq: The token to identify the message. When a message returns, the
  66. * platform returns the whole message header unmodified including the
  67. * token
  68. * @status: Status of the transfer once it's complete
  69. * @poll_completion: Indicate if the transfer needs to be polled for
  70. * completion or interrupt mode is used
  71. */
  72. struct scmi_msg_hdr {
  73. u8 id;
  74. u8 protocol_id;
  75. u8 type;
  76. u16 seq;
  77. u32 status;
  78. bool poll_completion;
  79. };
  80. /**
  81. * struct scmi_xfer - Structure representing a message flow
  82. *
  83. * @transfer_id: Unique ID for debug & profiling purpose
  84. * @hdr: Transmit message header
  85. * @tx: Transmit message
  86. * @rx: Receive message, the buffer should be pre-allocated to store
  87. * message. If request-ACK protocol is used, we can reuse the same
  88. * buffer for the rx path as we use for the tx path.
  89. * @done: command message transmit completion event
  90. * @async_done: pointer to delayed response message received event completion
  91. * @pending: True for xfers added to @pending_xfers hashtable
  92. * @node: An hlist_node reference used to store this xfer, alternatively, on
  93. * the free list @free_xfers or in the @pending_xfers hashtable
  94. * @users: A refcount to track the active users for this xfer.
  95. * This is meant to protect against the possibility that, when a command
  96. * transaction times out concurrently with the reception of a valid
  97. * response message, the xfer could be finally put on the TX path, and
  98. * so vanish, while on the RX path scmi_rx_callback() is still
  99. * processing it: in such a case this refcounting will ensure that, even
  100. * though the timed-out transaction will anyway cause the command
  101. * request to be reported as failed by time-out, the underlying xfer
  102. * cannot be discarded and possibly reused until the last one user on
  103. * the RX path has released it.
  104. * @busy: An atomic flag to ensure exclusive write access to this xfer
  105. * @state: The current state of this transfer, with states transitions deemed
  106. * valid being:
  107. * - SCMI_XFER_SENT_OK -> SCMI_XFER_RESP_OK [ -> SCMI_XFER_DRESP_OK ]
  108. * - SCMI_XFER_SENT_OK -> SCMI_XFER_DRESP_OK
  109. * (Missing synchronous response is assumed OK and ignored)
  110. * @lock: A spinlock to protect state and busy fields.
  111. * @priv: A pointer for transport private usage.
  112. */
  113. struct scmi_xfer {
  114. int transfer_id;
  115. struct scmi_msg_hdr hdr;
  116. struct scmi_msg tx;
  117. struct scmi_msg rx;
  118. struct completion done;
  119. struct completion *async_done;
  120. bool pending;
  121. struct hlist_node node;
  122. refcount_t users;
  123. #define SCMI_XFER_FREE 0
  124. #define SCMI_XFER_BUSY 1
  125. atomic_t busy;
  126. #define SCMI_XFER_SENT_OK 0
  127. #define SCMI_XFER_RESP_OK 1
  128. #define SCMI_XFER_DRESP_OK 2
  129. int state;
  130. /* A lock to protect state and busy fields */
  131. spinlock_t lock;
  132. void *priv;
  133. };
  134. struct scmi_xfer_ops;
  135. struct scmi_proto_helpers_ops;
  136. /**
  137. * struct scmi_protocol_handle - Reference to an initialized protocol instance
  138. *
  139. * @dev: A reference to the associated SCMI instance device (handle->dev).
  140. * @xops: A reference to a struct holding refs to the core xfer operations that
  141. * can be used by the protocol implementation to generate SCMI messages.
  142. * @set_priv: A method to set protocol private data for this instance.
  143. * @get_priv: A method to get protocol private data previously set.
  144. *
  145. * This structure represents a protocol initialized against specific SCMI
  146. * instance and it will be used as follows:
  147. * - as a parameter fed from the core to the protocol initialization code so
  148. * that it can access the core xfer operations to build and generate SCMI
  149. * messages exclusively for the specific underlying protocol instance.
  150. * - as an opaque handle fed by an SCMI driver user when it tries to access
  151. * this protocol through its own protocol operations.
  152. * In this case this handle will be returned as an opaque object together
  153. * with the related protocol operations when the SCMI driver tries to access
  154. * the protocol.
  155. */
  156. struct scmi_protocol_handle {
  157. struct device *dev;
  158. const struct scmi_xfer_ops *xops;
  159. const struct scmi_proto_helpers_ops *hops;
  160. int (*set_priv)(const struct scmi_protocol_handle *ph, void *priv);
  161. void *(*get_priv)(const struct scmi_protocol_handle *ph);
  162. };
  163. /**
  164. * struct scmi_iterator_state - Iterator current state descriptor
  165. * @desc_index: Starting index for the current mulit-part request.
  166. * @num_returned: Number of returned items in the last multi-part reply.
  167. * @num_remaining: Number of remaining items in the multi-part message.
  168. * @max_resources: Maximum acceptable number of items, configured by the caller
  169. * depending on the underlying resources that it is querying.
  170. * @loop_idx: The iterator loop index in the current multi-part reply.
  171. * @rx_len: Size in bytes of the currenly processed message; it can be used by
  172. * the user of the iterator to verify a reply size.
  173. * @priv: Optional pointer to some additional state-related private data setup
  174. * by the caller during the iterations.
  175. */
  176. struct scmi_iterator_state {
  177. unsigned int desc_index;
  178. unsigned int num_returned;
  179. unsigned int num_remaining;
  180. unsigned int max_resources;
  181. unsigned int loop_idx;
  182. size_t rx_len;
  183. void *priv;
  184. };
  185. /**
  186. * struct scmi_iterator_ops - Custom iterator operations
  187. * @prepare_message: An operation to provide the custom logic to fill in the
  188. * SCMI command request pointed by @message. @desc_index is
  189. * a reference to the next index to use in the multi-part
  190. * request.
  191. * @update_state: An operation to provide the custom logic to update the
  192. * iterator state from the actual message response.
  193. * @process_response: An operation to provide the custom logic needed to process
  194. * each chunk of the multi-part message.
  195. */
  196. struct scmi_iterator_ops {
  197. void (*prepare_message)(void *message, unsigned int desc_index,
  198. const void *priv);
  199. int (*update_state)(struct scmi_iterator_state *st,
  200. const void *response, void *priv);
  201. int (*process_response)(const struct scmi_protocol_handle *ph,
  202. const void *response,
  203. struct scmi_iterator_state *st, void *priv);
  204. };
  205. struct scmi_fc_db_info {
  206. int width;
  207. u64 set;
  208. u64 mask;
  209. void __iomem *addr;
  210. };
  211. struct scmi_fc_info {
  212. void __iomem *set_addr;
  213. void __iomem *get_addr;
  214. struct scmi_fc_db_info *set_db;
  215. };
  216. /**
  217. * struct scmi_proto_helpers_ops - References to common protocol helpers
  218. * @extended_name_get: A common helper function to retrieve extended naming
  219. * for the specified resource using the specified command.
  220. * Result is returned as a NULL terminated string in the
  221. * pre-allocated area pointed to by @name with maximum
  222. * capacity of @len bytes.
  223. * @iter_response_init: A common helper to initialize a generic iterator to
  224. * parse multi-message responses: when run the iterator
  225. * will take care to send the initial command request as
  226. * specified by @msg_id and @tx_size and then to parse the
  227. * multi-part responses using the custom operations
  228. * provided in @ops.
  229. * @iter_response_run: A common helper to trigger the run of a previously
  230. * initialized iterator.
  231. * @fastchannel_init: A common helper used to initialize FC descriptors by
  232. * gathering FC descriptions from the SCMI platform server.
  233. * @fastchannel_db_ring: A common helper to ring a FC doorbell.
  234. */
  235. struct scmi_proto_helpers_ops {
  236. int (*extended_name_get)(const struct scmi_protocol_handle *ph,
  237. u8 cmd_id, u32 res_id, char *name, size_t len);
  238. void *(*iter_response_init)(const struct scmi_protocol_handle *ph,
  239. struct scmi_iterator_ops *ops,
  240. unsigned int max_resources, u8 msg_id,
  241. size_t tx_size, void *priv);
  242. int (*iter_response_run)(void *iter);
  243. void (*fastchannel_init)(const struct scmi_protocol_handle *ph,
  244. u8 describe_id, u32 message_id,
  245. u32 valid_size, u32 domain,
  246. void __iomem **p_addr,
  247. struct scmi_fc_db_info **p_db);
  248. void (*fastchannel_db_ring)(struct scmi_fc_db_info *db);
  249. };
  250. /**
  251. * struct scmi_xfer_ops - References to the core SCMI xfer operations.
  252. * @version_get: Get this version protocol.
  253. * @xfer_get_init: Initialize one struct xfer if any xfer slot is free.
  254. * @reset_rx_to_maxsz: Reset rx size to max transport size.
  255. * @do_xfer: Do the SCMI transfer.
  256. * @do_xfer_with_response: Do the SCMI transfer waiting for a response.
  257. * @xfer_put: Free the xfer slot.
  258. *
  259. * Note that all this operations expect a protocol handle as first parameter;
  260. * they then internally use it to infer the underlying protocol number: this
  261. * way is not possible for a protocol implementation to forge messages for
  262. * another protocol.
  263. */
  264. struct scmi_xfer_ops {
  265. int (*version_get)(const struct scmi_protocol_handle *ph, u32 *version);
  266. int (*xfer_get_init)(const struct scmi_protocol_handle *ph, u8 msg_id,
  267. size_t tx_size, size_t rx_size,
  268. struct scmi_xfer **p);
  269. void (*reset_rx_to_maxsz)(const struct scmi_protocol_handle *ph,
  270. struct scmi_xfer *xfer);
  271. int (*do_xfer)(const struct scmi_protocol_handle *ph,
  272. struct scmi_xfer *xfer);
  273. int (*do_xfer_with_response)(const struct scmi_protocol_handle *ph,
  274. struct scmi_xfer *xfer);
  275. void (*xfer_put)(const struct scmi_protocol_handle *ph,
  276. struct scmi_xfer *xfer);
  277. };
  278. typedef int (*scmi_prot_init_ph_fn_t)(const struct scmi_protocol_handle *);
  279. /**
  280. * struct scmi_protocol - Protocol descriptor
  281. * @id: Protocol ID.
  282. * @owner: Module reference if any.
  283. * @instance_init: Mandatory protocol initialization function.
  284. * @instance_deinit: Optional protocol de-initialization function.
  285. * @ops: Optional reference to the operations provided by the protocol and
  286. * exposed in scmi_protocol.h.
  287. * @events: An optional reference to the events supported by this protocol.
  288. */
  289. struct scmi_protocol {
  290. const u8 id;
  291. struct module *owner;
  292. const scmi_prot_init_ph_fn_t instance_init;
  293. const scmi_prot_init_ph_fn_t instance_deinit;
  294. const void *ops;
  295. const struct scmi_protocol_events *events;
  296. };
  297. #define DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(name, proto) \
  298. static const struct scmi_protocol *__this_proto = &(proto); \
  299. \
  300. int __init scmi_##name##_register(void) \
  301. { \
  302. return scmi_protocol_register(__this_proto); \
  303. } \
  304. \
  305. void __exit scmi_##name##_unregister(void) \
  306. { \
  307. scmi_protocol_unregister(__this_proto); \
  308. }
  309. #define DECLARE_SCMI_REGISTER_UNREGISTER(func) \
  310. int __init scmi_##func##_register(void); \
  311. void __exit scmi_##func##_unregister(void)
  312. DECLARE_SCMI_REGISTER_UNREGISTER(base);
  313. DECLARE_SCMI_REGISTER_UNREGISTER(clock);
  314. DECLARE_SCMI_REGISTER_UNREGISTER(perf);
  315. DECLARE_SCMI_REGISTER_UNREGISTER(power);
  316. DECLARE_SCMI_REGISTER_UNREGISTER(reset);
  317. DECLARE_SCMI_REGISTER_UNREGISTER(sensors);
  318. DECLARE_SCMI_REGISTER_UNREGISTER(voltage);
  319. DECLARE_SCMI_REGISTER_UNREGISTER(system);
  320. DECLARE_SCMI_REGISTER_UNREGISTER(powercap);
  321. #endif /* _SCMI_PROTOCOLS_H */