pci.c 2.8 KB

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