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 kprintf("dev.%d:%d:%d %x:%x\n",
72 pci_probe_msi_info(device);
73 pci_probe_bar_info(device);
75 llist_append(&pci_devices, &device->dev_chain);
82 // XXX: 尽管最多会有256条PCI总线,但就目前而言,只考虑bus #0就足够了
83 for (int bus = 0; bus < 256; bus++) {
84 for (int dev = 0; dev < 32; dev++) {
85 pci_probe_device(bus, dev, 0);
91 pci_probe_bar_info(struct pci_device* device)
94 struct pci_base_addr* ba;
95 for (size_t i = 0; i < 6; i++) {
97 ba->size = pci_bar_sizing(device, &bar, i + 1);
98 if (PCI_BAR_MMIO(bar)) {
99 ba->start = PCI_BAR_ADDR_MM(bar);
100 ba->type |= PCI_BAR_CACHEABLE(bar) ? BAR_TYPE_CACHABLE : 0;
101 ba->type |= BAR_TYPE_MMIO;
103 ba->start = PCI_BAR_ADDR_IO(bar);
109 pci_probe_msi_info(struct pci_device* device)
111 // Note that Virtualbox have to use ICH9 chipset for MSI support.
112 // Qemu seems ok with default PIIX3, Bochs is pending to test...
113 // See https://www.virtualbox.org/manual/ch03.html (section 3.5.1)
115 pci_read_cspace(device->cspace_base, PCI_REG_STATUS_CMD) >> 16;
117 if (!(status & 0x10)) {
122 pci_reg_t cap_ptr = pci_read_cspace(device->cspace_base, 0x34) & 0xff;
126 cap_hdr = pci_read_cspace(device->cspace_base, cap_ptr);
127 if ((cap_hdr & 0xff) == 0x5) {
129 device->msi_loc = cap_ptr;
132 cap_ptr = (cap_hdr >> 8) & 0xff;
137 __pci_read_cspace(struct twimap* map)
139 struct pci_device* pcidev = (struct pci_device*)(map->data);
141 for (size_t i = 0; i < 256; i += sizeof(pci_reg_t)) {
142 *(pci_reg_t*)(map->buffer + i) =
143 pci_read_cspace(pcidev->cspace_base, i);
150 __pci_read_revid(struct twimap* map)
152 int class = twimap_data(map, struct pci_device*)->class_info;
153 twimap_printf(map, "0x%x", PCI_DEV_REV(class));
157 __pci_read_class(struct twimap* map)
159 int class = twimap_data(map, struct pci_device*)->class_info;
160 twimap_printf(map, "0x%x", PCI_DEV_CLASS(class));
164 __pci_bar_read(struct twimap* map)
166 struct pci_device* pcidev = twimap_data(map, struct pci_device*);
167 int bar_index = twimap_index(map, int);
169 struct pci_base_addr* bar = &pcidev->bar[bar_index];
171 if (!bar->start && !bar->size) {
172 twimap_printf(map, "[%d] not present \n", bar_index);
177 map, "[%d] base=%.8p, size=%.8p, ", bar_index, bar->start, bar->size);
179 if ((bar->type & BAR_TYPE_MMIO)) {
180 twimap_printf(map, "mmio");
181 if ((bar->type & BAR_TYPE_CACHABLE)) {
182 twimap_printf(map, ", prefetchable");
185 twimap_printf(map, "io");
188 twimap_printf(map, "\n");
192 __pci_bar_gonext(struct twimap* map)
194 if (twimap_index(map, int) >= 5) {
202 pci_build_fsmapping()
204 struct twifs_node *pci_class = twifs_dir_node(NULL, "pci"), *pci_dev;
205 struct pci_device *pos, *n;
207 llist_for_each(pos, n, &pci_devices, dev_chain)
209 pci_dev = twifs_dir_node(pci_class,
210 "%.2d:%.2d:%.2d.%.4x:%.4x",
211 PCI_BUS_NUM(pos->cspace_base),
212 PCI_SLOT_NUM(pos->cspace_base),
213 PCI_FUNCT_NUM(pos->cspace_base),
214 PCI_DEV_VENDOR(pos->device_info),
215 PCI_DEV_DEVID(pos->device_info));
217 map = twifs_mapping(pci_dev, pos, "config");
218 map->read = __pci_read_cspace;
220 map = twifs_mapping(pci_dev, pos, "revision");
221 map->read = __pci_read_revid;
223 map = twifs_mapping(pci_dev, pos, "class");
224 map->read = __pci_read_class;
226 map = twifs_mapping(pci_dev, pos, "io_bases");
227 map->read = __pci_bar_read;
228 map->go_next = __pci_bar_gonext;
233 pci_bar_sizing(struct pci_device* dev, uint32_t* bar_out, uint32_t bar_num)
235 pci_reg_t bar = pci_read_cspace(dev->cspace_base, PCI_REG_BAR(bar_num));
241 pci_write_cspace(dev->cspace_base, PCI_REG_BAR(bar_num), 0xffffffff);
243 pci_read_cspace(dev->cspace_base, PCI_REG_BAR(bar_num)) & ~0x1;
244 if (PCI_BAR_MMIO(bar)) {
245 sized = PCI_BAR_ADDR_MM(sized);
248 pci_write_cspace(dev->cspace_base, PCI_REG_BAR(bar_num), bar);
253 pci_setup_msi(struct pci_device* device, int vector)
255 // Dest: APIC#0, Physical Destination, No redirection
256 uint32_t msi_addr = (__APIC_BASE_PADDR);
258 // Edge trigger, Fixed delivery
259 uint32_t msi_data = vector;
262 device->cspace_base, PCI_MSI_ADDR(device->msi_loc), msi_addr);
264 pci_reg_t reg1 = pci_read_cspace(device->cspace_base, device->msi_loc);
265 pci_reg_t msg_ctl = reg1 >> 16;
267 int offset = !!(msg_ctl & MSI_CAP_64BIT) * 4;
268 pci_write_cspace(device->cspace_base,
269 PCI_MSI_DATA(device->msi_loc, offset),
272 if ((msg_ctl & MSI_CAP_MASK)) {
274 device->cspace_base, PCI_MSI_MASK(device->msi_loc, offset), 0);
277 // manipulate the MSI_CTRL to allow device using MSI to request service.
278 reg1 = (reg1 & 0xff8fffff) | 0x10000;
279 pci_write_cspace(device->cspace_base, device->msi_loc, reg1);
283 pci_get_device_by_id(uint16_t vendorId, uint16_t deviceId)
285 uint32_t dev_info = vendorId | (deviceId << 16);
286 struct pci_device *pos, *n;
287 llist_for_each(pos, n, &pci_devices, dev_chain)
289 if (pos->device_info == dev_info) {
298 pci_get_device_by_class(uint32_t class)
300 struct pci_device *pos, *n;
301 llist_for_each(pos, n, &pci_devices, dev_chain)
303 if (PCI_DEV_CLASS(pos->class_info) == class) {
314 llist_init_head(&pci_devices);
315 acpi_context* acpi = acpi_get_context();
316 assert_msg(acpi, "ACPI not initialized.");
317 if (acpi->mcfg.alloc_num) {
318 // PCIe Enhanced Configuration Mechanism is supported.
319 // TODO: support PCIe addressing mechanism
321 // Otherwise, fallback to use legacy PCI 3.0 method.
324 pci_build_fsmapping();