/*
 * Copyright © 2022 Imagination Technologies Ltd.
 *
 * 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 (including the next
 * paragraph) 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.
 */

#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <xf86drm.h>

#include "pvr_csb.h"
#include "pvr_device_info.h"
#include "pvr_private.h"
#include "pvr_srv.h"
#include "pvr_srv_bo.h"
#include "pvr_srv_bridge.h"
#include "pvr_srv_job_compute.h"
#include "pvr_srv_job_render.h"
#include "pvr_srv_public.h"
#include "pvr_srv_syncobj.h"
#include "pvr_winsys.h"
#include "pvr_winsys_helper.h"
#include "util/log.h"
#include "util/os_misc.h"
#include "vk_log.h"

/* Amount of space used to hold sync prim values (in bytes). */
#define PVR_SRV_SYNC_PRIM_VALUE_SIZE 4U

static VkResult pvr_srv_heap_init(
   struct pvr_srv_winsys *srv_ws,
   struct pvr_srv_winsys_heap *srv_heap,
   uint32_t heap_idx,
   const struct pvr_winsys_static_data_offsets *const static_data_offsets)
{
   pvr_dev_addr_t base_address;
   uint32_t log2_page_size;
   uint64_t reserved_size;
   VkResult result;
   uint64_t size;

   result = pvr_srv_get_heap_details(srv_ws->render_fd,
                                     heap_idx,
                                     0,
                                     NULL,
                                     &base_address,
                                     &size,
                                     &reserved_size,
                                     &log2_page_size);
   if (result != VK_SUCCESS)
      return result;

   result = pvr_winsys_helper_winsys_heap_init(&srv_ws->base,
                                               base_address,
                                               size,
                                               base_address,
                                               reserved_size,
                                               log2_page_size,
                                               static_data_offsets,
                                               &srv_heap->base);
   if (result != VK_SUCCESS)
      return result;

   assert(srv_heap->base.page_size == srv_ws->base.page_size);
   assert(srv_heap->base.log2_page_size == srv_ws->base.log2_page_size);
   assert(srv_heap->base.reserved_size % PVR_SRV_RESERVED_SIZE_GRANULARITY ==
          0);

   /* Create server-side counterpart of Device Memory heap */
   result = pvr_srv_int_heap_create(srv_ws->render_fd,
                                    srv_heap->base.base_addr,
                                    srv_heap->base.size,
                                    srv_heap->base.log2_page_size,
                                    srv_ws->server_memctx,
                                    &srv_heap->server_heap);
   if (result != VK_SUCCESS) {
      pvr_winsys_helper_winsys_heap_finish(&srv_heap->base);
      return result;
   }

   return VK_SUCCESS;
}

static bool pvr_srv_heap_finish(struct pvr_srv_winsys *srv_ws,
                                struct pvr_srv_winsys_heap *srv_heap)
{
   if (!pvr_winsys_helper_winsys_heap_finish(&srv_heap->base))
      return false;

   pvr_srv_int_heap_destroy(srv_ws->render_fd, srv_heap->server_heap);

   return true;
}

