gpio: implement more of server library for boot sysmodule client usage

This commit is contained in:
Michael Scire 2020-10-31 03:22:01 -07:00 committed by SciresM
parent e1b5d81d65
commit bd3ab76fd2
26 changed files with 1162 additions and 24 deletions

View file

@ -19,6 +19,71 @@
namespace ams::gpio::driver::board::nintendo_nx::impl {
void InterruptEventHandler::Initialize(DriverImpl *drv, os::InterruptName intr, int ctlr) {
/* Set fields. */
this->driver = drv;
this->interrupt_name = intr;
this->controller_number = ctlr;
/* Initialize interrupt event. */
os::InitializeInterruptEvent(std::addressof(this->interrupt_event), intr, os::EventClearMode_ManualClear);
/* Initialize base. */
IEventHandler::Initialize(std::addressof(this->interrupt_event));
}
void InterruptEventHandler::HandleEvent() {
/* Lock the driver's interrupt mutex. */
std::scoped_lock lk(this->driver->interrupt_control_mutex);
/* Check each pad. */
bool found = false;
for (auto it = this->driver->interrupt_pad_list.begin(); !found && it != this->driver->interrupt_pad_list.end(); ++it) {
found = this->CheckAndHandleInterrupt(*it);
}
/* If we didn't find a pad, clear the interrupt event. */
if (!found) {
os::ClearInterruptEvent(std::addressof(this->interrupt_event));
}
}
bool InterruptEventHandler::CheckAndHandleInterrupt(TegraPad &pad) {
/* Get the pad's number. */
const InternalGpioPadNumber pad_number = static_cast<InternalGpioPadNumber>(pad.GetPadNumber());
/* Check if the pad matches our controller number. */
if (this->controller_number != ConvertInternalGpioPadNumberToController(pad_number)) {
return false;
}
/* Get the addresses of INT_STA, INT_ENB. */
const uintptr_t sta_address = GetGpioRegisterAddress(this->driver->gpio_virtual_address, GpioRegisterType_GPIO_INT_STA, pad_number);
const uintptr_t enb_address = GetGpioRegisterAddress(this->driver->gpio_virtual_address, GpioRegisterType_GPIO_INT_STA, pad_number);
const uintptr_t pad_index = ConvertInternalGpioPadNumberToBitIndex(pad_number);
/* Check if both STA and ENB are set. */
if (reg::Read(sta_address, 1u << pad_index) == 0 || reg::Read(enb_address, 1u << pad_index) == 0) {
return false;
}
/* The pad is signaled. First, clear the enb bit. */
SetMaskedBit(enb_address, pad_index, 0);
reg::Read(enb_address);
/* Disable the interrupt on the pad. */
pad.SetInterruptEnabled(false);
this->driver->RemoveInterruptPad(std::addressof(pad));
/* Clear the interrupt event. */
os::ClearInterruptEvent(std::addressof(this->interrupt_event));
/* Signal the pad's bound event. */
pad.SignalInterruptBoundEvent();
return true;
}
DriverImpl::DriverImpl(dd::PhysicalAddress reg_paddr, size_t size) : gpio_physical_address(reg_paddr), gpio_virtual_address(), suspend_handler(this), interrupt_pad_list(), interrupt_control_mutex() {
/* Get the corresponding virtual address for our physical address. */
this->gpio_virtual_address = dd::QueryIoMapping(reg_paddr, size);

View file

@ -29,13 +29,13 @@ namespace ams::gpio::driver::board::nintendo_nx::impl {
DriverImpl *driver;
os::InterruptName interrupt_name;
os::InterruptEventType interrupt_event;
int port_number;
int controller_number;
private:
void CheckAndHandleInterrupt(TegraPad *pad);
bool CheckAndHandleInterrupt(TegraPad &pad);
public:
InterruptEventHandler() : IEventHandler(), driver(nullptr), interrupt_name(), interrupt_event(), port_number() { /* ... */ }
InterruptEventHandler() : IEventHandler(), driver(nullptr), interrupt_name(), interrupt_event(), controller_number() { /* ... */ }
void Initialize(DriverImpl *driver, os::InterruptName intr, int port);
void Initialize(DriverImpl *drv, os::InterruptName intr, int ctlr);
virtual void HandleEvent() override;
};
@ -44,11 +44,12 @@ namespace ams::gpio::driver::board::nintendo_nx::impl {
NON_COPYABLE(DriverImpl);
NON_MOVEABLE(DriverImpl);
AMS_DDSF_CASTABLE_TRAITS(ams::gpio::driver::board::nintendo_nx::impl::DriverImpl, ::ams::gpio::driver::IGpioDriver);
friend class InterruptEventHandler;
private:
dd::PhysicalAddress gpio_physical_address;
uintptr_t gpio_virtual_address;
SuspendHandler suspend_handler;
TegraPad::List interrupt_pad_list;
TegraPad::InterruptList interrupt_pad_list;
mutable os::SdkMutex interrupt_control_mutex;
public:
DriverImpl(dd::PhysicalAddress reg_paddr, size_t size);
@ -96,6 +97,33 @@ namespace ams::gpio::driver::board::nintendo_nx::impl {
virtual Result SuspendLow() override;
virtual Result Resume() override;
virtual Result ResumeLow() override;
private:
static constexpr ALWAYS_INLINE TegraPad &GetTegraPad(Pad *pad) {
AMS_ASSERT(pad != nullptr);
return static_cast<TegraPad &>(*pad);
}
static ALWAYS_INLINE const PadInfo &GetInfo(Pad *pad) {
return GetTegraPad(pad).GetInfo();
}
static ALWAYS_INLINE PadStatus &GetStatus(Pad *pad) {
return GetTegraPad(pad).GetStatus();
}
void AddInterruptPad(TegraPad *pad) {
AMS_ASSERT(pad != nullptr);
if (!pad->IsLinkedToInterruptBoundPadList()) {
this->interrupt_pad_list.push_back(*pad);
}
}
void RemoveInterruptPad(TegraPad *pad) {
AMS_ASSERT(pad != nullptr);
if (pad->IsLinkedToInterruptBoundPadList()) {
this->interrupt_pad_list.erase(this->interrupt_pad_list.iterator_to(*pad));
}
}
};
}

View file

@ -0,0 +1,48 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* 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 <stratosphere.hpp>
#include "impl/gpio_driver_core.hpp"
namespace ams::gpio::driver {
bool Pad::IsAnySessionBoundToInterrupt() const {
/* Check to see if any session has an interrupt bound. */
bool bound = false;
this->ForEachSession([&](const ddsf::ISession &session) -> bool {
const auto &impl = session.SafeCastTo<impl::PadSessionImpl>();
if (impl.IsInterruptBound()) {
bound = true;
return false;
}
return true;
});
return bound;
}
void Pad::SignalInterruptBoundEvent() {
/* Signal relevant sessions. */
this->ForEachSession([&](ddsf::ISession &session) -> bool {
auto &impl = session.SafeCastTo<impl::PadSessionImpl>();
if (impl.IsInterruptBound()) {
impl.SignalInterruptBoundEvent();
}
return true;
});
}
}

View file

@ -0,0 +1,150 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* 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 <stratosphere.hpp>
namespace ams::gpio::driver::impl {
Result PadSessionImpl::Open(Pad *pad, ddsf::AccessMode access_mode) {
/* Check if the pad has any open sessions. */
const bool first_session = !pad->HasAnyOpenSession();
/* Open the session. */
R_TRY(ddsf::OpenSession(pad, this, access_mode));
auto pad_guard = SCOPE_GUARD { ddsf::CloseSession(this); };
/* If we're the first, we want to initialize the pad. */
if (first_session) {
R_TRY(pad->GetDriver().SafeCastTo<IGpioDriver>().InitializePad(pad));
}
/* We opened successfully. */
pad_guard.Cancel();
return ResultSuccess();
}
void PadSessionImpl::Close() {
/* If the session isn't open, nothing to do. */
if (!this->IsOpen()) {
return;
}
/* Unbind the interrupt, if it's bound. */
if (this->IsInterruptBound()) {
this->UnbindInterrupt();
}
/* Get the pad we're a session for. */
auto &pad = this->GetDevice().SafeCastTo<Pad>();
/* Close the session. */
ddsf::CloseSession(this);
/* If we were the last session on the pad, finalize the pad. */
if (!pad.HasAnyOpenSession()) {
pad.GetDriver().SafeCastTo<IGpioDriver>().FinalizePad(std::addressof(pad));
}
}
Result PadSessionImpl::BindInterrupt(os::SystemEventType *event) {
/* Acquire exclusive access to the relevant interrupt control mutex. */
auto &pad = this->GetDevice().SafeCastTo<Pad>();
auto &mutex = pad.GetDriver().SafeCastTo<IGpioDriver>().GetInterruptControlMutex(pad);
std::scoped_lock lk(mutex);
/* Check that we're not already bound. */
R_UNLESS(!this->IsInterruptBound(), gpio::ResultAlreadyBound());
R_UNLESS(!this->GetDevice().SafeCastTo<Pad>().IsAnySessionBoundToInterrupt(), gpio::ResultAlreadyBound());
/* Create the system event. */
R_TRY(os::CreateSystemEvent(event, os::EventClearMode_ManualClear, true));
auto ev_guard = SCOPE_GUARD { os::DestroySystemEvent(event); };
/* Attach the event to our holder. */
this->event_holder.AttachEvent(event);
auto hl_guard = SCOPE_GUARD { this->event_holder.DetachEvent(); };
/* Update interrupt needed. */
R_TRY(this->UpdateDriverInterruptEnabled());
/* We succeeded. */
hl_guard.Cancel();
ev_guard.Cancel();
return ResultSuccess();
}
void PadSessionImpl::UnbindInterrupt() {
/* Acquire exclusive access to the relevant interrupt control mutex. */
auto &pad = this->GetDevice().SafeCastTo<Pad>();
auto &mutex = pad.GetDriver().SafeCastTo<IGpioDriver>().GetInterruptControlMutex(pad);
std::scoped_lock lk(mutex);
/* If we're not bound, nothing to do. */
if (!this->IsInterruptBound()) {
return;
}
/* Detach and destroy the event */
os::DestroySystemEvent(this->event_holder.DetachEvent());
/* Update interrupt needed. */
R_ABORT_UNLESS(this->UpdateDriverInterruptEnabled());
}
Result PadSessionImpl::UpdateDriverInterruptEnabled() {
/* Check we have exclusive access to the relevant interrupt control mutex. */
auto &pad = this->GetDevice().SafeCastTo<Pad>();
auto &driver = pad.GetDriver().SafeCastTo<IGpioDriver>();
AMS_ASSERT(driver.GetInterruptControlMutex(pad).IsLockedByCurrentThread());
/* Set interrupt enabled. */
return driver.SetInterruptEnabled(std::addressof(pad), pad.IsInterruptRequiredForDriver());
}
Result PadSessionImpl::GetInterruptEnabled(bool *out) const {
*out = this->GetDevice().SafeCastTo<Pad>().IsInterruptEnabled();
return ResultSuccess();
}
Result PadSessionImpl::SetInterruptEnabled(bool en) {
/* Acquire exclusive access to the relevant interrupt control mutex. */
auto &pad = this->GetDevice().SafeCastTo<Pad>();
auto &mutex = pad.GetDriver().SafeCastTo<IGpioDriver>().GetInterruptControlMutex(pad);
std::scoped_lock lk(mutex);
/* Set the interrupt enable. */
const bool prev = pad.IsInterruptEnabled();
pad.SetInterruptEnabled(en);
auto pad_guard = SCOPE_GUARD { pad.SetInterruptEnabled(prev); };
/* Update interrupt needed. */
R_TRY(this->UpdateDriverInterruptEnabled());
pad_guard.Cancel();
return ResultSuccess();
}
void PadSessionImpl::SignalInterruptBoundEvent() {
/* Check we have exclusive access to the relevant interrupt control mutex. */
auto &pad = this->GetDevice().SafeCastTo<Pad>();
auto &driver = pad.GetDriver().SafeCastTo<IGpioDriver>();
AMS_ASSERT(driver.GetInterruptControlMutex(pad).IsLockedByCurrentThread());
if (auto *event = this->event_holder.GetSystemEvent(); event != nullptr) {
os::SignalSystemEvent(event);
}
}
}