update the comments on Event
[edk2.git] / IntelFrameworkModulePkg / Bus / Isa / IsaBusDxe / IsaBus.c
blob8cf9ac9c2cfc0f2ace0b1572cfe5f921cce20500
1 /**@file
2 ISA Bus UEFI driver.
4 Discovers all the ISA Controllers and their resources by using the ISA ACPI
5 Protocol, produces an instance of the ISA I/O Protocol for every ISA
6 Controller found. This driver is designed to manage a PCI-to-ISA bridge Device
7 such as LPC bridge.
9 Copyright (c) 2006 - 2009, Intel Corporation.<BR>
10 All rights reserved. This program and the accompanying materials
11 are licensed and made available under the terms and conditions of the BSD License
12 which accompanies this distribution. The full text of the license may be found at
13 http://opensource.org/licenses/bsd-license.php
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 **/
20 #include "InternalIsaBus.h"
23 // ISA Bus Driver Global Variables
25 EFI_DRIVER_BINDING_PROTOCOL gIsaBusControllerDriver = {
26 IsaBusControllerDriverSupported,
27 IsaBusControllerDriverStart,
28 IsaBusControllerDriverStop,
29 0xa,
30 NULL,
31 NULL
34 /**
35 The main entry point for the ISA Bus driver.
37 @param[in] ImageHandle The firmware allocated handle for the EFI image.
38 @param[in] SystemTable A pointer to the EFI System Table.
40 @retval EFI_SUCCESS The entry point is executed successfully.
41 @retval EFI_OUT_OF_RESOURCES There was not enough memory in pool to install all the protocols.
42 **/
43 EFI_STATUS
44 EFIAPI
45 InitializeIsaBus(
46 IN EFI_HANDLE ImageHandle,
47 IN EFI_SYSTEM_TABLE *SystemTable
50 EFI_STATUS Status;
53 // Install driver model protocol(s).
55 Status = EfiLibInstallDriverBindingComponentName2 (
56 ImageHandle,
57 SystemTable,
58 &gIsaBusControllerDriver,
59 ImageHandle,
60 &gIsaBusComponentName,
61 &gIsaBusComponentName2
63 ASSERT_EFI_ERROR (Status);
65 return Status;
68 /**
69 Tests to see if a controller can be managed by the ISA Bus Driver. If a child device is provided,
70 it further tests to see if this driver supports creating a handle for the specified child device.
72 Note that the ISA Bus driver always creates all of its child handles on the first call to Start().
73 How the Start() function of a driver is implemented can affect how the Supported() function is implemented.
75 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
76 @param[in] Controller The handle of the controller to test.
77 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path.
79 @retval EFI_SUCCESS The device is supported by this driver.
80 @retval EFI_ALREADY_STARTED The device is already being managed by this driver.
81 @retval EFI_ACCESS_DENIED The device is already being managed by a different driver
82 or an application that requires exclusive access.
83 @retval EFI_UNSUPPORTED The device is is not supported by this driver.
85 **/
86 EFI_STATUS
87 EFIAPI
88 IsaBusControllerDriverSupported (
89 IN EFI_DRIVER_BINDING_PROTOCOL *This,
90 IN EFI_HANDLE Controller,
91 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
94 EFI_STATUS Status;
95 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
96 EFI_ISA_ACPI_PROTOCOL *IsaAcpi;
99 // If RemainingDevicePath is not NULL, it should verify that the first device
100 // path node in RemainingDevicePath is an ACPI Device path node which is a
101 // legal Device Path Node for this bus driver's children.
103 if (RemainingDevicePath != NULL) {
104 if (RemainingDevicePath->Type != ACPI_DEVICE_PATH) {
105 return EFI_UNSUPPORTED;
106 } else if (RemainingDevicePath->SubType == ACPI_DP) {
107 if (DevicePathNodeLength (RemainingDevicePath) != sizeof (ACPI_HID_DEVICE_PATH)) {
108 return EFI_UNSUPPORTED;
110 } else if (RemainingDevicePath->SubType == ACPI_EXTENDED_DP) {
111 if (DevicePathNodeLength (RemainingDevicePath) != sizeof (ACPI_EXTENDED_HID_DEVICE_PATH)) {
112 return EFI_UNSUPPORTED;
114 } else {
115 return EFI_UNSUPPORTED;
119 // Try to open EFI DEVICE PATH protocol on the controller
121 Status = gBS->OpenProtocol (
122 Controller,
123 &gEfiDevicePathProtocolGuid,
124 (VOID **) &ParentDevicePath,
125 This->DriverBindingHandle,
126 Controller,
127 EFI_OPEN_PROTOCOL_BY_DRIVER
130 // Although this driver creates all child handles at one time,
131 // but because all child handles may be not stopped at one time in EFI Driver Binding.Stop(),
132 // So it is allowed to create child handles again in successive calls to EFI Driver Binding.Start().
134 if (Status == EFI_ALREADY_STARTED) {
135 return EFI_SUCCESS;
138 if (EFI_ERROR (Status)) {
139 return Status;
142 gBS->CloseProtocol (
143 Controller,
144 &gEfiDevicePathProtocolGuid,
145 This->DriverBindingHandle,
146 Controller
150 // Try to get Pci IO Protocol because it is assumed
151 // to have been opened by ISA ACPI driver
153 Status = gBS->OpenProtocol (
154 Controller,
155 &gEfiPciIoProtocolGuid,
156 NULL,
157 This->DriverBindingHandle,
158 Controller,
159 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
161 if (EFI_ERROR (Status)) {
162 return Status;
166 // Try to open the Isa Acpi protocol on the controller
168 Status = gBS->OpenProtocol (
169 Controller,
170 &gEfiIsaAcpiProtocolGuid,
171 (VOID **) &IsaAcpi,
172 This->DriverBindingHandle,
173 Controller,
174 EFI_OPEN_PROTOCOL_BY_DRIVER
176 if (EFI_ERROR (Status)) {
177 return Status;
181 // Add more check to see if the child device is valid by calling IsaAcpi->DeviceEnumerate?
184 gBS->CloseProtocol (
185 Controller,
186 &gEfiIsaAcpiProtocolGuid,
187 This->DriverBindingHandle,
188 Controller
191 return Status;
195 Start this driver on ControllerHandle.
197 Note that the ISA Bus driver always creates all of its child handles on the first call to Start().
198 The Start() function is designed to be invoked from the EFI boot service ConnectController().
199 As a result, much of the error checking on the parameters to Start() has been moved into this
200 common boot service. It is legal to call Start() from other locations, but the following calling
201 restrictions must be followed or the system behavior will not be deterministic.
202 1. ControllerHandle must be a valid EFI_HANDLE.
203 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
204 EFI_DEVICE_PATH_PROTOCOL.
205 3. Prior to calling Start(), the Supported() function for the driver specified by This must
206 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
208 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
209 @param[in] ControllerHandle The handle of the controller to start. This handle
210 must support a protocol interface that supplies
211 an I/O abstraction to the driver.
212 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path.
213 This parameter is ignored by device drivers, and is optional for bus drivers.
215 @retval EFI_SUCCESS The device was started.
216 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.
217 Currently not implemented.
218 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
219 @retval Others The driver failded to start the device.
221 EFI_STATUS
222 EFIAPI
223 IsaBusControllerDriverStart (
224 IN EFI_DRIVER_BINDING_PROTOCOL *This,
225 IN EFI_HANDLE Controller,
226 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
229 EFI_STATUS Status;
230 EFI_PCI_IO_PROTOCOL *PciIo;
231 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
232 EFI_ISA_ACPI_PROTOCOL *IsaAcpi;
233 EFI_ISA_ACPI_DEVICE_ID *IsaDevice;
234 EFI_ISA_ACPI_RESOURCE_LIST *ResourceList;
235 EFI_GENERIC_MEMORY_TEST_PROTOCOL *GenMemoryTest;
238 // Local variables declaration for StatusCode reporting
240 EFI_DEVICE_PATH_PROTOCOL *DevicePathData;
243 // Get Pci IO Protocol
245 Status = gBS->OpenProtocol (
246 Controller,
247 &gEfiPciIoProtocolGuid,
248 (VOID **) &PciIo,
249 This->DriverBindingHandle,
250 Controller,
251 EFI_OPEN_PROTOCOL_GET_PROTOCOL
253 if (EFI_ERROR (Status)) {
254 return Status;
258 // Open Device Path Protocol
260 Status = gBS->OpenProtocol (
261 Controller,
262 &gEfiDevicePathProtocolGuid,
263 (VOID **) &ParentDevicePath,
264 This->DriverBindingHandle,
265 Controller,
266 EFI_OPEN_PROTOCOL_BY_DRIVER
268 if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {
269 return Status;
273 // Open ISA Acpi Protocol
275 Status = gBS->OpenProtocol (
276 Controller,
277 &gEfiIsaAcpiProtocolGuid,
278 (VOID **) &IsaAcpi,
279 This->DriverBindingHandle,
280 Controller,
281 EFI_OPEN_PROTOCOL_BY_DRIVER
283 if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {
285 // Close opened protocol
287 gBS->CloseProtocol (
288 Controller,
289 &gEfiDevicePathProtocolGuid,
290 This->DriverBindingHandle,
291 Controller
293 return Status;
296 // The IsaBus driver will use memory below 16M, which is not tested yet,
297 // so call CompatibleRangeTest to test them. Since memory below 1M should
298 // be reserved to CSM, and 15M~16M might be reserved for Isa hole, test 1M
299 // ~15M here
301 Status = gBS->LocateProtocol (
302 &gEfiGenericMemTestProtocolGuid,
303 NULL,
304 (VOID **) &GenMemoryTest
307 if (!EFI_ERROR (Status)) {
308 Status = GenMemoryTest->CompatibleRangeTest (
309 GenMemoryTest,
310 0x100000,
311 0xE00000
315 // Report Status Code here since we will initialize the host controller
317 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
318 EFI_PROGRESS_CODE,
319 (EFI_IO_BUS_LPC | EFI_IOB_PC_INIT),
320 ParentDevicePath
324 // first init ISA interface
326 IsaAcpi->InterfaceInit (IsaAcpi);
329 // Report Status Code here since we will enable the host controller
331 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
332 EFI_PROGRESS_CODE,
333 (EFI_IO_BUS_LPC | EFI_IOB_PC_ENABLE),
334 ParentDevicePath
338 // Create each ISA device handle in this ISA bus
340 IsaDevice = NULL;
341 do {
342 Status = IsaAcpi->DeviceEnumerate (IsaAcpi, &IsaDevice);
343 if (EFI_ERROR (Status)) {
344 break;
347 // Get current resource of this ISA device
349 ResourceList = NULL;
350 Status = IsaAcpi->GetCurResource (IsaAcpi, IsaDevice, &ResourceList);
351 if (EFI_ERROR (Status)) {
352 continue;
356 // Create handle for this ISA device
358 // If any child device handle was created in previous call to Start() and not stopped
359 // in previous call to Stop(), it will not be created again because the
360 // InstallMultipleProtocolInterfaces() boot service will reject same device path.
362 Status = IsaCreateDevice (
363 This,
364 Controller,
365 PciIo,
366 ParentDevicePath,
367 ResourceList,
368 &DevicePathData
371 if (EFI_ERROR (Status)) {
372 continue;
375 // Initialize ISA device
377 IsaAcpi->InitDevice (IsaAcpi, IsaDevice);
380 // Set resources for this ISA device
382 Status = IsaAcpi->SetResource (IsaAcpi, IsaDevice, ResourceList);
385 // Report Status Code here when failed to resource conflicts
387 if (EFI_ERROR (Status) && (Status != EFI_UNSUPPORTED)) {
389 // It's hard to tell which resource conflicts
391 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
392 EFI_ERROR_CODE,
393 (EFI_IO_BUS_LPC | EFI_IOB_EC_RESOURCE_CONFLICT),
394 DevicePathData
399 // Set power for this ISA device
401 IsaAcpi->SetPower (IsaAcpi, IsaDevice, TRUE);
404 // Enable this ISA device
406 IsaAcpi->EnableDevice (IsaAcpi, IsaDevice, TRUE);
408 } while (TRUE);
410 return EFI_SUCCESS;
414 Stop this driver on ControllerHandle.
416 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
417 As a result, much of the error checking on the parameters to Stop() has been moved
418 into this common boot service. It is legal to call Stop() from other locations,
419 but the following calling restrictions must be followed or the system behavior will not be deterministic.
420 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
421 same driver's Start() function.
422 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
423 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
424 Start() function, and the Start() function must have called OpenProtocol() on
425 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
427 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
428 @param[in] ControllerHandle A handle to the device being stopped. The handle must
429 support a bus specific I/O protocol for the driver
430 to use to stop the device.
431 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
432 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
433 if NumberOfChildren is 0.
435 @retval EFI_SUCCESS The device was stopped.
436 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
438 EFI_STATUS
439 EFIAPI
440 IsaBusControllerDriverStop (
441 IN EFI_DRIVER_BINDING_PROTOCOL * This,
442 IN EFI_HANDLE Controller,
443 IN UINTN NumberOfChildren,
444 IN EFI_HANDLE * ChildHandleBuffer OPTIONAL
447 EFI_STATUS Status;
448 UINTN Index;
449 BOOLEAN AllChildrenStopped;
450 ISA_IO_DEVICE *IsaIoDevice;
451 EFI_ISA_IO_PROTOCOL *IsaIo;
452 EFI_PCI_IO_PROTOCOL *PciIo;
454 if (NumberOfChildren == 0) {
456 // Close the bus driver
458 Status = gBS->CloseProtocol (
459 Controller,
460 &gEfiDevicePathProtocolGuid,
461 This->DriverBindingHandle,
462 Controller
464 if (EFI_ERROR (Status)) {
465 return Status;
468 Status = gBS->CloseProtocol (
469 Controller,
470 &gEfiIsaAcpiProtocolGuid,
471 This->DriverBindingHandle,
472 Controller
474 if (EFI_ERROR (Status)) {
475 return Status;
478 return EFI_SUCCESS;
481 // Complete all outstanding transactions to Controller.
482 // Don't allow any new transaction to Controller to be started.
485 // Stop all the children
486 // Find all the ISA devices that were discovered on this PCI to ISA Bridge
487 // with the Start() function.
489 AllChildrenStopped = TRUE;
491 for (Index = 0; Index < NumberOfChildren; Index++) {
493 Status = gBS->OpenProtocol (
494 ChildHandleBuffer[Index],
495 &gEfiIsaIoProtocolGuid,
496 (VOID **) &IsaIo,
497 This->DriverBindingHandle,
498 Controller,
499 EFI_OPEN_PROTOCOL_GET_PROTOCOL
501 if (!EFI_ERROR (Status)) {
503 IsaIoDevice = ISA_IO_DEVICE_FROM_ISA_IO_THIS (IsaIo);
506 // Close the child handle
509 Status = gBS->CloseProtocol (
510 Controller,
511 &gEfiPciIoProtocolGuid,
512 This->DriverBindingHandle,
513 ChildHandleBuffer[Index]
515 Status = gBS->UninstallMultipleProtocolInterfaces (
516 ChildHandleBuffer[Index],
517 &gEfiDevicePathProtocolGuid,
518 IsaIoDevice->DevicePath,
519 &gEfiIsaIoProtocolGuid,
520 &IsaIoDevice->IsaIo,
521 NULL
524 if (!EFI_ERROR (Status)) {
525 FreePool (IsaIoDevice->DevicePath);
526 FreePool (IsaIoDevice);
527 } else {
529 // Re-open PCI IO Protocol on behalf of the child device
530 // because of failure of destroying the child device handle
532 gBS->OpenProtocol (
533 Controller,
534 &gEfiPciIoProtocolGuid,
535 (VOID **) &PciIo,
536 This->DriverBindingHandle,
537 ChildHandleBuffer[Index],
538 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
543 if (EFI_ERROR (Status)) {
544 AllChildrenStopped = FALSE;
548 if (!AllChildrenStopped) {
549 return EFI_DEVICE_ERROR;
552 return EFI_SUCCESS;
556 // Internal Function
560 Create EFI Handle for a ISA device found via ISA ACPI Protocol
562 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL instance.
563 @param[in] Controller The handle of ISA bus controller(PCI to ISA bridge)
564 @param[in] PciIo The Pointer to the PCI protocol
565 @param[in] ParentDevicePath Device path of the ISA bus controller
566 @param[in] IsaDeviceResourceList The resource list of the ISA device
567 @param[in] ChildDevicePath The pointer to the child device.
569 @retval EFI_SUCCESS The handle for the child device was created.
570 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
571 @retval EFI_DEVICE_ERROR The handle for the child device can not be created.
573 EFI_STATUS
574 IsaCreateDevice (
575 IN EFI_DRIVER_BINDING_PROTOCOL *This,
576 IN EFI_HANDLE Controller,
577 IN EFI_PCI_IO_PROTOCOL *PciIo,
578 IN EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath,
579 IN EFI_ISA_ACPI_RESOURCE_LIST *IsaDeviceResourceList,
580 OUT EFI_DEVICE_PATH_PROTOCOL **ChildDevicePath
583 EFI_STATUS Status;
584 ISA_IO_DEVICE *IsaIoDevice;
585 EFI_DEV_PATH Node;
588 // Initialize the PCI_IO_DEVICE structure
590 IsaIoDevice = AllocateZeroPool (sizeof (ISA_IO_DEVICE));
591 if (IsaIoDevice == NULL) {
592 return EFI_OUT_OF_RESOURCES;
595 IsaIoDevice->Signature = ISA_IO_DEVICE_SIGNATURE;
596 IsaIoDevice->Handle = NULL;
597 IsaIoDevice->PciIo = PciIo;
600 // Initialize the ISA I/O instance structure
602 InitializeIsaIoInstance (IsaIoDevice, IsaDeviceResourceList);
605 // Build the child device path
607 Node.DevPath.Type = ACPI_DEVICE_PATH;
608 Node.DevPath.SubType = ACPI_DP;
609 SetDevicePathNodeLength (&Node.DevPath, sizeof (ACPI_HID_DEVICE_PATH));
610 Node.Acpi.HID = IsaDeviceResourceList->Device.HID;
611 Node.Acpi.UID = IsaDeviceResourceList->Device.UID;
613 IsaIoDevice->DevicePath = AppendDevicePathNode (
614 ParentDevicePath,
615 &Node.DevPath
618 if (IsaIoDevice->DevicePath == NULL) {
619 Status = EFI_OUT_OF_RESOURCES;
620 goto Done;
623 *ChildDevicePath = IsaIoDevice->DevicePath;
626 // Create a child handle and install Device Path and ISA I/O protocols
628 Status = gBS->InstallMultipleProtocolInterfaces (
629 &IsaIoDevice->Handle,
630 &gEfiDevicePathProtocolGuid,
631 IsaIoDevice->DevicePath,
632 &gEfiIsaIoProtocolGuid,
633 &IsaIoDevice->IsaIo,
634 NULL
636 if (EFI_ERROR (Status)) {
637 goto Done;
640 Status = gBS->OpenProtocol (
641 Controller,
642 &gEfiPciIoProtocolGuid,
643 (VOID **) &PciIo,
644 This->DriverBindingHandle,
645 IsaIoDevice->Handle,
646 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
648 if (EFI_ERROR (Status)) {
649 gBS->UninstallMultipleProtocolInterfaces (
650 IsaIoDevice->Handle,
651 &gEfiDevicePathProtocolGuid,
652 IsaIoDevice->DevicePath,
653 &gEfiIsaIoProtocolGuid,
654 &IsaIoDevice->IsaIo,
655 NULL
659 Done:
661 if (EFI_ERROR (Status)) {
662 if (IsaIoDevice->DevicePath != NULL) {
663 FreePool (IsaIoDevice->DevicePath);
666 FreePool (IsaIoDevice);
669 return Status;