]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ArmVirtPkg/FdtClientDxe/FdtClientDxe.c
ArmVirtPkg/FdtClientDxe: take DT node 'status' properties into account
[mirror_edk2.git] / ArmVirtPkg / FdtClientDxe / FdtClientDxe.c
index 9c589e620cb40c7d542a85d4b8e26c05d9db2d7a..5bfde381ecd0d53996272771bdee6ebe5d5ada96 100644 (file)
@@ -20,7 +20,9 @@
 #include <Library/HobLib.h>\r
 #include <libfdt.h>\r
 \r
+#include <Guid/Fdt.h>\r
 #include <Guid/FdtHob.h>\r
+#include <Guid/PlatformHasDeviceTree.h>\r
 \r
 #include <Protocol/FdtClient.h>\r
 \r
@@ -28,6 +30,7 @@ STATIC VOID  *mDeviceTreeBase;
 \r
 STATIC\r
 EFI_STATUS\r
+EFIAPI\r
 GetNodeProperty (\r
   IN  FDT_CLIENT_PROTOCOL     *This,\r
   IN  INT32                   Node,\r
@@ -54,6 +57,7 @@ GetNodeProperty (
 \r
 STATIC\r
 EFI_STATUS\r
+EFIAPI\r
 SetNodeProperty (\r
   IN  FDT_CLIENT_PROTOCOL     *This,\r
   IN  INT32                   Node,\r
@@ -74,6 +78,33 @@ SetNodeProperty (
   return EFI_SUCCESS;\r
 }\r
 \r
+STATIC\r
+BOOLEAN\r
+IsNodeEnabled (\r
+  INT32                       Node\r
+  )\r
+{\r
+  CONST CHAR8   *NodeStatus;\r
+  INT32         Len;\r
+\r
+  //\r
+  // A missing status property implies 'ok' so ignore any errors that\r
+  // may occur here. If the status property is present, check whether\r
+  // it is set to 'ok' or 'okay', anything else is treated as 'disabled'.\r
+  //\r
+  NodeStatus = fdt_getprop (mDeviceTreeBase, Node, "status", &Len);\r
+  if (NodeStatus == NULL) {\r
+    return TRUE;\r
+  }\r
+  if (Len >= 5 && AsciiStrCmp (NodeStatus, "okay") == 0) {\r
+    return TRUE;\r
+  }\r
+  if (Len >= 3 && AsciiStrCmp (NodeStatus, "ok") == 0) {\r
+    return TRUE;\r
+  }\r
+  return FALSE;\r
+}\r
+\r
 STATIC\r
 EFI_STATUS\r
 EFIAPI\r
@@ -97,6 +128,10 @@ FindNextCompatibleNode (
       break;\r
     }\r
 \r
+    if (!IsNodeEnabled (Next)) {\r
+      continue;\r
+    }\r
+\r
     Type = fdt_getprop (mDeviceTreeBase, Next, "compatible", &Len);\r
     if (Type == NULL) {\r
       continue;\r
@@ -158,7 +193,8 @@ FindCompatibleNodeReg (
   IN  FDT_CLIENT_PROTOCOL     *This,\r
   IN  CONST CHAR8             *CompatibleString,\r
   OUT CONST VOID              **Reg,\r
-  OUT UINT32                  *RegElemSize,\r
+  OUT UINTN                   *AddressCells,\r
+  OUT UINTN                   *SizeCells,\r
   OUT UINT32                  *RegSize\r
   )\r
 {\r
@@ -177,20 +213,100 @@ FindCompatibleNodeReg (
     return Status;\r
   }\r
 \r
-  if ((*RegSize % 8) != 0) {\r
+  if ((*RegSize % 16) != 0) {\r
     DEBUG ((EFI_D_ERROR,\r
       "%a: '%a' compatible node has invalid 'reg' property (size == 0x%x)\n",\r
       __FUNCTION__, CompatibleString, *RegSize));\r
     return EFI_NOT_FOUND;\r
   }\r
 \r
-  *RegElemSize = 8;\r
+  *AddressCells = 2;\r
+  *SizeCells = 2;\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
 STATIC\r
 EFI_STATUS\r
+EFIAPI\r
+FindNextMemoryNodeReg (\r
+  IN  FDT_CLIENT_PROTOCOL     *This,\r
+  IN  INT32                   PrevNode,\r
+  OUT INT32                   *Node,\r
+  OUT CONST VOID              **Reg,\r
+  OUT UINTN                   *AddressCells,\r
+  OUT UINTN                   *SizeCells,\r
+  OUT UINT32                  *RegSize\r
+  )\r
+{\r
+  INT32          Prev, Next;\r
+  CONST CHAR8    *DeviceType;\r
+  INT32          Len;\r
+  EFI_STATUS     Status;\r
+\r
+  ASSERT (mDeviceTreeBase != NULL);\r
+  ASSERT (Node != NULL);\r
+\r
+  for (Prev = PrevNode;; Prev = Next) {\r
+    Next = fdt_next_node (mDeviceTreeBase, Prev, NULL);\r
+    if (Next < 0) {\r
+      break;\r
+    }\r
+\r
+    if (!IsNodeEnabled (Next)) {\r
+      DEBUG ((DEBUG_WARN, "%a: ignoring disabled memory node\n", __FUNCTION__));\r
+      continue;\r
+    }\r
+\r
+    DeviceType = fdt_getprop (mDeviceTreeBase, Next, "device_type", &Len);\r
+    if (DeviceType != NULL && AsciiStrCmp (DeviceType, "memory") == 0) {\r
+      //\r
+      // Get the 'reg' property of this memory node. For now, we will assume\r
+      // 8 byte quantities for base and size, respectively.\r
+      // TODO use #cells root properties instead\r
+      //\r
+      Status = GetNodeProperty (This, Next, "reg", Reg, RegSize);\r
+      if (EFI_ERROR (Status)) {\r
+        DEBUG ((EFI_D_WARN,\r
+          "%a: ignoring memory node with no 'reg' property\n",\r
+          __FUNCTION__));\r
+        continue;\r
+      }\r
+      if ((*RegSize % 16) != 0) {\r
+        DEBUG ((EFI_D_WARN,\r
+          "%a: ignoring memory node with invalid 'reg' property (size == 0x%x)\n",\r
+          __FUNCTION__, *RegSize));\r
+        continue;\r
+      }\r
+\r
+      *Node = Next;\r
+      *AddressCells = 2;\r
+      *SizeCells = 2;\r
+      return EFI_SUCCESS;\r
+    }\r
+  }\r
+  return EFI_NOT_FOUND;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+FindMemoryNodeReg (\r
+  IN  FDT_CLIENT_PROTOCOL     *This,\r
+  OUT INT32                   *Node,\r
+  OUT CONST VOID              **Reg,\r
+  OUT UINTN                   *AddressCells,\r
+  OUT UINTN                   *SizeCells,\r
+  OUT UINT32                  *RegSize\r
+  )\r
+{\r
+  return FindNextMemoryNodeReg (This, 0, Node, Reg, AddressCells, SizeCells,\r
+           RegSize);\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
 GetOrInsertChosenNode (\r
   IN  FDT_CLIENT_PROTOCOL     *This,\r
   OUT INT32                   *Node\r
@@ -222,9 +338,45 @@ STATIC FDT_CLIENT_PROTOCOL mFdtClientProtocol = {
   FindNextCompatibleNode,\r
   FindCompatibleNodeProperty,\r
   FindCompatibleNodeReg,\r
+  FindMemoryNodeReg,\r
+  FindNextMemoryNodeReg,\r
   GetOrInsertChosenNode,\r
 };\r
 \r
+STATIC\r
+VOID\r
+EFIAPI\r
+OnPlatformHasDeviceTree (\r
+  IN EFI_EVENT Event,\r
+  IN VOID      *Context\r
+  )\r
+{\r
+  EFI_STATUS Status;\r
+  VOID       *Interface;\r
+  VOID       *DeviceTreeBase;\r
+\r
+  Status = gBS->LocateProtocol (\r
+                  &gEdkiiPlatformHasDeviceTreeGuid,\r
+                  NULL,                             // Registration\r
+                  &Interface\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return;\r
+  }\r
+\r
+  DeviceTreeBase = Context;\r
+  DEBUG ((\r
+    DEBUG_INFO,\r
+    "%a: exposing DTB @ 0x%p to OS\n",\r
+    __FUNCTION__,\r
+    DeviceTreeBase\r
+    ));\r
+  Status = gBS->InstallConfigurationTable (&gFdtTableGuid, DeviceTreeBase);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  gBS->CloseEvent (Event);\r
+}\r
+\r
 EFI_STATUS\r
 EFIAPI\r
 InitializeFdtClientDxe (\r
@@ -234,6 +386,9 @@ InitializeFdtClientDxe (
 {\r
   VOID              *Hob;\r
   VOID              *DeviceTreeBase;\r
+  EFI_STATUS        Status;\r
+  EFI_EVENT         PlatformHasDeviceTreeEvent;\r
+  VOID              *Registration;\r
 \r
   Hob = GetFirstGuidHob (&gFdtHobGuid);\r
   if (Hob == NULL || GET_GUID_HOB_DATA_SIZE (Hob) != sizeof (UINT64)) {\r
@@ -251,6 +406,65 @@ InitializeFdtClientDxe (
 \r
   DEBUG ((EFI_D_INFO, "%a: DTB @ 0x%p\n", __FUNCTION__, mDeviceTreeBase));\r
 \r
-  return gBS->InstallProtocolInterface (&ImageHandle, &gFdtClientProtocolGuid,\r
-                EFI_NATIVE_INTERFACE, &mFdtClientProtocol);\r
+  //\r
+  // Register a protocol notify for the EDKII Platform Has Device Tree\r
+  // Protocol.\r
+  //\r
+  Status = gBS->CreateEvent (\r
+                  EVT_NOTIFY_SIGNAL,\r
+                  TPL_CALLBACK,\r
+                  OnPlatformHasDeviceTree,\r
+                  DeviceTreeBase,             // Context\r
+                  &PlatformHasDeviceTreeEvent\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((DEBUG_ERROR, "%a: CreateEvent(): %r\n", __FUNCTION__, Status));\r
+    return Status;\r
+  }\r
+\r
+  Status = gBS->RegisterProtocolNotify (\r
+                  &gEdkiiPlatformHasDeviceTreeGuid,\r
+                  PlatformHasDeviceTreeEvent,\r
+                  &Registration\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((\r
+      DEBUG_ERROR,\r
+      "%a: RegisterProtocolNotify(): %r\n",\r
+      __FUNCTION__,\r
+      Status\r
+      ));\r
+    goto CloseEvent;\r
+  }\r
+\r
+  //\r
+  // Kick the event; the protocol could be available already.\r
+  //\r
+  Status = gBS->SignalEvent (PlatformHasDeviceTreeEvent);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((DEBUG_ERROR, "%a: SignalEvent(): %r\n", __FUNCTION__, Status));\r
+    goto CloseEvent;\r
+  }\r
+\r
+  Status = gBS->InstallProtocolInterface (\r
+                  &ImageHandle,\r
+                  &gFdtClientProtocolGuid,\r
+                  EFI_NATIVE_INTERFACE,\r
+                  &mFdtClientProtocol\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((\r
+      DEBUG_ERROR,\r
+      "%a: InstallProtocolInterface(): %r\n",\r
+      __FUNCTION__,\r
+      Status\r
+      ));\r
+    goto CloseEvent;\r
+  }\r
+\r
+  return Status;\r
+\r
+CloseEvent:\r
+  gBS->CloseEvent (PlatformHasDeviceTreeEvent);\r
+  return Status;\r
 }\r