summaryrefslogtreecommitdiff
path: root/Silicon/Intel/PurleyRcPkg/Library/UsraAccessLib/UsraAccessLib.c
blob: d01d69192fc98153b9e1abdc1b08ec32ee2a63f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/** @file

Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials are licensed and made available under
the terms and conditions of the BSD License that accompanies this distribution.
The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php.

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include "UsraAccessLib.h"

GET_ALLIGNED_ACCESS_ADDRESS mAccessAddrPtr[] =
{
  &GetPcieAccessAddress,     // AddrTypePCIE
  &GetPcieAccessAddress,     // AddrTypePCIEBLK
  &GetCsrAccessAddress,      // AddrTypeCSR
};

REGISTER_READ mRegisterReadPtr[] =
{
  &PcieRegisterRead,        // AddrTypePCIE
  &PcieBlkRegisterRead,     // AddrTypePCIEBLK
  &CsrRegisterRead,         // AddrTypeCSR
};

REGISTER_WRITE mRegisterWritePtr[] =
{
  &PcieRegisterWrite,       // AddrTypePCIE
  &PcieBlkRegisterWrite,    // AddrTypePCIEBLK
  &CsrRegisterWrite,        // AddrTypeCSR
};

REGISTER_MODIFY mRegisterModifyPtr[] =
{
  &PcieRegisterModify,      // AddrTypePCIE
  &PcieRegisterModify,      // AddrTypePCIEBLK
  &CsrRegisterModify,       // AddrTypeCSR
};

/**
  Perform MMIO read 
  
  @param[in] AccessWidth          Access Width
  @param[in] AlignedAddress       An address to be read out
  @param[in] Buffer               A pointer of buffer contains the data to be read out

  @retval RETURN_SUCCESS          The function completed successfully.
**/
RETURN_STATUS
UsraRegAlignedRead (
  IN UINT32                   AccessWidth,
  IN UINTN                    AlignedAddress,
  OUT VOID                    *Buffer
  )
{
  switch (AccessWidth)
  {
  case  UsraWidth8:
    *((UINT8*)Buffer) = MmioRead8 (AlignedAddress);
    break;
  case  UsraWidth16:
    *((UINT16*)Buffer) = MmioRead16 (AlignedAddress);
    break;
  case  UsraWidth32:
    *((UINT32*)Buffer) = MmioRead32 (AlignedAddress);
    break;
  default:
    *((UINT64*)Buffer) = MmioRead64 (AlignedAddress);
    break;
  }

  return RETURN_SUCCESS;
};

/**
  Perform MMIO write 
  
  @param[in] AccessWidth          Access Width
  @param[in] AlignedAddress       An address to be written
  @param[in] Buffer               A pointer of buffer contains the data to be written

  @retval RETURN_SUCCESS          The function completed successfully.
**/
RETURN_STATUS
UsraRegAlignedWrite (
  IN UINT32                   AccessWidth,
  IN UINTN                    AlignedAddress,
  OUT VOID                    *Buffer
  )
{
  switch (AccessWidth)
  {
  case  UsraWidth8:
    MmioWrite8 (AlignedAddress,*((UINT8*)Buffer));
    break;
  case  UsraWidth16:
    MmioWrite16 (AlignedAddress,*((UINT16*)Buffer));
    break;
  case  UsraWidth32:
    MmioWrite32 (AlignedAddress,*((UINT32*)Buffer));
    break;
  default:
    MmioWrite64 (AlignedAddress, *((UINT64*)Buffer));
    break;
  }
  return RETURN_SUCCESS;
}

