diff --git a/piolib/examples/rp1sm.c b/piolib/examples/rp1sm.c index d30e9ba..df28654 100644 --- a/piolib/examples/rp1sm.c +++ b/piolib/examples/rp1sm.c @@ -47,7 +47,7 @@ int main(int argc, const char **argv) if (argc > 2) usage(); - fd = open(devname, O_RDWR, O_CLOEXEC); + fd = open(devname, O_RDWR | O_CLOEXEC); if (fd < 0) return -errno; diff --git a/piolib/pio_rp1.c b/piolib/pio_rp1.c index 75cbfca..fd49589 100644 --- a/piolib/pio_rp1.c +++ b/piolib/pio_rp1.c @@ -83,9 +83,6 @@ typedef struct rp1_pio_handle { struct pio_instance base; const char *devname; int fd; - bool irq_thread_running; - pthread_mutex_t lock; - pthread_t irq_thread; } *RP1_PIO; #define smc_to_rp1(_config, _c) rp1_pio_sm_config *_c = (rp1_pio_sm_config*)_config @@ -104,6 +101,20 @@ static inline void check_sm_mask(__unused uint mask) valid_params_if(PIO, mask < (1u << RP1_PIO_SM_COUNT)); } +/* + * The instructions are copied into a fixed-size buffer, so this has to be a + * real check, not a parameter assertion that is usually compiled out. + */ +static bool rp1_pio_program_fits(const pio_program_t *program, uint offset) +{ + if (program->length > RP1_PIO_INSTRUCTION_COUNT) + return false; + if (offset == PIO_ORIGIN_ANY) + return true; + return offset < RP1_PIO_INSTRUCTION_COUNT && + offset + program->length <= RP1_PIO_INSTRUCTION_COUNT; +} + static pio_sm_config rp1_pio_get_default_sm_config(PIO) { pio_sm_config c = { { 0 } }; @@ -303,6 +314,8 @@ static bool rp1_pio_can_add_program_at_offset(PIO pio, const pio_program_t *prog valid_params_if(PIO, offset < RP1_PIO_INSTRUCTION_COUNT || offset == PIO_ORIGIN_ANY); valid_params_if(PIO, program->length <= RP1_PIO_INSTRUCTION_COUNT); valid_params_if(PIO, offset + program->length <= RP1_PIO_INSTRUCTION_COUNT || offset == PIO_ORIGIN_ANY); + if (!rp1_pio_program_fits(program, offset)) + return false; if (program->origin >= 0 && (uint)program->origin != offset) return false; if (offset != PIO_ORIGIN_ANY) @@ -320,6 +333,8 @@ static uint rp1_pio_add_program_at_offset(PIO pio, const pio_program_t *program, valid_params_if(PIO, offset < RP1_PIO_INSTRUCTION_COUNT || offset == PIO_ORIGIN_ANY); valid_params_if(PIO, program->length <= RP1_PIO_INSTRUCTION_COUNT); valid_params_if(PIO, offset + program->length <= RP1_PIO_INSTRUCTION_COUNT || offset == PIO_ORIGIN_ANY); + if (!rp1_pio_program_fits(program, offset)) + return PIO_ORIGIN_INVALID; if (offset != PIO_ORIGIN_ANY) args.origin = offset; memcpy(args.instrs, program->instructions, program->length * sizeof(uint16_t)); @@ -969,38 +984,10 @@ static void rp1_pio_interrupt_clear(PIO pio, uint pio_interrupt_num) (void)rp1_ioctl(pio, PIO_IOC_INTERRUPT_CLEAR, &args); } -static void *irq_wait_thread(void *arg) { - RP1_PIO rp = (RP1_PIO)arg; - struct rp1_pio_irq_wait_args args; - - args.timeout_ms = 0; - while (1) { - rp1_ioctl(&rp->base, PIO_IOC_IRQ_WAIT, &args); - if (!args.active_mask) - break; - printf("woke %x\n", args.active_mask); - } - - return NULL; -} - -static void rp1_pio_irq_start_thread(PIO pio) { - RP1_PIO rp = (RP1_PIO)pio; - pthread_mutex_lock(&rp->lock); - if (!rp->irq_thread_running) { - if (pthread_create(&rp->irq_thread, NULL, irq_wait_thread, rp)) - pio_panic("Failed to create irq wait thread!"); - rp->irq_thread_running = true; - } - pthread_mutex_unlock(&rp->lock); -} - static int rp1_pio_irq_claim(PIO pio) { struct rp1_pio_irq_claim_args args = { .irq_index = -1 }; (void)rp1_ioctl(pio, PIO_IOC_IRQ_CLAIM, &args); - if (args.irq_index >= 0) - rp1_pio_irq_start_thread(pio); return args.irq_index; } @@ -1030,7 +1017,10 @@ static PIO rp1_create_instance(PIO_CHIP_T *chip, uint index) pio->base.chip = chip; pio->fd = -1; pio->devname = strdup(pathbuf); - pthread_mutex_init(&pio->lock, NULL); + if (!pio->devname) { + free(pio); + return PIO_ERR(-ENOMEM); + } return &pio->base; } @@ -1040,7 +1030,7 @@ static int rp1_open_instance(PIO pio) RP1_PIO rp = (RP1_PIO)pio; int fd; - fd = open(rp->devname, O_RDWR, O_CLOEXEC); + fd = open(rp->devname, O_RDWR | O_CLOEXEC); if (fd < 0) return -errno; rp->fd = fd; @@ -1051,6 +1041,8 @@ static void rp1_close_instance(PIO pio) { RP1_PIO rp = (RP1_PIO)pio; close(rp->fd); + /* Don't leave a stale descriptor that might end up referring to another file */ + rp->fd = -1; } DECLARE_PIO_CHIP(rp1) { diff --git a/piolib/piolib.c b/piolib/piolib.c index c0a9b33..c84bc94 100644 --- a/piolib/piolib.c +++ b/piolib/piolib.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -20,6 +21,12 @@ #define PIO_MAX_INSTANCES 4 #define PIO_MAX_IRQS 16 +/* + * How often the IRQ thread checks whether it should exit, which is also how + * long pio_close may have to wait for it. + */ +#define PIO_IRQ_POLL_MS 100 + static __thread PIO __pio; struct pio_irq_handler { @@ -30,8 +37,10 @@ struct pio_irq_handler { static PIO pio_instances[PIO_MAX_INSTANCES]; static uint num_instances; -static pthread_mutex_t pio_handle_lock; +static pthread_mutex_t pio_handle_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_t pio_irq_threads[PIO_MAX_INSTANCES]; +static bool pio_irq_thread_running[PIO_MAX_INSTANCES]; +static atomic_bool pio_irq_thread_stop[PIO_MAX_INSTANCES]; static struct pio_irq_handler irq_handlers[PIO_MAX_IRQS]; static uint num_irqs; @@ -60,9 +69,15 @@ int pio_get_index(PIO pio) static void *irq_wait_thread(void *arg) { PIO pio = (PIO)arg; - - while (1) { - uint32_t active = pio->chip->pio_irq_wait(pio, 0); + int idx = pio_get_index(pio); + + /* + * Wait with a timeout, so that pio_close can stop the thread. A thread + * blocked indefinitely in the kernel would keep the device open, along + * with everything that had been claimed through it. + */ + while (!atomic_load(&pio_irq_thread_stop[idx])) { + uint32_t active = pio->chip->pio_irq_wait(pio, PIO_IRQ_POLL_MS); uint i; if (active == ~0u) break; @@ -70,8 +85,10 @@ static void *irq_wait_thread(void *arg) { uint32_t mask = (1 << i); if (active & mask) { struct pio_irq_handler *h = &irq_handlers[pio->irq_base + i]; + irq_handler_t handler = h->handler; active &= ~mask; - (h->handler)(h->context); + if (handler) + handler(h->context); } } } @@ -79,6 +96,50 @@ static void *irq_wait_thread(void *arg) { return NULL; } +/* The IRQ thread is only needed (and only started) once an IRQ is in use */ +static void pio_irq_thread_start(PIO pio) +{ + int idx = pio_get_index(pio); + + if (idx < 0) + return; + + pthread_mutex_lock(&pio_handle_lock); + if (!pio_irq_thread_running[idx]) { + atomic_store(&pio_irq_thread_stop[idx], false); + if (pthread_create(&pio_irq_threads[idx], NULL, irq_wait_thread, pio)) + pio_panic("Failed to create irq wait thread!"); + pio_irq_thread_running[idx] = true; + } + pthread_mutex_unlock(&pio_handle_lock); +} + +static void pio_irq_thread_stop_and_wait(PIO pio) +{ + int idx = pio_get_index(pio); + pthread_t thread; + bool running; + + if (idx < 0) + return; + + pthread_mutex_lock(&pio_handle_lock); + running = pio_irq_thread_running[idx]; + thread = pio_irq_threads[idx]; + pio_irq_thread_running[idx] = false; + pthread_mutex_unlock(&pio_handle_lock); + + if (!running) + return; + + atomic_store(&pio_irq_thread_stop[idx], true); + /* Closing from an IRQ handler means the thread can't wait for itself */ + if (pthread_equal(thread, pthread_self())) + pthread_detach(thread); + else + pthread_join(thread, NULL); +} + int pio_init(void) { #if LIBRARY_BUILD @@ -91,20 +152,29 @@ int pio_init(void) static bool initialised; const PIO_CHIP_T * const *p; uint i = 0; - int err; - if (initialised) + pthread_mutex_lock(&pio_handle_lock); + + if (initialised) { + pthread_mutex_unlock(&pio_handle_lock); return 0; + } num_instances = 0; num_irqs = 0; p = start; - while (p < end) + while (p < end && num_instances < PIO_MAX_INSTANCES) { PIO_CHIP_T *chip = *p; PIO pio = chip->create_instance(chip, i); if (pio && !PIO_IS_ERR(pio)) { - int irq_count = chip->irq_count; + uint irq_count = chip->irq_count; + /* Stay within the per-instance and global IRQ tables */ + if (irq_count > NUM_PIO_IRQS) + irq_count = NUM_PIO_IRQS; + if (num_irqs + irq_count > PIO_MAX_IRQS) + irq_count = 0; + pio->irq_count = 0; if (irq_count) { pio->irq_base = num_irqs; pio->irq_count = irq_count; @@ -123,11 +193,8 @@ int pio_init(void) } } - err = pthread_mutex_init(&pio_handle_lock, NULL); - if (err) - return err; - initialised = true; + pthread_mutex_unlock(&pio_handle_lock); return 0; } @@ -161,16 +228,16 @@ PIO pio_open(uint idx) err = pio->chip->open_instance(pio); if (err) { + pthread_mutex_lock(&pio_handle_lock); pio->in_use = 0; + pthread_mutex_unlock(&pio_handle_lock); return PIO_ERR(err); - } else if (pio->irq_count) { - for (i = 0; i < pio->irq_count; i++) - pio->irqs[i] = -1; - - if (pthread_create(&pio_irq_threads[idx], NULL, irq_wait_thread, pio)) - pio_panic("Failed to create irq wait thread!"); } + /* The IRQ thread is started when the first IRQ is mapped */ + for (i = 0; i < pio->irq_count; i++) + pio->irqs[i] = -1; + pio_select(pio); return pio; @@ -199,7 +266,7 @@ PIO pio_open_by_name(const char *name) PIO pio_open_helper(uint idx) { - PIO pio = pio_instances[idx]; + PIO pio = (idx < PIO_MAX_INSTANCES) ? pio_instances[idx] : NULL; if (!pio || !pio->in_use) { pio = pio_open(idx); if (PIO_IS_ERR(pio)) { @@ -213,6 +280,8 @@ PIO pio_open_helper(uint idx) void pio_close(PIO pio) { + /* Stop the IRQ thread first, otherwise it keeps the device open */ + pio_irq_thread_stop_and_wait(pio); pio->chip->close_instance(pio); pthread_mutex_lock(&pio_handle_lock); pio->in_use = 0; @@ -237,16 +306,21 @@ void sleep_us(uint64_t us) uint pio_irq_map(PIO pio, uint irq_index) { int pirq; check_pio_param(pio); - invalid_params_if(PIO, irq_index > pio->irq_count); + /* This indexes an array, so it has to be a real check */ + if (irq_index >= pio->irq_count) + pio_panic("irq index out of range"); pirq = pio->irqs[irq_index]; if (pirq < 0) { pirq = pio->chip->pio_irq_claim(pio); + if (pirq >= (int)pio->irq_count) + pirq = -1; if (pirq >= 0) { pio->irqs[irq_index] = pirq; } } if (pirq < 0) pio_panic("Unable to map irq"); + pio_irq_thread_start(pio); return (uint)pirq; }