2 * Copyright 2013, Paweł Dziepak, pdziepak@quarnos.org.
3 * Copyright 2002-2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
4 * Distributed under the terms of the MIT License.
6 * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
7 * Distributed under the terms of the NewOS License.
11 #include <boot/kernel_args.h>
18 #include <arch/atomic.h>
23 #include <arch/x86/apic.h>
24 #include <arch/x86/arch_smp.h>
25 #include <arch/x86/smp_priv.h>
26 #include <arch/x86/timer.h>
34 //#define TRACE_ARCH_SMP
36 # define TRACE(x) dprintf x
42 #define ICI_VECTOR 0xfd
45 static uint32 sCPUAPICIds
[SMP_MAX_CPUS
];
46 static uint32 sAPICVersions
[SMP_MAX_CPUS
];
50 x86_ici_interrupt(void *data
)
52 // genuine inter-cpu interrupt
53 int cpu
= smp_get_current_cpu();
54 TRACE(("inter-cpu interrupt on cpu %d\n", cpu
));
55 return smp_intercpu_int_handler(cpu
);
60 x86_spurious_interrupt(void *data
)
63 TRACE(("spurious interrupt on cpu %" B_PRId32
"\n", smp_get_current_cpu()));
65 // spurious interrupts must not be acknowledged as it does not expect
66 // a end of interrupt - if we still do it we would loose the next best
68 return B_HANDLED_INTERRUPT
;
73 x86_smp_error_interrupt(void *data
)
75 // smp error interrupt
76 TRACE(("smp error interrupt on cpu %" B_PRId32
"\n", smp_get_current_cpu()));
77 return B_HANDLED_INTERRUPT
;
82 x86_get_cpu_apic_id(int32 cpu
)
84 ASSERT(cpu
>= 0 && cpu
< SMP_MAX_CPUS
);
85 return sCPUAPICIds
[cpu
];
90 arch_smp_init(kernel_args
*args
)
92 TRACE(("%s: entry\n", __func__
));
94 if (!apic_available()) {
95 // if we don't have an apic we can't do smp
96 TRACE(("%s: apic not available for smp\n", __func__
));
100 // setup some globals
101 memcpy(sCPUAPICIds
, args
->arch_args
.cpu_apic_id
, sizeof(args
->arch_args
.cpu_apic_id
));
102 memcpy(sAPICVersions
, args
->arch_args
.cpu_apic_version
, sizeof(args
->arch_args
.cpu_apic_version
));
104 // set up the local apic on the boot cpu
105 arch_smp_per_cpu_init(args
, 0);
107 if (args
->num_cpus
> 1) {
108 // I/O interrupts start at ARCH_INTERRUPT_BASE, so all interrupts are shifted
109 reserve_io_interrupt_vectors(3, 0xfd - ARCH_INTERRUPT_BASE
,
111 install_io_interrupt_handler(0xfd - ARCH_INTERRUPT_BASE
, &x86_ici_interrupt
, NULL
, B_NO_LOCK_VECTOR
);
112 install_io_interrupt_handler(0xfe - ARCH_INTERRUPT_BASE
, &x86_smp_error_interrupt
, NULL
, B_NO_LOCK_VECTOR
);
113 install_io_interrupt_handler(0xff - ARCH_INTERRUPT_BASE
, &x86_spurious_interrupt
, NULL
, B_NO_LOCK_VECTOR
);
121 arch_smp_per_cpu_init(kernel_args
*args
, int32 cpu
)
123 // set up the local apic on the current cpu
124 TRACE(("arch_smp_init_percpu: setting up the apic on cpu %" B_PRId32
"\n",
126 apic_per_cpu_init(args
, cpu
);
128 // setup FPU and SSE if supported
136 arch_smp_send_multicast_ici(CPUSet
& cpuSet
)
139 if (are_interrupts_enabled())
140 panic("arch_smp_send_multicast_ici: called with interrupts enabled");
143 memory_write_barrier();
146 int32 cpuCount
= smp_get_num_cpus();
148 int32 logicalModeCPUs
;
149 if (x2apic_available())
150 logicalModeCPUs
= cpuCount
;
152 logicalModeCPUs
= std::min(cpuCount
, int32(8));
154 uint32 destination
= 0;
155 for (; i
< logicalModeCPUs
; i
++) {
156 if (cpuSet
.GetBit(i
) && i
!= smp_get_current_cpu())
157 destination
|= gCPU
[i
].arch
.logical_apic_id
;
160 uint32 mode
= ICI_VECTOR
| APIC_DELIVERY_MODE_FIXED
161 | APIC_INTR_COMMAND_1_ASSERT
162 | APIC_INTR_COMMAND_1_DEST_MODE_LOGICAL
163 | APIC_INTR_COMMAND_1_DEST_FIELD
;
165 while (!apic_interrupt_delivered())
167 apic_set_interrupt_command(destination
, mode
);
169 for (; i
< cpuCount
; i
++) {
170 if (cpuSet
.GetBit(i
)) {
171 uint32 destination
= sCPUAPICIds
[i
];
172 uint32 mode
= ICI_VECTOR
| APIC_DELIVERY_MODE_FIXED
173 | APIC_INTR_COMMAND_1_ASSERT
174 | APIC_INTR_COMMAND_1_DEST_MODE_PHYSICAL
175 | APIC_INTR_COMMAND_1_DEST_FIELD
;
177 while (!apic_interrupt_delivered())
179 apic_set_interrupt_command(destination
, mode
);
186 arch_smp_send_broadcast_ici(void)
189 if (are_interrupts_enabled())
190 panic("arch_smp_send_broadcast_ici: called with interrupts enabled");
193 memory_write_barrier();
195 uint32 mode
= ICI_VECTOR
| APIC_DELIVERY_MODE_FIXED
196 | APIC_INTR_COMMAND_1_ASSERT
197 | APIC_INTR_COMMAND_1_DEST_MODE_PHYSICAL
198 | APIC_INTR_COMMAND_1_DEST_ALL_BUT_SELF
;
200 while (!apic_interrupt_delivered())
202 apic_set_interrupt_command(0, mode
);
207 arch_smp_send_ici(int32 target_cpu
)
210 if (are_interrupts_enabled())
211 panic("arch_smp_send_ici: called with interrupts enabled");
214 memory_write_barrier();
216 uint32 destination
= sCPUAPICIds
[target_cpu
];
217 uint32 mode
= ICI_VECTOR
| APIC_DELIVERY_MODE_FIXED
218 | APIC_INTR_COMMAND_1_ASSERT
219 | APIC_INTR_COMMAND_1_DEST_MODE_PHYSICAL
220 | APIC_INTR_COMMAND_1_DEST_FIELD
;
222 while (!apic_interrupt_delivered())
224 apic_set_interrupt_command(destination
, mode
);