]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - EmbeddedPkg/Library/CoherentDmaLib/CoherentDmaLib.c
EmbeddedPkg/CoherentDmaLib: Add missing checks to DmaMap
[mirror_edk2.git] / EmbeddedPkg / Library / CoherentDmaLib / CoherentDmaLib.c
... / ...
CommitLineData
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
5\r
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
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
30\r
31/**\r
32 Provides the DMA controller-specific addresses needed to access system memory.\r
33\r
34 Operation is relative to the DMA bus master.\r
35\r
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
39 that were mapped.\r
40 @param DeviceAddress The resulting map address for the bus master controller to use to\r
41 access the hosts HostAddress.\r
42 @param Mapping A resulting value to pass to Unmap().\r
43\r
44 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.\r
45 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.\r
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
49\r
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
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
67 *DeviceAddress = HostToDeviceAddress (HostAddress);\r
68 *Mapping = NULL;\r
69 return EFI_SUCCESS;\r
70}\r
71\r
72\r
73/**\r
74 Completes the DmaMapBusMasterRead(), DmaMapBusMasterWrite(), or DmaMapBusMasterCommonBuffer()\r
75 operation and releases any corresponding resources.\r
76\r
77 @param Mapping The mapping value returned from DmaMap*().\r
78\r
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
81\r
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
92/**\r
93 Allocates pages that are suitable for an DmaMap() of type MapOperationBusMasterCommonBuffer.\r
94 mapping.\r
95\r
96 @param MemoryType The type of memory to allocate, EfiBootServicesData or\r
97 EfiRuntimeServicesData.\r
98 @param Pages The number of pages to allocate.\r
99 @param HostAddress A pointer to store the base system memory address of the\r
100 allocated range.\r
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
104 MEMORY_WRITE_COMBINE and MEMORY_CACHED.\r
105 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
106 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.\r
107\r
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
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
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
161 if (MemoryType == EfiBootServicesData) {\r
162 *HostAddress = AllocateAlignedPages (Pages, Alignment);\r
163 } else if (MemoryType == EfiRuntimeServicesData) {\r
164 *HostAddress = AllocateAlignedRuntimePages (Pages, Alignment);\r
165 } else {\r
166 return EFI_INVALID_PARAMETER;\r
167 }\r
168\r
169 if (*HostAddress == NULL) {\r
170 return EFI_OUT_OF_RESOURCES;\r
171 }\r
172 return EFI_SUCCESS;\r
173}\r
174\r
175\r
176/**\r
177 Frees memory that was allocated with DmaAllocateBuffer().\r
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
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
185\r
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
196 }\r
197\r
198 FreePages (HostAddress, Pages);\r
199 return EFI_SUCCESS;\r
200}\r
201\r