common.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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-2024 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 <linux/unaligned.h>
  24. #include "protocols.h"
  25. #include "notify.h"
  26. #define SCMI_MAX_CHANNELS 256
  27. #define SCMI_MAX_RESPONSE_TIMEOUT (2 * MSEC_PER_SEC)
  28. enum scmi_error_codes {
  29. SCMI_SUCCESS = 0, /* Success */
  30. SCMI_ERR_SUPPORT = -1, /* Not supported */
  31. SCMI_ERR_PARAMS = -2, /* Invalid Parameters */
  32. SCMI_ERR_ACCESS = -3, /* Invalid access/permission denied */
  33. SCMI_ERR_ENTRY = -4, /* Not found */
  34. SCMI_ERR_RANGE = -5, /* Value out of range */
  35. SCMI_ERR_BUSY = -6, /* Device busy */
  36. SCMI_ERR_COMMS = -7, /* Communication Error */
  37. SCMI_ERR_GENERIC = -8, /* Generic Error */
  38. SCMI_ERR_HARDWARE = -9, /* Hardware Error */
  39. SCMI_ERR_PROTOCOL = -10,/* Protocol Error */
  40. };
  41. static const int scmi_linux_errmap[] = {
  42. /* better than switch case as long as return value is continuous */
  43. 0, /* SCMI_SUCCESS */
  44. -EOPNOTSUPP, /* SCMI_ERR_SUPPORT */
  45. -EINVAL, /* SCMI_ERR_PARAM */
  46. -EACCES, /* SCMI_ERR_ACCESS */
  47. -ENOENT, /* SCMI_ERR_ENTRY */
  48. -ERANGE, /* SCMI_ERR_RANGE */
  49. -EBUSY, /* SCMI_ERR_BUSY */
  50. -ECOMM, /* SCMI_ERR_COMMS */
  51. -EIO, /* SCMI_ERR_GENERIC */
  52. -EREMOTEIO, /* SCMI_ERR_HARDWARE */
  53. -EPROTO, /* SCMI_ERR_PROTOCOL */
  54. };
  55. static inline int scmi_to_linux_errno(int errno)
  56. {
  57. int err_idx = -errno;
  58. if (err_idx >= SCMI_SUCCESS && err_idx < ARRAY_SIZE(scmi_linux_errmap))
  59. return scmi_linux_errmap[err_idx];
  60. return -EIO;
  61. }
  62. #define MSG_ID_MASK GENMASK(7, 0)
  63. #define MSG_XTRACT_ID(hdr) FIELD_GET(MSG_ID_MASK, (hdr))
  64. #define MSG_TYPE_MASK GENMASK(9, 8)
  65. #define MSG_XTRACT_TYPE(hdr) FIELD_GET(MSG_TYPE_MASK, (hdr))
  66. #define MSG_TYPE_COMMAND 0
  67. #define MSG_TYPE_DELAYED_RESP 2
  68. #define MSG_TYPE_NOTIFICATION 3
  69. #define MSG_PROTOCOL_ID_MASK GENMASK(17, 10)
  70. #define MSG_XTRACT_PROT_ID(hdr) FIELD_GET(MSG_PROTOCOL_ID_MASK, (hdr))
  71. #define MSG_TOKEN_ID_MASK GENMASK(27, 18)
  72. #define MSG_XTRACT_TOKEN(hdr) FIELD_GET(MSG_TOKEN_ID_MASK, (hdr))
  73. #define MSG_TOKEN_MAX (MSG_XTRACT_TOKEN(MSG_TOKEN_ID_MASK) + 1)
  74. /*
  75. * Size of @pending_xfers hashtable included in @scmi_xfers_info; ideally, in
  76. * order to minimize space and collisions, this should equal max_msg, i.e. the
  77. * maximum number of in-flight messages on a specific platform, but such value
  78. * is only available at runtime while kernel hashtables are statically sized:
  79. * pick instead as a fixed static size the maximum number of entries that can
  80. * fit the whole table into one 4k page.
  81. */
  82. #define SCMI_PENDING_XFERS_HT_ORDER_SZ 9
  83. /**
  84. * pack_scmi_header() - packs and returns 32-bit header
  85. *
  86. * @hdr: pointer to header containing all the information on message id,
  87. * protocol id, sequence id and type.
  88. *
  89. * Return: 32-bit packed message header to be sent to the platform.
  90. */
  91. static inline u32 pack_scmi_header(struct scmi_msg_hdr *hdr)
  92. {
  93. return FIELD_PREP(MSG_ID_MASK, hdr->id) |
  94. FIELD_PREP(MSG_TYPE_MASK, hdr->type) |
  95. FIELD_PREP(MSG_TOKEN_ID_MASK, hdr->seq) |
  96. FIELD_PREP(MSG_PROTOCOL_ID_MASK, hdr->protocol_id);
  97. }
  98. /**
  99. * unpack_scmi_header() - unpacks and records message and protocol id
  100. *
  101. * @msg_hdr: 32-bit packed message header sent from the platform
  102. * @hdr: pointer to header to fetch message and protocol id.
  103. */
  104. static inline void unpack_scmi_header(u32 msg_hdr, struct scmi_msg_hdr *hdr)
  105. {
  106. hdr->id = MSG_XTRACT_ID(msg_hdr);
  107. hdr->protocol_id = MSG_XTRACT_PROT_ID(msg_hdr);
  108. hdr->type = MSG_XTRACT_TYPE(msg_hdr);
  109. }
  110. /*
  111. * An helper macro to lookup an xfer from the @pending_xfers hashtable
  112. * using the message sequence number token as a key.
  113. */
  114. #define XFER_FIND(__ht, __k) \
  115. ({ \
  116. typeof(__k) k_ = __k; \
  117. struct scmi_xfer *xfer_ = NULL; \
  118. \
  119. hash_for_each_possible((__ht), xfer_, node, k_) \
  120. if (xfer_->hdr.seq == k_) \
  121. break; \
  122. xfer_; \
  123. })
  124. struct scmi_revision_info *
  125. scmi_revision_area_get(const struct scmi_protocol_handle *ph);
  126. void scmi_setup_protocol_implemented(const struct scmi_protocol_handle *ph,
  127. u8 *prot_imp);
  128. extern const struct bus_type scmi_bus_type;
  129. #define SCMI_BUS_NOTIFY_DEVICE_REQUEST 0
  130. #define SCMI_BUS_NOTIFY_DEVICE_UNREQUEST 1
  131. extern struct blocking_notifier_head scmi_requested_devices_nh;
  132. struct scmi_device *scmi_device_create(struct device_node *np,
  133. struct device *parent, int protocol,
  134. const char *name);
  135. void scmi_device_destroy(struct device *parent, int protocol, const char *name);
  136. int scmi_protocol_acquire(const struct scmi_handle *handle, u8 protocol_id);
  137. void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id);
  138. /* SCMI Transport */
  139. /**
  140. * struct scmi_chan_info - Structure representing a SCMI channel information
  141. *
  142. * @id: An identifier for this channel: this matches the protocol number
  143. * used to initialize this channel
  144. * @dev: Reference to device in the SCMI hierarchy corresponding to this
  145. * channel
  146. * @is_p2a: A flag to identify a channel as P2A (RX)
  147. * @rx_timeout_ms: The configured RX timeout in milliseconds.
  148. * @handle: Pointer to SCMI entity handle
  149. * @no_completion_irq: Flag to indicate that this channel has no completion
  150. * interrupt mechanism for synchronous commands.
  151. * This can be dynamically set by transports at run-time
  152. * inside their provided .chan_setup().
  153. * @transport_info: Transport layer related information
  154. */
  155. struct scmi_chan_info {
  156. int id;
  157. struct device *dev;
  158. bool is_p2a;
  159. unsigned int rx_timeout_ms;
  160. struct scmi_handle *handle;
  161. bool no_completion_irq;
  162. void *transport_info;
  163. };
  164. /**
  165. * struct scmi_transport_ops - Structure representing a SCMI transport ops
  166. *
  167. * @chan_available: Callback to check if channel is available or not
  168. * @chan_setup: Callback to allocate and setup a channel
  169. * @chan_free: Callback to free a channel
  170. * @get_max_msg: Optional callback to provide max_msg dynamically
  171. * Returns the maximum number of messages for the channel type
  172. * (tx or rx) that can be pending simultaneously in the system
  173. * @send_message: Callback to send a message
  174. * @mark_txdone: Callback to mark tx as done
  175. * @fetch_response: Callback to fetch response
  176. * @fetch_notification: Callback to fetch notification
  177. * @clear_channel: Callback to clear a channel
  178. * @poll_done: Callback to poll transfer status
  179. */
  180. struct scmi_transport_ops {
  181. bool (*chan_available)(struct device_node *of_node, int idx);
  182. int (*chan_setup)(struct scmi_chan_info *cinfo, struct device *dev,
  183. bool tx);
  184. int (*chan_free)(int id, void *p, void *data);
  185. unsigned int (*get_max_msg)(struct scmi_chan_info *base_cinfo);
  186. int (*send_message)(struct scmi_chan_info *cinfo,
  187. struct scmi_xfer *xfer);
  188. void (*mark_txdone)(struct scmi_chan_info *cinfo, int ret,
  189. struct scmi_xfer *xfer);
  190. void (*fetch_response)(struct scmi_chan_info *cinfo,
  191. struct scmi_xfer *xfer);
  192. void (*fetch_notification)(struct scmi_chan_info *cinfo,
  193. size_t max_len, struct scmi_xfer *xfer);
  194. void (*clear_channel)(struct scmi_chan_info *cinfo);
  195. bool (*poll_done)(struct scmi_chan_info *cinfo, struct scmi_xfer *xfer);
  196. };
  197. /**
  198. * struct scmi_desc - Description of SoC integration
  199. *
  200. * @ops: Pointer to the transport specific ops structure
  201. * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
  202. * @max_msg: Maximum number of messages for a channel type (tx or rx) that can
  203. * be pending simultaneously in the system. May be overridden by the
  204. * get_max_msg op.
  205. * @max_msg_size: Maximum size of data per message that can be handled.
  206. * @force_polling: Flag to force this whole transport to use SCMI core polling
  207. * mechanism instead of completion interrupts even if available.
  208. * @sync_cmds_completed_on_ret: Flag to indicate that the transport assures
  209. * synchronous-command messages are atomically
  210. * completed on .send_message: no need to poll
  211. * actively waiting for a response.
  212. * Used by core internally only when polling is
  213. * selected as a waiting for reply method: i.e.
  214. * if a completion irq was found use that anyway.
  215. * @atomic_enabled: Flag to indicate that this transport, which is assured not
  216. * to sleep anywhere on the TX path, can be used in atomic mode
  217. * when requested.
  218. */
  219. struct scmi_desc {
  220. const struct scmi_transport_ops *ops;
  221. int max_rx_timeout_ms;
  222. int max_msg;
  223. int max_msg_size;
  224. const bool force_polling;
  225. const bool sync_cmds_completed_on_ret;
  226. const bool atomic_enabled;
  227. };
  228. static inline bool is_polling_required(struct scmi_chan_info *cinfo,
  229. const struct scmi_desc *desc)
  230. {
  231. return cinfo->no_completion_irq || desc->force_polling;
  232. }
  233. static inline bool is_transport_polling_capable(const struct scmi_desc *desc)
  234. {
  235. return desc->ops->poll_done || desc->sync_cmds_completed_on_ret;
  236. }
  237. static inline bool is_polling_enabled(struct scmi_chan_info *cinfo,
  238. const struct scmi_desc *desc)
  239. {
  240. return is_polling_required(cinfo, desc) &&
  241. is_transport_polling_capable(desc);
  242. }
  243. void scmi_xfer_raw_put(const struct scmi_handle *handle,
  244. struct scmi_xfer *xfer);
  245. struct scmi_xfer *scmi_xfer_raw_get(const struct scmi_handle *handle);
  246. struct scmi_chan_info *
  247. scmi_xfer_raw_channel_get(const struct scmi_handle *handle, u8 protocol_id);
  248. int scmi_xfer_raw_inflight_register(const struct scmi_handle *handle,
  249. struct scmi_xfer *xfer);
  250. int scmi_xfer_raw_wait_for_message_response(struct scmi_chan_info *cinfo,
  251. struct scmi_xfer *xfer,
  252. unsigned int timeout_ms);
  253. enum debug_counters {
  254. SENT_OK,
  255. SENT_FAIL,
  256. SENT_FAIL_POLLING_UNSUPPORTED,
  257. SENT_FAIL_CHANNEL_NOT_FOUND,
  258. RESPONSE_OK,
  259. NOTIFICATION_OK,
  260. DELAYED_RESPONSE_OK,
  261. XFERS_RESPONSE_TIMEOUT,
  262. XFERS_RESPONSE_POLLED_TIMEOUT,
  263. RESPONSE_POLLED_OK,
  264. ERR_MSG_UNEXPECTED,
  265. ERR_MSG_INVALID,
  266. ERR_MSG_NOMEM,
  267. ERR_PROTOCOL,
  268. SCMI_DEBUG_COUNTERS_LAST
  269. };
  270. /**
  271. * struct scmi_debug_info - Debug common info
  272. * @top_dentry: A reference to the top debugfs dentry
  273. * @name: Name of this SCMI instance
  274. * @type: Type of this SCMI instance
  275. * @is_atomic: Flag to state if the transport of this instance is atomic
  276. * @counters: An array of atomic_c's used for tracking statistics (if enabled)
  277. */
  278. struct scmi_debug_info {
  279. struct dentry *top_dentry;
  280. const char *name;
  281. const char *type;
  282. bool is_atomic;
  283. atomic_t counters[SCMI_DEBUG_COUNTERS_LAST];
  284. };
  285. static inline void scmi_inc_count(struct scmi_debug_info *dbg, int stat)
  286. {
  287. if (IS_ENABLED(CONFIG_ARM_SCMI_DEBUG_COUNTERS)) {
  288. if (dbg)
  289. atomic_inc(&dbg->counters[stat]);
  290. }
  291. }
  292. enum scmi_bad_msg {
  293. MSG_UNEXPECTED = -1,
  294. MSG_INVALID = -2,
  295. MSG_UNKNOWN = -3,
  296. MSG_NOMEM = -4,
  297. MSG_MBOX_SPURIOUS = -5,
  298. };
  299. /* shmem related declarations */
  300. struct scmi_shared_mem;
  301. /**
  302. * struct scmi_shared_mem_operations - Transport core operations for
  303. * Shared Memory
  304. *
  305. * @tx_prepare: Prepare the @xfer message for transmission on the chosen @shmem
  306. * @read_header: Read header of the message currently hold in @shmem
  307. * @fetch_response: Copy the message response from @shmem into @xfer
  308. * @fetch_notification: Copy the message notification from @shmem into @xfer
  309. * @clear_channel: Clear the @shmem channel busy flag
  310. * @poll_done: Check if poll has completed for @xfer on @shmem
  311. * @channel_free: Check if @shmem channel is marked as free
  312. * @channel_intr_enabled: Check is @shmem channel has requested a completion irq
  313. * @setup_iomap: Setup IO shared memory for channel @cinfo
  314. */
  315. struct scmi_shared_mem_operations {
  316. void (*tx_prepare)(struct scmi_shared_mem __iomem *shmem,
  317. struct scmi_xfer *xfer,
  318. struct scmi_chan_info *cinfo);
  319. u32 (*read_header)(struct scmi_shared_mem __iomem *shmem);
  320. void (*fetch_response)(struct scmi_shared_mem __iomem *shmem,
  321. struct scmi_xfer *xfer);
  322. void (*fetch_notification)(struct scmi_shared_mem __iomem *shmem,
  323. size_t max_len, struct scmi_xfer *xfer);
  324. void (*clear_channel)(struct scmi_shared_mem __iomem *shmem);
  325. bool (*poll_done)(struct scmi_shared_mem __iomem *shmem,
  326. struct scmi_xfer *xfer);
  327. bool (*channel_free)(struct scmi_shared_mem __iomem *shmem);
  328. bool (*channel_intr_enabled)(struct scmi_shared_mem __iomem *shmem);
  329. void __iomem *(*setup_iomap)(struct scmi_chan_info *cinfo,
  330. struct device *dev,
  331. bool tx, struct resource *res);
  332. };
  333. const struct scmi_shared_mem_operations *scmi_shared_mem_operations_get(void);
  334. /* declarations for message passing transports */
  335. struct scmi_msg_payld;
  336. /* Maximum overhead of message w.r.t. struct scmi_desc.max_msg_size */
  337. #define SCMI_MSG_MAX_PROT_OVERHEAD (2 * sizeof(__le32))
  338. /**
  339. * struct scmi_message_operations - Transport core operations for Message
  340. *
  341. * @response_size: Get calculated response size for @xfer
  342. * @command_size: Get calculated command size for @xfer
  343. * @tx_prepare: Prepare the @xfer message for transmission on the provided @msg
  344. * @read_header: Read header of the message currently hold in @msg
  345. * @fetch_response: Copy the message response from @msg into @xfer
  346. * @fetch_notification: Copy the message notification from @msg into @xfer
  347. */
  348. struct scmi_message_operations {
  349. size_t (*response_size)(struct scmi_xfer *xfer);
  350. size_t (*command_size)(struct scmi_xfer *xfer);
  351. void (*tx_prepare)(struct scmi_msg_payld *msg, struct scmi_xfer *xfer);
  352. u32 (*read_header)(struct scmi_msg_payld *msg);
  353. void (*fetch_response)(struct scmi_msg_payld *msg, size_t len,
  354. struct scmi_xfer *xfer);
  355. void (*fetch_notification)(struct scmi_msg_payld *msg, size_t len,
  356. size_t max_len, struct scmi_xfer *xfer);
  357. };
  358. const struct scmi_message_operations *scmi_message_operations_get(void);
  359. /**
  360. * struct scmi_transport_core_operations - Transpoert core operations
  361. *
  362. * @bad_message_trace: An helper to report a malformed/unexpected message
  363. * @rx_callback: Callback to report received messages
  364. * @shmem: Datagram operations for shared memory based transports
  365. * @msg: Datagram operations for message based transports
  366. */
  367. struct scmi_transport_core_operations {
  368. void (*bad_message_trace)(struct scmi_chan_info *cinfo,
  369. u32 msg_hdr, enum scmi_bad_msg err);
  370. void (*rx_callback)(struct scmi_chan_info *cinfo, u32 msg_hdr,
  371. void *priv);
  372. const struct scmi_shared_mem_operations *shmem;
  373. const struct scmi_message_operations *msg;
  374. };
  375. /**
  376. * struct scmi_transport - A structure representing a configured transport
  377. *
  378. * @supplier: Device representing the transport and acting as a supplier for
  379. * the core SCMI stack
  380. * @desc: Transport descriptor
  381. * @core_ops: A pointer to a pointer used by the core SCMI stack to make the
  382. * core transport operations accessible to the transports.
  383. */
  384. struct scmi_transport {
  385. struct device *supplier;
  386. struct scmi_desc *desc;
  387. struct scmi_transport_core_operations **core_ops;
  388. };
  389. #define DEFINE_SCMI_TRANSPORT_DRIVER(__tag, __drv, __desc, __match, __core_ops)\
  390. static void __tag##_dev_free(void *data) \
  391. { \
  392. struct platform_device *spdev = data; \
  393. \
  394. platform_device_unregister(spdev); \
  395. } \
  396. \
  397. static int __tag##_probe(struct platform_device *pdev) \
  398. { \
  399. struct device *dev = &pdev->dev; \
  400. struct platform_device *spdev; \
  401. struct scmi_transport strans; \
  402. int ret; \
  403. \
  404. spdev = platform_device_alloc("arm-scmi", PLATFORM_DEVID_AUTO); \
  405. if (!spdev) \
  406. return -ENOMEM; \
  407. \
  408. device_set_of_node_from_dev(&spdev->dev, dev); \
  409. \
  410. strans.supplier = dev; \
  411. strans.desc = &(__desc); \
  412. strans.core_ops = &(__core_ops); \
  413. \
  414. ret = platform_device_add_data(spdev, &strans, sizeof(strans)); \
  415. if (ret) \
  416. goto err; \
  417. \
  418. ret = platform_device_add(spdev); \
  419. if (ret) \
  420. goto err; \
  421. \
  422. return devm_add_action_or_reset(dev, __tag##_dev_free, spdev); \
  423. \
  424. err: \
  425. platform_device_put(spdev); \
  426. return ret; \
  427. } \
  428. \
  429. static struct platform_driver __drv = { \
  430. .driver = { \
  431. .name = #__tag "_transport", \
  432. .of_match_table = __match, \
  433. }, \
  434. .probe = __tag##_probe, \
  435. }
  436. void scmi_notification_instance_data_set(const struct scmi_handle *handle,
  437. void *priv);
  438. void *scmi_notification_instance_data_get(const struct scmi_handle *handle);
  439. #endif /* _SCMI_COMMON_H */