summaryrefslogtreecommitdiff
path: root/OvmfPkg/AcpiPlatformDxe/PciDecoding.c
blob: d63b57e701358143e153039118837f51db121e57 (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
/** @file
  Temporarily enable IO and MMIO decoding for all PCI devices while QEMU
  regenerates the ACPI tables.

  Copyright (C) 2016, Red Hat, Inc.

  This program and the accompanying materials are licensed and made available
  under the terms and conditions of the BSD License which 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 <Library/MemoryAllocationLib.h>

#include "AcpiPlatform.h"


/**
  Collect all PciIo protocol instances in the system. Save their original
  attributes, and enable IO and MMIO decoding for each.

  This is a best effort function; it doesn't return status codes. Its
  caller is supposed to proceed even if this function fails.

  @param[out] OriginalAttributes  On output, a dynamically allocated array of
                                  ORIGINAL_ATTRIBUTES elements. The array lists
                                  the PciIo protocol instances found in the
                                  system at the time of the call, plus the
                                  original PCI attributes for each.

                                  Before returning, the function enables IO and
                                  MMIO decoding for each PciIo instance it
                                  finds.

                                  On error, or when no such instances are
                                  found, OriginalAttributes is set to NULL.

  @param[out] Count               On output, the number of elements in
                                  OriginalAttributes. On error it is set to
                                  zero.
**/
VOID
EnablePciDecoding (
  OUT ORIGINAL_ATTRIBUTES **OriginalAttributes,
  OUT UINTN               *Count
  )
{
  EFI_STATUS          Status;
  UINTN               NoHandles;
  EFI_HANDLE          *Handles;
  ORIGINAL_ATTRIBUTES *OrigAttrs;
  UINTN               Idx;

  *OriginalAttributes = NULL;
  *Count              = 0;

  if (PcdGetBool (PcdPciDisableBusEnumeration)) {
    //
    // The platform downloads ACPI tables from QEMU in general, but there are
    // no root bridges in this execution. We're done.
    //
    return;
  }

  Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiPciIoProtocolGuid,
                  NULL /* SearchKey */, &NoHandles, &Handles);
  if (Status == EFI_NOT_FOUND) {
    //
    // No PCI devices were found on either of the root bridges. We're done.
    //
    return;
  }

  if (EFI_ERROR (Status)) {
    DEBUG ((EFI_D_WARN, "%a: LocateHandleBuffer(): %r\n", __FUNCTION__,
      Status));
    return;
  }

  OrigAttrs = AllocatePool (NoHandles * sizeof *OrigAttrs);
  if (OrigAttrs == NULL) {
    DEBUG ((EFI_D_WARN, "%a: AllocatePool(): out of resources\n",
      __FUNCTION__));
    goto FreeHandles;
  }

  for (Idx = 0; Idx < NoHandles; ++Idx) {
    EFI_PCI_IO_PROTOCOL *PciIo;
    UINT64              Attributes;

    //
    // Look up PciIo on the handle and stash it
    //
    Status = gBS->HandleProtocol (Handles[Idx], &gEfiPciIoProtocolGuid,
                    (VOID**)&PciIo);
    ASSERT_EFI_ERROR (Status);
    OrigAttrs[Idx].PciIo = PciIo;

    //
    // Stash the current attributes
    //
    Status = PciIo->Attributes (PciIo, EfiPciIoAttributeOperationGet, 0,
                      &OrigAttrs[Idx].PciAttributes);
    if (EFI_ERROR (Status)) {
      DEBUG ((EFI_D_WARN, "%a: EfiPciIoAttributeOperationGet: %r\n",
        __FUNCTION__, Status));
      goto RestoreAttributes;
    }

    //
    // Retrieve supported attributes
    //
    Status = PciIo->Attributes (PciIo, EfiPciIoAttributeOperationSupported, 0,
                      &Attributes);
    if (EFI_ERROR (Status)) {
      DEBUG ((EFI_D_WARN, "%a: EfiPciIoAttributeOperationSupported: %r\n",
        __FUNCTION__, Status));
      goto RestoreAttributes;
    }

    //
    // Enable IO and MMIO decoding
    //
    Attributes &= EFI_PCI_IO_ATTRIBUTE_IO | EFI_PCI_IO_ATTRIBUTE_MEMORY;
    Status = PciIo->Attributes (PciIo, EfiPciIoAttributeOperationEnable,
                      Attributes, NULL);
    if (EFI_ERROR (Status)) {
      DEBUG ((EFI_D_WARN, "%a: EfiPciIoAttributeOperationEnable: %r\n",
        __FUNCTION__, Status));
      goto RestoreAttributes;
    }
  }

  //
  // Success
  //
  FreePool (Handles);
  *OriginalAttributes = OrigAttrs;
  *Count              = NoHandles;
  return;

RestoreAttributes:
  while (Idx > 0) {
    --Idx;
    OrigAttrs[Idx].PciIo->Attributes (OrigAttrs[Idx].PciIo,
                            EfiPciIoAttributeOperationSet,
                            OrigAttrs[Idx].PciAttributes,
                            NULL
                            );
  }
  FreePool (OrigAttrs);

FreeHandles:
  FreePool (Handles);
}


/**
  Restore the original PCI attributes saved with EnablePciDecoding().

  @param[in] OriginalAttributes  The array allocated and populated by
                                 EnablePciDecoding(). This parameter may be
                                 NULL. If OriginalAttributes is NULL, then the
                                 function is a no-op; otherwise the PciIo
                                 attributes will be restored, and the
                                 OriginalAttributes array will be freed.

  @param[in] Count               The Count value stored by EnablePciDecoding(),
                                 the number of elements in OriginalAttributes.
                                 Count may be zero if and only if
                                 OriginalAttributes is NULL.
**/
VOID
RestorePciDecoding (
  IN ORIGINAL_ATTRIBUTES *OriginalAttributes,
  IN UINTN               Count
  )
{
  UINTN Idx;

  ASSERT ((OriginalAttributes == NULL) == (Count == 0));
  if (OriginalAttributes == NULL) {
    return;
  }

  for (Idx = 0; Idx < Count; ++Idx) {
    OriginalAttributes[Idx].PciIo->Attributes (
                                     OriginalAttributes[Idx].PciIo,
                                     EfiPciIoAttributeOperationSet,
                                     OriginalAttributes[Idx].PciAttributes,
                                     NULL
                                     );
  }
  FreePool (OriginalAttributes);
}