All of lore.kernel.org
 help / color / mirror / Atom feed
From: Wesley Cheng <quic_wcheng@quicinc.com>
To: <srinivas.kandagatla@linaro.org>, <mathias.nyman@intel.com>,
	<perex@perex.cz>, <lgirdwood@gmail.com>, <andersson@kernel.org>,
	<krzysztof.kozlowski+dt@linaro.org>, <gregkh@linuxfoundation.org>,
	<Thinh.Nguyen@synopsys.com>, <broonie@kernel.org>,
	<bgoswami@quicinc.com>, <tiwai@suse.com>, <robh+dt@kernel.org>,
	<agross@kernel.org>
Cc: <linux-kernel@vger.kernel.org>, <linux-arm-msm@vger.kernel.org>,
	<alsa-devel@alsa-project.org>, <devicetree@vger.kernel.org>,
	<linux-usb@vger.kernel.org>, <quic_jackp@quicinc.com>,
	<quic_plai@quicinc.com>, Wesley Cheng <quic_wcheng@quicinc.com>
Subject: [RFC PATCH v2 06/22] usb: host: xhci-mem: Cleanup pending secondary event ring events
Date: Wed, 25 Jan 2023 19:14:08 -0800	[thread overview]
Message-ID: <20230126031424.14582-7-quic_wcheng@quicinc.com> (raw)
In-Reply-To: <20230126031424.14582-1-quic_wcheng@quicinc.com>

As part of xHCI bus suspend, the XHCI is halted.  However, if there are
pending events in the secondary event ring, it is observed that the xHCI
controller stops responding to further commands upon host or device
initiated bus resume.  Iterate through all pending events and updating the
dequeue pointer to the last pending event trb.

Signed-off-by: Wesley Cheng <quic_wcheng@quicinc.com>
---
 drivers/usb/host/xhci-mem.c | 74 ++++++++++++++++++++++++++++++++++---
 1 file changed, 69 insertions(+), 5 deletions(-)

diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
index 45ac77a5d8e4..5c5266c18910 100644
--- a/drivers/usb/host/xhci-mem.c
+++ b/drivers/usb/host/xhci-mem.c
@@ -1819,17 +1819,85 @@ int xhci_alloc_erst(struct xhci_hcd *xhci,
 	return 0;
 }
 
