]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Pci/XhciPei/UsbHcMem.h
MdeModulePkg/PciBusDxe: Fix small memory leak in FreePciDevice
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / XhciPei / UsbHcMem.h
1 /** @file
2 Private Header file for Usb Host Controller PEIM
3
4 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
5
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions
8 of the BSD License which accompanies this distribution. The
9 full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #ifndef _EFI_PEI_XHCI_MEM_H_
18 #define _EFI_PEI_XHCI_MEM_H_
19
20 #include <Uefi.h>
21
22 #define USBHC_MEM_DEFAULT_PAGES 16
23
24 typedef struct _USBHC_MEM_BLOCK USBHC_MEM_BLOCK;
25
26 struct _USBHC_MEM_BLOCK {
27 UINT8 *Bits; // Bit array to record which unit is allocated
28 UINTN BitsLen;
29 UINT8 *Buf;
30 UINT8 *BufHost;
31 UINTN BufLen; // Memory size in bytes
32 VOID *Mapping;
33 USBHC_MEM_BLOCK *Next;
34 };
35
36 //
37 // Memory allocation unit, must be 2^n, n>4
38 //
39 #define USBHC_MEM_UNIT 64
40
41 #define USBHC_MEM_UNIT_MASK (USBHC_MEM_UNIT - 1)
42 #define USBHC_MEM_ROUND(Len) (((Len) + USBHC_MEM_UNIT_MASK) & (~USBHC_MEM_UNIT_MASK))
43
44 #define USB_HC_BIT(a) ((UINTN)(1 << (a)))
45
46 #define USB_HC_BIT_IS_SET(Data, Bit) \
47 ((BOOLEAN)(((Data) & USB_HC_BIT(Bit)) == USB_HC_BIT(Bit)))
48
49 //
50 // Advance the byte and bit to the next bit, adjust byte accordingly.
51 //
52 #define NEXT_BIT(Byte, Bit) \
53 do { \
54 (Bit)++; \
55 if ((Bit) > 7) { \
56 (Byte)++; \
57 (Bit) = 0; \
58 } \
59 } while (0)
60
61 //
62 // USBHC_MEM_POOL is used to manage the memory used by USB
63 // host controller. XHCI requires the control memory and transfer
64 // data to be on the same 4G memory.
65 //
66 typedef struct _USBHC_MEM_POOL {
67 BOOLEAN Check4G;
68 UINT32 Which4G;
69 USBHC_MEM_BLOCK *Head;
70 } USBHC_MEM_POOL;
71
72 /**
73 Calculate the corresponding pci bus address according to the Mem parameter.
74
75 @param Pool The memory pool of the host controller.
76 @param Mem The pointer to host memory.
77 @param Size The size of the memory region.
78
79 @return The pci memory address
80
81 **/
82 EFI_PHYSICAL_ADDRESS
83 UsbHcGetPciAddrForHostAddr (
84 IN USBHC_MEM_POOL *Pool,
85 IN VOID *Mem,
86 IN UINTN Size
87 );
88
89 /**
90 Calculate the corresponding host address according to the pci address.
91
92 @param Pool The memory pool of the host controller.
93 @param Mem The pointer to pci memory.
94 @param Size The size of the memory region.
95
96 @return The host memory address
97
98 **/
99 EFI_PHYSICAL_ADDRESS
100 UsbHcGetHostAddrForPciAddr (
101 IN USBHC_MEM_POOL *Pool,
102 IN VOID *Mem,
103 IN UINTN Size
104 );
105
106 /**
107 Allocates pages at a specified alignment.
108
109 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
110
111 @param Pages The number of pages to allocate.
112 @param Alignment The requested alignment of the allocation. Must be a power of two.
113 @param HostAddress The system memory address to map to the PCI controller.
114 @param DeviceAddress The resulting map address for the bus master PCI controller to
115 use to access the hosts HostAddress.
116 @param Mapping A resulting value to pass to Unmap().
117
118 @retval EFI_SUCCESS Success to allocate aligned pages.
119 @retval EFI_INVALID_PARAMETER Pages or Alignment is not valid.
120 @retval EFI_OUT_OF_RESOURCES Do not have enough resources to allocate memory.
121
122 **/
123 EFI_STATUS
124 UsbHcAllocateAlignedPages (
125 IN UINTN Pages,
126 IN UINTN Alignment,
127 OUT VOID **HostAddress,
128 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,
129 OUT VOID **Mapping
130 );
131
132 /**
133 Frees memory that was allocated with UsbHcAllocateAlignedPages().
134
135 @param HostAddress The system memory address to map to the PCI controller.
136 @param Pages The number of pages to free.
137 @param Mapping The mapping value returned from Map().
138
139 **/
140 VOID
141 UsbHcFreeAlignedPages (
142 IN VOID *HostAddress,
143 IN UINTN Pages,
144 IN VOID *Mapping
145 );
146
147 #endif