public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Marcin Juszkiewicz" <marcin.juszkiewicz@linaro.org>
To: devel@edk2.groups.io
Cc: 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>,
	 Marcin Juszkiewicz <marcin.juszkiewicz@linaro.org>
Subject: [edk2-devel] [PATCH edk2-platforms v6 7/7] Platform/SbsaQemu: add DeviceTree fallbacks to parse memory information
Date: Wed, 06 Mar 2024 03:42:06 -0800	[thread overview]
Message-ID: <20240306-no-dt-for-cpu-v6-7-acd8727a1b59@linaro.org> (raw)
In-Reply-To: <20240306-no-dt-for-cpu-v6-0-acd8727a1b59@linaro.org>

From: Xiong Yining <xiongyining1480@phytium.com.cn>

Add the DeviceTree fallbacks to parsing the information about memory
if the related SMC calls Failed.

Signed-off-by: Xiong Yining <xiongyining1480@phytium.com.cn>
Signed-off-by: Chen Baozi <chenbaozi@phytium.com.cn>
---
 .../SbsaQemuHardwareInfoLib.c                       | 106 +++++++++++++++++++-
 1 file changed, 104 insertions(+), 2 deletions(-)

diff --git a/Silicon/Qemu/SbsaQemu/Library/SbsaQemuHardwareInfoLib/SbsaQemuHardwareInfoLib.c b/Silicon/Qemu/SbsaQemu/Library/SbsaQemuHardwareInfoLib/SbsaQemuHardwareInfoLib.c
index 88b50e46c072..d63cfbf7d5ef 100644
--- a/Silicon/Qemu/SbsaQemu/Library/SbsaQemuHardwareInfoLib/SbsaQemuHardwareInfoLib.c
+++ b/Silicon/Qemu/SbsaQemu/Library/SbsaQemuHardwareInfoLib/SbsaQemuHardwareInfoLib.c
@@ -118,6 +118,106 @@ FdtHelperCountCpus (
   return CpuCount;
 }
 
+/**
+  Walks through the Device Tree created by Qemu and counts the number of Memory node present in it.
+
+  @retval                   the number of memory nodes.
+**/
+UINT32
+FdtHelperMemNodeCount (
+  VOID
+  )
+{
+  VOID          *DeviceTreeBase;
+  CONST CHAR8   *Type;
+  UINT32        MemNodeCount;
+  INT32         Node;
+  INT32         Prev;
+  INT32         Len;
+
+  DeviceTreeBase = (VOID *)(UINTN)PcdGet64 (PcdDeviceTreeBaseAddress);
+  ASSERT (DeviceTreeBase != NULL);
+
+  // Make sure we have a valid device tree blob
+  ASSERT (fdt_check_header (DeviceTreeBase) == 0);
+
+  MemNodeCount = 0;
+  for (Prev = 0;; Prev = Node) {
+    Node = fdt_next_node (DeviceTreeBase, Prev, NULL);
+    if (Node < 0){
+      break;
+    }
+
+    Type = fdt_getprop (DeviceTreeBase, Node, "device_type", &Len);
+    if (Type && AsciiStrnCmp (Type, "memory", Len) == 0) {
+      MemNodeCount++;
+    }
+  }
+
+  return MemNodeCount;
+}
+
+/** Walks through the Device Tree created by Qemu and counts the adress、size and node-id of the Memory node present in it.
+
+  @param [in]   MemoryId    Index of memory to retrieve memory information.
+
+  @retval                   memory infomation for given memory node.
+**/
+MemoryInfo
+FdtHelperGetMemInfo (
+  IN UINTN   MemoryId
+)
+{
+  VOID          *DeviceTreeBase;
+  CONST UINT64  *RegProp;
+  CONST UINT32  *NodeProp;
+  CONST CHAR8   *Type;
+  INT64         MemNodeCount;
+  INT32         Len;
+  INT32         Node;
+  INT32         Prev;
+  MemoryInfo    MemInfo = {0};
+
+  DeviceTreeBase = (VOID *)(UINTN)PcdGet64 (PcdDeviceTreeBaseAddress);
+  ASSERT (DeviceTreeBase != NULL);
+
+  // Make sure we have a valid device tree blob
+  ASSERT (fdt_check_header (DeviceTreeBase) == 0);
+
+  MemNodeCount = 0;
+
+  for (Prev = 0;; Prev = Node){
+    Node = fdt_next_node (DeviceTreeBase, Prev, NULL);
+    if (Node < 0){
+      break;
+    }
+
+    Type = fdt_getprop (DeviceTreeBase, Node, "device_type", &Len);
+    if (Type && AsciiStrnCmp (Type, "memory", Len) == 0){
+      if (MemoryId == MemNodeCount){
+        RegProp = fdt_getprop (DeviceTreeBase, Node, "reg", &Len);
+        if (RegProp != 0 && Len == (2 * sizeof (UINT64))) {
+            MemInfo.AddressBase = fdt64_to_cpu (ReadUnaligned64 (RegProp));
+            MemInfo.AddressSize= fdt64_to_cpu (ReadUnaligned64 (RegProp + 1));
+        } else {
+          DEBUG ((DEBUG_ERROR, "Failed to find the address and size of the memory"));
+        }
+
+        NodeProp= fdt_getprop (DeviceTreeBase, Node, "numa-node-id", &Len);
+        if (NodeProp){
+          MemInfo.NodeId= fdt32_to_cpu (ReadUnaligned32 (NodeProp));
+        } else {
+          DEBUG ((DEBUG_ERROR, "Failed to find the node-id of the memory"));
+        }
+      }
+
+      MemNodeCount ++;
+    }
+  }
+
+  return MemInfo;
+}
+
 /**
   Get CPU count from information passed by Qemu.
 
@@ -212,7 +312,8 @@ SbsaQemuGetMemNodeCount (
 
   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_ERROR, "SIP_SVC_GET_MEMORY_NODE_COUNT call failed. We have to get the number of memory nodes from DT.\n"));
+    Arg0 = FdtHelperMemNodeCount();
   }
 
   DEBUG(( DEBUG_INFO, "The number of the memory nodes is %ld\n", Arg0));
@@ -234,7 +335,8 @@ SbsaQemuGetMemInfo (
 
   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"));
+    DEBUG ((DEBUG_ERROR, "SIP_SVC_GET_MEMORY_NODE call failed. We have to get memory info from DT.\n"));
+    MemInfo = FdtHelperGetMemInfo(MemoryId);
   } else {
     MemInfo.NodeId = Arg0;
     MemInfo.AddressBase = Arg1;

-- 
2.44.0



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



      parent reply	other threads:[~2024-03-06 11:42 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
2024-03-06 11:42 ` Marcin Juszkiewicz [this message]

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=20240306-no-dt-for-cpu-v6-7-acd8727a1b59@linaro.org \
    --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