linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Mathias Nyman <mathias.nyman@linux.intel.com>
To: Wesley Cheng <quic_wcheng@quicinc.com>,
	srinivas.kandagatla@linaro.org, mathias.nyman@intel.com,
	perex@perex.cz, conor+dt@kernel.org, dmitry.torokhov@gmail.com,
	corbet@lwn.net, broonie@kernel.org, lgirdwood@gmail.com,
	krzk+dt@kernel.org, pierre-louis.bossart@linux.intel.com,
	Thinh.Nguyen@synopsys.com, tiwai@suse.com, robh@kernel.org,
	gregkh@linuxfoundation.org
Cc: linux-kernel@vger.kernel.org, devicetree@vger.kernel.org,
	linux-sound@vger.kernel.org, linux-usb@vger.kernel.org,
	linux-input@vger.kernel.org, linux-arm-msm@vger.kernel.org,
	linux-doc@vger.kernel.org
Subject: Re: [PATCH v30 02/30] xhci: sec-intr: add initial api to register a secondary interrupter entity
Date: Wed, 20 Nov 2024 16:36:07 +0200	[thread overview]
Message-ID: <9b86a2c9-de7f-46b7-b63d-451ebc9c87dd@linux.intel.com> (raw)
In-Reply-To: <20241106193413.1730413-3-quic_wcheng@quicinc.com>

On 6.11.2024 21.33, Wesley Cheng wrote:
> From: Mathias Nyman <mathias.nyman@linux.intel.com>
> 
> Introduce XHCI sec intr, which manages the USB endpoints being requested by
> a client driver.  This is used for when client drivers are attempting to
> offload USB endpoints to another entity for handling USB transfers.  XHCI
> sec intr will allow for drivers to fetch the required information about the
> transfer ring, so the user can submit transfers independently.  Expose the
> required APIs for drivers to register and request for a USB endpoint and to
> manage XHCI secondary interrupters.
> 
> Driver renaming, multiple ring segment page linking, proper endpoint clean
> up, and allowing module compilation added by Wesley Cheng to complete
> original concept code by Mathias Nyman.
> 
> Signed-off-by: Mathias Nyman <mathias.nyman@linux.intel.com>
> Co-developed-by: Wesley Cheng <quic_wcheng@quicinc.com>
> Signed-off-by: Wesley Cheng <quic_wcheng@quicinc.com>
> ---
>   drivers/usb/host/Kconfig          |  11 +
>   drivers/usb/host/Makefile         |   2 +
>   drivers/usb/host/xhci-sec-intr.c  | 438 ++++++++++++++++++++++++++++++
>   drivers/usb/host/xhci.h           |   4 +
>   include/linux/usb/xhci-sec-intr.h |  70 +++++
>   5 files changed, 525 insertions(+)
>   create mode 100644 drivers/usb/host/xhci-sec-intr.c
>   create mode 100644 include/linux/usb/xhci-sec-intr.h
> 
> diff --git a/drivers/usb/host/Kconfig b/drivers/usb/host/Kconfig
> index d011d6c753ed..a2d549e3e076 100644
> --- a/drivers/usb/host/Kconfig
> +++ b/drivers/usb/host/Kconfig
> @@ -104,6 +104,17 @@ config USB_XHCI_RZV2M
>   	  Say 'Y' to enable the support for the xHCI host controller
>   	  found in Renesas RZ/V2M SoC.
>   
> +config USB_XHCI_SEC_INTR
> +	tristate "xHCI support for secondary interrupter management"
> +	help
> +	  Say 'Y' to enable the support for the xHCI secondary management.
> +	  Provide a mechanism for a sideband datapath for payload associated
> +	  with audio class endpoints. This allows for an audio DSP to use
> +	  xHCI USB endpoints directly, allowing CPU to sleep while playing
> +	  audio.  This is not the same feature as the audio sideband
> +	  capability mentioned within the xHCI specification, and continues
> +	  to utilize main system memory for data transfers.

