summaryrefslogtreecommitdiff
path: root/ReferenceCode/Haswell/SampleCode/Library/SmmIo/SmmPciIo.c
blob: f6fd18f6ee8b2cdc3dcc3791a4f7c1307df46b66 (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
/** @file
  SMM PCI config space I/O access utility implementation file, for Ia32

@copyright
  Copyright (c) 1999 - 2012 Intel Corporation. All rights reserved
  This software and associated documentation (if any) is furnished
  under a license and may only be used or copied in accordance
  with the terms of the license. Except as permitted by such
  license, no part of this software or documentation may be
  reproduced, stored in a retrieval system, or transmitted in any
  form or by any means without the express written consent of
  Intel Corporation.

  This file contains a 'Sample Driver' and is licensed as such
  under the terms of your license agreement with Intel or your
  vendor.  This file may be modified by the user, subject to
  the additional terms of the license agreement
**/
#include "SmmIoLib.h"

STATIC
EFI_STATUS
SmmSingleSegmentPciAccess (
  IN EFI_SMM_CPU_IO_INTERFACE *CpuIo,
  IN BOOLEAN                  IsWrite,
  IN SMM_PCI_IO_WIDTH         Width,
  IN SMM_PCI_IO_ADDRESS       *Address,
  IN OUT VOID                 *Buffer
  );

/**
  Read value from the specified PCI config space register

  @param[in] Width   - The width (8, 16 or 32 bits) of accessed pci config space register
  @param[in] Address - The address of the accessed pci register (bus, dev, func, offset)
  @param[in] Buffer  - The returned value

  @retval EFI_SUCCESS           - All operations successfully
  @retval EFI_INVALID_PARAMETER - Width is not valid or dosn't match register address
  @retval Other error code      - If any error occured when calling libiary functions
**/
EFI_STATUS
SmmPciCfgRead (
  IN SMM_PCI_IO_WIDTH   Width,
  IN SMM_PCI_IO_ADDRESS *Address,
  IN OUT VOID           *Buffer
  )
{
  EFI_SMM_CPU_IO_INTERFACE *SmmCpuIo;

  ASSERT (mSmst);

  SmmCpuIo = &(mSmst->SmmIo);

  return SmmSingleSegmentPciAccess (SmmCpuIo, FALSE, Width, Address, Buffer);
}

/**
  Write value into the specified PCI config space register

  @param[in] Width   - The width (8, 16 or 32 bits) of accessed pci config space register
  @param[in] Address - The address of the accessed pci register (bus, dev, func, offset)
  @param[in] Buffer  - The returned value

  @retval EFI_SUCCESS           - All operations successfully
  @retval EFI_INVALID_PARAMETER - Width is not valid or dosn't match register address
  @retval Other error code      - If any error occured when calling libiary functions
**/
EFI_STATUS
SmmPciCfgWrite (
  IN SMM_PCI_IO_WIDTH   Width,
  IN SMM_PCI_IO_ADDRESS *Address,
  IN OUT VOID           *Buffer
  )
{
  EFI_SMM_CPU_IO_INTERFACE *SmmCpuIo;

  ASSERT (mSmst);

  SmmCpuIo = &(mSmst->SmmIo);

  return SmmSingleSegmentPciAccess (SmmCpuIo, TRUE, Width, Address, Buffer);
}

/**
  Access a PCI config space address, including read and write

  @param[in] CpuIo   - The cpu I/O accessing interface provided by EFI runtime sys table
  @param[in] IsWrite - Indicates whether this operation is a write access or read
  @param[in] Width   - The width (8, 16 or 32 bits) of accessed pci config space register
  @param[in] Address - The address of the accessed pci register (bus, dev, func, offset)
  @param[in] Buffer  - The returned value when this is a reading operation or the data
            to be written when this is a writing one

  @retval EFI_SUCCESS           - All operations successfully
  @retval EFI_INVALID_PARAMETER - Width is not valid or dosn't match register address
  @retval Other error code      - If any error occured when calling libiary functions
**/
STATIC
EFI_STATUS
SmmSingleSegmentPciAccess (
  IN EFI_SMM_CPU_IO_INTERFACE *CpuIo,
  IN BOOLEAN                  IsWrite,
  IN SMM_PCI_IO_WIDTH         Width,
  IN SMM_PCI_IO_ADDRESS       *Address,
  IN OUT VOID                 *Buffer
  )
{
  EFI_STATUS            Status;
  PCI_CONFIG_ACCESS_CF8 PciCf8Data;
  UINT64                PciDataReg;

  ///
  /// PCI Config access are all 32-bit alligned, but by accessing the
  /// CONFIG_DATA_REGISTER (0xcfc) with different widths more cycle types
  /// are possible on PCI.
  ///
  /// To read a byte of PCI config space you load 0xcf8 and
  /// read 0xcfc, 0xcfd, 0xcfe, 0xcff
  ///
  /// The validation of passed in arguments "Address" will be checked in the
  /// CPU IO functions, so we don't check them here
  ///
  if (Width >= SmmPciWidthMaximum) {
    return EFI_INVALID_PARAMETER;
  }

  PciCf8Data.Reg      = Address->Register & 0xfc;
  PciCf8Data.Func     = Address->Function;
  PciCf8Data.Dev      = Address->Device;
  PciCf8Data.Bus      = Address->Bus;
  PciCf8Data.Reserved = 0;
  PciCf8Data.Enable   = 1;

  Status              = CpuIo->Io.Write (CpuIo, SmmPciWidthUint32, 0xcf8, 1, &PciCf8Data);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  PciDataReg = 0xcfc + (Address->Register & 0x03);

  if (IsWrite) {
    ///
    /// This is a Pci write operation, write data into (0xcfc + offset)
    ///
    Status = CpuIo->Io.Write (CpuIo, Width, PciDataReg, 1, Buffer);
    if (EFI_ERROR (Status)) {
      return Status;
    }
  } else {
    ///
    /// This is a Pci Read operation, read returned data from (0xcfc + offset)
    ///
    Status = CpuIo->Io.Read (CpuIo, Width, PciDataReg, 1, Buffer);
    if (EFI_ERROR (Status)) {
      return Status;
    }
  }

  return EFI_SUCCESS;
}