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);
25 pci_probe_msi_info(struct pci_device* device);
27 static struct pci_device*
28 pci_create_device(ptr_t pci_base, int devinfo)
30 pci_reg_t class = pci_read_cspace(pci_base, 0x8);
31 struct hbucket* bucket = device_definitions_byif(DEVIF_PCI);
33 u32_t devid = PCI_DEV_DEVID(devinfo);
34 u32_t vendor = PCI_DEV_VENDOR(devinfo);
36 kappendf(".%x:%x, ", vendor, devid);
38 struct pci_device_def *pos, *n;
39 hashtable_bucket_foreach(bucket, pos, n, devdef.hlist_if)
41 if (pos->dev_class != PCI_DEV_CLASS(class)) {
45 int result = (pos->dev_vendor & vendor) == vendor &&
46 (pos->dev_id & devid) == devid;
53 kappendf(KWARN "unknown device\n");
58 pci_reg_t intr = pci_read_cspace(pci_base, 0x3c);
60 struct pci_device* device = vzalloc(sizeof(struct pci_device));
61 device->class_info = class;
62 device->device_info = devinfo;
63 device->cspace_base = pci_base;
64 device->intr_info = intr;
66 device_prepare(&device->dev);
68 pci_probe_msi_info(device);
69 pci_probe_bar_info(device);
71 kappendf("%s (dev.%x:%x:%x) \n",
73 pos->devdef.class.meta,
74 pos->devdef.class.device,
75 pos->devdef.class.variant);
77 if (!pos->devdef.init_for) {
78 kappendf(KERROR "bad def\n");
82 int errno = pos->devdef.init_for(&pos->devdef, &device->dev);
84 kappendf(KERROR "failed (e=%d)\n", errno);
88 llist_append(&pci_devices, &device->dev_chain);
98 pci_probe_device(int bus, int dev, int funct)
100 u32_t base = PCI_ADDRESS(bus, dev, funct);
101 pci_reg_t reg1 = pci_read_cspace(base, 0);
103 // Vendor=0xffff则表示设备不存在
104 if (PCI_DEV_VENDOR(reg1) == PCI_VENDOR_INVLD) {
108 pci_reg_t hdr_type = pci_read_cspace(base, 0xc);
109 hdr_type = (hdr_type >> 16) & 0xff;
112 // QEMU的ICH9/Q35实现似乎有点问题,对于多功能设备的每一个功能的header type
113 // 都将第七位置位。而virtualbox 就没有这个毛病。
114 if ((hdr_type & 0x80) && funct == 0) {
115 hdr_type = hdr_type & ~0x80;
116 // 探测多用途设备(multi-function device)
117 for (int i = 1; i < 7; i++) {
118 pci_probe_device(bus, dev, i);
122 if (hdr_type != PCI_TDEV) {
123 // XXX: 目前忽略所有桥接设备,比如PCI-PCI桥接器,或者是CardBus桥接器
127 kprintf("pci.%d:%d:%d", bus, dev, funct);
129 pci_create_device(base, reg1);
135 for (int bus = 0; bus < 256; bus++) {
136 for (int dev = 0; dev < 32; dev++) {
137 pci_probe_device(bus, dev, 0);
143 pci_probe_bar_info(struct pci_device* device)
146 struct pci_base_addr* ba;
147 for (size_t i = 0; i < 6; i++) {
148 ba = &device->bar[i];
149 ba->size = pci_bar_sizing(device, &bar, i + 1);
150 if (PCI_BAR_MMIO(bar)) {
151 ba->start = PCI_BAR_ADDR_MM(bar);
152 ba->type |= PCI_BAR_CACHEABLE(bar) ? BAR_TYPE_CACHABLE : 0;
153 ba->type |= BAR_TYPE_MMIO;
155 ba->start = PCI_BAR_ADDR_IO(bar);
161 pci_probe_msi_info(struct pci_device* device)
163 // Note that Virtualbox have to use ICH9 chipset for MSI support.
164 // Qemu seems ok with default PIIX3, Bochs is pending to test...
165 // See https://www.virtualbox.org/manual/ch03.html (section 3.5.1)
167 pci_read_cspace(device->cspace_base, PCI_REG_STATUS_CMD) >> 16;
169 if (!(status & 0x10)) {
174 pci_reg_t cap_ptr = pci_read_cspace(device->cspace_base, 0x34) & 0xff;
178 cap_hdr = pci_read_cspace(device->cspace_base, cap_ptr);
179 if ((cap_hdr & 0xff) == 0x5) {
181 device->msi_loc = cap_ptr;
184 cap_ptr = (cap_hdr >> 8) & 0xff;
189 pci_bar_sizing(struct pci_device* dev, u32_t* bar_out, u32_t bar_num)
191 pci_reg_t bar = pci_read_cspace(dev->cspace_base, PCI_REG_BAR(bar_num));
197 pci_write_cspace(dev->cspace_base, PCI_REG_BAR(bar_num), 0xffffffff);
199 pci_read_cspace(dev->cspace_base, PCI_REG_BAR(bar_num)) & ~0x1;
200 if (PCI_BAR_MMIO(bar)) {
201 sized = PCI_BAR_ADDR_MM(sized);
204 pci_write_cspace(dev->cspace_base, PCI_REG_BAR(bar_num), bar);
209 pci_get_device_by_id(u16_t vendorId, u16_t deviceId)
211 u32_t dev_info = vendorId | (deviceId << 16);
212 struct pci_device *pos, *n;
213 llist_for_each(pos, n, &pci_devices, dev_chain)
215 if (pos->device_info == dev_info) {
224 pci_get_device_by_class(u32_t class)
226 struct pci_device *pos, *n;
227 llist_for_each(pos, n, &pci_devices, dev_chain)
229 if (PCI_DEV_CLASS(pos->class_info) == class) {
238 __pci_read_cspace(struct twimap* map)
240 struct pci_device* pcidev = (struct pci_device*)(map->data);
242 for (size_t i = 0; i < 256; i += sizeof(pci_reg_t)) {
243 *(pci_reg_t*)(map->buffer + i) =
244 pci_read_cspace(pcidev->cspace_base, i);
250 /*---------- TwiFS interface definition ----------*/
253 __pci_read_revid(struct twimap* map)
255 int class = twimap_data(map, struct pci_device*)->class_info;
256 twimap_printf(map, "0x%x", PCI_DEV_REV(class));
260 __pci_read_class(struct twimap* map)
262 int class = twimap_data(map, struct pci_device*)->class_info;
263 twimap_printf(map, "0x%x", PCI_DEV_CLASS(class));
267 __pci_bar_read(struct twimap* map)
269 struct pci_device* pcidev = twimap_data(map, struct pci_device*);
270 int bar_index = twimap_index(map, int);
272 struct pci_base_addr* bar = &pcidev->bar[bar_index];
274 if (!bar->start && !bar->size) {
275 twimap_printf(map, "[%d] not present \n", bar_index);
280 map, "[%d] base=%.8p, size=%.8p, ", bar_index, bar->start, bar->size);
282 if ((bar->type & BAR_TYPE_MMIO)) {
283 twimap_printf(map, "mmio");
284 if ((bar->type & BAR_TYPE_CACHABLE)) {
285 twimap_printf(map, ", prefetchable");
288 twimap_printf(map, "io");
291 twimap_printf(map, "\n");
295 __pci_bar_gonext(struct twimap* map)
297 if (twimap_index(map, int) >= 5) {
305 pci_build_fsmapping()
307 struct twifs_node *pci_class = twifs_dir_node(NULL, "pci"), *pci_dev;
308 struct pci_device *pos, *n;
310 llist_for_each(pos, n, &pci_devices, dev_chain)
312 pci_dev = twifs_dir_node(pci_class,
313 "%.2d:%.2d:%.2d.%.4x:%.4x",
314 PCI_BUS_NUM(pos->cspace_base),
315 PCI_SLOT_NUM(pos->cspace_base),
316 PCI_FUNCT_NUM(pos->cspace_base),
317 PCI_DEV_VENDOR(pos->device_info),
318 PCI_DEV_DEVID(pos->device_info));
320 map = twifs_mapping(pci_dev, pos, "config");
321 map->read = __pci_read_cspace;
323 map = twifs_mapping(pci_dev, pos, "revision");
324 map->read = __pci_read_revid;
326 map = twifs_mapping(pci_dev, pos, "class");
327 map->read = __pci_read_class;
329 map = twifs_mapping(pci_dev, pos, "io_bases");
330 map->read = __pci_bar_read;
331 map->go_next = __pci_bar_gonext;
334 EXPORT_TWIFS_PLUGIN(pci_devs, pci_build_fsmapping);
336 /*---------- PCI 3.0 HBA device definition ----------*/
339 pci_load_devices(struct device_def* def)
346 static struct device_def pci_def = {
347 .name = "pci3.0-hba",
348 .class = DEVCLASS(DEVIF_SOC, DEVFN_BUSIF, DEV_BUS, 0),
349 .init = pci_load_devices
351 EXPORT_DEVICE(pci3hba, &pci_def, load_poststage);