make irq specifier to be provided when assigining irq
[lunaix-os.git] / lunaix-os / hal / bus / 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
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>
18
19 LOG_MODULE("PCI")
20
21 /*
22     device instance for pci bridge, 
23     currently, lunaix only support one bridge controller.
24 */
25 static struct device* pci_bridge;
26
27 static morph_t* pci_probers;
28 static DECLARE_HASHTABLE(pci_drivers, 8);
29
30 static inline void
31 pci_log_device(struct pci_probe* probe)
32 {
33     pciaddr_t loc = probe->loc;
34
35     kprintf("pci.%03d:%02d:%02d, class=%p, vendor:dev=%04x:%04x",
36             PCILOC_BUS(loc),
37             PCILOC_DEV(loc),
38             PCILOC_FN(loc),
39             probe->class_info,
40             PCI_DEV_VENDOR(probe->device_info),
41             PCI_DEV_DEVID(probe->device_info));
42 }
43
44 static void
45 __pci_probe_msi_info(struct pci_probe* probe)
46 {
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)
50     pci_reg_t status =
51       pci_read_cspace(probe->cspace_base, PCI_REG_STATUS_CMD) >> 16;
52
53     if (!(status & 0x10)) {
54         probe->msi_loc = 0;
55         return;
56     }
57
58     pci_reg_t cap_ptr = pci_read_cspace(probe->cspace_base, 0x34) & 0xff;
59     u32_t cap_hdr;
60
61     while (cap_ptr) {
62         cap_hdr = pci_read_cspace(probe->cspace_base, cap_ptr);
63         if ((cap_hdr & 0xff) == 0x5) {
64             // MSI
65             probe->msi_loc = cap_ptr;
66             return;
67         }
68         cap_ptr = (cap_hdr >> 8) & 0xff;
69     }
70 }
71
72
73 static void
74 __pci_probe_bar_info(struct pci_probe* probe)
75 {
76     u32_t bar;
77     struct pci_base_addr* ba;
78     for (size_t i = 0; i < PCI_BAR_COUNT; i++) {
79         ba = &probe->bar[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;
85         } else {
86             ba->start = PCI_BAR_ADDR_IO(bar);
87         }
88     }
89 }
90
91 static void
92 __pci_add_prober(pciaddr_t loc, ptr_t pci_base, int devinfo)
93 {
94     struct pci_probe* prober;
95     morph_t* probe_morph;
96
97     pci_reg_t class = pci_read_cspace(pci_base, 0x8);
98
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);
102
103     prober = vzalloc(sizeof(*prober));
104
105     prober->class_info = class;
106     prober->device_info = devinfo;
107     prober->cspace_base = pci_base;
108     prober->intr_info = intr;
109     prober->loc = loc;
110     prober->irq_domain = irq_get_domain(pci_bridge);
111
112     changeling_morph_anon(pci_probers, prober->mobj, pci_probe_morpher);
113
114     __pci_probe_msi_info(prober);
115     __pci_probe_bar_info(prober);
116
117     pci_log_device(prober);
118 }
119
120 static int
121 __pci_bind(struct pci_registry* reg, struct pci_probe* probe)
122 {
123     int errno;
124     struct device_def* devdef;
125
126     if (probe->bind) {
127         return EEXIST;
128     }
129
130     if (!reg->check_compact(probe)) {
131         return EINVAL;
132     }
133
134     devdef = reg->definition;
135     errno = devdef->create(devdef, &probe->mobj);
136
137     if (errno) {
138         ERROR("pci_loc:%x, bind (%xh:%xh.%d) failed, e=%d",
139                 probe->loc,
140                 devdef->class.fn_grp,
141                 devdef->class.device,
142                 devdef->class.variant,
143                 errno);
144     }
145
146     return errno;
147 }
148
149 int
150 pci_bind_driver(struct pci_registry* reg)
151 {
152     struct pci_probe* probe;
153     int errno = 0;
154     
155     morph_t *pos, *n;
156     changeling_for_each(pos, n, pci_probers)
157     {
158         probe = changeling_reveal(pos, pci_probe_morpher);
159
160         errno = __pci_bind(reg, probe);
161         if (errno) {
162             continue;
163         }
164     }
165
166     return errno;
167 }
168
169 void
170 pci_probe_device(pciaddr_t pci_loc)
171 {
172     u32_t base = PCI_CFGADDR(pci_loc);
173     pci_reg_t reg1 = pci_read_cspace(base, 0);
174
175     // Vendor=0xffff则表示设备不存在
176     if (PCI_DEV_VENDOR(reg1) == PCI_VENDOR_INVLD) {
177         return;
178     }
179
180     pci_reg_t hdr_type = pci_read_cspace(base, 0xc);
181     hdr_type = (hdr_type >> 16) & 0xff;
182
183     // 防止堆栈溢出
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);
191         }
192     }
193
194     struct pci_probe* prober;
195     morph_t *pos, *n;
196     changeling_for_each(pos, n, pci_probers) 
197     {
198         prober = changeling_reveal(pos, pci_probe_morpher);
199         if (prober->loc == pci_loc) {
200             pci_log_device(prober);
201             return;
202         }
203     }
204
205     __pci_add_prober(pci_loc, base, reg1);
206 }
207
208 static struct pci_registry*
209 __pci_registry_get(struct device_def* def)
210 {
211     struct pci_registry *pos, *n;
212     unsigned int hash;
213
214     hash = hash_32(__ptr(def), HSTR_FULL_HASH);
215     hashtable_hash_foreach(pci_drivers, hash, pos, n, entries)
216     {
217         if (pos->definition == def) {
218             return pos;
219         }
220     }
221
222     return NULL;
223 }
224
225 static int
226 __pci_proxied_devdef_load(struct device_def* def)
227 {
228     struct pci_registry* reg;
229     int errno = 0;
230
231     reg = __pci_registry_get(def);
232     assert(reg);
233
234     return pci_bind_driver(reg);
235 }
236
237 bool
238 pci_register_driver(struct device_def* def, pci_id_checker_t checker)
239 {
240     struct pci_registry* reg;
241     unsigned int hash;
242
243     if (!checker) {
244         return false;
245     }
246
247     if (__pci_registry_get(def)) {
248         return false;
249     }
250
251     reg = valloc(sizeof(*reg));
252     
253     *reg = (struct pci_registry) {
254         .check_compact = checker,
255         .definition = def,
256     };
257
258     device_chain_loader(def, __pci_proxied_devdef_load);
259
260     hash = hash_32(__ptr(def), HSTR_FULL_HASH);
261     hashtable_hash_in(pci_drivers, &reg->entries, hash);
262
263     return true;
264 }
265
266 void
267 pci_scan()
268 {
269     for (u32_t loc = 0; loc < (pciaddr_t)-1; loc += 8) {
270         pci_probe_device((pciaddr_t)loc);
271     }
272 }
273
274 static void
275 __pci_config_msi(struct pci_probe* probe, irq_t irq)
276 {
277     // PCI LB Spec. (Rev 3) Section 6.8 & 6.8.1
278
279     ptr_t msi_addr = irq->msi->wr_addr;
280     u32_t msi_data = irq->msi->message;
281
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;
285
286     pci_write_cspace(probe->cspace_base, 
287                      PCI_MSI_ADDR_LO(probe->msi_loc), 
288                      msi_addr);
289     
290     if (offset_cap64) {
291         pci_write_cspace(probe->cspace_base, 
292                          PCI_MSI_ADDR_HI(probe->msi_loc), 
293                          (u64_t)msi_addr >> 32);
294     }
295
296     pci_write_cspace(probe->cspace_base,
297                      PCI_MSI_DATA(probe->msi_loc, offset_cap64),
298                      msi_data & 0xffff);
299
300     if ((msg_ctl & MSI_CAP_MASK)) {
301         pci_write_cspace(
302           probe->cspace_base, PCI_MSI_MASK(probe->msi_loc, offset_cap64), 0);
303     }
304
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);
308 }
309
310 irq_t
311 pci_declare_msi_irq(irq_servant callback, struct pci_probe* probe)
312 {
313     return irq_declare_msg(callback, probe->loc, probe->loc);
314 }
315
316 int
317 pci_assign_msi(struct pci_probe* probe, irq_t irq, void* irq_spec)
318 {
319     int err = 0;
320
321     assert(irq->type == IRQ_MESSAGE);
322
323     err = irq_assign(probe->irq_domain, irq, irq_spec);
324     if (err) {
325         return err;
326     }
327
328     __pci_config_msi(probe, irq);
329     return 0;
330 }
331
332 size_t
333 pci_bar_sizing(struct pci_probe* probe, u32_t* bar_out, u32_t bar_num)
334 {
335     pci_reg_t sized, bar;
336     
337     bar = pci_read_cspace(probe->cspace_base, PCI_REG_BAR(bar_num));
338     if (!bar) {
339         *bar_out = 0;
340         return 0;
341     }
342
343     pci_write_cspace(probe->cspace_base, PCI_REG_BAR(bar_num), 0xffffffff);
344     
345     sized =
346       pci_read_cspace(probe->cspace_base, PCI_REG_BAR(bar_num)) & ~0x1;
347     
348     if (PCI_BAR_MMIO(bar)) {
349         sized = PCI_BAR_ADDR_MM(sized);
350     }
351     
352     *bar_out = bar;
353     pci_write_cspace(probe->cspace_base, PCI_REG_BAR(bar_num), bar);
354     
355     return ~sized + 1;
356 }
357
358 void
359 pci_apply_command(struct pci_probe* probe, pci_reg_t cmd)
360 {
361     pci_reg_t rcmd;
362     ptr_t base;
363
364     base = probe->cspace_base;
365     rcmd = pci_read_cspace(base, PCI_REG_STATUS_CMD);
366
367     cmd  = cmd & 0xffff;
368     rcmd = (rcmd & 0xffff0000) | cmd;
369
370     pci_write_cspace(base, PCI_REG_STATUS_CMD, rcmd);
371 }
372
373 static void
374 __pci_read_cspace(struct twimap* map)
375 {
376     struct pci_probe* probe;
377
378     probe = twimap_data(map, struct pci_probe*);
379
380     for (size_t i = 0; i < 256; i += sizeof(pci_reg_t)) {
381         *(pci_reg_t*)(map->buffer + i) =
382           pci_read_cspace(probe->cspace_base, i);
383     }
384
385     map->size_acc = 256;
386 }
387
388 /*---------- TwiFS interface definition ----------*/
389
390 static void
391 __pci_read_revid(struct twimap* map)
392 {
393     struct pci_probe* probe;
394
395     probe = twimap_data(map, struct pci_probe*);
396     twimap_printf(map, "0x%x", PCI_DEV_REV(probe->class_info));
397 }
398
399 static void
400 __pci_read_class(struct twimap* map)
401 {
402     struct pci_probe* probe;
403
404     probe = twimap_data(map, struct pci_probe*);
405     twimap_printf(map, "0x%x", PCI_DEV_CLASS(probe->class_info));
406 }
407
408 static void
409 __pci_read_devinfo(struct twimap* map)
410 {
411     struct pci_probe* probe;
412
413     probe = twimap_data(map, struct pci_probe*);
414     twimap_printf(map, "%x:%x", 
415             PCI_DEV_VENDOR(probe->device_info), 
416             PCI_DEV_DEVID(probe->device_info));
417 }
418
419 static void
420 __pci_bar_read(struct twimap* map)
421 {
422     struct pci_probe* probe;
423     int bar_index;
424
425     probe = twimap_data(map, struct pci_probe*);
426     bar_index = twimap_index(map, int);
427
428     struct pci_base_addr* bar = &probe->bar[bar_index];
429
430     if (!bar->start && !bar->size) {
431         twimap_printf(map, "[%d] not present \n", bar_index);
432         return;
433     }
434
435     twimap_printf(
436       map, "[%d] base=%.8p, size=%.8p, ", bar_index, bar->start, bar->size);
437
438     if ((bar->type & BAR_TYPE_MMIO)) {
439         twimap_printf(map, "mmio");
440         if ((bar->type & BAR_TYPE_CACHABLE)) {
441             twimap_printf(map, ", prefetchable");
442         }
443     } else {
444         twimap_printf(map, "io");
445     }
446
447     twimap_printf(map, "\n");
448 }
449
450 static int
451 __pci_bar_gonext(struct twimap* map)
452 {
453     if (twimap_index(map, int) >= 5) {
454         return 0;
455     }
456     map->index += 1;
457     return 1;
458 }
459
460 static void
461 __pci_read_binding(struct twimap* map)
462 {
463     struct pci_probe* probe;
464     struct devident* devid;
465
466     probe = twimap_data(map, struct pci_probe*);
467     if (!probe->bind) {
468         return;
469     }
470
471     devid = &probe->bind->ident;
472
473     twimap_printf(map, "%xh:%xh.%d",
474                   devid->fn_grp,
475                   DEV_KIND_FROM(devid->unique),
476                   DEV_VAR_FROM(devid->unique));
477 }
478
479 static void
480 __pci_trigger_bus_rescan(struct twimap* map)
481 {
482     pci_scan();
483 }
484
485 void
486 pci_build_fsmapping()
487 {
488     struct twifs_node *pci_class = twifs_dir_node(NULL, "pci"), *pci_dev;
489     struct twimap* map;
490     struct pci_probe* probe;
491     morph_t *pos, *n;
492
493     // TODO bus rescan is not ready yet
494     // map = twifs_mapping(pci_class, NULL, "rescan");
495     // map->read = __pci_trigger_bus_rescan;
496
497     changeling_for_each(pos, n, pci_probers)
498     {
499         probe = changeling_reveal(pos, pci_probe_morpher);
500         pci_dev = twifs_dir_node(pci_class, "%x", probe->loc);
501
502         map = twifs_mapping(pci_dev, probe, "config");
503         map->read = __pci_read_cspace;
504
505         map = twifs_mapping(pci_dev, probe, "revision");
506         map->read = __pci_read_revid;
507
508         map = twifs_mapping(pci_dev, probe, "class");
509         map->read = __pci_read_class;
510
511         map = twifs_mapping(pci_dev, probe, "binding");
512         map->read = __pci_read_binding;
513
514         map = twifs_mapping(pci_dev, probe, "io_bases");
515         map->read = __pci_bar_read;
516         map->go_next = __pci_bar_gonext;
517     }
518 }
519 EXPORT_TWIFS_PLUGIN(pci_devs, pci_build_fsmapping);
520
521 /*---------- PCI 3.0 HBA device definition ----------*/
522
523 static int
524 __pci_irq_install(struct irq_domain* domain, irq_t irq)
525 {
526     struct irq_domain* parent;
527     int err;
528
529     parent = domain->parent;
530     err = parent->ops->install_irq(parent, irq);
531     if (err) {
532         return err;
533     }
534
535     if (irq->type == IRQ_MESSAGE) {
536         irq->msi->message = irq->vector;
537     }
538
539     return 0;
540 }
541
542 static struct irq_domain_ops pci_irq_ops = {
543     .install_irq = __pci_irq_install
544 };
545
546 static int
547 pci_register(struct device_def* def)
548 {
549     pci_probers = changeling_spawn(NULL, "pci_realm");
550
551     return 0;
552 }
553
554 static int
555 pci_create(struct device_def* def, morph_t* obj)
556 {
557     struct irq_domain *pci_domain;
558     pci_bridge = device_allocsys(NULL, NULL);
559
560 #ifdef CONFIG_USE_DEVICETREE
561     devtree_link_t devtree_node;
562     devtree_node = changeling_try_reveal(obj, dt_node_morpher);
563     device_set_devtree_node(pci_bridge, devtree_node);
564 #endif
565
566     pci_domain = irq_create_domain(pci_bridge, &pci_irq_ops);
567     irq_attach_domain(irq_get_default_domain(), pci_domain);
568
569     register_device(pci_bridge, &def->class, "pci_bridge");
570     pci_scan();
571
572     return 0;
573 }
574
575 static struct device_def pci_def = {
576     def_device_name("Generic PCI"),
577     def_device_class(GENERIC, BUSIF, PCI),
578
579     def_on_register(pci_register),
580     def_on_create(pci_create)
581 };
582 EXPORT_DEVICE(pci3hba, &pci_def, load_onboot);