NRF UARTE zephyr driver may mismatch PM device get/put leading to inflated PM usage count for device

With PM device runtime enabled (CONFIG_PM_DEVICE_RUNTIME=y), calling uart_irq_tx_disable (actual driver function uarte_nrfx_irq_tx_disable) may result in increase the PM device usage count without PUTing a previous GET.

This has been observed using the modem_backend_uart_isr option with the modem cellular driver.

The cause seems to be due to the tx disable being deferred. The call to uarte_nrfx_irq_tx_disable() increases the PM device usage count (pm_device_runtime_get()) and sets a flag (disable_tx_irq) . When the transmission is detected as stopped in the driver ISR, a matching pm_device_runtime_put_async(dev, K_NO_WAIT) is performed.

However, if another uarte_nrfx_irq_tx_disable()/uarte_nrfx_irq_tx_enable() call is made before the disable_tx_irq flag is detected, the flag is unconditionally cleared, an the previous pm_device_runtime_get() is left unmatched.

Scenario:

  • As part of modem_backend_uart_isr_transmit(), uart_irq_tx_disable() and uart_irq_tx_enable() are used around access to the backend's ring buffer:
    • uart_irq_tx_disable(backend->uart);
    • written = ring_buf_put(&backend->isr.transmit_rb, buf, size);
    • uart_irq_tx_enable(backend->uart);
  • If modem+backend_uart_isr_transmit() is called again, before the current transmission is finished (i.e. int_driven->disable_tx_irq hasn't been picked up by the handler for tx ready in the uart driver IRQ), the disable_tx_irq flag is unconditionally cleared without the pm_device_runtime_put_async() ever being called to match the pm_device_runtime_get() performed during the previous call, so PM usage count grows.

This doesn't seem to be specific to the modem_cellular driver with modem_backend_uart_isr, but the modem_cellular driver does seem to be more likely to have multiple transmit blocks being queued up (i.e. transmitting while a transmit is underway), specifically during modem setup/configuration running AT commands.

I have done some preliminary testing with a modified uart_nrfx_uarte.c file that seems to successfully address the mismatch.

The following change to uarte_nrfx_irq_tx_enable() successfully addresses the problem for me:

static void uarte_nrfx_irq_tx_enable(const struct device *dev)

{

    NRF_UARTE_Type *uarte = get_uarte_instance(dev);
    struct uarte_nrfx_data *data = dev->data;
    bool already_enabled;

    pm_device_runtime_get(dev);

    unsigned int key = irq_lock();

    already_enabled = data->int_driven->tx_irq_enabled;
    if (!already_enabled) {
        /* Start of new code */
        if (data->int_driven->disable_tx_irq) {
            /* Flag is still set, so the pm_device_runtime_get() from
             * the previous call to uarte_nrfx_irq_enable() hasn't been
             * matched, so we need to do that here
             */
            pm_device_runtime_put(dev);
        }
        /* End of new code */
        data->int_driven->disable_tx_irq = false;
        data->int_driven->tx_irq_enabled = true;
        nrf_uarte_int_enable(uarte, NRF_UARTE_INT_TXSTOPPED_MASK);
    }

    irq_unlock(key);

    if (already_enabled) {
        pm_device_runtime_put(dev);
    }
}
Can anyone confirm this issue?
Regards,
Nathan Boyd
Related