2 * Copyright 2003-2011 Netlogic Microsystems (Netlogic). All rights
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * THIS SOFTWARE IS PROVIDED BY Netlogic Microsystems ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NETLOGIC OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 #include "opt_platform.h"
36 #include <sys/param.h>
39 #include <sys/rtprio.h>
40 #include <sys/systm.h>
41 #include <sys/interrupt.h>
42 #include <sys/limits.h>
44 #include <sys/malloc.h>
45 #include <sys/mutex.h>
46 #include <sys/random.h>
48 #include <sys/cons.h> /* cinit() */
50 #include <sys/reboot.h>
51 #include <sys/queue.h>
53 #include <sys/timetc.h>
56 #include <vm/vm_page.h>
58 #include <machine/cpu.h>
59 #include <machine/cpufunc.h>
60 #include <machine/cpuinfo.h>
61 #include <machine/tlb.h>
62 #include <machine/cpuregs.h>
63 #include <machine/frame.h>
64 #include <machine/hwfunc.h>
65 #include <machine/md_var.h>
66 #include <machine/asm.h>
67 #include <machine/pmap.h>
68 #include <machine/trap.h>
69 #include <machine/clock.h>
70 #include <machine/fls64.h>
71 #include <machine/intr_machdep.h>
72 #include <machine/smp.h>
74 #include <mips/nlm/hal/mips-extns.h>
75 #include <mips/nlm/hal/haldefs.h>
76 #include <mips/nlm/hal/iomap.h>
77 #include <mips/nlm/hal/sys.h>
78 #include <mips/nlm/hal/pic.h>
79 #include <mips/nlm/hal/uart.h>
80 #include <mips/nlm/hal/mmu.h>
81 #include <mips/nlm/hal/bridge.h>
82 #include <mips/nlm/hal/cpucontrol.h>
83 #include <mips/nlm/hal/cop2.h>
85 #include <mips/nlm/clock.h>
86 #include <mips/nlm/interrupt.h>
87 #include <mips/nlm/board.h>
88 #include <mips/nlm/xlp.h>
89 #include <mips/nlm/msgring.h>
92 #include <dev/fdt/fdt_common.h>
93 #include <dev/ofw/openfirm.h>
96 /* 4KB static data aread to keep a copy of the bootload env until
97 the dynamic kenv is setup */
100 uint64_t xlp_cpu_frequency;
101 uint64_t xlp_io_base = MIPS_PHYS_TO_DIRECT_UNCACHED(XLP_DEFAULT_IO_BASE);
104 int xlp_threads_per_core;
105 uint32_t xlp_hw_thread_mask;
106 int xlp_cpuid_to_hwtid[MAXCPU];
107 int xlp_hwtid_to_cpuid[MAXCPU];
108 uint64_t xlp_pic_base;
110 static int xlp_mmuval;
112 extern uint32_t _end;
113 extern char XLPResetEntry[], XLPResetEntryEnd[];
120 reg = nlm_mfcr(LSU_DEFEATURE);
121 /* Enable Unaligned and L2HPE */
122 reg |= (1 << 30) | (1 << 23);
124 * Experimental : Enable SUE
125 * Speculative Unmap Enable. Enable speculative L2 cache request for
129 /* Clear S1RCM - A0 errata */
131 nlm_mtcr(LSU_DEFEATURE, reg);
133 reg = nlm_mfcr(SCHED_DEFEATURE);
134 /* Experimental: Disable BRU accepting ALU ops - A0 errata */
136 nlm_mtcr(SCHED_DEFEATURE, reg);
144 if (nlm_threadid() == 0) {
145 nlm_setup_extended_pagemask(0);
146 nlm_large_variable_tlb_en(1);
147 nlm_extended_tlb_en(1);
148 nlm_mmu_setup(0, 0, 0);
151 /* Enable no-read, no-exec, large-physical-address */
152 pagegrain = mips_rd_pagegrain();
153 pagegrain |= (1 << 31) | /* RIE */
154 (1 << 30) | /* XIE */
155 (1 << 29); /* ELPA */
156 mips_wr_pagegrain(pagegrain);
160 xlp_parse_mmu_options(void)
163 uint32_t cpu_map = xlp_hw_thread_mask;
164 uint32_t core0_thr_mask, core_thr_mask, cpu_rst_mask;
169 cpu_map = 0xffffffff;
170 #else /* Uniprocessor! */
173 else if (cpu_map != 0x1) {
174 printf("WARNING: Starting uniprocessor kernel on cpumask [0x%lx]!\n"
175 "WARNING: Other CPUs will be unused.\n", (u_long)cpu_map);
181 core0_thr_mask = cpu_map & 0xf;
182 switch (core0_thr_mask) {
184 xlp_threads_per_core = 1;
188 xlp_threads_per_core = 2;
192 xlp_threads_per_core = 4;
199 /* Try to find the enabled cores from SYS block */
200 sysbase = nlm_get_sys_regbase(0);
201 cpu_rst_mask = nlm_read_sys_reg(sysbase, SYS_CPU_RESET) & 0xff;
203 /* XLP 416 does not report this correctly, fix */
204 if (nlm_processor_id() == CHIP_PROCESSOR_ID_XLP_416)
207 /* Take out cores which do not exist on chip */
208 for (i = 1; i < XLP_MAX_CORES; i++) {
209 if ((cpu_rst_mask & (1 << i)) == 0)
210 cpu_map &= ~(0xfu << (4 * i));
213 /* Verify other cores' CPU masks */
214 for (i = 1; i < XLP_MAX_CORES; i++) {
215 core_thr_mask = (cpu_map >> (4 * i)) & 0xf;
216 if (core_thr_mask == 0)
218 if (core_thr_mask != core0_thr_mask)
223 xlp_hw_thread_mask = cpu_map;
224 /* setup hardware processor id to cpu id mapping */
225 for (i = 0; i< MAXCPU; i++)
226 xlp_cpuid_to_hwtid[i] =
227 xlp_hwtid_to_cpuid[i] = -1;
228 for (i = 0, k = 0; i < XLP_MAX_CORES; i++) {
229 if (((cpu_map >> (i * 4)) & 0xf) == 0)
231 for (j = 0; j < xlp_threads_per_core; j++) {
232 xlp_cpuid_to_hwtid[k] = i * 4 + j;
233 xlp_hwtid_to_cpuid[i * 4 + j] = k;
241 printf("ERROR : Unsupported CPU mask [use 1,2 or 4 threads per core].\n"
242 "\tcore0 thread mask [%lx], boot cpu mask [%lx].\n",
243 (u_long)core0_thr_mask, (u_long)cpu_map);
244 panic("Invalid CPU mask - halting.\n");
248 /* Parse cmd line args as env - copied from ar71xx */
250 xlp_parse_bootargs(char *cmdline)
254 while ((v = strsep(&cmdline, " \n")) != NULL) {
261 case 'a': boothowto |= RB_ASKNAME; break;
262 case 'd': boothowto |= RB_KDB; break;
263 case 'g': boothowto |= RB_GDB; break;
264 case 's': boothowto |= RB_SINGLE; break;
265 case 'v': boothowto |= RB_VERBOSE; break;
280 xlp_bootargs_init(__register_t arg)
282 char buf[2048]; /* early stack is big enough */
287 dtbp = (void *)(intptr_t)arg;
288 #if defined(FDT_DTB_STATIC)
290 * In case the device tree blob was not passed as argument try
291 * to use the statically embedded one.
294 dtbp = &fdt_static_dtb;
296 if (OF_install(OFW_FDT, 0) == FALSE)
298 if (OF_init((void *)dtbp) != 0)
300 if (fdt_immr_addr(xlp_io_base) != 0)
302 OF_interpret("perform-fixup", 0);
304 chosen = OF_finddevice("/chosen");
305 if (OF_getprop(chosen, "cpumask", &mask, sizeof(mask)) != -1) {
306 xlp_hw_thread_mask = mask;
309 if (OF_getprop(chosen, "bootargs", buf, sizeof(buf)) != -1)
310 xlp_parse_bootargs(buf);
314 * arg is a pointer to the environment block, the format of the block is
318 xlp_bootargs_init(__register_t arg)
320 char buf[2048]; /* early stack is big enough */
325 * provide backward compat for passing cpu mask as arg
328 xlp_hw_thread_mask = arg;
332 p = (void *)(intptr_t)arg;
334 strlcpy(buf, p, sizeof(buf));
344 /* CPU mask can be passed thru env */
345 if (getenv_uint("cpumask", &mask) != 0)
346 xlp_hw_thread_mask = mask;
348 /* command line argument */
349 v = getenv("bootargs");
351 strlcpy(buf, v, sizeof(buf));
352 xlp_parse_bootargs(buf);
362 init_param2(physmem);
365 cpuinfo.cache_coherent_dma = TRUE;
371 if (boothowto & RB_KDB) {
372 kdb_enter("Boot flags requested debugger", NULL);
378 platform_get_timecount(struct timecounter *tc __unused)
380 uint64_t count = nlm_pic_read_timer(xlp_pic_base, PIC_CLOCK_TIMER);
382 return (unsigned int)~count;
388 struct timecounter pic_timecounter = {
389 platform_get_timecount, /* get_timecount */
391 ~0U, /* counter_mask */
392 XLP_IO_CLK, /* frequency */
394 2000, /* quality (adjusted in code) */
399 xlp_pic_base = nlm_get_pic_regbase(0); /* TOOD: Add other nodes */
400 maxirt = nlm_read_reg(nlm_get_pic_pcibase(nlm_nodeid()),
401 XLP_PCI_DEVINFO_REG0);
402 printf("Initializing PIC...@%jx %d IRTs\n", (uintmax_t)xlp_pic_base,
404 /* Bind all PIC irqs to cpu 0 */
405 for (i = 0; i < maxirt; i++)
406 nlm_pic_write_irt(xlp_pic_base, i, 0, 0, 1, 0,
409 nlm_pic_set_timer(xlp_pic_base, PIC_CLOCK_TIMER, ~0ULL, 0, 0);
410 platform_timecounter = &pic_timecounter;
413 #if defined(__mips_n32) || defined(__mips_n64) /* PHYSADDR_64_BIT */
415 #define XLP_MEM_LIM 0x200000000ULL
417 #define XLP_MEM_LIM 0x10000000000ULL
420 #define XLP_MEM_LIM 0xfffff000UL
422 static vm_paddr_t xlp_mem_excl[] = {
423 0, 0, /* entry for kernel image, set by xlp_mem_init*/
424 0x0c000000, 0x0d000000, /* uboot mess */
425 0x10000000, 0x14000000, /* cms queue and other stuff */
426 0x1fc00000, 0x1fd00000, /* reset vec */
427 0x1e000000, 0x1e200000, /* poe buffers */
431 mem_exclude_add(vm_paddr_t *avail, vm_paddr_t mstart, vm_paddr_t mend)
433 int nreg = sizeof(xlp_mem_excl)/sizeof(xlp_mem_excl[0]);
437 for (i = 0; i < nreg; i += 2) {
438 if (mstart > xlp_mem_excl[i + 1])
440 if (mstart < xlp_mem_excl[i]) {
441 avail[pos++] = mstart;
442 if (mend < xlp_mem_excl[i])
445 avail[pos++] = xlp_mem_excl[i];
447 mstart = xlp_mem_excl[i + 1];
452 avail[pos++] = mstart;
461 vm_paddr_t physsz, tmp;
462 uint64_t bridgebase, base, lim, val;
465 /* update kernel image area in exclude regions */
466 tmp = (vm_paddr_t)MIPS_KSEG0_TO_PHYS(&_end);
467 tmp = round_page(tmp) + 0x20000; /* round up */
468 xlp_mem_excl[1] = tmp;
470 printf("Memory (from DRAM BARs):\n");
471 bridgebase = nlm_get_bridge_regbase(0); /* TODO: Add other nodes */
473 for (i = 0, j = 0; i < 8; i++) {
474 val = nlm_read_bridge_reg(bridgebase, BRIDGE_DRAM_BAR(i));
475 val = (val >> 12) & 0xfffff;
477 val = nlm_read_bridge_reg(bridgebase, BRIDGE_DRAM_LIMIT(i));
478 val = (val >> 12) & 0xfffff;
479 if (val == 0) /* BAR not enabled */
481 lim = (val + 1) << 20;
482 printf(" BAR %d: %#jx - %#jx : ", i, (intmax_t)base,
486 printf("\tskipped - malformed %#jx -> %#jx\n",
487 (intmax_t)base, (intmax_t)lim);
489 } else if (base >= XLP_MEM_LIM) {
490 printf(" skipped - outside usable limit %#jx.\n",
491 (intmax_t)XLP_MEM_LIM);
493 } else if (lim >= XLP_MEM_LIM) {
495 printf(" truncated to %#jx.\n", (intmax_t)XLP_MEM_LIM);
499 /* exclude unusable regions from BAR and add rest */
500 n = mem_exclude_add(&phys_avail[j], base, lim);
501 for (k = j; k < j + n; k += 2) {
502 physsz += phys_avail[k + 1] - phys_avail[k];
503 printf("\tMem[%d]: %#jx - %#jx\n", k/2,
504 (intmax_t)phys_avail[k], (intmax_t)phys_avail[k+1]);
509 /* setup final entry with 0 */
510 phys_avail[j] = phys_avail[j + 1] = 0;
512 /* copy phys_avail to dump_avail */
513 for (i = 0; i <= j + 1; i++)
514 dump_avail[i] = phys_avail[i];
516 realmem = physmem = btoc(physsz);
520 platform_start(__register_t a0 __unused,
521 __register_t a1 __unused,
522 __register_t a2 __unused,
523 __register_t a3 __unused)
526 /* Initialize pcpu stuff */
529 /* initialize console so that we have printf */
530 boothowto |= (RB_SERIAL | RB_MULTIPLE); /* Use multiple consoles */
532 init_static_kenv(boot1_env, sizeof(boot1_env));
533 xlp_bootargs_init(a0);
535 /* clockrate used by delay, so initialize it here */
536 xlp_cpu_frequency = xlp_get_cpu_frequency(0, 0);
537 cpu_clock = xlp_cpu_frequency / 1000000;
538 mips_timer_early_init(xlp_cpu_frequency);
540 /* Init console please */
543 /* Early core init and fixes for errata */
546 xlp_parse_mmu_options();
549 bcopy(XLPResetEntry, (void *)MIPS_RESET_EXC_VEC,
550 XLPResetEntryEnd - XLPResetEntry);
553 * We will enable the other threads in core 0 here
554 * so that the TLB and cache info is correct when
557 xlp_enable_threads(xlp_mmuval);
559 /* setup for the startup core */
562 /* Read/Guess/setup board information */
563 nlm_board_info_setup();
565 /* MIPS generic init */
569 * XLP specific post initialization
570 * initialize other on chip stuff
574 mips_timer_init_params(xlp_cpu_frequency, 0);
585 uint64_t sysbase = nlm_get_sys_regbase(0);
587 nlm_write_sys_reg(sysbase, SYS_CHIP_RESET, 1);
589 __asm __volatile("wait");
594 * XLP threads are started simultaneously when we enable threads, this will
595 * ensure that the threads are blocked in platform_init_ap, until they are
596 * ready to proceed to smp_init_secondary()
598 static volatile int thr_unblock[4];
601 platform_start_ap(int cpuid)
603 uint32_t coremask, val;
604 uint64_t sysbase = nlm_get_sys_regbase(0);
605 int hwtid = xlp_cpuid_to_hwtid[cpuid];
611 /* First thread in core, do core wake up */
612 coremask = 1u << core;
614 /* Enable core clock */
615 val = nlm_read_sys_reg(sysbase, SYS_CORE_DFS_DIS_CTRL);
617 nlm_write_sys_reg(sysbase, SYS_CORE_DFS_DIS_CTRL, val);
619 /* Remove CPU Reset */
620 val = nlm_read_sys_reg(sysbase, SYS_CPU_RESET);
621 val &= ~coremask & 0xff;
622 nlm_write_sys_reg(sysbase, SYS_CPU_RESET, val);
625 printf("Waking up core %d ...", core);
627 /* Poll for CPU to mark itself coherent */
629 val = nlm_read_sys_reg(sysbase, SYS_CPU_NONCOHERENT_MODE);
630 } while ((val & coremask) != 0);
634 /* otherwise release the threads stuck in platform_init_ap */
635 thr_unblock[thr] = 1;
642 platform_init_ap(int cpuid)
647 /* The first thread has to setup the MMU and enable other threads */
648 thr = nlm_threadid();
651 xlp_enable_threads(xlp_mmuval);
654 * FIXME busy wait here eats too many cycles, especially
655 * in the core 0 while bootup
657 while (thr_unblock[thr] == 0)
658 __asm__ __volatile__ ("nop;nop;nop;nop");
659 thr_unblock[thr] = 0;
663 stat = mips_rd_status();
664 KASSERT((stat & MIPS_SR_INT_IE) == 0,
665 ("Interrupts enabled in %s!", __func__));
666 stat |= MIPS_SR_COP_2_BIT | MIPS_SR_COP_0_BIT;
667 mips_wr_status(stat);
669 nlm_write_c0_eimr(0ull);
670 xlp_enable_irq(IRQ_IPI);
671 xlp_enable_irq(IRQ_TIMER);
672 xlp_enable_irq(IRQ_MSGRING);
678 platform_ipi_intrnum(void)
685 platform_ipi_send(int cpuid)
688 nlm_pic_send_ipi(xlp_pic_base, xlp_cpuid_to_hwtid[cpuid],
689 platform_ipi_intrnum(), 0);
693 platform_ipi_clear(void)
698 platform_processor_id(void)
701 return (xlp_hwtid_to_cpuid[nlm_cpuid()]);
705 platform_cpu_mask(cpuset_t *mask)
710 s = xlp_ncores * xlp_threads_per_core;
711 for (i = 0; i < s; i++)
719 return (smp_topo_2level(CG_SHARE_L2, xlp_ncores, CG_SHARE_L1,
720 xlp_threads_per_core, CG_FLAG_THREAD));