Skip to contentSkip to conversation

riscv: pci: allow write-combining mmap of PCI BARs

PATCH Applied Troy Mitchell 3 participants 3 messages Nov 10 – Nov 11

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: pci: allow write-combining mmap of PCI BARs

Define arch_can_pci_mmap_wc() for RISC-V, so that the PCI core can create resourceN_wc in sysfs for prefetchable BARs. Without it, the only sysfs mapping of such a BAR is resourceN, which uses pgprot_device():
with Svpbmt or XTheadMae that is the IO memory type, where every access is strongly ordered and nothing is merged.

resourceN_wc is mapped with pgprot_writecombine(), which selects the NC memory type when the kernel uses Svpbmt or XTheadMae. Without either, _PAGE_NOCACHE is 0 and pgprot_writecombine() changes nothing, so resourceN_wc would get the same PMA-defined attributes as resourceN and no write-combining at all. Make arch_can_pci_mmap_wc() true only when _PAGE_NOCACHE is non-zero. On 64-bit this is a runtime check, as _PAGE_NOCACHE is chosen at boot from the CPU extensions; on 32-bit it is always 0, so nothing changes there.

---
 arch/riscv/include/asm/pci.h | 9 +++++++++
 1 file changed, 9 insertions(+)
arch/riscv/include/asm/pci.h +9 -0
index b2f91abebd61..2b2db5533576 100644
@@ -9,6 +9,7 @@
 #include <linux/types.h>
 #include <linux/slab.h>
 #include <linux/dma-mapping.h>
+#include <linux/pgtable.h>
 
 #include <asm/io.h>
 
@@ -32,6 +33,14 @@ static inline int pcibus_to_node(struct pci_bus *bus)
 #endif
 #endif /* defined(CONFIG_PCI) && defined(CONFIG_NUMA) */
 
+/*
+ * resourceN_wc is mapped with pgprot_writecombine(), which gives the NC
+ * memory type only with Svpbmt or XTheadMae. With neither it changes
+ * nothing and resourceN_wc would be no different from resourceN, so only
+ * offer WC mmap when there is an NC type to use.
+ */
+#define arch_can_pci_mmap_wc()	(_PAGE_NOCACHE != 0)
+
 /* Generic PCI */
 #include <asm-generic/pci.h>
 

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261109-riscv-pci-mmap-wc-f56ec5e12557

Best regards,

Signature · 1 line
Troy Mitchell <[email protected]>
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 15 lines

Define arch_can_pci_mmap_wc() for RISC-V, so that the PCI core can create resourceN_wc in sysfs for prefetchable BARs. Without it, the only sysfs mapping of such a BAR is resourceN, which uses pgprot_device():
with Svpbmt or XTheadMae that is the IO memory type, where every access is strongly ordered and nothing is merged.

resourceN_wc is mapped with pgprot_writecombine(), which selects the NC memory type when the kernel uses Svpbmt or XTheadMae. Without either, _PAGE_NOCACHE is 0 and pgprot_writecombine() changes nothing, so resourceN_wc would get the same PMA-defined attributes as resourceN and no write-combining at all. Make arch_can_pci_mmap_wc() true only when _PAGE_NOCACHE is non-zero. On 64-bit this is a runtime check, as _PAGE_NOCACHE is chosen at boot from the CPU extensions; on 32-bit it is always 0, so nothing changes there.

Quoted 1 line

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Troy Mitchell
This patch was applied to riscv/linux.git (fixes) by Paul Walmsley <[email protected]>:

Hello:

This patch was applied to riscv/linux.git (fixes)
by Paul Walmsley <[email protected]>:

Quoted 15 lines · On Tue, 10 Nov 2026 15:04:49 +0000 you wrote:

Define arch_can_pci_mmap_wc() for RISC-V, so that the PCI core can create resourceN_wc in sysfs for prefetchable BARs. Without it, the only sysfs mapping of such a BAR is resourceN, which uses pgprot_device():
with Svpbmt or XTheadMae that is the IO memory type, where every access is strongly ordered and nothing is merged.

resourceN_wc is mapped with pgprot_writecombine(), which selects the NC memory type when the kernel uses Svpbmt or XTheadMae. Without either, _PAGE_NOCACHE is 0 and pgprot_writecombine() changes nothing, so resourceN_wc would get the same PMA-defined attributes as resourceN and no write-combining at all. Make arch_can_pci_mmap_wc() true only when _PAGE_NOCACHE is non-zero. On 64-bit this is a runtime check, as _PAGE_NOCACHE is chosen at boot from the CPU extensions; on 32-bit it is always 0, so nothing changes there.

Quoted 1 line

[...]

Here is the summary with links:
- riscv: pci: allow write-combining mmap of PCI BARs

    https://git.kernel.org/riscv/c/79fc9d9467a1

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html