summaryrefslogtreecommitdiff
path: root/Silicon/Intel/PurleySktPkg/Library/CsrToPcieLib/CsrToPcieLib.c
blob: d6b98f2aec7efb81ed12304264f7bd2a4c603a30 (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
/** @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 "CpuCsrAccessDefine.h"
#include "CpuPciAccess.h"

CPU_CSR_ACCESS_VAR  *gCpuCsrAccessVarPtr = NULL;
CPU_CSR_ACCESS_VAR gCpuCsrAccessVar;


UINT32
GetSegmentNumber (
  IN USRA_ADDRESS    *Address
  )
{
  return 0;
};

UINT32
GetBDFNumber (
  IN USRA_ADDRESS    *Address,
  CPU_CSR_ACCESS_VAR          *CpuCsrAccessVar,
  IN UINT8                    BDFType
//  UINT8    SocId,
//  UINT8    BoxType
  )
/*++

Routine Description:
  Indetifies the bus number for given SocId & BoxType

Arguments:
  Address   - A pointer of the address of the USRA Address Structure with Csr or CsrEx type

Returns:
  PCI bus number

--*/
{
  UINT32   Data32 =0 ;
  UINT8    SocId;
  UINT8    BoxType;
  UINT8    BoxInst;
  UINT8    FuncBlk;
  PSYSHOST host;

  SocId = (UINT8)Address->Csr.SocketId;
  BoxType = (UINT8)((CSR_OFFSET *)(&Address->Csr.Offset))->Bits.boxtype;
  BoxInst = (UINT8)Address->Csr.InstId;
  FuncBlk = (UINT8)((CSR_OFFSET *)(&Address->Csr.Offset))->Bits.funcblk;
#ifdef IA32
  host = (PSYSHOST)Address->Attribute.HostPtr;
#else
  host = (PSYSHOST)NULL;
#endif
  if(BDFType == BUS_CLASS){
    Data32 = GetBusNumber(host, SocId, BoxType, BoxInst, FuncBlk, CpuCsrAccessVar);
  } else if(BDFType == DEVICE_CLASS){
    Data32 = GetDeviceNumber(host, BoxType, BoxInst, FuncBlk, CpuCsrAccessVar);
  } else {
    Data32 = GetFunctionNumber(host, BoxType, BoxInst, FuncBlk, CpuCsrAccessVar);
  }
  return Data32;

}

VOID
GetBDF (
  IN USRA_ADDRESS    *Address,
  CPU_CSR_ACCESS_VAR        *CpuCsrAccessVar,
  USRA_PCIE_ADDR_TYPE       *PcieAddress
  )
/*++

Routine Description:
  Indetifies the bus number for given SocId & BoxType

Arguments:
  Address   - A pointer of the address of the USRA Address Structure with Csr or CsrEx type

Returns:
  PCI bus number

--*/
{
  UINT8    SocId;
  UINT8    BoxType;
  UINT8    BoxInst;
  UINT8    FuncBlk;
  PSYSHOST host;

  SocId = (UINT8)Address->Csr.SocketId;
  BoxType = (UINT8)((CSR_OFFSET *)(&Address->Csr.Offset))->Bits.boxtype;
  BoxInst = (UINT8)Address->Csr.InstId;
  FuncBlk = (UINT8)((CSR_OFFSET *)(&Address->Csr.Offset))->Bits.funcblk;
#ifdef IA32
  host = (PSYSHOST)Address->Attribute.HostPtr;
#else
  host = (PSYSHOST)NULL;
#endif
    PcieAddress->Bus = GetBusNumber(host, SocId, BoxType, BoxInst, FuncBlk, CpuCsrAccessVar);
    PcieAddress->Dev = GetDeviceNumber(host, BoxType, BoxInst, FuncBlk, CpuCsrAccessVar);
    PcieAddress->Func = GetFunctionNumber(host, BoxType, BoxInst, FuncBlk, CpuCsrAccessVar);
    PcieAddress->Seg = SocId;  // Refcode and EFI data structure difference.   Refcode treats this array as 1 entry per socket, and not per segment, thus we index by SocId for now..  
}     
//////////////////////////////////////////////////////////////////////////
//
// USRA Silicon Address Library
// This Lib provide the way use platform Library instance
//
//////////////////////////////////////////////////////////////////////////

/**
  This Lib Convert the logical address (CSR type, e.g. CPU ID, Boxtype, Box instance etc.) into physical address
  
  @param[in] Global               Global pointer
  @param[in] Virtual              Virtual address
  @param[in] Address              A pointer of the address of the USRA Address Structure
  @param[out] AlignedAddress      A pointer of aligned address converted from USRA address

  @retval NULL                    The function completed successfully.
  @retval <>NULL                  Return Error
**/
UINTN
EFIAPI
CsrGetPcieAlignAddress (
  IN VOID                     *Global,
  IN BOOLEAN                  Virtual,
  IN USRA_ADDRESS             *Address,
  OUT UINTN                   *AlignedAddress
  )
{
  CPU_CSR_ACCESS_VAR *pCpuCsrAccessVar;
  USRA_ADDRESS                UsraAddress;
  INTN                        MmCfgBase;

  /*********************************************************************************************************
    ToDo:
        For now, this implementation only covers the Bus/Dev/Fun number generation for IVT and HSX CPUs.
        Register offset and size information comes from the HSX style register offset passed to this function.
        When the auto generation of header files using the new format is available, then we need to implement
        the logic to translate the register pseudo offset into real offset.
   *********************************************************************************************************/
    Address->Attribute.AccessWidth = (UINT8) (((((CSR_OFFSET *) &Address->Csr.Offset)->Bits.size) & 0x06) >> 1);

#if defined (IA32) || defined (SIM_BUILD) || defined(KTI_SW_SIMULATION) || defined (HEADLESS_MRC)
  CpuDeadLoop();
  pCpuCsrAccessVar = NULL;

#else
  gCpuCsrAccessVarPtr = &gCpuCsrAccessVar;
  GetCpuCsrAccessVar_RC ((PSYSHOST)host, &gCpuCsrAccessVar);

  pCpuCsrAccessVar = &gCpuCsrAccessVar;
#endif // defined

  //
  // Identify the PCI Bus/Device/Function number for the access
  //
  USRA_ZERO_ADDRESS_TYPE(UsraAddress, AddrTypePCIE);
  GetBDF(Address, pCpuCsrAccessVar, &UsraAddress.Pcie);

  UsraAddress.Pcie.Offset = (UINT16)((CSR_OFFSET *) &Address->Csr.Offset)->Bits.offset;
  UsraAddress.Attribute.HostPtr = Address->Attribute.HostPtr;
  
  MmCfgBase = GetPcieSegMmcfgBaseAddress(&UsraAddress);
  *AlignedAddress = MmCfgBase + (UINTN)(UsraAddress.Attribute.RawData32[0] & 0x0fffffff);

#if defined (IA32) || defined (SIM_BUILD) || defined(KTI_SW_SIMULATION) || defined (HEADLESS_MRC)
  CpuDeadLoop();
#endif

  return 0;
};