2 #include <lunaix/mm/kalloc.h>
3 #include <lunaix/syslog.h>
7 static struct llist_header pci_devices;
10 pci_probe_device(int bus, int dev, int funct)
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;
16 // Vendor=0xffff则表示设备不存在
17 if (vendor == PCI_VENDOR_INVLD) {
21 pci_reg_t hdr_type = pci_read_cspace(bus, dev, funct, 3);
22 hdr_type = (hdr_type >> 16) & 0xff;
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);
33 // XXX: 目前忽略所有桥接设备,比如PCI-PCI桥接器,或者是CardBus桥接器
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;
40 struct pci_device* device = lxmalloc(sizeof(struct pci_device));
41 *device = (struct pci_device){ .bus = bus,
48 .intr_line = intr & 0xff,
49 .intr_pintype = (intr >> 8) & 0xff };
52 for (int i = 0; i < 6; i++) {
53 device->bars[i] = pci_read_cspace(bus, dev, funct, 4 + i);
56 llist_append(&pci_devices, &device->dev_chain);
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);
74 struct pci_device *pos, *n;
75 llist_for_each(pos, n, &pci_devices, dev_chain)
77 kprintf(KINFO "(B%xh:D%xh:F%xh) Dev %x:%x, Class 0x%x\n",
85 for (int i = 0; i < 6; i++) {
86 kprintf(KINFO "\t BAR#%d: %p\n", i, pos->bars[i]);
89 KINFO "\t IRQ: %d, INT#x: %d\n\n", pos->intr_line, pos->intr_pintype);
94 pci_get_device(uint16_t vendorId, uint16_t deviceId)
96 struct pci_device *pos, *n;
97 llist_for_each(pos, n, &pci_devices, dev_chain)
99 if (pos->vendor == vendorId && pos->deviceId == deviceId) {
110 llist_init_head(&pci_devices);