mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Julius Bairaktaris <julius@bairaktaris.de>
To: Jeff Johnson <jjohnson@kernel.org>
Cc: ath11k@lists.infradead.org, linux-wireless@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Baochen Qiang <baochen.qiang@oss.qualcomm.com>,
	Vasanthakumar Thiagarajan
	<vasanthakumar.thiagarajan@oss.qualcomm.com>,
	Rameshkumar Sundaram <rameshkumar.sundaram@oss.qualcomm.com>,
	Praneesh P <praneesh.p@oss.qualcomm.com>,
	Lorenzo Bianconi <lorenzo.bianconi@oss.qualcomm.com>,
	Mark Ruvald Pedersen <wabsie@gmail.com>,
	Rosen Penev <rosenp@gmail.com>
Subject: [PATCH ath-next v1 05/10] wifi: ath11k: sync a cached ring's new entries once per head read
Date: Mon, 23 Nov 2026 14:41:32 +0100	[thread overview]
Message-ID: <20261123134137.718575-6-julius@bairaktaris.de> (raw)
In-Reply-To: <20261123134137.718575-1-julius@bairaktaris.de>

The REO destination and TX completion rings are cacheable on the chips
with alloc_cacheable_memory (IPQ8074, IPQ6018, QCN9074, IPQ5018). The CPU
takes each entry from the device with a dma_sync_single_for_cpu() of its
32 bytes when the entry before it is handed out, and prefetches it then.
That is a call into the DMA layer and a cache invalidation with its
barrier for every frame received and every frame completed, and a
prefetch issued a few dozen instructions before the load: in a profile
of bridged downlink at 900 Mbit/s (IPQ8074, one run) the instruction
after the first load of a completion entry in
ath11k_dp_tx_completion_handler() takes 0.70 % of all samples.

Invalidate the entries where they become the CPU's, when a read of the
head pointer moves cached_hp: the whole range from the old head to the
new one, in one call or two where it wraps, in access_begin and in
ath11k_hal_srng_dst_num_free(), the two places that move it. An entry is
read only below cached_hp, so each is invalidated once after the device
wrote it, and a line the CPU loaded earlier, by a prefetch or
speculatively, is discarded before its entries are read: the guarantee
the per-entry sync gave. The range is clamped to the ring. Prefetch the
entries two cache lines ahead, which may run past the head: such a line
is discarded again when the head exposes it.

ath11k_hal_srng_dst_num_free() now reads the head with READ_ONCE() and
orders the entry reads after it with dma_rmb(), as access_begin does.
Rings in coherent memory are not touched.

Tested-on: IPQ8074 hw2.0 AHB WLAN.HK.2.9.0.1-02146-QCAHKSWPL_SILICONZ-1
Signed-off-by: Julius Bairaktaris <julius@bairaktaris.de>
---
 drivers/net/wireless/ath/ath11k/hal.c | 86 ++++++++++++++++++---------
 1 file changed, 57 insertions(+), 29 deletions(-)

diff --git a/drivers/net/wireless/ath/ath11k/hal.c b/drivers/net/wireless/ath/ath11k/hal.c
index 98bd9e3f0aae..e5c35647a136 100644
--- a/drivers/net/wireless/ath/ath11k/hal.c
+++ b/drivers/net/wireless/ath/ath11k/hal.c
@@ -627,34 +627,54 @@ u32 *ath11k_hal_srng_dst_peek(struct ath11k_base *ab, struct hal_srng *srng)
 	return NULL;
 }
 
