SysFSPCI.cpp 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/String.h>
  7. #include <Kernel/Bus/PCI/API.h>
  8. #include <Kernel/Bus/PCI/Access.h>
  9. #include <Kernel/Bus/PCI/SysFSPCI.h>
  10. #include <Kernel/Debug.h>
  11. #include <Kernel/Sections.h>
  12. namespace Kernel::PCI {
  13. UNMAP_AFTER_INIT NonnullRefPtr<PCIDeviceSysFSDirectory> PCIDeviceSysFSDirectory::create(const SysFSDirectory& parent_directory, Address address)
  14. {
  15. return adopt_ref(*new (nothrow) PCIDeviceSysFSDirectory(parent_directory, address));
  16. }
  17. UNMAP_AFTER_INIT PCIDeviceSysFSDirectory::PCIDeviceSysFSDirectory(const SysFSDirectory& parent_directory, Address address)
  18. : SysFSDirectory(String::formatted("{:04x}:{:02x}:{:02x}.{}", address.domain(), address.bus(), address.device(), address.function()), parent_directory)
  19. , m_address(address)
  20. {
  21. m_components.append(PCIDeviceAttributeSysFSComponent::create("vendor"sv, *this, PCI::RegisterOffset::VENDOR_ID, 2));
  22. m_components.append(PCIDeviceAttributeSysFSComponent::create("device_id"sv, *this, PCI::RegisterOffset::DEVICE_ID, 2));
  23. m_components.append(PCIDeviceAttributeSysFSComponent::create("class"sv, *this, PCI::RegisterOffset::CLASS, 1));
  24. m_components.append(PCIDeviceAttributeSysFSComponent::create("subclass"sv, *this, PCI::RegisterOffset::SUBCLASS, 1));
  25. m_components.append(PCIDeviceAttributeSysFSComponent::create("revision"sv, *this, PCI::RegisterOffset::REVISION_ID, 1));
  26. m_components.append(PCIDeviceAttributeSysFSComponent::create("progif"sv, *this, PCI::RegisterOffset::PROG_IF, 1));
  27. m_components.append(PCIDeviceAttributeSysFSComponent::create("subsystem_vendor"sv, *this, PCI::RegisterOffset::SUBSYSTEM_VENDOR_ID, 2));
  28. m_components.append(PCIDeviceAttributeSysFSComponent::create("subsystem_id"sv, *this, PCI::RegisterOffset::SUBSYSTEM_ID, 2));
  29. }
  30. UNMAP_AFTER_INIT void PCIBusSysFSDirectory::initialize()
  31. {
  32. auto pci_directory = adopt_ref(*new (nothrow) PCIBusSysFSDirectory());
  33. SysFSComponentRegistry::the().register_new_bus_directory(pci_directory);
  34. }
  35. UNMAP_AFTER_INIT PCIBusSysFSDirectory::PCIBusSysFSDirectory()
  36. : SysFSDirectory("pci", SysFSComponentRegistry::the().buses_directory())
  37. {
  38. PCI::enumerate([&](DeviceIdentifier const& device_identifier) {
  39. auto pci_device = PCI::PCIDeviceSysFSDirectory::create(*this, device_identifier.address());
  40. m_components.append(pci_device);
  41. });
  42. }
  43. NonnullRefPtr<PCIDeviceAttributeSysFSComponent> PCIDeviceAttributeSysFSComponent::create(StringView name, const PCIDeviceSysFSDirectory& device, PCI::RegisterOffset offset, size_t field_bytes_width)
  44. {
  45. return adopt_ref(*new (nothrow) PCIDeviceAttributeSysFSComponent(name, device, offset, field_bytes_width));
  46. }
  47. PCIDeviceAttributeSysFSComponent::PCIDeviceAttributeSysFSComponent(StringView name, const PCIDeviceSysFSDirectory& device, PCI::RegisterOffset offset, size_t field_bytes_width)
  48. : SysFSComponent(name)
  49. , m_device(device)
  50. , m_offset(offset)
  51. , m_field_bytes_width(field_bytes_width)
  52. {
  53. }
  54. ErrorOr<size_t> PCIDeviceAttributeSysFSComponent::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const
  55. {
  56. auto blob = TRY(try_to_generate_buffer());
  57. if ((size_t)offset >= blob->size())
  58. return 0;
  59. ssize_t nread = min(static_cast<off_t>(blob->size() - offset), static_cast<off_t>(count));
  60. TRY(buffer.write(blob->data() + offset, nread));
  61. return nread;
  62. }
  63. ErrorOr<NonnullOwnPtr<KBuffer>> PCIDeviceAttributeSysFSComponent::try_to_generate_buffer() const
  64. {
  65. OwnPtr<KString> value;
  66. switch (m_field_bytes_width) {
  67. case 1:
  68. value = TRY(KString::formatted("{:#x}", PCI::read8(m_device->address(), m_offset)));
  69. break;
  70. case 2:
  71. value = TRY(KString::formatted("{:#x}", PCI::read16(m_device->address(), m_offset)));
  72. break;
  73. case 4:
  74. value = TRY(KString::formatted("{:#x}", PCI::read32(m_device->address(), m_offset)));
  75. break;
  76. default:
  77. VERIFY_NOT_REACHED();
  78. }
  79. return KBuffer::try_create_with_bytes(value->view().bytes());
  80. }
  81. }