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