source: mainline/kernel/arch/amd64/src/amd64.c@ ebf5ced6

Last change on this file since ebf5ced6 was de96d3b, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 19 months ago

On x86 CPUs supporting it, use write-combining memory mode for framebuffer

With this, kernel printouts are about three times faster in QEMU.

  • Property mode set to 100644
File size: 7.4 KB
Line 
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 kernel_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 <memw.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/syscall.h>
48#include <genarch/acpi/acpi.h>
49#include <genarch/drivers/ega/ega.h>
50#include <genarch/drivers/i8042/i8042.h>
51#include <genarch/drivers/i8259/i8259.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#include <arch/mm/pat.h>
63#include <genarch/pic/pic_ops.h>
64
65#ifdef CONFIG_SMP
66#include <arch/smp/apic.h>
67#endif
68
69static void amd64_pre_mm_init(void);
70static void amd64_post_mm_init(void);
71static void amd64_post_cpu_init(void);
72static void amd64_pre_smp_init(void);
73static void amd64_post_smp_init(void);
74
75arch_ops_t amd64_ops = {
76 .pre_mm_init = amd64_pre_mm_init,
77 .post_mm_init = amd64_post_mm_init,
78 .post_cpu_init = amd64_post_cpu_init,
79 .pre_smp_init = amd64_pre_smp_init,
80 .post_smp_init = amd64_post_smp_init
81};
82
83arch_ops_t *arch_ops = &amd64_ops;
84
85/** Perform amd64-specific initialization before main_bsp() is called.
86 *
87 * @param signature Multiboot signature.
88 * @param info Multiboot information structure.
89 *
90 */
91void amd64_pre_main(uint32_t signature, void *info)
92{
93 /* Parse multiboot information obtained from the bootloader. */
94 multiboot_info_parse(signature, (multiboot_info_t *) info);
95 multiboot2_info_parse(signature, (multiboot2_info_t *) info);
96
97#ifdef CONFIG_SMP
98 size_t unmapped_size = (uintptr_t) unmapped_end - BOOT_OFFSET;
99 /* Copy AP bootstrap routines below 1 MB. */
100 memcpy((void *) AP_BOOT_OFFSET, (void *) BOOT_OFFSET, unmapped_size);
101#endif
102}
103
104void amd64_pre_mm_init(void)
105{
106 /* Enable no-execute pages */
107 write_msr(AMD_MSR_EFER, read_msr(AMD_MSR_EFER) | AMD_NXE);
108 /* Enable FPU */
109 cpu_setup_fpu();
110
111 /* Initialize segmentation */
112 pm_init();
113
114 /* Disable I/O on nonprivileged levels, clear the nested-thread flag */
115 write_rflags(read_rflags() & ~(RFLAGS_IOPL | RFLAGS_NT));
116 /* Disable alignment check */
117 write_cr0(read_cr0() & ~CR0_AM);
118
119 /* Use PCD+PWT bit combination in PTE to mean write-combining mode. */
120 if (pat_supported())
121 pat_set_mapping(false, true, true, PAT_TYPE_WRITE_COMBINING);
122
123 if (config.cpu_active == 1) {
124 interrupt_init();
125 bios_init();
126
127 /* PIC */
128 i8259_init((i8259_t *) I8259_PIC0_BASE,
129 (i8259_t *) I8259_PIC1_BASE, IVT_IRQBASE);
130
131 /* Set PIC operations. */
132 pic_ops = &i8259_pic_ops;
133 }
134}
135
136void amd64_post_mm_init(void)
137{
138 vreg_init();
139 kseg_init();
140
141 if (config.cpu_active == 1) {
142 /* Initialize IRQ routing */
143 irq_init(IRQ_COUNT, IRQ_COUNT);
144
145 /* hard clock */
146 i8254_init();
147
148#if (defined(CONFIG_FB) || defined(CONFIG_EGA))
149 bool bfb = false;
150#endif
151
152#ifdef CONFIG_FB
153 bfb = bfb_init();
154#endif
155
156#ifdef CONFIG_EGA
157 if (!bfb) {
158 outdev_t *egadev = ega_init(EGA_BASE, EGA_VIDEORAM);
159 if (egadev)
160 stdout_wire(egadev);
161 }
162#endif
163
164 /* Merge all memory zones to 1 big zone */
165 zone_merge_all();
166 }
167
168 /* Setup fast SYSCALL/SYSRET */
169 syscall_setup_cpu();
170}
171
172void amd64_post_cpu_init(void)
173{
174#ifdef CONFIG_SMP
175 if (config.cpu_active > 1) {
176 l_apic_init();
177 l_apic_debug();
178 }
179#endif
180}
181
182void amd64_pre_smp_init(void)
183{
184 if (config.cpu_active == 1) {
185#ifdef CONFIG_SMP
186 acpi_init();
187#endif /* CONFIG_SMP */
188 }
189}
190
191void amd64_post_smp_init(void)
192{
193 /* Currently the only supported platform for amd64 is 'pc'. */
194 static const char *platform = "pc";
195
196 sysinfo_set_item_data("platform", NULL, (void *) platform,
197 str_size(platform));
198
199#ifdef CONFIG_PC_KBD
200 /*
201 * Initialize the i8042 controller. Then initialize the keyboard
202 * module and connect it to i8042. Enable keyboard interrupts.
203 */
204 i8042_instance_t *i8042_instance = i8042_init((i8042_t *) I8042_BASE, IRQ_KBD);
205 if (i8042_instance) {
206 kbrd_instance_t *kbrd_instance = kbrd_init();
207 if (kbrd_instance) {
208 indev_t *sink = stdin_wire();
209 indev_t *kbrd = kbrd_wire(kbrd_instance, sink);
210 i8042_wire(i8042_instance, kbrd);
211 pic_ops->enable_irqs(1 << IRQ_KBD);
212 pic_ops->enable_irqs(1 << IRQ_MOUSE);
213 }
214 }
215#endif
216
217#if (defined(CONFIG_NS16550) || defined(CONFIG_NS16550_OUT))
218 /*
219 * Initialize the ns16550 controller.
220 */
221#ifdef CONFIG_NS16550_OUT
222 outdev_t *ns16550_out;
223 outdev_t **ns16550_out_ptr = &ns16550_out;
224#else
225 outdev_t **ns16550_out_ptr = NULL;
226#endif
227 ns16550_instance_t *ns16550_instance =
228 ns16550_init(NS16550_BASE, 0, IRQ_NS16550, NULL, NULL,
229 ns16550_out_ptr);
230 if (ns16550_instance) {
231 ns16550_format_set(ns16550_instance, 38400,
232 LCR_PARITY_NONE | LCR_STOP_BIT_TWO | LCR_WORD_LEN_8);
233#ifdef CONFIG_NS16550
234 srln_instance_t *srln_instance = srln_init();
235 if (srln_instance) {
236 indev_t *sink = stdin_wire();
237 indev_t *srln = srln_wire(srln_instance, sink);
238 ns16550_wire(ns16550_instance, srln);
239 pic_ops->enable_irqs(1 << IRQ_NS16550);
240 }
241#endif
242#ifdef CONFIG_NS16550_OUT
243 if (ns16550_out) {
244 stdout_wire(ns16550_out);
245 }
246#endif
247 }
248#endif
249
250 sysinfo_set_item_val(pic_ops->get_name(), NULL, true);
251}
252
253void calibrate_delay_loop(void)
254{
255 i8254_calibrate_delay_loop();
256 if (config.cpu_active == 1) {
257 /*
258 * This has to be done only on UP.
259 * On SMP, i8254 is not used for time keeping and its interrupt pin remains masked.
260 */
261 i8254_normal_operation();
262 }
263}
264
265/** Construct function pointer
266 *
267 * @param fptr function pointer structure
268 * @param addr function address
269 * @param caller calling function address
270 *
271 * @return address of the function pointer
272 *
273 */
274void *arch_construct_function(fncptr_t *fptr, void *addr, void *caller)
275{
276 return addr;
277}
278
279void arch_reboot(void)
280{
281#ifdef CONFIG_PC_KBD
282 i8042_cpu_reset((i8042_t *) I8042_BASE);
283#endif
284}
285
286void irq_initialize_arch(irq_t *irq)
287{
288 (void) irq;
289}
290
291/** @}
292 */
Note: See TracBrowser for help on using the repository browser.