common.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* SPDX-License-Identifier: GPL-2.0 */
  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-2022 ARM Ltd.
  8. */
  9. #ifndef _SCMI_COMMON_H
  10. #define _SCMI_COMMON_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. #include "protocols.h"
  25. #include "notify.h"
  26. #define MSG_ID_MASK GENMASK(7, 0)
  27. #define MSG_XTRACT_ID(hdr) FIELD_GET(MSG_ID_MASK, (hdr))
  28. #define MSG_TYPE_MASK GENMASK(9, 8)
  29. #define MSG_XTRACT_TYPE(hdr) FIELD_GET(MSG_TYPE_MASK, (hdr))
  30. #define MSG_TYPE_COMMAND 0
  31. #define MSG_TYPE_DELAYED_RESP 2
  32. #define MSG_TYPE_NOTIFICATION 3
  33. #define MSG_PROTOCOL_ID_MASK GENMASK(17, 10)
  34. #define MSG_XTRACT_PROT_ID(hdr) FIELD_GET(MSG_PROTOCOL_ID_MASK, (hdr))
  35. #define MSG_TOKEN_ID_MASK GENMASK(27, 18)
  36. #define MSG_XTRACT_TOKEN(hdr) FIELD_GET(MSG_TOKEN_ID_MASK, (hdr))
  37. #define MSG_TOKEN_MAX (MSG_XTRACT_TOKEN(MSG_TOKEN_ID_MASK) + 1)
  38. /*
  39. * Size of @pending_xfers hashtable included in @scmi_xfers_info; ideally, in
  40. * order to minimize space and collisions, this should equal max_msg, i.e. the
  41. * maximum number of in-flight messages on a specific platform, but such value
  42. * is only available at runtime while kernel hashtables are statically sized:
  43. * pick instead as a fixed static size the maximum number of entries that can
  44. * fit the whole table into one 4k page.
  45. */
  46. #define SCMI_PENDING_XFERS_HT_ORDER_SZ 9
  47. /**
  48. * pack_scmi_header() - packs and returns 32-bit header
  49. *
  50. * @hdr: pointer to header containing all the information on message id,
  51. * protocol id, sequence id and type.
  52. *
  53. * Return: 32-bit packed message header to be sent to the platform.
  54. */
  55. static inline u32 pack_scmi_header(struct scmi_msg_hdr *hdr)
  56. {
  57. return FIELD_PREP(MSG_ID_MASK, hdr->id) |
  58. FIELD_PREP(MSG_TYPE_MASK, hdr->type) |
  59. FIELD_PREP(MSG_TOKEN_ID_MASK, hdr->seq) |
  60. FIELD_PREP(MSG_PROTOCOL_ID_MASK, hdr->protocol_id);
  61. }
  62. /**
  63. * unpack_scmi_header() - unpacks and records message and protocol id
  64. *
  65. * @msg_hdr: 32-bit packed message header sent from the platform
  66. * @hdr: pointer to header to fetch message and protocol id.
  67. */
  68. static inline void unpack_scmi_header(u32 msg_hdr, struct scmi_msg_hdr *hdr)
  69. {
  70. hdr->id = MSG_XTRACT_ID(msg_hdr);
  71. hdr->protocol_id = MSG_XTRACT_PROT_ID(msg_hdr);
  72. hdr->type = MSG_XTRACT_TYPE(msg_hdr);
  73. }
  74. /*
  75. * An helper macro to lookup an xfer from the @pending_xfers hashtable
  76. * using the message sequence number token as a key.
  77. */
  78. #define XFER_FIND(__ht, __k) \
  79. ({ \
  80. typeof(__k) k_ = __k; \
  81. struct scmi_xfer *xfer_ = NULL; \
  82. \
  83. hash_for_each_possible((__ht), xfer_, node, k_) \
  84. if (xfer_->hdr.seq == k_) \
  85. break; \
  86. xfer_; \
  87. })
  88. struct scmi_revision_info *
  89. scmi_revision_area_get(const struct scmi_protocol_handle *ph);
  90. int scmi_handle_put(const struct scmi_handle *handle);
  91. void scmi_device_link_add(struct device *consumer, struct device *supplier);
  92. struct scmi_handle *scmi_handle_get(struct device *dev);
  93. void scmi_set_handle(struct scmi_device *scmi_dev);
  94. void scmi_setup_protocol_implemented(const struct scmi_protocol_handle *ph,
  95. u8 *prot_imp);
  96. int __init scmi_bus_init(void);
  97. void __exit scmi_bus_exit(void);
  98. const struct scmi_protocol *scmi_protocol_get(int protocol_id);
  99. void scmi_protocol_put(int protocol_id);
  100. int scmi_protocol_acquire(const struct scmi_handle *handle, u8 protocol_id);
  101. void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id);
  102. /* SCMI Transport */
  103. /**
  104. * struct scmi_chan_info - Structure representing a SCMI channel information
  105. *
  106. * @dev: Reference to device in the SCMI hierarchy corresponding to this
  107. * channel
  108. * @rx_timeout_ms: The configured RX timeout in milliseconds.
  109. * @handle: Pointer to SCMI entity handle
  110. * @no_completion_irq: Flag to indicate that this channel has no completion
  111. * interrupt mechanism for synchronous commands.
  112. * This can be dynamically set by transports at run-time
  113. * inside their provided .chan_setup().
  114. * @transport_info: Transport layer related information
  115. */
  116. struct scmi_chan_info {
  117. struct device *dev;
  118. unsigned int rx_timeout_ms;
  119. struct scmi_handle *handle;
  120. bool no_completion_irq;
  121. void *transport_info;
  122. };
  123. /**
  124. * struct scmi_transport_ops - Structure representing a SCMI transport ops
  125. *
  126. * @link_supplier: Optional callback to add link to a supplier device
  127. * @chan_available: Callback to check if channel is available or not
  128. * @chan_setup: Callback to allocate and setup a channel
  129. * @chan_free: Callback to free a channel
  130. * @get_max_msg: Optional callback to provide max_msg dynamically
  131. * Returns the maximum number of messages for the channel type
  132. * (tx or rx) that can be pending simultaneously in the system
  133. * @send_message: Callback to send a message
  134. * @mark_txdone: Callback to mark tx as done
  135. * @fetch_response: Callback to fetch response
  136. * @fetch_notification: Callback to fetch notification
  137. * @clear_channel: Callback to clear a channel
  138. * @poll_done: Callback to poll transfer status
  139. */
  140. struct scmi_transport_ops {
  141. int (*link_supplier)(struct device *dev);
  142. bool (*chan_available)(struct device *dev, int idx);
  143. int (*chan_setup)(struct scmi_chan_info *cinfo, struct device *dev,
  144. bool tx);
  145. int (*chan_free)(int id, void *p, void *data);
  146. unsigned int (*get_max_msg)(struct scmi_chan_info *base_cinfo);
  147. int (*send_message)(struct scmi_chan_info *cinfo,
  148. struct scmi_xfer *xfer);
  149. void (*mark_txdone)(struct scmi_chan_info *cinfo, int ret,
  150. struct scmi_xfer *xfer);
  151. void (*fetch_response)(struct scmi_chan_info *cinfo,
  152. struct scmi_xfer *xfer);
  153. void (*fetch_notification)(struct scmi_chan_info *cinfo,
  154. size_t max_len, struct scmi_xfer *xfer);
  155. void (*clear_channel)(struct scmi_chan_info *cinfo);
  156. bool (*poll_done)(struct scmi_chan_info *cinfo, struct scmi_xfer *xfer);
  157. };
  158. int scmi_protocol_device_request(const struct scmi_device_id *id_table);
  159. void scmi_protocol_device_unrequest(const struct scmi_device_id *id_table);
  160. struct scmi_device *scmi_child_dev_find(struct device *parent,
  161. int prot_id, const char *name);
  162. /**
  163. * struct scmi_desc - Description of SoC integration
  164. *
  165. * @transport_init: An optional function that a transport can provide to
  166. * initialize some transport-specific setup during SCMI core
  167. * initialization, so ahead of SCMI core probing.
  168. * @transport_exit: An optional function that a transport can provide to
  169. * de-initialize some transport-specific setup during SCMI core
  170. * de-initialization, so after SCMI core removal.
  171. * @ops: Pointer to the transport specific ops structure
  172. * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
  173. * @max_msg: Maximum number of messages for a channel type (tx or rx) that can
  174. * be pending simultaneously in the system. May be overridden by the
  175. * get_max_msg op.
  176. * @max_msg_size: Maximum size of data per message that can be handled.
  177. * @force_polling: Flag to force this whole transport to use SCMI core polling
  178. * mechanism instead of completion interrupts even if available.
  179. * @sync_cmds_completed_on_ret: Flag to indicate that the transport assures
  180. * synchronous-command messages are atomically
  181. * completed on .send_message: no need to poll
  182. * actively waiting for a response.
  183. * Used by core internally only when polling is
  184. * selected as a waiting for reply method: i.e.
  185. * if a completion irq was found use that anyway.
  186. * @atomic_enabled: Flag to indicate that this transport, which is assured not
  187. * to sleep anywhere on the TX path, can be used in atomic mode
  188. * when requested.
  189. */
  190. struct scmi_desc {
  191. int (*transport_init)(void);
  192. void (*transport_exit)(void);
  193. const struct scmi_transport_ops *ops;
  194. int max_rx_timeout_ms;
  195. int max_msg;
  196. int max_msg_size;
  197. const bool force_polling;
  198. const bool sync_cmds_completed_on_ret;
  199. const bool atomic_enabled;
  200. };
  201. #ifdef CONFIG_ARM_SCMI_TRANSPORT_MAILBOX
  202. extern const struct scmi_desc scmi_mailbox_desc;
  203. #endif
  204. #ifdef CONFIG_ARM_SCMI_TRANSPORT_SMC
  205. extern const struct scmi_desc scmi_smc_desc;
  206. #endif
  207. #ifdef CONFIG_ARM_SCMI_TRANSPORT_VIRTIO
  208. extern const struct scmi_desc scmi_virtio_desc;
  209. #endif
  210. #ifdef CONFIG_ARM_SCMI_TRANSPORT_OPTEE
  211. extern const struct scmi_desc scmi_optee_desc;
  212. #endif
  213. void scmi_rx_callback(struct scmi_chan_info *cinfo, u32 msg_hdr, void *priv);
  214. void scmi_free_channel(struct scmi_chan_info *cinfo, struct idr *idr, int id);
  215. /* shmem related declarations */
  216. struct scmi_shared_mem;
  217. void shmem_tx_prepare(struct scmi_shared_mem __iomem *shmem,
  218. struct scmi_xfer *xfer, struct scmi_chan_info *cinfo);
  219. u32 shmem_read_header(struct scmi_shared_mem __iomem *shmem);
  220. void shmem_fetch_response(struct scmi_shared_mem __iomem *shmem,
  221. struct scmi_xfer *xfer);
  222. void shmem_fetch_notification(struct scmi_shared_mem __iomem *shmem,
  223. size_t max_len, struct scmi_xfer *xfer);
  224. void shmem_clear_channel(struct scmi_shared_mem __iomem *shmem);
  225. bool shmem_poll_done(struct scmi_shared_mem __iomem *shmem,
  226. struct scmi_xfer *xfer);
  227. /* declarations for message passing transports */
  228. struct scmi_msg_payld;
  229. /* Maximum overhead of message w.r.t. struct scmi_desc.max_msg_size */
  230. #define SCMI_MSG_MAX_PROT_OVERHEAD (2 * sizeof(__le32))
  231. size_t msg_response_size(struct scmi_xfer *xfer);
  232. size_t msg_command_size(struct scmi_xfer *xfer);
  233. void msg_tx_prepare(struct scmi_msg_payld *msg, struct scmi_xfer *xfer);
  234. u32 msg_read_header(struct scmi_msg_payld *msg);
  235. void msg_fetch_response(struct scmi_msg_payld *msg, size_t len,
  236. struct scmi_xfer *xfer);
  237. void msg_fetch_notification(struct scmi_msg_payld *msg, size_t len,
  238. size_t max_len, struct scmi_xfer *xfer);
  239. void scmi_notification_instance_data_set(const struct scmi_handle *handle,
  240. void *priv);
  241. void *scmi_notification_instance_data_get(const struct scmi_handle *handle);
  242. #endif /* _SCMI_COMMON_H */