mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 09:17:45 +00:00
Kernel/VirtIO: Introduce the concept of transport options
The VirtIO specification defines many types of devices with different purposes, and it also defines 3 possible transport mediums where devices could be connected to the host machine. We only care about the PCIe transport, but this commit puts the actual foundations for supporting the lean MMIO transport too in the future. To ensure things are kept abstracted but still functional, the VirtIO transport code is responsible for what is deemed as related to an actual transport type - allocation of interrupt handlers and tinkering with low level transport-related registers, etc.
This commit is contained in:
parent
68c3f9aa5a
commit
d61c23569e
24 changed files with 732 additions and 429 deletions
52
Kernel/Bus/VirtIO/Transport/PCIe/Detect.cpp
Normal file
52
Kernel/Bus/VirtIO/Transport/PCIe/Detect.cpp
Normal file
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright (c) 2021, the SerenityOS developers.
|
||||
* Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Try.h>
|
||||
#include <Kernel/Boot/CommandLine.h>
|
||||
#include <Kernel/Bus/PCI/API.h>
|
||||
#include <Kernel/Bus/PCI/IDs.h>
|
||||
#include <Kernel/Bus/VirtIO/Console.h>
|
||||
#include <Kernel/Bus/VirtIO/Device.h>
|
||||
#include <Kernel/Bus/VirtIO/RNG.h>
|
||||
#include <Kernel/Bus/VirtIO/Transport/PCIe/Detect.h>
|
||||
#include <Kernel/Sections.h>
|
||||
|
||||
namespace Kernel::VirtIO {
|
||||
|
||||
UNMAP_AFTER_INIT void detect_pci_instances()
|
||||
{
|
||||
if (kernel_command_line().disable_virtio())
|
||||
return;
|
||||
MUST(PCI::enumerate([&](PCI::DeviceIdentifier const& device_identifier) {
|
||||
if (device_identifier.hardware_id().is_null())
|
||||
return;
|
||||
// TODO: We should also be checking that the device_id is in between 0x1000 - 0x107F inclusive
|
||||
if (device_identifier.hardware_id().vendor_id != PCI::VendorID::VirtIO)
|
||||
return;
|
||||
switch (device_identifier.hardware_id().device_id) {
|
||||
case PCI::DeviceID::VirtIOConsole: {
|
||||
auto& console = Console::must_create_for_pci_instance(device_identifier).leak_ref();
|
||||
MUST(console.initialize_virtio_resources());
|
||||
break;
|
||||
}
|
||||
case PCI::DeviceID::VirtIOEntropy: {
|
||||
auto& rng = RNG::must_create_for_pci_instance(device_identifier).leak_ref();
|
||||
MUST(rng.initialize_virtio_resources());
|
||||
break;
|
||||
}
|
||||
case PCI::DeviceID::VirtIOGPU: {
|
||||
// This should have been initialized by the graphics subsystem
|
||||
break;
|
||||
}
|
||||
default:
|
||||
dbgln_if(VIRTIO_DEBUG, "VirtIO: Unknown VirtIO device with ID: {}", device_identifier.hardware_id().device_id);
|
||||
break;
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
}
|
13
Kernel/Bus/VirtIO/Transport/PCIe/Detect.h
Normal file
13
Kernel/Bus/VirtIO/Transport/PCIe/Detect.h
Normal file
|
@ -0,0 +1,13 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace Kernel::VirtIO {
|
||||
|
||||
void detect_pci_instances();
|
||||
|
||||
}
|
27
Kernel/Bus/VirtIO/Transport/PCIe/InterruptHandler.cpp
Normal file
27
Kernel/Bus/VirtIO/Transport/PCIe/InterruptHandler.cpp
Normal file
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Copyright (c) 2021-2022, Liav A. <liavalb@hotmail.co.il>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <Kernel/Bus/VirtIO/Transport/PCIe/InterruptHandler.h>
|
||||
|
||||
namespace Kernel::VirtIO {
|
||||
|
||||
ErrorOr<NonnullOwnPtr<PCIeTransportInterruptHandler>> PCIeTransportInterruptHandler::create(PCIeTransportLink& transport_link, VirtIO::Device& parent_device, u8 irq)
|
||||
{
|
||||
return TRY(adopt_nonnull_own_or_enomem(new (nothrow) PCIeTransportInterruptHandler(transport_link, parent_device, irq)));
|
||||
}
|
||||
|
||||
PCIeTransportInterruptHandler::PCIeTransportInterruptHandler(PCIeTransportLink& transport_link, VirtIO::Device& parent_device, u8 irq)
|
||||
: TransportInterruptHandler(parent_device)
|
||||
, PCI::IRQHandler(transport_link, irq)
|
||||
{
|
||||
}
|
||||
|
||||
bool PCIeTransportInterruptHandler::handle_irq(RegisterState const&)
|
||||
{
|
||||
return notify_parent_device_on_interrupt();
|
||||
}
|
||||
|
||||
}
|
31
Kernel/Bus/VirtIO/Transport/PCIe/InterruptHandler.h
Normal file
31
Kernel/Bus/VirtIO/Transport/PCIe/InterruptHandler.h
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Kernel/Bus/VirtIO/Device.h>
|
||||
#include <Kernel/Bus/VirtIO/Transport/InterruptHandler.h>
|
||||
#include <Kernel/Bus/VirtIO/Transport/PCIe/TransportLink.h>
|
||||
#include <Kernel/Interrupts/PCIIRQHandler.h>
|
||||
|
||||
namespace Kernel::VirtIO {
|
||||
|
||||
class PCIeTransportInterruptHandler final
|
||||
: public TransportInterruptHandler
|
||||
, public PCI::IRQHandler {
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<PCIeTransportInterruptHandler>> create(PCIeTransportLink&, VirtIO::Device&, u8 irq);
|
||||
virtual ~PCIeTransportInterruptHandler() override = default;
|
||||
|
||||
virtual StringView purpose() const override { return "VirtIO PCI IRQ Handler"sv; }
|
||||
|
||||
private:
|
||||
PCIeTransportInterruptHandler(PCIeTransportLink&, VirtIO::Device&, u8 irq);
|
||||
|
||||
//^ IRQHandler
|
||||
virtual bool handle_irq(RegisterState const&) override;
|
||||
};
|
||||
}
|
161
Kernel/Bus/VirtIO/Transport/PCIe/TransportLink.cpp
Normal file
161
Kernel/Bus/VirtIO/Transport/PCIe/TransportLink.cpp
Normal file
|
@ -0,0 +1,161 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <Kernel/Bus/PCI/API.h>
|
||||
#include <Kernel/Bus/PCI/IDs.h>
|
||||
#include <Kernel/Bus/VirtIO/Transport/PCIe/InterruptHandler.h>
|
||||
#include <Kernel/Bus/VirtIO/Transport/PCIe/TransportLink.h>
|
||||
|
||||
namespace Kernel::VirtIO {
|
||||
|
||||
ErrorOr<NonnullOwnPtr<TransportEntity>> PCIeTransportLink::create(PCI::DeviceIdentifier const& pci_identifier)
|
||||
{
|
||||
return TRY(adopt_nonnull_own_or_enomem(new (nothrow) PCIeTransportLink(pci_identifier)));
|
||||
}
|
||||
|
||||
StringView PCIeTransportLink::determine_device_class_name() const
|
||||
{
|
||||
if (device_identifier().revision_id().value() == 0) {
|
||||
// Note: If the device is a legacy (or transitional) device, therefore,
|
||||
// probe the subsystem ID in the PCI header and figure out the
|
||||
auto subsystem_device_id = device_identifier().subsystem_id().value();
|
||||
switch (subsystem_device_id) {
|
||||
case 1:
|
||||
return "VirtIONetAdapter"sv;
|
||||
case 2:
|
||||
return "VirtIOBlockDevice"sv;
|
||||
case 3:
|
||||
return "VirtIOConsole"sv;
|
||||
case 4:
|
||||
return "VirtIORNG"sv;
|
||||
default:
|
||||
dbgln("VirtIO: Unknown subsystem_device_id {}", subsystem_device_id);
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
auto id = device_identifier().hardware_id();
|
||||
VERIFY(id.vendor_id == PCI::VendorID::VirtIO);
|
||||
switch (id.device_id) {
|
||||
case PCI::DeviceID::VirtIONetAdapter:
|
||||
return "VirtIONetAdapter"sv;
|
||||
case PCI::DeviceID::VirtIOBlockDevice:
|
||||
return "VirtIOBlockDevice"sv;
|
||||
case PCI::DeviceID::VirtIOConsole:
|
||||
return "VirtIOConsole"sv;
|
||||
case PCI::DeviceID::VirtIOEntropy:
|
||||
return "VirtIORNG"sv;
|
||||
case PCI::DeviceID::VirtIOGPU:
|
||||
return "VirtIOGPU"sv;
|
||||
default:
|
||||
dbgln("VirtIO: Unknown device_id {}", id.vendor_id);
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
ErrorOr<void> PCIeTransportLink::create_interrupt_handler(VirtIO::Device& parent_device)
|
||||
{
|
||||
TRY(reserve_irqs(1, false));
|
||||
auto irq = MUST(allocate_irq(0));
|
||||
m_irq_handler = TRY(PCIeTransportInterruptHandler::create(*this, parent_device, irq));
|
||||
return {};
|
||||
}
|
||||
|
||||
PCIeTransportLink::PCIeTransportLink(PCI::DeviceIdentifier const& pci_identifier)
|
||||
: PCI::Device(pci_identifier)
|
||||
{
|
||||
dbgln("{}: Found @ {}", determine_device_class_name(), device_identifier().address());
|
||||
}
|
||||
|
||||
ErrorOr<void> PCIeTransportLink::locate_configurations_and_resources(Badge<VirtIO::Device>, VirtIO::Device& parent_device)
|
||||
{
|
||||
TRY(create_interrupt_handler(parent_device));
|
||||
PCI::enable_bus_mastering(device_identifier());
|
||||
|
||||
auto capabilities = device_identifier().capabilities();
|
||||
for (auto& capability : capabilities) {
|
||||
if (capability.id().value() == PCI::Capabilities::ID::VendorSpecific) {
|
||||
// We have a virtio_pci_cap
|
||||
Configuration config {};
|
||||
auto raw_config_type = capability.read8(0x3);
|
||||
// NOTE: The VirtIO specification allows iteration of configurations
|
||||
// through a special PCI capbility structure with the VIRTIO_PCI_CAP_PCI_CFG tag:
|
||||
//
|
||||
// "Each structure can be mapped by a Base Address register (BAR) belonging to the function, or accessed via
|
||||
// the special VIRTIO_PCI_CAP_PCI_CFG field in the PCI configuration space"
|
||||
//
|
||||
// "The VIRTIO_PCI_CAP_PCI_CFG capability creates an alternative (and likely suboptimal) access method
|
||||
// to the common configuration, notification, ISR and device-specific configuration regions."
|
||||
//
|
||||
// Also, it is *very* likely to see this PCI capability as the first vendor-specific capbility of a certain PCI function,
|
||||
// but this is not guaranteed by the VirtIO specification.
|
||||
// Therefore, ignore this type of configuration as this is not needed by our implementation currently.
|
||||
if (raw_config_type == static_cast<u8>(ConfigurationType::PCICapabilitiesAccess))
|
||||
continue;
|
||||
if (raw_config_type < static_cast<u8>(ConfigurationType::Common) || raw_config_type > static_cast<u8>(ConfigurationType::PCICapabilitiesAccess)) {
|
||||
dbgln("{}: Unknown capability configuration type: {}", device_name(), raw_config_type);
|
||||
return Error::from_errno(ENXIO);
|
||||
}
|
||||
config.cfg_type = static_cast<ConfigurationType>(raw_config_type);
|
||||
auto cap_length = capability.read8(0x2);
|
||||
if (cap_length < 0x10) {
|
||||
dbgln("{}: Unexpected capability size: {}", device_name(), cap_length);
|
||||
break;
|
||||
}
|
||||
config.resource_index = capability.read8(0x4);
|
||||
if (config.resource_index > 0x5) {
|
||||
dbgln("{}: Unexpected capability BAR value: {}", device_name(), config.resource_index);
|
||||
break;
|
||||
}
|
||||
config.offset = capability.read32(0x8);
|
||||
config.length = capability.read32(0xc);
|
||||
// NOTE: Configuration length of zero is an invalid configuration, or at the very least a configuration
|
||||
// type we don't know how to handle correctly...
|
||||
// The VIRTIO_PCI_CAP_PCI_CFG configuration structure has length of 0
|
||||
// but because we ignore that type and all other types should have a length
|
||||
// greater than 0, we should ignore any other configuration in case this condition is not met.
|
||||
if (config.length == 0) {
|
||||
dbgln("{}: Found configuration {}, with invalid length of 0", device_name(), (u32)config.cfg_type);
|
||||
continue;
|
||||
}
|
||||
dbgln_if(VIRTIO_DEBUG, "{}: Found configuration {}, resource: {}, offset: {}, length: {}", device_name(), (u32)config.cfg_type, config.resource_index, config.offset, config.length);
|
||||
if (config.cfg_type == ConfigurationType::Common)
|
||||
m_use_mmio = true;
|
||||
else if (config.cfg_type == ConfigurationType::Notify)
|
||||
m_notify_multiplier = capability.read32(0x10);
|
||||
|
||||
m_configs.append(config);
|
||||
}
|
||||
}
|
||||
|
||||
if (m_use_mmio) {
|
||||
for (auto& cfg : m_configs) {
|
||||
auto mapping_io_window = TRY(IOWindow::create_for_pci_device_bar(device_identifier(), static_cast<PCI::HeaderType0BaseRegister>(cfg.resource_index)));
|
||||
m_register_bases[cfg.resource_index] = move(mapping_io_window);
|
||||
}
|
||||
m_common_cfg = TRY(get_config(ConfigurationType::Common, 0));
|
||||
m_notify_cfg = TRY(get_config(ConfigurationType::Notify, 0));
|
||||
m_isr_cfg = TRY(get_config(ConfigurationType::ISR, 0));
|
||||
} else {
|
||||
auto mapping_io_window = TRY(IOWindow::create_for_pci_device_bar(device_identifier(), PCI::HeaderType0BaseRegister::BAR0));
|
||||
m_register_bases[0] = move(mapping_io_window);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
void PCIeTransportLink::disable_interrupts(Badge<VirtIO::Device>)
|
||||
{
|
||||
disable_pin_based_interrupts();
|
||||
m_irq_handler->disable_irq();
|
||||
}
|
||||
|
||||
void PCIeTransportLink::enable_interrupts(Badge<VirtIO::Device>)
|
||||
{
|
||||
m_irq_handler->enable_irq();
|
||||
enable_pin_based_interrupts();
|
||||
}
|
||||
|
||||
}
|
40
Kernel/Bus/VirtIO/Transport/PCIe/TransportLink.h
Normal file
40
Kernel/Bus/VirtIO/Transport/PCIe/TransportLink.h
Normal file
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* Copyright (c) 2021, the SerenityOS developers.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Types.h>
|
||||
#include <Kernel/Bus/PCI/Device.h>
|
||||
#include <Kernel/Bus/VirtIO/Transport/Entity.h>
|
||||
#include <Kernel/Interrupts/PCIIRQHandler.h>
|
||||
|
||||
namespace Kernel::VirtIO {
|
||||
|
||||
class PCIeTransportLink final
|
||||
: public TransportEntity
|
||||
, public PCI::Device {
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<TransportEntity>> create(PCI::DeviceIdentifier const& pci_identifier);
|
||||
|
||||
virtual StringView device_name() const override { return "VirtIOTransportLink"sv; }
|
||||
virtual StringView determine_device_class_name() const override;
|
||||
|
||||
private:
|
||||
explicit PCIeTransportLink(PCI::DeviceIdentifier const& pci_identifier);
|
||||
|
||||
// ^TransportEntity
|
||||
virtual ErrorOr<void> locate_configurations_and_resources(Badge<VirtIO::Device>, VirtIO::Device&) override;
|
||||
virtual void disable_interrupts(Badge<VirtIO::Device>) override;
|
||||
virtual void enable_interrupts(Badge<VirtIO::Device>) override;
|
||||
|
||||
ErrorOr<void> create_interrupt_handler(VirtIO::Device&);
|
||||
|
||||
// FIXME: There could be multiple IRQ (MSI-X) handlers for a VirtIO device.
|
||||
// Find a way to use all of them.
|
||||
OwnPtr<PCI::IRQHandler> m_irq_handler;
|
||||
};
|
||||
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue