SsdtPcieGenerator.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. /** @file
  2. SSDT Pcie Table Generator.
  3. Copyright (c) 2021 - 2022, Arm Limited. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. @par Reference(s):
  6. - PCI Firmware Specification - Revision 3.0
  7. - ACPI 6.4 specification:
  8. - s6.2.13 "_PRT (PCI Routing Table)"
  9. - s6.1.1 "_ADR (Address)"
  10. - linux kernel code
  11. - Arm Base Boot Requirements v1.0
  12. - Arm Base System Architecture v1.0
  13. **/
  14. #include <Library/AcpiLib.h>
  15. #include <Library/BaseLib.h>
  16. #include <Library/BaseMemoryLib.h>
  17. #include <Library/DebugLib.h>
  18. #include <Library/MemoryAllocationLib.h>
  19. #include <Protocol/AcpiTable.h>
  20. // Module specific include files.
  21. #include <AcpiTableGenerator.h>
  22. #include <ConfigurationManagerObject.h>
  23. #include <ConfigurationManagerHelper.h>
  24. #include <Library/AcpiHelperLib.h>
  25. #include <Library/TableHelperLib.h>
  26. #include <Library/AmlLib/AmlLib.h>
  27. #include <Library/SsdtPcieSupportLib.h>
  28. #include <Protocol/ConfigurationManagerProtocol.h>
  29. #include "SsdtPcieGenerator.h"
  30. /** ARM standard SSDT Pcie Table Generator.
  31. Requirements:
  32. The following Configuration Manager Object(s) are required by
  33. this Generator:
  34. - EArmObjCmRef
  35. - EArmObjPciConfigSpaceInfo
  36. - EArmObjPciAddressMapInfo
  37. - EArmObjPciInterruptMapInfo
  38. */
  39. /** This macro expands to a function that retrieves the cross-CM-object-
  40. reference information from the Configuration Manager.
  41. */
  42. GET_OBJECT_LIST (
  43. EObjNameSpaceArm,
  44. EArmObjCmRef,
  45. CM_ARM_OBJ_REF
  46. );
  47. /** This macro expands to a function that retrieves the Pci
  48. Configuration Space Information from the Configuration Manager.
  49. */
  50. GET_OBJECT_LIST (
  51. EObjNameSpaceArm,
  52. EArmObjPciConfigSpaceInfo,
  53. CM_ARM_PCI_CONFIG_SPACE_INFO
  54. );
  55. /** This macro expands to a function that retrieves the Pci
  56. Address Mapping Information from the Configuration Manager.
  57. */
  58. GET_OBJECT_LIST (
  59. EObjNameSpaceArm,
  60. EArmObjPciAddressMapInfo,
  61. CM_ARM_PCI_ADDRESS_MAP_INFO
  62. );
  63. /** This macro expands to a function that retrieves the Pci
  64. Interrupt Mapping Information from the Configuration Manager.
  65. */
  66. GET_OBJECT_LIST (
  67. EObjNameSpaceArm,
  68. EArmObjPciInterruptMapInfo,
  69. CM_ARM_PCI_INTERRUPT_MAP_INFO
  70. );
  71. /** Initialize the MappingTable.
  72. @param [in] MappingTable The mapping table structure.
  73. @param [in] Count Number of entries to allocate in the
  74. MappingTable.
  75. @retval EFI_SUCCESS Success.
  76. @retval EFI_INVALID_PARAMETER Invalid parameter.
  77. @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
  78. **/
  79. STATIC
  80. EFI_STATUS
  81. EFIAPI
  82. MappingTableInitialize (
  83. IN MAPPING_TABLE *MappingTable,
  84. IN UINT32 Count
  85. )
  86. {
  87. UINT32 *Table;
  88. if ((MappingTable == NULL) ||
  89. (Count == 0))
  90. {
  91. ASSERT (0);
  92. return EFI_INVALID_PARAMETER;
  93. }
  94. Table = AllocateZeroPool (sizeof (*Table) * Count);
  95. if (Table == NULL) {
  96. ASSERT (0);
  97. return EFI_OUT_OF_RESOURCES;
  98. }
  99. MappingTable->Table = Table;
  100. MappingTable->LastIndex = 0;
  101. MappingTable->MaxIndex = Count;
  102. return EFI_SUCCESS;
  103. }
  104. /** Free the MappingTable.
  105. @param [in, out] MappingTable The mapping table structure.
  106. **/
  107. STATIC
  108. VOID
  109. EFIAPI
  110. MappingTableFree (
  111. IN OUT MAPPING_TABLE *MappingTable
  112. )
  113. {
  114. ASSERT (MappingTable != NULL);
  115. ASSERT (MappingTable->Table != NULL);
  116. if (MappingTable->Table != NULL) {
  117. FreePool (MappingTable->Table);
  118. }
  119. }
  120. /** Add a new entry to the MappingTable and return its index.
  121. If an entry with [Integer] is already available in the table,
  122. return its index without adding a new entry.
  123. @param [in] MappingTable The mapping table structure.
  124. @param [in] Integer New Integer entry to add.
  125. @retval The index of the Integer entry in the MappingTable.
  126. **/
  127. STATIC
  128. UINT32
  129. EFIAPI
  130. MappingTableAdd (
  131. IN MAPPING_TABLE *MappingTable,
  132. IN UINT32 Integer
  133. )
  134. {
  135. UINT32 *Table;
  136. UINT32 Index;
  137. UINT32 LastIndex;
  138. ASSERT (MappingTable != NULL);
  139. ASSERT (MappingTable->Table != NULL);
  140. Table = MappingTable->Table;
  141. LastIndex = MappingTable->LastIndex;
  142. // Search if there is already an entry with this Integer.
  143. for (Index = 0; Index < LastIndex; Index++) {
  144. if (Table[Index] == Integer) {
  145. return Index;
  146. }
  147. }
  148. ASSERT (LastIndex < MappingTable->MaxIndex);
  149. // If no, create a new entry.
  150. Table[LastIndex] = Integer;
  151. return MappingTable->LastIndex++;
  152. }
  153. /** Generate required Pci device information.
  154. ASL code:
  155. Name (_UID, <Uid>) // Uid of the Pci device
  156. Name (_HID, EISAID ("PNP0A08")) // PCI Express Root Bridge
  157. Name (_CID, EISAID ("PNP0A03")) // Compatible PCI Root Bridge
  158. Name (_SEG, <Pci segment group>) // PCI Segment Group number
  159. Name (_BBN, <Bus number>) // PCI Base Bus Number
  160. Name (_CCA, 1) // Initially mark the PCI coherent
  161. @param [in] PciInfo Pci device information.
  162. @param [in] Uid Unique Id of the Pci device.
  163. @param [in, out] PciNode Pci node to amend.
  164. @retval EFI_SUCCESS Success.
  165. @retval EFI_INVALID_PARAMETER Invalid parameter.
  166. @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
  167. **/
  168. STATIC
  169. EFI_STATUS
  170. EFIAPI
  171. GeneratePciDeviceInfo (
  172. IN CONST CM_ARM_PCI_CONFIG_SPACE_INFO *PciInfo,
  173. IN UINT32 Uid,
  174. IN OUT AML_OBJECT_NODE_HANDLE PciNode
  175. )
  176. {
  177. EFI_STATUS Status;
  178. UINT32 EisaId;
  179. ASSERT (PciInfo != NULL);
  180. ASSERT (PciNode != NULL);
  181. // ASL: Name (_UID, <Uid>)
  182. Status = AmlCodeGenNameInteger ("_UID", Uid, PciNode, NULL);
  183. if (EFI_ERROR (Status)) {
  184. ASSERT (0);
  185. return Status;
  186. }
  187. // ASL: Name (_HID, EISAID ("PNP0A08"))
  188. Status = AmlGetEisaIdFromString ("PNP0A08", &EisaId);
  189. if (EFI_ERROR (Status)) {
  190. ASSERT (0);
  191. return Status;
  192. }
  193. Status = AmlCodeGenNameInteger ("_HID", EisaId, PciNode, NULL);
  194. if (EFI_ERROR (Status)) {
  195. ASSERT (0);
  196. return Status;
  197. }
  198. // ASL: Name (_CID, EISAID ("PNP0A03"))
  199. Status = AmlGetEisaIdFromString ("PNP0A03", &EisaId);
  200. if (EFI_ERROR (Status)) {
  201. ASSERT (0);
  202. return Status;
  203. }
  204. Status = AmlCodeGenNameInteger ("_CID", EisaId, PciNode, NULL);
  205. if (EFI_ERROR (Status)) {
  206. ASSERT (0);
  207. return Status;
  208. }
  209. // ASL: Name (_SEG, <Pci segment group>)
  210. Status = AmlCodeGenNameInteger (
  211. "_SEG",
  212. PciInfo->PciSegmentGroupNumber,
  213. PciNode,
  214. NULL
  215. );
  216. if (EFI_ERROR (Status)) {
  217. ASSERT (0);
  218. return Status;
  219. }
  220. // ASL: Name (_BBN, <Bus number>)
  221. Status = AmlCodeGenNameInteger (
  222. "_BBN",
  223. PciInfo->StartBusNumber,
  224. PciNode,
  225. NULL
  226. );
  227. if (EFI_ERROR (Status)) {
  228. ASSERT (0);
  229. return Status;
  230. }
  231. // ASL: Name (_CCA, 1)
  232. // Must be aligned with the IORT CCA property in
  233. // "Table 14 Memory access properties"
  234. Status = AmlCodeGenNameInteger ("_CCA", 1, PciNode, NULL);
  235. ASSERT_EFI_ERROR (Status);
  236. return Status;
  237. }
  238. /** Generate a _PRT object (Pci Routing Table) for the Pci device.
  239. Cf. ACPI 6.4 specification, s6.2.13 "_PRT (PCI Routing Table)"
  240. The first model (defining a _CRS object) is used. This is necessary because
  241. PCI legacy interrupts are active low and GICv2 SPI interrupts are active
  242. high.
  243. Even though PCI interrupts cannot be re-routed, only the first model allows
  244. to specify the activation state (low/high).
  245. @param [in] Generator The SSDT Pci generator.
  246. @param [in] CfgMgrProtocol Pointer to the Configuration Manager
  247. Protocol interface.
  248. @param [in] PciInfo Pci device information.
  249. @param [in, out] PciNode Pci node to amend.
  250. @retval EFI_SUCCESS The function completed successfully.
  251. @retval EFI_INVALID_PARAMETER Invalid parameter.
  252. @retval EFI_OUT_OF_RESOURCES Could not allocate memory.
  253. **/
  254. STATIC
  255. EFI_STATUS
  256. EFIAPI
  257. GeneratePrt (
  258. IN ACPI_PCI_GENERATOR *Generator,
  259. IN CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL *CONST CfgMgrProtocol,
  260. IN CONST CM_ARM_PCI_CONFIG_SPACE_INFO *PciInfo,
  261. IN OUT AML_OBJECT_NODE_HANDLE PciNode
  262. )
  263. {
  264. EFI_STATUS Status;
  265. INT32 Index;
  266. AML_OBJECT_NODE_HANDLE PrtNode;
  267. CM_ARM_OBJ_REF *RefInfo;
  268. UINT32 RefCount;
  269. CM_ARM_PCI_INTERRUPT_MAP_INFO *IrqMapInfo;
  270. ASSERT (Generator != NULL);
  271. ASSERT (CfgMgrProtocol != NULL);
  272. ASSERT (PciInfo != NULL);
  273. ASSERT (PciNode != NULL);
  274. PrtNode = NULL;
  275. // Get the array of CM_ARM_OBJ_REF referencing the
  276. // CM_ARM_PCI_INTERRUPT_MAP_INFO objects.
  277. Status = GetEArmObjCmRef (
  278. CfgMgrProtocol,
  279. PciInfo->InterruptMapToken,
  280. &RefInfo,
  281. &RefCount
  282. );
  283. if (EFI_ERROR (Status)) {
  284. ASSERT (0);
  285. return Status;
  286. }
  287. // Initialized DeviceTable.
  288. Status = MappingTableInitialize (&Generator->DeviceTable, RefCount);
  289. if (EFI_ERROR (Status)) {
  290. ASSERT (0);
  291. goto exit_handler0;
  292. }
  293. // ASL: Name (_PRT, Package () {})
  294. Status = AmlCodeGenNamePackage ("_PRT", NULL, &PrtNode);
  295. if (EFI_ERROR (Status)) {
  296. ASSERT (0);
  297. goto exit_handler;
  298. }
  299. for (Index = 0; Index < RefCount; Index++) {
  300. // Get CM_ARM_PCI_INTERRUPT_MAP_INFO structures one by one.
  301. Status = GetEArmObjPciInterruptMapInfo (
  302. CfgMgrProtocol,
  303. RefInfo[Index].ReferenceToken,
  304. &IrqMapInfo,
  305. NULL
  306. );
  307. if (EFI_ERROR (Status)) {
  308. ASSERT (0);
  309. goto exit_handler;
  310. }
  311. // Check that the interrupts flags are SPIs, level high.
  312. // Cf. Arm BSA v1.0, sE.6 "Legacy interrupts"
  313. if ((Index > 0) &&
  314. (IrqMapInfo->IntcInterrupt.Interrupt >= 32) &&
  315. (IrqMapInfo->IntcInterrupt.Interrupt < 1020) &&
  316. ((IrqMapInfo->IntcInterrupt.Flags & 0xB) != 0))
  317. {
  318. Status = EFI_INVALID_PARAMETER;
  319. ASSERT_EFI_ERROR (Status);
  320. goto exit_handler;
  321. }
  322. // Add the device to the DeviceTable.
  323. MappingTableAdd (&Generator->DeviceTable, IrqMapInfo->PciDevice);
  324. /* Add a _PRT entry.
  325. ASL
  326. Name (_PRT, Package () {
  327. <OldPrtEntries>,
  328. <NewPrtEntry>
  329. })
  330. Address is set as:
  331. ACPI 6.4 specification, Table 6.2: "ADR Object Address Encodings"
  332. High word-Device #, Low word-Function #. (for example, device 3,
  333. function 2 is 0x00030002). To refer to all the functions on a device #,
  334. use a function number of FFFF).
  335. */
  336. Status = AmlAddPrtEntry (
  337. (IrqMapInfo->PciDevice << 16) | 0xFFFF,
  338. IrqMapInfo->PciInterrupt,
  339. NULL,
  340. IrqMapInfo->IntcInterrupt.Interrupt,
  341. PrtNode
  342. );
  343. if (EFI_ERROR (Status)) {
  344. ASSERT (0);
  345. goto exit_handler;
  346. }
  347. } // for
  348. // Attach the _PRT entry.
  349. Status = AmlAttachNode (PciNode, PrtNode);
  350. if (EFI_ERROR (Status)) {
  351. ASSERT_EFI_ERROR (Status);
  352. goto exit_handler;
  353. }
  354. PrtNode = NULL;
  355. // Generate the Pci slots once all the device have been added.
  356. Status = GeneratePciSlots (PciInfo, &Generator->DeviceTable, PciNode);
  357. if (EFI_ERROR (Status)) {
  358. ASSERT (0);
  359. goto exit_handler;
  360. }
  361. exit_handler:
  362. MappingTableFree (&Generator->DeviceTable);
  363. exit_handler0:
  364. if (PrtNode != NULL) {
  365. AmlDeleteTree (PrtNode);
  366. }
  367. return Status;
  368. }
  369. /** Generate a _CRS method for the Pci device.
  370. @param [in] Generator The SSDT Pci generator.
  371. @param [in] CfgMgrProtocol Pointer to the Configuration Manager
  372. Protocol interface.
  373. @param [in] PciInfo Pci device information.
  374. @param [in, out] PciNode Pci node to amend.
  375. @retval EFI_SUCCESS The function completed successfully.
  376. @retval EFI_INVALID_PARAMETER Invalid parameter.
  377. @retval EFI_OUT_OF_RESOURCES Could not allocate memory.
  378. **/
  379. STATIC
  380. EFI_STATUS
  381. EFIAPI
  382. GeneratePciCrs (
  383. IN ACPI_PCI_GENERATOR *Generator,
  384. IN CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL *CONST CfgMgrProtocol,
  385. IN CONST CM_ARM_PCI_CONFIG_SPACE_INFO *PciInfo,
  386. IN OUT AML_OBJECT_NODE_HANDLE PciNode
  387. )
  388. {
  389. EFI_STATUS Status;
  390. BOOLEAN Translation;
  391. UINT32 Index;
  392. CM_ARM_OBJ_REF *RefInfo;
  393. UINT32 RefCount;
  394. CM_ARM_PCI_ADDRESS_MAP_INFO *AddrMapInfo;
  395. AML_OBJECT_NODE_HANDLE CrsNode;
  396. BOOLEAN IsPosDecode;
  397. ASSERT (Generator != NULL);
  398. ASSERT (CfgMgrProtocol != NULL);
  399. ASSERT (PciInfo != NULL);
  400. ASSERT (PciNode != NULL);
  401. // ASL: Name (_CRS, ResourceTemplate () {})
  402. Status = AmlCodeGenNameResourceTemplate ("_CRS", PciNode, &CrsNode);
  403. if (EFI_ERROR (Status)) {
  404. ASSERT (0);
  405. return Status;
  406. }
  407. // ASL:
  408. // WordBusNumber ( // Bus numbers assigned to this root
  409. // ResourceProducer, MinFixed, MaxFixed, PosDecode,
  410. // 0, // AddressGranularity
  411. // <Start>, // AddressMinimum - Minimum Bus Number
  412. // <End>, // AddressMaximum - Maximum Bus Number
  413. // 0, // AddressTranslation - Set to 0
  414. // <End> - <Start> + 1 // RangeLength - Number of Busses
  415. // )
  416. Status = AmlCodeGenRdWordBusNumber (
  417. FALSE,
  418. TRUE,
  419. TRUE,
  420. TRUE,
  421. 0,
  422. PciInfo->StartBusNumber,
  423. PciInfo->EndBusNumber,
  424. 0,
  425. PciInfo->EndBusNumber - PciInfo->StartBusNumber + 1,
  426. 0,
  427. NULL,
  428. CrsNode,
  429. NULL
  430. );
  431. if (EFI_ERROR (Status)) {
  432. ASSERT (0);
  433. return Status;
  434. }
  435. // Get the array of CM_ARM_OBJ_REF referencing the
  436. // CM_ARM_PCI_ADDRESS_MAP_INFO objects.
  437. Status = GetEArmObjCmRef (
  438. CfgMgrProtocol,
  439. PciInfo->AddressMapToken,
  440. &RefInfo,
  441. &RefCount
  442. );
  443. if (EFI_ERROR (Status)) {
  444. ASSERT (0);
  445. return Status;
  446. }
  447. for (Index = 0; Index < RefCount; Index++) {
  448. // Get CM_ARM_PCI_ADDRESS_MAP_INFO structures one by one.
  449. Status = GetEArmObjPciAddressMapInfo (
  450. CfgMgrProtocol,
  451. RefInfo[Index].ReferenceToken,
  452. &AddrMapInfo,
  453. NULL
  454. );
  455. if (EFI_ERROR (Status)) {
  456. ASSERT (0);
  457. return Status;
  458. }
  459. Translation = (AddrMapInfo->CpuAddress != AddrMapInfo->PciAddress);
  460. if (AddrMapInfo->CpuAddress >= AddrMapInfo->PciAddress) {
  461. IsPosDecode = TRUE;
  462. } else {
  463. IsPosDecode = FALSE;
  464. }
  465. switch (AddrMapInfo->SpaceCode) {
  466. case PCI_SS_IO:
  467. Status = AmlCodeGenRdDWordIo (
  468. FALSE,
  469. TRUE,
  470. TRUE,
  471. IsPosDecode,
  472. 3,
  473. 0,
  474. AddrMapInfo->PciAddress,
  475. AddrMapInfo->PciAddress + AddrMapInfo->AddressSize - 1,
  476. Translation ? AddrMapInfo->CpuAddress - AddrMapInfo->PciAddress : 0,
  477. AddrMapInfo->AddressSize,
  478. 0,
  479. NULL,
  480. TRUE,
  481. FALSE,
  482. CrsNode,
  483. NULL
  484. );
  485. break;
  486. case PCI_SS_M32:
  487. Status = AmlCodeGenRdDWordMemory (
  488. FALSE,
  489. IsPosDecode,
  490. TRUE,
  491. TRUE,
  492. TRUE,
  493. TRUE,
  494. 0,
  495. AddrMapInfo->PciAddress,
  496. AddrMapInfo->PciAddress + AddrMapInfo->AddressSize - 1,
  497. Translation ? AddrMapInfo->CpuAddress - AddrMapInfo->PciAddress : 0,
  498. AddrMapInfo->AddressSize,
  499. 0,
  500. NULL,
  501. 0,
  502. TRUE,
  503. CrsNode,
  504. NULL
  505. );
  506. break;
  507. case PCI_SS_M64:
  508. Status = AmlCodeGenRdQWordMemory (
  509. FALSE,
  510. IsPosDecode,
  511. TRUE,
  512. TRUE,
  513. TRUE,
  514. TRUE,
  515. 0,
  516. AddrMapInfo->PciAddress,
  517. AddrMapInfo->PciAddress + AddrMapInfo->AddressSize - 1,
  518. Translation ? AddrMapInfo->CpuAddress - AddrMapInfo->PciAddress : 0,
  519. AddrMapInfo->AddressSize,
  520. 0,
  521. NULL,
  522. 0,
  523. TRUE,
  524. CrsNode,
  525. NULL
  526. );
  527. break;
  528. default:
  529. Status = EFI_INVALID_PARAMETER;
  530. } // switch
  531. if (EFI_ERROR (Status)) {
  532. ASSERT (0);
  533. return Status;
  534. }
  535. } // for
  536. return Status;
  537. }
  538. /** Generate a Pci device.
  539. @param [in] Generator The SSDT Pci generator.
  540. @param [in] CfgMgrProtocol Pointer to the Configuration Manager
  541. Protocol interface.
  542. @param [in] PciInfo Pci device information.
  543. @param [in] Uid Unique Id of the Pci device.
  544. @param [in, out] RootNode RootNode of the AML tree to populate.
  545. @retval EFI_SUCCESS The function completed successfully.
  546. @retval EFI_INVALID_PARAMETER Invalid parameter.
  547. @retval EFI_OUT_OF_RESOURCES Could not allocate memory.
  548. **/
  549. STATIC
  550. EFI_STATUS
  551. EFIAPI
  552. GeneratePciDevice (
  553. IN ACPI_PCI_GENERATOR *Generator,
  554. IN CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL *CONST CfgMgrProtocol,
  555. IN CONST CM_ARM_PCI_CONFIG_SPACE_INFO *PciInfo,
  556. IN UINT32 Uid,
  557. IN OUT AML_ROOT_NODE_HANDLE *RootNode
  558. )
  559. {
  560. EFI_STATUS Status;
  561. CHAR8 AslName[AML_NAME_SEG_SIZE + 1];
  562. AML_OBJECT_NODE_HANDLE ScopeNode;
  563. AML_OBJECT_NODE_HANDLE PciNode;
  564. ASSERT (Generator != NULL);
  565. ASSERT (CfgMgrProtocol != NULL);
  566. ASSERT (PciInfo != NULL);
  567. ASSERT (RootNode != NULL);
  568. PciNode = NULL;
  569. // ASL: Scope (\_SB) {}
  570. Status = AmlCodeGenScope (SB_SCOPE, RootNode, &ScopeNode);
  571. if (EFI_ERROR (Status)) {
  572. ASSERT (0);
  573. return Status;
  574. }
  575. // Write the name of the PCI device.
  576. CopyMem (AslName, "PCIx", AML_NAME_SEG_SIZE + 1);
  577. AslName[AML_NAME_SEG_SIZE - 1] = AsciiFromHex (Uid & 0xF);
  578. if (Uid > 0xF) {
  579. AslName[AML_NAME_SEG_SIZE - 2] = AsciiFromHex ((Uid >> 4) & 0xF);
  580. }
  581. // ASL: Device (PCIx) {}
  582. Status = AmlCodeGenDevice (AslName, ScopeNode, &PciNode);
  583. if (EFI_ERROR (Status)) {
  584. ASSERT (0);
  585. return Status;
  586. }
  587. // Populate the PCIx node with some Id values.
  588. Status = GeneratePciDeviceInfo (PciInfo, Uid, PciNode);
  589. if (EFI_ERROR (Status)) {
  590. ASSERT (0);
  591. return Status;
  592. }
  593. // Generate the Pci Routing Table (_PRT).
  594. if (PciInfo->InterruptMapToken != CM_NULL_TOKEN) {
  595. Status = GeneratePrt (
  596. Generator,
  597. CfgMgrProtocol,
  598. PciInfo,
  599. PciNode
  600. );
  601. if (EFI_ERROR (Status)) {
  602. ASSERT (0);
  603. return Status;
  604. }
  605. }
  606. // Generate the _CRS method.
  607. Status = GeneratePciCrs (Generator, CfgMgrProtocol, PciInfo, PciNode);
  608. if (EFI_ERROR (Status)) {
  609. ASSERT (0);
  610. return Status;
  611. }
  612. // Add the template _OSC method.
  613. Status = AddOscMethod (PciInfo, PciNode);
  614. ASSERT_EFI_ERROR (Status);
  615. return Status;
  616. }
  617. /** Build an Ssdt table describing a Pci device.
  618. @param [in] Generator The SSDT Pci generator.
  619. @param [in] CfgMgrProtocol Pointer to the Configuration Manager
  620. Protocol interface.
  621. @param [in] AcpiTableInfo Pointer to the ACPI table information.
  622. @param [in] PciInfo Pci device information.
  623. @param [in] Uid Unique Id of the Pci device.
  624. @param [out] Table If success, contains the created SSDT table.
  625. @retval EFI_SUCCESS The function completed successfully.
  626. @retval EFI_INVALID_PARAMETER Invalid parameter.
  627. @retval EFI_OUT_OF_RESOURCES Could not allocate memory.
  628. **/
  629. STATIC
  630. EFI_STATUS
  631. EFIAPI
  632. BuildSsdtPciTable (
  633. IN ACPI_PCI_GENERATOR *Generator,
  634. IN CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL *CONST CfgMgrProtocol,
  635. IN CONST CM_STD_OBJ_ACPI_TABLE_INFO *CONST AcpiTableInfo,
  636. IN CONST CM_ARM_PCI_CONFIG_SPACE_INFO *PciInfo,
  637. IN UINT32 Uid,
  638. OUT EFI_ACPI_DESCRIPTION_HEADER **Table
  639. )
  640. {
  641. EFI_STATUS Status;
  642. EFI_STATUS Status1;
  643. AML_ROOT_NODE_HANDLE RootNode;
  644. ASSERT (Generator != NULL);
  645. ASSERT (CfgMgrProtocol != NULL);
  646. ASSERT (PciInfo != NULL);
  647. ASSERT (Table != NULL);
  648. // Create a new Ssdt table.
  649. Status = AddSsdtAcpiHeader (
  650. CfgMgrProtocol,
  651. &Generator->Header,
  652. AcpiTableInfo,
  653. &RootNode
  654. );
  655. if (EFI_ERROR (Status)) {
  656. ASSERT (0);
  657. return Status;
  658. }
  659. Status = GeneratePciDevice (
  660. Generator,
  661. CfgMgrProtocol,
  662. PciInfo,
  663. Uid,
  664. RootNode
  665. );
  666. if (EFI_ERROR (Status)) {
  667. ASSERT (0);
  668. goto exit_handler;
  669. }
  670. // Serialize the tree.
  671. Status = AmlSerializeDefinitionBlock (
  672. RootNode,
  673. Table
  674. );
  675. if (EFI_ERROR (Status)) {
  676. DEBUG ((
  677. DEBUG_ERROR,
  678. "ERROR: SSDT-PCI: Failed to Serialize SSDT Table Data."
  679. " Status = %r\n",
  680. Status
  681. ));
  682. }
  683. exit_handler:
  684. // Cleanup
  685. Status1 = AmlDeleteTree (RootNode);
  686. if (EFI_ERROR (Status1)) {
  687. DEBUG ((
  688. DEBUG_ERROR,
  689. "ERROR: SSDT-PCI: Failed to cleanup AML tree."
  690. " Status = %r\n",
  691. Status1
  692. ));
  693. // If Status was success but we failed to delete the AML Tree
  694. // return Status1 else return the original error code, i.e. Status.
  695. if (!EFI_ERROR (Status)) {
  696. return Status1;
  697. }
  698. }
  699. return Status;
  700. }
  701. /** Construct SSDT tables describing Pci root complexes.
  702. This function invokes the Configuration Manager protocol interface
  703. to get the required hardware information for generating the ACPI
  704. table.
  705. If this function allocates any resources then they must be freed
  706. in the FreeXXXXTableResourcesEx function.
  707. @param [in] This Pointer to the ACPI table generator.
  708. @param [in] AcpiTableInfo Pointer to the ACPI table information.
  709. @param [in] CfgMgrProtocol Pointer to the Configuration Manager
  710. Protocol interface.
  711. @param [out] Table Pointer to a list of generated ACPI table(s).
  712. @param [out] TableCount Number of generated ACPI table(s).
  713. @retval EFI_SUCCESS Table generated successfully.
  714. @retval EFI_BAD_BUFFER_SIZE The size returned by the Configuration
  715. Manager is less than the Object size for
  716. the requested object.
  717. @retval EFI_INVALID_PARAMETER A parameter is invalid.
  718. @retval EFI_NOT_FOUND Could not find information.
  719. @retval EFI_OUT_OF_RESOURCES Could not allocate memory.
  720. @retval EFI_UNSUPPORTED Unsupported configuration.
  721. **/
  722. STATIC
  723. EFI_STATUS
  724. EFIAPI
  725. BuildSsdtPciTableEx (
  726. IN CONST ACPI_TABLE_GENERATOR *This,
  727. IN CONST CM_STD_OBJ_ACPI_TABLE_INFO *CONST AcpiTableInfo,
  728. IN CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL *CONST CfgMgrProtocol,
  729. OUT EFI_ACPI_DESCRIPTION_HEADER ***Table,
  730. OUT UINTN *CONST TableCount
  731. )
  732. {
  733. EFI_STATUS Status;
  734. CM_ARM_PCI_CONFIG_SPACE_INFO *PciInfo;
  735. UINT32 PciCount;
  736. UINTN Index;
  737. EFI_ACPI_DESCRIPTION_HEADER **TableList;
  738. ACPI_PCI_GENERATOR *Generator;
  739. ASSERT (This != NULL);
  740. ASSERT (AcpiTableInfo != NULL);
  741. ASSERT (CfgMgrProtocol != NULL);
  742. ASSERT (Table != NULL);
  743. ASSERT (TableCount != NULL);
  744. ASSERT (AcpiTableInfo->TableGeneratorId == This->GeneratorID);
  745. ASSERT (AcpiTableInfo->AcpiTableSignature == This->AcpiTableSignature);
  746. *TableCount = 0;
  747. Generator = (ACPI_PCI_GENERATOR *)This;
  748. Status = GetEArmObjPciConfigSpaceInfo (
  749. CfgMgrProtocol,
  750. CM_NULL_TOKEN,
  751. &PciInfo,
  752. &PciCount
  753. );
  754. if (EFI_ERROR (Status)) {
  755. ASSERT (0);
  756. return Status;
  757. }
  758. if (PciCount > MAX_PCI_ROOT_COMPLEXES_SUPPORTED) {
  759. DEBUG ((
  760. DEBUG_ERROR,
  761. "ERROR: SSDT-PCI: Too many Pci root complexes: %d."
  762. " Maximum Pci root complexes supported = %d.\n",
  763. PciCount,
  764. MAX_PCI_ROOT_COMPLEXES_SUPPORTED
  765. ));
  766. return EFI_INVALID_PARAMETER;
  767. }
  768. // Allocate a table to store pointers to the SSDT tables.
  769. TableList = (EFI_ACPI_DESCRIPTION_HEADER **)
  770. AllocateZeroPool (
  771. (sizeof (EFI_ACPI_DESCRIPTION_HEADER *) * PciCount)
  772. );
  773. if (TableList == NULL) {
  774. Status = EFI_OUT_OF_RESOURCES;
  775. DEBUG ((
  776. DEBUG_ERROR,
  777. "ERROR: SSDT-PCI: Failed to allocate memory for Table List."
  778. " Status = %r\n",
  779. Status
  780. ));
  781. return Status;
  782. }
  783. // Setup the table list early so that appropriate cleanup
  784. // can be done in case of failure.
  785. *Table = TableList;
  786. for (Index = 0; Index < PciCount; Index++) {
  787. // Build a SSDT table describing the Pci devices.
  788. Status = BuildSsdtPciTable (
  789. Generator,
  790. CfgMgrProtocol,
  791. AcpiTableInfo,
  792. &PciInfo[Index],
  793. Index,
  794. &TableList[Index]
  795. );
  796. if (EFI_ERROR (Status)) {
  797. DEBUG ((
  798. DEBUG_ERROR,
  799. "ERROR: SSDT-PCI: Failed to build associated SSDT table."
  800. " Status = %r\n",
  801. Status
  802. ));
  803. goto error_handler;
  804. }
  805. *TableCount += 1;
  806. } // for
  807. error_handler:
  808. // Note: Table list and Table count have been setup. The
  809. // error handler does nothing here as the framework will invoke
  810. // FreeSsdtPciTableEx () even on failure.
  811. return Status;
  812. }
  813. /** Free any resources allocated for constructing the tables.
  814. @param [in] This Pointer to the ACPI table generator.
  815. @param [in] AcpiTableInfo Pointer to the ACPI Table Info.
  816. @param [in] CfgMgrProtocol Pointer to the Configuration Manager
  817. Protocol Interface.
  818. @param [in, out] Table Pointer to an array of pointers
  819. to ACPI Table(s).
  820. @param [in] TableCount Number of ACPI table(s).
  821. @retval EFI_SUCCESS The resources were freed successfully.
  822. @retval EFI_INVALID_PARAMETER The table pointer is NULL or invalid.
  823. **/
  824. STATIC
  825. EFI_STATUS
  826. EFIAPI
  827. FreeSsdtPciTableEx (
  828. IN CONST ACPI_TABLE_GENERATOR *CONST This,
  829. IN CONST CM_STD_OBJ_ACPI_TABLE_INFO *CONST AcpiTableInfo,
  830. IN CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL *CONST CfgMgrProtocol,
  831. IN OUT EFI_ACPI_DESCRIPTION_HEADER ***CONST Table,
  832. IN CONST UINTN TableCount
  833. )
  834. {
  835. EFI_ACPI_DESCRIPTION_HEADER **TableList;
  836. UINTN Index;
  837. ASSERT (This != NULL);
  838. ASSERT (AcpiTableInfo != NULL);
  839. ASSERT (CfgMgrProtocol != NULL);
  840. ASSERT (AcpiTableInfo->TableGeneratorId == This->GeneratorID);
  841. ASSERT (AcpiTableInfo->AcpiTableSignature == This->AcpiTableSignature);
  842. if ((Table == NULL) ||
  843. (*Table == NULL) ||
  844. (TableCount == 0))
  845. {
  846. DEBUG ((DEBUG_ERROR, "ERROR: SSDT-PCI: Invalid Table Pointer\n"));
  847. return EFI_INVALID_PARAMETER;
  848. }
  849. TableList = *Table;
  850. for (Index = 0; Index < TableCount; Index++) {
  851. if ((TableList[Index] != NULL) &&
  852. (TableList[Index]->Signature ==
  853. EFI_ACPI_6_3_SECONDARY_SYSTEM_DESCRIPTION_TABLE_SIGNATURE))
  854. {
  855. FreePool (TableList[Index]);
  856. } else {
  857. DEBUG ((
  858. DEBUG_ERROR,
  859. "ERROR: SSDT-PCI: Could not free SSDT table at index %d.",
  860. Index
  861. ));
  862. return EFI_INVALID_PARAMETER;
  863. }
  864. } // for
  865. // Free the table list.
  866. FreePool (*Table);
  867. return EFI_SUCCESS;
  868. }
  869. /** This macro defines the SSDT Pci Table Generator revision.
  870. */
  871. #define SSDT_PCI_GENERATOR_REVISION CREATE_REVISION (1, 0)
  872. /** The interface for the SSDT Pci Table Generator.
  873. */
  874. STATIC
  875. ACPI_PCI_GENERATOR SsdtPcieGenerator = {
  876. // ACPI table generator header
  877. {
  878. // Generator ID
  879. CREATE_STD_ACPI_TABLE_GEN_ID (EStdAcpiTableIdSsdtPciExpress),
  880. // Generator Description
  881. L"ACPI.STD.SSDT.PCI.GENERATOR",
  882. // ACPI Table Signature
  883. EFI_ACPI_6_3_SECONDARY_SYSTEM_DESCRIPTION_TABLE_SIGNATURE,
  884. // ACPI Table Revision - Unused
  885. 0,
  886. // Minimum ACPI Table Revision - Unused
  887. 0,
  888. // Creator ID
  889. TABLE_GENERATOR_CREATOR_ID_ARM,
  890. // Creator Revision
  891. SSDT_PCI_GENERATOR_REVISION,
  892. // Build table function. Use the extended version instead.
  893. NULL,
  894. // Free table function. Use the extended version instead.
  895. NULL,
  896. // Extended Build table function.
  897. BuildSsdtPciTableEx,
  898. // Extended free function.
  899. FreeSsdtPciTableEx
  900. },
  901. // Private fields are defined from here.
  902. // DeviceTable
  903. {
  904. // Table
  905. NULL,
  906. // LastIndex
  907. 0,
  908. // MaxIndex
  909. 0
  910. },
  911. };
  912. /** Register the Generator with the ACPI Table Factory.
  913. @param [in] ImageHandle The handle to the image.
  914. @param [in] SystemTable Pointer to the System Table.
  915. @retval EFI_SUCCESS The Generator is registered.
  916. @retval EFI_INVALID_PARAMETER A parameter is invalid.
  917. @retval EFI_ALREADY_STARTED The Generator for the Table ID
  918. is already registered.
  919. **/
  920. EFI_STATUS
  921. EFIAPI
  922. AcpiSsdtPcieLibConstructor (
  923. IN EFI_HANDLE ImageHandle,
  924. IN EFI_SYSTEM_TABLE *SystemTable
  925. )
  926. {
  927. EFI_STATUS Status;
  928. Status = RegisterAcpiTableGenerator (&SsdtPcieGenerator.Header);
  929. DEBUG ((
  930. DEBUG_INFO,
  931. "SSDT-PCI: Register Generator. Status = %r\n",
  932. Status
  933. ));
  934. ASSERT_EFI_ERROR (Status);
  935. return Status;
  936. }
  937. /** Deregister the Generator from the ACPI Table Factory.
  938. @param [in] ImageHandle The handle to the image.
  939. @param [in] SystemTable Pointer to the System Table.
  940. @retval EFI_SUCCESS The Generator is deregistered.
  941. @retval EFI_INVALID_PARAMETER A parameter is invalid.
  942. @retval EFI_NOT_FOUND The Generator is not registered.
  943. **/
  944. EFI_STATUS
  945. EFIAPI
  946. AcpiSsdtPcieLibDestructor (
  947. IN EFI_HANDLE ImageHandle,
  948. IN EFI_SYSTEM_TABLE *SystemTable
  949. )
  950. {
  951. EFI_STATUS Status;
  952. Status = DeregisterAcpiTableGenerator (&SsdtPcieGenerator.Header);
  953. DEBUG ((
  954. DEBUG_INFO,
  955. "SSDT-PCI: Deregister Generator. Status = %r\n",
  956. Status
  957. ));
  958. ASSERT_EFI_ERROR (Status);
  959. return Status;
  960. }