#include "dm_services_types.h"
 #include "dc.h"
 #include "dc_link_dp.h"
+#include "link_enc_cfg.h"
 #include "dc/inc/core_types.h"
 #include "dal_asic_id.h"
 #include "dmub/dmub_srv.h"
 void dmub_hpd_callback(struct amdgpu_device *adev, struct dmub_notification *notify)
 {
        struct amdgpu_dm_connector *aconnector;
+       struct amdgpu_dm_connector *hpd_aconnector = NULL;
        struct drm_connector *connector;
        struct drm_connector_list_iter iter;
        struct dc_link *link;
                aconnector = to_amdgpu_dm_connector(connector);
                if (link && aconnector->dc_link == link) {
                        DRM_INFO("DMUB HPD callback: link_index=%u\n", link_index);
-                       handle_hpd_irq_helper(aconnector);
+                       hpd_aconnector = aconnector;
                        break;
                }
        }
        drm_connector_list_iter_end(&iter);
        drm_modeset_unlock(&dev->mode_config.connection_mutex);
 
+       if (hpd_aconnector)
+               handle_hpd_irq_helper(hpd_aconnector);
 }
 
 /**
        struct dmcub_trace_buf_entry entry = { 0 };
        uint32_t count = 0;
        struct dmub_hpd_work *dmub_hpd_wrk;
+       struct dc_link *plink = NULL;
 
-       if (dc_enable_dmub_notifications(adev->dm.dc)) {
+       if (dc_enable_dmub_notifications(adev->dm.dc) &&
+               irq_params->irq_src == DC_IRQ_SOURCE_DMCUB_OUTBOX) {
                dmub_hpd_wrk = kzalloc(sizeof(*dmub_hpd_wrk), GFP_ATOMIC);
                if (!dmub_hpd_wrk) {
                        DRM_ERROR("Failed to allocate dmub_hpd_wrk");
                }
                INIT_WORK(&dmub_hpd_wrk->handle_hpd_work, dm_handle_hpd_work);
 
-               if (irq_params->irq_src == DC_IRQ_SOURCE_DMCUB_OUTBOX) {
-                       do {
-                               dc_stat_get_dmub_notification(adev->dm.dc, ¬ify);
-                               if (notify.type > ARRAY_SIZE(dm->dmub_thread_offload)) {
-                                       DRM_ERROR("DM: notify type %d larger than the array size %zu!", notify.type,
-                                       ARRAY_SIZE(dm->dmub_thread_offload));
-                                       continue;
-                               }
-                               if (dm->dmub_thread_offload[notify.type] == true) {
-                                       dmub_hpd_wrk->dmub_notify = ¬ify;
-                                       dmub_hpd_wrk->adev = adev;
-                                       queue_work(adev->dm.delayed_hpd_wq, &dmub_hpd_wrk->handle_hpd_work);
-                               } else {
-                                       dm->dmub_callback[notify.type](adev, ¬ify);
+               do {
+                       dc_stat_get_dmub_notification(adev->dm.dc, ¬ify);
+                       if (notify.type > ARRAY_SIZE(dm->dmub_thread_offload)) {
+                               DRM_ERROR("DM: notify type %d invalid!", notify.type);
+                               continue;
+                       }
+                       if (dm->dmub_thread_offload[notify.type] == true) {
+                               dmub_hpd_wrk->dmub_notify = ¬ify;
+                               dmub_hpd_wrk->adev = adev;
+                               if (notify.type == DMUB_NOTIFICATION_HPD) {
+                                       plink = adev->dm.dc->links[notify.link_index];
+                                       if (plink) {
+                                               plink->hpd_status =
+                                                       notify.hpd_status ==
+                                                       DP_HPD_PLUG ? true : false;
+                                       }
                                }
-
-                       } while (notify.pending_notification);
-
-               } else {
-                       DRM_ERROR("DM: Failed to receive correct outbox IRQ !");
-               }
+                               queue_work(adev->dm.delayed_hpd_wq, &dmub_hpd_wrk->handle_hpd_work);
+                       } else {
+                               dm->dmub_callback[notify.type](adev, ¬ify);
+                       }
+               } while (notify.pending_notification);
        }
 
 
 
        } while (count <= DMUB_TRACE_MAX_READ);
 
-       ASSERT(count <= DMUB_TRACE_MAX_READ);
+       if (count > DMUB_TRACE_MAX_READ)
+               DRM_DEBUG_DRIVER("Warning : count > DMUB_TRACE_MAX_READ");
 }
 #endif
 
        if (aconnector->base.force && new_connection_type == dc_connection_none) {
                emulated_link_detect(aconnector->dc_link);
 
-
                drm_modeset_lock_all(dev);
                dm_restore_drm_connector_state(dev, connector);
                drm_modeset_unlock_all(dev);
                break;
        case DRM_MODE_CONNECTOR_DisplayPort:
                aconnector->base.polled = DRM_CONNECTOR_POLL_HPD;
-               aconnector->base.ycbcr_420_allowed =
+               if (link->is_dig_mapping_flexible &&
+                   link->dc->res_pool->funcs->link_encs_assign) {
+                       link->link_enc =
+                               link_enc_cfg_get_link_enc_used_by_link(link->ctx->dc, link);
+                       if (!link->link_enc)
+                               link->link_enc =
+                                       link_enc_cfg_get_next_avail_link_enc(link->ctx->dc);
+               }
+
+               if (link->link_enc)
+                       aconnector->base.ycbcr_420_allowed =
                        link->link_enc->features.dp_ycbcr420_supported ? true : false;
                break;
        case DRM_MODE_CONNECTOR_DVID:
        snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d", link_index);
        i2c_set_adapdata(&i2c->base, i2c);
        i2c->ddc_service = ddc_service;
-       i2c->ddc_service->ddc_pin->hw_info.ddc_channel = link_index;
+       if (i2c->ddc_service->ddc_pin)
+               i2c->ddc_service->ddc_pin->hw_info.ddc_channel = link_index;
 
        return i2c;
 }