alternative.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * alternative runtime patching
  4. * inspired by the ARM64 and x86 version
  5. *
  6. * Copyright (C) 2021 Sifive.
  7. */
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/cpu.h>
  11. #include <linux/uaccess.h>
  12. #include <asm/alternative.h>
  13. #include <asm/sections.h>
  14. #include <asm/vendorid_list.h>
  15. #include <asm/sbi.h>
  16. #include <asm/csr.h>
  17. struct cpu_manufacturer_info_t {
  18. unsigned long vendor_id;
  19. unsigned long arch_id;
  20. unsigned long imp_id;
  21. void (*patch_func)(struct alt_entry *begin, struct alt_entry *end,
  22. unsigned long archid, unsigned long impid,
  23. unsigned int stage);
  24. };
  25. static void __init_or_module riscv_fill_cpu_mfr_info(struct cpu_manufacturer_info_t *cpu_mfr_info)
  26. {
  27. #ifdef CONFIG_RISCV_M_MODE
  28. cpu_mfr_info->vendor_id = csr_read(CSR_MVENDORID);
  29. cpu_mfr_info->arch_id = csr_read(CSR_MARCHID);
  30. cpu_mfr_info->imp_id = csr_read(CSR_MIMPID);
  31. #else
  32. cpu_mfr_info->vendor_id = sbi_get_mvendorid();
  33. cpu_mfr_info->arch_id = sbi_get_marchid();
  34. cpu_mfr_info->imp_id = sbi_get_mimpid();
  35. #endif
  36. switch (cpu_mfr_info->vendor_id) {
  37. #ifdef CONFIG_ERRATA_SIFIVE
  38. case SIFIVE_VENDOR_ID:
  39. cpu_mfr_info->patch_func = sifive_errata_patch_func;
  40. break;
  41. #endif
  42. #ifdef CONFIG_ERRATA_THEAD
  43. case THEAD_VENDOR_ID:
  44. cpu_mfr_info->patch_func = thead_errata_patch_func;
  45. break;
  46. #endif
  47. default:
  48. cpu_mfr_info->patch_func = NULL;
  49. }
  50. }
  51. /*
  52. * This is called very early in the boot process (directly after we run
  53. * a feature detect on the boot CPU). No need to worry about other CPUs
  54. * here.
  55. */
  56. static void __init_or_module _apply_alternatives(struct alt_entry *begin,
  57. struct alt_entry *end,
  58. unsigned int stage)
  59. {
  60. struct cpu_manufacturer_info_t cpu_mfr_info;
  61. riscv_fill_cpu_mfr_info(&cpu_mfr_info);
  62. riscv_cpufeature_patch_func(begin, end, stage);
  63. if (!cpu_mfr_info.patch_func)
  64. return;
  65. cpu_mfr_info.patch_func(begin, end,
  66. cpu_mfr_info.arch_id,
  67. cpu_mfr_info.imp_id,
  68. stage);
  69. }
  70. void __init apply_boot_alternatives(void)
  71. {
  72. /* If called on non-boot cpu things could go wrong */
  73. WARN_ON(smp_processor_id() != 0);
  74. _apply_alternatives((struct alt_entry *)__alt_start,
  75. (struct alt_entry *)__alt_end,
  76. RISCV_ALTERNATIVES_BOOT);
  77. }
  78. /*
  79. * apply_early_boot_alternatives() is called from setup_vm() with MMU-off.
  80. *
  81. * Following requirements should be honoured for it to work correctly:
  82. * 1) It should use PC-relative addressing for accessing kernel symbols.
  83. * To achieve this we always use GCC cmodel=medany.
  84. * 2) The compiler instrumentation for FTRACE will not work for setup_vm()
  85. * so disable compiler instrumentation when FTRACE is enabled.
  86. *
  87. * Currently, the above requirements are honoured by using custom CFLAGS
  88. * for alternative.o in kernel/Makefile.
  89. */
  90. void __init apply_early_boot_alternatives(void)
  91. {
  92. #ifdef CONFIG_RISCV_ALTERNATIVE_EARLY
  93. _apply_alternatives((struct alt_entry *)__alt_start,
  94. (struct alt_entry *)__alt_end,
  95. RISCV_ALTERNATIVES_EARLY_BOOT);
  96. #endif
  97. }
  98. #ifdef CONFIG_MODULES
  99. void apply_module_alternatives(void *start, size_t length)
  100. {
  101. _apply_alternatives((struct alt_entry *)start,
  102. (struct alt_entry *)(start + length),
  103. RISCV_ALTERNATIVES_MODULE);
  104. }
  105. #endif