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;
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(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(vaddr
);
92 vaddr_cur
< trunc_page(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
, vaddr_cur
, vaddr_cur
+PAGE_SIZE
);
98 vm_page_create(paddr_cur
,paddr_cur
+PAGE_SIZE
);
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 boolean_t
fake_get_interrupts_enabled(void)
152 * The scheduler is not active on this cpu. There is no need to disable
153 * preemption. The current thread wont be dispatched on anhother cpu.
155 return((per_proc_info
[cpu_number()].cpu_flags
& turnEEon
) != 0);
158 boolean_t
fake_set_interrupts_enabled(boolean_t enable
)
160 boolean_t interrupt_state_prev
;
163 * The scheduler is not active on this cpu. There is no need to disable
164 * preemption. The current thread wont be dispatched on anhother cpu.
166 interrupt_state_prev
=
167 (per_proc_info
[cpu_number()].cpu_flags
& turnEEon
) != 0;
168 if (interrupt_state_prev
!= enable
)
169 per_proc_info
[cpu_number()].cpu_flags
^= turnEEon
;
170 return(interrupt_state_prev
);
173 /* Get Interrupts Enabled */
174 boolean_t
ml_get_interrupts_enabled(void)
176 if (per_proc_info
[cpu_number()].interrupts_enabled
== TRUE
)
177 return(get_interrupts_enabled());
179 return(fake_get_interrupts_enabled());
182 boolean_t
get_interrupts_enabled(void)
184 return((mfmsr() & MASK(MSR_EE
)) != 0);
187 /* Check if running at interrupt context */
188 boolean_t
ml_at_interrupt_context(void)
191 boolean_t current_state
;
193 current_state
= ml_set_interrupts_enabled(FALSE
);
194 ret
= (per_proc_info
[cpu_number()].istackptr
== 0);
195 ml_set_interrupts_enabled(current_state
);
199 /* Generate a fake interrupt */
200 void ml_cause_interrupt(void)
205 void ml_thread_policy(
208 unsigned policy_info
)
210 if ((policy_id
== MACHINE_GROUP
) &&
211 ((per_proc_info
[0].pf
.Available
) & pfSMPcap
))
212 thread_bind(thread
, master_processor
);
214 if (policy_info
& MACHINE_NETWORK_WORKLOOP
) {
215 spl_t s
= splsched();
219 thread
->sched_mode
|= TH_MODE_FORCEDPREEMPT
;
220 set_priority(thread
, thread
->priority
+ 1);
222 thread_unlock(thread
);
227 void machine_idle(void)
229 if (per_proc_info
[cpu_number()].interrupts_enabled
== TRUE
) {
235 * protect against a lost decrementer trap
236 * if the current decrementer value is negative
237 * by more than 10 ticks, re-arm it since it's
238 * unlikely to fire at this point... a hardware
239 * interrupt got us out of machine_idle and may
240 * also be contributing to this state
242 cur_decr
= isync_mfdec();
244 if (cur_decr
< -10) {
252 processor_t processor
)
254 (void)cpu_signal(processor
->slot_num
, SIGPwake
, 0, 0);
258 ml_processor_register(
259 ml_processor_info_t
*processor_info
,
260 processor_t
*processor
,
261 ipi_handler_t
*ipi_handler
)
266 if (processor_info
->boot_cpu
== FALSE
) {
267 if (cpu_register(&target_cpu
) != KERN_SUCCESS
)
270 /* boot_cpu is always 0 */
274 per_proc_info
[target_cpu
].cpu_id
= processor_info
->cpu_id
;
275 per_proc_info
[target_cpu
].start_paddr
= processor_info
->start_paddr
;
277 if(per_proc_info
[target_cpu
].pf
.Available
& pfCanNap
)
278 if(processor_info
->supports_nap
)
279 per_proc_info
[target_cpu
].pf
.Available
|= pfWillNap
;
281 if(processor_info
->time_base_enable
!= (void(*)(cpu_id_t
, boolean_t
))NULL
)
282 per_proc_info
[target_cpu
].time_base_enable
= processor_info
->time_base_enable
;
284 per_proc_info
[target_cpu
].time_base_enable
= (void(*)(cpu_id_t
, boolean_t
))NULL
;
286 if(target_cpu
== cpu_number())
287 __asm__
volatile("mtsprg 2,%0" : : "r" (per_proc_info
[target_cpu
].pf
.Available
)); /* Set live value */
289 *processor
= cpu_to_processor(target_cpu
);
290 *ipi_handler
= cpu_signal_handler
;
296 ml_enable_nap(int target_cpu
, boolean_t nap_enabled
)
298 boolean_t prev_value
= (per_proc_info
[target_cpu
].pf
.Available
& pfCanNap
) && (per_proc_info
[target_cpu
].pf
.Available
& pfWillNap
);
300 if(per_proc_info
[target_cpu
].pf
.Available
& pfCanNap
) { /* Can the processor nap? */
301 if (nap_enabled
) per_proc_info
[target_cpu
].pf
.Available
|= pfWillNap
; /* Is nap supported on this machine? */
302 else per_proc_info
[target_cpu
].pf
.Available
&= ~pfWillNap
; /* Clear if not */
305 if(target_cpu
== cpu_number())
306 __asm__
volatile("mtsprg 2,%0" : : "r" (per_proc_info
[target_cpu
].pf
.Available
)); /* Set live value */
312 ml_init_max_cpus(unsigned long max_cpus
)
314 boolean_t current_state
;
316 current_state
= ml_set_interrupts_enabled(FALSE
);
317 if (max_cpus_initialized
!= MAX_CPUS_SET
) {
318 if (max_cpus
> 0 && max_cpus
< NCPUS
)
319 machine_info
.max_cpus
= max_cpus
;
320 if (max_cpus_initialized
== MAX_CPUS_WAIT
)
321 wakeup((event_t
)&max_cpus_initialized
);
322 max_cpus_initialized
= MAX_CPUS_SET
;
324 (void) ml_set_interrupts_enabled(current_state
);
328 ml_get_max_cpus(void)
330 boolean_t current_state
;
332 current_state
= ml_set_interrupts_enabled(FALSE
);
333 if (max_cpus_initialized
!= MAX_CPUS_SET
) {
334 max_cpus_initialized
= MAX_CPUS_WAIT
;
335 assert_wait((event_t
)&max_cpus_initialized
, THREAD_UNINT
);
336 (void)thread_block(THREAD_CONTINUE_NULL
);
338 (void) ml_set_interrupts_enabled(current_state
);
339 return(machine_info
.max_cpus
);
343 ml_get_current_cpus(void)
345 return machine_info
.avail_cpus
;
349 ml_cpu_get_info(ml_cpu_info_t
*cpu_info
)
351 if (cpu_info
== 0) return;
353 cpu_info
->vector_unit
= (per_proc_info
[0].pf
.Available
& pfAltivec
) != 0;
354 cpu_info
->cache_line_size
= per_proc_info
[0].pf
.lineSize
;
355 cpu_info
->l1_icache_size
= per_proc_info
[0].pf
.l1iSize
;
356 cpu_info
->l1_dcache_size
= per_proc_info
[0].pf
.l1dSize
;
358 if (per_proc_info
[0].pf
.Available
& pfL2
) {
359 cpu_info
->l2_settings
= per_proc_info
[0].pf
.l2cr
;
360 cpu_info
->l2_cache_size
= per_proc_info
[0].pf
.l2Size
;
362 cpu_info
->l2_settings
= 0;
363 cpu_info
->l2_cache_size
= 0xFFFFFFFF;
365 if (per_proc_info
[0].pf
.Available
& pfL3
) {
366 cpu_info
->l3_settings
= per_proc_info
[0].pf
.l3cr
;
367 cpu_info
->l3_cache_size
= per_proc_info
[0].pf
.l3Size
;
369 cpu_info
->l3_settings
= 0;
370 cpu_info
->l3_cache_size
= 0xFFFFFFFF;
374 #define l2em 0x80000000
375 #define l3em 0x80000000
377 extern int real_ncpus
;
380 ml_enable_cache_level(int cache_level
, int enable
)
383 unsigned long available
, ccr
;
385 if (real_ncpus
!= 1) return -1;
387 available
= per_proc_info
[0].pf
.Available
;
389 if ((cache_level
== 2) && (available
& pfL2
)) {
390 ccr
= per_proc_info
[0].pf
.l2cr
;
391 old_mode
= (ccr
& l2em
) ? TRUE
: FALSE
;
392 if (old_mode
!= enable
) {
393 if (enable
) ccr
= per_proc_info
[0].pf
.l2crOriginal
;
395 per_proc_info
[0].pf
.l2cr
= ccr
;
402 if ((cache_level
== 3) && (available
& pfL3
)) {
403 ccr
= per_proc_info
[0].pf
.l3cr
;
404 old_mode
= (ccr
& l3em
) ? TRUE
: FALSE
;
405 if (old_mode
!= enable
) {
406 if (enable
) ccr
= per_proc_info
[0].pf
.l3crOriginal
;
408 per_proc_info
[0].pf
.l3cr
= ccr
;
419 init_ast_check(processor_t processor
)
424 processor_t processor
)
426 if ( processor
!= current_processor() &&
427 per_proc_info
[processor
->slot_num
].interrupts_enabled
== TRUE
)
428 cpu_signal(processor
->slot_num
, SIGPast
, NULL
, NULL
);
432 switch_to_shutdown_context(
434 void (*doshutdown
)(processor_t
),
435 processor_t processor
)
438 return((thread_t
)(per_proc_info
[cpu_number()].old_thread
));
446 boolean_t current_state
;
448 current_state
= ml_set_interrupts_enabled(FALSE
); /* Can't allow interruptions when mucking with per_proc flags */
449 mycpu
= cpu_number();
450 per_proc_info
[mycpu
].cpu_flags
|= traceBE
;
451 (void) ml_set_interrupts_enabled(current_state
);
459 boolean_t current_state
;
461 current_state
= ml_set_interrupts_enabled(FALSE
); /* Can't allow interruptions when mucking with per_proc flags */
462 mycpu
= cpu_number();
463 per_proc_info
[mycpu
].cpu_flags
&= ~traceBE
;
464 (void) ml_set_interrupts_enabled(current_state
);
471 int mycpu
= cpu_number();
472 return(per_proc_info
[mycpu
].cpu_flags
& traceBE
);