1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
6 #include <linux/seq_file.h>
8 #include <linux/delay.h>
9 #include <linux/root_dev.h>
10 #include <linux/clk.h>
11 #include <linux/clk-provider.h>
12 #include <linux/clocksource.h>
13 #include <linux/console.h>
14 #include <linux/module.h>
15 #include <linux/sizes.h>
16 #include <linux/cpu.h>
17 #include <linux/of_fdt.h>
19 #include <linux/cache.h>
20 #include <uapi/linux/mount.h>
21 #include <asm/sections.h>
22 #include <asm/arcregs.h>
24 #include <asm/setup.h>
27 #include <asm/unwind.h>
28 #include <asm/mach_desc.h>
31 #define FIX_PTR(x) __asm__ __volatile__(";" : "+r"(x))
33 unsigned int intr_to_DE_cnt;
35 /* Part of U-boot ABI: see head.S */
36 int __initdata uboot_tag;
37 int __initdata uboot_magic;
38 char __initdata *uboot_arg;
40 const struct machine_desc *machine_desc;
42 struct task_struct *_current_task[NR_CPUS]; /* For stack switching */
44 struct cpuinfo_arc cpuinfo_arc700[NR_CPUS];
46 static const struct id_to_str arc_legacy_rel[] = {
47 /* ID.ARCVER, Release */
48 #ifdef CONFIG_ISA_ARCOMPACT
59 static const struct id_to_str arc_cpu_rel[] = {
60 /* UARCH.MAJOR, Release */
66 static void read_decode_ccm_bcr(struct cpuinfo_arc *cpu)
68 if (is_isa_arcompact()) {
69 struct bcr_iccm_arcompact iccm;
70 struct bcr_dccm_arcompact dccm;
72 READ_BCR(ARC_REG_ICCM_BUILD, iccm);
74 cpu->iccm.sz = 4096 << iccm.sz; /* 8K to 512K */
75 cpu->iccm.base_addr = iccm.base << 16;
78 READ_BCR(ARC_REG_DCCM_BUILD, dccm);
81 cpu->dccm.sz = 2048 << dccm.sz; /* 2K to 256K */
83 base = read_aux_reg(ARC_REG_DCCM_BASE_BUILD);
84 cpu->dccm.base_addr = base & ~0xF;
87 struct bcr_iccm_arcv2 iccm;
88 struct bcr_dccm_arcv2 dccm;
91 READ_BCR(ARC_REG_ICCM_BUILD, iccm);
93 cpu->iccm.sz = 256 << iccm.sz00; /* 512B to 16M */
94 if (iccm.sz00 == 0xF && iccm.sz01 > 0)
95 cpu->iccm.sz <<= iccm.sz01;
97 region = read_aux_reg(ARC_REG_AUX_ICCM);
98 cpu->iccm.base_addr = region & 0xF0000000;
101 READ_BCR(ARC_REG_DCCM_BUILD, dccm);
103 cpu->dccm.sz = 256 << dccm.sz0;
104 if (dccm.sz0 == 0xF && dccm.sz1 > 0)
105 cpu->dccm.sz <<= dccm.sz1;
107 region = read_aux_reg(ARC_REG_AUX_DCCM);
108 cpu->dccm.base_addr = region & 0xF0000000;
113 static void decode_arc_core(struct cpuinfo_arc *cpu)
115 struct bcr_uarch_build_arcv2 uarch;
116 const struct id_to_str *tbl;
119 * Up until (including) the first core4 release (0x54) things were
120 * simple: AUX IDENTITY.ARCVER was sufficient to identify arc family
121 * and release: 0x50 to 0x53 was HS38, 0x54 was HS48 (dual issue)
124 if (cpu->core.family < 0x54) { /* includes arc700 */
126 for (tbl = &arc_legacy_rel[0]; tbl->id != 0; tbl++) {
127 if (cpu->core.family == tbl->id) {
128 cpu->release = tbl->str;
133 if (is_isa_arcompact())
134 cpu->name = "ARC700";
138 cpu->name = cpu->release = "Unknown";
144 * However the subsequent HS release (same 0x54) allow HS38 or HS48
145 * configurations and encode this info in a different BCR.
146 * The BCR was introduced in 0x54 so can't be read unconditionally.
149 READ_BCR(ARC_REG_MICRO_ARCH_BCR, uarch);
151 if (uarch.prod == 4) {
159 for (tbl = &arc_cpu_rel[0]; tbl->id != 0xFF; tbl++) {
160 if (uarch.maj == tbl->id) {
161 cpu->release = tbl->str;
167 static void read_arc_build_cfg_regs(void)
169 struct bcr_timer timer;
170 struct bcr_generic bcr;
171 struct cpuinfo_arc *cpu = &cpuinfo_arc700[smp_processor_id()];
172 struct bcr_isa_arcv2 isa;
173 struct bcr_actionpoint ap;
177 READ_BCR(AUX_IDENTITY, cpu->core);
178 decode_arc_core(cpu);
180 READ_BCR(ARC_REG_TIMERS_BCR, timer);
181 cpu->extn.timer0 = timer.t0;
182 cpu->extn.timer1 = timer.t1;
183 cpu->extn.rtc = timer.rtc;
185 cpu->vec_base = read_aux_reg(AUX_INTR_VEC_BASE);
187 READ_BCR(ARC_REG_MUL_BCR, cpu->extn_mpy);
189 /* Read CCM BCRs for boot reporting even if not enabled in Kconfig */
190 read_decode_ccm_bcr(cpu);
192 read_decode_mmu_bcr();
193 read_decode_cache_bcr();
195 if (is_isa_arcompact()) {
196 struct bcr_fp_arcompact sp, dp;
197 struct bcr_bpu_arcompact bpu;
199 READ_BCR(ARC_REG_FP_BCR, sp);
200 READ_BCR(ARC_REG_DPFP_BCR, dp);
201 cpu->extn.fpu_sp = sp.ver ? 1 : 0;
202 cpu->extn.fpu_dp = dp.ver ? 1 : 0;
204 READ_BCR(ARC_REG_BPU_BCR, bpu);
205 cpu->bpu.ver = bpu.ver;
206 cpu->bpu.full = bpu.fam ? 1 : 0;
208 cpu->bpu.num_cache = 256 << (bpu.ent - 1);
209 cpu->bpu.num_pred = 256 << (bpu.ent - 1);
212 struct bcr_fp_arcv2 spdp;
213 struct bcr_bpu_arcv2 bpu;
215 READ_BCR(ARC_REG_FP_V2_BCR, spdp);
216 cpu->extn.fpu_sp = spdp.sp ? 1 : 0;
217 cpu->extn.fpu_dp = spdp.dp ? 1 : 0;
219 READ_BCR(ARC_REG_BPU_BCR, bpu);
220 cpu->bpu.ver = bpu.ver;
221 cpu->bpu.full = bpu.ft;
222 cpu->bpu.num_cache = 256 << bpu.bce;
223 cpu->bpu.num_pred = 2048 << bpu.pte;
224 cpu->bpu.ret_stk = 4 << bpu.rse;
226 /* if dual issue hardware, is it enabled ? */
227 if (cpu->extn.dual) {
228 unsigned int exec_ctrl;
230 READ_BCR(AUX_EXEC_CTRL, exec_ctrl);
231 cpu->extn.dual_enb = !(exec_ctrl & 1);
235 READ_BCR(ARC_REG_AP_BCR, ap);
237 cpu->extn.ap_num = 2 << ap.num;
238 cpu->extn.ap_full = !ap.min;
241 READ_BCR(ARC_REG_SMART_BCR, bcr);
242 cpu->extn.smart = bcr.ver ? 1 : 0;
244 READ_BCR(ARC_REG_RTT_BCR, bcr);
245 cpu->extn.rtt = bcr.ver ? 1 : 0;
247 READ_BCR(ARC_REG_ISA_CFG_BCR, isa);
249 /* some hacks for lack of feature BCR info in old ARC700 cores */
250 if (is_isa_arcompact()) {
251 if (!isa.ver) /* ISA BCR absent, use Kconfig info */
252 cpu->isa.atomic = IS_ENABLED(CONFIG_ARC_HAS_LLSC);
254 /* ARC700_BUILD only has 2 bits of isa info */
255 struct bcr_generic bcr = *(struct bcr_generic *)&isa;
256 cpu->isa.atomic = bcr.info & 1;
259 cpu->isa.be = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN);
261 /* there's no direct way to distinguish 750 vs. 770 */
262 if (unlikely(cpu->core.family < 0x34 || cpu->mmu.ver < 3))
263 cpu->name = "ARC750";
269 static char *arc_cpu_mumbojumbo(int cpu_id, char *buf, int len)
271 struct cpuinfo_arc *cpu = &cpuinfo_arc700[cpu_id];
272 struct bcr_identity *core = &cpu->core;
278 n += scnprintf(buf + n, len - n,
279 "\nIDENTITY\t: ARCVER [%#02x] ARCNUM [%#02x] CHIPID [%#4x]\n",
280 core->family, core->cpu_id, core->chip_id);
282 n += scnprintf(buf + n, len - n, "processor [%d]\t: %s %s (%s ISA) %s%s%s\n",
283 cpu_id, cpu->name, cpu->release,
284 is_isa_arcompact() ? "ARCompact" : "ARCv2",
285 IS_AVAIL1(cpu->isa.be, "[Big-Endian]"),
286 IS_AVAIL3(cpu->extn.dual, cpu->extn.dual_enb, " Dual-Issue "));
288 n += scnprintf(buf + n, len - n, "Timers\t\t: %s%s%s%s%s%s\nISA Extn\t: ",
289 IS_AVAIL1(cpu->extn.timer0, "Timer0 "),
290 IS_AVAIL1(cpu->extn.timer1, "Timer1 "),
291 IS_AVAIL2(cpu->extn.rtc, "RTC [UP 64-bit] ", CONFIG_ARC_TIMERS_64BIT),
292 IS_AVAIL2(cpu->extn.gfrc, "GFRC [SMP 64-bit] ", CONFIG_ARC_TIMERS_64BIT));
294 if (cpu->extn_mpy.ver) {
295 if (is_isa_arcompact()) {
296 scnprintf(mpy_opt, 16, "mpy");
299 int opt = 2; /* stock MPY/MPYH */
301 if (cpu->extn_mpy.dsp) /* OPT 7-9 */
302 opt = cpu->extn_mpy.dsp + 6;
304 scnprintf(mpy_opt, 16, "mpy[opt %d] ", opt);
308 n += scnprintf(buf + n, len - n, "%s%s%s%s%s%s%s%s\n",
309 IS_AVAIL2(cpu->isa.atomic, "atomic ", CONFIG_ARC_HAS_LLSC),
310 IS_AVAIL2(cpu->isa.ldd, "ll64 ", CONFIG_ARC_HAS_LL64),
311 IS_AVAIL2(cpu->isa.unalign, "unalign ", CONFIG_ARC_USE_UNALIGNED_MEM_ACCESS),
312 IS_AVAIL1(cpu->extn_mpy.ver, mpy_opt),
313 IS_AVAIL1(cpu->isa.div_rem, "div_rem "));
316 n += scnprintf(buf + n, len - n,
317 "BPU\t\t: %s%s match, cache:%d, Predict Table:%d Return stk: %d",
318 IS_AVAIL1(cpu->bpu.full, "full"),
319 IS_AVAIL1(!cpu->bpu.full, "partial"),
320 cpu->bpu.num_cache, cpu->bpu.num_pred, cpu->bpu.ret_stk);
322 if (is_isa_arcv2()) {
325 READ_BCR(ARC_REG_LPB_BUILD, lpb);
328 ctl = read_aux_reg(ARC_REG_LPB_CTRL);
330 n += scnprintf(buf + n, len - n, " Loop Buffer:%d %s",
332 IS_DISABLED_RUN(!ctl));
335 n += scnprintf(buf + n, len - n, "\n");
341 static char *arc_extn_mumbojumbo(int cpu_id, char *buf, int len)
344 struct cpuinfo_arc *cpu = &cpuinfo_arc700[cpu_id];
348 n += scnprintf(buf + n, len - n, "Vector Table\t: %#x\n", cpu->vec_base);
350 if (cpu->extn.fpu_sp || cpu->extn.fpu_dp)
351 n += scnprintf(buf + n, len - n, "FPU\t\t: %s%s\n",
352 IS_AVAIL1(cpu->extn.fpu_sp, "SP "),
353 IS_AVAIL1(cpu->extn.fpu_dp, "DP "));
355 if (cpu->extn.ap_num | cpu->extn.smart | cpu->extn.rtt) {
356 n += scnprintf(buf + n, len - n, "DEBUG\t\t: %s%s",
357 IS_AVAIL1(cpu->extn.smart, "smaRT "),
358 IS_AVAIL1(cpu->extn.rtt, "RTT "));
359 if (cpu->extn.ap_num) {
360 n += scnprintf(buf + n, len - n, "ActionPoint %d/%s",
362 cpu->extn.ap_full ? "full":"min");
364 n += scnprintf(buf + n, len - n, "\n");
367 if (cpu->dccm.sz || cpu->iccm.sz)
368 n += scnprintf(buf + n, len - n, "Extn [CCM]\t: DCCM @ %x, %d KB / ICCM: @ %x, %d KB\n",
369 cpu->dccm.base_addr, TO_KB(cpu->dccm.sz),
370 cpu->iccm.base_addr, TO_KB(cpu->iccm.sz));
372 if (is_isa_arcv2()) {
374 /* Error Protection: ECC/Parity */
376 READ_BCR(ARC_REG_ERP_BUILD, erp);
380 READ_BCR(ARC_REG_ERP_CTRL, ctl);
382 /* inverted bits: 0 means enabled */
383 n += scnprintf(buf + n, len - n, "Extn [ECC]\t: %s%s%s%s%s%s\n",
384 IS_AVAIL3(erp.ic, !ctl.dpi, "IC "),
385 IS_AVAIL3(erp.dc, !ctl.dpd, "DC "),
386 IS_AVAIL3(erp.mmu, !ctl.mpd, "MMU "));
393 static void arc_chk_core_config(void)
395 struct cpuinfo_arc *cpu = &cpuinfo_arc700[smp_processor_id()];
396 int saved = 0, present = 0;
399 if (!cpu->extn.timer0)
400 panic("Timer0 is not present!\n");
402 if (!cpu->extn.timer1)
403 panic("Timer1 is not present!\n");
405 #ifdef CONFIG_ARC_HAS_DCCM
407 * DCCM can be arbit placed in hardware.
408 * Make sure it's placement/sz matches what Linux is built with
410 if ((unsigned int)__arc_dccm_base != cpu->dccm.base_addr)
411 panic("Linux built with incorrect DCCM Base address\n");
413 if (CONFIG_ARC_DCCM_SZ * SZ_1K != cpu->dccm.sz)
414 panic("Linux built with incorrect DCCM Size\n");
417 #ifdef CONFIG_ARC_HAS_ICCM
418 if (CONFIG_ARC_ICCM_SZ * SZ_1K != cpu->iccm.sz)
419 panic("Linux built with incorrect ICCM Size\n");
423 * FP hardware/software config sanity
424 * -If hardware present, kernel needs to save/restore FPU state
425 * -If not, it will crash trying to save/restore the non-existant regs
428 if (is_isa_arcompact()) {
429 opt_nm = "CONFIG_ARC_FPU_SAVE_RESTORE";
430 saved = IS_ENABLED(CONFIG_ARC_FPU_SAVE_RESTORE);
432 /* only DPDP checked since SP has no arch visible regs */
433 present = cpu->extn.fpu_dp;
435 opt_nm = "CONFIG_ARC_HAS_ACCL_REGS";
436 saved = IS_ENABLED(CONFIG_ARC_HAS_ACCL_REGS);
438 /* Accumulator Low:High pair (r58:59) present if DSP MPY or FPU */
439 present = cpu->extn_mpy.dsp | cpu->extn.fpu_sp | cpu->extn.fpu_dp;
442 if (present && !saved)
443 pr_warn("Enable %s for working apps\n", opt_nm);
444 else if (!present && saved)
445 panic("Disable %s, hardware NOT present\n", opt_nm);
449 * Initialize and setup the processor core
450 * This is called by all the CPUs thus should not do special case stuff
451 * such as only for boot CPU etc
454 void setup_processor(void)
457 int cpu_id = smp_processor_id();
459 read_arc_build_cfg_regs();
462 pr_info("%s", arc_cpu_mumbojumbo(cpu_id, str, sizeof(str)));
467 pr_info("%s", arc_extn_mumbojumbo(cpu_id, str, sizeof(str)));
468 pr_info("%s", arc_platform_smp_cpuinfo());
470 arc_chk_core_config();
473 static inline bool uboot_arg_invalid(unsigned long addr)
476 * Check that it is a untranslated address (although MMU is not enabled
477 * yet, it being a high address ensures this is not by fluke)
479 if (addr < PAGE_OFFSET)
482 /* Check that address doesn't clobber resident kernel image */
483 return addr >= (unsigned long)_stext && addr <= (unsigned long)_end;
486 #define IGNORE_ARGS "Ignore U-boot args: "
488 /* uboot_tag values for U-boot - kernel ABI revision 0; see head.S */
489 #define UBOOT_TAG_NONE 0
490 #define UBOOT_TAG_CMDLINE 1
491 #define UBOOT_TAG_DTB 2
492 /* We always pass 0 as magic from U-boot */
493 #define UBOOT_MAGIC_VALUE 0
495 void __init handle_uboot_args(void)
497 bool use_embedded_dtb = true;
498 bool append_cmdline = false;
500 /* check that we know this tag */
501 if (uboot_tag != UBOOT_TAG_NONE &&
502 uboot_tag != UBOOT_TAG_CMDLINE &&
503 uboot_tag != UBOOT_TAG_DTB) {
504 pr_warn(IGNORE_ARGS "invalid uboot tag: '%08x'\n", uboot_tag);
505 goto ignore_uboot_args;
508 if (uboot_magic != UBOOT_MAGIC_VALUE) {
509 pr_warn(IGNORE_ARGS "non zero uboot magic\n");
510 goto ignore_uboot_args;
513 if (uboot_tag != UBOOT_TAG_NONE &&
514 uboot_arg_invalid((unsigned long)uboot_arg)) {
515 pr_warn(IGNORE_ARGS "invalid uboot arg: '%px'\n", uboot_arg);
516 goto ignore_uboot_args;
519 /* see if U-boot passed an external Device Tree blob */
520 if (uboot_tag == UBOOT_TAG_DTB) {
521 machine_desc = setup_machine_fdt((void *)uboot_arg);
523 /* external Device Tree blob is invalid - use embedded one */
524 use_embedded_dtb = !machine_desc;
527 if (uboot_tag == UBOOT_TAG_CMDLINE)
528 append_cmdline = true;
532 if (use_embedded_dtb) {
533 machine_desc = setup_machine_fdt(__dtb_start);
535 panic("Embedded DT invalid\n");
539 * NOTE: @boot_command_line is populated by setup_machine_fdt() so this
540 * append processing can only happen after.
542 if (append_cmdline) {
543 /* Ensure a whitespace between the 2 cmdlines */
544 strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
545 strlcat(boot_command_line, uboot_arg, COMMAND_LINE_SIZE);
549 void __init setup_arch(char **cmdline_p)
553 /* Save unparsed command line copy for /proc/cmdline */
554 *cmdline_p = boot_command_line;
556 /* To force early parsing of things like mem=xxx */
559 /* Platform/board specific: e.g. early console registration */
560 if (machine_desc->init_early)
561 machine_desc->init_early();
568 /* copy flat DT out of .init and then unflatten it */
569 unflatten_and_copy_device_tree();
571 /* Can be issue if someone passes cmd line arg "ro"
572 * But that is unlikely so keeping it as it is
574 root_mountflags &= ~MS_RDONLY;
576 #if defined(CONFIG_VT) && defined(CONFIG_DUMMY_CONSOLE)
577 conswitchp = &dummy_con;
584 * Called from start_kernel() - boot CPU only
586 void __init time_init(void)
592 static int __init customize_machine(void)
594 if (machine_desc->init_machine)
595 machine_desc->init_machine();
599 arch_initcall(customize_machine);
601 static int __init init_late_machine(void)
603 if (machine_desc->init_late)
604 machine_desc->init_late();
608 late_initcall(init_late_machine);
610 * Get CPU information for use by the procfs.
613 #define cpu_to_ptr(c) ((void *)(0xFFFF0000 | (unsigned int)(c)))
614 #define ptr_to_cpu(p) (~0xFFFF0000UL & (unsigned int)(p))
616 static int show_cpuinfo(struct seq_file *m, void *v)
619 int cpu_id = ptr_to_cpu(v);
620 struct device *cpu_dev = get_cpu_device(cpu_id);
622 unsigned long freq = 0;
624 if (!cpu_online(cpu_id)) {
625 seq_printf(m, "processor [%d]\t: Offline\n", cpu_id);
629 str = (char *)__get_free_page(GFP_KERNEL);
633 seq_printf(m, arc_cpu_mumbojumbo(cpu_id, str, PAGE_SIZE));
635 cpu_clk = clk_get(cpu_dev, NULL);
636 if (IS_ERR(cpu_clk)) {
637 seq_printf(m, "CPU speed \t: Cannot get clock for processor [%d]\n",
640 freq = clk_get_rate(cpu_clk);
643 seq_printf(m, "CPU speed\t: %lu.%02lu Mhz\n",
644 freq / 1000000, (freq / 10000) % 100);
646 seq_printf(m, "Bogo MIPS\t: %lu.%02lu\n",
647 loops_per_jiffy / (500000 / HZ),
648 (loops_per_jiffy / (5000 / HZ)) % 100);
650 seq_printf(m, arc_mmu_mumbojumbo(cpu_id, str, PAGE_SIZE));
651 seq_printf(m, arc_cache_mumbojumbo(cpu_id, str, PAGE_SIZE));
652 seq_printf(m, arc_extn_mumbojumbo(cpu_id, str, PAGE_SIZE));
653 seq_printf(m, arc_platform_smp_cpuinfo());
655 free_page((unsigned long)str);
662 static void *c_start(struct seq_file *m, loff_t *pos)
665 * Callback returns cpu-id to iterator for show routine, NULL to stop.
666 * However since NULL is also a valid cpu-id (0), we use a round-about
667 * way to pass it w/o having to kmalloc/free a 2 byte string.
668 * Encode cpu-id as 0xFFcccc, which is decoded by show routine.
670 return *pos < nr_cpu_ids ? cpu_to_ptr(*pos) : NULL;
673 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
676 return c_start(m, pos);
679 static void c_stop(struct seq_file *m, void *v)
683 const struct seq_operations cpuinfo_op = {
690 static DEFINE_PER_CPU(struct cpu, cpu_topology);
692 static int __init topology_init(void)
696 for_each_present_cpu(cpu)
697 register_cpu(&per_cpu(cpu_topology, cpu), cpu);
702 subsys_initcall(topology_init);