feat: MSI capability detection.
[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_msi_info(struct pci_device* device);
11
12 void
13 pci_probe_device(int bus, int dev, int funct)
14 {
15     uint32_t base = PCI_ADDRESS(bus, dev, funct);
16     pci_reg_t reg1 = pci_read_cspace(base, 0);
17
18     // Vendor=0xffff则表示设备不存在
19     if (PCI_DEV_VENDOR(reg1) == PCI_VENDOR_INVLD) {
20         return;
21     }
22
23     pci_reg_t hdr_type = pci_read_cspace(base, 0xc);
24     hdr_type = (hdr_type >> 16) & 0xff;
25
26     if ((hdr_type & 0x80)) {
27         hdr_type = hdr_type & ~0x80;
28         // 探测多用途设备(multi-function device)
29         for (int i = 1; i < 7; i++) {
30             pci_probe_device(bus, dev, i);
31         }
32     }
33
34     if (hdr_type != 0) {
35         // XXX: 目前忽略所有桥接设备,比如PCI-PCI桥接器,或者是CardBus桥接器
36         return;
37     }
38
39     pci_reg_t intr = pci_read_cspace(base, 0x3c);
40     pci_reg_t class = pci_read_cspace(base, 0x8);
41
42     struct pci_device* device = lxmalloc(sizeof(struct pci_device));
43     *device = (struct pci_device){ .cspace_base = base,
44                                    .class_info = class,
45                                    .device_info = reg1,
46                                    .intr_info = intr };
47
48     pci_probe_msi_info(device);
49
50     llist_append(&pci_devices, &device->dev_chain);
51 }
52
53 void
54 pci_probe()
55 {
56     // 暴力扫描所有PCI设备
57     // XXX: 尽管最多会有256条PCI总线,但就目前而言,只考虑bus #0就足够了
58     for (int bus = 0; bus < 1; bus++) {
59         for (int dev = 0; dev < 32; dev++) {
60             pci_probe_device(bus, dev, 0);
61         }
62     }
63 }
64
65 void
66 pci_probe_msi_info(struct pci_device* device)
67 {
68     pci_reg_t status =
69       pci_read_cspace(device->cspace_base, PCI_REG_STATUS_CMD) >> 16;
70
71     if (!(status & 0x10)) {
72         device->msi_loc = 0;
73         return;
74     }
75
76     pci_reg_t cap_ptr = pci_read_cspace(device->cspace_base, 0x34) & 0xff;
77     uint32_t cap_hdr;
78
79     while (cap_ptr) {
80         cap_hdr = pci_read_cspace(device->cspace_base, cap_ptr);
81         if ((cap_hdr & 0xff) == 0x5) {
82             // MSI
83             device->msi_loc = cap_ptr;
84             return;
85         }
86         cap_ptr = (cap_hdr >> 8) & 0xff;
87     }
88 }
89
90 void
91 pci_print_device()
92 {
93     struct pci_device *pos, *n;
94     llist_for_each(pos, n, &pci_devices, dev_chain)
95     {
96         kprintf(KINFO "(B%xh:D%xh:F%xh) Dev %x:%x, Class 0x%x\n",
97                 PCI_BUS_NUM(pos->cspace_base),
98                 PCI_SLOT_NUM(pos->cspace_base),
99                 PCI_FUNCT_NUM(pos->cspace_base),
100                 PCI_DEV_VENDOR(pos->device_info),
101                 PCI_DEV_DEVID(pos->device_info),
102                 PCI_DEV_CLASS(pos->class_info));
103
104         kprintf(KINFO "\t IRQ: %d, INT#x: %d\n",
105                 PCI_INTR_IRQ(pos->intr_info),
106                 PCI_INTR_PIN(pos->intr_info));
107
108         if (pos->msi_loc) {
109             kprintf(KINFO "\t MSI supported (@%xh)\n", pos->msi_loc);
110         }
111     }
112 }
113
114 struct pci_device*
115 pci_get_device_by_id(uint16_t vendorId, uint16_t deviceId)
116 {
117     uint32_t dev_info = vendorId | (deviceId << 16);
118     struct pci_device *pos, *n;
119     llist_for_each(pos, n, &pci_devices, dev_chain)
120     {
121         if (pos->device_info == dev_info) {
122             return pos;
123         }
124     }
125
126     return NULL;
127 }
128
129 struct pci_device*
130 pci_get_device_by_class(uint32_t class)
131 {
132     struct pci_device *pos, *n;
133     llist_for_each(pos, n, &pci_devices, dev_chain)
134     {
135         if (PCI_DEV_CLASS(pos->class_info) == class) {
136             return pos;
137         }
138     }
139
140     return NULL;
141 }
142
143 void
144 pci_init()
145 {
146     llist_init_head(&pci_devices);
147     pci_probe();
148 }