Skip to content

Commit

Permalink
Update shared queue head/tail in dequeue/enqueue by default when NULL…
Browse files Browse the repository at this point in the history
… is passed

Signed-off-by: Courtney Darville <courtneydarville94@outlook.com>
  • Loading branch information
Courtney3141 authored and Ivan-Velickovic committed Jan 30, 2025
1 parent b717295 commit 64e66fc
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 23 deletions.
4 changes: 2 additions & 2 deletions drivers/serial/arm/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static void tx_provide(void)
bool transferred = false;
while (reprocess) {
char c;
while (!(uart_regs->fr & PL011_FR_TXFF) && !serial_dequeue(&tx_queue_handle, &tx_queue_handle.queue->head, &c)) {
while (!(uart_regs->fr & PL011_FR_TXFF) && !serial_dequeue(&tx_queue_handle, NULL, &c)) {
uart_regs->dr = (uint32_t)c;
transferred = true;
}
Expand Down Expand Up @@ -79,7 +79,7 @@ static void rx_return(void)
while (reprocess) {
while (!(uart_regs->fr & PL011_FR_RXFE) && !serial_queue_full(&rx_queue_handle, rx_queue_handle.queue->tail)) {
char c = (char)(uart_regs->dr & PL011_DR_DATA_MASK);
serial_enqueue(&rx_queue_handle, &rx_queue_handle.queue->tail, c);
serial_enqueue(&rx_queue_handle, NULL, c);
enqueued = true;
}

Expand Down
5 changes: 2 additions & 3 deletions drivers/serial/imx/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ static void tx_provide(void)
bool transferred = false;
while (reprocess) {
char c;
while (!(uart_regs->ts & UART_TST_TX_FIFO_FULL)
&& !serial_dequeue(&tx_queue_handle, &tx_queue_handle.queue->head, &c)) {
while (!(uart_regs->ts & UART_TST_TX_FIFO_FULL) && !serial_dequeue(&tx_queue_handle, NULL, &c)) {
uart_regs->txd = (uint32_t)c;
transferred = true;
}
Expand Down Expand Up @@ -81,7 +80,7 @@ static void rx_return(void)
while (reprocess) {
while (!(uart_regs->ts & UART_TST_RX_FIFO_EMPTY) && !serial_queue_full(&rx_queue_handle, rx_queue_handle.queue->tail)) {
char c = (char) uart_regs->rxd;
serial_enqueue(&rx_queue_handle, &rx_queue_handle.queue->tail, c);
serial_enqueue(&rx_queue_handle, NULL, c);
enqueued = true;
}

Expand Down
4 changes: 2 additions & 2 deletions drivers/serial/meson/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static void tx_provide(void)
bool transferred = false;
while (reprocess) {
char c;
while (!(uart_regs->sr & AML_UART_TX_FULL) && !serial_dequeue(&tx_queue_handle, &tx_queue_handle.queue->head, &c)) {
while (!(uart_regs->sr & AML_UART_TX_FULL) && !serial_dequeue(&tx_queue_handle, NULL, &c)) {
uart_regs->wfifo = (uint32_t)c;
transferred = true;
}
Expand Down Expand Up @@ -111,7 +111,7 @@ static void rx_return(void)
while (reprocess) {
while (!(uart_regs->sr & AML_UART_RX_EMPTY) && !serial_queue_full(&rx_queue_handle, rx_queue_handle.queue->tail)) {
char c = (char) uart_regs->rfifo;
serial_enqueue(&rx_queue_handle, &rx_queue_handle.queue->tail, c);
serial_enqueue(&rx_queue_handle, NULL, c);
enqueued = true;
}

Expand Down
5 changes: 2 additions & 3 deletions drivers/serial/snps/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ static void tx_provide(void)
while (reprocess) {
char c;

while ((*REG_PTR(UART_LSR) & UART_LSR_THRE)
&& !serial_dequeue(&tx_queue_handle, &tx_queue_handle.queue->head, &c)) {
while ((*REG_PTR(UART_LSR) & UART_LSR_THRE) && !serial_dequeue(&tx_queue_handle, NULL, &c)) {
*REG_PTR(UART_THR) = c;
transferred = true;
}
Expand Down Expand Up @@ -90,7 +89,7 @@ static void rx_return(void)
while ((*REG_PTR(UART_LSR) & UART_LSR_DR)
&& !serial_queue_full(&rx_queue_handle, rx_queue_handle.queue->tail)) {
char c = *REG_PTR(UART_RBR);
int err = serial_enqueue(&rx_queue_handle, &rx_queue_handle.queue->tail, c);
int err = serial_enqueue(&rx_queue_handle, NULL, c);
assert(!err);
enqueued = true;
}
Expand Down
7 changes: 3 additions & 4 deletions drivers/serial/virtio/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ static void tx_provide(void)
bool transferred = false;
while (reprocess) {
char c;
while (!virtio_avail_full_tx(&tx_virtq)
&& !serial_dequeue(&tx_queue_handle, &tx_queue_handle.queue->head, &c)) {
while (!virtio_avail_full_tx(&tx_virtq) && !serial_dequeue(&tx_queue_handle, NULL, &c)) {

/* First, allocate somewhere to put the character */
uint32_t char_idx = -1;
Expand Down Expand Up @@ -250,7 +249,7 @@ static void rx_return(void)
assert(!(pkt.flags & VIRTQ_DESC_F_NEXT));

uint32_t char_idx = addr - virtio_rx_char_paddr;
serial_enqueue(&rx_queue_handle, &rx_queue_handle.queue->tail, virtio_rx_char[char_idx]);
serial_enqueue(&rx_queue_handle, NULL, virtio_rx_char[char_idx]);

/* Free the packet descriptor */
int err = ialloc_free(&rx_ialloc_desc, pkt_used.id);
Expand Down Expand Up @@ -442,4 +441,4 @@ void notified(microkit_channel ch)
LOG_DRIVER_ERR("received notification on unexpected channel: %u\n", ch);
break;
}
}
}
2 changes: 1 addition & 1 deletion examples/serial/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void notified(microkit_channel ch)
bool reprocess = true;
char c;
while (reprocess) {
while (!serial_dequeue(&rx_queue_handle, &rx_queue_handle.queue->head, &c)) {
while (!serial_dequeue(&rx_queue_handle, NULL, &c)) {
if (c == '\r') {
sddf_putchar_unbuffered('\\');
sddf_putchar_unbuffered('r');
Expand Down
18 changes: 12 additions & 6 deletions include/sddf/serial/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,22 @@ static inline int serial_queue_full(serial_queue_handle_t *queue_handle, uint32_
* @param queue_handle queue to enqueue into.
* @param local_tail address of the tail to be incremented. This allows for clients to
* enqueue multiple characters before making the changes visible.
* Leave NULL to increment shared queue tail.
* @param character character to be enqueued.
*
* @return -1 when queue is empty, 0 on success.
*/
static inline int serial_enqueue(serial_queue_handle_t *queue_handle, uint32_t *local_tail,
char character)
{
if (serial_queue_full(queue_handle, *local_tail)) {
uint32_t *tail = (local_tail == NULL) ? &queue_handle->queue->tail : local_tail;

if (serial_queue_full(queue_handle, *tail)) {
return -1;
}

queue_handle->data_region[*local_tail % queue_handle->capacity] = character;
(*local_tail)++;
queue_handle->data_region[*tail % queue_handle->capacity] = character;
(*tail)++;

return 0;
}
Expand All @@ -85,19 +88,22 @@ static inline int serial_enqueue(serial_queue_handle_t *queue_handle, uint32_t *
* @param queue_handle queue to dequeue from.
* @param local_head address of the head to be incremented. This allows for clients to
* dequeue multiple characters before making the changes visible.
* Leave NULL to increment shared queue head.
* @param character character to copy into.
*
* @return -1 when queue is empty, 0 on success.
*/
static inline int serial_dequeue(serial_queue_handle_t *queue_handle, uint32_t *local_head,
char *character)
{
if (serial_queue_empty(queue_handle, *local_head)) {
uint32_t *head = (local_head == NULL) ? &queue_handle->queue->head : local_head;

if (serial_queue_empty(queue_handle, *head)) {
return -1;
}

*character = queue_handle->data_region[*local_head % queue_handle->capacity];
(*local_head)++;
*character = queue_handle->data_region[*head % queue_handle->capacity];
(*head)++;

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion serial/components/virt_rx.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void rx_return(void)
uint32_t local_tail = rx_queue_handle_cli[current_client].queue->tail;
char c = '\0';
while (reprocess) {
while (!serial_dequeue(&rx_queue_handle_drv, &rx_queue_handle_drv.queue->head, &c)) {
while (!serial_dequeue(&rx_queue_handle_drv, NULL, &c)) {
switch (current_mode) {
case normal:
if (c == config.switch_char) {
Expand Down
2 changes: 1 addition & 1 deletion util/putchar_serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ void serial_putchar_init(microkit_channel serial_tx_ch, serial_queue_handle_t *s
tx_ch = serial_tx_ch;
tx_queue_handle = serial_tx_queue_handle;
local_tail = serial_tx_queue_handle->queue->tail;
}
}

0 comments on commit 64e66fc

Please sign in to comment.