+/** @file\r
+ The SioBusDxe driver is used to create child devices on the ISA bus and\r
+ installs the Super I/O protocols on them.\r
+\r
+ Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>\r
+\r
+ This program and the accompanying materials\r
+ are licensed and made available under the terms and conditions\r
+ of the BSD License which accompanies this distribution. The\r
+ full text of the license may be found at\r
+ http://opensource.org/licenses/bsd-license.php\r
+\r
+ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include "SioBusDxe.h"\r
+\r
+//\r
+// SioBus Driver Binding Protocol\r
+//\r
+EFI_DRIVER_BINDING_PROTOCOL gSioBusDriverBinding = {\r
+ SioBusDriverBindingSupported,\r
+ SioBusDriverBindingStart,\r
+ SioBusDriverBindingStop,\r
+ 0x10,\r
+ NULL,\r
+ NULL\r
+};\r
+\r
+\r
+/**\r
+ Tests to see if this driver supports a given controller. If a child device is\r
+ provided, it further tests to see if this driver supports creating a handle\r
+ for the specified child device.\r
+\r
+ This function checks to see if the driver specified by This supports the\r
+ device specified by ControllerHandle. Drivers will typically use the device\r
+ path attached to ControllerHandle and/or the services from the bus I/O\r
+ abstraction attached to ControllerHandle to determine if the driver supports\r
+ ControllerHandle. This function may be called many times during platform\r
+ initialization. In order to reduce boot times, the tests performed by this\r
+ function must be very small, and take as little time as possible to execute.\r
+ This function must not change the state of any hardware devices, and this\r
+ function must be aware that the device specified by ControllerHandle may\r
+ already be managed by the same driver or a different driver. This function\r
+ must match its calls to AllocatePages() with FreePages(), AllocatePool() with\r
+ FreePool(), and OpenProtocol() with CloseProtocol(). Since ControllerHandle\r
+ may have been previously started by the same driver, if a protocol is already\r
+ in the opened state, then it must not be closed with CloseProtocol(). This is\r
+ required to guarantee the state of ControllerHandle is not modified by this\r
+ function.\r
+\r
+ @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL\r
+ instance.\r
+ @param[in] ControllerHandle The handle of the controller to test. This\r
+ handle must support a protocol interface\r
+ that supplies an I/O abstraction to the\r
+ driver.\r
+ @param[in] RemainingDevicePath A pointer to the remaining portion of a\r
+ device path. This parameter is ignored by\r
+ device drivers, and is optional for bus\r
+ drivers. For bus drivers, if this parameter\r
+ is not NULL, then the bus driver must\r
+ determine if the bus controller specified by\r
+ ControllerHandle and the child controller\r
+ specified by RemainingDevicePath are both\r
+ supported by this bus driver.\r
+\r
+ @retval EFI_SUCCESS The device specified by ControllerHandle and\r
+ RemainingDevicePath is supported by the\r
+ driver specified by This.\r
+ @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and\r
+ RemainingDevicePath is already being managed\r
+ by the driver specified by This.\r
+ @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and\r
+ RemainingDevicePath is already being managed\r
+ by a different driver or an application that\r
+ requires exclusive access.\r
+ @retval EFI_UNSUPPORTED The device specified by ControllerHandle and\r
+ RemainingDevicePath is not supported by the\r
+ driver specified by This.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+SioBusDriverBindingSupported (\r
+ IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
+ IN EFI_HANDLE Controller,\r
+ IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_PCI_IO_PROTOCOL *PciIo;\r
+ PCI_TYPE00 Pci;\r
+ UINTN SegmentNumber;\r
+ UINTN BusNumber;\r
+ UINTN DeviceNumber;\r
+ UINTN FunctionNumber;\r
+\r
+ //\r
+ // Get PciIo protocol instance\r
+ //\r
+ Status = gBS->OpenProtocol (\r
+ Controller,\r
+ &gEfiPciIoProtocolGuid,\r
+ (VOID**)&PciIo,\r
+ This->DriverBindingHandle,\r
+ Controller,\r
+ EFI_OPEN_PROTOCOL_BY_DRIVER\r
+ );\r
+ if (EFI_ERROR(Status)) {\r
+ return Status;\r
+ }\r
+\r
+ Status = PciIo->Pci.Read (\r
+ PciIo,\r
+ EfiPciIoWidthUint32,\r
+ 0,\r
+ sizeof(Pci) / sizeof(UINT32),\r
+ &Pci);\r
+\r
+ if (!EFI_ERROR (Status)) {\r
+ Status = EFI_UNSUPPORTED;\r
+ if ((Pci.Hdr.Command & 0x03) == 0x03) {\r
+ if (Pci.Hdr.ClassCode[2] == PCI_CLASS_BRIDGE) {\r
+ //\r
+ // See if this is a standard PCI to ISA Bridge from the Base Code and\r
+ // Class Code\r
+ //\r
+ if (Pci.Hdr.ClassCode[1] == PCI_CLASS_BRIDGE_ISA) {\r
+ Status = EFI_SUCCESS;\r
+ }\r
+\r
+ //\r
+ // See if this is an Intel PCI to ISA bridge in Positive Decode Mode\r
+ //\r
+ if (Pci.Hdr.ClassCode[1] == PCI_CLASS_BRIDGE_ISA_PDECODE &&\r
+ Pci.Hdr.VendorId == 0x8086) {\r
+ //\r
+ // See if this is on Function #0 to avoid false positives on\r
+ // PCI_CLASS_BRIDGE_OTHER that has the same value as\r
+ // PCI_CLASS_BRIDGE_ISA_PDECODE\r
+ //\r
+ Status = PciIo->GetLocation (\r
+ PciIo,\r
+ &SegmentNumber,\r
+ &BusNumber,\r
+ &DeviceNumber,\r
+ &FunctionNumber\r
+ );\r
+ if (!EFI_ERROR (Status) && FunctionNumber == 0) {\r
+ Status = EFI_SUCCESS;\r
+ } else {\r
+ Status = EFI_UNSUPPORTED;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ gBS->CloseProtocol (\r
+ Controller,\r
+ &gEfiPciIoProtocolGuid,\r
+ This->DriverBindingHandle,\r
+ Controller\r
+ );\r
+\r
+ return Status;\r
+}\r
+\r
+/**\r
+ Starts a device controller or a bus controller.\r
+\r
+ The Start() function is designed to be invoked from the EFI boot service\r
+ ConnectController(). As a result, much of the error checking on the\r
+ parameters to Start() has been moved into this common boot service. It is\r
+ legal to call Start() from other locations, but the following calling\r
+ restrictions must be followed or the system behavior will not be\r
+ deterministic.\r
+ 1. ControllerHandle must be a valid EFI_HANDLE.\r
+ 2. If RemainingDevicePath is not NULL, then it must be a pointer to a\r
+ naturally aligned EFI_DEVICE_PATH_PROTOCOL.\r
+ 3. Prior to calling Start(), the Supported() function for the driver\r
+ specified by This must have been called with the same calling parameters,\r
+ and Supported() must have returned EFI_SUCCESS.\r
+\r
+ @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL\r
+ instance.\r
+ @param[in] ControllerHandle The handle of the controller to start. This\r
+ handle must support a protocol interface\r
+ that supplies an I/O abstraction to the\r
+ driver.\r
+ @param[in] RemainingDevicePath A pointer to the remaining portion of a\r
+ device path. This parameter is ignored by\r
+ device drivers, and is optional for bus\r
+ drivers. For a bus driver, if this parameter\r
+ is NULL, then handles for all the children\r
+ of Controller are created by this driver. If\r
+ this parameter is not NULL and the first\r
+ Device Path Node is not the End of Device\r
+ Path Node, then only the handle for the\r
+ child device specified by the first Device\r
+ Path Node of RemainingDevicePath is created\r
+ by this driver. If the first Device Path\r
+ Node of RemainingDevicePath is the End of\r
+ Device Path Node, no child handle is created\r
+ by this driver.\r
+\r
+ @retval EFI_SUCCESS The device was started.\r
+ @retval EFI_DEVICE_ERROR The device could not be started due to a\r
+ device error.\r
+ @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a\r
+ lack of resources.\r
+ @retval Others The driver failded to start the device.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+SioBusDriverBindingStart (\r
+ IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
+ IN EFI_HANDLE Controller,\r
+ IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_PCI_IO_PROTOCOL *PciIo;\r
+ EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;\r
+ UINT64 Supports;\r
+ UINT64 OriginalAttributes;\r
+ UINT64 Attributes;\r
+ BOOLEAN Enabled;\r
+ SIO_BUS_DRIVER_PRIVATE_DATA *Private;\r
+ UINT32 ChildDeviceNumber;\r
+\r
+ Enabled = FALSE;\r
+ Supports = 0;\r
+ OriginalAttributes = 0;\r
+ Private = NULL;\r
+\r
+ //\r
+ // Open the PCI I/O Protocol Interface\r
+ //\r
+ PciIo = NULL;\r
+ Status = gBS->OpenProtocol (\r
+ Controller,\r
+ &gEfiPciIoProtocolGuid,\r
+ (VOID**) &PciIo,\r
+ This->DriverBindingHandle,\r
+ Controller,\r
+ EFI_OPEN_PROTOCOL_BY_DRIVER\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ //\r
+ // Open Device Path Protocol\r
+ //\r
+ Status = gBS->OpenProtocol (\r
+ Controller,\r
+ &gEfiDevicePathProtocolGuid,\r
+ (VOID **) &ParentDevicePath,\r
+ This->DriverBindingHandle,\r
+ Controller,\r
+ EFI_OPEN_PROTOCOL_BY_DRIVER\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ gBS->CloseProtocol (\r
+ Controller,\r
+ &gEfiPciIoProtocolGuid,\r
+ This->DriverBindingHandle,\r
+ Controller\r
+ );\r
+ return Status;\r
+ }\r
+\r
+ //\r
+ // Get supported PCI attributes\r
+ //\r
+ Status = PciIo->Attributes (\r
+ PciIo,\r
+ EfiPciIoAttributeOperationSupported,\r
+ 0,\r
+ &Supports\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ goto Done;\r
+ }\r
+\r
+ Supports &= (UINT64) (EFI_PCI_IO_ATTRIBUTE_ISA_IO |\r
+ EFI_PCI_IO_ATTRIBUTE_ISA_IO_16);\r
+ if (Supports == 0 ||\r
+ Supports == (EFI_PCI_IO_ATTRIBUTE_ISA_IO |\r
+ EFI_PCI_IO_ATTRIBUTE_ISA_IO_16)) {\r
+ Status = EFI_UNSUPPORTED;\r
+ goto Done;\r
+ }\r
+\r
+ Status = PciIo->Attributes (\r
+ PciIo,\r
+ EfiPciIoAttributeOperationGet,\r
+ 0,\r
+ &OriginalAttributes\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ goto Done;\r
+ }\r
+\r
+ Attributes = EFI_PCI_DEVICE_ENABLE |\r
+ Supports |\r
+ EFI_PCI_IO_ATTRIBUTE_ISA_MOTHERBOARD_IO;\r
+ Status = PciIo->Attributes (\r
+ PciIo,\r
+ EfiPciIoAttributeOperationEnable,\r
+ Attributes,\r
+ NULL\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ goto Done;\r
+ }\r
+\r
+ Enabled = TRUE;\r
+\r
+ //\r
+ // Store the OriginalAttributes for the restore in BindingStop()\r
+ //\r
+ Private = AllocateZeroPool (sizeof (SIO_BUS_DRIVER_PRIVATE_DATA));\r
+ if (Private == NULL) {\r
+ Status = EFI_OUT_OF_RESOURCES;\r
+ goto Done;\r
+ }\r
+ Private->PciIo = PciIo;\r
+ Private->OriginalAttributes = OriginalAttributes;\r
+\r
+ Status = gBS->InstallProtocolInterface (\r
+ &Controller,\r
+ &gEfiCallerIdGuid,\r
+ EFI_NATIVE_INTERFACE,\r
+ Private\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ goto Done;\r
+ }\r
+\r
+ //\r
+ // Report status code for the start of general controller initialization\r
+ //\r
+ REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
+ EFI_PROGRESS_CODE,\r
+ (EFI_IO_BUS_LPC | EFI_IOB_PC_INIT),\r
+ ParentDevicePath\r
+ );\r
+\r
+ //\r
+ // Report status code for the start of enabling devices on the bus\r
+ //\r
+ REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
+ EFI_PROGRESS_CODE,\r
+ (EFI_IO_BUS_LPC | EFI_IOB_PC_ENABLE),\r
+ ParentDevicePath\r
+ );\r
+\r
+ //\r
+ // Create all the children upon the first entrance\r
+ //\r
+ ChildDeviceNumber = SioCreateAllChildDevices (\r
+ This,\r
+ Controller,\r
+ PciIo,\r
+ ParentDevicePath\r
+ );\r
+ if (ChildDeviceNumber == 0) {\r
+ Status = EFI_DEVICE_ERROR;\r
+ }\r
+\r
+Done:\r
+ if (EFI_ERROR (Status)) {\r
+ if (PciIo != NULL && Enabled) {\r
+ PciIo->Attributes (\r
+ PciIo,\r
+ EfiPciIoAttributeOperationSet,\r
+ OriginalAttributes,\r
+ NULL\r
+ );\r
+ }\r
+\r
+ gBS->CloseProtocol (\r
+ Controller,\r
+ &gEfiDevicePathProtocolGuid,\r
+ This->DriverBindingHandle,\r
+ Controller\r
+ );\r
+\r
+ gBS->CloseProtocol (\r
+ Controller,\r
+ &gEfiPciIoProtocolGuid,\r
+ This->DriverBindingHandle,\r
+ Controller\r
+ );\r
+\r
+ if (Private != NULL) {\r
+ gBS->UninstallMultipleProtocolInterfaces (\r
+ Controller,\r
+ &gEfiCallerIdGuid,\r
+ Private,\r
+ NULL\r
+ );\r
+ FreePool (Private);\r
+ }\r
+\r
+ return Status;\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Stops a device controller or a bus controller.\r
+\r
+ The Stop() function is designed to be invoked from the EFI boot service\r
+ DisconnectController(). As a result, much of the error checking on the\r
+ parameters to Stop() has been moved into this common boot service. It is\r
+ legal to call Stop() from other locations, but the following calling\r
+ restrictions must be followed or the system behavior will not be\r
+ deterministic.\r
+ 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous\r
+ call to this same driver's Start() function.\r
+ 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a\r
+ valid EFI_HANDLE. In addition, all of these handles must have been created\r
+ in this driver's Start() function, and the Start() function must have\r
+ called OpenProtocol() on ControllerHandle with an Attribute of\r
+ EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.\r
+\r
+ @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL\r
+ instance.\r
+ @param[in] ControllerHandle A handle to the device being stopped. The\r
+ handle must support a bus specific I/O\r
+ protocol for the driver to use to stop the\r
+ device.\r
+ @param[in] NumberOfChildren The number of child device handles in\r
+ ChildHandleBuffer.\r
+ @param[in] ChildHandleBuffer An array of child handles to be freed. May be\r
+ NULL if NumberOfChildren is 0.\r
+\r
+ @retval EFI_SUCCESS The device was stopped.\r
+ @retval EFI_DEVICE_ERROR The device could not be stopped due to a\r
+ device error.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+SioBusDriverBindingStop (\r
+ IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
+ IN EFI_HANDLE Controller,\r
+ IN UINTN NumberOfChildren,\r
+ IN EFI_HANDLE *ChildHandleBuffer\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ SIO_BUS_DRIVER_PRIVATE_DATA *Private;\r
+ UINTN Index;\r
+ BOOLEAN AllChildrenStopped;\r
+ EFI_SIO_PROTOCOL *Sio;\r
+ SIO_DEV *SioDevice;\r
+ EFI_PCI_IO_PROTOCOL *PciIo;\r
+\r
+ if (NumberOfChildren == 0) {\r
+ //\r
+ // Restore PCI attributes\r
+ //\r
+ Status = gBS->OpenProtocol (\r
+ Controller,\r
+ &gEfiCallerIdGuid,\r
+ (VOID **) &Private,\r
+ This->DriverBindingHandle,\r
+ Controller,\r
+ EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ Status = Private->PciIo->Attributes (\r
+ Private->PciIo,\r
+ EfiPciIoAttributeOperationSet,\r
+ Private->OriginalAttributes,\r
+ NULL\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ gBS->UninstallProtocolInterface (\r
+ Controller,\r
+ &gEfiCallerIdGuid,\r
+ Private\r
+ );\r
+ FreePool (Private);\r
+\r
+ //\r
+ // Close the bus driver\r
+ //\r
+ Status = gBS->CloseProtocol (\r
+ Controller,\r
+ &gEfiDevicePathProtocolGuid,\r
+ This->DriverBindingHandle,\r
+ Controller\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ Status = gBS->CloseProtocol (\r
+ Controller,\r
+ &gEfiPciIoProtocolGuid,\r
+ This->DriverBindingHandle,\r
+ Controller\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+ }\r
+\r
+ //\r
+ // Stop all the children\r
+ //\r
+ AllChildrenStopped = TRUE;\r
+\r
+ for (Index = 0; Index < NumberOfChildren; Index++) {\r
+ Status = gBS->OpenProtocol (\r
+ ChildHandleBuffer[Index],\r
+ &gEfiSioProtocolGuid,\r
+ (VOID **) &Sio,\r
+ This->DriverBindingHandle,\r
+ Controller,\r
+ EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+ );\r
+ if (!EFI_ERROR (Status)) {\r
+ SioDevice = SIO_DEV_FROM_SIO (Sio);\r
+\r
+ //\r
+ // Close the child handle\r
+ //\r
+ Status = gBS->CloseProtocol (\r
+ Controller,\r
+ &gEfiPciIoProtocolGuid,\r
+ This->DriverBindingHandle,\r
+ ChildHandleBuffer[Index]\r
+ );\r
+ Status = gBS->UninstallMultipleProtocolInterfaces (\r
+ ChildHandleBuffer[Index],\r
+ &gEfiDevicePathProtocolGuid,\r
+ SioDevice->DevicePath,\r
+ &gEfiSioProtocolGuid,\r
+ &SioDevice->Sio,\r
+ NULL\r
+ );\r
+\r
+ if (!EFI_ERROR (Status)) {\r
+ FreePool (SioDevice->DevicePath);\r
+ FreePool (SioDevice);\r
+ } else {\r
+ //\r
+ // Re-open PCI IO Protocol on behalf of the child device\r
+ // because of failure of destroying the child device handle\r
+ //\r
+ gBS->OpenProtocol (\r
+ Controller,\r
+ &gEfiPciIoProtocolGuid,\r
+ (VOID **) &PciIo,\r
+ This->DriverBindingHandle,\r
+ ChildHandleBuffer[Index],\r
+ EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
+ );\r
+ }\r
+ }\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ AllChildrenStopped = FALSE;\r
+ }\r
+ }\r
+\r
+ if (!AllChildrenStopped) {\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ The entry point for the SioBusDxe driver.\r
+\r
+ @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
+ @param[in] SystemTable A pointer to the EFI System Table.\r
+\r
+ @retval EFI_SUCCESS The entry point is executed successfully.\r
+ @retval other Some error occurs when executing this entry point.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+SioBusDxeDriverEntryPoint (\r
+ IN EFI_HANDLE ImageHandle,\r
+ IN EFI_SYSTEM_TABLE *SystemTable\r
+ )\r
+{\r
+ //\r
+ // Install driver model protocol(s).\r
+ //\r
+ return EfiLibInstallDriverBindingComponentName2 (\r
+ ImageHandle,\r
+ SystemTable,\r
+ &gSioBusDriverBinding,\r
+ ImageHandle,\r
+ &gSioBusComponentName,\r
+ &gSioBusComponentName2\r
+ );\r
+}\r