2020-02-22 20:56:55 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-22 20:56:55 +03:00
|
|
|
*/
|
|
|
|
|
2022-05-30 10:04:37 +03:00
|
|
|
#include <Kernel/Arch/InterruptManagement.h>
|
2020-02-22 20:56:55 +03:00
|
|
|
#include <Kernel/Assertions.h>
|
2021-01-25 18:07:10 +03:00
|
|
|
#include <Kernel/Debug.h>
|
2020-02-22 20:56:55 +03:00
|
|
|
#include <Kernel/Interrupts/IRQHandler.h>
|
|
|
|
#include <Kernel/Interrupts/SharedIRQHandler.h>
|
2021-06-22 18:40:16 +03:00
|
|
|
#include <Kernel/Sections.h>
|
2020-02-22 20:56:55 +03:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-02-19 23:29:46 +03:00
|
|
|
UNMAP_AFTER_INIT void SharedIRQHandler::initialize(u8 interrupt_number)
|
2020-02-22 20:56:55 +03:00
|
|
|
{
|
2021-02-27 03:17:57 +03:00
|
|
|
auto* handler = new SharedIRQHandler(interrupt_number);
|
|
|
|
handler->register_interrupt_handler();
|
|
|
|
handler->disable_interrupt_vector();
|
2020-02-22 20:56:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SharedIRQHandler::register_handler(GenericInterruptHandler& handler)
|
|
|
|
{
|
2021-03-12 15:50:55 +03:00
|
|
|
dbgln_if(INTERRUPT_DEBUG, "Interrupt Handler registered @ Shared Interrupt Handler {}", interrupt_number());
|
2021-07-21 21:21:29 +03:00
|
|
|
m_handlers.append(handler);
|
2020-02-22 20:56:55 +03:00
|
|
|
enable_interrupt_vector();
|
|
|
|
}
|
|
|
|
void SharedIRQHandler::unregister_handler(GenericInterruptHandler& handler)
|
|
|
|
{
|
2021-03-12 15:50:55 +03:00
|
|
|
dbgln_if(INTERRUPT_DEBUG, "Interrupt Handler unregistered @ Shared Interrupt Handler {}", interrupt_number());
|
2021-07-21 21:21:29 +03:00
|
|
|
m_handlers.remove(handler);
|
2020-02-22 20:56:55 +03:00
|
|
|
if (m_handlers.is_empty())
|
|
|
|
disable_interrupt_vector();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SharedIRQHandler::eoi()
|
|
|
|
{
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(INTERRUPT_DEBUG, "EOI IRQ {}", interrupt_number());
|
2020-03-08 13:47:33 +03:00
|
|
|
m_responsible_irq_controller->eoi(*this);
|
2020-02-22 20:56:55 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-05 07:50:27 +03:00
|
|
|
void SharedIRQHandler::enumerate_handlers(Function<void(GenericInterruptHandler&)>& callback)
|
|
|
|
{
|
2021-07-21 21:21:29 +03:00
|
|
|
for (auto& handler : m_handlers) {
|
|
|
|
callback(handler);
|
2021-06-05 07:50:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-22 20:56:55 +03:00
|
|
|
SharedIRQHandler::SharedIRQHandler(u8 irq)
|
|
|
|
: GenericInterruptHandler(irq)
|
2020-02-28 23:33:41 +03:00
|
|
|
, m_responsible_irq_controller(InterruptManagement::the().get_responsible_irq_controller(irq))
|
2020-02-22 20:56:55 +03:00
|
|
|
{
|
2021-03-12 15:50:55 +03:00
|
|
|
dbgln_if(INTERRUPT_DEBUG, "Shared Interrupt Handler registered @ {}", interrupt_number());
|
2020-02-22 20:56:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
SharedIRQHandler::~SharedIRQHandler()
|
|
|
|
{
|
2021-03-12 15:50:55 +03:00
|
|
|
dbgln_if(INTERRUPT_DEBUG, "Shared Interrupt Handler unregistered @ {}", interrupt_number());
|
2020-02-22 20:56:55 +03:00
|
|
|
disable_interrupt_vector();
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool SharedIRQHandler::handle_interrupt(RegisterState const& regs)
|
2020-02-22 20:56:55 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_INTERRUPTS_DISABLED();
|
2021-01-14 02:21:21 +03:00
|
|
|
|
2021-01-24 01:59:27 +03:00
|
|
|
if constexpr (INTERRUPT_DEBUG) {
|
2021-01-14 02:21:21 +03:00
|
|
|
dbgln("Interrupt @ {}", interrupt_number());
|
2021-07-21 21:21:29 +03:00
|
|
|
dbgln("Interrupt Handlers registered - {}", m_handlers.size_slow());
|
2021-01-14 02:21:21 +03:00
|
|
|
}
|
2020-02-22 20:56:55 +03:00
|
|
|
int i = 0;
|
2021-06-05 09:00:18 +03:00
|
|
|
bool was_handled = false;
|
2021-07-21 21:21:29 +03:00
|
|
|
for (auto& handler : m_handlers) {
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(INTERRUPT_DEBUG, "Going for Interrupt Handling @ {}, Shared Interrupt {}", i, interrupt_number());
|
2021-07-21 21:21:29 +03:00
|
|
|
if (handler.handle_interrupt(regs)) {
|
2022-11-18 23:39:49 +03:00
|
|
|
handler.increment_call_count();
|
2021-06-05 09:00:18 +03:00
|
|
|
was_handled = true;
|
|
|
|
}
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(INTERRUPT_DEBUG, "Going for Interrupt Handling @ {}, Shared Interrupt {} - End", i, interrupt_number());
|
2020-02-22 20:56:55 +03:00
|
|
|
i++;
|
|
|
|
}
|
2021-06-05 09:00:18 +03:00
|
|
|
return was_handled;
|
2020-02-22 20:56:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SharedIRQHandler::enable_interrupt_vector()
|
|
|
|
{
|
|
|
|
if (m_enabled)
|
|
|
|
return;
|
|
|
|
m_enabled = true;
|
2020-03-08 13:47:33 +03:00
|
|
|
m_responsible_irq_controller->enable(*this);
|
2020-02-22 20:56:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void SharedIRQHandler::disable_interrupt_vector()
|
|
|
|
{
|
|
|
|
if (!m_enabled)
|
|
|
|
return;
|
|
|
|
m_enabled = false;
|
2020-03-08 13:47:33 +03:00
|
|
|
m_responsible_irq_controller->disable(*this);
|
2020-02-22 20:56:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|