public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: Laszlo Ersek <lersek@redhat.com>
To: edk2-devel-01 <edk2-devel@lists.01.org>
Cc: Jordan Justen <jordan.l.justen@intel.com>,
	Marcel Apfelbaum <marcel@redhat.com>,
	Ruiyu Ni <ruiyu.ni@intel.com>
Subject: [PATCH 6/7] OvmfPkg/PciHotPlugInitDxe: add helper functions for setting up paddings
Date: Mon, 25 Sep 2017 21:58:23 +0200	[thread overview]
Message-ID: <20170925195824.10866-7-lersek@redhat.com> (raw)
In-Reply-To: <20170925195824.10866-1-lersek@redhat.com>

Extract the SetIoPadding() and SetMmioPadding() functions, so that we can
set EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR fields using parameter names and
values that are more friendly than the original field names and their
expected values.

Introduce the HighBitSetRoundUp32() and HighBitSetRoundUp64() functions
for calculating the last parameter ("SizeExponent") of SetIoPadding() and
SetMmioPadding().

Put the new functions to use when requesting the default reservations. (In
order to be consistent with a later patch, "SizeExponent" is calculated
for SetIoPadding() with HighBitSetRoundUp64().)

Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Marcel Apfelbaum <marcel@redhat.com>
Cc: Ruiyu Ni <ruiyu.ni@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
---
 OvmfPkg/PciHotPlugInitDxe/PciHotPlugInit.inf |   1 +
 OvmfPkg/PciHotPlugInitDxe/PciHotPlugInit.c   | 165 ++++++++++++++++++--
 2 files changed, 156 insertions(+), 10 deletions(-)

diff --git a/OvmfPkg/PciHotPlugInitDxe/PciHotPlugInit.inf b/OvmfPkg/PciHotPlugInitDxe/PciHotPlugInit.inf
index 91729ae1ed04..e0ec9baae1c2 100644
--- a/OvmfPkg/PciHotPlugInitDxe/PciHotPlugInit.inf
+++ b/OvmfPkg/PciHotPlugInitDxe/PciHotPlugInit.inf
@@ -28,8 +28,9 @@ [Packages]
   MdeModulePkg/MdeModulePkg.dec
   MdePkg/MdePkg.dec
 
 [LibraryClasses]
+  BaseLib
   BaseMemoryLib
   DebugLib
   DevicePathLib
   MemoryAllocationLib
diff --git a/OvmfPkg/PciHotPlugInitDxe/PciHotPlugInit.c b/OvmfPkg/PciHotPlugInitDxe/PciHotPlugInit.c
index b1b2c5cd8ddc..39646973794b 100644
--- a/OvmfPkg/PciHotPlugInitDxe/PciHotPlugInit.c
+++ b/OvmfPkg/PciHotPlugInitDxe/PciHotPlugInit.c
@@ -14,8 +14,9 @@
 **/
 
 #include <IndustryStandard/Acpi10.h>
 
+#include <Library/BaseLib.h>
 #include <Library/BaseMemoryLib.h>
 #include <Library/DebugLib.h>
 #include <Library/DevicePathLib.h>
 #include <Library/MemoryAllocationLib.h>
@@ -97,8 +98,155 @@ InitializeResourcePadding (
   ResourcePadding->EndDesc.Desc = ACPI_END_TAG_DESCRIPTOR;
 }
 
 
