Skip to content

Commit 6005752

Browse files
xhackerustcopsiff
authored andcommitted
riscv: errata: thead: use riscv_nonstd_cache_ops for CMO
Previously, we use alternative mechanism to dynamically patch the CMO operations for THEAD C906/C910 during boot for performance reason. But as pointed out by Arnd, "there is already a significant cost in accessing the invalidated cache lines afterwards, which is likely going to be much higher than the cost of an indirect branch". And indeed, there's no performance difference with GMAC and EMMC per my test on Sipeed Lichee Pi 4A board. Use riscv_nonstd_cache_ops for THEAD C906/C910 CMO to simplify the alternative code, and to acchieve Arnd's goal -- "I think moving the THEAD ops at the same level as all nonstandard operations makes sense, but I'd still leave CMO as an explicit fast path that avoids the indirect branch. This seems like the right thing to do both for readability and for platforms on which the indirect branch has a noticeable overhead." Signed-off-by: Jisheng Zhang <[email protected]> Tested-by: Emil Renner Berthing <[email protected]> Reviewed-by: Conor Dooley <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]> (cherry picked from commit a4ff64e) Signed-off-by: Han Gao <[email protected]>
1 parent 36057dc commit 6005752

File tree

3 files changed

+80
-46
lines changed

3 files changed

+80
-46
lines changed

arch/riscv/Kconfig.errata

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ config ERRATA_THEAD_CMO
7979
depends on ERRATA_THEAD && MMU
8080
select DMA_DIRECT_REMAP
8181
select RISCV_DMA_NONCOHERENT
82+
select RISCV_NONSTANDARD_CACHE_OPS
8283
default y
8384
help
8485
This will apply the cache management errata to handle the

arch/riscv/errata/thead/errata.c

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
#include <asm/alternative.h>
1313
#include <asm/cacheflush.h>
1414
#include <asm/cpufeature.h>
15+
#include <asm/dma-noncoherent.h>
1516
#include <asm/errata_list.h>
1617
#include <asm/hwprobe.h>
18+
#include <asm/io.h>
1719
#include <asm/patch.h>
1820
#include <asm/vendorid_list.h>
1921

@@ -33,6 +35,75 @@ static bool errata_probe_pbmt(unsigned int stage,
3335
return false;
3436
}
3537

