Home | History | Annotate | Line # | Download | only in rmixl
machdep.c revision 1.9
      1  1.9   matt /*	$NetBSD: machdep.c,v 1.9 2011/04/12 00:21:10 matt Exp $	*/
      2  1.2   matt 
      3  1.2   matt /*
      4  1.2   matt  * Copyright 2001, 2002 Wasabi Systems, Inc.
      5  1.2   matt  * All rights reserved.
      6  1.2   matt  *
      7  1.2   matt  * Written by Jason R. Thorpe and Simon Burge for Wasabi Systems, Inc.
      8  1.2   matt  *
      9  1.2   matt  * Redistribution and use in source and binary forms, with or without
     10  1.2   matt  * modification, are permitted provided that the following conditions
     11  1.2   matt  * are met:
     12  1.2   matt  * 1. Redistributions of source code must retain the above copyright
     13  1.2   matt  *    notice, this list of conditions and the following disclaimer.
     14  1.2   matt  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.2   matt  *    notice, this list of conditions and the following disclaimer in the
     16  1.2   matt  *    documentation and/or other materials provided with the distribution.
     17  1.2   matt  * 3. All advertising materials mentioning features or use of this software
     18  1.2   matt  *    must display the following acknowledgement:
     19  1.2   matt  *      This product includes software developed for the NetBSD Project by
     20  1.2   matt  *      Wasabi Systems, Inc.
     21  1.2   matt  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  1.2   matt  *    or promote products derived from this software without specific prior
     23  1.2   matt  *    written permission.
     24  1.2   matt  *
     25  1.2   matt  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  1.2   matt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  1.2   matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  1.2   matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  1.2   matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  1.2   matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  1.2   matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  1.2   matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  1.2   matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  1.2   matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  1.2   matt  * POSSIBILITY OF SUCH DAMAGE.
     36  1.2   matt  */
     37  1.2   matt 
     38  1.2   matt /*
     39  1.6  rmind  * Copyright (c) 1988 University of Utah.
     40  1.2   matt  * Copyright (c) 1992, 1993
     41  1.2   matt  *	The Regents of the University of California.  All rights reserved.
     42  1.2   matt  *
     43  1.2   matt  * This code is derived from software contributed to Berkeley by
     44  1.2   matt  * the Systems Programming Group of the University of Utah Computer
     45  1.2   matt  * Science Department, The Mach Operating System project at
     46  1.2   matt  * Carnegie-Mellon University and Ralph Campbell.
     47  1.2   matt  *
     48  1.2   matt  * Redistribution and use in source and binary forms, with or without
     49  1.2   matt  * modification, are permitted provided that the following conditions
     50  1.2   matt  * are met:
     51  1.2   matt  * 1. Redistributions of source code must retain the above copyright
     52  1.2   matt  *    notice, this list of conditions and the following disclaimer.
     53  1.2   matt  * 2. Redistributions in binary form must reproduce the above copyright
     54  1.2   matt  *    notice, this list of conditions and the following disclaimer in the
     55  1.2   matt  *    documentation and/or other materials provided with the distribution.
     56  1.2   matt  * 3. Neither the name of the University nor the names of its contributors
     57  1.2   matt  *    may be used to endorse or promote products derived from this software
     58  1.2   matt  *    without specific prior written permission.
     59  1.2   matt  *
     60  1.2   matt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61  1.2   matt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62  1.2   matt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63  1.2   matt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64  1.2   matt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65  1.2   matt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66  1.2   matt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  1.2   matt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  1.2   matt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  1.2   matt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  1.2   matt  * SUCH DAMAGE.
     71  1.2   matt  *
     72  1.2   matt  *	@(#)machdep.c   8.3 (Berkeley) 1/12/94
     73  1.2   matt  *	from: Utah Hdr: machdep.c 1.63 91/04/24
     74  1.2   matt  */
     75  1.2   matt 
     76  1.2   matt #include <sys/cdefs.h>
     77  1.9   matt __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.9 2011/04/12 00:21:10 matt Exp $");
     78  1.2   matt 
     79  1.7   matt #define __INTR_PRIVATE
     80  1.7   matt 
     81  1.7   matt #include "opt_multiprocessor.h"
     82  1.2   matt #include "opt_ddb.h"
     83  1.2   matt #include "opt_com.h"
     84  1.2   matt #include "opt_execfmt.h"
     85  1.2   matt #include "opt_memsize.h"
     86  1.7   matt #include "rmixl_pcix.h"
     87  1.7   matt #include "rmixl_pcie.h"
     88  1.2   matt 
     89  1.2   matt #include <sys/param.h>
     90  1.2   matt #include <sys/systm.h>
     91  1.2   matt #include <sys/kernel.h>
     92  1.2   matt #include <sys/buf.h>
     93  1.2   matt #include <sys/reboot.h>
     94  1.2   matt #include <sys/mount.h>
     95  1.2   matt #include <sys/kcore.h>
     96  1.2   matt #include <sys/boot_flag.h>
     97  1.2   matt #include <sys/termios.h>
     98  1.2   matt #include <sys/ksyms.h>
     99  1.2   matt #include <sys/bus.h>
    100  1.2   matt #include <sys/device.h>
    101  1.2   matt #include <sys/extent.h>
    102  1.2   matt #include <sys/malloc.h>
    103  1.2   matt 
    104  1.2   matt #include <uvm/uvm_extern.h>
    105  1.2   matt 
    106  1.2   matt #include <dev/cons.h>
    107  1.2   matt 
    108  1.2   matt #include "ksyms.h"
    109  1.2   matt 
    110  1.2   matt #if NKSYMS || defined(DDB) || defined(LKM)
    111  1.8   matt #include <mips/db_machdep.h>
    112  1.2   matt #include <ddb/db_extern.h>
    113  1.2   matt #endif
    114  1.2   matt 
    115  1.8   matt #include <mips/cpu.h>
    116  1.8   matt #include <mips/psl.h>
    117  1.8   matt #include <mips/cache.h>
    118  1.8   matt #include <mips/mips_opcode.h>
    119  1.2   matt 
    120  1.2   matt #include "com.h"
    121  1.2   matt #if NCOM == 0
    122  1.2   matt #error no serial console
    123  1.2   matt #endif
    124  1.2   matt 
    125  1.2   matt #include <dev/ic/comreg.h>
    126  1.2   matt #include <dev/ic/comvar.h>
    127  1.2   matt 
    128  1.7   matt #include <mips/include/intr.h>
    129  1.7   matt 
    130  1.7   matt #include <mips/rmi/rmixlreg.h>
    131  1.2   matt #include <mips/rmi/rmixlvar.h>
    132  1.7   matt #include <mips/rmi/rmixl_intr.h>
    133  1.2   matt #include <mips/rmi/rmixl_firmware.h>
    134  1.7   matt #include <mips/rmi/rmixl_comvar.h>
    135  1.7   matt #include <mips/rmi/rmixl_pcievar.h>
    136  1.7   matt #include <mips/rmi/rmixl_pcixvar.h>
    137  1.2   matt 
    138  1.2   matt #ifdef MACHDEP_DEBUG
    139  1.2   matt int machdep_debug=MACHDEP_DEBUG;
    140  1.2   matt # define DPRINTF(x)	do { if (machdep_debug) printf x ; } while(0)
    141  1.2   matt #else
    142  1.2   matt # define DPRINTF(x)
    143  1.2   matt #endif
    144  1.2   matt 
    145  1.2   matt #ifndef CONSFREQ
    146  1.7   matt # define CONSFREQ 66000000
    147  1.2   matt #endif
    148  1.2   matt #ifndef CONSPEED
    149  1.2   matt # define CONSPEED 38400
    150  1.2   matt #endif
    151  1.2   matt #ifndef CONMODE
    152  1.2   matt # define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8)
    153  1.2   matt #endif
    154  1.2   matt #ifndef CONSADDR
    155  1.2   matt # define CONSADDR RMIXL_IO_DEV_UART_1
    156  1.2   matt #endif
    157  1.2   matt 
    158  1.2   matt int		comcnfreq  = CONSFREQ;
    159  1.2   matt int		comcnspeed = CONSPEED;
    160  1.2   matt tcflag_t	comcnmode  = CONMODE;
    161  1.2   matt bus_addr_t	comcnaddr  = (bus_addr_t)CONSADDR;
    162  1.2   matt 
    163  1.2   matt struct rmixl_config rmixl_configuration;
    164  1.2   matt 
    165  1.2   matt 
    166  1.2   matt /*
    167  1.2   matt  * array of tested firmware versions
    168  1.2   matt  * if you find new ones and they work
    169  1.2   matt  * please add them
    170  1.2   matt  */
    171  1.7   matt typedef struct rmiclfw_psb_id {
    172  1.7   matt 	uint64_t		psb_version;
    173  1.7   matt 	rmixlfw_psb_type_t	psb_type;
    174  1.7   matt } rmiclfw_psb_id_t;
    175  1.7   matt static rmiclfw_psb_id_t rmiclfw_psb_id[] = {
    176  1.7   matt 	{	0x4958d4fb00000056ULL, PSB_TYPE_RMI  },
    177  1.7   matt 	{	0x4aacdb6a00000056ULL, PSB_TYPE_RMI  },
    178  1.7   matt 	{	0x4b67d03200000056ULL, PSB_TYPE_RMI  },
    179  1.7   matt 	{	0x4c17058b00000056ULL, PSB_TYPE_RMI  },
    180  1.7   matt 	{	0x49a5a8fa00000056ULL, PSB_TYPE_DELL },
    181  1.7   matt 	{	0x4b8ead3100000056ULL, PSB_TYPE_DELL },
    182  1.2   matt };
    183  1.2   matt #define RMICLFW_PSB_VERSIONS_LEN \
    184  1.7   matt 	(sizeof(rmiclfw_psb_id)/sizeof(rmiclfw_psb_id[0]))
    185  1.2   matt 
    186  1.2   matt /*
    187  1.2   matt  * storage for fixed extent used to allocate physical address regions
    188  1.2   matt  * because extent(9) start and end values are u_long, they are only
    189  1.2   matt  * 32 bits on a 32 bit kernel, which is insuffucuent since XLS physical
    190  1.2   matt  * address is 40 bits wide.  So the "physaddr" map stores regions
    191  1.2   matt  * in units of megabytes.
    192  1.2   matt  */
    193  1.2   matt static u_long rmixl_physaddr_storage[
    194  1.2   matt 	EXTENT_FIXED_STORAGE_SIZE(32)/sizeof(u_long)
    195  1.2   matt ];
    196  1.2   matt 
    197  1.2   matt /* For sysctl_hw. */
    198  1.2   matt extern char cpu_model[];
    199  1.2   matt 
    200  1.2   matt /* Our exported CPU info; we can have only one. */
    201  1.2   matt struct cpu_info cpu_info_store;
    202  1.2   matt 
    203  1.2   matt /* Maps for VM objects. */
    204  1.2   matt struct vm_map *phys_map = NULL;
    205  1.2   matt 
    206  1.2   matt int	physmem;		/* Total physical memory */
    207  1.2   matt 
    208  1.2   matt int	netboot;		/* Are we netbooting? */
    209  1.2   matt 
    210  1.2   matt 
    211  1.2   matt phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
    212  1.7   matt u_quad_t mem_cluster_maxaddr;
    213  1.2   matt u_int mem_cluster_cnt;
    214  1.2   matt 
    215  1.2   matt 
    216  1.2   matt void configure(void);
    217  1.2   matt void mach_init(int, int32_t *, void *, int64_t);
    218  1.7   matt static uint64_t rmixlfw_init(int64_t);
    219  1.7   matt static uint64_t mem_clusters_init(rmixlfw_mmap_t *, rmixlfw_mmap_t *);
    220  1.7   matt static void __attribute__((__noreturn__)) rmixl_reset(void);
    221  1.2   matt static void rmixl_physaddr_init(void);
    222  1.2   matt static u_int ram_seg_resv(phys_ram_seg_t *, u_int, u_quad_t, u_quad_t);
    223  1.2   matt void rmixlfw_mmap_print(rmixlfw_mmap_t *);
    224  1.2   matt 
    225  1.2   matt 
    226  1.7   matt #ifdef MULTIPROCESSOR
    227  1.7   matt static bool rmixl_fixup_cop0_oscratch(int32_t, uint32_t [2]);
    228  1.7   matt void rmixl_get_wakeup_info(struct rmixl_config *);
    229  1.7   matt #ifdef MACHDEP_DEBUG
    230  1.7   matt static void rmixl_wakeup_info_print(volatile rmixlfw_cpu_wakeup_info_t *);
    231  1.7   matt #endif	/* MACHDEP_DEBUG */
    232  1.7   matt #endif	/* MULTIPROCESSOR */
    233  1.8   matt static void rmixl_fixup_curcpu(void);
    234  1.2   matt 
    235  1.2   matt /*
    236  1.2   matt  * Do all the stuff that locore normally does before calling main().
    237  1.2   matt  */
    238  1.2   matt void
    239  1.2   matt mach_init(int argc, int32_t *argv, void *envp, int64_t infop)
    240  1.2   matt {
    241  1.2   matt 	struct rmixl_config *rcp = &rmixl_configuration;
    242  1.3  rmind 	void *kernend;
    243  1.7   matt 	uint64_t memsize;
    244  1.2   matt 	extern char edata[], end[];
    245  1.2   matt 
    246  1.7   matt 	rmixl_pcr_init_core();
    247  1.2   matt 
    248  1.2   matt 	/*
    249  1.2   matt 	 * Clear the BSS segment.
    250  1.2   matt 	 */
    251  1.2   matt 	kernend = (void *)mips_round_page(end);
    252  1.2   matt 	memset(edata, 0, (char *)kernend - edata);
    253  1.2   matt 
    254  1.2   matt 	/*
    255  1.2   matt 	 * Set up the exception vectors and CPU-specific function
    256  1.2   matt 	 * vectors early on.  We need the wbflush() vector set up
    257  1.2   matt 	 * before comcnattach() is called (or at least before the
    258  1.2   matt 	 * first printf() after that is called).
    259  1.2   matt 	 * Also clears the I+D caches.
    260  1.7   matt 	 *
    261  1.7   matt 	 * specify chip-specific EIRR/EIMR based spl functions
    262  1.2   matt 	 */
    263  1.7   matt #ifdef MULTIPROCESSOR
    264  1.7   matt 	mips_vector_init(&rmixl_splsw, true);
    265  1.7   matt #else
    266  1.7   matt 	mips_vector_init(&rmixl_splsw, false);
    267  1.7   matt #endif
    268  1.7   matt 
    269  1.7   matt 	/* mips_vector_init initialized mips_options */
    270  1.7   matt 	strcpy(cpu_model, mips_options.mips_cpu->cpu_name);
    271  1.2   matt 
    272  1.7   matt 	/* get system info from firmware */
    273  1.2   matt 	memsize = rmixlfw_init(infop);
    274  1.2   matt 
    275  1.2   matt 	/* set the VM page size */
    276  1.2   matt 	uvm_setpagesize();
    277  1.2   matt 
    278  1.2   matt 	physmem = btoc(memsize);
    279  1.2   matt 
    280  1.7   matt 	rmixl_obio_eb_bus_mem_init(&rcp->rc_obio_eb_memt, rcp);
    281  1.2   matt 
    282  1.2   matt #if NCOM > 0
    283  1.2   matt 	rmixl_com_cnattach(comcnaddr, comcnspeed, comcnfreq,
    284  1.2   matt 		COM_TYPE_NORMAL, comcnmode);
    285  1.2   matt #endif
    286  1.2   matt 
    287  1.2   matt 	printf("\nNetBSD/rmixl\n");
    288  1.7   matt 	printf("memsize = %#"PRIx64"\n", memsize);
    289  1.7   matt #ifdef MEMLIMIT
    290  1.7   matt 	printf("memlimit = %#"PRIx64"\n", (uint64_t)MEMLIMIT);
    291  1.7   matt #endif
    292  1.7   matt 
    293  1.7   matt #if defined(MULTIPROCESSOR) && defined(MACHDEP_DEBUG)
    294  1.7   matt 	rmixl_wakeup_info_print(rcp->rc_cpu_wakeup_info);
    295  1.7   matt 	rmixl_wakeup_info_print(rcp->rc_cpu_wakeup_info + 1);
    296  1.7   matt 	printf("cpu_wakeup_info %p, cpu_wakeup_end %p\n",
    297  1.7   matt 		rcp->rc_cpu_wakeup_info,
    298  1.7   matt 		rcp->rc_cpu_wakeup_end);
    299  1.7   matt 	printf("userapp_cpu_map: %#"PRIx64"\n",
    300  1.7   matt 		rcp->rc_psb_info.userapp_cpu_map);
    301  1.7   matt 	printf("wakeup: %#"PRIx64"\n", rcp->rc_psb_info.wakeup);
    302  1.7   matt {
    303  1.7   matt 	register_t sp;
    304  1.7   matt 	asm volatile ("move	%0, $sp\n" : "=r"(sp));
    305  1.7   matt 	printf("sp: %#"PRIx64"\n", sp);
    306  1.7   matt }
    307  1.7   matt #endif
    308  1.2   matt 
    309  1.2   matt 	rmixl_physaddr_init();
    310  1.2   matt 
    311  1.2   matt 	/*
    312  1.2   matt 	 * Obtain the cpu frequency
    313  1.2   matt 	 * Compute the number of ticks for hz.
    314  1.2   matt 	 * Compute the delay divisor.
    315  1.2   matt 	 * Double the Hz if this CPU runs at twice the
    316  1.2   matt          *  external/cp0-count frequency
    317  1.2   matt 	 */
    318  1.7   matt 	curcpu()->ci_cpu_freq = rcp->rc_psb_info.cpu_frequency;
    319  1.7   matt 	curcpu()->ci_cctr_freq = curcpu()->ci_cpu_freq;
    320  1.2   matt 	curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
    321  1.2   matt 	curcpu()->ci_divisor_delay =
    322  1.2   matt 		((curcpu()->ci_cpu_freq + 500000) / 1000000);
    323  1.7   matt         if (mips_options.mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
    324  1.2   matt 		curcpu()->ci_cpu_freq *= 2;
    325  1.2   matt 
    326  1.2   matt 	/*
    327  1.2   matt 	 * Look at arguments passed to us and compute boothowto.
    328  1.2   matt 	 * - rmixl firmware gives us a 32 bit argv[i], so adapt
    329  1.2   matt 	 *   by forcing sign extension in cast to (char *)
    330  1.2   matt 	 */
    331  1.2   matt 	boothowto = RB_AUTOBOOT;
    332  1.2   matt 	for (int i = 1; i < argc; i++) {
    333  1.2   matt 		for (char *cp = (char *)(intptr_t)argv[i]; *cp; cp++) {
    334  1.2   matt 			int howto;
    335  1.2   matt 			/* Ignore superfluous '-', if there is one */
    336  1.2   matt 			if (*cp == '-')
    337  1.2   matt 				continue;
    338  1.2   matt 
    339  1.2   matt 			howto = 0;
    340  1.2   matt 			BOOT_FLAG(*cp, howto);
    341  1.2   matt 			if (howto != 0)
    342  1.2   matt 				boothowto |= howto;
    343  1.2   matt #ifdef DIAGNOSTIC
    344  1.2   matt 			else
    345  1.2   matt 				printf("bootflag '%c' not recognised\n", *cp);
    346  1.2   matt #endif
    347  1.2   matt 		}
    348  1.2   matt 	}
    349  1.2   matt #ifdef DIAGNOSTIC
    350  1.2   matt 	printf("boothowto %#x\n", boothowto);
    351  1.2   matt #endif
    352  1.2   matt 
    353  1.2   matt 	/*
    354  1.2   matt 	 * Reserve pages from the VM system.
    355  1.7   matt 	 */
    356  1.2   matt 
    357  1.2   matt 	/* reserve 0..start..kernend pages */
    358  1.7   matt 	mem_cluster_cnt = ram_seg_resv(mem_clusters, mem_cluster_cnt,
    359  1.2   matt 		0, round_page(MIPS_KSEG0_TO_PHYS(kernend)));
    360  1.2   matt 
    361  1.2   matt 	/* reserve reset exception vector page */
    362  1.2   matt 	/* should never be in our clusters anyway... */
    363  1.7   matt 	mem_cluster_cnt = ram_seg_resv(mem_clusters, mem_cluster_cnt,
    364  1.7   matt 		0x1FC00000, 0x1FC00000+NBPG);
    365  1.7   matt 
    366  1.7   matt #ifdef MULTIPROCEESOR
    367  1.7   matt 	/* reserve the cpu_wakeup_info area */
    368  1.7   matt 	mem_cluster_cnt = ram_seg_resv(mem_clusters, mem_cluster_cnt,
    369  1.7   matt 		(u_quad_t)trunc_page(rcp->rc_cpu_wakeup_info),
    370  1.7   matt 		(u_quad_t)round_page(rcp->rc_cpu_wakeup_end));
    371  1.7   matt #endif
    372  1.7   matt 
    373  1.7   matt #ifdef MEMLIMIT
    374  1.7   matt 	/* reserve everything >= MEMLIMIT */
    375  1.7   matt 	mem_cluster_cnt = ram_seg_resv(mem_clusters, mem_cluster_cnt,
    376  1.7   matt 		(u_quad_t)MEMLIMIT, (u_quad_t)~0);
    377  1.7   matt #endif
    378  1.7   matt 
    379  1.7   matt 	/* get maximum RAM address from the VM clusters */
    380  1.7   matt 	mem_cluster_maxaddr = 0;
    381  1.7   matt 	for (u_int i=0; i < mem_cluster_cnt; i++) {
    382  1.7   matt 		u_quad_t tmp = round_page(
    383  1.7   matt 			mem_clusters[i].start + mem_clusters[i].size);
    384  1.7   matt 		if (tmp > mem_cluster_maxaddr)
    385  1.7   matt 			mem_cluster_maxaddr = tmp;
    386  1.7   matt 	}
    387  1.7   matt 	DPRINTF(("mem_cluster_maxaddr %#"PRIx64"\n", mem_cluster_maxaddr));
    388  1.2   matt 
    389  1.2   matt 	/*
    390  1.7   matt 	 * Load mem_clusters[] into the VM system.
    391  1.2   matt 	 */
    392  1.7   matt 	mips_page_physload(MIPS_KSEG0_START, (vaddr_t) kernend,
    393  1.7   matt 	    mem_clusters, mem_cluster_cnt, NULL, 0);
    394  1.2   matt 
    395  1.2   matt 	/*
    396  1.2   matt 	 * Initialize error message buffer (at end of core).
    397  1.2   matt 	 */
    398  1.2   matt 	mips_init_msgbuf();
    399  1.2   matt 
    400  1.2   matt 	pmap_bootstrap();
    401  1.2   matt 
    402  1.2   matt 	/*
    403  1.3  rmind 	 * Allocate uarea page for lwp0 and set it.
    404  1.2   matt 	 */
    405  1.3  rmind 	mips_init_lwp0_uarea();
    406  1.2   matt 
    407  1.2   matt #if defined(DDB)
    408  1.2   matt 	if (boothowto & RB_KDB)
    409  1.2   matt 		Debugger();
    410  1.2   matt #endif
    411  1.7   matt 	/*
    412  1.7   matt 	 * store (cpu#0) curcpu in COP0 OSSCRATCH0
    413  1.7   matt 	 * used in exception vector
    414  1.7   matt 	 */
    415  1.7   matt 	__asm __volatile("dmtc0 %0,$%1"
    416  1.7   matt 		:: "r"(&cpu_info_store), "n"(MIPS_COP_0_OSSCRATCH));
    417  1.9   matt #ifdef MULTIPROCESSOR
    418  1.7   matt 	mips_fixup_exceptions(rmixl_fixup_cop0_oscratch);
    419  1.7   matt #endif
    420  1.8   matt 	rmixl_fixup_curcpu();
    421  1.2   matt }
    422  1.2   matt 
    423  1.2   matt /*
    424  1.7   matt  * set up Processor Control Regs for this core
    425  1.7   matt  */
    426  1.7   matt void
    427  1.7   matt rmixl_pcr_init_core()
    428  1.7   matt {
    429  1.7   matt 	uint32_t r;
    430  1.7   matt 
    431  1.7   matt #ifdef MULTIPROCESSOR
    432  1.7   matt 	rmixl_mtcr(RMIXL_PCR_MMU_SETUP, __BITS(2,0));
    433  1.7   matt 						/* enable MMU clock gating */
    434  1.7   matt 						/* 4 threads active -- why needed if Global? */
    435  1.7   matt 						/* enable global TLB mode */
    436  1.7   matt #else
    437  1.7   matt 	rmixl_mtcr(RMIXL_PCR_THREADEN, 1);	/* disable all threads except #0 */
    438  1.7   matt 	rmixl_mtcr(RMIXL_PCR_MMU_SETUP, 0);	/* enable MMU clock gating */
    439  1.7   matt 						/* set single MMU Thread Mode */
    440  1.7   matt 						/* TLB is partitioned (1 partition) */
    441  1.7   matt #endif
    442  1.7   matt 
    443  1.7   matt 	r = rmixl_mfcr(RMIXL_PCR_L1D_CONFIG0);
    444  1.7   matt 	r &= ~__BIT(14);			/* disable Unaligned Access */
    445  1.7   matt 	rmixl_mtcr(RMIXL_PCR_L1D_CONFIG0, r);
    446  1.7   matt 
    447  1.7   matt #if defined(DDB) && defined(MIPS_DDB_WATCH)
    448  1.7   matt 	/*
    449  1.7   matt 	 * clear IEU_DEFEATURE[DBE]
    450  1.7   matt 	 * this enables COP0 watchpoint to trigger T_WATCH exception
    451  1.7   matt 	 * instead of signaling JTAG.
    452  1.7   matt 	 */
    453  1.7   matt 	r = rmixl_mfcr(RMIXL_PCR_IEU_DEFEATURE);
    454  1.7   matt 	r &= ~__BIT(7);
    455  1.7   matt 	rmixl_mtcr(RMIXL_PCR_IEU_DEFEATURE, r);
    456  1.7   matt #endif
    457  1.7   matt }
    458  1.7   matt 
    459  1.7   matt #ifdef MULTIPROCESSOR
    460  1.7   matt static bool
    461  1.7   matt rmixl_fixup_cop0_oscratch(int32_t load_addr, uint32_t new_insns[2])
    462  1.7   matt {
    463  1.7   matt 	size_t offset = load_addr - (intptr_t)&cpu_info_store;
    464  1.7   matt 
    465  1.7   matt 	KASSERT(MIPS_KSEG0_P(load_addr));
    466  1.7   matt 	KASSERT(offset < sizeof(struct cpu_info));
    467  1.7   matt 
    468  1.7   matt 	/*
    469  1.7   matt 	 * Fixup this direct load cpu_info_store to actually get the current
    470  1.7   matt 	 * CPU's cpu_info from COP0 OSSCRATCH0 and then fix the load to be
    471  1.7   matt 	 * relative from the start of struct cpu_info.
    472  1.7   matt 	 */
    473  1.7   matt 
    474  1.7   matt 	/* [0] = [d]mfc0 rX, $22 (OSScratch) */
    475  1.7   matt 	new_insns[0] = (020 << 26)
    476  1.7   matt #ifdef _LP64
    477  1.7   matt 	    | (1 << 21)		/* double move */
    478  1.7   matt #endif
    479  1.7   matt 	    | (new_insns[0] & 0x001f0000)
    480  1.7   matt 	    | (MIPS_COP_0_OSSCRATCH << 11) | (0 << 0);
    481  1.7   matt 
    482  1.7   matt 	/* [1] = [ls][dw] rX, offset(rX) */
    483  1.7   matt 	new_insns[1] = (new_insns[1] & 0xffff0000) | offset;
    484  1.7   matt 
    485  1.7   matt 	return true;
    486  1.7   matt }
    487  1.7   matt #endif /* MULTIPROCESSOR */
    488  1.7   matt 
    489  1.7   matt /*
    490  1.8   matt  * The following changes all	lX	rN, L_CPU(MIPS_CURLWP) [curlwp->l_cpu]
    491  1.8   matt  * to			     	[d]mfc0	rN, $22 [MIPS_COP_0_OSSCRATCH]
    492  1.8   matt  *
    493  1.8   matt  * the mfc0 is 3 cycles shorter than the load.
    494  1.8   matt  */
    495  1.8   matt #define	LOAD_CURCPU_0	((MIPS_CURLWP_REG << 21) | offsetof(lwp_t, l_cpu))
    496  1.8   matt #define	MFC0_CURCPU_0	((OP_COP0 << 26) | (MIPS_COP_0_OSSCRATCH << 11))
    497  1.8   matt #ifdef _LP64
    498  1.8   matt #define	LOAD_CURCPU	((uint32_t)(OP_LD << 26) | LOAD_CURCPU_0)
    499  1.8   matt #define	MFC0_CURCPU	((uint32_t)(OP_DMF << 21) | MFC0_CURCPU_0)
    500  1.8   matt #else
    501  1.8   matt #define	LOAD_CURCPU	((uint32_t)(OP_LW << 26) | LOAD_CURCPU_0)
    502  1.8   matt #define	MFC0_CURCPU	((uint32_t)(OP_MF << 21) | MFC0_CURCPU_0)
    503  1.8   matt #endif
    504  1.8   matt #define	LOAD_CURCPU_MASK	0xffe0ffff
    505  1.8   matt 
    506  1.8   matt static void
    507  1.8   matt rmixl_fixup_curcpu(void)
    508  1.8   matt {
    509  1.8   matt 	extern uint32_t _ftext[];
    510  1.8   matt 	extern uint32_t _etext[];
    511  1.8   matt 
    512  1.8   matt 	for (uint32_t *insnp = _ftext; insnp < _etext; insnp++) {
    513  1.8   matt 		const uint32_t insn = *insnp;
    514  1.8   matt 		if (__predict_false((insn & LOAD_CURCPU_MASK) == LOAD_CURCPU)) {
    515  1.8   matt 			/*
    516  1.8   matt 			 * Since the register to loaded is located in bits
    517  1.8   matt 			 * 16-20 for the mfc0 and the load instruction we can
    518  1.8   matt 			 * just change the instruction bits around it.
    519  1.8   matt 			 */
    520  1.8   matt 			*insnp = insn ^ LOAD_CURCPU ^ MFC0_CURCPU;
    521  1.8   matt 			mips_icache_sync_range((vaddr_t)insnp, 4);
    522  1.8   matt 		}
    523  1.8   matt 	}
    524  1.8   matt }
    525  1.8   matt 
    526  1.8   matt /*
    527  1.2   matt  * ram_seg_resv - cut reserved regions out of segs, fragmenting as needed
    528  1.2   matt  *
    529  1.2   matt  * we simply build a new table of segs, then copy it back over the given one
    530  1.2   matt  * this is inefficient but simple and called only a few times
    531  1.2   matt  *
    532  1.2   matt  * note: 'last' here means 1st addr past the end of the segment (start+size)
    533  1.2   matt  */
    534  1.2   matt static u_int
    535  1.2   matt ram_seg_resv(phys_ram_seg_t *segs, u_int nsegs,
    536  1.2   matt 	u_quad_t resv_first, u_quad_t resv_last)
    537  1.2   matt {
    538  1.2   matt         u_quad_t first, last;
    539  1.2   matt 	int new_nsegs=0;
    540  1.2   matt 	int resv_flag;
    541  1.2   matt 	phys_ram_seg_t new_segs[VM_PHYSSEG_MAX];
    542  1.2   matt 
    543  1.2   matt 	for (u_int i=0; i < nsegs; i++) {
    544  1.2   matt 		resv_flag = 0;
    545  1.2   matt 		first = trunc_page(segs[i].start);
    546  1.2   matt 		last = round_page(segs[i].start + segs[i].size);
    547  1.2   matt 
    548  1.2   matt 		KASSERT(new_nsegs < VM_PHYSSEG_MAX);
    549  1.2   matt 		if ((resv_first <= first) && (resv_last >= last)) {
    550  1.2   matt 			/* whole segment is resverved */
    551  1.2   matt 			continue;
    552  1.2   matt 		}
    553  1.2   matt 		if ((resv_first > first) && (resv_first < last)) {
    554  1.2   matt 			u_quad_t new_last;
    555  1.2   matt 
    556  1.2   matt 			/*
    557  1.2   matt 			 * reserved start in segment
    558  1.2   matt 			 * salvage the leading fragment
    559  1.2   matt 			 */
    560  1.2   matt 			resv_flag = 1;
    561  1.2   matt 			new_last = last - (last - resv_first);
    562  1.2   matt 			KASSERT (new_last > first);
    563  1.2   matt 			new_segs[new_nsegs].start = first;
    564  1.2   matt 			new_segs[new_nsegs].size = new_last - first;
    565  1.2   matt 			new_nsegs++;
    566  1.2   matt 		}
    567  1.2   matt 		if ((resv_last > first) && (resv_last < last)) {
    568  1.2   matt 			u_quad_t new_first;
    569  1.2   matt 
    570  1.2   matt 			/*
    571  1.2   matt 			 * reserved end in segment
    572  1.2   matt 			 * salvage the trailing fragment
    573  1.2   matt 			 */
    574  1.2   matt 			resv_flag = 1;
    575  1.2   matt 			new_first = first + (resv_last - first);
    576  1.2   matt 			KASSERT (last > (new_first + NBPG));
    577  1.2   matt 			new_segs[new_nsegs].start = new_first;
    578  1.2   matt 			new_segs[new_nsegs].size = last - new_first;
    579  1.2   matt 			new_nsegs++;
    580  1.2   matt 		}
    581  1.2   matt 		if (resv_flag == 0) {
    582  1.2   matt 			/*
    583  1.2   matt 			 * nothing reserved here, take it all
    584  1.2   matt 			 */
    585  1.2   matt 			new_segs[new_nsegs].start = first;
    586  1.2   matt 			new_segs[new_nsegs].size = last - first;
    587  1.2   matt 			new_nsegs++;
    588  1.2   matt 		}
    589  1.2   matt 
    590  1.2   matt 	}
    591  1.2   matt 
    592  1.2   matt 	memcpy(segs, new_segs, sizeof(new_segs));
    593  1.2   matt 
    594  1.2   matt 	return new_nsegs;
    595  1.2   matt }
    596  1.2   matt 
    597  1.2   matt /*
    598  1.2   matt  * create an extent for physical address space
    599  1.2   matt  * these are in units of MB for sake of compression (for sake of 32 bit kernels)
    600  1.2   matt  * allocate the regions where we have known functions (DRAM, IO, etc)
    601  1.2   matt  * what remains can be allocated as needed for other stuff
    602  1.2   matt  * e.g. to configure BARs that are not already initialized and enabled.
    603  1.2   matt  */
    604  1.2   matt static void
    605  1.2   matt rmixl_physaddr_init(void)
    606  1.2   matt {
    607  1.2   matt 	struct extent *ext;
    608  1.2   matt 	unsigned long start = 0UL;
    609  1.2   matt 	unsigned long end = (__BIT(40) / (1024 * 1024)) -1;
    610  1.2   matt 	u_long base;
    611  1.2   matt 	u_long size;
    612  1.2   matt 	uint32_t r;
    613  1.2   matt 
    614  1.2   matt 	ext = extent_create("physaddr", start, end, M_DEVBUF,
    615  1.2   matt 		(void *)rmixl_physaddr_storage, sizeof(rmixl_physaddr_storage),
    616  1.2   matt 		EX_NOWAIT | EX_NOCOALESCE);
    617  1.2   matt 
    618  1.2   matt 	if (ext == NULL)
    619  1.2   matt 		panic("%s: extent_create failed", __func__);
    620  1.2   matt 
    621  1.2   matt 	/*
    622  1.2   matt 	 * grab regions per DRAM BARs
    623  1.2   matt 	 */
    624  1.2   matt 	for (u_int i=0; i < RMIXL_SBC_DRAM_NBARS; i++) {
    625  1.2   matt 		r = RMIXL_IOREG_READ(RMIXL_SBC_DRAM_BAR(i));
    626  1.2   matt 		if ((r & RMIXL_DRAM_BAR_STATUS) == 0)
    627  1.2   matt 			continue;	/* not enabled */
    628  1.2   matt 		base = (u_long)(DRAM_BAR_TO_BASE((uint64_t)r) / (1024 * 1024));
    629  1.2   matt 		size = (u_long)(DRAM_BAR_TO_SIZE((uint64_t)r) / (1024 * 1024));
    630  1.2   matt 
    631  1.2   matt 		DPRINTF(("%s: %d: %d: 0x%08x -- 0x%010lx:%lu MB\n",
    632  1.2   matt 			__func__, __LINE__, i, r, base * (1024 * 1024), size));
    633  1.2   matt 		if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
    634  1.2   matt 			panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
    635  1.2   matt 				"failed", __func__, ext, base, size, EX_NOWAIT);
    636  1.2   matt 	}
    637  1.2   matt 
    638  1.2   matt 	/*
    639  1.7   matt 	 * get chip-dependent physaddr regions
    640  1.2   matt 	 */
    641  1.7   matt 	switch(cpu_rmixl_chip_type(mips_options.mips_cpu)) {
    642  1.7   matt 	case CIDFL_RMI_TYPE_XLR:
    643  1.7   matt #if NRMIXL_PCIX
    644  1.7   matt 		rmixl_physaddr_init_pcix(ext);
    645  1.7   matt #endif
    646  1.7   matt 		break;
    647  1.7   matt 	case CIDFL_RMI_TYPE_XLS:
    648  1.7   matt #if NRMIXL_PCIE
    649  1.7   matt 		rmixl_physaddr_init_pcie(ext);
    650  1.7   matt #endif
    651  1.7   matt 		break;
    652  1.7   matt 	case CIDFL_RMI_TYPE_XLP:
    653  1.7   matt 		/* XXX TBD */
    654  1.7   matt 		panic("%s: RMI XLP not yet supported", __func__);
    655  1.2   matt 	}
    656  1.2   matt 
    657  1.2   matt 	/*
    658  1.2   matt 	 *  at this point all regions left in "physaddr" extent
    659  1.2   matt 	 *  are unused holes in the physical adress space
    660  1.2   matt 	 *  available for use as needed.
    661  1.2   matt 	 */
    662  1.2   matt 	rmixl_configuration.rc_phys_ex = ext;
    663  1.2   matt #ifdef MACHDEP_DEBUG
    664  1.2   matt 	extent_print(ext);
    665  1.2   matt #endif
    666  1.2   matt }
    667  1.2   matt 
    668  1.7   matt static uint64_t
    669  1.2   matt rmixlfw_init(int64_t infop)
    670  1.2   matt {
    671  1.2   matt 	struct rmixl_config *rcp = &rmixl_configuration;
    672  1.2   matt 
    673  1.7   matt #ifdef MULTIPROCESSOR
    674  1.7   matt 	rmixl_get_wakeup_info(rcp);
    675  1.7   matt #endif
    676  1.2   matt 
    677  1.2   matt 	infop |= MIPS_KSEG0_START;
    678  1.7   matt 	rcp->rc_psb_info = *(rmixlfw_info_t *)(intptr_t)infop;
    679  1.2   matt 
    680  1.7   matt 	rcp->rc_psb_type = PSB_TYPE_UNKNOWN;
    681  1.2   matt 	for (int i=0; i < RMICLFW_PSB_VERSIONS_LEN; i++) {
    682  1.7   matt 		if (rmiclfw_psb_id[i].psb_version ==
    683  1.7   matt 		    rcp->rc_psb_info.psb_version) {
    684  1.7   matt 			rcp->rc_psb_type = rmiclfw_psb_id[i].psb_type;
    685  1.2   matt 			goto found;
    686  1.7   matt 		}
    687  1.2   matt 	}
    688  1.2   matt 
    689  1.2   matt 	rcp->rc_io_pbase = RMIXL_IO_DEV_PBASE;
    690  1.2   matt 	rmixl_putchar_init(rcp->rc_io_pbase);
    691  1.2   matt 
    692  1.2   matt #ifdef DIAGNOSTIC
    693  1.2   matt 	rmixl_puts("\r\nWARNING: untested psb_version: ");
    694  1.7   matt 	rmixl_puthex64(rcp->rc_psb_info.psb_version);
    695  1.2   matt 	rmixl_puts("\r\n");
    696  1.2   matt #endif
    697  1.2   matt 
    698  1.7   matt #ifdef MEMSIZE
    699  1.2   matt 	/* XXX trust and use MEMSIZE */
    700  1.2   matt 	mem_clusters[0].start = 0;
    701  1.2   matt 	mem_clusters[0].size = MEMSIZE;
    702  1.2   matt 	mem_cluster_cnt = 1;
    703  1.2   matt 	return MEMSIZE;
    704  1.7   matt #else
    705  1.7   matt 	rmixl_puts("\r\nERROR: configure MEMSIZE\r\n");
    706  1.7   matt 	cpu_reboot(RB_HALT, NULL);
    707  1.7   matt 	/* NOTREACHED */
    708  1.7   matt #endif
    709  1.2   matt 
    710  1.2   matt  found:
    711  1.7   matt 	rcp->rc_io_pbase = MIPS_KSEG1_TO_PHYS(rcp->rc_psb_info.io_base);
    712  1.2   matt 	rmixl_putchar_init(rcp->rc_io_pbase);
    713  1.2   matt #ifdef MACHDEP_DEBUG
    714  1.2   matt 	rmixl_puts("\r\ninfop: ");
    715  1.2   matt 	rmixl_puthex64((uint64_t)(intptr_t)infop);
    716  1.2   matt #endif
    717  1.2   matt #ifdef DIAGNOSTIC
    718  1.7   matt 	rmixl_puts("\r\nrecognized psb_version=");
    719  1.7   matt 	rmixl_puthex64(rcp->rc_psb_info.psb_version);
    720  1.7   matt 	rmixl_puts(", psb_type=");
    721  1.7   matt 	rmixl_puts(rmixlfw_psb_type_name(rcp->rc_psb_type));
    722  1.2   matt 	rmixl_puts("\r\n");
    723  1.2   matt #endif
    724  1.2   matt 
    725  1.2   matt 	return mem_clusters_init(
    726  1.7   matt 		(rmixlfw_mmap_t *)(intptr_t)rcp->rc_psb_info.psb_physaddr_map,
    727  1.7   matt 		(rmixlfw_mmap_t *)(intptr_t)rcp->rc_psb_info.avail_mem_map);
    728  1.2   matt }
    729  1.2   matt 
    730  1.2   matt void
    731  1.2   matt rmixlfw_mmap_print(rmixlfw_mmap_t *map)
    732  1.2   matt {
    733  1.2   matt #ifdef MACHDEP_DEBUG
    734  1.2   matt 	for (uint32_t i=0; i < map->nmmaps; i++) {
    735  1.2   matt 		rmixl_puthex32(i);
    736  1.2   matt 		rmixl_puts(", ");
    737  1.2   matt 		rmixl_puthex64(map->entry[i].start);
    738  1.2   matt 		rmixl_puts(", ");
    739  1.2   matt 		rmixl_puthex64(map->entry[i].size);
    740  1.2   matt 		rmixl_puts(", ");
    741  1.2   matt 		rmixl_puthex32(map->entry[i].type);
    742  1.2   matt 		rmixl_puts("\r\n");
    743  1.2   matt 	}
    744  1.2   matt #endif
    745  1.2   matt }
    746  1.2   matt 
    747  1.2   matt /*
    748  1.2   matt  * mem_clusters_init
    749  1.2   matt  *
    750  1.2   matt  * initialize mem_clusters[] table based on memory address mapping
    751  1.2   matt  * provided by boot firmware.
    752  1.2   matt  *
    753  1.2   matt  * prefer avail_mem_map if we can, otherwise use psb_physaddr_map.
    754  1.2   matt  * these will be limited by MEMSIZE if it is configured.
    755  1.2   matt  * if neither are available, just use MEMSIZE.
    756  1.2   matt  */
    757  1.7   matt static uint64_t
    758  1.2   matt mem_clusters_init(
    759  1.2   matt 	rmixlfw_mmap_t *psb_physaddr_map,
    760  1.2   matt 	rmixlfw_mmap_t *avail_mem_map)
    761  1.2   matt {
    762  1.2   matt 	rmixlfw_mmap_t *map = NULL;
    763  1.2   matt 	const char *mapname;
    764  1.2   matt 	uint64_t sz;
    765  1.2   matt 	uint64_t sum;
    766  1.2   matt 	u_int cnt;
    767  1.2   matt #ifdef MEMSIZE
    768  1.7   matt 	uint64_t memsize = MEMSIZE;
    769  1.2   matt #endif
    770  1.2   matt 
    771  1.2   matt #ifdef MACHDEP_DEBUG
    772  1.2   matt 	rmixl_puts("psb_physaddr_map: ");
    773  1.2   matt 	rmixl_puthex64((uint64_t)(intptr_t)psb_physaddr_map);
    774  1.2   matt 	rmixl_puts("\r\n");
    775  1.2   matt #endif
    776  1.2   matt 	if (psb_physaddr_map != NULL) {
    777  1.7   matt 		map = psb_physaddr_map;
    778  1.2   matt 		mapname = "psb_physaddr_map";
    779  1.2   matt 		rmixlfw_mmap_print(map);
    780  1.2   matt 	}
    781  1.2   matt #ifdef DIAGNOSTIC
    782  1.2   matt 	else {
    783  1.2   matt 		rmixl_puts("WARNING: no psb_physaddr_map\r\n");
    784  1.2   matt 	}
    785  1.2   matt #endif
    786  1.2   matt 
    787  1.2   matt #ifdef MACHDEP_DEBUG
    788  1.2   matt 	rmixl_puts("avail_mem_map: ");
    789  1.2   matt 	rmixl_puthex64((uint64_t)(intptr_t)avail_mem_map);
    790  1.2   matt 	rmixl_puts("\r\n");
    791  1.2   matt #endif
    792  1.2   matt 	if (avail_mem_map != NULL) {
    793  1.7   matt 		map = avail_mem_map;
    794  1.2   matt 		mapname = "avail_mem_map";
    795  1.2   matt 		rmixlfw_mmap_print(map);
    796  1.2   matt 	}
    797  1.2   matt #ifdef DIAGNOSTIC
    798  1.2   matt 	else {
    799  1.2   matt 		rmixl_puts("WARNING: no avail_mem_map\r\n");
    800  1.2   matt 	}
    801  1.2   matt #endif
    802  1.2   matt 
    803  1.2   matt 	if (map == NULL) {
    804  1.2   matt #ifndef MEMSIZE
    805  1.2   matt 		rmixl_puts("panic: no firmware memory map, "
    806  1.2   matt 			"must configure MEMSIZE\r\n");
    807  1.2   matt 		for(;;);	/* XXX */
    808  1.2   matt #else
    809  1.2   matt #ifdef DIAGNOSTIC
    810  1.2   matt 		rmixl_puts("WARNING: no avail_mem_map, "
    811  1.2   matt 			"using MEMSIZE\r\n");
    812  1.2   matt #endif
    813  1.2   matt 
    814  1.2   matt 		mem_clusters[0].start = 0;
    815  1.2   matt 		mem_clusters[0].size = MEMSIZE;
    816  1.2   matt 		mem_cluster_cnt = 1;
    817  1.2   matt 		return MEMSIZE;
    818  1.2   matt #endif	/* MEMSIZE */
    819  1.2   matt 	}
    820  1.2   matt 
    821  1.2   matt #ifdef DIAGNOSTIC
    822  1.2   matt 	rmixl_puts("using ");
    823  1.2   matt 	rmixl_puts(mapname);
    824  1.2   matt 	rmixl_puts("\r\n");
    825  1.2   matt #endif
    826  1.2   matt #ifdef MACHDEP_DEBUG
    827  1.2   matt 	rmixl_puts("memory clusters:\r\n");
    828  1.2   matt #endif
    829  1.2   matt 	sum = 0;
    830  1.2   matt 	cnt = 0;
    831  1.2   matt 	for (uint32_t i=0; i < map->nmmaps; i++) {
    832  1.2   matt 		if (map->entry[i].type != RMIXLFW_MMAP_TYPE_RAM)
    833  1.2   matt 			continue;
    834  1.2   matt 		mem_clusters[cnt].start = map->entry[i].start;
    835  1.2   matt 		sz = map->entry[i].size;
    836  1.2   matt 		sum += sz;
    837  1.2   matt 		mem_clusters[cnt].size = sz;
    838  1.2   matt #ifdef MACHDEP_DEBUG
    839  1.2   matt 		rmixl_puthex32(i);
    840  1.2   matt 		rmixl_puts(": ");
    841  1.2   matt 		rmixl_puthex64(mem_clusters[cnt].start);
    842  1.2   matt 		rmixl_puts(", ");
    843  1.2   matt 		rmixl_puthex64(sz);
    844  1.2   matt 		rmixl_puts(": ");
    845  1.2   matt 		rmixl_puthex64(sum);
    846  1.2   matt 		rmixl_puts("\r\n");
    847  1.2   matt #endif
    848  1.2   matt #ifdef MEMSIZE
    849  1.2   matt 		/*
    850  1.2   matt 		 * configurably limit memsize
    851  1.2   matt 		 */
    852  1.2   matt 		if (sum == memsize)
    853  1.2   matt 			break;
    854  1.2   matt 		if (sum > memsize) {
    855  1.7   matt 			uint64_t tmp;
    856  1.7   matt 
    857  1.2   matt 			tmp = sum - memsize;
    858  1.2   matt 			sz -= tmp;
    859  1.2   matt 			sum -= tmp;
    860  1.2   matt 			mem_clusters[cnt].size = sz;
    861  1.7   matt 			cnt++;
    862  1.2   matt 			break;
    863  1.2   matt 		}
    864  1.2   matt #endif
    865  1.2   matt 		cnt++;
    866  1.2   matt 	}
    867  1.2   matt 	mem_cluster_cnt = cnt;
    868  1.2   matt 	return sum;
    869  1.2   matt }
    870  1.2   matt 
    871  1.7   matt #ifdef MULTIPROCESSOR
    872  1.7   matt /*
    873  1.7   matt  * RMI firmware passes wakeup info structure in CP0 OS Scratch reg #7
    874  1.7   matt  * they do not explicitly give us the size of the wakeup area.
    875  1.7   matt  * we "know" that firmware loader sets wip->gp thusly:
    876  1.7   matt  *   gp = stack_start[vcpu] = round_page(wakeup_end) + (vcpu * (PAGE_SIZE * 2))
    877  1.7   matt  * so
    878  1.7   matt  *   round_page(wakeup_end) == gp - (vcpu * (PAGE_SIZE * 2))
    879  1.7   matt  * Only the "master" cpu runs this function, so
    880  1.7   matt  *   vcpu = wip->master_cpu
    881  1.7   matt  */
    882  1.7   matt void
    883  1.7   matt rmixl_get_wakeup_info(struct rmixl_config *rcp)
    884  1.7   matt {
    885  1.7   matt 	volatile rmixlfw_cpu_wakeup_info_t *wip;
    886  1.7   matt 	int32_t scratch_7;
    887  1.7   matt 	intptr_t end;
    888  1.7   matt 
    889  1.7   matt 	__asm__ volatile(
    890  1.7   matt 		".set push"				"\n"
    891  1.7   matt 		".set noreorder"			"\n"
    892  1.7   matt 		".set mips64"				"\n"
    893  1.7   matt 		"dmfc0	%0, $22, 7"			"\n"
    894  1.7   matt 		".set pop"				"\n"
    895  1.7   matt 			: "=r"(scratch_7));
    896  1.7   matt 
    897  1.7   matt 	wip = (volatile rmixlfw_cpu_wakeup_info_t *)
    898  1.7   matt 			(intptr_t)scratch_7;
    899  1.7   matt 	end = wip->entry.gp - (wip->master_cpu & (PAGE_SIZE * 2));;
    900  1.7   matt 
    901  1.7   matt 	if (wip->valid == 1) {
    902  1.7   matt 		rcp->rc_cpu_wakeup_end = (const void *)end;
    903  1.7   matt 		rcp->rc_cpu_wakeup_info = wip;
    904  1.7   matt 	}
    905  1.7   matt };
    906  1.7   matt 
    907  1.7   matt #ifdef MACHDEP_DEBUG
    908  1.7   matt static void
    909  1.7   matt rmixl_wakeup_info_print(volatile rmixlfw_cpu_wakeup_info_t *wip)
    910  1.7   matt {
    911  1.7   matt 	int i;
    912  1.7   matt 
    913  1.7   matt 	printf("%s: wip %p, size %lu\n", __func__, wip, sizeof(*wip));
    914  1.7   matt 
    915  1.7   matt 	printf("cpu_status %#x\n",  wip->cpu_status);
    916  1.7   matt 	printf("valid: %d\n", wip->valid);
    917  1.7   matt 	printf("entry: addr %#x, args %#x, sp %#"PRIx64", gp %#"PRIx64"\n",
    918  1.7   matt 		wip->entry.addr,
    919  1.7   matt 		wip->entry.args,
    920  1.7   matt 		wip->entry.sp,
    921  1.7   matt 		wip->entry.gp);
    922  1.7   matt 	printf("master_cpu %d\n", wip->master_cpu);
    923  1.7   matt 	printf("master_cpu_mask %#x\n", wip->master_cpu_mask);
    924  1.7   matt 	printf("buddy_cpu_mask %#x\n", wip->buddy_cpu_mask);
    925  1.7   matt 	printf("psb_os_cpu_map %#x\n", wip->psb_os_cpu_map);
    926  1.7   matt 	printf("argc %d\n", wip->argc);
    927  1.7   matt 	printf("argv:");
    928  1.7   matt 	for (i=0; i < wip->argc; i++)
    929  1.7   matt 		printf(" %#x", wip->argv[i]);
    930  1.7   matt 	printf("\n");
    931  1.7   matt 	printf("valid_tlb_entries %d\n", wip->valid_tlb_entries);
    932  1.7   matt 	printf("tlb_map:\n");
    933  1.7   matt 	for (i=0; i < wip->valid_tlb_entries; i++) {
    934  1.7   matt 		volatile const struct lib_cpu_tlb_mapping *m =
    935  1.7   matt 			&wip->tlb_map[i];
    936  1.7   matt 		printf(" %d", m->page_size);
    937  1.7   matt 		printf(", %d", m->asid);
    938  1.7   matt 		printf(", %d", m->coherency);
    939  1.7   matt 		printf(", %d", m->coherency);
    940  1.7   matt 		printf(", %d", m->attr);
    941  1.7   matt 		printf(", %#x", m->virt);
    942  1.7   matt 		printf(", %#"PRIx64"\n", m->phys);
    943  1.7   matt 	}
    944  1.7   matt 	printf("elf segs:\n");
    945  1.7   matt 	for (i=0; i < MAX_ELF_SEGMENTS; i++) {
    946  1.7   matt 		volatile const struct core_segment_info *e =
    947  1.7   matt 			&wip->seg_info[i];
    948  1.7   matt 		printf(" %#"PRIx64"", e->vaddr);
    949  1.7   matt 		printf(", %#"PRIx64"", e->memsz);
    950  1.7   matt 		printf(", %#x\n", e->flags);
    951  1.7   matt 	}
    952  1.7   matt 	printf("envc %d\n", wip->envc);
    953  1.7   matt 	for (i=0; i < wip->envc; i++)
    954  1.7   matt 		printf(" %#x \"%s\"", wip->envs[i],
    955  1.7   matt 			(char *)(intptr_t)(int32_t)(wip->envs[i]));
    956  1.7   matt 	printf("\n");
    957  1.7   matt 	printf("app_mode %d\n", wip->app_mode);
    958  1.7   matt 	printf("printk_lock %#x\n", wip->printk_lock);
    959  1.7   matt 	printf("kseg_master %d\n", wip->kseg_master);
    960  1.7   matt 	printf("kuseg_reentry_function %#x\n", wip->kuseg_reentry_function);
    961  1.7   matt 	printf("kuseg_reentry_args %#x\n", wip->kuseg_reentry_args);
    962  1.7   matt 	printf("app_shared_mem_addr %#"PRIx64"\n", wip->app_shared_mem_addr);
    963  1.7   matt 	printf("app_shared_mem_size %#"PRIx64"\n", wip->app_shared_mem_size);
    964  1.7   matt 	printf("app_shared_mem_orig %#"PRIx64"\n", wip->app_shared_mem_orig);
    965  1.7   matt 	printf("loader_lock %#x\n", wip->loader_lock);
    966  1.7   matt 	printf("global_wakeup_mask %#x\n", wip->global_wakeup_mask);
    967  1.7   matt 	printf("unused_0 %#x\n", wip->unused_0);
    968  1.7   matt }
    969  1.7   matt #endif	/* MACHDEP_DEBUG */
    970  1.7   matt #endif 	/* MULTIPROCESSOR */
    971  1.7   matt 
    972  1.2   matt void
    973  1.2   matt consinit(void)
    974  1.2   matt {
    975  1.2   matt 
    976  1.2   matt 	/*
    977  1.2   matt 	 * Everything related to console initialization is done
    978  1.2   matt 	 * in mach_init().
    979  1.2   matt 	 */
    980  1.2   matt }
    981  1.2   matt 
    982  1.2   matt /*
    983  1.2   matt  * Allocate memory for variable-sized tables,
    984  1.2   matt  */
    985  1.2   matt void
    986  1.2   matt cpu_startup()
    987  1.2   matt {
    988  1.2   matt 	vaddr_t minaddr, maxaddr;
    989  1.2   matt 	char pbuf[9];
    990  1.2   matt 
    991  1.2   matt 	/*
    992  1.2   matt 	 * Good {morning,afternoon,evening,night}.
    993  1.2   matt 	 */
    994  1.2   matt 	printf("%s%s", copyright, version);
    995  1.7   matt 	format_bytes(pbuf, sizeof(pbuf), ctob((uint64_t)physmem));
    996  1.2   matt 	printf("total memory = %s\n", pbuf);
    997  1.2   matt 
    998  1.2   matt 	/*
    999  1.2   matt 	 * Virtual memory is bootstrapped -- notify the bus spaces
   1000  1.2   matt 	 * that memory allocation is now safe.
   1001  1.2   matt 	 */
   1002  1.2   matt 	rmixl_configuration.rc_mallocsafe = 1;
   1003  1.2   matt 
   1004  1.2   matt 	minaddr = 0;
   1005  1.2   matt 	/*
   1006  1.2   matt 	 * Allocate a submap for physio.
   1007  1.2   matt 	 */
   1008  1.2   matt 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
   1009  1.2   matt 				    VM_PHYS_SIZE, 0, FALSE, NULL);
   1010  1.2   matt 
   1011  1.2   matt 	/*
   1012  1.2   matt 	 * (No need to allocate an mbuf cluster submap.  Mbuf clusters
   1013  1.2   matt 	 * are allocated via the pool allocator, and we use XKSEG to
   1014  1.2   matt 	 * map those pages.)
   1015  1.2   matt 	 */
   1016  1.2   matt 
   1017  1.2   matt 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
   1018  1.2   matt 	printf("avail memory = %s\n", pbuf);
   1019  1.2   matt }
   1020  1.2   matt 
   1021  1.2   matt int	waittime = -1;
   1022  1.2   matt 
   1023  1.2   matt void
   1024  1.3  rmind cpu_reboot(int howto, char *bootstr)
   1025  1.2   matt {
   1026  1.2   matt 
   1027  1.2   matt 	/* Take a snapshot before clobbering any registers. */
   1028  1.7   matt 	savectx(curpcb);
   1029  1.2   matt 
   1030  1.2   matt 	if (cold) {
   1031  1.2   matt 		howto |= RB_HALT;
   1032  1.2   matt 		goto haltsys;
   1033  1.2   matt 	}
   1034  1.2   matt 
   1035  1.2   matt 	/* If "always halt" was specified as a boot flag, obey. */
   1036  1.2   matt 	if (boothowto & RB_HALT)
   1037  1.2   matt 		howto |= RB_HALT;
   1038  1.2   matt 
   1039  1.2   matt 	boothowto = howto;
   1040  1.2   matt 	if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
   1041  1.2   matt 		waittime = 0;
   1042  1.2   matt 		vfs_shutdown();
   1043  1.2   matt 
   1044  1.2   matt 		/*
   1045  1.2   matt 		 * If we've been adjusting the clock, the todr
   1046  1.2   matt 		 * will be out of synch; adjust it now.
   1047  1.2   matt 		 */
   1048  1.2   matt 		resettodr();
   1049  1.2   matt 	}
   1050  1.2   matt 
   1051  1.2   matt 	splhigh();
   1052  1.2   matt 
   1053  1.2   matt 	if (howto & RB_DUMP)
   1054  1.2   matt 		dumpsys();
   1055  1.2   matt 
   1056  1.2   matt haltsys:
   1057  1.2   matt 	doshutdownhooks();
   1058  1.2   matt 
   1059  1.2   matt 	if (howto & RB_HALT) {
   1060  1.2   matt 		printf("\n");
   1061  1.2   matt 		printf("The operating system has halted.\n");
   1062  1.2   matt 		printf("Please press any key to reboot.\n\n");
   1063  1.2   matt 		cnpollc(1);	/* For proper keyboard command handling */
   1064  1.2   matt 		cngetc();
   1065  1.2   matt 		cnpollc(0);
   1066  1.2   matt 	}
   1067  1.2   matt 
   1068  1.2   matt 	printf("rebooting...\n\n");
   1069  1.2   matt 
   1070  1.7   matt 	rmixl_reset();
   1071  1.2   matt }
   1072  1.2   matt 
   1073  1.2   matt /*
   1074  1.2   matt  * goodbye world
   1075  1.2   matt  */
   1076  1.2   matt void __attribute__((__noreturn__))
   1077  1.7   matt rmixl_reset(void)
   1078  1.2   matt {
   1079  1.7   matt 	uint32_t r;
   1080  1.7   matt 
   1081  1.7   matt 	r = RMIXL_IOREG_READ(RMIXL_IO_DEV_GPIO + RMIXL_GPIO_RESET);
   1082  1.7   matt 	r |= RMIXL_GPIO_RESET_RESET;
   1083  1.7   matt 	RMIXL_IOREG_WRITE(RMIXL_IO_DEV_GPIO + RMIXL_GPIO_RESET, r);
   1084  1.7   matt 
   1085  1.7   matt 	printf("soft reset failed, spinning...\n");
   1086  1.2   matt 	for (;;);
   1087  1.2   matt }
   1088