2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 #include <i386/machine_routines.h>
24 #include <i386/io_map_entries.h>
25 #include <i386/cpuid.h>
27 #include <kern/processor.h>
28 #include <kern/machine.h>
29 #include <kern/cpu_data.h>
30 #include <kern/cpu_number.h>
31 #include <kern/thread.h>
32 #include <i386/cpu_data.h>
33 #include <i386/machine_cpu.h>
35 #include <i386/mp_events.h>
36 #include <i386/cpu_threads.h>
37 #include <i386/pmap.h>
38 #include <i386/misc_protos.h>
39 #include <mach/vm_param.h>
41 #define MIN(a,b) ((a)<(b)? (a) : (b))
43 extern void initialize_screen(Boot_Video
*, unsigned int);
44 extern void wakeup(void *);
46 static int max_cpus_initialized
= 0;
48 #define MAX_CPUS_SET 0x1
49 #define MAX_CPUS_WAIT 0x2
51 /* IO memory map services */
53 /* Map memory map IO space */
54 vm_offset_t
ml_io_map(
55 vm_offset_t phys_addr
,
58 return(io_map(phys_addr
,size
));
61 /* boot memory allocation */
62 vm_offset_t
ml_static_malloc(
63 __unused vm_size_t size
)
65 return((vm_offset_t
)NULL
);
72 return (vm_offset_t
)((unsigned) paddr
| LINEAR_KERNEL_ADDRESS
);
77 * Routine: ml_static_mfree
85 vm_offset_t vaddr_cur
;
88 if (vaddr
< VM_MIN_KERNEL_ADDRESS
) return;
90 assert((vaddr
& (PAGE_SIZE
-1)) == 0); /* must be page aligned */
92 for (vaddr_cur
= vaddr
;
93 vaddr_cur
< round_page_32(vaddr
+size
);
94 vaddr_cur
+= PAGE_SIZE
) {
95 ppn
= pmap_find_phys(kernel_pmap
, (addr64_t
)vaddr_cur
);
96 if (ppn
!= (vm_offset_t
)NULL
) {
97 pmap_remove(kernel_pmap
, (addr64_t
)vaddr_cur
, (addr64_t
)(vaddr_cur
+PAGE_SIZE
));
98 vm_page_create(ppn
,(ppn
+1));
104 /* virtual to physical on wired pages */
105 vm_offset_t
ml_vtophys(
108 return kvtophys(vaddr
);
111 /* Interrupt handling */
113 /* Initialize Interrupts */
114 void ml_init_interrupt(void)
116 (void) ml_set_interrupts_enabled(TRUE
);
119 /* Get Interrupts Enabled */
120 boolean_t
ml_get_interrupts_enabled(void)
124 __asm__
volatile("pushf; popl %0" : "=r" (flags
));
125 return (flags
& EFL_IF
) != 0;
128 /* Set Interrupts Enabled */
129 boolean_t
ml_set_interrupts_enabled(boolean_t enable
)
133 __asm__
volatile("pushf; popl %0" : "=r" (flags
));
136 __asm__
volatile("sti");
138 __asm__
volatile("cli");
140 return (flags
& EFL_IF
) != 0;
143 /* Check if running at interrupt context */
144 boolean_t
ml_at_interrupt_context(void)
146 return get_interrupt_level() != 0;
149 /* Generate a fake interrupt */
150 void ml_cause_interrupt(void)
152 panic("ml_cause_interrupt not defined yet on Intel");
155 void ml_thread_policy(
158 unsigned policy_info
)
160 if (policy_id
== MACHINE_GROUP
)
161 thread_bind(thread
, master_processor
);
163 if (policy_info
& MACHINE_NETWORK_WORKLOOP
) {
164 spl_t s
= splsched();
168 set_priority(thread
, thread
->priority
+ 1);
170 thread_unlock(thread
);
175 /* Initialize Interrupts */
176 void ml_install_interrupt_handler(
180 IOInterruptHandler handler
,
183 boolean_t current_state
;
185 current_state
= ml_get_interrupts_enabled();
187 PE_install_interrupt_handler(nub
, source
, target
,
188 (IOInterruptHandler
) handler
, refCon
);
190 (void) ml_set_interrupts_enabled(current_state
);
192 initialize_screen(0, kPEAcquireScreen
);
198 __asm__
volatile("sti; hlt": : :"memory");
200 void (*cpu_idle_handler
)(void) = cpu_idle
;
205 cpu_core_t
*my_core
= cpu_core();
209 * We halt this cpu thread
210 * unless kernel param idlehalt is false and no other thread
211 * in the same core is active - if so, don't halt so that this
212 * core doesn't go into a low-power mode.
214 others_active
= !atomic_decl_and_test(
215 (long *) &my_core
->active_threads
, 1);
216 if (idlehalt
|| others_active
) {
217 DBGLOG(cpu_handle
, cpu_number(), MP_IDLE
);
219 DBGLOG(cpu_handle
, cpu_number(), MP_UNIDLE
);
221 __asm__
volatile("sti");
223 atomic_incl((long *) &my_core
->active_threads
, 1);
228 processor_t processor
)
230 cpu_interrupt(PROCESSOR_DATA(processor
, slot_num
));
234 ml_processor_register(
237 processor_t
*processor_out
,
238 ipi_handler_t
*ipi_handler
,
242 cpu_data_t
*this_cpu_datap
;
244 this_cpu_datap
= cpu_data_alloc(boot_cpu
);
245 if (this_cpu_datap
== NULL
) {
248 target_cpu
= this_cpu_datap
->cpu_number
;
249 assert((boot_cpu
&& (target_cpu
== 0)) ||
250 (!boot_cpu
&& (target_cpu
!= 0)));
252 lapic_cpu_map(lapic_id
, target_cpu
);
254 this_cpu_datap
->cpu_id
= cpu_id
;
255 this_cpu_datap
->cpu_phys_number
= lapic_id
;
257 this_cpu_datap
->cpu_console_buf
= console_cpu_alloc(boot_cpu
);
258 if (this_cpu_datap
->cpu_console_buf
== NULL
)
262 this_cpu_datap
->cpu_pmap
= pmap_cpu_alloc(boot_cpu
);
263 if (this_cpu_datap
->cpu_pmap
== NULL
)
266 this_cpu_datap
->cpu_processor
= cpu_processor_alloc(boot_cpu
);
267 if (this_cpu_datap
->cpu_processor
== NULL
)
269 processor_init(this_cpu_datap
->cpu_processor
, target_cpu
);
272 *processor_out
= this_cpu_datap
->cpu_processor
;
278 cpu_processor_free(this_cpu_datap
->cpu_processor
);
279 pmap_cpu_free(this_cpu_datap
->cpu_pmap
);
280 console_cpu_free(this_cpu_datap
->cpu_console_buf
);
285 ml_cpu_get_info(ml_cpu_info_t
*cpu_infop
)
287 boolean_t os_supports_sse
;
288 i386_cpu_info_t
*cpuid_infop
;
290 if (cpu_infop
== NULL
)
294 * Are we supporting XMM/SSE/SSE2?
295 * As distinct from whether the cpu has these capabilities.
297 os_supports_sse
= get_cr4() & CR4_XMM
;
298 if ((cpuid_features() & CPUID_FEATURE_SSE2
) && os_supports_sse
)
299 cpu_infop
->vector_unit
= 4;
300 else if ((cpuid_features() & CPUID_FEATURE_SSE
) && os_supports_sse
)
301 cpu_infop
->vector_unit
= 3;
302 else if (cpuid_features() & CPUID_FEATURE_MMX
)
303 cpu_infop
->vector_unit
= 2;
305 cpu_infop
->vector_unit
= 0;
307 cpuid_infop
= cpuid_info();
309 cpu_infop
->cache_line_size
= cpuid_infop
->cache_linesize
;
311 cpu_infop
->l1_icache_size
= cpuid_infop
->cache_size
[L1I
];
312 cpu_infop
->l1_dcache_size
= cpuid_infop
->cache_size
[L1D
];
314 if (cpuid_infop
->cache_size
[L2U
] > 0) {
315 cpu_infop
->l2_settings
= 1;
316 cpu_infop
->l2_cache_size
= cpuid_infop
->cache_size
[L2U
];
318 cpu_infop
->l2_settings
= 0;
319 cpu_infop
->l2_cache_size
= 0xFFFFFFFF;
322 if (cpuid_infop
->cache_size
[L3U
] > 0) {
323 cpu_infop
->l2_settings
= 1;
324 cpu_infop
->l2_cache_size
= cpuid_infop
->cache_size
[L3U
];
326 cpu_infop
->l3_settings
= 0;
327 cpu_infop
->l3_cache_size
= 0xFFFFFFFF;
332 ml_init_max_cpus(unsigned long max_cpus
)
334 boolean_t current_state
;
336 current_state
= ml_set_interrupts_enabled(FALSE
);
337 if (max_cpus_initialized
!= MAX_CPUS_SET
) {
338 if (max_cpus
> 0 && max_cpus
<= MAX_CPUS
) {
340 * Note: max_cpus is the number of enable processors
341 * that ACPI found; max_ncpus is the maximum number
342 * that the kernel supports or that the "cpus="
343 * boot-arg has set. Here we take int minimum.
345 machine_info
.max_cpus
= MIN(max_cpus
, max_ncpus
);
347 if (max_cpus_initialized
== MAX_CPUS_WAIT
)
348 wakeup((event_t
)&max_cpus_initialized
);
349 max_cpus_initialized
= MAX_CPUS_SET
;
351 (void) ml_set_interrupts_enabled(current_state
);
355 ml_get_max_cpus(void)
357 boolean_t current_state
;
359 current_state
= ml_set_interrupts_enabled(FALSE
);
360 if (max_cpus_initialized
!= MAX_CPUS_SET
) {
361 max_cpus_initialized
= MAX_CPUS_WAIT
;
362 assert_wait((event_t
)&max_cpus_initialized
, THREAD_UNINT
);
363 (void)thread_block(THREAD_CONTINUE_NULL
);
365 (void) ml_set_interrupts_enabled(current_state
);
366 return(machine_info
.max_cpus
);
370 * This is called from the machine-independent routine cpu_up()
371 * to perform machine-dependent info updates. Defer to cpu_thread_init().
380 * This is called from the machine-independent routine cpu_down()
381 * to perform machine-dependent info updates.
389 /* Stubs for pc tracing mechanism */
392 int pc_trace_cnt
= 0;
413 * The following are required for parts of the kernel
414 * that cannot resolve these functions as inlines:
416 extern thread_t
current_act(void);
420 return(current_thread_fast());
423 #undef current_thread
424 extern thread_t
current_thread(void);
428 return(current_thread_fast());