RISC-V: Use Zicboz in clear_page when available
authorAndrew Jones <ajones@ventanamicro.com>
Fri, 24 Feb 2023 16:26:29 +0000 (17:26 +0100)
committerPalmer Dabbelt <palmer@rivosinc.com>
Wed, 15 Mar 2023 04:26:06 +0000 (21:26 -0700)
commitab0f77465e3e4ec2d2583cd770b157b16cc22844
tree78f3a70baf7f1121d4a32aecfc13d415ce50e865
parentd25f256332cc795b0fe16ba541fe0c05f0eb2c59
RISC-V: Use Zicboz in clear_page when available

Using memset() to zero a 4K page takes 563 total instructions, where
20 are branches. clear_page(), with Zicboz and a 64 byte block size,
takes 169 total instructions, where 4 are branches and 33 are nops.
Even though the block size is a variable, thanks to alternatives, we
can still implement a Duff device without having to do any preliminary
calculations. This is achieved by using the alternatives' cpufeature
value (the upper 16 bits of patch_id). The value used is the maximum
zicboz block size order accepted at the patch site. This enables us
to stop patching / unrolling when 4K bytes have been zeroed (we would
loop and continue after 4K if the page size would be larger)

For 4K pages, unrolling 16 times allows block sizes of 64 and 128 to
only loop a few times and larger block sizes to not loop at all. Since
cbo.zero doesn't take an offset, we also need an 'add' after each
instruction, making the loop body 112 to 160 bytes. Hopefully this
is small enough to not cause icache misses.

Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
Acked-by: Conor Dooley <conor.dooley@microchip.com>
Link: https://lore.kernel.org/r/20230224162631.405473-7-ajones@ventanamicro.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
arch/riscv/Kconfig
arch/riscv/include/asm/insn-def.h
arch/riscv/include/asm/page.h
arch/riscv/kernel/cpufeature.c
arch/riscv/lib/Makefile
arch/riscv/lib/clear_page.S [new file with mode: 0644]