cpuinfo.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Record and handle CPU attributes.
  3. *
  4. * Copyright (C) 2014 ARM Ltd.
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <asm/arch_timer.h>
  18. #include <asm/cachetype.h>
  19. #include <asm/cpu.h>
  20. #include <asm/cputype.h>
  21. #include <linux/bitops.h>
  22. #include <linux/init.h>
  23. #include <linux/kernel.h>
  24. #include <linux/printk.h>
  25. #include <linux/smp.h>
  26. /*
  27. * In case the boot CPU is hotpluggable, we record its initial state and
  28. * current state separately. Certain system registers may contain different
  29. * values depending on configuration at or after reset.
  30. */
  31. DEFINE_PER_CPU(struct cpuinfo_arm64, cpu_data);
  32. static struct cpuinfo_arm64 boot_cpu_data;
  33. static char *icache_policy_str[] = {
  34. [ICACHE_POLICY_RESERVED] = "RESERVED/UNKNOWN",
  35. [ICACHE_POLICY_AIVIVT] = "AIVIVT",
  36. [ICACHE_POLICY_VIPT] = "VIPT",
  37. [ICACHE_POLICY_PIPT] = "PIPT",
  38. };
  39. unsigned long __icache_flags;
  40. static void cpuinfo_detect_icache_policy(struct cpuinfo_arm64 *info)
  41. {
  42. unsigned int cpu = smp_processor_id();
  43. u32 l1ip = CTR_L1IP(info->reg_ctr);
  44. if (l1ip != ICACHE_POLICY_PIPT)
  45. set_bit(ICACHEF_ALIASING, &__icache_flags);
  46. if (l1ip == ICACHE_POLICY_AIVIVT)
  47. set_bit(ICACHEF_AIVIVT, &__icache_flags);
  48. pr_info("Detected %s I-cache on CPU%d\n", icache_policy_str[l1ip], cpu);
  49. }
  50. static int check_reg_mask(char *name, u64 mask, u64 boot, u64 cur, int cpu)
  51. {
  52. if ((boot & mask) == (cur & mask))
  53. return 0;
  54. pr_warn("SANITY CHECK: Unexpected variation in %s. Boot CPU: %#016lx, CPU%d: %#016lx\n",
  55. name, (unsigned long)boot, cpu, (unsigned long)cur);
  56. return 1;
  57. }
  58. #define CHECK_MASK(field, mask, boot, cur, cpu) \
  59. check_reg_mask(#field, mask, (boot)->reg_ ## field, (cur)->reg_ ## field, cpu)
  60. #define CHECK(field, boot, cur, cpu) \
  61. CHECK_MASK(field, ~0ULL, boot, cur, cpu)
  62. /*
  63. * Verify that CPUs don't have unexpected differences that will cause problems.
  64. */
  65. static void cpuinfo_sanity_check(struct cpuinfo_arm64 *cur)
  66. {
  67. unsigned int cpu = smp_processor_id();
  68. struct cpuinfo_arm64 *boot = &boot_cpu_data;
  69. unsigned int diff = 0;
  70. /*
  71. * The kernel can handle differing I-cache policies, but otherwise
  72. * caches should look identical. Userspace JITs will make use of
  73. * *minLine.
  74. */
  75. diff |= CHECK_MASK(ctr, 0xffff3fff, boot, cur, cpu);
  76. /*
  77. * Userspace may perform DC ZVA instructions. Mismatched block sizes
  78. * could result in too much or too little memory being zeroed if a
  79. * process is preempted and migrated between CPUs.
  80. */
  81. diff |= CHECK(dczid, boot, cur, cpu);
  82. /* If different, timekeeping will be broken (especially with KVM) */
  83. diff |= CHECK(cntfrq, boot, cur, cpu);
  84. /*
  85. * Even in big.LITTLE, processors should be identical instruction-set
  86. * wise.
  87. */
  88. diff |= CHECK(id_aa64isar0, boot, cur, cpu);
  89. diff |= CHECK(id_aa64isar1, boot, cur, cpu);
  90. /*
  91. * Differing PARange support is fine as long as all peripherals and
  92. * memory are mapped within the minimum PARange of all CPUs.
  93. * Linux should not care about secure memory.
  94. * ID_AA64MMFR1 is currently RES0.
  95. */
  96. diff |= CHECK_MASK(id_aa64mmfr0, 0xffffffffffff0ff0, boot, cur, cpu);
  97. diff |= CHECK(id_aa64mmfr1, boot, cur, cpu);
  98. /*
  99. * EL3 is not our concern.
  100. * ID_AA64PFR1 is currently RES0.
  101. */
  102. diff |= CHECK_MASK(id_aa64pfr0, 0xffffffffffff0fff, boot, cur, cpu);
  103. diff |= CHECK(id_aa64pfr1, boot, cur, cpu);
  104. /*
  105. * If we have AArch32, we care about 32-bit features for compat. These
  106. * registers should be RES0 otherwise.
  107. */
  108. diff |= CHECK(id_isar0, boot, cur, cpu);
  109. diff |= CHECK(id_isar1, boot, cur, cpu);
  110. diff |= CHECK(id_isar2, boot, cur, cpu);
  111. diff |= CHECK(id_isar3, boot, cur, cpu);
  112. diff |= CHECK(id_isar4, boot, cur, cpu);
  113. diff |= CHECK(id_isar5, boot, cur, cpu);
  114. diff |= CHECK(id_mmfr0, boot, cur, cpu);
  115. diff |= CHECK(id_mmfr1, boot, cur, cpu);
  116. diff |= CHECK(id_mmfr2, boot, cur, cpu);
  117. diff |= CHECK(id_mmfr3, boot, cur, cpu);
  118. diff |= CHECK(id_pfr0, boot, cur, cpu);
  119. diff |= CHECK(id_pfr1, boot, cur, cpu);
  120. /*
  121. * Mismatched CPU features are a recipe for disaster. Don't even
  122. * pretend to support them.
  123. */
  124. WARN_TAINT_ONCE(diff, TAINT_CPU_OUT_OF_SPEC,
  125. "Unsupported CPU feature variation.");
  126. }
  127. static void __cpuinfo_store_cpu(struct cpuinfo_arm64 *info)
  128. {
  129. info->reg_cntfrq = arch_timer_get_cntfrq();
  130. info->reg_ctr = read_cpuid_cachetype();
  131. info->reg_dczid = read_cpuid(DCZID_EL0);
  132. info->reg_midr = read_cpuid_id();
  133. info->reg_id_aa64isar0 = read_cpuid(ID_AA64ISAR0_EL1);
  134. info->reg_id_aa64isar1 = read_cpuid(ID_AA64ISAR1_EL1);
  135. info->reg_id_aa64mmfr0 = read_cpuid(ID_AA64MMFR0_EL1);
  136. info->reg_id_aa64mmfr1 = read_cpuid(ID_AA64MMFR1_EL1);
  137. info->reg_id_aa64pfr0 = read_cpuid(ID_AA64PFR0_EL1);
  138. info->reg_id_aa64pfr1 = read_cpuid(ID_AA64PFR1_EL1);
  139. info->reg_id_isar0 = read_cpuid(ID_ISAR0_EL1);
  140. info->reg_id_isar1 = read_cpuid(ID_ISAR1_EL1);
  141. info->reg_id_isar2 = read_cpuid(ID_ISAR2_EL1);
  142. info->reg_id_isar3 = read_cpuid(ID_ISAR3_EL1);
  143. info->reg_id_isar4 = read_cpuid(ID_ISAR4_EL1);
  144. info->reg_id_isar5 = read_cpuid(ID_ISAR5_EL1);
  145. info->reg_id_mmfr0 = read_cpuid(ID_MMFR0_EL1);
  146. info->reg_id_mmfr1 = read_cpuid(ID_MMFR1_EL1);
  147. info->reg_id_mmfr2 = read_cpuid(ID_MMFR2_EL1);
  148. info->reg_id_mmfr3 = read_cpuid(ID_MMFR3_EL1);
  149. info->reg_id_pfr0 = read_cpuid(ID_PFR0_EL1);
  150. info->reg_id_pfr1 = read_cpuid(ID_PFR1_EL1);
  151. cpuinfo_detect_icache_policy(info);
  152. }
  153. void cpuinfo_store_cpu(void)
  154. {
  155. struct cpuinfo_arm64 *info = this_cpu_ptr(&cpu_data);
  156. __cpuinfo_store_cpu(info);
  157. cpuinfo_sanity_check(info);
  158. }
  159. void __init cpuinfo_store_boot_cpu(void)
  160. {
  161. struct cpuinfo_arm64 *info = &per_cpu(cpu_data, 0);
  162. __cpuinfo_store_cpu(info);
  163. boot_cpu_data = *info;
  164. }