Access.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 <AK/Debug.h>
  27. #include <Kernel/IO.h>
  28. #include <Kernel/PCI/Access.h>
  29. #include <Kernel/PCI/IOAccess.h>
  30. //#define PCI_DEBUG
  31. namespace Kernel {
  32. namespace PCI {
  33. static Access* s_access;
  34. inline void write8(Address address, u32 field, u8 value) { Access::the().write8_field(address, field, value); }
  35. inline void write16(Address address, u32 field, u16 value) { Access::the().write16_field(address, field, value); }
  36. inline void write32(Address address, u32 field, u32 value) { Access::the().write32_field(address, field, value); }
  37. inline u8 read8(Address address, u32 field) { return Access::the().read8_field(address, field); }
  38. inline u16 read16(Address address, u32 field) { return Access::the().read16_field(address, field); }
  39. inline u32 read32(Address address, u32 field) { return Access::the().read32_field(address, field); }
  40. Access& Access::the()
  41. {
  42. if (s_access == nullptr) {
  43. ASSERT_NOT_REACHED(); // We failed to initialize the PCI subsystem, so stop here!
  44. }
  45. return *s_access;
  46. }
  47. bool Access::is_initialized()
  48. {
  49. return (s_access != nullptr);
  50. }
  51. Access::Access()
  52. {
  53. s_access = this;
  54. }
  55. PhysicalID Access::get_physical_id(Address address) const
  56. {
  57. for (auto physical_id : m_physical_ids) {
  58. if (physical_id.address().seg() == address.seg()
  59. && physical_id.address().bus() == address.bus()
  60. && physical_id.address().slot() == address.slot()
  61. && physical_id.address().function() == address.function()) {
  62. return physical_id;
  63. }
  64. }
  65. ASSERT_NOT_REACHED();
  66. }
  67. u8 Access::early_read8_field(Address address, u32 field)
  68. {
  69. dbgln<debug_pci>("PCI: Early reading 8-bit field {:#08x} for {}", field, address);
  70. IO::out32(PCI_ADDRESS_PORT, address.io_address_for_field(field));
  71. return IO::in8(PCI_VALUE_PORT + (field & 3));
  72. }
  73. u16 Access::early_read16_field(Address address, u32 field)
  74. {
  75. dbgln<debug_pci>("PCI: Early reading 16-bit field {:#08x} for {}", field, address);
  76. IO::out32(PCI_ADDRESS_PORT, address.io_address_for_field(field));
  77. return IO::in16(PCI_VALUE_PORT + (field & 2));
  78. }
  79. u32 Access::early_read32_field(Address address, u32 field)
  80. {
  81. dbgln<debug_pci>("PCI: Early reading 32-bit field {:#08x} for {}", field, address);
  82. IO::out32(PCI_ADDRESS_PORT, address.io_address_for_field(field));
  83. return IO::in32(PCI_VALUE_PORT);
  84. }
  85. u16 Access::early_read_type(Address address)
  86. {
  87. dbgln<debug_pci>("PCI: Early reading type for {}", address);
  88. return (early_read8_field(address, PCI_CLASS) << 8u) | early_read8_field(address, PCI_SUBCLASS);
  89. }
  90. void Access::enumerate_functions(int type, u8 bus, u8 slot, u8 function, Function<void(Address, ID)>& callback)
  91. {
  92. dbgln<debug_pci>("PCI: Enumerating function type={}, bus={}, slot={}, function={}", type, bus, slot, function);
  93. Address address(0, bus, slot, function);
  94. if (type == -1 || type == early_read_type(address))
  95. callback(address, { early_read16_field(address, PCI_VENDOR_ID), early_read16_field(address, PCI_DEVICE_ID) });
  96. if (early_read_type(address) == PCI_TYPE_BRIDGE) {
  97. u8 secondary_bus = early_read8_field(address, PCI_SECONDARY_BUS);
  98. #ifdef PCI_DEBUG
  99. klog() << "PCI: Found secondary bus: " << secondary_bus;
  100. #endif
  101. ASSERT(secondary_bus != bus);
  102. enumerate_bus(type, secondary_bus, callback);
  103. }
  104. }
  105. void Access::enumerate_slot(int type, u8 bus, u8 slot, Function<void(Address, ID)>& callback)
  106. {
  107. dbgln<debug_pci>("PCI: Enumerating slot type={}, bus={}, slot={}", type, bus, slot);
  108. Address address(0, bus, slot, 0);
  109. if (early_read16_field(address, PCI_VENDOR_ID) == PCI_NONE)
  110. return;
  111. enumerate_functions(type, bus, slot, 0, callback);
  112. if (!(early_read8_field(address, PCI_HEADER_TYPE) & 0x80))
  113. return;
  114. for (u8 function = 1; function < 8; ++function) {
  115. Address address(0, bus, slot, function);
  116. if (early_read16_field(address, PCI_VENDOR_ID) != PCI_NONE)
  117. enumerate_functions(type, bus, slot, function, callback);
  118. }
  119. }
  120. void Access::enumerate_bus(int type, u8 bus, Function<void(Address, ID)>& callback)
  121. {
  122. dbgln<debug_pci>("PCI: Enumerating bus type={}, bus={}", type, bus);
  123. for (u8 slot = 0; slot < 32; ++slot)
  124. enumerate_slot(type, bus, slot, callback);
  125. }
  126. void Access::enumerate(Function<void(Address, ID)>& callback) const
  127. {
  128. for (auto& physical_id : m_physical_ids) {
  129. callback(physical_id.address(), physical_id.id());
  130. }
  131. }
  132. void enumerate(Function<void(Address, ID)> callback)
  133. {
  134. Access::the().enumerate(callback);
  135. }
  136. Optional<u8> get_capabilities_pointer(Address address)
  137. {
  138. dbgln<debug_pci>("PCI: Getting capabilities pointer for {}", address);
  139. if (PCI::read16(address, PCI_STATUS) & (1 << 4)) {
  140. dbgln<debug_pci>("PCI: Found capabilities pointer for {}", address);
  141. return PCI::read8(address, PCI_CAPABILITIES_POINTER);
  142. }
  143. dbgln<debug_pci>("PCI: No capabilities pointer for {}", address);
  144. return {};
  145. }
  146. PhysicalID get_physical_id(Address address)
  147. {
  148. return Access::the().get_physical_id(address);
  149. }
  150. Vector<Capability> get_capabilities(Address address)
  151. {
  152. dbgln<debug_pci>("PCI: Getting capabilities for {}", address);
  153. auto capabilities_pointer = PCI::get_capabilities_pointer(address);
  154. if (!capabilities_pointer.has_value()) {
  155. dbgln<debug_pci>("PCI: No capabilities for {}", address);
  156. return {};
  157. }
  158. Vector<Capability> capabilities;
  159. auto capability_pointer = capabilities_pointer.value();
  160. while (capability_pointer != 0) {
  161. dbgln<debug_pci>("PCI: Reading in capability at {:#02x} for {}", capability_pointer, address);
  162. u16 capability_header = PCI::read16(address, capability_pointer);
  163. u8 capability_id = capability_header & 0xff;
  164. capability_pointer = capability_header >> 8;
  165. capabilities.append({ capability_id, capability_pointer });
  166. }
  167. return capabilities;
  168. }
  169. void raw_access(Address address, u32 field, size_t access_size, u32 value)
  170. {
  171. ASSERT(access_size != 0);
  172. if (access_size == 1) {
  173. write8(address, field, value);
  174. return;
  175. }
  176. if (access_size == 2) {
  177. write16(address, field, value);
  178. return;
  179. }
  180. if (access_size == 4) {
  181. write32(address, field, value);
  182. return;
  183. }
  184. ASSERT_NOT_REACHED();
  185. }
  186. ID get_id(Address address)
  187. {
  188. return { read16(address, PCI_VENDOR_ID), read16(address, PCI_DEVICE_ID) };
  189. }
  190. void enable_interrupt_line(Address address)
  191. {
  192. write16(address, PCI_COMMAND, read16(address, PCI_COMMAND) & ~(1 << 10));
  193. }
  194. void disable_interrupt_line(Address address)
  195. {
  196. write16(address, PCI_COMMAND, read16(address, PCI_COMMAND) | 1 << 10);
  197. }
  198. u8 get_interrupt_line(Address address)
  199. {
  200. return read8(address, PCI_INTERRUPT_LINE);
  201. }
  202. u32 get_BAR0(Address address)
  203. {
  204. return read32(address, PCI_BAR0);
  205. }
  206. u32 get_BAR1(Address address)
  207. {
  208. return read32(address, PCI_BAR1);
  209. }
  210. u32 get_BAR2(Address address)
  211. {
  212. return read32(address, PCI_BAR2);
  213. }
  214. u32 get_BAR3(Address address)
  215. {
  216. return read16(address, PCI_BAR3);
  217. }
  218. u32 get_BAR4(Address address)
  219. {
  220. return read32(address, PCI_BAR4);
  221. }
  222. u32 get_BAR5(Address address)
  223. {
  224. return read32(address, PCI_BAR5);
  225. }
  226. u8 get_revision_id(Address address)
  227. {
  228. return read8(address, PCI_REVISION_ID);
  229. }
  230. u8 get_subclass(Address address)
  231. {
  232. return read8(address, PCI_SUBCLASS);
  233. }
  234. u8 get_class(Address address)
  235. {
  236. return read8(address, PCI_CLASS);
  237. }
  238. u8 get_programming_interface(Address address)
  239. {
  240. return read8(address, PCI_PROG_IF);
  241. }
  242. u16 get_subsystem_id(Address address)
  243. {
  244. return read16(address, PCI_SUBSYSTEM_ID);
  245. }
  246. u16 get_subsystem_vendor_id(Address address)
  247. {
  248. return read16(address, PCI_SUBSYSTEM_VENDOR_ID);
  249. }
  250. void enable_bus_mastering(Address address)
  251. {
  252. auto value = read16(address, PCI_COMMAND);
  253. value |= (1 << 2);
  254. value |= (1 << 0);
  255. write16(address, PCI_COMMAND, value);
  256. }
  257. void disable_bus_mastering(Address address)
  258. {
  259. auto value = read16(address, PCI_COMMAND);
  260. value &= ~(1 << 2);
  261. value |= (1 << 0);
  262. write16(address, PCI_COMMAND, value);
  263. }
  264. size_t get_BAR_space_size(Address address, u8 bar_number)
  265. {
  266. // See PCI Spec 2.3, Page 222
  267. ASSERT(bar_number < 6);
  268. u8 field = (PCI_BAR0 + (bar_number << 2));
  269. u32 bar_reserved = read32(address, field);
  270. write32(address, field, 0xFFFFFFFF);
  271. u32 space_size = read32(address, field);
  272. write32(address, field, bar_reserved);
  273. space_size &= 0xfffffff0;
  274. space_size = (~space_size) + 1;
  275. return space_size;
  276. }
  277. }
  278. }