[f761f1eb] | 1 | /*
|
---|
[3526f4f3] | 2 | * Copyright (c) 2024 Jiri Svoboda
|
---|
[df4ed85] | 3 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
[deca67b] | 4 | * Copyright (c) 2009 Martin Decky
|
---|
[f761f1eb] | 5 | * All rights reserved.
|
---|
| 6 | *
|
---|
| 7 | * Redistribution and use in source and binary forms, with or without
|
---|
| 8 | * modification, are permitted provided that the following conditions
|
---|
| 9 | * are met:
|
---|
| 10 | *
|
---|
| 11 | * - Redistributions of source code must retain the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer.
|
---|
| 13 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 14 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 15 | * documentation and/or other materials provided with the distribution.
|
---|
| 16 | * - The name of the author may not be used to endorse or promote products
|
---|
| 17 | * derived from this software without specific prior written permission.
|
---|
| 18 | *
|
---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 29 | */
|
---|
| 30 |
|
---|
[c5429fe] | 31 | /** @addtogroup kernel_ia32
|
---|
[b45c443] | 32 | * @{
|
---|
| 33 | */
|
---|
| 34 | /** @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[f761f1eb] | 37 | #include <arch.h>
|
---|
[36df4109] | 38 | #include <arch/arch.h>
|
---|
[83dab11] | 39 | #include <stdint.h>
|
---|
[d8db519] | 40 | #include <errno.h>
|
---|
[b169619] | 41 | #include <memw.h>
|
---|
[fcfac420] | 42 | #include <interrupt.h>
|
---|
[41d33ac] | 43 | #include <console/console.h>
|
---|
[d8db519] | 44 | #include <syscall/syscall.h>
|
---|
[4c7257b] | 45 | #include <sysinfo/sysinfo.h>
|
---|
[d8db519] | 46 | #include <arch/bios/bios.h>
|
---|
[deca67b] | 47 | #include <arch/boot/boot.h>
|
---|
[d8db519] | 48 | #include <arch/drivers/i8254.h>
|
---|
| 49 | #include <genarch/acpi/acpi.h>
|
---|
| 50 | #include <genarch/drivers/ega/ega.h>
|
---|
| 51 | #include <genarch/drivers/i8042/i8042.h>
|
---|
[87a5796] | 52 | #include <genarch/drivers/i8259/i8259.h>
|
---|
[3296df5] | 53 | #include <genarch/drivers/ns16550/ns16550.h>
|
---|
[d8db519] | 54 | #include <genarch/drivers/legacy/ia32/io.h>
|
---|
| 55 | #include <genarch/fb/bfb.h>
|
---|
| 56 | #include <genarch/kbrd/kbrd.h>
|
---|
[3296df5] | 57 | #include <genarch/srln/srln.h>
|
---|
[d8db519] | 58 | #include <genarch/multiboot/multiboot.h>
|
---|
| 59 | #include <genarch/multiboot/multiboot2.h>
|
---|
[2a103b5] | 60 | #include <genarch/pic/pic_ops.h>
|
---|
[d6f9fff] | 61 | #include <arch/pm.h>
|
---|
| 62 | #include <arch/vreg.h>
|
---|
[de96d3b] | 63 | #include <arch/mm/pat.h>
|
---|
[ad36bd6] | 64 |
|
---|
[26678e5] | 65 | #ifdef CONFIG_SMP
|
---|
| 66 | #include <arch/smp/apic.h>
|
---|
| 67 | #endif
|
---|
| 68 |
|
---|
[36df4109] | 69 | static void ia32_pre_mm_init(void);
|
---|
| 70 | static void ia32_post_mm_init(void);
|
---|
| 71 | static void ia32_post_cpu_init(void);
|
---|
| 72 | static void ia32_pre_smp_init(void);
|
---|
| 73 | static void ia32_post_smp_init(void);
|
---|
| 74 |
|
---|
| 75 | arch_ops_t ia32_ops = {
|
---|
| 76 | .pre_mm_init = ia32_pre_mm_init,
|
---|
| 77 | .post_mm_init = ia32_post_mm_init,
|
---|
| 78 | .post_cpu_init = ia32_post_cpu_init,
|
---|
| 79 | .pre_smp_init = ia32_pre_smp_init,
|
---|
| 80 | .post_smp_init = ia32_post_smp_init,
|
---|
| 81 | };
|
---|
| 82 |
|
---|
| 83 | arch_ops_t *arch_ops = &ia32_ops;
|
---|
| 84 |
|
---|
[5d8d71e] | 85 | /** Perform ia32-specific initialization before main_bsp() is called.
|
---|
[deca67b] | 86 | *
|
---|
[1f5c9c96] | 87 | * @param signature Multiboot signature.
|
---|
| 88 | * @param info Multiboot information structure.
|
---|
| 89 | *
|
---|
[deca67b] | 90 | */
|
---|
[36df4109] | 91 | void ia32_pre_main(uint32_t signature, void *info)
|
---|
[deca67b] | 92 | {
|
---|
[5d8d71e] | 93 | /* Parse multiboot information obtained from the bootloader. */
|
---|
[1f5c9c96] | 94 | multiboot_info_parse(signature, (multiboot_info_t *) info);
|
---|
| 95 | multiboot2_info_parse(signature, (multiboot2_info_t *) info);
|
---|
[a35b458] | 96 |
|
---|
[deca67b] | 97 | #ifdef CONFIG_SMP
|
---|
[bae43dc] | 98 | size_t unmapped_size = (uintptr_t) unmapped_end - BOOT_OFFSET;
|
---|
[deca67b] | 99 | /* Copy AP bootstrap routines below 1 MB. */
|
---|
[8a1afd2] | 100 | memcpy((void *) AP_BOOT_OFFSET, (void *) BOOT_OFFSET, unmapped_size);
|
---|
[deca67b] | 101 | #endif
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[36df4109] | 104 | void ia32_pre_mm_init(void)
|
---|
[f761f1eb] | 105 | {
|
---|
| 106 | pm_init();
|
---|
| 107 |
|
---|
[3526f4f3] | 108 | #ifndef PROCESSOR_i486
|
---|
[de96d3b] | 109 | /* Use PCD+PWT bit combination in PTE to mean write-combining mode. */
|
---|
| 110 | if (pat_supported())
|
---|
| 111 | pat_set_mapping(false, true, true, PAT_TYPE_WRITE_COMBINING);
|
---|
[3526f4f3] | 112 | #endif
|
---|
[de96d3b] | 113 |
|
---|
[f761f1eb] | 114 | if (config.cpu_active == 1) {
|
---|
[cea12e9] | 115 | interrupt_init();
|
---|
[dba84ff] | 116 | bios_init();
|
---|
[a35b458] | 117 |
|
---|
[cea12e9] | 118 | /* PIC */
|
---|
[d1cbad5] | 119 | i8259_init((i8259_t *) I8259_PIC0_BASE,
|
---|
[3daba42e] | 120 | (i8259_t *) I8259_PIC1_BASE, IVT_IRQBASE);
|
---|
[bbb99f82] | 121 |
|
---|
[2a103b5] | 122 | /* Set PIC operations. */
|
---|
| 123 | pic_ops = &i8259_pic_ops;
|
---|
[f761f1eb] | 124 | }
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[36df4109] | 127 | void ia32_post_mm_init(void)
|
---|
[7eade45] | 128 | {
|
---|
[d6f9fff] | 129 | vreg_init();
|
---|
| 130 |
|
---|
[425913b] | 131 | if (config.cpu_active == 1) {
|
---|
[cea12e9] | 132 | /* Initialize IRQ routing */
|
---|
| 133 | irq_init(IRQ_COUNT, IRQ_COUNT);
|
---|
[a35b458] | 134 |
|
---|
[cea12e9] | 135 | /* hard clock */
|
---|
| 136 | i8254_init();
|
---|
[a35b458] | 137 |
|
---|
[a71c158] | 138 | #if (defined(CONFIG_FB) || defined(CONFIG_EGA))
|
---|
[1f5c9c96] | 139 | bool bfb = false;
|
---|
[a71c158] | 140 | #endif
|
---|
[a35b458] | 141 |
|
---|
[22cf454d] | 142 | #ifdef CONFIG_FB
|
---|
[1f5c9c96] | 143 | bfb = bfb_init();
|
---|
[22cf454d] | 144 | #endif
|
---|
[a35b458] | 145 |
|
---|
[ec944b1] | 146 | #ifdef CONFIG_EGA
|
---|
[1f5c9c96] | 147 | if (!bfb) {
|
---|
[a71c158] | 148 | outdev_t *egadev = ega_init(EGA_BASE, EGA_VIDEORAM);
|
---|
| 149 | if (egadev)
|
---|
| 150 | stdout_wire(egadev);
|
---|
| 151 | }
|
---|
[ec944b1] | 152 | #endif
|
---|
[a35b458] | 153 |
|
---|
[381465e] | 154 | /* Merge all memory zones to 1 big zone */
|
---|
| 155 | zone_merge_all();
|
---|
[babcb148] | 156 | }
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[36df4109] | 159 | void ia32_post_cpu_init(void)
|
---|
[26678e5] | 160 | {
|
---|
| 161 | #ifdef CONFIG_SMP
|
---|
[49eb681] | 162 | if (config.cpu_active > 1) {
|
---|
[26678e5] | 163 | l_apic_init();
|
---|
| 164 | l_apic_debug();
|
---|
| 165 | }
|
---|
| 166 | #endif
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[36df4109] | 169 | void ia32_pre_smp_init(void)
|
---|
[babcb148] | 170 | {
|
---|
| 171 | if (config.cpu_active == 1) {
|
---|
[f619ec11] | 172 | #ifdef CONFIG_SMP
|
---|
[85bfdcc8] | 173 | acpi_init();
|
---|
[f619ec11] | 174 | #endif /* CONFIG_SMP */
|
---|
[425913b] | 175 | }
|
---|
[7eade45] | 176 | }
|
---|
| 177 |
|
---|
[36df4109] | 178 | void ia32_post_smp_init(void)
|
---|
[7453929] | 179 | {
|
---|
[eff1f033] | 180 | /* Currently the only supported platform for ia32 is 'pc'. */
|
---|
| 181 | static const char *platform = "pc";
|
---|
| 182 |
|
---|
| 183 | sysinfo_set_item_data("platform", NULL, (void *) platform,
|
---|
| 184 | str_size(platform));
|
---|
| 185 |
|
---|
[2a34e4c] | 186 | #ifdef CONFIG_PC_KBD
|
---|
[411b6a6] | 187 | /*
|
---|
[2a34e4c] | 188 | * Initialize the i8042 controller. Then initialize the keyboard
|
---|
| 189 | * module and connect it to i8042. Enable keyboard interrupts.
|
---|
[411b6a6] | 190 | */
|
---|
[c2417bc] | 191 | i8042_instance_t *i8042_instance = i8042_init((i8042_t *) I8042_BASE, IRQ_KBD);
|
---|
| 192 | if (i8042_instance) {
|
---|
| 193 | kbrd_instance_t *kbrd_instance = kbrd_init();
|
---|
| 194 | if (kbrd_instance) {
|
---|
| 195 | indev_t *sink = stdin_wire();
|
---|
| 196 | indev_t *kbrd = kbrd_wire(kbrd_instance, sink);
|
---|
| 197 | i8042_wire(i8042_instance, kbrd);
|
---|
[2a103b5] | 198 | pic_ops->enable_irqs(1 << IRQ_KBD);
|
---|
| 199 | pic_ops->enable_irqs(1 << IRQ_MOUSE);
|
---|
[c2417bc] | 200 | }
|
---|
[2a34e4c] | 201 | }
|
---|
| 202 | #endif
|
---|
[3296df5] | 203 |
|
---|
[6bbe470] | 204 | #if (defined(CONFIG_NS16550) || defined(CONFIG_NS16550_OUT))
|
---|
[3296df5] | 205 | /*
|
---|
[6bbe470] | 206 | * Initialize the ns16550 controller.
|
---|
[3296df5] | 207 | */
|
---|
[21b6307] | 208 | #ifdef CONFIG_NS16550_OUT
|
---|
| 209 | outdev_t *ns16550_out;
|
---|
| 210 | outdev_t **ns16550_out_ptr = &ns16550_out;
|
---|
| 211 | #else
|
---|
| 212 | outdev_t **ns16550_out_ptr = NULL;
|
---|
| 213 | #endif
|
---|
[3bacee1] | 214 | ns16550_instance_t *ns16550_instance =
|
---|
| 215 | ns16550_init(NS16550_BASE, 0, IRQ_NS16550, NULL, NULL,
|
---|
[21b6307] | 216 | ns16550_out_ptr);
|
---|
[3296df5] | 217 | if (ns16550_instance) {
|
---|
[98a935e] | 218 | ns16550_format_set(ns16550_instance, 38400,
|
---|
| 219 | LCR_PARITY_NONE | LCR_STOP_BIT_TWO | LCR_WORD_LEN_8);
|
---|
[6bbe470] | 220 | #ifdef CONFIG_NS16550
|
---|
[3296df5] | 221 | srln_instance_t *srln_instance = srln_init();
|
---|
| 222 | if (srln_instance) {
|
---|
| 223 | indev_t *sink = stdin_wire();
|
---|
| 224 | indev_t *srln = srln_wire(srln_instance, sink);
|
---|
| 225 | ns16550_wire(ns16550_instance, srln);
|
---|
[2a103b5] | 226 | pic_ops->enable_irqs(1 << IRQ_NS16550);
|
---|
[3296df5] | 227 | }
|
---|
| 228 | #endif
|
---|
[6bbe470] | 229 | #ifdef CONFIG_NS16550_OUT
|
---|
| 230 | if (ns16550_out) {
|
---|
| 231 | stdout_wire(ns16550_out);
|
---|
| 232 | }
|
---|
| 233 | #endif
|
---|
[24b06199] | 234 | }
|
---|
| 235 | #endif
|
---|
[a35b458] | 236 |
|
---|
[2a103b5] | 237 | sysinfo_set_item_val(pic_ops->get_name(), NULL, true);
|
---|
[7453929] | 238 | }
|
---|
| 239 |
|
---|
[f761f1eb] | 240 | void calibrate_delay_loop(void)
|
---|
| 241 | {
|
---|
| 242 | i8254_calibrate_delay_loop();
|
---|
[f701b236] | 243 | if (config.cpu_active == 1) {
|
---|
| 244 | /*
|
---|
| 245 | * This has to be done only on UP.
|
---|
| 246 | * On SMP, i8254 is not used for time keeping and its interrupt pin remains masked.
|
---|
| 247 | */
|
---|
| 248 | i8254_normal_operation();
|
---|
| 249 | }
|
---|
[f761f1eb] | 250 | }
|
---|
[281b607] | 251 |
|
---|
[6da1013f] | 252 | /** Construct function pointer
|
---|
| 253 | *
|
---|
| 254 | * @param fptr function pointer structure
|
---|
| 255 | * @param addr function address
|
---|
| 256 | * @param caller calling function address
|
---|
| 257 | *
|
---|
| 258 | * @return address of the function pointer
|
---|
| 259 | *
|
---|
| 260 | */
|
---|
| 261 | void *arch_construct_function(fncptr_t *fptr, void *addr, void *caller)
|
---|
| 262 | {
|
---|
| 263 | return addr;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
[149d14e5] | 266 | void arch_reboot(void)
|
---|
| 267 | {
|
---|
| 268 | #ifdef CONFIG_PC_KBD
|
---|
| 269 | i8042_cpu_reset((i8042_t *) I8042_BASE);
|
---|
| 270 | #endif
|
---|
| 271 | }
|
---|
| 272 |
|
---|
[3a2f8aa] | 273 | void irq_initialize_arch(irq_t *irq)
|
---|
| 274 | {
|
---|
| 275 | (void) irq;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[06e1e95] | 278 | /** @}
|
---|
[b45c443] | 279 | */
|
---|