Skip to content

Commit aca317d

Browse files
supercomputer7awesomekling
authored andcommitted
Kernel: PCI MMIO no longer uses map_for_kernel()
PCI MMIO access is done by modifying the related PhysicalPage directly, then we request to remap the region to create the mapping.
1 parent 24f2596 commit aca317d

File tree

4 files changed

+19
-45
lines changed

4 files changed

+19
-45
lines changed

Kernel/PCI/Definitions.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ struct Address {
9494
{
9595
}
9696

97+
Address(const Address& address)
98+
: m_seg(address.seg())
99+
, m_bus(address.bus())
100+
, m_slot(address.slot())
101+
, m_function(address.function())
102+
{
103+
}
104+
97105
bool is_null() const { return !m_bus && !m_slot && !m_function; }
98106
operator bool() const { return !is_null(); }
99107

Kernel/PCI/IOAccess.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class PCI::IOAccess final : public PCI::Access {
3333
virtual void enumerate_all(Function<void(Address, ID)>&) override final;
3434

3535
virtual String get_access_type() override final { return "IO-Access"; };
36-
36+
virtual uint32_t get_segments_count() { return 1; };
3737
protected:
3838
IOAccess();
3939

@@ -44,8 +44,7 @@ class PCI::IOAccess final : public PCI::Access {
4444
virtual void write8_field(Address address, u32, u8) override final;
4545
virtual void write16_field(Address address, u32, u16) override final;
4646
virtual void write32_field(Address address, u32, u32) override final;
47-
48-
virtual uint32_t get_segments_count() { return 1; };
47+
4948
virtual uint8_t get_segment_start_bus(u32) { return 0x0; };
5049
virtual uint8_t get_segment_end_bus(u32) { return 0xFF; };
5150
};

Kernel/PCI/MMIOAccess.cpp

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,23 @@ PCI::MMIOAccess::MMIOAccess(ACPI_RAW::MCFG& raw_mcfg)
5858
, m_mapped_address(ChangeableAddress(0xFFFF, 0xFF, 0xFF, 0xFF))
5959
{
6060
kprintf("PCI: Using MMIO Mechanism for PCI Configuartion Space Access\n");
61-
m_mmio_window = *AnonymousVMObject::create_with_size(PAGE_ROUND_UP(PCI_MMIO_CONFIG_SPACE_SIZE));
62-
m_mmio_window_region = MM.allocate_kernel_region_with_vmobject(*m_mmio_window, m_mmio_window->size(), "PCI MMIO", Region::Access::Read | Region::Access::Write);
61+
m_mmio_window_region = MM.allocate_kernel_region(PAGE_ROUND_UP(PCI_MMIO_CONFIG_SPACE_SIZE), "PCI MMIO", Region::Access::Read | Region::Access::Write);
6362

64-
auto checkup_region = MM.allocate_kernel_region((PAGE_SIZE * 2), "PCI MCFG Checkup", Region::Access::Read | Region::Access::Write);
63+
auto checkup_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((u32)&raw_mcfg)),(PAGE_SIZE * 2), "PCI MCFG Checkup", Region::Access::Read | Region::Access::Write);
6564
#ifdef PCI_DEBUG
6665
dbgprintf("PCI: Checking MCFG Table length to choose the correct mapping size\n");
6766
#endif
68-
mmap_region(*checkup_region, PhysicalAddress((u32)&raw_mcfg & 0xfffff000));
69-
ACPI_RAW::SDTHeader* sdt = (ACPI_RAW::SDTHeader*)(checkup_region->vaddr().get() + ((u32)&raw_mcfg & 0xfff));
67+
68+
ACPI_RAW::SDTHeader* sdt = (ACPI_RAW::SDTHeader*)checkup_region->vaddr().offset(offset_in_page((u32)&raw_mcfg)).as_ptr();
7069
u32 length = sdt->length;
7170
u8 revision = sdt->revision;
7271

7372
kprintf("PCI: MCFG, length - %u, revision %d\n", length, revision);
7473
checkup_region->unmap();
7574

76-
auto mcfg_region = MM.allocate_kernel_region(PAGE_ROUND_UP(length) + PAGE_SIZE, "PCI Parsing MCFG", Region::Access::Read | Region::Access::Write);
77-
mmap_region(*mcfg_region, PhysicalAddress((u32)&raw_mcfg & 0xfffff000));
75+
auto mcfg_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((u32)&raw_mcfg)), PAGE_ROUND_UP(length) + PAGE_SIZE, "PCI Parsing MCFG", Region::Access::Read | Region::Access::Write);
7876

