2020-07-14 11:26:02 +01:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-07-14 11:26:02 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <mesosphere.hpp>
|
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constinit KLightLock g_interrupt_event_lock;
|
|
|
|
constinit KInterruptEventTask *g_interrupt_event_task_table[KInterruptController::NumInterrupts] = {};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Result KInterruptEvent::Initialize(int32_t interrupt_name, ams::svc::InterruptType type) {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
2021-04-07 09:25:42 +01:00
|
|
|
/* Verify the interrupt is defined and global. */
|
|
|
|
R_UNLESS(Kernel::GetInterruptManager().IsInterruptDefined(interrupt_name), svc::ResultOutOfRange());
|
|
|
|
R_UNLESS(Kernel::GetInterruptManager().IsGlobal(interrupt_name), svc::ResultOutOfRange());
|
|
|
|
|
2020-07-14 11:26:02 +01:00
|
|
|
/* Set interrupt id. */
|
2020-12-18 01:18:47 +00:00
|
|
|
m_interrupt_id = interrupt_name;
|
2020-07-14 11:26:02 +01:00
|
|
|
|
2021-04-07 09:25:42 +01:00
|
|
|
/* Set core id. */
|
|
|
|
m_core_id = GetCurrentCoreId();
|
|
|
|
|
2020-07-14 11:26:02 +01:00
|
|
|
/* Initialize readable event base. */
|
|
|
|
KReadableEvent::Initialize(nullptr);
|
|
|
|
|
|
|
|
/* Try to register the task. */
|
2021-04-07 09:25:42 +01:00
|
|
|
R_TRY(KInterruptEventTask::Register(m_interrupt_id, m_core_id, type == ams::svc::InterruptType_Level, this));
|
2020-07-14 11:26:02 +01:00
|
|
|
|
|
|
|
/* Mark initialized. */
|
2020-12-18 01:18:47 +00:00
|
|
|
m_is_initialized = true;
|
2022-02-14 22:45:32 +00:00
|
|
|
R_SUCCEED();
|
2020-07-14 11:26:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void KInterruptEvent::Finalize() {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
2021-04-07 09:25:42 +01:00
|
|
|
g_interrupt_event_task_table[m_interrupt_id]->Unregister(m_interrupt_id, m_core_id);
|
2020-12-01 22:17:25 +00:00
|
|
|
|
|
|
|
/* Perform inherited finalization. */
|
2021-10-24 05:13:26 +01:00
|
|
|
KReadableEvent::Finalize();
|
2020-07-14 11:26:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KInterruptEvent::Reset() {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
2021-09-17 23:34:24 +01:00
|
|
|
/* Lock the scheduler. */
|
|
|
|
KScopedSchedulerLock sl;
|
2020-07-14 11:26:02 +01:00
|
|
|
|
|
|
|
/* Clear the event. */
|
|
|
|
R_TRY(KReadableEvent::Reset());
|
|
|
|
|
|
|
|
/* Clear the interrupt. */
|
2021-04-07 09:25:42 +01:00
|
|
|
Kernel::GetInterruptManager().ClearInterrupt(m_interrupt_id, m_core_id);
|
2020-07-14 11:26:02 +01:00
|
|
|
|
2022-02-14 22:45:32 +00:00
|
|
|
R_SUCCEED();
|
2020-07-14 11:26:02 +01:00
|
|
|
}
|
|
|
|
|
2021-04-07 09:25:42 +01:00
|
|
|
Result KInterruptEventTask::Register(s32 interrupt_id, s32 core_id, bool level, KInterruptEvent *event) {
|
2020-07-14 11:26:02 +01:00
|
|
|
/* Lock the task table. */
|
|
|
|
KScopedLightLock lk(g_interrupt_event_lock);
|
|
|
|
|
|
|
|
/* Get a task for the id. */
|
|
|
|
bool allocated = false;
|
|
|
|
KInterruptEventTask *task = g_interrupt_event_task_table[interrupt_id];
|
|
|
|
if (task != nullptr) {
|
|
|
|
/* Check that there's not already an event for this task. */
|
2020-12-18 01:18:47 +00:00
|
|
|
R_UNLESS(task->m_event == nullptr, svc::ResultBusy());
|
2020-07-14 11:26:02 +01:00
|
|
|
} else {
|
|
|
|
/* Allocate a new task. */
|
|
|
|
task = KInterruptEventTask::Allocate();
|
|
|
|
R_UNLESS(task != nullptr, svc::ResultOutOfResource());
|
|
|
|
|
|
|
|
allocated = true;
|
|
|
|
}
|
|
|
|
|
2020-12-01 22:17:25 +00:00
|
|
|
/* Ensure that the task is cleaned up if anything goes wrong. */
|
2022-02-14 22:45:32 +00:00
|
|
|
ON_RESULT_FAILURE { if (allocated) { KInterruptEventTask::Free(task); } };
|
2020-12-01 22:17:25 +00:00
|
|
|
|
2020-07-14 11:26:02 +01:00
|
|
|
/* Register/bind the interrupt task. */
|
|
|
|
{
|
2021-09-17 23:34:24 +01:00
|
|
|
/* Lock the scheduler. */
|
|
|
|
KScopedSchedulerLock sl;
|
2020-07-14 11:26:02 +01:00
|
|
|
|
|
|
|
/* Bind the interrupt handler. */
|
2021-04-07 09:25:42 +01:00
|
|
|
R_TRY(Kernel::GetInterruptManager().BindHandler(task, interrupt_id, core_id, KInterruptController::PriorityLevel_High, true, level));
|
2020-07-14 11:26:02 +01:00
|
|
|
|
2020-12-01 22:17:25 +00:00
|
|
|
/* Set the event. */
|
2020-12-18 01:18:47 +00:00
|
|
|
task->m_event = event;
|
2020-07-14 11:26:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If we allocated, set the event in the table. */
|
|
|
|
if (allocated) {
|
|
|
|
g_interrupt_event_task_table[interrupt_id] = task;
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:45:32 +00:00
|
|
|
R_SUCCEED();
|
2020-07-14 11:26:02 +01:00
|
|
|
}
|
|
|
|
|
2021-04-07 09:25:42 +01:00
|
|
|
void KInterruptEventTask::Unregister(s32 interrupt_id, s32 core_id) {
|
2020-07-14 11:26:02 +01:00
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
|
|
|
/* Lock the task table. */
|
|
|
|
KScopedLightLock lk(g_interrupt_event_lock);
|
|
|
|
|
2021-09-17 23:34:24 +01:00
|
|
|
/* Lock the scheduler. */
|
|
|
|
KScopedSchedulerLock sl;
|
2020-12-01 22:17:25 +00:00
|
|
|
|
2020-07-14 11:26:02 +01:00
|
|
|
/* Ensure we can unregister. */
|
2020-12-01 22:17:25 +00:00
|
|
|
MESOSPHERE_ABORT_UNLESS(g_interrupt_event_task_table[interrupt_id] == this);
|
2020-12-18 01:18:47 +00:00
|
|
|
MESOSPHERE_ABORT_UNLESS(m_event != nullptr);
|
2020-07-14 11:26:02 +01:00
|
|
|
|
|
|
|
/* Unbind the interrupt. */
|
2020-12-18 01:18:47 +00:00
|
|
|
m_event = nullptr;
|
2021-04-07 09:25:42 +01:00
|
|
|
Kernel::GetInterruptManager().UnbindHandler(interrupt_id, core_id);
|
2020-07-14 11:26:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
KInterruptTask *KInterruptEventTask::OnInterrupt(s32 interrupt_id) {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
2020-08-17 22:20:24 +01:00
|
|
|
MESOSPHERE_UNUSED(interrupt_id);
|
2020-07-14 11:26:02 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KInterruptEventTask::DoTask() {
|
|
|
|
MESOSPHERE_ASSERT_THIS();
|
|
|
|
|
2021-09-17 23:34:24 +01:00
|
|
|
/* Lock the scheduler. */
|
|
|
|
KScopedSchedulerLock sl;
|
2020-07-14 11:26:02 +01:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
if (m_event != nullptr) {
|
|
|
|
m_event->Signal();
|
2020-07-14 11:26:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|