1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 15:28:11 +00:00

Kernel/PCI: Hold a reference to DeviceIdentifier in the Device class

There are now 2 separate classes for almost the same object type:
- EnumerableDeviceIdentifier, which is used in the enumeration code for
  all PCI host controller classes. This is allowed to be moved and
  copied, as it doesn't support ref-counting.
- DeviceIdentifier, which inherits from EnumerableDeviceIdentifier. This
  class uses ref-counting, and is not allowed to be copied. It has a
  spinlock member in its structure to allow safely executing complicated
  IO sequences on a PCI device and its space configuration.
  There's a static method that allows a quick conversion from
  EnumerableDeviceIdentifier to DeviceIdentifier while creating a
  NonnullRefPtr out of it.

The reason for doing this is for the sake of integrity and reliablity of
the system in 2 places:
- Ensure that "complicated" tasks that rely on manipulating PCI device
  registers are done in a safe manner. For example, determining a PCI
  BAR space size requires multiple read and writes to the same register,
  and if another CPU tries to do something else with our selected
  register, then the result will be a catastrophe.
- Allow the PCI API to have a united form around a shared object which
  actually holds much more data than the PCI::Address structure. This is
  fundamental if we want to do certain types of optimizations, and be
  able to support more features of the PCI bus in the foreseeable
  future.

This patch already has several implications:
- All PCI::Device(s) hold a reference to a DeviceIdentifier structure
  being given originally from the PCI::Access singleton. This means that
  all instances of DeviceIdentifier structures are located in one place,
  and all references are pointing to that location. This ensures that
  locking the operation spinlock will take effect in all the appropriate
  places.
- We no longer support adding PCI host controllers and then immediately
  allow for enumerating it with a lambda function. It was found that
  this method is extremely broken and too much complicated to work
  reliably with the new paradigm being introduced in this patch. This
  means that for Volume Management Devices (Intel VMD devices), we
  simply first enumerate the PCI bus for such devices in the storage
  code, and if we find a device, we attach it in the PCI::Access method
  which will scan for devices behind that bridge and will add new
  DeviceIdentifier(s) objects to its internal Vector. Afterwards, we
  just continue as usual with scanning for actual storage controllers,
  so we will find a corresponding NVMe controllers if there were any
  behind that VMD bridge.
This commit is contained in:
Liav A 2022-02-10 18:33:13 +02:00 committed by Jelle Raaijmakers
parent 3226ce3d83
commit 1f9d3a3523
39 changed files with 493 additions and 390 deletions

View file