static VkResult pvr_srv_memctx_init(struct pvr_srv_winsys *srv_ws)
{
   const struct pvr_winsys_static_data_offsets
      general_heap_static_data_offsets = {
         .yuv_csc = FWIF_GENERAL_HEAP_YUV_CSC_OFFSET_BYTES,
      };
   const struct pvr_winsys_static_data_offsets pds_heap_static_data_offsets = {
      .eot = FWIF_PDS_HEAP_EOT_OFFSET_BYTES,
      .vdm_sync = FWIF_PDS_HEAP_VDM_SYNC_OFFSET_BYTES,
   };
   const struct pvr_winsys_static_data_offsets usc_heap_static_data_offsets = {
      .vdm_sync = FWIF_USC_HEAP_VDM_SYNC_OFFSET_BYTES,
   };
   const struct pvr_winsys_static_data_offsets no_static_data_offsets = { 0 };

   char heap_name[PVR_SRV_DEVMEM_HEAPNAME_MAXLENGTH];
   int vis_test_heap_idx = -1;
   int general_heap_idx = -1;
   int rgn_hdr_heap_idx = -1;
   int pds_heap_idx = -1;
   int usc_heap_idx = -1;
   uint32_t heap_count;
   VkResult result;

   result = pvr_srv_int_ctx_create(srv_ws->render_fd,
                                   &srv_ws->server_memctx,
                                   &srv_ws->server_memctx_data);
   if (result != VK_SUCCESS)
      return result;

   os_get_page_size(&srv_ws->base.page_size);
   srv_ws->base.log2_page_size = util_logbase2(srv_ws->base.page_size);

   result = pvr_srv_get_heap_count(srv_ws->render_fd, &heap_count);
   if (result != VK_SUCCESS)
      goto err_pvr_srv_int_ctx_destroy;

   assert(heap_count > 0);

   for (uint32_t i = 0; i < heap_count; i++) {
      result = pvr_srv_get_heap_details(srv_ws->render_fd,
                                        i,
                                        sizeof(heap_name),
                                        heap_name,
                                        NULL,
                                        NULL,
                                        NULL,
                                        NULL);
      if (result != VK_SUCCESS)
         goto err_pvr_srv_int_ctx_destroy;

      if (general_heap_idx == -1 &&
          strncmp(heap_name,
                  PVR_SRV_GENERAL_HEAP_IDENT,
                  sizeof(PVR_SRV_GENERAL_HEAP_IDENT)) == 0) {
         general_heap_idx = i;
      } else if (pds_heap_idx == -1 &&
                 strncmp(heap_name,
                         PVR_SRV_PDSCODEDATA_HEAP_IDENT,
                         sizeof(PVR_SRV_PDSCODEDATA_HEAP_IDENT)) == 0) {
         pds_heap_idx = i;
      } else if (rgn_hdr_heap_idx == -1 &&
                 strncmp(heap_name,
                         PVR_SRV_RGNHDR_BRN_63142_HEAP_IDENT,
                         sizeof(PVR_SRV_RGNHDR_BRN_63142_HEAP_IDENT)) == 0) {
         rgn_hdr_heap_idx = i;
      } else if (usc_heap_idx == -1 &&
                 strncmp(heap_name,
                         PVR_SRV_USCCODE_HEAP_IDENT,
                         sizeof(PVR_SRV_USCCODE_HEAP_IDENT)) == 0) {
         usc_heap_idx = i;
      } else if (vis_test_heap_idx == -1 &&
                 strncmp(heap_name,
                         PVR_SRV_VISIBILITY_TEST_HEAP_IDENT,
                         sizeof(PVR_SRV_VISIBILITY_TEST_HEAP_IDENT)) == 0) {
         vis_test_heap_idx = i;
      }
   }

   /* Check for and initialise required heaps. */
   if (general_heap_idx == -1 || pds_heap_idx == -1 || usc_heap_idx == -1 ||
       vis_test_heap_idx == -1) {
      result = vk_error(NULL, VK_ERROR_INITIALIZATION_FAILED);
      goto err_pvr_srv_int_ctx_destroy;
   }

   result = pvr_srv_heap_init(srv_ws,
                              &srv_ws->general_heap,
                              general_heap_idx,
                              &general_heap_static_data_offsets);
   if (result != VK_SUCCESS)
      goto err_pvr_srv_int_ctx_destroy;

   result = pvr_srv_heap_init(srv_ws,
                              &srv_ws->pds_heap,
                              pds_heap_idx,
                              &pds_heap_static_data_offsets);
   if (result != VK_SUCCESS)
      goto err_pvr_srv_heap_finish_general;

   result = pvr_srv_heap_init(srv_ws,
                              &srv_ws->usc_heap,
                              usc_heap_idx,
                              &usc_heap_static_data_offsets);
   if (result != VK_SUCCESS)
      goto err_pvr_srv_heap_finish_pds;

   result = pvr_srv_heap_init(srv_ws,
                              &srv_ws->vis_test_heap,
                              vis_test_heap_idx,
                              &no_static_data_offsets);
   if (result != VK_SUCCESS)
      goto err_pvr_srv_heap_finish_usc;

   /* Check for and set up optional heaps. */
   if (rgn_hdr_heap_idx != -1) {
      result = pvr_srv_heap_init(srv_ws,
                                 &srv_ws->rgn_hdr_heap,
                                 rgn_hdr_heap_idx,
                                 &no_static_data_offsets);
      if (result != VK_SUCCESS)
         goto err_pvr_srv_heap_finish_vis_test;
      srv_ws->rgn_hdr_heap_present = true;
   } else {
      srv_ws->rgn_hdr_heap_present = false;
   }

   result =
      pvr_winsys_helper_allocate_static_memory(&srv_ws->base,
                                               pvr_srv_heap_alloc_reserved,
                                               &srv_ws->general_heap.base,
                                               &srv_ws->pds_heap.base,
                                               &srv_ws->usc_heap.base,
                                               &srv_ws->general_vma,
                                               &srv_ws->pds_vma,
                                               &srv_ws->usc_vma);
   if (result != VK_SUCCESS)
      goto err_pvr_srv_heap_finish_rgn_hdr;

   result = pvr_winsys_helper_fill_static_memory(&srv_ws->base,
                                                 srv_ws->general_vma,
                                                 srv_ws->pds_vma,
                                                 srv_ws->usc_vma);
   if (result != VK_SUCCESS)
      goto err_pvr_srv_free_static_memory;

   return VK_SUCCESS;

err_pvr_srv_free_static_memory:
   pvr_winsys_helper_free_static_memory(srv_ws->general_vma,
                                        srv_ws->pds_vma,
                                        srv_ws->usc_vma);

err_pvr_srv_heap_finish_rgn_hdr:
   if (srv_ws->rgn_hdr_heap_present)
      pvr_srv_heap_finish(srv_ws, &srv_ws->rgn_hdr_heap);

err_pvr_srv_heap_finish_vis_test:
   pvr_srv_heap_finish(srv_ws, &srv_ws->vis_test_heap);

err_pvr_srv_heap_finish_usc:
   pvr_srv_heap_finish(srv_ws, &srv_ws->usc_heap);

err_pvr_srv_heap_finish_pds:
   pvr_srv_heap_finish(srv_ws, &srv_ws->pds_heap);

err_pvr_srv_heap_finish_general:
   pvr_srv_heap_finish(srv_ws, &srv_ws->general_heap);

err_pvr_srv_int_ctx_destroy:
   pvr_srv_int_ctx_destroy(srv_ws->render_fd, srv_ws->server_memctx);

   return result;
}

