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