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