/* Copyright (c) 2019 - 2021 Advanced Micro Devices, Inc.

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE. */
#ifndef NCCL_HSA_EXTRA_H_
#define NCCL_HSA_EXTRA_H_
#include "hsa/hsa.h"
#include <dlfcn.h>
#include <mutex>

#ifdef HYGON_SDMA_FEATURE
#ifdef DEFINE_SDMA_STRUCT
typedef struct hsa_sdma_info_s {
  uint32_t queue_id;
  uint32_t dep_signal;
  uint32_t completion_signal;
  uint32_t data_size;
  uint64_t src_addr;
  uint64_t dst_addr;
  uint64_t start_ts;
  uint64_t end_ts;
  uint64_t *wptr;
  uint64_t *rptr;
  uint64_t reserved[8];
} hsa_sdma_info_t;

typedef enum hsa_sdma_group_queue_flag_s {
  HSA_SDMA_GROUP_QUEUE_FLAG_DEFAULT   = 0,
  HSA_SDMA_GROUP_QUEUE_FLAG_PROFILING = 1
} hsa_sdma_group_queue_flag_t;

#define HSA_MAX_SDMA_QUEUE_NUM  7

typedef struct hsa_sdma_group_queue_s {
  uint32_t queue_count;
  hsa_sdma_info_t *sdma_info[HSA_MAX_SDMA_QUEUE_NUM];
} hsa_sdma_group_queue_t;

hsa_status_t hsa_ext_submit_sdma_task(hsa_agent_t agent, uint32_t count);

hsa_status_t hsa_ext_get_xhcl_link_count(hsa_agent_t src_agent,
  hsa_agent_t dst_agent, uint32_t *link_count);

hsa_status_t hsa_ext_create_sdma_group_queue(hsa_agent_t src_agent,
  hsa_agent_t dst_agent, uint32_t size, uint32_t flag,
  hsa_sdma_group_queue_t *group_queue);

hsa_status_t hsa_ext_destroy_sdma_group_queue(hsa_agent_t agent);

#endif

typedef hsa_status_t (*hsa_ext_submit_sdma_task_t)(hsa_agent_t agent, int flags);
typedef hsa_status_t (*hsa_ext_destroy_sdma_group_queue_t)(hsa_agent_t agent);
typedef hsa_status_t (*hsa_agent_get_info_t)(hsa_agent_t agent,hsa_agent_info_t attribute,void* value);
typedef hsa_status_t (*hsa_agent_callback_t)(hsa_agent_t agent, void* data);
typedef hsa_status_t (*hsa_iterate_agents_t)(hsa_status_t (*callback)(hsa_agent_t agent, void* data),void* data);
typedef hsa_status_t (*hsa_ext_get_xhcl_link_count_t)(hsa_agent_t src_agent,hsa_agent_t dst_agent, uint32_t *link_count);
typedef hsa_status_t (*hsa_ext_create_sdma_group_queue_t)(hsa_agent_t src_agent,hsa_agent_t dst_agent, uint32_t size, uint32_t flag,hsa_sdma_group_queue_t *group_queue);

static void *hsaLib = nullptr;
static std::mutex hsaMtx;

static void* loadHsaLib() {
  char path[1024];
  char *ncclRocrPath = getenv("RCCL_ROCR_PATH");

  if (ncclRocrPath == NULL) {
    snprintf(path, sizeof(path), "libhsa-runtime64.so");
  } else {
    snprintf(path, sizeof(path), "%s/%s", ncclRocrPath, "libhsa-runtime64.so");
  }

  void* hsaLib = dlopen(path, RTLD_LAZY);
  if (hsaLib == nullptr) {
    const char *error = dlerror();  
    fprintf(stderr, "Failed to find ROCm runtime library in %s (RCCL_ROCR_PATH=%s): %s\n",
      path, ncclRocrPath, error ? error : "Unknown error");
    return nullptr;
  }

  return hsaLib;
}

static void* getHsaLib() {
  if (hsaLib == nullptr) {    
    std::lock_guard<std::mutex> lock(hsaMtx);
    if (hsaLib == nullptr) {
      hsaLib = loadHsaLib();
    }
  }

  return hsaLib;
}

#endif
#endif 