static void pvr_srv_memctx_finish(struct pvr_srv_winsys *srv_ws)
{
   pvr_winsys_helper_free_static_memory(srv_ws->general_vma,
                                        srv_ws->pds_vma,
                                        srv_ws->usc_vma);

   if (srv_ws->rgn_hdr_heap_present) {
      if (!pvr_srv_heap_finish(srv_ws, &srv_ws->rgn_hdr_heap)) {
         vk_errorf(NULL,
                   VK_ERROR_UNKNOWN,
                   "Region header heap in use, can not deinit");
      }
   }

   if (!pvr_srv_heap_finish(srv_ws, &srv_ws->vis_test_heap)) {
      vk_errorf(NULL,
                VK_ERROR_UNKNOWN,
                "Visibility test heap in use, can not deinit");
   }

   if (!pvr_srv_heap_finish(srv_ws, &srv_ws->usc_heap))
      vk_errorf(NULL, VK_ERROR_UNKNOWN, "USC heap in use, can not deinit");

   if (!pvr_srv_heap_finish(srv_ws, &srv_ws->pds_heap))
      vk_errorf(NULL, VK_ERROR_UNKNOWN, "PDS heap in use, can not deinit");

   if (!pvr_srv_heap_finish(srv_ws, &srv_ws->general_heap)) {
      vk_errorf(NULL, VK_ERROR_UNKNOWN, "General heap in use, can not deinit");
   }

   pvr_srv_int_ctx_destroy(srv_ws->render_fd, srv_ws->server_memctx);
}

static VkResult pvr_srv_sync_prim_block_init(struct pvr_srv_winsys *srv_ws)
{
   /* We don't currently make use of this value, but we're required to provide
    * a valid pointer to pvr_srv_alloc_sync_primitive_block.
    */
   void *sync_block_pmr;

   return pvr_srv_alloc_sync_primitive_block(srv_ws->render_fd,
                                             &srv_ws->sync_block_handle,
                                             &sync_block_pmr,
                                             &srv_ws->sync_block_size,
                                             &srv_ws->sync_block_fw_addr);
}

static void pvr_srv_sync_prim_block_finish(struct pvr_srv_winsys *srv_ws)
{
   pvr_srv_free_sync_primitive_block(srv_ws->render_fd,
                                     srv_ws->sync_block_handle);
   srv_ws->sync_block_handle = NULL;
}

