public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Ard Biesheuvel" <ardb@kernel.org>
To: Marcin Juszkiewicz <marcin.juszkiewicz@linaro.org>
Cc: devel@edk2.groups.io, Leif Lindholm <quic_llindhol@quicinc.com>,
	 Ard Biesheuvel <ardb+tianocore@kernel.org>,
	Graeme Gregory <graeme@xora.org.uk>,
	 Xiong Yining <xiongyining1480@phytium.com.cn>,
	Chen Baozi <chenbaozi@phytium.com.cn>
Subject: Re: [edk2-devel] [PATCH edk2-platforms v6 6/7] Platform/SbsaQemu: get the information of memory via SMC calls
Date: Thu, 14 Mar 2024 16:18:02 +0100	[thread overview]
Message-ID: <CAMj1kXEgr7E8HD4pGs=wcnTEQ4YsoTu2X=4Wh3ewow48VrTRoQ@mail.gmail.com> (raw)
In-Reply-To: <20240306-no-dt-for-cpu-v6-6-acd8727a1b59@linaro.org>

On Wed, 6 Mar 2024 at 12:42, Marcin Juszkiewicz
<marcin.juszkiewicz@linaro.org> wrote:
>
> From: Xiong Yining <xiongyining1480@phytium.com.cn>
>
> Provide functions to check for memory information:
>
> - amount of memory nodes
> - memory address
> - NUMA node id for memory
>
> Values are read from TF-A using platform specific SMC calls.
>
> Signed-off-by: Xiong Yining <xiongyining1480@phytium.com.cn>
> Signed-off-by: Chen Baozi <chenbaozi@phytium.com.cn>
> ---
>  .../SbsaQemu/Library/SbsaQemuLib/SbsaQemuLib.inf     |  2 +-
>  .../SbsaQemu/Include/IndustryStandard/SbsaQemuSmc.h  |  2 +
>  .../Include/Library/SbsaQemuHardwareInfoLib.h        | 28 ++++++++++
>  .../SbsaQemuHardwareInfoLib.c                        | 47 +++++++++++++++++
>  .../Qemu/SbsaQemu/Library/SbsaQemuLib/SbsaQemuMem.c  | 54 +++++---------------
>  5 files changed, 91 insertions(+), 42 deletions(-)
>
> diff --git a/Silicon/Qemu/SbsaQemu/Library/SbsaQemuLib/SbsaQemuLib.inf b/Silicon/Qemu/SbsaQemu/Library/SbsaQemuLib/SbsaQemuLib.inf
> index c067a80cc715..fb856efe4c27 100644
> --- a/Silicon/Qemu/SbsaQemu/Library/SbsaQemuLib/SbsaQemuLib.inf
> +++ b/Silicon/Qemu/SbsaQemu/Library/SbsaQemuLib/SbsaQemuLib.inf
> @@ -32,9 +32,9 @@ [LibraryClasses]
>    ArmLib
>    BaseMemoryLib
>    DebugLib
> -  FdtLib
>    MemoryAllocationLib
>    PcdLib
> +  SbsaQemuHardwareInfoLib
>
>  [Pcd]
>    gArmTokenSpaceGuid.PcdSystemMemoryBase
> diff --git a/Silicon/Qemu/SbsaQemu/Include/IndustryStandard/SbsaQemuSmc.h b/Silicon/Qemu/SbsaQemu/Include/IndustryStandard/SbsaQemuSmc.h
> index d9faee7fa5b2..e7bf54978d4e 100644
> --- a/Silicon/Qemu/SbsaQemu/Include/IndustryStandard/SbsaQemuSmc.h
> +++ b/Silicon/Qemu/SbsaQemu/Include/IndustryStandard/SbsaQemuSmc.h
> @@ -16,6 +16,8 @@
>  #define SIP_SVC_GET_GIC_ITS    SMC_SIP_FUNCTION_ID(101)
>  #define SIP_SVC_GET_CPU_COUNT  SMC_SIP_FUNCTION_ID(200)
>  #define SIP_SVC_GET_CPU_NODE   SMC_SIP_FUNCTION_ID(201)
> +#define SIP_SVC_GET_MEMORY_NODE_COUNT SMC_SIP_FUNCTION_ID(300)
> +#define SIP_SVC_GET_MEMORY_NODE SMC_SIP_FUNCTION_ID(301)
>
>  /*
>   *  SMCC does not define return codes for SiP functions.
> diff --git a/Silicon/Qemu/SbsaQemu/Include/Library/SbsaQemuHardwareInfoLib.h b/Silicon/Qemu/SbsaQemu/Include/Library/SbsaQemuHardwareInfoLib.h
> index ca52c6b27093..0b71a3f7e6eb 100644
> --- a/Silicon/Qemu/SbsaQemu/Include/Library/SbsaQemuHardwareInfoLib.h
> +++ b/Silicon/Qemu/SbsaQemu/Include/Library/SbsaQemuHardwareInfoLib.h
> @@ -9,6 +9,12 @@
>  #ifndef SBSA_QEMU_HARDWARE_INFO_
>  #define SBSA_QEMU_HARDWARE_INFO_
>
> +typedef struct{
> +  UINT32  NodeId;
> +  UINT64  AddressBase;
> +  UINT64  AddressSize;
> +} MemoryInfo;
> +
>  /**
>    Get CPU count from information passed by Qemu.
>
> @@ -42,4 +48,26 @@ SbsaQemuGetCpuNumaNode (
>    IN UINTN  CpuId
>    );
>
> +/**
> +  Get the number of memory node from device tree passed by Qemu.
> +
> +  @retval                   the number of memory nodes.
> +**/
> +UINT32
> +SbsaQemuGetMemNodeCount (
> +  VOID
> +  );
> +
> +/**
> +  Get memory infomation(node-id, addressbase, addresssize) for a given memory node from device tree passed by Qemu.
> +
> +  @param [in]   MemoryId    Index of memory to retrieve memory information.
> +
> +  @retval                   memory infomation for given memory node.
> +**/
> +MemoryInfo

