1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 05:27:46 +00:00

Kernel: Introduce the IOWindow class

This class is intended to replace all IOAddress usages in the Kernel
codebase altogether. The idea is to ensure IO can be done in
arch-specific manner that is determined mostly in compile-time, but to
still be able to use most of the Kernel code in non-x86 builds. Specific
devices that rely on x86-specific IO instructions are already placed in
the Arch/x86 directory and are omitted for non-x86 builds.

The reason this works so well is the fact that x86 IO space acts in a
similar fashion to the traditional memory space being available in most
CPU architectures - the x86 IO space is essentially just an array of
bytes like the physical memory address space, but requires x86 IO
instructions to load and store data. Therefore, many devices allow host
software to interact with the hardware registers in both ways, with a
noticeable trend even in the modern x86 hardware to move away from the
old x86 IO space to exclusively using memory-mapped IO.

Therefore, the IOWindow class encapsulates both methods for x86 builds.
The idea is to allow PCI devices to be used in either way in x86 builds,
so when trying to map an IOWindow on a PCI BAR, the Kernel will try to
find the proper method being declared with the PCI BAR flags.
For old PCI hardware on non-x86 builds this might turn into a problem as
we can't use port mapped IO, so the Kernel will gracefully fail with
ENOTSUP error code if that's the case, as there's really nothing we can
do within such case.

For general IO, the read{8,16,32} and write{8,16,32} methods are
available as a convenient API for other places in the Kernel. There are
simply no direct 64-bit IO API methods yet, as it's not needed right now
and is not considered to be Arch-agnostic too - the x86 IO space doesn't
support generating 64 bit cycle on IO bus and instead requires two 2
32-bit accesses. If for whatever reason it appears to be necessary to do
IO in such manner, it could probably be added with some neat tricks to
do so. It is recommended to use Memory::TypedMapping struct if direct 64
bit IO is actually needed.
This commit is contained in:
Liav A 2022-09-23 11:50:04 +03:00 committed by Linus Groh
parent 6bafbd64e2
commit 05ba034000
36 changed files with 919 additions and 469 deletions

View file

@ -7,10 +7,10 @@
#pragma once
#include <AK/NonnullOwnPtrVector.h>
#include <Kernel/Arch/x86/IO.h>
#include <Kernel/Bus/PCI/Access.h>
#include <Kernel/Bus/PCI/Device.h>
#include <Kernel/Bus/VirtIO/Queue.h>
#include <Kernel/IOWindow.h>
#include <Kernel/Interrupts/IRQHandler.h>
#include <Kernel/Memory/MemoryManager.h>
@ -94,30 +94,6 @@ public:
protected:
virtual StringView class_name() const { return "VirtIO::Device"sv; }
explicit Device(PCI::DeviceIdentifier const&);
struct MappedMMIO {
OwnPtr<Memory::Region> base;
size_t size { 0 };
template<typename T>
T read(u32 offset) const
{
if (!base)
return 0;
VERIFY(size >= sizeof(T));
VERIFY(offset + sizeof(T) <= size);
return *(volatile T*)(base->vaddr().offset(offset).get());
}
template<typename T>
void write(u32 offset, T value)
{
if (!base)
return;
VERIFY(size >= sizeof(T));
VERIFY(offset + sizeof(T) <= size);
*(volatile T*)(base->vaddr().offset(offset).get()) = value;
}
};
Configuration const* get_config(ConfigurationType cfg_type, u32 index = 0) const
{
@ -156,7 +132,7 @@ protected:
void config_write32(Configuration const&, u32, u32);
void config_write64(Configuration const&, u32, u64);
auto mapping_for_bar(u8) -> MappedMMIO&;
auto mapping_for_bar(u8) -> IOWindow&;
u8 read_status_bits();
void mask_status_bits(u8 status_mask);
@ -203,18 +179,6 @@ protected:
virtual void handle_queue_update(u16 queue_index) = 0;
private:
template<typename T>
void out(u16 address, T value)
{
m_io_base.offset(address).out(value);
}
template<typename T>
T in(u16 address)
{
return m_io_base.offset(address).in<T>();
}
bool accept_device_features(u64 device_features, u64 accepted_features);
bool setup_queue(u16 queue_index);
@ -232,8 +196,8 @@ private:
Configuration const* m_notify_cfg { nullptr }; // Cached due to high usage
Configuration const* m_isr_cfg { nullptr }; // Cached due to high usage
IOAddress m_io_base;
MappedMMIO m_mmio[6];
IOWindow& base_io_window();
Array<OwnPtr<IOWindow>, 6> m_register_bases;
StringView const m_class_name;