2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 #include <ppc/machine_routines.h>
23 #include <ppc/machine_cpu.h>
24 #include <ppc/exception.h>
25 #include <ppc/misc_protos.h>
26 #include <ppc/Firmware.h>
27 #include <vm/vm_page.h>
29 #include <ppc/proc_reg.h>
30 #include <kern/processor.h>
32 unsigned int max_cpus_initialized
= 0;
33 unsigned int LockTimeOut
= 12500000;
34 unsigned int MutexSpin
= 0;
37 #define MAX_CPUS_SET 0x1
38 #define MAX_CPUS_WAIT 0x2
40 boolean_t
get_interrupts_enabled(void);
42 /* Map memory map IO space */
45 vm_offset_t phys_addr
,
48 return(io_map(phys_addr
,size
));
51 /* static memory allocation */
56 extern vm_offset_t static_memory_end
;
57 extern boolean_t pmap_initialized
;
61 return((vm_offset_t
)NULL
);
63 vaddr
= static_memory_end
;
64 static_memory_end
= round_page_32(vaddr
+size
);
73 extern vm_offset_t static_memory_end
;
76 /* Static memory is map V=R */
78 if ( (vaddr
< static_memory_end
) && (pmap_extract(kernel_pmap
, vaddr
)==paddr
) )
81 return((vm_offset_t
)NULL
);
89 vm_offset_t paddr_cur
, vaddr_cur
;
91 for (vaddr_cur
= round_page_32(vaddr
);
92 vaddr_cur
< trunc_page_32(vaddr
+size
);
93 vaddr_cur
+= PAGE_SIZE
) {
94 paddr_cur
= pmap_extract(kernel_pmap
, vaddr_cur
);
95 if (paddr_cur
!= (vm_offset_t
)NULL
) {
97 pmap_remove(kernel_pmap
, (addr64_t
)vaddr_cur
, (addr64_t
)(vaddr_cur
+PAGE_SIZE
));
98 vm_page_create(paddr_cur
>>12,(paddr_cur
+PAGE_SIZE
)>>12);
103 /* virtual to physical on wired pages */
104 vm_offset_t
ml_vtophys(
107 return(pmap_extract(kernel_pmap
, vaddr
));
110 /* Initialize Interrupt Handler */
111 void ml_install_interrupt_handler(
115 IOInterruptHandler handler
,
119 boolean_t current_state
;
121 current_cpu
= cpu_number();
122 current_state
= ml_get_interrupts_enabled();
124 per_proc_info
[current_cpu
].interrupt_nub
= nub
;
125 per_proc_info
[current_cpu
].interrupt_source
= source
;
126 per_proc_info
[current_cpu
].interrupt_target
= target
;
127 per_proc_info
[current_cpu
].interrupt_handler
= handler
;
128 per_proc_info
[current_cpu
].interrupt_refCon
= refCon
;
130 per_proc_info
[current_cpu
].interrupts_enabled
= TRUE
;
131 (void) ml_set_interrupts_enabled(current_state
);
133 initialize_screen(0, kPEAcquireScreen
);
136 /* Initialize Interrupts */
137 void ml_init_interrupt(void)
140 boolean_t current_state
;
142 current_state
= ml_get_interrupts_enabled();
144 current_cpu
= cpu_number();
145 per_proc_info
[current_cpu
].interrupts_enabled
= TRUE
;
146 (void) ml_set_interrupts_enabled(current_state
);
149 /* Get Interrupts Enabled */
150 boolean_t
ml_get_interrupts_enabled(void)
152 return((mfmsr() & MASK(MSR_EE
)) != 0);
155 /* Check if running at interrupt context */
156 boolean_t
ml_at_interrupt_context(void)
159 boolean_t current_state
;
161 current_state
= ml_set_interrupts_enabled(FALSE
);
162 ret
= (per_proc_info
[cpu_number()].istackptr
== 0);
163 ml_set_interrupts_enabled(current_state
);
167 /* Generate a fake interrupt */
168 void ml_cause_interrupt(void)
173 void ml_thread_policy(
176 unsigned policy_info
)
180 if ((policy_id
== MACHINE_GROUP
) &&
181 ((per_proc_info
[0].pf
.Available
) & pfSMPcap
))
182 thread_bind(thread
, master_processor
);
184 if (policy_info
& MACHINE_NETWORK_WORKLOOP
) {
185 spl_t s
= splsched();
190 thread
->sched_mode
|= TH_MODE_FORCEDPREEMPT
;
191 set_priority(thread
, thread
->priority
+ 1);
193 thread_unlock(thread
);
198 void machine_idle(void)
200 if (per_proc_info
[cpu_number()].interrupts_enabled
== TRUE
) {
206 * protect against a lost decrementer trap
207 * if the current decrementer value is negative
208 * by more than 10 ticks, re-arm it since it's
209 * unlikely to fire at this point... a hardware
210 * interrupt got us out of machine_idle and may
211 * also be contributing to this state
213 cur_decr
= isync_mfdec();
215 if (cur_decr
< -10) {
223 processor_t processor
)
225 if (per_proc_info
[processor
->slot_num
].pf
.Available
& (pfCanDoze
|pfWillNap
))
226 (void)cpu_signal(processor
->slot_num
, SIGPwake
, 0, 0);
230 ml_processor_register(
231 ml_processor_info_t
*processor_info
,
232 processor_t
*processor
,
233 ipi_handler_t
*ipi_handler
)
239 if (processor_info
->boot_cpu
== FALSE
) {
240 if (cpu_register(&target_cpu
) != KERN_SUCCESS
)
243 /* boot_cpu is always 0 */
247 per_proc_info
[target_cpu
].cpu_id
= processor_info
->cpu_id
;
248 per_proc_info
[target_cpu
].start_paddr
= processor_info
->start_paddr
;
250 if (per_proc_info
[target_cpu
].pf
.pfPowerModes
& pmPowerTune
) {
251 per_proc_info
[target_cpu
].pf
.pfPowerTune0
= processor_info
->power_mode_0
;
252 per_proc_info
[target_cpu
].pf
.pfPowerTune1
= processor_info
->power_mode_1
;
255 donap
= processor_info
->supports_nap
; /* Assume we use requested nap */
256 if(forcenap
) donap
= forcenap
- 1; /* If there was an override, use that */
258 if(per_proc_info
[target_cpu
].pf
.Available
& pfCanNap
)
260 per_proc_info
[target_cpu
].pf
.Available
|= pfWillNap
;
262 if(processor_info
->time_base_enable
!= (void(*)(cpu_id_t
, boolean_t
))NULL
)
263 per_proc_info
[target_cpu
].time_base_enable
= processor_info
->time_base_enable
;
265 per_proc_info
[target_cpu
].time_base_enable
= (void(*)(cpu_id_t
, boolean_t
))NULL
;
267 if(target_cpu
== cpu_number())
268 __asm__
volatile("mtsprg 2,%0" : : "r" (per_proc_info
[target_cpu
].pf
.Available
)); /* Set live value */
270 *processor
= cpu_to_processor(target_cpu
);
271 *ipi_handler
= cpu_signal_handler
;
277 ml_enable_nap(int target_cpu
, boolean_t nap_enabled
)
279 boolean_t prev_value
= (per_proc_info
[target_cpu
].pf
.Available
& pfCanNap
) && (per_proc_info
[target_cpu
].pf
.Available
& pfWillNap
);
281 if(forcenap
) nap_enabled
= forcenap
- 1; /* If we are to force nap on or off, do it */
283 if(per_proc_info
[target_cpu
].pf
.Available
& pfCanNap
) { /* Can the processor nap? */
284 if (nap_enabled
) per_proc_info
[target_cpu
].pf
.Available
|= pfWillNap
; /* Is nap supported on this machine? */
285 else per_proc_info
[target_cpu
].pf
.Available
&= ~pfWillNap
; /* Clear if not */
288 if(target_cpu
== cpu_number())
289 __asm__
volatile("mtsprg 2,%0" : : "r" (per_proc_info
[target_cpu
].pf
.Available
)); /* Set live value */
295 ml_init_max_cpus(unsigned long max_cpus
)
297 boolean_t current_state
;
299 current_state
= ml_set_interrupts_enabled(FALSE
);
300 if (max_cpus_initialized
!= MAX_CPUS_SET
) {
301 if (max_cpus
> 0 && max_cpus
< NCPUS
)
302 machine_info
.max_cpus
= max_cpus
;
303 if (max_cpus_initialized
== MAX_CPUS_WAIT
)
304 wakeup((event_t
)&max_cpus_initialized
);
305 max_cpus_initialized
= MAX_CPUS_SET
;
307 (void) ml_set_interrupts_enabled(current_state
);
311 ml_get_max_cpus(void)
313 boolean_t current_state
;
315 current_state
= ml_set_interrupts_enabled(FALSE
);
316 if (max_cpus_initialized
!= MAX_CPUS_SET
) {
317 max_cpus_initialized
= MAX_CPUS_WAIT
;
318 assert_wait((event_t
)&max_cpus_initialized
, THREAD_UNINT
);
319 (void)thread_block(THREAD_CONTINUE_NULL
);
321 (void) ml_set_interrupts_enabled(current_state
);
322 return(machine_info
.max_cpus
);
326 ml_cpu_get_info(ml_cpu_info_t
*cpu_info
)
328 if (cpu_info
== 0) return;
330 cpu_info
->vector_unit
= (per_proc_info
[0].pf
.Available
& pfAltivec
) != 0;
331 cpu_info
->cache_line_size
= per_proc_info
[0].pf
.lineSize
;
332 cpu_info
->l1_icache_size
= per_proc_info
[0].pf
.l1iSize
;
333 cpu_info
->l1_dcache_size
= per_proc_info
[0].pf
.l1dSize
;
335 if (per_proc_info
[0].pf
.Available
& pfL2
) {
336 cpu_info
->l2_settings
= per_proc_info
[0].pf
.l2cr
;
337 cpu_info
->l2_cache_size
= per_proc_info
[0].pf
.l2Size
;
339 cpu_info
->l2_settings
= 0;
340 cpu_info
->l2_cache_size
= 0xFFFFFFFF;
342 if (per_proc_info
[0].pf
.Available
& pfL3
) {
343 cpu_info
->l3_settings
= per_proc_info
[0].pf
.l3cr
;
344 cpu_info
->l3_cache_size
= per_proc_info
[0].pf
.l3Size
;
346 cpu_info
->l3_settings
= 0;
347 cpu_info
->l3_cache_size
= 0xFFFFFFFF;
351 #define l2em 0x80000000
352 #define l3em 0x80000000
354 extern int real_ncpus
;
357 ml_enable_cache_level(int cache_level
, int enable
)
360 unsigned long available
, ccr
;
362 if (real_ncpus
!= 1) return -1;
364 available
= per_proc_info
[0].pf
.Available
;
366 if ((cache_level
== 2) && (available
& pfL2
)) {
367 ccr
= per_proc_info
[0].pf
.l2cr
;
368 old_mode
= (ccr
& l2em
) ? TRUE
: FALSE
;
369 if (old_mode
!= enable
) {
370 if (enable
) ccr
= per_proc_info
[0].pf
.l2crOriginal
;
372 per_proc_info
[0].pf
.l2cr
= ccr
;
379 if ((cache_level
== 3) && (available
& pfL3
)) {
380 ccr
= per_proc_info
[0].pf
.l3cr
;
381 old_mode
= (ccr
& l3em
) ? TRUE
: FALSE
;
382 if (old_mode
!= enable
) {
383 if (enable
) ccr
= per_proc_info
[0].pf
.l3crOriginal
;
385 per_proc_info
[0].pf
.l3cr
= ccr
;
396 ml_init_lock_timeout(void)
401 nanoseconds_to_absolutetime(NSEC_PER_SEC
>>2, &abstime
);
402 LockTimeOut
= (unsigned int)abstime
;
404 if (PE_parse_boot_arg("mtxspin", &mtxspin
)) {
405 if (mtxspin
> USEC_PER_SEC
>>4)
406 mtxspin
= USEC_PER_SEC
>>4;
407 nanoseconds_to_absolutetime(mtxspin
*NSEC_PER_USEC
, &abstime
);
409 nanoseconds_to_absolutetime(20*NSEC_PER_USEC
, &abstime
);
411 MutexSpin
= (unsigned int)abstime
;
415 init_ast_check(processor_t processor
)
420 processor_t processor
)
422 if ( processor
!= current_processor() &&
423 per_proc_info
[processor
->slot_num
].interrupts_enabled
== TRUE
)
424 cpu_signal(processor
->slot_num
, SIGPast
, NULL
, NULL
);
428 switch_to_shutdown_context(
430 void (*doshutdown
)(processor_t
),
431 processor_t processor
)
434 return((thread_t
)(per_proc_info
[cpu_number()].old_thread
));
442 boolean_t current_state
;
444 current_state
= ml_set_interrupts_enabled(FALSE
); /* Can't allow interruptions when mucking with per_proc flags */
445 mycpu
= cpu_number();
446 per_proc_info
[mycpu
].cpu_flags
|= traceBE
;
447 (void) ml_set_interrupts_enabled(current_state
);
455 boolean_t current_state
;
457 current_state
= ml_set_interrupts_enabled(FALSE
); /* Can't allow interruptions when mucking with per_proc flags */
458 mycpu
= cpu_number();
459 per_proc_info
[mycpu
].cpu_flags
&= ~traceBE
;
460 (void) ml_set_interrupts_enabled(current_state
);
467 int mycpu
= cpu_number();
468 return(per_proc_info
[mycpu
].cpu_flags
& traceBE
);