cpuinfo.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/bug.h>
  23. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/preempt.h>
  26. #include <linux/printk.h>
  27. #include <linux/smp.h>
  28. /*
  29. * In case the boot CPU is hotpluggable, we record its initial state and
  30. * current state separately. Certain system registers may contain different
  31. * values depending on configuration at or after reset.
  32. */
  33. DEFINE_PER_CPU(struct cpuinfo_arm64, cpu_data);
  34. static struct cpuinfo_arm64 boot_cpu_data;
  35. static char *icache_policy_str[] = {
  36. [ICACHE_POLICY_RESERVED] = "RESERVED/UNKNOWN",
  37. [ICACHE_POLICY_AIVIVT] = "AIVIVT",
  38. [ICACHE_POLICY_VIPT] = "VIPT",
  39. [ICACHE_POLICY_PIPT] = "PIPT",
  40. };
  41. unsigned long __icache_flags;
  42. static void cpuinfo_detect_icache_policy(struct cpuinfo_arm64 *info)
  43. {
  44. unsigned int cpu = smp_processor_id();
  45. u32 l1ip = CTR_L1IP(info->reg_ctr);
  46. if (l1ip != ICACHE_POLICY_PIPT) {
  47. /*
  48. * VIPT caches are non-aliasing if the VA always equals the PA
  49. * in all bit positions that are covered by the index. This is
  50. * the case if the size of a way (# of sets * line size) does
  51. * not exceed PAGE_SIZE.
  52. */
  53. u32 waysize = icache_get_numsets() * icache_get_linesize();
  54. if (l1ip != ICACHE_POLICY_VIPT || waysize > PAGE_SIZE)
  55. set_bit(ICACHEF_ALIASING, &__icache_flags);
  56. }
  57. if (l1ip == ICACHE_POLICY_AIVIVT)
  58. set_bit(ICACHEF_AIVIVT, &__icache_flags);
  59. pr_info("Detected %s I-cache on CPU%d\n", icache_policy_str[l1ip], cpu);
  60. }
  61. static int check_reg_mask(char *name, u64 mask, u64 boot, u64 cur, int cpu)
  62. {
  63. if ((boot & mask) == (cur & mask))
  64. return 0;
  65. pr_warn("SANITY CHECK: Unexpected variation in %s. Boot CPU: %#016lx, CPU%d: %#016lx\n",
  66. name, (unsigned long)boot, cpu, (unsigned long)cur);
  67. return 1;
  68. }
  69. #define CHECK_MASK(field, mask, boot, cur, cpu) \
  70. check_reg_mask(#field, mask, (boot)->reg_ ## field, (cur)->reg_ ## field, cpu)
  71. #define CHECK(field, boot, cur, cpu) \
  72. CHECK_MASK(field, ~0ULL, boot, cur, cpu)
  73. /*
  74. * Verify that CPUs don't have unexpected differences that will cause problems.
  75. */
  76. static void cpuinfo_sanity_check(struct cpuinfo_arm64 *cur)
  77. {
  78. unsigned int cpu = smp_processor_id();
  79. struct cpuinfo_arm64 *boot = &boot_cpu_data;
  80. unsigned int diff = 0;
  81. /*
  82. * The kernel can handle differing I-cache policies, but otherwise
  83. * caches should look identical. Userspace JITs will make use of
  84. * *minLine.
  85. */
  86. diff |= CHECK_MASK(ctr, 0xffff3fff, boot, cur, cpu);
  87. /*
  88. * Userspace may perform DC ZVA instructions. Mismatched block sizes
  89. * could result in too much or too little memory being zeroed if a
  90. * process is preempted and migrated between CPUs.
  91. */
  92. diff |= CHECK(dczid, boot, cur, cpu);
  93. /* If different, timekeeping will be broken (especially with KVM) */
  94. diff |= CHECK(cntfrq, boot, cur, cpu);
  95. /*
  96. * Even in big.LITTLE, processors should be identical instruction-set
  97. * wise.
  98. */
  99. diff |= CHECK(id_aa64isar0, boot, cur, cpu);
  100. diff |= CHECK(id_aa64isar1, boot, cur, cpu);
  101. /*
  102. * Differing PARange support is fine as long as all peripherals and
  103. * memory are mapped within the minimum PARange of all CPUs.
  104. * Linux should not care about secure memory.
  105. * ID_AA64MMFR1 is currently RES0.
  106. */
  107. diff |= CHECK_MASK(id_aa64mmfr0, 0xffffffffffff0ff0, boot, cur, cpu);
  108. diff |= CHECK(id_aa64mmfr1, boot, cur, cpu);
  109. /*
  110. * EL3 is not our concern.
  111. * ID_AA64PFR1 is currently RES0.
  112. */
  113. diff |= CHECK_MASK(id_aa64pfr0, 0xffffffffffff0fff, boot, cur, cpu);
  114. diff |= CHECK(id_aa64pfr1, boot, cur, cpu);
  115. /*
  116. * If we have AArch32, we care about 32-bit features for compat. These
  117. * registers should be RES0 otherwise.
  118. */
  119. diff |= CHECK(id_isar0, boot, cur, cpu);
  120. diff |= CHECK(id_isar1, boot, cur, cpu);
  121. diff |= CHECK(id_isar2, boot, cur, cpu);
  122. diff |= CHECK(id_isar3, boot, cur, cpu);
  123. diff |= CHECK(id_isar4, boot, cur, cpu);
  124. diff |= CHECK(id_isar5, boot, cur, cpu);
  125. diff |= CHECK(id_mmfr0, boot, cur, cpu);
  126. diff |= CHECK(id_mmfr1, boot, cur, cpu);
  127. diff |= CHECK(id_mmfr2, boot, cur, cpu);
  128. diff |= CHECK(id_mmfr3, boot, cur, cpu);
  129. diff |= CHECK(id_pfr0, boot, cur, cpu);
  130. diff |= CHECK(id_pfr1, boot, cur, cpu);
  131. /*
  132. * Mismatched CPU features are a recipe for disaster. Don't even
  133. * pretend to support them.
  134. */
  135. WARN_TAINT_ONCE(diff, TAINT_CPU_OUT_OF_SPEC,
  136. "Unsupported CPU feature variation.");
  137. }
  138. static void __cpuinfo_store_cpu(struct cpuinfo_arm64 *info)
  139. {
  140. info->reg_cntfrq = arch_timer_get_cntfrq();
  141. info->reg_ctr = read_cpuid_cachetype();
  142. info->reg_dczid = read_cpuid(DCZID_EL0);
  143. info->reg_midr = read_cpuid_id();
  144. info->reg_id_aa64isar0 = read_cpuid(ID_AA64ISAR0_EL1);
  145. info->reg_id_aa64isar1 = read_cpuid(ID_AA64ISAR1_EL1);
  146. info->reg_id_aa64mmfr0 = read_cpuid(ID_AA64MMFR0_EL1);
  147. info->reg_id_aa64mmfr1 = read_cpuid(ID_AA64MMFR1_EL1);
  148. info->reg_id_aa64pfr0 = read_cpuid(ID_AA64PFR0_EL1);
  149. info->reg_id_aa64pfr1 = read_cpuid(ID_AA64PFR1_EL1);
  150. info->reg_id_isar0 = read_cpuid(ID_ISAR0_EL1);
  151. info->reg_id_isar1 = read_cpuid(ID_ISAR1_EL1);
  152. info->reg_id_isar2 = read_cpuid(ID_ISAR2_EL1);
  153. info->reg_id_isar3 = read_cpuid(ID_ISAR3_EL1);
  154. info->reg_id_isar4 = read_cpuid(ID_ISAR4_EL1);
  155. info->reg_id_isar5 = read_cpuid(ID_ISAR5_EL1);
  156. info->reg_id_mmfr0 = read_cpuid(ID_MMFR0_EL1);
  157. info->reg_id_mmfr1 = read_cpuid(ID_MMFR1_EL1);
  158. info->reg_id_mmfr2 = read_cpuid(ID_MMFR2_EL1);
  159. info->reg_id_mmfr3 = read_cpuid(ID_MMFR3_EL1);
  160. info->reg_id_pfr0 = read_cpuid(ID_PFR0_EL1);
  161. info->reg_id_pfr1 = read_cpuid(ID_PFR1_EL1);
  162. cpuinfo_detect_icache_policy(info);
  163. }
  164. void cpuinfo_store_cpu(void)
  165. {
  166. struct cpuinfo_arm64 *info = this_cpu_ptr(&cpu_data);
  167. __cpuinfo_store_cpu(info);
  168. cpuinfo_sanity_check(info);
  169. }
  170. void __init cpuinfo_store_boot_cpu(void)
  171. {
  172. struct cpuinfo_arm64 *info = &per_cpu(cpu_data, 0);
  173. __cpuinfo_store_cpu(info);
  174. boot_cpu_data = *info;
  175. }
  176. u64 __attribute_const__ icache_get_ccsidr(void)
  177. {
  178. u64 ccsidr;
  179. WARN_ON(preemptible());
  180. /* Select L1 I-cache and read its size ID register */
  181. asm("msr csselr_el1, %1; isb; mrs %0, ccsidr_el1"
  182. : "=r"(ccsidr) : "r"(1L));
  183. return ccsidr;
  184. }