[PATCH v1] mailbox: support timeout when sending

Simon Glass sjg at chromium.org
Mon Sep 30 20:52:02 CEST 2024


Hi,

On Sun, 29 Sept 2024 at 06:54, <alice.guo at oss.nxp.com> wrote:
>
> From: Peng Fan <peng.fan at nxp.com>
>
> Add timeout support for mbox_send.
>
> Signed-off-by: Peng Fan <peng.fan at nxp.com>
> Signed-off-by: Alice Guo <alice.guo at nxp.com>
> Reviewed-by: Ye Li <ye.li at nxp.com>
> ---
>  drivers/mailbox/mailbox-uclass.c | 19 ++++++++++++++++++-
>  include/mailbox.h                |  2 ++
>  2 files changed, 20 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/mailbox/mailbox-uclass.c b/drivers/mailbox/mailbox-uclass.c
> index 4bf4987ce0..0ca8b3770f 100644
> --- a/drivers/mailbox/mailbox-uclass.c
> +++ b/drivers/mailbox/mailbox-uclass.c
> @@ -117,10 +117,27 @@ int mbox_free(struct mbox_chan *chan)
>  int mbox_send(struct mbox_chan *chan, const void *data)
>  {
>         struct mbox_ops *ops = mbox_dev_ops(chan->dev);
> +       ulong start_time, timeout_us;
> +       int ret;
>
>         debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
>
> -       return ops->send(chan, data);
> +       start_time = timer_get_us();
> +       timeout_us = chan->tx_timeout_us;
> +       /*
> +        * Account for partial us ticks, but if timeout_us is 0, ensure we
> +        * still don't wait at all.
> +        */
> +       if (timeout_us)
> +               timeout_us++;
> +
> +       for (;;) {
> +               ret = ops->send(chan, data);
> +               if (ret != -EBUSY)
> +                       return ret;
> +               if ((timer_get_us() - start_time) >= timeout_us)

get_timer_us_long(start_time) >= timeout_us

would be better here as it is designed to do the maths for you!

> +                       return -ETIMEDOUT;
> +       }
>  }
>
>  int mbox_recv(struct mbox_chan *chan, void *data, ulong timeout_us)
> diff --git a/include/mailbox.h b/include/mailbox.h
> index e70266fb61..3cc64f20b7 100644
> --- a/include/mailbox.h
> +++ b/include/mailbox.h
> @@ -46,6 +46,7 @@ struct udevice;
>   *
>   * @dev: The device which implements the mailbox.
>   * @id: The mailbox channel ID within the provider.
> + * @tx_timeout_us: The tx timeout in us.
>   * @con_priv: Hook for controller driver to attach private data
>   *
>   * Currently, the mailbox API assumes that a single integer ID is enough to
> @@ -61,6 +62,7 @@ struct mbox_chan {
>         struct udevice *dev;
>         /* Written by of_xlate.*/
>         unsigned long id;
> +       unsigned long tx_timeout_us;

ulong

>         void *con_priv;
>  };
>
> --
> 2.34.1
>

Please also update test/dm/mailbox.c so this new feature is tested in
CI. You can use timer_test_add_offset() in a test to add to the
current (faked) time.

Regards,
Simon


More information about the U-Boot mailing list