feat: PCI bus probing
[lunaix-os.git] / lunaix-os / hal / pci.c
1 #include <hal/pci.h>
2 #include <lunaix/mm/kalloc.h>
3 #include <lunaix/syslog.h>
4
5 LOG_MODULE("PCI")
6
7 static struct llist_header pci_devices;
8
9 void
10 pci_probe_device(int bus, int dev, int funct)
11 {
12     pci_reg_t reg1 = pci_read_cspace(bus, dev, funct, 0);
13     uint32_t vendor = reg1 & 0xffff;
14     pci_reg_t dev_id = reg1 >> 16;
15
16     // Vendor=0xffff则表示设备不存在
17     if (vendor == PCI_VENDOR_INVLD) {
18         return;
19     }
20
21     pci_reg_t hdr_type = pci_read_cspace(bus, dev, funct, 3);
22     hdr_type = (hdr_type >> 16) & 0xff;
23
24     if ((hdr_type & 0x80)) {
25         hdr_type = hdr_type & ~0x80;
26         // 探测多用途设备(multi-function device)
27         for (int i = 1; i < 7; i++) {
28             pci_probe_device(bus, dev, i);
29         }
30     }
31
32     if (hdr_type != 0) {
33         // XXX: 目前忽略所有桥接设备,比如PCI-PCI桥接器,或者是CardBus桥接器
34         return;
35     }
36
37     pci_reg_t intr = pci_read_cspace(bus, dev, funct, 15);
38     pci_reg_t class = pci_read_cspace(bus, dev, funct, 2) >> 8;
39
40     struct pci_device* device = lxmalloc(sizeof(struct pci_device));
41     *device = (struct pci_device){ .bus = bus,
42                                    .dev = dev,
43                                    .function = funct,
44                                    .class_code = class,
45                                    .vendor = vendor,
46                                    .deviceId = dev_id,
47                                    .type = hdr_type,
48                                    .intr_line = intr & 0xff,
49                                    .intr_pintype = (intr >> 8) & 0xff };
50
51     // 读取设备的内存映射的寄存器的基地址
52     for (int i = 0; i < 6; i++) {
53         device->bars[i] = pci_read_cspace(bus, dev, funct, 4 + i);
54     }
55
56     llist_append(&pci_devices, &device->dev_chain);
57 }
58
59 void
60 pci_probe()
61 {
62     // 暴力扫描所有PCI设备
63     // XXX: 尽管最多会有256条PCI总线,但就目前而言,只考虑bus #0就足够了
64     for (int bus = 0; bus < 1; bus++) {
65         for (int dev = 0; dev < 32; dev++) {
66             pci_probe_device(bus, dev, 0);
67         }
68     }
69 }
70
71 void
72 pci_print_device()
73 {
74     struct pci_device *pos, *n;
75     llist_for_each(pos, n, &pci_devices, dev_chain)
76     {
77         kprintf(KINFO "(B%xh:D%xh:F%xh) Dev %x:%x, Class 0x%x\n",
78                 pos->bus,
79                 pos->dev,
80                 pos->function,
81                 pos->vendor,
82                 pos->deviceId,
83                 pos->class_code);
84
85         for (int i = 0; i < 6; i++) {
86             kprintf(KINFO "\t BAR#%d: %p\n", i, pos->bars[i]);
87         }
88         kprintf(
89           KINFO "\t IRQ: %d, INT#x: %d\n\n", pos->intr_line, pos->intr_pintype);
90     }
91 }
92
93 struct pci_device*
94 pci_get_device(uint16_t vendorId, uint16_t deviceId)
95 {
96     struct pci_device *pos, *n;
97     llist_for_each(pos, n, &pci_devices, dev_chain)
98     {
99         if (pos->vendor == vendorId && pos->deviceId == deviceId) {
100             return pos;
101         }
102     }
103
104     return NULL;
105 }
106
107 void
108 pci_init()
109 {
110     llist_init_head(&pci_devices);
111     pci_probe();
112 }