From: Zixian Zeng <sycamoremoon376@gmail.com>
To: Yao Zi <me@ziyao.cc>,
u-boot@lists.u-boot-project.org, u-boot-spacemit@groups.io
Cc: Tom Rini <trini@konsulko.com>,
Jerome Forissier <jerome.forissier@arm.com>,
Huan Zhou <pericycle.cc@gmail.com>,
Guodong Xu <guodong.xu@riscstar.com>,
Quentin Schulz <quentin.schulz@cherry.de>,
Julien Stephan <jstephan@baylibre.com>,
Johan Jonker <jbx6244@gmail.com>,
David Lechner <dlechner@baylibre.com>,
Raymond Mao <raymond.mao@riscstar.com>,
Eric Chung <eric.chung@riscstar.com>, Yixun Lan <dlan@kernel.org>,
James Hilliard <james.hilliard1@gmail.com>,
Guodong Xu <guodong@riscstar.com>, E Shattow <e@freeshell.de>,
Dave Klotz <DaveWK225@protonmail.com>
Subject: Re: [PATCH v3 1/2] net: spacemit: k1: add Ethernet MAC driver
Date: Sun, 22 Nov 2026 20:37:30 +0800 [thread overview]
Message-ID: <3e0188b3-6191-4ebc-924e-e76807a11241@gmail.com> (raw)
In-Reply-To: <awKjZ70AfaHI0-ky@pie>
Hi Yao Zi,
On 2026/11/22 16:10, Yao Zi wrote:
> On Sun, Nov 22, 2026 at 02:23:56PM +0800, Zixian Zeng wrote:
>> Hi, Yao Zi,
>>
>> Thank you for the detailed review and the follow-up clarifications. I
>> went through both replies against the RISC-V CMO and RVWMO specifications,
>> the U-Boot cache/MMIO implementations, and the Linux K1 driver used as the
>> reference for this port.
>>
>> On 2026/11/21 00:56, Yao Zi wrote:
>>> On Fri, Nov 20, 2026 at 02:35:56PM +0800, Zixian Zeng wrote:
>>>> Add Ethernet MAC driver support for the SpacemiT K1 SoC. The driver
>>>> supports RGMII and RMII interfaces, synchronous transmission, and exposes
>>>> the integrated MDIO controller through the MDIO uclass.
>>>>
>>>> The register definitions, descriptor layout, MDIO protocol, RGMII delay
>>>> line handling and hardware initialisation sequences are derived from the
>>>> mainline Linux commit bfec6d7f2001 (net: spacemit: Add K1 Ethernet MAC).
>>>>
>>>> Link: https://lore.kernel.org/all/20250914-net-k1-emac-v12-0-65b31b398f44@iscas.ac.cn/
>>>> Tested-by: Dave Klotz <DaveWK225@protonmail.com>
>>>> Tested-by: Yixun Lan <dlan@kernel.org> # MusePi Pro
>>>> Signed-off-by: Zixian Zeng <sycamoremoon376@gmail.com>
>>>> ---
>>>> drivers/net/Kconfig | 1 +
>>>> drivers/net/Makefile | 1 +
>>>> drivers/net/spacemit/Kconfig | 14 +
>>>> drivers/net/spacemit/Makefile | 3 +
>>>> drivers/net/spacemit/k1_emac.c | 957 +++++++++++++++++++++++++++++++++++++++++
>>>> 5 files changed, 976 insertions(+)
>
> [...]
>
>>>> +static void k1_emac_prepare_rx(struct k1_emac_priv *priv, unsigned int index)
>>>> +{
>>>> + struct k1_emac_desc *desc = k1_desc_at(priv->rx_desc, index);
>>>> + u8 *buffer = k1_rx_buf_at(priv, index);
>>>> +
>>>> + k1_buffer_flush(buffer);
>>>
>>> Does flushing the RX buffer really make a difference on the hardware's
>>> behavior?
>>
>> The purpose is to prevent dirty CPU cache lines from a previous use from
>> being written back over a later DMA receive.
>>
>> The buffer is passed directly to the U-Boot network stack before
>> free_pkt(). In particular, ping_receive() modifies the received Ethernet,
>> IP and ICMP headers in place when answering an ICMP echo request, before
>> copying the reply to a TX buffer. Consequently, an RX buffer can contain
>> dirty cache lines when it is recycled.
>>
>> If cache maintenance is omitted before returning the buffer,
>> the following sequence of events may occur:
>>
>> 1. The CPU modifies an old packet A, leaving the updated content A′ in a "dirty" cache line.
>> 2. The driver hands the buffer over to the DMA, which then writes a new packet B into memory.
>> 3. The CPU’s dirty cache line is subsequently evicted, causing A′ to be written back to the same memory location.
>> 4. Part of the new packet B is overwritten.
>
> Oh, this makes sense. Please replace flush with a clean though.
OK, do you mean using a clean-only cache operation here, or just renaming
the helper from k1_buffer_flush() to k1_buffer_flush() to reflect its purpose?
The current RISC-V implementation does not expose a public range clean API.
>> The Linux RISC-V DMA implementation at the reference commit also performs
>> a clean for DMA_FROM_DEVICE before device ownership, followed by an
>> invalidate when ownership returns to the CPU. The Linux K1 driver's
>> dma_map_single(..., DMA_FROM_DEVICE) provides that preparation implicitly.
>>
>> I therefore plan to retain the pre-DMA buffer maintenance and add a short
>> comment explaining the dirty-line case. The current flush supplies the
>> required clean as well as invalidation.
>>
>>>> + desc->control = FIELD_PREP(K1_RX_DESC_BUFFER_SIZE, K1_DMA_BUF_SIZE);
>>>> + if (index == K1_RX_DESC_COUNT - 1)
>>>> + desc->control |= K1_RX_DESC_END_RING;
>>>> + desc->buffer_addr1 = priv->rx_buf_dma[index];
>>>> + desc->buffer_addr2 = 0;
>>>> + desc->status = K1_RX_DESC_OWN;
>>>> +}
>>>> +
>>>> +static void k1_emac_advance_rx(struct k1_emac_priv *priv)
>>>> +{
>>>> + unsigned int index = priv->rx_index;
>>>> + unsigned int first;
>>>> +
>>>> + /*
>>>> + * Multiple 16-byte descriptors share a cache line. Do not clean that
>>>> + * line while DMA may still be updating another descriptor in it. Return
>>>> + * descriptors to DMA only after the complete cache-line-sized group has
>>>> + * been consumed.
>>>> + */
>>>> + if (!((index + 1) % K1_DESCS_PER_CACHELINE)) {
>>>> + first = index + 1 - K1_DESCS_PER_CACHELINE;
>>>> + for (; first <= index; first++)
>>>> + k1_emac_prepare_rx(priv, first);
>>>> +
>>>> + /* Complete descriptor stores before cleaning the cache line. */
>>>> + mb();
>>>> + k1_desc_flush(k1_desc_at(priv->rx_desc, index));
>>>> + writel(1, priv->base + K1_DMA_RX_POLL_DEMAND);
>>>> + }
>>>> +
>>>> + priv->rx_index = (index + 1) % K1_RX_DESC_COUNT;
>>>> +}
>
> [...]
>
>>>> +static int k1_emac_send(struct udevice *dev, void *packet, int length)
>>>> +{
>>>> + struct k1_emac_priv *priv = dev_get_priv(dev);
>>>> + unsigned int index = priv->tx_index;
>>>> + struct k1_emac_desc *desc = k1_desc_at(priv->tx_desc, index);
>>>> + u8 *buffer = k1_tx_buf_at(priv, index);
>>>> + ulong start;
>>>> +
>>>> + if (length <= 0 || length > K1_DMA_BUF_SIZE)
>>>> + return -EMSGSIZE;
>>>> +
>>>> + k1_desc_invalidate(desc);
>>>> + if (desc->status & K1_TX_DESC_OWN)
>>>> + return -EBUSY;
>>>> +
>>>> + memcpy(buffer, packet, length);
>>>> + flush_dcache_range((ulong)buffer,
>>>> + ALIGN((ulong)buffer + length, ARCH_DMA_MINALIGN));
>>>> +
>>>> + /* Clear stale state before handing the descriptor to DMA. */
>>>> + writel(K1_DMA_STATUS_TX_DESC_UNAVAILABLE, priv->base + K1_DMA_STATUS);
>>>> + /* Complete the W1C write before asking the DMA engine to resume. */
>>>> + readl(priv->base + K1_DMA_STATUS);
>>>> +
>>>> + desc->buffer_addr1 = priv->tx_buf_dma[index];
>>>> + desc->buffer_addr2 = 0;
>>>> + desc->control = FIELD_PREP(K1_TX_DESC_BUFFER_SIZE, length) |
>>>> + K1_TX_DESC_FIRST | K1_TX_DESC_LAST;
>>>> + if (index == K1_TX_DESC_COUNT - 1)
>>>> + desc->control |= K1_TX_DESC_END_RING;
>>>> + desc->status = K1_TX_DESC_OWN;
>>>> + /* Complete descriptor stores before cleaning its cache line. */
>>>> + mb();
>>>
>>> Probably redundant barrier since RISC-V guarantees order between
>>> overlapping stores...
>>
>> Thank you for supplying the precise PPO rule in your follow-up. I agree
>> that the descriptor stores and their overlapping flush do not themselves
>> require an intervening barrier.
>>
>> But after I checked linux k1-emac source code and documents,
>> I noticed another similar thing which has different purpose:
>> Links:
>> https://docs.kernel.org/core-api/dma-api-howto.html#types-of-dma-mappings
>> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/spacemit/k1_emac.c?h=v7.4-rc3#n917
>
> Did you quote the wrong line? This link points to emac_change_mtu().
> I assume you're referring to the dma_wmb() at line 790.
Yes, exactly. I quoted the wrong line.
> The dma_wmb() in the Linux driver serves for different purpose. On Linux
> side, a new packet could be queued at any time, even the DMA engine is
> still working and probably accessing the ring, so it's importatnt to
> ensure TX_DESC_OWN could only be observed by the engine after other
> fields in the descriptor has been set up.
>
> But for U-Boot, the situation seems different. Packet are transmitted
> strictly one by one, and since you disable the AUTOPOLL feature and
> poll for the DMA engine to stop (K1_DMA_STATUS_TX_DESC_UNAVAILABLE) when
> transmitting, the engine is always stopped, never polling the descriptor
> ring during setup of the descriptor, until a write to
> K1_DMA_TX_POLL_DEMAND wakes it up.
>
> Since K1_DMA_TX_POLL_DEMAND is written with writel(), which ensures
> the descriptor setup (including K1_TX_DESC_OWN bit) becomes visible to
> the EMAC before waking the engine up, this shouldn't cause any problems.
>
>> Be a bit more conservative, I prepare move it before the OWN store
>> to order the other descriptor fields before publishing ownership to DMA.
>> just as what drivers/net/dwc_eth_qos.c +1121 did
>
> The case in K1's EMAC and dwc_eth_qos.c are different. It seems
> dwc_eth_qos' DMA engine keeps polling the ring when no descriptor is
> available, while in which case K1's stops.
Understood, my previous understanding was incomplete.
Thanks for the explanation, I will remove this mb() only.
>>>> + k1_desc_flush(desc);
>>>> + writel(1, priv->base + K1_DMA_TX_POLL_DEMAND);
>>>> +
>>>> + start = get_timer(0);
>>>> + do {
>>>
>>> The could would be simpler if you use a loop simply bounded by retry
>>> counts.
>>
>> A retry-bounded loop would work if the intended limit is a number of
>> attempts. However, I prefer to retain an elapsed-time timeout here, thanks.
>
> Why? The code would look simpler with the change, like
>
> for (; retry < K1_TX_TIMEOUT_MS / 10; retry++) {
> /* ... */
> udelay(10);
> }
Just a preference :-) I will switch to a retry-bounded loop in next revision.
With udelay(10), I will calculate the retry limit as K1_TX_TIMEOUT_MS * 1000 / 10,
which converting ms to us.
>>>> + k1_desc_invalidate(desc);
>>>> + /*
>>>> + * OWN is cleared before transmission finishes. Descriptor
>>>> + * unavailable is raised after DMA completes the packet and moves
>>>> + * to the next CPU-owned descriptor.
>>>> + */
>>>> + if (!(desc->status & K1_TX_DESC_OWN) &&
>>>> + (readl(priv->base + K1_DMA_STATUS) &
>>>> + K1_DMA_STATUS_TX_DESC_UNAVAILABLE)) {
>>>> + priv->tx_index = (index + 1) % K1_TX_DESC_COUNT;
>>>> + return 0;
>>>> + }
>>>> + udelay(10);
>>>> + } while (get_timer(start) <= K1_TX_TIMEOUT_MS);
>
> [...]
>
Best regards,
Zixian
next prev parent reply other threads:[~2026-11-22 13:21 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 6:35 [PATCH v3 0/2] net: add SpacemiT K1 EMAC support Zixian Zeng
2026-11-20 6:35 ` [PATCH v3 1/2] net: spacemit: k1: add Ethernet MAC driver Zixian Zeng
2026-11-20 16:56 ` Yao Zi
2026-11-21 18:31 ` Yao Zi
2026-11-22 6:23 ` Zixian Zeng
2026-11-22 8:10 ` Yao Zi
2026-11-22 12:37 ` Zixian Zeng [this message]
2026-11-22 17:10 ` Yao Zi
2026-11-20 6:35 ` [PATCH v3 2/2] configs: spacemit: k1: enable Ethernet support Zixian Zeng
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=3e0188b3-6191-4ebc-924e-e76807a11241@gmail.com \
--to=sycamoremoon376@gmail.com \
--cc=DaveWK225@protonmail.com \
--cc=dlan@kernel.org \
--cc=dlechner@baylibre.com \
--cc=e@freeshell.de \
--cc=eric.chung@riscstar.com \
--cc=guodong.xu@riscstar.com \
--cc=guodong@riscstar.com \
--cc=james.hilliard1@gmail.com \
--cc=jbx6244@gmail.com \
--cc=jerome.forissier@arm.com \
--cc=jstephan@baylibre.com \
--cc=me@ziyao.cc \
--cc=pericycle.cc@gmail.com \
--cc=quentin.schulz@cherry.de \
--cc=raymond.mao@riscstar.com \
--cc=trini@konsulko.com \
--cc=u-boot-spacemit@groups.io \
--cc=u-boot@lists.u-boot-project.org \
/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