libdrmutils: Retry drmOpen to handle delayed probe

Add retry mechanism to open drm master fd to handle case where
DSI probe has not yet completed. This is a common scenario with
dlkm

Change-Id: I41b3add9e594855aaa27ed319b0ea37203d96fc4
diff --git a/libdrmutils/drm_master.cpp b/libdrmutils/drm_master.cpp
index a417ac8..fc78dd2 100644
--- a/libdrmutils/drm_master.cpp
+++ b/libdrmutils/drm_master.cpp
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2017 - 2018, The Linux Foundation. All rights reserved.
+* Copyright (c) 2017 - 2018, 2021 The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
@@ -40,6 +40,8 @@
 #include <display/drm/sde_drm.h>
 #include <algorithm>
 #include <iterator>
+#include <chrono>
+#include <thread>
 
 #include "drm_master.h"
 
@@ -80,11 +82,17 @@
 }
 
 int DRMMaster::Init() {
-  dev_fd_ = drmOpen("msm_drm", nullptr);
-  if (dev_fd_ < 0) {
-    DRM_LOGE("drmOpen failed with error %d", dev_fd_);
-    return -ENODEV;
-  }
+  uint8_t retry = 0;
+  do {
+    dev_fd_ = drmOpen("msm_drm", nullptr);
+    if(dev_fd_ < 0) {
+      DRM_LOGE("drmOpen failed with error %d, retry %d", dev_fd_, retry);
+      if (retry >= MAX_RETRY) {
+        return -ENODEV;
+      }
+      std::this_thread::sleep_for(std::chrono::milliseconds(100));
+    }
+  } while(dev_fd_ < 0 && retry++ < MAX_RETRY);
 
   return 0;
 }
diff --git a/libdrmutils/drm_master.h b/libdrmutils/drm_master.h
index 18f51eb..8e9c7fc 100644
--- a/libdrmutils/drm_master.h
+++ b/libdrmutils/drm_master.h
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2017, The Linux Foundation. All rights reserved.
+* Copyright (c) 2017, 2021 The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
@@ -36,6 +36,8 @@
 
 namespace drm_utils {
 
+#define MAX_RETRY 10
+
 struct DRMBuffer {
   int fd = -1;
   uint32_t width = 0;