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