aa6acd16f036ff0adb8b2c864a0370a54ae750a9
[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     pci_probe_msi_info(device);
66     pci_probe_bar_info(device);
67
68     llist_append(&pci_devices, &device->dev_chain);
69 }
70
71 void
72 pci_probe()
73 {
74     // 暴力扫描所有PCI设备
75     // XXX: 尽管最多会有256条PCI总线,但就目前而言,只考虑bus #0就足够了
76     for (int bus = 0; bus < 1; bus++) {
77         for (int dev = 0; dev < 32; dev++) {
78             pci_probe_device(bus, dev, 0);
79         }
80     }
81 }
82
83 void
84 pci_probe_bar_info(struct pci_device* device)
85 {
86     uint32_t bar;
87     struct pci_base_addr* ba;
88     for (size_t i = 0; i < 6; i++) {
89         ba = &device->bar[i];
90         ba->size = pci_bar_sizing(device, &bar, i + 1);
91         if (PCI_BAR_MMIO(bar)) {
92             ba->start = PCI_BAR_ADDR_MM(bar);
93             ba->type |= PCI_BAR_CACHEABLE(bar) ? BAR_TYPE_CACHABLE : 0;
94             ba->type |= BAR_TYPE_MMIO;
95         } else {
96             ba->start = PCI_BAR_ADDR_IO(bar);
97         }
98     }
99 }
100
101 void
102 pci_probe_msi_info(struct pci_device* device)
103 {
104     // Note that Virtualbox have to use ICH9 chipset for MSI support.
105     // Qemu seems ok with default PIIX3, Bochs is pending to test...
106     //    See https://www.virtualbox.org/manual/ch03.html (section 3.5.1)
107     pci_reg_t status =
108       pci_read_cspace(device->cspace_base, PCI_REG_STATUS_CMD) >> 16;
109
110     if (!(status & 0x10)) {
111         device->msi_loc = 0;
112         return;
113     }
114
115     pci_reg_t cap_ptr = pci_read_cspace(device->cspace_base, 0x34) & 0xff;
116     uint32_t cap_hdr;
117
118     while (cap_ptr) {
119         cap_hdr = pci_read_cspace(device->cspace_base, cap_ptr);
120         if ((cap_hdr & 0xff) == 0x5) {
121             // MSI
122             device->msi_loc = cap_ptr;
123             return;
124         }
125         cap_ptr = (cap_hdr >> 8) & 0xff;
126     }
127 }
128
129 void
130 __pci_read_cspace(struct twimap* map)
131 {
132     struct pci_device* pcidev = (struct pci_device*)(map->data);
133
134     for (size_t i = 0; i < 256; i += sizeof(pci_reg_t)) {
135         *(pci_reg_t*)(map->buffer + i) =
136           pci_read_cspace(pcidev->cspace_base, i);
137     }
138
139     map->size_acc = 256;
140 }
141
142 void
143 __pci_read_revid(struct twimap* map)
144 {
145     int class = twimap_data(map, struct pci_device*)->class_info;
146     twimap_printf(map, "0x%x", PCI_DEV_REV(class));
147 }
148
149 void
150 __pci_read_class(struct twimap* map)
151 {
152     int class = twimap_data(map, struct pci_device*)->class_info;
153     twimap_printf(map, "0x%x", PCI_DEV_CLASS(class));
154 }
155
156 void
157 __pci_bar_read(struct twimap* map)
158 {
159     struct pci_device* pcidev = twimap_data(map, struct pci_device*);
160     int bar_index = twimap_index(map, int);
161
162     struct pci_base_addr* bar = &pcidev->bar[bar_index];
163
164     if (!bar->start && !bar->size) {
165         twimap_printf(map, "[%d] not present \n", bar_index);
166         return;
167     }
168
169     twimap_printf(
170       map, "[%d] base=%.8p, size=%.8p, ", bar_index, bar->start, bar->size);
171
172     if ((bar->type & BAR_TYPE_MMIO)) {
173         twimap_printf(map, "mmio");
174         if ((bar->type & BAR_TYPE_CACHABLE)) {
175             twimap_printf(map, ", prefetchable");
176         }
177     } else {
178         twimap_printf(map, "io");
179     }
180
181     twimap_printf(map, "\n");
182 }
183
184 int
185 __pci_bar_gonext(struct twimap* map)
186 {
187     if (twimap_index(map, int) >= 5) {
188         return 0;
189     }
190     map->index += 1;
191     return 1;
192 }
193
194 void
195 pci_build_fsmapping()
196 {
197     struct twifs_node *pci_class = twifs_dir_node(NULL, "pci"), *pci_dev;
198     struct pci_device *pos, *n;
199     struct twimap* map;
200     llist_for_each(pos, n, &pci_devices, dev_chain)
201     {
202         pci_dev = twifs_dir_node(pci_class,
203                                  "%.2d:%.2d:%.2d.%.4x:%.4x",
204                                  PCI_BUS_NUM(pos->cspace_base),
205                                  PCI_SLOT_NUM(pos->cspace_base),
206                                  PCI_FUNCT_NUM(pos->cspace_base),
207                                  PCI_DEV_VENDOR(pos->device_info),
208                                  PCI_DEV_DEVID(pos->device_info));
209
210         map = twifs_mapping(pci_dev, pos, "config");
211         map->read = __pci_read_cspace;
212
213         map = twifs_mapping(pci_dev, pos, "revision");
214         map->read = __pci_read_revid;
215
216         map = twifs_mapping(pci_dev, pos, "class");
217         map->read = __pci_read_class;
218
219         map = twifs_mapping(pci_dev, pos, "io_bases");
220         map->read = __pci_bar_read;
221         map->go_next = __pci_bar_gonext;
222     }
223 }
224
225 size_t
226 pci_bar_sizing(struct pci_device* dev, uint32_t* bar_out, uint32_t bar_num)
227 {
228     pci_reg_t bar = pci_read_cspace(dev->cspace_base, PCI_REG_BAR(bar_num));
229     if (!bar) {
230         *bar_out = 0;
231         return 0;
232     }
233
234     pci_write_cspace(dev->cspace_base, PCI_REG_BAR(bar_num), 0xffffffff);
235     pci_reg_t sized =
236       pci_read_cspace(dev->cspace_base, PCI_REG_BAR(bar_num)) & ~0x1;
237     if (PCI_BAR_MMIO(bar)) {
238         sized = PCI_BAR_ADDR_MM(sized);
239     }
240     *bar_out = bar;
241     pci_write_cspace(dev->cspace_base, PCI_REG_BAR(bar_num), bar);
242     return ~sized + 1;
243 }
244
245 void
246 pci_setup_msi(struct pci_device* device, int vector)
247 {
248     // Dest: APIC#0, Physical Destination, No redirection
249     uint32_t msi_addr = (__APIC_BASE_PADDR);
250
251     // Edge trigger, Fixed delivery
252     uint32_t msi_data = vector;
253
254     pci_write_cspace(
255       device->cspace_base, PCI_MSI_ADDR(device->msi_loc), msi_addr);
256
257     pci_reg_t reg1 = pci_read_cspace(device->cspace_base, device->msi_loc);
258     pci_reg_t msg_ctl = reg1 >> 16;
259
260     int offset = !!(msg_ctl & MSI_CAP_64BIT) * 4;
261     pci_write_cspace(device->cspace_base,
262                      PCI_MSI_DATA(device->msi_loc, offset),
263                      msi_data & 0xffff);
264
265     if ((msg_ctl & MSI_CAP_MASK)) {
266         pci_write_cspace(
267           device->cspace_base, PCI_MSI_MASK(device->msi_loc, offset), 0);
268     }
269
270     // manipulate the MSI_CTRL to allow device using MSI to request service.
271     reg1 = (reg1 & 0xff8fffff) | 0x10000;
272     pci_write_cspace(device->cspace_base, device->msi_loc, reg1);
273 }
274
275 struct pci_device*
276 pci_get_device_by_id(uint16_t vendorId, uint16_t deviceId)
277 {
278     uint32_t dev_info = vendorId | (deviceId << 16);
279     struct pci_device *pos, *n;
280     llist_for_each(pos, n, &pci_devices, dev_chain)
281     {
282         if (pos->device_info == dev_info) {
283             return pos;
284         }
285     }
286
287     return NULL;
288 }
289
290 struct pci_device*
291 pci_get_device_by_class(uint32_t class)
292 {
293     struct pci_device *pos, *n;
294     llist_for_each(pos, n, &pci_devices, dev_chain)
295     {
296         if (PCI_DEV_CLASS(pos->class_info) == class) {
297             return pos;
298         }
299     }
300
301     return NULL;
302 }
303
304 void
305 pci_init()
306 {
307     llist_init_head(&pci_devices);
308     acpi_context* acpi = acpi_get_context();
309     assert_msg(acpi, "ACPI not initialized.");
310     if (acpi->mcfg.alloc_num) {
311         // PCIe Enhanced Configuration Mechanism is supported.
312         // TODO: support PCIe addressing mechanism
313     }
314     // Otherwise, fallback to use legacy PCI 3.0 method.
315     pci_probe();
316
317     pci_build_fsmapping();
318 }