alternative.h 2.3 KB

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