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