pci.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2011-2018 K. Lange
  5. *
  6. * ToAruOS PCI Initialization
  7. */
  8. #include <kernel/system.h>
  9. #include <kernel/pci.h>
  10. #include <kernel/logging.h>
  11. void pci_write_field(uint32_t device, int field, int size, uint32_t value) {
  12. outportl(PCI_ADDRESS_PORT, pci_get_addr(device, field));
  13. outportl(PCI_VALUE_PORT, value);
  14. }
  15. uint32_t pci_read_field(uint32_t device, int field, int size) {
  16. outportl(PCI_ADDRESS_PORT, pci_get_addr(device, field));
  17. if (size == 4) {
  18. uint32_t t = inportl(PCI_VALUE_PORT);
  19. return t;
  20. } else if (size == 2) {
  21. uint16_t t = inports(PCI_VALUE_PORT + (field & 2));
  22. return t;
  23. } else if (size == 1) {
  24. uint8_t t = inportb(PCI_VALUE_PORT + (field & 3));
  25. return t;
  26. }
  27. return 0xFFFF;
  28. }
  29. uint16_t pci_find_type(uint32_t dev) {
  30. return (pci_read_field(dev, PCI_CLASS, 1) << 8) | pci_read_field(dev, PCI_SUBCLASS, 1);
  31. }
  32. struct {
  33. uint16_t id;
  34. const char * name;
  35. } _pci_vendors[] = {
  36. {0x1022, "AMD"},
  37. {0x106b, "Apple, Inc."},
  38. {0x1234, "Bochs/QEMU"},
  39. {0x1274, "Ensoniq"},
  40. {0x15ad, "VMWare"},
  41. {0x8086, "Intel Corporation"},
  42. {0x80EE, "VirtualBox"},
  43. };
  44. struct {
  45. uint16_t ven_id;
  46. uint16_t dev_id;
  47. const char * name;
  48. } _pci_devices[] = {
  49. {0x1022, 0x2000, "PCNet Ethernet Controller (pcnet)"},
  50. {0x106b, 0x003f, "OHCI Controller"},
  51. {0x1234, 0x1111, "VGA BIOS Graphics Extensions"},
  52. {0x1274, 0x1371, "Creative Labs CT2518 (ensoniq audio)"},
  53. {0x15ad, 0x0740, "VM Communication Interface"},
  54. {0x15ad, 0x0405, "SVGA II Adapter"},
  55. {0x15ad, 0x0790, "PCI bridge"},
  56. {0x15ad, 0x07a0, "PCI Express Root Port"},
  57. {0x8086, 0x100e, "Gigabit Ethernet Controller (e1000)"},
  58. {0x8086, 0x100f, "Gigabit Ethernet Controller (e1000)"},
  59. {0x8086, 0x1237, "PCI & Memory"},
  60. {0x8086, 0x2415, "AC'97 Audio Chipset"},
  61. {0x8086, 0x7000, "PCI-to-ISA Bridge"},
  62. {0x8086, 0x7010, "IDE Interface"},
  63. {0x8086, 0x7110, "PIIX4 ISA"},
  64. {0x8086, 0x7111, "PIIX4 IDE"},
  65. {0x8086, 0x7113, "Power Management Controller"},
  66. {0x8086, 0x7190, "Host Bridge"},
  67. {0x8086, 0x7191, "AGP Bridge"},
  68. {0x80EE, 0xBEEF, "Bochs/QEMU-compatible Graphics Adapter"},
  69. {0x80EE, 0xCAFE, "Guest Additions Device"},
  70. };
  71. const char * pci_vendor_lookup(unsigned short vendor_id) {
  72. for (unsigned int i = 0; i < sizeof(_pci_vendors)/sizeof(_pci_vendors[0]); ++i) {
  73. if (_pci_vendors[i].id == vendor_id) {
  74. return _pci_vendors[i].name;
  75. }
  76. }
  77. return "";
  78. }
  79. const char * pci_device_lookup(unsigned short vendor_id, unsigned short device_id) {
  80. for (unsigned int i = 0; i < sizeof(_pci_devices)/sizeof(_pci_devices[0]); ++i) {
  81. if (_pci_devices[i].ven_id == vendor_id && _pci_devices[i].dev_id == device_id) {
  82. return _pci_devices[i].name;
  83. }
  84. }
  85. return "";
  86. }
  87. void pci_scan_hit(pci_func_t f, uint32_t dev, void * extra) {
  88. int dev_vend = (int)pci_read_field(dev, PCI_VENDOR_ID, 2);
  89. int dev_dvid = (int)pci_read_field(dev, PCI_DEVICE_ID, 2);
  90. f(dev, dev_vend, dev_dvid, extra);
  91. }
  92. void pci_scan_func(pci_func_t f, int type, int bus, int slot, int func, void * extra) {
  93. uint32_t dev = pci_box_device(bus, slot, func);
  94. if (type == -1 || type == pci_find_type(dev)) {
  95. pci_scan_hit(f, dev, extra);
  96. }
  97. if (pci_find_type(dev) == PCI_TYPE_BRIDGE) {
  98. pci_scan_bus(f, type, pci_read_field(dev, PCI_SECONDARY_BUS, 1), extra);
  99. }
  100. }
  101. void pci_scan_slot(pci_func_t f, int type, int bus, int slot, void * extra) {
  102. uint32_t dev = pci_box_device(bus, slot, 0);
  103. if (pci_read_field(dev, PCI_VENDOR_ID, 2) == PCI_NONE) {
  104. return;
  105. }
  106. pci_scan_func(f, type, bus, slot, 0, extra);
  107. if (!pci_read_field(dev, PCI_HEADER_TYPE, 1)) {
  108. return;
  109. }
  110. for (int func = 1; func < 8; func++) {
  111. uint32_t dev = pci_box_device(bus, slot, func);
  112. if (pci_read_field(dev, PCI_VENDOR_ID, 2) != PCI_NONE) {
  113. pci_scan_func(f, type, bus, slot, func, extra);
  114. }
  115. }
  116. }
  117. void pci_scan_bus(pci_func_t f, int type, int bus, void * extra) {
  118. for (int slot = 0; slot < 32; ++slot) {
  119. pci_scan_slot(f, type, bus, slot, extra);
  120. }
  121. }
  122. void pci_scan(pci_func_t f, int type, void * extra) {
  123. if ((pci_read_field(0, PCI_HEADER_TYPE, 1) & 0x80) == 0) {
  124. pci_scan_bus(f,type,0,extra);
  125. return;
  126. }
  127. for (int func = 0; func < 8; ++func) {
  128. uint32_t dev = pci_box_device(0, 0, func);
  129. if (pci_read_field(dev, PCI_VENDOR_ID, 2) != PCI_NONE) {
  130. pci_scan_bus(f, type, func, extra);
  131. } else {
  132. break;
  133. }
  134. }
  135. }
  136. static void find_isa_bridge(uint32_t device, uint16_t vendorid, uint16_t deviceid, void * extra) {
  137. if (vendorid == 0x8086 && (deviceid == 0x7000 || deviceid == 0x7110)) {
  138. *((uint32_t *)extra) = device;
  139. }
  140. }
  141. static uint32_t pci_isa = 0;
  142. static uint8_t pci_remaps[4] = {0};
  143. void pci_remap(void) {
  144. pci_scan(&find_isa_bridge, -1, &pci_isa);
  145. if (pci_isa) {
  146. for (int i = 0; i < 4; ++i) {
  147. pci_remaps[i] = pci_read_field(pci_isa, 0x60+i, 1);
  148. }
  149. uint32_t out = 0;
  150. memcpy(&out, &pci_remaps, 4);
  151. pci_write_field(pci_isa, 0x60, 4, out);
  152. }
  153. }
  154. int pci_get_interrupt(uint32_t device) {
  155. if (pci_isa) {
  156. uint32_t irq_pin = pci_read_field(device, 0x3D, 1);
  157. if (irq_pin == 0) {
  158. /* ??? */
  159. debug_print(ERROR, "PCI device does not specific interrupt line");
  160. return pci_read_field(device, PCI_INTERRUPT_LINE, 1);
  161. }
  162. int pirq = (irq_pin + pci_extract_slot(device) - 2) % 4;
  163. int int_line = pci_read_field(device, PCI_INTERRUPT_LINE, 1);
  164. debug_print(ERROR, "slot is %d, irq pin is %d, so pirq is %d and that maps to %d? int_line=%d", pci_extract_slot(device), irq_pin, pirq, pci_remaps[pirq], int_line);
  165. if (pci_remaps[pirq] == 0x80) {
  166. debug_print(ERROR, "not mapped, remapping?\n");
  167. pci_remaps[pirq] = int_line;
  168. uint32_t out = 0;
  169. memcpy(&out, &pci_remaps, 4);
  170. pci_write_field(pci_isa, 0x60, 4, out);
  171. return pci_read_field(device, PCI_INTERRUPT_LINE, 1);
  172. }
  173. return pci_remaps[pirq];
  174. } else {
  175. return pci_read_field(device, PCI_INTERRUPT_LINE, 1);
  176. }
  177. }