static void pvr_srv_winsys_destroy(struct pvr_winsys *ws)
{
   struct pvr_srv_winsys *srv_ws = to_pvr_srv_winsys(ws);
   int fd = srv_ws->render_fd;

   pvr_srv_sync_prim_block_finish(srv_ws);
   pvr_srv_memctx_finish(srv_ws);
   vk_free(srv_ws->alloc, srv_ws);
   pvr_srv_connection_destroy(fd);
}

static int
pvr_srv_winsys_device_info_init(struct pvr_winsys *ws,
                                struct pvr_device_info *dev_info,
                                struct pvr_device_runtime_info *runtime_info)
{
   struct pvr_srv_winsys *srv_ws = to_pvr_srv_winsys(ws);
   VkResult result;
   int ret;

   ret = pvr_device_info_init(dev_info, srv_ws->bvnc);
   if (ret) {
      mesa_logw("Unsupported BVNC: %u.%u.%u.%u\n",
                PVR_BVNC_UNPACK_B(srv_ws->bvnc),
                PVR_BVNC_UNPACK_V(srv_ws->bvnc),
                PVR_BVNC_UNPACK_N(srv_ws->bvnc),
                PVR_BVNC_UNPACK_C(srv_ws->bvnc));
      return ret;
   }

   if (PVR_HAS_FEATURE(dev_info, gpu_multicore_support)) {
      result = pvr_srv_get_multicore_info(srv_ws->render_fd,
                                          0,
                                          NULL,
                                          &runtime_info->core_count);
      if (result != VK_SUCCESS)
         return -ENODEV;
   } else {
      runtime_info->core_count = 1;
   }

   return 0;
}

static void pvr_srv_winsys_get_heaps_info(struct pvr_winsys *ws,
                                          struct pvr_winsys_heaps *heaps)
{
   struct pvr_srv_winsys *srv_ws = to_pvr_srv_winsys(ws);

   heaps->general_heap = &srv_ws->general_heap.base;
   heaps->pds_heap = &srv_ws->pds_heap.base;
   heaps->usc_heap = &srv_ws->usc_heap.base;
   heaps->vis_test_heap = &srv_ws->vis_test_heap.base;

   if (srv_ws->rgn_hdr_heap_present)
      heaps->rgn_hdr_heap = &srv_ws->rgn_hdr_heap.base;
   else
      heaps->rgn_hdr_heap = &srv_ws->general_heap.base;
}

static const struct pvr_winsys_ops srv_winsys_ops = {
   .destroy = pvr_srv_winsys_destroy,
   .device_info_init = pvr_srv_winsys_device_info_init,
   .get_heaps_info = pvr_srv_winsys_get_heaps_info,
   .buffer_create = pvr_srv_winsys_buffer_create,
   .buffer_create_from_fd = pvr_srv_winsys_buffer_create_from_fd,
   .buffer_destroy = pvr_srv_winsys_buffer_destroy,
   .buffer_get_fd = pvr_srv_winsys_buffer_get_fd,
   .buffer_map = pvr_srv_winsys_buffer_map,
   .buffer_unmap = pvr_srv_winsys_buffer_unmap,
   .heap_alloc = pvr_srv_winsys_heap_alloc,
   .heap_free = pvr_srv_winsys_heap_free,
   .vma_map = pvr_srv_winsys_vma_map,
   .vma_unmap = pvr_srv_winsys_vma_unmap,
   .syncobj_create = pvr_srv_winsys_syncobj_create,
   .syncobj_destroy = pvr_srv_winsys_syncobj_destroy,
   .syncobjs_reset = pvr_srv_winsys_syncobjs_reset,
   .syncobjs_signal = pvr_srv_winsys_syncobjs_signal,
   .syncobjs_wait = pvr_srv_winsys_syncobjs_wait,
   .syncobjs_merge = pvr_srv_winsys_syncobjs_merge,
   .free_list_create = pvr_srv_winsys_free_list_create,
   .free_list_destroy = pvr_srv_winsys_free_list_destroy,
   .render_target_dataset_create = pvr_srv_render_target_dataset_create,
   .render_target_dataset_destroy = pvr_srv_render_target_dataset_destroy,
   .render_ctx_create = pvr_srv_winsys_render_ctx_create,
   .render_ctx_destroy = pvr_srv_winsys_render_ctx_destroy,
   .render_submit = pvr_srv_winsys_render_submit,
   .compute_ctx_create = pvr_srv_winsys_compute_ctx_create,
   .compute_ctx_destroy = pvr_srv_winsys_compute_ctx_destroy,
   .compute_submit = pvr_srv_winsys_compute_submit,
};

