IOAccess.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <Kernel/Debug.h>
  27. #include <Kernel/IO.h>
  28. #include <Kernel/PCI/IOAccess.h>
  29. namespace Kernel {
  30. namespace PCI {
  31. void IOAccess::initialize()
  32. {
  33. if (!Access::is_initialized()) {
  34. new IOAccess();
  35. dbgln<PCI_DEBUG>("PCI: IO access initialised.");
  36. }
  37. }
  38. IOAccess::IOAccess()
  39. {
  40. klog() << "PCI: Using I/O instructions for PCI configuration space access";
  41. enumerate_hardware([&](const Address& address, ID id) {
  42. m_physical_ids.append({ address, id, get_capabilities(address) });
  43. });
  44. }
  45. u8 IOAccess::read8_field(Address address, u32 field)
  46. {
  47. dbgln<PCI_DEBUG>("PCI: IO Reading 8-bit field {:#08x} for {}", field, address);
  48. return Access::early_read8_field(address, field);
  49. }
  50. u16 IOAccess::read16_field(Address address, u32 field)
  51. {
  52. dbgln<PCI_DEBUG>("PCI: IO Reading 16-bit field {:#08x} for {}", field, address);
  53. return Access::early_read16_field(address, field);
  54. }
  55. u32 IOAccess::read32_field(Address address, u32 field)
  56. {
  57. dbgln<PCI_DEBUG>("PCI: IO Reading 32-bit field {:#08x} for {}", field, address);
  58. return Access::early_read32_field(address, field);
  59. }
  60. void IOAccess::write8_field(Address address, u32 field, u8 value)
  61. {
  62. dbgln<PCI_DEBUG>("PCI: IO Writing to 8-bit field {:#08x}, value={:#02x} for {}", field, value, address);
  63. IO::out32(PCI_ADDRESS_PORT, address.io_address_for_field(field));
  64. IO::out8(PCI_VALUE_PORT + (field & 3), value);
  65. }
  66. void IOAccess::write16_field(Address address, u32 field, u16 value)
  67. {
  68. dbgln<PCI_DEBUG>("PCI: IO Writing to 16-bit field {:#08x}, value={:#02x} for {}", field, value, address);
  69. IO::out32(PCI_ADDRESS_PORT, address.io_address_for_field(field));
  70. IO::out16(PCI_VALUE_PORT + (field & 2), value);
  71. }
  72. void IOAccess::write32_field(Address address, u32 field, u32 value)
  73. {
  74. dbgln<PCI_DEBUG>("PCI: IO Writing to 32-bit field {:#08x}, value={:#02x} for {}", field, value, address);
  75. IO::out32(PCI_ADDRESS_PORT, address.io_address_for_field(field));
  76. IO::out32(PCI_VALUE_PORT, value);
  77. }
  78. void IOAccess::enumerate_hardware(Function<void(Address, ID)> callback)
  79. {
  80. #if PCI_DEBUG
  81. dbgln("PCI: IO enumerating hardware");
  82. #endif
  83. // Single PCI host controller.
  84. if ((read8_field(Address(), PCI_HEADER_TYPE) & 0x80) == 0) {
  85. enumerate_bus(-1, 0, callback);
  86. return;
  87. }
  88. // Multiple PCI host controllers.
  89. for (u8 function = 0; function < 8; ++function) {
  90. if (read16_field(Address(0, 0, 0, function), PCI_VENDOR_ID) == PCI_NONE)
  91. break;
  92. enumerate_bus(-1, function, callback);
  93. }
  94. }
  95. }
  96. }