^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2013 Altera Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2011 Tobias Klauser <tklauser@distanz.ch>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Based on cpuinfo.c from microblaze
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <asm/cpuinfo.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct cpuinfo cpuinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define err_cpu(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) pr_err("ERROR: Nios II " x " different for kernel and DTS\n")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static inline u32 fcpu(struct device_node *cpu, const char *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) u32 val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) of_property_read_u32(cpu, n, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) void __init setup_cpuinfo(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct device_node *cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) const char *str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) cpu = of_get_cpu_node(0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (!cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) panic("%s: No CPU found in devicetree!\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (!of_property_read_bool(cpu, "altr,has-initda"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) panic("initda instruction is unimplemented. Please update your "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) "hardware system to have more than 4-byte line data "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) "cache\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) cpuinfo.cpu_clock_freq = fcpu(cpu, "clock-frequency");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) str = of_get_property(cpu, "altr,implementation", &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) strlcpy(cpuinfo.cpu_impl, str, sizeof(cpuinfo.cpu_impl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) strcpy(cpuinfo.cpu_impl, "<unknown>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) cpuinfo.has_div = of_property_read_bool(cpu, "altr,has-div");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) cpuinfo.has_mul = of_property_read_bool(cpu, "altr,has-mul");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) cpuinfo.has_mulx = of_property_read_bool(cpu, "altr,has-mulx");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) cpuinfo.has_bmx = of_property_read_bool(cpu, "altr,has-bmx");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) cpuinfo.has_cdx = of_property_read_bool(cpu, "altr,has-cdx");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) cpuinfo.mmu = of_property_read_bool(cpu, "altr,has-mmu");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (IS_ENABLED(CONFIG_NIOS2_HW_DIV_SUPPORT) && !cpuinfo.has_div)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) err_cpu("DIV");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (IS_ENABLED(CONFIG_NIOS2_HW_MUL_SUPPORT) && !cpuinfo.has_mul)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) err_cpu("MUL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (IS_ENABLED(CONFIG_NIOS2_HW_MULX_SUPPORT) && !cpuinfo.has_mulx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) err_cpu("MULX");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (IS_ENABLED(CONFIG_NIOS2_BMX_SUPPORT) && !cpuinfo.has_bmx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) err_cpu("BMX");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (IS_ENABLED(CONFIG_NIOS2_CDX_SUPPORT) && !cpuinfo.has_cdx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) err_cpu("CDX");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) cpuinfo.tlb_num_ways = fcpu(cpu, "altr,tlb-num-ways");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (!cpuinfo.tlb_num_ways)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) panic("altr,tlb-num-ways can't be 0. Please check your hardware "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) "system\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) cpuinfo.icache_line_size = fcpu(cpu, "icache-line-size");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) cpuinfo.icache_size = fcpu(cpu, "icache-size");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (CONFIG_NIOS2_ICACHE_SIZE != cpuinfo.icache_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) pr_warn("Warning: icache size configuration mismatch "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) "(0x%x vs 0x%x) of CONFIG_NIOS2_ICACHE_SIZE vs "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) "device tree icache-size\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) CONFIG_NIOS2_ICACHE_SIZE, cpuinfo.icache_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) cpuinfo.dcache_line_size = fcpu(cpu, "dcache-line-size");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (CONFIG_NIOS2_DCACHE_LINE_SIZE != cpuinfo.dcache_line_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) pr_warn("Warning: dcache line size configuration mismatch "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) "(0x%x vs 0x%x) of CONFIG_NIOS2_DCACHE_LINE_SIZE vs "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) "device tree dcache-line-size\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) CONFIG_NIOS2_DCACHE_LINE_SIZE, cpuinfo.dcache_line_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) cpuinfo.dcache_size = fcpu(cpu, "dcache-size");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (CONFIG_NIOS2_DCACHE_SIZE != cpuinfo.dcache_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) pr_warn("Warning: dcache size configuration mismatch "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) "(0x%x vs 0x%x) of CONFIG_NIOS2_DCACHE_SIZE vs "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) "device tree dcache-size\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) CONFIG_NIOS2_DCACHE_SIZE, cpuinfo.dcache_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) cpuinfo.tlb_pid_num_bits = fcpu(cpu, "altr,pid-num-bits");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) cpuinfo.tlb_num_ways_log2 = ilog2(cpuinfo.tlb_num_ways);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) cpuinfo.tlb_num_entries = fcpu(cpu, "altr,tlb-num-entries");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) cpuinfo.tlb_num_lines = cpuinfo.tlb_num_entries / cpuinfo.tlb_num_ways;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) cpuinfo.tlb_ptr_sz = fcpu(cpu, "altr,tlb-ptr-sz");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) cpuinfo.reset_addr = fcpu(cpu, "altr,reset-addr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) cpuinfo.exception_addr = fcpu(cpu, "altr,exception-addr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) cpuinfo.fast_tlb_miss_exc_addr = fcpu(cpu, "altr,fast-tlb-miss-addr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) of_node_put(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * Get CPU information for use by the procfs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static int show_cpuinfo(struct seq_file *m, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) const u32 clockfreq = cpuinfo.cpu_clock_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) seq_printf(m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) "CPU:\t\tNios II/%s\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) "REV:\t\t%i\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) "MMU:\t\t%s\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) "FPU:\t\tnone\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) "Clocking:\t%u.%02u MHz\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) "BogoMips:\t%lu.%02lu\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) "Calibration:\t%lu loops\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) cpuinfo.cpu_impl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) CONFIG_NIOS2_ARCH_REVISION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) cpuinfo.mmu ? "present" : "none",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) clockfreq / 1000000, (clockfreq / 100000) % 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) (loops_per_jiffy * HZ) / 500000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) ((loops_per_jiffy * HZ) / 5000) % 100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) (loops_per_jiffy * HZ));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) seq_printf(m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) "HW:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) " MUL:\t\t%s\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) " MULX:\t\t%s\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) " DIV:\t\t%s\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) " BMX:\t\t%s\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) " CDX:\t\t%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) cpuinfo.has_mul ? "yes" : "no",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) cpuinfo.has_mulx ? "yes" : "no",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) cpuinfo.has_div ? "yes" : "no",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) cpuinfo.has_bmx ? "yes" : "no",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) cpuinfo.has_cdx ? "yes" : "no");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) seq_printf(m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) "Icache:\t\t%ukB, line length: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) cpuinfo.icache_size >> 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) cpuinfo.icache_line_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) seq_printf(m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) "Dcache:\t\t%ukB, line length: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) cpuinfo.dcache_size >> 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) cpuinfo.dcache_line_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) seq_printf(m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) "TLB:\t\t%u ways, %u entries, %u PID bits\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) cpuinfo.tlb_num_ways,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) cpuinfo.tlb_num_entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) cpuinfo.tlb_pid_num_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static void *cpuinfo_start(struct seq_file *m, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) unsigned long i = *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return i < num_possible_cpus() ? (void *) (i + 1) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static void *cpuinfo_next(struct seq_file *m, void *v, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) ++*pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return cpuinfo_start(m, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static void cpuinfo_stop(struct seq_file *m, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) const struct seq_operations cpuinfo_op = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) .start = cpuinfo_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) .next = cpuinfo_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) .stop = cpuinfo_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) .show = show_cpuinfo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) #endif /* CONFIG_PROC_FS */