2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright 2003-2011 Netlogic Microsystems (Netlogic). All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * THIS SOFTWARE IS PROVIDED BY Netlogic Microsystems ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NETLOGIC OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 #include "opt_platform.h"
38 #include <sys/param.h>
41 #include <sys/rtprio.h>
42 #include <sys/systm.h>
43 #include <sys/interrupt.h>
44 #include <sys/limits.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
48 #include <sys/random.h>
50 #include <sys/cons.h> /* cinit() */
53 #include <sys/reboot.h>
54 #include <sys/queue.h>
56 #include <sys/timetc.h>
59 #include <vm/vm_param.h>
60 #include <vm/vm_page.h>
61 #include <vm/vm_phys.h>
63 #include <machine/cpu.h>
64 #include <machine/cpufunc.h>
65 #include <machine/cpuinfo.h>
66 #include <machine/tlb.h>
67 #include <machine/cpuregs.h>
68 #include <machine/frame.h>
69 #include <machine/hwfunc.h>
70 #include <machine/md_var.h>
71 #include <machine/asm.h>
72 #include <machine/pmap.h>
73 #include <machine/trap.h>
74 #include <machine/clock.h>
75 #include <machine/fls64.h>
76 #include <machine/intr_machdep.h>
77 #include <machine/smp.h>
79 #include <mips/nlm/hal/mips-extns.h>
80 #include <mips/nlm/hal/haldefs.h>
81 #include <mips/nlm/hal/iomap.h>
82 #include <mips/nlm/hal/sys.h>
83 #include <mips/nlm/hal/pic.h>
84 #include <mips/nlm/hal/uart.h>
85 #include <mips/nlm/hal/mmu.h>
86 #include <mips/nlm/hal/bridge.h>
87 #include <mips/nlm/hal/cpucontrol.h>
88 #include <mips/nlm/hal/cop2.h>
90 #include <mips/nlm/clock.h>
91 #include <mips/nlm/interrupt.h>
92 #include <mips/nlm/board.h>
93 #include <mips/nlm/xlp.h>
94 #include <mips/nlm/msgring.h>
97 #include <dev/fdt/fdt_common.h>
98 #include <dev/ofw/openfirm.h>
101 /* 4KB static data aread to keep a copy of the bootload env until
102 the dynamic kenv is setup */
103 char boot1_env[4096];
105 uint64_t xlp_cpu_frequency;
106 uint64_t xlp_io_base = MIPS_PHYS_TO_DIRECT_UNCACHED(XLP_DEFAULT_IO_BASE);
109 int xlp_threads_per_core;
110 uint32_t xlp_hw_thread_mask;
111 int xlp_cpuid_to_hwtid[MAXCPU];
112 int xlp_hwtid_to_cpuid[MAXCPU];
113 uint64_t xlp_pic_base;
115 static int xlp_mmuval;
117 extern uint32_t _end;
118 extern char XLPResetEntry[], XLPResetEntryEnd[];
125 reg = nlm_mfcr(LSU_DEFEATURE);
126 /* Enable Unaligned and L2HPE */
127 reg |= (1 << 30) | (1 << 23);
129 * Experimental : Enable SUE
130 * Speculative Unmap Enable. Enable speculative L2 cache request for
134 /* Clear S1RCM - A0 errata */
136 nlm_mtcr(LSU_DEFEATURE, reg);
138 reg = nlm_mfcr(SCHED_DEFEATURE);
139 /* Experimental: Disable BRU accepting ALU ops - A0 errata */
141 nlm_mtcr(SCHED_DEFEATURE, reg);
149 if (nlm_threadid() == 0) {
150 nlm_setup_extended_pagemask(0);
151 nlm_large_variable_tlb_en(1);
152 nlm_extended_tlb_en(1);
153 nlm_mmu_setup(0, 0, 0);
156 /* Enable no-read, no-exec, large-physical-address */
157 pagegrain = mips_rd_pagegrain();
158 pagegrain |= (1U << 31) | /* RIE */
159 (1 << 30) | /* XIE */
160 (1 << 29); /* ELPA */
161 mips_wr_pagegrain(pagegrain);
165 xlp_enable_blocks(void)
170 for (i = 0; i < XLP_MAX_NODES; i++) {
171 if (!nlm_dev_exists(XLP_IO_SYS_OFFSET(i)))
173 sysbase = nlm_get_sys_regbase(i);
174 nlm_sys_enable_block(sysbase, DFS_DEVICE_RSA);
179 xlp_parse_mmu_options(void)
182 uint32_t cpu_map = xlp_hw_thread_mask;
183 uint32_t core0_thr_mask, core_thr_mask, cpu_rst_mask;
188 cpu_map = 0xffffffff;
189 #else /* Uniprocessor! */
192 else if (cpu_map != 0x1) {
193 printf("WARNING: Starting uniprocessor kernel on cpumask [0x%lx]!\n"
194 "WARNING: Other CPUs will be unused.\n", (u_long)cpu_map);
200 core0_thr_mask = cpu_map & 0xf;
201 switch (core0_thr_mask) {
203 xlp_threads_per_core = 1;
207 xlp_threads_per_core = 2;
211 xlp_threads_per_core = 4;
218 /* Try to find the enabled cores from SYS block */
219 sysbase = nlm_get_sys_regbase(0);
220 cpu_rst_mask = nlm_read_sys_reg(sysbase, SYS_CPU_RESET) & 0xff;
222 /* XLP 416 does not report this correctly, fix */
223 if (nlm_processor_id() == CHIP_PROCESSOR_ID_XLP_416)
226 /* Take out cores which do not exist on chip */
227 for (i = 1; i < XLP_MAX_CORES; i++) {
228 if ((cpu_rst_mask & (1 << i)) == 0)
229 cpu_map &= ~(0xfu << (4 * i));
232 /* Verify other cores' CPU masks */
233 for (i = 1; i < XLP_MAX_CORES; i++) {
234 core_thr_mask = (cpu_map >> (4 * i)) & 0xf;
235 if (core_thr_mask == 0)
237 if (core_thr_mask != core0_thr_mask)
242 xlp_hw_thread_mask = cpu_map;
243 /* setup hardware processor id to cpu id mapping */
244 for (i = 0; i< MAXCPU; i++)
245 xlp_cpuid_to_hwtid[i] =
246 xlp_hwtid_to_cpuid[i] = -1;
247 for (i = 0, k = 0; i < XLP_MAX_CORES; i++) {
248 if (((cpu_map >> (i * 4)) & 0xf) == 0)
250 for (j = 0; j < xlp_threads_per_core; j++) {
251 xlp_cpuid_to_hwtid[k] = i * 4 + j;
252 xlp_hwtid_to_cpuid[i * 4 + j] = k;
260 printf("ERROR : Unsupported CPU mask [use 1,2 or 4 threads per core].\n"
261 "\tcore0 thread mask [%lx], boot cpu mask [%lx].\n",
262 (u_long)core0_thr_mask, (u_long)cpu_map);
263 panic("Invalid CPU mask - halting.\n");
269 xlp_bootargs_init(__register_t arg)
271 char buf[2048]; /* early stack is big enough */
276 dtbp = (void *)(intptr_t)arg;
277 #if defined(FDT_DTB_STATIC)
279 * In case the device tree blob was not passed as argument try
280 * to use the statically embedded one.
283 dtbp = &fdt_static_dtb;
285 if (OF_install(OFW_FDT, 0) == FALSE)
287 if (OF_init((void *)dtbp) != 0)
289 OF_interpret("perform-fixup", 0);
291 chosen = OF_finddevice("/chosen");
292 if (OF_getprop(chosen, "cpumask", &mask, sizeof(mask)) != -1) {
293 xlp_hw_thread_mask = mask;
296 if (OF_getprop(chosen, "bootargs", buf, sizeof(buf)) != -1)
297 boothowto |= boot_parse_cmdline(buf);
301 * arg is a pointer to the environment block, the format of the block is
305 xlp_bootargs_init(__register_t arg)
307 char buf[2048]; /* early stack is big enough */
312 * provide backward compat for passing cpu mask as arg
315 xlp_hw_thread_mask = arg;
319 p = (void *)(intptr_t)arg;
321 strlcpy(buf, p, sizeof(buf));
331 /* CPU mask can be passed thru env */
332 if (getenv_uint("cpumask", &mask) != 0)
333 xlp_hw_thread_mask = mask;
335 /* command line argument */
336 v = kern_getenv("bootargs");
338 strlcpy(buf, v, sizeof(buf));
339 boothowto |= boot_parse_cmdline(buf);
349 init_param2(physmem);
352 cpuinfo.cache_coherent_dma = TRUE;
358 if (boothowto & RB_KDB) {
359 kdb_enter("Boot flags requested debugger", NULL);
365 platform_get_timecount(struct timecounter *tc __unused)
367 uint64_t count = nlm_pic_read_timer(xlp_pic_base, PIC_CLOCK_TIMER);
369 return (unsigned int)~count;
375 struct timecounter pic_timecounter = {
376 platform_get_timecount, /* get_timecount */
378 ~0U, /* counter_mask */
379 XLP_IO_CLK, /* frequency */
381 2000, /* quality (adjusted in code) */
386 xlp_pic_base = nlm_get_pic_regbase(0); /* TOOD: Add other nodes */
387 maxirt = nlm_read_reg(nlm_get_pic_pcibase(nlm_nodeid()),
388 XLP_PCI_DEVINFO_REG0);
389 printf("Initializing PIC...@%jx %d IRTs\n", (uintmax_t)xlp_pic_base,
391 /* Bind all PIC irqs to cpu 0 */
392 for (i = 0; i < maxirt; i++)
393 nlm_pic_write_irt(xlp_pic_base, i, 0, 0, 1, 0,
396 nlm_pic_set_timer(xlp_pic_base, PIC_CLOCK_TIMER, ~0ULL, 0, 0);
397 platform_timecounter = &pic_timecounter;
400 #if defined(__mips_n32) || defined(__mips_n64) /* PHYSADDR_64_BIT */
402 #define XLP_MEM_LIM 0x200000000ULL
404 #define XLP_MEM_LIM 0x10000000000ULL
407 #define XLP_MEM_LIM 0xfffff000UL
409 static vm_paddr_t xlp_mem_excl[] = {
410 0, 0, /* for kernel image region, see xlp_mem_init */
411 0x0c000000, 0x14000000, /* uboot area, cms queue and other stuff */
412 0x1fc00000, 0x1fd00000, /* reset vec */
413 0x1e000000, 0x1e200000, /* poe buffers */
417 mem_exclude_add(vm_paddr_t *avail, vm_paddr_t mstart, vm_paddr_t mend)
422 for (i = 0; i < nitems(xlp_mem_excl); i += 2) {
423 if (mstart > xlp_mem_excl[i + 1])
425 if (mstart < xlp_mem_excl[i]) {
426 avail[pos++] = mstart;
427 if (mend < xlp_mem_excl[i])
430 avail[pos++] = xlp_mem_excl[i];
432 mstart = xlp_mem_excl[i + 1];
437 avail[pos++] = mstart;
446 vm_paddr_t physsz, tmp;
447 uint64_t bridgebase, base, lim, val;
450 /* update kernel image area in exclude regions */
451 tmp = (vm_paddr_t)MIPS_KSEG0_TO_PHYS(&_end);
452 tmp = round_page(tmp) + 0x20000; /* round up */
453 xlp_mem_excl[1] = tmp;
455 printf("Memory (from DRAM BARs):\n");
456 bridgebase = nlm_get_bridge_regbase(0); /* TODO: Add other nodes */
458 for (i = 0, j = 0; i < 8; i++) {
459 val = nlm_read_bridge_reg(bridgebase, BRIDGE_DRAM_BAR(i));
460 val = (val >> 12) & 0xfffff;
462 val = nlm_read_bridge_reg(bridgebase, BRIDGE_DRAM_LIMIT(i));
463 val = (val >> 12) & 0xfffff;
464 if (val == 0) /* BAR not enabled */
466 lim = (val + 1) << 20;
467 printf(" BAR %d: %#jx - %#jx : ", i, (intmax_t)base,
471 printf("\tskipped - malformed %#jx -> %#jx\n",
472 (intmax_t)base, (intmax_t)lim);
474 } else if (base >= XLP_MEM_LIM) {
475 printf(" skipped - outside usable limit %#jx.\n",
476 (intmax_t)XLP_MEM_LIM);
478 } else if (lim >= XLP_MEM_LIM) {
480 printf(" truncated to %#jx.\n", (intmax_t)XLP_MEM_LIM);
484 /* exclude unusable regions from BAR and add rest */
485 n = mem_exclude_add(&phys_avail[j], base, lim);
486 for (k = j; k < j + n; k += 2) {
487 physsz += phys_avail[k + 1] - phys_avail[k];
488 printf("\tMem[%d]: %#jx - %#jx\n", k/2,
489 (intmax_t)phys_avail[k], (intmax_t)phys_avail[k+1]);
494 /* setup final entry with 0 */
495 phys_avail[j] = phys_avail[j + 1] = 0;
497 /* copy phys_avail to dump_avail */
498 for (i = 0; i <= j + 1; i++)
499 dump_avail[i] = phys_avail[i];
501 realmem = physmem = btoc(physsz);
505 platform_start(__register_t a0 __unused,
506 __register_t a1 __unused,
507 __register_t a2 __unused,
508 __register_t a3 __unused)
511 /* Initialize pcpu stuff */
514 /* initialize console so that we have printf */
515 boothowto |= (RB_SERIAL | RB_MULTIPLE); /* Use multiple consoles */
517 init_static_kenv(boot1_env, sizeof(boot1_env));
518 xlp_bootargs_init(a0);
520 /* clockrate used by delay, so initialize it here */
521 xlp_cpu_frequency = xlp_get_cpu_frequency(0, 0);
522 cpu_clock = xlp_cpu_frequency / 1000000;
523 mips_timer_early_init(xlp_cpu_frequency);
525 /* Init console please */
528 /* Early core init and fixes for errata */
531 xlp_parse_mmu_options();
534 bcopy(XLPResetEntry, (void *)MIPS_RESET_EXC_VEC,
535 XLPResetEntryEnd - XLPResetEntry);
538 * We will enable the other threads in core 0 here
539 * so that the TLB and cache info is correct when
542 xlp_enable_threads(xlp_mmuval);
544 /* setup for the startup core */
549 /* Read/Guess/setup board information */
550 nlm_board_info_setup();
552 /* MIPS generic init */
556 * XLP specific post initialization
557 * initialize other on chip stuff
561 mips_timer_init_params(xlp_cpu_frequency, 0);
572 uint64_t sysbase = nlm_get_sys_regbase(0);
574 nlm_write_sys_reg(sysbase, SYS_CHIP_RESET, 1);
576 __asm __volatile("wait");
581 * XLP threads are started simultaneously when we enable threads, this will
582 * ensure that the threads are blocked in platform_init_ap, until they are
583 * ready to proceed to smp_init_secondary()
585 static volatile int thr_unblock[4];
588 platform_start_ap(int cpuid)
590 uint32_t coremask, val;
591 uint64_t sysbase = nlm_get_sys_regbase(0);
592 int hwtid = xlp_cpuid_to_hwtid[cpuid];
598 /* First thread in core, do core wake up */
599 coremask = 1u << core;
601 /* Enable core clock */
602 val = nlm_read_sys_reg(sysbase, SYS_CORE_DFS_DIS_CTRL);
604 nlm_write_sys_reg(sysbase, SYS_CORE_DFS_DIS_CTRL, val);
606 /* Remove CPU Reset */
607 val = nlm_read_sys_reg(sysbase, SYS_CPU_RESET);
608 val &= ~coremask & 0xff;
609 nlm_write_sys_reg(sysbase, SYS_CPU_RESET, val);
612 printf("Waking up core %d ...", core);
614 /* Poll for CPU to mark itself coherent */
616 val = nlm_read_sys_reg(sysbase, SYS_CPU_NONCOHERENT_MODE);
617 } while ((val & coremask) != 0);
621 /* otherwise release the threads stuck in platform_init_ap */
622 thr_unblock[thr] = 1;
629 platform_init_ap(int cpuid)
634 /* The first thread has to setup the MMU and enable other threads */
635 thr = nlm_threadid();
638 xlp_enable_threads(xlp_mmuval);
641 * FIXME busy wait here eats too many cycles, especially
642 * in the core 0 while bootup
644 while (thr_unblock[thr] == 0)
645 __asm__ __volatile__ ("nop;nop;nop;nop");
646 thr_unblock[thr] = 0;
650 stat = mips_rd_status();
651 KASSERT((stat & MIPS_SR_INT_IE) == 0,
652 ("Interrupts enabled in %s!", __func__));
653 stat |= MIPS_SR_COP_2_BIT | MIPS_SR_COP_0_BIT;
654 mips_wr_status(stat);
656 nlm_write_c0_eimr(0ull);
657 xlp_enable_irq(IRQ_IPI);
658 xlp_enable_irq(IRQ_TIMER);
659 xlp_enable_irq(IRQ_MSGRING);
665 platform_ipi_hardintr_num(void)
672 platform_ipi_softintr_num(void)
679 platform_ipi_send(int cpuid)
682 nlm_pic_send_ipi(xlp_pic_base, xlp_cpuid_to_hwtid[cpuid],
683 platform_ipi_hardintr_num(), 0);
687 platform_ipi_clear(void)
692 platform_processor_id(void)
695 return (xlp_hwtid_to_cpuid[nlm_cpuid()]);
699 platform_cpu_mask(cpuset_t *mask)
704 s = xlp_ncores * xlp_threads_per_core;
705 for (i = 0; i < s; i++)
713 return (smp_topo_2level(CG_SHARE_L2, xlp_ncores, CG_SHARE_L1,
714 xlp_threads_per_core, CG_FLAG_THREAD));