-static u32 *ath11k_hal_srng_dst_peek_with_dma(struct ath11k_base *ab,
-					      struct hal_srng *srng, dma_addr_t *paddr)
+/*
+ * Hands the CPU the entries of a cached destination ring from word @from up
+ * to word @to, which the head pointer the caller just read shows the device
+ * has written. Every entry the ring hands out passes through here once, after
+ * the head pointer that exposed it was read, so a line of the ring that the
+ * CPU loaded before, by a prefetch or speculatively, is discarded before it is
+ * read. The range is clamped to the ring, whatever the head pointer holds.
+ */
+static void ath11k_hal_srng_dst_sync(struct ath11k_base *ab,
+				     struct hal_srng *srng, u32 from, u32 to)
 {
-	lockdep_assert_held(&srng->lock);
+	u32 size = srng->ring_size;
 
-	if (srng->u.dst_ring.tp != srng->u.dst_ring.cached_hp) {
-		*paddr = srng->ring_base_paddr +
-			  sizeof(*srng->ring_base_vaddr) * srng->u.dst_ring.tp;
-		return srng->ring_base_vaddr + srng->u.dst_ring.tp;
-	}
+	if (!(srng->flags & HAL_SRNG_FLAGS_CACHED) || from >= size)
+		return;
 
-	return NULL;
+	to = min(to, size);
+	if (to < from) {
+		dma_sync_single_for_cpu(ab->dev, srng->ring_base_paddr +
+					from * sizeof(u32),
+					(size - from) * sizeof(u32),
+					DMA_FROM_DEVICE);
+		from = 0;
+	}
+	if (to > from)
+		dma_sync_single_for_cpu(ab->dev, srng->ring_base_paddr +
+					from * sizeof(u32),
+					(to - from) * sizeof(u32),
+					DMA_FROM_DEVICE);
 }
 
-static void ath11k_hal_srng_prefetch_desc(struct ath11k_base *ab,
-					  struct hal_srng *srng)
+/*
+ * Starts the load of the entries @lines cache lines past word @tp of a cached
+ * ring. A line past the head pointer is discarded again by
+ * ath11k_hal_srng_dst_sync() before its entries are read.
+ */
+static void ath11k_hal_srng_dst_prefetch(struct hal_srng *srng, u32 tp,
+					 u32 lines)
 {
-	dma_addr_t desc_paddr;
-	u32 *desc;
+	u32 ahead = tp + lines * L1_CACHE_BYTES / sizeof(u32);
 
-	/* prefetch only if desc is available */
-	desc = ath11k_hal_srng_dst_peek_with_dma(ab, srng, &desc_paddr);
-	if (likely(desc)) {
-		dma_sync_single_for_cpu(ab->dev, desc_paddr,
-					(srng->entry_size * sizeof(u32)),
-					DMA_FROM_DEVICE);
-		prefetch(desc);
-	}
+	if (!(srng->flags & HAL_SRNG_FLAGS_CACHED))
+		return;
+
+	if (ahead >= srng->ring_size)
+		ahead -= srng->ring_size;
+	if (ahead < srng->ring_size)
+		prefetch(srng->ring_base_vaddr + ahead);
 }
 
 u32 *ath11k_hal_srng_dst_get_next_entry(struct ath11k_base *ab,
@@ -675,9 +695,7 @@ u32 *ath11k_hal_srng_dst_get_next_entry(struct ath11k_base *ab,
 	if (srng->u.dst_ring.tp == srng->ring_size)
 		srng->u.dst_ring.tp = 0;
 
-	/* Try to prefetch the next descriptor in the ring */
-	if (srng->flags & HAL_SRNG_FLAGS_CACHED)
-		ath11k_hal_srng_prefetch_desc(ab, srng);
+	ath11k_hal_srng_dst_prefetch(srng, srng->u.dst_ring.tp, 2);
 
 	return desc;
 }
@@ -692,8 +710,15 @@ int ath11k_hal_srng_dst_num_free(struct ath11k_base *ab, struct hal_srng *srng,
 	tp = srng->u.dst_ring.tp;
 
 	if (sync_hw_ptr) {
-		hp = *srng->u.dst_ring.hp_addr;
-		srng->u.dst_ring.cached_hp = hp;
+		hp = READ_ONCE(*srng->u.dst_ring.hp_addr);
+		if (hp != srng->u.dst_ring.cached_hp) {
+			u32 old = srng->u.dst_ring.cached_hp;
+
+			srng->u.dst_ring.cached_hp = hp;
+			/* the entries are read after the head pointer */
+			dma_rmb();
+			ath11k_hal_srng_dst_sync(ab, srng, old, hp);
+		}
 	} else {
 		hp = srng->u.dst_ring.cached_hp;
 	}
@@ -835,16 +860,19 @@ void ath11k_hal_srng_access_begin(struct ath11k_base *ab, struct hal_srng *srng)
 		hp = READ_ONCE(*srng->u.dst_ring.hp_addr);
 
 		if (hp != srng->u.dst_ring.cached_hp) {
+			u32 old = srng->u.dst_ring.cached_hp;
+
 			srng->u.dst_ring.cached_hp = hp;
 			/* Make sure descriptor is read after the head
 			 * pointer.
 			 */
 			dma_rmb();
+			ath11k_hal_srng_dst_sync(ab, srng, old, hp);
 		}
 
-		/* Try to prefetch the next descriptor in the ring */
-		if (srng->flags & HAL_SRNG_FLAGS_CACHED)
-			ath11k_hal_srng_prefetch_desc(ab, srng);
+		ath11k_hal_srng_dst_prefetch(srng, srng->u.dst_ring.tp, 0);
+		ath11k_hal_srng_dst_prefetch(srng, srng->u.dst_ring.tp, 1);
+		ath11k_hal_srng_dst_prefetch(srng, srng->u.dst_ring.tp, 2);
 	}
 }
 
