From 1596ee241ffcbdbabf77f93b5132a782ae730923 Mon Sep 17 00:00:00 2001 From: Liav A Date: Fri, 2 Sep 2022 13:41:48 +0300 Subject: Kernel/PCI: Move IO based HostBridge code to x86 arch-specific directory The simple PCI::HostBridge class implements access to the PCI configuration space by using x86 IO instructions. Therefore, it should be put in the Arch/x86/PCI directory so it can be easily omitted for non-x86 builds. --- Kernel/Arch/x86/PCI/Controller/HostBridge.cpp | 62 +++++++++++++++++++++++++++ Kernel/Arch/x86/PCI/Controller/HostBridge.h | 34 +++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 Kernel/Arch/x86/PCI/Controller/HostBridge.cpp create mode 100644 Kernel/Arch/x86/PCI/Controller/HostBridge.h (limited to 'Kernel/Arch') diff --git a/Kernel/Arch/x86/PCI/Controller/HostBridge.cpp b/Kernel/Arch/x86/PCI/Controller/HostBridge.cpp new file mode 100644 index 0000000000..81c7e34444 --- /dev/null +++ b/Kernel/Arch/x86/PCI/Controller/HostBridge.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2022, Liav A. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include + +namespace Kernel::PCI { + +NonnullOwnPtr HostBridge::must_create_with_io_access() +{ + PCI::Domain domain { 0, 0, 0xff }; + return adopt_own_if_nonnull(new (nothrow) HostBridge(domain)).release_nonnull(); +} + +HostBridge::HostBridge(PCI::Domain const& domain) + : HostController(domain) +{ +} + +static u32 io_address_for_pci_field(BusNumber bus, DeviceNumber device, FunctionNumber function, u8 field) +{ + return 0x80000000u | (bus.value() << 16u) | (device.value() << 11u) | (function.value() << 8u) | (field & 0xfc); +} + +void HostBridge::write8_field(BusNumber bus, DeviceNumber device, FunctionNumber function, u32 field, u8 value) +{ + IO::out32(PCI::address_port, io_address_for_pci_field(bus, device, function, field)); + IO::out8(PCI::value_port + (field & 3), value); +} +void HostBridge::write16_field(BusNumber bus, DeviceNumber device, FunctionNumber function, u32 field, u16 value) +{ + IO::out32(PCI::address_port, io_address_for_pci_field(bus, device, function, field)); + IO::out16(PCI::value_port + (field & 2), value); +} +void HostBridge::write32_field(BusNumber bus, DeviceNumber device, FunctionNumber function, u32 field, u32 value) +{ + IO::out32(PCI::address_port, io_address_for_pci_field(bus, device, function, field)); + IO::out32(PCI::value_port, value); +} + +u8 HostBridge::read8_field(BusNumber bus, DeviceNumber device, FunctionNumber function, u32 field) +{ + IO::out32(PCI::address_port, io_address_for_pci_field(bus, device, function, field)); + return IO::in8(PCI::value_port + (field & 3)); +} +u16 HostBridge::read16_field(BusNumber bus, DeviceNumber device, FunctionNumber function, u32 field) +{ + IO::out32(PCI::address_port, io_address_for_pci_field(bus, device, function, field)); + return IO::in16(PCI::value_port + (field & 2)); +} +u32 HostBridge::read32_field(BusNumber bus, DeviceNumber device, FunctionNumber function, u32 field) +{ + IO::out32(PCI::address_port, io_address_for_pci_field(bus, device, function, field)); + return IO::in32(PCI::value_port); +} + +} diff --git a/Kernel/Arch/x86/PCI/Controller/HostBridge.h b/Kernel/Arch/x86/PCI/Controller/HostBridge.h new file mode 100644 index 0000000000..32705ea565 --- /dev/null +++ b/Kernel/Arch/x86/PCI/Controller/HostBridge.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2022, Liav A. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace Kernel::PCI { + +class HostBridge : public HostController { +public: + static NonnullOwnPtr must_create_with_io_access(); + + virtual void write8_field(BusNumber, DeviceNumber, FunctionNumber, u32 field, u8 value) override; + virtual void write16_field(BusNumber, DeviceNumber, FunctionNumber, u32 field, u16 value) override; + virtual void write32_field(BusNumber, DeviceNumber, FunctionNumber, u32 field, u32 value) override; + + virtual u8 read8_field(BusNumber, DeviceNumber, FunctionNumber, u32 field) override; + virtual u16 read16_field(BusNumber, DeviceNumber, FunctionNumber, u32 field) override; + virtual u32 read32_field(BusNumber, DeviceNumber, FunctionNumber, u32 field) override; + +private: + explicit HostBridge(PCI::Domain const&); +}; + +} -- cgit v1.2.3