79-
ACPI_RAW::MCFG& mcfg = *((ACPI_RAW::MCFG*)(mcfg_region->vaddr().get() + ((u32)&raw_mcfg & 0xfff)));
77+
auto& mcfg = *(ACPI_RAW::MCFG*)mcfg_region->vaddr().offset(offset_in_page((u32)&raw_mcfg)).as_ptr();
8078
#ifdef PCI_DEBUG
8179
dbgprintf("PCI: Checking MCFG @ V 0x%x, P 0x%x\n", &mcfg, &raw_mcfg);
8280
#endif
@@ -116,7 +114,8 @@ void PCI::MMIOAccess::map_device(Address address)
116114
#ifdef PCI_DEBUG
117115
dbgprintf("PCI: Mapping device @ pci (%w:%b:%b.%b), V 0x%x, P 0x%x\n", address.seg(), address.bus(), address.slot(), address.function(), m_mmio_window_region->vaddr().get(), device_physical_mmio_space.get());
118116
#endif
119-
MM.map_for_kernel(m_mmio_window_region->vaddr(), device_physical_mmio_space);
117+
m_mmio_window_region->vmobject().physical_pages()[0] = PhysicalPage::create(device_physical_mmio_space,false,false);
118+
m_mmio_window_region->remap();
120119
m_mapped_address = address;
121120
}
122121

@@ -205,33 +204,6 @@ void PCI::MMIOAccess::enumerate_all(Function<void(Address, ID)>& callback)
205204
}
206205
}
207206

208-
void PCI::MMIOAccess::mmap(VirtualAddress vaddr, PhysicalAddress paddr, u32 length)
209-
{
210-
unsigned i = 0;
211-
while (length >= PAGE_SIZE) {
212-
MM.map_for_kernel(VirtualAddress(vaddr.offset(i * PAGE_SIZE).get()), PhysicalAddress(paddr.offset(i * PAGE_SIZE).get()));
213-
#ifdef PCI_DEBUG
214-
dbgprintf("PCI: map - V 0x%x -> P 0x%x\n", vaddr.offset(i * PAGE_SIZE).get(), paddr.offset(i * PAGE_SIZE).get());
215-
#endif
216-
length -= PAGE_SIZE;
217-
i++;
218-
}
219-
if (length > 0) {
220-
MM.map_for_kernel(vaddr.offset(i * PAGE_SIZE), paddr.offset(i * PAGE_SIZE), true);
221-
}
222-
#ifdef PCI_DEBUG
223-
dbgprintf("PCI: Finished mapping\n");
224-
#endif
225-
}
226-
227-
void PCI::MMIOAccess::mmap_region(Region& region, PhysicalAddress paddr)
228-
{
229-
#ifdef PCI_DEBUG
230-
dbgprintf("PCI: Mapping region, size - %u\n", region.size());
231-
#endif
232-
mmap(region.vaddr(), paddr, region.size());
233-
}
234-
235207
PCI::MMIOSegment::MMIOSegment(PhysicalAddress segment_base_addr, u8 start_bus, u8 end_bus)
236208
: m_base_addr(segment_base_addr)
237209
, m_start_bus(start_bus)

Kernel/PCI/MMIOAccess.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class PCI::MMIOAccess final : public PCI::Access {
4141
virtual void enumerate_all(Function<void(Address, ID)>&) override final;
4242

4343
virtual String get_access_type() override final { return "MMIO-Access"; };
44-
44+
virtual u32 get_segments_count();
4545
protected:
4646
explicit MMIOAccess(ACPI_RAW::MCFG&);
4747

@@ -54,16 +54,11 @@ class PCI::MMIOAccess final : public PCI::Access {
5454
virtual void write32_field(Address address, u32, u32) override final;
5555

5656
void map_device(Address address);
57-
void mmap(VirtualAddress preferred_vaddr, PhysicalAddress paddr, u32);
58-
void mmap_region(Region& region, PhysicalAddress paddr);
59-
60-
virtual u32 get_segments_count();
6157
virtual u8 get_segment_start_bus(u32);
6258
virtual u8 get_segment_end_bus(u32);
6359

6460
ACPI_RAW::MCFG& m_mcfg;
6561
HashMap<u16, MMIOSegment*>& m_segments;
66-
RefPtr<VMObject> m_mmio_window;
6762
OwnPtr<Region> m_mmio_window_region;
6863
PCI::ChangeableAddress m_mapped_address;
6964
};

0 commit comments

Comments
 (0)