cputable.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2001 Ben. Herrenschmidt (benh@kernel.crashing.org)
  4. *
  5. * Modifications for ppc64:
  6. * Copyright (C) 2003 Dave Engebretsen <engebret@us.ibm.com>
  7. */
  8. #include <linux/string.h>
  9. #include <linux/sched.h>
  10. #include <linux/threads.h>
  11. #include <linux/init.h>
  12. #include <linux/export.h>
  13. #include <linux/jump_label.h>
  14. #include <linux/of.h>
  15. #include <asm/cputable.h>
  16. #include <asm/mce.h>
  17. #include <asm/mmu.h>
  18. #include <asm/setup.h>
  19. #include <asm/cpu_setup.h>
  20. static struct cpu_spec the_cpu_spec __ro_after_init;
  21. struct cpu_spec *cur_cpu_spec __ro_after_init = NULL;
  22. EXPORT_SYMBOL(cur_cpu_spec);
  23. /* The platform string corresponding to the real PVR */
  24. const char *powerpc_base_platform;
  25. #include "cpu_specs.h"
  26. void __init set_cur_cpu_spec(struct cpu_spec *s)
  27. {
  28. struct cpu_spec *t = &the_cpu_spec;
  29. t = PTRRELOC(t);
  30. /*
  31. * use memcpy() instead of *t = *s so that GCC replaces it
  32. * by __memcpy() when KASAN is active
  33. */
  34. memcpy(t, s, sizeof(*t));
  35. *PTRRELOC(&cur_cpu_spec) = &the_cpu_spec;
  36. }
  37. static struct cpu_spec * __init setup_cpu_spec(unsigned long offset,
  38. struct cpu_spec *s)
  39. {
  40. struct cpu_spec *t = &the_cpu_spec;
  41. struct cpu_spec old;
  42. t = PTRRELOC(t);
  43. old = *t;
  44. /*
  45. * Copy everything, then do fixups. Use memcpy() instead of *t = *s
  46. * so that GCC replaces it by __memcpy() when KASAN is active
  47. */
  48. memcpy(t, s, sizeof(*t));
  49. /*
  50. * If we are overriding a previous value derived from the real
  51. * PVR with a new value obtained using a logical PVR value,
  52. * don't modify the performance monitor fields.
  53. */
  54. if (old.num_pmcs && !s->num_pmcs) {
  55. t->num_pmcs = old.num_pmcs;
  56. t->pmc_type = old.pmc_type;
  57. /*
  58. * Let's ensure that the
  59. * fix for the PMAO bug is enabled on compatibility mode.
  60. */
  61. t->cpu_features |= old.cpu_features & CPU_FTR_PMAO_BUG;
  62. }
  63. /* Set kuap ON at startup, will be disabled later if cmdline has 'nosmap' */
  64. if (IS_ENABLED(CONFIG_PPC_KUAP) && IS_ENABLED(CONFIG_PPC32))
  65. t->mmu_features |= MMU_FTR_KUAP;
  66. *PTRRELOC(&cur_cpu_spec) = &the_cpu_spec;
  67. /*
  68. * Set the base platform string once; assumes
  69. * we're called with real pvr first.
  70. */
  71. if (*PTRRELOC(&powerpc_base_platform) == NULL)
  72. *PTRRELOC(&powerpc_base_platform) = t->platform;
  73. #if defined(CONFIG_PPC64) || defined(CONFIG_BOOKE)
  74. /* ppc64 and booke expect identify_cpu to also call setup_cpu for
  75. * that processor. I will consolidate that at a later time, for now,
  76. * just use #ifdef. We also don't need to PTRRELOC the function
  77. * pointer on ppc64 and booke as we are running at 0 in real mode
  78. * on ppc64 and reloc_offset is always 0 on booke.
  79. */
  80. if (t->cpu_setup) {
  81. t->cpu_setup(offset, t);
  82. }
  83. #endif /* CONFIG_PPC64 || CONFIG_BOOKE */
  84. return t;
  85. }
  86. struct cpu_spec * __init identify_cpu(unsigned long offset, unsigned int pvr)
  87. {
  88. struct cpu_spec *s = cpu_specs;
  89. int i;
  90. BUILD_BUG_ON(!ARRAY_SIZE(cpu_specs));
  91. s = PTRRELOC(s);
  92. for (i = 0; i < ARRAY_SIZE(cpu_specs); i++,s++) {
  93. if ((pvr & s->pvr_mask) == s->pvr_value)
  94. return setup_cpu_spec(offset, s);
  95. }
  96. BUG();
  97. return NULL;
  98. }
  99. /*
  100. * Used by cpufeatures to get the name for CPUs with a PVR table.
  101. * If they don't hae a PVR table, cpufeatures gets the name from
  102. * cpu device-tree node.
  103. */
  104. void __init identify_cpu_name(unsigned int pvr)
  105. {
  106. struct cpu_spec *s = cpu_specs;
  107. struct cpu_spec *t = &the_cpu_spec;
  108. int i;
  109. s = PTRRELOC(s);
  110. t = PTRRELOC(t);
  111. for (i = 0; i < ARRAY_SIZE(cpu_specs); i++,s++) {
  112. if ((pvr & s->pvr_mask) == s->pvr_value) {
  113. t->cpu_name = s->cpu_name;
  114. return;
  115. }
  116. }
  117. }
  118. #ifdef CONFIG_JUMP_LABEL_FEATURE_CHECKS
  119. struct static_key_true cpu_feature_keys[NUM_CPU_FTR_KEYS] = {
  120. [0 ... NUM_CPU_FTR_KEYS - 1] = STATIC_KEY_TRUE_INIT
  121. };
  122. EXPORT_SYMBOL_GPL(cpu_feature_keys);
  123. void __init cpu_feature_keys_init(void)
  124. {
  125. int i;
  126. for (i = 0; i < NUM_CPU_FTR_KEYS; i++) {
  127. unsigned long f = 1ul << i;
  128. if (!(cur_cpu_spec->cpu_features & f))
  129. static_branch_disable(&cpu_feature_keys[i]);
  130. }
  131. }
  132. struct static_key_true mmu_feature_keys[NUM_MMU_FTR_KEYS] = {
  133. [0 ... NUM_MMU_FTR_KEYS - 1] = STATIC_KEY_TRUE_INIT
  134. };
  135. EXPORT_SYMBOL(mmu_feature_keys);
  136. void __init mmu_feature_keys_init(void)
  137. {
  138. int i;
  139. for (i = 0; i < NUM_MMU_FTR_KEYS; i++) {
  140. unsigned long f = 1ul << i;
  141. if (!(cur_cpu_spec->mmu_features & f))
  142. static_branch_disable(&mmu_feature_keys[i]);
  143. }
  144. }
  145. #endif