pci.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of XNIX. */
  3. /*
  4. XNIX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) and later version.
  8. */
  9. /*
  10. XNIX is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. */
  15. /*
  16. You should have received a copy of the GNU General Public License
  17. along with XNIX. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include <kernel/init.h>
  20. #include <kernel/errno.h>
  21. #include <kernel/types.h>
  22. #include <kernel/int.h>
  23. #include <kernel/pci.h>
  24. #include <kernel/pci_vendors.h>
  25. #include <kernel/schk.h>
  26. #include <lib/string.h>
  27. #include <arch/x86/int.h>
  28. #include <arch/io.h>
  29. #include <arch/sys.h>
  30. __uint32_t pci_read_config(__uint8_t bus, __uint8_t slot, __uint8_t func, __uint8_t offset) {
  31. schk_init();
  32. __uint32_t addr;
  33. #define lbus ((__uint32_t) bus)
  34. #define lslot ((__uint32_t) slot)
  35. #define lfunc ((__uint32_t) func)
  36. addr = (lbus << 16) | (lslot << 11) | (lfunc << 8) | (offset & 0xfc) | 0x80000000ul;
  37. outl(addr, 0xcf8);
  38. return inl(0xcfc);
  39. }
  40. __uint32_t pci_lspci(void) {
  41. schk_init();
  42. __uint8_t bus, slot, func;
  43. __uint32_t devs = 0;
  44. for (bus = 0; bus < 255; bus++) {
  45. for (slot = 0; slot < 32; slot++) {
  46. for (func = 0; func < 8; func++) {
  47. if (pci_read_config(bus, slot, func, 0) != 0xffffffff) {
  48. /* exists */
  49. printk(yellow, "pci: found %b:%b:%b %w %x. \n",
  50. bus, slot, func,
  51. pci_read_config(bus, slot, func, 8) >> 16,
  52. pci_read_config(bus, slot, func, 0));
  53. devs++;
  54. pci_infodev(bus, slot, func);
  55. }
  56. }
  57. }
  58. }
  59. schk_leave();
  60. return devs;
  61. }
  62. void pci_infodev(__uint8_t bus, __uint8_t slot, __uint8_t func) {
  63. schk_init();
  64. __uint32_t buf[256];
  65. struct pci_header header;
  66. int i;
  67. for (i = 0; i < 256 / 4; i++)
  68. buf[i] = pci_read_config(bus, slot, func, i << 2);
  69. memcpy((char *) &header, (char *) buf, sizeof(struct pci_header));
  70. /* put the info of this device on the screen */
  71. /* printk(yellow, "vendor id: %w\n", header.vendor);
  72. printk(yellow, "device id: %w\n", header.dev);
  73. printk(yellow, "revision: %b\n", header.revision);
  74. printk(yellow, "sub class id: %b\n", header.subclass);
  75. printk(yellow, "class code: %b\n", header.class_code);
  76. printk(yellow, "cache line size: %b\n", header.cache_size);
  77. printk(yellow, "latency: %b\n", header.latency);
  78. printk(yellow, "header type: %b\n", header.hdr_type);
  79. */
  80. struct pci_header0 *hdr = (struct pci_header0 *) buf;
  81. /* printk(yellow, "bar0: %x ", hdr->bar0);
  82. printk(yellow, "bar1: %x\n", hdr->bar1);
  83. printk(yellow, "bar2: %x ", hdr->bar2);
  84. printk(yellow, "bar3: %x\n", hdr->bar3);
  85. printk(yellow, "bar4: %x ", hdr->bar4);
  86. printk(yellow, "bar5: %x\n", hdr->bar5);
  87. printk(yellow, "irq: %b\n", hdr->irqnr);
  88. */
  89. /* search in the vendor-dev table, and got the dev_init */
  90. errno_t (* dev_init) (struct pci_header0 *) = NULL;
  91. for (i = 0; ; i++) {
  92. if (pci_vendors[i].class == 0xff)
  93. break;
  94. if (pci_vendors[i].class == header.class_code &&
  95. pci_vendors[i].subclass == header.subclass) {
  96. dev_init = pci_vendors[i].dev_init;
  97. break;
  98. }
  99. }
  100. if (!dev_init) {
  101. printk(red, "module not found for vendor %w, device %w. \n",
  102. header.vendor, header.dev);
  103. return;
  104. }
  105. printk(yellow, "dev_init: %q\n", dev_init);
  106. dev_init(hdr);
  107. schk_leave();
  108. return;
  109. }