feat: partial PCIe support (MCFG table parsing)
[lunaix-os.git] / lunaix-os / hal / pci.c
1 #include <hal/acpi/acpi.h>
2 #include <hal/apic.h>
3 #include <hal/pci.h>
4 #include <lunaix/mm/kalloc.h>
5 #include <lunaix/spike.h>
6 #include <lunaix/syslog.h>
7
8 LOG_MODULE("PCI")
9
10 static struct llist_header pci_devices;
11
12 void
13 pci_probe_msi_info(struct pci_device* device);
14
15 void
16 pci_probe_device(int bus, int dev, int funct)
17 {
18     uint32_t base = PCI_ADDRESS(bus, dev, funct);
19     pci_reg_t reg1 = pci_read_cspace(base, 0);
20
21     // Vendor=0xffff则表示设备不存在
22     if (PCI_DEV_VENDOR(reg1) == PCI_VENDOR_INVLD) {
23         return;
24     }
25
26     pci_reg_t hdr_type = pci_read_cspace(base, 0xc);
27     hdr_type = (hdr_type >> 16) & 0xff;
28
29     // 防止堆栈溢出
30     // QEMU的ICH9/Q35实现似乎有点问题,对于多功能设备的每一个功能的header type
31     //  都将第七位置位。而virtualbox 就没有这个毛病。
32     if ((hdr_type & 0x80) && funct == 0) {
33         hdr_type = hdr_type & ~0x80;
34         // 探测多用途设备(multi-function device)
35         for (int i = 1; i < 7; i++) {
36             pci_probe_device(bus, dev, i);
37         }
38     }
39
40     if (hdr_type != PCI_TDEV) {
41         // XXX: 目前忽略所有桥接设备,比如PCI-PCI桥接器,或者是CardBus桥接器
42         return;
43     }
44
45     pci_reg_t intr = pci_read_cspace(base, 0x3c);
46     pci_reg_t class = pci_read_cspace(base, 0x8);
47
48     struct pci_device* device = lxmalloc(sizeof(struct pci_device));
49     *device = (struct pci_device){ .cspace_base = base,
50                                    .class_info = class,
51                                    .device_info = reg1,
52                                    .intr_info = intr };
53
54     pci_probe_msi_info(device);
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_probe_msi_info(struct pci_device* device)
73 {
74     // Note that Virtualbox have to use ICH9 chipset for MSI support.
75     // Qemu seems ok with default PIIX3, Bochs is pending to test...
76     //    See https://www.virtualbox.org/manual/ch03.html (section 3.5.1)
77     pci_reg_t status =
78       pci_read_cspace(device->cspace_base, PCI_REG_STATUS_CMD) >> 16;
79
80     if (!(status & 0x10)) {
81         device->msi_loc = 0;
82         return;
83     }
84
85     pci_reg_t cap_ptr = pci_read_cspace(device->cspace_base, 0x34) & 0xff;
86     uint32_t cap_hdr;
87
88     while (cap_ptr) {
89         cap_hdr = pci_read_cspace(device->cspace_base, cap_ptr);
90         if ((cap_hdr & 0xff) == 0x5) {
91             // MSI
92             device->msi_loc = cap_ptr;
93             return;
94         }
95         cap_ptr = (cap_hdr >> 8) & 0xff;
96     }
97 }
98
99 #define PCI_PRINT_BAR_LISTING
100
101 void
102 pci_print_device()
103 {
104     struct pci_device *pos, *n;
105     llist_for_each(pos, n, &pci_devices, dev_chain)
106     {
107         kprintf(KINFO "(B%xh:D%xh:F%xh) Dev %x:%x, Class 0x%x\n",
108                 PCI_BUS_NUM(pos->cspace_base),
109                 PCI_SLOT_NUM(pos->cspace_base),
110                 PCI_FUNCT_NUM(pos->cspace_base),
111                 PCI_DEV_VENDOR(pos->device_info),
112                 PCI_DEV_DEVID(pos->device_info),
113                 PCI_DEV_CLASS(pos->class_info));
114
115         kprintf(KINFO "\t IRQ: %d, INT#x: %d\n",
116                 PCI_INTR_IRQ(pos->intr_info),
117                 PCI_INTR_PIN(pos->intr_info));
118 #ifdef PCI_PRINT_BAR_LISTING
119         pci_reg_t bar;
120         for (size_t i = 1; i <= 6; i++) {
121             size_t size = pci_bar_sizing(pos, &bar, i);
122             if (!bar)
123                 continue;
124             if (PCI_BAR_MMIO(bar)) {
125                 kprintf(KINFO "\t BAR#%d (MMIO) %p [%d]\n",
126                         i,
127                         PCI_BAR_ADDR_MM(bar),
128                         size);
129             } else {
130                 kprintf(KINFO "\t BAR#%d (I/O) %p [%d]\n",
131                         i,
132                         PCI_BAR_ADDR_IO(bar),
133                         size);
134             }
135         }
136 #endif
137         if (pos->msi_loc) {
138             kprintf(KINFO "\t MSI supported (@%xh)\n", pos->msi_loc);
139         }
140     }
141 }
142
143 size_t
144 pci_bar_sizing(struct pci_device* dev, uint32_t* bar_out, uint32_t bar_num)
145 {
146     pci_reg_t bar = pci_read_cspace(dev->cspace_base, PCI_REG_BAR(bar_num));
147     if (!bar) {
148         *bar_out = 0;
149         return 0;
150     }
151
152     pci_write_cspace(dev->cspace_base, PCI_REG_BAR(bar_num), 0xffffffff);
153     pci_reg_t sized =
154       pci_read_cspace(dev->cspace_base, PCI_REG_BAR(bar_num)) & ~0x1;
155     if (PCI_BAR_MMIO(bar)) {
156         sized = PCI_BAR_ADDR_MM(sized);
157     }
158     *bar_out = bar;
159     return ~sized + 1;
160 }
161
162 void
163 pci_setup_msi(struct pci_device* device, int vector)
164 {
165     // Dest: APIC#0, Physical Destination, No redirection
166     uint32_t msi_addr = (__APIC_BASE_PADDR | 0x8);
167
168     // Edge trigger, Fixed delivery
169     uint32_t msi_data = vector;
170
171     pci_write_cspace(
172       device->cspace_base, PCI_MSI_ADDR(device->msi_loc), msi_addr);
173     pci_write_cspace(
174       device->cspace_base, PCI_MSI_DATA(device->msi_loc), msi_data & 0xffff);
175
176     pci_reg_t reg1 = pci_read_cspace(device->cspace_base, device->msi_loc);
177
178     // manipulate the MSI_CTRL to allow device using MSI to request service.
179     reg1 = ((((reg1 >> 16) & ~0x70) | 0x1) << 16) | (reg1 & 0xffff);
180     pci_write_cspace(device->cspace_base, device->msi_loc, reg1);
181 }
182
183 struct pci_device*
184 pci_get_device_by_id(uint16_t vendorId, uint16_t deviceId)
185 {
186     uint32_t dev_info = vendorId | (deviceId << 16);
187     struct pci_device *pos, *n;
188     llist_for_each(pos, n, &pci_devices, dev_chain)
189     {
190         if (pos->device_info == dev_info) {
191             return pos;
192         }
193     }
194
195     return NULL;
196 }
197
198 struct pci_device*
199 pci_get_device_by_class(uint32_t class)
200 {
201     struct pci_device *pos, *n;
202     llist_for_each(pos, n, &pci_devices, dev_chain)
203     {
204         if (PCI_DEV_CLASS(pos->class_info) == class) {
205             return pos;
206         }
207     }
208
209     return NULL;
210 }
211
212 void
213 pci_init()
214 {
215     llist_init_head(&pci_devices);
216     acpi_context* acpi = acpi_get_context();
217     assert_msg(acpi, "ACPI not initialized.");
218     if (acpi->mcfg.alloc_num) {
219         // PCIe Enhanced Configuration Mechanism is supported.
220         // TODO: support PCIe addressing mechanism
221     }
222     // Otherwise, fallback to use legacy PCI 3.0 method.
223     pci_probe();
224 }