rework external irq system, introduce hierarchical 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, 
312                     struct pci_probe* probe, void *irq_extra)
313 {
314     return irq_declare_msg(callback, probe->loc, probe->loc, irq_extra);
315 }
316
317 int
318 pci_assign_msi(struct pci_probe* probe, irq_t irq)
319 {
320     int err = 0;
321
322     assert(irq->type == IRQ_MESSAGE);
323
324     err = irq_assign(probe->irq_domain, irq);
325     if (err) {
326         return err;
327     }
328
329     __pci_config_msi(probe, irq);
330     return 0;
331 }
332
333 size_t
334 pci_bar_sizing(struct pci_probe* probe, u32_t* bar_out, u32_t bar_num)
335 {
336     pci_reg_t sized, bar;
337     
338     bar = pci_read_cspace(probe->cspace_base, PCI_REG_BAR(bar_num));
339     if (!bar) {
340         *bar_out = 0;
341         return 0;
342     }
343
344     pci_write_cspace(probe->cspace_base, PCI_REG_BAR(bar_num), 0xffffffff);
345     
346     sized =
347       pci_read_cspace(probe->cspace_base, PCI_REG_BAR(bar_num)) & ~0x1;
348     
349     if (PCI_BAR_MMIO(bar)) {
350         sized = PCI_BAR_ADDR_MM(sized);
351     }
352     
353     *bar_out = bar;
354     pci_write_cspace(probe->cspace_base, PCI_REG_BAR(bar_num), bar);
355     
356     return ~sized + 1;
357 }
358
359 void
360 pci_apply_command(struct pci_probe* probe, pci_reg_t cmd)
361 {
362     pci_reg_t rcmd;
363     ptr_t base;
364
365     base = probe->cspace_base;
366     rcmd = pci_read_cspace(base, PCI_REG_STATUS_CMD);
367
368     cmd  = cmd & 0xffff;
369     rcmd = (rcmd & 0xffff0000) | cmd;
370
371     pci_write_cspace(base, PCI_REG_STATUS_CMD, rcmd);
372 }
373
374 static void
375 __pci_read_cspace(struct twimap* map)
376 {
377     struct pci_probe* probe;
378
379     probe = twimap_data(map, struct pci_probe*);
380
381     for (size_t i = 0; i < 256; i += sizeof(pci_reg_t)) {
382         *(pci_reg_t*)(map->buffer + i) =
383           pci_read_cspace(probe->cspace_base, i);
384     }
385
386     map->size_acc = 256;
387 }
388
389 /*---------- TwiFS interface definition ----------*/
390
391 static void
392 __pci_read_revid(struct twimap* map)
393 {
394     struct pci_probe* probe;
395
396     probe = twimap_data(map, struct pci_probe*);
397     twimap_printf(map, "0x%x", PCI_DEV_REV(probe->class_info));
398 }
399
400 static void
401 __pci_read_class(struct twimap* map)
402 {
403     struct pci_probe* probe;
404
405     probe = twimap_data(map, struct pci_probe*);
406     twimap_printf(map, "0x%x", PCI_DEV_CLASS(probe->class_info));
407 }
408
409 static void
410 __pci_read_devinfo(struct twimap* map)
411 {
412     struct pci_probe* probe;
413
414     probe = twimap_data(map, struct pci_probe*);
415     twimap_printf(map, "%x:%x", 
416             PCI_DEV_VENDOR(probe->device_info), 
417             PCI_DEV_DEVID(probe->device_info));
418 }
419
420 static void
421 __pci_bar_read(struct twimap* map)
422 {
423     struct pci_probe* probe;
424     int bar_index;
425
426     probe = twimap_data(map, struct pci_probe*);
427     bar_index = twimap_index(map, int);
428
429     struct pci_base_addr* bar = &probe->bar[bar_index];
430
431     if (!bar->start && !bar->size) {
432         twimap_printf(map, "[%d] not present \n", bar_index);
433         return;
434     }
435
436     twimap_printf(
437       map, "[%d] base=%.8p, size=%.8p, ", bar_index, bar->start, bar->size);
438
439     if ((bar->type & BAR_TYPE_MMIO)) {
440         twimap_printf(map, "mmio");
441         if ((bar->type & BAR_TYPE_CACHABLE)) {
442             twimap_printf(map, ", prefetchable");
443         }
444     } else {
445         twimap_printf(map, "io");
446     }
447
448     twimap_printf(map, "\n");
449 }
450
451 static int
452 __pci_bar_gonext(struct twimap* map)
453 {
454     if (twimap_index(map, int) >= 5) {
455         return 0;
456     }
457     map->index += 1;
458     return 1;
459 }
460
461 static void
462 __pci_read_binding(struct twimap* map)
463 {
464     struct pci_probe* probe;
465     struct devident* devid;
466
467     probe = twimap_data(map, struct pci_probe*);
468     if (!probe->bind) {
469         return;
470     }
471
472     devid = &probe->bind->ident;
473
474     twimap_printf(map, "%xh:%xh.%d",
475                   devid->fn_grp,
476                   DEV_KIND_FROM(devid->unique),
477                   DEV_VAR_FROM(devid->unique));
478 }
479
480 static void
481 __pci_trigger_bus_rescan(struct twimap* map)
482 {
483     pci_scan();
484 }
485
486 void
487 pci_build_fsmapping()
488 {
489     struct twifs_node *pci_class = twifs_dir_node(NULL, "pci"), *pci_dev;
490     struct twimap* map;
491     struct pci_probe* probe;
492     morph_t *pos, *n;
493
494     // TODO bus rescan is not ready yet
495     // map = twifs_mapping(pci_class, NULL, "rescan");
496     // map->read = __pci_trigger_bus_rescan;
497
498     changeling_for_each(pos, n, pci_probers)
499     {
500         probe = changeling_reveal(pos, pci_probe_morpher);
501         pci_dev = twifs_dir_node(pci_class, "%x", probe->loc);
502
503         map = twifs_mapping(pci_dev, probe, "config");
504         map->read = __pci_read_cspace;
505
506         map = twifs_mapping(pci_dev, probe, "revision");
507         map->read = __pci_read_revid;
508
509         map = twifs_mapping(pci_dev, probe, "class");
510         map->read = __pci_read_class;
511
512         map = twifs_mapping(pci_dev, probe, "binding");
513         map->read = __pci_read_binding;
514
515         map = twifs_mapping(pci_dev, probe, "io_bases");
516         map->read = __pci_bar_read;
517         map->go_next = __pci_bar_gonext;
518     }
519 }
520 EXPORT_TWIFS_PLUGIN(pci_devs, pci_build_fsmapping);
521
522 /*---------- PCI 3.0 HBA device definition ----------*/
523
524 static int
525 __pci_irq_install(struct irq_domain* domain, irq_t irq)
526 {
527     struct irq_domain* parent;
528     int err;
529
530     parent = domain->parent;
531     err = parent->ops->install_irq(parent, irq);
532     if (err) {
533         return err;
534     }
535
536     if (irq->type == IRQ_MESSAGE) {
537         irq->msi->message = irq->vector;
538     }
539
540     return 0;
541 }
542
543 static struct irq_domain_ops pci_irq_ops = {
544     .install_irq = __pci_irq_install
545 };
546
547 static int
548 pci_register(struct device_def* def)
549 {
550     pci_probers = changeling_spawn(NULL, "pci_realm");
551
552     return 0;
553 }
554
555 static int
556 pci_create(struct device_def* def, morph_t* obj)
557 {
558     struct irq_domain *pci_domain;
559     pci_bridge = device_allocsys(NULL, NULL);
560
561 #ifdef CONFIG_USE_DEVICETREE
562     devtree_link_t devtree_node;
563     devtree_node = changeling_try_reveal(obj, dt_node_morpher);
564     device_set_devtree_node(pci_bridge, devtree_node);
565 #endif
566
567     pci_domain = irq_create_domain(pci_bridge, &pci_irq_ops);
568     irq_attach_domain(irq_get_default_domain(), pci_domain);
569
570     register_device(pci_bridge, &def->class, "pci_bridge");
571     pci_scan();
572
573     return 0;
574 }
575
576 static struct device_def pci_def = {
577     def_device_name("Generic PCI"),
578     def_device_class(GENERIC, BUSIF, PCI),
579
580     def_on_register(pci_register),
581     def_on_create(pci_create)
582 };
583 EXPORT_DEVICE(pci3hba, &pci_def, load_onboot);