Home | History | Annotate | Line # | Download | only in sbmips
cpu.c revision 1.1
      1  1.1  mrg /* $NetBSD: cpu.c,v 1.1 2017/07/24 08:56:29 mrg Exp $ */
      2  1.1  mrg 
      3  1.1  mrg /*
      4  1.1  mrg  * Copyright 2000, 2001
      5  1.1  mrg  * Broadcom Corporation. All rights reserved.
      6  1.1  mrg  *
      7  1.1  mrg  * This software is furnished under license and may be used and copied only
      8  1.1  mrg  * in accordance with the following terms and conditions.  Subject to these
      9  1.1  mrg  * conditions, you may download, copy, install, use, modify and distribute
     10  1.1  mrg  * modified or unmodified copies of this software in source and/or binary
     11  1.1  mrg  * form. No title or ownership is transferred hereby.
     12  1.1  mrg  *
     13  1.1  mrg  * 1) Any source code used, modified or distributed must reproduce and
     14  1.1  mrg  *    retain this copyright notice and list of conditions as they appear in
     15  1.1  mrg  *    the source file.
     16  1.1  mrg  *
     17  1.1  mrg  * 2) No right is granted to use any trade name, trademark, or logo of
     18  1.1  mrg  *    Broadcom Corporation.  The "Broadcom Corporation" name may not be
     19  1.1  mrg  *    used to endorse or promote products derived from this software
     20  1.1  mrg  *    without the prior written permission of Broadcom Corporation.
     21  1.1  mrg  *
     22  1.1  mrg  * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR IMPLIED
     23  1.1  mrg  *    WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES OF
     24  1.1  mrg  *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
     25  1.1  mrg  *    NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM BE LIABLE
     26  1.1  mrg  *    FOR ANY DAMAGES WHATSOEVER, AND IN PARTICULAR, BROADCOM SHALL NOT BE
     27  1.1  mrg  *    LIABLE FOR DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  1.1  mrg  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  1.1  mrg  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     30  1.1  mrg  *    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     31  1.1  mrg  *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     32  1.1  mrg  *    OR OTHERWISE), EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  1.1  mrg  */
     34  1.1  mrg 
     35  1.1  mrg #include <sys/cdefs.h>
     36  1.1  mrg __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.1 2017/07/24 08:56:29 mrg Exp $");
     37  1.1  mrg 
     38  1.1  mrg #include "opt_multiprocessor.h"
     39  1.1  mrg 
     40  1.1  mrg #include <sys/param.h>
     41  1.1  mrg #include <sys/cpu.h>
     42  1.1  mrg #include <sys/device.h>
     43  1.1  mrg #include <sys/kernel.h>
     44  1.1  mrg #include <sys/systm.h>
     45  1.1  mrg 
     46  1.1  mrg #include <mips/locore.h>
     47  1.1  mrg #include <mips/cache.h>
     48  1.1  mrg 
     49  1.1  mrg #include <sbmips/cpuvar.h>
     50  1.1  mrg 
     51  1.1  mrg #include <mips/sibyte/include/zbbusvar.h>
     52  1.1  mrg #include <mips/sibyte/include/sb1250_regs.h>
     53  1.1  mrg #include <mips/sibyte/include/sb1250_scd.h>
     54  1.1  mrg #include <mips/sibyte/dev/sbscdvar.h>
     55  1.1  mrg #include <mips/cfe/cfe_api.h>
     56  1.1  mrg 
     57  1.1  mrg #define	READ_REG(rp)		mips3_ld((register_t)(rp))
     58  1.1  mrg 
     59  1.1  mrg static int	cpu_match(device_t, cfdata_t, void *);
     60  1.1  mrg static void	cpu_attach(device_t, device_t, void *);
     61  1.1  mrg 
     62  1.1  mrg CFATTACH_DECL_NEW(cpu, sizeof(struct cpu_softc),
     63  1.1  mrg     cpu_match, cpu_attach, NULL, NULL);
     64  1.1  mrg 
     65  1.1  mrg static u_int found = 0;
     66  1.1  mrg 
     67  1.1  mrg static int
     68  1.1  mrg cpu_match(device_t parent, cfdata_t match, void *aux)
     69  1.1  mrg {
     70  1.1  mrg 	struct zbbus_attach_args *zap = aux;
     71  1.1  mrg 	int part;
     72  1.1  mrg 
     73  1.1  mrg 	if (zap->za_locs.za_type != ZBBUS_ENTTYPE_CPU)
     74  1.1  mrg 		return (0);
     75  1.1  mrg 
     76  1.1  mrg 	/*
     77  1.1  mrg 	 * The 3rd hex digit of the part number is the number of CPUs;
     78  1.1  mrg 	 * ref Table 26, p38 1250-UM101-R.
     79  1.1  mrg 	 */
     80  1.1  mrg 	part = G_SYS_PART(READ_REG(MIPS_PHYS_TO_KSEG1(A_SCD_SYSTEM_REVISION)));
     81  1.1  mrg 	return (found < ((part >> 8) & 0xf));
     82  1.1  mrg }
     83  1.1  mrg 
     84  1.1  mrg static void
     85  1.1  mrg cpu_attach(device_t parent, device_t self, void *aux)
     86  1.1  mrg {
     87  1.1  mrg 	struct cpu_info *ci;
     88  1.1  mrg 	struct cpu_softc * const cpu = device_private(self);
     89  1.1  mrg 	const char * const xname = device_xname(self);
     90  1.1  mrg 	uint32_t config;
     91  1.1  mrg 	int plldiv;
     92  1.1  mrg 
     93  1.1  mrg 	found++;
     94  1.1  mrg 
     95  1.1  mrg 	/* XXX this code must run on the target CPU */
     96  1.1  mrg 	config = mips3_cp0_config_read();
     97  1.1  mrg 	__USE(config);
     98  1.1  mrg 	KASSERT((config & MIPS3_CONFIG_K0_MASK) == 5);
     99  1.1  mrg 
    100  1.1  mrg 	/* Determine CPU frequency */
    101  1.1  mrg 
    102  1.1  mrg 	/* XXX:  We should determine the CPU frequency from a time source
    103  1.1  mrg 	 *       not coupled with the CPU crystal, like the RTC.  Unfortunately
    104  1.1  mrg 	 *       we don't attach that yet...
    105  1.1  mrg 	 */
    106  1.1  mrg 	plldiv = G_SYS_PLL_DIV(READ_REG(MIPS_PHYS_TO_KSEG1(A_SCD_SYSTEM_CFG)));
    107  1.1  mrg 	if (plldiv == 0) {
    108  1.1  mrg 		aprint_normal(": PLL_DIV of zero found, assuming 6 (300MHz)\n");
    109  1.1  mrg 		plldiv = 6;
    110  1.1  mrg 
    111  1.1  mrg 		aprint_normal("%s", xname);
    112  1.1  mrg 	}
    113  1.1  mrg 
    114  1.1  mrg 	if (found == 1) {
    115  1.1  mrg 		ci = curcpu();
    116  1.1  mrg 		ci->ci_cpu_freq = 50000000 * plldiv;
    117  1.1  mrg 		/* Compute the delay divisor. */
    118  1.1  mrg 		ci->ci_divisor_delay = (ci->ci_cpu_freq + 500000) / 1000000;
    119  1.1  mrg 		/* Compute clock cycles per hz */
    120  1.1  mrg 		ci->ci_cycles_per_hz = (ci->ci_cpu_freq + hz / 2 ) / hz;
    121  1.1  mrg 
    122  1.1  mrg 		aprint_normal(": %lu.%02luMHz (hz cycles = %lu, delay divisor = %lu)\n",
    123  1.1  mrg 		    ci->ci_cpu_freq / 1000000,
    124  1.1  mrg 		    (ci->ci_cpu_freq % 1000000) / 10000,
    125  1.1  mrg 		    ci->ci_cycles_per_hz, ci->ci_divisor_delay);
    126  1.1  mrg 
    127  1.1  mrg 		KASSERT(ci->ci_cpuid == 0);
    128  1.1  mrg 
    129  1.1  mrg 		cpu->sb1cpu_dev = self;
    130  1.1  mrg 		cpu->sb1cpu_ci = ci;
    131  1.1  mrg 		ci->ci_softc = cpu;
    132  1.1  mrg 
    133  1.1  mrg 		sb1250_cpu_init(cpu);
    134  1.1  mrg 	} else {
    135  1.1  mrg #if defined(MULTIPROCESSOR)
    136  1.1  mrg 		int status;
    137  1.1  mrg 		ci = cpu_info_alloc(NULL, found - 1, 0, found - 1, 0);
    138  1.1  mrg 		KASSERT(ci);
    139  1.1  mrg 
    140  1.1  mrg 		cpu->sb1cpu_dev = self;
    141  1.1  mrg 		cpu->sb1cpu_ci = ci;
    142  1.1  mrg 		ci->ci_softc = cpu;
    143  1.1  mrg 
    144  1.1  mrg 		sb1250_cpu_init(cpu);
    145  1.1  mrg 
    146  1.1  mrg 		status = cfe_cpu_start(ci->ci_cpuid, cpu_trampoline,
    147  1.1  mrg 		    (long) ci->ci_data.cpu_idlelwp->l_md.md_utf, 0,
    148  1.1  mrg 		    (long) ci);
    149  1.1  mrg 		if (status != 0) {
    150  1.1  mrg 			aprint_error(": CFE call to start failed: %d\n",
    151  1.1  mrg 			    status);
    152  1.1  mrg 		}
    153  1.1  mrg 		const u_long cpu_mask = 1L << cpu_index(ci);
    154  1.1  mrg 		for (size_t i = 0; i < 10000; i++) {
    155  1.1  mrg 			if (cpus_hatched & cpu_mask)
    156  1.1  mrg 				 break;
    157  1.1  mrg 			DELAY(100);
    158  1.1  mrg 		}
    159  1.1  mrg 		if ((cpus_hatched & cpu_mask) == 0) {
    160  1.1  mrg 			aprint_error(": failed to hatch!\n");
    161  1.1  mrg 			return;
    162  1.1  mrg 		}
    163  1.1  mrg #else
    164  1.1  mrg 		aprint_normal_dev(self,
    165  1.1  mrg 		    "processor off-line; "
    166  1.1  mrg 		    "multiprocessor support not present in kernel\n");
    167  1.1  mrg 		return;
    168  1.1  mrg #endif
    169  1.1  mrg 	}
    170  1.1  mrg 
    171  1.1  mrg 	/*
    172  1.1  mrg 	 * Announce ourselves.
    173  1.1  mrg 	 */
    174  1.1  mrg 	aprint_normal("%s: ", xname);
    175  1.1  mrg 	cpu_identify(self);
    176  1.1  mrg 
    177  1.1  mrg 	cpu_attach_common(self, ci);
    178  1.1  mrg }
    179