6f962cfa0decbb0e42d9ee0c794cd09b1583c4fe
[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 /* Set 'Configuration Data' value */
158 Size = FIELD_OFFSET(CM_PARTIAL_RESOURCE_LIST, PartialDescriptors) +
159 2 * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) + Table->Size;
160 PartialResourceList = MmHeapAlloc(Size);
161 if (PartialResourceList == NULL)
162 {
163 DPRINTM(DPRINT_HWDETECT,
164 "Failed to allocate resource descriptor\n");
165 return;
166 }
167
168 /* Initialize resource descriptor */
169 memset(PartialResourceList, 0, Size);
170 PartialResourceList->Version = 1;
171 PartialResourceList->Revision = 1;
172 PartialResourceList->Count = 2;
173
174 PartialDescriptor = &PartialResourceList->PartialDescriptors[0];
175 PartialDescriptor->Type = CmResourceTypeBusNumber;
176 PartialDescriptor->ShareDisposition = CmResourceShareDeviceExclusive;
177 PartialDescriptor->u.BusNumber.Start = 0;
178 PartialDescriptor->u.BusNumber.Length = 1;
179
180 PartialDescriptor = &PartialResourceList->PartialDescriptors[1];
181 PartialDescriptor->Type = CmResourceTypeDeviceSpecific;
182 PartialDescriptor->ShareDisposition = CmResourceShareUndetermined;
183 PartialDescriptor->u.DeviceSpecificData.DataSize = Table->Size;
184
185 memcpy(&PartialResourceList->PartialDescriptors[2],
186 Table, Table->Size);
187
188 FldrCreateComponentKey(BusKey,
189 PeripheralClass,
190 RealModeIrqRoutingTable,
191 0x0,
192 0x0,
193 0xFFFFFFFF,
194 "PCI Real-mode IRQ Routing Table",
195 PartialResourceList,
196 Size,
197 &TableKey);
198
199 MmHeapFree(PartialResourceList);
200 }
201 }
202
203
204 VOID
205 DetectPciBios(PCONFIGURATION_COMPONENT_DATA SystemKey, ULONG *BusNumber)
206 {
207 PCM_PARTIAL_RESOURCE_LIST PartialResourceList;
208 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor;
209 PCI_REGISTRY_INFO BusData;
210 PCONFIGURATION_COMPONENT_DATA BiosKey;
211 ULONG Size;
212 PCONFIGURATION_COMPONENT_DATA BusKey;
213 ULONG i;
214
215 /* Report the PCI BIOS */
216 if (FindPciBios(&BusData))
217 {
218 /* Set 'Configuration Data' value */
219 Size = FIELD_OFFSET(CM_PARTIAL_RESOURCE_LIST,
220 PartialDescriptors);
221 PartialResourceList = MmHeapAlloc(Size);
222 if (PartialResourceList == NULL)
223 {
224 DPRINTM(DPRINT_HWDETECT,
225 "Failed to allocate resource descriptor\n");
226 return;
227 }
228
229 /* Initialize resource descriptor */
230 memset(PartialResourceList, 0, Size);
231
232 /* Create new bus key */
233 FldrCreateComponentKey(SystemKey,
234 AdapterClass,
235 MultiFunctionAdapter,
236 0x0,
237 0x0,
238 0xFFFFFFFF,
239 "PCI BIOS",
240 PartialResourceList,
241 Size,
242 &BiosKey);
243
244 /* Increment bus number */
245 (*BusNumber)++;
246
247 MmHeapFree(PartialResourceList);
248
249 DetectPciIrqRoutingTable(BiosKey);
250
251 /* Report PCI buses */
252 for (i = 0; i < (ULONG)BusData.NoBuses; i++)
253 {
254 /* Check if this is the first bus */
255 if (i == 0)
256 {
257 /* Set 'Configuration Data' value */
258 Size = FIELD_OFFSET(CM_PARTIAL_RESOURCE_LIST,
259 PartialDescriptors) +
260 sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) +
261 sizeof(PCI_REGISTRY_INFO);
262 PartialResourceList = MmHeapAlloc(Size);
263 if (!PartialResourceList)
264 {
265 DPRINTM(DPRINT_HWDETECT,
266 "Failed to allocate resource descriptor\n");
267 return;
268 }
269
270 /* Initialize resource descriptor */
271 memset(PartialResourceList, 0, Size);
272 PartialResourceList->Version = 1;
273 PartialResourceList->Revision = 1;
274 PartialResourceList->Count = 1;
275 PartialDescriptor = &PartialResourceList->PartialDescriptors[0];
276 PartialDescriptor->Type = CmResourceTypeDeviceSpecific;
277 PartialDescriptor->ShareDisposition = CmResourceShareUndetermined;
278 PartialDescriptor->u.DeviceSpecificData.DataSize = sizeof(PCI_REGISTRY_INFO);
279 memcpy(&PartialResourceList->PartialDescriptors[1],
280 &BusData,
281 sizeof(PCI_REGISTRY_INFO));
282 }
283 else
284 {
285 /* Set 'Configuration Data' value */
286 Size = FIELD_OFFSET(CM_PARTIAL_RESOURCE_LIST,
287 PartialDescriptors);
288 PartialResourceList = MmHeapAlloc(Size);
289 if (!PartialResourceList)
290 {
291 DPRINTM(DPRINT_HWDETECT,
292 "Failed to allocate resource descriptor\n");
293 return;
294 }
295
296 /* Initialize resource descriptor */
297 memset(PartialResourceList, 0, Size);
298 }
299
300 /* Create the bus key */
301 FldrCreateComponentKey(SystemKey,
302 AdapterClass,
303 MultiFunctionAdapter,
304 0x0,
305 0x0,
306 0xFFFFFFFF,
307 "PCI",
308 PartialResourceList,
309 Size,
310 &BusKey);
311
312 MmHeapFree(PartialResourceList);
313
314 /* Increment bus number */
315 (*BusNumber)++;
316 }
317 }
318 }
319
320 /* EOF */