-- 
2.53.0


  parent reply	other threads:[~2026-11-23 13:41 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 13:41 [PATCH ath-next v1 00/10] wifi: ath11k: cut the per-packet cost of the RX path Julius Bairaktaris
2026-11-23 13:41 ` [PATCH ath-next v1 01/10] wifi: ath11k: use page_pool for the RX refill ring Julius Bairaktaris
2026-11-23 13:41 ` [PATCH ath-next v1 02/10] wifi: ath11k: replace the RX buffer IDR with a table Julius Bairaktaris
2026-11-23 13:41 ` [PATCH ath-next v1 03/10] wifi: ath11k: request only the PPDU stats TLVs it reads Julius Bairaktaris
2026-11-23 13:41 ` [PATCH ath-next v1 04/10] wifi: ath11k: set the skb hash of a received frame from the hardware Julius Bairaktaris
2026-11-23 13:41 ` Julius Bairaktaris [this message]
2026-11-23 13:41 ` [PATCH ath-next v1 06/10] wifi: ath11k: prefetch the buffers of the REO entries ahead of their reap Julius Bairaktaris
2026-11-23 13:41 ` [PATCH ath-next v1 07/10] wifi: ath11k: look the peer of a received MSDU up once Julius Bairaktaris
2026-11-23 13:41 ` [PATCH ath-next v1 08/10] wifi: ath11k: take the refill buffer lock without a second BH disable Julius Bairaktaris
2026-11-23 13:41 ` [PATCH ath-next v1 09/10] wifi: ath11k: hand the CPU only the part of a REO buffer the device wrote Julius Bairaktaris
2026-11-23 13:41 ` [PATCH ath-next v1 10/10] wifi: ath11k: give the refill buffers their ids a chunk at a time Julius Bairaktaris

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=20261123134137.718575-6-julius@bairaktaris.de \
    --to=julius@bairaktaris.de \
    --cc=ath11k@lists.infradead.org \
    --cc=baochen.qiang@oss.qualcomm.com \
    --cc=jjohnson@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-wireless@vger.kernel.org \
    --cc=lorenzo.bianconi@oss.qualcomm.com \
    --cc=praneesh.p@oss.qualcomm.com \
    --cc=rameshkumar.sundaram@oss.qualcomm.com \
    --cc=rosenp@gmail.com \
    --cc=vasanthakumar.thiagarajan@oss.qualcomm.com \
    --cc=wabsie@gmail.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

all inboxes | Powered by JetHome®