2 * Copyright (c) 2006-2009 RMI Corporation
3 * Copyright (c) 2002-2004 Juli Mallett <jmallett@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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 the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
36 #include <sys/rtprio.h>
37 #include <sys/systm.h>
38 #include <sys/interrupt.h>
39 #include <sys/limits.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <sys/random.h>
45 #include <sys/cons.h> /* cinit() */
47 #include <sys/reboot.h>
48 #include <sys/queue.h>
50 #include <sys/timetc.h>
53 #include <vm/vm_page.h>
55 #include <machine/cpu.h>
56 #include <machine/cpufunc.h>
57 #include <machine/cpuinfo.h>
58 #include <machine/cpuregs.h>
59 #include <machine/frame.h>
60 #include <machine/hwfunc.h>
61 #include <machine/md_var.h>
62 #include <machine/asm.h>
63 #include <machine/pmap.h>
64 #include <machine/trap.h>
65 #include <machine/clock.h>
66 #include <machine/fls64.h>
67 #include <machine/intr_machdep.h>
68 #include <machine/smp.h>
70 #include <mips/rmi/iomap.h>
71 #include <mips/rmi/msgring.h>
72 #include <mips/rmi/interrupt.h>
73 #include <mips/rmi/pic.h>
74 #include <mips/rmi/board.h>
75 #include <mips/rmi/rmi_mips_exts.h>
76 #include <mips/rmi/rmi_boot_info.h>
79 unsigned long xlr_io_base = (unsigned long)(DEFAULT_XLR_IO_BASE);
81 /* 4KB static data aread to keep a copy of the bootload env until
82 the dynamic kenv is setup */
84 int rmi_spin_mutex_safe=0;
85 struct mtx xlr_pic_lock;
88 * Parameters from boot loader
90 struct boot1_info xlr_boot1_info;
93 int32_t *xlr_argv, *xlr_envp;
94 uint64_t cpu_mask_info;
95 uint32_t xlr_online_cpumask;
96 uint32_t xlr_core_cpu_mask = 0x1; /* Core 0 thread 0 is always there */
98 int xlr_shtlb_enabled;
100 int xlr_threads_per_core;
101 uint32_t xlr_hw_thread_mask;
102 int xlr_cpuid_to_hwtid[MAXCPU];
103 int xlr_hwtid_to_cpuid[MAXCPU];
106 xlr_setup_mmu_split(void)
111 if (xlr_threads_per_core == 4 && xlr_shtlb_enabled == 0)
112 return; /* no change from boot setup */
114 switch (xlr_threads_per_core) {
123 mmu_setup = read_xlr_ctrl_register(4, 0);
124 mmu_setup = mmu_setup & ~0x06;
125 mmu_setup |= (val << 1);
127 /* turn on global mode */
128 if (xlr_shtlb_enabled)
131 write_xlr_ctrl_register(4, 0, mmu_setup);
135 xlr_parse_mmu_options(void)
138 char *hw_env, *start, *end;
141 uint8_t core0_thr_mask, core_thr_mask;
144 /* First check for the shared TLB setup */
145 xlr_shtlb_enabled = 0;
148 * We don't support sharing TLB per core - TODO
150 xlr_shtlb_enabled = 0;
151 if ((hw_env = getenv("xlr.shtlb")) != NULL) {
153 tmp = strtoul(start, &end, 0);
155 xlr_shtlb_enabled = (tmp != 0);
157 printf("Bad value for xlr.shtlb [%s]\n", hw_env);
162 * XLR supports splitting the 64 TLB entries across one, two or four
163 * threads (split mode). XLR also allows the 64 TLB entries to be shared
164 * across all threads in the core using a global flag (shared TLB mode).
165 * We will support 1/2/4 threads in split mode or shared mode.
169 cpu_map = xlr_boot1_info.cpu_online_map;
171 #ifndef SMP /* Uniprocessor! */
172 if (cpu_map != 0x1) {
173 printf("WARNING: Starting uniprocessor kernel on cpumask [0x%lx]!\n"
174 "WARNING: Other CPUs will be unused.\n", (u_long)cpu_map);
178 core0_thr_mask = cpu_map & 0xf;
179 switch (core0_thr_mask) {
181 xlr_threads_per_core = 1; break;
183 xlr_threads_per_core = 2; break;
185 xlr_threads_per_core = 4; break;
190 /* Verify other cores CPU masks */
191 for (i = 1; i < XLR_MAX_CORES; i++) {
192 core_thr_mask = (cpu_map >> (i*4)) & 0xf;
194 if (core_thr_mask != core0_thr_mask)
199 xlr_hw_thread_mask = cpu_map;
201 /* setup hardware processor id to cpu id mapping */
202 for (i = 0; i< MAXCPU; i++)
203 xlr_cpuid_to_hwtid[i] =
204 xlr_hwtid_to_cpuid [i] = -1;
205 for (i = 0, k = 0; i < XLR_MAX_CORES; i++) {
206 if (((cpu_map >> (i*4)) & 0xf) == 0)
208 for (j = 0; j < xlr_threads_per_core; j++) {
209 xlr_cpuid_to_hwtid[k] = i*4 + j;
210 xlr_hwtid_to_cpuid[i*4 + j] = k;
215 /* setup for the startup core */
216 xlr_setup_mmu_split();
220 printf("ERROR : Unsupported CPU mask [use 1,2 or 4 threads per core].\n"
221 "\tcore0 thread mask [%lx], boot cpu mask [%lx]\n"
222 "\tUsing default, 16 TLB entries per CPU, split mode\n",
223 (u_long)core0_thr_mask, (u_long)cpu_map);
224 panic("Invalid CPU mask - halting.\n");
229 xlr_set_boot_flags(void)
233 p = getenv("bootflags");
235 p = getenv("boot_flags"); /* old style */
239 for (; p && *p != '\0'; p++) {
251 boothowto |= RB_VERBOSE;
254 case 's': /* single-user (default, supported for sanity) */
256 boothowto |= RB_SINGLE;
260 printf("Unrecognized boot flag '%c'.\n", *p);
268 extern uint32_t _end;
274 init_param2(physmem);
277 cpuinfo.cache_coherent_dma = TRUE;
281 if (boothowto & RB_KDB) {
282 kdb_enter("Boot flags requested debugger", NULL);
290 platform_get_timecount(struct timecounter *tc __unused)
293 return (0xffffffffU - pic_timer_count32(PIC_CLOCK_TIMER));
299 struct timecounter pic_timecounter = {
300 platform_get_timecount, /* get_timecount */
302 ~0U, /* counter_mask */
303 PIC_TIMER_HZ, /* frequency */
305 2000, /* quality (adjusted in code) */
307 xlr_reg_t *mmio = xlr_io_mmio(XLR_IO_PIC_OFFSET);
310 write_c0_eimr64(0ULL);
311 mtx_init(&xlr_pic_lock, "pic", NULL, MTX_SPIN);
312 xlr_write_reg(mmio, PIC_CTRL, 0);
314 /* Initialize all IRT entries */
315 for (i = 0; i < PIC_NUM_IRTS; i++) {
316 irq = PIC_INTR_TO_IRQ(i);
319 * Disable all IRTs. Set defaults (local scheduling, high
320 * polarity, level * triggered, and CPU irq)
322 xlr_write_reg(mmio, PIC_IRT_1(i), (1 << 30) | (1 << 6) | irq);
323 /* Bind all PIC irqs to cpu 0 */
324 xlr_write_reg(mmio, PIC_IRT_0(i), 0x01);
327 /* Setup timer 7 of PIC as a timestamp, no interrupts */
328 pic_init_timer(PIC_CLOCK_TIMER);
329 pic_set_timer(PIC_CLOCK_TIMER, ~UINT64_C(0));
330 platform_timecounter = &pic_timecounter;
336 struct xlr_boot1_mem_map *boot_map;
337 vm_size_t physsz = 0;
340 /* get physical memory info from boot loader */
341 boot_map = (struct xlr_boot1_mem_map *)
342 (unsigned long)xlr_boot1_info.psb_mem_map;
343 for (i = 0, j = 0; i < boot_map->num_entries; i++, j += 2) {
344 if (boot_map->physmem_map[i].type != BOOT1_MEM_RAM)
347 printf("*** ERROR *** memory map too large ***\n");
351 /* start after kernel end */
352 phys_avail[0] = (vm_paddr_t)
353 MIPS_KSEG0_TO_PHYS(&_end) + 0x20000;
354 /* boot loader start */
355 /* HACK to Use bootloaders memory region */
356 if (boot_map->physmem_map[0].size == 0x0c000000) {
357 boot_map->physmem_map[0].size = 0x0ff00000;
359 phys_avail[1] = boot_map->physmem_map[0].addr +
360 boot_map->physmem_map[0].size;
361 printf("First segment: addr:%#jx -> %#jx \n",
362 (uintmax_t)phys_avail[0],
363 (uintmax_t)phys_avail[1]);
365 dump_avail[0] = phys_avail[0];
366 dump_avail[1] = phys_avail[1];
368 #if !defined(__mips_n64) && !defined(__mips_n32) /* !PHYSADDR_64_BIT */
370 * In 32 bit physical address mode we cannot use
373 if (boot_map->physmem_map[i].addr > 0xfffff000U) {
374 printf("Memory: start %#jx size %#jx ignored"
376 (intmax_t)boot_map->physmem_map[i].addr,
377 (intmax_t)boot_map->physmem_map[i].size);
380 if (boot_map->physmem_map[i].addr +
381 boot_map->physmem_map[i].size > 0xfffff000U) {
382 boot_map->physmem_map[i].size = 0xfffff000U -
383 boot_map->physmem_map[i].addr;
384 printf("Memory: start %#jx limited to 4GB\n",
385 (intmax_t)boot_map->physmem_map[i].addr);
387 #endif /* !PHYSADDR_64_BIT */
388 phys_avail[j] = (vm_paddr_t)
389 boot_map->physmem_map[i].addr;
390 phys_avail[j + 1] = phys_avail[j] +
391 boot_map->physmem_map[i].size;
392 printf("Next segment : addr:%#jx -> %#jx\n",
393 (uintmax_t)phys_avail[j],
394 (uintmax_t)phys_avail[j+1]);
397 dump_avail[j] = phys_avail[j];
398 dump_avail[j+1] = phys_avail[j+1];
400 physsz += boot_map->physmem_map[i].size;
403 phys_avail[j] = phys_avail[j + 1] = 0;
404 realmem = physmem = btoc(physsz);
408 platform_start(__register_t a0 __unused,
409 __register_t a1 __unused,
410 __register_t a2 __unused,
411 __register_t a3 __unused)
416 void (*wakeup) (void *, void *, unsigned int);
419 /* Save boot loader and other stuff from scratch regs */
420 xlr_boot1_info = *(struct boot1_info *)(intptr_t)(int)read_c0_register32(MIPS_COP_0_OSSCRATCH, 0);
421 cpu_mask_info = read_c0_register64(MIPS_COP_0_OSSCRATCH, 1);
422 xlr_online_cpumask = read_c0_register32(MIPS_COP_0_OSSCRATCH, 2);
423 xlr_run_mode = read_c0_register32(MIPS_COP_0_OSSCRATCH, 3);
424 xlr_argc = read_c0_register32(MIPS_COP_0_OSSCRATCH, 4);
426 * argv and envp are passed in array of 32bit pointers
428 xlr_argv = (int32_t *)(intptr_t)(int)read_c0_register32(MIPS_COP_0_OSSCRATCH, 5);
429 xlr_envp = (int32_t *)(intptr_t)(int)read_c0_register32(MIPS_COP_0_OSSCRATCH, 6);
431 /* Initialize pcpu stuff */
434 /* initialize console so that we have printf */
435 boothowto |= (RB_SERIAL | RB_MULTIPLE); /* Use multiple consoles */
437 /* clockrate used by delay, so initialize it here */
438 cpu_clock = xlr_boot1_info.cpu_frequency / 1000000;
441 * Note the time counter on CPU0 runs not at system clock speed, but
442 * at PIC time counter speed (which is returned by
443 * platform_get_frequency(). Thus we do not use
444 * xlr_boot1_info.cpu_frequency here.
446 mips_timer_early_init(xlr_boot1_info.cpu_frequency);
448 /* Init console please */
450 init_static_kenv(boot1_env, sizeof(boot1_env));
451 printf("Environment (from %d args):\n", xlr_argc - 1);
454 for (i = 1; i < xlr_argc; i++) {
457 arg = (char *)(intptr_t)xlr_argv[i];
458 printf("\t%s\n", arg);
459 n = strsep(&arg, "=");
466 xlr_set_boot_flags();
467 xlr_parse_mmu_options();
470 /* Set up hz, among others. */
475 * If thread 0 of any core is not available then mark whole core as
478 tmp = xlr_boot1_info.cpu_online_map;
479 for (i = 4; i < MAXCPU; i += 4) {
480 if ((tmp & (0xf << i)) && !(tmp & (0x1 << i))) {
482 * Oops.. thread 0 is not available. Disable whole
485 tmp = tmp & ~(0xf << i);
486 printf("WARNING: Core %d is disabled because thread 0"
487 " of this core is not enabled.\n", i / 4);
490 xlr_boot1_info.cpu_online_map = tmp;
492 /* Wakeup Other cpus, and put them in bsd park code. */
493 wakeup = ((void (*) (void *, void *, unsigned int))
494 (unsigned long)(xlr_boot1_info.wakeup));
495 printf("Waking up CPUs 0x%jx.\n",
496 (intmax_t)xlr_boot1_info.cpu_online_map & ~(0x1U));
497 if (xlr_boot1_info.cpu_online_map & ~(0x1U))
499 (unsigned int)xlr_boot1_info.cpu_online_map);
502 /* xlr specific post initialization */
503 /* initialize other on chip stuff */
504 xlr_board_info_setup();
505 xlr_msgring_config();
507 xlr_msgring_cpu_init();
509 mips_timer_init_params(xlr_boot1_info.cpu_frequency, 0);
511 printf("Platform specific startup now completes\n");
522 xlr_reg_t *mmio = xlr_io_mmio(XLR_IO_GPIO_OFFSET);
524 /* write 1 to GPIO software reset register */
525 xlr_write_reg(mmio, 8, 1);
529 int xlr_ap_release[MAXCPU];
532 platform_start_ap(int cpuid)
534 int hwid = xlr_cpuid_to_hwtid[cpuid];
536 if (xlr_boot1_info.cpu_online_map & (1<<hwid)) {
538 * other cpus are enabled by the boot loader and they will be
539 * already looping in mpwait, release them
541 atomic_store_rel_int(&xlr_ap_release[hwid], 1);
548 platform_init_ap(int cpuid)
552 /* The first thread has to setup the core MMU split */
553 if (xlr_thr_id() == 0)
554 xlr_setup_mmu_split();
556 /* Setup interrupts for secondary CPUs here */
557 stat = mips_rd_status();
558 KASSERT((stat & MIPS_SR_INT_IE) == 0,
559 ("Interrupts enabled in %s!", __func__));
560 stat |= MIPS_SR_COP_2_BIT | MIPS_SR_COP_0_BIT;
561 mips_wr_status(stat);
563 write_c0_eimr64(0ULL);
564 xlr_enable_irq(IRQ_IPI);
565 xlr_enable_irq(IRQ_TIMER);
566 if (xlr_thr_id() == 0)
567 xlr_msgring_cpu_init();
568 xlr_enable_irq(IRQ_MSGRING);
574 platform_ipi_intrnum(void)
581 platform_ipi_send(int cpuid)
584 pic_send_ipi(xlr_cpuid_to_hwtid[cpuid], platform_ipi_intrnum());
588 platform_ipi_clear(void)
593 platform_processor_id(void)
596 return (xlr_hwtid_to_cpuid[xlr_cpu_id()]);
600 platform_cpu_mask(cpuset_t *mask)
605 s = xlr_ncores * xlr_threads_per_core;
606 for (i = 0; i < s; i++)
614 return (smp_topo_2level(CG_SHARE_L2, xlr_ncores, CG_SHARE_L1,
615 xlr_threads_per_core, CG_FLAG_THREAD));