EmbeddedPkg/CoherentDmaLib: Add missing checks to DmaMap
[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
aaeef063 6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include <Uefi.h>\r
17#include <Library/DebugLib.h>\r
18#include <Library/DmaLib.h>\r
19#include <Library/MemoryAllocationLib.h>\r
20\r
21\r
0bcb8010
AB
22STATIC\r
23PHYSICAL_ADDRESS\r
24HostToDeviceAddress (\r
25 IN VOID *Address\r
26 )\r
27{\r
28 return (PHYSICAL_ADDRESS)(UINTN)Address + PcdGet64 (PcdDmaDeviceOffset);\r
29}\r
aaeef063 30\r
3402aac7 31/**\r
aaeef063 32 Provides the DMA controller-specific addresses needed to access system memory.\r
3402aac7 33\r
aaeef063 34 Operation is relative to the DMA bus master.\r
3402aac7 35\r
aaeef063 36 @param Operation Indicates if the bus master is going to read or write to system memory.\r
37 @param HostAddress The system memory address to map to the DMA controller.\r
38 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes\r
3402aac7 39 that were mapped.\r
aaeef063 40 @param DeviceAddress The resulting map address for the bus master controller to use to\r
3402aac7 41 access the hosts HostAddress.\r
aaeef063 42 @param Mapping A resulting value to pass to Unmap().\r
3402aac7 43\r
aaeef063 44 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.\r
3402aac7 45 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.\r
aaeef063 46 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
47 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
48 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.\r
3402aac7 49\r
aaeef063 50**/\r
51EFI_STATUS\r
52EFIAPI\r
53DmaMap (\r
54 IN DMA_MAP_OPERATION Operation,\r
55 IN VOID *HostAddress,\r
56 IN OUT UINTN *NumberOfBytes,\r
57 OUT PHYSICAL_ADDRESS *DeviceAddress,\r
58 OUT VOID **Mapping\r
59 )\r
60{\r
c4709260
VO
61 if (HostAddress == NULL ||\r
62 NumberOfBytes == NULL ||\r
63 DeviceAddress == NULL ||\r
64 Mapping == NULL ) {\r
65 return EFI_INVALID_PARAMETER;\r
66 }\r
0bcb8010 67 *DeviceAddress = HostToDeviceAddress (HostAddress);\r
6be6c2e0 68 *Mapping = NULL;\r
aaeef063 69 return EFI_SUCCESS;\r
70}\r
71\r
72\r
3402aac7 73/**\r
aaeef063 74 Completes the DmaMapBusMasterRead(), DmaMapBusMasterWrite(), or DmaMapBusMasterCommonBuffer()\r
75 operation and releases any corresponding resources.\r
3402aac7 76\r
aaeef063 77 @param Mapping The mapping value returned from DmaMap*().\r
3402aac7 78\r
aaeef063 79 @retval EFI_SUCCESS The range was unmapped.\r
80 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.\r
3402aac7 81\r
aaeef063 82**/\r
83EFI_STATUS\r
84EFIAPI\r
85DmaUnmap (\r
86 IN VOID *Mapping\r
87 )\r
88{\r
89 return EFI_SUCCESS;\r
90}\r
91\r
3402aac7 92/**\r
aaeef063 93 Allocates pages that are suitable for an DmaMap() of type MapOperationBusMasterCommonBuffer.\r
3402aac7
RC
94 mapping.\r
95\r
aaeef063 96 @param MemoryType The type of memory to allocate, EfiBootServicesData or\r
3402aac7
RC
97 EfiRuntimeServicesData.\r
98 @param Pages The number of pages to allocate.\r
aaeef063 99 @param HostAddress A pointer to store the base system memory address of the\r
3402aac7 100 allocated range.\r
aaeef063 101\r
102 @retval EFI_SUCCESS The requested memory pages were allocated.\r
103 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are\r
3402aac7 104 MEMORY_WRITE_COMBINE and MEMORY_CACHED.\r
aaeef063 105 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
3402aac7
RC
106 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.\r
107\r
aaeef063 108**/\r
109EFI_STATUS\r
110EFIAPI\r
111DmaAllocateBuffer (\r
112 IN EFI_MEMORY_TYPE MemoryType,\r
113 IN UINTN Pages,\r
114 OUT VOID **HostAddress\r
115 )\r
116{\r
deef290f
AB
117 return DmaAllocateAlignedBuffer (MemoryType, Pages, 0, HostAddress);\r
118}\r
119\r
120\r
121/**\r
122 Allocates pages that are suitable for an DmaMap() of type\r
123 MapOperationBusMasterCommonBuffer mapping, at the requested alignment.\r
124\r
125 @param MemoryType The type of memory to allocate, EfiBootServicesData or\r
126 EfiRuntimeServicesData.\r
127 @param Pages The number of pages to allocate.\r
128 @param Alignment Alignment in bytes of the base of the returned\r
129 buffer (must be a power of 2)\r
130 @param HostAddress A pointer to store the base system memory address of the\r
131 allocated range.\r
132\r
133 @retval EFI_SUCCESS The requested memory pages were allocated.\r
134 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are\r
135 MEMORY_WRITE_COMBINE and MEMORY_CACHED.\r
136 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
137 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.\r
138\r
139**/\r
140EFI_STATUS\r
141EFIAPI\r
142DmaAllocateAlignedBuffer (\r
143 IN EFI_MEMORY_TYPE MemoryType,\r
144 IN UINTN Pages,\r
145 IN UINTN Alignment,\r
146 OUT VOID **HostAddress\r
147 )\r
148{\r
149 if (Alignment == 0) {\r
150 Alignment = EFI_PAGE_SIZE;\r
151 }\r
152\r
153 if (HostAddress == NULL ||\r
154 (Alignment & (Alignment - 1)) != 0) {\r
aaeef063 155 return EFI_INVALID_PARAMETER;\r
156 }\r
157\r
158 //\r
159 // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData\r
160 //\r
aaeef063 161 if (MemoryType == EfiBootServicesData) {\r
deef290f 162 *HostAddress = AllocateAlignedPages (Pages, Alignment);\r
c783da65 163 } else if (MemoryType == EfiRuntimeServicesData) {\r
deef290f 164 *HostAddress = AllocateAlignedRuntimePages (Pages, Alignment);\r
aaeef063 165 } else {\r
166 return EFI_INVALID_PARAMETER;\r
167 }\r
168\r
deef290f
AB
169 if (*HostAddress == NULL) {\r
170 return EFI_OUT_OF_RESOURCES;\r
171 }\r
aaeef063 172 return EFI_SUCCESS;\r
173}\r
174\r
175\r
3402aac7 176/**\r
aaeef063 177 Frees memory that was allocated with DmaAllocateBuffer().\r
3402aac7
RC
178\r
179 @param Pages The number of pages to free.\r
180 @param HostAddress The base system memory address of the allocated range.\r
181\r
aaeef063 182 @retval EFI_SUCCESS The requested memory pages were freed.\r
183 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages\r
184 was not allocated with DmaAllocateBuffer().\r
3402aac7 185\r
aaeef063 186**/\r
187EFI_STATUS\r
188EFIAPI\r
189DmaFreeBuffer (\r
190 IN UINTN Pages,\r
191 IN VOID *HostAddress\r
192 )\r
193{\r
194 if (HostAddress == NULL) {\r
195 return EFI_INVALID_PARAMETER;\r
3402aac7
RC
196 }\r
197\r
aaeef063 198 FreePages (HostAddress, Pages);\r
199 return EFI_SUCCESS;\r
200}\r
201\r