3859e0c368e073449860319e834b22b61715d18f
[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
24 void
25 pci_probe_msi_info(struct pci_device* device);
26
27 static struct pci_device*
28 pci_create_device(ptr_t pci_base, int devinfo)
29 {
30     pci_reg_t class = pci_read_cspace(pci_base, 0x8);
31     struct hbucket* bucket = device_definitions_byif(DEVIF_PCI);
32
33     u32_t devid = PCI_DEV_DEVID(devinfo);
34     u32_t vendor = PCI_DEV_VENDOR(devinfo);
35
36     kappendf(".%x:%x, ", vendor, devid);
37
38     struct pci_device_def *pos, *n;
39     hashtable_bucket_foreach(bucket, pos, n, devdef.hlist_if)
40     {
41         if (pos->dev_class != PCI_DEV_CLASS(class)) {
42             continue;
43         }
44
45         int result = (pos->dev_vendor & vendor) == vendor &&
46                      (pos->dev_id & devid) == devid;
47
48         if (result) {
49             goto found;
50         }
51     }
52
53     kappendf(KWARN "unknown device\n");
54
55     return NULL;
56
57 found:
58     pci_reg_t intr = pci_read_cspace(pci_base, 0x3c);
59
60     struct pci_device* device = vzalloc(sizeof(struct pci_device));
61     device->class_info = class;
62     device->device_info = devinfo;
63     device->cspace_base = pci_base;
64     device->intr_info = intr;
65
66     device_prepare(&device->dev, &pos->devdef.class);
67
68     pci_probe_msi_info(device);
69     pci_probe_bar_info(device);
70
71     kappendf("%s (dev.%x:%x:%x) \n",
72              pos->devdef.name,
73              pos->devdef.class.meta,
74              pos->devdef.class.device,
75              pos->devdef.class.variant);
76
77     if (!pos->devdef.init_for) {
78         kappendf(KERROR "bad def\n");
79         goto fail;
80     }
81
82     int errno = pos->devdef.init_for(&pos->devdef, &device->dev);
83     if (errno) {
84         kappendf(KERROR "failed (e=%d)\n", errno);
85         goto fail;
86     }
87
88     llist_append(&pci_devices, &device->dev_chain);
89
90     return device;
91
92 fail:
93     vfree(device);
94     return NULL;
95 }
96
97 void
98 pci_probe_device(int bus, int dev, int funct)
99 {
100     u32_t base = PCI_ADDRESS(bus, dev, funct);
101     pci_reg_t reg1 = pci_read_cspace(base, 0);
102
103     // Vendor=0xffff则表示设备不存在
104     if (PCI_DEV_VENDOR(reg1) == PCI_VENDOR_INVLD) {
105         return;
106     }
107
108     pci_reg_t hdr_type = pci_read_cspace(base, 0xc);
109     hdr_type = (hdr_type >> 16) & 0xff;
110
111     // 防止堆栈溢出
112     // QEMU的ICH9/Q35实现似乎有点问题,对于多功能设备的每一个功能的header type
113     //  都将第七位置位。而virtualbox 就没有这个毛病。
114     if ((hdr_type & 0x80) && funct == 0) {
115         hdr_type = hdr_type & ~0x80;
116         // 探测多用途设备(multi-function device)
117         for (int i = 1; i < 7; i++) {
118             pci_probe_device(bus, dev, i);
119         }
120     }
121
122     if (hdr_type != PCI_TDEV) {
123         // XXX: 目前忽略所有桥接设备,比如PCI-PCI桥接器,或者是CardBus桥接器
124         return;
125     }
126
127     kprintf("pci.%d:%d:%d", bus, dev, funct);
128
129     pci_create_device(base, reg1);
130 }
131
132 void
133 pci_scan()
134 {
135     for (int bus = 0; bus < 256; bus++) {
136         for (int dev = 0; dev < 32; dev++) {
137             pci_probe_device(bus, dev, 0);
138         }
139     }
140 }
141
142 void
143 pci_probe_bar_info(struct pci_device* device)
144 {
145     u32_t bar;
146     struct pci_base_addr* ba;
147     for (size_t i = 0; i < 6; i++) {
148         ba = &device->bar[i];
149         ba->size = pci_bar_sizing(device, &bar, i + 1);
150         if (PCI_BAR_MMIO(bar)) {
151             ba->start = PCI_BAR_ADDR_MM(bar);
152             ba->type |= PCI_BAR_CACHEABLE(bar) ? BAR_TYPE_CACHABLE : 0;
153             ba->type |= BAR_TYPE_MMIO;
154         } else {
155             ba->start = PCI_BAR_ADDR_IO(bar);
156         }
157     }
158 }
159
160 void
161 pci_probe_msi_info(struct pci_device* device)
162 {
163     // Note that Virtualbox have to use ICH9 chipset for MSI support.
164     // Qemu seems ok with default PIIX3, Bochs is pending to test...
165     //    See https://www.virtualbox.org/manual/ch03.html (section 3.5.1)
166     pci_reg_t status =
167       pci_read_cspace(device->cspace_base, PCI_REG_STATUS_CMD) >> 16;
168
169     if (!(status & 0x10)) {
170         device->msi_loc = 0;
171         return;
172     }
173
174     pci_reg_t cap_ptr = pci_read_cspace(device->cspace_base, 0x34) & 0xff;
175     u32_t cap_hdr;
176
177     while (cap_ptr) {
178         cap_hdr = pci_read_cspace(device->cspace_base, cap_ptr);
179         if ((cap_hdr & 0xff) == 0x5) {
180             // MSI
181             device->msi_loc = cap_ptr;
182             return;
183         }
184         cap_ptr = (cap_hdr >> 8) & 0xff;
185     }
186 }
187
188 size_t
189 pci_bar_sizing(struct pci_device* dev, u32_t* bar_out, u32_t bar_num)
190 {
191     pci_reg_t bar = pci_read_cspace(dev->cspace_base, PCI_REG_BAR(bar_num));
192     if (!bar) {
193         *bar_out = 0;
194         return 0;
195     }
196
197     pci_write_cspace(dev->cspace_base, PCI_REG_BAR(bar_num), 0xffffffff);
198     pci_reg_t sized =
199       pci_read_cspace(dev->cspace_base, PCI_REG_BAR(bar_num)) & ~0x1;
200     if (PCI_BAR_MMIO(bar)) {
201         sized = PCI_BAR_ADDR_MM(sized);
202     }
203     *bar_out = bar;
204     pci_write_cspace(dev->cspace_base, PCI_REG_BAR(bar_num), bar);
205     return ~sized + 1;
206 }
207
208 struct pci_device*
209 pci_get_device_by_id(u16_t vendorId, u16_t deviceId)
210 {
211     u32_t dev_info = vendorId | (deviceId << 16);
212     struct pci_device *pos, *n;
213     llist_for_each(pos, n, &pci_devices, dev_chain)
214     {
215         if (pos->device_info == dev_info) {
216             return pos;
217         }
218     }
219
220     return NULL;
221 }
222
223 struct pci_device*
224 pci_get_device_by_class(u32_t class)
225 {
226     struct pci_device *pos, *n;
227     llist_for_each(pos, n, &pci_devices, dev_chain)
228     {
229         if (PCI_DEV_CLASS(pos->class_info) == class) {
230             return pos;
231         }
232     }
233
234     return NULL;
235 }
236
237 static void
238 __pci_read_cspace(struct twimap* map)
239 {
240     struct pci_device* pcidev = (struct pci_device*)(map->data);
241
242     for (size_t i = 0; i < 256; i += sizeof(pci_reg_t)) {
243         *(pci_reg_t*)(map->buffer + i) =
244           pci_read_cspace(pcidev->cspace_base, i);
245     }
246
247     map->size_acc = 256;
248 }
249
250 /*---------- TwiFS interface definition ----------*/
251
252 static void
253 __pci_read_revid(struct twimap* map)
254 {
255     int class = twimap_data(map, struct pci_device*)->class_info;
256     twimap_printf(map, "0x%x", PCI_DEV_REV(class));
257 }
258
259 static void
260 __pci_read_class(struct twimap* map)
261 {
262     int class = twimap_data(map, struct pci_device*)->class_info;
263     twimap_printf(map, "0x%x", PCI_DEV_CLASS(class));
264 }
265
266 static void
267 __pci_bar_read(struct twimap* map)
268 {
269     struct pci_device* pcidev = twimap_data(map, struct pci_device*);
270     int bar_index = twimap_index(map, int);
271
272     struct pci_base_addr* bar = &pcidev->bar[bar_index];
273
274     if (!bar->start && !bar->size) {
275         twimap_printf(map, "[%d] not present \n", bar_index);
276         return;
277     }
278
279     twimap_printf(
280       map, "[%d] base=%.8p, size=%.8p, ", bar_index, bar->start, bar->size);
281
282     if ((bar->type & BAR_TYPE_MMIO)) {
283         twimap_printf(map, "mmio");
284         if ((bar->type & BAR_TYPE_CACHABLE)) {
285             twimap_printf(map, ", prefetchable");
286         }
287     } else {
288         twimap_printf(map, "io");
289     }
290
291     twimap_printf(map, "\n");
292 }
293
294 static int
295 __pci_bar_gonext(struct twimap* map)
296 {
297     if (twimap_index(map, int) >= 5) {
298         return 0;
299     }
300     map->index += 1;
301     return 1;
302 }
303
304 void
305 pci_build_fsmapping()
306 {
307     struct twifs_node *pci_class = twifs_dir_node(NULL, "pci"), *pci_dev;
308     struct pci_device *pos, *n;
309     struct twimap* map;
310     llist_for_each(pos, n, &pci_devices, dev_chain)
311     {
312         pci_dev = twifs_dir_node(pci_class,
313                                  "%.2d:%.2d:%.2d.%.4x:%.4x",
314                                  PCI_BUS_NUM(pos->cspace_base),
315                                  PCI_SLOT_NUM(pos->cspace_base),
316                                  PCI_FUNCT_NUM(pos->cspace_base),
317                                  PCI_DEV_VENDOR(pos->device_info),
318                                  PCI_DEV_DEVID(pos->device_info));
319
320         map = twifs_mapping(pci_dev, pos, "config");
321         map->read = __pci_read_cspace;
322
323         map = twifs_mapping(pci_dev, pos, "revision");
324         map->read = __pci_read_revid;
325
326         map = twifs_mapping(pci_dev, pos, "class");
327         map->read = __pci_read_class;
328
329         map = twifs_mapping(pci_dev, pos, "io_bases");
330         map->read = __pci_bar_read;
331         map->go_next = __pci_bar_gonext;
332     }
333 }
334 EXPORT_TWIFS_PLUGIN(pci_devs, pci_build_fsmapping);
335
336 /*---------- PCI 3.0 HBA device definition ----------*/
337
338 static int
339 pci_load_devices(struct device_def* def)
340 {
341     pci_scan();
342
343     return 0;
344 }
345
346 static struct device_def pci_def = {
347     .name = "pci3.0-hba",
348     .class = DEVCLASS(DEVIF_SOC, DEVFN_BUSIF, DEV_BUS, 0),
349     .init = pci_load_devices
350 };
351 EXPORT_DEVICE(pci3hba, &pci_def, load_poststage);