+/**
+  Set up a descriptor entry for reserving IO space.
+
+  @param[in,out] Descriptor  The descriptor to configure. The caller shall have
+                             initialized Descriptor earlier, with
+                             InitializeResourcePadding().
+
+  @param[in] SizeExponent    The size and natural alignment of the reservation
+                             are determined by raising two to this power.
+**/
+STATIC
+VOID
+SetIoPadding (
+  IN OUT EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptor,
+  IN     UINTN                             SizeExponent
+  )
+{
+  Descriptor->ResType = ACPI_ADDRESS_SPACE_TYPE_IO;
+  Descriptor->AddrLen = LShiftU64 (1, SizeExponent);
+  Descriptor->AddrRangeMax = Descriptor->AddrLen - 1;
+}
+
+
+/**
+  Set up a descriptor entry for reserving MMIO space.
+
+  @param[in,out] Descriptor    The descriptor to configure. The caller shall
+                               have initialized Descriptor earlier, with
+                               InitializeResourcePadding().
+
+  @param[in] Prefetchable      TRUE if the descriptor should reserve
+                               prefetchable MMIO space. Pass FALSE for
+                               reserving non-prefetchable MMIO space.
+
+  @param[in] ThirtyTwoBitOnly  TRUE if the reservation should be limited to
+                               32-bit address space. FALSE if the reservation
+                               can be satisfied from 64-bit address space.
+                               ThirtyTwoBitOnly is ignored if Prefetchable is
+                               FALSE; in that case ThirtyTwoBitOnly is always
+                               considered TRUE.
+
+  @param[in] SizeExponent      The size and natural alignment of the
+                               reservation are determined by raising two to
+                               this power.
+**/
+STATIC
+VOID
+SetMmioPadding (
+  IN OUT EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptor,
+  IN     BOOLEAN                           Prefetchable,
+  IN     BOOLEAN                           ThirtyTwoBitOnly,
+  IN     UINTN                             SizeExponent
+  )
+{
+  Descriptor->ResType = ACPI_ADDRESS_SPACE_TYPE_MEM;
+  if (Prefetchable) {
+    Descriptor->SpecificFlag =
+      EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE;
+    Descriptor->AddrSpaceGranularity = ThirtyTwoBitOnly ? 32 : 64;
+  } else {
+    Descriptor->SpecificFlag =
+      EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_NON_CACHEABLE;
+    Descriptor->AddrSpaceGranularity = 32;
+  }
+  Descriptor->AddrLen = LShiftU64 (1, SizeExponent);
+  Descriptor->AddrRangeMax = Descriptor->AddrLen - 1;
+}
+
+
+/**
+  Round up a positive 32-bit value to the next whole power of two, and return
+  the bit position of the highest bit set in the result. Equivalent to
+  ceil(log2(x)).
+
+  @param[in] Operand  The 32-bit operand to evaluate.
+
+  @retval -1     Operand is zero.
+
+  @retval -1     Operand is positive, not a whole power of two, and rounding it
+                 up to the next power of two does not fit into 32 bits.
+
+  @retval 0..31  Otherwise, return ceil(log2(Value)).
+**/
+STATIC
+INTN
+HighBitSetRoundUp32 (
+  IN UINT32 Operand
+  )
+{
+  INTN HighBit;
+
+  HighBit = HighBitSet32 (Operand);
+  if (HighBit == -1) {
+    //
+    // Operand is zero.
+    //
+    return HighBit;
+  }
+  if ((Operand & (Operand - 1)) != 0) {
+    //
+    // Operand is not a whole power of two.
+    //
+    ++HighBit;
+  }
+  return (HighBit < 32) ? HighBit : -1;
+}
+
+
+/**
+  Round up a positive 64-bit value to the next whole power of two, and return
+  the bit position of the highest bit set in the result. Equivalent to
+  ceil(log2(x)).
+
+  @param[in] Operand  The 64-bit operand to evaluate.
+
+  @retval -1     Operand is zero.
+
+  @retval -1     Operand is positive, not a whole power of two, and rounding it
+                 up to the next power of two does not fit into 64 bits.
+
+  @retval 0..63  Otherwise, return ceil(log2(Value)).
+**/
+STATIC
+INTN
+HighBitSetRoundUp64 (
+  IN UINT64 Operand
+  )
+{
+  INTN HighBit;
+
+  HighBit = HighBitSet64 (Operand);
+  if (HighBit == -1) {
+    //
+    // Operand is zero.
+    //
+    return HighBit;
+  }
+  if ((Operand & (Operand - 1)) != 0) {
+    //
+    // Operand is not a whole power of two.
+    //
+    ++HighBit;
+  }
+  return (HighBit < 64) ? HighBit : -1;
+}
+
+
 /**
   Returns a list of root Hot Plug Controllers (HPCs) that require
   initialization during the boot process.
 
@@ -297,12 +445,9 @@ GetResourcePadding (
   if (DefaultIo) {
     //
     // Request defaults.
     //
-    --FirstResource;
-    FirstResource->ResType      = ACPI_ADDRESS_SPACE_TYPE_IO;
-    FirstResource->AddrRangeMax = 512 - 1; // align at 512 IO ports
-    FirstResource->AddrLen      = 512;     // 512 IO ports
+    SetIoPadding (--FirstResource, (UINTN)HighBitSetRoundUp64 (512));
   }
 
   //
   // (c) Reserve non-prefetchable MMIO space (32-bit only).
@@ -310,14 +455,14 @@ GetResourcePadding (
   if (DefaultMmio) {
     //
     // Request defaults.
     //
-    --FirstResource;
-    FirstResource->ResType              = ACPI_ADDRESS_SPACE_TYPE_MEM;
-    FirstResource->SpecificFlag         = 0;            // non-prefetchable
-    FirstResource->AddrSpaceGranularity = 32;           // 32-bit aperture
-    FirstResource->AddrRangeMax         = SIZE_2MB - 1; // align at 2MB
-    FirstResource->AddrLen              = SIZE_2MB;     // 2MB padding
+    SetMmioPadding (
+      --FirstResource,
+      FALSE,
+      TRUE,
+      (UINTN)HighBitSetRoundUp32 (SIZE_2MB)
+      );
   }
 
   //
   // Output a copy of ReservationRequest from the lowest-address populated
-- 
2.14.1.3.gb7cf6e02401b




  parent reply	other threads:[~2017-09-25 19:55 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-25 19:58 [PATCH 0/7] OvmfPkg/PciHotPlugInitDxe: obey PCI resource reservation hints from QEMU Laszlo Ersek
2017-09-25 19:58 ` [PATCH 1/7] MdePkg/IndustryStandard/Pci23: add vendor-specific capability header Laszlo Ersek
2017-09-27  6:26   ` Gao, Liming
2017-09-25 19:58 ` [PATCH 2/7] OvmfPkg/IndustryStandard: define PCI Capabilities for QEMU's PCI Bridges Laszlo Ersek
2017-09-25 19:58 ` [PATCH 3/7] OvmfPkg/PciHotPlugInitDxe: clean up protocol usage comment Laszlo Ersek
2017-09-25 19:58 ` [PATCH 4/7] OvmfPkg/PciHotPlugInitDxe: clean up addr. range for non-prefetchable MMIO Laszlo Ersek
2017-09-25 19:58 ` [PATCH 5/7] OvmfPkg/PciHotPlugInitDxe: generalize RESOURCE_PADDING composition Laszlo Ersek
2017-09-25 19:58 ` Laszlo Ersek [this message]
2017-09-25 19:58 ` [PATCH 7/7] OvmfPkg/PciHotPlugInitDxe: translate QEMU's resource reservation hints Laszlo Ersek
2017-10-02 17:43   ` Jordan Justen
2017-10-02 20:00     ` Laszlo Ersek
2017-10-03 14:09 ` [PATCH 0/7] OvmfPkg/PciHotPlugInitDxe: obey PCI resource reservation hints from QEMU Laszlo Ersek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170925195824.10866-7-lersek@redhat.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox