1 | /* $Id: MemDetect.c 48674 2013-09-25 08:26:15Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * MemDetect.c
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | /**@file
|
---|
28 | Memory Detection for Virtual Machines.
|
---|
29 |
|
---|
30 | Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
|
---|
31 | This program and the accompanying materials
|
---|
32 | are licensed and made available under the terms and conditions of the BSD License
|
---|
33 | which accompanies this distribution. The full text of the license may be found at
|
---|
34 | http://opensource.org/licenses/bsd-license.php
|
---|
35 |
|
---|
36 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
---|
37 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
---|
38 |
|
---|
39 | Module Name:
|
---|
40 |
|
---|
41 | MemDetect.c
|
---|
42 |
|
---|
43 | **/
|
---|
44 |
|
---|
45 | //
|
---|
46 | // The package level header files this module uses
|
---|
47 | //
|
---|
48 | #include <PiPei.h>
|
---|
49 |
|
---|
50 | //
|
---|
51 | // The Library classes this module consumes
|
---|
52 | //
|
---|
53 | #include <Library/DebugLib.h>
|
---|
54 | #include <Library/HobLib.h>
|
---|
55 | #include <Library/IoLib.h>
|
---|
56 | #include <Library/PcdLib.h>
|
---|
57 | #include <Library/PeimEntryPoint.h>
|
---|
58 | #include <Library/ResourcePublicationLib.h>
|
---|
59 | #include <Library/MtrrLib.h>
|
---|
60 |
|
---|
61 | #include "Platform.h"
|
---|
62 | #include "Cmos.h"
|
---|
63 |
|
---|
64 | STATIC
|
---|
65 | UINTN
|
---|
66 | GetSystemMemorySizeBelow4gb (
|
---|
67 | )
|
---|
68 | {
|
---|
69 | UINT8 Cmos0x34;
|
---|
70 | UINT8 Cmos0x35;
|
---|
71 |
|
---|
72 | //
|
---|
73 | // CMOS 0x34/0x35 specifies the system memory above 16 MB.
|
---|
74 | // * CMOS(0x35) is the high byte
|
---|
75 | // * CMOS(0x34) is the low byte
|
---|
76 | // * The size is specified in 64kb chunks
|
---|
77 | // * Since this is memory above 16MB, the 16MB must be added
|
---|
78 | // into the calculation to get the total memory size.
|
---|
79 | //
|
---|
80 |
|
---|
81 | Cmos0x34 = (UINT8) CmosRead8 (0x34);
|
---|
82 | Cmos0x35 = (UINT8) CmosRead8 (0x35);
|
---|
83 |
|
---|
84 | return (((UINTN)((Cmos0x35 << 8) + Cmos0x34) << 16) + SIZE_16MB);
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | STATIC
|
---|
89 | UINT64
|
---|
90 | GetSystemMemorySizeAbove4gb (
|
---|
91 | )
|
---|
92 | {
|
---|
93 | UINT32 Size;
|
---|
94 | UINTN CmosIndex;
|
---|
95 |
|
---|
96 | //
|
---|
97 | // CMOS 0x5b-0x5d specifies the system memory above 4GB MB.
|
---|
98 | // * CMOS(0x5d) is the most significant size byte
|
---|
99 | // * CMOS(0x5c) is the middle size byte
|
---|
100 | // * CMOS(0x5b) is the least significant size byte
|
---|
101 | // * The size is specified in 64kb chunks
|
---|
102 | //
|
---|
103 |
|
---|
104 | Size = 0;
|
---|
105 | for (CmosIndex = 0x5d; CmosIndex >= 0x5b; CmosIndex--) {
|
---|
106 | Size = (UINT32) (Size << 8) + (UINT32) CmosRead8 (CmosIndex);
|
---|
107 | }
|
---|
108 |
|
---|
109 | return LShiftU64 (Size, 16);
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | /**
|
---|
114 | Peform Memory Detection
|
---|
115 |
|
---|
116 | @return EFI_SUCCESS The PEIM initialized successfully.
|
---|
117 |
|
---|
118 | **/
|
---|
119 | EFI_PHYSICAL_ADDRESS
|
---|
120 | MemDetect (
|
---|
121 | )
|
---|
122 | {
|
---|
123 | EFI_STATUS Status;
|
---|
124 | EFI_PHYSICAL_ADDRESS MemoryBase;
|
---|
125 | UINT64 MemorySize;
|
---|
126 | UINT64 LowerMemorySize;
|
---|
127 | UINT64 UpperMemorySize;
|
---|
128 |
|
---|
129 | DEBUG ((EFI_D_ERROR, "MemDetect called\n"));
|
---|
130 |
|
---|
131 | //
|
---|
132 | // Determine total memory size available
|
---|
133 | //
|
---|
134 | LowerMemorySize = GetSystemMemorySizeBelow4gb ();
|
---|
135 | UpperMemorySize = GetSystemMemorySizeAbove4gb ();
|
---|
136 |
|
---|
137 | //
|
---|
138 | // Determine the range of memory to use during PEI
|
---|
139 | //
|
---|
140 | MemoryBase = PcdGet32 (PcdOvmfMemFvBase) + PcdGet32 (PcdOvmfMemFvSize);
|
---|
141 | MemorySize = LowerMemorySize - MemoryBase;
|
---|
142 | if (MemorySize > SIZE_64MB) {
|
---|
143 | MemoryBase = LowerMemorySize - SIZE_64MB;
|
---|
144 | MemorySize = SIZE_64MB;
|
---|
145 | }
|
---|
146 | MemorySize -= BASE_64KB; /* Reserves 64KB for ACPI tables. */
|
---|
147 |
|
---|
148 | //
|
---|
149 | // Publish this memory to the PEI Core
|
---|
150 | //
|
---|
151 | Status = PublishSystemMemory(MemoryBase, MemorySize);
|
---|
152 | ASSERT_EFI_ERROR (Status);
|
---|
153 |
|
---|
154 | //
|
---|
155 | // Create memory HOBs
|
---|
156 | //
|
---|
157 | AddMemoryBaseSizeHob (MemoryBase, MemorySize);
|
---|
158 | AddMemoryRangeHob (BASE_1MB, MemoryBase);
|
---|
159 | MtrrSetMemoryAttribute (BASE_1MB, MemoryBase + MemorySize - BASE_1MB, CacheWriteBack);
|
---|
160 | AddMemoryRangeHob (0, BASE_512KB + BASE_128KB);
|
---|
161 | MtrrSetMemoryAttribute (0, BASE_512KB + BASE_128KB, CacheWriteBack);
|
---|
162 |
|
---|
163 |
|
---|
164 | if (UpperMemorySize != 0) {
|
---|
165 | AddUntestedMemoryBaseSizeHob (BASE_4GB, UpperMemorySize);
|
---|
166 |
|
---|
167 | MtrrSetMemoryAttribute (BASE_4GB, UpperMemorySize, CacheWriteBack);
|
---|
168 | }
|
---|
169 |
|
---|
170 | return MemoryBase + MemorySize;
|
---|
171 | }
|
---|
172 |
|
---|