3 * @author Lunaixsky (zelong56@gmail.com)
4 * @brief A software implementation of PCI Local Bus Specification Revision 3.0
8 * @copyright Copyright (c) 2022
13 #include <klibc/string.h>
14 #include <lunaix/fs/twifs.h>
15 #include <lunaix/mm/valloc.h>
16 #include <lunaix/spike.h>
17 #include <lunaix/syslog.h>
22 device instance for pci bridge,
23 currently, lunaix only support one bridge controller.
25 static struct device* pci_bridge;
27 static morph_t* pci_probers;
28 static DECLARE_HASHTABLE(pci_drivers, 8);
31 pci_log_device(struct pci_probe* probe)
33 pciaddr_t loc = probe->loc;
35 kprintf("pci.%03d:%02d:%02d, class=%p, vendor:dev=%04x:%04x",
40 PCI_DEV_VENDOR(probe->device_info),
41 PCI_DEV_DEVID(probe->device_info));
45 __pci_probe_msi_info(struct pci_probe* probe)
47 // Note that Virtualbox have to use ICH9 chipset for MSI support.
48 // Qemu seems ok with default PIIX3, Bochs is pending to test...
49 // See https://www.virtualbox.org/manual/ch03.html (section 3.5.1)
51 pci_read_cspace(probe->cspace_base, PCI_REG_STATUS_CMD) >> 16;
53 if (!(status & 0x10)) {
58 pci_reg_t cap_ptr = pci_read_cspace(probe->cspace_base, 0x34) & 0xff;
62 cap_hdr = pci_read_cspace(probe->cspace_base, cap_ptr);
63 if ((cap_hdr & 0xff) == 0x5) {
65 probe->msi_loc = cap_ptr;
68 cap_ptr = (cap_hdr >> 8) & 0xff;
74 __pci_probe_bar_info(struct pci_probe* probe)
77 struct pci_base_addr* ba;
78 for (size_t i = 0; i < PCI_BAR_COUNT; i++) {
80 ba->size = pci_bar_sizing(probe, &bar, i + 1);
81 if (PCI_BAR_MMIO(bar)) {
82 ba->start = PCI_BAR_ADDR_MM(bar);
83 ba->type |= PCI_BAR_CACHEABLE(bar) ? BAR_TYPE_CACHABLE : 0;
84 ba->type |= BAR_TYPE_MMIO;
86 ba->start = PCI_BAR_ADDR_IO(bar);
92 __pci_add_prober(pciaddr_t loc, ptr_t pci_base, int devinfo)
94 struct pci_probe* prober;
97 pci_reg_t class = pci_read_cspace(pci_base, 0x8);
99 u32_t devid = PCI_DEV_DEVID(devinfo);
100 u32_t vendor = PCI_DEV_VENDOR(devinfo);
101 pci_reg_t intr = pci_read_cspace(pci_base, 0x3c);
103 prober = vzalloc(sizeof(*prober));
105 prober->class_info = class;
106 prober->device_info = devinfo;
107 prober->cspace_base = pci_base;
108 prober->intr_info = intr;
110 prober->irq_domain = irq_get_domain(pci_bridge);
112 changeling_morph_anon(pci_probers, prober->mobj, pci_probe_morpher);
114 __pci_probe_msi_info(prober);
115 __pci_probe_bar_info(prober);
117 pci_log_device(prober);
121 __pci_bind(struct pci_registry* reg, struct pci_probe* probe)
124 struct device_def* devdef;
130 if (!reg->check_compact(probe)) {
134 devdef = reg->definition;
135 errno = devdef->create(devdef, &probe->mobj);
138 ERROR("pci_loc:%x, bind (%xh:%xh.%d) failed, e=%d",
140 devdef->class.fn_grp,
141 devdef->class.device,
142 devdef->class.variant,
150 pci_bind_driver(struct pci_registry* reg)
152 struct pci_probe* probe;
156 changeling_for_each(pos, n, pci_probers)
158 probe = changeling_reveal(pos, pci_probe_morpher);
160 errno = __pci_bind(reg, probe);
170 pci_probe_device(pciaddr_t pci_loc)
172 u32_t base = PCI_CFGADDR(pci_loc);
173 pci_reg_t reg1 = pci_read_cspace(base, 0);
175 // Vendor=0xffff则表示设备不存在
176 if (PCI_DEV_VENDOR(reg1) == PCI_VENDOR_INVLD) {
180 pci_reg_t hdr_type = pci_read_cspace(base, 0xc);
181 hdr_type = (hdr_type >> 16) & 0xff;
184 // QEMU的ICH9/Q35实现似乎有点问题,对于多功能设备的每一个功能的header type
185 // 都将第七位置位。而virtualbox 就没有这个毛病。
186 if ((hdr_type & 0x80) && PCILOC_FN(pci_loc) == 0) {
187 hdr_type = hdr_type & ~0x80;
188 // 探测多用途设备(multi-function device)
189 for (int i = 1; i < 7; i++) {
190 pci_probe_device(pci_loc + i);
194 struct pci_probe* prober;
196 changeling_for_each(pos, n, pci_probers)
198 prober = changeling_reveal(pos, pci_probe_morpher);
199 if (prober->loc == pci_loc) {
200 pci_log_device(prober);
205 __pci_add_prober(pci_loc, base, reg1);
208 static struct pci_registry*
209 __pci_registry_get(struct device_def* def)
211 struct pci_registry *pos, *n;
214 hash = hash_32(__ptr(def), HSTR_FULL_HASH);
215 hashtable_hash_foreach(pci_drivers, hash, pos, n, entries)
217 if (pos->definition == def) {
226 __pci_proxied_devdef_load(struct device_def* def)
228 struct pci_registry* reg;
231 reg = __pci_registry_get(def);
234 return pci_bind_driver(reg);
238 pci_register_driver(struct device_def* def, pci_id_checker_t checker)
240 struct pci_registry* reg;
247 if (__pci_registry_get(def)) {
251 reg = valloc(sizeof(*reg));
253 *reg = (struct pci_registry) {
254 .check_compact = checker,
258 device_chain_loader(def, __pci_proxied_devdef_load);
260 hash = hash_32(__ptr(def), HSTR_FULL_HASH);
261 hashtable_hash_in(pci_drivers, ®->entries, hash);
269 for (u32_t loc = 0; loc < (pciaddr_t)-1; loc += 8) {
270 pci_probe_device((pciaddr_t)loc);
275 __pci_config_msi(struct pci_probe* probe, irq_t irq)
277 // PCI LB Spec. (Rev 3) Section 6.8 & 6.8.1
279 ptr_t msi_addr = irq->msi->wr_addr;
280 u32_t msi_data = irq->msi->message;
282 pci_reg_t reg1 = pci_read_cspace(probe->cspace_base, probe->msi_loc);
283 pci_reg_t msg_ctl = reg1 >> 16;
284 int offset_cap64 = !!(msg_ctl & MSI_CAP_64BIT) * 4;
286 pci_write_cspace(probe->cspace_base,
287 PCI_MSI_ADDR_LO(probe->msi_loc),
291 pci_write_cspace(probe->cspace_base,
292 PCI_MSI_ADDR_HI(probe->msi_loc),
293 (u64_t)msi_addr >> 32);
296 pci_write_cspace(probe->cspace_base,
297 PCI_MSI_DATA(probe->msi_loc, offset_cap64),
300 if ((msg_ctl & MSI_CAP_MASK)) {
302 probe->cspace_base, PCI_MSI_MASK(probe->msi_loc, offset_cap64), 0);
305 // manipulate the MSI_CTRL to allow device using MSI to request service.
306 reg1 = (reg1 & 0xff8fffff) | 0x10000;
307 pci_write_cspace(probe->cspace_base, probe->msi_loc, reg1);
311 pci_declare_msi_irq(irq_servant callback,
312 struct pci_probe* probe, void *irq_extra)
314 return irq_declare_msg(callback, probe->loc, probe->loc, irq_extra);
318 pci_assign_msi(struct pci_probe* probe, irq_t irq)
322 assert(irq->type == IRQ_MESSAGE);
324 err = irq_assign(probe->irq_domain, irq);
329 __pci_config_msi(probe, irq);
334 pci_bar_sizing(struct pci_probe* probe, u32_t* bar_out, u32_t bar_num)
336 pci_reg_t sized, bar;
338 bar = pci_read_cspace(probe->cspace_base, PCI_REG_BAR(bar_num));
344 pci_write_cspace(probe->cspace_base, PCI_REG_BAR(bar_num), 0xffffffff);
347 pci_read_cspace(probe->cspace_base, PCI_REG_BAR(bar_num)) & ~0x1;
349 if (PCI_BAR_MMIO(bar)) {
350 sized = PCI_BAR_ADDR_MM(sized);
354 pci_write_cspace(probe->cspace_base, PCI_REG_BAR(bar_num), bar);
360 pci_apply_command(struct pci_probe* probe, pci_reg_t cmd)
365 base = probe->cspace_base;
366 rcmd = pci_read_cspace(base, PCI_REG_STATUS_CMD);
369 rcmd = (rcmd & 0xffff0000) | cmd;
371 pci_write_cspace(base, PCI_REG_STATUS_CMD, rcmd);
375 __pci_read_cspace(struct twimap* map)
377 struct pci_probe* probe;
379 probe = twimap_data(map, struct pci_probe*);
381 for (size_t i = 0; i < 256; i += sizeof(pci_reg_t)) {
382 *(pci_reg_t*)(map->buffer + i) =
383 pci_read_cspace(probe->cspace_base, i);
389 /*---------- TwiFS interface definition ----------*/
392 __pci_read_revid(struct twimap* map)
394 struct pci_probe* probe;
396 probe = twimap_data(map, struct pci_probe*);
397 twimap_printf(map, "0x%x", PCI_DEV_REV(probe->class_info));
401 __pci_read_class(struct twimap* map)
403 struct pci_probe* probe;
405 probe = twimap_data(map, struct pci_probe*);
406 twimap_printf(map, "0x%x", PCI_DEV_CLASS(probe->class_info));
410 __pci_read_devinfo(struct twimap* map)
412 struct pci_probe* probe;
414 probe = twimap_data(map, struct pci_probe*);
415 twimap_printf(map, "%x:%x",
416 PCI_DEV_VENDOR(probe->device_info),
417 PCI_DEV_DEVID(probe->device_info));
421 __pci_bar_read(struct twimap* map)
423 struct pci_probe* probe;
426 probe = twimap_data(map, struct pci_probe*);
427 bar_index = twimap_index(map, int);
429 struct pci_base_addr* bar = &probe->bar[bar_index];
431 if (!bar->start && !bar->size) {
432 twimap_printf(map, "[%d] not present \n", bar_index);
437 map, "[%d] base=%.8p, size=%.8p, ", bar_index, bar->start, bar->size);
439 if ((bar->type & BAR_TYPE_MMIO)) {
440 twimap_printf(map, "mmio");
441 if ((bar->type & BAR_TYPE_CACHABLE)) {
442 twimap_printf(map, ", prefetchable");
445 twimap_printf(map, "io");
448 twimap_printf(map, "\n");
452 __pci_bar_gonext(struct twimap* map)
454 if (twimap_index(map, int) >= 5) {
462 __pci_read_binding(struct twimap* map)
464 struct pci_probe* probe;
465 struct devident* devid;
467 probe = twimap_data(map, struct pci_probe*);
472 devid = &probe->bind->ident;
474 twimap_printf(map, "%xh:%xh.%d",
476 DEV_KIND_FROM(devid->unique),
477 DEV_VAR_FROM(devid->unique));
481 __pci_trigger_bus_rescan(struct twimap* map)
487 pci_build_fsmapping()
489 struct twifs_node *pci_class = twifs_dir_node(NULL, "pci"), *pci_dev;
491 struct pci_probe* probe;
494 // TODO bus rescan is not ready yet
495 // map = twifs_mapping(pci_class, NULL, "rescan");
496 // map->read = __pci_trigger_bus_rescan;
498 changeling_for_each(pos, n, pci_probers)
500 probe = changeling_reveal(pos, pci_probe_morpher);
501 pci_dev = twifs_dir_node(pci_class, "%x", probe->loc);
503 map = twifs_mapping(pci_dev, probe, "config");
504 map->read = __pci_read_cspace;
506 map = twifs_mapping(pci_dev, probe, "revision");
507 map->read = __pci_read_revid;
509 map = twifs_mapping(pci_dev, probe, "class");
510 map->read = __pci_read_class;
512 map = twifs_mapping(pci_dev, probe, "binding");
513 map->read = __pci_read_binding;
515 map = twifs_mapping(pci_dev, probe, "io_bases");
516 map->read = __pci_bar_read;
517 map->go_next = __pci_bar_gonext;
520 EXPORT_TWIFS_PLUGIN(pci_devs, pci_build_fsmapping);
522 /*---------- PCI 3.0 HBA device definition ----------*/
525 __pci_irq_install(struct irq_domain* domain, irq_t irq)
527 struct irq_domain* parent;
530 parent = domain->parent;
531 err = parent->ops->install_irq(parent, irq);
536 if (irq->type == IRQ_MESSAGE) {
537 irq->msi->message = irq->vector;
543 static struct irq_domain_ops pci_irq_ops = {
544 .install_irq = __pci_irq_install
548 pci_register(struct device_def* def)
550 pci_probers = changeling_spawn(NULL, "pci_realm");
556 pci_create(struct device_def* def, morph_t* obj)
558 struct irq_domain *pci_domain;
559 pci_bridge = device_allocsys(NULL, NULL);
561 #ifdef CONFIG_USE_DEVICETREE
562 devtree_link_t devtree_node;
563 devtree_node = changeling_try_reveal(obj, dt_node_morpher);
564 device_set_devtree_node(pci_bridge, devtree_node);
567 pci_domain = irq_create_domain(pci_bridge, &pci_irq_ops);
568 irq_attach_domain(irq_get_default_domain(), pci_domain);
570 register_device(pci_bridge, &def->class, "pci_bridge");
576 static struct device_def pci_def = {
577 def_device_name("Generic PCI"),
578 def_device_class(GENERIC, BUSIF, PCI),
580 def_on_register(pci_register),
581 def_on_create(pci_create)
583 EXPORT_DEVICE(pci3hba, &pci_def, load_onboot);