IgdOpRegion.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. /*++
  2. Copyright (c) 2011 - 2020, Intel Corporation. All rights reserved
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. Module Name:
  5. IgdOpRegion.c
  6. Abstract:
  7. This is part of the implementation of an Intel Graphics drivers OpRegion /
  8. Software SCI interface between system BIOS, ASL code, and Graphics drivers.
  9. The code in this file will load the driver and initialize the interface
  10. Supporting Specifiction: OpRegion / Software SCI SPEC 0.70
  11. Acronyms:
  12. IGD: Internal Graphics Device
  13. NVS: ACPI Non Volatile Storage
  14. OpRegion: ACPI Operational Region
  15. VBT: Video BIOS Table (OEM customizable data)
  16. --*/
  17. //
  18. // Include files
  19. //
  20. #include "IgdOpRegion.h"
  21. #include "VlvPlatformInit.h"
  22. #include <PiDxe.h>
  23. #include <PchRegs.h>
  24. #include <Protocol/IgdOpRegion.h>
  25. #include <Protocol/FirmwareVolume2.h>
  26. #include <Protocol/PlatformGopPolicy.h>
  27. #include <Protocol/PciIo.h>
  28. #include <Protocol/CpuIo2.h>
  29. #include <Protocol/GlobalNvsArea.h>
  30. #include <Protocol/DxeSmmReadyToLock.h>
  31. #include <Protocol/PciRootBridgeIo.h>
  32. #include <Library/MemoryAllocationLib.h>
  33. #include <Library/BaseLib.h>
  34. #include <Library/S3BootScriptLib.h>
  35. #include <Library/IoLib.h>
  36. #include <Library/DevicePathLib.h>
  37. #include <Protocol/DriverBinding.h>
  38. #include <Library/PrintLib.h>
  39. #include <Library/BaseMemoryLib.h>
  40. UINT8 gSVER[12] = "Intel";
  41. extern DXE_VLV_PLATFORM_POLICY_PROTOCOL *DxePlatformSaPolicy;
  42. //
  43. // Global variables
  44. //
  45. IGD_OPREGION_PROTOCOL mIgdOpRegion;
  46. EFI_EVENT mConOutEvent;
  47. EFI_EVENT mSetGOPverEvent;
  48. VOID *mConOutReg;
  49. #define DEFAULT_FORM_BUFFER_SIZE 0xFFFF
  50. #ifndef ECP_FLAG
  51. #if 0
  52. /**
  53. Get the HII protocol interface
  54. @param Hii HII protocol interface
  55. @retval Status code
  56. **/
  57. static
  58. EFI_STATUS
  59. GetHiiInterface (
  60. OUT EFI_HII_PROTOCOL **Hii
  61. )
  62. {
  63. EFI_STATUS Status;
  64. //
  65. // There should only be one HII protocol
  66. //
  67. Status = gBS->LocateProtocol (
  68. &gEfiHiiProtocolGuid,
  69. NULL,
  70. (VOID **) Hii
  71. );
  72. return Status;;
  73. }
  74. #endif
  75. #endif
  76. /**
  77. Get VBT data.
  78. @param[in] VbtFileBuffer Pointer to VBT data buffer.
  79. @retval EFI_SUCCESS VBT data was returned.
  80. @retval EFI_NOT_FOUND VBT data not found.
  81. @exception EFI_UNSUPPORTED Invalid signature in VBT data.
  82. **/
  83. EFI_STATUS
  84. GetIntegratedIntelVbtPtr (
  85. OUT VBIOS_VBT_STRUCTURE **VbtFileBuffer
  86. )
  87. {
  88. EFI_STATUS Status;
  89. EFI_PHYSICAL_ADDRESS VbtAddress = 0;
  90. UINTN FvProtocolCount;
  91. EFI_HANDLE *FvHandles;
  92. EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;
  93. UINTN Index;
  94. UINT32 AuthenticationStatus;
  95. UINT8 *Buffer;
  96. UINTN VbtBufferSize = 0;
  97. Buffer = 0;
  98. FvHandles = NULL;
  99. *VbtFileBuffer = NULL;
  100. Status = gBS->LocateHandleBuffer (
  101. ByProtocol,
  102. &gEfiFirmwareVolume2ProtocolGuid,
  103. NULL,
  104. &FvProtocolCount,
  105. &FvHandles
  106. );
  107. if (!EFI_ERROR (Status)) {
  108. for (Index = 0; Index < FvProtocolCount; Index++) {
  109. Status = gBS->HandleProtocol (
  110. FvHandles[Index],
  111. &gEfiFirmwareVolume2ProtocolGuid,
  112. (VOID **) &Fv
  113. );
  114. VbtBufferSize = 0;
  115. Status = Fv->ReadSection (
  116. Fv,
  117. &gBmpImageGuid,
  118. EFI_SECTION_RAW,
  119. 0,
  120. (void **)&Buffer,
  121. &VbtBufferSize,
  122. &AuthenticationStatus
  123. );
  124. if (!EFI_ERROR (Status)) {
  125. VbtAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)Buffer;
  126. Status = EFI_SUCCESS;
  127. break;
  128. }
  129. }
  130. } else {
  131. Status = EFI_NOT_FOUND;
  132. }
  133. if (FvHandles != NULL) {
  134. FreePool(FvHandles);
  135. FvHandles = NULL;
  136. }
  137. //
  138. // Check VBT signature
  139. //
  140. *VbtFileBuffer = (VBIOS_VBT_STRUCTURE *) (UINTN) VbtAddress;
  141. if (*VbtFileBuffer != NULL) {
  142. if ((*((UINT32 *) ((*VbtFileBuffer)->HeaderSignature))) != VBT_SIGNATURE) {
  143. if (*VbtFileBuffer != NULL) {
  144. *VbtFileBuffer = NULL;
  145. }
  146. return EFI_UNSUPPORTED;
  147. }
  148. //
  149. // Check VBT size
  150. //
  151. if ((*VbtFileBuffer)->HeaderVbtSize > VbtBufferSize) {
  152. (*VbtFileBuffer)->HeaderVbtSize = (UINT16) VbtBufferSize;
  153. }
  154. }
  155. return EFI_SUCCESS;
  156. }
  157. //
  158. // Function implementations.
  159. //
  160. /**
  161. Get a pointer to an uncompressed image of the Intel video BIOS.
  162. Note: This function would only be called if the video BIOS at 0xC000 is
  163. missing or not an Intel video BIOS. It may not be an Intel video BIOS
  164. if the Intel graphic contoller is considered a secondary adapter.
  165. @param VBiosROMImage Pointer to an uncompressed Intel video BIOS. This pointer must
  166. be set to NULL if an uncompressed image of the Intel Video BIOS
  167. is not obtainable.
  168. @retval EFI_SUCCESS VBiosPtr is updated.
  169. **/
  170. EFI_STATUS
  171. GetIntegratedIntelVBiosPtr (
  172. INTEL_VBIOS_OPTION_ROM_HEADER **VBiosImage
  173. )
  174. {
  175. EFI_HANDLE *HandleBuffer;
  176. UINTN HandleCount;
  177. UINTN Index;
  178. INTEL_VBIOS_PCIR_STRUCTURE *PcirBlockPtr;
  179. EFI_STATUS Status;
  180. EFI_PCI_IO_PROTOCOL *PciIo;
  181. INTEL_VBIOS_OPTION_ROM_HEADER *VBiosRomImage;
  182. //
  183. // Set as if an umcompressed Intel video BIOS image was not obtainable.
  184. //
  185. VBiosRomImage = NULL;
  186. *VBiosImage = NULL;
  187. //
  188. // Get all PCI IO protocols
  189. //
  190. Status = gBS->LocateHandleBuffer (
  191. ByProtocol,
  192. &gEfiPciIoProtocolGuid,
  193. NULL,
  194. &HandleCount,
  195. &HandleBuffer
  196. );
  197. ASSERT_EFI_ERROR (Status);
  198. //
  199. // Find the video BIOS by checking each PCI IO handle for an Intel video
  200. // BIOS OPROM.
  201. //
  202. for (Index = 0; Index < HandleCount; Index++) {
  203. Status = gBS->HandleProtocol (
  204. HandleBuffer[Index],
  205. &gEfiPciIoProtocolGuid,
  206. (void **)&PciIo
  207. );
  208. ASSERT_EFI_ERROR (Status);
  209. VBiosRomImage = PciIo->RomImage;
  210. //
  211. // If this PCI device doesn't have a ROM image, skip to the next device.
  212. //
  213. if (!VBiosRomImage) {
  214. continue;
  215. }
  216. //
  217. // Get pointer to PCIR structure
  218. //
  219. PcirBlockPtr = (INTEL_VBIOS_PCIR_STRUCTURE *)((UINT8 *) VBiosRomImage + VBiosRomImage->PcirOffset);
  220. //
  221. // Check if we have an Intel video BIOS OPROM.
  222. //
  223. if ((VBiosRomImage->Signature == OPTION_ROM_SIGNATURE) &&
  224. (PcirBlockPtr->VendorId == IGD_VID) &&
  225. (PcirBlockPtr->ClassCode[0] == 0x00) &&
  226. (PcirBlockPtr->ClassCode[1] == 0x00) &&
  227. (PcirBlockPtr->ClassCode[2] == 0x03)
  228. ) {
  229. //
  230. // Found Intel video BIOS.
  231. //
  232. *VBiosImage = VBiosRomImage;
  233. return EFI_SUCCESS;
  234. }
  235. }
  236. //
  237. // No Intel video BIOS found.
  238. //
  239. //
  240. // Free any allocated buffers
  241. //
  242. return EFI_UNSUPPORTED;
  243. }
  244. EFI_STATUS
  245. SearchChildHandle(
  246. EFI_HANDLE Father,
  247. EFI_HANDLE *Child
  248. )
  249. {
  250. EFI_STATUS Status;
  251. UINTN HandleIndex;
  252. EFI_GUID **ProtocolGuidArray = NULL;
  253. UINTN ArrayCount;
  254. UINTN ProtocolIndex;
  255. UINTN OpenInfoCount;
  256. UINTN OpenInfoIndex;
  257. EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfo = NULL;
  258. UINTN mHandleCount;
  259. EFI_HANDLE *mHandleBuffer= NULL;
  260. //
  261. // Retrieve the list of all handles from the handle database
  262. //
  263. Status = gBS->LocateHandleBuffer (
  264. AllHandles,
  265. NULL,
  266. NULL,
  267. &mHandleCount,
  268. &mHandleBuffer
  269. );
  270. for (HandleIndex = 0; HandleIndex < mHandleCount; HandleIndex++) {
  271. //
  272. // Retrieve the list of all the protocols on each handle
  273. //
  274. Status = gBS->ProtocolsPerHandle (
  275. mHandleBuffer[HandleIndex],
  276. &ProtocolGuidArray,
  277. &ArrayCount
  278. );
  279. if (!EFI_ERROR (Status)) {
  280. for (ProtocolIndex = 0; ProtocolIndex < ArrayCount; ProtocolIndex++) {
  281. Status = gBS->OpenProtocolInformation (
  282. mHandleBuffer[HandleIndex],
  283. ProtocolGuidArray[ProtocolIndex],
  284. &OpenInfo,
  285. &OpenInfoCount
  286. );
  287. if (!EFI_ERROR (Status)) {
  288. for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) {
  289. if(OpenInfo[OpenInfoIndex].AgentHandle == Father) {
  290. if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) == EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
  291. *Child = mHandleBuffer[HandleIndex];
  292. Status = EFI_SUCCESS;
  293. goto TryReturn;
  294. }
  295. }
  296. }
  297. Status = EFI_NOT_FOUND;
  298. }
  299. }
  300. if(OpenInfo != NULL) {
  301. FreePool(OpenInfo);
  302. OpenInfo = NULL;
  303. }
  304. }
  305. FreePool (ProtocolGuidArray);
  306. ProtocolGuidArray = NULL;
  307. }
  308. TryReturn:
  309. if(OpenInfo != NULL) {
  310. FreePool (OpenInfo);
  311. OpenInfo = NULL;
  312. }
  313. if(ProtocolGuidArray != NULL) {
  314. FreePool(ProtocolGuidArray);
  315. ProtocolGuidArray = NULL;
  316. }
  317. if(mHandleBuffer != NULL) {
  318. FreePool (mHandleBuffer);
  319. mHandleBuffer = NULL;
  320. }
  321. return Status;
  322. }
  323. EFI_STATUS
  324. JudgeHandleIsPCIDevice(
  325. EFI_HANDLE Handle,
  326. UINT8 Device,
  327. UINT8 Funs
  328. )
  329. {
  330. EFI_STATUS Status;
  331. EFI_DEVICE_PATH *DPath;
  332. Status = gBS->HandleProtocol (
  333. Handle,
  334. &gEfiDevicePathProtocolGuid,
  335. (VOID **) &DPath
  336. );
  337. if(!EFI_ERROR(Status)) {
  338. while(!IsDevicePathEnd(DPath)) {
  339. if((DPath->Type == HARDWARE_DEVICE_PATH) && (DPath->SubType == HW_PCI_DP)) {
  340. PCI_DEVICE_PATH *PCIPath;
  341. PCIPath = (PCI_DEVICE_PATH*) DPath;
  342. DPath = NextDevicePathNode(DPath);
  343. if(IsDevicePathEnd(DPath) && (PCIPath->Device == Device) && (PCIPath->Function == Funs)) {
  344. return EFI_SUCCESS;
  345. }
  346. } else {
  347. DPath = NextDevicePathNode(DPath);
  348. }
  349. }
  350. }
  351. return EFI_UNSUPPORTED;
  352. }
  353. EFI_STATUS
  354. GetDriverName(
  355. EFI_HANDLE Handle,
  356. CHAR16 *GopVersion
  357. )
  358. {
  359. EFI_DRIVER_BINDING_PROTOCOL *BindHandle = NULL;
  360. EFI_STATUS Status;
  361. UINT32 Version;
  362. UINT16 *Ptr;
  363. Status = gBS->OpenProtocol(
  364. Handle,
  365. &gEfiDriverBindingProtocolGuid,
  366. (VOID**)&BindHandle,
  367. NULL,
  368. NULL,
  369. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  370. );
  371. if (EFI_ERROR(Status)) {
  372. return EFI_NOT_FOUND;
  373. }
  374. Version = BindHandle->Version;
  375. Ptr = (UINT16*)&Version;
  376. UnicodeSPrint(GopVersion, 40, L"7.0.%04d", *(Ptr));
  377. return EFI_SUCCESS;
  378. }
  379. EFI_STATUS
  380. GetGOPDriverVersion(
  381. CHAR16 *GopVersion
  382. )
  383. {
  384. UINTN HandleCount;
  385. EFI_HANDLE *Handles= NULL;
  386. UINTN Index;
  387. EFI_STATUS Status;
  388. EFI_HANDLE Child = 0;
  389. Status = gBS->LocateHandleBuffer(
  390. ByProtocol,
  391. &gEfiDriverBindingProtocolGuid,
  392. NULL,
  393. &HandleCount,
  394. &Handles
  395. );
  396. for (Index = 0; Index < HandleCount ; Index++) {
  397. Status = SearchChildHandle(Handles[Index], &Child);
  398. if(!EFI_ERROR(Status)) {
  399. Status = JudgeHandleIsPCIDevice(Child, 0x02, 0x00);
  400. if(!EFI_ERROR(Status)) {
  401. return GetDriverName(Handles[Index], GopVersion);
  402. }
  403. }
  404. }
  405. return EFI_UNSUPPORTED;
  406. }
  407. /**
  408. Get Intel GOP driver version and copy it into IGD OpRegion GVER. This version
  409. is picked up by IGD driver and displayed in CUI.
  410. @param Event A pointer to the Event that triggered the callback.
  411. @param Context A pointer to private data registered with the callback function.
  412. @retval EFI_SUCCESS Video BIOS VBT information returned.
  413. @retval EFI_UNSUPPORTED Could not find VBT information (*VBiosVbtPtr = NULL).
  414. **/
  415. EFI_STATUS
  416. EFIAPI
  417. SetGOPVersionCallback (
  418. IN EFI_EVENT Event,
  419. IN VOID *Context
  420. )
  421. {
  422. CHAR16 GopVersion[16];
  423. EFI_STATUS Status;
  424. ZeroMem (GopVersion, sizeof (GopVersion));
  425. Status = GetGOPDriverVersion(GopVersion);
  426. if(!EFI_ERROR(Status)) {
  427. StrCpyS ((CHAR16*)&(mIgdOpRegion.OpRegion->Header.GOPV[0]), 16, GopVersion);
  428. return Status;
  429. }
  430. return EFI_UNSUPPORTED;
  431. }
  432. /**
  433. Get Intel video BIOS VBT information (i.e. Pointer to VBT and VBT size).
  434. The VBT (Video BIOS Table) is a block of customizable data that is built
  435. within the video BIOS and edited by customers.
  436. @param Event A pointer to the Event that triggered the callback.
  437. @param Context A pointer to private data registered with the callback function.
  438. @retval EFI_SUCCESS Video BIOS VBT information returned.
  439. @retval EFI_UNSUPPORTED Could not find VBT information (*VBiosVbtPtr = NULL).
  440. **/
  441. EFI_STATUS
  442. GetVBiosVbtCallback (
  443. IN EFI_EVENT Event,
  444. IN VOID *Context
  445. )
  446. {
  447. INTEL_VBIOS_PCIR_STRUCTURE *PcirBlockPtr;
  448. UINT16 PciVenderId;
  449. UINT16 PciDeviceId;
  450. INTEL_VBIOS_OPTION_ROM_HEADER *VBiosPtr;
  451. VBIOS_VBT_STRUCTURE *VBiosVbtPtr;
  452. VBIOS_VBT_STRUCTURE *VbtFileBuffer = NULL;
  453. VBiosPtr = (INTEL_VBIOS_OPTION_ROM_HEADER *)(UINTN)(VBIOS_LOCATION_PRIMARY);
  454. PcirBlockPtr = (INTEL_VBIOS_PCIR_STRUCTURE *)((UINT8 *)VBiosPtr + VBiosPtr->PcirOffset);
  455. PciVenderId = PcirBlockPtr->VendorId;
  456. PciDeviceId = PcirBlockPtr->DeviceId;
  457. //
  458. // If the video BIOS is not at 0xC0000 or it is not an Intel video BIOS get
  459. // the integrated Intel video BIOS (must be uncompressed).
  460. //
  461. if ((VBiosPtr->Signature != OPTION_ROM_SIGNATURE) || (PciVenderId != IGD_VID) || (PciDeviceId != IGD_DID_VLV)) {
  462. GetIntegratedIntelVBiosPtr (&VBiosPtr);
  463. if(VBiosPtr) {
  464. //
  465. // Video BIOS found.
  466. //
  467. PcirBlockPtr = (INTEL_VBIOS_PCIR_STRUCTURE *)((UINT8 *)VBiosPtr + VBiosPtr->PcirOffset);
  468. PciVenderId = PcirBlockPtr->VendorId;
  469. if( (VBiosPtr->Signature != OPTION_ROM_SIGNATURE) || (PciVenderId != IGD_VID)) {
  470. //
  471. // Intel video BIOS not found.
  472. //
  473. VBiosVbtPtr = NULL;
  474. return EFI_UNSUPPORTED;
  475. }
  476. } else {
  477. //
  478. // No Video BIOS found, try to get VBT from FV.
  479. //
  480. GetIntegratedIntelVbtPtr (&VbtFileBuffer);
  481. if (VbtFileBuffer != NULL) {
  482. //
  483. // Video BIOS not found, use VBT from FV
  484. //
  485. DEBUG ((EFI_D_ERROR, "VBT data found\n"));
  486. (gBS->CopyMem) (
  487. mIgdOpRegion.OpRegion->VBT.GVD1,
  488. VbtFileBuffer,
  489. VbtFileBuffer->HeaderVbtSize
  490. );
  491. FreePool (VbtFileBuffer);
  492. return EFI_SUCCESS;
  493. }
  494. }
  495. if (VBiosPtr == NULL) {
  496. //
  497. // Intel video BIOS not found.
  498. //
  499. VBiosVbtPtr = NULL;
  500. return EFI_UNSUPPORTED;
  501. }
  502. }
  503. DEBUG ((EFI_D_ERROR, "VBIOS found at 0x%X\n", VBiosPtr));
  504. VBiosVbtPtr = (VBIOS_VBT_STRUCTURE *) ((UINT8 *) VBiosPtr + VBiosPtr->VbtOffset);
  505. if ((*((UINT32 *) (VBiosVbtPtr->HeaderSignature))) != VBT_SIGNATURE) {
  506. return EFI_UNSUPPORTED;
  507. }
  508. //
  509. // Initialize Video BIOS version with its build number.
  510. //
  511. mIgdOpRegion.OpRegion->Header.VVER[0] = VBiosVbtPtr->CoreBlockBiosBuild[0];
  512. mIgdOpRegion.OpRegion->Header.VVER[1] = VBiosVbtPtr->CoreBlockBiosBuild[1];
  513. mIgdOpRegion.OpRegion->Header.VVER[2] = VBiosVbtPtr->CoreBlockBiosBuild[2];
  514. mIgdOpRegion.OpRegion->Header.VVER[3] = VBiosVbtPtr->CoreBlockBiosBuild[3];
  515. (gBS->CopyMem) (
  516. mIgdOpRegion.OpRegion->VBT.GVD1,
  517. VBiosVbtPtr,
  518. VBiosVbtPtr->HeaderVbtSize
  519. );
  520. //
  521. // Return final status
  522. //
  523. return EFI_SUCCESS;
  524. }
  525. /**
  526. Graphics OpRegion / Software SCI driver installation function.
  527. @param ImageHandle Handle for this drivers loaded image protocol.
  528. @param SystemTable EFI system table.
  529. @retval EFI_SUCCESS The driver installed without error.
  530. @retval EFI_ABORTED The driver encountered an error and could not complete
  531. installation of the ACPI tables.
  532. **/
  533. EFI_STATUS
  534. IgdOpRegionInit (
  535. void
  536. )
  537. {
  538. EFI_HANDLE Handle;
  539. EFI_STATUS Status;
  540. EFI_GLOBAL_NVS_AREA_PROTOCOL *GlobalNvsArea;
  541. UINT32 DwordData;
  542. EFI_CPU_IO2_PROTOCOL *CpuIo;
  543. UINT16 Data16;
  544. UINT16 AcpiBase;
  545. VOID *gConOutNotifyReg;
  546. //
  547. // Locate the Global NVS Protocol.
  548. //
  549. Status = gBS->LocateProtocol (
  550. &gEfiGlobalNvsAreaProtocolGuid,
  551. NULL,
  552. (void **)&GlobalNvsArea
  553. );
  554. ASSERT_EFI_ERROR (Status);
  555. //
  556. // Allocate an ACPI NVS memory buffer as the IGD OpRegion, zero initialize
  557. // the first 1K, and set the IGD OpRegion pointer in the Global NVS
  558. // area structure.
  559. //
  560. Status = (gBS->AllocatePool) (
  561. EfiACPIMemoryNVS,
  562. sizeof (IGD_OPREGION_STRUC),
  563. (void **)&mIgdOpRegion.OpRegion
  564. );
  565. ASSERT_EFI_ERROR (Status);
  566. (gBS->SetMem) (
  567. mIgdOpRegion.OpRegion,
  568. sizeof (IGD_OPREGION_STRUC),
  569. 0
  570. );
  571. GlobalNvsArea->Area->IgdOpRegionAddress = (UINT32)(UINTN)(mIgdOpRegion.OpRegion);
  572. //
  573. // If IGD is disabled return
  574. //
  575. if (IgdMmPci32 (0) == 0xFFFFFFFF) {
  576. return EFI_SUCCESS;
  577. }
  578. //
  579. // Initialize OpRegion Header
  580. //
  581. (gBS->CopyMem) (
  582. mIgdOpRegion.OpRegion->Header.SIGN,
  583. HEADER_SIGNATURE,
  584. sizeof(HEADER_SIGNATURE)
  585. );
  586. //
  587. // Set OpRegion Size in KBs
  588. //
  589. mIgdOpRegion.OpRegion->Header.SIZE = HEADER_SIZE/1024;
  590. //
  591. // FIXME: Need to check Header OVER Field and the supported version.
  592. //
  593. mIgdOpRegion.OpRegion->Header.OVER = (UINT32) (LShiftU64 (HEADER_OPREGION_VER, 16) + LShiftU64 (HEADER_OPREGION_REV, 8));
  594. #ifdef ECP_FLAG
  595. CopyMem(mIgdOpRegion.OpRegion->Header.SVER, gSVER, sizeof(gSVER));
  596. #else
  597. gBS->CopyMem(
  598. mIgdOpRegion.OpRegion->Header.SVER,
  599. gSVER,
  600. sizeof(gSVER)
  601. );
  602. #endif
  603. DEBUG ((EFI_D_ERROR, "System BIOS ID is %a\n", mIgdOpRegion.OpRegion->Header.SVER));
  604. mIgdOpRegion.OpRegion->Header.MBOX = HEADER_MBOX_SUPPORT;
  605. if( 1 == DxePlatformSaPolicy->IdleReserve) {
  606. mIgdOpRegion.OpRegion->Header.PCON = (mIgdOpRegion.OpRegion->Header.PCON & 0xFFFC) | BIT1;
  607. } else {
  608. mIgdOpRegion.OpRegion->Header.PCON = (mIgdOpRegion.OpRegion->Header.PCON & 0xFFFC) | (BIT1 | BIT0);
  609. }
  610. //
  611. //For graphics driver to identify if LPE Audio/HD Audio is enabled on the platform
  612. //
  613. mIgdOpRegion.OpRegion->Header.PCON &= AUDIO_TYPE_SUPPORT_MASK;
  614. mIgdOpRegion.OpRegion->Header.PCON &= AUDIO_TYPE_FIELD_MASK;
  615. if ( 1 == DxePlatformSaPolicy->AudioTypeSupport ) {
  616. mIgdOpRegion.OpRegion->Header.PCON = HD_AUDIO_SUPPORT;
  617. mIgdOpRegion.OpRegion->Header.PCON |= AUDIO_TYPE_FIELD_VALID;
  618. }
  619. //
  620. // Initialize OpRegion Mailbox 1 (Public ACPI Methods).
  621. //
  622. //<TODO> The initial setting of mailbox 1 fields is implementation specific.
  623. // Adjust them as needed many even coming from user setting in setup.
  624. //
  625. //Workaround to solve LVDS is off after entering OS in desktop platform
  626. //
  627. mIgdOpRegion.OpRegion->MBox1.CLID = DxePlatformSaPolicy->IgdPanelFeatures.LidStatus;
  628. //
  629. // Initialize OpRegion Mailbox 3 (ASLE Interrupt and Power Conservation).
  630. //
  631. //<TODO> The initial setting of mailbox 3 fields is implementation specific.
  632. // Adjust them as needed many even coming from user setting in setup.
  633. //
  634. //
  635. // Do not initialize TCHE. This field is written by the graphics driver only.
  636. //
  637. //
  638. // The ALSI field is generally initialized by ASL code by reading the embedded controller.
  639. //
  640. mIgdOpRegion.OpRegion->MBox3.BCLP = BACKLIGHT_BRIGHTNESS;
  641. mIgdOpRegion.OpRegion->MBox3.PFIT = (FIELD_VALID_BIT | PFIT_STRETCH);
  642. if ( DxePlatformSaPolicy->IgdPanelFeatures.PFITStatus == 2) {
  643. //
  644. // Center
  645. //
  646. mIgdOpRegion.OpRegion->MBox3.PFIT = (FIELD_VALID_BIT | PFIT_CENTER);
  647. } else if (DxePlatformSaPolicy->IgdPanelFeatures.PFITStatus == 1) {
  648. //
  649. // Stretch
  650. //
  651. mIgdOpRegion.OpRegion->MBox3.PFIT = (FIELD_VALID_BIT | PFIT_STRETCH);
  652. } else {
  653. //
  654. // Auto
  655. //
  656. mIgdOpRegion.OpRegion->MBox3.PFIT = (FIELD_VALID_BIT | PFIT_SETUP_AUTO);
  657. }
  658. //
  659. // Set Initial current Brightness
  660. //
  661. mIgdOpRegion.OpRegion->MBox3.CBLV = (INIT_BRIGHT_LEVEL | FIELD_VALID_BIT);
  662. //
  663. // <EXAMPLE> Create a static Backlight Brightness Level Duty cycle Mapping Table
  664. // Possible 20 entries (example used 10), each 16 bits as follows:
  665. // [15] = Field Valid bit, [14:08] = Level in Percentage (0-64h), [07:00] = Desired duty cycle (0 - FFh).
  666. //
  667. // % Brightness
  668. mIgdOpRegion.OpRegion->MBox3.BCLM[0] = ( ( 0 << 8 ) + ( 0xFF - 0xFC ) + WORD_FIELD_VALID_BIT);
  669. mIgdOpRegion.OpRegion->MBox3.BCLM[1] = ( ( 1 << 8 ) + ( 0xFF - 0xFC ) + WORD_FIELD_VALID_BIT);
  670. mIgdOpRegion.OpRegion->MBox3.BCLM[2] = ( ( 10 << 8 ) + ( 0xFF - 0xE5 ) + WORD_FIELD_VALID_BIT);
  671. mIgdOpRegion.OpRegion->MBox3.BCLM[3] = ( ( 19 << 8 ) + ( 0xFF - 0xCE ) + WORD_FIELD_VALID_BIT);
  672. mIgdOpRegion.OpRegion->MBox3.BCLM[4] = ( ( 28 << 8 ) + ( 0xFF - 0xB7 ) + WORD_FIELD_VALID_BIT);
  673. mIgdOpRegion.OpRegion->MBox3.BCLM[5] = ( ( 37 << 8 ) + ( 0xFF - 0xA0 ) + WORD_FIELD_VALID_BIT);
  674. mIgdOpRegion.OpRegion->MBox3.BCLM[6] = ( ( 46 << 8 ) + ( 0xFF - 0x89 ) + WORD_FIELD_VALID_BIT);
  675. mIgdOpRegion.OpRegion->MBox3.BCLM[7] = ( ( 55 << 8 ) + ( 0xFF - 0x72 ) + WORD_FIELD_VALID_BIT);
  676. mIgdOpRegion.OpRegion->MBox3.BCLM[8] = ( ( 64 << 8 ) + ( 0xFF - 0x5B ) + WORD_FIELD_VALID_BIT);
  677. mIgdOpRegion.OpRegion->MBox3.BCLM[9] = ( ( 73 << 8 ) + ( 0xFF - 0x44 ) + WORD_FIELD_VALID_BIT);
  678. mIgdOpRegion.OpRegion->MBox3.BCLM[10] = ( ( 82 << 8 ) + ( 0xFF - 0x2D ) + WORD_FIELD_VALID_BIT);
  679. mIgdOpRegion.OpRegion->MBox3.BCLM[11] = ( ( 91 << 8 ) + ( 0xFF - 0x16 ) + WORD_FIELD_VALID_BIT);
  680. mIgdOpRegion.OpRegion->MBox3.BCLM[12] = ( (100 << 8 ) + ( 0xFF - 0x00 ) + WORD_FIELD_VALID_BIT);
  681. mIgdOpRegion.OpRegion->MBox3.PCFT = ((UINT32) GlobalNvsArea->Area->IgdPowerConservation) | BIT31;
  682. //
  683. // Create the notification and register callback function on the PciIo installation,
  684. //
  685. //
  686. Status = gBS->CreateEvent (
  687. EVT_NOTIFY_SIGNAL,
  688. TPL_CALLBACK,
  689. (EFI_EVENT_NOTIFY)GetVBiosVbtCallback,
  690. NULL,
  691. &mConOutEvent
  692. );
  693. ASSERT_EFI_ERROR (Status);
  694. if (EFI_ERROR (Status)) {
  695. return Status;
  696. }
  697. Status = gBS->RegisterProtocolNotify (
  698. #ifdef ECP_FLAG
  699. &gExitPmAuthProtocolGuid,
  700. #else
  701. &gEfiDxeSmmReadyToLockProtocolGuid,
  702. #endif
  703. mConOutEvent,
  704. &gConOutNotifyReg
  705. );
  706. Status = gBS->CreateEvent (
  707. EVT_NOTIFY_SIGNAL,
  708. TPL_CALLBACK,
  709. (EFI_EVENT_NOTIFY)SetGOPVersionCallback,
  710. NULL,
  711. &mSetGOPverEvent
  712. );
  713. ASSERT_EFI_ERROR (Status);
  714. if (EFI_ERROR (Status)) {
  715. return Status;
  716. }
  717. Status = gBS->RegisterProtocolNotify (
  718. &gEfiGraphicsOutputProtocolGuid,
  719. mSetGOPverEvent,
  720. &gConOutNotifyReg
  721. );
  722. //
  723. // Initialize hardware state:
  724. // Set ASLS Register to the OpRegion physical memory address.
  725. // Set SWSCI register bit 15 to a "1" to activate SCI interrupts.
  726. //
  727. IgdMmPci32 (IGD_ASLS_OFFSET) = (UINT32)(UINTN)(mIgdOpRegion.OpRegion);
  728. IgdMmPci16AndThenOr (IGD_SWSCI_OFFSET, ~(BIT0), BIT15);
  729. DwordData = IgdMmPci32 (IGD_ASLS_OFFSET);
  730. S3BootScriptSavePciCfgWrite (
  731. S3BootScriptWidthUint32,
  732. (UINTN) (EFI_PCI_ADDRESS (IGD_BUS, IGD_DEV, IGD_FUN_0, IGD_ASLS_OFFSET)),
  733. 1,
  734. &DwordData
  735. );
  736. DwordData = IgdMmPci32 (IGD_SWSCI_OFFSET);
  737. S3BootScriptSavePciCfgWrite (
  738. S3BootScriptWidthUint32,
  739. (UINTN) (EFI_PCI_ADDRESS (IGD_BUS, IGD_DEV, IGD_FUN_0, IGD_SWSCI_OFFSET)),
  740. 1,
  741. &DwordData
  742. );
  743. AcpiBase = MmPci16 (
  744. 0,
  745. DEFAULT_PCI_BUS_NUMBER_PCH,
  746. PCI_DEVICE_NUMBER_PCH_LPC,
  747. PCI_FUNCTION_NUMBER_PCH_LPC,
  748. R_PCH_LPC_ACPI_BASE
  749. ) & B_PCH_LPC_ACPI_BASE_BAR;
  750. //
  751. // Find the CPU I/O Protocol. ASSERT if not found.
  752. //
  753. Status = gBS->LocateProtocol (
  754. &gEfiCpuIo2ProtocolGuid,
  755. NULL,
  756. (void **)&CpuIo
  757. );
  758. ASSERT_EFI_ERROR (Status);
  759. CpuIo->Io.Read (
  760. CpuIo,
  761. EfiCpuIoWidthUint16,
  762. AcpiBase + R_PCH_ACPI_GPE0a_STS,
  763. 1,
  764. &Data16
  765. );
  766. //
  767. // Clear the B_PCH_ACPI_GPE0a_STS_GUNIT_SCI bit in R_PCH_ACPI_GPE0a_STS by writing a '1'.
  768. //
  769. Data16 |= B_PCH_ACPI_GPE0a_STS_GUNIT_SCI;
  770. CpuIo->Io.Write (
  771. CpuIo,
  772. EfiCpuIoWidthUint16,
  773. AcpiBase + R_PCH_ACPI_GPE0a_STS,
  774. 1,
  775. &Data16
  776. );
  777. //
  778. // Install OpRegion / Software SCI protocol
  779. //
  780. Handle = NULL;
  781. Status = gBS->InstallMultipleProtocolInterfaces (
  782. &Handle,
  783. &gIgdOpRegionProtocolGuid,
  784. &mIgdOpRegion,
  785. NULL
  786. );
  787. ASSERT_EFI_ERROR (Status);
  788. //
  789. // Return final status
  790. //
  791. return EFI_SUCCESS;
  792. }