HiKey960Mem.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /** @file
  2. *
  3. * Copyright (c) 2018, Linaro Limited. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause-Patent
  6. *
  7. **/
  8. #include <Library/ArmPlatformLib.h>
  9. #include <Library/DebugLib.h>
  10. #include <Library/HobLib.h>
  11. #include <Library/PcdLib.h>
  12. #include <Library/IoLib.h>
  13. #include <Library/MemoryAllocationLib.h>
  14. // The total number of descriptors, including the final "end-of-table" descriptor.
  15. #define MAX_VIRTUAL_MEMORY_MAP_DESCRIPTORS 12
  16. // DDR attributes
  17. #define DDR_ATTRIBUTES_CACHED ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK
  18. #define DDR_ATTRIBUTES_UNCACHED ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED
  19. #define HI3660_PERIPH_BASE 0xE0000000
  20. #define HI3660_PERIPH_SZ 0x20000000
  21. #define HIKEY960_EXTRA_SYSTEM_MEMORY_BASE 0x0000000100000000
  22. #define HIKEY960_EXTRA_SYSTEM_MEMORY_SIZE 0x0000000020000000
  23. #define HIKEY960_MEMORY_SIZE 0x0000000100000000
  24. STATIC struct HiKey960ReservedMemory {
  25. EFI_PHYSICAL_ADDRESS Offset;
  26. EFI_PHYSICAL_ADDRESS Size;
  27. } HiKey960ReservedMemoryBuffer [] = {
  28. { 0x1AC00000, 0x00098000 }, // ARM-TF reserved
  29. { 0x32000000, 0x00100000 }, // PSTORE/RAMOOPS
  30. { 0x32100000, 0x00001000 }, // ADB REBOOT "REASON"
  31. { 0x3E000000, 0x02000000 }, // TEE OS
  32. { 0x89B80000, 0x00100000 }, // MCU Code reserved
  33. { 0x89C80000, 0x00040000 } // MCU reserved
  34. };
  35. /**
  36. Return the Virtual Memory Map of your platform
  37. This Virtual Memory Map is used by MemoryInitPei Module to initialize the MMU on your platform.
  38. @param[out] VirtualMemoryMap Array of ARM_MEMORY_REGION_DESCRIPTOR describing a Physical-to-
  39. Virtual Memory mapping. This array must be ended by a zero-filled
  40. entry
  41. **/
  42. VOID
  43. ArmPlatformGetVirtualMemoryMap (
  44. IN ARM_MEMORY_REGION_DESCRIPTOR** VirtualMemoryMap
  45. )
  46. {
  47. ARM_MEMORY_REGION_ATTRIBUTES CacheAttributes;
  48. ARM_MEMORY_REGION_DESCRIPTOR *VirtualMemoryTable;
  49. EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttributes;
  50. UINTN Index = 0, Count, ReservedTop;
  51. EFI_PEI_HOB_POINTERS NextHob;
  52. UINT64 ResourceLength;
  53. EFI_PHYSICAL_ADDRESS ResourceTop;
  54. ResourceAttributes = (
  55. EFI_RESOURCE_ATTRIBUTE_PRESENT |
  56. EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
  57. EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
  58. EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
  59. EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |
  60. EFI_RESOURCE_ATTRIBUTE_TESTED
  61. );
  62. // Create initial Base Hob for system memory.
  63. BuildResourceDescriptorHob (
  64. EFI_RESOURCE_SYSTEM_MEMORY,
  65. ResourceAttributes,
  66. PcdGet64 (PcdSystemMemoryBase),
  67. PcdGet64 (PcdSystemMemorySize)
  68. );
  69. NextHob.Raw = GetHobList ();
  70. Count = sizeof (HiKey960ReservedMemoryBuffer) / sizeof (struct HiKey960ReservedMemory);
  71. while ((NextHob.Raw = GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, NextHob.Raw)) != NULL)
  72. {
  73. if (Index >= Count)
  74. break;
  75. if ((NextHob.ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) &&
  76. (HiKey960ReservedMemoryBuffer[Index].Offset >= NextHob.ResourceDescriptor->PhysicalStart) &&
  77. ((HiKey960ReservedMemoryBuffer[Index].Offset + HiKey960ReservedMemoryBuffer[Index].Size) <=
  78. NextHob.ResourceDescriptor->PhysicalStart + NextHob.ResourceDescriptor->ResourceLength))
  79. {
  80. ResourceAttributes = NextHob.ResourceDescriptor->ResourceAttribute;
  81. ResourceLength = NextHob.ResourceDescriptor->ResourceLength;
  82. ResourceTop = NextHob.ResourceDescriptor->PhysicalStart + ResourceLength;
  83. ReservedTop = HiKey960ReservedMemoryBuffer[Index].Offset + HiKey960ReservedMemoryBuffer[Index].Size;
  84. // Create the System Memory HOB for the reserved buffer
  85. BuildResourceDescriptorHob (
  86. EFI_RESOURCE_MEMORY_RESERVED,
  87. EFI_RESOURCE_ATTRIBUTE_PRESENT,
  88. HiKey960ReservedMemoryBuffer[Index].Offset,
  89. HiKey960ReservedMemoryBuffer[Index].Size
  90. );
  91. // Update the HOB
  92. NextHob.ResourceDescriptor->ResourceLength = HiKey960ReservedMemoryBuffer[Index].Offset -
  93. NextHob.ResourceDescriptor->PhysicalStart;
  94. // If there is some memory available on the top of the reserved memory then create a HOB
  95. if (ReservedTop < ResourceTop)
  96. {
  97. BuildResourceDescriptorHob (EFI_RESOURCE_SYSTEM_MEMORY,
  98. ResourceAttributes,
  99. ReservedTop,
  100. ResourceTop - ReservedTop);
  101. }
  102. Index++;
  103. }
  104. NextHob.Raw = GET_NEXT_HOB (NextHob);
  105. }
  106. ASSERT (VirtualMemoryMap != NULL);
  107. VirtualMemoryTable = (ARM_MEMORY_REGION_DESCRIPTOR*)AllocatePages (
  108. EFI_SIZE_TO_PAGES (sizeof(ARM_MEMORY_REGION_DESCRIPTOR) * MAX_VIRTUAL_MEMORY_MAP_DESCRIPTORS)
  109. );
  110. if (VirtualMemoryTable == NULL) {
  111. return;
  112. }
  113. CacheAttributes = DDR_ATTRIBUTES_CACHED;
  114. Index = 0;
  115. // DDR - 3.0GB section
  116. VirtualMemoryTable[Index].PhysicalBase = PcdGet64 (PcdSystemMemoryBase);
  117. VirtualMemoryTable[Index].VirtualBase = PcdGet64 (PcdSystemMemoryBase);
  118. VirtualMemoryTable[Index].Length = PcdGet64 (PcdSystemMemorySize);
  119. VirtualMemoryTable[Index].Attributes = CacheAttributes;
  120. // Hi3660 SOC peripherals
  121. VirtualMemoryTable[++Index].PhysicalBase = HI3660_PERIPH_BASE;
  122. VirtualMemoryTable[Index].VirtualBase = HI3660_PERIPH_BASE;
  123. VirtualMemoryTable[Index].Length = HI3660_PERIPH_SZ;
  124. VirtualMemoryTable[Index].Attributes = ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;
  125. // End of Table
  126. VirtualMemoryTable[++Index].PhysicalBase = 0;
  127. VirtualMemoryTable[Index].VirtualBase = 0;
  128. VirtualMemoryTable[Index].Length = 0;
  129. VirtualMemoryTable[Index].Attributes = (ARM_MEMORY_REGION_ATTRIBUTES)0;
  130. ASSERT((Index + 1) <= MAX_VIRTUAL_MEMORY_MAP_DESCRIPTORS);
  131. *VirtualMemoryMap = VirtualMemoryTable;
  132. }