]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.h
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / XhciDxe / UsbHcMem.h
CommitLineData
1847ed0b
EL
1/** @file\r
2\r
3 This file contains the definination for host controller memory management routines.\r
4\r
d1102dba 5Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>\r
1847ed0b
EL
6This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#ifndef _EFI_XHCI_MEM_H_\r
17#define _EFI_XHCI_MEM_H_\r
18\r
19#define USB_HC_BIT(a) ((UINTN)(1 << (a)))\r
20\r
21#define USB_HC_BIT_IS_SET(Data, Bit) \\r
22 ((BOOLEAN)(((Data) & USB_HC_BIT(Bit)) == USB_HC_BIT(Bit)))\r
23\r
24typedef struct _USBHC_MEM_BLOCK USBHC_MEM_BLOCK;\r
25struct _USBHC_MEM_BLOCK {\r
26 UINT8 *Bits; // Bit array to record which unit is allocated\r
27 UINTN BitsLen;\r
28 UINT8 *Buf;\r
29 UINT8 *BufHost;\r
30 UINTN BufLen; // Memory size in bytes\r
31 VOID *Mapping;\r
32 USBHC_MEM_BLOCK *Next;\r
33};\r
34\r
35//\r
36// USBHC_MEM_POOL is used to manage the memory used by USB\r
37// host controller. XHCI requires the control memory and transfer\r
38// data to be on the same 4G memory.\r
39//\r
40typedef struct _USBHC_MEM_POOL {\r
41 EFI_PCI_IO_PROTOCOL *PciIo;\r
42 BOOLEAN Check4G;\r
43 UINT32 Which4G;\r
44 USBHC_MEM_BLOCK *Head;\r
45} USBHC_MEM_POOL;\r
46\r
47//\r
48// Memory allocation unit, must be 2^n, n>4\r
49//\r
50#define USBHC_MEM_UNIT 64\r
51\r
52#define USBHC_MEM_UNIT_MASK (USBHC_MEM_UNIT - 1)\r
53#define USBHC_MEM_DEFAULT_PAGES 16\r
54\r
55#define USBHC_MEM_ROUND(Len) (((Len) + USBHC_MEM_UNIT_MASK) & (~USBHC_MEM_UNIT_MASK))\r
56\r
57//\r
58// Advance the byte and bit to the next bit, adjust byte accordingly.\r
59//\r
60#define NEXT_BIT(Byte, Bit) \\r
61 do { \\r
62 (Bit)++; \\r
63 if ((Bit) > 7) { \\r
64 (Byte)++; \\r
65 (Bit) = 0; \\r
66 } \\r
67 } while (0)\r
68\r
69\r
70\r
71/**\r
72 Initialize the memory management pool for the host controller.\r
73\r
74 @param PciIo The PciIo that can be used to access the host controller.\r
75\r
76 @retval EFI_SUCCESS The memory pool is initialized.\r
77 @retval EFI_OUT_OF_RESOURCE Fail to init the memory pool.\r
78\r
79**/\r
80USBHC_MEM_POOL *\r
81UsbHcInitMemPool (\r
82 IN EFI_PCI_IO_PROTOCOL *PciIo\r
83 );\r
84\r
85\r
86/**\r
87 Release the memory management pool.\r
88\r
89 @param Pool The USB memory pool to free.\r
90\r
91 @retval EFI_SUCCESS The memory pool is freed.\r
92 @retval EFI_DEVICE_ERROR Failed to free the memory pool.\r
93\r
94**/\r
95EFI_STATUS\r
96UsbHcFreeMemPool (\r
97 IN USBHC_MEM_POOL *Pool\r
98 );\r
99\r
100\r
101/**\r
102 Allocate some memory from the host controller's memory pool\r
103 which can be used to communicate with host controller.\r
104\r
105 @param Pool The host controller's memory pool.\r
106 @param Size Size of the memory to allocate.\r
107\r
108 @return The allocated memory or NULL.\r
109\r
110**/\r
111VOID *\r
112UsbHcAllocateMem (\r
113 IN USBHC_MEM_POOL *Pool,\r
114 IN UINTN Size\r
115 );\r
116\r
117\r
118/**\r
119 Free the allocated memory back to the memory pool.\r
120\r
121 @param Pool The memory pool of the host controller.\r
122 @param Mem The memory to free.\r
123 @param Size The size of the memory to free.\r
124\r
125**/\r
126VOID\r
127UsbHcFreeMem (\r
128 IN USBHC_MEM_POOL *Pool,\r
129 IN VOID *Mem,\r
130 IN UINTN Size\r
131 );\r
132\r
133/**\r
134 Calculate the corresponding pci bus address according to the Mem parameter.\r
135\r
136 @param Pool The memory pool of the host controller.\r
137 @param Mem The pointer to host memory.\r
138 @param Size The size of the memory region.\r
139\r
140 @return The pci memory address\r
141\r
142**/\r
143EFI_PHYSICAL_ADDRESS\r
144UsbHcGetPciAddrForHostAddr (\r
145 IN USBHC_MEM_POOL *Pool,\r
146 IN VOID *Mem,\r
147 IN UINTN Size\r
148 );\r
149\r
150/**\r
151 Calculate the corresponding host address according to the pci address.\r
152\r
153 @param Pool The memory pool of the host controller.\r
154 @param Mem The pointer to pci memory.\r
155 @param Size The size of the memory region.\r
156\r
157 @return The host memory address\r
158\r
159**/\r
160EFI_PHYSICAL_ADDRESS\r
161UsbHcGetHostAddrForPciAddr (\r
162 IN USBHC_MEM_POOL *Pool,\r
163 IN VOID *Mem,\r
164 IN UINTN Size\r
165 );\r
166\r
d1102dba 167/**\r
1847ed0b 168 Allocates pages at a specified alignment that are suitable for an EfiPciIoOperationBusMasterCommonBuffer mapping.\r
d1102dba 169\r
1847ed0b
EL
170 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
171\r
172 @param PciIo The PciIo that can be used to access the host controller.\r
173 @param Pages The number of pages to allocate.\r
174 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
175 @param HostAddress The system memory address to map to the PCI controller.\r
d1102dba 176 @param DeviceAddress The resulting map address for the bus master PCI controller to\r
1847ed0b
EL
177 use to access the hosts HostAddress.\r
178 @param Mapping A resulting value to pass to Unmap().\r
179\r
180 @retval EFI_SUCCESS Success to allocate aligned pages.\r
181 @retval EFI_INVALID_PARAMETER Pages or Alignment is not valid.\r
182 @retval EFI_OUT_OF_RESOURCES Do not have enough resources to allocate memory.\r
d1102dba 183\r
1847ed0b
EL
184\r
185**/\r
186EFI_STATUS\r
187UsbHcAllocateAlignedPages (\r
188 IN EFI_PCI_IO_PROTOCOL *PciIo,\r
189 IN UINTN Pages,\r
190 IN UINTN Alignment,\r
191 OUT VOID **HostAddress,\r
192 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,\r
193 OUT VOID **Mapping\r
194 );\r
d1102dba 195\r
1847ed0b
EL
196/**\r
197 Frees memory that was allocated with UsbHcAllocateAlignedPages().\r
d1102dba 198\r
1847ed0b
EL
199 @param PciIo The PciIo that can be used to access the host controller.\r
200 @param HostAddress The system memory address to map to the PCI controller.\r
201 @param Pages The number of pages to free.\r
202 @param Mapping The mapping value returned from Map().\r
203\r
204**/\r
205VOID\r
206UsbHcFreeAlignedPages (\r
207 IN EFI_PCI_IO_PROTOCOL *PciIo,\r
208 IN VOID *HostAddress,\r
209 IN UINTN Pages,\r
210 VOID *Mapping\r
211 );\r
212\r
213#endif\r