Merge FldrCreateComponentKey and FldrSetComponentInformation
[reactos.git] / reactos / boot / freeldr / freeldr / arch / i386 / hwpci.c
1 /*
2 * FreeLoader
3 *
4 * Copyright (C) 2004 Eric Kohl
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <freeldr.h>
22
23 #define NDEBUG
24 #include <debug.h>
25
26 typedef struct _ROUTING_SLOT
27 {
28 UCHAR BusNumber;
29 UCHAR DeviceNumber;
30 UCHAR LinkA;
31 USHORT BitmapA;
32 UCHAR LinkB;
33 USHORT BitmapB;
34 UCHAR LinkC;
35 USHORT BitmapC;
36 UCHAR LinkD;
37 USHORT BitmapD;
38 UCHAR SlotNumber;
39 UCHAR Reserved;
40 } __attribute__((packed)) ROUTING_SLOT, *PROUTING_SLOT;
41
42 typedef struct _PCI_IRQ_ROUTING_TABLE
43 {
44 ULONG Signature;
45 USHORT Version;
46 USHORT Size;
47 UCHAR RouterBus;
48 UCHAR RouterSlot;
49 USHORT ExclusiveIRQs;
50 ULONG CompatibleRouter;
51 ULONG MiniportData;
52 UCHAR Reserved[11];
53 UCHAR Checksum;
54 ROUTING_SLOT Slot[1];
55 } __attribute__((packed)) PCI_IRQ_ROUTING_TABLE, *PPCI_IRQ_ROUTING_TABLE;
56
57 typedef struct _PCI_REGISTRY_INFO
58 {
59 UCHAR MajorRevision;
60 UCHAR MinorRevision;
61 UCHAR NoBuses;
62 UCHAR HardwareMechanism;
63 } PCI_REGISTRY_INFO, *PPCI_REGISTRY_INFO;
64
65 static PPCI_IRQ_ROUTING_TABLE
66 GetPciIrqRoutingTable(VOID)
67 {
68 PPCI_IRQ_ROUTING_TABLE Table;
69 PUCHAR Ptr;
70 ULONG Sum;
71 ULONG i;
72
73 Table = (PPCI_IRQ_ROUTING_TABLE)0xF0000;
74 while ((ULONG_PTR)Table < 0x100000)
75 {
76 if (Table->Signature == 0x52495024)
77 {
78 DPRINTM(DPRINT_HWDETECT,
79 "Found signature\n");
80
81 Ptr = (PUCHAR)Table;
82 Sum = 0;
83 for (i = 0; i < Table->Size; i++)
84 {
85 Sum += Ptr[i];
86 }
87
88 if ((Sum & 0xFF) != 0)
89 {
90 DPRINTM(DPRINT_HWDETECT,
91 "Invalid routing table\n");
92 return NULL;
93 }
94
95 DPRINTM(DPRINT_HWDETECT,
96 "Valid checksum\n");
97
98 return Table;
99 }
100
101 Table = (PPCI_IRQ_ROUTING_TABLE)((ULONG_PTR)Table + 0x10);
102 }
103
104 return NULL;
105 }
106
107
108 static BOOLEAN
109 FindPciBios(PPCI_REGISTRY_INFO BusData)
110 {
111 REGS RegsIn;
112 REGS RegsOut;
113
114 RegsIn.b.ah = 0xB1; /* Subfunction B1h */
115 RegsIn.b.al = 0x01; /* PCI BIOS present */
116
117 Int386(0x1A, &RegsIn, &RegsOut);
118
119 if (INT386_SUCCESS(RegsOut) && RegsOut.d.edx == 0x20494350 && RegsOut.b.ah == 0)
120 {
121 DPRINTM(DPRINT_HWDETECT, "Found PCI bios\n");
122
123 DPRINTM(DPRINT_HWDETECT, "AL: %x\n", RegsOut.b.al);
124 DPRINTM(DPRINT_HWDETECT, "BH: %x\n", RegsOut.b.bh);
125 DPRINTM(DPRINT_HWDETECT, "BL: %x\n", RegsOut.b.bl);
126 DPRINTM(DPRINT_HWDETECT, "CL: %x\n", RegsOut.b.cl);
127
128 BusData->NoBuses = RegsOut.b.cl + 1;
129 BusData->MajorRevision = RegsOut.b.bh;
130 BusData->MinorRevision = RegsOut.b.bl;
131 BusData->HardwareMechanism = RegsOut.b.cl;
132
133 return TRUE;
134 }
135
136
137 DPRINTM(DPRINT_HWDETECT, "No PCI bios found\n");
138
139 return FALSE;
140 }
141
142
143 static VOID
144 DetectPciIrqRoutingTable(PCONFIGURATION_COMPONENT_DATA BusKey)
145 {
146 PCM_PARTIAL_RESOURCE_LIST PartialResourceList;
147 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor;
148 PPCI_IRQ_ROUTING_TABLE Table;
149 PCONFIGURATION_COMPONENT_DATA TableKey;
150 ULONG Size;
151
152 Table = GetPciIrqRoutingTable();
153 if (Table != NULL)
154 {
155 DPRINTM(DPRINT_HWDETECT, "Table size: %u\n", Table->Size);
156
157 FldrCreateComponentKey(BusKey,
158 PeripheralClass,
159 RealModeIrqRoutingTable,
160 0x0,
161 0x0,
162 0xFFFFFFFF,
163 &TableKey);
164
165 /* Set 'Identifier' value */
166 FldrSetIdentifier(TableKey, "PCI Real-mode IRQ Routing Table");
167
168 /* Set 'Configuration Data' value */
169 Size = FIELD_OFFSET(CM_PARTIAL_RESOURCE_LIST, PartialDescriptors) +
170 2 * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) + Table->Size;
171 PartialResourceList = MmHeapAlloc(Size);
172 if (PartialResourceList == NULL)
173 {
174 DPRINTM(DPRINT_HWDETECT,
175 "Failed to allocate resource descriptor\n");
176 return;
177 }
178
179 /* Initialize resource descriptor */
180 memset(PartialResourceList, 0, Size);
181 PartialResourceList->Version = 1;
182 PartialResourceList->Revision = 1;
183 PartialResourceList->Count = 2;
184
185 PartialDescriptor = &PartialResourceList->PartialDescriptors[0];
186 PartialDescriptor->Type = CmResourceTypeBusNumber;
187 PartialDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
188 PartialDescriptor->u.BusNumber.Start = 0;
189 PartialDescriptor->u.BusNumber.Length = 1;
190
191 PartialDescriptor = &PartialResourceList->PartialDescriptors[1];
192 PartialDescriptor->Type = CmResourceTypeDeviceSpecific;
193 PartialDescriptor->ShareDisposition = CmResourceShareUndetermined;
194 PartialDescriptor->u.DeviceSpecificData.DataSize = Table->Size;
195
196 memcpy(&PartialResourceList->PartialDescriptors[2],
197 Table, Table->Size);
198
199 /* Set 'Configuration Data' value */
200 FldrSetConfigurationData(TableKey, PartialResourceList, Size);
201 MmHeapFree(PartialResourceList);
202 }
203 }
204
205
206 VOID
207 DetectPciBios(PCONFIGURATION_COMPONENT_DATA SystemKey, ULONG *BusNumber)
208 {
209 PCM_PARTIAL_RESOURCE_LIST PartialResourceList;
210 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor;
211 PCI_REGISTRY_INFO BusData;
212 PCONFIGURATION_COMPONENT_DATA BiosKey;
213 ULONG Size;
214 PCONFIGURATION_COMPONENT_DATA BusKey;
215 ULONG i;
216 CHAR szPci[] = "PCI";
217
218 /* Report the PCI BIOS */
219 if (FindPciBios(&BusData))
220 {
221 /* Create new bus key */
222 FldrCreateComponentKey(SystemKey,
223 AdapterClass,
224 MultiFunctionAdapter,
225 0x0,
226 0x0,
227 0xFFFFFFFF,
228 &BiosKey);
229
230 /* Increment bus number */
231 (*BusNumber)++;
232
233 /* Set 'Identifier' value */
234 FldrSetIdentifier(BiosKey, "PCI BIOS");
235
236 /* Set 'Configuration Data' value */
237 Size = FIELD_OFFSET(CM_PARTIAL_RESOURCE_LIST,
238 PartialDescriptors);
239 PartialResourceList = MmHeapAlloc(Size);
240 if (PartialResourceList == NULL)
241 {
242 DPRINTM(DPRINT_HWDETECT,
243 "Failed to allocate resource descriptor\n");
244 return;
245 }
246
247 /* Initialize resource descriptor */
248 memset(PartialResourceList, 0, Size);
249
250 /* Set 'Configuration Data' value */
251 FldrSetConfigurationData(BiosKey, PartialResourceList, Size);
252 MmHeapFree(PartialResourceList);
253
254 DetectPciIrqRoutingTable(BiosKey);
255
256 /* Report PCI buses */
257 for (i = 0; i < (ULONG)BusData.NoBuses; i++)
258 {
259 /* Create the bus key */
260 FldrCreateComponentKey(SystemKey,
261 AdapterClass,
262 MultiFunctionAdapter,
263 0x0,
264 0x0,
265 0xFFFFFFFF,
266 &BusKey);
267
268 /* Check if this is the first bus */
269 if (i == 0)
270 {
271 /* Set 'Configuration Data' value */
272 Size = FIELD_OFFSET(CM_PARTIAL_RESOURCE_LIST,
273 PartialDescriptors) +
274 sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) +
275 sizeof(PCI_REGISTRY_INFO);
276 PartialResourceList = MmHeapAlloc(Size);
277 if (!PartialResourceList)
278 {
279 DPRINTM(DPRINT_HWDETECT,
280 "Failed to allocate resource descriptor\n");
281 return;
282 }
283
284 /* Initialize resource descriptor */
285 memset(PartialResourceList, 0, Size);
286 PartialResourceList->Version = 1;
287 PartialResourceList->Revision = 1;
288 PartialResourceList->Count = 1;
289 PartialDescriptor = &PartialResourceList->PartialDescriptors[0];
290 PartialDescriptor->Type = CmResourceTypeDeviceSpecific;
291 PartialDescriptor->ShareDisposition = CmResourceShareUndetermined;
292 PartialDescriptor->u.DeviceSpecificData.DataSize = sizeof(PCI_REGISTRY_INFO);
293 memcpy(&PartialResourceList->PartialDescriptors[1],
294 &BusData,
295 sizeof(PCI_REGISTRY_INFO));
296
297 /* Set 'Configuration Data' value */
298 FldrSetConfigurationData(BusKey, PartialResourceList, Size);
299 MmHeapFree(PartialResourceList);
300 }
301 else
302 {
303 /* Set 'Configuration Data' value */
304 Size = FIELD_OFFSET(CM_PARTIAL_RESOURCE_LIST,
305 PartialDescriptors);
306 PartialResourceList = MmHeapAlloc(Size);
307 if (!PartialResourceList)
308 {
309 DPRINTM(DPRINT_HWDETECT,
310 "Failed to allocate resource descriptor\n");
311 return;
312 }
313
314 /* Initialize resource descriptor */
315 memset(PartialResourceList, 0, Size);
316
317 /* Set 'Configuration Data' value */
318 FldrSetConfigurationData(BusKey, PartialResourceList, Size);
319 MmHeapFree(PartialResourceList);
320 }
321
322 /* Increment bus number */
323 (*BusNumber)++;
324
325 /* Set 'Identifier' value */
326 FldrSetIdentifier(BusKey, szPci);
327 }
328 }
329 }
330
331 /* EOF */