/**
  Perform AND then OR operations for a input data 
  
  @param[in out] Data             A pointer of the address of the register to be modified
  @param[in] AndBuffer            A pointer of buffer for the value used for AND operation
                                  A NULL pointer means no AND operation. RegisterModify() equivalents to RegisterOr()
  @param[in] OrBuffer             A pointer of buffer for the value used for OR operation
                                  A NULL pointer means no OR operation. RegisterModify() equivalents to RegisterAnd()
  @param[in] NumOfByte            NumOfByte Count of byte data to be performed

  @retval NONE
**/
VOID
DataAndOr (
  IN  UINT64                  *Data,
  IN  VOID                    *AndBuffer,
  IN  VOID                    *OrBuffer,
  IN  UINT8                   NumOfByte
)
{
  union{
    UINT64      QW;
    UINT8       Byte[8];
  } Buffer;
  UINT8                       AndData[8], OrData[8], i;

  Buffer.QW = *Data;
  for(i=0;i<NumOfByte;i++)
  {
    if (AndBuffer == NULL)
      ((UINT8*)AndData)[i] = 0xff;
    else
      AndData[i] = ((UINT8*)AndBuffer)[i];
    if (OrBuffer == NULL)
      ((UINT8*)OrData)[i] = 0;
    else
      OrData[i] = ((UINT8*)OrBuffer)[i];
    Buffer.Byte[i] = (Buffer.Byte[i] & AndData[i]) | OrData[i];
  }
  
  *Data = Buffer.QW;
}

//////////////////////////////////////////////////////////////////////////
//
// USRA Hardware Access Library
//
//////////////////////////////////////////////////////////////////////////

/**
  This API gets the flat address from the given USRA Address.
  
  @param[in] Address              A pointer of the address of the USRA Address Structure to be read out

  @retval                         The flat address
**/
INTN
EFIAPI
GetRegisterAddress (
  IN USRA_ADDRESS            *Address
  )
{
  UINTN                       AlignedAddress;

  mAccessAddrPtr[Address->Attribute.AddrType] (NULL, 0, Address, &AlignedAddress);

  return AlignedAddress;
};

/**
  This API performs 8-bit, 16-bit, 32-bit or 64-bit silicon register read operations. 
  It transfers data from a register into a naturally aligned data buffer.
  
  @param[in] Address              A pointer of the address of the USRA Address Structure to be read out
  @param[in] Buffer               A pointer of buffer for the value read from the register

  @retval RETURN_SUCCESS          The function completed successfully.
**/
RETURN_STATUS
EFIAPI
RegisterRead (
  IN USRA_ADDRESS             *Address,
  IN VOID                     *Buffer
  )
{
  return mRegisterReadPtr[Address->Attribute.AddrType] (Address, Buffer);
};

/**
  This API performs 8-bit, 16-bit, 32-bit or 64-bit silicon register write operations. 
  It transfers data from a naturally aligned data buffer into a silicon register.
  
  @param[in] Address              A pointer of the address of the USRA Address Structure to be written
  @param[in] Buffer               A pointer of buffer for the value write to the register

  @retval RETURN_SUCCESS          The function completed successfully.
**/
RETURN_STATUS
EFIAPI
RegisterWrite (
  IN USRA_ADDRESS             *Address,
  OUT VOID                    *Buffer
  )
{
  return mRegisterWritePtr[Address->Attribute.AddrType] (Address, Buffer);
};

/**
  This API performs 8-bit, 16-bit, 32-bit or 64-bit silicon register AND then OR operations. It read data from a
  register, And it with the AndBuffer, then Or it with the OrBuffer, and write the result back to the register
  
  @param[in] Address              A pointer of the address of the silicon register to be written
  @param[in] AndBuffer            A pointer of buffer for the value used for AND operation
                                  A NULL pointer means no AND operation. RegisterModify() equivalents to RegisterOr()
  @param[in] OrBuffer             A pointer of buffer for the value used for OR operation
                                  A NULL pointer means no OR operation. RegisterModify() equivalents to RegisterAnd()

  @retval RETURN_SUCCESS          The function completed successfully.
**/
RETURN_STATUS
EFIAPI
RegisterModify (
  IN USRA_ADDRESS             *Address,
  IN VOID                     *AndBuffer,
  IN VOID                     *OrBuffer
  )
{
  return mRegisterModifyPtr[Address->Attribute.AddrType] (Address, AndBuffer, OrBuffer);
};