xref: /linux/arch/riscv/include/asm/alternative.h (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2021 Sifive.
4  */
5 
6 #ifndef __ASM_ALTERNATIVE_H
7 #define __ASM_ALTERNATIVE_H
8 
9 #include <asm/alternative-macros.h>
10 
11 #ifndef __ASSEMBLER__
12 
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/stddef.h>
17 #include <asm/hwcap.h>
18 
19 #define PATCH_ID_CPUFEATURE_ID(p)		lower_16_bits(p)
20 #define PATCH_ID_CPUFEATURE_VALUE(p)		upper_16_bits(p)
21 
22 #define RISCV_ALTERNATIVES_BOOT		0 /* alternatives applied during regular boot */
23 #define RISCV_ALTERNATIVES_MODULE	1 /* alternatives applied during module-init */
24 #define RISCV_ALTERNATIVES_EARLY_BOOT	2 /* alternatives applied before mmu start */
25 
26 /* add the relative offset to the address of the offset to get the absolute address */
27 #define __ALT_PTR(a, f)			((void *)&(a)->f + (a)->f)
28 #define ALT_OLD_PTR(a)			__ALT_PTR(a, old_offset)
29 #define ALT_ALT_PTR(a)			__ALT_PTR(a, alt_offset)
30 
31 void __init apply_boot_alternatives(void);
32 void __init apply_early_boot_alternatives(void);
33 void apply_module_alternatives(void *start, size_t length);
34 
35 void riscv_alternative_fix_offsets(void *alt_ptr, unsigned int len,
36 				   int patch_offset, bool early);
37 
38 struct alt_entry {
39 	s32 old_offset;		/* offset relative to original instruction or data  */
40 	s32 alt_offset;		/* offset relative to replacement instruction or data */
41 	u16 vendor_id;		/* CPU vendor ID */
42 	u16 alt_len;		/* The replacement size */
43 	u32 patch_id;		/* The patch ID (erratum ID or cpufeature ID) */
44 };
45 
46 void andes_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
47 			     unsigned long archid, unsigned long impid,
48 			     unsigned int stage);
49 void mips_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
50 			    unsigned long archid, unsigned long impid,
51 			    unsigned int stage);
52 void sifive_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
53 			      unsigned long archid, unsigned long impid,
54 			      unsigned int stage);
55 void thead_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
56 			     unsigned long archid, unsigned long impid,
57 			     unsigned int stage);
58 
59 void riscv_cpufeature_patch_func(struct alt_entry *begin, struct alt_entry *end,
60 				 unsigned int stage);
61 
62 #endif
63 #endif
64