public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: Meenakshi Aggarwal <meenakshi.aggarwal@nxp.com>
To: <ard.biesheuvel@linaro.org>, <leif.lindholm@linaro.org>,
	<michael.d.kinney@intel.com>, <edk2-devel@lists.01.org>
Subject: [PATCH 01/10] Platform/NXP: Library to provide helper functions.
Date: Tue, 7 Nov 2017 20:12:07 +0530	[thread overview]
Message-ID: <1510065736-9394-2-git-send-email-meenakshi.aggarwal@nxp.com> (raw)
In-Reply-To: <1510065736-9394-1-git-send-email-meenakshi.aggarwal@nxp.com>

UtilsLib provide helper functions which will be needed
by NXP SoCs Library and Drivers.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Meenakshi Aggarwal <meenakshi.aggarwal@nxp.com>
---
 Platform/NXP/Include/Library/Utils.h    | 137 ++++++++++++++++++++++++++++++++
 Platform/NXP/Library/UtilsLib/Utils.c   |  97 ++++++++++++++++++++++
 Platform/NXP/Library/UtilsLib/Utils.inf |  30 +++++++
 3 files changed, 264 insertions(+)
 create mode 100644 Platform/NXP/Include/Library/Utils.h
 create mode 100644 Platform/NXP/Library/UtilsLib/Utils.c
 create mode 100644 Platform/NXP/Library/UtilsLib/Utils.inf

