Access.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  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/PCI/Access.h>
  27. #include <Kernel/PCI/IOAccess.h>
  28. namespace Kernel {
  29. static PCI::Access* s_access;
  30. PCI::Access& PCI::Access::the()
  31. {
  32. if (s_access == nullptr) {
  33. ASSERT_NOT_REACHED(); // We failed to initialize the PCI subsystem, so stop here!
  34. }
  35. return *s_access;
  36. }
  37. bool PCI::Access::is_initialized()
  38. {
  39. return (s_access != nullptr);
  40. }
  41. PCI::Access::Access()
  42. {
  43. s_access = this;
  44. }
  45. void PCI::Access::enumerate_functions(int type, u8 bus, u8 slot, u8 function, Function<void(Address, ID)>& callback)
  46. {
  47. Address address(0, bus, slot, function);
  48. if (type == -1 || type == read_type(address))
  49. callback(address, { read16_field(address, PCI_VENDOR_ID), read16_field(address, PCI_DEVICE_ID) });
  50. if (read_type(address) == PCI_TYPE_BRIDGE) {
  51. u8 secondary_bus = read8_field(address, PCI_SECONDARY_BUS);
  52. #ifdef PCI_DEBUG
  53. klog() << "PCI: Found secondary bus: " << secondary_bus;
  54. #endif
  55. ASSERT(secondary_bus != bus);
  56. enumerate_bus(type, secondary_bus, callback);
  57. }
  58. }
  59. void PCI::Access::enumerate_slot(int type, u8 bus, u8 slot, Function<void(Address, ID)>& callback)
  60. {
  61. Address address(0, bus, slot, 0);
  62. if (read16_field(address, PCI_VENDOR_ID) == PCI_NONE)
  63. return;
  64. enumerate_functions(type, bus, slot, 0, callback);
  65. if (!(read8_field(address, PCI_HEADER_TYPE) & 0x80))
  66. return;
  67. for (u8 function = 1; function < 8; ++function) {
  68. Address address(0, bus, slot, function);
  69. if (read16_field(address, PCI_VENDOR_ID) != PCI_NONE)
  70. enumerate_functions(type, bus, slot, function, callback);
  71. }
  72. }
  73. PCI::ID PCI::Access::get_id(Address address)
  74. {
  75. return { read16_field(address, PCI_VENDOR_ID), read16_field(address, PCI_DEVICE_ID) };
  76. }
  77. void PCI::Access::enumerate_bus(int type, u8 bus, Function<void(Address, ID)>& callback)
  78. {
  79. for (u8 slot = 0; slot < 32; ++slot)
  80. enumerate_slot(type, bus, slot, callback);
  81. }
  82. void PCI::Access::enable_bus_mastering(Address address)
  83. {
  84. auto value = read16_field(address, PCI_COMMAND);
  85. value |= (1 << 2);
  86. value |= (1 << 0);
  87. write16_field(address, PCI_COMMAND, value);
  88. }
  89. void PCI::Access::disable_bus_mastering(Address address)
  90. {
  91. auto value = read16_field(address, PCI_COMMAND);
  92. value &= ~(1 << 2);
  93. value |= (1 << 0);
  94. write16_field(address, PCI_COMMAND, value);
  95. }
  96. namespace PCI {
  97. void enumerate_all(Function<void(Address, ID)> callback)
  98. {
  99. PCI::Access::the().enumerate_all(callback);
  100. }
  101. ID get_id(Address address)
  102. {
  103. return PCI::Access::the().get_id(address);
  104. }
  105. void enable_interrupt_line(Address address)
  106. {
  107. PCI::Access::the().enable_interrupt_line(address);
  108. }
  109. void disable_interrupt_line(Address address)
  110. {
  111. PCI::Access::the().disable_interrupt_line(address);
  112. }
  113. u8 get_interrupt_line(Address address)
  114. {
  115. return PCI::Access::the().get_interrupt_line(address);
  116. }
  117. u32 get_BAR0(Address address)
  118. {
  119. return PCI::Access::the().get_BAR0(address);
  120. }
  121. u32 get_BAR1(Address address)
  122. {
  123. return PCI::Access::the().get_BAR1(address);
  124. }
  125. u32 get_BAR2(Address address)
  126. {
  127. return PCI::Access::the().get_BAR2(address);
  128. }
  129. u32 get_BAR3(Address address)
  130. {
  131. return PCI::Access::the().get_BAR3(address);
  132. }
  133. u32 get_BAR4(Address address)
  134. {
  135. return PCI::Access::the().get_BAR4(address);
  136. }
  137. u32 get_BAR5(Address address)
  138. {
  139. return PCI::Access::the().get_BAR5(address);
  140. }
  141. u8 get_revision_id(Address address)
  142. {
  143. return PCI::Access::the().get_revision_id(address);
  144. }
  145. u8 get_subclass(Address address)
  146. {
  147. return PCI::Access::the().get_subclass(address);
  148. }
  149. u8 get_class(Address address)
  150. {
  151. return PCI::Access::the().get_class(address);
  152. }
  153. u16 get_subsystem_id(Address address)
  154. {
  155. return PCI::Access::the().get_subsystem_id(address);
  156. }
  157. u16 get_subsystem_vendor_id(Address address)
  158. {
  159. return PCI::Access::the().get_subsystem_vendor_id(address);
  160. }
  161. void enable_bus_mastering(Address address)
  162. {
  163. PCI::Access::the().enable_bus_mastering(address);
  164. }
  165. void disable_bus_mastering(Address address)
  166. {
  167. PCI::Access::the().disable_bus_mastering(address);
  168. }
  169. size_t get_BAR_Space_Size(Address address, u8 bar_number)
  170. {
  171. return PCI::Access::the().get_BAR_Space_Size(address, bar_number);
  172. }
  173. }
  174. }