@ -123,38 +123,26 @@ UNMAP_AFTER_INIT bool Access::initialize_for_one_pci_domain()
}
#endif
ErrorOr<void> Access::add_host_controller_and_enumerate_attached_devices(NonnullOwnPtr<HostController> controller, Function<void(DeviceIdentifier const&)> callback)
ErrorOr<void> Access::add_host_controller_and_scan_for_devices(NonnullOwnPtr<HostController> controller)
{
// Note: We hold the spinlocks for a moment just to ensure we append the
// device identifiers safely. Afterwards, enumeration goes lockless to allow
// IRQs to be fired if necessary.
Vector<DeviceIdentifier> device_identifiers_behind_host_controller;
{
SpinlockLocker locker(m_access_lock);
SpinlockLocker scan_locker(m_scan_lock);
auto domain_number = controller->domain_number();
SpinlockLocker locker(m_access_lock);
SpinlockLocker scan_locker(m_scan_lock);
auto domain_number = controller->domain_number();
VERIFY(!m_host_controllers.contains(domain_number));
// Note: We need to register the new controller as soon as possible, and
// definitely before enumerating devices behind that.
m_host_controllers.set(domain_number, move(controller));
ErrorOr<void> expansion_result;
m_host_controllers.get(domain_number).value()->enumerate_attached_devices([&](DeviceIdentifier const& device_identifier) -> IterationDecision {
m_device_identifiers.append(device_identifier);
auto result = device_identifiers_behind_host_controller.try_append(device_identifier);
if (result.is_error()) {
expansion_result = result;
return IterationDecision::Break;
}
return IterationDecision::Continue;
});
if (expansion_result.is_error())
return expansion_result;
}
for (auto const& device_identifier : device_identifiers_behind_host_controller) {
callback(device_identifier);
}
VERIFY(!m_host_controllers.contains(domain_number));
// Note: We need to register the new controller as soon as possible, and
// definitely before enumerating devices behind that.
m_host_controllers.set(domain_number, move(controller));
ErrorOr<void> error_or_void {};
m_host_controllers.get(domain_number).value()->enumerate_attached_devices([&](EnumerableDeviceIdentifier const& device_identifier) -> IterationDecision {
auto device_identifier_or_error = DeviceIdentifier::from_enumerable_identifier(device_identifier);
if (device_identifier_or_error.is_error()) {
error_or_void = device_identifier_or_error.error();
return IterationDecision::Break;
}
m_device_identifiers.append(device_identifier_or_error.release_value());
return IterationDecision::Continue;
});
return {};
}
@ -174,19 +162,29 @@ UNMAP_AFTER_INIT void Access::rescan_hardware()
SpinlockLocker locker(m_access_lock);
SpinlockLocker scan_locker(m_scan_lock);
VERIFY(m_device_identifiers.is_empty());
ErrorOr<void> error_or_void {};
for (auto it = m_host_controllers.begin(); it != m_host_controllers.end(); ++it) {
(*it).value->enumerate_attached_devices([this](DeviceIdentifier device_identifier) -> IterationDecision {
m_device_identifiers.append(device_identifier);
(*it).value->enumerate_attached_devices([this, &error_or_void](EnumerableDeviceIdentifier device_identifier) -> IterationDecision {
auto device_identifier_or_error = DeviceIdentifier::from_enumerable_identifier(device_identifier);
if (device_identifier_or_error.is_error()) {
error_or_void = device_identifier_or_error.error();
return IterationDecision::Break;
}
m_device_identifiers.append(device_identifier_or_error.release_value());
return IterationDecision::Continue;
});
}
if (error_or_void.is_error()) {
dmesgln("Failed during PCI Access::rescan_hardware due to {}", error_or_void.error());
VERIFY_NOT_REACHED();
}
}
ErrorOr<void> Access::fast_enumerate(Function<void(DeviceIdentifier const&)>& callback) const
{
// Note: We hold the m_access_lock for a brief moment just to ensure we get
// a complete Vector in case someone wants to mutate it.
Vector<DeviceIdentifier> device_identifiers;
NonnullRefPtrVector<DeviceIdentifier> device_identifiers;
{
SpinlockLocker locker(m_access_lock);
VERIFY(!m_device_identifiers.is_empty());
@ -198,9 +196,9 @@ ErrorOr<void> Access::fast_enumerate(Function<void(DeviceIdentifier const&)>& ca
return {};
}
DeviceIdentifier Access::get_device_identifier(Address address) const
DeviceIdentifier const& Access::get_device_identifier(Address address) const
{
for (auto device_identifier : m_device_identifiers) {
for (auto& device_identifier : m_device_identifiers) {
if (device_identifier.address().domain() == address.domain()
&& device_identifier.address().bus() == address.bus()
&& device_identifier.address().device() == address.device()
@ -211,57 +209,65 @@ DeviceIdentifier Access::get_device_identifier(Address address) const
VERIFY_NOT_REACHED();
}
void Access::write8_field(Address address, u32 field, u8 value)
void Access::write8_field(DeviceIdentifier const& identifier, u32 field, u8 value)
{
VERIFY(identifier.operation_lock().is_locked());
SpinlockLocker locker(m_access_lock);
VERIFY(m_host_controllers.contains(address.domain()));
auto& controller = *m_host_controllers.get(address.domain()).value();
controller.write8_field(address.bus(), address.device(), address.function(), field, value);
VERIFY(m_host_controllers.contains(identifier.address().domain()));
auto& controller = *m_host_controllers.get(identifier.address().domain()).value();
controller.write8_field(identifier.address().bus(), identifier.address().device(), identifier.address().function(), field, value);
}
void Access::write16_field(Address address, u32 field, u16 value)
void Access::write16_field(DeviceIdentifier const& identifier, u32 field, u16 value)
{
VERIFY(identifier.operation_lock().is_locked());
SpinlockLocker locker(m_access_lock);
VERIFY(m_host_controllers.contains(address.domain()));
auto& controller = *m_host_controllers.get(address.domain()).value();
controller.write16_field(address.bus(), address.device(), address.function(), field, value);
VERIFY(m_host_controllers.contains(identifier.address().domain()));
auto& controller = *m_host_controllers.get(identifier.address().domain()).value();
controller.write16_field(identifier.address().bus(), identifier.address().device(), identifier.address().function(), field, value);
}
void Access::write32_field(Address address, u32 field, u32 value)
void Access::write32_field(DeviceIdentifier const& identifier, u32 field, u32 value)
{
VERIFY(identifier.operation_lock().is_locked());
SpinlockLocker locker(m_access_lock);
VERIFY(m_host_controllers.contains(address.domain()));
auto& controller = *m_host_controllers.get(address.domain()).value();
controller.write32_field(address.bus(), address.device(), address.function(), field, value);
VERIFY(m_host_controllers.contains(identifier.address().domain()));
auto& controller = *m_host_controllers.get(identifier.address().domain()).value();
controller.write32_field(identifier.address().bus(), identifier.address().device(), identifier.address().function(), field, value);
}
u8 Access::read8_field(Address address, RegisterOffset field)
u8 Access::read8_field(DeviceIdentifier const& identifier, RegisterOffset field)
{
return read8_field(address, to_underlying(field));
VERIFY(identifier.operation_lock().is_locked());
return read8_field(identifier, to_underlying(field));
}
u16 Access::read16_field(Address address, RegisterOffset field)
u16 Access::read16_field(DeviceIdentifier const& identifier, RegisterOffset field)
{
return read16_field(address, to_underlying(field));
VERIFY(identifier.operation_lock().is_locked());
return read16_field(identifier, to_underlying(field));
}
u8 Access::read8_field(Address address, u32 field)
u8 Access::read8_field(DeviceIdentifier const& identifier, u32 field)
{
VERIFY(identifier.operation_lock().is_locked());
SpinlockLocker locker(m_access_lock);
VERIFY(m_host_controllers.contains(address.domain()));
auto& controller = *m_host_controllers.get(address.domain()).value();
return controller.read8_field(address.bus(), address.device(), address.function(), field);
VERIFY(m_host_controllers.contains(identifier.address().domain()));
auto& controller = *m_host_controllers.get(identifier.address().domain()).value();
return controller.read8_field(identifier.address().bus(), identifier.address().device(), identifier.address().function(), field);
}
u16 Access::read16_field(Address address, u32 field)
u16 Access::read16_field(DeviceIdentifier const& identifier, u32 field)
{
VERIFY(identifier.operation_lock().is_locked());
SpinlockLocker locker(m_access_lock);
VERIFY(m_host_controllers.contains(address.domain()));
auto& controller = *m_host_controllers.get(address.domain()).value();
return controller.read16_field(address.bus(), address.device(), address.function(), field);
VERIFY(m_host_controllers.contains(identifier.address().domain()));
auto& controller = *m_host_controllers.get(identifier.address().domain()).value();
return controller.read16_field(identifier.address().bus(), identifier.address().device(), identifier.address().function(), field);
}
u32 Access::read32_field(Address address, u32 field)
u32 Access::read32_field(DeviceIdentifier const& identifier, u32 field)
{
VERIFY(identifier.operation_lock().is_locked());
SpinlockLocker locker(m_access_lock);
VERIFY(m_host_controllers.contains(address.domain()));
auto& controller = *m_host_controllers.get(address.domain()).value();
return controller.read32_field(address.bus(), address.device(), address.function(), field);
VERIFY(m_host_controllers.contains(identifier.address().domain()));
auto& controller = *m_host_controllers.get(identifier.address().domain()).value();
return controller.read32_field(identifier.address().bus(), identifier.address().device(), identifier.address().function(), field);
}
}