PcuSio.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. /** @file
  2. Produces the SIO Protocols for ISA devices integrated in the Platform
  3. Controller Unit (PCU).
  4. Copyright (c) 2004 - 2019, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "PcuSio.h"
  8. //
  9. // PCU SIO Driver Global Variables
  10. //
  11. EFI_DRIVER_BINDING_PROTOCOL gPcuSioDriverBinding = {
  12. PcuSioDriverBindingSupported,
  13. PcuSioDriverBindingStart,
  14. PcuSioDriverBindingStop,
  15. 0x10,
  16. NULL,
  17. NULL
  18. };
  19. //
  20. // Super I/O Protocol interfaces
  21. //
  22. EFI_SIO_PROTOCOL mSioInterface = {
  23. SioRegisterAccess,
  24. SioGetResources,
  25. SioSetResources,
  26. SioPossibleResources,
  27. SioModify
  28. };
  29. //
  30. // COM 1 UART Controller
  31. //
  32. GLOBAL_REMOVE_IF_UNREFERENCED
  33. SIO_RESOURCES_IO mCom1Resources = {
  34. { { ACPI_FIXED_LOCATION_IO_PORT_DESCRIPTOR }, 0x3F8, 8 },
  35. { ACPI_END_TAG_DESCRIPTOR, 0 }
  36. };
  37. //
  38. // Table of SIO Controllers
  39. //
  40. GLOBAL_REMOVE_IF_UNREFERENCED
  41. SIO_DEVICE_INFO mDevicesInfo[] = {
  42. {
  43. EISA_PNP_ID (0x501),
  44. 0,
  45. { (ACPI_SMALL_RESOURCE_HEADER *) &mCom1Resources }
  46. }
  47. };
  48. //
  49. // ACPI Device Path Node template
  50. //
  51. GLOBAL_REMOVE_IF_UNREFERENCED
  52. ACPI_HID_DEVICE_PATH mAcpiDeviceNodeTemplate = {
  53. { // Header
  54. ACPI_DEVICE_PATH,
  55. ACPI_DP,
  56. {
  57. (UINT8) (sizeof (ACPI_HID_DEVICE_PATH)),
  58. (UINT8) ((sizeof (ACPI_HID_DEVICE_PATH)) >> 8)
  59. }
  60. },
  61. 0x0, // HID
  62. 0x0 // UID
  63. };
  64. /**
  65. Provides a low level access to the registers for the Super I/O.
  66. @param[in] This Indicates a pointer to the calling context.
  67. @param[in] Write Specifies the type of the register operation.
  68. If this parameter is TRUE, Value is interpreted
  69. as an input parameter and the operation is a
  70. register write. If this parameter is FALSE,
  71. Value is interpreted as an output parameter and
  72. the operation is a register read.
  73. @param[in] ExitCfgMode Exit Configuration Mode Indicator. If this
  74. parameter is set to TRUE, the Super I/O driver
  75. will turn off configuration mode of the Super
  76. I/O prior to returning from this function. If
  77. this parameter is set to FALSE, the Super I/O
  78. driver will leave Super I/O in the
  79. configuration mode. The Super I/O driver must
  80. track the current state of the Super I/O and
  81. enable the configuration mode of Super I/O if
  82. necessary prior to register access.
  83. @param[in] Register Register number.
  84. @param[in,out] Value If Write is TRUE, Value is a pointer to the
  85. buffer containing the byte of data to be
  86. written to the Super I/O register. If Write is
  87. FALSE, Value is a pointer to the destination
  88. buffer for the byte of data to be read from the
  89. Super I/O register.
  90. @retval EFI_SUCCESS The operation completed successfully.
  91. @retval EFI_INVALID_PARAMETER The Value is NULL.
  92. @retval EFI_INVALID_PARAMETER Invalid Register number.
  93. **/
  94. EFI_STATUS
  95. EFIAPI
  96. SioRegisterAccess (
  97. IN CONST EFI_SIO_PROTOCOL *This,
  98. IN BOOLEAN Write,
  99. IN BOOLEAN ExitCfgMode,
  100. IN UINT8 Register,
  101. IN OUT UINT8 *Value
  102. )
  103. {
  104. return EFI_SUCCESS;
  105. }
  106. /**
  107. Provides an interface to get a list of the current resources consumed by the
  108. device in the ACPI Resource Descriptor format.
  109. GetResources() returns a list of resources currently consumed by the device.
  110. The ResourceList is a pointer to the buffer containing resource descriptors
  111. for the device. The descriptors are in the format of Small or Large ACPI
  112. resource descriptor as defined by ACPI specification (2.0 & 3.0). The buffer
  113. of resource descriptors is terminated with the 'End tag' resource descriptor.
  114. @param[in] This Indicates a pointer to the calling context.
  115. @param[out] ResourceList A pointer to an ACPI resource descriptor list
  116. that defines the current resources used by the
  117. device.
  118. @retval EFI_SUCCESS The operation completed successfully.
  119. @retval EFI_INVALID_PARAMETER ResourceList is NULL.
  120. **/
  121. EFI_STATUS
  122. EFIAPI
  123. SioGetResources (
  124. IN CONST EFI_SIO_PROTOCOL *This,
  125. OUT ACPI_RESOURCE_HEADER_PTR *ResourceList
  126. )
  127. {
  128. SIO_DEV *SioDevice;
  129. if (ResourceList == NULL) {
  130. return EFI_INVALID_PARAMETER;
  131. }
  132. SioDevice = SIO_DEV_FROM_SIO (This);
  133. if (SioDevice->DeviceIndex < ARRAY_SIZE (mDevicesInfo)) {
  134. *ResourceList = mDevicesInfo[SioDevice->DeviceIndex].Resources;
  135. }
  136. return EFI_SUCCESS;
  137. }
  138. /**
  139. Sets the resources for the device.
  140. @param[in] This Indicates a pointer to the calling context.
  141. @param[in] ResourceList Pointer to the ACPI resource descriptor list.
  142. @retval EFI_SUCCESS The operation completed successfully.
  143. @retval EFI_INVALID_PARAMETER ResourceList is invalid.
  144. @retval EFI_ACCESS_DENIED Some of the resources in ResourceList are in
  145. use.
  146. **/
  147. EFI_STATUS
  148. EFIAPI
  149. SioSetResources (
  150. IN CONST EFI_SIO_PROTOCOL *This,
  151. IN ACPI_RESOURCE_HEADER_PTR ResourceList
  152. )
  153. {
  154. return EFI_SUCCESS;
  155. }
  156. /**
  157. Provides a collection of resource descriptor lists. Each resource descriptor
  158. list in the collection defines a combination of resources that can
  159. potentially be used by the device.
  160. @param[in] This Indicates a pointer to the calling context.
  161. @param[out] ResourceCollection Collection of the resource descriptor
  162. lists.
  163. @retval EFI_SUCCESS The operation completed successfully.
  164. @retval EFI_INVALID_PARAMETER ResourceCollection is NULL.
  165. **/
  166. EFI_STATUS
  167. EFIAPI
  168. SioPossibleResources (
  169. IN CONST EFI_SIO_PROTOCOL *This,
  170. OUT ACPI_RESOURCE_HEADER_PTR *ResourceCollection
  171. )
  172. {
  173. return EFI_SUCCESS;
  174. }
  175. /**
  176. Provides an interface for a table based programming of the Super I/O
  177. registers.
  178. The Modify() function provides an interface for table based programming of
  179. the Super I/O registers. This function can be used to perform programming of
  180. multiple Super I/O registers with a single function call. For each table
  181. entry, the Register is read, its content is bitwise ANDed with AndMask, and
  182. then ORed with OrMask before being written back to the Register. The Super
  183. I/O driver must track the current state of the Super I/O and enable the
  184. configuration mode of Super I/O if necessary prior to table processing. Once
  185. the table is processed, the Super I/O device has to be returned to the
  186. original state.
  187. @param[in] This Indicates a pointer to the calling context.
  188. @param[in] Command A pointer to an array of NumberOfCommands
  189. EFI_SIO_REGISTER_MODIFY structures. Each
  190. structure specifies a single Super I/O register
  191. modify operation.
  192. @param[in] NumberOfCommands Number of elements in the Command array.
  193. @retval EFI_SUCCESS The operation completed successfully.
  194. @retval EFI_INVALID_PARAMETER Command is NULL.
  195. **/
  196. EFI_STATUS
  197. EFIAPI
  198. SioModify (
  199. IN CONST EFI_SIO_PROTOCOL *This,
  200. IN CONST EFI_SIO_REGISTER_MODIFY *Command,
  201. IN UINTN NumberOfCommands
  202. )
  203. {
  204. return EFI_SUCCESS;
  205. }
  206. /**
  207. Create the child device with a given device index.
  208. @param[in] This The EFI_DRIVER_BINDING_PROTOCOL instance.
  209. @param[in] Controller The handle of ISA bus controller.
  210. @param[in] PciIo The pointer to the PCI protocol.
  211. @param[in] ParentDevicePath Device path of the ISA bus controller.
  212. @param[in] DeviceIndex Index of the device supported by this driver.
  213. @retval EFI_SUCCESS The child device has been created successfully.
  214. @retval Others Error occurred during the child device creation.
  215. **/
  216. EFI_STATUS
  217. SioCreateChildDevice (
  218. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  219. IN EFI_HANDLE Controller,
  220. IN EFI_PCI_IO_PROTOCOL *PciIo,
  221. IN EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath,
  222. IN UINT32 DeviceIndex
  223. )
  224. {
  225. EFI_STATUS Status;
  226. SIO_DEV *SioDevice;
  227. //
  228. // Initialize the SIO_DEV structure
  229. //
  230. SioDevice = AllocateZeroPool (sizeof (SIO_DEV));
  231. if (SioDevice == NULL) {
  232. DEBUG ((DEBUG_ERROR, "SioCreateChildDevice(): Can not allocate SIO Device structure\n"));
  233. return EFI_OUT_OF_RESOURCES;
  234. }
  235. SioDevice->Signature = SIO_DEV_SIGNATURE;
  236. SioDevice->Handle = NULL;
  237. SioDevice->PciIo = PciIo;
  238. CopyMem (&SioDevice->Sio, &mSioInterface, sizeof (EFI_SIO_PROTOCOL));
  239. SioDevice->DeviceIndex = DeviceIndex;
  240. //
  241. // Construct the child device path
  242. //
  243. mAcpiDeviceNodeTemplate.HID = mDevicesInfo[DeviceIndex].Hid;
  244. mAcpiDeviceNodeTemplate.UID = mDevicesInfo[DeviceIndex].Uid;
  245. SioDevice->DevicePath = AppendDevicePathNode (
  246. ParentDevicePath,
  247. (EFI_DEVICE_PATH_PROTOCOL *) &mAcpiDeviceNodeTemplate
  248. );
  249. if (SioDevice->DevicePath == NULL) {
  250. DEBUG ((DEBUG_ERROR, "SioCreateChildDevice(): Can not allocate SIO Device path\n"));
  251. Status = EFI_OUT_OF_RESOURCES;
  252. goto Done;
  253. }
  254. //
  255. // Create a child handle and install Device Path and Super I/O protocols
  256. //
  257. Status = gBS->InstallMultipleProtocolInterfaces (
  258. &SioDevice->Handle,
  259. &gEfiDevicePathProtocolGuid,
  260. SioDevice->DevicePath,
  261. &gEfiSioProtocolGuid,
  262. &SioDevice->Sio,
  263. NULL
  264. );
  265. if (EFI_ERROR (Status)) {
  266. DEBUG ((DEBUG_ERROR, "SioCreateChildDevice(): Can not install child protocols %r\n", Status));
  267. goto Done;
  268. }
  269. Status = gBS->OpenProtocol (
  270. Controller,
  271. &gEfiPciIoProtocolGuid,
  272. (VOID **) &PciIo,
  273. This->DriverBindingHandle,
  274. SioDevice->Handle,
  275. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
  276. );
  277. if (EFI_ERROR (Status)) {
  278. DEBUG ((DEBUG_ERROR, "SioCreateChildDevice(): Can not open parent PCI I/O Protocol by child controller %r\n", Status));
  279. gBS->UninstallMultipleProtocolInterfaces (
  280. SioDevice->Handle,
  281. &gEfiDevicePathProtocolGuid,
  282. SioDevice->DevicePath,
  283. &gEfiSioProtocolGuid,
  284. &SioDevice->Sio,
  285. NULL
  286. );
  287. }
  288. Done:
  289. if (EFI_ERROR (Status)) {
  290. if (SioDevice->DevicePath != NULL) {
  291. FreePool (SioDevice->DevicePath);
  292. }
  293. FreePool (SioDevice);
  294. }
  295. return Status;
  296. }
  297. /**
  298. Create all the ISA child devices on the ISA bus controller (PCI to ISA
  299. bridge).
  300. @param[in] This The EFI_DRIVER_BINDING_PROTOCOL instance.
  301. @param[in] Controller The handle of ISA bus controller.
  302. @param[in] PciIo The pointer to the PCI protocol.
  303. @param[in] ParentDevicePath Device path of the ISA bus controller.
  304. @retval The number of child device that is successfully created.
  305. **/
  306. UINT32
  307. SioCreateAllChildDevices (
  308. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  309. IN EFI_HANDLE Controller,
  310. IN EFI_PCI_IO_PROTOCOL *PciIo,
  311. IN EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath
  312. )
  313. {
  314. UINT32 Index;
  315. UINT32 ChildDeviceNumber;
  316. EFI_STATUS Status;
  317. ChildDeviceNumber = 0;
  318. for (Index = 0; Index < ARRAY_SIZE (mDevicesInfo); Index++) {
  319. Status = SioCreateChildDevice (
  320. This,
  321. Controller,
  322. PciIo,
  323. ParentDevicePath,
  324. Index
  325. );
  326. if (!EFI_ERROR (Status)) {
  327. ChildDeviceNumber++;
  328. }
  329. }
  330. return ChildDeviceNumber;
  331. }
  332. /**
  333. Tests to see if this driver supports a given controller. If a child device is
  334. provided, it further tests to see if this driver supports creating a handle
  335. for the specified child device.
  336. This function checks to see if the driver specified by This supports the
  337. device specified by ControllerHandle. Drivers will typically use the device
  338. path attached to ControllerHandle and/or the services from the bus I/O
  339. abstraction attached to ControllerHandle to determine if the driver supports
  340. ControllerHandle. This function may be called many times during platform
  341. initialization. In order to reduce boot times, the tests performed by this
  342. function must be very small, and take as little time as possible to execute.
  343. This function must not change the state of any hardware devices, and this
  344. function must be aware that the device specified by ControllerHandle may
  345. already be managed by the same driver or a different driver. This function
  346. must match its calls to AllocatePages() with FreePages(), AllocatePool() with
  347. FreePool(), and OpenProtocol() with CloseProtocol(). Since ControllerHandle
  348. may have been previously started by the same driver, if a protocol is already
  349. in the opened state, then it must not be closed with CloseProtocol(). This is
  350. required to guarantee the state of ControllerHandle is not modified by this
  351. function.
  352. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL
  353. instance.
  354. @param[in] ControllerHandle The handle of the controller to test. This
  355. handle must support a protocol interface
  356. that supplies an I/O abstraction to the
  357. driver.
  358. @param[in] RemainingDevicePath A pointer to the remaining portion of a
  359. device path. This parameter is ignored by
  360. device drivers, and is optional for bus
  361. drivers. For bus drivers, if this parameter
  362. is not NULL, then the bus driver must
  363. determine if the bus controller specified by
  364. ControllerHandle and the child controller
  365. specified by RemainingDevicePath are both
  366. supported by this bus driver.
  367. @retval EFI_SUCCESS The device specified by ControllerHandle and
  368. RemainingDevicePath is supported by the
  369. driver specified by This.
  370. @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
  371. RemainingDevicePath is already being managed
  372. by the driver specified by This.
  373. @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
  374. RemainingDevicePath is already being managed
  375. by a different driver or an application that
  376. requires exclusive access.
  377. @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
  378. RemainingDevicePath is not supported by the
  379. driver specified by This.
  380. **/
  381. EFI_STATUS
  382. EFIAPI
  383. PcuSioDriverBindingSupported (
  384. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  385. IN EFI_HANDLE Controller,
  386. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
  387. )
  388. {
  389. EFI_STATUS Status;
  390. EFI_PCI_IO_PROTOCOL *PciIo;
  391. PCI_TYPE00 Pci;
  392. //
  393. // Get PciIo protocol instance
  394. //
  395. Status = gBS->OpenProtocol (
  396. Controller,
  397. &gEfiPciIoProtocolGuid,
  398. (VOID **)&PciIo,
  399. This->DriverBindingHandle,
  400. Controller,
  401. EFI_OPEN_PROTOCOL_BY_DRIVER
  402. );
  403. if (EFI_ERROR(Status)) {
  404. return Status;
  405. }
  406. Status = PciIo->Pci.Read (
  407. PciIo,
  408. EfiPciIoWidthUint32,
  409. 0,
  410. sizeof(Pci) / sizeof(UINT32),
  411. &Pci
  412. );
  413. if (!EFI_ERROR (Status)) {
  414. Status = EFI_UNSUPPORTED;
  415. if ((Pci.Hdr.Command & 0x03) == 0x03) {
  416. if (Pci.Hdr.ClassCode[2] == PCI_CLASS_BRIDGE) {
  417. //
  418. // See if this is a standard PCI to ISA Bridge from the Base Code
  419. // and Class Code
  420. //
  421. if (Pci.Hdr.ClassCode[1] == PCI_CLASS_BRIDGE_ISA) {
  422. Status = EFI_SUCCESS;
  423. }
  424. //
  425. // See if this is an Intel PCI to ISA bridge in Positive Decode Mode
  426. //
  427. if (Pci.Hdr.ClassCode[1] == PCI_CLASS_BRIDGE_ISA_PDECODE &&
  428. Pci.Hdr.VendorId == 0x8086 && Pci.Hdr.DeviceId == 0x7110) {
  429. Status = EFI_SUCCESS;
  430. }
  431. }
  432. }
  433. }
  434. gBS->CloseProtocol (
  435. Controller,
  436. &gEfiPciIoProtocolGuid,
  437. This->DriverBindingHandle,
  438. Controller
  439. );
  440. return Status;
  441. }
  442. /**
  443. Starts a device controller or a bus controller.
  444. The Start() function is designed to be invoked from the EFI boot service
  445. ConnectController(). As a result, much of the error checking on the
  446. parameters to Start() has been moved into this common boot service. It is
  447. legal to call Start() from other locations, but the following calling
  448. restrictions must be followed or the system behavior will not be
  449. deterministic.
  450. 1. ControllerHandle must be a valid EFI_HANDLE.
  451. 2. If RemainingDevicePath is not NULL, then it must be a pointer to a
  452. naturally aligned EFI_DEVICE_PATH_PROTOCOL.
  453. 3. Prior to calling Start(), the Supported() function for the driver
  454. specified by This must have been called with the same calling parameters,
  455. and Supported() must have returned EFI_SUCCESS.
  456. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL
  457. instance.
  458. @param[in] ControllerHandle The handle of the controller to start. This
  459. handle must support a protocol interface
  460. that supplies an I/O abstraction to the
  461. driver.
  462. @param[in] RemainingDevicePath A pointer to the remaining portion of a
  463. device path. This parameter is ignored by
  464. device drivers, and is optional for bus
  465. drivers. For a bus driver, if this parameter
  466. is NULL, then handles for all the children
  467. of Controller are created by this driver. If
  468. this parameter is not NULL and the first
  469. Device Path Node is not the End of Device
  470. Path Node, then only the handle for the
  471. child device specified by the first Device
  472. Path Node of RemainingDevicePath is created
  473. by this driver. If the first Device Path
  474. Node of RemainingDevicePath is the End of
  475. Device Path Node, no child handle is created
  476. by this driver.
  477. @retval EFI_SUCCESS The device was started.
  478. @retval EFI_DEVICE_ERROR The device could not be started due to a
  479. device error.
  480. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
  481. lack of resources.
  482. @retval Others The driver failed to start the device.
  483. **/
  484. EFI_STATUS
  485. EFIAPI
  486. PcuSioDriverBindingStart (
  487. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  488. IN EFI_HANDLE Controller,
  489. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
  490. )
  491. {
  492. EFI_STATUS Status;
  493. EFI_PCI_IO_PROTOCOL *PciIo;
  494. EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
  495. UINT64 Supports;
  496. UINT64 OriginalAttributes;
  497. UINT64 Attributes;
  498. BOOLEAN Enabled;
  499. SIO_BUS_DRIVER_PRIVATE_DATA *Private;
  500. UINT32 ChildDeviceNumber;
  501. Enabled = FALSE;
  502. Supports = 0;
  503. OriginalAttributes = 0;
  504. Private = NULL;
  505. //
  506. // Open the PCI I/O Protocol Interface
  507. //
  508. PciIo = NULL;
  509. Status = gBS->OpenProtocol (
  510. Controller,
  511. &gEfiPciIoProtocolGuid,
  512. (VOID**) &PciIo,
  513. This->DriverBindingHandle,
  514. Controller,
  515. EFI_OPEN_PROTOCOL_BY_DRIVER
  516. );
  517. if (EFI_ERROR (Status)) {
  518. DEBUG ((DEBUG_ERROR, "PcuSioDriverBindingStart(): No PCI I/O Protocol %r\n", Status));
  519. return Status;
  520. }
  521. //
  522. // Open Device Path Protocol
  523. //
  524. Status = gBS->OpenProtocol (
  525. Controller,
  526. &gEfiDevicePathProtocolGuid,
  527. (VOID **) &ParentDevicePath,
  528. This->DriverBindingHandle,
  529. Controller,
  530. EFI_OPEN_PROTOCOL_BY_DRIVER
  531. );
  532. if (EFI_ERROR (Status)) {
  533. DEBUG ((DEBUG_ERROR, "PcuSioDriverBindingStart(): No Device Path Protocol %r\n", Status));
  534. gBS->CloseProtocol (
  535. Controller,
  536. &gEfiPciIoProtocolGuid,
  537. This->DriverBindingHandle,
  538. Controller
  539. );
  540. return Status;
  541. }
  542. //
  543. // Get supported PCI attributes
  544. //
  545. Status = PciIo->Attributes (
  546. PciIo,
  547. EfiPciIoAttributeOperationSupported,
  548. 0,
  549. &Supports
  550. );
  551. if (EFI_ERROR (Status)) {
  552. DEBUG ((DEBUG_ERROR, "PcuSioDriverBindingStart(): Error reading PCI I/O Supported Attributes %r\n", Status));
  553. goto Done;
  554. }
  555. Supports &= (UINT64) (EFI_PCI_IO_ATTRIBUTE_ISA_IO |
  556. EFI_PCI_IO_ATTRIBUTE_ISA_IO_16);
  557. Status = PciIo->Attributes (
  558. PciIo,
  559. EfiPciIoAttributeOperationGet,
  560. 0,
  561. &OriginalAttributes
  562. );
  563. if (EFI_ERROR (Status)) {
  564. DEBUG ((DEBUG_ERROR, "PcuSioDriverBindingStart(): Error reading PCI I/O Attributes %r\n", Status));
  565. goto Done;
  566. }
  567. Attributes = EFI_PCI_DEVICE_ENABLE |
  568. Supports |
  569. EFI_PCI_IO_ATTRIBUTE_ISA_MOTHERBOARD_IO;
  570. Status = PciIo->Attributes (
  571. PciIo,
  572. EfiPciIoAttributeOperationEnable,
  573. Attributes,
  574. NULL
  575. );
  576. if (EFI_ERROR (Status)) {
  577. DEBUG ((DEBUG_ERROR, "PcuSioDriverBindingStart(): Error enabling PCI I/O Attributes %r\n", Status));
  578. goto Done;
  579. }
  580. Enabled = TRUE;
  581. //
  582. // Store the OriginalAttributes for the restore in BindingStop()
  583. //
  584. Private = AllocateZeroPool (sizeof (SIO_BUS_DRIVER_PRIVATE_DATA));
  585. if (Private == NULL) {
  586. Status = EFI_OUT_OF_RESOURCES;
  587. DEBUG ((DEBUG_ERROR, "PcuSioDriverBindingStart(): Error allocating SIO private data structure %r\n", Status));
  588. goto Done;
  589. }
  590. Private->PciIo = PciIo;
  591. Private->OriginalAttributes = OriginalAttributes;
  592. Status = gBS->InstallProtocolInterface (
  593. &Controller,
  594. &gEfiCallerIdGuid,
  595. EFI_NATIVE_INTERFACE,
  596. Private
  597. );
  598. if (EFI_ERROR (Status)) {
  599. DEBUG ((DEBUG_ERROR, "PcuSioDriverBindingStart(): Error installing gEfiCallerIdGuid %r\n", Status));
  600. goto Done;
  601. }
  602. //
  603. // Report status code for the start of general controller initialization
  604. //
  605. REPORT_STATUS_CODE_WITH_DEVICE_PATH (
  606. EFI_PROGRESS_CODE,
  607. (EFI_IO_BUS_LPC | EFI_IOB_PC_INIT),
  608. ParentDevicePath
  609. );
  610. //
  611. // Report status code for the start of enabling devices on the bus
  612. //
  613. REPORT_STATUS_CODE_WITH_DEVICE_PATH (
  614. EFI_PROGRESS_CODE,
  615. (EFI_IO_BUS_LPC | EFI_IOB_PC_ENABLE),
  616. ParentDevicePath
  617. );
  618. //
  619. // Create all the children upon the first entrance
  620. //
  621. ChildDeviceNumber = SioCreateAllChildDevices (
  622. This,
  623. Controller,
  624. PciIo,
  625. ParentDevicePath
  626. );
  627. if (ChildDeviceNumber == 0) {
  628. Status = EFI_DEVICE_ERROR;
  629. DEBUG ((DEBUG_ERROR, "PcuSioDriverBindingStart(): Error creating child SIO devices %r\n", Status));
  630. goto Done;
  631. }
  632. Done:
  633. if (EFI_ERROR (Status)) {
  634. if (PciIo != NULL && Enabled) {
  635. PciIo->Attributes (
  636. PciIo,
  637. EfiPciIoAttributeOperationSet,
  638. OriginalAttributes,
  639. NULL
  640. );
  641. }
  642. gBS->CloseProtocol (
  643. Controller,
  644. &gEfiDevicePathProtocolGuid,
  645. This->DriverBindingHandle,
  646. Controller
  647. );
  648. gBS->CloseProtocol (
  649. Controller,
  650. &gEfiPciIoProtocolGuid,
  651. This->DriverBindingHandle,
  652. Controller
  653. );
  654. if (Private != NULL) {
  655. gBS->UninstallMultipleProtocolInterfaces (
  656. Controller,
  657. &gEfiCallerIdGuid,
  658. Private,
  659. NULL
  660. );
  661. FreePool (Private);
  662. }
  663. }
  664. return Status;
  665. }
  666. /**
  667. Stops a device controller or a bus controller.
  668. The Stop() function is designed to be invoked from the EFI boot service
  669. DisconnectController(). As a result, much of the error checking on the
  670. parameters to Stop() has been moved into this common boot service. It is
  671. legal to call Stop() from other locations, but the following calling
  672. restrictions must be followed or the system behavior will not be
  673. deterministic.
  674. 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous
  675. call to this same driver's Start() function.
  676. 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a
  677. valid EFI_HANDLE. In addition, all of these handles must have been created
  678. in this driver's Start() function, and the Start() function must have
  679. called OpenProtocol() on ControllerHandle with an Attribute of
  680. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
  681. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL
  682. instance.
  683. @param[in] ControllerHandle A handle to the device being stopped. The
  684. handle must support a bus specific I/O
  685. protocol for the driver to use to stop the
  686. device.
  687. @param[in] NumberOfChildren The number of child device handles in
  688. ChildHandleBuffer.
  689. @param[in] ChildHandleBuffer An array of child handles to be freed. May be
  690. NULL if NumberOfChildren is 0.
  691. @retval EFI_SUCCESS The device was stopped.
  692. @retval EFI_DEVICE_ERROR The device could not be stopped due to a
  693. device error.
  694. **/
  695. EFI_STATUS
  696. EFIAPI
  697. PcuSioDriverBindingStop (
  698. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  699. IN EFI_HANDLE Controller,
  700. IN UINTN NumberOfChildren,
  701. IN EFI_HANDLE *ChildHandleBuffer
  702. )
  703. {
  704. EFI_STATUS Status;
  705. SIO_BUS_DRIVER_PRIVATE_DATA *Private;
  706. UINTN Index;
  707. BOOLEAN AllChildrenStopped;
  708. EFI_SIO_PROTOCOL *Sio;
  709. SIO_DEV *SioDevice;
  710. EFI_PCI_IO_PROTOCOL *PciIo;
  711. if (NumberOfChildren == 0) {
  712. //
  713. // Restore PCI attributes
  714. //
  715. Status = gBS->OpenProtocol (
  716. Controller,
  717. &gEfiCallerIdGuid,
  718. (VOID **) &Private,
  719. This->DriverBindingHandle,
  720. Controller,
  721. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  722. );
  723. if (EFI_ERROR (Status)) {
  724. return Status;
  725. }
  726. Status = Private->PciIo->Attributes (
  727. Private->PciIo,
  728. EfiPciIoAttributeOperationSet,
  729. Private->OriginalAttributes,
  730. NULL
  731. );
  732. if (EFI_ERROR (Status)) {
  733. return Status;
  734. }
  735. gBS->UninstallProtocolInterface (
  736. Controller,
  737. &gEfiCallerIdGuid,
  738. Private
  739. );
  740. FreePool (Private);
  741. //
  742. // Close the bus driver
  743. //
  744. Status = gBS->CloseProtocol (
  745. Controller,
  746. &gEfiDevicePathProtocolGuid,
  747. This->DriverBindingHandle,
  748. Controller
  749. );
  750. if (EFI_ERROR (Status)) {
  751. return Status;
  752. }
  753. Status = gBS->CloseProtocol (
  754. Controller,
  755. &gEfiPciIoProtocolGuid,
  756. This->DriverBindingHandle,
  757. Controller
  758. );
  759. if (EFI_ERROR (Status)) {
  760. return Status;
  761. }
  762. return EFI_SUCCESS;
  763. }
  764. //
  765. // Stop all the children
  766. //
  767. AllChildrenStopped = TRUE;
  768. for (Index = 0; Index < NumberOfChildren; Index++) {
  769. Status = gBS->OpenProtocol (
  770. ChildHandleBuffer[Index],
  771. &gEfiSioProtocolGuid,
  772. (VOID **) &Sio,
  773. This->DriverBindingHandle,
  774. Controller,
  775. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  776. );
  777. if (!EFI_ERROR (Status)) {
  778. SioDevice = SIO_DEV_FROM_SIO (Sio);
  779. //
  780. // Close the child handle
  781. //
  782. Status = gBS->CloseProtocol (
  783. Controller,
  784. &gEfiPciIoProtocolGuid,
  785. This->DriverBindingHandle,
  786. ChildHandleBuffer[Index]
  787. );
  788. Status = gBS->UninstallMultipleProtocolInterfaces (
  789. ChildHandleBuffer[Index],
  790. &gEfiDevicePathProtocolGuid,
  791. SioDevice->DevicePath,
  792. &gEfiSioProtocolGuid,
  793. &SioDevice->Sio,
  794. NULL
  795. );
  796. if (!EFI_ERROR (Status)) {
  797. FreePool (SioDevice->DevicePath);
  798. FreePool (SioDevice);
  799. } else {
  800. //
  801. // Re-open PCI IO Protocol on behalf of the child device
  802. // because of failure of destroying the child device handle
  803. //
  804. gBS->OpenProtocol (
  805. Controller,
  806. &gEfiPciIoProtocolGuid,
  807. (VOID **) &PciIo,
  808. This->DriverBindingHandle,
  809. ChildHandleBuffer[Index],
  810. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
  811. );
  812. }
  813. }
  814. if (EFI_ERROR (Status)) {
  815. AllChildrenStopped = FALSE;
  816. }
  817. }
  818. if (!AllChildrenStopped) {
  819. return EFI_DEVICE_ERROR;
  820. }
  821. return EFI_SUCCESS;
  822. }
  823. /**
  824. The entry point for the PCU SIO driver.
  825. @param[in] ImageHandle The firmware allocated handle for the EFI image.
  826. @param[in] SystemTable A pointer to the EFI System Table.
  827. @retval EFI_SUCCESS The entry point is executed successfully.
  828. @retval other Some error occurs when executing this entry point.
  829. **/
  830. EFI_STATUS
  831. EFIAPI
  832. PcuSioDriverEntryPoint (
  833. IN EFI_HANDLE ImageHandle,
  834. IN EFI_SYSTEM_TABLE *SystemTable
  835. )
  836. {
  837. return EfiLibInstallDriverBinding (
  838. ImageHandle,
  839. SystemTable,
  840. &gPcuSioDriverBinding,
  841. ImageHandle
  842. );
  843. }