1 /** @file 2 This file contains the definination for host controller memory 3 management routines. 4 5 Copyright (c) 2013-2015 Intel Corporation. 6 7 This program and the accompanying materials 8 are licensed and made available under the terms and conditions of the BSD License 9 which accompanies this distribution. The 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 _USB_HC_MEM_H_ 18 #define _USB_HC_MEM_H_ 19 20 #define USB_HC_BIT(a) ((UINTN)(1 << (a))) 21 22 #define USB_HC_BIT_IS_SET(Data, Bit) \ 23 ((BOOLEAN)(((Data) & USB_HC_BIT(Bit)) == USB_HC_BIT(Bit))) 24 25 #define USB_HC_HIGH_32BIT(Addr64) \ 26 ((UINT32)(RShiftU64((UINTN)(Addr64), 32) & 0XFFFFFFFF)) 27 28 typedef struct _USBHC_MEM_BLOCK USBHC_MEM_BLOCK; 29 struct _USBHC_MEM_BLOCK { 30 UINT8 *Bits; // Bit array to record which unit is allocated 31 UINTN BitsLen; 32 UINT8 *Buf; 33 UINT8 *BufHost; 34 UINTN BufLen; // Memory size in bytes 35 VOID *Mapping; 36 USBHC_MEM_BLOCK *Next; 37 }; 38 39 // 40 // USBHC_MEM_POOL is used to manage the memory used by USB 41 // host controller. EHCI requires the control memory and transfer 42 // data to be on the same 4G memory. 43 // 44 typedef struct _USBHC_MEM_POOL { 45 BOOLEAN Check4G; 46 UINT32 Which4G; 47 USBHC_MEM_BLOCK *Head; 48 } USBHC_MEM_POOL; 49 50 // 51 // Memory allocation unit, must be 2^n, n>4 52 // 53 #define USBHC_MEM_UNIT 64 54 55 #define USBHC_MEM_UNIT_MASK (USBHC_MEM_UNIT - 1) 56 #define USBHC_MEM_DEFAULT_PAGES 16 57 58 #define USBHC_MEM_ROUND(Len) (((Len) + USBHC_MEM_UNIT_MASK) & (~USBHC_MEM_UNIT_MASK)) 59 60 // 61 // Advance the byte and bit to the next bit, adjust byte accordingly. 62 // 63 #define NEXT_BIT(Byte, Bit) \ 64 do { \ 65 (Bit)++; \ 66 if ((Bit) > 7) { \ 67 (Byte)++; \ 68 (Bit) = 0; \ 69 } \ 70 } while (0) 71 72 73 74 /** 75 Initialize the memory management pool for the host controller. 76 77 @param PciIo The PciIo that can be used to access the host controller. 78 @param Check4G Whether the host controller requires allocated memory 79 from one 4G address space. 80 @param Which4G The 4G memory area each memory allocated should be from. 81 82 @retval EFI_SUCCESS The memory pool is initialized. 83 @retval EFI_OUT_OF_RESOURCE Fail to init the memory pool. 84 85 **/ 86 USBHC_MEM_POOL * 87 UsbHcInitMemPool ( 88 IN BOOLEAN Check4G, 89 IN UINT32 Which4G 90 ); 91 92 93 /** 94 Release the memory management pool. 95 96 @param Pool The USB memory pool to free. 97 98 @retval EFI_SUCCESS The memory pool is freed. 99 @retval EFI_DEVICE_ERROR Failed to free the memory pool. 100 101 **/ 102 EFI_STATUS 103 UsbHcFreeMemPool ( 104 IN USBHC_MEM_POOL *Pool 105 ); 106 107 108 /** 109 Allocate some memory from the host controller's memory pool 110 which can be used to communicate with host controller. 111 112 @param Pool The host controller's memory pool. 113 @param Size Size of the memory to allocate. 114 115 @return The allocated memory or NULL. 116 117 **/ 118 VOID * 119 UsbHcAllocateMem ( 120 IN USBHC_MEM_POOL *Pool, 121 IN UINTN Size 122 ); 123 124 125 /** 126 Free the allocated memory back to the memory pool. 127 128 @param Pool The memory pool of the host controller. 129 @param Mem The memory to free. 130 @param Size The size of the memory to free. 131 132 **/ 133 VOID 134 UsbHcFreeMem ( 135 IN USBHC_MEM_POOL *Pool, 136 IN VOID *Mem, 137 IN UINTN Size 138 ); 139 140 #endif 141