3 * @author Lunaixsky (zelong56@gmail.com)
4 * @brief A software implementation of PCI Local Bus Specification Revision 3.0
8 * @copyright Copyright (c) 2022
12 #include <sys/pci_hba.h>
14 #include <klibc/string.h>
15 #include <lunaix/fs/twifs.h>
16 #include <lunaix/mm/valloc.h>
17 #include <lunaix/spike.h>
18 #include <lunaix/syslog.h>
22 static DEFINE_LLIST(pci_devices);
24 static struct device* pcidev_cat;
27 pci_probe_msi_info(struct pci_device* device);
29 static struct pci_device*
30 pci_create_device(ptr_t pci_base, int devinfo)
32 pci_reg_t class = pci_read_cspace(pci_base, 0x8);
33 struct hbucket* bucket = device_definitions_byif(DEVIF_PCI);
35 u32_t devid = PCI_DEV_DEVID(devinfo);
36 u32_t vendor = PCI_DEV_VENDOR(devinfo);
38 kappendf(".%x:%x, ", vendor, devid);
40 struct pci_device_def *pos, *n;
41 hashtable_bucket_foreach(bucket, pos, n, devdef.hlist_if)
43 if (pos->dev_class != PCI_DEV_CLASS(class)) {
47 u32_t idm = pos->ident_mask;
48 int result = (pos->dev_ident & idm) == (devinfo & idm);
55 kappendf(KWARN "unknown device\n");
60 pci_reg_t intr = pci_read_cspace(pci_base, 0x3c);
62 struct pci_device* device = vzalloc(sizeof(struct pci_device));
63 device->class_info = class;
64 device->device_info = devinfo;
65 device->cspace_base = pci_base;
66 device->intr_info = intr;
68 device_create(&device->dev, pcidev_cat, DEV_IFSYS, NULL);
70 pci_probe_msi_info(device);
71 pci_probe_bar_info(device);
73 kappendf("%s (dev.%x:%x:%x) \n",
75 pos->devdef.class.fn_grp,
76 pos->devdef.class.device,
77 pos->devdef.class.variant);
79 if (!pos->devdef.init_for) {
80 kappendf(KERROR "bad def\n");
84 int errno = pos->devdef.init_for(&pos->devdef, &device->dev);
86 kappendf(KERROR "failed (e=%d)\n", errno);
90 llist_append(&pci_devices, &device->dev_chain);
91 device_register(&device->dev, &pos->devdef.class, "%x:%x", vendor, devid);
101 pci_probe_device(int bus, int dev, int funct)
103 u32_t base = PCI_ADDRESS(bus, dev, funct);
104 pci_reg_t reg1 = pci_read_cspace(base, 0);
106 // Vendor=0xffff则表示设备不存在
107 if (PCI_DEV_VENDOR(reg1) == PCI_VENDOR_INVLD) {
111 pci_reg_t hdr_type = pci_read_cspace(base, 0xc);
112 hdr_type = (hdr_type >> 16) & 0xff;
115 // QEMU的ICH9/Q35实现似乎有点问题,对于多功能设备的每一个功能的header type
116 // 都将第七位置位。而virtualbox 就没有这个毛病。
117 if ((hdr_type & 0x80) && funct == 0) {
118 hdr_type = hdr_type & ~0x80;
119 // 探测多用途设备(multi-function device)
120 for (int i = 1; i < 7; i++) {
121 pci_probe_device(bus, dev, i);
125 if (hdr_type != PCI_TDEV) {
126 // XXX: 目前忽略所有桥接设备,比如PCI-PCI桥接器,或者是CardBus桥接器
130 kprintf("pci.%d:%d:%d", bus, dev, funct);
132 pci_create_device(base, reg1);
138 for (int bus = 0; bus < 256; bus++) {
139 for (int dev = 0; dev < 32; dev++) {
140 pci_probe_device(bus, dev, 0);
146 pci_probe_bar_info(struct pci_device* device)
149 struct pci_base_addr* ba;
150 for (size_t i = 0; i < 6; i++) {
151 ba = &device->bar[i];
152 ba->size = pci_bar_sizing(device, &bar, i + 1);
153 if (PCI_BAR_MMIO(bar)) {
154 ba->start = PCI_BAR_ADDR_MM(bar);
155 ba->type |= PCI_BAR_CACHEABLE(bar) ? BAR_TYPE_CACHABLE : 0;
156 ba->type |= BAR_TYPE_MMIO;
158 ba->start = PCI_BAR_ADDR_IO(bar);
164 pci_probe_msi_info(struct pci_device* device)
166 // Note that Virtualbox have to use ICH9 chipset for MSI support.
167 // Qemu seems ok with default PIIX3, Bochs is pending to test...
168 // See https://www.virtualbox.org/manual/ch03.html (section 3.5.1)
170 pci_read_cspace(device->cspace_base, PCI_REG_STATUS_CMD) >> 16;
172 if (!(status & 0x10)) {
177 pci_reg_t cap_ptr = pci_read_cspace(device->cspace_base, 0x34) & 0xff;
181 cap_hdr = pci_read_cspace(device->cspace_base, cap_ptr);
182 if ((cap_hdr & 0xff) == 0x5) {
184 device->msi_loc = cap_ptr;
187 cap_ptr = (cap_hdr >> 8) & 0xff;
192 pci_bar_sizing(struct pci_device* dev, u32_t* bar_out, u32_t bar_num)
194 pci_reg_t bar = pci_read_cspace(dev->cspace_base, PCI_REG_BAR(bar_num));
200 pci_write_cspace(dev->cspace_base, PCI_REG_BAR(bar_num), 0xffffffff);
202 pci_read_cspace(dev->cspace_base, PCI_REG_BAR(bar_num)) & ~0x1;
203 if (PCI_BAR_MMIO(bar)) {
204 sized = PCI_BAR_ADDR_MM(sized);
207 pci_write_cspace(dev->cspace_base, PCI_REG_BAR(bar_num), bar);
212 pci_get_device_by_id(u16_t vendorId, u16_t deviceId)
214 u32_t dev_info = vendorId | (deviceId << 16);
215 struct pci_device *pos, *n;
216 llist_for_each(pos, n, &pci_devices, dev_chain)
218 if (pos->device_info == dev_info) {
227 pci_get_device_by_class(u32_t class)
229 struct pci_device *pos, *n;
230 llist_for_each(pos, n, &pci_devices, dev_chain)
232 if (PCI_DEV_CLASS(pos->class_info) == class) {
241 __pci_read_cspace(struct twimap* map)
243 struct pci_device* pcidev = (struct pci_device*)(map->data);
245 for (size_t i = 0; i < 256; i += sizeof(pci_reg_t)) {
246 *(pci_reg_t*)(map->buffer + i) =
247 pci_read_cspace(pcidev->cspace_base, i);
253 /*---------- TwiFS interface definition ----------*/
256 __pci_read_revid(struct twimap* map)
258 int class = twimap_data(map, struct pci_device*)->class_info;
259 twimap_printf(map, "0x%x", PCI_DEV_REV(class));
263 __pci_read_class(struct twimap* map)
265 int class = twimap_data(map, struct pci_device*)->class_info;
266 twimap_printf(map, "0x%x", PCI_DEV_CLASS(class));
270 __pci_bar_read(struct twimap* map)
272 struct pci_device* pcidev = twimap_data(map, struct pci_device*);
273 int bar_index = twimap_index(map, int);
275 struct pci_base_addr* bar = &pcidev->bar[bar_index];
277 if (!bar->start && !bar->size) {
278 twimap_printf(map, "[%d] not present \n", bar_index);
283 map, "[%d] base=%.8p, size=%.8p, ", bar_index, bar->start, bar->size);
285 if ((bar->type & BAR_TYPE_MMIO)) {
286 twimap_printf(map, "mmio");
287 if ((bar->type & BAR_TYPE_CACHABLE)) {
288 twimap_printf(map, ", prefetchable");
291 twimap_printf(map, "io");
294 twimap_printf(map, "\n");
298 __pci_bar_gonext(struct twimap* map)
300 if (twimap_index(map, int) >= 5) {
308 __pci_read_binding(struct twimap* map)
310 struct pci_device* pcidev = twimap_data(map, struct pci_device*);
311 // check if device binding has been initialized
312 struct device* dev = device_cast(&pcidev->dev);
317 twimap_printf(map, "0x%x:0x%x", dev->ident.fn_grp, dev->ident.unique);
321 pci_build_fsmapping()
323 struct twifs_node *pci_class = twifs_dir_node(NULL, "pci"), *pci_dev;
324 struct pci_device *pos, *n;
326 llist_for_each(pos, n, &pci_devices, dev_chain)
328 pci_dev = twifs_dir_node(pci_class,
329 "%.2d:%.2d:%.2d.%.4x:%.4x",
330 PCI_BUS_NUM(pos->cspace_base),
331 PCI_SLOT_NUM(pos->cspace_base),
332 PCI_FUNCT_NUM(pos->cspace_base),
333 PCI_DEV_VENDOR(pos->device_info),
334 PCI_DEV_DEVID(pos->device_info));
336 map = twifs_mapping(pci_dev, pos, "config");
337 map->read = __pci_read_cspace;
339 map = twifs_mapping(pci_dev, pos, "revision");
340 map->read = __pci_read_revid;
342 map = twifs_mapping(pci_dev, pos, "class");
343 map->read = __pci_read_class;
345 map = twifs_mapping(pci_dev, pos, "binding");
346 map->read = __pci_read_binding;
348 map = twifs_mapping(pci_dev, pos, "io_bases");
349 map->read = __pci_bar_read;
350 map->go_next = __pci_bar_gonext;
353 EXPORT_TWIFS_PLUGIN(pci_devs, pci_build_fsmapping);
355 /*---------- PCI 3.0 HBA device definition ----------*/
358 pci_load_devices(struct device_def* def)
360 pcidev_cat = device_addcat(NULL, "pci");
367 static struct device_def pci_def = {
368 .name = "pci3.0-hba",
369 .class = DEVCLASS(DEVIF_SOC, DEVFN_BUSIF, DEV_PCI),
370 .init = pci_load_devices
372 EXPORT_DEVICE(pci3hba, &pci_def, load_poststage);