diff --git a/Platform/NXP/Include/Library/Utils.h b/Platform/NXP/Include/Library/Utils.h
new file mode 100644
index 0000000..8920e4d
--- /dev/null
+++ b/Platform/NXP/Include/Library/Utils.h
@@ -0,0 +1,137 @@
+/** Utils.h
+  Header defining the General Purpose Utilities
+
+  Copyright 2017 NXP
+
+  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.
+
+**/
+
+#ifndef __UTILS_H__
+#define __UTILS_H__
+
+/*
+ * Divide positive or negative dividend by positive divisor and round
+ * to closest UINTNeger. Result is undefined for negative divisors and
+ * for negative dividends if the divisor variable type is unsigned.
+ */
+#define DIV_ROUND_CLOSEST(X, Divisor)(           \
+{                                                \
+  typeof(X) __X = X;                             \
+  typeof(Divisor) __D = Divisor;                 \
+  (((typeof(X))-1) > 0 ||                        \
+    ((typeof(Divisor))-1) > 0 || (__X) > 0) ?    \
+      (((__X) + ((__D) / 2)) / (__D)) :          \
+      (((__X) - ((__D) / 2)) / (__D));           \
+}                                                \
+)
+
+/*
+ * HammingWeight32: returns the hamming weight (i.e. the number
+ * of bits set) of a 32-bit word
+ */
+STATIC
+inline
+UINTN
+HammingWeight32 (
+  IN  UINTN  W
+  )
+{
+  UINTN Res;
+
+  Res = (W & 0x55555555) + ((W >> 1) & 0x55555555);
+  Res = (Res & 0x33333333) + ((Res >> 2) & 0x33333333);
+  Res = (Res & 0x0F0F0F0F) + ((Res >> 4) & 0x0F0F0F0F);
+  Res = (Res & 0x00FF00FF) + ((Res >> 8) & 0x00FF00FF);
+
+  return (Res & 0x0000FFFF) + ((Res >> 16) & 0x0000FFFF);
+}
+
+STATIC
+inline
+UINTN
+CpuMaskNext (
+  IN  UINTN  Cpu,
+  IN  UINTN  Mask
+  )
+{
+  for (Cpu++; !((1 << Cpu) & Mask); Cpu++)
+    ;
+
+  return Cpu;
+}
+
+#define ForEachCpu(Iter, Cpu, NumCpus, Mask) \
+  for (Iter = 0, Cpu = CpuMaskNext(-1, Mask); \
+    Iter < NumCpus; \
+    Iter++, Cpu = CpuMaskNext(Cpu, Mask)) \
+
+/**
+  Find last (most-significant) bit set
+
+  @param   X :        the word to search
+
+  Note Fls(0) = 0, Fls(1) = 1, Fls(0x80000000) = 32.
+
+**/
+STATIC
+inline
+INT32
+GenericFls (
+  IN  INT32  X
+  )
+{
+  INT32 R = 32;
+
+  if (!X)
+    return 0;
+
+  if (!(X & 0xffff0000u)) {
+    X <<= 16;
+    R -= 16;
+  }
+  if (!(X & 0xff000000u)) {
+    X <<= 8;
+    R -= 8;
+  }
+  if (!(X & 0xf0000000u)) {
+    X <<= 4;
+    R -= 4;
+  }
+  if (!(X & 0xc0000000u)) {
+    X <<= 2;
+    R -= 2;
+  }
+  if (!(X & 0x80000000u)) {
+    X <<= 1;
+    R -= 1;
+  }
+
+  return R;
+}
+
+/*
+ * PrINT32 Sizes As "Xxx KiB", "Xxx.Y KiB", "Xxx MiB", "Xxx.Y MiB",
+ * Xxx GiB, Xxx.Y GiB, Etc As Needed; Allow for Optional Trailing String
+ * (Like "\n")
+ */
+VOID
+PrintSize (
+  IN  UINT64 Size,
+  IN  CONST INT8 *S
+  );
+
+/* Function to convert a frequency to MHz */
+CHAR8 *StringToMHz (
+  CHAR8   *Buf,
+  UINT32  Size,
+  UINT64  Hz
+  );
+
+#endif
diff --git a/Platform/NXP/Library/UtilsLib/Utils.c b/Platform/NXP/Library/UtilsLib/Utils.c
new file mode 100644
index 0000000..4f5a15c
--- /dev/null
+++ b/Platform/NXP/Library/UtilsLib/Utils.c
@@ -0,0 +1,97 @@
+/** Utils.c
+
+  Copyright 2017 NXP
+
+  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/DebugLib.h>
+#include <Library/PrintLib.h>
+#include <Library/Utils.h>
+
+/* Function to convert a frequency to MHz */
+CHAR8 *
+StringToMHz (
+  IN  CHAR8   *Buf,
+  IN  UINT32  Size,
+  IN  UINT64  Hz
+  )
+{
+  UINT64 L;
+  UINT64 M;
+  UINT64 N;
+
+  N = DIV_ROUND_CLOSEST(Hz, 1000) / 1000L;
+  L = AsciiSPrint (Buf, Size, "%ld", N);
+
+  Hz -= N * 1000000L;
+  M = DIV_ROUND_CLOSEST(Hz, 1000L);
+
+  if (M != 0) {
+    AsciiSPrint (Buf + L, Size, ".%03ld", M);
+  }
+
+  return (Buf);
+}
+
+/*
+ * PrINT32 Sizes As "Xxx KiB", "Xxx.Y KiB", "Xxx MiB", "Xxx.Y MiB",
+ * Xxx GiB, Xxx.Y GiB, Etc As Needed; Allow for Optional Trailing String
+ * (Like "\n")
+ */
+VOID
+PrintSize (
+  IN  UINT64 Size,
+  IN  CONST  INT8 *S
+  )
+{
+  UINT64 M;
+  UINT64 N;
+  UINT64 F;
+  UINT64 D;
+  CHAR8 C;
+  UINT32 I;
+  INT8 Names[6] = {'E', 'P', 'T', 'G', 'M', 'K'};
+
+  M = 0;
+  D = 10 * ARRAY_SIZE(Names);
+  C = 0;
+
+  for (I = 0; I < ARRAY_SIZE(Names); I++, D -= 10) {
+    if (Size >> D) {
+      C = Names[I];
+      break;
+    }
+  }
+
+  if (!C) {
+    DEBUG((DEBUG_ERROR, "%Ld Bytes,\n %a", Size, S));
+    return;
+  }
+
+  N = Size >> D;
+  F = Size & ((1ULL << D) - 1);
+
+  /* if There'S A Remainder, Deal With It */
+  if (F) {
+    M = (10ULL * F + (1ULL << (D - 1))) >> D;
+
+    if (M >= 10) {
+           M -= 10;
+           N += 1;
+    }
+  }
+
+  DEBUG((DEBUG_ERROR, "%Ld", N));
+  if (M) {
+    DEBUG((DEBUG_ERROR, ".%Ld", M));
+  }
+  DEBUG((DEBUG_ERROR, " %ciB, %a ", C, S));
+}
diff --git a/Platform/NXP/Library/UtilsLib/Utils.inf b/Platform/NXP/Library/UtilsLib/Utils.inf
new file mode 100644
index 0000000..9901445
--- /dev/null
+++ b/Platform/NXP/Library/UtilsLib/Utils.inf
@@ -0,0 +1,30 @@
+#  @Utils.inf
+
+#  Copyright 2017 NXP
+#
+#  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.
+#
+
+[Defines]
+  INF_VERSION                    = 0x00010005
+  BASE_NAME                      = UtilsLib
+  FILE_GUID                      = 0985d4e8-5a41-40cf-ad12-2ad5d35e817f
+  MODULE_TYPE                    = BASE
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = UtilsLib
+
+[Packages]
+  MdePkg/MdePkg.dec
+  edk2-platforms/Platform/NXP/NxpQoriqLs.dec
+
+[LibraryClasses]
+  PrintLib
+
+[Sources.common]
+  Utils.c
-- 
1.9.1



  reply	other threads:[~2017-11-07  8:50 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-07 14:42 [PATCH 00/10] edk2-platforms/Platform/NXP Meenakshi Aggarwal
2017-11-07 14:42 ` Meenakshi Aggarwal [this message]
2017-11-13 12:06   ` [PATCH 01/10] Platform/NXP: Library to provide helper functions Ard Biesheuvel
2017-11-14  5:15     ` Meenakshi Aggarwal
2017-11-13 13:14   ` Leif Lindholm
2017-11-14  5:52     ` Meenakshi Aggarwal
2017-11-07 14:42 ` [PATCH 02/10] Platform/NXP: Add support for system reset library Meenakshi Aggarwal
2017-11-13 12:09   ` Ard Biesheuvel
2017-11-14  5:19     ` Meenakshi Aggarwal
2017-11-07 14:42 ` [PATCH 03/10] Platform/NXP: Add support for Big Endian Mmio APIs Meenakshi Aggarwal
2017-11-13 12:26   ` Ard Biesheuvel
2017-11-14  4:10     ` Meenakshi Aggarwal
2017-11-07 14:42 ` [PATCH 04/10] Platform/NXP : Add support for Watchdog driver Meenakshi Aggarwal
2017-11-13 12:23   ` Ard Biesheuvel
2017-11-14  5:36     ` Meenakshi Aggarwal
2017-11-07 14:42 ` [PATCH 05/10] Platform/NXP : Add support for DUART library Meenakshi Aggarwal
2017-11-13 12:28   ` Ard Biesheuvel
2017-11-14  4:12     ` Meenakshi Aggarwal
2017-11-07 14:42 ` [PATCH 06/10] Platform/NXP: Add support for I2c operations library Meenakshi Aggarwal
2017-11-13 12:36   ` Ard Biesheuvel
2017-11-14  5:36     ` Meenakshi Aggarwal
2017-11-07 14:42 ` [PATCH 07/10] Platform/NXP : Add support for DS1307 RTC library Meenakshi Aggarwal
2017-11-13 12:42   ` Ard Biesheuvel
2017-11-14  5:51     ` Meenakshi Aggarwal
2017-11-07 14:42 ` [PATCH 08/10] Platform/NXP: Add support for ArmPlatformLib Meenakshi Aggarwal
2017-11-07 14:42 ` [PATCH 09/10] SocLib : Add support for initialization of peripherals Meenakshi Aggarwal
2017-11-07 14:42 ` [PATCH 10/10] Compilation : Add the fdf, dsc and dec files Meenakshi Aggarwal
2017-11-22 15:48 ` [PATCH v2 1/9] Platform/NXP: Add support for Big Endian Mmio APIs Meenakshi Aggarwal
2017-11-22 15:48   ` [PATCH v2 2/9] Platform/NXP : Add support for Watchdog driver Meenakshi Aggarwal
2017-11-22 15:48   ` [PATCH v2 3/9] SocLib : Add support for initialization of peripherals Meenakshi Aggarwal
2017-11-22 15:48   ` [PATCH v2 4/9] Platform/NXP : Add support for DUART library Meenakshi Aggarwal
2017-11-22 15:48   ` [PATCH v2 5/9] Platform/NXP: Add support for I2c driver Meenakshi Aggarwal
2017-11-22 15:48   ` [PATCH v2 6/9] Silicon/Maxim : Add support for DS1307 RTC library Meenakshi Aggarwal
2017-11-22 15:48   ` [PATCH v2 7/9] Platform/NXP: Add support for ArmPlatformLib Meenakshi Aggarwal
2017-11-22 15:48   ` [PATCH v2 8/9] Compilation : Add the fdf, dsc and dec files Meenakshi Aggarwal
2017-11-22 15:49   ` [PATCH v2 9/9] Build : Add build script and environment script Meenakshi Aggarwal
2017-11-26 15:48   ` [PATCH v2 1/9] Platform/NXP: Add support for Big Endian Mmio APIs Leif Lindholm
2017-11-27  5:08     ` Meenakshi Aggarwal
2017-11-27  7:10     ` Udit Kumar

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=1510065736-9394-2-git-send-email-meenakshi.aggarwal@nxp.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