virtio.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Virtio Transport driver for Arm System Control and Management Interface
  4. * (SCMI).
  5. *
  6. * Copyright (C) 2020-2022 OpenSynergy.
  7. * Copyright (C) 2021-2022 ARM Ltd.
  8. */
  9. /**
  10. * DOC: Theory of Operation
  11. *
  12. * The scmi-virtio transport implements a driver for the virtio SCMI device.
  13. *
  14. * There is one Tx channel (virtio cmdq, A2P channel) and at most one Rx
  15. * channel (virtio eventq, P2A channel). Each channel is implemented through a
  16. * virtqueue. Access to each virtqueue is protected by spinlocks.
  17. */
  18. #include <linux/completion.h>
  19. #include <linux/errno.h>
  20. #include <linux/refcount.h>
  21. #include <linux/slab.h>
  22. #include <linux/virtio.h>
  23. #include <linux/virtio_config.h>
  24. #include <uapi/linux/virtio_ids.h>
  25. #include <uapi/linux/virtio_scmi.h>
  26. #include "common.h"
  27. #define VIRTIO_MAX_RX_TIMEOUT_MS 60000
  28. #define VIRTIO_SCMI_MAX_MSG_SIZE 128 /* Value may be increased. */
  29. #define VIRTIO_SCMI_MAX_PDU_SIZE \
  30. (VIRTIO_SCMI_MAX_MSG_SIZE + SCMI_MSG_MAX_PROT_OVERHEAD)
  31. #define DESCRIPTORS_PER_TX_MSG 2
  32. /**
  33. * struct scmi_vio_channel - Transport channel information
  34. *
  35. * @vqueue: Associated virtqueue
  36. * @cinfo: SCMI Tx or Rx channel
  37. * @free_lock: Protects access to the @free_list.
  38. * @free_list: List of unused scmi_vio_msg, maintained for Tx channels only
  39. * @deferred_tx_work: Worker for TX deferred replies processing
  40. * @deferred_tx_wq: Workqueue for TX deferred replies
  41. * @pending_lock: Protects access to the @pending_cmds_list.
  42. * @pending_cmds_list: List of pre-fetched commands queueud for later processing
  43. * @is_rx: Whether channel is an Rx channel
  44. * @max_msg: Maximum number of pending messages for this channel.
  45. * @lock: Protects access to all members except users, free_list and
  46. * pending_cmds_list.
  47. * @shutdown_done: A reference to a completion used when freeing this channel.
  48. * @users: A reference count to currently active users of this channel.
  49. */
  50. struct scmi_vio_channel {
  51. struct virtqueue *vqueue;
  52. struct scmi_chan_info *cinfo;
  53. /* lock to protect access to the free list. */
  54. spinlock_t free_lock;
  55. struct list_head free_list;
  56. /* lock to protect access to the pending list. */
  57. spinlock_t pending_lock;
  58. struct list_head pending_cmds_list;
  59. struct work_struct deferred_tx_work;
  60. struct workqueue_struct *deferred_tx_wq;
  61. bool is_rx;
  62. unsigned int max_msg;
  63. /*
  64. * Lock to protect access to all members except users, free_list and
  65. * pending_cmds_list
  66. */
  67. spinlock_t lock;
  68. struct completion *shutdown_done;
  69. refcount_t users;
  70. };
  71. enum poll_states {
  72. VIO_MSG_NOT_POLLED,
  73. VIO_MSG_POLL_TIMEOUT,
  74. VIO_MSG_POLLING,
  75. VIO_MSG_POLL_DONE,
  76. };
  77. /**
  78. * struct scmi_vio_msg - Transport PDU information
  79. *
  80. * @request: SDU used for commands
  81. * @input: SDU used for (delayed) responses and notifications
  82. * @list: List which scmi_vio_msg may be part of
  83. * @rx_len: Input SDU size in bytes, once input has been received
  84. * @poll_idx: Last used index registered for polling purposes if this message
  85. * transaction reply was configured for polling.
  86. * @poll_status: Polling state for this message.
  87. * @poll_lock: A lock to protect @poll_status
  88. * @users: A reference count to track this message users and avoid premature
  89. * freeing (and reuse) when polling and IRQ execution paths interleave.
  90. */
  91. struct scmi_vio_msg {
  92. struct scmi_msg_payld *request;
  93. struct scmi_msg_payld *input;
  94. struct list_head list;
  95. unsigned int rx_len;
  96. unsigned int poll_idx;
  97. enum poll_states poll_status;
  98. /* Lock to protect access to poll_status */
  99. spinlock_t poll_lock;
  100. refcount_t users;
  101. };
  102. /* Only one SCMI VirtIO device can possibly exist */
  103. static struct virtio_device *scmi_vdev;
  104. static void scmi_vio_channel_ready(struct scmi_vio_channel *vioch,
  105. struct scmi_chan_info *cinfo)
  106. {
  107. unsigned long flags;
  108. spin_lock_irqsave(&vioch->lock, flags);
  109. cinfo->transport_info = vioch;
  110. /* Indirectly setting channel not available any more */
  111. vioch->cinfo = cinfo;
  112. spin_unlock_irqrestore(&vioch->lock, flags);
  113. refcount_set(&vioch->users, 1);
  114. }
  115. static inline bool scmi_vio_channel_acquire(struct scmi_vio_channel *vioch)
  116. {
  117. return refcount_inc_not_zero(&vioch->users);
  118. }
  119. static inline void scmi_vio_channel_release(struct scmi_vio_channel *vioch)
  120. {
  121. if (refcount_dec_and_test(&vioch->users)) {
  122. unsigned long flags;
  123. spin_lock_irqsave(&vioch->lock, flags);
  124. if (vioch->shutdown_done) {
  125. vioch->cinfo = NULL;
  126. complete(vioch->shutdown_done);
  127. }
  128. spin_unlock_irqrestore(&vioch->lock, flags);
  129. }
  130. }
  131. static void scmi_vio_channel_cleanup_sync(struct scmi_vio_channel *vioch)
  132. {
  133. unsigned long flags;
  134. DECLARE_COMPLETION_ONSTACK(vioch_shutdown_done);
  135. /*
  136. * Prepare to wait for the last release if not already released
  137. * or in progress.
  138. */
  139. spin_lock_irqsave(&vioch->lock, flags);
  140. if (!vioch->cinfo || vioch->shutdown_done) {
  141. spin_unlock_irqrestore(&vioch->lock, flags);
  142. return;
  143. }
  144. vioch->shutdown_done = &vioch_shutdown_done;
  145. if (!vioch->is_rx && vioch->deferred_tx_wq)
  146. /* Cannot be kicked anymore after this...*/
  147. vioch->deferred_tx_wq = NULL;
  148. spin_unlock_irqrestore(&vioch->lock, flags);
  149. scmi_vio_channel_release(vioch);
  150. /* Let any possibly concurrent RX path release the channel */
  151. wait_for_completion(vioch->shutdown_done);
  152. }
  153. /* Assumes to be called with vio channel acquired already */
  154. static struct scmi_vio_msg *
  155. scmi_virtio_get_free_msg(struct scmi_vio_channel *vioch)
  156. {
  157. unsigned long flags;
  158. struct scmi_vio_msg *msg;
  159. spin_lock_irqsave(&vioch->free_lock, flags);
  160. if (list_empty(&vioch->free_list)) {
  161. spin_unlock_irqrestore(&vioch->free_lock, flags);
  162. return NULL;
  163. }
  164. msg = list_first_entry(&vioch->free_list, typeof(*msg), list);
  165. list_del_init(&msg->list);
  166. spin_unlock_irqrestore(&vioch->free_lock, flags);
  167. /* Still no users, no need to acquire poll_lock */
  168. msg->poll_status = VIO_MSG_NOT_POLLED;
  169. refcount_set(&msg->users, 1);
  170. return msg;
  171. }
  172. static inline bool scmi_vio_msg_acquire(struct scmi_vio_msg *msg)
  173. {
  174. return refcount_inc_not_zero(&msg->users);
  175. }
  176. /* Assumes to be called with vio channel acquired already */
  177. static inline bool scmi_vio_msg_release(struct scmi_vio_channel *vioch,
  178. struct scmi_vio_msg *msg)
  179. {
  180. bool ret;
  181. ret = refcount_dec_and_test(&msg->users);
  182. if (ret) {
  183. unsigned long flags;
  184. spin_lock_irqsave(&vioch->free_lock, flags);
  185. list_add_tail(&msg->list, &vioch->free_list);
  186. spin_unlock_irqrestore(&vioch->free_lock, flags);
  187. }
  188. return ret;
  189. }
  190. static bool scmi_vio_have_vq_rx(struct virtio_device *vdev)
  191. {
  192. return virtio_has_feature(vdev, VIRTIO_SCMI_F_P2A_CHANNELS);
  193. }
  194. static int scmi_vio_feed_vq_rx(struct scmi_vio_channel *vioch,
  195. struct scmi_vio_msg *msg)
  196. {
  197. struct scatterlist sg_in;
  198. int rc;
  199. unsigned long flags;
  200. struct device *dev = &vioch->vqueue->vdev->dev;
  201. sg_init_one(&sg_in, msg->input, VIRTIO_SCMI_MAX_PDU_SIZE);
  202. spin_lock_irqsave(&vioch->lock, flags);
  203. rc = virtqueue_add_inbuf(vioch->vqueue, &sg_in, 1, msg, GFP_ATOMIC);
  204. if (rc)
  205. dev_err(dev, "failed to add to RX virtqueue (%d)\n", rc);
  206. else
  207. virtqueue_kick(vioch->vqueue);
  208. spin_unlock_irqrestore(&vioch->lock, flags);
  209. return rc;
  210. }
  211. /*
  212. * Assume to be called with channel already acquired or not ready at all;
  213. * vioch->lock MUST NOT have been already acquired.
  214. */
  215. static void scmi_finalize_message(struct scmi_vio_channel *vioch,
  216. struct scmi_vio_msg *msg)
  217. {
  218. if (vioch->is_rx)
  219. scmi_vio_feed_vq_rx(vioch, msg);
  220. else
  221. scmi_vio_msg_release(vioch, msg);
  222. }
  223. static void scmi_vio_complete_cb(struct virtqueue *vqueue)
  224. {
  225. unsigned long flags;
  226. unsigned int length;
  227. struct scmi_vio_channel *vioch;
  228. struct scmi_vio_msg *msg;
  229. bool cb_enabled = true;
  230. if (WARN_ON_ONCE(!vqueue->vdev->priv))
  231. return;
  232. vioch = &((struct scmi_vio_channel *)vqueue->vdev->priv)[vqueue->index];
  233. for (;;) {
  234. if (!scmi_vio_channel_acquire(vioch))
  235. return;
  236. spin_lock_irqsave(&vioch->lock, flags);
  237. if (cb_enabled) {
  238. virtqueue_disable_cb(vqueue);
  239. cb_enabled = false;
  240. }
  241. msg = virtqueue_get_buf(vqueue, &length);
  242. if (!msg) {
  243. if (virtqueue_enable_cb(vqueue)) {
  244. spin_unlock_irqrestore(&vioch->lock, flags);
  245. scmi_vio_channel_release(vioch);
  246. return;
  247. }
  248. cb_enabled = true;
  249. }
  250. spin_unlock_irqrestore(&vioch->lock, flags);
  251. if (msg) {
  252. msg->rx_len = length;
  253. scmi_rx_callback(vioch->cinfo,
  254. msg_read_header(msg->input), msg);
  255. scmi_finalize_message(vioch, msg);
  256. }
  257. /*
  258. * Release vio channel between loop iterations to allow
  259. * virtio_chan_free() to eventually fully release it when
  260. * shutting down; in such a case, any outstanding message will
  261. * be ignored since this loop will bail out at the next
  262. * iteration.
  263. */
  264. scmi_vio_channel_release(vioch);
  265. }
  266. }
  267. static void scmi_vio_deferred_tx_worker(struct work_struct *work)
  268. {
  269. unsigned long flags;
  270. struct scmi_vio_channel *vioch;
  271. struct scmi_vio_msg *msg, *tmp;
  272. vioch = container_of(work, struct scmi_vio_channel, deferred_tx_work);
  273. if (!scmi_vio_channel_acquire(vioch))
  274. return;
  275. /*
  276. * Process pre-fetched messages: these could be non-polled messages or
  277. * late timed-out replies to polled messages dequeued by chance while
  278. * polling for some other messages: this worker is in charge to process
  279. * the valid non-expired messages and anyway finally free all of them.
  280. */
  281. spin_lock_irqsave(&vioch->pending_lock, flags);
  282. /* Scan the list of possibly pre-fetched messages during polling. */
  283. list_for_each_entry_safe(msg, tmp, &vioch->pending_cmds_list, list) {
  284. list_del(&msg->list);
  285. /*
  286. * Channel is acquired here (cannot vanish) and this message
  287. * is no more processed elsewhere so no poll_lock needed.
  288. */
  289. if (msg->poll_status == VIO_MSG_NOT_POLLED)
  290. scmi_rx_callback(vioch->cinfo,
  291. msg_read_header(msg->input), msg);
  292. /* Free the processed message once done */
  293. scmi_vio_msg_release(vioch, msg);
  294. }
  295. spin_unlock_irqrestore(&vioch->pending_lock, flags);
  296. /* Process possibly still pending messages */
  297. scmi_vio_complete_cb(vioch->vqueue);
  298. scmi_vio_channel_release(vioch);
  299. }
  300. static const char *const scmi_vio_vqueue_names[] = { "tx", "rx" };
  301. static vq_callback_t *scmi_vio_complete_callbacks[] = {
  302. scmi_vio_complete_cb,
  303. scmi_vio_complete_cb
  304. };
  305. static unsigned int virtio_get_max_msg(struct scmi_chan_info *base_cinfo)
  306. {
  307. struct scmi_vio_channel *vioch = base_cinfo->transport_info;
  308. return vioch->max_msg;
  309. }
  310. static int virtio_link_supplier(struct device *dev)
  311. {
  312. if (!scmi_vdev) {
  313. dev_notice(dev,
  314. "Deferring probe after not finding a bound scmi-virtio device\n");
  315. return -EPROBE_DEFER;
  316. }
  317. if (!device_link_add(dev, &scmi_vdev->dev,
  318. DL_FLAG_AUTOREMOVE_CONSUMER)) {
  319. dev_err(dev, "Adding link to supplier virtio device failed\n");
  320. return -ECANCELED;
  321. }
  322. return 0;
  323. }
  324. static bool virtio_chan_available(struct device *dev, int idx)
  325. {
  326. struct scmi_vio_channel *channels, *vioch = NULL;
  327. if (WARN_ON_ONCE(!scmi_vdev))
  328. return false;
  329. channels = (struct scmi_vio_channel *)scmi_vdev->priv;
  330. switch (idx) {
  331. case VIRTIO_SCMI_VQ_TX:
  332. vioch = &channels[VIRTIO_SCMI_VQ_TX];
  333. break;
  334. case VIRTIO_SCMI_VQ_RX:
  335. if (scmi_vio_have_vq_rx(scmi_vdev))
  336. vioch = &channels[VIRTIO_SCMI_VQ_RX];
  337. break;
  338. default:
  339. return false;
  340. }
  341. return vioch && !vioch->cinfo;
  342. }
  343. static void scmi_destroy_tx_workqueue(void *deferred_tx_wq)
  344. {
  345. destroy_workqueue(deferred_tx_wq);
  346. }
  347. static int virtio_chan_setup(struct scmi_chan_info *cinfo, struct device *dev,
  348. bool tx)
  349. {
  350. struct scmi_vio_channel *vioch;
  351. int index = tx ? VIRTIO_SCMI_VQ_TX : VIRTIO_SCMI_VQ_RX;
  352. int i;
  353. if (!scmi_vdev)
  354. return -EPROBE_DEFER;
  355. vioch = &((struct scmi_vio_channel *)scmi_vdev->priv)[index];
  356. /* Setup a deferred worker for polling. */
  357. if (tx && !vioch->deferred_tx_wq) {
  358. int ret;
  359. vioch->deferred_tx_wq =
  360. alloc_workqueue(dev_name(&scmi_vdev->dev),
  361. WQ_UNBOUND | WQ_FREEZABLE | WQ_SYSFS,
  362. 0);
  363. if (!vioch->deferred_tx_wq)
  364. return -ENOMEM;
  365. ret = devm_add_action_or_reset(dev, scmi_destroy_tx_workqueue,
  366. vioch->deferred_tx_wq);
  367. if (ret)
  368. return ret;
  369. INIT_WORK(&vioch->deferred_tx_work,
  370. scmi_vio_deferred_tx_worker);
  371. }
  372. for (i = 0; i < vioch->max_msg; i++) {
  373. struct scmi_vio_msg *msg;
  374. msg = devm_kzalloc(dev, sizeof(*msg), GFP_KERNEL);
  375. if (!msg)
  376. return -ENOMEM;
  377. if (tx) {
  378. msg->request = devm_kzalloc(dev,
  379. VIRTIO_SCMI_MAX_PDU_SIZE,
  380. GFP_KERNEL);
  381. if (!msg->request)
  382. return -ENOMEM;
  383. spin_lock_init(&msg->poll_lock);
  384. refcount_set(&msg->users, 1);
  385. }
  386. msg->input = devm_kzalloc(dev, VIRTIO_SCMI_MAX_PDU_SIZE,
  387. GFP_KERNEL);
  388. if (!msg->input)
  389. return -ENOMEM;
  390. scmi_finalize_message(vioch, msg);
  391. }
  392. scmi_vio_channel_ready(vioch, cinfo);
  393. return 0;
  394. }
  395. static int virtio_chan_free(int id, void *p, void *data)
  396. {
  397. struct scmi_chan_info *cinfo = p;
  398. struct scmi_vio_channel *vioch = cinfo->transport_info;
  399. /*
  400. * Break device to inhibit further traffic flowing while shutting down
  401. * the channels: doing it later holding vioch->lock creates unsafe
  402. * locking dependency chains as reported by LOCKDEP.
  403. */
  404. virtio_break_device(vioch->vqueue->vdev);
  405. scmi_vio_channel_cleanup_sync(vioch);
  406. scmi_free_channel(cinfo, data, id);
  407. return 0;
  408. }
  409. static int virtio_send_message(struct scmi_chan_info *cinfo,
  410. struct scmi_xfer *xfer)
  411. {
  412. struct scmi_vio_channel *vioch = cinfo->transport_info;
  413. struct scatterlist sg_out;
  414. struct scatterlist sg_in;
  415. struct scatterlist *sgs[DESCRIPTORS_PER_TX_MSG] = { &sg_out, &sg_in };
  416. unsigned long flags;
  417. int rc;
  418. struct scmi_vio_msg *msg;
  419. if (!scmi_vio_channel_acquire(vioch))
  420. return -EINVAL;
  421. msg = scmi_virtio_get_free_msg(vioch);
  422. if (!msg) {
  423. scmi_vio_channel_release(vioch);
  424. return -EBUSY;
  425. }
  426. msg_tx_prepare(msg->request, xfer);
  427. sg_init_one(&sg_out, msg->request, msg_command_size(xfer));
  428. sg_init_one(&sg_in, msg->input, msg_response_size(xfer));
  429. spin_lock_irqsave(&vioch->lock, flags);
  430. /*
  431. * If polling was requested for this transaction:
  432. * - retrieve last used index (will be used as polling reference)
  433. * - bind the polled message to the xfer via .priv
  434. * - grab an additional msg refcount for the poll-path
  435. */
  436. if (xfer->hdr.poll_completion) {
  437. msg->poll_idx = virtqueue_enable_cb_prepare(vioch->vqueue);
  438. /* Still no users, no need to acquire poll_lock */
  439. msg->poll_status = VIO_MSG_POLLING;
  440. scmi_vio_msg_acquire(msg);
  441. /* Ensure initialized msg is visibly bound to xfer */
  442. smp_store_mb(xfer->priv, msg);
  443. }
  444. rc = virtqueue_add_sgs(vioch->vqueue, sgs, 1, 1, msg, GFP_ATOMIC);
  445. if (rc)
  446. dev_err(vioch->cinfo->dev,
  447. "failed to add to TX virtqueue (%d)\n", rc);
  448. else
  449. virtqueue_kick(vioch->vqueue);
  450. spin_unlock_irqrestore(&vioch->lock, flags);
  451. if (rc) {
  452. /* Ensure order between xfer->priv clear and vq feeding */
  453. smp_store_mb(xfer->priv, NULL);
  454. if (xfer->hdr.poll_completion)
  455. scmi_vio_msg_release(vioch, msg);
  456. scmi_vio_msg_release(vioch, msg);
  457. }
  458. scmi_vio_channel_release(vioch);
  459. return rc;
  460. }
  461. static void virtio_fetch_response(struct scmi_chan_info *cinfo,
  462. struct scmi_xfer *xfer)
  463. {
  464. struct scmi_vio_msg *msg = xfer->priv;
  465. if (msg)
  466. msg_fetch_response(msg->input, msg->rx_len, xfer);
  467. }
  468. static void virtio_fetch_notification(struct scmi_chan_info *cinfo,
  469. size_t max_len, struct scmi_xfer *xfer)
  470. {
  471. struct scmi_vio_msg *msg = xfer->priv;
  472. if (msg)
  473. msg_fetch_notification(msg->input, msg->rx_len, max_len, xfer);
  474. }
  475. /**
  476. * virtio_mark_txdone - Mark transmission done
  477. *
  478. * Free only completed polling transfer messages.
  479. *
  480. * Note that in the SCMI VirtIO transport we never explicitly release still
  481. * outstanding but timed-out messages by forcibly re-adding them to the
  482. * free-list inside the TX code path; we instead let IRQ/RX callbacks, or the
  483. * TX deferred worker, eventually clean up such messages once, finally, a late
  484. * reply is received and discarded (if ever).
  485. *
  486. * This approach was deemed preferable since those pending timed-out buffers are
  487. * still effectively owned by the SCMI platform VirtIO device even after timeout
  488. * expiration: forcibly freeing and reusing them before they had been returned
  489. * explicitly by the SCMI platform could lead to subtle bugs due to message
  490. * corruption.
  491. * An SCMI platform VirtIO device which never returns message buffers is
  492. * anyway broken and it will quickly lead to exhaustion of available messages.
  493. *
  494. * For this same reason, here, we take care to free only the polled messages
  495. * that had been somehow replied (only if not by chance already processed on the
  496. * IRQ path - the initial scmi_vio_msg_release() takes care of this) and also
  497. * any timed-out polled message if that indeed appears to have been at least
  498. * dequeued from the virtqueues (VIO_MSG_POLL_DONE): this is needed since such
  499. * messages won't be freed elsewhere. Any other polled message is marked as
  500. * VIO_MSG_POLL_TIMEOUT.
  501. *
  502. * Possible late replies to timed-out polled messages will be eventually freed
  503. * by RX callbacks if delivered on the IRQ path or by the deferred TX worker if
  504. * dequeued on some other polling path.
  505. *
  506. * @cinfo: SCMI channel info
  507. * @ret: Transmission return code
  508. * @xfer: Transfer descriptor
  509. */
  510. static void virtio_mark_txdone(struct scmi_chan_info *cinfo, int ret,
  511. struct scmi_xfer *xfer)
  512. {
  513. unsigned long flags;
  514. struct scmi_vio_channel *vioch = cinfo->transport_info;
  515. struct scmi_vio_msg *msg = xfer->priv;
  516. if (!msg || !scmi_vio_channel_acquire(vioch))
  517. return;
  518. /* Ensure msg is unbound from xfer anyway at this point */
  519. smp_store_mb(xfer->priv, NULL);
  520. /* Must be a polled xfer and not already freed on the IRQ path */
  521. if (!xfer->hdr.poll_completion || scmi_vio_msg_release(vioch, msg)) {
  522. scmi_vio_channel_release(vioch);
  523. return;
  524. }
  525. spin_lock_irqsave(&msg->poll_lock, flags);
  526. /* Do not free timedout polled messages only if still inflight */
  527. if (ret != -ETIMEDOUT || msg->poll_status == VIO_MSG_POLL_DONE)
  528. scmi_vio_msg_release(vioch, msg);
  529. else if (msg->poll_status == VIO_MSG_POLLING)
  530. msg->poll_status = VIO_MSG_POLL_TIMEOUT;
  531. spin_unlock_irqrestore(&msg->poll_lock, flags);
  532. scmi_vio_channel_release(vioch);
  533. }
  534. /**
  535. * virtio_poll_done - Provide polling support for VirtIO transport
  536. *
  537. * @cinfo: SCMI channel info
  538. * @xfer: Reference to the transfer being poll for.
  539. *
  540. * VirtIO core provides a polling mechanism based only on last used indexes:
  541. * this means that it is possible to poll the virtqueues waiting for something
  542. * new to arrive from the host side, but the only way to check if the freshly
  543. * arrived buffer was indeed what we were waiting for is to compare the newly
  544. * arrived message descriptor with the one we are polling on.
  545. *
  546. * As a consequence it can happen to dequeue something different from the buffer
  547. * we were poll-waiting for: if that is the case such early fetched buffers are
  548. * then added to a the @pending_cmds_list list for later processing by a
  549. * dedicated deferred worker.
  550. *
  551. * So, basically, once something new is spotted we proceed to de-queue all the
  552. * freshly received used buffers until we found the one we were polling on, or,
  553. * we have 'seemingly' emptied the virtqueue; if some buffers are still pending
  554. * in the vqueue at the end of the polling loop (possible due to inherent races
  555. * in virtqueues handling mechanisms), we similarly kick the deferred worker
  556. * and let it process those, to avoid indefinitely looping in the .poll_done
  557. * busy-waiting helper.
  558. *
  559. * Finally, we delegate to the deferred worker also the final free of any timed
  560. * out reply to a polled message that we should dequeue.
  561. *
  562. * Note that, since we do NOT have per-message suppress notification mechanism,
  563. * the message we are polling for could be alternatively delivered via usual
  564. * IRQs callbacks on another core which happened to have IRQs enabled while we
  565. * are actively polling for it here: in such a case it will be handled as such
  566. * by scmi_rx_callback() and the polling loop in the SCMI Core TX path will be
  567. * transparently terminated anyway.
  568. *
  569. * Return: True once polling has successfully completed.
  570. */
  571. static bool virtio_poll_done(struct scmi_chan_info *cinfo,
  572. struct scmi_xfer *xfer)
  573. {
  574. bool pending, found = false;
  575. unsigned int length, any_prefetched = 0;
  576. unsigned long flags;
  577. struct scmi_vio_msg *next_msg, *msg = xfer->priv;
  578. struct scmi_vio_channel *vioch = cinfo->transport_info;
  579. if (!msg)
  580. return true;
  581. /*
  582. * Processed already by other polling loop on another CPU ?
  583. *
  584. * Note that this message is acquired on the poll path so cannot vanish
  585. * while inside this loop iteration even if concurrently processed on
  586. * the IRQ path.
  587. *
  588. * Avoid to acquire poll_lock since polled_status can be changed
  589. * in a relevant manner only later in this same thread of execution:
  590. * any other possible changes made concurrently by other polling loops
  591. * or by a reply delivered on the IRQ path have no meaningful impact on
  592. * this loop iteration: in other words it is harmless to allow this
  593. * possible race but let has avoid spinlocking with irqs off in this
  594. * initial part of the polling loop.
  595. */
  596. if (msg->poll_status == VIO_MSG_POLL_DONE)
  597. return true;
  598. if (!scmi_vio_channel_acquire(vioch))
  599. return true;
  600. /* Has cmdq index moved at all ? */
  601. pending = virtqueue_poll(vioch->vqueue, msg->poll_idx);
  602. if (!pending) {
  603. scmi_vio_channel_release(vioch);
  604. return false;
  605. }
  606. spin_lock_irqsave(&vioch->lock, flags);
  607. virtqueue_disable_cb(vioch->vqueue);
  608. /*
  609. * Process all new messages till the polled-for message is found OR
  610. * the vqueue is empty.
  611. */
  612. while ((next_msg = virtqueue_get_buf(vioch->vqueue, &length))) {
  613. bool next_msg_done = false;
  614. /*
  615. * Mark any dequeued buffer message as VIO_MSG_POLL_DONE so
  616. * that can be properly freed even on timeout in mark_txdone.
  617. */
  618. spin_lock(&next_msg->poll_lock);
  619. if (next_msg->poll_status == VIO_MSG_POLLING) {
  620. next_msg->poll_status = VIO_MSG_POLL_DONE;
  621. next_msg_done = true;
  622. }
  623. spin_unlock(&next_msg->poll_lock);
  624. next_msg->rx_len = length;
  625. /* Is the message we were polling for ? */
  626. if (next_msg == msg) {
  627. found = true;
  628. break;
  629. } else if (next_msg_done) {
  630. /* Skip the rest if this was another polled msg */
  631. continue;
  632. }
  633. /*
  634. * Enqueue for later processing any non-polled message and any
  635. * timed-out polled one that we happen to have dequeued.
  636. */
  637. spin_lock(&next_msg->poll_lock);
  638. if (next_msg->poll_status == VIO_MSG_NOT_POLLED ||
  639. next_msg->poll_status == VIO_MSG_POLL_TIMEOUT) {
  640. spin_unlock(&next_msg->poll_lock);
  641. any_prefetched++;
  642. spin_lock(&vioch->pending_lock);
  643. list_add_tail(&next_msg->list,
  644. &vioch->pending_cmds_list);
  645. spin_unlock(&vioch->pending_lock);
  646. } else {
  647. spin_unlock(&next_msg->poll_lock);
  648. }
  649. }
  650. /*
  651. * When the polling loop has successfully terminated if something
  652. * else was queued in the meantime, it will be served by a deferred
  653. * worker OR by the normal IRQ/callback OR by other poll loops.
  654. *
  655. * If we are still looking for the polled reply, the polling index has
  656. * to be updated to the current vqueue last used index.
  657. */
  658. if (found) {
  659. pending = !virtqueue_enable_cb(vioch->vqueue);
  660. } else {
  661. msg->poll_idx = virtqueue_enable_cb_prepare(vioch->vqueue);
  662. pending = virtqueue_poll(vioch->vqueue, msg->poll_idx);
  663. }
  664. if (vioch->deferred_tx_wq && (any_prefetched || pending))
  665. queue_work(vioch->deferred_tx_wq, &vioch->deferred_tx_work);
  666. spin_unlock_irqrestore(&vioch->lock, flags);
  667. scmi_vio_channel_release(vioch);
  668. return found;
  669. }
  670. static const struct scmi_transport_ops scmi_virtio_ops = {
  671. .link_supplier = virtio_link_supplier,
  672. .chan_available = virtio_chan_available,
  673. .chan_setup = virtio_chan_setup,
  674. .chan_free = virtio_chan_free,
  675. .get_max_msg = virtio_get_max_msg,
  676. .send_message = virtio_send_message,
  677. .fetch_response = virtio_fetch_response,
  678. .fetch_notification = virtio_fetch_notification,
  679. .mark_txdone = virtio_mark_txdone,
  680. .poll_done = virtio_poll_done,
  681. };
  682. static int scmi_vio_probe(struct virtio_device *vdev)
  683. {
  684. struct device *dev = &vdev->dev;
  685. struct scmi_vio_channel *channels;
  686. bool have_vq_rx;
  687. int vq_cnt;
  688. int i;
  689. int ret;
  690. struct virtqueue *vqs[VIRTIO_SCMI_VQ_MAX_CNT];
  691. /* Only one SCMI VirtiO device allowed */
  692. if (scmi_vdev) {
  693. dev_err(dev,
  694. "One SCMI Virtio device was already initialized: only one allowed.\n");
  695. return -EBUSY;
  696. }
  697. have_vq_rx = scmi_vio_have_vq_rx(vdev);
  698. vq_cnt = have_vq_rx ? VIRTIO_SCMI_VQ_MAX_CNT : 1;
  699. channels = devm_kcalloc(dev, vq_cnt, sizeof(*channels), GFP_KERNEL);
  700. if (!channels)
  701. return -ENOMEM;
  702. if (have_vq_rx)
  703. channels[VIRTIO_SCMI_VQ_RX].is_rx = true;
  704. ret = virtio_find_vqs(vdev, vq_cnt, vqs, scmi_vio_complete_callbacks,
  705. scmi_vio_vqueue_names, NULL);
  706. if (ret) {
  707. dev_err(dev, "Failed to get %d virtqueue(s)\n", vq_cnt);
  708. return ret;
  709. }
  710. for (i = 0; i < vq_cnt; i++) {
  711. unsigned int sz;
  712. spin_lock_init(&channels[i].lock);
  713. spin_lock_init(&channels[i].free_lock);
  714. INIT_LIST_HEAD(&channels[i].free_list);
  715. spin_lock_init(&channels[i].pending_lock);
  716. INIT_LIST_HEAD(&channels[i].pending_cmds_list);
  717. channels[i].vqueue = vqs[i];
  718. sz = virtqueue_get_vring_size(channels[i].vqueue);
  719. /* Tx messages need multiple descriptors. */
  720. if (!channels[i].is_rx)
  721. sz /= DESCRIPTORS_PER_TX_MSG;
  722. if (sz > MSG_TOKEN_MAX) {
  723. dev_info(dev,
  724. "%s virtqueue could hold %d messages. Only %ld allowed to be pending.\n",
  725. channels[i].is_rx ? "rx" : "tx",
  726. sz, MSG_TOKEN_MAX);
  727. sz = MSG_TOKEN_MAX;
  728. }
  729. channels[i].max_msg = sz;
  730. }
  731. vdev->priv = channels;
  732. /* Ensure initialized scmi_vdev is visible */
  733. smp_store_mb(scmi_vdev, vdev);
  734. return 0;
  735. }
  736. static void scmi_vio_remove(struct virtio_device *vdev)
  737. {
  738. /*
  739. * Once we get here, virtio_chan_free() will have already been called by
  740. * the SCMI core for any existing channel and, as a consequence, all the
  741. * virtio channels will have been already marked NOT ready, causing any
  742. * outstanding message on any vqueue to be ignored by complete_cb: now
  743. * we can just stop processing buffers and destroy the vqueues.
  744. */
  745. virtio_reset_device(vdev);
  746. vdev->config->del_vqs(vdev);
  747. /* Ensure scmi_vdev is visible as NULL */
  748. smp_store_mb(scmi_vdev, NULL);
  749. }
  750. static int scmi_vio_validate(struct virtio_device *vdev)
  751. {
  752. #ifdef CONFIG_ARM_SCMI_TRANSPORT_VIRTIO_VERSION1_COMPLIANCE
  753. if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
  754. dev_err(&vdev->dev,
  755. "device does not comply with spec version 1.x\n");
  756. return -EINVAL;
  757. }
  758. #endif
  759. return 0;
  760. }
  761. static unsigned int features[] = {
  762. VIRTIO_SCMI_F_P2A_CHANNELS,
  763. };
  764. static const struct virtio_device_id id_table[] = {
  765. { VIRTIO_ID_SCMI, VIRTIO_DEV_ANY_ID },
  766. { 0 }
  767. };
  768. static struct virtio_driver virtio_scmi_driver = {
  769. .driver.name = "scmi-virtio",
  770. .driver.owner = THIS_MODULE,
  771. .feature_table = features,
  772. .feature_table_size = ARRAY_SIZE(features),
  773. .id_table = id_table,
  774. .probe = scmi_vio_probe,
  775. .remove = scmi_vio_remove,
  776. .validate = scmi_vio_validate,
  777. };
  778. static int __init virtio_scmi_init(void)
  779. {
  780. return register_virtio_driver(&virtio_scmi_driver);
  781. }
  782. static void virtio_scmi_exit(void)
  783. {
  784. unregister_virtio_driver(&virtio_scmi_driver);
  785. }
  786. const struct scmi_desc scmi_virtio_desc = {
  787. .transport_init = virtio_scmi_init,
  788. .transport_exit = virtio_scmi_exit,
  789. .ops = &scmi_virtio_ops,
  790. /* for non-realtime virtio devices */
  791. .max_rx_timeout_ms = VIRTIO_MAX_RX_TIMEOUT_MS,
  792. .max_msg = 0, /* overridden by virtio_get_max_msg() */
  793. .max_msg_size = VIRTIO_SCMI_MAX_MSG_SIZE,
  794. .atomic_enabled = IS_ENABLED(CONFIG_ARM_SCMI_TRANSPORT_VIRTIO_ATOMIC_ENABLE),
  795. };