+static void xhci_handle_sec_intr_events(struct xhci_hcd *xhci,
+	struct xhci_ring *ring,	struct xhci_intr_reg __iomem *ir_set,
+	struct xhci_erst *erst)
+{
+	union xhci_trb *erdp_trb, *current_trb;
+	struct xhci_segment	*seg;
+	u64 erdp_reg;
+	u32 iman_reg;
+	dma_addr_t deq;
+	unsigned long segment_offset;
+
+	/* disable irq, ack pending interrupt and ack all pending events */
+	iman_reg = readl_relaxed(&ir_set->irq_pending);
+	iman_reg &= ~IMAN_IE;
+	writel_relaxed(iman_reg, &ir_set->irq_pending);
+	iman_reg = readl_relaxed(&ir_set->irq_pending);
+	if (iman_reg & IMAN_IP)
+		writel_relaxed(iman_reg, &ir_set->irq_pending);
+
+	/* last acked event trb is in erdp reg  */
+	erdp_reg = xhci_read_64(xhci, &ir_set->erst_dequeue);
+	deq = (dma_addr_t)(erdp_reg & ~ERST_PTR_MASK);
+	if (!deq) {
+		xhci_dbg(xhci, "event ring handling not required\n");
+		return;
+	}
+
+	seg = ring->first_seg;
+	segment_offset = deq - seg->dma;
+
+	/* find out virtual address of the last acked event trb */
+	erdp_trb = current_trb = &seg->trbs[0] +
+				(segment_offset/sizeof(*current_trb));
+
+	/* read cycle state of the last acked trb to find out CCS */
+	ring->cycle_state = le32_to_cpu(current_trb->event_cmd.flags) & TRB_CYCLE;
+
+	while (1) {
+		/* last trb of the event ring: toggle cycle state */
+		if (current_trb == &seg->trbs[TRBS_PER_SEGMENT - 1]) {
+			ring->cycle_state ^= 1;
+			current_trb = &seg->trbs[0];
+		} else {
+			current_trb++;
+		}
+
+		/* cycle state transition */
+		if ((le32_to_cpu(current_trb->event_cmd.flags) & TRB_CYCLE) !=
+		    ring->cycle_state)
+			break;
+	}
+
+	if (erdp_trb != current_trb) {
+		deq = xhci_trb_virt_to_dma(ring->deq_seg, current_trb);
+		if (deq == 0)
+			xhci_warn(xhci,
+				"WARN invalid SW event ring dequeue ptr.\n");
+		/* Update HC event ring dequeue pointer */
+		erdp_reg &= ERST_PTR_MASK;
+		erdp_reg |= ((u64) deq & (u64) ~ERST_PTR_MASK);
+	}
+
+	/* Clear the event handler busy flag (RW1C); event ring is empty. */
+	erdp_reg |= ERST_EHB;
+	xhci_write_64(xhci, erdp_reg, &ir_set->erst_dequeue);
+}
+
 static void
 xhci_free_interrupter(struct xhci_hcd *xhci, struct xhci_interrupter *ir)
 {
 	struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
 	size_t erst_size;
-	u64 tmp64;
 	u32 tmp;
 
 	if (!ir)
 		return;
 
+	xhci_handle_sec_intr_events(xhci, ir->event_ring, ir->ir_set, &ir->erst);
+
 	erst_size = sizeof(struct xhci_erst_entry) * (ir->erst.num_entries);
 	if (ir->erst.entries)
 		dma_free_coherent(dev, erst_size,
@@ -1842,10 +1910,6 @@ xhci_free_interrupter(struct xhci_hcd *xhci, struct xhci_interrupter *ir)
 	tmp &= ERST_SIZE_MASK;
 	writel(tmp, &ir->ir_set->erst_size);
 
-	tmp64 = xhci_read_64(xhci, &ir->ir_set->erst_dequeue);
-	tmp64 &= (u64) ERST_PTR_MASK;
-	xhci_write_64(xhci, tmp64, &ir->ir_set->erst_dequeue);
-
 	/* free interrrupter event ring */
 	if (ir->event_ring)
 		xhci_ring_free(xhci, ir->event_ring);

WARNING: multiple messages have this Message-ID (diff)
From: Wesley Cheng <quic_wcheng@quicinc.com>
To: <srinivas.kandagatla@linaro.org>, <mathias.nyman@intel.com>,
	<perex@perex.cz>, <lgirdwood@gmail.com>, <andersson@kernel.org>,
	<krzysztof.kozlowski+dt@linaro.org>, <gregkh@linuxfoundation.org>,
	<Thinh.Nguyen@synopsys.com>, <broonie@kernel.org>,
	<bgoswami@quicinc.com>, <tiwai@suse.com>, <robh+dt@kernel.org>,
	<agross@kernel.org>
Cc: devicetree@vger.kernel.org, alsa-devel@alsa-project.org,
	linux-arm-msm@vger.kernel.org, linux-usb@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Wesley Cheng <quic_wcheng@quicinc.com>,
	quic_jackp@quicinc.com, quic_plai@quicinc.com
Subject: [RFC PATCH v2 06/22] usb: host: xhci-mem: Cleanup pending secondary event ring events
Date: Wed, 25 Jan 2023 19:14:08 -0800	[thread overview]
Message-ID: <20230126031424.14582-7-quic_wcheng@quicinc.com> (raw)
In-Reply-To: <20230126031424.14582-1-quic_wcheng@quicinc.com>

As part of xHCI bus suspend, the XHCI is halted.  However, if there are
pending events in the secondary event ring, it is observed that the xHCI
controller stops responding to further commands upon host or device
initiated bus resume.  Iterate through all pending events and updating the
dequeue pointer to the last pending event trb.

Signed-off-by: Wesley Cheng <quic_wcheng@quicinc.com>
---
 drivers/usb/host/xhci-mem.c | 74 ++++++++++++++++++++++++++++++++++---
 1 file changed, 69 insertions(+), 5 deletions(-)

diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
index 45ac77a5d8e4..5c5266c18910 100644
--- a/drivers/usb/host/xhci-mem.c
+++ b/drivers/usb/host/xhci-mem.c
@@ -1819,17 +1819,85 @@ int xhci_alloc_erst(struct xhci_hcd *xhci,
 	return 0;
 }
 
+static void xhci_handle_sec_intr_events(struct xhci_hcd *xhci,
+	struct xhci_ring *ring,	struct xhci_intr_reg __iomem *ir_set,
+	struct xhci_erst *erst)
+{
+	union xhci_trb *erdp_trb, *current_trb;
+	struct xhci_segment	*seg;
+	u64 erdp_reg;
+	u32 iman_reg;
+	dma_addr_t deq;
+	unsigned long segment_offset;
+
+	/* disable irq, ack pending interrupt and ack all pending events */
+	iman_reg = readl_relaxed(&ir_set->irq_pending);
+	iman_reg &= ~IMAN_IE;
+	writel_relaxed(iman_reg, &ir_set->irq_pending);
+	iman_reg = readl_relaxed(&ir_set->irq_pending);
+	if (iman_reg & IMAN_IP)
+		writel_relaxed(iman_reg, &ir_set->irq_pending);
+
+	/* last acked event trb is in erdp reg  */
+	erdp_reg = xhci_read_64(xhci, &ir_set->erst_dequeue);
+	deq = (dma_addr_t)(erdp_reg & ~ERST_PTR_MASK);
+	if (!deq) {
+		xhci_dbg(xhci, "event ring handling not required\n");
+		return;
+	}
+
+	seg = ring->first_seg;
+	segment_offset = deq - seg->dma;
+
+	/* find out virtual address of the last acked event trb */
+	erdp_trb = current_trb = &seg->trbs[0] +
+				(segment_offset/sizeof(*current_trb));
+
+	/* read cycle state of the last acked trb to find out CCS */
+	ring->cycle_state = le32_to_cpu(current_trb->event_cmd.flags) & TRB_CYCLE;
+
+	while (1) {
+		/* last trb of the event ring: toggle cycle state */
+		if (current_trb == &seg->trbs[TRBS_PER_SEGMENT - 1]) {
+			ring->cycle_state ^= 1;
+			current_trb = &seg->trbs[0];
+		} else {
+			current_trb++;
+		}
+
+		/* cycle state transition */
+		if ((le32_to_cpu(current_trb->event_cmd.flags) & TRB_CYCLE) !=
+		    ring->cycle_state)
+			break;
+	}
+
+	if (erdp_trb != current_trb) {
+		deq = xhci_trb_virt_to_dma(ring->deq_seg, current_trb);
+		if (deq == 0)
+			xhci_warn(xhci,
+				"WARN invalid SW event ring dequeue ptr.\n");
+		/* Update HC event ring dequeue pointer */
+		erdp_reg &= ERST_PTR_MASK;
+		erdp_reg |= ((u64) deq & (u64) ~ERST_PTR_MASK);
+	}
+
+	/* Clear the event handler busy flag (RW1C); event ring is empty. */
+	erdp_reg |= ERST_EHB;
+	xhci_write_64(xhci, erdp_reg, &ir_set->erst_dequeue);
+}
+
 static void
 xhci_free_interrupter(struct xhci_hcd *xhci, struct xhci_interrupter *ir)
 {
 	struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
 	size_t erst_size;
-	u64 tmp64;
 	u32 tmp;
 
 	if (!ir)
 		return;
 
+	xhci_handle_sec_intr_events(xhci, ir->event_ring, ir->ir_set, &ir->erst);
+
 	erst_size = sizeof(struct xhci_erst_entry) * (ir->erst.num_entries);
 	if (ir->erst.entries)
 		dma_free_coherent(dev, erst_size,
@@ -1842,10 +1910,6 @@ xhci_free_interrupter(struct xhci_hcd *xhci, struct xhci_interrupter *ir)
 	tmp &= ERST_SIZE_MASK;
 	writel(tmp, &ir->ir_set->erst_size);
 
-	tmp64 = xhci_read_64(xhci, &ir->ir_set->erst_dequeue);
-	tmp64 &= (u64) ERST_PTR_MASK;
-	xhci_write_64(xhci, tmp64, &ir->ir_set->erst_dequeue);
-
 	/* free interrrupter event ring */
 	if (ir->event_ring)
 		xhci_ring_free(xhci, ir->event_ring);

  parent reply	other threads:[~2023-01-26  3:16 UTC|newest]

Thread overview: 135+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-26  3:14 [RFC PATCH v2 00/22] Introduce QC USB SND audio offloading support Wesley Cheng
2023-01-26  3:14 ` Wesley Cheng
2023-01-26  3:14 ` [RFC PATCH v2 01/22] xhci: fix event ring segment table related masks and variables in header Wesley Cheng
2023-01-26  3:14   ` Wesley Cheng
2023-01-26  3:14 ` [RFC PATCH v2 02/22] xhci: remove xhci_test_trb_in_td_math early development check Wesley Cheng
2023-01-26  3:14   ` Wesley Cheng
2023-01-26  7:48   ` Greg KH
2023-01-26  7:48     ` Greg KH
2023-01-26  3:14 ` [RFC PATCH v2 03/22] xhci: Refactor interrupter code for initial multi interrupter support Wesley Cheng
2023-01-26  3:14   ` Wesley Cheng
2023-01-26  3:14 ` [RFC PATCH v2 04/22] xhci: Add support to allocate several interrupters Wesley Cheng
2023-01-26  3:14   ` Wesley Cheng
2023-01-26  3:14 ` [RFC PATCH v2 05/22] usb: xhci: Add XHCI APIs to support USB offloading Wesley Cheng
2023-01-26  3:14   ` Wesley Cheng
2023-01-26  3:14 ` Wesley Cheng [this message]
2023-01-26  3:14   ` [RFC PATCH v2 06/22] usb: host: xhci-mem: Cleanup pending secondary event ring events Wesley Cheng
2023-01-26  3:14 ` [RFC PATCH v2 07/22] ASoC: Add SOC USB APIs for adding an USB backend Wesley Cheng
2023-01-26  3:14   ` Wesley Cheng
2023-01-26 15:32   ` Pierre-Louis Bossart
2023-01-30 22:36     ` Wesley Cheng
2023-01-30 22:36       ` Wesley Cheng
2023-01-28 13:26   ` Greg KH
2023-01-28 13:26     ` Greg KH
2023-01-29  6:54     ` Zhou Furong
2023-01-29  6:54       ` Zhou Furong
2023-01-29  7:09       ` Greg KH
2023-01-29  7:09         ` Greg KH
2023-01-30  8:34         ` Zhou Furong
2023-01-30  8:34           ` Zhou Furong
2023-01-30  9:27           ` Greg KH
2023-01-30  9:27             ` Greg KH
2023-02-10 22:46     ` Wesley Cheng
2023-02-10 22:46       ` Wesley Cheng
2023-01-26  3:14 ` [RFC PATCH v2 08/22] ASoC: dt-bindings: Add USB_RX port Wesley Cheng
2023-01-26  3:14   ` Wesley Cheng
2023-01-26 11:55   ` Krzysztof Kozlowski
2023-01-26 11:55     ` Krzysztof Kozlowski
2023-01-30 21:52     ` Wesley Cheng
2023-01-30 21:52       ` Wesley Cheng
2023-01-26  3:14 ` [RFC PATCH v2 09/22] ASoC: qcom: qdsp6: Introduce USB AFE port to q6dsp Wesley Cheng
2023-01-26  3:14   ` Wesley Cheng
2023-01-26 15:07   ` Srinivas Kandagatla
2023-01-26 15:07     ` Srinivas Kandagatla
2023-01-26 15:38   ` Pierre-Louis Bossart
2023-01-30 22:54     ` Wesley Cheng
2023-01-30 22:54       ` Wesley Cheng
2023-01-30 23:59       ` Pierre-Louis Bossart
2023-02-01  2:40         ` Wesley Cheng
2023-02-01  2:40           ` Wesley Cheng
2023-02-01  3:02           ` Pierre-Louis Bossart
2023-02-03  1:23             ` Wesley Cheng
2023-02-03  1:23               ` Wesley Cheng
2023-02-03  1:44               ` Wesley Cheng
2023-02-03  1:44                 ` Wesley Cheng
2023-01-26  3:14 ` [RFC PATCH v2 10/22] ASoC: qdsp6: q6afe: Increase APR timeout Wesley Cheng
2023-01-26  3:14   ` Wesley Cheng
2023-01-26 15:09   ` Srinivas Kandagatla
2023-01-26 15:09     ` Srinivas Kandagatla
2023-01-26  3:14 ` [RFC PATCH v2 11/22] ASoC: qcom: Add USB backend ASoC driver for Q6 Wesley Cheng
2023-01-26  3:14   ` Wesley Cheng
2023-01-26 15:44   ` Pierre-Louis Bossart
2023-01-30 22:59     ` Wesley Cheng
2023-01-30 22:59       ` Wesley Cheng
2023-01-30 23:50       ` Pierre-Louis Bossart
2023-01-26 16:35   ` Srinivas Kandagatla
2023-01-26 16:35     ` Srinivas Kandagatla
2023-01-26  3:14 ` [RFC PATCH v2 12/22] sound: usb: card: Introduce USB SND platform op callbacks Wesley Cheng
2023-01-26  3:14   ` Wesley Cheng
2023-01-26 15:50   ` Pierre-Louis Bossart
2023-01-30 23:00     ` Wesley Cheng
2023-01-30 23:00       ` Wesley Cheng
2023-01-28 13:28   ` Greg KH
2023-01-28 13:28     ` Greg KH
2023-02-10 22:49     ` Wesley Cheng
2023-02-10 22:49       ` Wesley Cheng
2023-02-28  2:59       ` Wesley Cheng
2023-02-28  2:59         ` Wesley Cheng
2023-02-28  7:30         ` Greg KH
2023-02-28  7:30           ` Greg KH
2023-02-28  9:19           ` Wesley Cheng
2023-02-28  9:19             ` Wesley Cheng
2023-02-28 15:29             ` Greg KH
2023-02-28 15:29               ` Greg KH
2023-02-20 17:11   ` Albert Wang
2023-02-20 17:11     ` Albert Wang
2023-01-26  3:14 ` [RFC PATCH v2 13/22] sound: usb: Export USB SND APIs for modules Wesley Cheng
2023-01-26  3:14   ` Wesley Cheng
2023-01-26  3:14 ` [RFC PATCH v2 14/22] dt-bindings: usb: dwc3: Add snps,num-hc-interrupters definition Wesley Cheng
2023-01-26  3:14   ` [RFC PATCH v2 14/22] dt-bindings: usb: dwc3: Add snps, num-hc-interrupters definition Wesley Cheng
2023-01-26 12:01   ` [RFC PATCH v2 14/22] dt-bindings: usb: dwc3: Add snps,num-hc-interrupters definition Krzysztof Kozlowski
2023-01-26 12:01     ` Krzysztof Kozlowski
2023-01-30 22:02     ` Wesley Cheng
2023-01-30 22:02       ` Wesley Cheng
2023-01-26  3:14 ` [RFC PATCH v2 15/22] usb: dwc3: Add DT parameter to specify maximum number of interrupters Wesley Cheng
2023-01-26  3:14   ` Wesley Cheng
2023-01-26  3:14 ` [RFC PATCH v2 16/22] sound: usb: Introduce QC USB SND offloading support Wesley Cheng
2023-01-26  3:14   ` Wesley Cheng
2023-01-26 16:07   ` Pierre-Louis Bossart
2023-01-28 13:32   ` Greg KH
2023-01-28 13:32     ` Greg KH
2023-02-11  0:03     ` Wesley Cheng
2023-02-11  0:03       ` Wesley Cheng
2023-01-26  3:14 ` [RFC PATCH v2 17/22] sound: usb: card: Check for support for requested audio format Wesley Cheng
2023-01-26  3:14   ` Wesley Cheng
2023-01-26  3:14 ` [RFC PATCH v2 18/22] sound: soc: soc-usb: Add PCM format check API for USB backend Wesley Cheng
2023-01-26  3:14   ` Wesley Cheng
2023-01-26  3:14 ` [RFC PATCH v2 19/22] sound: soc: qcom: qusb6: Ensure PCM format is supported by USB audio device Wesley Cheng
2023-01-26  3:14   ` Wesley Cheng
2023-01-26  3:14 ` [RFC PATCH v2 20/22] sound: usb: Prevent starting of audio stream if in use Wesley Cheng
2023-01-26  3:14   ` Wesley Cheng
2023-01-26 16:12   ` Pierre-Louis Bossart
2023-02-07  1:15     ` Wesley Cheng
2023-02-07  1:15       ` Wesley Cheng
2023-02-07 13:29       ` Pierre-Louis Bossart
2023-02-07 13:29         ` Pierre-Louis Bossart
2023-02-11  9:52         ` Wesley Cheng
2023-02-11  9:52           ` Wesley Cheng
2023-02-13 15:22           ` Pierre-Louis Bossart
2023-02-13 20:12             ` Wesley Cheng
2023-02-13 20:12               ` Wesley Cheng
2023-01-26  3:14 ` [RFC PATCH v2 21/22] ASoC: dt-bindings: Add Q6USB backend bindings Wesley Cheng
2023-01-26  3:14   ` Wesley Cheng
2023-01-26 12:03   ` Krzysztof Kozlowski
2023-01-26 12:03     ` Krzysztof Kozlowski
2023-01-26 16:01   ` Srinivas Kandagatla
2023-01-26 16:01     ` Srinivas Kandagatla
2023-01-26  3:14 ` [RFC PATCH v2 22/22] ASoC: dt-bindings: Update example for enabling USB offload on SM8250 Wesley Cheng
2023-01-26  3:14   ` Wesley Cheng
2023-01-26 12:05   ` Krzysztof Kozlowski
2023-01-26 12:05     ` Krzysztof Kozlowski
2023-01-26  9:23 ` [RFC PATCH v2 00/22] Introduce QC USB SND audio offloading support Mathias Nyman
2023-01-26  9:23   ` Mathias Nyman
2023-01-30 21:51   ` Wesley Cheng
2023-01-30 21:51     ` Wesley Cheng
2023-01-26 16:22 ` Pierre-Louis Bossart

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=20230126031424.14582-7-quic_wcheng@quicinc.com \
    --to=quic_wcheng@quicinc.com \
    --cc=Thinh.Nguyen@synopsys.com \
    --cc=agross@kernel.org \
    --cc=alsa-devel@alsa-project.org \
    --cc=andersson@kernel.org \
    --cc=bgoswami@quicinc.com \
    --cc=broonie@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=mathias.nyman@intel.com \
    --cc=perex@perex.cz \
    --cc=quic_jackp@quicinc.com \
    --cc=quic_plai@quicinc.com \
    --cc=robh+dt@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.