Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kernel: New IO mapped Register API and a rework of the E1000 drivers #25521

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions AK/Concepts.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ concept Enum = IsEnum<T>;
template<typename T, typename U>
concept SameAs = IsSame<T, U>;

template<typename T, typename U>
concept SameAsIgnoringCVReference = IsSameIgnoringCV<RemoveReference<T>, RemoveReference<U>>;

template<class From, class To>
concept ConvertibleTo = IsConvertible<From, To>;

Expand Down Expand Up @@ -182,6 +185,7 @@ using AK::Concepts::IteratorPairWith;
using AK::Concepts::OneOf;
using AK::Concepts::OneOfIgnoringCV;
using AK::Concepts::SameAs;
using AK::Concepts::SameAsIgnoringCVReference;
using AK::Concepts::Signed;
using AK::Concepts::SpecializationOf;
using AK::Concepts::Unsigned;
Expand Down
1 change: 0 additions & 1 deletion Kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ set(KERNEL_SOURCES
Library/KLexicalPath.cpp
Library/KString.cpp
Library/UserOrKernelBuffer.cpp
Net/Intel/E1000ENetworkAdapter.cpp
Net/Intel/E1000NetworkAdapter.cpp
Net/Realtek/RTL8168NetworkAdapter.cpp
Net/VirtIO/VirtIONetworkAdapter.cpp
Expand Down
134 changes: 134 additions & 0 deletions Kernel/Library/IORegister.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright (c) 2024, Leon Albrecht <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#pragma once

#include <AK/Concepts.h>
#include <AK/Types.h>
#include <Kernel/Library/IOWindow.h>

namespace Kernel {

enum IOSize {
Byte,
Word,
DWord,
};

template<typename T>
consteval IOSize io_size_for()
{
if constexpr (sizeof(T) == 1)
return IOSize::Byte;
else if constexpr (sizeof(T) == 2)
return IOSize::Word;
else if constexpr (sizeof(T) == 4)
return IOSize::DWord;
else
static_assert(false, "Invalid IO size");
}

template<Enum E, E L, typename T, bool _Debug = false>
struct IOReg {
static constexpr E Location = L;
static constexpr IOSize Size = io_size_for<T>();
static constexpr bool Debug = _Debug;
using Type = T;
};

template<typename T, typename E>
concept RegisterEntry = IsEnum<E> && requires {
typename T::Type;
{ T::Location } -> SameAsIgnoringCVReference<E>;
{ T::Size } -> SameAsIgnoringCVReference<IOSize>;
};

template<Enum E, RegisterEntry<E>... Entries>
class IORegister {
template<E Reg, RegisterEntry<E> First, RegisterEntry<E>... Rest>
static consteval auto find_entry(First, Rest...)
{
if constexpr (First::Location == Reg)
return First();
else if constexpr (sizeof...(Rest) > 0)
return find_entry<Reg>(Rest()...);
else
static_assert(false, "Register not found");
}

public:
explicit IORegister(NonnullOwnPtr<IOWindow> window)
: m_window(move(window))
{
}

template<E Reg, typename Entry = decltype(find_entry<Reg>(declval<Entries>()...))>
requires(Entry::Size == IOSize::Byte)
auto read()
{
auto value = m_window->read8(static_cast<u64>(Reg));
if constexpr (Entry::Debug)
dbgln("Read {:#04x}: {:#02x}", (u32)Entry::Location, value);
return bit_cast<typename Entry::Type>(value);
}

template<E Reg, typename Entry = decltype(find_entry<Reg>(declval<Entries>()...))>
requires(Entry::Size == IOSize::Word)
auto read()
{
auto value = m_window->read16(static_cast<u64>(Reg));
if constexpr (Entry::Debug)
dbgln("Read {:#04x}: {:#04x}", (u32)Entry::Location, value);
return bit_cast<typename Entry::Type>(value);
}

template<E Reg, typename Entry = decltype(find_entry<Reg>(declval<Entries>()...))>
requires(Entry::Size == IOSize::DWord)
auto read()
{
auto value = m_window->read32(static_cast<u64>(Reg));
if constexpr (Entry::Debug)
dbgln("Read {:#04x}: {:#08x}", (u32)Entry::Location, value);
return bit_cast<typename Entry::Type>(value);
}

template<E Reg, typename Entry = decltype(find_entry<Reg>(declval<Entries>()...))>
requires(Entry::Size == IOSize::Byte)
void write(Entry::Type value)
{
auto v = bit_cast<u8>(value);
if constexpr (Entry::Debug)
dbgln("Write {:#04x}: {:#02x}", (u32)Entry::Location, v);
m_window->write8(static_cast<u64>(Reg), v);
}

template<E Reg, typename Entry = decltype(find_entry<Reg>(declval<Entries>()...))>
requires(Entry::Size == IOSize::Word)
void write(Entry::Type value)
{
auto v = bit_cast<u16>(value);
if constexpr (Entry::Debug)
dbgln("Write {:#04x}: {:#04x}", (u32)Entry::Location, v);
m_window->write16(static_cast<u64>(Reg), v);
}

template<E Reg, typename Entry = decltype(find_entry<Reg>(declval<Entries>()...))>
requires(Entry::Size == IOSize::DWord)
void write(Entry::Type value)
{
auto v = bit_cast<u32>(value);
if constexpr (Entry::Debug)
dbgln("Write {:#04x}: {:#08x}", (u32)Entry::Location, v);
m_window->write32(static_cast<u64>(Reg), v);
}

IOWindow& window() { return *m_window; }

private:
NonnullOwnPtr<IOWindow> m_window;
};

}
Loading