cpuinfo.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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/cache.h>
  19. #include <asm/cpu.h>
  20. #include <asm/cputype.h>
  21. #include <asm/cpufeature.h>
  22. #include <asm/fpsimd.h>
  23. #include <linux/bitops.h>
  24. #include <linux/bug.h>
  25. #include <linux/compat.h>
  26. #include <linux/elf.h>
  27. #include <linux/init.h>
  28. #include <linux/kernel.h>
  29. #include <linux/personality.h>
  30. #include <linux/preempt.h>
  31. #include <linux/printk.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/sched.h>
  34. #include <linux/smp.h>
  35. #include <linux/delay.h>
  36. /*
  37. * In case the boot CPU is hotpluggable, we record its initial state and
  38. * current state separately. Certain system registers may contain different
  39. * values depending on configuration at or after reset.
  40. */
  41. DEFINE_PER_CPU(struct cpuinfo_arm64, cpu_data);
  42. static struct cpuinfo_arm64 boot_cpu_data;
  43. static char *icache_policy_str[] = {
  44. [0 ... ICACHE_POLICY_PIPT] = "RESERVED/UNKNOWN",
  45. [ICACHE_POLICY_VIPT] = "VIPT",
  46. [ICACHE_POLICY_PIPT] = "PIPT",
  47. [ICACHE_POLICY_VPIPT] = "VPIPT",
  48. };
  49. unsigned long __icache_flags;
  50. static const char *const hwcap_str[] = {
  51. "fp",
  52. "asimd",
  53. "evtstrm",
  54. "aes",
  55. "pmull",
  56. "sha1",
  57. "sha2",
  58. "crc32",
  59. "atomics",
  60. "fphp",
  61. "asimdhp",
  62. "cpuid",
  63. "asimdrdm",
  64. "jscvt",
  65. "fcma",
  66. "lrcpc",
  67. "dcpop",
  68. "sha3",
  69. "sm3",
  70. "sm4",
  71. "asimddp",
  72. "sha512",
  73. "sve",
  74. "asimdfhm",
  75. "dit",
  76. "uscat",
  77. "ilrcpc",
  78. "flagm",
  79. "ssbs",
  80. NULL
  81. };
  82. #ifdef CONFIG_COMPAT
  83. static const char *const compat_hwcap_str[] = {
  84. "swp",
  85. "half",
  86. "thumb",
  87. "26bit",
  88. "fastmult",
  89. "fpa",
  90. "vfp",
  91. "edsp",
  92. "java",
  93. "iwmmxt",
  94. "crunch",
  95. "thumbee",
  96. "neon",
  97. "vfpv3",
  98. "vfpv3d16",
  99. "tls",
  100. "vfpv4",
  101. "idiva",
  102. "idivt",
  103. "vfpd32",
  104. "lpae",
  105. "evtstrm",
  106. NULL
  107. };
  108. static const char *const compat_hwcap2_str[] = {
  109. "aes",
  110. "pmull",
  111. "sha1",
  112. "sha2",
  113. "crc32",
  114. NULL
  115. };
  116. #endif /* CONFIG_COMPAT */
  117. static int c_show(struct seq_file *m, void *v)
  118. {
  119. int i, j;
  120. bool compat = personality(current->personality) == PER_LINUX32;
  121. for_each_online_cpu(i) {
  122. struct cpuinfo_arm64 *cpuinfo = &per_cpu(cpu_data, i);
  123. u32 midr = cpuinfo->reg_midr;
  124. /*
  125. * glibc reads /proc/cpuinfo to determine the number of
  126. * online processors, looking for lines beginning with
  127. * "processor". Give glibc what it expects.
  128. */
  129. seq_printf(m, "processor\t: %d\n", i);
  130. if (compat)
  131. seq_printf(m, "model name\t: ARMv8 Processor rev %d (%s)\n",
  132. MIDR_REVISION(midr), COMPAT_ELF_PLATFORM);
  133. seq_printf(m, "BogoMIPS\t: %lu.%02lu\n",
  134. loops_per_jiffy / (500000UL/HZ),
  135. loops_per_jiffy / (5000UL/HZ) % 100);
  136. /*
  137. * Dump out the common processor features in a single line.
  138. * Userspace should read the hwcaps with getauxval(AT_HWCAP)
  139. * rather than attempting to parse this, but there's a body of
  140. * software which does already (at least for 32-bit).
  141. */
  142. seq_puts(m, "Features\t:");
  143. if (compat) {
  144. #ifdef CONFIG_COMPAT
  145. for (j = 0; compat_hwcap_str[j]; j++)
  146. if (compat_elf_hwcap & (1 << j))
  147. seq_printf(m, " %s", compat_hwcap_str[j]);
  148. for (j = 0; compat_hwcap2_str[j]; j++)
  149. if (compat_elf_hwcap2 & (1 << j))
  150. seq_printf(m, " %s", compat_hwcap2_str[j]);
  151. #endif /* CONFIG_COMPAT */
  152. } else {
  153. for (j = 0; hwcap_str[j]; j++)
  154. if (elf_hwcap & (1 << j))
  155. seq_printf(m, " %s", hwcap_str[j]);
  156. }
  157. seq_puts(m, "\n");
  158. seq_printf(m, "CPU implementer\t: 0x%02x\n",
  159. MIDR_IMPLEMENTOR(midr));
  160. seq_printf(m, "CPU architecture: 8\n");
  161. seq_printf(m, "CPU variant\t: 0x%x\n", MIDR_VARIANT(midr));
  162. seq_printf(m, "CPU part\t: 0x%03x\n", MIDR_PARTNUM(midr));
  163. seq_printf(m, "CPU revision\t: %d\n\n", MIDR_REVISION(midr));
  164. }
  165. return 0;
  166. }
  167. static void *c_start(struct seq_file *m, loff_t *pos)
  168. {
  169. return *pos < 1 ? (void *)1 : NULL;
  170. }
  171. static void *c_next(struct seq_file *m, void *v, loff_t *pos)
  172. {
  173. ++*pos;
  174. return NULL;
  175. }
  176. static void c_stop(struct seq_file *m, void *v)
  177. {
  178. }
  179. const struct seq_operations cpuinfo_op = {
  180. .start = c_start,
  181. .next = c_next,
  182. .stop = c_stop,
  183. .show = c_show
  184. };
  185. static struct kobj_type cpuregs_kobj_type = {
  186. .sysfs_ops = &kobj_sysfs_ops,
  187. };
  188. /*
  189. * The ARM ARM uses the phrase "32-bit register" to describe a register
  190. * whose upper 32 bits are RES0 (per C5.1.1, ARM DDI 0487A.i), however
  191. * no statement is made as to whether the upper 32 bits will or will not
  192. * be made use of in future, and between ARM DDI 0487A.c and ARM DDI
  193. * 0487A.d CLIDR_EL1 was expanded from 32-bit to 64-bit.
  194. *
  195. * Thus, while both MIDR_EL1 and REVIDR_EL1 are described as 32-bit
  196. * registers, we expose them both as 64 bit values to cater for possible
  197. * future expansion without an ABI break.
  198. */
  199. #define kobj_to_cpuinfo(kobj) container_of(kobj, struct cpuinfo_arm64, kobj)
  200. #define CPUREGS_ATTR_RO(_name, _field) \
  201. static ssize_t _name##_show(struct kobject *kobj, \
  202. struct kobj_attribute *attr, char *buf) \
  203. { \
  204. struct cpuinfo_arm64 *info = kobj_to_cpuinfo(kobj); \
  205. \
  206. if (info->reg_midr) \
  207. return sprintf(buf, "0x%016x\n", info->reg_##_field); \
  208. else \
  209. return 0; \
  210. } \
  211. static struct kobj_attribute cpuregs_attr_##_name = __ATTR_RO(_name)
  212. CPUREGS_ATTR_RO(midr_el1, midr);
  213. CPUREGS_ATTR_RO(revidr_el1, revidr);
  214. static struct attribute *cpuregs_id_attrs[] = {
  215. &cpuregs_attr_midr_el1.attr,
  216. &cpuregs_attr_revidr_el1.attr,
  217. NULL
  218. };
  219. static const struct attribute_group cpuregs_attr_group = {
  220. .attrs = cpuregs_id_attrs,
  221. .name = "identification"
  222. };
  223. static int cpuid_cpu_online(unsigned int cpu)
  224. {
  225. int rc;
  226. struct device *dev;
  227. struct cpuinfo_arm64 *info = &per_cpu(cpu_data, cpu);
  228. dev = get_cpu_device(cpu);
  229. if (!dev) {
  230. rc = -ENODEV;
  231. goto out;
  232. }
  233. rc = kobject_add(&info->kobj, &dev->kobj, "regs");
  234. if (rc)
  235. goto out;
  236. rc = sysfs_create_group(&info->kobj, &cpuregs_attr_group);
  237. if (rc)
  238. kobject_del(&info->kobj);
  239. out:
  240. return rc;
  241. }
  242. static int cpuid_cpu_offline(unsigned int cpu)
  243. {
  244. struct device *dev;
  245. struct cpuinfo_arm64 *info = &per_cpu(cpu_data, cpu);
  246. dev = get_cpu_device(cpu);
  247. if (!dev)
  248. return -ENODEV;
  249. if (info->kobj.parent) {
  250. sysfs_remove_group(&info->kobj, &cpuregs_attr_group);
  251. kobject_del(&info->kobj);
  252. }
  253. return 0;
  254. }
  255. static int __init cpuinfo_regs_init(void)
  256. {
  257. int cpu, ret;
  258. for_each_possible_cpu(cpu) {
  259. struct cpuinfo_arm64 *info = &per_cpu(cpu_data, cpu);
  260. kobject_init(&info->kobj, &cpuregs_kobj_type);
  261. }
  262. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "arm64/cpuinfo:online",
  263. cpuid_cpu_online, cpuid_cpu_offline);
  264. if (ret < 0) {
  265. pr_err("cpuinfo: failed to register hotplug callbacks.\n");
  266. return ret;
  267. }
  268. return 0;
  269. }
  270. static void cpuinfo_detect_icache_policy(struct cpuinfo_arm64 *info)
  271. {
  272. unsigned int cpu = smp_processor_id();
  273. u32 l1ip = CTR_L1IP(info->reg_ctr);
  274. switch (l1ip) {
  275. case ICACHE_POLICY_PIPT:
  276. break;
  277. case ICACHE_POLICY_VPIPT:
  278. set_bit(ICACHEF_VPIPT, &__icache_flags);
  279. break;
  280. default:
  281. /* Fallthrough */
  282. case ICACHE_POLICY_VIPT:
  283. /* Assume aliasing */
  284. set_bit(ICACHEF_ALIASING, &__icache_flags);
  285. }
  286. pr_info("Detected %s I-cache on CPU%d\n", icache_policy_str[l1ip], cpu);
  287. }
  288. static void __cpuinfo_store_cpu(struct cpuinfo_arm64 *info)
  289. {
  290. info->reg_cntfrq = arch_timer_get_cntfrq();
  291. info->reg_ctr = read_cpuid_cachetype();
  292. info->reg_dczid = read_cpuid(DCZID_EL0);
  293. info->reg_midr = read_cpuid_id();
  294. info->reg_revidr = read_cpuid(REVIDR_EL1);
  295. info->reg_id_aa64dfr0 = read_cpuid(ID_AA64DFR0_EL1);
  296. info->reg_id_aa64dfr1 = read_cpuid(ID_AA64DFR1_EL1);
  297. info->reg_id_aa64isar0 = read_cpuid(ID_AA64ISAR0_EL1);
  298. info->reg_id_aa64isar1 = read_cpuid(ID_AA64ISAR1_EL1);
  299. info->reg_id_aa64mmfr0 = read_cpuid(ID_AA64MMFR0_EL1);
  300. info->reg_id_aa64mmfr1 = read_cpuid(ID_AA64MMFR1_EL1);
  301. info->reg_id_aa64mmfr2 = read_cpuid(ID_AA64MMFR2_EL1);
  302. info->reg_id_aa64pfr0 = read_cpuid(ID_AA64PFR0_EL1);
  303. info->reg_id_aa64pfr1 = read_cpuid(ID_AA64PFR1_EL1);
  304. info->reg_id_aa64zfr0 = read_cpuid(ID_AA64ZFR0_EL1);
  305. /* Update the 32bit ID registers only if AArch32 is implemented */
  306. if (id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0)) {
  307. info->reg_id_dfr0 = read_cpuid(ID_DFR0_EL1);
  308. info->reg_id_isar0 = read_cpuid(ID_ISAR0_EL1);
  309. info->reg_id_isar1 = read_cpuid(ID_ISAR1_EL1);
  310. info->reg_id_isar2 = read_cpuid(ID_ISAR2_EL1);
  311. info->reg_id_isar3 = read_cpuid(ID_ISAR3_EL1);
  312. info->reg_id_isar4 = read_cpuid(ID_ISAR4_EL1);
  313. info->reg_id_isar5 = read_cpuid(ID_ISAR5_EL1);
  314. info->reg_id_mmfr0 = read_cpuid(ID_MMFR0_EL1);
  315. info->reg_id_mmfr1 = read_cpuid(ID_MMFR1_EL1);
  316. info->reg_id_mmfr2 = read_cpuid(ID_MMFR2_EL1);
  317. info->reg_id_mmfr3 = read_cpuid(ID_MMFR3_EL1);
  318. info->reg_id_pfr0 = read_cpuid(ID_PFR0_EL1);
  319. info->reg_id_pfr1 = read_cpuid(ID_PFR1_EL1);
  320. info->reg_mvfr0 = read_cpuid(MVFR0_EL1);
  321. info->reg_mvfr1 = read_cpuid(MVFR1_EL1);
  322. info->reg_mvfr2 = read_cpuid(MVFR2_EL1);
  323. }
  324. if (IS_ENABLED(CONFIG_ARM64_SVE) &&
  325. id_aa64pfr0_sve(info->reg_id_aa64pfr0))
  326. info->reg_zcr = read_zcr_features();
  327. cpuinfo_detect_icache_policy(info);
  328. }
  329. void cpuinfo_store_cpu(void)
  330. {
  331. struct cpuinfo_arm64 *info = this_cpu_ptr(&cpu_data);
  332. __cpuinfo_store_cpu(info);
  333. update_cpu_features(smp_processor_id(), info, &boot_cpu_data);
  334. }
  335. void __init cpuinfo_store_boot_cpu(void)
  336. {
  337. struct cpuinfo_arm64 *info = &per_cpu(cpu_data, 0);
  338. __cpuinfo_store_cpu(info);
  339. boot_cpu_data = *info;
  340. init_cpu_features(&boot_cpu_data);
  341. }
  342. device_initcall(cpuinfo_regs_init);