[PATCH V4 16/17] i3c: mipi-i3c-hci: Consolidate DMA ring allocation
Frank Li
Frank.li at nxp.com
Tue Jun 2 09:49:52 PDT 2026
On Fri, May 15, 2026 at 07:26:20PM +0300, Adrian Hunter wrote:
> dma_alloc_coherent() allocates memory in whole pages, which can waste
> space when command and response queues are allocated separately.
>
> Allocate the DMA command and response queues from a single coherent
> allocation instead, while preserving the required 4-byte alignment.
>
> This reduces memory overhead without changing behavior.
>
> Signed-off-by: Adrian Hunter <adrian.hunter at intel.com>
> ---
Reviewed-by: Frank Li <Frank.Li at nxp.com>
>
>
> Changes in V4:
>
> Cache allocation size in rh->xfer_alloc_sz to avoid recomputing in
> hci_dma_free()
>
> Changes in V3:
>
> None
>
> Changes in V2:
>
> Check for failed allocation before assignments to avoid doing
> arithmetic with NULL pointers
>
>
> drivers/i3c/master/mipi-i3c-hci/dma.c | 21 ++++++++-------------
> 1 file changed, 8 insertions(+), 13 deletions(-)
>
> diff --git a/drivers/i3c/master/mipi-i3c-hci/dma.c b/drivers/i3c/master/mipi-i3c-hci/dma.c
> index 9a01c740760f..0136f3064ada 100644
> --- a/drivers/i3c/master/mipi-i3c-hci/dma.c
> +++ b/drivers/i3c/master/mipi-i3c-hci/dma.c
> @@ -130,7 +130,7 @@ struct hci_rh_data {
> dma_addr_t xfer_dma, resp_dma, ibi_status_dma, ibi_data_dma;
> unsigned int xfer_entries, ibi_status_entries, ibi_chunks_total;
> unsigned int xfer_struct_sz, resp_struct_sz, ibi_status_sz, ibi_chunk_sz;
> - unsigned int done_ptr, ibi_chunk_ptr, xfer_space;
> + unsigned int xfer_alloc_sz, done_ptr, ibi_chunk_ptr, xfer_space;
> struct hci_xfer **src_xfers;
> struct completion op_done;
> };
> @@ -187,13 +187,7 @@ static void hci_dma_free(void *data)
> rh = &rings->headers[i];
>
> if (rh->xfer)
> - dma_free_coherent(rings->sysdev,
> - rh->xfer_struct_sz * rh->xfer_entries,
> - rh->xfer, rh->xfer_dma);
> - if (rh->resp)
> - dma_free_coherent(rings->sysdev,
> - rh->resp_struct_sz * rh->xfer_entries,
> - rh->resp, rh->resp_dma);
> + dma_free_coherent(rings->sysdev, rh->xfer_alloc_sz, rh->xfer, rh->xfer_dma);
> kfree(rh->src_xfers);
> if (rh->ibi_status)
> dma_free_coherent(rings->sysdev,
> @@ -359,18 +353,19 @@ static int hci_dma_init(struct i3c_hci *hci)
> dev_dbg(&hci->master.dev,
> "xfer_struct_sz = %d, resp_struct_sz = %d",
> rh->xfer_struct_sz, rh->resp_struct_sz);
> - xfers_sz = rh->xfer_struct_sz * rh->xfer_entries;
> + xfers_sz = round_up(rh->xfer_struct_sz * rh->xfer_entries, 4);
> resps_sz = rh->resp_struct_sz * rh->xfer_entries;
> + rh->xfer_alloc_sz = xfers_sz + resps_sz;
>
> - rh->xfer = dma_alloc_coherent(rings->sysdev, xfers_sz,
> + rh->xfer = dma_alloc_coherent(rings->sysdev, rh->xfer_alloc_sz,
> &rh->xfer_dma, GFP_KERNEL);
> - rh->resp = dma_alloc_coherent(rings->sysdev, resps_sz,
> - &rh->resp_dma, GFP_KERNEL);
> rh->src_xfers =
> kzalloc_objs(*rh->src_xfers, rh->xfer_entries);
> ret = -ENOMEM;
> - if (!rh->xfer || !rh->resp || !rh->src_xfers)
> + if (!rh->xfer || !rh->src_xfers)
> goto err_out;
> + rh->resp = rh->xfer + xfers_sz;
> + rh->resp_dma = rh->xfer_dma + xfers_sz;
>
> /* IBIs */
>
> --
> 2.51.0
>
More information about the linux-i3c
mailing list