xref: /xnu-10002.61.3/osfmk/i386/lapic.c (revision 0f4c859e951fba394238ab619495c4e1d54d0f34) !
1 /*
2  * Copyright (c) 2008-2010 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /*
29  * @OSF_COPYRIGHT@
30  */
31 
32 #include <mach/mach_types.h>
33 #include <mach/kern_return.h>
34 
35 #include <i386/lapic.h>
36 #include <i386/cpuid.h>
37 #include <i386/proc_reg.h>
38 #include <i386/machine_cpu.h>
39 #include <i386/misc_protos.h>
40 #include <i386/mp.h>
41 #include <i386/postcode.h>
42 #include <i386/cpu_threads.h>
43 #include <i386/machine_routines.h>
44 #include <i386/tsc.h>
45 
46 #include <sys/kdebug.h>
47 
48 /* Base vector for local APIC interrupt sources */
49 int lapic_interrupt_base = LAPIC_DEFAULT_INTERRUPT_BASE;
50 
51 int             lapic_to_cpu[MAX_LAPICIDS];
52 int             cpu_to_lapic[MAX_CPUS];
53 
54 void
lapic_cpu_map_init(void)55 lapic_cpu_map_init(void)
56 {
57 	int     i;
58 
59 	for (i = 0; i < MAX_CPUS; i++) {
60 		cpu_to_lapic[i] = -1;
61 	}
62 	for (i = 0; i < MAX_LAPICIDS; i++) {
63 		lapic_to_cpu[i] = -1;
64 	}
65 }
66 
67 void
lapic_cpu_map(int apic_id,int cpu)68 lapic_cpu_map(int apic_id, int cpu)
69 {
70 	assert(apic_id < MAX_LAPICIDS);
71 	assert(cpu < MAX_CPUS);
72 	cpu_to_lapic[cpu] = apic_id;
73 	lapic_to_cpu[apic_id] = cpu;
74 }
75 
76 /*
77  * Retrieve the local apic ID a cpu.
78  *
79  * Returns the local apic ID for the given processor.
80  * If the processor does not exist or apic not configured, returns -1.
81  */
82 
83 uint32_t
ml_get_apicid(uint32_t cpu)84 ml_get_apicid(uint32_t cpu)
85 {
86 	if (cpu >= (uint32_t)MAX_CPUS) {
87 		return 0xFFFFFFFF;      /* Return -1 if cpu too big */
88 	}
89 	/* Return the apic ID (or -1 if not configured) */
90 	return (uint32_t)cpu_to_lapic[cpu];
91 }
92 
93 uint32_t
ml_get_cpuid(uint32_t lapic_index)94 ml_get_cpuid(uint32_t lapic_index)
95 {
96 	if (lapic_index >= (uint32_t)MAX_LAPICIDS) {
97 		return 0xFFFFFFFF;      /* Return -1 if cpu too big */
98 	}
99 	/* Return the cpu ID (or -1 if not configured) */
100 	return (uint32_t)lapic_to_cpu[lapic_index];
101 }
102