|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il> |
| 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 are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 19 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 21 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 22 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 23 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <Kernel/Arch/i386/CPU.h> |
| 28 | +#include <Kernel/Arch/i386/PIC.h> |
| 29 | +#include <Kernel/InterruptHandler.h> |
| 30 | +#include <Kernel/SharedInterruptHandler.h> |
| 31 | + |
| 32 | +//#define INTERRUPT_DEBUG |
| 33 | + |
| 34 | +SharedInterruptHandler& SharedInterruptHandler::from(u8 interrupt_number) |
| 35 | +{ |
| 36 | + return get_interrupt_handler(interrupt_number); |
| 37 | +} |
| 38 | + |
| 39 | +void SharedInterruptHandler::initialize(u8 interrupt_number) |
| 40 | +{ |
| 41 | + new SharedInterruptHandler(interrupt_number); |
| 42 | +} |
| 43 | + |
| 44 | +void SharedInterruptHandler::register_handler(InterruptHandler& handler) |
| 45 | +{ |
| 46 | +#ifdef INTERRUPT_DEBUG |
| 47 | + kprintf("Interrupt Handler registered @ Shared Interrupt Handler %d\n", m_interrupt_number); |
| 48 | +#endif |
| 49 | + m_handlers.set(&handler); |
| 50 | + enable_interrupt_vector(); |
| 51 | +} |
| 52 | +void SharedInterruptHandler::unregister_handler(InterruptHandler& handler) |
| 53 | +{ |
| 54 | +#ifdef INTERRUPT_DEBUG |
| 55 | + kprintf("Interrupt Handler unregistered @ Shared Interrupt Handler %d\n", m_interrupt_number); |
| 56 | +#endif |
| 57 | + m_handlers.remove(&handler); |
| 58 | + if (m_handlers.is_empty()) |
| 59 | + disable_interrupt_vector(); |
| 60 | +} |
| 61 | + |
| 62 | +SharedInterruptHandler::SharedInterruptHandler(u8 interrupt_number) |
| 63 | + : m_interrupt_number(interrupt_number) |
| 64 | + , m_enabled(true) |
| 65 | +{ |
| 66 | +#ifdef INTERRUPT_DEBUG |
| 67 | + kprintf("Shared Interrupt Handler registered @ %d\n", m_interrupt_number); |
| 68 | +#endif |
| 69 | + register_shared_interrupt_handler(m_interrupt_number, *this); |
| 70 | + disable_interrupt_vector(); |
| 71 | +} |
| 72 | + |
| 73 | +SharedInterruptHandler::~SharedInterruptHandler() |
| 74 | +{ |
| 75 | +#ifdef INTERRUPT_DEBUG |
| 76 | + kprintf("Shared Interrupt Handler unregistered @ %d\n", m_interrupt_number); |
| 77 | +#endif |
| 78 | + disable_interrupt_vector(); |
| 79 | + unregister_shared_interrupt_handler(m_interrupt_number, *this); |
| 80 | +} |
| 81 | + |
| 82 | +void SharedInterruptHandler::handle_interrupt() |
| 83 | +{ |
| 84 | +#ifdef INTERRUPT_DEBUG |
| 85 | + kprintf("Interrupt @ %d\n", m_interrupt_number); |
| 86 | + kprintf("Interrupt Handlers registered - %d\n", m_handlers.size()); |
| 87 | +#endif |
| 88 | + int i = 0; |
| 89 | + for (auto* handler : m_handlers) { |
| 90 | +#ifdef INTERRUPT_DEBUG |
| 91 | + kprintf("Going for Interrupt Handling @ %d, Shared Interrupt %d\n", i, m_interrupt_number); |
| 92 | +#endif |
| 93 | + ASSERT(handler != nullptr); |
| 94 | + if (handler->is_enabled()) |
| 95 | + handler->handle_interrupt(); |
| 96 | + |
| 97 | +#ifdef INTERRUPT_DEBUG |
| 98 | + kprintf("Going for Interrupt Handling @ %d, Shared Interrupt %d - End\n", i, m_interrupt_number); |
| 99 | +#endif |
| 100 | + i++; |
| 101 | + } |
| 102 | + // FIXME: Determine if we use IRQs or MSIs (in the future) to send EOI... |
| 103 | +} |
| 104 | + |
| 105 | +void SharedInterruptHandler::enable_interrupt_vector() |
| 106 | +{ |
| 107 | + if (m_enabled) |
| 108 | + return; |
| 109 | + m_enabled = true; |
| 110 | + // FIXME: Determine if we use IRQs or MSIs (in the future) to enable the interrupt vector... |
| 111 | + PIC::enable(m_interrupt_number); |
| 112 | +} |
| 113 | + |
| 114 | +void SharedInterruptHandler::disable_interrupt_vector() |
| 115 | +{ |
| 116 | + if (!m_enabled) |
| 117 | + return; |
| 118 | + m_enabled = false; |
| 119 | + // FIXME: Determine if we use IRQs or MSIs (in the future) to disable the interrupt vector... |
| 120 | + PIC::disable(m_interrupt_number); |
| 121 | +} |
0 commit comments