Home | History | Annotate | Download | only in AcpiPlatformDxe
      1 /** @file
      2   Temporarily enable IO and MMIO decoding for all PCI devices while QEMU
      3   regenerates the ACPI tables.
      4 
      5   Copyright (C) 2016, Red Hat, Inc.
      6 
      7   This program and the accompanying materials are licensed and made available
      8   under the terms and conditions of the BSD License which accompanies this
      9   distribution.  The full text of the license may be found at
     10   http://opensource.org/licenses/bsd-license.php
     11 
     12   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
     13   WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     14 **/
     15 
     16 #include <Library/MemoryAllocationLib.h>
     17 
     18 #include "AcpiPlatform.h"
     19 
     20 
     21 /**
     22   Collect all PciIo protocol instances in the system. Save their original
     23   attributes, and enable IO and MMIO decoding for each.
     24 
     25   This is a best effort function; it doesn't return status codes. Its
     26   caller is supposed to proceed even if this function fails.
     27 
     28   @param[out] OriginalAttributes  On output, a dynamically allocated array of
     29                                   ORIGINAL_ATTRIBUTES elements. The array lists
     30                                   the PciIo protocol instances found in the
     31                                   system at the time of the call, plus the
     32                                   original PCI attributes for each.
     33 
     34                                   Before returning, the function enables IO and
     35                                   MMIO decoding for each PciIo instance it
     36                                   finds.
     37 
     38                                   On error, or when no such instances are
     39                                   found, OriginalAttributes is set to NULL.
     40 
     41   @param[out] Count               On output, the number of elements in
     42                                   OriginalAttributes. On error it is set to
     43                                   zero.
     44 **/
     45 VOID
     46 EnablePciDecoding (
     47   OUT ORIGINAL_ATTRIBUTES **OriginalAttributes,
     48   OUT UINTN               *Count
     49   )
     50 {
     51   EFI_STATUS          Status;
     52   UINTN               NoHandles;
     53   EFI_HANDLE          *Handles;
     54   ORIGINAL_ATTRIBUTES *OrigAttrs;
     55   UINTN               Idx;
     56 
     57   *OriginalAttributes = NULL;
     58   *Count              = 0;
     59 
     60   if (PcdGetBool (PcdPciDisableBusEnumeration)) {
     61     //
     62     // The platform downloads ACPI tables from QEMU in general, but there are
     63     // no root bridges in this execution. We're done.
     64     //
     65     return;
     66   }
     67 
     68   Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiPciIoProtocolGuid,
     69                   NULL /* SearchKey */, &NoHandles, &Handles);
     70   if (Status == EFI_NOT_FOUND) {
     71     //
     72     // No PCI devices were found on either of the root bridges. We're done.
     73     //
     74     return;
     75   }
     76 
     77   if (EFI_ERROR (Status)) {
     78     DEBUG ((EFI_D_WARN, "%a: LocateHandleBuffer(): %r\n", __FUNCTION__,
     79       Status));
     80     return;
     81   }
     82 
     83   OrigAttrs = AllocatePool (NoHandles * sizeof *OrigAttrs);
     84   if (OrigAttrs == NULL) {
     85     DEBUG ((EFI_D_WARN, "%a: AllocatePool(): out of resources\n",
     86       __FUNCTION__));
     87     goto FreeHandles;
     88   }
     89 
     90   for (Idx = 0; Idx < NoHandles; ++Idx) {
     91     EFI_PCI_IO_PROTOCOL *PciIo;
     92     UINT64              Attributes;
     93 
     94     //
     95     // Look up PciIo on the handle and stash it
     96     //
     97     Status = gBS->HandleProtocol (Handles[Idx], &gEfiPciIoProtocolGuid,
     98                     (VOID**)&PciIo);
     99     ASSERT_EFI_ERROR (Status);
    100     OrigAttrs[Idx].PciIo = PciIo;
    101 
    102     //
    103     // Stash the current attributes
    104     //
    105     Status = PciIo->Attributes (PciIo, EfiPciIoAttributeOperationGet, 0,
    106                       &OrigAttrs[Idx].PciAttributes);
    107     if (EFI_ERROR (Status)) {
    108       DEBUG ((EFI_D_WARN, "%a: EfiPciIoAttributeOperationGet: %r\n",
    109         __FUNCTION__, Status));
    110       goto RestoreAttributes;
    111     }
    112 
    113     //
    114     // Retrieve supported attributes
    115     //
    116     Status = PciIo->Attributes (PciIo, EfiPciIoAttributeOperationSupported, 0,
    117                       &Attributes);
    118     if (EFI_ERROR (Status)) {
    119       DEBUG ((EFI_D_WARN, "%a: EfiPciIoAttributeOperationSupported: %r\n",
    120         __FUNCTION__, Status));
    121       goto RestoreAttributes;
    122     }
    123 
    124     //
    125     // Enable IO and MMIO decoding
    126     //
    127     Attributes &= EFI_PCI_IO_ATTRIBUTE_IO | EFI_PCI_IO_ATTRIBUTE_MEMORY;
    128     Status = PciIo->Attributes (PciIo, EfiPciIoAttributeOperationEnable,
    129                       Attributes, NULL);
    130     if (EFI_ERROR (Status)) {
    131       DEBUG ((EFI_D_WARN, "%a: EfiPciIoAttributeOperationEnable: %r\n",
    132         __FUNCTION__, Status));
    133       goto RestoreAttributes;
    134     }
    135   }
    136 
    137   //
    138   // Success
    139   //
    140   FreePool (Handles);
    141   *OriginalAttributes = OrigAttrs;
    142   *Count              = NoHandles;
    143   return;
    144 
    145 RestoreAttributes:
    146   while (Idx > 0) {
    147     --Idx;
    148     OrigAttrs[Idx].PciIo->Attributes (OrigAttrs[Idx].PciIo,
    149                             EfiPciIoAttributeOperationSet,
    150                             OrigAttrs[Idx].PciAttributes,
    151                             NULL
    152                             );
    153   }
    154   FreePool (OrigAttrs);
    155 
    156 FreeHandles:
    157   FreePool (Handles);
    158 }
    159 
    160 
    161 /**
    162   Restore the original PCI attributes saved with EnablePciDecoding().
    163 
    164   @param[in] OriginalAttributes  The array allocated and populated by
    165                                  EnablePciDecoding(). This parameter may be
    166                                  NULL. If OriginalAttributes is NULL, then the
    167                                  function is a no-op; otherwise the PciIo
    168                                  attributes will be restored, and the
    169                                  OriginalAttributes array will be freed.
    170 
    171   @param[in] Count               The Count value stored by EnablePciDecoding(),
    172                                  the number of elements in OriginalAttributes.
    173                                  Count may be zero if and only if
    174                                  OriginalAttributes is NULL.
    175 **/
    176 VOID
    177 RestorePciDecoding (
    178   IN ORIGINAL_ATTRIBUTES *OriginalAttributes,
    179   IN UINTN               Count
    180   )
    181 {
    182   UINTN Idx;
    183 
    184   ASSERT ((OriginalAttributes == NULL) == (Count == 0));
    185   if (OriginalAttributes == NULL) {
    186     return;
    187   }
    188 
    189   for (Idx = 0; Idx < Count; ++Idx) {
    190     OriginalAttributes[Idx].PciIo->Attributes (
    191                                      OriginalAttributes[Idx].PciIo,
    192                                      EfiPciIoAttributeOperationSet,
    193                                      OriginalAttributes[Idx].PciAttributes,
    194                                      NULL
    195                                      );
    196   }
    197   FreePool (OriginalAttributes);
    198 }
    199