feat: support user-spcae pci rescan
[lunaix-os.git] / lunaix-os / hal / pci.c
1 /**
2  * @file pci.c
3  * @author Lunaixsky (zelong56@gmail.com)
4  * @brief A software implementation of PCI Local Bus Specification Revision 3.0
5  * @version 0.1
6  * @date 2022-06-28
7  *
8  * @copyright Copyright (c) 2022
9  *
10  */
11 #include <hal/pci.h>
12 #include <sys/pci_hba.h>
13
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>
19
20 LOG_MODULE("PCI")
21
22 static DEFINE_LLIST(pci_devices);
23 static DECLARE_HASHTABLE(pci_devcache, 8);
24
25 static struct device* pcidev_cat;
26 static struct device_def pci_def;
27
28 void
29 pci_probe_msi_info(struct pci_device* device);
30
31 static inline void
32 pci_log_device(struct pci_device* pcidev)
33 {
34     pciaddr_t loc = pcidev->loc;
35     struct device_def* binddef = pcidev->binding.def;
36
37     if (!binddef) {
38         kprintf("pci.%d:%d:%d, no binding\n",
39                 PCILOC_BUS(loc),
40                 PCILOC_DEV(loc),
41                 PCILOC_FN(loc));
42         return;
43     }
44
45     kprintf("pci.%d:%d:%d, dev.%xh:%xh.%d, %s\n",
46             PCILOC_BUS(loc),
47             PCILOC_DEV(loc),
48             PCILOC_FN(loc),
49             binddef->class.fn_grp,
50             binddef->class.device,
51             binddef->class.variant,
52             binddef->name);
53 }
54
55 static struct pci_device*
56 pci_create_device(pciaddr_t loc, ptr_t pci_base, int devinfo)
57 {
58     pci_reg_t class = pci_read_cspace(pci_base, 0x8);
59     struct hbucket* bucket = device_definitions_byif(DEVIF_PCI);
60
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);
64
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;
70
71     device_create(&device->dev, pcidev_cat, DEV_IFSYS, NULL);
72
73     pci_probe_msi_info(device);
74     pci_probe_bar_info(device);
75
76     llist_append(&pci_devices, &device->dev_chain);
77     device_register(&device->dev, &pci_def.class, "%x", loc);
78     pci_def.class.variant++;
79
80     // find a suitable binding
81
82     struct pci_device_def *pos, *n;
83     hashtable_bucket_foreach(bucket, pos, n, devdef.hlist_if)
84     {
85         if (pos->dev_class != PCI_DEV_CLASS(class)) {
86             continue;
87         }
88
89         u32_t idm = pos->ident_mask;
90         int result = (pos->dev_ident & idm) == (devinfo & idm);
91
92         if (result) {
93             goto found;
94         }
95     }
96
97     goto done;
98
99 found:
100     if (!pos->devdef.bind) {
101         kprintf(KERROR "pci_loc:%x, (%xh:%xh.%d) unbindable\n",
102                 loc,
103                 pos->devdef.class.fn_grp,
104                 pos->devdef.class.device,
105                 pos->devdef.class.variant);
106         goto done;
107     }
108
109     int errno = pos->devdef.bind(&pos->devdef, &device->dev);
110     if (errno) {
111         kprintf(KERROR "pci_loc:%x, (%xh:%xh.%d) failed, e=%d\n",
112                 loc,
113                 pos->devdef.class.fn_grp,
114                 pos->devdef.class.device,
115                 pos->devdef.class.variant,
116                 errno);
117         goto done;
118     }
119
120     device->binding.def = &pos->devdef;
121
122 done:
123     return device;
124 }
125
126 void
127 pci_probe_device(pciaddr_t pci_loc)
128 {
129     u32_t base = PCI_CFGADDR(pci_loc);
130     pci_reg_t reg1 = pci_read_cspace(base, 0);
131
132     // Vendor=0xffff则表示设备不存在
133     if (PCI_DEV_VENDOR(reg1) == PCI_VENDOR_INVLD) {
134         return;
135     }
136
137     pci_reg_t hdr_type = pci_read_cspace(base, 0xc);
138     hdr_type = (hdr_type >> 16) & 0xff;
139
140     // 防止堆栈溢出
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);
148         }
149     }
150
151     struct pci_device *pos, *n;
152     hashtable_hash_foreach(pci_devcache, pci_loc, pos, n, dev_cache)
153     {
154         if (pos->loc == pci_loc) {
155             pci_log_device(pos);
156             return;
157         }
158     }
159
160     struct pci_device* pcidev = pci_create_device(pci_loc, base, reg1);
161     if (pcidev) {
162         pcidev->loc = pci_loc;
163         hashtable_hash_in(pci_devcache, &pcidev->dev_cache, pci_loc);
164         pci_log_device(pcidev);
165     }
166 }
167
168 void
169 pci_scan()
170 {
171     for (u32_t loc = 0; loc < (pciaddr_t)-1; loc += 8) {
172         pci_probe_device((pciaddr_t)loc);
173     }
174 }
175
176 void
177 pci_probe_bar_info(struct pci_device* device)
178 {
179     u32_t bar;
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;
188         } else {
189             ba->start = PCI_BAR_ADDR_IO(bar);
190         }
191     }
192 }
193
194 void
195 pci_probe_msi_info(struct pci_device* device)
196 {
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)
200     pci_reg_t status =
201       pci_read_cspace(device->cspace_base, PCI_REG_STATUS_CMD) >> 16;
202
203     if (!(status & 0x10)) {
204         device->msi_loc = 0;
205         return;
206     }
207
208     pci_reg_t cap_ptr = pci_read_cspace(device->cspace_base, 0x34) & 0xff;
209     u32_t cap_hdr;
210
211     while (cap_ptr) {
212         cap_hdr = pci_read_cspace(device->cspace_base, cap_ptr);
213         if ((cap_hdr & 0xff) == 0x5) {
214             // MSI
215             device->msi_loc = cap_ptr;
216             return;
217         }
218         cap_ptr = (cap_hdr >> 8) & 0xff;
219     }
220 }
221
222 size_t
223 pci_bar_sizing(struct pci_device* dev, u32_t* bar_out, u32_t bar_num)
224 {
225     pci_reg_t bar = pci_read_cspace(dev->cspace_base, PCI_REG_BAR(bar_num));
226     if (!bar) {
227         *bar_out = 0;
228         return 0;
229     }
230
231     pci_write_cspace(dev->cspace_base, PCI_REG_BAR(bar_num), 0xffffffff);
232     pci_reg_t sized =
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);
236     }
237     *bar_out = bar;
238     pci_write_cspace(dev->cspace_base, PCI_REG_BAR(bar_num), bar);
239     return ~sized + 1;
240 }
241
242 struct pci_device*
243 pci_get_device_by_id(u16_t vendorId, u16_t deviceId)
244 {
245     u32_t dev_info = vendorId | (deviceId << 16);
246     struct pci_device *pos, *n;
247     llist_for_each(pos, n, &pci_devices, dev_chain)
248     {
249         if (pos->device_info == dev_info) {
250             return pos;
251         }
252     }
253
254     return NULL;
255 }
256
257 struct pci_device*
258 pci_get_device_by_class(u32_t class)
259 {
260     struct pci_device *pos, *n;
261     llist_for_each(pos, n, &pci_devices, dev_chain)
262     {
263         if (PCI_DEV_CLASS(pos->class_info) == class) {
264             return pos;
265         }
266     }
267
268     return NULL;
269 }
270
271 static void
272 __pci_read_cspace(struct twimap* map)
273 {
274     struct pci_device* pcidev = (struct pci_device*)(map->data);
275
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);
279     }
280
281     map->size_acc = 256;
282 }
283
284 /*---------- TwiFS interface definition ----------*/
285
286 static void
287 __pci_read_revid(struct twimap* map)
288 {
289     int class = twimap_data(map, struct pci_device*)->class_info;
290     twimap_printf(map, "0x%x", PCI_DEV_REV(class));
291 }
292
293 static void
294 __pci_read_class(struct twimap* map)
295 {
296     int class = twimap_data(map, struct pci_device*)->class_info;
297     twimap_printf(map, "0x%x", PCI_DEV_CLASS(class));
298 }
299
300 static void
301 __pci_read_devinfo(struct twimap* map)
302 {
303     int devinfo = twimap_data(map, struct pci_device*)->device_info;
304     twimap_printf(
305       map, "%x:%x", PCI_DEV_VENDOR(devinfo), PCI_DEV_DEVID(devinfo));
306 }
307
308 static void
309 __pci_bar_read(struct twimap* map)
310 {
311     struct pci_device* pcidev = twimap_data(map, struct pci_device*);
312     int bar_index = twimap_index(map, int);
313
314     struct pci_base_addr* bar = &pcidev->bar[bar_index];
315
316     if (!bar->start && !bar->size) {
317         twimap_printf(map, "[%d] not present \n", bar_index);
318         return;
319     }
320
321     twimap_printf(
322       map, "[%d] base=%.8p, size=%.8p, ", bar_index, bar->start, bar->size);
323
324     if ((bar->type & BAR_TYPE_MMIO)) {
325         twimap_printf(map, "mmio");
326         if ((bar->type & BAR_TYPE_CACHABLE)) {
327             twimap_printf(map, ", prefetchable");
328         }
329     } else {
330         twimap_printf(map, "io");
331     }
332
333     twimap_printf(map, "\n");
334 }
335
336 static int
337 __pci_bar_gonext(struct twimap* map)
338 {
339     if (twimap_index(map, int) >= 5) {
340         return 0;
341     }
342     map->index += 1;
343     return 1;
344 }
345
346 static void
347 __pci_read_binding(struct twimap* map)
348 {
349     struct pci_device* pcidev = twimap_data(map, struct pci_device*);
350     struct device_def* devdef = pcidev->binding.def;
351     if (!devdef) {
352         return;
353     }
354
355     twimap_printf(map,
356                   "%xh:%xh.%d",
357                   devdef->class.fn_grp,
358                   devdef->class.device,
359                   devdef->class.variant);
360 }
361
362 static void
363 __pci_trigger_bus_rescan(struct twimap* map)
364 {
365     pci_scan();
366 }
367
368 void
369 pci_build_fsmapping()
370 {
371     struct twifs_node *pci_class = twifs_dir_node(NULL, "pci"), *pci_dev;
372     struct pci_device *pos, *n;
373     struct twimap* map;
374
375     map = twifs_mapping(pci_class, NULL, "rescan");
376     map->read = __pci_trigger_bus_rescan;
377
378     llist_for_each(pos, n, &pci_devices, dev_chain)
379     {
380         pci_dev = twifs_dir_node(pci_class, "%x", pos->loc);
381
382         map = twifs_mapping(pci_dev, pos, "config");
383         map->read = __pci_read_cspace;
384
385         map = twifs_mapping(pci_dev, pos, "revision");
386         map->read = __pci_read_revid;
387
388         map = twifs_mapping(pci_dev, pos, "class");
389         map->read = __pci_read_class;
390
391         map = twifs_mapping(pci_dev, pos, "binding");
392         map->read = __pci_read_binding;
393
394         map = twifs_mapping(pci_dev, pos, "io_bases");
395         map->read = __pci_bar_read;
396         map->go_next = __pci_bar_gonext;
397     }
398 }
399 EXPORT_TWIFS_PLUGIN(pci_devs, pci_build_fsmapping);
400
401 /*---------- PCI 3.0 HBA device definition ----------*/
402
403 static int
404 pci_load_devices(struct device_def* def)
405 {
406     pcidev_cat = device_addcat(NULL, "pci");
407
408     pci_scan();
409
410     return 0;
411 }
412
413 void
414 pci_bind_instance(struct pci_device* pcidev, void* devobj)
415 {
416     pcidev->dev.underlay = devobj;
417     pcidev->binding.dev = devobj;
418 }
419
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
424 };
425 EXPORT_DEVICE(pci3hba, &pci_def, load_poststage);