1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:27:44 +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

@ -193,7 +193,8 @@ UNMAP_AFTER_INIT LockRefPtr<RTL8168NetworkAdapter> RTL8168NetworkAdapter::try_to
auto interface_name_or_error = NetworkingManagement::generate_interface_name_from_pci_address(pci_device_identifier);
if (interface_name_or_error.is_error())
return {};
return adopt_lock_ref_if_nonnull(new (nothrow) RTL8168NetworkAdapter(pci_device_identifier.address(), irq, interface_name_or_error.release_value()));
auto registers_io_window = MUST(IOWindow::create_for_pci_device_bar(pci_device_identifier, PCI::HeaderType0BaseRegister::BAR0));
return adopt_lock_ref_if_nonnull(new (nothrow) RTL8168NetworkAdapter(pci_device_identifier.address(), irq, move(registers_io_window), interface_name_or_error.release_value()));
}
bool RTL8168NetworkAdapter::determine_supported_version() const
@ -241,16 +242,16 @@ bool RTL8168NetworkAdapter::determine_supported_version() const
}
}
UNMAP_AFTER_INIT RTL8168NetworkAdapter::RTL8168NetworkAdapter(PCI::Address address, u8 irq, NonnullOwnPtr<KString> interface_name)
UNMAP_AFTER_INIT RTL8168NetworkAdapter::RTL8168NetworkAdapter(PCI::Address address, u8 irq, NonnullOwnPtr<IOWindow> registers_io_window, NonnullOwnPtr<KString> interface_name)
: NetworkAdapter(move(interface_name))
, PCI::Device(address)
, IRQHandler(irq)
, m_io_base(PCI::get_BAR0(pci_address()) & ~1)
, m_registers_io_window(move(registers_io_window))
, m_rx_descriptors_region(MM.allocate_contiguous_kernel_region(Memory::page_round_up(sizeof(TXDescriptor) * (number_of_rx_descriptors + 1)).release_value_but_fixme_should_propagate_errors(), "RTL8168 RX"sv, Memory::Region::Access::ReadWrite).release_value())
, m_tx_descriptors_region(MM.allocate_contiguous_kernel_region(Memory::page_round_up(sizeof(RXDescriptor) * (number_of_tx_descriptors + 1)).release_value_but_fixme_should_propagate_errors(), "RTL8168 TX"sv, Memory::Region::Access::ReadWrite).release_value())
{
dmesgln("RTL8168: Found @ {}", pci_address());
dmesgln("RTL8168: I/O port base: {}", m_io_base);
dmesgln("RTL8168: I/O port base: {}", m_registers_io_window);
identify_chip_version();
dmesgln("RTL8168: Version detected - {} ({}{})", possible_device_name(), (u8)m_version, m_version_uncertain ? "?" : "");
@ -1258,39 +1259,39 @@ void RTL8168NetworkAdapter::receive()
void RTL8168NetworkAdapter::out8(u16 address, u8 data)
{
m_io_base.offset(address).out(data);
m_registers_io_window->write8(address, data);
}
void RTL8168NetworkAdapter::out16(u16 address, u16 data)
{
m_io_base.offset(address).out(data);
m_registers_io_window->write16(address, data);
}
void RTL8168NetworkAdapter::out32(u16 address, u32 data)
{
m_io_base.offset(address).out(data);
m_registers_io_window->write32(address, data);
}
void RTL8168NetworkAdapter::out64(u16 address, u64 data)
{
// ORDER MATTERS: Some NICs require the high part of the address to be written first
m_io_base.offset(address + 4).out((u32)(data >> 32));
m_io_base.offset(address).out((u32)(data & 0xFFFFFFFF));
m_registers_io_window->write32(address + 4, (u32)(data >> 32));
m_registers_io_window->write32(address, (u32)(data & 0xFFFFFFFF));
}
u8 RTL8168NetworkAdapter::in8(u16 address)
{
return m_io_base.offset(address).in<u8>();
return m_registers_io_window->read8(address);
}
u16 RTL8168NetworkAdapter::in16(u16 address)
{
return m_io_base.offset(address).in<u16>();
return m_registers_io_window->read16(address);
}
u32 RTL8168NetworkAdapter::in32(u16 address)
{
return m_io_base.offset(address).in<u32>();
return m_registers_io_window->read32(address);
}
void RTL8168NetworkAdapter::phy_out(u8 address, u16 data)