38+
/*
39+
* th.dcache.ipa rs1 (invalidate, physical address)
40+
* | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
41+
* 0000001 01010 rs1 000 00000 0001011
42+
* th.dcache.iva rs1 (invalidate, virtual address)
43+
* 0000001 00110 rs1 000 00000 0001011
44+
*
45+
* th.dcache.cpa rs1 (clean, physical address)
46+
* | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
47+
* 0000001 01001 rs1 000 00000 0001011
48+
* th.dcache.cva rs1 (clean, virtual address)
49+
* 0000001 00101 rs1 000 00000 0001011
50+
*
51+
* th.dcache.cipa rs1 (clean then invalidate, physical address)
52+
* | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
53+
* 0000001 01011 rs1 000 00000 0001011
54+
* th.dcache.civa rs1 (clean then invalidate, virtual address)
55+
* 0000001 00111 rs1 000 00000 0001011
56+
*
57+
* th.sync.s (make sure all cache operations finished)
58+
* | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
59+
* 0000000 11001 00000 000 00000 0001011
60+
*/
61+
#define THEAD_INVAL_A0 ".long 0x0265000b"
62+
#define THEAD_CLEAN_A0 ".long 0x0255000b"
63+
#define THEAD_FLUSH_A0 ".long 0x0275000b"
64+
#define THEAD_SYNC_S ".long 0x0190000b"
65+
66+
#define THEAD_CMO_OP(_op, _start, _size, _cachesize) \
67+
asm volatile("mv a0, %1\n\t" \
68+
"j 2f\n\t" \
69+
"3:\n\t" \
70+
THEAD_##_op##_A0 "\n\t" \
71+
"add a0, a0, %0\n\t" \
72+
"2:\n\t" \
73+
"bltu a0, %2, 3b\n\t" \
74+
THEAD_SYNC_S \
75+
: : "r"(_cachesize), \
76+
"r"((unsigned long)(_start) & ~((_cachesize) - 1UL)), \
77+
"r"((unsigned long)(_start) + (_size)) \
78+
: "a0")
79+
80+
static void thead_errata_cache_inv(phys_addr_t paddr, size_t size)
81+
{
82+
void *vaddr = phys_to_virt(paddr);
83+
84+
THEAD_CMO_OP(INVAL, vaddr, size, riscv_cbom_block_size);
85+
}
86+
87+
static void thead_errata_cache_wback(phys_addr_t paddr, size_t size)
88+
{
89+
void *vaddr = phys_to_virt(paddr);
90+
91+
THEAD_CMO_OP(CLEAN, vaddr, size, riscv_cbom_block_size);
92+
}
93+
94+
static void thead_errata_cache_wback_inv(phys_addr_t paddr, size_t size)
95+
{
96+
void *vaddr = phys_to_virt(paddr);
97+
98+
THEAD_CMO_OP(FLUSH, vaddr, size, riscv_cbom_block_size);
99+
}
100+
101+
static const struct riscv_nonstd_cache_ops thead_errata_cmo_ops = {
102+
.wback = &thead_errata_cache_wback,
103+
.inv = &thead_errata_cache_inv,
104+
.wback_inv = &thead_errata_cache_wback_inv,
105+
};
106+
36107
static bool errata_probe_cmo(unsigned int stage,
37108
unsigned long arch_id, unsigned long impid)
38109
{
@@ -48,6 +119,7 @@ static bool errata_probe_cmo(unsigned int stage,
48119
if (stage == RISCV_ALTERNATIVES_BOOT) {
49120
riscv_cbom_block_size = L1_CACHE_BYTES;
50121
riscv_noncoherent_supported();
122+
riscv_noncoherent_register_cache_ops(&thead_errata_cmo_ops);
51123
}
52124

53125
return true;
@@ -77,8 +149,7 @@ static u32 thead_errata_probe(unsigned int stage,
77149
if (errata_probe_pbmt(stage, archid, impid))
78150
cpu_req_errata |= BIT(ERRATA_THEAD_PBMT);
79151

80-
if (errata_probe_cmo(stage, archid, impid))
81-
cpu_req_errata |= BIT(ERRATA_THEAD_CMO);
152+
errata_probe_cmo(stage, archid, impid);
82153

83154
if (errata_probe_pmu(stage, archid, impid))
84155
cpu_req_errata |= BIT(ERRATA_THEAD_PMU);

arch/riscv/include/asm/errata_list.h

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@
2424

2525
#ifdef CONFIG_ERRATA_THEAD
2626
#define ERRATA_THEAD_PBMT 0
27-
#define ERRATA_THEAD_CMO 1
28-
#define ERRATA_THEAD_PMU 2
29-
#define ERRATA_THEAD_NUMBER 3
27+
#define ERRATA_THEAD_PMU 1
28+
#define ERRATA_THEAD_NUMBER 2
3029
#endif
3130

3231
#ifdef __ASSEMBLY__
@@ -104,54 +103,17 @@ asm volatile(ALTERNATIVE( \
104103
#define ALT_THEAD_PMA(_val)
105104
#endif
106105

107-
/*
108-
* th.dcache.ipa rs1 (invalidate, physical address)
109-
* | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
110-
* 0000001 01010 rs1 000 00000 0001011
111-
* th.dache.iva rs1 (invalida, virtual address)
112-
* 0000001 00110 rs1 000 00000 0001011
113-
*
114-
* th.dcache.cpa rs1 (clean, physical address)
115-
* | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
116-
* 0000001 01001 rs1 000 00000 0001011
117-
* th.dcache.cva rs1 (clean, virtual address)
118-
* 0000001 00101 rs1 000 00000 0001011
119-
*
120-
* th.dcache.cipa rs1 (clean then invalidate, physical address)
121-
* | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
122-
* 0000001 01011 rs1 000 00000 0001011
123-
* th.dcache.civa rs1 (... virtual address)
124-
* 0000001 00111 rs1 000 00000 0001011
125-
*
126-
* th.sync.s (make sure all cache operations finished)
127-
* | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
128-
* 0000000 11001 00000 000 00000 0001011
129-
*/
130-
#define THEAD_INVAL_A0 ".long 0x0265000b"
131-
#define THEAD_CLEAN_A0 ".long 0x0255000b"
132-
#define THEAD_FLUSH_A0 ".long 0x0275000b"
133-
#define THEAD_SYNC_S ".long 0x0190000b"
134-
135106
#define ALT_CMO_OP(_op, _start, _size, _cachesize) \
136-
asm volatile(ALTERNATIVE_2( \
137-
__nops(6), \
107+
asm volatile(ALTERNATIVE( \
108+
__nops(5), \
138109
"mv a0, %1\n\t" \
139110
"j 2f\n\t" \
140111
"3:\n\t" \
141112
CBO_##_op(a0) \
142113
"add a0, a0, %0\n\t" \
143114
"2:\n\t" \
144-
"bltu a0, %2, 3b\n\t" \
145-
"nop", 0, RISCV_ISA_EXT_ZICBOM, CONFIG_RISCV_ISA_ZICBOM, \
146-
"mv a0, %1\n\t" \
147-
"j 2f\n\t" \
148-
"3:\n\t" \
149-
THEAD_##_op##_A0 "\n\t" \
150-
"add a0, a0, %0\n\t" \
151-
"2:\n\t" \
152-
"bltu a0, %2, 3b\n\t" \
153-
THEAD_SYNC_S, THEAD_VENDOR_ID, \
154-
ERRATA_THEAD_CMO, CONFIG_ERRATA_THEAD_CMO) \
115+
"bltu a0, %2, 3b\n\t", \
116+
0, RISCV_ISA_EXT_ZICBOM, CONFIG_RISCV_ISA_ZICBOM) \
155117
: : "r"(_cachesize), \
156118
"r"((unsigned long)(_start) & ~((_cachesize) - 1UL)), \
157119
"r"((unsigned long)(_start) + (_size)) \

0 commit comments

Comments
 (0)