From 0fdf8b6f68475afc8c67025c6f104da0fecebff0 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 16 Oct 2023 16:50:49 +0300 Subject: [PATCH] Kernel: Fix PCI bugs IO BarRegion used vaddr instead of the correct paddr. Add API for memory region iobase query. --- kernel/include/kernel/PCI.h | 5 +++-- kernel/kernel/PCI.cpp | 23 ++++++++++++++--------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/kernel/include/kernel/PCI.h b/kernel/include/kernel/PCI.h index 0ca2d4d2e8..64fd7167a0 100644 --- a/kernel/include/kernel/PCI.h +++ b/kernel/include/kernel/PCI.h @@ -29,8 +29,9 @@ namespace Kernel::PCI ~BarRegion(); BarType type() const { return m_type; } - vaddr_t vaddr() const { return m_vaddr; } - paddr_t paddr() const { return m_paddr; } + vaddr_t iobase() const { ASSERT(m_type == BarType::IO); return m_paddr; } + vaddr_t vaddr() const { ASSERT(m_type == BarType::MEM); return m_vaddr; } + paddr_t paddr() const { ASSERT(m_type == BarType::MEM); return m_paddr; } size_t size() const { return m_size; } void write8(off_t, uint8_t); diff --git a/kernel/kernel/PCI.cpp b/kernel/kernel/PCI.cpp index 5fa243f034..c7d75b05b4 100644 --- a/kernel/kernel/PCI.cpp +++ b/kernel/kernel/PCI.cpp @@ -77,7 +77,7 @@ namespace Kernel::PCI uint32_t temp = read_config_dword(bus, dev, func, offset & ~3); temp &= ~(0xFF << byte); temp |= (uint32_t)value << byte; - write_config_dword(bus, dev, func, offset, temp); + write_config_dword(bus, dev, func, offset & ~3, temp); } static uint16_t get_vendor_id(uint8_t bus, uint8_t dev, uint8_t func) @@ -252,8 +252,13 @@ namespace Kernel::PCI device.func() ); dprintln(" type: {}", region->type() == BarType::IO ? "IO" : "MEM"); - dprintln(" paddr {}", (void*)region->paddr()); - dprintln(" vaddr {}", (void*)region->vaddr()); + if (region->type() == BarType::IO) + dprintln(" iobase {8H}", region->iobase()); + else + { + dprintln(" paddr {}", (void*)region->paddr()); + dprintln(" vaddr {}", (void*)region->vaddr()); + } dprintln(" size {}", region->size()); #endif @@ -290,42 +295,42 @@ namespace Kernel::PCI void BarRegion::write8(off_t reg, uint8_t val) { if (m_type == BarType::IO) - return IO::outb(m_vaddr + reg, val); + return IO::outb(m_paddr + reg, val); MMIO::write8(m_vaddr + reg, val); } void BarRegion::write16(off_t reg, uint16_t val) { if (m_type == BarType::IO) - return IO::outw(m_vaddr + reg, val); + return IO::outw(m_paddr + reg, val); MMIO::write16(m_vaddr + reg, val); } void BarRegion::write32(off_t reg, uint32_t val) { if (m_type == BarType::IO) - return IO::outl(m_vaddr + reg, val); + return IO::outl(m_paddr + reg, val); MMIO::write32(m_vaddr + reg, val); } uint8_t BarRegion::read8(off_t reg) { if (m_type == BarType::IO) - return IO::inb(m_vaddr + reg); + return IO::inb(m_paddr + reg); return MMIO::read8(m_vaddr + reg); } uint16_t BarRegion::read16(off_t reg) { if (m_type == BarType::IO) - return IO::inw(m_vaddr + reg); + return IO::inw(m_paddr + reg); return MMIO::read16(m_vaddr + reg); } uint32_t BarRegion::read32(off_t reg) { if (m_type == BarType::IO) - return IO::inl(m_vaddr + reg); + return IO::inl(m_paddr + reg); return MMIO::read32(m_vaddr + reg); }