[HAL]: Bus support in the HAL actually creates a further wedge between the different...
[reactos.git] / reactos / hal / halx86 / generic / legacy / bus / bushndlr.c
1 /*
2 * PROJECT: ReactOS HAL
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: hal/halx86/generic/bus/bushndlr.c
5 * PURPOSE: Generic HAL Bus Handler Support
6 * PROGRAMMERS: Stefan Ginsberg (stefan.ginsberg@reactos.org)
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #include <hal.h>
12 #define NDEBUG
13 #include <debug.h>
14
15 /* GLOBALS ********************************************************************/
16
17 KSPIN_LOCK HalpBusDatabaseSpinLock;
18 KEVENT HalpBusDatabaseEvent;
19 LIST_ENTRY HalpAllBusHandlers;
20 PARRAY HalpBusTable;
21 PARRAY HalpConfigTable;
22
23 /* PRIVATE FUNCTIONS **********************************************************/
24
25 PARRAY
26 NTAPI
27 HalpAllocateArray(IN ULONG ArraySize)
28 {
29 PARRAY Array;
30 ULONG Size;
31
32 /* Compute array size */
33 if (ArraySize == MAXULONG) ArraySize = 0;
34 Size = ArraySize * sizeof(PARRAY) + sizeof(ARRAY);
35
36 /* Allocate the array */
37 Array = ExAllocatePoolWithTag(NonPagedPool,
38 Size,
39 'BusH');
40 if (!Array) KeBugCheckEx(HAL_MEMORY_ALLOCATION, Size, 0, (ULONG_PTR)__FILE__, __LINE__);
41
42 /* Initialize it */
43 Array->ArraySize = ArraySize;
44 RtlZeroMemory(Array->Element, sizeof(PVOID) * (ArraySize + 1));
45 return Array;
46 }
47
48 VOID
49 NTAPI
50 HalpGrowArray(IN PARRAY *CurrentArray,
51 IN PARRAY *NewArray)
52 {
53 PVOID Tmp;
54
55 /* Check if the current array doesn't exist yet, or if it's smaller than the new one */
56 if (!(*CurrentArray) || ((*NewArray)->ArraySize > (*CurrentArray)->ArraySize))
57 {
58 /* Does it exist (and can it fit?) */
59 if (*CurrentArray)
60 {
61 /* Copy the current array into the new one */
62 RtlCopyMemory(&(*NewArray)->Element,
63 &(*CurrentArray)->Element,
64 sizeof(PVOID) * ((*CurrentArray)->ArraySize + 1));
65 }
66
67 /* Swap the pointers (XOR swap would be more l33t) */
68 Tmp = *CurrentArray;
69 *CurrentArray = *NewArray;
70 *NewArray = Tmp;
71 }
72 }
73
74 PBUS_HANDLER
75 FASTCALL
76 HalpLookupHandler(IN PARRAY Array,
77 IN ULONG Type,
78 IN ULONG Number,
79 IN BOOLEAN AddReference)
80 {
81 PHAL_BUS_HANDLER Bus;
82 PBUS_HANDLER Handler = NULL;
83
84 /* Make sure the entry exists */
85 if (Array->ArraySize >= Type)
86 {
87 /* Retrieve it */
88 Array = Array->Element[Type];
89
90 /* Make sure the entry array exists */
91 if ((Array) && (Array->ArraySize >= Number))
92 {
93 /* Retrieve the bus and its handler */
94 Bus = Array->Element[Number];
95 Handler = &Bus->Handler;
96
97 /* Reference the handler if needed */
98 if (AddReference) Bus->ReferenceCount++;
99 }
100 }
101
102 /* Return the handler */
103 return Handler;
104 }
105
106 ULONG
107 NTAPI
108 HalpNoBusData(IN PBUS_HANDLER BusHandler,
109 IN PBUS_HANDLER RootHandler,
110 IN ULONG SlotNumber,
111 IN PVOID Buffer,
112 IN ULONG Offset,
113 IN ULONG Length)
114 {
115 /* Not implemented */
116 DPRINT1("STUB GetSetBusData\n");
117 return 0;
118 }
119
120 NTSTATUS
121 NTAPI
122 HalpNoAdjustResourceList(IN PBUS_HANDLER BusHandler,
123 IN PBUS_HANDLER RootHandler,
124 IN OUT PIO_RESOURCE_REQUIREMENTS_LIST *pResourceList)
125 {
126 DPRINT1("STUB Adjustment\n");
127 return STATUS_UNSUCCESSFUL;
128 }
129
130 NTSTATUS
131 NTAPI
132 HalpNoAssignSlotResources(IN PBUS_HANDLER BusHandler,
133 IN PBUS_HANDLER RootHandler,
134 IN PUNICODE_STRING RegistryPath,
135 IN PUNICODE_STRING DriverClassName OPTIONAL,
136 IN PDRIVER_OBJECT DriverObject,
137 IN PDEVICE_OBJECT DeviceObject OPTIONAL,
138 IN ULONG SlotNumber,
139 IN OUT PCM_RESOURCE_LIST *AllocatedResources)
140 {
141 DPRINT1("STUB Assignment\n");
142 return STATUS_NOT_SUPPORTED;
143 }
144
145 VOID
146 FASTCALL
147 HaliReferenceBusHandler(IN PBUS_HANDLER Handler)
148 {
149 PHAL_BUS_HANDLER Bus;
150
151 /* Find and reference the bus handler */
152 Bus = CONTAINING_RECORD(Handler, HAL_BUS_HANDLER, Handler);
153 Bus->ReferenceCount++;
154 }
155
156 VOID
157 FASTCALL
158 HaliDereferenceBusHandler(IN PBUS_HANDLER Handler)
159 {
160 PHAL_BUS_HANDLER Bus;
161
162 /* Find and dereference the bus handler */
163 Bus = CONTAINING_RECORD(Handler, HAL_BUS_HANDLER, Handler);
164 Bus->ReferenceCount--;
165 ASSERT(Bus->ReferenceCount != 0);
166 }
167
168 PBUS_HANDLER
169 FASTCALL
170 HaliHandlerForBus(IN INTERFACE_TYPE InterfaceType,
171 IN ULONG BusNumber)
172 {
173 /* Lookup the interface in the bus table */
174 return HalpLookupHandler(HalpBusTable, InterfaceType, BusNumber, FALSE);
175 }
176
177 PBUS_HANDLER
178 FASTCALL
179 HaliHandlerForConfigSpace(IN BUS_DATA_TYPE ConfigType,
180 IN ULONG BusNumber)
181 {
182 /* Lookup the configuration in the configuration table */
183 return HalpLookupHandler(HalpConfigTable, ConfigType, BusNumber, FALSE);
184 }
185
186 PBUS_HANDLER
187 FASTCALL
188 HaliReferenceHandlerForBus(IN INTERFACE_TYPE InterfaceType,
189 IN ULONG BusNumber)
190 {
191 /* Lookup the interface in the bus table, and reference the handler */
192 return HalpLookupHandler(HalpBusTable, InterfaceType, BusNumber, TRUE);
193 }
194
195 PBUS_HANDLER
196 FASTCALL
197 HaliReferenceHandlerForConfigSpace(IN BUS_DATA_TYPE ConfigType,
198 IN ULONG BusNumber)
199 {
200 /* Lookup the configuration in the configuration table and add a reference */
201 return HalpLookupHandler(HalpConfigTable, ConfigType, BusNumber, TRUE);
202 }
203
204 #ifndef _MINIHAL_
205 NTSTATUS
206 NTAPI
207 HaliRegisterBusHandler(IN INTERFACE_TYPE InterfaceType,
208 IN BUS_DATA_TYPE ConfigType,
209 IN ULONG BusNumber,
210 IN INTERFACE_TYPE ParentBusType,
211 IN ULONG ParentBusNumber,
212 IN ULONG ExtraData,
213 IN PINSTALL_BUS_HANDLER InstallCallback,
214 OUT PBUS_HANDLER *ReturnedBusHandler)
215 {
216 PHAL_BUS_HANDLER Bus, OldHandler = NULL;
217 PHAL_BUS_HANDLER* BusEntry;
218 PVOID CodeHandle;
219 PARRAY InterfaceArray, InterfaceBusNumberArray, ConfigArray, ConfigBusNumberArray;
220 PBUS_HANDLER ParentHandler;
221 KIRQL OldIrql;
222 NTSTATUS Status;
223 DPRINT1("HAL BUS REGISTRATION: %d.%d on bus %d with parent %d on bus %d\n",
224 InterfaceType, ConfigType, BusNumber, ParentBusType, ParentBusNumber);
225
226 /* Make sure we have a valid handler */
227 ASSERT((InterfaceType != InterfaceTypeUndefined) ||
228 (ConfigType != ConfigurationSpaceUndefined));
229
230 /* Allocate the bus handler */
231 Bus = ExAllocatePoolWithTag(NonPagedPool,
232 sizeof(HAL_BUS_HANDLER) + ExtraData,
233 'HsuB');
234 if (!Bus) return STATUS_INSUFFICIENT_RESOURCES;
235
236 /* Return the handler */
237 *ReturnedBusHandler = &Bus->Handler;
238
239 /* Don't page us out */
240 CodeHandle = MmLockPagableDataSection(&HaliRegisterBusHandler);
241
242 /* Synchronize with anyone else */
243 KeWaitForSingleObject(&HalpBusDatabaseEvent,
244 WrExecutive,
245 KernelMode,
246 FALSE,
247 NULL);
248
249 /* Check for unknown/root bus */
250 if (BusNumber == -1)
251 {
252 /* We must have an interface */
253 ASSERT(InterfaceType != InterfaceTypeUndefined);
254
255 /* Find the right bus */
256 BusNumber = 0;
257 while (HaliHandlerForBus(InterfaceType, BusNumber)) BusNumber++;
258 }
259
260 /* Allocate arrays for the handler */
261 InterfaceArray = HalpAllocateArray(InterfaceType);
262 InterfaceBusNumberArray = HalpAllocateArray(BusNumber);
263 ConfigArray = HalpAllocateArray(ConfigType);
264 ConfigBusNumberArray = HalpAllocateArray(BusNumber);
265
266 /* Only proceed if all allocations succeeded */
267 if (InterfaceArray && InterfaceBusNumberArray && ConfigArray && ConfigBusNumberArray)
268 {
269 /* Find the parent handler if any */
270 ParentHandler = HaliReferenceHandlerForBus(ParentBusType, ParentBusNumber);
271
272 /* Initialize the handler */
273 RtlZeroMemory(Bus, sizeof(HAL_BUS_HANDLER) + ExtraData);
274 Bus->ReferenceCount = 1;
275
276 /* Fill out bus data */
277 Bus->Handler.BusNumber = BusNumber;
278 Bus->Handler.InterfaceType = InterfaceType;
279 Bus->Handler.ConfigurationType = ConfigType;
280 Bus->Handler.ParentHandler = ParentHandler;
281
282 /* Fill out dummy handlers */
283 Bus->Handler.GetBusData = HalpNoBusData;
284 Bus->Handler.SetBusData = HalpNoBusData;
285 Bus->Handler.AdjustResourceList = HalpNoAdjustResourceList;
286 Bus->Handler.AssignSlotResources = HalpNoAssignSlotResources;
287
288 /* Make space for extra data */
289 if (ExtraData) Bus->Handler.BusData = Bus + 1;
290
291 /* Check for a parent handler */
292 if (ParentHandler)
293 {
294 /* Inherit the parent routines */
295 Bus->Handler.GetBusData = ParentHandler->GetBusData;
296 Bus->Handler.SetBusData = ParentHandler->SetBusData;
297 Bus->Handler.AdjustResourceList = ParentHandler->AdjustResourceList;
298 Bus->Handler.AssignSlotResources = ParentHandler->AssignSlotResources;
299 Bus->Handler.TranslateBusAddress = ParentHandler->TranslateBusAddress;
300 Bus->Handler.GetInterruptVector = ParentHandler->GetInterruptVector;
301 }
302
303 /* We don't support this yet */
304 ASSERT(!InstallCallback);
305
306 /* Lock the buses */
307 KeAcquireSpinLock(&HalpBusDatabaseSpinLock, &OldIrql);
308
309 /* Make space for the interface */
310 HalpGrowArray(&HalpBusTable, &InterfaceArray);
311
312 /* Check if we really have an interface */
313 if (InterfaceType != InterfaceTypeUndefined)
314 {
315 /* Make space for the association */
316 HalpGrowArray((PARRAY*)&HalpBusTable->Element[InterfaceType],
317 &InterfaceBusNumberArray);
318
319 /* Get the bus handler pointer */
320 BusEntry = (PHAL_BUS_HANDLER*)&((PARRAY)HalpBusTable->Element[InterfaceType])->Element[BusNumber];
321
322 /* Check if there was already a handler there, and set the new one */
323 if (*BusEntry) OldHandler = *BusEntry;
324 *BusEntry = Bus;
325 }
326
327 /* Now add a space for the configuration space */
328 HalpGrowArray(&HalpConfigTable, &ConfigArray);
329
330 /* Check if we really have one */
331 if (ConfigType != ConfigurationSpaceUndefined)
332 {
333 /* Make space for this association */
334 HalpGrowArray((PARRAY*)&HalpConfigTable->Element[ConfigType],
335 &ConfigBusNumberArray);
336
337 /* Get the bus handler pointer */
338 BusEntry = (PHAL_BUS_HANDLER*)&((PARRAY)HalpConfigTable->Element[ConfigType])->Element[BusNumber];
339 if (*BusEntry)
340 {
341 /* Get the old entry, but make sure it's the same we had before */
342 ASSERT((OldHandler == NULL) || (OldHandler == *BusEntry));
343 OldHandler = *BusEntry;
344 }
345
346 /* Set the new entry */
347 *BusEntry = Bus;
348 }
349
350 /* Link the adapter */
351 InsertTailList(&HalpAllBusHandlers, &Bus->AllHandlers);
352
353 /* Remove the old linkage */
354 Bus = OldHandler;
355 if (Bus) RemoveEntryList(&Bus->AllHandlers);
356
357 /* Release the lock */
358 KeReleaseSpinLock(&HalpBusDatabaseSpinLock, OldIrql);
359 Status = STATUS_SUCCESS;
360 }
361 else
362 {
363 /* Fail */
364 Status = STATUS_INSUFFICIENT_RESOURCES;
365 }
366
367 /* Signal the event */
368 KeSetEvent(&HalpBusDatabaseEvent, 0, FALSE);
369
370 /* Re-page the function */
371 MmUnlockPagableImageSection(CodeHandle);
372
373 /* Free all allocations */
374 if (Bus) ExFreePool(Bus);
375 if (InterfaceArray) ExFreePool(InterfaceArray);
376 if (InterfaceBusNumberArray) ExFreePool(InterfaceBusNumberArray);
377 if (ConfigArray) ExFreePool(ConfigArray);
378 if (ConfigBusNumberArray) ExFreePool(ConfigBusNumberArray);
379
380 /* And we're done */
381 DPRINT1("Bus handler has been registered: %p\n", *ReturnedBusHandler);
382 return Status;
383 }
384 #endif
385
386 VOID
387 NTAPI
388 HalpInitBusHandler(VOID)
389 {
390 /* Setup the bus lock */
391 KeInitializeSpinLock(&HalpBusDatabaseSpinLock);
392
393 /* Setup the bus event */
394 KeInitializeEvent(&HalpBusDatabaseEvent, SynchronizationEvent, TRUE);
395
396 /* Setup the bus configuration and bus table */
397 HalpBusTable = HalpAllocateArray(0);
398 HalpConfigTable = HalpAllocateArray(0);
399
400 /* Setup the bus list */
401 InitializeListHead(&HalpAllBusHandlers);
402
403 /* Setup the HAL Dispatch routines */
404 #ifndef _MINIHAL_
405 HalRegisterBusHandler = HaliRegisterBusHandler;
406 HalHandlerForBus = HaliHandlerForBus;
407 HalHandlerForConfigSpace = HaliHandlerForConfigSpace;
408 HalReferenceHandlerForBus = HaliReferenceHandlerForBus;
409 HalReferenceBusHandler = HaliReferenceBusHandler;
410 HalDereferenceBusHandler = HaliDereferenceBusHandler;
411 #endif
412 HalPciAssignSlotResources = HalpAssignSlotResources;
413 /* FIXME: Fix later */
414 #if 0
415 HalPciTranslateBusAddress = HaliTranslateBusAddress;
416 if (!HalFindBusAddressTranslation) HalFindBusAddressTranslation = HaliFindBusAddressTranslation;
417 #else
418 /* These should be written by the PCI driver later, but we give defaults */
419 HalPciTranslateBusAddress = HalpTranslateBusAddress;
420 HalFindBusAddressTranslation = HalpFindBusAddressTranslation;
421 #endif
422 }
423
424 /* EOF */