mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-12-27 13:30:45 -05:00
i3c: master: Add helpers for DMA mapping and bounce buffer handling
Some I3C controllers such as MIPI I3C HCI may pad the last DWORD (32-bit) with stale data from the RX FIFO in DMA transfers if the receive length is not DWORD aligned and when the device DMA is IOMMU mapped. In such a case, a properly sized bounce buffer is required in order to avoid possible data corruption. In a review discussion, proposal was to have a common helpers in I3C core for DMA mapping and bounce buffer handling. Drivers may use the helper i3c_master_dma_map_single() to map a buffer for a DMA transfer. It internally allocates a bounce buffer if buffer is not DMA'able or when the driver requires it for a transfer. Helper i3c_master_dma_unmap_single() does the needed cleanups and data copying from the bounce buffer. Signed-off-by: Jarkko Nikula <jarkko.nikula@linux.intel.com> Reviewed-by: Frank Li <Frank.Li@nxp.com> Link: https://lore.kernel.org/r/20250822105630.2820009-2-jarkko.nikula@linux.intel.com Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
This commit is contained in:
committed by
Alexandre Belloni
parent
d515503f3c
commit
f8d9e56aeb
@@ -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
|
||||
* @force_bounce: true, force to use a bounce buffer,
|
||||
* false, function will auto check is a bounce buffer required
|
||||
* @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 force_bounce, enum dma_data_direction dir)
|
||||
{
|
||||
struct i3c_dma *dma_xfer __free(kfree) = NULL;
|
||||
void *bounce __free(kfree) = NULL;
|
||||
void *dma_buf = buf;
|
||||
|
||||
dma_xfer = kzalloc(sizeof(*dma_xfer), GFP_KERNEL);
|
||||
if (!dma_xfer)
|
||||
return NULL;
|
||||
|
||||
dma_xfer->dev = dev;
|
||||
dma_xfer->buf = buf;
|
||||
dma_xfer->dir = dir;
|
||||
dma_xfer->len = len;
|
||||
dma_xfer->map_len = len;
|
||||
|
||||
if (is_vmalloc_addr(buf))
|
||||
force_bounce = true;
|
||||
|
||||
if (force_bounce) {
|
||||
dma_xfer->map_len = ALIGN(len, cache_line_size());
|
||||
if (dir == DMA_FROM_DEVICE)
|
||||
bounce = kzalloc(dma_xfer->map_len, GFP_KERNEL);
|
||||
else
|
||||
bounce = kmemdup(buf, dma_xfer->map_len, GFP_KERNEL);
|
||||
if (!bounce)
|
||||
return NULL;
|
||||
dma_buf = bounce;
|
||||
}
|
||||
|
||||
dma_xfer->addr = dma_map_single(dev, dma_buf, dma_xfer->map_len, dir);
|
||||
if (dma_mapping_error(dev, dma_xfer->addr))
|
||||
return NULL;
|
||||
|
||||
dma_xfer->bounce_buf = no_free_ptr(bounce);
|
||||
return no_free_ptr(dma_xfer);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(i3c_master_dma_map_single);
|
||||
|
||||
/**
|
||||
* i3c_master_dma_unmap_single() - Unmap buffer after DMA
|
||||
* @dma_xfer: DMA transfer and mapping descriptor
|
||||
*
|
||||
* Unmap buffer and cleanup DMA transfer descriptor.
|
||||
*/
|
||||
void i3c_master_dma_unmap_single(struct i3c_dma *dma_xfer)
|
||||
{
|
||||
dma_unmap_single(dma_xfer->dev, dma_xfer->addr,
|
||||
dma_xfer->map_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
|
||||
|
||||
@@ -558,6 +558,26 @@ 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
|
||||
* @dev: device object of a device doing DMA
|
||||
* @buf: destination/source buffer for DMA
|
||||
* @len: length of transfer
|
||||
* @map_len: length of DMA mapping
|
||||
* @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 {
|
||||
struct device *dev;
|
||||
void *buf;
|
||||
size_t len;
|
||||
size_t map_len;
|
||||
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 +595,12 @@ 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 force_bounce,
|
||||
enum dma_data_direction dir);
|
||||
void i3c_master_dma_unmap_single(struct i3c_dma *dma_xfer);
|
||||
DEFINE_FREE(i3c_master_dma_unmap_single, void *,
|
||||
if (_T) i3c_master_dma_unmap_single(_T))
|
||||
|
||||
int i3c_master_set_info(struct i3c_master_controller *master,
|
||||
const struct i3c_device_info *info);
|
||||
|
||||
Reference in New Issue
Block a user