static bool pvr_is_driver_compatible(int render_fd)
{
   drmVersionPtr version;

   version = drmGetVersion(render_fd);
   if (!version)
      return false;

   assert(strcmp(version->name, "pvr") == 0);

   /* Only the 1.14 driver is supported for now. */
   if (version->version_major != PVR_SRV_VERSION_MAJ ||
       version->version_minor != PVR_SRV_VERSION_MIN) {
      vk_errorf(NULL,
                VK_ERROR_INCOMPATIBLE_DRIVER,
                "Unsupported downstream driver version (%u.%u)",
                version->version_major,
                version->version_minor);
      drmFreeVersion(version);

      return false;
   }

   drmFreeVersion(version);

   return true;
}

struct pvr_winsys *pvr_srv_winsys_create(int master_fd,
                                         int render_fd,
                                         const VkAllocationCallbacks *alloc)
{
   struct pvr_srv_winsys *srv_ws;
   VkResult result;
   uint64_t bvnc;

   if (!pvr_is_driver_compatible(render_fd))
      return NULL;

   result = pvr_srv_connection_create(render_fd, &bvnc);
   if (result != VK_SUCCESS)
      return NULL;

   srv_ws =
      vk_zalloc(alloc, sizeof(*srv_ws), 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
   if (!srv_ws) {
      vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY);
      goto err_pvr_srv_connection_destroy;
   }

   srv_ws->base.ops = &srv_winsys_ops;
   srv_ws->bvnc = bvnc;
   srv_ws->master_fd = master_fd;
   srv_ws->render_fd = render_fd;
   srv_ws->alloc = alloc;

   result = pvr_srv_memctx_init(srv_ws);
   if (result != VK_SUCCESS)
      goto err_vk_free_srv_ws;

   result = pvr_srv_sync_prim_block_init(srv_ws);
   if (result != VK_SUCCESS)
      goto err_pvr_srv_memctx_finish;

   return &srv_ws->base;

err_pvr_srv_memctx_finish:
   pvr_srv_memctx_finish(srv_ws);

err_vk_free_srv_ws:
   vk_free(alloc, srv_ws);

err_pvr_srv_connection_destroy:
   pvr_srv_connection_destroy(render_fd);

   return NULL;
}

struct pvr_srv_sync_prim *pvr_srv_sync_prim_alloc(struct pvr_srv_winsys *srv_ws)
{
   struct pvr_srv_sync_prim *sync_prim;

   if (p_atomic_read(&srv_ws->sync_block_offset) == srv_ws->sync_block_size) {
      vk_error(NULL, VK_ERROR_UNKNOWN);
      return NULL;
   }

   sync_prim = vk_alloc(srv_ws->alloc,
                        sizeof(*sync_prim),
                        8,
                        VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
   if (!sync_prim) {
      vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY);
      return NULL;
   }

   /* p_atomic_add_return() returns the new value rather than the old one, so
    * we have to subtract PVR_SRV_SYNC_PRIM_VALUE_SIZE to get the old value.
    */
   sync_prim->offset = p_atomic_add_return(&srv_ws->sync_block_offset,
                                           PVR_SRV_SYNC_PRIM_VALUE_SIZE);
   sync_prim->offset -= PVR_SRV_SYNC_PRIM_VALUE_SIZE;
   if (sync_prim->offset == srv_ws->sync_block_size) {
      /* FIXME: need to free offset back to srv_ws->sync_block_offset. */
      vk_free(srv_ws->alloc, sync_prim);

      vk_error(NULL, VK_ERROR_UNKNOWN);

      return NULL;
   }

   sync_prim->srv_ws = srv_ws;

   return sync_prim;
}

/* FIXME: Add support for freeing offsets back to the sync block. */
void pvr_srv_sync_prim_free(struct pvr_srv_sync_prim *sync_prim)
{
   if (sync_prim) {
      struct pvr_srv_winsys *srv_ws = sync_prim->srv_ws;

      vk_free(srv_ws->alloc, sync_prim);
   }
}
