]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Library/LockBoxLib/LockBoxDxe.c
OvmfPkg/Virtio: take RingBaseShift in SetQueueAddress()
[mirror_edk2.git] / OvmfPkg / Library / LockBoxLib / LockBoxDxe.c
CommitLineData
6a7cba79
LE
1/** @file\r
2\r
522e1754 3 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
6a7cba79
LE
4\r
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php.\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <Uefi.h>\r
16\r
17#include <Library/MemoryAllocationLib.h>\r
18#include <Library/UefiBootServicesTableLib.h>\r
19#include <Library/BaseMemoryLib.h>\r
20#include <Library/DebugLib.h>\r
522e1754 21#include <Library/QemuFwCfgLib.h>\r
687f7521 22#include <Library/QemuFwCfgS3Lib.h>\r
522e1754 23#include <Protocol/LockBox.h>\r
6a7cba79
LE
24#include <LockBoxLib.h>\r
25\r
26/**\r
27 Allocate memory below 4G memory address.\r
28\r
29 This function allocates memory below 4G memory address.\r
30\r
31 @param MemoryType Memory type of memory to allocate.\r
32 @param Size Size of memory to allocate.\r
33 \r
34 @return Allocated address for output.\r
35\r
36**/\r
37STATIC\r
38VOID *\r
39AllocateMemoryBelow4G (\r
40 IN EFI_MEMORY_TYPE MemoryType,\r
41 IN UINTN Size\r
42 )\r
43{\r
44 UINTN Pages;\r
45 EFI_PHYSICAL_ADDRESS Address;\r
46 EFI_STATUS Status;\r
47 VOID* Buffer;\r
48 UINTN AllocRemaining;\r
49\r
50 Pages = EFI_SIZE_TO_PAGES (Size);\r
51 Address = 0xffffffff;\r
52\r
53 //\r
54 // Since we need to use gBS->AllocatePages to get a buffer below\r
55 // 4GB, there is a good chance that space will be wasted for very\r
56 // small allocation. We keep track of unused portions of the page\r
57 // allocations, and use these to allocate memory for small buffers.\r
58 //\r
59 ASSERT (mLockBoxGlobal->Signature == LOCK_BOX_GLOBAL_SIGNATURE);\r
60 if ((UINTN) mLockBoxGlobal->SubPageRemaining >= Size) {\r
61 Buffer = (VOID*)(UINTN) mLockBoxGlobal->SubPageBuffer;\r
62 mLockBoxGlobal->SubPageBuffer += (UINT32) Size;\r
63 mLockBoxGlobal->SubPageRemaining -= (UINT32) Size;\r
64 return Buffer;\r
65 }\r
66\r
67 Status = gBS->AllocatePages (\r
68 AllocateMaxAddress,\r
69 MemoryType,\r
70 Pages,\r
71 &Address\r
72 );\r
73 if (EFI_ERROR (Status)) {\r
74 return NULL;\r
75 }\r
76\r
77 Buffer = (VOID *) (UINTN) Address;\r
78 ZeroMem (Buffer, EFI_PAGES_TO_SIZE (Pages));\r
79\r
80 AllocRemaining = EFI_PAGES_TO_SIZE (Pages) - Size;\r
81 if (AllocRemaining > (UINTN) mLockBoxGlobal->SubPageRemaining) {\r
82 mLockBoxGlobal->SubPageBuffer = (UINT32) (Address + Size);\r
83 mLockBoxGlobal->SubPageRemaining = (UINT32) AllocRemaining;\r
84 }\r
85\r
86 return Buffer;\r
87}\r
88\r
89\r
90/**\r
91 Allocates a buffer of type EfiACPIMemoryNVS.\r
92\r
93 Allocates the number bytes specified by AllocationSize of type\r
94 EfiACPIMemoryNVS and returns a pointer to the allocated buffer.\r
95 If AllocationSize is 0, then a valid buffer of 0 size is\r
96 returned. If there is not enough memory remaining to satisfy\r
97 the request, then NULL is returned.\r
98\r
99 @param AllocationSize The number of bytes to allocate.\r
100\r
101 @return A pointer to the allocated buffer or NULL if allocation fails.\r
102\r
103**/\r
104VOID *\r
105EFIAPI\r
106AllocateAcpiNvsPool (\r
107 IN UINTN AllocationSize\r
108 )\r
109{\r
110 return AllocateMemoryBelow4G (EfiACPIMemoryNVS, AllocationSize);\r
111}\r
112\r
113\r
114EFI_STATUS\r
115EFIAPI\r
116LockBoxDxeLibInitialize (\r
117 IN EFI_HANDLE ImageHandle,\r
118 IN EFI_SYSTEM_TABLE *SystemTable\r
119 )\r
120{\r
522e1754
SZ
121 EFI_STATUS Status;\r
122 VOID *Interface;\r
123\r
124 Status = LockBoxLibInitialize ();\r
125 if (!EFI_ERROR (Status)) {\r
126 if (QemuFwCfgS3Enabled ()) {\r
127 //\r
128 // When S3 enabled, the first driver run with this library linked will\r
129 // have this library constructor to install LockBox protocol on the\r
130 // ImageHandle. As other drivers may have gEfiLockBoxProtocolGuid\r
131 // dependency, the first driver should run before them.\r
132 //\r
133 Status = gBS->LocateProtocol (&gEfiLockBoxProtocolGuid, NULL, &Interface);\r
134 if (EFI_ERROR (Status)) {\r
135 Status = gBS->InstallProtocolInterface (\r
136 &ImageHandle,\r
137 &gEfiLockBoxProtocolGuid,\r
138 EFI_NATIVE_INTERFACE,\r
139 NULL\r
140 );\r
141 ASSERT_EFI_ERROR (Status);\r
142 }\r
143 }\r
144 }\r
145\r
146 return Status;\r
6a7cba79 147}\r