]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/PlatformPei/MemDetect.c
78b43d22b75f5bb2c641f6ad86eaf2a4f25802b1
[mirror_edk2.git] / OvmfPkg / PlatformPei / MemDetect.c
1 /**@file
2 Memory Detection for Virtual Machines.
3
4 Copyright (c) 2006 - 2009, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 Module Name:
14
15 MemDetect.c
16
17 **/
18
19 //
20 // The package level header files this module uses
21 //
22 #include <PiPei.h>
23
24 //
25 // The Library classes this module consumes
26 //
27 #include <Library/DebugLib.h>
28 #include <Library/HobLib.h>
29 #include <Library/IoLib.h>
30 #include <Library/PcdLib.h>
31 #include <Library/PeimEntryPoint.h>
32 #include <Library/ResourcePublicationLib.h>
33
34 #include "Platform.h"
35 #include "Cmos.h"
36
37 STATIC
38 UINTN
39 GetSystemMemorySizeBelow4gb (
40 )
41 {
42 UINT8 Cmos0x34;
43 UINT8 Cmos0x35;
44
45 //
46 // CMOS 0x34/0x35 specifies the system memory above 16 MB.
47 // * CMOS(0x35) is the high byte
48 // * CMOS(0x34) is the low byte
49 // * The size is specified in 64kb chunks
50 // * Since this is memory above 16MB, the 16MB must be added
51 // into the calculation to get the total memory size.
52 //
53
54 Cmos0x34 = (UINT8) CmosRead8 (0x34);
55 Cmos0x35 = (UINT8) CmosRead8 (0x35);
56
57 return (((UINTN)((Cmos0x35 << 8) + Cmos0x34) << 16) + SIZE_16MB);
58 }
59
60
61 STATIC
62 UINT64
63 GetSystemMemorySizeAbove4gb (
64 )
65 {
66 UINT32 Size;
67 UINTN CmosIndex;
68
69 //
70 // CMOS 0x5b-0x5d specifies the system memory above 4GB MB.
71 // * CMOS(0x5d) is the most significant size byte
72 // * CMOS(0x5c) is the middle size byte
73 // * CMOS(0x5b) is the least significant size byte
74 // * The size is specified in 64kb chunks
75 //
76
77 Size = 0;
78 for (CmosIndex = 0x5d; CmosIndex >= 0x5b; CmosIndex--) {
79 Size = (UINT32) (Size << 8) + (UINT32) CmosRead8 (CmosIndex);
80 }
81
82 return LShiftU64 (Size, 16);
83 }
84
85
86 /**
87 Peform Memory Detection
88
89 @return EFI_SUCCESS The PEIM initialized successfully.
90
91 **/
92 EFI_PHYSICAL_ADDRESS
93 MemDetect (
94 )
95 {
96 EFI_STATUS Status;
97 EFI_PHYSICAL_ADDRESS MemoryBase;
98 UINT64 MemorySize;
99 UINT64 LowerMemorySize;
100 UINT64 UpperMemorySize;
101
102 DEBUG ((EFI_D_ERROR, "MemDetect called\n"));
103
104 //
105 // Determine total memory size available
106 //
107 LowerMemorySize = GetSystemMemorySizeBelow4gb ();
108 UpperMemorySize = GetSystemMemorySizeAbove4gb ();
109
110 //
111 // Determine the range of memory to use during PEI
112 //
113 MemoryBase = PcdGet32 (PcdOvmfMemFvBase) + PcdGet32 (PcdOvmfMemFvSize);
114 MemorySize = LowerMemorySize - MemoryBase;
115 if (MemorySize > SIZE_64MB) {
116 MemoryBase = LowerMemorySize - SIZE_64MB;
117 MemorySize = SIZE_64MB;
118 }
119
120 //
121 // Publish this memory to the PEI Core
122 //
123 Status = PublishSystemMemory(MemoryBase, MemorySize);
124 ASSERT_EFI_ERROR (Status);
125
126 //
127 // Create memory HOBs
128 //
129 AddMemoryBaseSizeHob (MemoryBase, MemorySize);
130 AddMemoryRangeHob (BASE_1MB, MemoryBase);
131 AddMemoryRangeHob (0, BASE_512KB + BASE_128KB);
132
133 if (UpperMemorySize != 0) {
134 AddUntestedMemoryBaseSizeHob (BASE_4GB, UpperMemorySize);
135 }
136
137 return MemoryBase + MemorySize;
138 }
139