1 1.4 andvar /* $NetBSD: cpu.c,v 1.4 2023/12/05 19:16:48 andvar 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.4 andvar __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.4 2023/12/05 19:16:48 andvar 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.2 mrg #include <evbmips/sbmips/cpuvar.h> 50 1.2 mrg #include <evbmips/sbmips/systemsw.h> 51 1.1 mrg 52 1.1 mrg #include <mips/sibyte/include/zbbusvar.h> 53 1.1 mrg #include <mips/sibyte/include/sb1250_regs.h> 54 1.1 mrg #include <mips/sibyte/include/sb1250_scd.h> 55 1.1 mrg #include <mips/sibyte/dev/sbscdvar.h> 56 1.1 mrg #include <mips/cfe/cfe_api.h> 57 1.1 mrg 58 1.1 mrg #define READ_REG(rp) mips3_ld((register_t)(rp)) 59 1.1 mrg 60 1.1 mrg static int cpu_match(device_t, cfdata_t, void *); 61 1.1 mrg static void cpu_attach(device_t, device_t, void *); 62 1.1 mrg 63 1.1 mrg CFATTACH_DECL_NEW(cpu, sizeof(struct cpu_softc), 64 1.1 mrg cpu_match, cpu_attach, NULL, NULL); 65 1.1 mrg 66 1.1 mrg static u_int found = 0; 67 1.1 mrg 68 1.1 mrg static int 69 1.1 mrg cpu_match(device_t parent, cfdata_t match, void *aux) 70 1.1 mrg { 71 1.1 mrg struct zbbus_attach_args *zap = aux; 72 1.1 mrg int part; 73 1.1 mrg 74 1.1 mrg if (zap->za_locs.za_type != ZBBUS_ENTTYPE_CPU) 75 1.1 mrg return (0); 76 1.1 mrg 77 1.1 mrg /* 78 1.1 mrg * The 3rd hex digit of the part number is the number of CPUs; 79 1.1 mrg * ref Table 26, p38 1250-UM101-R. 80 1.1 mrg */ 81 1.1 mrg part = G_SYS_PART(READ_REG(MIPS_PHYS_TO_KSEG1(A_SCD_SYSTEM_REVISION))); 82 1.1 mrg return (found < ((part >> 8) & 0xf)); 83 1.1 mrg } 84 1.1 mrg 85 1.1 mrg static void 86 1.1 mrg cpu_attach(device_t parent, device_t self, void *aux) 87 1.1 mrg { 88 1.1 mrg struct cpu_info *ci; 89 1.1 mrg struct cpu_softc * const cpu = device_private(self); 90 1.1 mrg const char * const xname = device_xname(self); 91 1.1 mrg uint32_t config; 92 1.1 mrg int plldiv; 93 1.1 mrg 94 1.1 mrg found++; 95 1.1 mrg 96 1.1 mrg /* XXX this code must run on the target CPU */ 97 1.1 mrg config = mips3_cp0_config_read(); 98 1.1 mrg __USE(config); 99 1.1 mrg KASSERT((config & MIPS3_CONFIG_K0_MASK) == 5); 100 1.1 mrg 101 1.1 mrg /* Determine CPU frequency */ 102 1.1 mrg 103 1.1 mrg /* XXX: We should determine the CPU frequency from a time source 104 1.1 mrg * not coupled with the CPU crystal, like the RTC. Unfortunately 105 1.1 mrg * we don't attach that yet... 106 1.1 mrg */ 107 1.1 mrg plldiv = G_SYS_PLL_DIV(READ_REG(MIPS_PHYS_TO_KSEG1(A_SCD_SYSTEM_CFG))); 108 1.1 mrg if (plldiv == 0) { 109 1.1 mrg aprint_normal(": PLL_DIV of zero found, assuming 6 (300MHz)\n"); 110 1.1 mrg plldiv = 6; 111 1.1 mrg 112 1.1 mrg aprint_normal("%s", xname); 113 1.1 mrg } 114 1.1 mrg 115 1.1 mrg if (found == 1) { 116 1.1 mrg ci = curcpu(); 117 1.1 mrg ci->ci_cpu_freq = 50000000 * plldiv; 118 1.1 mrg /* Compute the delay divisor. */ 119 1.1 mrg ci->ci_divisor_delay = (ci->ci_cpu_freq + 500000) / 1000000; 120 1.1 mrg /* Compute clock cycles per hz */ 121 1.1 mrg ci->ci_cycles_per_hz = (ci->ci_cpu_freq + hz / 2 ) / hz; 122 1.1 mrg 123 1.1 mrg aprint_normal(": %lu.%02luMHz (hz cycles = %lu, delay divisor = %lu)\n", 124 1.1 mrg ci->ci_cpu_freq / 1000000, 125 1.1 mrg (ci->ci_cpu_freq % 1000000) / 10000, 126 1.1 mrg ci->ci_cycles_per_hz, ci->ci_divisor_delay); 127 1.1 mrg 128 1.1 mrg KASSERT(ci->ci_cpuid == 0); 129 1.1 mrg 130 1.1 mrg cpu->sb1cpu_dev = self; 131 1.1 mrg cpu->sb1cpu_ci = ci; 132 1.1 mrg ci->ci_softc = cpu; 133 1.1 mrg 134 1.1 mrg sb1250_cpu_init(cpu); 135 1.1 mrg } else { 136 1.1 mrg #if defined(MULTIPROCESSOR) 137 1.1 mrg int status; 138 1.1 mrg ci = cpu_info_alloc(NULL, found - 1, 0, found - 1, 0); 139 1.1 mrg KASSERT(ci); 140 1.1 mrg 141 1.1 mrg cpu->sb1cpu_dev = self; 142 1.1 mrg cpu->sb1cpu_ci = ci; 143 1.1 mrg ci->ci_softc = cpu; 144 1.1 mrg 145 1.1 mrg sb1250_cpu_init(cpu); 146 1.1 mrg 147 1.1 mrg status = cfe_cpu_start(ci->ci_cpuid, cpu_trampoline, 148 1.1 mrg (long) ci->ci_data.cpu_idlelwp->l_md.md_utf, 0, 149 1.1 mrg (long) ci); 150 1.1 mrg if (status != 0) { 151 1.1 mrg aprint_error(": CFE call to start failed: %d\n", 152 1.1 mrg status); 153 1.1 mrg } 154 1.1 mrg for (size_t i = 0; i < 10000; i++) { 155 1.4 andvar if (kcpuset_isset(cpus_hatched, cpu_index(ci))) 156 1.1 mrg break; 157 1.1 mrg DELAY(100); 158 1.1 mrg } 159 1.4 andvar if (!kcpuset_isset(cpus_hatched, cpu_index(ci))) { 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.3 simonb aprint_normal("\n"); 165 1.1 mrg aprint_normal_dev(self, 166 1.1 mrg "processor off-line; " 167 1.1 mrg "multiprocessor support not present in kernel\n"); 168 1.1 mrg return; 169 1.1 mrg #endif 170 1.1 mrg } 171 1.1 mrg 172 1.1 mrg /* 173 1.1 mrg * Announce ourselves. 174 1.1 mrg */ 175 1.1 mrg aprint_normal("%s: ", xname); 176 1.1 mrg cpu_identify(self); 177 1.1 mrg 178 1.1 mrg cpu_attach_common(self, ci); 179 1.1 mrg } 180