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