Skip to content

PIO FIFO debug flags and interrupt support#7502

Open
pelwell wants to merge 8 commits into
raspberrypi:rpi-6.18.yfrom
pelwell:pioirq
Open

PIO FIFO debug flags and interrupt support#7502
pelwell wants to merge 8 commits into
raspberrypi:rpi-6.18.yfrom
pelwell:pioirq

Conversation

@pelwell

@pelwell pelwell commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Add support for querying and clearing the FIFO debug/error flags, and for using PIO interrupts, both within the kernel and from the userspace piolib. This needs an RP1 firmware update, and a piolib update for userspace support.

@pelwell
pelwell force-pushed the pioirq branch 2 times, most recently from 9f52906 to 3602529 Compare July 21, 2026 21:34
@pelwell

pelwell commented Jul 21, 2026

Copy link
Copy Markdown
Contributor Author

PR updated with non-blocking support for the blocking operations, and a PIO-based UART driver.

Fix a few issues discovered after testing the kernel API with a port of
the userspace apitest app.

Signed-off-by: Phil Elwell <phil@raspberrypi.com>
Comment thread drivers/misc/rp1-pio.c
int ret;

if (pio->fw_pio_count >= PIO_SM_GET_DMACTRL) {
struct rp1_access_hw_args hwargs;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two branches look swapped? The else only runs when fw_pio_count < PIO_SM_GET_DMACTRL, but then rp1_pio_message_resp() rejects the op and returns -EOPNOTSUPP, so the dedicated op is never used.

Swap them and use > for the dedicated op, else READ_HW?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll put the fallback case second, which is what I intended to do.

Comment thread drivers/misc/rp1-pio.c Outdated
struct rp1_pio_device *pio = client->pio;
uint32_t intmask = 1 << (irqi - pio->irqs);

spin_lock(&client->lock);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Runs in hardirq, but client->lock is also taken plain in process context (irq_wait/irq_map/close). Use spin_lock_irqsave() everywhere (or a same-CPU IRQ deadlocks)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Runs in hardirq,

Does it? devm_request_irq is an alias for devm_request_threaded_irq nowadays. I'm happy using spin_lock_irqsave, even if it's only for appearances (and I'm not saying it is).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIU devm_request_irq() calls devm_request_threaded_irq() with thread_fn = NULL, so the handler stays the primary and runs in hardirq. The alias does not thread it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, just saw that my comment went to the wrong line in the commit, the _irqsave was meant for irq_wait(), irq_remove_handler() and close().

rp1_pio_irq_handler_userspace() already runs with irqs disabled in hardirq.

Comment thread drivers/misc/rp1-pio.c
enable_irq(irqi->irq);
irqi->enabled = true;
} else if (!args->enabled && irqi->enabled) {
disable_irq(irqi->irq);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

disable_irq() can sleep, so it can't run under pio->lock. Use disable_irq_nosync()?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've not come across that one before.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably will only surface with CONFIG_DEBUG_ATOMIC_SLEEP, disable_irq_nosync() is cheaper anyway, but it's probably academic 😄

Comment thread drivers/misc/rp1-pio.c Outdated

if (!irqi || !irqi->handler) {
pr_err("disabling unwanted interrupt\n");
disable_irq(irq);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handler for irq calling disable_irq() would wait for itself => hang. Use disable_irq_nosync()?

Comment thread drivers/misc/rp1-pio.c
if (irqi->enabled)
irq_set_enabled(client, i, false);

irqi->owner = NULL;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleared without pio->lock, but claim/add/remove hold it. Needs the lock here too.

Comment thread drivers/tty/serial/rp1-pio-uart.c Outdated
Comment on lines +639 to +643
cancel_work_sync(&rfu->tx_work);
cancel_work_sync(&rfu->rx_rearm_work);
timer_delete_sync(&rfu->rx_timeout);
cancel_work_sync(&rfu->rx_timeout_work);
cancel_work_sync(&rfu->break_work);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consumers cancelled before producers stopped. Break IRQ + RX SM/DMA are shut off later, so a break/RX chunk in between re-queues break_work (runs after rfu->pio = NULL => NULL deref) Stopping IRQ + RX first should do the trick

Comment thread drivers/tty/serial/rp1-pio-uart.c Outdated
/* A chunk just completed, so the FIFO isn't backed up: push the
* deadline for the CPU fallback back out.
*/
mod_timer(&rfu->rx_timeout, jiffies + rp1_pio_uart_rx_timeout_jiffies(&rfu->port));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-arms unconditionally, so a chunk completing after timer_delete_sync() revives the timer, nothing cancels it later => use after free on freed rfu. Gating it on shutting_down should work

pelwell added 4 commits July 22, 2026 22:25
pio_sm_get_dmactrl is the reverse of pio_sm_set_dmactrl.
pio_sm_get_flags allows the FIFO error flags - txoverflow, rxunderflow,
txstall and rxstall - to be polled, cleared, and optionally waited for.

Signed-off-by: Phil Elwell <phil@raspberrypi.com>
The PIO block exposes 2 assignable interrupts. Make them available to
kernel and userspace PIO applications.

Signed-off-by: Phil Elwell <phil@raspberrypi.com>
The PIO hardware drives 2 interrupt lines - declare them.

Signed-off-by: Phil Elwell <phil@raspberrypi.com>
The piolib API has three blocking operations:
* pio_sm_get_blocking
* pio_sm_put_blocking
* pio_sm_exec_wait_blocking

Rather than blocking in the firmware, stalling the whole API, allow
the firmware to return EAGAIN to indicate that it has not completed.
With a small change on each side it then becomes possible to poll for
completion, allowing other operations to continue in the meantime.

Signed-off-by: Phil Elwell <phil@raspberrypi.com>
pelwell added 3 commits July 23, 2026 17:00
This is a tty driver that implements a UART using RP1's PIO block,
ported from pico-examples/pio/uart_dma. Like that example, it is
limited to 8N1, with no hardware flow control.

Signed-off-by: Phil Elwell <phil@raspberrypi.com>
Enable building the RP1 PIO UART for all kernels that support the Pi 5.

Signed-off-by: Phil Elwell <phil@raspberrypi.com>
An overlay to enable the PIO-based UART.

Signed-off-by: Phil Elwell <phil@raspberrypi.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants