]> git.proxmox.com Git - mirror_edk2.git/blame - EmbeddedPkg/Library/CoherentDmaLib/CoherentDmaLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / EmbeddedPkg / Library / CoherentDmaLib / CoherentDmaLib.c
CommitLineData
aaeef063 1/** @file\r
2 Generic ARM implementation of DmaLib.h\r
3\r
4 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>\r
3402aac7 5\r
878b807a 6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
aaeef063 7\r
8**/\r
9\r
10#include <Uefi.h>\r
11#include <Library/DebugLib.h>\r
12#include <Library/DmaLib.h>\r
13#include <Library/MemoryAllocationLib.h>\r
14\r
0bcb8010
AB
15STATIC\r
16PHYSICAL_ADDRESS\r
17HostToDeviceAddress (\r
e7108d0e 18 IN VOID *Address\r
0bcb8010
AB
19 )\r
20{\r
21 return (PHYSICAL_ADDRESS)(UINTN)Address + PcdGet64 (PcdDmaDeviceOffset);\r
22}\r
aaeef063 23\r
3402aac7 24/**\r
aaeef063 25 Provides the DMA controller-specific addresses needed to access system memory.\r
3402aac7 26\r
aaeef063 27 Operation is relative to the DMA bus master.\r
3402aac7 28\r
aaeef063 29 @param Operation Indicates if the bus master is going to read or write to system memory.\r
30 @param HostAddress The system memory address to map to the DMA controller.\r
31 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes\r
3402aac7 32 that were mapped.\r
aaeef063 33 @param DeviceAddress The resulting map address for the bus master controller to use to\r
3402aac7 34 access the hosts HostAddress.\r
aaeef063 35 @param Mapping A resulting value to pass to Unmap().\r
3402aac7 36\r
aaeef063 37 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.\r
3402aac7 38 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.\r
aaeef063 39 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
40 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
41 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.\r
3402aac7 42\r
aaeef063 43**/\r
44EFI_STATUS\r
45EFIAPI\r
46DmaMap (\r
e7108d0e
MK
47 IN DMA_MAP_OPERATION Operation,\r
48 IN VOID *HostAddress,\r
49 IN OUT UINTN *NumberOfBytes,\r
50 OUT PHYSICAL_ADDRESS *DeviceAddress,\r
51 OUT VOID **Mapping\r
aaeef063 52 )\r
53{\r
e7108d0e
MK
54 if ((HostAddress == NULL) ||\r
55 (NumberOfBytes == NULL) ||\r
56 (DeviceAddress == NULL) ||\r
57 (Mapping == NULL))\r
58 {\r
c4709260
VO
59 return EFI_INVALID_PARAMETER;\r
60 }\r
e7108d0e 61\r
0bcb8010 62 *DeviceAddress = HostToDeviceAddress (HostAddress);\r
e7108d0e 63 *Mapping = NULL;\r
aaeef063 64 return EFI_SUCCESS;\r
65}\r
66\r
3402aac7 67/**\r
aaeef063 68 Completes the DmaMapBusMasterRead(), DmaMapBusMasterWrite(), or DmaMapBusMasterCommonBuffer()\r
69 operation and releases any corresponding resources.\r
3402aac7 70\r
aaeef063 71 @param Mapping The mapping value returned from DmaMap*().\r
3402aac7 72\r
aaeef063 73 @retval EFI_SUCCESS The range was unmapped.\r
74 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.\r
3402aac7 75\r
aaeef063 76**/\r
77EFI_STATUS\r
78EFIAPI\r
79DmaUnmap (\r
e7108d0e 80 IN VOID *Mapping\r
aaeef063 81 )\r
82{\r
83 return EFI_SUCCESS;\r
84}\r
85\r
3402aac7 86/**\r
aaeef063 87 Allocates pages that are suitable for an DmaMap() of type MapOperationBusMasterCommonBuffer.\r
3402aac7
RC
88 mapping.\r
89\r
aaeef063 90 @param MemoryType The type of memory to allocate, EfiBootServicesData or\r
3402aac7
RC
91 EfiRuntimeServicesData.\r
92 @param Pages The number of pages to allocate.\r
aaeef063 93 @param HostAddress A pointer to store the base system memory address of the\r
3402aac7 94 allocated range.\r
aaeef063 95\r
96 @retval EFI_SUCCESS The requested memory pages were allocated.\r
97 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are\r
3402aac7 98 MEMORY_WRITE_COMBINE and MEMORY_CACHED.\r
aaeef063 99 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
3402aac7
RC
100 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.\r
101\r
aaeef063 102**/\r
103EFI_STATUS\r
104EFIAPI\r
105DmaAllocateBuffer (\r
e7108d0e
MK
106 IN EFI_MEMORY_TYPE MemoryType,\r
107 IN UINTN Pages,\r
108 OUT VOID **HostAddress\r
aaeef063 109 )\r
110{\r
deef290f
AB
111 return DmaAllocateAlignedBuffer (MemoryType, Pages, 0, HostAddress);\r
112}\r
113\r
deef290f
AB
114/**\r
115 Allocates pages that are suitable for an DmaMap() of type\r
116 MapOperationBusMasterCommonBuffer mapping, at the requested alignment.\r
117\r
118 @param MemoryType The type of memory to allocate, EfiBootServicesData or\r
119 EfiRuntimeServicesData.\r
120 @param Pages The number of pages to allocate.\r
121 @param Alignment Alignment in bytes of the base of the returned\r
122 buffer (must be a power of 2)\r
123 @param HostAddress A pointer to store the base system memory address of the\r
124 allocated range.\r
125\r
126 @retval EFI_SUCCESS The requested memory pages were allocated.\r
127 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are\r
128 MEMORY_WRITE_COMBINE and MEMORY_CACHED.\r
129 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
130 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.\r
131\r
132**/\r
133EFI_STATUS\r
134EFIAPI\r
135DmaAllocateAlignedBuffer (\r
e7108d0e
MK
136 IN EFI_MEMORY_TYPE MemoryType,\r
137 IN UINTN Pages,\r
138 IN UINTN Alignment,\r
139 OUT VOID **HostAddress\r
deef290f
AB
140 )\r
141{\r
142 if (Alignment == 0) {\r
143 Alignment = EFI_PAGE_SIZE;\r
144 }\r
145\r
e7108d0e
MK
146 if ((HostAddress == NULL) ||\r
147 ((Alignment & (Alignment - 1)) != 0))\r
148 {\r
aaeef063 149 return EFI_INVALID_PARAMETER;\r
150 }\r
151\r
152 //\r
153 // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData\r
154 //\r
aaeef063 155 if (MemoryType == EfiBootServicesData) {\r
deef290f 156 *HostAddress = AllocateAlignedPages (Pages, Alignment);\r
c783da65 157 } else if (MemoryType == EfiRuntimeServicesData) {\r
deef290f 158 *HostAddress = AllocateAlignedRuntimePages (Pages, Alignment);\r
aaeef063 159 } else {\r
160 return EFI_INVALID_PARAMETER;\r
161 }\r
162\r
deef290f
AB
163 if (*HostAddress == NULL) {\r
164 return EFI_OUT_OF_RESOURCES;\r
165 }\r
e7108d0e 166\r
aaeef063 167 return EFI_SUCCESS;\r
168}\r
169\r
3402aac7 170/**\r
aaeef063 171 Frees memory that was allocated with DmaAllocateBuffer().\r
3402aac7
RC
172\r
173 @param Pages The number of pages to free.\r
174 @param HostAddress The base system memory address of the allocated range.\r
175\r
aaeef063 176 @retval EFI_SUCCESS The requested memory pages were freed.\r
177 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages\r
178 was not allocated with DmaAllocateBuffer().\r
3402aac7 179\r
aaeef063 180**/\r
181EFI_STATUS\r
182EFIAPI\r
183DmaFreeBuffer (\r
e7108d0e
MK
184 IN UINTN Pages,\r
185 IN VOID *HostAddress\r
aaeef063 186 )\r
187{\r
188 if (HostAddress == NULL) {\r
e7108d0e 189 return EFI_INVALID_PARAMETER;\r
3402aac7
RC
190 }\r
191\r
aaeef063 192 FreePages (HostAddress, Pages);\r
193 return EFI_SUCCESS;\r
194}\r