Let's avoid aggregates as return types. Pass/return by reference
instead using an OUT pointer.


> +SbsaQemuGetMemInfo (
> +  IN UINTN   MemoryId
> +  );
> +
>  #endif /* SBSA_QEMU_HARDWARE_INFO_ */
> diff --git a/Silicon/Qemu/SbsaQemu/Library/SbsaQemuHardwareInfoLib/SbsaQemuHardwareInfoLib.c b/Silicon/Qemu/SbsaQemu/Library/SbsaQemuHardwareInfoLib/SbsaQemuHardwareInfoLib.c
> index e1f1a9588b19..88b50e46c072 100644
> --- a/Silicon/Qemu/SbsaQemu/Library/SbsaQemuHardwareInfoLib/SbsaQemuHardwareInfoLib.c
> +++ b/Silicon/Qemu/SbsaQemu/Library/SbsaQemuHardwareInfoLib/SbsaQemuHardwareInfoLib.c
> @@ -201,3 +201,50 @@ SbsaQemuGetCpuNumaNode (
>
>    return Arg0;
>  }
> +
> +UINT32
> +SbsaQemuGetMemNodeCount (
> +  VOID
> +  )
> +{
> +  UINTN            SmcResult;
> +  UINTN            Arg0;
> +
> +  SmcResult = ArmCallSmc0 (SIP_SVC_GET_MEMORY_NODE_COUNT, &Arg0, NULL, NULL);
> +  if (SmcResult != SMC_SIP_CALL_SUCCESS) {
> +    DEBUG ((DEBUG_ERROR, "SIP_SVC_GET_MEMORY_NODE_COUNT call failed.\n"));
> +  }
> +
> +  DEBUG(( DEBUG_INFO, "The number of the memory nodes is %ld\n", Arg0));
> +  return (UINT32)Arg0;
> +}
> +
> +MemoryInfo
> +SbsaQemuGetMemInfo (
> +  IN UINTN   MemoryId
> +  )
> +{
> +  UINTN           SmcResult;
> +  UINTN           Arg0;
> +  UINTN           Arg1;
> +  UINTN           Arg2;
> +  MemoryInfo      MemInfo;
> +
> +  Arg0 = MemoryId;
> +
> +  SmcResult = ArmCallSmc1 (SIP_SVC_GET_MEMORY_NODE, &Arg0, &Arg1, &Arg2);
> +  if (SmcResult != SMC_SIP_CALL_SUCCESS) {
> +    DEBUG ((DEBUG_ERROR, "SIP_SVC_GET_MEMORY_NODE call failed.\n"));
> +  } else {
> +    MemInfo.NodeId = Arg0;
> +    MemInfo.AddressBase = Arg1;
> +    MemInfo.AddressSize = Arg2;
> +  }
> +
> +  DEBUG(( DEBUG_INFO, "NUMA node for System RAM:%d = 0x%lx - 0x%lx\n",
> +      MemInfo.NodeId,
> +      MemInfo.AddressBase,
> +      MemInfo.AddressBase + MemInfo.AddressSize -1 ));
> +
> +  return MemInfo;
> +}
> diff --git a/Silicon/Qemu/SbsaQemu/Library/SbsaQemuLib/SbsaQemuMem.c b/Silicon/Qemu/SbsaQemu/Library/SbsaQemuLib/SbsaQemuMem.c
> index 8c2eb0b6a028..5a418a461174 100644
> --- a/Silicon/Qemu/SbsaQemu/Library/SbsaQemuLib/SbsaQemuMem.c
> +++ b/Silicon/Qemu/SbsaQemu/Library/SbsaQemuLib/SbsaQemuMem.c
> @@ -12,7 +12,7 @@
>  #include <Library/DebugLib.h>
>  #include <Library/MemoryAllocationLib.h>
>  #include <Library/PcdLib.h>
> -#include <libfdt.h>
> +#include <Library/SbsaQemuHardwareInfoLib.h>
>
>  // Number of Virtual Memory Map Descriptors
>  #define MAX_VIRTUAL_MEMORY_MAP_DESCRIPTORS          4
> @@ -23,53 +23,25 @@ SbsaQemuLibConstructor (
>    VOID
>    )
>  {
> -  VOID          *DeviceTreeBase;
> -  INT32         Node, Prev;
>    UINT64        NewBase, CurBase;
>    UINT64        NewSize, CurSize;
> -  CONST CHAR8   *Type;
> -  INT32         Len;
> -  CONST UINT64  *RegProp;
> +  UINT32        NumMemNodes;
> +  UINT32        Index;
> +  MemoryInfo    MemInfo;
>    RETURN_STATUS PcdStatus;
>
>    NewBase = 0;
>    NewSize = 0;
>
> -  DeviceTreeBase = (VOID *)(UINTN)PcdGet64 (PcdDeviceTreeBaseAddress);
> -  ASSERT (DeviceTreeBase != NULL);
> -
> -  // Make sure we have a valid device tree blob
> -  ASSERT (fdt_check_header (DeviceTreeBase) == 0);
> -
> -  // Look for the lowest memory node
> -  for (Prev = 0;; Prev = Node) {
> -    Node = fdt_next_node (DeviceTreeBase, Prev, NULL);
> -    if (Node < 0) {
> -      break;
> -    }
> -
> -    // Check for memory node
> -    Type = fdt_getprop (DeviceTreeBase, Node, "device_type", &Len);
> -    if (Type && AsciiStrnCmp (Type, "memory", Len) == 0) {
> -      // Get the 'reg' property of this node. For now, we will assume
> -      // two 8 byte quantities for base and size, respectively.
> -      RegProp = fdt_getprop (DeviceTreeBase, Node, "reg", &Len);
> -      if (RegProp != 0 && Len == (2 * sizeof (UINT64))) {
> -
> -        CurBase = fdt64_to_cpu (ReadUnaligned64 (RegProp));
> -        CurSize = fdt64_to_cpu (ReadUnaligned64 (RegProp + 1));
> -
> -        DEBUG ((DEBUG_INFO, "%a: System RAM @ 0x%lx - 0x%lx\n",
> -          __FUNCTION__, CurBase, CurBase + CurSize - 1));
> -
> -        if (NewBase > CurBase || NewBase == 0) {
> -          NewBase = CurBase;
> -          NewSize = CurSize;
> -        }
> -      } else {
> -        DEBUG ((DEBUG_ERROR, "%a: Failed to parse FDT memory node\n",
> -          __FUNCTION__));
> -      }
> +  NumMemNodes = SbsaQemuGetMemNodeCount();
> +  for(Index = 0; Index < NumMemNodes; Index++){
> +    MemInfo = SbsaQemuGetMemInfo(Index);
> +    CurBase = MemInfo.AddressBase;
> +    CurSize = MemInfo.AddressSize;
> +
> +    if (NewBase > CurBase || NewBase == 0) {
> +      NewBase = CurBase;
> +      NewSize = CurSize;
>      }
>    }
>
>
> --
> 2.44.0
>


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#116770): https://edk2.groups.io/g/devel/message/116770
Mute This Topic: https://groups.io/mt/104763768/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



  reply	other threads:[~2024-03-14 15:18 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-06 11:41 [edk2-devel] [PATCH edk2-platforms v6 0/7] get rid of DeviceTree from SbsaQemu Marcin Juszkiewicz
2024-03-06 11:41 ` [edk2-devel] [PATCH edk2-platforms v6 1/7] Platform/SbsaQemu: add SbsaQemuHardwareInfoLib Marcin Juszkiewicz
2024-03-14 15:09   ` Ard Biesheuvel
2024-03-14 15:14     ` Ard Biesheuvel
2024-03-15 11:34       ` Marcin Juszkiewicz
2024-03-06 11:41 ` [edk2-devel] [PATCH edk2-platforms v6 2/7] Platform/SbsaQemu: read amount of cpus during init Marcin Juszkiewicz
2024-03-14 15:13   ` Ard Biesheuvel
2024-03-15 11:49     ` Marcin Juszkiewicz
2024-03-19 10:25       ` Marcin Juszkiewicz
2024-03-19 11:02         ` Ard Biesheuvel
2024-03-19 11:27           ` Marcin Juszkiewicz
2024-03-06 11:42 ` [edk2-devel] [PATCH edk2-platforms v6 3/7] Platform/SbsaQemu: use PcdCoreCount directly Marcin Juszkiewicz
2024-03-14 15:15   ` Ard Biesheuvel
2024-03-06 11:42 ` [edk2-devel] [PATCH edk2-platforms v6 4/7] Platform/SbsaQemu: move FdtHandlerLib to SbsaQemuHardwareInfoLib Marcin Juszkiewicz
2024-03-14 15:16   ` Ard Biesheuvel
2024-03-06 11:42 ` [edk2-devel] [PATCH edk2-platforms v6 5/7] Platform/SbsaQemu: hang if there is no cpu information Marcin Juszkiewicz
2024-03-06 11:42 ` [edk2-devel] [PATCH edk2-platforms v6 6/7] Platform/SbsaQemu: get the information of memory via SMC calls Marcin Juszkiewicz
2024-03-14 15:18   ` Ard Biesheuvel [this message]
2024-03-06 11:42 ` [edk2-devel] [PATCH edk2-platforms v6 7/7] Platform/SbsaQemu: add DeviceTree fallbacks to parse memory information Marcin Juszkiewicz

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='CAMj1kXEgr7E8HD4pGs=wcnTEQ4YsoTu2X=4Wh3ewow48VrTRoQ@mail.gmail.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