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