Kernel: Simplify PCI::MMIOAccess segment storage

Instead of nesting a bunch of heap allocations, just store them in
a simple HashMap<u16, MMIOSegment>.

Also fix a bunch of double hash lookups like this:

    ASSERT(map.contains(key));
    auto thing = map.get(key).value();

They now look like this instead:

    auto thing = map.get(key);
    ASSERT(thing.has_value());
This commit is contained in:
Andreas Kling 2020-04-08 17:23:20 +02:00
parent 0de368e36c
commit c8b309a3b5
Notes: sideshowbarker 2024-07-19 07:48:02 +09:00
2 changed files with 11 additions and 10 deletions

View File

@ -55,14 +55,16 @@ uint32_t MMIOAccess::segment_count() const
uint8_t MMIOAccess::segment_start_bus(u32 seg) const
{
ASSERT(m_segments.contains(seg));
return m_segments.get(seg).value()->get_start_bus();
auto segment = m_segments.get(seg);
ASSERT(segment.has_value());
return segment.value().get_start_bus();
}
uint8_t MMIOAccess::segment_end_bus(u32 seg) const
{
ASSERT(m_segments.contains(seg));
return m_segments.get(seg).value()->get_end_bus();
auto segment = m_segments.get(seg);
ASSERT(segment.has_value());
return segment.value().get_end_bus();
}
void MMIOAccess::initialize(PhysicalAddress mcfg)
@ -73,7 +75,6 @@ void MMIOAccess::initialize(PhysicalAddress mcfg)
MMIOAccess::MMIOAccess(PhysicalAddress p_mcfg)
: m_mcfg(p_mcfg)
, m_segments(*new HashMap<u16, MMIOSegment*>())
, m_mapped_address(ChangeableAddress(0xFFFF, 0xFF, 0xFF, 0xFF))
{
klog() << "PCI: Using MMIO Mechanism for PCI Configuartion Space Access";
@ -103,7 +104,7 @@ MMIOAccess::MMIOAccess(PhysicalAddress p_mcfg)
u8 end_bus = mcfg.descriptors[index].end_pci_bus;
u32 lower_addr = mcfg.descriptors[index].base_addr;
m_segments.set(index, new MMIOSegment(PhysicalAddress(lower_addr), start_bus, end_bus));
m_segments.set(index, { PhysicalAddress(lower_addr), start_bus, end_bus });
klog() << "PCI: New PCI segment @ " << PhysicalAddress(lower_addr) << ", PCI buses (" << start_bus << "-" << end_bus << ")";
}
mcfg_region->unmap();
@ -124,11 +125,11 @@ void MMIOAccess::map_device(Address address)
return;
// FIXME: Map and put some lock!
ASSERT_INTERRUPTS_DISABLED();
ASSERT(m_segments.contains(address.seg()));
auto segment = m_segments.get(address.seg());
PhysicalAddress segment_lower_addr = segment.value()->get_paddr();
ASSERT(segment.has_value());
PhysicalAddress segment_lower_addr = segment.value().get_paddr();
PhysicalAddress device_physical_mmio_space = segment_lower_addr.offset(
PCI_MMIO_CONFIG_SPACE_SIZE * address.function() + (PCI_MMIO_CONFIG_SPACE_SIZE * PCI_MAX_FUNCTIONS_PER_DEVICE) * address.slot() + (PCI_MMIO_CONFIG_SPACE_SIZE * PCI_MAX_FUNCTIONS_PER_DEVICE * PCI_MAX_DEVICES_PER_BUS) * (address.bus() - segment.value()->get_start_bus()));
PCI_MMIO_CONFIG_SPACE_SIZE * address.function() + (PCI_MMIO_CONFIG_SPACE_SIZE * PCI_MAX_FUNCTIONS_PER_DEVICE) * address.slot() + (PCI_MMIO_CONFIG_SPACE_SIZE * PCI_MAX_FUNCTIONS_PER_DEVICE * PCI_MAX_DEVICES_PER_BUS) * (address.bus() - segment.value().get_start_bus()));
#ifdef PCI_DEBUG
dbg() << "PCI: Mapping device @ pci (" << String::format("%w", address.seg()) << ":" << String::format("%b", address.bus()) << ":" << String::format("%b", address.slot()) << "." << String::format("%b", address.function()) << ")"

View File

@ -63,7 +63,7 @@ private:
virtual u8 segment_end_bus(u32) const override;
PhysicalAddress m_mcfg;
HashMap<u16, MMIOSegment*>& m_segments;
HashMap<u16, MMIOSegment> m_segments;
OwnPtr<Region> m_mmio_window_region;
ChangeableAddress m_mapped_address;
};