1 // SPDX-License-Identifier: GPL-2.0
3 * Virtio Transport driver for Arm System Control and Management Interface
6 * Copyright (C) 2020-2021 OpenSynergy.
7 * Copyright (C) 2021 ARM Ltd.
11 * DOC: Theory of Operation
13 * The scmi-virtio transport implements a driver for the virtio SCMI device.
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.
20 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/virtio.h>
23 #include <linux/virtio_config.h>
25 #include <uapi/linux/virtio_ids.h>
26 #include <uapi/linux/virtio_scmi.h>
30 #define VIRTIO_SCMI_MAX_MSG_SIZE 128 /* Value may be increased. */
31 #define VIRTIO_SCMI_MAX_PDU_SIZE \
32 (VIRTIO_SCMI_MAX_MSG_SIZE + SCMI_MSG_MAX_PROT_OVERHEAD)
33 #define DESCRIPTORS_PER_TX_MSG 2
36 * struct scmi_vio_channel - Transport channel information
38 * @vqueue: Associated virtqueue
39 * @cinfo: SCMI Tx or Rx channel
40 * @free_list: List of unused scmi_vio_msg, maintained for Tx channels only
41 * @is_rx: Whether channel is an Rx channel
42 * @ready: Whether transport user is ready to hear about channel
43 * @max_msg: Maximum number of pending messages for this channel.
44 * @lock: Protects access to all members except ready.
45 * @ready_lock: Protects access to ready. If required, it must be taken before
48 struct scmi_vio_channel {
49 struct virtqueue *vqueue;
50 struct scmi_chan_info *cinfo;
51 struct list_head free_list;
55 /* lock to protect access to all members except ready. */
57 /* lock to rotects access to ready flag. */
58 spinlock_t ready_lock;
62 * struct scmi_vio_msg - Transport PDU information
64 * @request: SDU used for commands
65 * @input: SDU used for (delayed) responses and notifications
66 * @list: List which scmi_vio_msg may be part of
67 * @rx_len: Input SDU size in bytes, once input has been received
70 struct scmi_msg_payld *request;
71 struct scmi_msg_payld *input;
72 struct list_head list;
76 /* Only one SCMI VirtIO device can possibly exist */
77 static struct virtio_device *scmi_vdev;
79 static bool scmi_vio_have_vq_rx(struct virtio_device *vdev)
81 return virtio_has_feature(vdev, VIRTIO_SCMI_F_P2A_CHANNELS);
84 static int scmi_vio_feed_vq_rx(struct scmi_vio_channel *vioch,
85 struct scmi_vio_msg *msg)
87 struct scatterlist sg_in;
91 sg_init_one(&sg_in, msg->input, VIRTIO_SCMI_MAX_PDU_SIZE);
93 spin_lock_irqsave(&vioch->lock, flags);
95 rc = virtqueue_add_inbuf(vioch->vqueue, &sg_in, 1, msg, GFP_ATOMIC);
97 dev_err_once(vioch->cinfo->dev,
98 "failed to add to virtqueue (%d)\n", rc);
100 virtqueue_kick(vioch->vqueue);
102 spin_unlock_irqrestore(&vioch->lock, flags);
107 static void scmi_finalize_message(struct scmi_vio_channel *vioch,
108 struct scmi_vio_msg *msg)
111 scmi_vio_feed_vq_rx(vioch, msg);
115 spin_lock_irqsave(&vioch->lock, flags);
116 list_add(&msg->list, &vioch->free_list);
117 spin_unlock_irqrestore(&vioch->lock, flags);
121 static void scmi_vio_complete_cb(struct virtqueue *vqueue)
123 unsigned long ready_flags;
126 struct scmi_vio_channel *vioch;
127 struct scmi_vio_msg *msg;
128 bool cb_enabled = true;
130 if (WARN_ON_ONCE(!vqueue->vdev->priv))
132 vioch = &((struct scmi_vio_channel *)vqueue->vdev->priv)[vqueue->index];
135 spin_lock_irqsave(&vioch->ready_lock, ready_flags);
139 (void)virtqueue_enable_cb(vqueue);
140 goto unlock_ready_out;
143 spin_lock_irqsave(&vioch->lock, flags);
145 virtqueue_disable_cb(vqueue);
148 msg = virtqueue_get_buf(vqueue, &length);
150 if (virtqueue_enable_cb(vqueue))
154 spin_unlock_irqrestore(&vioch->lock, flags);
157 msg->rx_len = length;
158 scmi_rx_callback(vioch->cinfo,
159 msg_read_header(msg->input), msg);
161 scmi_finalize_message(vioch, msg);
164 spin_unlock_irqrestore(&vioch->ready_lock, ready_flags);
168 spin_unlock_irqrestore(&vioch->lock, flags);
170 spin_unlock_irqrestore(&vioch->ready_lock, ready_flags);
173 static const char *const scmi_vio_vqueue_names[] = { "tx", "rx" };
175 static vq_callback_t *scmi_vio_complete_callbacks[] = {
176 scmi_vio_complete_cb,
180 static unsigned int virtio_get_max_msg(struct scmi_chan_info *base_cinfo)
182 struct scmi_vio_channel *vioch = base_cinfo->transport_info;
184 return vioch->max_msg;
187 static int virtio_link_supplier(struct device *dev)
191 "Deferring probe after not finding a bound scmi-virtio device\n");
192 return -EPROBE_DEFER;
195 if (!device_link_add(dev, &scmi_vdev->dev,
196 DL_FLAG_AUTOREMOVE_CONSUMER)) {
197 dev_err(dev, "Adding link to supplier virtio device failed\n");
204 static bool virtio_chan_available(struct device *dev, int idx)
206 struct scmi_vio_channel *channels, *vioch = NULL;
208 if (WARN_ON_ONCE(!scmi_vdev))
211 channels = (struct scmi_vio_channel *)scmi_vdev->priv;
214 case VIRTIO_SCMI_VQ_TX:
215 vioch = &channels[VIRTIO_SCMI_VQ_TX];
217 case VIRTIO_SCMI_VQ_RX:
218 if (scmi_vio_have_vq_rx(scmi_vdev))
219 vioch = &channels[VIRTIO_SCMI_VQ_RX];
225 return vioch && !vioch->cinfo;
228 static int virtio_chan_setup(struct scmi_chan_info *cinfo, struct device *dev,
232 struct scmi_vio_channel *vioch;
233 int index = tx ? VIRTIO_SCMI_VQ_TX : VIRTIO_SCMI_VQ_RX;
237 return -EPROBE_DEFER;
239 vioch = &((struct scmi_vio_channel *)scmi_vdev->priv)[index];
241 for (i = 0; i < vioch->max_msg; i++) {
242 struct scmi_vio_msg *msg;
244 msg = devm_kzalloc(cinfo->dev, sizeof(*msg), GFP_KERNEL);
249 msg->request = devm_kzalloc(cinfo->dev,
250 VIRTIO_SCMI_MAX_PDU_SIZE,
256 msg->input = devm_kzalloc(cinfo->dev, VIRTIO_SCMI_MAX_PDU_SIZE,
262 spin_lock_irqsave(&vioch->lock, flags);
263 list_add_tail(&msg->list, &vioch->free_list);
264 spin_unlock_irqrestore(&vioch->lock, flags);
266 scmi_vio_feed_vq_rx(vioch, msg);
270 spin_lock_irqsave(&vioch->lock, flags);
271 cinfo->transport_info = vioch;
272 /* Indirectly setting channel not available any more */
273 vioch->cinfo = cinfo;
274 spin_unlock_irqrestore(&vioch->lock, flags);
276 spin_lock_irqsave(&vioch->ready_lock, flags);
278 spin_unlock_irqrestore(&vioch->ready_lock, flags);
283 static int virtio_chan_free(int id, void *p, void *data)
286 struct scmi_chan_info *cinfo = p;
287 struct scmi_vio_channel *vioch = cinfo->transport_info;
289 spin_lock_irqsave(&vioch->ready_lock, flags);
290 vioch->ready = false;
291 spin_unlock_irqrestore(&vioch->ready_lock, flags);
293 scmi_free_channel(cinfo, data, id);
295 spin_lock_irqsave(&vioch->lock, flags);
297 spin_unlock_irqrestore(&vioch->lock, flags);
302 static int virtio_send_message(struct scmi_chan_info *cinfo,
303 struct scmi_xfer *xfer)
305 struct scmi_vio_channel *vioch = cinfo->transport_info;
306 struct scatterlist sg_out;
307 struct scatterlist sg_in;
308 struct scatterlist *sgs[DESCRIPTORS_PER_TX_MSG] = { &sg_out, &sg_in };
311 struct scmi_vio_msg *msg;
313 spin_lock_irqsave(&vioch->lock, flags);
315 if (list_empty(&vioch->free_list)) {
316 spin_unlock_irqrestore(&vioch->lock, flags);
320 msg = list_first_entry(&vioch->free_list, typeof(*msg), list);
321 list_del(&msg->list);
323 msg_tx_prepare(msg->request, xfer);
325 sg_init_one(&sg_out, msg->request, msg_command_size(xfer));
326 sg_init_one(&sg_in, msg->input, msg_response_size(xfer));
328 rc = virtqueue_add_sgs(vioch->vqueue, sgs, 1, 1, msg, GFP_ATOMIC);
330 list_add(&msg->list, &vioch->free_list);
331 dev_err_once(vioch->cinfo->dev,
332 "%s() failed to add to virtqueue (%d)\n", __func__,
335 virtqueue_kick(vioch->vqueue);
338 spin_unlock_irqrestore(&vioch->lock, flags);
343 static void virtio_fetch_response(struct scmi_chan_info *cinfo,
344 struct scmi_xfer *xfer)
346 struct scmi_vio_msg *msg = xfer->priv;
349 msg_fetch_response(msg->input, msg->rx_len, xfer);
354 static void virtio_fetch_notification(struct scmi_chan_info *cinfo,
355 size_t max_len, struct scmi_xfer *xfer)
357 struct scmi_vio_msg *msg = xfer->priv;
360 msg_fetch_notification(msg->input, msg->rx_len, max_len, xfer);
365 static const struct scmi_transport_ops scmi_virtio_ops = {
366 .link_supplier = virtio_link_supplier,
367 .chan_available = virtio_chan_available,
368 .chan_setup = virtio_chan_setup,
369 .chan_free = virtio_chan_free,
370 .get_max_msg = virtio_get_max_msg,
371 .send_message = virtio_send_message,
372 .fetch_response = virtio_fetch_response,
373 .fetch_notification = virtio_fetch_notification,
376 static int scmi_vio_probe(struct virtio_device *vdev)
378 struct device *dev = &vdev->dev;
379 struct scmi_vio_channel *channels;
384 struct virtqueue *vqs[VIRTIO_SCMI_VQ_MAX_CNT];
386 /* Only one SCMI VirtiO device allowed */
390 have_vq_rx = scmi_vio_have_vq_rx(vdev);
391 vq_cnt = have_vq_rx ? VIRTIO_SCMI_VQ_MAX_CNT : 1;
393 channels = devm_kcalloc(dev, vq_cnt, sizeof(*channels), GFP_KERNEL);
398 channels[VIRTIO_SCMI_VQ_RX].is_rx = true;
400 ret = virtio_find_vqs(vdev, vq_cnt, vqs, scmi_vio_complete_callbacks,
401 scmi_vio_vqueue_names, NULL);
403 dev_err(dev, "Failed to get %d virtqueue(s)\n", vq_cnt);
407 for (i = 0; i < vq_cnt; i++) {
410 spin_lock_init(&channels[i].lock);
411 spin_lock_init(&channels[i].ready_lock);
412 INIT_LIST_HEAD(&channels[i].free_list);
413 channels[i].vqueue = vqs[i];
415 sz = virtqueue_get_vring_size(channels[i].vqueue);
416 /* Tx messages need multiple descriptors. */
417 if (!channels[i].is_rx)
418 sz /= DESCRIPTORS_PER_TX_MSG;
420 if (sz > MSG_TOKEN_MAX) {
422 "%s virtqueue could hold %d messages. Only %ld allowed to be pending.\n",
423 channels[i].is_rx ? "rx" : "tx",
427 channels[i].max_msg = sz;
430 vdev->priv = channels;
436 static void scmi_vio_remove(struct virtio_device *vdev)
438 vdev->config->reset(vdev);
439 vdev->config->del_vqs(vdev);
443 static int scmi_vio_validate(struct virtio_device *vdev)
445 if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
447 "device does not comply with spec version 1.x\n");
454 static unsigned int features[] = {
455 VIRTIO_SCMI_F_P2A_CHANNELS,
458 static const struct virtio_device_id id_table[] = {
459 { VIRTIO_ID_SCMI, VIRTIO_DEV_ANY_ID },
463 static struct virtio_driver virtio_scmi_driver = {
464 .driver.name = "scmi-virtio",
465 .driver.owner = THIS_MODULE,
466 .feature_table = features,
467 .feature_table_size = ARRAY_SIZE(features),
468 .id_table = id_table,
469 .probe = scmi_vio_probe,
470 .remove = scmi_vio_remove,
471 .validate = scmi_vio_validate,
474 static int __init virtio_scmi_init(void)
476 return register_virtio_driver(&virtio_scmi_driver);
479 static void __exit virtio_scmi_exit(void)
481 unregister_virtio_driver(&virtio_scmi_driver);
484 const struct scmi_desc scmi_virtio_desc = {
485 .transport_init = virtio_scmi_init,
486 .transport_exit = virtio_scmi_exit,
487 .ops = &scmi_virtio_ops,
488 .max_rx_timeout_ms = 60000, /* for non-realtime virtio devices */
489 .max_msg = 0, /* overridden by virtio_get_max_msg() */
490 .max_msg_size = VIRTIO_SCMI_MAX_MSG_SIZE,