LegacyRegion.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /** @file
  2. Legacy Region Support
  3. Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "LegacyRegion.h"
  7. //
  8. // 440/Q35 PAM map.
  9. //
  10. // PAM Range Offset Bits Operation
  11. // 440 Q35
  12. // =============== ==== ==== ==== ===============================================================
  13. // 0xC0000-0xC3FFF 0x5a 0x91 1:0 00 = DRAM Disabled, 01= Read Only, 10 = Write Only, 11 = Normal
  14. // 0xC4000-0xC7FFF 0x5a 0x91 5:4 00 = DRAM Disabled, 01= Read Only, 10 = Write Only, 11 = Normal
  15. // 0xC8000-0xCBFFF 0x5b 0x92 1:0 00 = DRAM Disabled, 01= Read Only, 10 = Write Only, 11 = Normal
  16. // 0xCC000-0xCFFFF 0x5b 0x92 5:4 00 = DRAM Disabled, 01= Read Only, 10 = Write Only, 11 = Normal
  17. // 0xD0000-0xD3FFF 0x5c 0x93 1:0 00 = DRAM Disabled, 01= Read Only, 10 = Write Only, 11 = Normal
  18. // 0xD4000-0xD7FFF 0x5c 0x93 5:4 00 = DRAM Disabled, 01= Read Only, 10 = Write Only, 11 = Normal
  19. // 0xD8000-0xDBFFF 0x5d 0x94 1:0 00 = DRAM Disabled, 01= Read Only, 10 = Write Only, 11 = Normal
  20. // 0xDC000-0xDFFFF 0x5d 0x94 5:4 00 = DRAM Disabled, 01= Read Only, 10 = Write Only, 11 = Normal
  21. // 0xE0000-0xE3FFF 0x5e 0x95 1:0 00 = DRAM Disabled, 01= Read Only, 10 = Write Only, 11 = Normal
  22. // 0xE4000-0xE7FFF 0x5e 0x95 5:4 00 = DRAM Disabled, 01= Read Only, 10 = Write Only, 11 = Normal
  23. // 0xE8000-0xEBFFF 0x5f 0x96 1:0 00 = DRAM Disabled, 01= Read Only, 10 = Write Only, 11 = Normal
  24. // 0xEC000-0xEFFFF 0x5f 0x96 5:4 00 = DRAM Disabled, 01= Read Only, 10 = Write Only, 11 = Normal
  25. // 0xF0000-0xFFFFF 0x59 0x90 5:4 00 = DRAM Disabled, 01= Read Only, 10 = Write Only, 11 = Normal
  26. //
  27. STATIC LEGACY_MEMORY_SECTION_INFO mSectionArray[] = {
  28. { 0xC0000, SIZE_16KB, FALSE, FALSE },
  29. { 0xC4000, SIZE_16KB, FALSE, FALSE },
  30. { 0xC8000, SIZE_16KB, FALSE, FALSE },
  31. { 0xCC000, SIZE_16KB, FALSE, FALSE },
  32. { 0xD0000, SIZE_16KB, FALSE, FALSE },
  33. { 0xD4000, SIZE_16KB, FALSE, FALSE },
  34. { 0xD8000, SIZE_16KB, FALSE, FALSE },
  35. { 0xDC000, SIZE_16KB, FALSE, FALSE },
  36. { 0xE0000, SIZE_16KB, FALSE, FALSE },
  37. { 0xE4000, SIZE_16KB, FALSE, FALSE },
  38. { 0xE8000, SIZE_16KB, FALSE, FALSE },
  39. { 0xEC000, SIZE_16KB, FALSE, FALSE },
  40. { 0xF0000, SIZE_64KB, FALSE, FALSE }
  41. };
  42. STATIC PAM_REGISTER_VALUE mRegisterValues440[] = {
  43. { PMC_REGISTER_PIIX4 (PIIX4_PAM1), 0x01, 0x02 },
  44. { PMC_REGISTER_PIIX4 (PIIX4_PAM1), 0x10, 0x20 },
  45. { PMC_REGISTER_PIIX4 (PIIX4_PAM2), 0x01, 0x02 },
  46. { PMC_REGISTER_PIIX4 (PIIX4_PAM2), 0x10, 0x20 },
  47. { PMC_REGISTER_PIIX4 (PIIX4_PAM3), 0x01, 0x02 },
  48. { PMC_REGISTER_PIIX4 (PIIX4_PAM3), 0x10, 0x20 },
  49. { PMC_REGISTER_PIIX4 (PIIX4_PAM4), 0x01, 0x02 },
  50. { PMC_REGISTER_PIIX4 (PIIX4_PAM4), 0x10, 0x20 },
  51. { PMC_REGISTER_PIIX4 (PIIX4_PAM5), 0x01, 0x02 },
  52. { PMC_REGISTER_PIIX4 (PIIX4_PAM5), 0x10, 0x20 },
  53. { PMC_REGISTER_PIIX4 (PIIX4_PAM6), 0x01, 0x02 },
  54. { PMC_REGISTER_PIIX4 (PIIX4_PAM6), 0x10, 0x20 },
  55. { PMC_REGISTER_PIIX4 (PIIX4_PAM0), 0x10, 0x20 }
  56. };
  57. STATIC PAM_REGISTER_VALUE mRegisterValuesQ35[] = {
  58. { DRAMC_REGISTER_Q35 (MCH_PAM1), 0x01, 0x02 },
  59. { DRAMC_REGISTER_Q35 (MCH_PAM1), 0x10, 0x20 },
  60. { DRAMC_REGISTER_Q35 (MCH_PAM2), 0x01, 0x02 },
  61. { DRAMC_REGISTER_Q35 (MCH_PAM2), 0x10, 0x20 },
  62. { DRAMC_REGISTER_Q35 (MCH_PAM3), 0x01, 0x02 },
  63. { DRAMC_REGISTER_Q35 (MCH_PAM3), 0x10, 0x20 },
  64. { DRAMC_REGISTER_Q35 (MCH_PAM4), 0x01, 0x02 },
  65. { DRAMC_REGISTER_Q35 (MCH_PAM4), 0x10, 0x20 },
  66. { DRAMC_REGISTER_Q35 (MCH_PAM5), 0x01, 0x02 },
  67. { DRAMC_REGISTER_Q35 (MCH_PAM5), 0x10, 0x20 },
  68. { DRAMC_REGISTER_Q35 (MCH_PAM6), 0x01, 0x02 },
  69. { DRAMC_REGISTER_Q35 (MCH_PAM6), 0x10, 0x20 },
  70. { DRAMC_REGISTER_Q35 (MCH_PAM0), 0x10, 0x20 }
  71. };
  72. STATIC PAM_REGISTER_VALUE *mRegisterValues;
  73. //
  74. // Handle used to install the Legacy Region Protocol
  75. //
  76. STATIC EFI_HANDLE mHandle = NULL;
  77. //
  78. // Instance of the Legacy Region Protocol to install into the handle database
  79. //
  80. STATIC EFI_LEGACY_REGION2_PROTOCOL mLegacyRegion2 = {
  81. LegacyRegion2Decode,
  82. LegacyRegion2Lock,
  83. LegacyRegion2BootLock,
  84. LegacyRegion2Unlock,
  85. LegacyRegionGetInfo
  86. };
  87. STATIC
  88. EFI_STATUS
  89. LegacyRegionManipulationInternal (
  90. IN UINT32 Start,
  91. IN UINT32 Length,
  92. IN BOOLEAN *ReadEnable,
  93. IN BOOLEAN *WriteEnable,
  94. OUT UINT32 *Granularity
  95. )
  96. {
  97. UINT32 EndAddress;
  98. UINTN Index;
  99. UINTN StartIndex;
  100. //
  101. // Validate input parameters.
  102. //
  103. if ((Length == 0) || (Granularity == NULL)) {
  104. return EFI_INVALID_PARAMETER;
  105. }
  106. EndAddress = Start + Length - 1;
  107. if ((Start < PAM_BASE_ADDRESS) || (EndAddress > PAM_LIMIT_ADDRESS)) {
  108. return EFI_INVALID_PARAMETER;
  109. }
  110. //
  111. // Loop to find the start PAM.
  112. //
  113. StartIndex = 0;
  114. for (Index = 0; Index < ARRAY_SIZE (mSectionArray); Index++) {
  115. if ((Start >= mSectionArray[Index].Start) && (Start < (mSectionArray[Index].Start + mSectionArray[Index].Length))) {
  116. StartIndex = Index;
  117. break;
  118. }
  119. }
  120. ASSERT (Index < ARRAY_SIZE (mSectionArray));
  121. //
  122. // Program PAM until end PAM is encountered
  123. //
  124. for (Index = StartIndex; Index < ARRAY_SIZE (mSectionArray); Index++) {
  125. if (ReadEnable != NULL) {
  126. if (*ReadEnable) {
  127. PciOr8 (
  128. mRegisterValues[Index].PAMRegPciLibAddress,
  129. mRegisterValues[Index].ReadEnableData
  130. );
  131. } else {
  132. PciAnd8 (
  133. mRegisterValues[Index].PAMRegPciLibAddress,
  134. (UINT8)(~mRegisterValues[Index].ReadEnableData)
  135. );
  136. }
  137. }
  138. if (WriteEnable != NULL) {
  139. if (*WriteEnable) {
  140. PciOr8 (
  141. mRegisterValues[Index].PAMRegPciLibAddress,
  142. mRegisterValues[Index].WriteEnableData
  143. );
  144. } else {
  145. PciAnd8 (
  146. mRegisterValues[Index].PAMRegPciLibAddress,
  147. (UINT8)(~mRegisterValues[Index].WriteEnableData)
  148. );
  149. }
  150. }
  151. //
  152. // If the end PAM is encountered, record its length as granularity and jump out.
  153. //
  154. if ((EndAddress >= mSectionArray[Index].Start) && (EndAddress < (mSectionArray[Index].Start + mSectionArray[Index].Length))) {
  155. *Granularity = mSectionArray[Index].Length;
  156. break;
  157. }
  158. }
  159. ASSERT (Index < ARRAY_SIZE (mSectionArray));
  160. return EFI_SUCCESS;
  161. }
  162. STATIC
  163. EFI_STATUS
  164. LegacyRegionGetInfoInternal (
  165. OUT UINT32 *DescriptorCount,
  166. OUT LEGACY_MEMORY_SECTION_INFO **Descriptor
  167. )
  168. {
  169. UINTN Index;
  170. UINT8 PamValue;
  171. //
  172. // Check input parameters
  173. //
  174. if ((DescriptorCount == NULL) || (Descriptor == NULL)) {
  175. return EFI_INVALID_PARAMETER;
  176. }
  177. //
  178. // Fill in current status of legacy region.
  179. //
  180. *DescriptorCount = sizeof (mSectionArray) / sizeof (mSectionArray[0]);
  181. for (Index = 0; Index < *DescriptorCount; Index++) {
  182. PamValue = PciRead8 (mRegisterValues[Index].PAMRegPciLibAddress);
  183. mSectionArray[Index].ReadEnabled = FALSE;
  184. if ((PamValue & mRegisterValues[Index].ReadEnableData) != 0) {
  185. mSectionArray[Index].ReadEnabled = TRUE;
  186. }
  187. mSectionArray[Index].WriteEnabled = FALSE;
  188. if ((PamValue & mRegisterValues[Index].WriteEnableData) != 0) {
  189. mSectionArray[Index].WriteEnabled = TRUE;
  190. }
  191. }
  192. *Descriptor = mSectionArray;
  193. return EFI_SUCCESS;
  194. }
  195. /**
  196. Modify the hardware to allow (decode) or disallow (not decode) memory reads in a region.
  197. If the On parameter evaluates to TRUE, this function enables memory reads in the address range
  198. Start to (Start + Length - 1).
  199. If the On parameter evaluates to FALSE, this function disables memory reads in the address range
  200. Start to (Start + Length - 1).
  201. @param This[in] Indicates the EFI_LEGACY_REGION_PROTOCOL instance.
  202. @param Start[in] The beginning of the physical address of the region whose attributes
  203. should be modified.
  204. @param Length[in] The number of bytes of memory whose attributes should be modified.
  205. The actual number of bytes modified may be greater than the number
  206. specified.
  207. @param Granularity[out] The number of bytes in the last region affected. This may be less
  208. than the total number of bytes affected if the starting address
  209. was not aligned to a region's starting address or if the length
  210. was greater than the number of bytes in the first region.
  211. @param On[in] Decode / Non-Decode flag.
  212. @retval EFI_SUCCESS The region's attributes were successfully modified.
  213. @retval EFI_INVALID_PARAMETER If Start or Length describe an address not in the Legacy Region.
  214. **/
  215. EFI_STATUS
  216. EFIAPI
  217. LegacyRegion2Decode (
  218. IN EFI_LEGACY_REGION2_PROTOCOL *This,
  219. IN UINT32 Start,
  220. IN UINT32 Length,
  221. OUT UINT32 *Granularity,
  222. IN BOOLEAN *On
  223. )
  224. {
  225. return LegacyRegionManipulationInternal (Start, Length, On, NULL, Granularity);
  226. }
  227. /**
  228. Modify the hardware to disallow memory attribute changes in a region.
  229. This function makes the attributes of a region read only. Once a region is boot-locked with this
  230. function, the read and write attributes of that region cannot be changed until a power cycle has
  231. reset the boot-lock attribute. Calls to Decode(), Lock() and Unlock() will have no effect.
  232. @param This[in] Indicates the EFI_LEGACY_REGION_PROTOCOL instance.
  233. @param Start[in] The beginning of the physical address of the region whose
  234. attributes should be modified.
  235. @param Length[in] The number of bytes of memory whose attributes should be modified.
  236. The actual number of bytes modified may be greater than the number
  237. specified.
  238. @param Granularity[out] The number of bytes in the last region affected. This may be less
  239. than the total number of bytes affected if the starting address was
  240. not aligned to a region's starting address or if the length was
  241. greater than the number of bytes in the first region.
  242. @retval EFI_SUCCESS The region's attributes were successfully modified.
  243. @retval EFI_INVALID_PARAMETER If Start or Length describe an address not in the Legacy Region.
  244. @retval EFI_UNSUPPORTED The chipset does not support locking the configuration registers in
  245. a way that will not affect memory regions outside the legacy memory
  246. region.
  247. **/
  248. EFI_STATUS
  249. EFIAPI
  250. LegacyRegion2BootLock (
  251. IN EFI_LEGACY_REGION2_PROTOCOL *This,
  252. IN UINT32 Start,
  253. IN UINT32 Length,
  254. OUT UINT32 *Granularity
  255. )
  256. {
  257. if ((Start < 0xC0000) || ((Start + Length - 1) > 0xFFFFF)) {
  258. return EFI_INVALID_PARAMETER;
  259. }
  260. return EFI_UNSUPPORTED;
  261. }
  262. /**
  263. Modify the hardware to disallow memory writes in a region.
  264. This function changes the attributes of a memory range to not allow writes.
  265. @param This[in] Indicates the EFI_LEGACY_REGION_PROTOCOL instance.
  266. @param Start[in] The beginning of the physical address of the region whose
  267. attributes should be modified.
  268. @param Length[in] The number of bytes of memory whose attributes should be modified.
  269. The actual number of bytes modified may be greater than the number
  270. specified.
  271. @param Granularity[out] The number of bytes in the last region affected. This may be less
  272. than the total number of bytes affected if the starting address was
  273. not aligned to a region's starting address or if the length was
  274. greater than the number of bytes in the first region.
  275. @retval EFI_SUCCESS The region's attributes were successfully modified.
  276. @retval EFI_INVALID_PARAMETER If Start or Length describe an address not in the Legacy Region.
  277. **/
  278. EFI_STATUS
  279. EFIAPI
  280. LegacyRegion2Lock (
  281. IN EFI_LEGACY_REGION2_PROTOCOL *This,
  282. IN UINT32 Start,
  283. IN UINT32 Length,
  284. OUT UINT32 *Granularity
  285. )
  286. {
  287. BOOLEAN WriteEnable;
  288. WriteEnable = FALSE;
  289. return LegacyRegionManipulationInternal (Start, Length, NULL, &WriteEnable, Granularity);
  290. }
  291. /**
  292. Modify the hardware to allow memory writes in a region.
  293. This function changes the attributes of a memory range to allow writes.
  294. @param This[in] Indicates the EFI_LEGACY_REGION_PROTOCOL instance.
  295. @param Start[in] The beginning of the physical address of the region whose
  296. attributes should be modified.
  297. @param Length[in] The number of bytes of memory whose attributes should be modified.
  298. The actual number of bytes modified may be greater than the number
  299. specified.
  300. @param Granularity[out] The number of bytes in the last region affected. This may be less
  301. than the total number of bytes affected if the starting address was
  302. not aligned to a region's starting address or if the length was
  303. greater than the number of bytes in the first region.
  304. @retval EFI_SUCCESS The region's attributes were successfully modified.
  305. @retval EFI_INVALID_PARAMETER If Start or Length describe an address not in the Legacy Region.
  306. **/
  307. EFI_STATUS
  308. EFIAPI
  309. LegacyRegion2Unlock (
  310. IN EFI_LEGACY_REGION2_PROTOCOL *This,
  311. IN UINT32 Start,
  312. IN UINT32 Length,
  313. OUT UINT32 *Granularity
  314. )
  315. {
  316. BOOLEAN WriteEnable;
  317. WriteEnable = TRUE;
  318. return LegacyRegionManipulationInternal (Start, Length, NULL, &WriteEnable, Granularity);
  319. }
  320. /**
  321. Get region information for the attributes of the Legacy Region.
  322. This function is used to discover the granularity of the attributes for the memory in the legacy
  323. region. Each attribute may have a different granularity and the granularity may not be the same
  324. for all memory ranges in the legacy region.
  325. @param This[in] Indicates the EFI_LEGACY_REGION_PROTOCOL instance.
  326. @param DescriptorCount[out] The number of region descriptor entries returned in the Descriptor
  327. buffer.
  328. @param Descriptor[out] A pointer to a pointer used to return a buffer where the legacy
  329. region information is deposited. This buffer will contain a list of
  330. DescriptorCount number of region descriptors. This function will
  331. provide the memory for the buffer.
  332. @retval EFI_SUCCESS The region's attributes were successfully modified.
  333. @retval EFI_INVALID_PARAMETER If Start or Length describe an address not in the Legacy Region.
  334. **/
  335. EFI_STATUS
  336. EFIAPI
  337. LegacyRegionGetInfo (
  338. IN EFI_LEGACY_REGION2_PROTOCOL *This,
  339. OUT UINT32 *DescriptorCount,
  340. OUT EFI_LEGACY_REGION_DESCRIPTOR **Descriptor
  341. )
  342. {
  343. LEGACY_MEMORY_SECTION_INFO *SectionInfo;
  344. UINT32 SectionCount;
  345. EFI_LEGACY_REGION_DESCRIPTOR *DescriptorArray;
  346. UINTN Index;
  347. UINTN DescriptorIndex;
  348. //
  349. // Get section numbers and information
  350. //
  351. LegacyRegionGetInfoInternal (&SectionCount, &SectionInfo);
  352. //
  353. // Each section has 3 descriptors, corresponding to readability, writeability, and lock status.
  354. //
  355. DescriptorArray = AllocatePool (sizeof (EFI_LEGACY_REGION_DESCRIPTOR) * SectionCount * 3);
  356. if (DescriptorArray == NULL) {
  357. return EFI_OUT_OF_RESOURCES;
  358. }
  359. DescriptorIndex = 0;
  360. for (Index = 0; Index < SectionCount; Index++) {
  361. DescriptorArray[DescriptorIndex].Start = SectionInfo[Index].Start;
  362. DescriptorArray[DescriptorIndex].Length = SectionInfo[Index].Length;
  363. DescriptorArray[DescriptorIndex].Granularity = SectionInfo[Index].Length;
  364. if (SectionInfo[Index].ReadEnabled) {
  365. DescriptorArray[DescriptorIndex].Attribute = LegacyRegionDecoded;
  366. } else {
  367. DescriptorArray[DescriptorIndex].Attribute = LegacyRegionNotDecoded;
  368. }
  369. DescriptorIndex++;
  370. //
  371. // Create descriptor for writeability, according to lock status
  372. //
  373. DescriptorArray[DescriptorIndex].Start = SectionInfo[Index].Start;
  374. DescriptorArray[DescriptorIndex].Length = SectionInfo[Index].Length;
  375. DescriptorArray[DescriptorIndex].Granularity = SectionInfo[Index].Length;
  376. if (SectionInfo[Index].WriteEnabled) {
  377. DescriptorArray[DescriptorIndex].Attribute = LegacyRegionWriteEnabled;
  378. } else {
  379. DescriptorArray[DescriptorIndex].Attribute = LegacyRegionWriteDisabled;
  380. }
  381. DescriptorIndex++;
  382. //
  383. // Chipset does not support bootlock.
  384. //
  385. DescriptorArray[DescriptorIndex].Start = SectionInfo[Index].Start;
  386. DescriptorArray[DescriptorIndex].Length = SectionInfo[Index].Length;
  387. DescriptorArray[DescriptorIndex].Granularity = SectionInfo[Index].Length;
  388. DescriptorArray[DescriptorIndex].Attribute = LegacyRegionNotLocked;
  389. DescriptorIndex++;
  390. }
  391. *DescriptorCount = (UINT32)DescriptorIndex;
  392. *Descriptor = DescriptorArray;
  393. return EFI_SUCCESS;
  394. }
  395. /**
  396. Initialize Legacy Region support
  397. @retval EFI_SUCCESS Successfully initialized
  398. **/
  399. EFI_STATUS
  400. LegacyRegionInit (
  401. VOID
  402. )
  403. {
  404. EFI_STATUS Status;
  405. UINT16 HostBridgeDevId;
  406. //
  407. // Query Host Bridge DID to determine platform type
  408. //
  409. HostBridgeDevId = PcdGet16 (PcdOvmfHostBridgePciDevId);
  410. switch (HostBridgeDevId) {
  411. case INTEL_82441_DEVICE_ID:
  412. mRegisterValues = mRegisterValues440;
  413. break;
  414. case INTEL_Q35_MCH_DEVICE_ID:
  415. mRegisterValues = mRegisterValuesQ35;
  416. break;
  417. default:
  418. DEBUG ((
  419. DEBUG_ERROR,
  420. "%a: Unknown Host Bridge Device ID: 0x%04x\n",
  421. __FUNCTION__,
  422. HostBridgeDevId
  423. ));
  424. ASSERT (FALSE);
  425. return RETURN_UNSUPPORTED;
  426. }
  427. //
  428. // Install the Legacy Region Protocol on a new handle
  429. //
  430. Status = gBS->InstallMultipleProtocolInterfaces (
  431. &mHandle,
  432. &gEfiLegacyRegion2ProtocolGuid,
  433. &mLegacyRegion2,
  434. NULL
  435. );
  436. ASSERT_EFI_ERROR (Status);
  437. return Status;
  438. }