cpu.c revision 1.21 1 1.21 ad /* $NetBSD: cpu.c,v 1.21 2008/05/11 15:02:34 ad Exp $ */
2 1.2 bouyer /* NetBSD: cpu.c,v 1.18 2004/02/20 17:35:01 yamt Exp */
3 1.2 bouyer
4 1.2 bouyer /*-
5 1.2 bouyer * Copyright (c) 2000 The NetBSD Foundation, Inc.
6 1.19 joerg * Copyright (c) 2002, 2006, 2007 YAMAMOTO Takashi,
7 1.2 bouyer * All rights reserved.
8 1.2 bouyer *
9 1.2 bouyer * This code is derived from software contributed to The NetBSD Foundation
10 1.2 bouyer * by RedBack Networks Inc.
11 1.2 bouyer *
12 1.2 bouyer * Author: Bill Sommerfeld
13 1.2 bouyer *
14 1.2 bouyer * Redistribution and use in source and binary forms, with or without
15 1.2 bouyer * modification, are permitted provided that the following conditions
16 1.2 bouyer * are met:
17 1.2 bouyer * 1. Redistributions of source code must retain the above copyright
18 1.2 bouyer * notice, this list of conditions and the following disclaimer.
19 1.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
20 1.2 bouyer * notice, this list of conditions and the following disclaimer in the
21 1.2 bouyer * documentation and/or other materials provided with the distribution.
22 1.2 bouyer *
23 1.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 1.2 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 1.2 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 1.2 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 1.2 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 1.2 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 1.2 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 1.2 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 1.2 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 1.2 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.2 bouyer * POSSIBILITY OF SUCH DAMAGE.
34 1.2 bouyer */
35 1.2 bouyer
36 1.2 bouyer /*
37 1.2 bouyer * Copyright (c) 1999 Stefan Grefen
38 1.2 bouyer *
39 1.2 bouyer * Redistribution and use in source and binary forms, with or without
40 1.2 bouyer * modification, are permitted provided that the following conditions
41 1.2 bouyer * are met:
42 1.2 bouyer * 1. Redistributions of source code must retain the above copyright
43 1.2 bouyer * notice, this list of conditions and the following disclaimer.
44 1.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
45 1.2 bouyer * notice, this list of conditions and the following disclaimer in the
46 1.2 bouyer * documentation and/or other materials provided with the distribution.
47 1.2 bouyer * 3. All advertising materials mentioning features or use of this software
48 1.2 bouyer * must display the following acknowledgement:
49 1.2 bouyer * This product includes software developed by the NetBSD
50 1.2 bouyer * Foundation, Inc. and its contributors.
51 1.2 bouyer * 4. Neither the name of The NetBSD Foundation nor the names of its
52 1.2 bouyer * contributors may be used to endorse or promote products derived
53 1.2 bouyer * from this software without specific prior written permission.
54 1.2 bouyer *
55 1.2 bouyer * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
56 1.2 bouyer * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 1.2 bouyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 1.2 bouyer * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS BE LIABLE
59 1.2 bouyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 1.2 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 1.2 bouyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 1.2 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 1.2 bouyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 1.2 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 1.2 bouyer * SUCH DAMAGE.
66 1.2 bouyer */
67 1.2 bouyer
68 1.2 bouyer #include <sys/cdefs.h>
69 1.21 ad __KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.21 2008/05/11 15:02:34 ad Exp $");
70 1.2 bouyer
71 1.2 bouyer #include "opt_ddb.h"
72 1.2 bouyer #include "opt_multiprocessor.h"
73 1.2 bouyer #include "opt_mpbios.h" /* for MPDEBUG */
74 1.2 bouyer #include "opt_mtrr.h"
75 1.2 bouyer #include "opt_xen.h"
76 1.2 bouyer
77 1.2 bouyer #include "lapic.h"
78 1.2 bouyer #include "ioapic.h"
79 1.2 bouyer
80 1.2 bouyer #include <sys/param.h>
81 1.2 bouyer #include <sys/proc.h>
82 1.2 bouyer #include <sys/user.h>
83 1.2 bouyer #include <sys/systm.h>
84 1.2 bouyer #include <sys/device.h>
85 1.2 bouyer #include <sys/malloc.h>
86 1.11 cegger #include <sys/cpu.h>
87 1.11 cegger #include <sys/atomic.h>
88 1.2 bouyer
89 1.2 bouyer #include <uvm/uvm_extern.h>
90 1.2 bouyer
91 1.2 bouyer #include <machine/cpufunc.h>
92 1.2 bouyer #include <machine/cpuvar.h>
93 1.2 bouyer #include <machine/pmap.h>
94 1.2 bouyer #include <machine/vmparam.h>
95 1.2 bouyer #include <machine/mpbiosvar.h>
96 1.2 bouyer #include <machine/pcb.h>
97 1.2 bouyer #include <machine/specialreg.h>
98 1.2 bouyer #include <machine/segments.h>
99 1.2 bouyer #include <machine/gdt.h>
100 1.2 bouyer #include <machine/mtrr.h>
101 1.2 bouyer #include <machine/pio.h>
102 1.2 bouyer
103 1.2 bouyer #ifdef XEN3
104 1.2 bouyer #include <xen/vcpuvar.h>
105 1.2 bouyer #endif
106 1.2 bouyer
107 1.2 bouyer #if NLAPIC > 0
108 1.2 bouyer #include <machine/apicvar.h>
109 1.2 bouyer #include <machine/i82489reg.h>
110 1.2 bouyer #include <machine/i82489var.h>
111 1.2 bouyer #endif
112 1.2 bouyer
113 1.2 bouyer #include <dev/ic/mc146818reg.h>
114 1.2 bouyer #include <dev/isa/isareg.h>
115 1.2 bouyer
116 1.10 cegger int cpu_match(device_t, cfdata_t, void *);
117 1.10 cegger void cpu_attach(device_t, device_t, void *);
118 1.2 bouyer #ifdef XEN3
119 1.10 cegger int vcpu_match(device_t, cfdata_t, void *);
120 1.10 cegger void vcpu_attach(device_t, device_t, void *);
121 1.2 bouyer #endif
122 1.10 cegger void cpu_attach_common(device_t, device_t, void *);
123 1.8 dogcow void cpu_offline_md(void);
124 1.2 bouyer
125 1.2 bouyer struct cpu_softc {
126 1.10 cegger device_t sc_dev; /* device tree glue */
127 1.2 bouyer struct cpu_info *sc_info; /* pointer to CPU info */
128 1.2 bouyer };
129 1.2 bouyer
130 1.5 joerg int mp_cpu_start(struct cpu_info *, paddr_t);
131 1.2 bouyer void mp_cpu_start_cleanup(struct cpu_info *);
132 1.2 bouyer const struct cpu_functions mp_cpu_funcs = { mp_cpu_start, NULL,
133 1.2 bouyer mp_cpu_start_cleanup };
134 1.2 bouyer
135 1.10 cegger CFATTACH_DECL_NEW(cpu, sizeof(struct cpu_softc),
136 1.2 bouyer cpu_match, cpu_attach, NULL, NULL);
137 1.2 bouyer #ifdef XEN3
138 1.10 cegger CFATTACH_DECL_NEW(vcpu, sizeof(struct cpu_softc),
139 1.2 bouyer vcpu_match, vcpu_attach, NULL, NULL);
140 1.2 bouyer #endif
141 1.2 bouyer
142 1.2 bouyer /*
143 1.2 bouyer * Statically-allocated CPU info for the primary CPU (or the only
144 1.2 bouyer * CPU, on uniprocessors). The CPU info list is initialized to
145 1.2 bouyer * point at it.
146 1.2 bouyer */
147 1.2 bouyer #ifdef TRAPLOG
148 1.2 bouyer #include <machine/tlog.h>
149 1.2 bouyer struct tlog tlog_primary;
150 1.2 bouyer #endif
151 1.2 bouyer struct cpu_info cpu_info_primary = {
152 1.7 bouyer .ci_dev = 0,
153 1.2 bouyer .ci_self = &cpu_info_primary,
154 1.4 bouyer .ci_idepth = -1,
155 1.2 bouyer .ci_curlwp = &lwp0,
156 1.2 bouyer #ifdef TRAPLOG
157 1.2 bouyer .ci_tlog = &tlog_primary,
158 1.2 bouyer #endif
159 1.2 bouyer
160 1.2 bouyer };
161 1.2 bouyer struct cpu_info phycpu_info_primary = {
162 1.7 bouyer .ci_dev = 0,
163 1.2 bouyer .ci_self = &phycpu_info_primary,
164 1.2 bouyer };
165 1.2 bouyer
166 1.2 bouyer struct cpu_info *cpu_info_list = &cpu_info_primary;
167 1.2 bouyer
168 1.2 bouyer static void cpu_set_tss_gates(struct cpu_info *ci);
169 1.2 bouyer
170 1.11 cegger uint32_t cpus_attached = 0;
171 1.11 cegger uint32_t cpus_running = 0;
172 1.11 cegger
173 1.11 cegger bool x86_mp_online;
174 1.11 cegger paddr_t mp_trampoline_paddr = MP_TRAMPOLINE;
175 1.2 bouyer
176 1.2 bouyer struct cpu_info *phycpu_info[X86_MAXPROCS] = { &cpu_info_primary };
177 1.2 bouyer
178 1.2 bouyer #ifdef MULTIPROCESSOR
179 1.2 bouyer /*
180 1.2 bouyer * Array of CPU info structures. Must be statically-allocated because
181 1.2 bouyer * curproc, etc. are used early.
182 1.2 bouyer */
183 1.2 bouyer struct cpu_info *cpu_info[X86_MAXPROCS] = { &cpu_info_primary };
184 1.2 bouyer
185 1.2 bouyer void cpu_hatch(void *);
186 1.2 bouyer static void cpu_boot_secondary(struct cpu_info *ci);
187 1.2 bouyer static void cpu_start_secondary(struct cpu_info *ci);
188 1.2 bouyer static void cpu_copy_trampoline(void);
189 1.2 bouyer
190 1.2 bouyer /*
191 1.2 bouyer * Runs once per boot once multiprocessor goo has been detected and
192 1.2 bouyer * the local APIC on the boot processor has been mapped.
193 1.2 bouyer *
194 1.2 bouyer * Called from lapic_boot_init() (from mpbios_scan()).
195 1.2 bouyer */
196 1.2 bouyer void
197 1.10 cegger cpu_init_first(void)
198 1.2 bouyer {
199 1.2 bouyer int cpunum = lapic_cpu_number();
200 1.2 bouyer
201 1.2 bouyer if (cpunum != 0) {
202 1.2 bouyer cpu_info[0] = NULL;
203 1.2 bouyer cpu_info[cpunum] = &cpu_info_primary;
204 1.2 bouyer }
205 1.2 bouyer
206 1.2 bouyer cpu_copy_trampoline();
207 1.2 bouyer }
208 1.2 bouyer #endif
209 1.2 bouyer
210 1.2 bouyer int
211 1.10 cegger cpu_match(device_t parent, cfdata_t match, void *aux)
212 1.2 bouyer {
213 1.2 bouyer
214 1.2 bouyer return 1;
215 1.2 bouyer }
216 1.2 bouyer
217 1.2 bouyer void
218 1.10 cegger cpu_attach(device_t parent, device_t self, void *aux)
219 1.2 bouyer {
220 1.2 bouyer #ifdef XEN3
221 1.10 cegger struct cpu_softc *sc = device_private(self);
222 1.2 bouyer struct cpu_attach_args *caa = aux;
223 1.2 bouyer struct cpu_info *ci;
224 1.2 bouyer int cpunum = caa->cpu_number;
225 1.2 bouyer
226 1.10 cegger sc->sc_dev = self;
227 1.10 cegger
228 1.2 bouyer /*
229 1.2 bouyer * If we're an Application Processor, allocate a cpu_info
230 1.2 bouyer * structure, otherwise use the primary's.
231 1.2 bouyer */
232 1.2 bouyer if (caa->cpu_role == CPU_ROLE_AP) {
233 1.2 bouyer ci = malloc(sizeof(*ci), M_DEVBUF, M_WAITOK | M_ZERO);
234 1.2 bouyer if (phycpu_info[cpunum] != NULL)
235 1.2 bouyer panic("cpu at apic id %d already attached?", cpunum);
236 1.2 bouyer phycpu_info[cpunum] = ci;
237 1.2 bouyer } else {
238 1.2 bouyer ci = &phycpu_info_primary;
239 1.2 bouyer if (cpunum != 0) {
240 1.2 bouyer phycpu_info[0] = NULL;
241 1.2 bouyer phycpu_info[cpunum] = ci;
242 1.2 bouyer }
243 1.2 bouyer }
244 1.2 bouyer
245 1.2 bouyer ci->ci_self = ci;
246 1.2 bouyer sc->sc_info = ci;
247 1.2 bouyer
248 1.2 bouyer ci->ci_dev = self;
249 1.2 bouyer ci->ci_apicid = caa->cpu_number;
250 1.2 bouyer ci->ci_cpuid = ci->ci_apicid;
251 1.16 cegger ci->ci_vcpu = NULL;
252 1.2 bouyer
253 1.2 bouyer printf(": ");
254 1.2 bouyer switch (caa->cpu_role) {
255 1.2 bouyer case CPU_ROLE_SP:
256 1.2 bouyer printf("(uniprocessor)\n");
257 1.2 bouyer ci->ci_flags |= CPUF_PRESENT | CPUF_SP | CPUF_PRIMARY;
258 1.2 bouyer break;
259 1.2 bouyer
260 1.2 bouyer case CPU_ROLE_BP:
261 1.2 bouyer printf("(boot processor)\n");
262 1.2 bouyer ci->ci_flags |= CPUF_PRESENT | CPUF_BSP | CPUF_PRIMARY;
263 1.2 bouyer break;
264 1.2 bouyer
265 1.2 bouyer case CPU_ROLE_AP:
266 1.2 bouyer /*
267 1.2 bouyer * report on an AP
268 1.2 bouyer */
269 1.2 bouyer printf("(application processor)\n");
270 1.2 bouyer break;
271 1.2 bouyer
272 1.2 bouyer default:
273 1.2 bouyer panic("unknown processor type??\n");
274 1.2 bouyer }
275 1.2 bouyer return;
276 1.2 bouyer #else
277 1.2 bouyer cpu_attach_common(parent, self, aux);
278 1.2 bouyer #endif
279 1.2 bouyer }
280 1.2 bouyer
281 1.2 bouyer #ifdef XEN3
282 1.2 bouyer int
283 1.10 cegger vcpu_match(device_t parent, cfdata_t match, void *aux)
284 1.2 bouyer {
285 1.2 bouyer struct vcpu_attach_args *vcaa = aux;
286 1.2 bouyer
287 1.2 bouyer if (strcmp(vcaa->vcaa_name, match->cf_name) == 0)
288 1.2 bouyer return 1;
289 1.2 bouyer return 0;
290 1.2 bouyer }
291 1.2 bouyer
292 1.2 bouyer void
293 1.10 cegger vcpu_attach(device_t parent, device_t self, void *aux)
294 1.2 bouyer {
295 1.2 bouyer struct vcpu_attach_args *vcaa = aux;
296 1.2 bouyer
297 1.2 bouyer cpu_attach_common(parent, self, &vcaa->vcaa_caa);
298 1.2 bouyer }
299 1.2 bouyer #endif
300 1.2 bouyer
301 1.2 bouyer static void
302 1.2 bouyer cpu_vm_init(struct cpu_info *ci)
303 1.2 bouyer {
304 1.2 bouyer int ncolors = 2, i;
305 1.2 bouyer
306 1.2 bouyer for (i = CAI_ICACHE; i <= CAI_L2CACHE; i++) {
307 1.2 bouyer struct x86_cache_info *cai;
308 1.2 bouyer int tcolors;
309 1.2 bouyer
310 1.2 bouyer cai = &ci->ci_cinfo[i];
311 1.2 bouyer
312 1.2 bouyer tcolors = atop(cai->cai_totalsize);
313 1.2 bouyer switch(cai->cai_associativity) {
314 1.2 bouyer case 0xff:
315 1.2 bouyer tcolors = 1; /* fully associative */
316 1.2 bouyer break;
317 1.2 bouyer case 0:
318 1.2 bouyer case 1:
319 1.2 bouyer break;
320 1.2 bouyer default:
321 1.2 bouyer tcolors /= cai->cai_associativity;
322 1.2 bouyer }
323 1.2 bouyer ncolors = max(ncolors, tcolors);
324 1.2 bouyer }
325 1.2 bouyer
326 1.2 bouyer /*
327 1.2 bouyer * Knowing the size of the largest cache on this CPU, re-color
328 1.2 bouyer * our pages.
329 1.2 bouyer */
330 1.2 bouyer if (ncolors <= uvmexp.ncolors)
331 1.2 bouyer return;
332 1.9 cegger printf("%s: %d page colors\n", device_xname(ci->ci_dev), ncolors);
333 1.2 bouyer uvm_page_recolor(ncolors);
334 1.2 bouyer }
335 1.2 bouyer
336 1.2 bouyer void
337 1.11 cegger cpu_attach_common(device_t parent, device_t self, void *aux)
338 1.2 bouyer {
339 1.10 cegger struct cpu_softc *sc = device_private(self);
340 1.2 bouyer struct cpu_attach_args *caa = aux;
341 1.2 bouyer struct cpu_info *ci;
342 1.12 cegger uintptr_t ptr;
343 1.2 bouyer int cpunum = caa->cpu_number;
344 1.2 bouyer
345 1.10 cegger sc->sc_dev = self;
346 1.10 cegger
347 1.2 bouyer /*
348 1.2 bouyer * If we're an Application Processor, allocate a cpu_info
349 1.2 bouyer * structure, otherwise use the primary's.
350 1.2 bouyer */
351 1.2 bouyer if (caa->cpu_role == CPU_ROLE_AP) {
352 1.17 cegger if (cpunum >= X86_MAXPROCS) {
353 1.17 cegger aprint_error(": apic id %d ignored, "
354 1.17 cegger "please increase X86_MAXPROCS\n", cpunum);
355 1.17 cegger }
356 1.17 cegger
357 1.12 cegger aprint_naive(": Application Processor\n");
358 1.12 cegger ptr = (uintptr_t)malloc(sizeof(*ci) + CACHE_LINE_SIZE - 1,
359 1.12 cegger M_DEVBUF, M_WAITOK);
360 1.12 cegger ci = (struct cpu_info *)((ptr + CACHE_LINE_SIZE - 1) &
361 1.12 cegger ~(CACHE_LINE_SIZE - 1));
362 1.12 cegger memset(ci, 0, sizeof(*ci));
363 1.2 bouyer #if defined(MULTIPROCESSOR)
364 1.2 bouyer if (cpu_info[cpunum] != NULL)
365 1.2 bouyer panic("cpu at apic id %d already attached?", cpunum);
366 1.2 bouyer cpu_info[cpunum] = ci;
367 1.2 bouyer #endif
368 1.2 bouyer #ifdef TRAPLOG
369 1.2 bouyer ci->ci_tlog_base = malloc(sizeof(struct tlog),
370 1.2 bouyer M_DEVBUF, M_WAITOK);
371 1.2 bouyer #endif
372 1.2 bouyer } else {
373 1.12 cegger aprint_naive(": %s Processor\n",
374 1.12 cegger caa->cpu_role == CPU_ROLE_SP ? "Single" : "Boot");
375 1.2 bouyer ci = &cpu_info_primary;
376 1.2 bouyer #if defined(MULTIPROCESSOR)
377 1.2 bouyer if (cpunum != lapic_cpu_number()) {
378 1.2 bouyer panic("%s: running CPU is at apic %d"
379 1.2 bouyer " instead of at expected %d",
380 1.9 cegger device_xname(sc->sc_dev), lapic_cpu_number(), cpunum);
381 1.2 bouyer }
382 1.2 bouyer #endif
383 1.2 bouyer }
384 1.2 bouyer
385 1.2 bouyer ci->ci_self = ci;
386 1.2 bouyer sc->sc_info = ci;
387 1.2 bouyer
388 1.2 bouyer ci->ci_dev = self;
389 1.16 cegger ci->ci_apicid = cpunum;
390 1.16 cegger
391 1.16 cegger KASSERT(HYPERVISOR_shared_info != NULL);
392 1.16 cegger ci->ci_vcpu = &HYPERVISOR_shared_info->vcpu_info[cpunum];
393 1.16 cegger
394 1.2 bouyer #ifdef MULTIPROCESSOR
395 1.2 bouyer ci->ci_cpuid = ci->ci_apicid;
396 1.2 bouyer #else
397 1.2 bouyer ci->ci_cpuid = 0; /* False for APs, but they're not used anyway */
398 1.2 bouyer #endif
399 1.2 bouyer ci->ci_cpumask = (1 << ci->ci_cpuid);
400 1.2 bouyer ci->ci_func = caa->cpu_func;
401 1.2 bouyer
402 1.2 bouyer if (caa->cpu_role == CPU_ROLE_AP) {
403 1.2 bouyer #if defined(MULTIPROCESSOR)
404 1.2 bouyer int error;
405 1.2 bouyer
406 1.2 bouyer error = mi_cpu_attach(ci);
407 1.2 bouyer if (error != 0) {
408 1.2 bouyer aprint_normal("\n");
409 1.10 cegger aprint_error_dev(sc->sc_dev, "mi_cpu_attach failed with %d\n",
410 1.9 cegger error);
411 1.2 bouyer return;
412 1.2 bouyer }
413 1.2 bouyer #endif
414 1.2 bouyer } else {
415 1.2 bouyer KASSERT(ci->ci_data.cpu_idlelwp != NULL);
416 1.2 bouyer }
417 1.2 bouyer
418 1.2 bouyer pmap_reference(pmap_kernel());
419 1.2 bouyer ci->ci_pmap = pmap_kernel();
420 1.2 bouyer ci->ci_tlbstate = TLBSTATE_STALE;
421 1.2 bouyer
422 1.2 bouyer /* further PCB init done later. */
423 1.2 bouyer
424 1.2 bouyer switch (caa->cpu_role) {
425 1.2 bouyer case CPU_ROLE_SP:
426 1.12 cegger aprint_normal(": (uniprocessor)\n");
427 1.12 cegger atomic_or_32(&ci->ci_flags,
428 1.12 cegger CPUF_PRESENT | CPUF_SP | CPUF_PRIMARY);
429 1.2 bouyer cpu_intr_init(ci);
430 1.21 ad cpu_get_tsc_freq(ci);
431 1.21 ad cpu_identify(ci);
432 1.2 bouyer cpu_init(ci);
433 1.2 bouyer cpu_set_tss_gates(ci);
434 1.12 cegger pmap_cpu_init_late(ci);
435 1.12 cegger #if 0
436 1.12 cegger x86_errata();
437 1.12 cegger #endif
438 1.2 bouyer break;
439 1.2 bouyer
440 1.2 bouyer case CPU_ROLE_BP:
441 1.12 cegger aprint_normal("apid %d (boot processor)\n", caa->cpu_number);
442 1.12 cegger atomic_or_32(&ci->ci_flags,
443 1.12 cegger CPUF_PRESENT | CPUF_BSP | CPUF_PRIMARY);
444 1.2 bouyer cpu_intr_init(ci);
445 1.21 ad cpu_get_tsc_freq(ci);
446 1.21 ad cpu_identify(ci);
447 1.2 bouyer cpu_init(ci);
448 1.2 bouyer cpu_set_tss_gates(ci);
449 1.12 cegger pmap_cpu_init_late(ci);
450 1.12 cegger #if NLAPIC > 0
451 1.12 cegger /*
452 1.12 cegger * Enable local apic
453 1.12 cegger */
454 1.12 cegger lapic_enable();
455 1.12 cegger lapic_set_lvt();
456 1.12 cegger lapic_calibrate_timer(ci);
457 1.12 cegger #endif
458 1.14 bouyer #if 0
459 1.12 cegger x86_errata();
460 1.12 cegger #endif
461 1.2 bouyer break;
462 1.2 bouyer
463 1.2 bouyer case CPU_ROLE_AP:
464 1.2 bouyer /*
465 1.2 bouyer * report on an AP
466 1.2 bouyer */
467 1.12 cegger aprint_normal("apid %d (application processor)\n", caa->cpu_number);
468 1.2 bouyer
469 1.2 bouyer #if defined(MULTIPROCESSOR)
470 1.2 bouyer cpu_intr_init(ci);
471 1.2 bouyer gdt_alloc_cpu(ci);
472 1.2 bouyer cpu_set_tss_gates(ci);
473 1.12 cegger pmap_cpu_init_early(ci);
474 1.12 cegger pmap_cpu_init_late(ci);
475 1.2 bouyer cpu_start_secondary(ci);
476 1.2 bouyer if (ci->ci_flags & CPUF_PRESENT) {
477 1.2 bouyer identifycpu(ci);
478 1.2 bouyer ci->ci_next = cpu_info_list->ci_next;
479 1.2 bouyer cpu_info_list->ci_next = ci;
480 1.2 bouyer }
481 1.2 bouyer #else
482 1.12 cegger aprint_normal_dev(sc->sc_dev, "not started\n");
483 1.2 bouyer #endif
484 1.2 bouyer break;
485 1.2 bouyer
486 1.2 bouyer default:
487 1.12 cegger aprint_normal("\n");
488 1.2 bouyer panic("unknown processor type??\n");
489 1.2 bouyer }
490 1.2 bouyer cpu_vm_init(ci);
491 1.2 bouyer
492 1.2 bouyer cpus_attached |= (1 << ci->ci_cpuid);
493 1.2 bouyer
494 1.12 cegger #if 0
495 1.12 cegger if (!pmf_device_register(self, cpu_suspend, cpu_resume))
496 1.12 cegger aprint_error_dev(self, "couldn't establish power handler\n");
497 1.12 cegger #endif
498 1.12 cegger
499 1.2 bouyer #if defined(MULTIPROCESSOR)
500 1.2 bouyer if (mp_verbose) {
501 1.2 bouyer struct lwp *l = ci->ci_data.cpu_idlelwp;
502 1.2 bouyer
503 1.12 cegger aprint_verbose_dev(sc->sc_dev, "idle lwp at %p, idle sp at 0x%p\n",
504 1.12 cegger l,
505 1.12 cegger #ifdef i386
506 1.12 cegger (void *)l->l_addr->u_pcb.pcb_esp
507 1.12 cegger #else
508 1.12 cegger (void *)l->l_addr->u_pcb.pcb_rsp
509 1.12 cegger #endif
510 1.12 cegger );
511 1.12 cegger
512 1.2 bouyer }
513 1.2 bouyer #endif
514 1.2 bouyer }
515 1.2 bouyer
516 1.2 bouyer /*
517 1.2 bouyer * Initialize the processor appropriately.
518 1.2 bouyer */
519 1.2 bouyer
520 1.2 bouyer void
521 1.10 cegger cpu_init(struct cpu_info *ci)
522 1.2 bouyer {
523 1.2 bouyer /* configure the CPU if needed */
524 1.2 bouyer if (ci->cpu_setup != NULL)
525 1.2 bouyer (*ci->cpu_setup)(ci);
526 1.2 bouyer
527 1.2 bouyer /*
528 1.2 bouyer * On a P6 or above, enable global TLB caching if the
529 1.2 bouyer * hardware supports it.
530 1.2 bouyer */
531 1.2 bouyer if (cpu_feature & CPUID_PGE)
532 1.2 bouyer lcr4(rcr4() | CR4_PGE); /* enable global TLB caching */
533 1.2 bouyer
534 1.2 bouyer #ifdef XXXMTRR
535 1.2 bouyer /*
536 1.2 bouyer * On a P6 or above, initialize MTRR's if the hardware supports them.
537 1.2 bouyer */
538 1.2 bouyer if (cpu_feature & CPUID_MTRR) {
539 1.2 bouyer if ((ci->ci_flags & CPUF_AP) == 0)
540 1.2 bouyer i686_mtrr_init_first();
541 1.2 bouyer mtrr_init_cpu(ci);
542 1.2 bouyer }
543 1.2 bouyer #endif
544 1.2 bouyer /*
545 1.2 bouyer * If we have FXSAVE/FXRESTOR, use them.
546 1.2 bouyer */
547 1.2 bouyer if (cpu_feature & CPUID_FXSR) {
548 1.2 bouyer lcr4(rcr4() | CR4_OSFXSR);
549 1.2 bouyer
550 1.2 bouyer /*
551 1.2 bouyer * If we have SSE/SSE2, enable XMM exceptions.
552 1.2 bouyer */
553 1.2 bouyer if (cpu_feature & (CPUID_SSE|CPUID_SSE2))
554 1.2 bouyer lcr4(rcr4() | CR4_OSXMMEXCPT);
555 1.2 bouyer }
556 1.2 bouyer
557 1.2 bouyer #ifdef MULTIPROCESSOR
558 1.11 cegger atomic_or_32(&ci->ci_flags, CPUF_RUNNING);
559 1.11 cegger atomic_or_32(&cpus_running, ci->ci_cpumask);
560 1.2 bouyer #endif
561 1.2 bouyer }
562 1.2 bouyer
563 1.2 bouyer
564 1.2 bouyer #ifdef MULTIPROCESSOR
565 1.2 bouyer void
566 1.10 cegger cpu_boot_secondary_processors(void)
567 1.2 bouyer {
568 1.2 bouyer struct cpu_info *ci;
569 1.2 bouyer u_long i;
570 1.2 bouyer
571 1.11 cegger for (i = 0; i < X86_MAXPROCS; i++) {
572 1.2 bouyer ci = cpu_info[i];
573 1.2 bouyer if (ci == NULL)
574 1.2 bouyer continue;
575 1.2 bouyer if (ci->ci_data.cpu_idlelwp == NULL)
576 1.2 bouyer continue;
577 1.2 bouyer if ((ci->ci_flags & CPUF_PRESENT) == 0)
578 1.2 bouyer continue;
579 1.2 bouyer if (ci->ci_flags & (CPUF_BSP|CPUF_SP|CPUF_PRIMARY))
580 1.2 bouyer continue;
581 1.2 bouyer cpu_boot_secondary(ci);
582 1.2 bouyer }
583 1.11 cegger
584 1.11 cegger x86_mp_online = true;
585 1.2 bouyer }
586 1.2 bouyer
587 1.2 bouyer static void
588 1.2 bouyer cpu_init_idle_lwp(struct cpu_info *ci)
589 1.2 bouyer {
590 1.2 bouyer struct lwp *l = ci->ci_data.cpu_idlelwp;
591 1.2 bouyer struct pcb *pcb = &l->l_addr->u_pcb;
592 1.2 bouyer
593 1.2 bouyer pcb->pcb_cr0 = rcr0();
594 1.2 bouyer }
595 1.2 bouyer
596 1.2 bouyer void
597 1.10 cegger cpu_init_idle_lwps(void)
598 1.2 bouyer {
599 1.2 bouyer struct cpu_info *ci;
600 1.2 bouyer u_long i;
601 1.2 bouyer
602 1.2 bouyer for (i = 0; i < X86_MAXPROCS; i++) {
603 1.2 bouyer ci = cpu_info[i];
604 1.2 bouyer if (ci == NULL)
605 1.2 bouyer continue;
606 1.2 bouyer if (ci->ci_data.cpu_idlelwp == NULL)
607 1.2 bouyer continue;
608 1.2 bouyer if ((ci->ci_flags & CPUF_PRESENT) == 0)
609 1.2 bouyer continue;
610 1.2 bouyer cpu_init_idle_lwp(ci);
611 1.2 bouyer }
612 1.2 bouyer }
613 1.2 bouyer
614 1.2 bouyer void
615 1.10 cegger cpu_start_secondary(struct cpu_info *ci)
616 1.2 bouyer {
617 1.2 bouyer int i;
618 1.2 bouyer struct pmap *kpm = pmap_kernel();
619 1.11 cegger extern uint32_t mp_pdirpa;
620 1.2 bouyer
621 1.2 bouyer mp_pdirpa = kpm->pm_pdirpa; /* XXX move elsewhere, not per CPU. */
622 1.2 bouyer
623 1.11 cegger atomic_or_32(&ci->ci_flags, CPUF_AP);
624 1.2 bouyer
625 1.11 cegger aprint_debug_dev(ci->ci_dev, "starting\n");
626 1.2 bouyer
627 1.2 bouyer ci->ci_curlwp = ci->ci_data.cpu_idlelwp;
628 1.11 cegger if (CPU_STARTUP(ci, mp_trampoline_paddr) != 0)
629 1.11 cegger return;
630 1.2 bouyer
631 1.2 bouyer /*
632 1.2 bouyer * wait for it to become ready
633 1.2 bouyer */
634 1.11 cegger for (i = 100000; (!(ci->ci_flags & CPUF_PRESENT)) && i > 0; i--) {
635 1.11 cegger #ifdef MPDEBUG
636 1.11 cegger extern int cpu_trace[3];
637 1.11 cegger static int otrace[3];
638 1.11 cegger if (memcmp(otrace, cpu_trace, sizeof(otrace)) != 0) {
639 1.11 cegger aprint_debug_dev(ci->ci_dev, "trace %02x %02x %02x\n",
640 1.11 cegger cpu_trace[0], cpu_trace[1], cpu_trace[2]);
641 1.11 cegger memcpy(otrace, cpu_trace, sizeof(otrace));
642 1.11 cegger }
643 1.11 cegger #endif
644 1.2 bouyer delay(10);
645 1.2 bouyer }
646 1.11 cegger if ((ci->ci_flags & CPUF_PRESENT) == 0) {
647 1.9 cegger aprint_error_dev(ci->ci_dev, "failed to become ready\n");
648 1.2 bouyer #if defined(MPDEBUG) && defined(DDB)
649 1.2 bouyer printf("dropping into debugger; continue from here to resume boot\n");
650 1.2 bouyer Debugger();
651 1.2 bouyer #endif
652 1.2 bouyer }
653 1.2 bouyer
654 1.2 bouyer CPU_START_CLEANUP(ci);
655 1.2 bouyer }
656 1.2 bouyer
657 1.2 bouyer void
658 1.10 cegger cpu_boot_secondary(struct cpu_info *ci)
659 1.2 bouyer {
660 1.2 bouyer int i;
661 1.2 bouyer
662 1.11 cegger atomic_or_32(&ci->ci_flags, CPUF_GO);
663 1.11 cegger for (i = 100000; (!(ci->ci_flags & CPUF_RUNNING)) && i > 0; i--) {
664 1.2 bouyer delay(10);
665 1.2 bouyer }
666 1.11 cegger if ((ci->ci_flags & CPUF_RUNNING) == 0) {
667 1.11 cegger aprint_error_dev(ci->ci_dev, "CPU failed to start\n");
668 1.2 bouyer #if defined(MPDEBUG) && defined(DDB)
669 1.2 bouyer printf("dropping into debugger; continue from here to resume boot\n");
670 1.2 bouyer Debugger();
671 1.2 bouyer #endif
672 1.2 bouyer }
673 1.2 bouyer }
674 1.2 bouyer
675 1.2 bouyer /*
676 1.2 bouyer * The CPU ends up here when its ready to run
677 1.2 bouyer * This is called from code in mptramp.s; at this point, we are running
678 1.2 bouyer * in the idle pcb/idle stack of the new CPU. When this function returns,
679 1.2 bouyer * this processor will enter the idle loop and start looking for work.
680 1.2 bouyer *
681 1.2 bouyer * XXX should share some of this with init386 in machdep.c
682 1.2 bouyer */
683 1.2 bouyer void
684 1.2 bouyer cpu_hatch(void *v)
685 1.2 bouyer {
686 1.2 bouyer struct cpu_info *ci = (struct cpu_info *)v;
687 1.11 cegger int s, i;
688 1.11 cegger uint32_t blacklist_features;
689 1.11 cegger
690 1.2 bouyer #ifdef __x86_64__
691 1.11 cegger cpu_init_msrs(ci, true);
692 1.2 bouyer #endif
693 1.2 bouyer
694 1.21 ad cpu_probe(ci);
695 1.11 cegger
696 1.2 bouyer /* not on Xen... */
697 1.11 cegger blacklist_features = ~(CPUID_PGE|CPUID_PSE|CPUID_MTRR|CPUID_FXSR|CPUID_NOX); /* XXX add CPUID_SVM */
698 1.2 bouyer
699 1.11 cegger cpu_feature &= blacklist_features;
700 1.2 bouyer
701 1.11 cegger KDASSERT((ci->ci_flags & CPUF_PRESENT) == 0);
702 1.11 cegger atomic_or_32(&ci->ci_flags, CPUF_PRESENT);
703 1.11 cegger while ((ci->ci_flags & CPUF_GO) == 0) {
704 1.11 cegger /* Don't use delay, boot CPU may be patching the text. */
705 1.11 cegger for (i = 10000; i != 0; i--)
706 1.11 cegger x86_pause();
707 1.11 cegger }
708 1.2 bouyer
709 1.11 cegger /* Because the text may have been patched in x86_patch(). */
710 1.11 cegger wbinvd();
711 1.11 cegger x86_flush();
712 1.2 bouyer
713 1.11 cegger KASSERT((ci->ci_flags & CPUF_RUNNING) == 0);
714 1.2 bouyer
715 1.12 cegger lcr3(pmap_kernel()->pm_pdirpa);
716 1.12 cegger curlwp->l_addr->u_pcb.pcb_cr3 = pmap_kernel()->pm_pdirpa;
717 1.2 bouyer lcr0(ci->ci_data.cpu_idlelwp->l_addr->u_pcb.pcb_cr0);
718 1.2 bouyer cpu_init_idt();
719 1.11 cegger gdt_init_cpu(ci);
720 1.11 cegger lapic_enable();
721 1.2 bouyer lapic_set_lvt();
722 1.11 cegger lapic_initclocks();
723 1.11 cegger
724 1.12 cegger #ifdef i386
725 1.2 bouyer npxinit(ci);
726 1.12 cegger #else
727 1.12 cegger fpuinit(ci);
728 1.12 cegger #endif
729 1.2 bouyer
730 1.2 bouyer lldt(GSEL(GLDT_SEL, SEL_KPL));
731 1.12 cegger ltr(ci->ci_tss_sel);
732 1.2 bouyer
733 1.2 bouyer cpu_init(ci);
734 1.11 cegger cpu_get_tsc_freq(ci);
735 1.2 bouyer
736 1.2 bouyer s = splhigh();
737 1.11 cegger #ifdef i386
738 1.2 bouyer lapic_tpr = 0;
739 1.11 cegger #else
740 1.11 cegger lcr8(0);
741 1.11 cegger #endif
742 1.11 cegger x86_enable_intr();
743 1.11 cegger splx(s);
744 1.12 cegger #if 0
745 1.11 cegger x86_errata();
746 1.11 cegger #endif
747 1.2 bouyer
748 1.11 cegger aprint_debug_dev(ci->ci_dev, "CPU %ld running\n",
749 1.11 cegger (long)ci->ci_cpuid);
750 1.2 bouyer }
751 1.2 bouyer
752 1.2 bouyer #if defined(DDB)
753 1.2 bouyer
754 1.2 bouyer #include <ddb/db_output.h>
755 1.2 bouyer #include <machine/db_machdep.h>
756 1.2 bouyer
757 1.2 bouyer /*
758 1.2 bouyer * Dump CPU information from ddb.
759 1.2 bouyer */
760 1.2 bouyer void
761 1.2 bouyer cpu_debug_dump(void)
762 1.2 bouyer {
763 1.2 bouyer struct cpu_info *ci;
764 1.2 bouyer CPU_INFO_ITERATOR cii;
765 1.2 bouyer
766 1.13 yamt db_printf("addr dev id flags ipis curlwp fpcurlwp\n");
767 1.2 bouyer for (CPU_INFO_FOREACH(cii, ci)) {
768 1.2 bouyer db_printf("%p %s %ld %x %x %10p %10p\n",
769 1.2 bouyer ci,
770 1.9 cegger ci->ci_dev == NULL ? "BOOT" : device_xname(ci->ci_dev),
771 1.12 cegger (long)ci->ci_cpuid,
772 1.2 bouyer ci->ci_flags, ci->ci_ipis,
773 1.2 bouyer ci->ci_curlwp,
774 1.2 bouyer ci->ci_fpcurlwp);
775 1.2 bouyer }
776 1.2 bouyer }
777 1.2 bouyer #endif
778 1.2 bouyer
779 1.2 bouyer static void
780 1.10 cegger cpu_copy_trampoline(void)
781 1.2 bouyer {
782 1.2 bouyer /*
783 1.2 bouyer * Copy boot code.
784 1.2 bouyer */
785 1.2 bouyer extern u_char cpu_spinup_trampoline[];
786 1.2 bouyer extern u_char cpu_spinup_trampoline_end[];
787 1.11 cegger
788 1.11 cegger vaddr_t mp_trampoline_vaddr;
789 1.11 cegger
790 1.11 cegger mp_trampoline_vaddr = uvm_km_alloc(kernel_map, PAGE_SIZE, 0,
791 1.11 cegger UVM_KMF_VAONLY);
792 1.11 cegger
793 1.11 cegger pmap_kenter_pa(mp_trampoline_vaddr, mp_trampoline_paddr,
794 1.11 cegger VM_PROT_READ | VM_PROT_WRITE);
795 1.11 cegger pmap_update(pmap_kernel());
796 1.11 cegger memcpy((void *)mp_trampoline_vaddr,
797 1.11 cegger cpu_spinup_trampoline,
798 1.11 cegger cpu_spinup_trampoline_end - cpu_spinup_trampoline);
799 1.11 cegger
800 1.11 cegger pmap_kremove(mp_trampoline_vaddr, PAGE_SIZE);
801 1.11 cegger pmap_update(pmap_kernel());
802 1.11 cegger uvm_km_free(kernel_map, mp_trampoline_vaddr, PAGE_SIZE, UVM_KMF_VAONLY);
803 1.2 bouyer }
804 1.2 bouyer
805 1.2 bouyer #endif
806 1.2 bouyer
807 1.11 cegger #ifdef i386
808 1.11 cegger #if 0
809 1.11 cegger static void
810 1.11 cegger tss_init(struct i386tss *tss, void *stack, void *func)
811 1.11 cegger {
812 1.11 cegger memset(tss, 0, sizeof *tss);
813 1.11 cegger tss->tss_esp0 = tss->tss_esp = (int)((char *)stack + USPACE - 16);
814 1.11 cegger tss->tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
815 1.11 cegger tss->__tss_cs = GSEL(GCODE_SEL, SEL_KPL);
816 1.11 cegger tss->tss_fs = GSEL(GCPU_SEL, SEL_KPL);
817 1.11 cegger tss->tss_gs = tss->__tss_es = tss->__tss_ds =
818 1.11 cegger tss->__tss_ss = GSEL(GDATA_SEL, SEL_KPL);
819 1.11 cegger tss->tss_cr3 = pmap_kernel()->pm_pdirpa;
820 1.11 cegger tss->tss_esp = (int)((char *)stack + USPACE - 16);
821 1.11 cegger tss->tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
822 1.11 cegger tss->__tss_eflags = PSL_MBO | PSL_NT; /* XXX not needed? */
823 1.11 cegger tss->__tss_eip = (int)func;
824 1.11 cegger }
825 1.11 cegger #endif
826 1.2 bouyer
827 1.2 bouyer /* XXX */
828 1.2 bouyer #define IDTVEC(name) __CONCAT(X, name)
829 1.2 bouyer typedef void (vector)(void);
830 1.2 bouyer extern vector IDTVEC(tss_trap08);
831 1.2 bouyer #ifdef DDB
832 1.2 bouyer extern vector Xintrddbipi;
833 1.2 bouyer extern int ddb_vec;
834 1.2 bouyer #endif
835 1.2 bouyer
836 1.2 bouyer static void
837 1.2 bouyer cpu_set_tss_gates(struct cpu_info *ci)
838 1.2 bouyer {
839 1.11 cegger #if 0
840 1.11 cegger struct segment_descriptor sd;
841 1.11 cegger
842 1.11 cegger ci->ci_doubleflt_stack = (char *)uvm_km_alloc(kernel_map, USPACE, 0,
843 1.11 cegger UVM_KMF_WIRED);
844 1.11 cegger tss_init(&ci->ci_doubleflt_tss, ci->ci_doubleflt_stack,
845 1.11 cegger IDTVEC(tss_trap08));
846 1.11 cegger setsegment(&sd, &ci->ci_doubleflt_tss, sizeof(struct i386tss) - 1,
847 1.11 cegger SDT_SYS386TSS, SEL_KPL, 0, 0);
848 1.11 cegger ci->ci_gdt[GTRAPTSS_SEL].sd = sd;
849 1.11 cegger setgate(&idt[8], NULL, 0, SDT_SYSTASKGT, SEL_KPL,
850 1.11 cegger GSEL(GTRAPTSS_SEL, SEL_KPL));
851 1.11 cegger #endif
852 1.11 cegger
853 1.2 bouyer #if defined(DDB) && defined(MULTIPROCESSOR)
854 1.2 bouyer /*
855 1.2 bouyer * Set up separate handler for the DDB IPI, so that it doesn't
856 1.2 bouyer * stomp on a possibly corrupted stack.
857 1.2 bouyer *
858 1.2 bouyer * XXX overwriting the gate set in db_machine_init.
859 1.2 bouyer * Should rearrange the code so that it's set only once.
860 1.2 bouyer */
861 1.2 bouyer ci->ci_ddbipi_stack = (char *)uvm_km_alloc(kernel_map, USPACE, 0,
862 1.2 bouyer UVM_KMF_WIRED);
863 1.6 yamt tss_init(&ci->ci_ddbipi_tss, ci->ci_ddbipi_stack,
864 1.2 bouyer Xintrddbipi);
865 1.2 bouyer
866 1.2 bouyer setsegment(&sd, &ci->ci_ddbipi_tss, sizeof(struct i386tss) - 1,
867 1.2 bouyer SDT_SYS386TSS, SEL_KPL, 0, 0);
868 1.2 bouyer ci->ci_gdt[GIPITSS_SEL].sd = sd;
869 1.2 bouyer
870 1.2 bouyer setgate(&idt[ddb_vec], NULL, 0, SDT_SYSTASKGT, SEL_KPL,
871 1.2 bouyer GSEL(GIPITSS_SEL, SEL_KPL));
872 1.2 bouyer #endif
873 1.2 bouyer }
874 1.11 cegger #else
875 1.11 cegger static void
876 1.11 cegger cpu_set_tss_gates(struct cpu_info *ci)
877 1.11 cegger {
878 1.11 cegger
879 1.11 cegger }
880 1.11 cegger #endif /* i386 */
881 1.2 bouyer
882 1.2 bouyer int
883 1.5 joerg mp_cpu_start(struct cpu_info *ci, paddr_t target)
884 1.2 bouyer {
885 1.2 bouyer #if 0
886 1.2 bouyer #if NLAPIC > 0
887 1.2 bouyer int error;
888 1.2 bouyer #endif
889 1.2 bouyer unsigned short dwordptr[2];
890 1.2 bouyer
891 1.2 bouyer /*
892 1.11 cegger * Bootstrap code must be addressable in real mode
893 1.11 cegger * and it must be page aligned.
894 1.11 cegger */
895 1.11 cegger KASSERT(target < 0x10000 && target % PAGE_SIZE == 0);
896 1.11 cegger
897 1.11 cegger /*
898 1.2 bouyer * "The BSP must initialize CMOS shutdown code to 0Ah ..."
899 1.2 bouyer */
900 1.2 bouyer
901 1.2 bouyer outb(IO_RTC, NVRAM_RESET);
902 1.2 bouyer outb(IO_RTC+1, NVRAM_RESET_JUMP);
903 1.2 bouyer
904 1.2 bouyer /*
905 1.2 bouyer * "and the warm reset vector (DWORD based at 40:67) to point
906 1.2 bouyer * to the AP startup code ..."
907 1.2 bouyer */
908 1.2 bouyer
909 1.2 bouyer dwordptr[0] = 0;
910 1.5 joerg dwordptr[1] = target >> 4;
911 1.2 bouyer
912 1.2 bouyer pmap_kenter_pa (0, 0, VM_PROT_READ|VM_PROT_WRITE);
913 1.11 cegger memcpy ((uint8_t *) 0x467, dwordptr, 4);
914 1.2 bouyer pmap_kremove (0, PAGE_SIZE);
915 1.2 bouyer
916 1.2 bouyer #if NLAPIC > 0
917 1.2 bouyer /*
918 1.2 bouyer * ... prior to executing the following sequence:"
919 1.2 bouyer */
920 1.2 bouyer
921 1.2 bouyer if (ci->ci_flags & CPUF_AP) {
922 1.2 bouyer if ((error = x86_ipi_init(ci->ci_apicid)) != 0)
923 1.2 bouyer return error;
924 1.2 bouyer
925 1.2 bouyer delay(10000);
926 1.2 bouyer
927 1.2 bouyer if (cpu_feature & CPUID_APIC) {
928 1.11 cegger error = x86_ipi_init(ci->ci_apicid);
929 1.11 cegger if (error != 0) {
930 1.11 cegger aprint_error_dev(ci->ci_dev, "%s: IPI not taken (1)\n",
931 1.11 cegger __func__);
932 1.11 cegger return error;
933 1.11 cegger }
934 1.11 cegger
935 1.11 cegger delay(10000);
936 1.2 bouyer
937 1.11 cegger error = x86_ipi(target / PAGE_SIZE, ci->ci_apicid,
938 1.11 cegger LAPIC_DLMODE_STARTUP);
939 1.11 cegger if (error != 0) {
940 1.11 cegger aprint_error_dev(ci->ci_dev, "%s: IPI not taken (2)\n",
941 1.11 cegger __func__);
942 1.2 bouyer return error;
943 1.11 cegger }
944 1.2 bouyer delay(200);
945 1.2 bouyer
946 1.11 cegger error = x86_ipi(target / PAGE_SIZE, ci->ci_apicid,
947 1.11 cegger LAPIC_DLMODE_STARTUP);
948 1.11 cegger if (error != 0) {
949 1.11 cegger aprint_error_dev(ci->ci_dev, "%s: IPI not taken ((3)\n",
950 1.11 cegger __func__);
951 1.2 bouyer return error;
952 1.11 cegger }
953 1.2 bouyer delay(200);
954 1.2 bouyer }
955 1.2 bouyer }
956 1.2 bouyer #endif
957 1.2 bouyer #endif /* 0 */
958 1.2 bouyer return 0;
959 1.2 bouyer }
960 1.2 bouyer
961 1.2 bouyer void
962 1.2 bouyer mp_cpu_start_cleanup(struct cpu_info *ci)
963 1.2 bouyer {
964 1.2 bouyer #if 0
965 1.2 bouyer /*
966 1.2 bouyer * Ensure the NVRAM reset byte contains something vaguely sane.
967 1.2 bouyer */
968 1.2 bouyer
969 1.2 bouyer outb(IO_RTC, NVRAM_RESET);
970 1.2 bouyer outb(IO_RTC+1, NVRAM_RESET_RST);
971 1.2 bouyer #endif
972 1.2 bouyer }
973 1.2 bouyer
974 1.2 bouyer #ifdef __x86_64__
975 1.2 bouyer
976 1.2 bouyer void
977 1.3 bouyer cpu_init_msrs(struct cpu_info *ci, bool full)
978 1.2 bouyer {
979 1.3 bouyer if (full) {
980 1.3 bouyer HYPERVISOR_set_segment_base (SEGBASE_FS, 0);
981 1.11 cegger HYPERVISOR_set_segment_base (SEGBASE_GS_KERNEL, (uint64_t) ci);
982 1.3 bouyer HYPERVISOR_set_segment_base (SEGBASE_GS_USER, 0);
983 1.3 bouyer }
984 1.2 bouyer }
985 1.2 bouyer #endif /* __x86_64__ */
986 1.2 bouyer
987 1.11 cegger void
988 1.11 cegger cpu_offline_md(void)
989 1.11 cegger {
990 1.11 cegger int s;
991 1.11 cegger
992 1.11 cegger s = splhigh();
993 1.11 cegger #ifdef __i386__
994 1.11 cegger npxsave_cpu(true);
995 1.11 cegger #else
996 1.11 cegger fpusave_cpu(true);
997 1.11 cegger #endif
998 1.11 cegger splx(s);
999 1.11 cegger }
1000 1.11 cegger
1001 1.11 cegger #if 0
1002 1.11 cegger /* XXX joerg restructure and restart CPUs individually */
1003 1.11 cegger static bool
1004 1.11 cegger cpu_suspend(device_t dv PMF_FN_ARGS)
1005 1.11 cegger {
1006 1.11 cegger struct cpu_softc *sc = device_private(dv);
1007 1.11 cegger struct cpu_info *ci = sc->sc_info;
1008 1.11 cegger int err;
1009 1.11 cegger
1010 1.11 cegger if (ci->ci_flags & CPUF_PRIMARY)
1011 1.11 cegger return true;
1012 1.11 cegger if (ci->ci_data.cpu_idlelwp == NULL)
1013 1.11 cegger return true;
1014 1.11 cegger if ((ci->ci_flags & CPUF_PRESENT) == 0)
1015 1.11 cegger return true;
1016 1.11 cegger
1017 1.11 cegger sc->sc_wasonline = !(ci->ci_schedstate.spc_flags & SPCF_OFFLINE);
1018 1.11 cegger
1019 1.11 cegger if (sc->sc_wasonline) {
1020 1.11 cegger mutex_enter(&cpu_lock);
1021 1.11 cegger err = cpu_setonline(ci, false);
1022 1.11 cegger mutex_exit(&cpu_lock);
1023 1.11 cegger
1024 1.11 cegger if (err)
1025 1.11 cegger return false;
1026 1.11 cegger }
1027 1.11 cegger
1028 1.11 cegger return true;
1029 1.11 cegger }
1030 1.11 cegger
1031 1.11 cegger static bool
1032 1.11 cegger cpu_resume(device_t dv PMF_FN_ARGS)
1033 1.11 cegger {
1034 1.11 cegger struct cpu_softc *sc = device_private(dv);
1035 1.11 cegger struct cpu_info *ci = sc->sc_info;
1036 1.11 cegger int err = 0;
1037 1.11 cegger
1038 1.11 cegger if (ci->ci_flags & CPUF_PRIMARY)
1039 1.11 cegger return true;
1040 1.11 cegger if (ci->ci_data.cpu_idlelwp == NULL)
1041 1.11 cegger return true;
1042 1.11 cegger if ((ci->ci_flags & CPUF_PRESENT) == 0)
1043 1.11 cegger return true;
1044 1.11 cegger
1045 1.11 cegger if (sc->sc_wasonline) {
1046 1.11 cegger mutex_enter(&cpu_lock);
1047 1.11 cegger err = cpu_setonline(ci, true);
1048 1.11 cegger mutex_exit(&cpu_lock);
1049 1.11 cegger }
1050 1.11 cegger
1051 1.11 cegger return err == 0;
1052 1.11 cegger }
1053 1.11 cegger #endif
1054 1.11 cegger
1055 1.2 bouyer void
1056 1.2 bouyer cpu_get_tsc_freq(struct cpu_info *ci)
1057 1.2 bouyer {
1058 1.2 bouyer #ifdef XEN3
1059 1.16 cegger const volatile vcpu_time_info_t *tinfo = &ci->ci_vcpu->time;
1060 1.2 bouyer delay(1000000);
1061 1.2 bouyer uint64_t freq = 1000000000ULL << 32;
1062 1.2 bouyer freq = freq / (uint64_t)tinfo->tsc_to_system_mul;
1063 1.2 bouyer if ( tinfo->tsc_shift < 0 )
1064 1.2 bouyer freq = freq << -tinfo->tsc_shift;
1065 1.2 bouyer else
1066 1.2 bouyer freq = freq >> tinfo->tsc_shift;
1067 1.20 ad ci->ci_data.cpu_cc_freq = freq;
1068 1.2 bouyer #else
1069 1.16 cegger /* Xen2 */
1070 1.2 bouyer /* XXX this needs to read the shared_info of the CPU being probed.. */
1071 1.20 ad ci->ci_data.cpu_cc_freq = HYPERVISOR_shared_info->cpu_freq;
1072 1.2 bouyer #endif /* XEN3 */
1073 1.2 bouyer }
1074 1.19 joerg
1075 1.19 joerg void
1076 1.19 joerg x86_cpu_idle_xen(void)
1077 1.19 joerg {
1078 1.19 joerg struct cpu_info *ci = curcpu();
1079 1.19 joerg
1080 1.19 joerg KASSERT(ci->ci_ilevel == IPL_NONE);
1081 1.19 joerg
1082 1.19 joerg x86_disable_intr();
1083 1.19 joerg if (!__predict_false(ci->ci_want_resched)) {
1084 1.19 joerg idle_block();
1085 1.19 joerg } else {
1086 1.19 joerg x86_enable_intr();
1087 1.19 joerg }
1088 1.19 joerg }
1089