pci.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * pci.c
  3. *
  4. * Copyright (C) 2016 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <pci.h>
  20. #include <log.h>
  21. #include <heap.h>
  22. #include <irq.h>
  23. const char *pci_device_classes[] = {
  24. "Legacy Device",
  25. "Mass Storage Controller",
  26. "Network Controller",
  27. "Display Controller",
  28. "Multimedia Controller",
  29. "Memory Controller",
  30. "Bridge Device",
  31. "Simple Communication Controller",
  32. "Base System Peripheral",
  33. "Input Device",
  34. "Docking Station",
  35. "Processor",
  36. "Serial Bus Controller",
  37. "Wireless Controller"
  38. };
  39. static DECLARE_LIST(pci_device_list);
  40. static inline bool_t pci_device_exists(dword_t bus, dword_t slot, dword_t func)
  41. {
  42. pci_device_t device = {
  43. .bus = bus,
  44. .slot = slot,
  45. .function = func
  46. };
  47. return PCI_READ_VALUE(&device, pci_header_t, vendor_id) != 0xFFFF;
  48. }
  49. dword_t pci_read(pci_device_t *device, dword_t reg)
  50. {
  51. cpu_write_port_dword(0x0CF8,
  52. 0x80000000
  53. | ((device->bus & 0xFF) << 16)
  54. | ((device->slot & 0x1F) << 11)
  55. | ((device->function & 0x07) << 8)
  56. | ((reg & 0x3F) << 2));
  57. return cpu_read_port_dword(0x0CFC);
  58. }
  59. void pci_write(pci_device_t *device, dword_t reg, dword_t data)
  60. {
  61. cpu_write_port_dword(0x0CF8,
  62. 0x80000000
  63. | ((device->bus & 0xFF) << 16)
  64. | ((device->slot & 0x1F) << 11)
  65. | ((device->function & 0x07) << 8)
  66. | ((reg & 0x3F) << 2));
  67. cpu_write_port_dword(0x0CFC, data);
  68. }
  69. list_entry_t *get_pci_device_list_head(void)
  70. {
  71. return &pci_device_list;
  72. }
  73. void pci_init(void)
  74. {
  75. byte_t buses[256] = {0};
  76. size_t num_buses = 1;
  77. int i, j, k;
  78. for (i = 0; i < num_buses; i++)
  79. {
  80. for (j = 0; j < 32; j++) for (k = 0; k < 8; k++)
  81. {
  82. if (!pci_device_exists(buses[i], j, k)) continue;
  83. pci_device_t *device = (pci_device_t*)malloc(sizeof(pci_device_t));
  84. if (device == NULL) continue;
  85. device->bus = buses[i];
  86. device->slot = j;
  87. device->function = k;
  88. dword_t reg2 = pci_read(device, 2);
  89. device->class = (reg2 >> 24) & 0xFF;
  90. device->subclass = (reg2 >> 16) & 0xFF;
  91. device->prog_if = (reg2 >> 8) & 0xFF;
  92. device->in_use = FALSE;
  93. if (device->class == PCI_BRIDGE_DEVICE)
  94. {
  95. if (device->subclass == 0)
  96. {
  97. if (device->function) buses[num_buses++] = device->function;
  98. }
  99. else if (device->subclass == 4 || device->subclass == 9)
  100. {
  101. buses[num_buses++] = PCI_READ_VALUE(device, pci_bridge_header_t, secondary_bus_num);
  102. }
  103. }
  104. log_write(LOG_NORMAL, "PCI bus #%u, slot #%u, function #%u: %s\n", buses[i], j, k, pci_device_classes[device->class]);
  105. list_append(&pci_device_list, &device->list);
  106. if (k == 0 && !(PCI_READ_VALUE(device, pci_header_t, header_type) & 0x80)) break;
  107. }
  108. }
  109. }