1 | /*
|
---|
2 | * Copyright (c) 2005 Ondrej Palkovsky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup amd64
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <arch.h>
|
---|
36 | #include <arch/arch.h>
|
---|
37 | #include <stdint.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <mem.h>
|
---|
40 | #include <interrupt.h>
|
---|
41 | #include <console/console.h>
|
---|
42 | #include <syscall/syscall.h>
|
---|
43 | #include <sysinfo/sysinfo.h>
|
---|
44 | #include <arch/bios/bios.h>
|
---|
45 | #include <arch/boot/boot.h>
|
---|
46 | #include <arch/drivers/i8254.h>
|
---|
47 | #include <arch/drivers/i8259.h>
|
---|
48 | #include <arch/syscall.h>
|
---|
49 | #include <genarch/acpi/acpi.h>
|
---|
50 | #include <genarch/drivers/ega/ega.h>
|
---|
51 | #include <genarch/drivers/i8042/i8042.h>
|
---|
52 | #include <genarch/drivers/ns16550/ns16550.h>
|
---|
53 | #include <genarch/drivers/legacy/ia32/io.h>
|
---|
54 | #include <genarch/fb/bfb.h>
|
---|
55 | #include <genarch/kbrd/kbrd.h>
|
---|
56 | #include <genarch/srln/srln.h>
|
---|
57 | #include <genarch/multiboot/multiboot.h>
|
---|
58 | #include <genarch/multiboot/multiboot2.h>
|
---|
59 | #include <arch/pm.h>
|
---|
60 | #include <arch/vreg.h>
|
---|
61 | #include <arch/kseg.h>
|
---|
62 |
|
---|
63 | #ifdef CONFIG_SMP
|
---|
64 | #include <arch/smp/apic.h>
|
---|
65 | #endif
|
---|
66 |
|
---|
67 | static void amd64_pre_mm_init(void);
|
---|
68 | static void amd64_post_mm_init(void);
|
---|
69 | static void amd64_post_cpu_init(void);
|
---|
70 | static void amd64_pre_smp_init(void);
|
---|
71 | static void amd64_post_smp_init(void);
|
---|
72 |
|
---|
73 | arch_ops_t amd64_ops = {
|
---|
74 | .pre_mm_init = amd64_pre_mm_init,
|
---|
75 | .post_mm_init = amd64_post_mm_init,
|
---|
76 | .post_cpu_init = amd64_post_cpu_init,
|
---|
77 | .pre_smp_init = amd64_pre_smp_init,
|
---|
78 | .post_smp_init = amd64_post_smp_init
|
---|
79 | };
|
---|
80 |
|
---|
81 | arch_ops_t *arch_ops = &amd64_ops;
|
---|
82 |
|
---|
83 | /** Perform amd64-specific initialization before main_bsp() is called.
|
---|
84 | *
|
---|
85 | * @param signature Multiboot signature.
|
---|
86 | * @param info Multiboot information structure.
|
---|
87 | *
|
---|
88 | */
|
---|
89 | void amd64_pre_main(uint32_t signature, void *info)
|
---|
90 | {
|
---|
91 | /* Parse multiboot information obtained from the bootloader. */
|
---|
92 | multiboot_info_parse(signature, (multiboot_info_t *) info);
|
---|
93 | multiboot2_info_parse(signature, (multiboot2_info_t *) info);
|
---|
94 |
|
---|
95 | #ifdef CONFIG_SMP
|
---|
96 | /* Copy AP bootstrap routines below 1 MB. */
|
---|
97 | memcpy((void *) AP_BOOT_OFFSET, (void *) BOOT_OFFSET,
|
---|
98 | (size_t) &_hardcoded_unmapped_size);
|
---|
99 | #endif
|
---|
100 | }
|
---|
101 |
|
---|
102 | void amd64_pre_mm_init(void)
|
---|
103 | {
|
---|
104 | /* Enable no-execute pages */
|
---|
105 | write_msr(AMD_MSR_EFER, read_msr(AMD_MSR_EFER) | AMD_NXE);
|
---|
106 | /* Enable FPU */
|
---|
107 | cpu_setup_fpu();
|
---|
108 |
|
---|
109 | /* Initialize segmentation */
|
---|
110 | pm_init();
|
---|
111 |
|
---|
112 | /* Disable I/O on nonprivileged levels, clear the nested-thread flag */
|
---|
113 | write_rflags(read_rflags() & ~(RFLAGS_IOPL | RFLAGS_NT));
|
---|
114 | /* Disable alignment check */
|
---|
115 | write_cr0(read_cr0() & ~CR0_AM);
|
---|
116 |
|
---|
117 | if (config.cpu_active == 1) {
|
---|
118 | interrupt_init();
|
---|
119 | bios_init();
|
---|
120 |
|
---|
121 | /* PIC */
|
---|
122 | i8259_init();
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | void amd64_post_mm_init(void)
|
---|
127 | {
|
---|
128 | vreg_init();
|
---|
129 | kseg_init();
|
---|
130 |
|
---|
131 | if (config.cpu_active == 1) {
|
---|
132 | /* Initialize IRQ routing */
|
---|
133 | irq_init(IRQ_COUNT, IRQ_COUNT);
|
---|
134 |
|
---|
135 | /* hard clock */
|
---|
136 | i8254_init();
|
---|
137 |
|
---|
138 | #if (defined(CONFIG_FB) || defined(CONFIG_EGA))
|
---|
139 | bool bfb = false;
|
---|
140 | #endif
|
---|
141 |
|
---|
142 | #ifdef CONFIG_FB
|
---|
143 | bfb = bfb_init();
|
---|
144 | #endif
|
---|
145 |
|
---|
146 | #ifdef CONFIG_EGA
|
---|
147 | if (!bfb) {
|
---|
148 | outdev_t *egadev = ega_init(EGA_BASE, EGA_VIDEORAM);
|
---|
149 | if (egadev)
|
---|
150 | stdout_wire(egadev);
|
---|
151 | }
|
---|
152 | #endif
|
---|
153 |
|
---|
154 | /* Merge all memory zones to 1 big zone */
|
---|
155 | zone_merge_all();
|
---|
156 | }
|
---|
157 |
|
---|
158 | /* Setup fast SYSCALL/SYSRET */
|
---|
159 | syscall_setup_cpu();
|
---|
160 | }
|
---|
161 |
|
---|
162 | void amd64_post_cpu_init(void)
|
---|
163 | {
|
---|
164 | #ifdef CONFIG_SMP
|
---|
165 | if (config.cpu_active > 1) {
|
---|
166 | l_apic_init();
|
---|
167 | l_apic_debug();
|
---|
168 | }
|
---|
169 | #endif
|
---|
170 | }
|
---|
171 |
|
---|
172 | void amd64_pre_smp_init(void)
|
---|
173 | {
|
---|
174 | if (config.cpu_active == 1) {
|
---|
175 | #ifdef CONFIG_SMP
|
---|
176 | acpi_init();
|
---|
177 | #endif /* CONFIG_SMP */
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | void amd64_post_smp_init(void)
|
---|
182 | {
|
---|
183 | /* Currently the only supported platform for amd64 is 'pc'. */
|
---|
184 | static const char *platform = "pc";
|
---|
185 |
|
---|
186 | sysinfo_set_item_data("platform", NULL, (void *) platform,
|
---|
187 | str_size(platform));
|
---|
188 |
|
---|
189 | #ifdef CONFIG_PC_KBD
|
---|
190 | /*
|
---|
191 | * Initialize the i8042 controller. Then initialize the keyboard
|
---|
192 | * module and connect it to i8042. Enable keyboard interrupts.
|
---|
193 | */
|
---|
194 | i8042_instance_t *i8042_instance = i8042_init((i8042_t *) I8042_BASE, IRQ_KBD);
|
---|
195 | if (i8042_instance) {
|
---|
196 | kbrd_instance_t *kbrd_instance = kbrd_init();
|
---|
197 | if (kbrd_instance) {
|
---|
198 | indev_t *sink = stdin_wire();
|
---|
199 | indev_t *kbrd = kbrd_wire(kbrd_instance, sink);
|
---|
200 | i8042_wire(i8042_instance, kbrd);
|
---|
201 | trap_virtual_enable_irqs(1 << IRQ_KBD);
|
---|
202 | trap_virtual_enable_irqs(1 << IRQ_MOUSE);
|
---|
203 | }
|
---|
204 | }
|
---|
205 | #endif
|
---|
206 |
|
---|
207 | #if (defined(CONFIG_NS16550) || defined(CONFIG_NS16550_OUT))
|
---|
208 | /*
|
---|
209 | * Initialize the ns16550 controller.
|
---|
210 | */
|
---|
211 | #ifdef CONFIG_NS16550_OUT
|
---|
212 | outdev_t *ns16550_out;
|
---|
213 | outdev_t **ns16550_out_ptr = &ns16550_out;
|
---|
214 | #else
|
---|
215 | outdev_t **ns16550_out_ptr = NULL;
|
---|
216 | #endif
|
---|
217 | ns16550_instance_t *ns16550_instance
|
---|
218 | = ns16550_init((ns16550_t *) NS16550_BASE, IRQ_NS16550, NULL, NULL,
|
---|
219 | ns16550_out_ptr);
|
---|
220 | if (ns16550_instance) {
|
---|
221 | #ifdef CONFIG_NS16550
|
---|
222 | srln_instance_t *srln_instance = srln_init();
|
---|
223 | if (srln_instance) {
|
---|
224 | indev_t *sink = stdin_wire();
|
---|
225 | indev_t *srln = srln_wire(srln_instance, sink);
|
---|
226 | ns16550_wire(ns16550_instance, srln);
|
---|
227 | trap_virtual_enable_irqs(1 << IRQ_NS16550);
|
---|
228 | }
|
---|
229 | #endif
|
---|
230 | #ifdef CONFIG_NS16550_OUT
|
---|
231 | if (ns16550_out) {
|
---|
232 | stdout_wire(ns16550_out);
|
---|
233 | }
|
---|
234 | #endif
|
---|
235 | }
|
---|
236 | #endif
|
---|
237 |
|
---|
238 | if (irqs_info != NULL)
|
---|
239 | sysinfo_set_item_val(irqs_info, NULL, true);
|
---|
240 | }
|
---|
241 |
|
---|
242 | void calibrate_delay_loop(void)
|
---|
243 | {
|
---|
244 | i8254_calibrate_delay_loop();
|
---|
245 | if (config.cpu_active == 1) {
|
---|
246 | /*
|
---|
247 | * This has to be done only on UP.
|
---|
248 | * On SMP, i8254 is not used for time keeping and its interrupt pin remains masked.
|
---|
249 | */
|
---|
250 | i8254_normal_operation();
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | /** Construct function pointer
|
---|
255 | *
|
---|
256 | * @param fptr function pointer structure
|
---|
257 | * @param addr function address
|
---|
258 | * @param caller calling function address
|
---|
259 | *
|
---|
260 | * @return address of the function pointer
|
---|
261 | *
|
---|
262 | */
|
---|
263 | void *arch_construct_function(fncptr_t *fptr, void *addr, void *caller)
|
---|
264 | {
|
---|
265 | return addr;
|
---|
266 | }
|
---|
267 |
|
---|
268 | void arch_reboot(void)
|
---|
269 | {
|
---|
270 | #ifdef CONFIG_PC_KBD
|
---|
271 | i8042_cpu_reset((i8042_t *) I8042_BASE);
|
---|
272 | #endif
|
---|
273 | }
|
---|
274 |
|
---|
275 | void irq_initialize_arch(irq_t *irq)
|
---|
276 | {
|
---|
277 | (void) irq;
|
---|
278 | }
|
---|
279 |
|
---|
280 | /** @}
|
---|
281 | */
|
---|