]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ppc/machine_routines.c
172e52aae85f430e8977a5929ff05e626c9363e2
[apple/xnu.git] / osfmk / ppc / machine_routines.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
13 * file.
14 *
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
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>
31 #include <ppc/pmap.h>
32 #include <ppc/proc_reg.h>
33 #include <kern/processor.h>
34
35 unsigned int max_cpus_initialized = 0;
36 extern int forcenap;
37
38 #define MAX_CPUS_SET 0x1
39 #define MAX_CPUS_WAIT 0x2
40
41 boolean_t get_interrupts_enabled(void);
42
43 /* Map memory map IO space */
44 vm_offset_t
45 ml_io_map(
46 vm_offset_t phys_addr,
47 vm_size_t size)
48 {
49 return(io_map(phys_addr,size));
50 }
51
52 /* static memory allocation */
53 vm_offset_t
54 ml_static_malloc(
55 vm_size_t size)
56 {
57 extern vm_offset_t static_memory_end;
58 extern boolean_t pmap_initialized;
59 vm_offset_t vaddr;
60
61 if (pmap_initialized)
62 return((vm_offset_t)NULL);
63 else {
64 vaddr = static_memory_end;
65 static_memory_end = round_page_32(vaddr+size);
66 return(vaddr);
67 }
68 }
69
70 vm_offset_t
71 ml_static_ptovirt(
72 vm_offset_t paddr)
73 {
74 extern vm_offset_t static_memory_end;
75 vm_offset_t vaddr;
76
77 /* Static memory is map V=R */
78 vaddr = paddr;
79 if ( (vaddr < static_memory_end) && (pmap_extract(kernel_pmap, vaddr)==paddr) )
80 return(vaddr);
81 else
82 return((vm_offset_t)NULL);
83 }
84
85 void
86 ml_static_mfree(
87 vm_offset_t vaddr,
88 vm_size_t size)
89 {
90 vm_offset_t paddr_cur, vaddr_cur;
91
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) {
97 vm_page_wire_count--;
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);
100 }
101 }
102 }
103
104 /* virtual to physical on wired pages */
105 vm_offset_t ml_vtophys(
106 vm_offset_t vaddr)
107 {
108 return(pmap_extract(kernel_pmap, vaddr));
109 }
110
111 /* Initialize Interrupt Handler */
112 void ml_install_interrupt_handler(
113 void *nub,
114 int source,
115 void *target,
116 IOInterruptHandler handler,
117 void *refCon)
118 {
119 int current_cpu;
120 boolean_t current_state;
121
122 current_cpu = cpu_number();
123 current_state = ml_get_interrupts_enabled();
124
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;
130
131 per_proc_info[current_cpu].interrupts_enabled = TRUE;
132 (void) ml_set_interrupts_enabled(current_state);
133
134 initialize_screen(0, kPEAcquireScreen);
135 }
136
137 /* Initialize Interrupts */
138 void ml_init_interrupt(void)
139 {
140 int current_cpu;
141 boolean_t current_state;
142
143 current_state = ml_get_interrupts_enabled();
144
145 current_cpu = cpu_number();
146 per_proc_info[current_cpu].interrupts_enabled = TRUE;
147 (void) ml_set_interrupts_enabled(current_state);
148 }
149
150 /* Get Interrupts Enabled */
151 boolean_t ml_get_interrupts_enabled(void)
152 {
153 return((mfmsr() & MASK(MSR_EE)) != 0);
154 }
155
156 /* Check if running at interrupt context */
157 boolean_t ml_at_interrupt_context(void)
158 {
159 boolean_t ret;
160 boolean_t current_state;
161
162 current_state = ml_set_interrupts_enabled(FALSE);
163 ret = (per_proc_info[cpu_number()].istackptr == 0);
164 ml_set_interrupts_enabled(current_state);
165 return(ret);
166 }
167
168 /* Generate a fake interrupt */
169 void ml_cause_interrupt(void)
170 {
171 CreateFakeIO();
172 }
173
174 void ml_thread_policy(
175 thread_t thread,
176 unsigned policy_id,
177 unsigned policy_info)
178 {
179 if ((policy_id == MACHINE_GROUP) &&
180 ((per_proc_info[0].pf.Available) & pfSMPcap))
181 thread_bind(thread, master_processor);
182
183 if (policy_info & MACHINE_NETWORK_WORKLOOP) {
184 spl_t s = splsched();
185
186 thread_lock(thread);
187
188 thread->sched_mode |= TH_MODE_FORCEDPREEMPT;
189 set_priority(thread, thread->priority + 1);
190
191 thread_unlock(thread);
192 splx(s);
193 }
194 }
195
196 void machine_idle(void)
197 {
198 if (per_proc_info[cpu_number()].interrupts_enabled == TRUE) {
199 int cur_decr;
200
201 machine_idle_ppc();
202
203 /*
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
210 */
211 cur_decr = isync_mfdec();
212
213 if (cur_decr < -10) {
214 mtdec(1);
215 }
216 }
217 }
218
219 void
220 machine_signal_idle(
221 processor_t processor)
222 {
223 (void)cpu_signal(processor->slot_num, SIGPwake, 0, 0);
224 }
225
226 kern_return_t
227 ml_processor_register(
228 ml_processor_info_t *processor_info,
229 processor_t *processor,
230 ipi_handler_t *ipi_handler)
231 {
232 kern_return_t ret;
233 int target_cpu, cpu;
234 int donap;
235
236 if (processor_info->boot_cpu == FALSE) {
237 if (cpu_register(&target_cpu) != KERN_SUCCESS)
238 return KERN_FAILURE;
239 } else {
240 /* boot_cpu is always 0 */
241 target_cpu = 0;
242 }
243
244 per_proc_info[target_cpu].cpu_id = processor_info->cpu_id;
245 per_proc_info[target_cpu].start_paddr = processor_info->start_paddr;
246
247 donap = processor_info->supports_nap; /* Assume we use requested nap */
248 if(forcenap) donap = forcenap - 1; /* If there was an override, use that */
249
250 if(per_proc_info[target_cpu].pf.Available & pfCanNap)
251 if(donap)
252 per_proc_info[target_cpu].pf.Available |= pfWillNap;
253
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;
256 else
257 per_proc_info[target_cpu].time_base_enable = (void(*)(cpu_id_t, boolean_t ))NULL;
258
259 if(target_cpu == cpu_number())
260 __asm__ volatile("mtsprg 2,%0" : : "r" (per_proc_info[target_cpu].pf.Available)); /* Set live value */
261
262 *processor = cpu_to_processor(target_cpu);
263 *ipi_handler = cpu_signal_handler;
264
265 return KERN_SUCCESS;
266 }
267
268 boolean_t
269 ml_enable_nap(int target_cpu, boolean_t nap_enabled)
270 {
271 boolean_t prev_value = (per_proc_info[target_cpu].pf.Available & pfCanNap) && (per_proc_info[target_cpu].pf.Available & pfWillNap);
272
273 if(forcenap) nap_enabled = forcenap - 1; /* If we are to force nap on or off, do it */
274
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 */
278 }
279
280 if(target_cpu == cpu_number())
281 __asm__ volatile("mtsprg 2,%0" : : "r" (per_proc_info[target_cpu].pf.Available)); /* Set live value */
282
283 return (prev_value);
284 }
285
286 void
287 ml_init_max_cpus(unsigned long max_cpus)
288 {
289 boolean_t current_state;
290
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;
298 }
299 (void) ml_set_interrupts_enabled(current_state);
300 }
301
302 int
303 ml_get_max_cpus(void)
304 {
305 boolean_t current_state;
306
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);
312 }
313 (void) ml_set_interrupts_enabled(current_state);
314 return(machine_info.max_cpus);
315 }
316
317 int
318 ml_get_current_cpus(void)
319 {
320 return machine_info.avail_cpus;
321 }
322
323 void
324 ml_cpu_get_info(ml_cpu_info_t *cpu_info)
325 {
326 if (cpu_info == 0) return;
327
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;
332
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;
336 } else {
337 cpu_info->l2_settings = 0;
338 cpu_info->l2_cache_size = 0xFFFFFFFF;
339 }
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;
343 } else {
344 cpu_info->l3_settings = 0;
345 cpu_info->l3_cache_size = 0xFFFFFFFF;
346 }
347 }
348
349 #define l2em 0x80000000
350 #define l3em 0x80000000
351
352 extern int real_ncpus;
353
354 int
355 ml_enable_cache_level(int cache_level, int enable)
356 {
357 int old_mode;
358 unsigned long available, ccr;
359
360 if (real_ncpus != 1) return -1;
361
362 available = per_proc_info[0].pf.Available;
363
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;
369 else ccr = 0;
370 per_proc_info[0].pf.l2cr = ccr;
371 cacheInit();
372 }
373
374 return old_mode;
375 }
376
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;
382 else ccr = 0;
383 per_proc_info[0].pf.l3cr = ccr;
384 cacheInit();
385 }
386
387 return old_mode;
388 }
389
390 return -1;
391 }
392
393 void
394 init_ast_check(processor_t processor)
395 {}
396
397 void
398 cause_ast_check(
399 processor_t processor)
400 {
401 if ( processor != current_processor() &&
402 per_proc_info[processor->slot_num].interrupts_enabled == TRUE )
403 cpu_signal(processor->slot_num, SIGPast, NULL, NULL);
404 }
405
406 thread_t
407 switch_to_shutdown_context(
408 thread_t thread,
409 void (*doshutdown)(processor_t),
410 processor_t processor)
411 {
412 CreateShutdownCTX();
413 return((thread_t)(per_proc_info[cpu_number()].old_thread));
414 }
415
416 int
417 set_be_bit()
418 {
419
420 int mycpu;
421 boolean_t current_state;
422
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);
427 return(1);
428 }
429
430 int
431 clr_be_bit()
432 {
433 int mycpu;
434 boolean_t current_state;
435
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);
440 return(1);
441 }
442
443 int
444 be_tracing()
445 {
446 int mycpu = cpu_number();
447 return(per_proc_info[mycpu].cpu_flags & traceBE);
448 }
449