From 65f939b55c3d2c7c5e9f7f8baf4b748693bf513a Mon Sep 17 00:00:00 2001 From: Liav A Date: Fri, 10 Apr 2020 20:25:03 +0300 Subject: [PATCH] Kernel: Keep records of PCI::Address & PCI::ID pairs for enumeration --- Kernel/Devices/BXVGADevice.cpp | 2 +- Kernel/Devices/PATAChannel.cpp | 2 +- Kernel/FileSystem/ProcFS.cpp | 2 +- Kernel/PCI/Access.cpp | 11 +++++++++-- Kernel/PCI/Access.h | 6 +++++- Kernel/PCI/Definitions.h | 18 +++++++++++++++++- Kernel/PCI/IOAccess.cpp | 5 ++++- Kernel/PCI/IOAccess.h | 2 +- Kernel/PCI/Initializer.cpp | 2 +- Kernel/PCI/MMIOAccess.cpp | 8 +++++++- Kernel/PCI/MMIOAccess.h | 2 +- Kernel/init.cpp | 2 +- 12 files changed, 49 insertions(+), 13 deletions(-) diff --git a/Kernel/Devices/BXVGADevice.cpp b/Kernel/Devices/BXVGADevice.cpp index 508f8504127..24c878d43bf 100644 --- a/Kernel/Devices/BXVGADevice.cpp +++ b/Kernel/Devices/BXVGADevice.cpp @@ -157,7 +157,7 @@ u32 BXVGADevice::find_framebuffer_address() static const PCI::ID bochs_vga_id = { 0x1234, 0x1111 }; static const PCI::ID virtualbox_vga_id = { 0x80ee, 0xbeef }; u32 framebuffer_address = 0; - PCI::enumerate_all([&framebuffer_address](const PCI::Address& address, PCI::ID id) { + PCI::enumerate([&framebuffer_address](const PCI::Address& address, PCI::ID id) { if (id == bochs_vga_id || id == virtualbox_vga_id) { framebuffer_address = PCI::get_BAR0(address) & 0xfffffff0; klog() << "BXVGA: framebuffer @ " << PhysicalAddress(framebuffer_address); diff --git a/Kernel/Devices/PATAChannel.cpp b/Kernel/Devices/PATAChannel.cpp index b7ed53b87fd..982ba85b5f8 100644 --- a/Kernel/Devices/PATAChannel.cpp +++ b/Kernel/Devices/PATAChannel.cpp @@ -118,7 +118,7 @@ static Lock& s_lock() OwnPtr PATAChannel::create(ChannelType type, bool force_pio) { PCI::Address pci_address; - PCI::enumerate_all([&](const PCI::Address& address, PCI::ID id) { + PCI::enumerate([&](const PCI::Address& address, PCI::ID id) { if (PCI::get_class(address) == PCI_Mass_Storage_Class && PCI::get_subclass(address) == PCI_IDE_Controller_Subclass) { pci_address = address; klog() << "PATAChannel: PATA Controller found, ID " << id; diff --git a/Kernel/FileSystem/ProcFS.cpp b/Kernel/FileSystem/ProcFS.cpp index 2df01329b02..c487b785c2a 100644 --- a/Kernel/FileSystem/ProcFS.cpp +++ b/Kernel/FileSystem/ProcFS.cpp @@ -338,7 +338,7 @@ Optional procfs$pci(InodeIdentifier) { KBufferBuilder builder; JsonArraySerializer array { builder }; - PCI::enumerate_all([&array](PCI::Address address, PCI::ID id) { + PCI::enumerate([&array](PCI::Address address, PCI::ID id) { auto obj = array.add_object(); obj.add("seg", address.seg()); obj.add("bus", address.bus()); diff --git a/Kernel/PCI/Access.cpp b/Kernel/PCI/Access.cpp index 8cd8685cabf..999f6b6002d 100644 --- a/Kernel/PCI/Access.cpp +++ b/Kernel/PCI/Access.cpp @@ -98,9 +98,16 @@ void Access::enumerate_bus(int type, u8 bus, Function& callba enumerate_slot(type, bus, slot, callback); } -void enumerate_all(Function callback) +void Access::enumerate(Function& callback) const { - Access::the().enumerate_all(callback); + for (auto& physical_id : m_physical_ids) { + callback(physical_id.address(), physical_id.id()); + } +} + +void enumerate(Function callback) +{ + Access::the().enumerate(callback); } void raw_access(Address address, u32 field, size_t access_size, u32 value) diff --git a/Kernel/PCI/Access.h b/Kernel/PCI/Access.h index 9c4afb3c146..47375f8f4a0 100644 --- a/Kernel/PCI/Access.h +++ b/Kernel/PCI/Access.h @@ -27,6 +27,7 @@ #pragma once #include +#include #include namespace Kernel { @@ -38,7 +39,7 @@ public: MMIO, }; - virtual void enumerate_all(Function&) = 0; + void enumerate(Function&) const; void enumerate_bus(int type, u8 bus, Function&); void enumerate_functions(int type, u8 bus, u8 slot, u8 function, Function& callback); @@ -60,7 +61,10 @@ public: virtual u32 read32_field(Address address, u32 field) = 0; protected: + virtual void enumerate_hardware(Function) = 0; + Access(); + Vector m_physical_ids; }; } diff --git a/Kernel/PCI/Definitions.h b/Kernel/PCI/Definitions.h index 634320f2984..7c003f331fa 100644 --- a/Kernel/PCI/Definitions.h +++ b/Kernel/PCI/Definitions.h @@ -172,8 +172,24 @@ struct ChangeableAddress : public Address { } }; +class PhysicalID { +public: + PhysicalID(Address address, ID id) + : m_address(address) + , m_id(id) + { + } + + const ID& id() const { return m_id; } + const Address& address() const { return m_address; } + +private: + Address m_address; + ID m_id; +}; + ID get_id(PCI::Address); -void enumerate_all(Function callback); +void enumerate(Function callback); void enable_interrupt_line(Address); void disable_interrupt_line(Address); u8 get_interrupt_line(Address); diff --git a/Kernel/PCI/IOAccess.cpp b/Kernel/PCI/IOAccess.cpp index 76d58ec9a18..0c6b993bd21 100644 --- a/Kernel/PCI/IOAccess.cpp +++ b/Kernel/PCI/IOAccess.cpp @@ -39,6 +39,9 @@ void IOAccess::initialize() IOAccess::IOAccess() { klog() << "PCI: Using I/O instructions for PCI configuration space access"; + enumerate_hardware([&](const Address& address, ID id) { + m_physical_ids.append({ address, id }); + }); } u8 IOAccess::read8_field(Address address, u32 field) @@ -75,7 +78,7 @@ void IOAccess::write32_field(Address address, u32 field, u32 value) IO::out32(PCI_VALUE_PORT, value); } -void IOAccess::enumerate_all(Function& callback) +void IOAccess::enumerate_hardware(Function callback) { // Single PCI host controller. if ((read8_field(Address(), PCI_HEADER_TYPE) & 0x80) == 0) { diff --git a/Kernel/PCI/IOAccess.h b/Kernel/PCI/IOAccess.h index 665d2d6560b..57b66da2b64 100644 --- a/Kernel/PCI/IOAccess.h +++ b/Kernel/PCI/IOAccess.h @@ -38,7 +38,7 @@ protected: IOAccess(); private: - virtual void enumerate_all(Function&) override; + virtual void enumerate_hardware(Function) override; virtual const char* access_type() const override { return "IO-Access"; }; virtual uint32_t segment_count() const override { return 1; }; virtual void write8_field(Address address, u32, u8) override final; diff --git a/Kernel/PCI/Initializer.cpp b/Kernel/PCI/Initializer.cpp index 9cc60ba2324..6ae6575520f 100644 --- a/Kernel/PCI/Initializer.cpp +++ b/Kernel/PCI/Initializer.cpp @@ -59,7 +59,7 @@ void initialize() else IOAccess::initialize(); - enumerate_all([&](const Address& address, ID id) { + enumerate([&](const Address& address, ID id) { klog() << address << " " << id; E1000NetworkAdapter::detect(address); RTL8139NetworkAdapter::detect(address); diff --git a/Kernel/PCI/MMIOAccess.cpp b/Kernel/PCI/MMIOAccess.cpp index 6fff1988e74..dfc8f96de76 100644 --- a/Kernel/PCI/MMIOAccess.cpp +++ b/Kernel/PCI/MMIOAccess.cpp @@ -109,7 +109,9 @@ MMIOAccess::MMIOAccess(PhysicalAddress p_mcfg) } mcfg_region->unmap(); klog() << "PCI: MMIO segments - " << m_segments.size(); + InterruptDisabler disabler; + #ifdef PCI_DEBUG dbg() << "PCI: mapped address (" << String::format("%w", m_mapped_address.seg()) << ":" << String::format("%b", m_mapped_address.bus()) << ":" << String::format("%b", m_mapped_address.slot()) << "." << String::format("%b", m_mapped_address.function()) << ")"; #endif @@ -117,6 +119,10 @@ MMIOAccess::MMIOAccess(PhysicalAddress p_mcfg) #ifdef PCI_DEBUG dbg() << "PCI: Default mapped address (" << String::format("%w", m_mapped_address.seg()) << ":" << String::format("%b", m_mapped_address.bus()) << ":" << String::format("%b", m_mapped_address.slot()) << "." << String::format("%b", m_mapped_address.function()) << ")"; #endif + + enumerate_hardware([&](const Address& address, ID id) { + m_physical_ids.append({ address, id }); + }); } void MMIOAccess::map_device(Address address) @@ -204,7 +210,7 @@ void MMIOAccess::write32_field(Address address, u32 field, u32 value) *((u32*)(m_mmio_window_region->vaddr().get() + (field & 0xfff))) = value; } -void MMIOAccess::enumerate_all(Function& callback) +void MMIOAccess::enumerate_hardware(Function callback) { for (u16 seg = 0; seg < m_segments.size(); seg++) { #ifdef PCI_DEBUG diff --git a/Kernel/PCI/MMIOAccess.h b/Kernel/PCI/MMIOAccess.h index d61808042b4..984dcf31d1d 100644 --- a/Kernel/PCI/MMIOAccess.h +++ b/Kernel/PCI/MMIOAccess.h @@ -48,7 +48,7 @@ protected: private: virtual const char* access_type() const override { return "MMIO-Access"; }; virtual u32 segment_count() const override; - virtual void enumerate_all(Function&) override; + virtual void enumerate_hardware(Function) override; virtual void write8_field(Address address, u32, u8) override; virtual void write16_field(Address address, u32, u16) override; virtual void write32_field(Address address, u32, u32) override; diff --git a/Kernel/init.cpp b/Kernel/init.cpp index dae45742629..8a5b49046ec 100644 --- a/Kernel/init.cpp +++ b/Kernel/init.cpp @@ -170,7 +170,7 @@ void init_stage2() dbg() << "Text mode enabled"; } else { bool bxvga_found = false; - PCI::enumerate_all([&](const PCI::Address&, PCI::ID id) { + PCI::enumerate([&](const PCI::Address&, PCI::ID id) { if (id.vendor_id == 0x1234 && id.device_id == 0x1111) bxvga_found = true; });