sdm: drm: Add error handling

Add error checking and handling around libdrm APIs.

CRs-Fixed: 2236754
Change-Id: Iae42919c05b6e31b02fc8ecaf052349211b66418
diff --git a/libdrmutils/drm_interface.h b/libdrmutils/drm_interface.h
index b71a097..73e530b 100644
--- a/libdrmutils/drm_interface.h
+++ b/libdrmutils/drm_interface.h
@@ -795,14 +795,16 @@
    * Will provide all the information of a selected crtc.
    * [input]: Use crtc id 0 to obtain system wide info
    * [output]: DRMCrtcInfo: Resource Info for the given CRTC id.
+   * [return]: 0 on success, a negative error value otherwise.
    */
-  virtual void GetCrtcInfo(uint32_t crtc_id, DRMCrtcInfo *info) = 0;
+  virtual int GetCrtcInfo(uint32_t crtc_id, DRMCrtcInfo *info) = 0;
 
   /*
    * Will provide all the information of a selected connector.
    * [output]: DRMConnectorInfo: Resource Info for the given connector id
+   * [return]: 0 on success, a negative error value otherwise.
    */
-  virtual void GetConnectorInfo(uint32_t conn_id, DRMConnectorInfo *info) = 0;
+  virtual int GetConnectorInfo(uint32_t conn_id, DRMConnectorInfo *info) = 0;
 
   /*
    * Will query post propcessing feature info of a CRTC.
diff --git a/sdm/libs/core/drm/hw_device_drm.cpp b/sdm/libs/core/drm/hw_device_drm.cpp
index 1401aef..dca25db 100644
--- a/sdm/libs/core/drm/hw_device_drm.cpp
+++ b/sdm/libs/core/drm/hw_device_drm.cpp
@@ -381,6 +381,7 @@
 }
 
 DisplayError HWDeviceDRM::Init() {
+  int ret = 0;
   DRMMaster *drm_master = {};
   DRMMaster::GetInstance(&drm_master);
   drm_master->GetHandle(&dev_fd_);
@@ -391,8 +392,36 @@
     return kErrorResources;
   }
 
-  drm_mgr_intf_->CreateAtomicReq(token_, &drm_atomic_intf_);
-  drm_mgr_intf_->GetConnectorInfo(token_.conn_id, &connector_info_);
+  if (token_.conn_id > INT32_MAX) {
+    DLOGE("Connector id %u beyond supported range", token_.conn_id);
+    drm_mgr_intf_->UnregisterDisplay(token_);
+    return kErrorNotSupported;
+  }
+
+  ret = drm_mgr_intf_->CreateAtomicReq(token_, &drm_atomic_intf_);
+  if (ret) {
+    DLOGE("Failed creating atomic request for connector id %u. Error: %d.", token_.conn_id, ret);
+    drm_mgr_intf_->UnregisterDisplay(token_);
+    return kErrorResources;
+  }
+
+  ret = drm_mgr_intf_->GetConnectorInfo(token_.conn_id, &connector_info_);
+  if (ret) {
+    DLOGE("Failed getting info for connector id %u. Error: %d.", token_.conn_id, ret);
+    drm_mgr_intf_->DestroyAtomicReq(drm_atomic_intf_);
+    drm_atomic_intf_ = {};
+    drm_mgr_intf_->UnregisterDisplay(token_);
+    return kErrorHardware;
+  }
+
+  if (connector_info_.modes.empty()) {
+    DLOGE("Critical error: Zero modes on connector id %u.", token_.conn_id);
+    drm_mgr_intf_->DestroyAtomicReq(drm_atomic_intf_);
+    drm_atomic_intf_ = {};
+    drm_mgr_intf_->UnregisterDisplay(token_);
+    return kErrorHardware;
+  }
+
   hw_info_intf_->GetHWResourceInfo(&hw_resource_);
 
   InitializeConfigs();
diff --git a/sdm/libs/core/drm/hw_info_drm.cpp b/sdm/libs/core/drm/hw_info_drm.cpp
index 0576b99..a3b6d4f 100644
--- a/sdm/libs/core/drm/hw_info_drm.cpp
+++ b/sdm/libs/core/drm/hw_info_drm.cpp
@@ -470,14 +470,22 @@
   HWSubBlockType sub_blk_type = kHWWBIntfOutput;
   vector<LayerBufferFormat> supported_sdm_formats;
   sde_drm::DRMDisplayToken token;
+  int ret = 0;
 
   // Fake register
-  if (drm_mgr_intf_->RegisterDisplay(sde_drm::DRMDisplayType::VIRTUAL, &token)) {
+  ret = drm_mgr_intf_->RegisterDisplay(sde_drm::DRMDisplayType::VIRTUAL, &token);
+  if (ret) {
+    DLOGE("Failed registering display %d. Error: %d.", sde_drm::DRMDisplayType::VIRTUAL, ret);
     return;
   }
 
   sde_drm::DRMConnectorInfo connector_info;
-  drm_mgr_intf_->GetConnectorInfo(token.conn_id, &connector_info);
+  ret = drm_mgr_intf_->GetConnectorInfo(token.conn_id, &connector_info);
+  if (ret) {
+    DLOGE("Failed getting info for connector id %u. Error: %d.", token.conn_id, ret);
+    drm_mgr_intf_->UnregisterDisplay(token);
+    return;
+  }
   for (auto &fmts : connector_info.formats_supported) {
     GetSDMFormat(fmts.first, fmts.second, &supported_sdm_formats);
   }
diff --git a/sdm/libs/core/drm/hw_virtual_drm.cpp b/sdm/libs/core/drm/hw_virtual_drm.cpp
index 5013181..20d27f9 100644
--- a/sdm/libs/core/drm/hw_virtual_drm.cpp
+++ b/sdm/libs/core/drm/hw_virtual_drm.cpp
@@ -163,6 +163,7 @@
   }
 
   int mode_index = -1;
+  int ret = 0;
   GetModeIndex(display_attributes, &mode_index);
 
   if (mode_index < 0) {
@@ -173,7 +174,11 @@
   }
 
   // Reload connector info for updated info
-  drm_mgr_intf_->GetConnectorInfo(token_.conn_id, &connector_info_);
+  ret = drm_mgr_intf_->GetConnectorInfo(token_.conn_id, &connector_info_);
+  if (ret) {
+    DLOGE("Failed getting info for connector id %u. Error: %d.", token_.conn_id, ret);
+    return kErrorHardware;
+  }
   GetModeIndex(display_attributes, &mode_index);
 
   if (mode_index < 0) {