- Further refactor the code by completing getting rid of the registry key parameters...
[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 _CM_PCI_BUS_DATA
58 {
59 UCHAR BusCount;
60 USHORT PciVersion;
61 UCHAR HardwareMechanism;
62 } __attribute__((packed)) CM_PCI_BUS_DATA, *PCM_PCI_BUS_DATA;
63
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)Table < 0x100000)
75 {
76 if (Table->Signature == 0x52495024)
77 {
78 DbgPrint((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 DbgPrint((DPRINT_HWDETECT,
91 "Invalid routing table\n"));
92 return NULL;
93 }
94
95 DbgPrint((DPRINT_HWDETECT,
96 "Valid checksum\n"));
97
98 return Table;
99 }
100
101 Table = (PPCI_IRQ_ROUTING_TABLE)((ULONG)Table + 0x10);
102 }
103
104 return NULL;
105 }
106
107
108 static BOOLEAN
109 FindPciBios(PCM_PCI_BUS_DATA 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 DbgPrint((DPRINT_HWDETECT, "Found PCI bios\n"));
122
123 DbgPrint((DPRINT_HWDETECT, "AL: %x\n", RegsOut.b.al));
124 DbgPrint((DPRINT_HWDETECT, "BH: %x\n", RegsOut.b.bh));
125 DbgPrint((DPRINT_HWDETECT, "BL: %x\n", RegsOut.b.bl));
126 DbgPrint((DPRINT_HWDETECT, "CL: %x\n", RegsOut.b.cl));
127
128 BusData->BusCount = RegsOut.b.cl + 1;
129 BusData->PciVersion = RegsOut.w.bx;
130 BusData->HardwareMechanism = RegsOut.b.cl;
131
132 return TRUE;
133 }
134
135
136 DbgPrint((DPRINT_HWDETECT, "No PCI bios found\n"));
137
138 return FALSE;
139 }
140
141
142 static VOID
143 DetectPciIrqRoutingTable(PCONFIGURATION_COMPONENT_DATA BusKey)
144 {
145 PCM_FULL_RESOURCE_DESCRIPTOR FullResourceDescriptor;
146 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor;
147 PPCI_IRQ_ROUTING_TABLE Table;
148 PCONFIGURATION_COMPONENT_DATA TableKey;
149 ULONG Size;
150
151 Table = GetPciIrqRoutingTable();
152 if (Table != NULL)
153 {
154 DbgPrint((DPRINT_HWDETECT, "Table size: %u\n", Table->Size));
155
156 FldrCreateComponentKey(BusKey,
157 L"RealModeIrqRoutingTable",
158 0,
159 SystemClass,
160 RealModeIrqRoutingTable,
161 &TableKey);
162
163 /* Set 'Component Information' */
164 FldrSetComponentInformation(TableKey,
165 0x0,
166 0x0,
167 0xFFFFFFFF);
168
169 /* Set 'Identifier' value */
170 FldrSetIdentifier(TableKey, L"PCI Real-mode IRQ Routing Table");
171
172 /* Set 'Configuration Data' value */
173 Size = FIELD_OFFSET(CM_FULL_RESOURCE_DESCRIPTOR, PartialResourceList.PartialDescriptors) +
174 2 * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) + Table->Size;
175 FullResourceDescriptor = MmAllocateMemory(Size);
176 if (FullResourceDescriptor == NULL)
177 {
178 DbgPrint((DPRINT_HWDETECT,
179 "Failed to allocate resource descriptor\n"));
180 return;
181 }
182
183 /* Initialize resource descriptor */
184 memset(FullResourceDescriptor, 0, Size);
185 FullResourceDescriptor->InterfaceType = Isa;
186 FullResourceDescriptor->BusNumber = 0;
187 FullResourceDescriptor->PartialResourceList.Version = 1;
188 FullResourceDescriptor->PartialResourceList.Revision = 1;
189 FullResourceDescriptor->PartialResourceList.Count = 2;
190
191 PartialDescriptor = &FullResourceDescriptor->PartialResourceList.PartialDescriptors[0];
192 PartialDescriptor->Type = CmResourceTypeBusNumber;
193 PartialDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
194 PartialDescriptor->u.BusNumber.Start = 0;
195 PartialDescriptor->u.BusNumber.Length = 1;
196
197 PartialDescriptor = &FullResourceDescriptor->PartialResourceList.PartialDescriptors[1];
198 PartialDescriptor->Type = CmResourceTypeDeviceSpecific;
199 PartialDescriptor->ShareDisposition = CmResourceShareUndetermined;
200 PartialDescriptor->u.DeviceSpecificData.DataSize = Table->Size;
201
202 memcpy(&FullResourceDescriptor->PartialResourceList.PartialDescriptors[2],
203 Table,
204 Table->Size);
205
206 /* Set 'Configuration Data' value */
207 FldrSetConfigurationData(TableKey, FullResourceDescriptor, Size);
208 MmFreeMemory(FullResourceDescriptor);
209 }
210 }
211
212
213 VOID
214 DetectPciBios(PCONFIGURATION_COMPONENT_DATA SystemKey, ULONG *BusNumber)
215 {
216 PCM_FULL_RESOURCE_DESCRIPTOR FullResourceDescriptor;
217 CM_PCI_BUS_DATA BusData;
218 PCONFIGURATION_COMPONENT_DATA BiosKey;
219 ULONG Size;
220 #if 0
221 PCONFIGURATION_COMPONENT_DATA BusKey;
222 ULONG i;
223 WCHAR szPci[] = L"PCI";
224 #endif
225
226 /* Report the PCI BIOS */
227 if (FindPciBios(&BusData))
228 {
229 /* Create new bus key */
230 FldrCreateComponentKey(SystemKey,
231 L"MultifunctionAdapter",
232 *BusNumber,
233 AdapterClass,
234 MultiFunctionAdapter,
235 &BiosKey);
236
237 /* Set 'Component Information' */
238 FldrSetComponentInformation(BiosKey,
239 0x0,
240 0x0,
241 0xFFFFFFFF);
242
243 /* Increment bus number */
244 (*BusNumber)++;
245
246 /* Set 'Identifier' value */
247 FldrSetIdentifier(BiosKey, L"PCI BIOS");
248
249 /* Set 'Configuration Data' value */
250 Size = sizeof(CM_FULL_RESOURCE_DESCRIPTOR);
251 FullResourceDescriptor = MmAllocateMemory(Size);
252 if (FullResourceDescriptor == NULL)
253 {
254 DbgPrint((DPRINT_HWDETECT,
255 "Failed to allocate resource descriptor\n"));
256 return;
257 }
258
259 /* Initialize resource descriptor */
260 memset(FullResourceDescriptor, 0, Size);
261 FullResourceDescriptor->InterfaceType = PCIBus;
262 FullResourceDescriptor->BusNumber = 0;
263 FullResourceDescriptor->PartialResourceList.Version = 1;
264 FullResourceDescriptor->PartialResourceList.Revision = 1;
265 FullResourceDescriptor->PartialResourceList.Count = 1;
266 FullResourceDescriptor->PartialResourceList.PartialDescriptors[0].Type = CmResourceTypeBusNumber;
267 FullResourceDescriptor->PartialResourceList.PartialDescriptors[0].ShareDisposition = CmResourceShareDeviceExclusive;
268 FullResourceDescriptor->PartialResourceList.PartialDescriptors[0].u.BusNumber.Start = 0;
269 FullResourceDescriptor->PartialResourceList.PartialDescriptors[0].u.BusNumber.Length = 1;
270
271 /* Set 'Configuration Data' value */
272 FldrSetConfigurationData(BiosKey, FullResourceDescriptor, Size);
273 MmFreeMemory(FullResourceDescriptor);
274
275 DetectPciIrqRoutingTable(BiosKey);
276
277 #if 0
278 /*
279 * FIXME:
280 * Enabling this piece of code will corrupt the boot sequence!
281 * This is probably caused by a bug in the registry code!
282 */
283
284 /* Report PCI buses */
285 for (i = 0; i < (ULONG)BusData.BusCount; i++)
286 {
287 FldrCreateComponentKey(SystemKey,
288 L"MultifunctionAdapter",
289 *BusNumber,
290 AdapterClass,
291 MultiFunctionAdapter,
292 &BiosKey);
293
294 /* Set 'Component Information' */
295 FldrSetComponentInformation(BusKey,
296 0x0,
297 0x0,
298 0xFFFFFFFF);
299
300 /* Increment bus number */
301 (*BusNumber)++;
302
303 /* Set 'Identifier' value */
304 FldrSetIdentifier(BiosKey, szPci);
305 }
306 #endif
307 }
308 }
309
310 /* EOF */