pci.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <lib/string.h>
  26. #include <arch/x86/int.h>
  27. #include <arch/io.h>
  28. #include <arch/sys.h>
  29. __uint32_t pci_read_config(__uint8_t bus, __uint8_t slot, __uint8_t func, __uint8_t offset) {
  30. __uint32_t addr;
  31. #define lbus ((__uint32_t) bus)
  32. #define lslot ((__uint32_t) slot)
  33. #define lfunc ((__uint32_t) func)
  34. addr = (lbus << 16) | (lslot << 11) | (lfunc << 8) | (offset & 0xfc) | 0x80000000ul;
  35. outl(addr, 0xcf8);
  36. return inl(0xcfc);
  37. }
  38. __uint32_t pci_lspci(void) {
  39. __uint8_t bus, slot, func;
  40. __uint32_t devs = 0;
  41. for (bus = 0; bus < 255; bus++) {
  42. for (slot = 0; slot < 32; slot++) {
  43. for (func = 0; func < 8; func++) {
  44. if (pci_read_config(bus, slot, func, 0) != 0xffffffff) {
  45. /* exists */
  46. printk(yellow, "pci: found %b:%b:%b %w %x. \n",
  47. bus, slot, func,
  48. pci_read_config(bus, slot, func, 8) >> 16,
  49. pci_read_config(bus, slot, func, 0));
  50. devs++;
  51. pci_infodev(bus, slot, func);
  52. }
  53. }
  54. }
  55. }
  56. return devs;
  57. }
  58. void pci_infodev(__uint8_t bus, __uint8_t slot, __uint8_t func) {
  59. __uint32_t buf[256];
  60. struct pci_header header;
  61. int i;
  62. for (i = 0; i < 256 / 4; i++)
  63. buf[i] = pci_read_config(bus, slot, func, i << 2);
  64. memcpy((char *) &header, (char *) buf, sizeof(struct pci_header));
  65. /* put the info of this device on the screen */
  66. /* printk(yellow, "vendor id: %w\n", header.vendor);
  67. printk(yellow, "device id: %w\n", header.dev);
  68. printk(yellow, "revision: %b\n", header.revision);
  69. printk(yellow, "sub class id: %b\n", header.subclass);
  70. printk(yellow, "class code: %b\n", header.class_code);
  71. printk(yellow, "cache line size: %b\n", header.cache_size);
  72. printk(yellow, "latency: %b\n", header.latency);
  73. printk(yellow, "header type: %b\n", header.hdr_type);
  74. */
  75. struct pci_header0 *hdr = (struct pci_header0 *) buf;
  76. /* printk(yellow, "bar0: %x ", hdr->bar0);
  77. printk(yellow, "bar1: %x\n", hdr->bar1);
  78. printk(yellow, "bar2: %x ", hdr->bar2);
  79. printk(yellow, "bar3: %x\n", hdr->bar3);
  80. printk(yellow, "bar4: %x ", hdr->bar4);
  81. printk(yellow, "bar5: %x\n", hdr->bar5);
  82. printk(yellow, "irq: %b\n", hdr->irqnr);
  83. */
  84. /* search in the vendor-dev table, and got the dev_init */
  85. errno_t (* dev_init) (struct pci_header0 *) = NULL;
  86. for (i = 0; ; i++) {
  87. if (pci_vendors[i].class == 0xff)
  88. break;
  89. if (pci_vendors[i].class == header.class_code &&
  90. pci_vendors[i].subclass == header.subclass) {
  91. dev_init = pci_vendors[i].dev_init;
  92. break;
  93. }
  94. }
  95. if (!dev_init) {
  96. printk(red, "module not found for vendor %w, device %w. \n",
  97. header.vendor, header.dev);
  98. return;
  99. }
  100. printk(yellow, "dev_init: %q\n", dev_init);
  101. dev_init(hdr);
  102. return;
  103. }