Skip to content

Commit b88d624

Browse files
author
Jakub Klimczak
committed
drivers: serial: virtio_console: Remove superfluous spinlocks
Spinlocks in functions virtconsole_control_recv_cb and virtconsole_send_control_msg were unnecessary since those were called from virtio_pci_isr or virtio_mmio_isr, which already use spinlocks. Signed-off-by: Jakub Klimczak <[email protected]>
1 parent 6d50eb0 commit b88d624

File tree

1 file changed

+83
-92
lines changed

1 file changed

+83
-92
lines changed

drivers/serial/uart_virtio_console.c

Lines changed: 83 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ struct virtconsole_data {
114114
/* bitmask of ports to be used as console */
115115
uint32_t console_ports;
116116
int8_t n_console_ports;
117-
struct k_spinlock ctlrxsl, ctltxsl;
118117
size_t txctlcurrent;
119118
struct _virtio_console_control rx_ctlbuf[CONFIG_UART_VIRTIO_CONSOLE_RX_CONTROL_BUFSIZE];
120119
struct _fifo_item_virtio_console_control
@@ -226,110 +225,102 @@ static void virtconsole_send_control_msg(const struct device *dev, uint32_t port
226225
const struct virtconsole_config *config = dev->config;
227226
struct virtconsole_data *data = dev->data;
228227

229-
K_SPINLOCK(&(data->ctltxsl)) {
230-
struct virtq *vq = virtio_get_virtqueue(config->vdev, VIRTQ_CONTROL_TX);
231-
232-
if (vq == NULL) {
233-
LOG_ERR("could not access virtqueue 3");
234-
K_SPINLOCK_BREAK;
235-
}
236-
struct _fifo_item_virtio_console_control *item =
237-
&(data->tx_ctlbuf[data->txctlcurrent]);
238-
struct _virtio_console_control *msg = &(item->msg);
228+
struct virtq *vq = virtio_get_virtqueue(config->vdev, VIRTQ_CONTROL_TX);
239229

240-
if (item->pending) {
241-
LOG_ERR("not enough free buffers for control message");
242-
K_SPINLOCK_BREAK;
243-
}
244-
msg->port = sys_cpu_to_le32(port);
245-
msg->event = sys_cpu_to_le16(event);
246-
msg->value = sys_cpu_to_le16(value);
247-
struct virtq_buf vqbuf = {.addr = msg, .len = sizeof(*msg)};
230+
if (vq == NULL) {
231+
LOG_ERR("could not access virtqueue 3");
232+
return;
233+
}
234+
struct _fifo_item_virtio_console_control *item = &(data->tx_ctlbuf[data->txctlcurrent]);
235+
struct _virtio_console_control *msg = &(item->msg);
248236

249-
int ret = virtq_add_buffer_chain(vq, &vqbuf, 1, 1, virtconsole_control_tx_flush,
250-
data, K_NO_WAIT);
251-
252-
if (ret == -EBUSY) {
253-
/* put in FIFO to be sent later, mark buffer */
254-
/* as occupied to prevent overwriting */
255-
k_fifo_put(&data->tx_ctlfifo, data->tx_ctlbuf + data->txctlcurrent);
256-
item->pending = true;
257-
} else if (ret) {
258-
LOG_ERR("could not send control message");
259-
K_SPINLOCK_BREAK;
260-
} else {
261-
virtio_notify_virtqueue(config->vdev, VIRTQ_CONTROL_TX);
262-
}
263-
data->txctlcurrent =
264-
(data->txctlcurrent + 1) % CONFIG_UART_VIRTIO_CONSOLE_TX_CONTROL_BUFSIZE;
237+
if (item->pending) {
238+
LOG_ERR("not enough free buffers for control message");
239+
return;
240+
}
241+
msg->port = sys_cpu_to_le32(port);
242+
msg->event = sys_cpu_to_le16(event);
243+
msg->value = sys_cpu_to_le16(value);
244+
struct virtq_buf vqbuf = {.addr = msg, .len = sizeof(*msg)};
245+
246+
int ret = virtq_add_buffer_chain(vq, &vqbuf, 1, 1, virtconsole_control_tx_flush, data,
247+
K_NO_WAIT);
248+
249+
if (ret == -EBUSY) {
250+
/* put in FIFO to be sent later, mark buffer as occupied to prevent overwriting */
251+
k_fifo_put(&data->tx_ctlfifo, data->tx_ctlbuf + data->txctlcurrent);
252+
item->pending = true;
253+
} else if (ret == 0) {
254+
virtio_notify_virtqueue(config->vdev, VIRTQ_CONTROL_TX);
255+
} else {
256+
LOG_ERR("could not send control message");
257+
return;
265258
}
259+
data->txctlcurrent =
260+
(data->txctlcurrent + 1) % CONFIG_UART_VIRTIO_CONSOLE_TX_CONTROL_BUFSIZE;
266261
}
267262

268263
static void virtconsole_control_recv_cb(void *priv, uint32_t len)
269264
{
270265
struct _ctl_cb_data *ctld = priv;
271266
struct virtconsole_data *data = ctld->data;
272267

273-
K_SPINLOCK(&data->ctlrxsl) {
274-
for (int i = 0; i < CONFIG_UART_VIRTIO_CONSOLE_RX_CONTROL_BUFSIZE; i++) {
275-
if (data->rx_ctlbuf[i].port == UINT32_MAX) {
276-
continue;
277-
}
278-
data->rx_ctlbuf[i].port = sys_le32_to_cpu(data->rx_ctlbuf[i].port);
279-
data->rx_ctlbuf[i].event = sys_le16_to_cpu(data->rx_ctlbuf[i].event);
280-
data->rx_ctlbuf[i].value = sys_le16_to_cpu(data->rx_ctlbuf[i].value);
281-
282-
switch (data->rx_ctlbuf[i].event) {
283-
case VIRTIO_CONSOLE_DEVICE_ADD:
284-
virtconsole_send_control_msg(data->dev, data->rx_ctlbuf[i].port,
285-
VIRTIO_CONSOLE_PORT_READY,
286-
(data->rx_ctlbuf[i].port) <
287-
VIRTIO_CONSOLE_MAX_PORTS);
288-
break;
289-
case VIRTIO_CONSOLE_DEVICE_REMOVE: {
290-
int port = data->rx_ctlbuf[i].port;
291-
292-
if ((port < VIRTIO_CONSOLE_MAX_PORTS) &&
293-
IS_BIT_SET(data->console_ports, port)) {
294-
/* Remove console port (unset bit) */
295-
data->console_ports = ~(data->console_ports);
296-
data->console_ports |= BIT(port);
297-
data->console_ports = ~(data->console_ports);
298-
data->n_console_ports--;
299-
}
300-
break;
301-
}
302-
case VIRTIO_CONSOLE_CONSOLE_PORT: {
303-
int port = data->rx_ctlbuf[i].port;
304-
305-
if ((port < VIRTIO_CONSOLE_MAX_PORTS) &&
306-
!IS_BIT_SET(data->console_ports, port)) {
307-
data->console_ports |= BIT(port);
308-
data->n_console_ports++;
309-
}
310-
virtconsole_send_control_msg(data->dev, data->rx_ctlbuf[i].port,
311-
VIRTIO_CONSOLE_PORT_OPEN, 1);
312-
break;
268+
for (int i = 0; i < CONFIG_UART_VIRTIO_CONSOLE_RX_CONTROL_BUFSIZE; i++) {
269+
if (data->rx_ctlbuf[i].port == UINT32_MAX) {
270+
continue;
271+
}
272+
data->rx_ctlbuf[i].port = sys_le32_to_cpu(data->rx_ctlbuf[i].port);
273+
data->rx_ctlbuf[i].event = sys_le16_to_cpu(data->rx_ctlbuf[i].event);
274+
data->rx_ctlbuf[i].value = sys_le16_to_cpu(data->rx_ctlbuf[i].value);
275+
276+
switch (data->rx_ctlbuf[i].event) {
277+
case VIRTIO_CONSOLE_DEVICE_ADD:
278+
virtconsole_send_control_msg(
279+
data->dev, data->rx_ctlbuf[i].port, VIRTIO_CONSOLE_PORT_READY,
280+
(data->rx_ctlbuf[i].port) < VIRTIO_CONSOLE_MAX_PORTS);
281+
break;
282+
case VIRTIO_CONSOLE_DEVICE_REMOVE: {
283+
int port = data->rx_ctlbuf[i].port;
284+
285+
if ((port < VIRTIO_CONSOLE_MAX_PORTS) &&
286+
IS_BIT_SET(data->console_ports, port)) {
287+
/* Remove console port (unset bit) */
288+
data->console_ports = ~(data->console_ports);
289+
data->console_ports |= BIT(port);
290+
data->console_ports = ~(data->console_ports);
291+
data->n_console_ports--;
313292
}
314-
case VIRTIO_CONSOLE_RESIZE:
315-
/* Terminal sizes are not supported by Zephyr and the */
316-
/* VIRTIO_CONSOLE_F_SIZE feature was not enabled */
317-
LOG_WRN("device tried to set console size");
318-
break;
319-
case VIRTIO_CONSOLE_PORT_OPEN:
320-
LOG_INF("port %u is ready", data->rx_ctlbuf[i].port);
321-
break;
322-
case VIRTIO_CONSOLE_PORT_NAME:
323-
LOG_INF("port %u is named \"%.*s\"", data->rx_ctlbuf[i].port,
324-
(int)ARRAY_SIZE(data->rx_ctlbuf[i].name),
325-
data->rx_ctlbuf[i].name);
326-
break;
327-
default:
328-
break;
293+
break;
294+
}
295+
case VIRTIO_CONSOLE_CONSOLE_PORT: {
296+
int port = data->rx_ctlbuf[i].port;
297+
298+
if ((port < VIRTIO_CONSOLE_MAX_PORTS) &&
299+
!IS_BIT_SET(data->console_ports, port)) {
300+
data->console_ports |= BIT(port);
301+
data->n_console_ports++;
329302
}
330-
data->rx_ctlbuf[i].port = UINT32_MAX;
331-
memset(&(data->rx_ctlbuf[i].name), 0, ARRAY_SIZE(data->rx_ctlbuf[i].name));
303+
virtconsole_send_control_msg(data->dev, data->rx_ctlbuf[i].port,
304+
VIRTIO_CONSOLE_PORT_OPEN, 1);
305+
break;
306+
}
307+
case VIRTIO_CONSOLE_RESIZE:
308+
/* Terminal sizes are not supported by Zephyr and the */
309+
/* VIRTIO_CONSOLE_F_SIZE feature was not enabled */
310+
LOG_WRN("device tried to set console size");
311+
break;
312+
case VIRTIO_CONSOLE_PORT_OPEN:
313+
LOG_INF("port %u is ready", data->rx_ctlbuf[i].port);
314+
break;
315+
case VIRTIO_CONSOLE_PORT_NAME:
316+
LOG_INF("port %u is named \"%.*s\"", data->rx_ctlbuf[i].port,
317+
(int)ARRAY_SIZE(data->rx_ctlbuf[i].name), data->rx_ctlbuf[i].name);
318+
break;
319+
default:
320+
break;
332321
}
322+
data->rx_ctlbuf[i].port = UINT32_MAX;
323+
memset(&(data->rx_ctlbuf[i].name), 0, ARRAY_SIZE(data->rx_ctlbuf[i].name));
333324
}
334325
virtconsole_recv_setup(data->dev, VIRTQ_CONTROL_RX, &data->rx_ctlbuf[ctld->buf_no],
335326
sizeof(struct _virtio_console_control), virtconsole_control_recv_cb,

0 commit comments

Comments
 (0)