xref: /haiku/src/system/kernel/arch/x86/apic.cpp (revision 541ff51a6ef4c47f8ab105ba6ff895cdbba83aca)
1 /*
2  * Copyright 2010, Michael Lotz, mmlr@mlotz.ch. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Copyright 2002-2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
6  * Distributed under the terms of the MIT License.
7  *
8  * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
9  * Distributed under the terms of the NewOS License.
10  */
11 
12 #include <arch/x86/apic.h>
13 #include <arch/x86/msi.h>
14 
15 #include <debug.h>
16 #include <vm/vm.h>
17 
18 #include "timers/apic_timer.h"
19 
20 
21 static void *sLocalAPIC = NULL;
22 
23 
24 bool
25 apic_available()
26 {
27 	return sLocalAPIC != NULL;
28 }
29 
30 
31 uint32
32 apic_read(uint32 offset)
33 {
34 	return *(volatile uint32 *)((char *)sLocalAPIC + offset);
35 }
36 
37 
38 void
39 apic_write(uint32 offset, uint32 data)
40 {
41 	*(volatile uint32 *)((char *)sLocalAPIC + offset) = data;
42 }
43 
44 
45 uint32
46 apic_local_id()
47 {
48 	return (apic_read(APIC_ID) & 0xffffffff) >> 24;
49 }
50 
51 
52 void
53 apic_end_of_interrupt()
54 {
55 	apic_write(APIC_EOI, 0);
56 }
57 
58 
59 void
60 apic_disable_local_ints()
61 {
62 	// just clear them out completely
63 	apic_write(APIC_LVT_LINT0, APIC_LVT_MASKED);
64 	apic_write(APIC_LVT_LINT1, APIC_LVT_MASKED);
65 }
66 
67 
68 status_t
69 apic_init(kernel_args *args)
70 {
71 	if (args->arch_args.apic == NULL)
72 		return B_NO_INIT;
73 
74 	sLocalAPIC = args->arch_args.apic;
75 	dprintf("mapping local apic at %p\n", sLocalAPIC);
76 	if (vm_map_physical_memory(B_SYSTEM_TEAM, "local apic", &sLocalAPIC,
77 			B_EXACT_ADDRESS, B_PAGE_SIZE,
78 			B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA,
79 			args->arch_args.apic_phys, true) < 0) {
80 		panic("mapping the local apic failed");
81 		return B_ERROR;
82 	}
83 
84 	return B_OK;
85 }
86 
87 
88 status_t
89 apic_per_cpu_init(kernel_args *args, int32 cpu)
90 {
91 	dprintf("setting up apic for CPU %ld: apic id %lu, version %lu\n", cpu,
92 		apic_local_id(), apic_read(APIC_VERSION));
93 
94 	/* set spurious interrupt vector to 0xff */
95 	uint32 config = apic_read(APIC_SPURIOUS_INTR_VECTOR) & 0xffffff00;
96 	config |= APIC_ENABLE | 0xff;
97 	apic_write(APIC_SPURIOUS_INTR_VECTOR, config);
98 
99 	// don't touch the LINT0/1 configuration in virtual wire mode
100 	// ToDo: implement support for other modes...
101 #if 0
102 	if (cpu == 0) {
103 		/* setup LINT0 as ExtINT */
104 		config = (apic_read(APIC_LINT0) & 0xffff00ff);
105 		config |= APIC_LVT_DM_ExtINT | APIC_LVT_IIPP | APIC_LVT_TM;
106 		apic_write(APIC_LINT0, config);
107 
108 		/* setup LINT1 as NMI */
109 		config = (apic_read(APIC_LINT1) & 0xffff00ff);
110 		config |= APIC_LVT_DM_NMI | APIC_LVT_IIPP;
111 		apic_write(APIC_LINT1, config);
112 	}
113 	if (cpu > 0) {
114 		dprintf("LINT0: %p\n", (void *)apic_read(APIC_LINT0));
115 		dprintf("LINT1: %p\n", (void *)apic_read(APIC_LINT1));
116 
117 		/* disable LINT0/1 */
118 		config = apic_read(APIC_LINT0);
119 		apic_write(APIC_LINT0, config | APIC_LVT_MASKED);
120 
121 		config = apic_read(APIC_LINT1);
122 		apic_write(APIC_LINT1, config | APIC_LVT_MASKED);
123 	} else {
124 		dprintf("0: LINT0: %p\n", (void *)apic_read(APIC_LINT0));
125 		dprintf("0: LINT1: %p\n", (void *)apic_read(APIC_LINT1));
126 	}
127 #endif
128 
129 	apic_timer_per_cpu_init(args, cpu);
130 
131 	/* setup error vector to 0xfe */
132 	config = (apic_read(APIC_LVT_ERROR) & 0xffffff00) | 0xfe;
133 	apic_write(APIC_LVT_ERROR, config);
134 
135 	/* accept all interrupts */
136 	config = apic_read(APIC_TASK_PRIORITY) & 0xffffff00;
137 	apic_write(APIC_TASK_PRIORITY, config);
138 
139 	config = apic_read(APIC_SPURIOUS_INTR_VECTOR);
140 	apic_end_of_interrupt();
141 
142 	return B_OK;
143 }
144