feat: a file system mapping for pci devices
[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 = valloc(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
67     llist_append(&pci_devices, &device->dev_chain);
68 }
69
70 void
71 pci_probe()
72 {
73     // 暴力扫描所有PCI设备
74     // XXX: 尽管最多会有256条PCI总线,但就目前而言,只考虑bus #0就足够了
75     for (int bus = 0; bus < 1; bus++) {
76         for (int dev = 0; dev < 32; dev++) {
77             pci_probe_device(bus, dev, 0);
78         }
79     }
80 }
81
82 void
83 pci_probe_msi_info(struct pci_device* device)
84 {
85     // Note that Virtualbox have to use ICH9 chipset for MSI support.
86     // Qemu seems ok with default PIIX3, Bochs is pending to test...
87     //    See https://www.virtualbox.org/manual/ch03.html (section 3.5.1)
88     pci_reg_t status =
89       pci_read_cspace(device->cspace_base, PCI_REG_STATUS_CMD) >> 16;
90
91     if (!(status & 0x10)) {
92         device->msi_loc = 0;
93         return;
94     }
95
96     pci_reg_t cap_ptr = pci_read_cspace(device->cspace_base, 0x34) & 0xff;
97     uint32_t cap_hdr;
98
99     while (cap_ptr) {
100         cap_hdr = pci_read_cspace(device->cspace_base, cap_ptr);
101         if ((cap_hdr & 0xff) == 0x5) {
102             // MSI
103             device->msi_loc = cap_ptr;
104             return;
105         }
106         cap_ptr = (cap_hdr >> 8) & 0xff;
107     }
108 }
109
110 #define PCI_PRINT_BAR_LISTING
111
112 int
113 __pci_read_cspace(struct v_inode* inode, void* buffer, size_t len, size_t fpos)
114 {
115     if (len < 256) {
116         return ERANGE;
117     }
118
119     struct twifs_node* node = (struct twifs_node*)(inode->data);
120     struct pci_device* pcidev = (struct pci_device*)(node->data);
121
122     for (size_t i = 0; i < 256; i += sizeof(pci_reg_t)) {
123         *(pci_reg_t*)(buffer + i) = pci_read_cspace(pcidev->cspace_base, i);
124     }
125
126     return 256;
127 }
128
129 void
130 pci_build_fsmapping()
131 {
132     struct twifs_node *pci_class = twifs_dir_node(NULL, "pci"), *pci_dev;
133     struct pci_device *pos, *n;
134     llist_for_each(pos, n, &pci_devices, dev_chain)
135     {
136         pci_dev = twifs_dir_node(pci_class,
137                                  "B%d:D%d:F%d.%x:%x",
138                                  PCI_BUS_NUM(pos->cspace_base),
139                                  PCI_SLOT_NUM(pos->cspace_base),
140                                  PCI_FUNCT_NUM(pos->cspace_base),
141                                  PCI_DEV_VENDOR(pos->device_info),
142                                  PCI_DEV_DEVID(pos->device_info));
143         struct twifs_node* fnode = twifs_file_node(pci_dev, "cspace");
144         fnode->data = pos;
145         fnode->ops.read = __pci_read_cspace;
146     }
147 }
148
149 size_t
150 pci_bar_sizing(struct pci_device* dev, uint32_t* bar_out, uint32_t bar_num)
151 {
152     pci_reg_t bar = pci_read_cspace(dev->cspace_base, PCI_REG_BAR(bar_num));
153     if (!bar) {
154         *bar_out = 0;
155         return 0;
156     }
157
158     pci_write_cspace(dev->cspace_base, PCI_REG_BAR(bar_num), 0xffffffff);
159     pci_reg_t sized =
160       pci_read_cspace(dev->cspace_base, PCI_REG_BAR(bar_num)) & ~0x1;
161     if (PCI_BAR_MMIO(bar)) {
162         sized = PCI_BAR_ADDR_MM(sized);
163     }
164     *bar_out = bar;
165     pci_write_cspace(dev->cspace_base, PCI_REG_BAR(bar_num), bar);
166     return ~sized + 1;
167 }
168
169 void
170 pci_setup_msi(struct pci_device* device, int vector)
171 {
172     // Dest: APIC#0, Physical Destination, No redirection
173     uint32_t msi_addr = (__APIC_BASE_PADDR);
174
175     // Edge trigger, Fixed delivery
176     uint32_t msi_data = vector;
177
178     pci_write_cspace(
179       device->cspace_base, PCI_MSI_ADDR(device->msi_loc), msi_addr);
180
181     pci_reg_t reg1 = pci_read_cspace(device->cspace_base, device->msi_loc);
182     pci_reg_t msg_ctl = reg1 >> 16;
183
184     int offset = !!(msg_ctl & MSI_CAP_64BIT) * 4;
185     pci_write_cspace(device->cspace_base,
186                      PCI_MSI_DATA(device->msi_loc, offset),
187                      msi_data & 0xffff);
188
189     if ((msg_ctl & MSI_CAP_MASK)) {
190         pci_write_cspace(
191           device->cspace_base, PCI_MSI_MASK(device->msi_loc, offset), 0);
192     }
193
194     // manipulate the MSI_CTRL to allow device using MSI to request service.
195     reg1 = (reg1 & 0xff8fffff) | 0x10000;
196     pci_write_cspace(device->cspace_base, device->msi_loc, reg1);
197 }
198
199 struct pci_device*
200 pci_get_device_by_id(uint16_t vendorId, uint16_t deviceId)
201 {
202     uint32_t dev_info = vendorId | (deviceId << 16);
203     struct pci_device *pos, *n;
204     llist_for_each(pos, n, &pci_devices, dev_chain)
205     {
206         if (pos->device_info == dev_info) {
207             return pos;
208         }
209     }
210
211     return NULL;
212 }
213
214 struct pci_device*
215 pci_get_device_by_class(uint32_t class)
216 {
217     struct pci_device *pos, *n;
218     llist_for_each(pos, n, &pci_devices, dev_chain)
219     {
220         if (PCI_DEV_CLASS(pos->class_info) == class) {
221             return pos;
222         }
223     }
224
225     return NULL;
226 }
227
228 void
229 pci_init()
230 {
231     llist_init_head(&pci_devices);
232     acpi_context* acpi = acpi_get_context();
233     assert_msg(acpi, "ACPI not initialized.");
234     if (acpi->mcfg.alloc_num) {
235         // PCIe Enhanced Configuration Mechanism is supported.
236         // TODO: support PCIe addressing mechanism
237     }
238     // Otherwise, fallback to use legacy PCI 3.0 method.
239     pci_probe();
240
241     pci_build_fsmapping();
242 }