3 * @author Lunaixsky (zelong56@gmail.com)
4 * @brief A software implementation of PCI Local Bus Specification Revision 3.0
8 * @copyright Copyright (c) 2022
11 #include <hal/acpi/acpi.h>
14 #include <lunaix/fs/twifs.h>
15 #include <lunaix/mm/valloc.h>
16 #include <lunaix/spike.h>
17 #include <lunaix/syslog.h>
21 static struct llist_header pci_devices;
24 pci_probe_msi_info(struct pci_device* device);
27 pci_probe_device(int bus, int dev, int funct)
29 uint32_t base = PCI_ADDRESS(bus, dev, funct);
30 pci_reg_t reg1 = pci_read_cspace(base, 0);
32 // Vendor=0xffff则表示设备不存在
33 if (PCI_DEV_VENDOR(reg1) == PCI_VENDOR_INVLD) {
37 pci_reg_t hdr_type = pci_read_cspace(base, 0xc);
38 hdr_type = (hdr_type >> 16) & 0xff;
41 // QEMU的ICH9/Q35实现似乎有点问题,对于多功能设备的每一个功能的header type
42 // 都将第七位置位。而virtualbox 就没有这个毛病。
43 if ((hdr_type & 0x80) && funct == 0) {
44 hdr_type = hdr_type & ~0x80;
45 // 探测多用途设备(multi-function device)
46 for (int i = 1; i < 7; i++) {
47 pci_probe_device(bus, dev, i);
51 if (hdr_type != PCI_TDEV) {
52 // XXX: 目前忽略所有桥接设备,比如PCI-PCI桥接器,或者是CardBus桥接器
56 pci_reg_t intr = pci_read_cspace(base, 0x3c);
57 pci_reg_t class = pci_read_cspace(base, 0x8);
59 struct pci_device* device = vzalloc(sizeof(struct pci_device));
60 *device = (struct pci_device){ .cspace_base = base,
65 pci_probe_msi_info(device);
66 pci_probe_bar_info(device);
68 llist_append(&pci_devices, &device->dev_chain);
75 // XXX: 尽管最多会有256条PCI总线,但就目前而言,只考虑bus #0就足够了
76 for (int bus = 0; bus < 1; bus++) {
77 for (int dev = 0; dev < 32; dev++) {
78 pci_probe_device(bus, dev, 0);
84 pci_probe_bar_info(struct pci_device* device)
87 struct pci_base_addr* ba;
88 for (size_t i = 0; i < 6; i++) {
90 ba->size = pci_bar_sizing(device, &bar, i + 1);
91 if (PCI_BAR_MMIO(bar)) {
92 ba->start = PCI_BAR_ADDR_MM(bar);
93 ba->type |= PCI_BAR_CACHEABLE(bar) ? BAR_TYPE_CACHABLE : 0;
94 ba->type |= BAR_TYPE_MMIO;
96 ba->start = PCI_BAR_ADDR_IO(bar);
102 pci_probe_msi_info(struct pci_device* device)
104 // Note that Virtualbox have to use ICH9 chipset for MSI support.
105 // Qemu seems ok with default PIIX3, Bochs is pending to test...
106 // See https://www.virtualbox.org/manual/ch03.html (section 3.5.1)
108 pci_read_cspace(device->cspace_base, PCI_REG_STATUS_CMD) >> 16;
110 if (!(status & 0x10)) {
115 pci_reg_t cap_ptr = pci_read_cspace(device->cspace_base, 0x34) & 0xff;
119 cap_hdr = pci_read_cspace(device->cspace_base, cap_ptr);
120 if ((cap_hdr & 0xff) == 0x5) {
122 device->msi_loc = cap_ptr;
125 cap_ptr = (cap_hdr >> 8) & 0xff;
130 __pci_read_cspace(struct twimap* map)
132 struct pci_device* pcidev = (struct pci_device*)(map->data);
134 for (size_t i = 0; i < 256; i += sizeof(pci_reg_t)) {
135 *(pci_reg_t*)(map->buffer + i) =
136 pci_read_cspace(pcidev->cspace_base, i);
143 __pci_read_revid(struct twimap* map)
145 int class = twimap_data(map, struct pci_device*)->class_info;
146 twimap_printf(map, "0x%x", PCI_DEV_REV(class));
150 __pci_read_class(struct twimap* map)
152 int class = twimap_data(map, struct pci_device*)->class_info;
153 twimap_printf(map, "0x%x", PCI_DEV_CLASS(class));
157 __pci_bar_read(struct twimap* map)
159 struct pci_device* pcidev = twimap_data(map, struct pci_device*);
160 int bar_index = twimap_index(map, int);
162 struct pci_base_addr* bar = &pcidev->bar[bar_index];
164 if (!bar->start && !bar->size) {
165 twimap_printf(map, "[%d] not present \n", bar_index);
170 map, "[%d] base=%.8p, size=%.8p, ", bar_index, bar->start, bar->size);
172 if ((bar->type & BAR_TYPE_MMIO)) {
173 twimap_printf(map, "mmio");
174 if ((bar->type & BAR_TYPE_CACHABLE)) {
175 twimap_printf(map, ", prefetchable");
178 twimap_printf(map, "io");
181 twimap_printf(map, "\n");
185 __pci_bar_gonext(struct twimap* map)
187 if (twimap_index(map, int) >= 5) {
195 pci_build_fsmapping()
197 struct twifs_node *pci_class = twifs_dir_node(NULL, "pci"), *pci_dev;
198 struct pci_device *pos, *n;
200 llist_for_each(pos, n, &pci_devices, dev_chain)
202 pci_dev = twifs_dir_node(pci_class,
203 "%.2d:%.2d:%.2d.%.4x:%.4x",
204 PCI_BUS_NUM(pos->cspace_base),
205 PCI_SLOT_NUM(pos->cspace_base),
206 PCI_FUNCT_NUM(pos->cspace_base),
207 PCI_DEV_VENDOR(pos->device_info),
208 PCI_DEV_DEVID(pos->device_info));
210 map = twifs_mapping(pci_dev, pos, "config");
211 map->read = __pci_read_cspace;
213 map = twifs_mapping(pci_dev, pos, "revision");
214 map->read = __pci_read_revid;
216 map = twifs_mapping(pci_dev, pos, "class");
217 map->read = __pci_read_class;
219 map = twifs_mapping(pci_dev, pos, "io_bases");
220 map->read = __pci_bar_read;
221 map->go_next = __pci_bar_gonext;
226 pci_bar_sizing(struct pci_device* dev, uint32_t* bar_out, uint32_t bar_num)
228 pci_reg_t bar = pci_read_cspace(dev->cspace_base, PCI_REG_BAR(bar_num));
234 pci_write_cspace(dev->cspace_base, PCI_REG_BAR(bar_num), 0xffffffff);
236 pci_read_cspace(dev->cspace_base, PCI_REG_BAR(bar_num)) & ~0x1;
237 if (PCI_BAR_MMIO(bar)) {
238 sized = PCI_BAR_ADDR_MM(sized);
241 pci_write_cspace(dev->cspace_base, PCI_REG_BAR(bar_num), bar);
246 pci_setup_msi(struct pci_device* device, int vector)
248 // Dest: APIC#0, Physical Destination, No redirection
249 uint32_t msi_addr = (__APIC_BASE_PADDR);
251 // Edge trigger, Fixed delivery
252 uint32_t msi_data = vector;
255 device->cspace_base, PCI_MSI_ADDR(device->msi_loc), msi_addr);
257 pci_reg_t reg1 = pci_read_cspace(device->cspace_base, device->msi_loc);
258 pci_reg_t msg_ctl = reg1 >> 16;
260 int offset = !!(msg_ctl & MSI_CAP_64BIT) * 4;
261 pci_write_cspace(device->cspace_base,
262 PCI_MSI_DATA(device->msi_loc, offset),
265 if ((msg_ctl & MSI_CAP_MASK)) {
267 device->cspace_base, PCI_MSI_MASK(device->msi_loc, offset), 0);
270 // manipulate the MSI_CTRL to allow device using MSI to request service.
271 reg1 = (reg1 & 0xff8fffff) | 0x10000;
272 pci_write_cspace(device->cspace_base, device->msi_loc, reg1);
276 pci_get_device_by_id(uint16_t vendorId, uint16_t deviceId)
278 uint32_t dev_info = vendorId | (deviceId << 16);
279 struct pci_device *pos, *n;
280 llist_for_each(pos, n, &pci_devices, dev_chain)
282 if (pos->device_info == dev_info) {
291 pci_get_device_by_class(uint32_t class)
293 struct pci_device *pos, *n;
294 llist_for_each(pos, n, &pci_devices, dev_chain)
296 if (PCI_DEV_CLASS(pos->class_info) == class) {
307 llist_init_head(&pci_devices);
308 acpi_context* acpi = acpi_get_context();
309 assert_msg(acpi, "ACPI not initialized.");
310 if (acpi->mcfg.alloc_num) {
311 // PCIe Enhanced Configuration Mechanism is supported.
312 // TODO: support PCIe addressing mechanism
314 // Otherwise, fallback to use legacy PCI 3.0 method.
317 pci_build_fsmapping();