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);
23 static DECLARE_HASHTABLE(pci_devcache, 8);
25 static struct device* pcidev_cat;
26 static struct device_def pci_def;
29 pci_probe_msi_info(struct pci_device* device);
32 pci_log_device(struct pci_device* pcidev)
34 pciaddr_t loc = pcidev->loc;
35 struct device_def* binddef = pcidev->binding.def;
38 kprintf("pci.%d:%d:%d, no binding\n",
45 kprintf("pci.%d:%d:%d, dev.%xh:%xh.%d, %s\n",
49 binddef->class.fn_grp,
50 binddef->class.device,
51 binddef->class.variant,
55 static struct pci_device*
56 pci_create_device(pciaddr_t loc, ptr_t pci_base, int devinfo)
58 pci_reg_t class = pci_read_cspace(pci_base, 0x8);
59 struct hbucket* bucket = device_definitions_byif(DEVIF_PCI);
61 u32_t devid = PCI_DEV_DEVID(devinfo);
62 u32_t vendor = PCI_DEV_VENDOR(devinfo);
63 pci_reg_t intr = pci_read_cspace(pci_base, 0x3c);
65 struct pci_device* device = vzalloc(sizeof(struct pci_device));
66 device->class_info = class;
67 device->device_info = devinfo;
68 device->cspace_base = pci_base;
69 device->intr_info = intr;
71 device_create(&device->dev, pcidev_cat, DEV_IFSYS, NULL);
73 pci_probe_msi_info(device);
74 pci_probe_bar_info(device);
76 llist_append(&pci_devices, &device->dev_chain);
77 device_register(&device->dev, &pci_def.class, "%x", loc);
78 pci_def.class.variant++;
80 // find a suitable binding
82 struct pci_device_def *pos, *n;
83 hashtable_bucket_foreach(bucket, pos, n, devdef.hlist_if)
85 if (pos->dev_class != PCI_DEV_CLASS(class)) {
89 u32_t idm = pos->ident_mask;
90 int result = (pos->dev_ident & idm) == (devinfo & idm);
100 if (!pos->devdef.bind) {
101 kprintf(KERROR "pci_loc:%x, (%xh:%xh.%d) unbindable\n",
103 pos->devdef.class.fn_grp,
104 pos->devdef.class.device,
105 pos->devdef.class.variant);
109 int errno = pos->devdef.bind(&pos->devdef, &device->dev);
111 kprintf(KERROR "pci_loc:%x, (%xh:%xh.%d) failed, e=%d\n",
113 pos->devdef.class.fn_grp,
114 pos->devdef.class.device,
115 pos->devdef.class.variant,
120 device->binding.def = &pos->devdef;
127 pci_probe_device(pciaddr_t pci_loc)
129 u32_t base = PCI_CFGADDR(pci_loc);
130 pci_reg_t reg1 = pci_read_cspace(base, 0);
132 // Vendor=0xffff则表示设备不存在
133 if (PCI_DEV_VENDOR(reg1) == PCI_VENDOR_INVLD) {
137 pci_reg_t hdr_type = pci_read_cspace(base, 0xc);
138 hdr_type = (hdr_type >> 16) & 0xff;
141 // QEMU的ICH9/Q35实现似乎有点问题,对于多功能设备的每一个功能的header type
142 // 都将第七位置位。而virtualbox 就没有这个毛病。
143 if ((hdr_type & 0x80) && PCILOC_FN(pci_loc) == 0) {
144 hdr_type = hdr_type & ~0x80;
145 // 探测多用途设备(multi-function device)
146 for (int i = 1; i < 7; i++) {
147 pci_probe_device(pci_loc + i);
151 struct pci_device *pos, *n;
152 hashtable_hash_foreach(pci_devcache, pci_loc, pos, n, dev_cache)
154 if (pos->loc == pci_loc) {
160 struct pci_device* pcidev = pci_create_device(pci_loc, base, reg1);
162 pcidev->loc = pci_loc;
163 hashtable_hash_in(pci_devcache, &pcidev->dev_cache, pci_loc);
164 pci_log_device(pcidev);
171 for (u32_t loc = 0; loc < (pciaddr_t)-1; loc += 8) {
172 pci_probe_device((pciaddr_t)loc);
177 pci_probe_bar_info(struct pci_device* device)
180 struct pci_base_addr* ba;
181 for (size_t i = 0; i < 6; i++) {
182 ba = &device->bar[i];
183 ba->size = pci_bar_sizing(device, &bar, i + 1);
184 if (PCI_BAR_MMIO(bar)) {
185 ba->start = PCI_BAR_ADDR_MM(bar);
186 ba->type |= PCI_BAR_CACHEABLE(bar) ? BAR_TYPE_CACHABLE : 0;
187 ba->type |= BAR_TYPE_MMIO;
189 ba->start = PCI_BAR_ADDR_IO(bar);
195 pci_probe_msi_info(struct pci_device* device)
197 // Note that Virtualbox have to use ICH9 chipset for MSI support.
198 // Qemu seems ok with default PIIX3, Bochs is pending to test...
199 // See https://www.virtualbox.org/manual/ch03.html (section 3.5.1)
201 pci_read_cspace(device->cspace_base, PCI_REG_STATUS_CMD) >> 16;
203 if (!(status & 0x10)) {
208 pci_reg_t cap_ptr = pci_read_cspace(device->cspace_base, 0x34) & 0xff;
212 cap_hdr = pci_read_cspace(device->cspace_base, cap_ptr);
213 if ((cap_hdr & 0xff) == 0x5) {
215 device->msi_loc = cap_ptr;
218 cap_ptr = (cap_hdr >> 8) & 0xff;
223 pci_bar_sizing(struct pci_device* dev, u32_t* bar_out, u32_t bar_num)
225 pci_reg_t bar = pci_read_cspace(dev->cspace_base, PCI_REG_BAR(bar_num));
231 pci_write_cspace(dev->cspace_base, PCI_REG_BAR(bar_num), 0xffffffff);
233 pci_read_cspace(dev->cspace_base, PCI_REG_BAR(bar_num)) & ~0x1;
234 if (PCI_BAR_MMIO(bar)) {
235 sized = PCI_BAR_ADDR_MM(sized);
238 pci_write_cspace(dev->cspace_base, PCI_REG_BAR(bar_num), bar);
243 pci_get_device_by_id(u16_t vendorId, u16_t deviceId)
245 u32_t dev_info = vendorId | (deviceId << 16);
246 struct pci_device *pos, *n;
247 llist_for_each(pos, n, &pci_devices, dev_chain)
249 if (pos->device_info == dev_info) {
258 pci_get_device_by_class(u32_t class)
260 struct pci_device *pos, *n;
261 llist_for_each(pos, n, &pci_devices, dev_chain)
263 if (PCI_DEV_CLASS(pos->class_info) == class) {
272 __pci_read_cspace(struct twimap* map)
274 struct pci_device* pcidev = (struct pci_device*)(map->data);
276 for (size_t i = 0; i < 256; i += sizeof(pci_reg_t)) {
277 *(pci_reg_t*)(map->buffer + i) =
278 pci_read_cspace(pcidev->cspace_base, i);
284 /*---------- TwiFS interface definition ----------*/
287 __pci_read_revid(struct twimap* map)
289 int class = twimap_data(map, struct pci_device*)->class_info;
290 twimap_printf(map, "0x%x", PCI_DEV_REV(class));
294 __pci_read_class(struct twimap* map)
296 int class = twimap_data(map, struct pci_device*)->class_info;
297 twimap_printf(map, "0x%x", PCI_DEV_CLASS(class));
301 __pci_read_devinfo(struct twimap* map)
303 int devinfo = twimap_data(map, struct pci_device*)->device_info;
305 map, "%x:%x", PCI_DEV_VENDOR(devinfo), PCI_DEV_DEVID(devinfo));
309 __pci_bar_read(struct twimap* map)
311 struct pci_device* pcidev = twimap_data(map, struct pci_device*);
312 int bar_index = twimap_index(map, int);
314 struct pci_base_addr* bar = &pcidev->bar[bar_index];
316 if (!bar->start && !bar->size) {
317 twimap_printf(map, "[%d] not present \n", bar_index);
322 map, "[%d] base=%.8p, size=%.8p, ", bar_index, bar->start, bar->size);
324 if ((bar->type & BAR_TYPE_MMIO)) {
325 twimap_printf(map, "mmio");
326 if ((bar->type & BAR_TYPE_CACHABLE)) {
327 twimap_printf(map, ", prefetchable");
330 twimap_printf(map, "io");
333 twimap_printf(map, "\n");
337 __pci_bar_gonext(struct twimap* map)
339 if (twimap_index(map, int) >= 5) {
347 __pci_read_binding(struct twimap* map)
349 struct pci_device* pcidev = twimap_data(map, struct pci_device*);
350 struct device_def* devdef = pcidev->binding.def;
357 devdef->class.fn_grp,
358 devdef->class.device,
359 devdef->class.variant);
363 __pci_trigger_bus_rescan(struct twimap* map)
369 pci_build_fsmapping()
371 struct twifs_node *pci_class = twifs_dir_node(NULL, "pci"), *pci_dev;
372 struct pci_device *pos, *n;
375 map = twifs_mapping(pci_class, NULL, "rescan");
376 map->read = __pci_trigger_bus_rescan;
378 llist_for_each(pos, n, &pci_devices, dev_chain)
380 pci_dev = twifs_dir_node(pci_class, "%x", pos->loc);
382 map = twifs_mapping(pci_dev, pos, "config");
383 map->read = __pci_read_cspace;
385 map = twifs_mapping(pci_dev, pos, "revision");
386 map->read = __pci_read_revid;
388 map = twifs_mapping(pci_dev, pos, "class");
389 map->read = __pci_read_class;
391 map = twifs_mapping(pci_dev, pos, "binding");
392 map->read = __pci_read_binding;
394 map = twifs_mapping(pci_dev, pos, "io_bases");
395 map->read = __pci_bar_read;
396 map->go_next = __pci_bar_gonext;
399 EXPORT_TWIFS_PLUGIN(pci_devs, pci_build_fsmapping);
401 /*---------- PCI 3.0 HBA device definition ----------*/
404 pci_load_devices(struct device_def* def)
406 pcidev_cat = device_addcat(NULL, "pci");
414 pci_bind_instance(struct pci_device* pcidev, void* devobj)
416 pcidev->dev.underlay = devobj;
417 pcidev->binding.dev = devobj;
420 static struct device_def pci_def = {
421 .name = "pci3.0-hba",
422 .class = DEVCLASS(DEVIF_SOC, DEVFN_BUSIF, DEV_PCI),
423 .init = pci_load_devices
425 EXPORT_DEVICE(pci3hba, &pci_def, load_poststage);