Linux-i3c Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Frank Li <Frank.li@nxp.com>
To: Jarkko Nikula <jarkko.nikula@linux.intel.com>
Cc: linux-i3c@lists.infradead.org,
	Alexandre Belloni <alexandre.belloni@bootlin.com>
Subject: Re: [PATCH 1/4] i3c: master: Add helpers for DMA mapping and bounce buffer handling
Date: Fri, 1 Aug 2025 10:54:52 -0400	[thread overview]
Message-ID: <aIzVPNnrOkPQ3fcd@lizhi-Precision-Tower-5810> (raw)
In-Reply-To: <20250731141420.422525-1-jarkko.nikula@linux.intel.com>

On Thu, Jul 31, 2025 at 05:14:17PM +0300, Jarkko Nikula wrote:
> Add helpers for I3C host controller drivers for DMA mapping/unmapping
> and bounce buffer handling. A bounce buffer is allocated if the buffer
> is not DMA'able or when the driver requires it for a transfer.

You'd better move some patch 2's description to this patch, to descriptor
why need bounce buffer.

>
> Signed-off-by: Jarkko Nikula <jarkko.nikula@linux.intel.com>
> ---
>  drivers/i3c/master.c       | 74 ++++++++++++++++++++++++++++++++++++++
>  include/linux/i3c/master.h | 20 +++++++++++
>  2 files changed, 94 insertions(+)
>
> diff --git a/drivers/i3c/master.c b/drivers/i3c/master.c
> index 2ef898a8fd80..a1daf18ea707 100644
> --- a/drivers/i3c/master.c
> +++ b/drivers/i3c/master.c
> @@ -8,6 +8,7 @@
>  #include <linux/atomic.h>
>  #include <linux/bug.h>
>  #include <linux/device.h>
> +#include <linux/dma-mapping.h>
>  #include <linux/err.h>
>  #include <linux/export.h>
>  #include <linux/kernel.h>
> @@ -1727,6 +1728,79 @@ int i3c_master_do_daa(struct i3c_master_controller *master)
>  }
>  EXPORT_SYMBOL_GPL(i3c_master_do_daa);
>
> +/**
> + * i3c_master_dma_map_single() - Map buffer for single DMA transfer
> + * @dev: device object of a device doing DMA
> + * @buf: destination/source buffer for DMA
> + * @len: length of transfer
> + * @need_bounce: true if buffer is not DMA safe and need a bounce buffer

Is it possible auto check if 'buf' dma-able?

> + * @dir: DMA direction
> + *
> + * Map buffer for a DMA transfer and allocate a bounce buffer if required.
> + *
> + * Return: I3C DMA transfer descriptor or NULL in case of error.
> + */
> +struct i3c_dma *i3c_master_dma_map_single(struct device *dev, void *buf,
> +	size_t len, bool need_bounce, enum dma_data_direction dir)
> +{
> +	struct i3c_dma *dma_xfer;
> +	void *dma_buf = buf;
> +
> +	dma_xfer = kzalloc(sizeof(*dma_xfer), GFP_KERNEL);

If use struct i3c_dma __free(kfree) *dma_xfer = NULL; it simple error handle

> +	if (!dma_xfer)
> +		return NULL;
> +
> +	dma_xfer->buf = buf;
> +	dma_xfer->dir = dir;
> +	dma_xfer->len = len;
> +	if (is_vmalloc_addr(buf))
> +		need_bounce = true;
> +
> +	if (need_bounce) {
> +		if (dir == DMA_FROM_DEVICE)
> +			dma_buf = kzalloc(ALIGN(len, cache_line_size()),
> +						GFP_KERNEL);
> +		else
> +			dma_buf = kmemdup(buf, len, GFP_KERNEL);
> +		if (!dma_buf)
> +			goto err_alloc;
> +
> +		dma_xfer->bounce_buf = dma_buf;
> +	}
> +
> +	dma_xfer->addr = dma_map_single(dev, dma_buf, len, dir);

len should be ALIGN(len, cache_line_size(), otherwise swiotlb will bounce
again if need_bounce is true

> +	if (dma_mapping_error(dev, dma_xfer->addr))
> +		goto err_map;
> +
> +	return dma_xfer;

return no_free_ptr(dma_xfer) if use __free.

> +err_map:
> +	kfree(dma_xfer->bounce_buf);
> +err_alloc:
> +	kfree(dma_xfer);
> +	return NULL;
> +}
> +EXPORT_SYMBOL_GPL(i3c_master_dma_map_single);
> +
> +/**
> + * i3c_master_dma_unmap_single() - Unmap buffer after DMA
> + * @dev: device object of a device doing DMA
> + * @dma_xfer: DMA transfer and mapping descriptor
> + *
> + * Unmap buffer and cleanup DMA transfer descriptor.
> + */
> +void i3c_master_dma_unmap_single(struct device *dev, struct i3c_dma *dma_xfer)
> +{
> +	dma_unmap_single(dev, dma_xfer->addr, dma_xfer->len, dma_xfer->dir);
> +	if (dma_xfer->bounce_buf) {
> +		if (dma_xfer->dir == DMA_FROM_DEVICE)
> +			memcpy(dma_xfer->buf, dma_xfer->bounce_buf,
> +			       dma_xfer->len);
> +		kfree(dma_xfer->bounce_buf);
> +	}
> +	kfree(dma_xfer);
> +}
> +EXPORT_SYMBOL_GPL(i3c_master_dma_unmap_single);
> +
>  /**
>   * i3c_master_set_info() - set master device information
>   * @master: master used to send frames on the bus
> diff --git a/include/linux/i3c/master.h b/include/linux/i3c/master.h
> index 043f5c7ff398..e39350b1038e 100644
> --- a/include/linux/i3c/master.h
> +++ b/include/linux/i3c/master.h
> @@ -558,6 +558,22 @@ struct i3c_master_controller {
>  #define i3c_bus_for_each_i3cdev(bus, dev)				\
>  	list_for_each_entry(dev, &(bus)->devs.i3c, common.node)
>
> +/**
> + * struct i3c_dma - DMA transfer and mapping descriptor
> + * @buf: destination/source buffer for DMA
> + * @len: length of transfer
> + * @addr: mapped DMA address for a Host Controller Driver
> + * @dir: DMA direction
> + * @bounce_buf: an allocated bounce buffer if transfer needs it or NULL
> + */
> +struct i3c_dma {
> +	void *buf;
> +	size_t len;

need dma_map_len to avoid swiotlb bounce again.

> +	dma_addr_t addr;
> +	enum dma_data_direction dir;
> +	void *bounce_buf;
> +};
> +
>  int i3c_master_do_i2c_xfers(struct i3c_master_controller *master,
>  			    const struct i2c_msg *xfers,
>  			    int nxfers);
> @@ -575,6 +591,10 @@ int i3c_master_get_free_addr(struct i3c_master_controller *master,
>  int i3c_master_add_i3c_dev_locked(struct i3c_master_controller *master,
>  				  u8 addr);
>  int i3c_master_do_daa(struct i3c_master_controller *master);
> +struct i3c_dma *i3c_master_dma_map_single(struct device *dev, void *ptr,
> +					  size_t len, bool dma_safe,
> +					  enum dma_data_direction dir);
> +void i3c_master_dma_unmap_single(struct device *dev, struct i3c_dma *dma_xfer);

needn't dev, save it into struct i3c_dma, so we can use cleanup easily


#define DEFINE_FREE(i3c_master_dma_unmap_single, struct i3c_dma *, if (_T) i3c_master_dma_map_single(_T))

Frank
>
>  int i3c_master_set_info(struct i3c_master_controller *master,
>  			const struct i3c_device_info *info);
> --
> 2.47.2
>
>
> --
> linux-i3c mailing list
> linux-i3c@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-i3c

-- 
linux-i3c mailing list
linux-i3c@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-i3c

  parent reply	other threads:[~2025-08-01 14:57 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-31 14:14 [PATCH 1/4] i3c: master: Add helpers for DMA mapping and bounce buffer handling Jarkko Nikula
2025-07-31 14:14 ` [PATCH 2/4] i3c: mipi-i3c-hci: Use core helpers for DMA mapping and bounce buffering Jarkko Nikula
2025-08-01 14:41   ` Frank Li
2025-07-31 14:14 ` [PATCH 3/4] i3c: mipi-i3c-hci: Use physical device pointer with DMA API Jarkko Nikula
2025-08-01 16:03   ` Frank Li
2025-08-04 14:34     ` Jarkko Nikula
2025-08-05 15:57       ` Frank Li
2025-07-31 14:14 ` [PATCH 4/4] i3c: mipi-i3c-hci: Use own DMA bounce buffer management for I2C transfers Jarkko Nikula
2025-08-01 14:59   ` Frank Li
2025-08-01 14:54 ` Frank Li [this message]
2025-08-04 13:35   ` [PATCH 1/4] i3c: master: Add helpers for DMA mapping and bounce buffer handling Jarkko Nikula

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aIzVPNnrOkPQ3fcd@lizhi-Precision-Tower-5810 \
    --to=frank.li@nxp.com \
    --cc=alexandre.belloni@bootlin.com \
    --cc=jarkko.nikula@linux.intel.com \
    --cc=linux-i3c@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox