]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Bus/Isa/IsaBusDxe/IsaBus.c
Coding style modification.
[mirror_edk2.git] / IntelFrameworkModulePkg / Bus / Isa / IsaBusDxe / IsaBus.c
1 /**@file
2
3 Discovers all the ISA Controllers and their resources by using the ISA PnP
4 Protocol, produces an instance of the ISA I/O Protocol for every ISA
5 Controller found, loads and initializes all ISA Device Drivers, matches ISA
6 Device Drivers with their respective ISA Controllers in a deterministic
7 manner, and informs a ISA Device Driver when it is to start managing an ISA
8 Controller.
9
10 Copyright (c) 2006 - 2007, Intel Corporation.<BR>
11 All rights reserved. This program and the accompanying materials
12 are licensed and made available under the terms and conditions of the BSD License
13 which accompanies this distribution. The full text of the license may be found at
14 http://opensource.org/licenses/bsd-license.php
15
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18
19 **/
20
21
22 #include "InternalIsaBus.h"
23
24 //
25 // ISA Bus Driver Global Variables
26 //
27 EFI_DRIVER_BINDING_PROTOCOL gIsaBusControllerDriver = {
28 IsaBusControllerDriverSupported,
29 IsaBusControllerDriverStart,
30 IsaBusControllerDriverStop,
31 0xa,
32 NULL,
33 NULL
34 };
35
36 /**
37 The user Entry Point for module IsaBus. The user code starts with this function.
38
39 @param[in] ImageHandle The firmware allocated handle for the EFI image.
40 @param[in] SystemTable A pointer to the EFI System Table.
41
42 @retval EFI_SUCCESS The entry point is executed successfully.
43 @retval other Some error occurs when executing this entry point.
44
45 **/
46 EFI_STATUS
47 EFIAPI
48 InitializeIsaBus(
49 IN EFI_HANDLE ImageHandle,
50 IN EFI_SYSTEM_TABLE *SystemTable
51 )
52 {
53 EFI_STATUS Status;
54
55 //
56 // Install driver model protocol(s).
57 //
58 Status = EfiLibInstallDriverBindingComponentName2 (
59 ImageHandle,
60 SystemTable,
61 &gIsaBusControllerDriver,
62 ImageHandle,
63 &gIsaBusComponentName,
64 &gIsaBusComponentName2
65 );
66 ASSERT_EFI_ERROR (Status);
67
68
69 return Status;
70 }
71
72
73 EFI_STATUS
74 EFIAPI
75 IsaBusControllerDriverSupported (
76 IN EFI_DRIVER_BINDING_PROTOCOL * This,
77 IN EFI_HANDLE Controller,
78 IN EFI_DEVICE_PATH_PROTOCOL * RemainingDevicePath OPTIONAL
79 )
80 /**
81
82 Routine Description:
83
84 This function checks to see if a controller can be managed by the ISA Bus
85 Driver. This is done by checking to see if the controller supports the
86 EFI_PCI_IO_PROTOCOL protocol, and then looking at the PCI Configuration
87 Header to see if the device is a PCI to ISA bridge. The class code of
88 PCI to ISA bridge: Base class 06h, Sub class 01h Interface 00h
89
90 Arguments:
91
92 This - The EFI_DRIVER_BINDING_PROTOCOL instance.
93 Controller - The handle of the device to check.
94 RemainingDevicePath - A pointer to the remaining portion of a device path.
95
96 Returns:
97
98 EFI_SUCCESS - The device is supported by this driver.
99 EFI_UNSUPPORTED - The device is not supported by this driver.
100
101 **/
102 {
103 EFI_STATUS Status;
104 EFI_ISA_ACPI_PROTOCOL *IsaAcpi;
105
106 //
107 // If RemainingDevicePath is not NULL, it should verify that the first device
108 // path node in RemainingDevicePath is an ACPI Device path node
109 //
110 if (RemainingDevicePath != NULL) {
111 if (RemainingDevicePath->Type != ACPI_DEVICE_PATH) {
112 return EFI_UNSUPPORTED;
113 } else if (RemainingDevicePath->SubType == ACPI_DP) {
114 if (DevicePathNodeLength (RemainingDevicePath) != sizeof (ACPI_HID_DEVICE_PATH)) {
115 return EFI_UNSUPPORTED;
116 }
117 } else if (RemainingDevicePath->SubType == ACPI_EXTENDED_DP) {
118 if (DevicePathNodeLength (RemainingDevicePath) != sizeof (ACPI_EXTENDED_HID_DEVICE_PATH)) {
119 return EFI_UNSUPPORTED;
120 }
121 } else {
122 return EFI_UNSUPPORTED;
123 }
124 }
125 //
126 // Test the existence of DEVICE_PATH protocol
127 //
128 Status = gBS->OpenProtocol (
129 Controller,
130 &gEfiDevicePathProtocolGuid,
131 NULL,
132 This->DriverBindingHandle,
133 Controller,
134 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
135 );
136 if (EFI_ERROR (Status)) {
137 return Status;
138 }
139 //
140 // Get the Isa Acpi protocol
141 //
142 Status = gBS->OpenProtocol (
143 Controller,
144 &gEfiIsaAcpiProtocolGuid,
145 (VOID **) &IsaAcpi,
146 This->DriverBindingHandle,
147 Controller,
148 EFI_OPEN_PROTOCOL_BY_DRIVER
149 );
150 if (Status == EFI_ALREADY_STARTED) {
151 return EFI_SUCCESS;
152 }
153
154 if (EFI_ERROR (Status)) {
155 return Status;
156 }
157
158 gBS->CloseProtocol (
159 Controller,
160 &gEfiIsaAcpiProtocolGuid,
161 This->DriverBindingHandle,
162 Controller
163 );
164
165 return Status;
166 }
167
168 EFI_STATUS
169 EFIAPI
170 IsaBusControllerDriverStart (
171 IN EFI_DRIVER_BINDING_PROTOCOL * This,
172 IN EFI_HANDLE Controller,
173 IN EFI_DEVICE_PATH_PROTOCOL * RemainingDevicePath OPTIONAL
174 )
175 /**
176
177 Routine Description:
178
179 This function tells the ISA Bus Driver to start managing a PCI to ISA
180 Bridge controller.
181
182 Arguments:
183
184 This - The EFI_DRIVER_BINDING_PROTOCOL instance.
185 Controller - A handle to the device being started.
186 RemainingDevicePath - A pointer to the remaining portion of a device path.
187
188 Returns:
189
190 EFI_SUCCESS - The device was started.
191 EFI_UNSUPPORTED - The device is not supported.
192 EFI_DEVICE_ERROR - The device could not be started due to a device error.
193 EFI_ALREADY_STARTED - The device has already been started.
194 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.
195 EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of
196 resources.
197
198 **/
199 {
200 EFI_STATUS Status;
201 EFI_PCI_IO_PROTOCOL *PciIo;
202 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
203 EFI_ISA_ACPI_PROTOCOL *IsaAcpi;
204 EFI_ISA_ACPI_DEVICE_ID *IsaDevice;
205 EFI_ISA_ACPI_RESOURCE_LIST *ResourceList;
206 EFI_GENERIC_MEMORY_TEST_PROTOCOL *GenMemoryTest;
207
208 //
209 // Local variables declaration for StatusCode reporting
210 //
211 EFI_RESOURCE_ALLOC_FAILURE_ERROR_DATA AllocFailExtendedData;
212 EFI_DEVICE_PATH_PROTOCOL *DevicePathData;
213
214 //
215 // Initialize status code structure
216 //
217 AllocFailExtendedData.DataHeader.HeaderSize = sizeof (EFI_STATUS_CODE_DATA);
218 AllocFailExtendedData.DataHeader.Size = sizeof (EFI_RESOURCE_ALLOC_FAILURE_ERROR_DATA) - sizeof (EFI_STATUS_CODE_DATA);
219 CopyMem (
220 &AllocFailExtendedData.DataHeader.Type,
221 &gEfiStatusCodeSpecificDataGuid,
222 sizeof (EFI_GUID)
223 );
224
225 //
226 // Open Device Path Protocol
227 //
228 Status = gBS->OpenProtocol (
229 Controller,
230 &gEfiDevicePathProtocolGuid,
231 (VOID **) &ParentDevicePath,
232 This->DriverBindingHandle,
233 Controller,
234 EFI_OPEN_PROTOCOL_BY_DRIVER
235 );
236 if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {
237 return Status;
238 }
239 //
240 // Open Pci IO Protocol
241 //
242 Status = gBS->OpenProtocol (
243 Controller,
244 &gEfiPciIoProtocolGuid,
245 (VOID **) &PciIo,
246 This->DriverBindingHandle,
247 Controller,
248 EFI_OPEN_PROTOCOL_GET_PROTOCOL
249 );
250 if (EFI_ERROR (Status)) {
251 //
252 // Close opened protocol
253 //
254 gBS->CloseProtocol (
255 Controller,
256 &gEfiDevicePathProtocolGuid,
257 This->DriverBindingHandle,
258 Controller
259 );
260 return Status;
261 }
262 //
263 // Open ISA Acpi Protocol
264 //
265 Status = gBS->OpenProtocol (
266 Controller,
267 &gEfiIsaAcpiProtocolGuid,
268 (VOID **) &IsaAcpi,
269 This->DriverBindingHandle,
270 Controller,
271 EFI_OPEN_PROTOCOL_BY_DRIVER
272 );
273 if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {
274 //
275 // Close opened protocol
276 //
277 gBS->CloseProtocol (
278 Controller,
279 &gEfiDevicePathProtocolGuid,
280 This->DriverBindingHandle,
281 Controller
282 );
283 gBS->CloseProtocol (
284 Controller,
285 &gEfiPciIoProtocolGuid,
286 This->DriverBindingHandle,
287 Controller
288 );
289 return Status;
290 }
291 //
292 // The IsaBus driver will use memory below 16M, which is not tested yet,
293 // so call CompatibleRangeTest to test them. Since memory below 1M should
294 // be reserved to CSM, and 15M~16M might be reserved for Isa hole, test 1M
295 // ~15M here
296 //
297 Status = gBS->LocateProtocol (
298 &gEfiGenericMemTestProtocolGuid,
299 NULL,
300 (VOID **) &GenMemoryTest
301 );
302
303 if (!EFI_ERROR (Status)) {
304 Status = GenMemoryTest->CompatibleRangeTest (
305 GenMemoryTest,
306 0x100000,
307 0xE00000
308 );
309 }
310 //
311 // Report Status Code here since we will initialize the host controller
312 //
313 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
314 EFI_PROGRESS_CODE,
315 (EFI_IO_BUS_LPC | EFI_IOB_PC_INIT),
316 ParentDevicePath
317 );
318
319 //
320 // first init ISA interface
321 //
322 IsaAcpi->InterfaceInit (IsaAcpi);
323
324 //
325 // Report Status Code here since we will enable the host controller
326 //
327 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
328 EFI_PROGRESS_CODE,
329 (EFI_IO_BUS_LPC | EFI_IOB_PC_ENABLE),
330 ParentDevicePath
331 );
332
333 //
334 // Create each ISA device handle in this ISA bus
335 //
336 IsaDevice = NULL;
337 do {
338 Status = IsaAcpi->DeviceEnumerate (IsaAcpi, &IsaDevice);
339 if (EFI_ERROR (Status)) {
340 break;
341 }
342 //
343 // Get current resource of this ISA device
344 //
345 ResourceList = NULL;
346 Status = IsaAcpi->GetCurResource (IsaAcpi, IsaDevice, &ResourceList);
347 if (EFI_ERROR (Status)) {
348 continue;
349 }
350
351 //
352 // Create handle for this ISA device
353 //
354 Status = IsaCreateDevice (
355 This,
356 Controller,
357 PciIo,
358 ParentDevicePath,
359 ResourceList,
360 &DevicePathData
361 //&AllocFailExtendedData.DevicePath
362 );
363
364 if (EFI_ERROR (Status)) {
365 continue;
366 }
367 //
368 // Initialize ISA device
369 //
370 IsaAcpi->InitDevice (IsaAcpi, IsaDevice);
371
372 //
373 // Set resources for this ISA device
374 //
375 Status = IsaAcpi->SetResource (IsaAcpi, IsaDevice, ResourceList);
376
377 //
378 // Report Status Code here when failed to resource conflicts
379 //
380 if (EFI_ERROR (Status) && (Status != EFI_UNSUPPORTED)) {
381 //
382 // It's hard to tell which resource conflicts
383 //
384 AllocFailExtendedData.Bar = 0;
385 AllocFailExtendedData.ReqRes = NULL;
386 AllocFailExtendedData.AllocRes = NULL;
387 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
388 EFI_ERROR_CODE,
389 (EFI_IO_BUS_LPC | EFI_IOB_EC_RESOURCE_CONFLICT),
390 DevicePathData
391 );
392
393 }
394 //
395 // Set power for this ISA device
396 //
397 IsaAcpi->SetPower (IsaAcpi, IsaDevice, TRUE);
398
399 //
400 // Enable this ISA device
401 //
402 IsaAcpi->EnableDevice (IsaAcpi, IsaDevice, TRUE);
403
404 } while (TRUE);
405
406 return EFI_SUCCESS;
407 }
408
409 EFI_STATUS
410 EFIAPI
411 IsaBusControllerDriverStop (
412 IN EFI_DRIVER_BINDING_PROTOCOL * This,
413 IN EFI_HANDLE Controller,
414 IN UINTN NumberOfChildren,
415 IN EFI_HANDLE * ChildHandleBuffer OPTIONAL
416 )
417 /**
418
419 Routine Description:
420
421 This function tells the ISA Bus Driver to stop managing a PCI to ISA
422 Bridge controller.
423
424 Arguments:
425
426 This - The EFI_DRIVER_BINDING_PROTOCOL instance.
427 Controller - A handle to the device being stopped.
428 NumberOfChindren - The number of child device handles in ChildHandleBuffer.
429 ChildHandleBuffer - An array of child handles to be freed.
430
431
432 Returns:
433
434 EFI_SUCCESS - The device was stopped.
435 EFI_DEVICE_ERROR - The device could not be stopped due to a device error.
436 EFI_NOT_STARTED - The device has not been started.
437 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.
438 EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of
439 resources.
440
441 **/
442 {
443 EFI_STATUS Status;
444 UINTN Index;
445 BOOLEAN AllChildrenStopped;
446 ISA_IO_DEVICE *IsaIoDevice;
447 EFI_ISA_IO_PROTOCOL *IsaIo;
448
449 if (NumberOfChildren == 0) {
450 //
451 // Close the bus driver
452 //
453 Status = gBS->CloseProtocol (
454 Controller,
455 &gEfiPciIoProtocolGuid,
456 This->DriverBindingHandle,
457 Controller
458 );
459 if (EFI_ERROR (Status)) {
460 return Status;
461 }
462
463 Status = gBS->CloseProtocol (
464 Controller,
465 &gEfiDevicePathProtocolGuid,
466 This->DriverBindingHandle,
467 Controller
468 );
469 if (EFI_ERROR (Status)) {
470 return Status;
471 }
472
473 Status = gBS->CloseProtocol (
474 Controller,
475 &gEfiIsaAcpiProtocolGuid,
476 This->DriverBindingHandle,
477 Controller
478 );
479 if (EFI_ERROR (Status)) {
480 return Status;
481 }
482
483 return EFI_SUCCESS;
484 }
485 //
486 // Complete all outstanding transactions to Controller.
487 // Don't allow any new transaction to Controller to be started.
488 //
489 //
490 // Stop all the children
491 // Find all the ISA devices that were discovered on this PCI to ISA Bridge
492 // with the Start() function.
493 //
494 AllChildrenStopped = TRUE;
495
496 for (Index = 0; Index < NumberOfChildren; Index++) {
497
498 Status = gBS->OpenProtocol (
499 ChildHandleBuffer[Index],
500 &gEfiIsaIoProtocolGuid,
501 (VOID **) &IsaIo,
502 This->DriverBindingHandle,
503 Controller,
504 EFI_OPEN_PROTOCOL_GET_PROTOCOL
505 );
506 if (!EFI_ERROR (Status)) {
507
508 IsaIoDevice = ISA_IO_DEVICE_FROM_ISA_IO_THIS (IsaIo);
509
510 Status = gBS->UninstallMultipleProtocolInterfaces (
511 ChildHandleBuffer[Index],
512 &gEfiDevicePathProtocolGuid,
513 IsaIoDevice->DevicePath,
514 &gEfiIsaIoProtocolGuid,
515 &IsaIoDevice->IsaIo,
516 NULL
517 );
518
519 if (!EFI_ERROR (Status)) {
520 //
521 // Close the child handle
522 //
523 Status = gBS->CloseProtocol (
524 Controller,
525 &gEfiPciIoProtocolGuid,
526 This->DriverBindingHandle,
527 ChildHandleBuffer[Index]
528 );
529
530 gBS->FreePool (IsaIoDevice->DevicePath);
531 gBS->FreePool (IsaIoDevice);
532 }
533 }
534
535 if (EFI_ERROR (Status)) {
536 AllChildrenStopped = FALSE;
537 }
538 }
539
540 if (!AllChildrenStopped) {
541 return EFI_DEVICE_ERROR;
542 }
543
544 return EFI_SUCCESS;
545 }
546 //
547 // Internal Function
548 //
549 EFI_STATUS
550 IsaCreateDevice (
551 IN EFI_DRIVER_BINDING_PROTOCOL *This,
552 IN EFI_HANDLE Controller,
553 IN EFI_PCI_IO_PROTOCOL *PciIo,
554 IN EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath,
555 IN EFI_ISA_ACPI_RESOURCE_LIST *IsaDeviceResourceList,
556 OUT EFI_DEVICE_PATH_PROTOCOL **ChildDevicePath
557 )
558 /**
559
560 Routine Description:
561
562 Create ISA device found by IsaPnpProtocol
563
564 Arguments:
565
566 This - The EFI_DRIVER_BINDING_PROTOCOL instance.
567 Controller - The handle of ISA bus controller(PCI to ISA bridge)
568 PciIo - The Pointer to the PCI protocol
569 ParentDevicePath - Device path of the ISA bus controller
570 IsaDeviceResourceList - The resource list of the ISA device
571 ChildDevicePath - The pointer to the child device.
572
573 Returns:
574
575 EFI_SUCCESS - Create the child device.
576 EFI_OUT_OF_RESOURCES - The request could not be completed due to a lack of
577 resources.
578 EFI_DEVICE_ERROR - Can not create child device.
579
580 **/
581 {
582 EFI_STATUS Status;
583 ISA_IO_DEVICE *IsaIoDevice;
584 EFI_DEV_PATH Node;
585
586 //
587 // Initialize the PCI_IO_DEVICE structure
588 //
589 IsaIoDevice = AllocateZeroPool (sizeof (ISA_IO_DEVICE));
590 if (IsaIoDevice == NULL) {
591 return EFI_OUT_OF_RESOURCES;
592 }
593
594 IsaIoDevice->Signature = ISA_IO_DEVICE_SIGNATURE;
595 IsaIoDevice->Handle = NULL;
596 IsaIoDevice->PciIo = PciIo;
597
598 //
599 // Initialize the ISA I/O instance structure
600 //
601 Status = InitializeIsaIoInstance (IsaIoDevice, IsaDeviceResourceList);
602 if (EFI_ERROR (Status)) {
603 gBS->FreePool (IsaIoDevice);
604 return Status;
605 }
606 //
607 // Build the child device path
608 //
609 Node.DevPath.Type = ACPI_DEVICE_PATH;
610 Node.DevPath.SubType = ACPI_DP;
611 SetDevicePathNodeLength (&Node.DevPath, sizeof (ACPI_HID_DEVICE_PATH));
612 Node.Acpi.HID = IsaDeviceResourceList->Device.HID;
613 Node.Acpi.UID = IsaDeviceResourceList->Device.UID;
614
615 IsaIoDevice->DevicePath = AppendDevicePathNode (
616 ParentDevicePath,
617 &Node.DevPath
618 );
619
620 if (IsaIoDevice->DevicePath == NULL) {
621 Status = EFI_DEVICE_ERROR;
622 goto Done;
623 }
624
625 *ChildDevicePath = IsaIoDevice->DevicePath;
626
627 //
628 // Create a child handle and attach the DevicePath,
629 // PCI I/O, and Controller State
630 //
631 Status = gBS->InstallMultipleProtocolInterfaces (
632 &IsaIoDevice->Handle,
633 &gEfiDevicePathProtocolGuid,
634 IsaIoDevice->DevicePath,
635 &gEfiIsaIoProtocolGuid,
636 &IsaIoDevice->IsaIo,
637 NULL
638 );
639 if (EFI_ERROR (Status)) {
640 goto Done;
641 }
642
643 Status = gBS->OpenProtocol (
644 Controller,
645 &gEfiPciIoProtocolGuid,
646 (VOID **) &PciIo,
647 This->DriverBindingHandle,
648 IsaIoDevice->Handle,
649 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
650 );
651 if (EFI_ERROR (Status)) {
652 gBS->UninstallMultipleProtocolInterfaces (
653 IsaIoDevice->Handle,
654 &gEfiDevicePathProtocolGuid,
655 IsaIoDevice->DevicePath,
656 &gEfiIsaIoProtocolGuid,
657 &IsaIoDevice->IsaIo,
658 NULL
659 );
660 }
661
662 Done:
663
664 if (EFI_ERROR (Status)) {
665 if (IsaIoDevice->DevicePath != NULL) {
666 gBS->FreePool (IsaIoDevice->DevicePath);
667 }
668
669 gBS->FreePool (IsaIoDevice);
670 }
671
672 return Status;
673 }