Commit 9a1699bd authored by Cristian Marussi's avatar Cristian Marussi Committed by Sudeep Holla

firmware: arm_scmi: Review virtio free_list handling

Add a new spinlock dedicated to the access of the TX free list and a couple
of helpers to get and put messages back and forth from the free_list.

Link: https://lore.kernel.org/r/20220217131234.50328-3-cristian.marussi@arm.com
Cc: "Michael S. Tsirkin" <mst@redhat.com>
Cc: Igor Skalkin <igor.skalkin@opensynergy.com>
Cc: Peter Hilber <peter.hilber@opensynergy.com>
Cc: virtualization@lists.linux-foundation.org
Signed-off-by: default avatarCristian Marussi <cristian.marussi@arm.com>
Signed-off-by: default avatarSudeep Holla <sudeep.holla@arm.com>
parent 42e90eb5
...@@ -40,20 +40,23 @@ ...@@ -40,20 +40,23 @@
* *
* @vqueue: Associated virtqueue * @vqueue: Associated virtqueue
* @cinfo: SCMI Tx or Rx channel * @cinfo: SCMI Tx or Rx channel
* @free_lock: Protects access to the @free_list.
* @free_list: List of unused scmi_vio_msg, maintained for Tx channels only * @free_list: List of unused scmi_vio_msg, maintained for Tx channels only
* @is_rx: Whether channel is an Rx channel * @is_rx: Whether channel is an Rx channel
* @max_msg: Maximum number of pending messages for this channel. * @max_msg: Maximum number of pending messages for this channel.
* @lock: Protects access to all members except users. * @lock: Protects access to all members except users, free_list.
* @shutdown_done: A reference to a completion used when freeing this channel. * @shutdown_done: A reference to a completion used when freeing this channel.
* @users: A reference count to currently active users of this channel. * @users: A reference count to currently active users of this channel.
*/ */
struct scmi_vio_channel { struct scmi_vio_channel {
struct virtqueue *vqueue; struct virtqueue *vqueue;
struct scmi_chan_info *cinfo; struct scmi_chan_info *cinfo;
/* lock to protect access to the free list. */
spinlock_t free_lock;
struct list_head free_list; struct list_head free_list;
bool is_rx; bool is_rx;
unsigned int max_msg; unsigned int max_msg;
/* lock to protect access to all members except users. */ /* lock to protect access to all members except users, free_list */
spinlock_t lock; spinlock_t lock;
struct completion *shutdown_done; struct completion *shutdown_done;
refcount_t users; refcount_t users;
...@@ -134,18 +137,49 @@ static void scmi_vio_channel_cleanup_sync(struct scmi_vio_channel *vioch) ...@@ -134,18 +137,49 @@ static void scmi_vio_channel_cleanup_sync(struct scmi_vio_channel *vioch)
wait_for_completion(vioch->shutdown_done); wait_for_completion(vioch->shutdown_done);
} }
/* Assumes to be called with vio channel acquired already */
static struct scmi_vio_msg *
scmi_virtio_get_free_msg(struct scmi_vio_channel *vioch)
{
unsigned long flags;
struct scmi_vio_msg *msg;
spin_lock_irqsave(&vioch->free_lock, flags);
if (list_empty(&vioch->free_list)) {
spin_unlock_irqrestore(&vioch->free_lock, flags);
return NULL;
}
msg = list_first_entry(&vioch->free_list, typeof(*msg), list);
list_del_init(&msg->list);
spin_unlock_irqrestore(&vioch->free_lock, flags);
return msg;
}
/* Assumes to be called with vio channel acquired already */
static void scmi_virtio_put_free_msg(struct scmi_vio_channel *vioch,
struct scmi_vio_msg *msg)
{
unsigned long flags;
spin_lock_irqsave(&vioch->free_lock, flags);
list_add_tail(&msg->list, &vioch->free_list);
spin_unlock_irqrestore(&vioch->free_lock, flags);
}
static bool scmi_vio_have_vq_rx(struct virtio_device *vdev) static bool scmi_vio_have_vq_rx(struct virtio_device *vdev)
{ {
return virtio_has_feature(vdev, VIRTIO_SCMI_F_P2A_CHANNELS); return virtio_has_feature(vdev, VIRTIO_SCMI_F_P2A_CHANNELS);
} }
static int scmi_vio_feed_vq_rx(struct scmi_vio_channel *vioch, static int scmi_vio_feed_vq_rx(struct scmi_vio_channel *vioch,
struct scmi_vio_msg *msg, struct scmi_vio_msg *msg)
struct device *dev)
{ {
struct scatterlist sg_in; struct scatterlist sg_in;
int rc; int rc;
unsigned long flags; unsigned long flags;
struct device *dev = &vioch->vqueue->vdev->dev;
sg_init_one(&sg_in, msg->input, VIRTIO_SCMI_MAX_PDU_SIZE); sg_init_one(&sg_in, msg->input, VIRTIO_SCMI_MAX_PDU_SIZE);
...@@ -162,17 +196,17 @@ static int scmi_vio_feed_vq_rx(struct scmi_vio_channel *vioch, ...@@ -162,17 +196,17 @@ static int scmi_vio_feed_vq_rx(struct scmi_vio_channel *vioch,
return rc; return rc;
} }
/*
* Assume to be called with channel already acquired or not ready at all;
* vioch->lock MUST NOT have been already acquired.
*/
static void scmi_finalize_message(struct scmi_vio_channel *vioch, static void scmi_finalize_message(struct scmi_vio_channel *vioch,
struct scmi_vio_msg *msg) struct scmi_vio_msg *msg)
{ {
if (vioch->is_rx) { if (vioch->is_rx)
scmi_vio_feed_vq_rx(vioch, msg, vioch->cinfo->dev); scmi_vio_feed_vq_rx(vioch, msg);
} else { else
/* Here IRQs are assumed to be already disabled by the caller */ scmi_virtio_put_free_msg(vioch, msg);
spin_lock(&vioch->lock);
list_add(&msg->list, &vioch->free_list);
spin_unlock(&vioch->lock);
}
} }
static void scmi_vio_complete_cb(struct virtqueue *vqueue) static void scmi_vio_complete_cb(struct virtqueue *vqueue)
...@@ -284,7 +318,6 @@ static bool virtio_chan_available(struct device *dev, int idx) ...@@ -284,7 +318,6 @@ static bool virtio_chan_available(struct device *dev, int idx)
static int virtio_chan_setup(struct scmi_chan_info *cinfo, struct device *dev, static int virtio_chan_setup(struct scmi_chan_info *cinfo, struct device *dev,
bool tx) bool tx)
{ {
unsigned long flags;
struct scmi_vio_channel *vioch; struct scmi_vio_channel *vioch;
int index = tx ? VIRTIO_SCMI_VQ_TX : VIRTIO_SCMI_VQ_RX; int index = tx ? VIRTIO_SCMI_VQ_TX : VIRTIO_SCMI_VQ_RX;
int i; int i;
...@@ -314,13 +347,7 @@ static int virtio_chan_setup(struct scmi_chan_info *cinfo, struct device *dev, ...@@ -314,13 +347,7 @@ static int virtio_chan_setup(struct scmi_chan_info *cinfo, struct device *dev,
if (!msg->input) if (!msg->input)
return -ENOMEM; return -ENOMEM;
if (tx) { scmi_finalize_message(vioch, msg);
spin_lock_irqsave(&vioch->lock, flags);
list_add_tail(&msg->list, &vioch->free_list);
spin_unlock_irqrestore(&vioch->lock, flags);
} else {
scmi_vio_feed_vq_rx(vioch, msg, cinfo->dev);
}
} }
scmi_vio_channel_ready(vioch, cinfo); scmi_vio_channel_ready(vioch, cinfo);
...@@ -354,33 +381,31 @@ static int virtio_send_message(struct scmi_chan_info *cinfo, ...@@ -354,33 +381,31 @@ static int virtio_send_message(struct scmi_chan_info *cinfo,
if (!scmi_vio_channel_acquire(vioch)) if (!scmi_vio_channel_acquire(vioch))
return -EINVAL; return -EINVAL;
spin_lock_irqsave(&vioch->lock, flags); msg = scmi_virtio_get_free_msg(vioch);
if (!msg) {
if (list_empty(&vioch->free_list)) {
spin_unlock_irqrestore(&vioch->lock, flags);
scmi_vio_channel_release(vioch); scmi_vio_channel_release(vioch);
return -EBUSY; return -EBUSY;
} }
msg = list_first_entry(&vioch->free_list, typeof(*msg), list);
list_del(&msg->list);
msg_tx_prepare(msg->request, xfer); msg_tx_prepare(msg->request, xfer);
sg_init_one(&sg_out, msg->request, msg_command_size(xfer)); sg_init_one(&sg_out, msg->request, msg_command_size(xfer));
sg_init_one(&sg_in, msg->input, msg_response_size(xfer)); sg_init_one(&sg_in, msg->input, msg_response_size(xfer));
spin_lock_irqsave(&vioch->lock, flags);
rc = virtqueue_add_sgs(vioch->vqueue, sgs, 1, 1, msg, GFP_ATOMIC); rc = virtqueue_add_sgs(vioch->vqueue, sgs, 1, 1, msg, GFP_ATOMIC);
if (rc) { if (rc)
list_add(&msg->list, &vioch->free_list);
dev_err(vioch->cinfo->dev, dev_err(vioch->cinfo->dev,
"failed to add to TX virtqueue (%d)\n", rc); "failed to add to TX virtqueue (%d)\n", rc);
} else { else
virtqueue_kick(vioch->vqueue); virtqueue_kick(vioch->vqueue);
}
spin_unlock_irqrestore(&vioch->lock, flags); spin_unlock_irqrestore(&vioch->lock, flags);
if (rc)
scmi_virtio_put_free_msg(vioch, msg);
scmi_vio_channel_release(vioch); scmi_vio_channel_release(vioch);
return rc; return rc;
...@@ -457,6 +482,7 @@ static int scmi_vio_probe(struct virtio_device *vdev) ...@@ -457,6 +482,7 @@ static int scmi_vio_probe(struct virtio_device *vdev)
unsigned int sz; unsigned int sz;
spin_lock_init(&channels[i].lock); spin_lock_init(&channels[i].lock);
spin_lock_init(&channels[i].free_lock);
INIT_LIST_HEAD(&channels[i].free_list); INIT_LIST_HEAD(&channels[i].free_list);
channels[i].vqueue = vqs[i]; channels[i].vqueue = vqs[i];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment