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