This same API should be used for the hardware xHCI sideband capability.
We should add a function that checks which types of xHC sideband capability xHC
hardware can support, and pick and pass a type to xhci xhci_sec_intr_register()
when registering a sideband/sec_intr

> +
>   config USB_XHCI_TEGRA
>   	tristate "xHCI support for NVIDIA Tegra SoCs"
>   	depends on PHY_TEGRA_XUSB
> diff --git a/drivers/usb/host/Makefile b/drivers/usb/host/Makefile
> index be4e5245c52f..d4b127f48cf9 100644
> --- a/drivers/usb/host/Makefile
> +++ b/drivers/usb/host/Makefile
> @@ -32,6 +32,8 @@ endif
>   xhci-rcar-hcd-y				+= xhci-rcar.o
>   xhci-rcar-hcd-$(CONFIG_USB_XHCI_RZV2M)	+= xhci-rzv2m.o
>   
> +obj-$(CONFIG_USB_XHCI_SEC_INTR) += xhci-sec-intr.o
> +
>   obj-$(CONFIG_USB_PCI)	+= pci-quirks.o
>   
>   obj-$(CONFIG_USB_EHCI_HCD)	+= ehci-hcd.o
> diff --git a/drivers/usb/host/xhci-sec-intr.c b/drivers/usb/host/xhci-sec-intr.c
> new file mode 100644
> index 000000000000..b112c3388368
> --- /dev/null
> +++ b/drivers/usb/host/xhci-sec-intr.c
> @@ -0,0 +1,438 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +/*
> + * xHCI host controller secondary interrupter management
> + *
> + * Provides logic for client drivers that support utilizing xHCI secondary
> + * interrupters.
> + *
> + * Copyright (c) 2023-2024, Intel Corporation.
> + *
> + * Author: Mathias Nyman
> + */
> +
> +#include <linux/usb/xhci-sec-intr.h>
> +#include <linux/dma-direct.h>
> +
> +#include "xhci.h"
> +
> +/* internal helpers */
> +static struct sg_table *
> +xhci_ring_to_sgtable(struct xhci_sec_intr *si, struct xhci_ring *ring)
> +{
> +	struct xhci_segment *seg;
> +	struct sg_table	*sgt;
> +	unsigned int n_pages;
> +	struct page **pages;
> +	struct device *dev;
> +	size_t sz;
> +	int i;
> +
> +	dev = xhci_to_hcd(si->xhci)->self.sysdev;
> +	sz = ring->num_segs * TRB_SEGMENT_SIZE;
> +	n_pages = PAGE_ALIGN(sz) >> PAGE_SHIFT;
> +	pages = kvmalloc_array(n_pages, sizeof(struct page *), GFP_KERNEL);
> +	if (!pages)
> +		return NULL;
> +
> +	sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
> +	if (!sgt) {
> +		kvfree(pages);
> +		return NULL;
> +	}
> +
> +	seg = ring->first_seg;
> +	if (!seg)
> +		goto err;
> +	/*
> +	 * Rings can potentially have multiple segments, create an array that
> +	 * carries page references to allocated segments.  Utilize the
> +	 * sg_alloc_table_from_pages() to create the sg table, and to ensure
> +	 * that page links are created.
> +	 */
> +	for (i = 0; i < ring->num_segs; i++) {
> +		dma_get_sgtable(dev, sgt, seg->trbs, seg->dma,
> +				TRB_SEGMENT_SIZE);
> +		pages[i] = sg_page(sgt->sgl);
> +		sg_free_table(sgt);
> +		seg = seg->next;
> +	}
> +
> +	if (sg_alloc_table_from_pages(sgt, pages, n_pages, 0, sz, GFP_KERNEL))
> +		goto err;
> +
> +	/*
> +	 * Save first segment dma address to sg dma_address field for the sideband
> +	 * client to have access to the IOVA of the ring.
> +	 */
> +	sg_dma_address(sgt->sgl) = ring->first_seg->dma;
> +
> +	return sgt;
> +
> +err:
> +	kvfree(pages);
> +	kfree(sgt);
> +
> +	return NULL;
> +}
> +
> +static void
> +__xhci_sec_intr_remove_endpoint(struct xhci_sec_intr *si, struct xhci_virt_ep *ep)
> +{
> +	/*
> +	 * Issue a stop endpoint command when an endpoint is removed.
> +	 * The stop ep cmd handler will handle the ring cleanup.
> +	 */
> +	xhci_stop_endpoint_sync(si->xhci, ep, 0, GFP_KERNEL);
> +
> +	ep->sec = NULL;
> +	si->eps[ep->ep_index] = NULL;
> +}
> +
> +/* endpoint api functions */
> +
> +/**
> + * xhci_sec_intr_add_endpoint - add endpoint to access list
> + * @si: secondary interrupter instance for this usb device
> + * @host_ep: usb host endpoint
> + *
> + * Adds an endpoint to the list of endpoints utilizing secondary interrupters
> + * for this usb device.
> + * After an endpoint is added the client can get the endpoint transfer ring
> + * buffer by calling xhci_sec_intr_get_endpoint_buffer()
> + *
> + * Return: 0 on success, negative error otherwise.
> + */
> +int
> +xhci_sec_intr_add_endpoint(struct xhci_sec_intr *si,
> +			   struct usb_host_endpoint *host_ep)
> +{
> +	struct xhci_virt_ep *ep;
> +	unsigned int ep_index;
> +
> +	mutex_lock(&si->mutex);
> +	ep_index = xhci_get_endpoint_index(&host_ep->desc);
> +	ep = &si->vdev->eps[ep_index];
> +
> +	if (ep->ep_state & EP_HAS_STREAMS) {
> +		mutex_unlock(&si->mutex);
> +		return -EINVAL;
> +	}
> +
> +	/*
> +	 * Note, we don't know the DMA mask of the audio DSP device, if its
> +	 * smaller than for xhci it won't be able to access the endpoint ring
> +	 * buffer. This could be solved by not allowing the audio class driver
> +	 * to add the endpoint the normal way, but instead offload it immediately,
> +	 * and let this function add the endpoint and allocate the ring buffer
> +	 * with the smallest common DMA mask
> +	 */
> +	if (si->eps[ep_index] || ep->sec) {
> +		mutex_unlock(&si->mutex);
> +		return -EBUSY;
> +	}
> +
> +	ep->sec = si;
> +	si->eps[ep_index] = ep;
> +	mutex_unlock(&si->mutex);

We should probably check in xhci-mem.c if ep->sec is set before freeing the
endpoint ring.
We don't want the sideband client driver to touch freed rings.
Maybe we even need a way for xhci driver to notify this sideband/sec_intr client
in case a offloaded device or endpoint is being freed.

I guess usb core in most cases ensures class drivers are properly removed,
and thus this sideband/sec_interrupt should be unregistered before xhci starts
freeing endpoints, but I'm not sure sure this is true in all corner cases.
This is the first time we share endpoint ring addresses.

Thanks
Mathias


  reply	other threads:[~2024-11-20 14:34 UTC|newest]

Thread overview: 81+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-06 19:33 [PATCH v30 00/30] Introduce QC USB SND audio offloading support Wesley Cheng
2024-11-06 19:33 ` [PATCH v30 01/30] usb: host: xhci: Repurpose event handler for skipping interrupter events Wesley Cheng
2024-11-20 11:48   ` Mathias Nyman
2024-11-20 18:48     ` Wesley Cheng
2024-11-06 19:33 ` [PATCH v30 02/30] xhci: sec-intr: add initial api to register a secondary interrupter entity Wesley Cheng
2024-11-20 14:36   ` Mathias Nyman [this message]
2024-11-21  1:34     ` Wesley Cheng
2024-11-21 19:15       ` Mathias Nyman
2024-11-21 20:24         ` Wesley Cheng
2024-11-06 19:33 ` [PATCH v30 03/30] usb: host: xhci-mem: Cleanup pending secondary event ring events Wesley Cheng
2024-11-06 19:33 ` [PATCH v30 04/30] usb: host: xhci-mem: Allow for interrupter clients to choose specific index Wesley Cheng
2024-11-06 19:33 ` [PATCH v30 05/30] usb: host: xhci-plat: Set XHCI max interrupters if property is present Wesley Cheng
2024-11-06 19:33 ` [PATCH v30 06/30] usb: dwc3: Specify maximum number of XHCI interrupters Wesley Cheng
2024-11-06 19:33 ` [PATCH v30 07/30] ALSA: Add USB audio device jack type Wesley Cheng
2024-11-20 11:51   ` Takashi Iwai
2024-11-06 19:33 ` [PATCH v30 08/30] ALSA: usb-audio: Export USB SND APIs for modules Wesley Cheng
2024-11-06 19:33 ` [PATCH v30 09/30] ALSA: usb-audio: Check for support for requested audio format Wesley Cheng
2024-11-06 19:33 ` [PATCH v30 10/30] ALSA: usb-audio: Save UAC sample size information Wesley Cheng
2024-11-06 19:33 ` [PATCH v30 11/30] ALSA: usb-audio: Prevent starting of audio stream if in use Wesley Cheng
2024-11-06 19:33 ` [PATCH v30 12/30] ASoC: Add SOC USB APIs for adding an USB backend Wesley Cheng
2024-11-06 19:33 ` [PATCH v30 13/30] ASoC: usb: Add PCM format check API for " Wesley Cheng
2024-11-06 19:33 ` [PATCH v30 14/30] ASoC: usb: Create SOC USB SND jack kcontrol Wesley Cheng
2024-12-03 16:14   ` Cezary Rojewski
2024-12-03 23:52     ` Wesley Cheng
2024-11-06 19:33 ` [PATCH v30 15/30] ASoC: usb: Fetch ASoC card and pcm device information Wesley Cheng
2024-11-20 12:23   ` Takashi Iwai
2024-11-20 22:36     ` Wesley Cheng
2024-11-06 19:33 ` [PATCH v30 16/30] ASoC: doc: Add documentation for SOC USB Wesley Cheng
2024-11-06 19:34 ` [PATCH v30 17/30] ASoC: dt-bindings: qcom,q6dsp-lpass-ports: Add USB_RX port Wesley Cheng
2024-11-06 19:34 ` [PATCH v30 18/30] ASoC: dt-bindings: Update example for enabling USB offload on SM8250 Wesley Cheng
2024-11-06 19:34 ` [PATCH v30 19/30] ASoC: qcom: qdsp6: Introduce USB AFE port to q6dsp Wesley Cheng
2024-11-06 19:34 ` [PATCH v30 20/30] ASoC: qcom: qdsp6: q6afe: Increase APR timeout Wesley Cheng
2024-11-06 19:34 ` [PATCH v30 21/30] ASoC: qcom: qdsp6: Add USB backend ASoC driver for Q6 Wesley Cheng
2024-11-06 19:34 ` [PATCH v30 22/30] ASoC: qcom: qdsp6: Add headphone jack for offload connection status Wesley Cheng
2024-11-06 19:34 ` [PATCH v30 23/30] ASoC: qcom: qdsp6: Fetch USB offload mapped card and PCM device Wesley Cheng
2024-11-06 19:34 ` [PATCH v30 24/30] ALSA: usb-audio: Introduce USB SND platform op callbacks Wesley Cheng
2024-11-06 19:34 ` [PATCH v30 25/30] ALSA: usb-audio: qcom: Add USB QMI definitions Wesley Cheng
2024-11-06 19:34 ` [PATCH v30 26/30] ALSA: usb-audio: qcom: Introduce QC USB SND offloading support Wesley Cheng
2024-11-20 12:15   ` Takashi Iwai
2024-11-20 22:10     ` Wesley Cheng
2024-11-06 19:34 ` [PATCH v30 27/30] ALSA: usb-audio: qcom: Don't allow USB offload path if PCM device is in use Wesley Cheng
2024-11-06 19:34 ` [PATCH v30 28/30] ALSA: usb-audio: Add USB offload route kcontrol Wesley Cheng
2024-11-20 12:12   ` Takashi Iwai
2024-11-20 19:13     ` Wesley Cheng
2024-11-21 15:50       ` Takashi Iwai
2024-11-25 20:33         ` Wesley Cheng
2024-11-26 14:14           ` Takashi Iwai
2024-11-26 23:19             ` Wesley Cheng
2024-12-03 16:13   ` Cezary Rojewski
2024-12-03 23:15     ` Wesley Cheng
2024-12-06  9:09       ` Cezary Rojewski
2024-12-06 20:43         ` Wesley Cheng
2024-12-10 15:24           ` Cezary Rojewski
2024-12-10 16:52             ` Takashi Iwai
2024-12-06 23:35         ` Wesley Cheng
2024-11-06 19:34 ` [PATCH v30 29/30] ALSA: usb-audio: Allow for rediscovery of connected USB SND devices Wesley Cheng
2024-11-06 19:34 ` [PATCH v30 30/30] ASoC: usb: Rediscover USB SND devices on USB port add Wesley Cheng
2024-11-15 22:42 ` [PATCH v30 00/30] Introduce QC USB SND audio offloading support Wesley Cheng
2024-11-16  7:42   ` Greg KH
2024-11-19 17:50     ` Wesley Cheng
2024-11-20 12:39       ` Takashi Iwai
2024-11-20 23:18         ` Wesley Cheng
2024-12-01  3:14           ` Pierre-Louis Bossart
2024-12-03 16:17             ` Cezary Rojewski
2024-12-03 16:57               ` Greg KH
2024-12-04 21:14                 ` Cezary Rojewski
2024-12-05  1:15                   ` Wesley Cheng
2024-12-05  6:50                     ` Greg KH
2024-12-03 20:38               ` Wesley Cheng
2024-12-04 22:01                 ` Cezary Rojewski
2024-12-06  0:28                   ` Wesley Cheng
2024-12-10 15:18                     ` Cezary Rojewski
2024-12-10 22:20                       ` Wesley Cheng
2024-12-17 23:20                       ` Pierre-Louis Bossart
     [not found]               ` <4C900353-B977-451C-B003-BAA51E458726@linux.dev>
2024-12-04 22:11                 ` Cezary Rojewski
     [not found]                   ` <4E9925AF-F297-42A5-9CB8-F8568F0A5EDF@linux.dev>
2024-12-06  0:53                     ` Wesley Cheng
2024-12-10  0:59                       ` Wesley Cheng
2024-12-10 16:40                         ` Takashi Iwai
2024-12-11  2:00                           ` Wesley Cheng
2024-12-13  9:10                             ` Guan-Yu Lin
2024-12-03 16:16   ` Cezary Rojewski

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=9b86a2c9-de7f-46b7-b63d-451ebc9c87dd@linux.intel.com \
    --to=mathias.nyman@linux.intel.com \
    --cc=Thinh.Nguyen@synopsys.com \
    --cc=broonie@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=corbet@lwn.net \
    --cc=devicetree@vger.kernel.org \
    --cc=dmitry.torokhov@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=krzk+dt@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=mathias.nyman@intel.com \
    --cc=perex@perex.cz \
    --cc=pierre-louis.bossart@linux.intel.com \
    --cc=quic_wcheng@quicinc.com \
    --cc=robh@kernel.org \
    --cc=srinivas.kandagatla@linaro.org \
    --cc=tiwai@suse.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;
as well as URLs for NNTP newsgroup(s).