From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Xu Yang <xu.yang_2@nxp.com>
Cc: ezequiel@vanguardiasur.com.ar, mchehab@kernel.org,
laurent.pinchart@ideasonboard.com, hdegoede@redhat.com,
gregkh@linuxfoundation.org, mingo@kernel.org, tglx@linutronix.de,
viro@zeniv.linux.org.uk, thomas.weissschuh@linutronix.de,
linux-media@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-usb@vger.kernel.org, imx@lists.linux.dev, jun.li@nxp.com
Subject: Re: [PATCH v2 1/3] usb: core: add dma-noncoherent buffer alloc and free API
Date: Fri, 27 Jun 2025 14:22:59 +0300 [thread overview]
Message-ID: <aF5_EwSHHMOOMRv9@smile.fi.intel.com> (raw)
In-Reply-To: <20250627101939.3649295-2-xu.yang_2@nxp.com>
On Fri, Jun 27, 2025 at 06:19:37PM +0800, Xu Yang wrote:
> This will add usb_alloc_noncoherent() and usb_free_noncoherent()
> functions to support alloc and free buffer in a dma-noncoherent way.
>
> To explicit manage the memory ownership for the kernel and device,
> this will also add usb_dma_noncoherent_sync_for_cpu/device() functions
> and call it at proper time. The management requires the user save
> sg_table returned by usb_alloc_noncoherent() to urb->sgt.
...
> +/**
> + * usb_alloc_noncoherent - allocate dma-noncoherent buffer for URB_NO_xxx_DMA_MAP
> + * @dev: device the buffer will be used with
> + * @size: requested buffer size
> + * @mem_flags: affect whether allocation may block
> + * @dma: used to return DMA address of buffer
> + * @dir: dma transfer direction
> + * @table: used to return sg_table of allocated memory
> + *
> + * Return: Either null (indicating no buffer could be allocated), or the
> + * cpu-space pointer to a buffer that may be used to perform DMA to the
> + * specified device. Such cpu-space buffers are returned along with the DMA
> + * address (through the pointer provided).
Return section should be last in the kernel-doc (this requirement is
documented).
> + * To explicit manage the memory ownership for the kernel vs the device by
> + * usb core, the user needs save sg_table to urb->sgt. Then usb core will
> + * do dma sync for cpu and device properly.
> + *
> + * When the buffer is no longer used, free it with usb_free_noncoherent().
Here and everywhere else in your series, pay respect to acronyms by using them
as acronyms:
dma --> DMA
cpu --> CPU
usb --> USB
and so on...
> + */
> +void *usb_alloc_noncoherent(struct usb_device *dev, size_t size,
> + gfp_t mem_flags, dma_addr_t *dma,
> + enum dma_data_direction dir,
> + struct sg_table **table)
> +{
> + struct device *dmadev;
> + struct sg_table *sgt;
> + void *buffer;
> +
> + if (!dev || !dev->bus)
When !dev case is possible?
> + return NULL;
> +
> + dmadev = bus_to_hcd(dev->bus)->self.sysdev;
> +
> + sgt = dma_alloc_noncontiguous(dmadev, size, dir, mem_flags, 0);
> + if (!sgt)
> + return NULL;
> +
> + buffer = dma_vmap_noncontiguous(dmadev, size, sgt);
> + if (!buffer) {
> + dma_free_noncontiguous(dmadev, size, sgt, dir);
> + return NULL;
> + }
> +
> + *table = sgt;
> + *dma = sg_dma_address(sgt->sgl);
> +
> + return buffer;
> +}
...
> +void usb_free_noncoherent(struct usb_device *dev, size_t size,
> + void *addr, enum dma_data_direction dir,
> + struct sg_table *table)
> +{
> + struct device *dmadev;
> +
> + if (!dev || !dev->bus)
Ditto.
> + return;
> + if (!addr)
> + return;
> +
> + dmadev = bus_to_hcd(dev->bus)->self.sysdev;
> + dma_vunmap_noncontiguous(dmadev, addr);
> + dma_free_noncontiguous(dmadev, size, table, dir);
> +}
--
With Best Regards,
Andy Shevchenko
next prev parent reply other threads:[~2025-06-27 11:23 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-27 10:19 [PATCH v2 0/3] add dma noncoherent API Xu Yang
2025-06-27 10:19 ` [PATCH v2 1/3] usb: core: add dma-noncoherent buffer alloc and free API Xu Yang
2025-06-27 11:22 ` Andy Shevchenko [this message]
2025-06-30 7:26 ` Xu Yang
2025-06-27 14:23 ` Alan Stern
2025-06-29 23:39 ` Laurent Pinchart
2025-06-30 6:48 ` Ricardo Ribalda
2025-06-30 8:23 ` Laurent Pinchart
2025-06-30 13:37 ` Christoph Hellwig
2025-06-30 8:45 ` Xu Yang
2025-06-30 8:18 ` Xu Yang
2025-06-30 14:16 ` Alan Stern
2025-06-30 17:38 ` Laurent Pinchart
2025-07-01 9:32 ` Xu Yang
2025-06-27 10:19 ` [PATCH v2 2/3] media: uvcvideo: use usb_alloc_noncoherent/usb_free_noncoherent() Xu Yang
2025-06-27 10:19 ` [PATCH v2 3/3] media: stk1160: " Xu Yang
2025-06-27 11:25 ` [PATCH v2 0/3] add dma noncoherent API Andy Shevchenko
2025-06-30 7:28 ` Xu Yang
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=aF5_EwSHHMOOMRv9@smile.fi.intel.com \
--to=andriy.shevchenko@linux.intel.com \
--cc=ezequiel@vanguardiasur.com.ar \
--cc=gregkh@linuxfoundation.org \
--cc=hdegoede@redhat.com \
--cc=imx@lists.linux.dev \
--cc=jun.li@nxp.com \
--cc=laurent.pinchart@ideasonboard.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=mchehab@kernel.org \
--cc=mingo@kernel.org \
--cc=tglx@linutronix.de \
--cc=thomas.weissschuh@linutronix.de \
--cc=viro@zeniv.linux.org.uk \
--cc=xu.yang_2@nxp.com \
/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