]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/ia64/ia64/mp_machdep.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / sys / ia64 / ia64 / mp_machdep.c
1 /*-
2  * Copyright (c) 2001-2005 Marcel Moolenaar
3  * Copyright (c) 2000 Doug Rabson
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include "opt_kstack_pages.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/ktr.h>
36 #include <sys/proc.h>
37 #include <sys/bus.h>
38 #include <sys/kthread.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/kernel.h>
43 #include <sys/pcpu.h>
44 #include <sys/sched.h>
45 #include <sys/smp.h>
46 #include <sys/sysctl.h>
47 #include <sys/uuid.h>
48
49 #include <vm/vm.h>
50 #include <vm/pmap.h>
51 #include <vm/vm_extern.h>
52 #include <vm/vm_kern.h>
53
54 #include <machine/atomic.h>
55 #include <machine/cpu.h>
56 #include <machine/fpu.h>
57 #include <machine/intr.h>
58 #include <machine/mca.h>
59 #include <machine/md_var.h>
60 #include <machine/pal.h>
61 #include <machine/pcb.h>
62 #include <machine/pmap.h>
63 #include <machine/sal.h>
64 #include <machine/smp.h>
65 #include <i386/include/specialreg.h>
66
67 MALLOC_DEFINE(M_SMP, "SMP", "SMP related allocations");
68
69 void ia64_ap_startup(void);
70
71 #define LID_SAPIC_ID(x)         ((int)((x) >> 24) & 0xff)
72 #define LID_SAPIC_EID(x)        ((int)((x) >> 16) & 0xff)
73 #define LID_SAPIC_SET(id,eid)   (((id & 0xff) << 8 | (eid & 0xff)) << 16);
74 #define LID_SAPIC_MASK          0xffff0000UL
75
76 /* Variables used by os_boot_rendez and ia64_ap_startup */
77 struct pcpu *ap_pcpu;
78 void *ap_stack;
79 uint64_t ap_vhpt;
80 volatile int ap_delay;
81 volatile int ap_awake;
82 volatile int ap_spin;
83
84 static void cpu_mp_unleash(void *);
85
86 struct cpu_group *
87 cpu_topo(void)
88 {
89
90         return smp_topo_none();
91 }
92
93 static void
94 ia64_store_mca_state(void* arg)
95 {
96         unsigned int ncpu = (unsigned int)(uintptr_t)arg;
97         struct thread* td;
98
99         /* ia64_mca_save_state() is CPU-sensitive, so bind ourself to our target CPU */
100         td = curthread;
101         thread_lock(td);
102         sched_bind(td, ncpu);
103         thread_unlock(td);
104
105         /*
106          * Get and save the CPU specific MCA records. Should we get the
107          * MCA state for each processor, or just the CMC state?
108          */
109         ia64_mca_save_state(SAL_INFO_MCA);
110         ia64_mca_save_state(SAL_INFO_CMC);
111
112         kproc_exit(0);
113 }
114
115 void
116 ia64_ap_startup(void)
117 {
118         volatile struct ia64_interrupt_block *ib = IA64_INTERRUPT_BLOCK;
119         int vector;
120
121         pcpup = ap_pcpu;
122         ia64_set_k4((intptr_t)pcpup);
123
124         map_vhpt(ap_vhpt);
125         ia64_set_pta(ap_vhpt + (1 << 8) + (pmap_vhpt_log2size << 2) + 1);
126         ia64_srlz_i();
127
128         ap_awake = 1;
129         ap_delay = 0;
130
131         map_pal_code();
132         map_gateway_page();
133
134         ia64_set_fpsr(IA64_FPSR_DEFAULT);
135
136         /* Wait until it's time for us to be unleashed */
137         while (ap_spin)
138                 cpu_spinwait();
139
140         /* Initialize curthread. */
141         KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread"));
142         PCPU_SET(curthread, PCPU_GET(idlethread));
143
144         atomic_add_int(&ap_awake, 1);
145         while (!smp_started)
146                 cpu_spinwait();
147
148         CTR1(KTR_SMP, "SMP: cpu%d launched", PCPU_GET(cpuid));
149
150         /* Acknowledge and EOI all interrupts. */
151         vector = ia64_get_ivr();
152         while (vector != 15) {
153                 ia64_srlz_d();
154                 if (vector == 0)
155                         vector = (int)ib->ib_inta;
156                 ia64_set_eoi(0);
157                 ia64_srlz_d();
158                 vector = ia64_get_ivr();
159         }
160         ia64_srlz_d();
161
162         /* kick off the clock on this AP */
163         pcpu_initclock();
164
165         ia64_set_tpr(0);
166         ia64_srlz_d();
167         enable_intr();
168
169         sched_throw(NULL);
170         /* NOTREACHED */
171 }
172
173 void
174 cpu_mp_setmaxid(void)
175 {
176
177         /*
178          * Count the number of processors in the system by walking the ACPI
179          * tables. Note that we record the actual number of processors, even
180          * if this is larger than MAXCPU. We only activate MAXCPU processors.
181          */
182         mp_ncpus = ia64_count_cpus();
183
184         /*
185          * Set the largest cpuid we're going to use. This is necessary for
186          * VM initialization.
187          */
188         mp_maxid = min(mp_ncpus, MAXCPU) - 1;
189 }
190
191 int
192 cpu_mp_probe(void)
193 {
194
195         /*
196          * If there's only 1 processor, or we don't have a wake-up vector,
197          * we're not going to enable SMP. Note that no wake-up vector can
198          * also mean that the wake-up mechanism is not supported. In this
199          * case we can have multiple processors, but we simply can't wake
200          * them up...
201          */
202         return (mp_ncpus > 1 && ipi_vector[IPI_AP_WAKEUP] != 0);
203 }
204
205 void
206 cpu_mp_add(u_int acpiid, u_int apicid, u_int apiceid)
207 {
208         struct pcpu *pc;
209         u_int64_t lid;
210         void *dpcpu;
211         u_int cpuid;
212
213         lid = LID_SAPIC_SET(apicid, apiceid);
214         cpuid = ((ia64_get_lid() & LID_SAPIC_MASK) == lid) ? 0 : smp_cpus++;
215
216         KASSERT((all_cpus & (1UL << cpuid)) == 0,
217             ("%s: cpu%d already in CPU map", __func__, acpiid));
218
219         if (cpuid != 0) {
220                 pc = (struct pcpu *)malloc(sizeof(*pc), M_SMP, M_WAITOK);
221                 pcpu_init(pc, cpuid, sizeof(*pc));
222                 dpcpu = (void *)kmem_alloc(kernel_map, DPCPU_SIZE);
223                 dpcpu_init(dpcpu, cpuid);
224         } else
225                 pc = pcpup;
226
227         pc->pc_acpi_id = acpiid;
228         pc->pc_lid = lid;
229         all_cpus |= (1UL << cpuid);
230 }
231
232 void
233 cpu_mp_announce()
234 {
235         struct pcpu *pc;
236         int i;
237
238         for (i = 0; i <= mp_maxid; i++) {
239                 pc = pcpu_find(i);
240                 if (pc != NULL) {
241                         printf("cpu%d: ACPI Id=%x, SAPIC Id=%x, SAPIC Eid=%x",
242                             i, pc->pc_acpi_id, LID_SAPIC_ID(pc->pc_lid),
243                             LID_SAPIC_EID(pc->pc_lid));
244                         if (i == 0)
245                                 printf(" (BSP)\n");
246                         else
247                                 printf("\n");
248                 }
249         }
250 }
251
252 void
253 cpu_mp_start()
254 {
255         struct pcpu *pc;
256
257         ap_spin = 1;
258
259         SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
260                 pc->pc_current_pmap = kernel_pmap;
261                 pc->pc_other_cpus = all_cpus & ~pc->pc_cpumask;
262                 if (pc->pc_cpuid > 0) {
263                         ap_pcpu = pc;
264                         ap_stack = malloc(KSTACK_PAGES * PAGE_SIZE, M_SMP,
265                             M_WAITOK);
266                         ap_vhpt = pmap_vhpt_base[pc->pc_cpuid];
267                         ap_delay = 2000;
268                         ap_awake = 0;
269
270                         if (bootverbose)
271                                 printf("SMP: waking up cpu%d\n", pc->pc_cpuid);
272
273                         ipi_send(pc, IPI_AP_WAKEUP);
274
275                         do {
276                                 DELAY(1000);
277                         } while (--ap_delay > 0);
278                         pc->pc_awake = ap_awake;
279
280                         if (!ap_awake)
281                                 printf("SMP: WARNING: cpu%d did not wake up\n",
282                                     pc->pc_cpuid);
283                 } else
284                         pc->pc_awake = 1;
285         }
286 }
287
288 static void
289 cpu_mp_unleash(void *dummy)
290 {
291         struct pcpu *pc;
292         int cpus;
293
294         if (mp_ncpus <= 1)
295                 return;
296
297         cpus = 0;
298         smp_cpus = 0;
299         SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
300                 cpus++;
301                 if (pc->pc_awake) {
302                         kproc_create(ia64_store_mca_state,
303                             (void*)((uintptr_t)pc->pc_cpuid), NULL, 0, 0,
304                             "mca %u", pc->pc_cpuid);
305                         smp_cpus++;
306                 }
307         }
308
309         ap_awake = 1;
310         ap_spin = 0;
311
312         while (ap_awake != smp_cpus)
313                 cpu_spinwait();
314
315         if (smp_cpus != cpus || cpus != mp_ncpus) {
316                 printf("SMP: %d CPUs found; %d CPUs usable; %d CPUs woken\n",
317                     mp_ncpus, cpus, smp_cpus);
318         }
319
320         smp_active = 1;
321         smp_started = 1;
322 }
323
324 /*
325  * send an IPI to a set of cpus.
326  */
327 void
328 ipi_selected(cpumask_t cpus, int ipi)
329 {
330         struct pcpu *pc;
331
332         SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
333                 if (cpus & pc->pc_cpumask)
334                         ipi_send(pc, ipi);
335         }
336 }
337
338 /*
339  * send an IPI to all CPUs EXCEPT myself.
340  */
341 void
342 ipi_all_but_self(int ipi)
343 {
344         struct pcpu *pc;
345
346         SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
347                 if (pc != pcpup)
348                         ipi_send(pc, ipi);
349         }
350 }
351
352 /*
353  * Send an IPI to the specified processor. The lid parameter holds the
354  * cr.lid (CR64) contents of the target processor. Only the id and eid
355  * fields are used here.
356  */
357 void
358 ipi_send(struct pcpu *cpu, int ipi)
359 {
360         volatile uint64_t *pipi;
361         uint64_t vector;
362
363         pipi = __MEMIO_ADDR(ia64_lapic_address |
364             ((cpu->pc_lid & LID_SAPIC_MASK) >> 12));
365         vector = (uint64_t)(ipi_vector[ipi] & 0xff);
366         KASSERT(vector != 0, ("IPI %d is not assigned a vector", ipi));
367         *pipi = vector;
368         CTR3(KTR_SMP, "ipi_send(%p, %ld), cpuid=%d", pipi, vector,
369             PCPU_GET(cpuid));
370 }
371
372 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, cpu_mp_unleash, NULL);