SysFSPCI.cpp 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/Bus/PCI/API.h>
  7. #include <Kernel/Bus/PCI/Access.h>
  8. #include <Kernel/Bus/PCI/SysFSPCI.h>
  9. #include <Kernel/Debug.h>
  10. #include <Kernel/IO.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", *this, PCI_VENDOR_ID, 2));
  22. m_components.append(PCIDeviceAttributeSysFSComponent::create("device_id", *this, PCI_DEVICE_ID, 2));
  23. m_components.append(PCIDeviceAttributeSysFSComponent::create("class", *this, PCI_CLASS, 1));
  24. m_components.append(PCIDeviceAttributeSysFSComponent::create("subclass", *this, PCI_SUBCLASS, 1));
  25. m_components.append(PCIDeviceAttributeSysFSComponent::create("revision", *this, PCI_REVISION_ID, 1));
  26. m_components.append(PCIDeviceAttributeSysFSComponent::create("progif", *this, PCI_PROG_IF, 1));
  27. m_components.append(PCIDeviceAttributeSysFSComponent::create("subsystem_vendor", *this, PCI_SUBSYSTEM_VENDOR_ID, 2));
  28. m_components.append(PCIDeviceAttributeSysFSComponent::create("subsystem_id", *this, PCI_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_component(pci_directory);
  34. }
  35. UNMAP_AFTER_INIT PCIBusSysFSDirectory::PCIBusSysFSDirectory()
  36. : SysFSDirectory("pci", SysFSComponentRegistry::the().root_directory())
  37. {
  38. PCI::enumerate([&](const Address& address, ID) {
  39. auto pci_device = PCI::PCIDeviceSysFSDirectory::create(*this, address);
  40. m_components.append(pci_device);
  41. });
  42. }
  43. NonnullRefPtr<PCIDeviceAttributeSysFSComponent> PCIDeviceAttributeSysFSComponent::create(String name, const PCIDeviceSysFSDirectory& device, size_t offset, size_t field_bytes_width)
  44. {
  45. return adopt_ref(*new (nothrow) PCIDeviceAttributeSysFSComponent(name, device, offset, field_bytes_width));
  46. }
  47. PCIDeviceAttributeSysFSComponent::PCIDeviceAttributeSysFSComponent(String name, const PCIDeviceSysFSDirectory& device, size_t 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. KResultOr<size_t> PCIDeviceAttributeSysFSComponent::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, FileDescription*) const
  55. {
  56. auto blob = try_to_generate_buffer();
  57. if (!blob)
  58. return KResult(EFAULT);
  59. if ((size_t)offset >= blob->size())
  60. return KSuccess;
  61. ssize_t nread = min(static_cast<off_t>(blob->size() - offset), static_cast<off_t>(count));
  62. if (!buffer.write(blob->data() + offset, nread))
  63. return KResult(EFAULT);
  64. return nread;
  65. }
  66. OwnPtr<KBuffer> PCIDeviceAttributeSysFSComponent::try_to_generate_buffer() const
  67. {
  68. String value;
  69. switch (m_field_bytes_width) {
  70. case 1:
  71. value = String::formatted("{:#x}", PCI::read8(m_device->address(), m_offset));
  72. break;
  73. case 2:
  74. value = String::formatted("{:#x}", PCI::read16(m_device->address(), m_offset));
  75. break;
  76. case 4:
  77. value = String::formatted("{:#x}", PCI::read32(m_device->address(), m_offset));
  78. break;
  79. default:
  80. VERIFY_NOT_REACHED();
  81. }
  82. return KBuffer::try_create_with_bytes(value.substring_view(0).bytes());
  83. }
  84. }