hc
2024-03-26 e0728245c89800c2038c23308f2d88969d5b41c8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/** @file
  Library of memory map for Phytium platform.
 
  Copyright (C) 2020, Phytium Technology Co Ltd. All rights reserved.<BR>
 
  SPDX-License-Identifier: BSD-2-Clause-Patent
 
**/
 
#include <Library/ArmPlatformLib.h>
#include <Library/DebugLib.h>
#include <Library/HobLib.h>
#include <Library/PcdLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/ArmSmcLib.h>
#include <SystemServiceInterface.h>
 
// Number of Virtual Memory Map Descriptors
#define MAX_VIRTUAL_MEMORY_MAP_DESCRIPTORS 32
 
// DDR attributes
#define DDR_ATTRIBUTES_CACHED      ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK
#define DDR_ATTRIBUTES_UNCACHED    ARM_MEMORY_REGION_ATTRIBUTE_UNCACHED_UNBUFFERED
 
/**
  Return the Virtual Memory Map of your platform
 
  This Virtual Memory Map is used by MemoryInitPei Module to initialize the MMU on your platform.
 
  @param[out]  VirtualMemoryMap  Array of ARM_MEMORY_REGION_DESCRIPTOR describing a Physical-to-
                                 Virtual Memory mapping. This array must be ended by a zero-filled
                                 entry
**/
VOID
ArmPlatformGetVirtualMemoryMap (
  IN ARM_MEMORY_REGION_DESCRIPTOR** VirtualMemoryMap
  )
{
  ARM_MEMORY_REGION_ATTRIBUTES  CacheAttributes;
  ARM_MEMORY_REGION_DESCRIPTOR  *VirtualMemoryTable;
  EFI_RESOURCE_ATTRIBUTE_TYPE   ResourceAttributes;
  PHYTIUM_MEMORY_BLOCK          *MemBlock;
  PHYTIUM_MEMORY_INFO           *MemInfo;
  ARM_SMC_ARGS                  ArmSmcArgs;
  UINT32                        MemBlockCnt;
  UINT32                        Index1;
  UINT32                        Index2;
 
  MemBlock = NULL;
  MemInfo  = NULL;
  MemBlockCnt = 0;
  Index1      = 0;
  Index2      = 0;
  CacheAttributes = ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK;
 
  ASSERT (VirtualMemoryMap != NULL);
  VirtualMemoryTable = (ARM_MEMORY_REGION_DESCRIPTOR*)AllocatePages \
                         (EFI_SIZE_TO_PAGES (sizeof (ARM_MEMORY_REGION_DESCRIPTOR) * \
                         MAX_VIRTUAL_MEMORY_MAP_DESCRIPTORS));
  if (VirtualMemoryTable == NULL) {
    return;
  }
 
  ResourceAttributes =
    EFI_RESOURCE_ATTRIBUTE_PRESENT |
    EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
    EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |
    EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
    EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
    EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |
    EFI_RESOURCE_ATTRIBUTE_TESTED;
 
  MemInfo = AllocatePages (1);
  ASSERT (MemInfo != NULL);
 
  ArmSmcArgs.Arg0 = PHYTIUM_OEM_SVC_MEM_REGIONS;
  ArmSmcArgs.Arg1 = (UINTN) MemInfo;
  ArmSmcArgs.Arg2 = EFI_PAGE_SIZE;
  ArmCallSmc (&ArmSmcArgs);
  if (ArmSmcArgs.Arg0 == 0) {
    MemBlockCnt = MemInfo->MemBlockCount;
    MemBlock = MemInfo->MemBlock;
  } else {
    ASSERT (FALSE);
  }
 
  //Soc Io Space
  VirtualMemoryTable[Index1].PhysicalBase   = PcdGet64 (PcdSystemIoBase);
  VirtualMemoryTable[Index1].VirtualBase    = PcdGet64 (PcdSystemIoBase);
  VirtualMemoryTable[Index1].Length         = PcdGet64 (PcdSystemIoSize);
  VirtualMemoryTable[Index1].Attributes     = ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;
 
  //
  // PCI Configuration Space
  //
  VirtualMemoryTable[++Index1].PhysicalBase  = PcdGet64 (PcdPciConfigBase);
  VirtualMemoryTable[Index1].VirtualBase     = PcdGet64 (PcdPciConfigBase);
  VirtualMemoryTable[Index1].Length          = PcdGet64 (PcdPciConfigSize);
  VirtualMemoryTable[Index1].Attributes      = ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;
 
  //
  // PCI Memory Space
  //
  VirtualMemoryTable[++Index1].PhysicalBase  = PcdGet64 (PcdPciIoBase) + PcdGet64 (PcdPciIoTranslation);
  VirtualMemoryTable[Index1].VirtualBase     = PcdGet64 (PcdPciIoBase) + PcdGet64 (PcdPciIoTranslation);
  VirtualMemoryTable[Index1].Length          = PcdGet64 (PcdPciIoSize);
  VirtualMemoryTable[Index1].Attributes      = ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;
 
  //
  // PCI Memory Space
  //
  VirtualMemoryTable[++Index1].PhysicalBase  = PcdGet32 (PcdPciMmio32Base);
  VirtualMemoryTable[Index1].VirtualBase     = PcdGet32 (PcdPciMmio32Base);
  VirtualMemoryTable[Index1].Length          = PcdGet32 (PcdPciMmio32Size);
  VirtualMemoryTable[Index1].Attributes      = ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;
 
  //
  // 64-bit PCI Memory Space
  //
  VirtualMemoryTable[++Index1].PhysicalBase  = PcdGet64 (PcdPciMmio64Base);
  VirtualMemoryTable[Index1].VirtualBase     = PcdGet64 (PcdPciMmio64Base);
  VirtualMemoryTable[Index1].Length          = PcdGet64 (PcdPciMmio64Size);
  VirtualMemoryTable[Index1].Attributes      = ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;
 
  //DDR
  for (Index2 = 0; Index2 < MemBlockCnt; Index2++) {
    VirtualMemoryTable[++Index1].PhysicalBase = MemBlock->MemStart;
    VirtualMemoryTable[Index1].VirtualBase    = MemBlock->MemStart;
    VirtualMemoryTable[Index1].Length         = MemBlock->MemSize;
    VirtualMemoryTable[Index1].Attributes     = CacheAttributes;
 
    BuildResourceDescriptorHob (
      EFI_RESOURCE_SYSTEM_MEMORY,
      ResourceAttributes,
      MemBlock->MemStart,
      MemBlock->MemSize
      );
 
    MemBlock++;
  }
 
  // End of Table
  VirtualMemoryTable[++Index1].PhysicalBase = 0;
  VirtualMemoryTable[Index1].VirtualBase    = 0;
  VirtualMemoryTable[Index1].Length         = 0;
  VirtualMemoryTable[Index1].Attributes     = (ARM_MEMORY_REGION_ATTRIBUTES)0;
 
 
  for (Index2 = 0; Index2 < Index1; Index2++) {
    DEBUG ((DEBUG_ERROR, "PhysicalBase %12lx VirtualBase %12lx Length %12lx Attributes %12lx\n",\
      VirtualMemoryTable[Index2].PhysicalBase, VirtualMemoryTable[Index2].VirtualBase, \
      VirtualMemoryTable[Index2].Length, VirtualMemoryTable[Index2].Attributes));
  }
 
  *VirtualMemoryMap = VirtualMemoryTable;
}