| 1 | /* | 
|---|
| 2 | * pic_apic.c - APIC PIC driver implementation | 
|---|
| 3 | * | 
|---|
| 4 | * Copyright (c) 2017 Maxime Villard | 
|---|
| 5 | * | 
|---|
| 6 | * This file is part of ALMOS-MKH. | 
|---|
| 7 | * | 
|---|
| 8 | * ALMOS-MKH is free software; you can redistribute it and/or modify it | 
|---|
| 9 | * under the terms of the GNU General Public License as published by | 
|---|
| 10 | * the Free Software Foundation; version 2.0 of the License. | 
|---|
| 11 | * | 
|---|
| 12 | * ALMOS-MKH is distributed in the hope that it will be useful, but | 
|---|
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 15 | * General Public License for more details. | 
|---|
| 16 | * | 
|---|
| 17 | * You should have received a copy of the GNU General Public License | 
|---|
| 18 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, | 
|---|
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | 
|---|
| 20 | */ | 
|---|
| 21 |  | 
|---|
| 22 | #include <hal_kernel_types.h> | 
|---|
| 23 | #include <chdev.h> | 
|---|
| 24 | #include <pic_apic.h> | 
|---|
| 25 | #include <errno.h> | 
|---|
| 26 | #include <string.h> | 
|---|
| 27 | #include <printk.h> | 
|---|
| 28 |  | 
|---|
| 29 | #include <hal_internal.h> | 
|---|
| 30 | #include <hal_segmentation.h> | 
|---|
| 31 | #include <hal_apic.h> | 
|---|
| 32 |  | 
|---|
| 33 | extern iopic_input_t iopic_input; | 
|---|
| 34 |  | 
|---|
| 35 | static void | 
|---|
| 36 | idt_set_seg(struct idt_seg *seg, void *func, int ist, int type, int dpl, int sel) | 
|---|
| 37 | { | 
|---|
| 38 | seg->gd_looffset = (uint64_t)func & 0xffff; | 
|---|
| 39 | seg->gd_selector = sel; | 
|---|
| 40 | seg->gd_ist = ist; | 
|---|
| 41 | seg->gd_type = type; | 
|---|
| 42 | seg->gd_dpl = dpl; | 
|---|
| 43 | seg->gd_p = 1; | 
|---|
| 44 | seg->gd_hioffset = (uint64_t)func >> 16; | 
|---|
| 45 | seg->gd_zero = 0; | 
|---|
| 46 | seg->gd_xx1 = 0; | 
|---|
| 47 | seg->gd_xx2 = 0; | 
|---|
| 48 | seg->gd_xx3 = 0; | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | /* -------------------------------------------------------------------------- */ | 
|---|
| 52 |  | 
|---|
| 53 | extern uint8_t *idtstore; | 
|---|
| 54 |  | 
|---|
| 55 | void pic_apic_init(chdev_t *pic) | 
|---|
| 56 | { | 
|---|
| 57 | /* XXX APIC already initialized earlier */ | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | void pic_apic_extend_init(uint32_t *xcu_base) | 
|---|
| 61 | { | 
|---|
| 62 | x86_panic((char *)__func__); | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | void pic_apic_bind_irq(lid_t lid, chdev_t *src_chdev) | 
|---|
| 66 | { | 
|---|
| 67 | struct idt_seg seg, *idt; | 
|---|
| 68 | void *isr; | 
|---|
| 69 | int slot; | 
|---|
| 70 |  | 
|---|
| 71 | /* get the source chdev functional type, channel, and direction */ | 
|---|
| 72 | uint32_t func    = src_chdev->func; | 
|---|
| 73 | uint32_t channel = src_chdev->channel; | 
|---|
| 74 |  | 
|---|
| 75 | /* get external IRQ index */ | 
|---|
| 76 | uint32_t irq_id; | 
|---|
| 77 | if (func == DEV_FUNC_IOC) | 
|---|
| 78 | irq_id = iopic_input.ioc[channel]; | 
|---|
| 79 | else if (func == DEV_FUNC_TXT) | 
|---|
| 80 | irq_id = iopic_input.txt[channel]; | 
|---|
| 81 | else | 
|---|
| 82 | assert(false, "unsupported device\n"); | 
|---|
| 83 |  | 
|---|
| 84 | /* get the ISR pointer */ | 
|---|
| 85 | isr = src_chdev->isr; | 
|---|
| 86 |  | 
|---|
| 87 | /* create the IDT entry, and register it */ | 
|---|
| 88 | idt_set_seg(&seg, isr, 0, SDT_SYS386IGT, SEL_KPL, | 
|---|
| 89 | GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL)); | 
|---|
| 90 | slot = idt_slot_alloc(); | 
|---|
| 91 | idt = (struct idt_seg *)&idtstore; | 
|---|
| 92 | memcpy(&idt[slot], &seg, sizeof(struct idt_seg)); | 
|---|
| 93 |  | 
|---|
| 94 | /* Bind the IRQ line in IOAPIC */ | 
|---|
| 95 | hal_ioapic_bind_irq(irq_id, slot, lid); | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | void pic_apic_enable_irq(lid_t lid, xptr_t src_chdev_xp) | 
|---|
| 99 | { | 
|---|
| 100 | chdev_t *src_chdev = (chdev_t *)src_chdev_xp; | 
|---|
| 101 | uint32_t func = src_chdev->func; | 
|---|
| 102 | uint32_t channel = src_chdev->channel; | 
|---|
| 103 | uint32_t irq_id; | 
|---|
| 104 |  | 
|---|
| 105 | /* get external IRQ index */ | 
|---|
| 106 | if (func == DEV_FUNC_IOC) | 
|---|
| 107 | irq_id = iopic_input.ioc[channel]; | 
|---|
| 108 | else if (func == DEV_FUNC_TXT) | 
|---|
| 109 | irq_id = iopic_input.txt[channel]; | 
|---|
| 110 | else | 
|---|
| 111 | assert(false, "unsupported device\n"); | 
|---|
| 112 |  | 
|---|
| 113 | /* enable the line */ | 
|---|
| 114 | hal_ioapic_enable_irq(irq_id); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | void pic_apic_disable_irq(lid_t lid, xptr_t src_chdev_xp) | 
|---|
| 118 | { | 
|---|
| 119 | chdev_t *src_chdev = (chdev_t *)src_chdev_xp; | 
|---|
| 120 | uint32_t func = src_chdev->func; | 
|---|
| 121 | uint32_t channel = src_chdev->channel; | 
|---|
| 122 | uint32_t irq_id; | 
|---|
| 123 |  | 
|---|
| 124 | /* get external IRQ index */ | 
|---|
| 125 | if (func == DEV_FUNC_IOC) | 
|---|
| 126 | irq_id = iopic_input.ioc[channel]; | 
|---|
| 127 | else if (func == DEV_FUNC_TXT) | 
|---|
| 128 | irq_id = iopic_input.txt[channel]; | 
|---|
| 129 | else | 
|---|
| 130 | assert(false, "unsupported device\n"); | 
|---|
| 131 |  | 
|---|
| 132 | /* disable the line */ | 
|---|
| 133 | hal_ioapic_disable_irq(irq_id); | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | void pic_apic_enable_timer(uint32_t period) | 
|---|
| 137 | { | 
|---|
| 138 | /* | 
|---|
| 139 | * TODO: right now, we don't do anything, because the LAPIC is already | 
|---|
| 140 | * calibrated and launched earlier, mostly for debugging purposes. But it | 
|---|
| 141 | * would be good to launch it here for real. | 
|---|
| 142 | */ | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | void pic_apic_enable_ipi( void ) | 
|---|
| 146 | { | 
|---|
| 147 | /* TODO */ | 
|---|
| 148 | //      x86_panic((char *)__func__); | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | void pic_apic_send_ipi(cxy_t cxy, lid_t lid) | 
|---|
| 152 | { | 
|---|
| 153 | x86_panic((char *)__func__); | 
|---|
| 154 | } | 
|---|