]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ppc/machine_routines.c
xnu-517.7.7.tar.gz
[apple/xnu.git] / osfmk / ppc / machine_routines.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
e5568f75
A
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.
1c79356b 11 *
e5568f75
A
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
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
e5568f75
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
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>
28#include <ppc/pmap.h>
29#include <ppc/proc_reg.h>
30#include <kern/processor.h>
31
43866e37 32unsigned int max_cpus_initialized = 0;
ab86ba33
A
33unsigned int LockTimeOut = 12500000;
34unsigned int MutexSpin = 0;
55e303ae 35extern int forcenap;
43866e37
A
36
37#define MAX_CPUS_SET 0x1
38#define MAX_CPUS_WAIT 0x2
39
1c79356b 40boolean_t get_interrupts_enabled(void);
1c79356b
A
41
42/* Map memory map IO space */
43vm_offset_t
44ml_io_map(
45 vm_offset_t phys_addr,
46 vm_size_t size)
47{
48 return(io_map(phys_addr,size));
49}
50
51/* static memory allocation */
52vm_offset_t
53ml_static_malloc(
54 vm_size_t size)
55{
56 extern vm_offset_t static_memory_end;
57 extern boolean_t pmap_initialized;
58 vm_offset_t vaddr;
59
60 if (pmap_initialized)
61 return((vm_offset_t)NULL);
62 else {
63 vaddr = static_memory_end;
55e303ae 64 static_memory_end = round_page_32(vaddr+size);
1c79356b
A
65 return(vaddr);
66 }
67}
68
69vm_offset_t
70ml_static_ptovirt(
71 vm_offset_t paddr)
72{
73 extern vm_offset_t static_memory_end;
74 vm_offset_t vaddr;
75
76 /* Static memory is map V=R */
77 vaddr = paddr;
78 if ( (vaddr < static_memory_end) && (pmap_extract(kernel_pmap, vaddr)==paddr) )
79 return(vaddr);
80 else
81 return((vm_offset_t)NULL);
82}
83
84void
85ml_static_mfree(
86 vm_offset_t vaddr,
87 vm_size_t size)
88{
89 vm_offset_t paddr_cur, vaddr_cur;
90
55e303ae
A
91 for (vaddr_cur = round_page_32(vaddr);
92 vaddr_cur < trunc_page_32(vaddr+size);
1c79356b
A
93 vaddr_cur += PAGE_SIZE) {
94 paddr_cur = pmap_extract(kernel_pmap, vaddr_cur);
95 if (paddr_cur != (vm_offset_t)NULL) {
96 vm_page_wire_count--;
55e303ae
A
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);
1c79356b
A
99 }
100 }
101}
102
103/* virtual to physical on wired pages */
104vm_offset_t ml_vtophys(
105 vm_offset_t vaddr)
106{
107 return(pmap_extract(kernel_pmap, vaddr));
108}
109
110/* Initialize Interrupt Handler */
111void ml_install_interrupt_handler(
112 void *nub,
113 int source,
114 void *target,
115 IOInterruptHandler handler,
116 void *refCon)
117{
118 int current_cpu;
119 boolean_t current_state;
120
121 current_cpu = cpu_number();
122 current_state = ml_get_interrupts_enabled();
123
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;
129
0b4e3aa0 130 per_proc_info[current_cpu].interrupts_enabled = TRUE;
1c79356b 131 (void) ml_set_interrupts_enabled(current_state);
9bccf70c
A
132
133 initialize_screen(0, kPEAcquireScreen);
1c79356b
A
134}
135
136/* Initialize Interrupts */
137void ml_init_interrupt(void)
138{
139 int current_cpu;
140 boolean_t current_state;
141
142 current_state = ml_get_interrupts_enabled();
143
144 current_cpu = cpu_number();
0b4e3aa0 145 per_proc_info[current_cpu].interrupts_enabled = TRUE;
1c79356b
A
146 (void) ml_set_interrupts_enabled(current_state);
147}
148
1c79356b
A
149/* Get Interrupts Enabled */
150boolean_t ml_get_interrupts_enabled(void)
1c79356b
A
151{
152 return((mfmsr() & MASK(MSR_EE)) != 0);
153}
154
1c79356b
A
155/* Check if running at interrupt context */
156boolean_t ml_at_interrupt_context(void)
157{
0b4e3aa0
A
158 boolean_t ret;
159 boolean_t current_state;
160
161 current_state = ml_set_interrupts_enabled(FALSE);
162 ret = (per_proc_info[cpu_number()].istackptr == 0);
163 ml_set_interrupts_enabled(current_state);
164 return(ret);
1c79356b
A
165}
166
167/* Generate a fake interrupt */
168void ml_cause_interrupt(void)
169{
170 CreateFakeIO();
171}
172
9bccf70c 173void ml_thread_policy(
d52fe63f
A
174 thread_t thread,
175 unsigned policy_id,
176 unsigned policy_info)
177{
55e303ae
A
178 extern int srv;
179
d52fe63f 180 if ((policy_id == MACHINE_GROUP) &&
9bccf70c
A
181 ((per_proc_info[0].pf.Available) & pfSMPcap))
182 thread_bind(thread, master_processor);
183
184 if (policy_info & MACHINE_NETWORK_WORKLOOP) {
185 spl_t s = splsched();
186
187 thread_lock(thread);
188
55e303ae
A
189 if (srv == 0)
190 thread->sched_mode |= TH_MODE_FORCEDPREEMPT;
9bccf70c
A
191 set_priority(thread, thread->priority + 1);
192
193 thread_unlock(thread);
194 splx(s);
195 }
d52fe63f
A
196}
197
1c79356b
A
198void machine_idle(void)
199{
0b4e3aa0 200 if (per_proc_info[cpu_number()].interrupts_enabled == TRUE) {
1c79356b
A
201 int cur_decr;
202
203 machine_idle_ppc();
204
205 /*
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
212 */
213 cur_decr = isync_mfdec();
214
215 if (cur_decr < -10) {
216 mtdec(1);
217 }
218 }
219}
220
221void
222machine_signal_idle(
223 processor_t processor)
224{
55e303ae
A
225 if (per_proc_info[processor->slot_num].pf.Available & (pfCanDoze|pfWillNap))
226 (void)cpu_signal(processor->slot_num, SIGPwake, 0, 0);
1c79356b
A
227}
228
229kern_return_t
230ml_processor_register(
231 ml_processor_info_t *processor_info,
232 processor_t *processor,
233 ipi_handler_t *ipi_handler)
234{
235 kern_return_t ret;
55e303ae
A
236 int target_cpu, cpu;
237 int donap;
1c79356b
A
238
239 if (processor_info->boot_cpu == FALSE) {
240 if (cpu_register(&target_cpu) != KERN_SUCCESS)
241 return KERN_FAILURE;
242 } else {
243 /* boot_cpu is always 0 */
55e303ae 244 target_cpu = 0;
1c79356b
A
245 }
246
247 per_proc_info[target_cpu].cpu_id = processor_info->cpu_id;
248 per_proc_info[target_cpu].start_paddr = processor_info->start_paddr;
249
55e303ae
A
250 donap = processor_info->supports_nap; /* Assume we use requested nap */
251 if(forcenap) donap = forcenap - 1; /* If there was an override, use that */
252
1c79356b 253 if(per_proc_info[target_cpu].pf.Available & pfCanNap)
55e303ae 254 if(donap)
1c79356b
A
255 per_proc_info[target_cpu].pf.Available |= pfWillNap;
256
257 if(processor_info->time_base_enable != (void(*)(cpu_id_t, boolean_t ))NULL)
258 per_proc_info[target_cpu].time_base_enable = processor_info->time_base_enable;
259 else
260 per_proc_info[target_cpu].time_base_enable = (void(*)(cpu_id_t, boolean_t ))NULL;
261
262 if(target_cpu == cpu_number())
263 __asm__ volatile("mtsprg 2,%0" : : "r" (per_proc_info[target_cpu].pf.Available)); /* Set live value */
264
265 *processor = cpu_to_processor(target_cpu);
266 *ipi_handler = cpu_signal_handler;
267
268 return KERN_SUCCESS;
269}
270
271boolean_t
272ml_enable_nap(int target_cpu, boolean_t nap_enabled)
273{
274 boolean_t prev_value = (per_proc_info[target_cpu].pf.Available & pfCanNap) && (per_proc_info[target_cpu].pf.Available & pfWillNap);
275
55e303ae
A
276 if(forcenap) nap_enabled = forcenap - 1; /* If we are to force nap on or off, do it */
277
1c79356b
A
278 if(per_proc_info[target_cpu].pf.Available & pfCanNap) { /* Can the processor nap? */
279 if (nap_enabled) per_proc_info[target_cpu].pf.Available |= pfWillNap; /* Is nap supported on this machine? */
280 else per_proc_info[target_cpu].pf.Available &= ~pfWillNap; /* Clear if not */
281 }
282
283 if(target_cpu == cpu_number())
284 __asm__ volatile("mtsprg 2,%0" : : "r" (per_proc_info[target_cpu].pf.Available)); /* Set live value */
55e303ae 285
de355530 286 return (prev_value);
d7e50217
A
287}
288
289void
43866e37
A
290ml_init_max_cpus(unsigned long max_cpus)
291{
292 boolean_t current_state;
293
294 current_state = ml_set_interrupts_enabled(FALSE);
295 if (max_cpus_initialized != MAX_CPUS_SET) {
296 if (max_cpus > 0 && max_cpus < NCPUS)
297 machine_info.max_cpus = max_cpus;
298 if (max_cpus_initialized == MAX_CPUS_WAIT)
299 wakeup((event_t)&max_cpus_initialized);
300 max_cpus_initialized = MAX_CPUS_SET;
301 }
302 (void) ml_set_interrupts_enabled(current_state);
303}
304
305int
306ml_get_max_cpus(void)
307{
308 boolean_t current_state;
309
310 current_state = ml_set_interrupts_enabled(FALSE);
311 if (max_cpus_initialized != MAX_CPUS_SET) {
312 max_cpus_initialized = MAX_CPUS_WAIT;
313 assert_wait((event_t)&max_cpus_initialized, THREAD_UNINT);
314 (void)thread_block(THREAD_CONTINUE_NULL);
315 }
316 (void) ml_set_interrupts_enabled(current_state);
317 return(machine_info.max_cpus);
318}
319
43866e37
A
320void
321ml_cpu_get_info(ml_cpu_info_t *cpu_info)
1c79356b
A
322{
323 if (cpu_info == 0) return;
324
325 cpu_info->vector_unit = (per_proc_info[0].pf.Available & pfAltivec) != 0;
326 cpu_info->cache_line_size = per_proc_info[0].pf.lineSize;
327 cpu_info->l1_icache_size = per_proc_info[0].pf.l1iSize;
328 cpu_info->l1_dcache_size = per_proc_info[0].pf.l1dSize;
329
330 if (per_proc_info[0].pf.Available & pfL2) {
331 cpu_info->l2_settings = per_proc_info[0].pf.l2cr;
332 cpu_info->l2_cache_size = per_proc_info[0].pf.l2Size;
333 } else {
334 cpu_info->l2_settings = 0;
335 cpu_info->l2_cache_size = 0xFFFFFFFF;
336 }
337 if (per_proc_info[0].pf.Available & pfL3) {
338 cpu_info->l3_settings = per_proc_info[0].pf.l3cr;
339 cpu_info->l3_cache_size = per_proc_info[0].pf.l3Size;
340 } else {
341 cpu_info->l3_settings = 0;
342 cpu_info->l3_cache_size = 0xFFFFFFFF;
343 }
344}
345
d52fe63f
A
346#define l2em 0x80000000
347#define l3em 0x80000000
348
349extern int real_ncpus;
350
351int
352ml_enable_cache_level(int cache_level, int enable)
353{
354 int old_mode;
355 unsigned long available, ccr;
356
357 if (real_ncpus != 1) return -1;
358
359 available = per_proc_info[0].pf.Available;
360
361 if ((cache_level == 2) && (available & pfL2)) {
362 ccr = per_proc_info[0].pf.l2cr;
363 old_mode = (ccr & l2em) ? TRUE : FALSE;
364 if (old_mode != enable) {
365 if (enable) ccr = per_proc_info[0].pf.l2crOriginal;
366 else ccr = 0;
367 per_proc_info[0].pf.l2cr = ccr;
368 cacheInit();
369 }
370
371 return old_mode;
372 }
373
374 if ((cache_level == 3) && (available & pfL3)) {
375 ccr = per_proc_info[0].pf.l3cr;
376 old_mode = (ccr & l3em) ? TRUE : FALSE;
377 if (old_mode != enable) {
378 if (enable) ccr = per_proc_info[0].pf.l3crOriginal;
379 else ccr = 0;
380 per_proc_info[0].pf.l3cr = ccr;
381 cacheInit();
382 }
383
384 return old_mode;
385 }
386
387 return -1;
388}
389
ab86ba33
A
390void
391ml_init_lock_timeout(void)
392{
393 uint64_t abstime;
394 uint32_t mtxspin;
395
396 nanoseconds_to_absolutetime(NSEC_PER_SEC>>2, &abstime);
397 LockTimeOut = (unsigned int)abstime;
398
399 if (PE_parse_boot_arg("mtxspin", &mtxspin)) {
400 if (mtxspin > USEC_PER_SEC>>4)
401 mtxspin = USEC_PER_SEC>>4;
402 nanoseconds_to_absolutetime(mtxspin*NSEC_PER_USEC, &abstime);
403 } else {
404 nanoseconds_to_absolutetime(20*NSEC_PER_USEC, &abstime);
405 }
406 MutexSpin = (unsigned int)abstime;
407}
408
1c79356b
A
409void
410init_ast_check(processor_t processor)
411{}
412
413void
9bccf70c
A
414cause_ast_check(
415 processor_t processor)
1c79356b 416{
9bccf70c
A
417 if ( processor != current_processor() &&
418 per_proc_info[processor->slot_num].interrupts_enabled == TRUE )
1c79356b
A
419 cpu_signal(processor->slot_num, SIGPast, NULL, NULL);
420}
421
422thread_t
423switch_to_shutdown_context(
424 thread_t thread,
425 void (*doshutdown)(processor_t),
426 processor_t processor)
427{
1c79356b 428 CreateShutdownCTX();
1c79356b
A
429 return((thread_t)(per_proc_info[cpu_number()].old_thread));
430}
431
432int
433set_be_bit()
434{
435
436 int mycpu;
437 boolean_t current_state;
438
439 current_state = ml_set_interrupts_enabled(FALSE); /* Can't allow interruptions when mucking with per_proc flags */
440 mycpu = cpu_number();
441 per_proc_info[mycpu].cpu_flags |= traceBE;
442 (void) ml_set_interrupts_enabled(current_state);
443 return(1);
444}
445
446int
447clr_be_bit()
448{
449 int mycpu;
450 boolean_t current_state;
451
452 current_state = ml_set_interrupts_enabled(FALSE); /* Can't allow interruptions when mucking with per_proc flags */
453 mycpu = cpu_number();
454 per_proc_info[mycpu].cpu_flags &= ~traceBE;
455 (void) ml_set_interrupts_enabled(current_state);
456 return(1);
457}
458
459int
460be_tracing()
461{
462 int mycpu = cpu_number();
463 return(per_proc_info[mycpu].cpu_flags & traceBE);
464}
0b4e3aa0 465