Home | History | Annotate | Line # | Download | only in cavium
machdep.c revision 1.1
      1 /*	$NetBSD: machdep.c,v 1.1 2015/04/29 08:32:01 hikaru Exp $	*/
      2 
      3 /*
      4  * Copyright 2001, 2002 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe and Simon Burge for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed for the NetBSD Project by
     20  *      Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Copyright (c) 1992, 1993
     40  *	The Regents of the University of California.  All rights reserved.
     41  *
     42  * This code is derived from software contributed to Berkeley by
     43  * the Systems Programming Group of the University of Utah Computer
     44  * Science Department, The Mach Operating System project at
     45  * Carnegie-Mellon University and Ralph Campbell.
     46  *
     47  * Redistribution and use in source and binary forms, with or without
     48  * modification, are permitted provided that the following conditions
     49  * are met:
     50  * 1. Redistributions of source code must retain the above copyright
     51  *    notice, this list of conditions and the following disclaimer.
     52  * 2. Redistributions in binary form must reproduce the above copyright
     53  *    notice, this list of conditions and the following disclaimer in the
     54  *    documentation and/or other materials provided with the distribution.
     55  * 3. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  *	@(#)machdep.c   8.3 (Berkeley) 1/12/94
     72  *	from: Utah Hdr: machdep.c 1.63 91/04/24
     73  */
     74 /*
     75  * Copyright (c) 1988 University of Utah.
     76  *
     77  * This code is derived from software contributed to Berkeley by
     78  * the Systems Programming Group of the University of Utah Computer
     79  * Science Department, The Mach Operating System project at
     80  * Carnegie-Mellon University and Ralph Campbell.
     81  *
     82  * Redistribution and use in source and binary forms, with or without
     83  * modification, are permitted provided that the following conditions
     84  * are met:
     85  * 1. Redistributions of source code must retain the above copyright
     86  *    notice, this list of conditions and the following disclaimer.
     87  * 2. Redistributions in binary form must reproduce the above copyright
     88  *    notice, this list of conditions and the following disclaimer in the
     89  *    documentation and/or other materials provided with the distribution.
     90  * 3. All advertising materials mentioning features or use of this software
     91  *    must display the following acknowledgement:
     92  *	This product includes software developed by the University of
     93  *	California, Berkeley and its contributors.
     94  * 4. Neither the name of the University nor the names of its contributors
     95  *    may be used to endorse or promote products derived from this software
     96  *    without specific prior written permission.
     97  *
     98  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     99  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    100  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    101  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    102  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    103  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    104  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    105  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    106  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    107  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    108  * SUCH DAMAGE.
    109  *
    110  *	@(#)machdep.c   8.3 (Berkeley) 1/12/94
    111  *	from: Utah Hdr: machdep.c 1.63 91/04/24
    112  */
    113 
    114 #include <sys/cdefs.h>
    115 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.1 2015/04/29 08:32:01 hikaru Exp $");
    116 
    117 #include <sys/param.h>
    118 #include <sys/systm.h>
    119 #include <sys/kernel.h>
    120 #include <sys/buf.h>
    121 #include <sys/reboot.h>
    122 #include <sys/mount.h>
    123 #include <sys/kcore.h>
    124 #include <sys/boot_flag.h>
    125 #include <sys/termios.h>
    126 #include <sys/ksyms.h>
    127 
    128 #include <uvm/uvm_extern.h>
    129 
    130 #include <dev/cons.h>
    131 
    132 #include "ksyms.h"
    133 
    134 #if NKSYMS || defined(DDB) || defined(LKM)
    135 #include <machine/db_machdep.h>
    136 #include <ddb/db_extern.h>
    137 #endif
    138 
    139 #include <machine/cpu.h>
    140 #include <machine/psl.h>
    141 #include <machine/locore.h>
    142 
    143 #include <mips/cavium/autoconf.h>
    144 #include <mips/cavium/octeonvar.h>
    145 #include <mips/cavium/include/iobusvar.h>
    146 #include <mips/cavium/include/bootbusvar.h>
    147 
    148 #include <mips/cavium/dev/octeon_uartreg.h>
    149 #include <mips/cavium/dev/octeon_ciureg.h>
    150 #include <mips/cavium/dev/octeon_gpioreg.h>
    151 
    152 #include <evbmips/cavium/octeon_uboot.h>
    153 
    154 static void	mach_init_bss(void);
    155 static void	mach_init_vector(void);
    156 static void	mach_init_bus_space(void);
    157 static void	mach_init_console(void);
    158 static void	mach_init_memory(u_quad_t);
    159 
    160 #include "com.h"
    161 #if NCOM > 0
    162 #include <dev/ic/comreg.h>
    163 #include <dev/ic/comvar.h>
    164 int	comcnrate = 115200;	/* XXX should be config option */
    165 #endif /* NCOM > 0 */
    166 
    167 /* Maps for VM objects. */
    168 struct vm_map *phys_map = NULL;
    169 
    170 int	netboot;
    171 
    172 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
    173 int mem_cluster_cnt;
    174 
    175 
    176 void	configure(void);
    177 void	mach_init(uint64_t, uint64_t, uint64_t, uint64_t);
    178 
    179 struct octeon_config octeon_configuration;
    180 struct octeon_btinfo octeon_btinfo;
    181 
    182 /*
    183  * Do all the stuff that locore normally does before calling main().
    184  */
    185 void
    186 mach_init(uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3)
    187 {
    188 	uint64_t btinfo_paddr;
    189 	u_quad_t memsize;
    190 	int corefreq;
    191 
    192 	mach_init_bss();
    193 
    194 	KASSERT(MIPS_XKPHYS_P(arg3));
    195 	btinfo_paddr = mips64_ld_a64(arg3 + OCTEON_BTINFO_PADDR_OFFSET);
    196 
    197 	/* Should be in first 256MB segment */
    198 	KASSERT(btinfo_paddr < 256 * 1024 * 1024);
    199 	memcpy(&octeon_btinfo,
    200 	    (struct octeon_btinfo *)MIPS_PHYS_TO_KSEG0(btinfo_paddr),
    201 	    sizeof(octeon_btinfo));
    202 
    203 	corefreq = octeon_btinfo.obt_eclock_hz;
    204 	memsize = octeon_btinfo.obt_dram_size * 1024 * 1024;
    205 
    206 	octeon_cal_timer(corefreq);
    207 
    208 	mach_init_vector();
    209 
    210 	/* set the VM page size */
    211 	uvm_setpagesize();
    212 
    213 	mach_init_bus_space();
    214 
    215 	mach_init_console();
    216 
    217 	mach_init_memory(memsize);
    218 
    219 	/*
    220 	 * Allocate uarea page for lwp0 and set it.
    221 	 */
    222 	mips_init_lwp0_uarea();
    223 
    224 	boothowto = RB_AUTOBOOT;
    225 
    226 #if defined(DDB)
    227 	if (boothowto & RB_KDB)
    228 		Debugger();
    229 #endif
    230 }
    231 
    232 void
    233 consinit(void)
    234 {
    235 
    236 	/*
    237 	 * Everything related to console initialization is done
    238 	 * in mach_init().
    239 	 */
    240 }
    241 
    242 void
    243 mach_init_bss(void)
    244 {
    245 	extern char edata[], end[];
    246 
    247 	/*
    248 	 * Clear the BSS segment.
    249 	 */
    250 	memset(edata, 0, mips_round_page(end) - (uintptr_t)edata);
    251 }
    252 
    253 void
    254 mach_init_vector(void)
    255 {
    256 
    257 	/* Make sure exception base at 0 (MIPS_COP_0_EBASE) */
    258 	asm volatile("mtc0 %0, $15, 1" : : "r"(0x80000000) );
    259 
    260 	/*
    261 	 * Set up the exception vectors and CPU-specific function
    262 	 * vectors early on.  We need the wbflush() vector set up
    263 	 * before comcnattach() is called (or at least before the
    264 	 * first printf() after that is called).
    265 	 * Also clears the I+D caches.
    266 	 */
    267 	mips_vector_init(NULL, false);
    268 }
    269 
    270 void
    271 mach_init_bus_space(void)
    272 {
    273 	struct octeon_config *mcp = &octeon_configuration;
    274 
    275 	octeon_dma_init(mcp);
    276 
    277 	iobus_bootstrap(mcp);
    278 	bootbus_bootstrap(mcp);
    279 }
    280 
    281 void
    282 mach_init_console(void)
    283 {
    284 #if NCOM > 0
    285 	struct octeon_config *mcp = &octeon_configuration;
    286 	int status;
    287 	extern int octeon_uart_com_cnattach(bus_space_tag_t, int, int);
    288 
    289 	/*
    290 	 * Delay to allow firmware putchars to complete.
    291 	 * FIFO depth * character time.
    292 	 * character time = (1000000 / (defaultrate / 10))
    293 	 */
    294 	delay(640000000 / comcnrate);
    295 
    296 	status = octeon_uart_com_cnattach(
    297 		&mcp->mc_iobus_bust,
    298 		0,	/* XXX port 0 */
    299 		comcnrate);
    300 	if (status != 0)
    301 		panic("can't initialize console!");	/* XXX print to nowhere! */
    302 #else
    303 	panic("octeon: not configured to use serial console");
    304 #endif /* NCOM > 0 */
    305 }
    306 
    307 void
    308 mach_init_memory(u_quad_t memsize)
    309 {
    310 	extern char end[];
    311 
    312 	physmem = btoc(memsize);
    313 
    314 	if (memsize <= 256 * 1024 * 1024) {
    315 		mem_clusters[0].start = 0;
    316 		mem_clusters[0].size = memsize;
    317 		mem_cluster_cnt = 1;
    318 	} else if (memsize <= 512 * 1024 * 1024) {
    319 		mem_clusters[0].start = 0;
    320 		mem_clusters[0].size = 256 * 1024 * 1024;
    321 		mem_clusters[1].start = 0x410000000ULL;
    322 		mem_clusters[1].size = memsize - 256 * 1024 * 1024;
    323 		mem_cluster_cnt = 2;
    324 	} else {
    325 		mem_clusters[0].start = 0;
    326 		mem_clusters[0].size = 256 * 1024 * 1024;
    327 		mem_clusters[1].start = 0x20000000;
    328 		mem_clusters[1].size = memsize - 512 * 1024 * 1024;
    329 		mem_clusters[2].start = 0x410000000ULL;
    330 		mem_clusters[2].size = 256 * 1024 * 1024;
    331 		mem_cluster_cnt = 3;
    332 	}
    333 
    334 	/*
    335 	 * Load the rest of the available pages into the VM system.
    336 	 */
    337 	mips_page_physload(MIPS_KSEG0_START, mips_round_page(end),
    338 	    mem_clusters, mem_cluster_cnt, NULL, 0);
    339 
    340 	/*
    341 	 * Initialize error message buffer (at end of core).
    342 	 */
    343 	mips_init_msgbuf();
    344 
    345 	pmap_bootstrap();
    346 }
    347 
    348 /*
    349  * cpu_startup
    350  * cpu_reboot
    351  */
    352 
    353 int	waittime = -1;
    354 
    355 /*
    356  * Allocate memory for variable-sized tables,
    357  */
    358 void
    359 cpu_startup(void)
    360 {
    361 	/*
    362 	 * Do the common startup items.
    363 	 */
    364 	cpu_startup_common();
    365 
    366 	/*
    367 	 * Virtual memory is bootstrapped -- notify the bus spaces
    368 	 * that memory allocation is now safe.
    369 	 */
    370 	octeon_configuration.mc_mallocsafe = 1;
    371 }
    372 
    373 void
    374 cpu_reboot(int howto, char *bootstr)
    375 {
    376 
    377 	/* Take a snapshot before clobbering any registers. */
    378 	savectx(curpcb);
    379 
    380 	if (cold) {
    381 		howto |= RB_HALT;
    382 		goto haltsys;
    383 	}
    384 
    385 	/* If "always halt" was specified as a boot flag, obey. */
    386 	if (boothowto & RB_HALT)
    387 		howto |= RB_HALT;
    388 
    389 	boothowto = howto;
    390 	if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
    391 		waittime = 0;
    392 		vfs_shutdown();
    393 
    394 		/*
    395 		 * If we've been adjusting the clock, the todr
    396 		 * will be out of synch; adjust it now.
    397 		 */
    398 		resettodr();
    399 	}
    400 
    401 	splhigh();
    402 
    403 	if (howto & RB_DUMP)
    404 		dumpsys();
    405 
    406 haltsys:
    407 	doshutdownhooks();
    408 
    409 	if (howto & RB_HALT) {
    410 		printf("\n");
    411 		printf("The operating system has halted.\n");
    412 		printf("Please press any key to reboot.\n\n");
    413 	}
    414 
    415 	printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
    416 
    417 	/*
    418 	 * Need a small delay here, otherwise we see the first few characters of
    419 	 * the warning below.
    420 	 */
    421 	delay(80000);
    422 
    423 	/* initiate chip soft-reset */
    424 	octeon_write_csr(CIU_SOFT_BIST, 0x0000000000000001ULL);
    425 	octeon_read_csr(CIU_SOFT_RST);
    426 	octeon_write_csr(CIU_SOFT_RST, 0x0000000000000001ULL);
    427 
    428 	delay(1000000);
    429 
    430 	printf("WARNING: reset failed!\nSpinning...");
    431 
    432 	for (;;)
    433 		/* spin forever */ ;	/* XXX */
    434 }
    435