refactor: more compact log message
[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/acpi/acpi.h>
12 #include <hal/apic.h>
13 #include <hal/pci.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 static struct llist_header pci_devices;
22
23 void
24 pci_probe_msi_info(struct pci_device* device);
25
26 void
27 pci_probe_device(int bus, int dev, int funct)
28 {
29     uint32_t base = PCI_ADDRESS(bus, dev, funct);
30     pci_reg_t reg1 = pci_read_cspace(base, 0);
31
32     // Vendor=0xffff则表示设备不存在
33     if (PCI_DEV_VENDOR(reg1) == PCI_VENDOR_INVLD) {
34         return;
35     }
36
37     pci_reg_t hdr_type = pci_read_cspace(base, 0xc);
38     hdr_type = (hdr_type >> 16) & 0xff;
39
40     // 防止堆栈溢出
41     // QEMU的ICH9/Q35实现似乎有点问题,对于多功能设备的每一个功能的header type
42     //  都将第七位置位。而virtualbox 就没有这个毛病。
43     if ((hdr_type & 0x80) && funct == 0) {
44         hdr_type = hdr_type & ~0x80;
45         // 探测多用途设备(multi-function device)
46         for (int i = 1; i < 7; i++) {
47             pci_probe_device(bus, dev, i);
48         }
49     }
50
51     if (hdr_type != PCI_TDEV) {
52         // XXX: 目前忽略所有桥接设备,比如PCI-PCI桥接器,或者是CardBus桥接器
53         return;
54     }
55
56     pci_reg_t intr = pci_read_cspace(base, 0x3c);
57     pci_reg_t class = pci_read_cspace(base, 0x8);
58
59     struct pci_device* device = vzalloc(sizeof(struct pci_device));
60     *device = (struct pci_device){ .cspace_base = base,
61                                    .class_info = class,
62                                    .device_info = reg1,
63                                    .intr_info = intr };
64
65     kprintf("dev.%d:%d:%d %x:%x\n",
66             bus,
67             dev,
68             funct,
69             PCI_DEV_VENDOR(reg1),
70             PCI_DEV_DEVID(reg1));
71
72     pci_probe_msi_info(device);
73     pci_probe_bar_info(device);
74
75     llist_append(&pci_devices, &device->dev_chain);
76 }
77
78 void
79 pci_probe()
80 {
81     // 暴力扫描所有PCI设备
82     // XXX: 尽管最多会有256条PCI总线,但就目前而言,只考虑bus #0就足够了
83     for (int bus = 0; bus < 256; bus++) {
84         for (int dev = 0; dev < 32; dev++) {
85             pci_probe_device(bus, dev, 0);
86         }
87     }
88 }
89
90 void
91 pci_probe_bar_info(struct pci_device* device)
92 {
93     uint32_t bar;
94     struct pci_base_addr* ba;
95     for (size_t i = 0; i < 6; i++) {
96         ba = &device->bar[i];
97         ba->size = pci_bar_sizing(device, &bar, i + 1);
98         if (PCI_BAR_MMIO(bar)) {
99             ba->start = PCI_BAR_ADDR_MM(bar);
100             ba->type |= PCI_BAR_CACHEABLE(bar) ? BAR_TYPE_CACHABLE : 0;
101             ba->type |= BAR_TYPE_MMIO;
102         } else {
103             ba->start = PCI_BAR_ADDR_IO(bar);
104         }
105     }
106 }
107
108 void
109 pci_probe_msi_info(struct pci_device* device)
110 {
111     // Note that Virtualbox have to use ICH9 chipset for MSI support.
112     // Qemu seems ok with default PIIX3, Bochs is pending to test...
113     //    See https://www.virtualbox.org/manual/ch03.html (section 3.5.1)
114     pci_reg_t status =
115       pci_read_cspace(device->cspace_base, PCI_REG_STATUS_CMD) >> 16;
116
117     if (!(status & 0x10)) {
118         device->msi_loc = 0;
119         return;
120     }
121
122     pci_reg_t cap_ptr = pci_read_cspace(device->cspace_base, 0x34) & 0xff;
123     uint32_t cap_hdr;
124
125     while (cap_ptr) {
126         cap_hdr = pci_read_cspace(device->cspace_base, cap_ptr);
127         if ((cap_hdr & 0xff) == 0x5) {
128             // MSI
129             device->msi_loc = cap_ptr;
130             return;
131         }
132         cap_ptr = (cap_hdr >> 8) & 0xff;
133     }
134 }
135
136 void
137 __pci_read_cspace(struct twimap* map)
138 {
139     struct pci_device* pcidev = (struct pci_device*)(map->data);
140
141     for (size_t i = 0; i < 256; i += sizeof(pci_reg_t)) {
142         *(pci_reg_t*)(map->buffer + i) =
143           pci_read_cspace(pcidev->cspace_base, i);
144     }
145
146     map->size_acc = 256;
147 }
148
149 void
150 __pci_read_revid(struct twimap* map)
151 {
152     int class = twimap_data(map, struct pci_device*)->class_info;
153     twimap_printf(map, "0x%x", PCI_DEV_REV(class));
154 }
155
156 void
157 __pci_read_class(struct twimap* map)
158 {
159     int class = twimap_data(map, struct pci_device*)->class_info;
160     twimap_printf(map, "0x%x", PCI_DEV_CLASS(class));
161 }
162
163 void
164 __pci_bar_read(struct twimap* map)
165 {
166     struct pci_device* pcidev = twimap_data(map, struct pci_device*);
167     int bar_index = twimap_index(map, int);
168
169     struct pci_base_addr* bar = &pcidev->bar[bar_index];
170
171     if (!bar->start && !bar->size) {
172         twimap_printf(map, "[%d] not present \n", bar_index);
173         return;
174     }
175
176     twimap_printf(
177       map, "[%d] base=%.8p, size=%.8p, ", bar_index, bar->start, bar->size);
178
179     if ((bar->type & BAR_TYPE_MMIO)) {
180         twimap_printf(map, "mmio");
181         if ((bar->type & BAR_TYPE_CACHABLE)) {
182             twimap_printf(map, ", prefetchable");
183         }
184     } else {
185         twimap_printf(map, "io");
186     }
187
188     twimap_printf(map, "\n");
189 }
190
191 int
192 __pci_bar_gonext(struct twimap* map)
193 {
194     if (twimap_index(map, int) >= 5) {
195         return 0;
196     }
197     map->index += 1;
198     return 1;
199 }
200
201 void
202 pci_build_fsmapping()
203 {
204     struct twifs_node *pci_class = twifs_dir_node(NULL, "pci"), *pci_dev;
205     struct pci_device *pos, *n;
206     struct twimap* map;
207     llist_for_each(pos, n, &pci_devices, dev_chain)
208     {
209         pci_dev = twifs_dir_node(pci_class,
210                                  "%.2d:%.2d:%.2d.%.4x:%.4x",
211                                  PCI_BUS_NUM(pos->cspace_base),
212                                  PCI_SLOT_NUM(pos->cspace_base),
213                                  PCI_FUNCT_NUM(pos->cspace_base),
214                                  PCI_DEV_VENDOR(pos->device_info),
215                                  PCI_DEV_DEVID(pos->device_info));
216
217         map = twifs_mapping(pci_dev, pos, "config");
218         map->read = __pci_read_cspace;
219
220         map = twifs_mapping(pci_dev, pos, "revision");
221         map->read = __pci_read_revid;
222
223         map = twifs_mapping(pci_dev, pos, "class");
224         map->read = __pci_read_class;
225
226         map = twifs_mapping(pci_dev, pos, "io_bases");
227         map->read = __pci_bar_read;
228         map->go_next = __pci_bar_gonext;
229     }
230 }
231
232 size_t
233 pci_bar_sizing(struct pci_device* dev, uint32_t* bar_out, uint32_t bar_num)
234 {
235     pci_reg_t bar = pci_read_cspace(dev->cspace_base, PCI_REG_BAR(bar_num));
236     if (!bar) {
237         *bar_out = 0;
238         return 0;
239     }
240
241     pci_write_cspace(dev->cspace_base, PCI_REG_BAR(bar_num), 0xffffffff);
242     pci_reg_t sized =
243       pci_read_cspace(dev->cspace_base, PCI_REG_BAR(bar_num)) & ~0x1;
244     if (PCI_BAR_MMIO(bar)) {
245         sized = PCI_BAR_ADDR_MM(sized);
246     }
247     *bar_out = bar;
248     pci_write_cspace(dev->cspace_base, PCI_REG_BAR(bar_num), bar);
249     return ~sized + 1;
250 }
251
252 void
253 pci_setup_msi(struct pci_device* device, int vector)
254 {
255     // Dest: APIC#0, Physical Destination, No redirection
256     uint32_t msi_addr = (__APIC_BASE_PADDR);
257
258     // Edge trigger, Fixed delivery
259     uint32_t msi_data = vector;
260
261     pci_write_cspace(
262       device->cspace_base, PCI_MSI_ADDR(device->msi_loc), msi_addr);
263
264     pci_reg_t reg1 = pci_read_cspace(device->cspace_base, device->msi_loc);
265     pci_reg_t msg_ctl = reg1 >> 16;
266
267     int offset = !!(msg_ctl & MSI_CAP_64BIT) * 4;
268     pci_write_cspace(device->cspace_base,
269                      PCI_MSI_DATA(device->msi_loc, offset),
270                      msi_data & 0xffff);
271
272     if ((msg_ctl & MSI_CAP_MASK)) {
273         pci_write_cspace(
274           device->cspace_base, PCI_MSI_MASK(device->msi_loc, offset), 0);
275     }
276
277     // manipulate the MSI_CTRL to allow device using MSI to request service.
278     reg1 = (reg1 & 0xff8fffff) | 0x10000;
279     pci_write_cspace(device->cspace_base, device->msi_loc, reg1);
280 }
281
282 struct pci_device*
283 pci_get_device_by_id(uint16_t vendorId, uint16_t deviceId)
284 {
285     uint32_t dev_info = vendorId | (deviceId << 16);
286     struct pci_device *pos, *n;
287     llist_for_each(pos, n, &pci_devices, dev_chain)
288     {
289         if (pos->device_info == dev_info) {
290             return pos;
291         }
292     }
293
294     return NULL;
295 }
296
297 struct pci_device*
298 pci_get_device_by_class(uint32_t class)
299 {
300     struct pci_device *pos, *n;
301     llist_for_each(pos, n, &pci_devices, dev_chain)
302     {
303         if (PCI_DEV_CLASS(pos->class_info) == class) {
304             return pos;
305         }
306     }
307
308     return NULL;
309 }
310
311 void
312 pci_init()
313 {
314     llist_init_head(&pci_devices);
315     acpi_context* acpi = acpi_get_context();
316     assert_msg(acpi, "ACPI not initialized.");
317     if (acpi->mcfg.alloc_num) {
318         // PCIe Enhanced Configuration Mechanism is supported.
319         // TODO: support PCIe addressing mechanism
320     }
321     // Otherwise, fallback to use legacy PCI 3.0 method.
322     pci_probe();
323
324     pci_build_fsmapping();
325 }