From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2607:f8b0:4001:c0b::241; helo=mail-it0-x241.google.com; envelope-from=ard.biesheuvel@linaro.org; receiver=edk2-devel@lists.01.org Received: from mail-it0-x241.google.com (mail-it0-x241.google.com [IPv6:2607:f8b0:4001:c0b::241]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 3BCC9203555F8 for ; Mon, 13 Nov 2017 04:02:04 -0800 (PST) Received: by mail-it0-x241.google.com with SMTP id l196so9035479itl.4 for ; Mon, 13 Nov 2017 04:06:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=gB6Pm6UAOd5t8xDzP/Y1hEw5AXlpmaD0OfYX28Qn0RQ=; b=deIOPJsyckwzeDB6mWuO0LYQ99VUdNqWhRV1+SGza8p/cMiB/IJAVG+M/ZNNIIPsaf X9an9GhnkIBrHqiog1mkP4C8H+lGuxPMU1Hfi6HZTSw5UI6M9rypornG4pFPRukuZRJ+ 0OzZKCHASkg6n5t44SToMrw3SgBf9ZMQ2ENjE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=gB6Pm6UAOd5t8xDzP/Y1hEw5AXlpmaD0OfYX28Qn0RQ=; b=PbF6DmmBOGtfDNym0lr2ZxP8B76NnYyS06OF0cTfnPwtPtJ900OYmVKyxZvfvj2e3H /YQAiD9RHYv2juA3W6SxO7hN5jpaB4HBFKVebpRcZa2Zs1yUDmb9JnR+JVoQH55G7oUk k6BJMuiCsYTV7j7UYWkGCVJp7dnmY3Bbh7mf7WlKiTWcEmG/6v3zFQNIPREsvxhkpfVG TtQvASqsFpx16fqZq4as4YlVkpKzOEIPQSIUgtu0EIxja9ENrhgjVkleoYV/j40S1W3r 5t4FoNTXEWhKOohG6bFhtWa+zbINVsrvpp84qTCANaDulPAQtuEJGz9HwVOlBGlWZ1AY XcQQ== X-Gm-Message-State: AJaThX5AdSq1SlA1Tj7bjIIUym1Dy7nOWE8+95TPXQZYcgll2KKGeldw +gL4WO8zHSfz0LoCTQg3fBuhVho/7v/Uf6Brtckwyw== X-Google-Smtp-Source: AGs4zMbpcI3M3h51aga7EhzcqQW9c1aWti79YCl7sxF6mIgB/KpTlUlApmQhAhIe5BgAw9hm+WvkYyavr4bNZHJTLI8= X-Received: by 10.36.141.70 with SMTP id w67mr10162046itd.58.1510574769542; Mon, 13 Nov 2017 04:06:09 -0800 (PST) MIME-Version: 1.0 Received: by 10.107.104.20 with HTTP; Mon, 13 Nov 2017 04:06:09 -0800 (PST) In-Reply-To: <1510065736-9394-2-git-send-email-meenakshi.aggarwal@nxp.com> References: <1510065736-9394-1-git-send-email-meenakshi.aggarwal@nxp.com> <1510065736-9394-2-git-send-email-meenakshi.aggarwal@nxp.com> From: Ard Biesheuvel Date: Mon, 13 Nov 2017 12:06:09 +0000 Message-ID: To: Meenakshi Aggarwal Cc: Leif Lindholm , "Kinney, Michael D" , "edk2-devel@lists.01.org" , Udit Kumar , Varun Sethi Subject: Re: [PATCH 01/10] Platform/NXP: Library to provide helper functions. X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 13 Nov 2017 12:02:04 -0000 Content-Type: text/plain; charset="UTF-8" On 7 November 2017 at 14:42, Meenakshi Aggarwal wrote: > 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 > --- > 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 Search/replace error > + * 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)); \ > +} \ > +) > + Which patch uses this function? > +/* > + * 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); > +} > + Please move this into the patch that uses it. > +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)) \ > + Same here > +/** > + 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; > +} > + Which patch uses this function? > +/* > + * PrINT32 Sizes As "Xxx KiB", "Xxx.Y KiB", "Xxx MiB", "Xxx.Y MiB", Search/replace error. > + * 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 > + ); > + Please move these into the patch that uses them. > +#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 > +#include > +#include > + > +/* 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)); > +} Why DEBUG_ERROR ? Don't use that for DEBUG code, and in general, adding DEBUG () to a library should be done with care. (It may break DXE_RUNTIME_DRIVER modules called by the OS) > 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 Should be 0x0001001A > + 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 Please drop the edk2-platforms prefix, and add it to your PACKAGES_PATH > + > +[LibraryClasses] > + PrintLib > + > +[Sources.common] > + Utils.c > -- > 1.9.1 >