]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/PcatSingleSegmentPciCfg2Pei/PciCfg2.c
ae177f487c1e669470d5d9d0213d5d3675c76263
[mirror_edk2.git] / MdeModulePkg / Universal / PcatSingleSegmentPciCfg2Pei / PciCfg2.c
1 /** @file
2 Installs Single Segment Pci Configuration PPI.
3
4 Copyright (c) 2006 - 2007, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include <PiPei.h>
16
17 #include <Ppi/PciCfg2.h>
18
19 #include <Library/BaseLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/PciLib.h>
22 #include <Library/PeimEntryPoint.h>
23
24 #include <IndustryStandard/Pci.h>
25
26 /**
27 Convert EFI_PEI_PCI_CFG_PPI_PCI_ADDRESS to PCI_LIB_ADDRESS.
28
29 @param Address PCI address with
30 EFI_PEI_PCI_CFG_PPI_PCI_ADDRESS format.
31
32 @return The PCI address with PCI_LIB_ADDRESS format.
33
34 **/
35 STATIC
36 UINTN
37 PciCfgAddressConvert (
38 EFI_PEI_PCI_CFG_PPI_PCI_ADDRESS *Address
39 )
40 {
41 if (Address->ExtendedRegister == 0) {
42 return PCI_LIB_ADDRESS (Address->Bus, Address->Device, Address->Function, Address->Register);
43 }
44
45 return PCI_LIB_ADDRESS (Address->Bus, Address->Device, Address->Function, Address->ExtendedRegister);
46 }
47
48 /**
49 Reads from a given location in the PCI configuration space.
50
51 @param PeiServices An indirect pointer to the PEI Services Table published by the PEI Foundation.
52
53 @param This Pointer to local data for the interface.
54
55 @param Width The width of the access. Enumerated in bytes.
56 See EFI_PEI_PCI_CFG_PPI_WIDTH above.
57
58 @param Address The physical address of the access. The format of
59 the address is described by EFI_PEI_PCI_CFG_PPI_PCI_ADDRESS.
60
61 @param Buffer A pointer to the buffer of data..
62
63
64 @retval EFI_SUCCESS The function completed successfully.
65
66 @retval EFI_DEVICE_ERROR There was a problem with the transaction.
67
68 @retval EFI_DEVICE_NOT_READY The device is not capable of supporting the operation at this
69 time.
70
71 **/
72 EFI_STATUS
73 EFIAPI
74 PciCfg2Read (
75 IN CONST EFI_PEI_SERVICES **PeiServices,
76 IN CONST EFI_PEI_PCI_CFG2_PPI *This,
77 IN EFI_PEI_PCI_CFG_PPI_WIDTH Width,
78 IN UINT64 Address,
79 IN OUT VOID *Buffer
80 );
81
82 /**
83 Write to a given location in the PCI configuration space.
84
85 @param PeiServices An indirect pointer to the PEI Services Table published by the PEI Foundation.
86
87 @param This Pointer to local data for the interface.
88
89 @param Width The width of the access. Enumerated in bytes.
90 See EFI_PEI_PCI_CFG_PPI_WIDTH above.
91
92 @param Address The physical address of the access. The format of
93 the address is described by EFI_PEI_PCI_CFG_PPI_PCI_ADDRESS.
94
95 @param Buffer A pointer to the buffer of data..
96
97
98 @retval EFI_SUCCESS The function completed successfully.
99
100 @retval EFI_DEVICE_ERROR There was a problem with the transaction.
101
102 @retval EFI_DEVICE_NOT_READY The device is not capable of supporting the operation at this
103 time.
104
105 **/
106 EFI_STATUS
107 EFIAPI
108 PciCfg2Write (
109 IN CONST EFI_PEI_SERVICES **PeiServices,
110 IN CONST EFI_PEI_PCI_CFG2_PPI *This,
111 IN EFI_PEI_PCI_CFG_PPI_WIDTH Width,
112 IN UINT64 Address,
113 IN OUT VOID *Buffer
114 );
115
116
117 /**
118 PCI read-modify-write operation.
119
120 @param PeiServices An indirect pointer to the PEI Services Table
121 published by the PEI Foundation.
122
123 @param This Pointer to local data for the interface.
124
125 @param Width The width of the access. Enumerated in bytes. Type
126 EFI_PEI_PCI_CFG_PPI_WIDTH is defined in Read().
127
128 @param Address The physical address of the access.
129
130 @param SetBits Points to value to bitwise-OR with the read configuration value.
131
132 The size of the value is determined by Width.
133
134 @param ClearBits Points to the value to negate and bitwise-AND with the read configuration value.
135 The size of the value is determined by Width.
136
137
138 @retval EFI_SUCCESS The function completed successfully.
139
140 @retval EFI_DEVICE_ERROR There was a problem with the transaction.
141
142 @retval EFI_DEVICE_NOT_READY The device is not capable of supporting
143 the operation at this time.
144
145 **/
146 EFI_STATUS
147 EFIAPI
148 PciCfg2Modify (
149 IN CONST EFI_PEI_SERVICES **PeiServices,
150 IN CONST EFI_PEI_PCI_CFG2_PPI *This,
151 IN EFI_PEI_PCI_CFG_PPI_WIDTH Width,
152 IN UINT64 Address,
153 IN VOID *SetBits,
154 IN VOID *ClearBits
155 );
156
157
158
159 /**
160 @par Ppi Description:
161 The EFI_PEI_PCI_CFG2_PPI interfaces are used to abstract
162 accesses to PCI controllers behind a PCI root bridge
163 controller.
164
165 @param Read PCI read services. See the Read() function description.
166
167 @param Write PCI write services. See the Write() function description.
168
169 @param Modify PCI read-modify-write services. See the Modify() function description.
170
171 @param Segment The PCI bus segment which the specified functions will access.
172
173 **/
174 GLOBAL_REMOVE_IF_UNREFERENCED
175 EFI_PEI_PCI_CFG2_PPI gPciCfg2Ppi = {
176 PciCfg2Read,
177 PciCfg2Write,
178 PciCfg2Modify
179 };
180
181 GLOBAL_REMOVE_IF_UNREFERENCED
182 EFI_PEI_PPI_DESCRIPTOR gPciCfg2PpiList = {
183 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
184 &gEfiPciCfg2PpiGuid,
185 &gPciCfg2Ppi
186 };
187
188 /**
189 Reads from a given location in the PCI configuration space.
190
191 @param PeiServices An indirect pointer to the PEI Services Table published by the PEI Foundation.
192
193 @param This Pointer to local data for the interface.
194
195 @param Width The width of the access. Enumerated in bytes.
196 See EFI_PEI_PCI_CFG_PPI_WIDTH above.
197
198 @param Address The physical address of the access. The format of
199 the address is described by EFI_PEI_PCI_CFG_PPI_PCI_ADDRESS.
200
201 @param Buffer A pointer to the buffer of data..
202
203
204 @retval EFI_SUCCESS The function completed successfully.
205
206 @retval EFI_DEVICE_ERROR There was a problem with the transaction.
207
208 @retval EFI_DEVICE_NOT_READY The device is not capable of supporting the operation at this
209 time.
210
211 **/
212 EFI_STATUS
213 EFIAPI
214 PciCfg2Read (
215 IN CONST EFI_PEI_SERVICES **PeiServices,
216 IN CONST EFI_PEI_PCI_CFG2_PPI *This,
217 IN EFI_PEI_PCI_CFG_PPI_WIDTH Width,
218 IN UINT64 Address,
219 IN OUT VOID *Buffer
220 )
221 {
222 UINTN PciLibAddress;
223
224 PciLibAddress = PciCfgAddressConvert ((EFI_PEI_PCI_CFG_PPI_PCI_ADDRESS *) &Address);
225
226 if (Width == EfiPeiPciCfgWidthUint8) {
227 *((UINT8 *) Buffer) = PciRead8 (PciLibAddress);
228 } else if (Width == EfiPeiPciCfgWidthUint16) {
229 *((UINT16 *) Buffer) = PciRead16 (PciLibAddress);
230 } else if (Width == EfiPeiPciCfgWidthUint32) {
231 *((UINT32 *) Buffer) = PciRead32 (PciLibAddress);
232 } else {
233 return EFI_INVALID_PARAMETER;
234 }
235
236 return EFI_SUCCESS;
237 }
238
239 /**
240 Write to a given location in the PCI configuration space.
241
242 @param PeiServices An indirect pointer to the PEI Services Table published by the PEI Foundation.
243
244 @param This Pointer to local data for the interface.
245
246 @param Width The width of the access. Enumerated in bytes.
247 See EFI_PEI_PCI_CFG_PPI_WIDTH above.
248
249 @param Address The physical address of the access. The format of
250 the address is described by EFI_PEI_PCI_CFG_PPI_PCI_ADDRESS.
251
252 @param Buffer A pointer to the buffer of data..
253
254
255 @retval EFI_SUCCESS The function completed successfully.
256
257 @retval EFI_DEVICE_ERROR There was a problem with the transaction.
258
259 @retval EFI_DEVICE_NOT_READY The device is not capable of supporting the operation at this
260 time.
261
262 **/
263 EFI_STATUS
264 EFIAPI
265 PciCfg2Write (
266 IN CONST EFI_PEI_SERVICES **PeiServices,
267 IN CONST EFI_PEI_PCI_CFG2_PPI *This,
268 IN EFI_PEI_PCI_CFG_PPI_WIDTH Width,
269 IN UINT64 Address,
270 IN OUT VOID *Buffer
271 )
272 {
273 UINTN PciLibAddress;
274
275 PciLibAddress = PciCfgAddressConvert ((EFI_PEI_PCI_CFG_PPI_PCI_ADDRESS *) &Address);
276
277 if (Width == EfiPeiPciCfgWidthUint8) {
278 PciWrite8 (PciLibAddress, *((UINT8 *) Buffer));
279 } else if (Width == EfiPeiPciCfgWidthUint16) {
280 PciWrite16 (PciLibAddress, *((UINT16 *) Buffer));
281 } else if (Width == EfiPeiPciCfgWidthUint32) {
282 PciWrite32 (PciLibAddress, *((UINT32 *) Buffer));
283 } else {
284 return EFI_INVALID_PARAMETER;
285 }
286
287 return EFI_SUCCESS;
288 }
289
290
291 /**
292 PCI read-modify-write operation.
293
294 @param PeiServices An indirect pointer to the PEI Services Table
295 published by the PEI Foundation.
296
297 @param This Pointer to local data for the interface.
298
299 @param Width The width of the access. Enumerated in bytes. Type
300 EFI_PEI_PCI_CFG_PPI_WIDTH is defined in Read().
301
302 @param Address The physical address of the access.
303
304 @param SetBits Points to value to bitwise-OR with the read configuration value.
305
306 The size of the value is determined by Width.
307
308 @param ClearBits Points to the value to negate and bitwise-AND with the read configuration value.
309 The size of the value is determined by Width.
310
311
312 @retval EFI_SUCCESS The function completed successfully.
313
314 @retval EFI_DEVICE_ERROR There was a problem with the transaction.
315
316 @retval EFI_DEVICE_NOT_READY The device is not capable of supporting
317 the operation at this time.
318
319 **/
320 EFI_STATUS
321 EFIAPI
322 PciCfg2Modify (
323 IN CONST EFI_PEI_SERVICES **PeiServices,
324 IN CONST EFI_PEI_PCI_CFG2_PPI *This,
325 IN EFI_PEI_PCI_CFG_PPI_WIDTH Width,
326 IN UINT64 Address,
327 IN VOID *SetBits,
328 IN VOID *ClearBits
329 )
330 {
331 UINTN PciLibAddress;
332 UINT16 ClearValue16;
333 UINT16 SetValue16;
334 UINT32 ClearValue32;
335 UINT32 SetValue32;
336
337 PciLibAddress = PciCfgAddressConvert ((EFI_PEI_PCI_CFG_PPI_PCI_ADDRESS *) &Address);
338
339 if (Width == EfiPeiPciCfgWidthUint8) {
340 PciAndThenOr8 (PciLibAddress, (UINT8) (~(*(UINT8 *) ClearBits)), *((UINT8 *) SetBits));
341 } else if (Width == EfiPeiPciCfgWidthUint16) {
342 ClearValue16 = (UINT16) (~ReadUnaligned16 ((UINT16 *) ClearBits));
343 SetValue16 = ReadUnaligned16 ((UINT16 *) SetBits);
344 PciAndThenOr16 (PciLibAddress, ClearValue16, SetValue16);
345 } else if (Width == EfiPeiPciCfgWidthUint32) {
346 ClearValue32 = (UINT32) (~ReadUnaligned32 ((UINT32 *) ClearBits));
347 SetValue32 = ReadUnaligned32 ((UINT32 *) SetBits);
348 PciAndThenOr32 (PciLibAddress, ClearValue32, SetValue32);
349 } else {
350 return EFI_INVALID_PARAMETER;
351 }
352 return EFI_SUCCESS;
353 }
354
355
356 EFI_STATUS
357 EFIAPI
358 PeimInitializePciCfg (
359 IN EFI_FFS_FILE_HEADER *FfsHeader,
360 IN EFI_PEI_SERVICES **PeiServices
361 )
362 {
363 EFI_STATUS Status;
364
365 ASSERT ((**PeiServices).Hdr.Revision >= PEI_SERVICES_REVISION);
366
367 (**PeiServices).PciCfg = &gPciCfg2Ppi;
368 Status = (**PeiServices).InstallPpi ((CONST EFI_PEI_SERVICES **)PeiServices, &gPciCfg2PpiList);
369
370 return Status;
371 }