Kernel: Use nested Kernel::PCI namespaces more to reduce PCI:: spam

This commit is contained in:
Andreas Kling 2020-04-08 17:29:37 +02:00
parent e5ec332eb1
commit a6a8f36a83
Notes: sideshowbarker 2024-07-19 07:47:57 +09:00
4 changed files with 57 additions and 50 deletions

View file

@ -28,17 +28,18 @@
#include <Kernel/PCI/IOAccess.h>
namespace Kernel {
namespace PCI {
static PCI::Access* s_access;
static Access* s_access;
inline void write8(PCI::Address address, u32 field, u8 value) { PCI::Access::the().write8_field(address, field, value); }
inline void write16(PCI::Address address, u32 field, u16 value) { PCI::Access::the().write16_field(address, field, value); }
inline void write32(PCI::Address address, u32 field, u32 value) { PCI::Access::the().write32_field(address, field, value); }
inline u8 read8(PCI::Address address, u32 field) { return PCI::Access::the().read8_field(address, field); }
inline u16 read16(PCI::Address address, u32 field) { return PCI::Access::the().read16_field(address, field); }
inline u32 read32(PCI::Address address, u32 field) { return PCI::Access::the().read32_field(address, field); }
inline void write8(Address address, u32 field, u8 value) { Access::the().write8_field(address, field, value); }
inline void write16(Address address, u32 field, u16 value) { Access::the().write16_field(address, field, value); }
inline void write32(Address address, u32 field, u32 value) { Access::the().write32_field(address, field, value); }
inline u8 read8(Address address, u32 field) { return Access::the().read8_field(address, field); }
inline u16 read16(Address address, u32 field) { return Access::the().read16_field(address, field); }
inline u32 read32(Address address, u32 field) { return Access::the().read32_field(address, field); }
PCI::Access& PCI::Access::the()
Access& Access::the()
{
if (s_access == nullptr) {
ASSERT_NOT_REACHED(); // We failed to initialize the PCI subsystem, so stop here!
@ -46,22 +47,22 @@ PCI::Access& PCI::Access::the()
return *s_access;
}
bool PCI::Access::is_initialized()
bool Access::is_initialized()
{
return (s_access != nullptr);
}
PCI::Access::Access()
Access::Access()
{
s_access = this;
}
static u16 read_type(PCI::Address address)
static u16 read_type(Address address)
{
return (read8(address, PCI_CLASS) << 8u) | read8(address, PCI_SUBCLASS);
}
void PCI::Access::enumerate_functions(int type, u8 bus, u8 slot, u8 function, Function<void(Address, ID)>& callback)
void Access::enumerate_functions(int type, u8 bus, u8 slot, u8 function, Function<void(Address, ID)>& callback)
{
Address address(0, bus, slot, function);
if (type == -1 || type == read_type(address))
@ -76,7 +77,7 @@ void PCI::Access::enumerate_functions(int type, u8 bus, u8 slot, u8 function, Fu
}
}
void PCI::Access::enumerate_slot(int type, u8 bus, u8 slot, Function<void(Address, ID)>& callback)
void Access::enumerate_slot(int type, u8 bus, u8 slot, Function<void(Address, ID)>& callback)
{
Address address(0, bus, slot, 0);
if (read16_field(address, PCI_VENDOR_ID) == PCI_NONE)
@ -91,17 +92,15 @@ void PCI::Access::enumerate_slot(int type, u8 bus, u8 slot, Function<void(Addres
}
}
void PCI::Access::enumerate_bus(int type, u8 bus, Function<void(Address, ID)>& callback)
void Access::enumerate_bus(int type, u8 bus, Function<void(Address, ID)>& callback)
{
for (u8 slot = 0; slot < 32; ++slot)
enumerate_slot(type, bus, slot, callback);
}
namespace PCI {
void enumerate_all(Function<void(Address, ID)> callback)
{
PCI::Access::the().enumerate_all(callback);
Access::the().enumerate_all(callback);
}
void raw_access(Address address, u32 field, size_t access_size, u32 value)
@ -226,6 +225,6 @@ size_t get_BAR_space_size(Address address, u8 bar_number)
space_size = (~space_size) + 1;
return space_size;
}
}
}
}

View file

@ -27,22 +27,26 @@
#include <Kernel/PCI/Device.h>
namespace Kernel {
PCI::Device::Device(PCI::Address address)
: IRQHandler(PCI::get_interrupt_line(address))
namespace PCI {
Device::Device(Address address)
: IRQHandler(get_interrupt_line(address))
, m_pci_address(address)
{
// FIXME: Register PCI device somewhere...
}
PCI::Device::Device(PCI::Address address, u8 interrupt_vector)
Device::Device(Address address, u8 interrupt_vector)
: IRQHandler(interrupt_vector)
, m_pci_address(address)
{
// FIXME: Register PCI device somewhere...
}
PCI::Device::~Device()
Device::~Device()
{
// FIXME: Unregister the device
}
}
}

View file

@ -28,53 +28,54 @@
#include <LibBareMetal/IO.h>
namespace Kernel {
namespace PCI {
void PCI::IOAccess::initialize()
void IOAccess::initialize()
{
if (!PCI::Access::is_initialized())
new PCI::IOAccess();
if (!Access::is_initialized())
new IOAccess();
}
PCI::IOAccess::IOAccess()
IOAccess::IOAccess()
{
klog() << "PCI: Using IO Mechanism for PCI Configuartion Space Access";
}
u8 PCI::IOAccess::read8_field(Address address, u32 field)
u8 IOAccess::read8_field(Address address, u32 field)
{
IO::out32(PCI_ADDRESS_PORT, address.io_address_for_field(field));
return IO::in8(PCI_VALUE_PORT + (field & 3));
}
u16 PCI::IOAccess::read16_field(Address address, u32 field)
u16 IOAccess::read16_field(Address address, u32 field)
{
IO::out32(PCI_ADDRESS_PORT, address.io_address_for_field(field));
return IO::in16(PCI_VALUE_PORT + (field & 2));
}
u32 PCI::IOAccess::read32_field(Address address, u32 field)
u32 IOAccess::read32_field(Address address, u32 field)
{
IO::out32(PCI_ADDRESS_PORT, address.io_address_for_field(field));
return IO::in32(PCI_VALUE_PORT);
}
void PCI::IOAccess::write8_field(Address address, u32 field, u8 value)
void IOAccess::write8_field(Address address, u32 field, u8 value)
{
IO::out32(PCI_ADDRESS_PORT, address.io_address_for_field(field));
IO::out8(PCI_VALUE_PORT + (field & 3), value);
}
void PCI::IOAccess::write16_field(Address address, u32 field, u16 value)
void IOAccess::write16_field(Address address, u32 field, u16 value)
{
IO::out32(PCI_ADDRESS_PORT, address.io_address_for_field(field));
IO::out16(PCI_VALUE_PORT + (field & 2), value);
}
void PCI::IOAccess::write32_field(Address address, u32 field, u32 value)
void IOAccess::write32_field(Address address, u32 field, u32 value)
{
IO::out32(PCI_ADDRESS_PORT, address.io_address_for_field(field));
IO::out32(PCI_VALUE_PORT, value);
}
void PCI::IOAccess::enumerate_all(Function<void(Address, ID)>& callback)
void IOAccess::enumerate_all(Function<void(Address, ID)>& callback)
{
// Single PCI host controller.
if ((read8_field(Address(), PCI_HEADER_TYPE) & 0x80) == 0) {
@ -91,3 +92,4 @@ void PCI::IOAccess::enumerate_all(Function<void(Address, ID)>& callback)
}
}
}

View file

@ -34,37 +34,38 @@
#include <LibBareMetal/IO.h>
namespace Kernel {
namespace PCI {
static PCI::Initializer* s_pci_initializer;
static Initializer* s_pci_initializer;
PCI::Initializer& PCI::Initializer::the()
Initializer& Initializer::the()
{
if (s_pci_initializer == nullptr) {
s_pci_initializer = new PCI::Initializer();
s_pci_initializer = new Initializer();
}
return *s_pci_initializer;
}
void PCI::Initializer::initialize_pci_mmio_access(PhysicalAddress mcfg)
void Initializer::initialize_pci_mmio_access(PhysicalAddress mcfg)
{
PCI::MMIOAccess::initialize(mcfg);
MMIOAccess::initialize(mcfg);
detect_devices();
}
void PCI::Initializer::initialize_pci_io_access()
void Initializer::initialize_pci_io_access()
{
PCI::IOAccess::initialize();
IOAccess::initialize();
detect_devices();
}
void PCI::Initializer::detect_devices()
void Initializer::detect_devices()
{
PCI::enumerate_all([&](const PCI::Address& address, PCI::ID id) {
enumerate_all([&](const Address& address, ID id) {
klog() << "PCI: device @ " << String::format("%w", address.seg()) << ":" << String::format("%b", address.bus()) << ":" << String::format("%b", address.slot()) << "." << String::format("%d", address.function()) << " [" << String::format("%w", id.vendor_id) << ":" << String::format("%w", id.device_id) << "]";
E1000NetworkAdapter::detect(address);
RTL8139NetworkAdapter::detect(address);
});
}
void PCI::Initializer::test_and_initialize(bool disable_pci_mmio)
void Initializer::test_and_initialize(bool disable_pci_mmio)
{
if (disable_pci_mmio) {
if (test_pci_io()) {
@ -95,10 +96,10 @@ void PCI::Initializer::test_and_initialize(bool disable_pci_mmio)
}
}
}
PCI::Initializer::Initializer()
Initializer::Initializer()
{
}
bool PCI::Initializer::test_acpi()
bool Initializer::test_acpi()
{
if ((kernel_command_line().contains("noacpi")) || !ACPI::Parser::the().is_operable())
return false;
@ -106,7 +107,7 @@ bool PCI::Initializer::test_acpi()
return true;
}
bool PCI::Initializer::test_pci_io()
bool Initializer::test_pci_io()
{
klog() << "Testing PCI via manual probing... ";
u32 tmp = 0x80000000;
@ -121,17 +122,17 @@ bool PCI::Initializer::test_pci_io()
return false;
}
bool PCI::Initializer::test_pci_mmio()
bool Initializer::test_pci_mmio()
{
return !ACPI::Parser::the().find_table("MCFG").is_null();
}
void PCI::Initializer::initialize_pci_mmio_access_after_test()
void Initializer::initialize_pci_mmio_access_after_test()
{
initialize_pci_mmio_access(ACPI::Parser::the().find_table("MCFG"));
}
void PCI::Initializer::dismiss()
void Initializer::dismiss()
{
if (s_pci_initializer == nullptr)
return;
@ -140,8 +141,9 @@ void PCI::Initializer::dismiss()
s_pci_initializer = nullptr;
}
PCI::Initializer::~Initializer()
Initializer::~Initializer()
{
}
}
}