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