2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 #include <ppc/machine_routines.h>
26 #include <ppc/machine_cpu.h>
27 #include <ppc/exception.h>
28 #include <ppc/misc_protos.h>
29 #include <ppc/Firmware.h>
30 #include <vm/vm_page.h>
32 #include <ppc/proc_reg.h>
33 #include <kern/processor.h>
35 unsigned int max_cpus_initialized
= 0;
38 #define MAX_CPUS_SET 0x1
39 #define MAX_CPUS_WAIT 0x2
41 boolean_t
get_interrupts_enabled(void);
43 /* Map memory map IO space */
46 vm_offset_t phys_addr
,
49 return(io_map(phys_addr
,size
));
52 /* static memory allocation */
57 extern vm_offset_t static_memory_end
;
58 extern boolean_t pmap_initialized
;
62 return((vm_offset_t
)NULL
);
64 vaddr
= static_memory_end
;
65 static_memory_end
= round_page_32(vaddr
+size
);
74 extern vm_offset_t static_memory_end
;
77 /* Static memory is map V=R */
79 if ( (vaddr
< static_memory_end
) && (pmap_extract(kernel_pmap
, vaddr
)==paddr
) )
82 return((vm_offset_t
)NULL
);
90 vm_offset_t paddr_cur
, vaddr_cur
;
92 for (vaddr_cur
= round_page_32(vaddr
);
93 vaddr_cur
< trunc_page_32(vaddr
+size
);
94 vaddr_cur
+= PAGE_SIZE
) {
95 paddr_cur
= pmap_extract(kernel_pmap
, vaddr_cur
);
96 if (paddr_cur
!= (vm_offset_t
)NULL
) {
98 pmap_remove(kernel_pmap
, (addr64_t
)vaddr_cur
, (addr64_t
)(vaddr_cur
+PAGE_SIZE
));
99 vm_page_create(paddr_cur
>>12,(paddr_cur
+PAGE_SIZE
)>>12);
104 /* virtual to physical on wired pages */
105 vm_offset_t
ml_vtophys(
108 return(pmap_extract(kernel_pmap
, vaddr
));
111 /* Initialize Interrupt Handler */
112 void ml_install_interrupt_handler(
116 IOInterruptHandler handler
,
120 boolean_t current_state
;
122 current_cpu
= cpu_number();
123 current_state
= ml_get_interrupts_enabled();
125 per_proc_info
[current_cpu
].interrupt_nub
= nub
;
126 per_proc_info
[current_cpu
].interrupt_source
= source
;
127 per_proc_info
[current_cpu
].interrupt_target
= target
;
128 per_proc_info
[current_cpu
].interrupt_handler
= handler
;
129 per_proc_info
[current_cpu
].interrupt_refCon
= refCon
;
131 per_proc_info
[current_cpu
].interrupts_enabled
= TRUE
;
132 (void) ml_set_interrupts_enabled(current_state
);
134 initialize_screen(0, kPEAcquireScreen
);
137 /* Initialize Interrupts */
138 void ml_init_interrupt(void)
141 boolean_t current_state
;
143 current_state
= ml_get_interrupts_enabled();
145 current_cpu
= cpu_number();
146 per_proc_info
[current_cpu
].interrupts_enabled
= TRUE
;
147 (void) ml_set_interrupts_enabled(current_state
);
150 /* Get Interrupts Enabled */
151 boolean_t
ml_get_interrupts_enabled(void)
153 return((mfmsr() & MASK(MSR_EE
)) != 0);
156 /* Check if running at interrupt context */
157 boolean_t
ml_at_interrupt_context(void)
160 boolean_t current_state
;
162 current_state
= ml_set_interrupts_enabled(FALSE
);
163 ret
= (per_proc_info
[cpu_number()].istackptr
== 0);
164 ml_set_interrupts_enabled(current_state
);
168 /* Generate a fake interrupt */
169 void ml_cause_interrupt(void)
174 void ml_thread_policy(
177 unsigned policy_info
)
179 if ((policy_id
== MACHINE_GROUP
) &&
180 ((per_proc_info
[0].pf
.Available
) & pfSMPcap
))
181 thread_bind(thread
, master_processor
);
183 if (policy_info
& MACHINE_NETWORK_WORKLOOP
) {
184 spl_t s
= splsched();
188 thread
->sched_mode
|= TH_MODE_FORCEDPREEMPT
;
189 set_priority(thread
, thread
->priority
+ 1);
191 thread_unlock(thread
);
196 void machine_idle(void)
198 if (per_proc_info
[cpu_number()].interrupts_enabled
== TRUE
) {
204 * protect against a lost decrementer trap
205 * if the current decrementer value is negative
206 * by more than 10 ticks, re-arm it since it's
207 * unlikely to fire at this point... a hardware
208 * interrupt got us out of machine_idle and may
209 * also be contributing to this state
211 cur_decr
= isync_mfdec();
213 if (cur_decr
< -10) {
221 processor_t processor
)
223 (void)cpu_signal(processor
->slot_num
, SIGPwake
, 0, 0);
227 ml_processor_register(
228 ml_processor_info_t
*processor_info
,
229 processor_t
*processor
,
230 ipi_handler_t
*ipi_handler
)
236 if (processor_info
->boot_cpu
== FALSE
) {
237 if (cpu_register(&target_cpu
) != KERN_SUCCESS
)
240 /* boot_cpu is always 0 */
244 per_proc_info
[target_cpu
].cpu_id
= processor_info
->cpu_id
;
245 per_proc_info
[target_cpu
].start_paddr
= processor_info
->start_paddr
;
247 donap
= processor_info
->supports_nap
; /* Assume we use requested nap */
248 if(forcenap
) donap
= forcenap
- 1; /* If there was an override, use that */
250 if(per_proc_info
[target_cpu
].pf
.Available
& pfCanNap
)
252 per_proc_info
[target_cpu
].pf
.Available
|= pfWillNap
;
254 if(processor_info
->time_base_enable
!= (void(*)(cpu_id_t
, boolean_t
))NULL
)
255 per_proc_info
[target_cpu
].time_base_enable
= processor_info
->time_base_enable
;
257 per_proc_info
[target_cpu
].time_base_enable
= (void(*)(cpu_id_t
, boolean_t
))NULL
;
259 if(target_cpu
== cpu_number())
260 __asm__
volatile("mtsprg 2,%0" : : "r" (per_proc_info
[target_cpu
].pf
.Available
)); /* Set live value */
262 *processor
= cpu_to_processor(target_cpu
);
263 *ipi_handler
= cpu_signal_handler
;
269 ml_enable_nap(int target_cpu
, boolean_t nap_enabled
)
271 boolean_t prev_value
= (per_proc_info
[target_cpu
].pf
.Available
& pfCanNap
) && (per_proc_info
[target_cpu
].pf
.Available
& pfWillNap
);
273 if(forcenap
) nap_enabled
= forcenap
- 1; /* If we are to force nap on or off, do it */
275 if(per_proc_info
[target_cpu
].pf
.Available
& pfCanNap
) { /* Can the processor nap? */
276 if (nap_enabled
) per_proc_info
[target_cpu
].pf
.Available
|= pfWillNap
; /* Is nap supported on this machine? */
277 else per_proc_info
[target_cpu
].pf
.Available
&= ~pfWillNap
; /* Clear if not */
280 if(target_cpu
== cpu_number())
281 __asm__
volatile("mtsprg 2,%0" : : "r" (per_proc_info
[target_cpu
].pf
.Available
)); /* Set live value */
287 ml_init_max_cpus(unsigned long max_cpus
)
289 boolean_t current_state
;
291 current_state
= ml_set_interrupts_enabled(FALSE
);
292 if (max_cpus_initialized
!= MAX_CPUS_SET
) {
293 if (max_cpus
> 0 && max_cpus
< NCPUS
)
294 machine_info
.max_cpus
= max_cpus
;
295 if (max_cpus_initialized
== MAX_CPUS_WAIT
)
296 wakeup((event_t
)&max_cpus_initialized
);
297 max_cpus_initialized
= MAX_CPUS_SET
;
299 (void) ml_set_interrupts_enabled(current_state
);
303 ml_get_max_cpus(void)
305 boolean_t current_state
;
307 current_state
= ml_set_interrupts_enabled(FALSE
);
308 if (max_cpus_initialized
!= MAX_CPUS_SET
) {
309 max_cpus_initialized
= MAX_CPUS_WAIT
;
310 assert_wait((event_t
)&max_cpus_initialized
, THREAD_UNINT
);
311 (void)thread_block(THREAD_CONTINUE_NULL
);
313 (void) ml_set_interrupts_enabled(current_state
);
314 return(machine_info
.max_cpus
);
318 ml_get_current_cpus(void)
320 return machine_info
.avail_cpus
;
324 ml_cpu_get_info(ml_cpu_info_t
*cpu_info
)
326 if (cpu_info
== 0) return;
328 cpu_info
->vector_unit
= (per_proc_info
[0].pf
.Available
& pfAltivec
) != 0;
329 cpu_info
->cache_line_size
= per_proc_info
[0].pf
.lineSize
;
330 cpu_info
->l1_icache_size
= per_proc_info
[0].pf
.l1iSize
;
331 cpu_info
->l1_dcache_size
= per_proc_info
[0].pf
.l1dSize
;
333 if (per_proc_info
[0].pf
.Available
& pfL2
) {
334 cpu_info
->l2_settings
= per_proc_info
[0].pf
.l2cr
;
335 cpu_info
->l2_cache_size
= per_proc_info
[0].pf
.l2Size
;
337 cpu_info
->l2_settings
= 0;
338 cpu_info
->l2_cache_size
= 0xFFFFFFFF;
340 if (per_proc_info
[0].pf
.Available
& pfL3
) {
341 cpu_info
->l3_settings
= per_proc_info
[0].pf
.l3cr
;
342 cpu_info
->l3_cache_size
= per_proc_info
[0].pf
.l3Size
;
344 cpu_info
->l3_settings
= 0;
345 cpu_info
->l3_cache_size
= 0xFFFFFFFF;
349 #define l2em 0x80000000
350 #define l3em 0x80000000
352 extern int real_ncpus
;
355 ml_enable_cache_level(int cache_level
, int enable
)
358 unsigned long available
, ccr
;
360 if (real_ncpus
!= 1) return -1;
362 available
= per_proc_info
[0].pf
.Available
;
364 if ((cache_level
== 2) && (available
& pfL2
)) {
365 ccr
= per_proc_info
[0].pf
.l2cr
;
366 old_mode
= (ccr
& l2em
) ? TRUE
: FALSE
;
367 if (old_mode
!= enable
) {
368 if (enable
) ccr
= per_proc_info
[0].pf
.l2crOriginal
;
370 per_proc_info
[0].pf
.l2cr
= ccr
;
377 if ((cache_level
== 3) && (available
& pfL3
)) {
378 ccr
= per_proc_info
[0].pf
.l3cr
;
379 old_mode
= (ccr
& l3em
) ? TRUE
: FALSE
;
380 if (old_mode
!= enable
) {
381 if (enable
) ccr
= per_proc_info
[0].pf
.l3crOriginal
;
383 per_proc_info
[0].pf
.l3cr
= ccr
;
394 init_ast_check(processor_t processor
)
399 processor_t processor
)
401 if ( processor
!= current_processor() &&
402 per_proc_info
[processor
->slot_num
].interrupts_enabled
== TRUE
)
403 cpu_signal(processor
->slot_num
, SIGPast
, NULL
, NULL
);
407 switch_to_shutdown_context(
409 void (*doshutdown
)(processor_t
),
410 processor_t processor
)
413 return((thread_t
)(per_proc_info
[cpu_number()].old_thread
));
421 boolean_t current_state
;
423 current_state
= ml_set_interrupts_enabled(FALSE
); /* Can't allow interruptions when mucking with per_proc flags */
424 mycpu
= cpu_number();
425 per_proc_info
[mycpu
].cpu_flags
|= traceBE
;
426 (void) ml_set_interrupts_enabled(current_state
);
434 boolean_t current_state
;
436 current_state
= ml_set_interrupts_enabled(FALSE
); /* Can't allow interruptions when mucking with per_proc flags */
437 mycpu
= cpu_number();
438 per_proc_info
[mycpu
].cpu_flags
&= ~traceBE
;
439 (void) ml_set_interrupts_enabled(current_state
);
446 int mycpu
= cpu_number();
447 return(per_proc_info
[mycpu
].cpu_flags
& traceBE
);