Switch to 64-bit syncobj handles

Removes the need to map magma semaphores to uint32 ids.

Change-Id: I04b65c0ce76d090d1d48ca76bdd890582e0943ec
diff --git a/include/drm-uapi/i915_drm.h b/include/drm-uapi/i915_drm.h
index 2531928..b7e27fe 100644
--- a/include/drm-uapi/i915_drm.h
+++ b/include/drm-uapi/i915_drm.h
@@ -836,7 +836,7 @@
 	/**
 	 * User's handle for a drm_syncobj to wait on or signal.
 	 */
-	__u32 handle;
+	__u64 handle; // magma uses 64bit buffer 'handles'
 
 #define I915_EXEC_FENCE_WAIT            (1<<0)
 #define I915_EXEC_FENCE_SIGNAL          (1<<1)
diff --git a/src/intel/vulkan/anv_batch_chain.c b/src/intel/vulkan/anv_batch_chain.c
index 4f98782..28c5ed1 100644
--- a/src/intel/vulkan/anv_batch_chain.c
+++ b/src/intel/vulkan/anv_batch_chain.c
@@ -1070,7 +1070,7 @@
 
 static VkResult
 anv_execbuf_add_syncobj(struct anv_execbuf *exec,
-                        uint32_t handle, uint32_t flags,
+                        anv_syncobj_handle_t handle, uint32_t flags,
                         const VkAllocationCallbacks *alloc)
 {
    assert(flags != 0);
diff --git a/src/intel/vulkan/anv_magma.cc b/src/intel/vulkan/anv_magma.cc
index 2694b7a..24573c7 100644
--- a/src/intel/vulkan/anv_magma.cc
+++ b/src/intel/vulkan/anv_magma.cc
@@ -165,14 +165,10 @@
 
    for (uint32_t i = 0; i < syncobj_count; i++) {
       auto& syncobj = reinterpret_cast<drm_i915_gem_exec_fence*>(execbuf->cliprects_ptr)[i];
-      magma_semaphore_t semaphore;
-      if (!static_cast<Connection*>(device->connection)->find_semaphore(syncobj.handle, &semaphore))
-         return DRET_MSG(-1, "failed to find semaphore");
-
       if (syncobj.flags & I915_EXEC_FENCE_WAIT) {
-         wait_semaphore_ids.push_back(magma_get_semaphore_id(semaphore));
+         wait_semaphore_ids.push_back(magma_get_semaphore_id(syncobj.handle));
       } else if (syncobj.flags & I915_EXEC_FENCE_SIGNAL) {
-         signal_semaphore_ids.push_back(magma_get_semaphore_id(semaphore));
+         signal_semaphore_ids.push_back(magma_get_semaphore_id(syncobj.handle));
       } else {
          return DRET_MSG(-1, "syncobj not wait or signal");
       }
@@ -339,15 +335,14 @@
    ANV_FROM_HANDLE(anv_device, device, vk_device);
    ANV_FROM_HANDLE(anv_semaphore, semaphore, info->semaphore);
 
-   magma_semaphore_t imported_semaphore;
+   magma_semaphore_t magma_semaphore;
    magma_status_t status =
-       magma_import_semaphore(magma_connection(device), info->handle, &imported_semaphore);
+       magma_import_semaphore(magma_connection(device), info->handle, &magma_semaphore);
    if (status != MAGMA_STATUS_OK)
       return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR);
 
    anv_semaphore_impl new_impl = {.type = ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ};
-   new_impl.syncobj =
-       static_cast<Connection*>(device->connection)->add_semaphore(imported_semaphore);
+   new_impl.syncobj = magma_semaphore;
 
    if (info->flags & VK_SEMAPHORE_IMPORT_TEMPORARY_BIT_KHR) {
       anv_semaphore_impl_cleanup(device, &semaphore->temporary);
@@ -373,21 +368,16 @@
    if (info->handleType != VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_FUCHSIA_FENCE_BIT_KHR)
       return VK_SUCCESS;
 
-   magma_semaphore_t magma_semaphore;
-   if (!static_cast<Connection*>(device->connection)
-            ->find_semaphores(&semaphore, &magma_semaphore, 1))
-      return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR);
-
-   uint32_t handle;
-   magma_status_t status =
-       magma_export_semaphore(magma_connection(device), magma_semaphore, &handle);
-   if (status != MAGMA_STATUS_OK)
-      return vk_error(VK_ERROR_TOO_MANY_OBJECTS);
-
    anv_semaphore_impl* impl = semaphore->temporary.type != ANV_SEMAPHORE_TYPE_NONE
                                   ? &semaphore->temporary
                                   : &semaphore->permanent;
 
+   uint32_t handle;
+   magma_status_t status =
+       magma_export_semaphore(magma_connection(device), impl->syncobj, &handle);
+   if (status != MAGMA_STATUS_OK)
+      return vk_error(VK_ERROR_TOO_MANY_OBJECTS);
+
    /* From the Vulkan 1.0.53 spec:
     *
     *    "Export operations have the same transference as the specified handle
@@ -404,7 +394,7 @@
 
 bool anv_gem_supports_syncobj_wait(int fd) { return true; }
 
-uint32_t anv_gem_syncobj_create(anv_device* device, uint32_t flags)
+anv_syncobj_handle_t anv_gem_syncobj_create(anv_device* device, uint32_t flags)
 {
    magma_semaphore_t semaphore;
    magma_status_t status = magma_create_semaphore(magma_connection(device), &semaphore);
@@ -414,39 +404,24 @@
    }
    if (flags & DRM_SYNCOBJ_CREATE_SIGNALED)
       magma_signal_semaphore(semaphore);
-
-   return static_cast<Connection*>(device->connection)->add_semaphore(semaphore);
+   return semaphore;
 }
 
-void anv_gem_syncobj_destroy(anv_device* device, uint32_t handle)
+void anv_gem_syncobj_destroy(anv_device* device, anv_syncobj_handle_t semaphore)
 {
-   magma_semaphore_t semaphore;
-   if (!static_cast<Connection*>(device->connection)->remove_semaphore(handle, &semaphore)) {
-      DLOG("failed to remove syncobj 0x%x", handle);
-      return;
-   }
    magma_release_semaphore(magma_connection(device), semaphore);
 }
 
-void anv_gem_syncobj_reset(anv_device* device, uint32_t handle)
+void anv_gem_syncobj_reset(anv_device* device, anv_syncobj_handle_t semaphore)
 {
-   magma_semaphore_t semaphore;
-   if (!static_cast<Connection*>(device->connection)->find_semaphore(handle, &semaphore)) {
-      DLOG("Failed to find syncobj 0x%x", handle);
-      return;
-   }
    magma_reset_semaphore(semaphore);
 }
 
-int anv_gem_syncobj_wait(anv_device* device, uint32_t* handles, uint32_t num_handles,
+int anv_gem_syncobj_wait(anv_device* device, anv_syncobj_handle_t* semaphores, uint32_t num_handles,
                          int64_t abs_timeout_ns, bool wait_all)
 {
    for (uint32_t i = 0; i < num_handles; i++) {
-      magma_semaphore_t semaphore;
-      if (!static_cast<Connection*>(device->connection)->find_semaphore(handles[i], &semaphore))
-         return DRET_MSG(-1, "failed to find semaphores");
-
-      magma_status_t status = magma_wait_semaphore(semaphore, abs_timeout_ns);
+      magma_status_t status = magma_wait_semaphore(semaphores[i], abs_timeout_ns);
       switch (status) {
       case MAGMA_STATUS_OK:
          break;
@@ -460,25 +435,25 @@
    return 0;
 }
 
-uint32_t anv_gem_syncobj_fd_to_handle(struct anv_device* device, int fd)
+anv_syncobj_handle_t anv_gem_syncobj_fd_to_handle(struct anv_device* device, int fd)
 {
    DASSERT(false);
    return 0;
 }
 
-int anv_gem_syncobj_handle_to_fd(struct anv_device* device, uint32_t handle)
+int anv_gem_syncobj_handle_to_fd(struct anv_device* device, anv_syncobj_handle_t handle)
 {
    DASSERT(false);
    return -1;
 }
 
-int anv_gem_syncobj_export_sync_file(struct anv_device* device, uint32_t handle)
+int anv_gem_syncobj_export_sync_file(struct anv_device* device, anv_syncobj_handle_t handle)
 {
    DASSERT(false);
    return -1;
 }
 
-int anv_gem_syncobj_import_sync_file(struct anv_device* device, uint32_t handle, int fd)
+int anv_gem_syncobj_import_sync_file(struct anv_device* device, anv_syncobj_handle_t handle, int fd)
 {
    DASSERT(false);
    return -1;
diff --git a/src/intel/vulkan/anv_magma.h b/src/intel/vulkan/anv_magma.h
index 7f1c361..789c4c6 100644
--- a/src/intel/vulkan/anv_magma.h
+++ b/src/intel/vulkan/anv_magma.h
@@ -11,8 +11,6 @@
 // clang-format off
 #include "anv_private.h"
 // clang-format on
-#include <map>
-#include <mutex>
 
 class Connection : public anv_connection {
 public:
@@ -22,62 +20,8 @@
 
    magma_connection_t* magma_connection() { return magma_connection_; }
 
-   bool find_semaphore(uint32_t id, magma_semaphore_t* semaphore_out)
-   {
-      std::unique_lock<std::mutex> lock(semaphore_map_lock_);
-      auto iter = semaphore_map_.find(id);
-      if (iter == semaphore_map_.end())
-         return DRETF(false, "id not in semaphore map: %u", id);
-      *semaphore_out = iter->second;
-      return true;
-   }
-
-   uint32_t add_semaphore(magma_semaphore_t semaphore)
-   {
-      std::unique_lock<std::mutex> lock(semaphore_map_lock_);
-      uint32_t id;
-      do {
-         id = next_semaphore_id_++;
-      } while (semaphore_map_.find(id) != semaphore_map_.end());
-      semaphore_map_[id] = semaphore;
-      return id;
-   }
-
-   bool remove_semaphore(uint32_t id, magma_semaphore_t* semaphore_out)
-   {
-      std::unique_lock<std::mutex> lock(semaphore_map_lock_);
-      auto iter = semaphore_map_.find(id);
-      if (iter == semaphore_map_.end())
-         return DRETF(false, "id not in semaphore map: %u", id);
-      *semaphore_out = iter->second;
-      semaphore_map_.erase(iter);
-      return true;
-   }
-
-   bool find_semaphores(anv_semaphore* semaphore[], magma_semaphore_t magma_semaphore_out[],
-                        uint32_t count)
-   {
-      std::unique_lock<std::mutex> lock(semaphore_map_lock_);
-
-      for (uint32_t i = 0; i < count; i++) {
-         anv_semaphore_impl* impl = semaphore[i]->temporary.type != ANV_SEMAPHORE_TYPE_NONE
-                                        ? &semaphore[i]->temporary
-                                        : &semaphore[i]->permanent;
-
-         auto iter = semaphore_map_.find(impl->syncobj);
-         if (iter == semaphore_map_.end())
-            return DRETF(false, "id not in semaphore map: %u", impl->syncobj);
-
-         magma_semaphore_out[i] = iter->second;
-      }
-      return true;
-   }
-
 private:
    magma_connection_t* magma_connection_;
-   std::mutex semaphore_map_lock_;
-   std::map<uint32_t, magma_semaphore_t> semaphore_map_;
-   uint32_t next_semaphore_id_ = 1000;
 };
 
 static magma_connection_t* magma_connection(anv_device* device)
diff --git a/src/intel/vulkan/anv_private.h b/src/intel/vulkan/anv_private.h
index bbb655a..53682e5 100644
--- a/src/intel/vulkan/anv_private.h
+++ b/src/intel/vulkan/anv_private.h
@@ -495,6 +495,7 @@
 }
 
 typedef uintptr_t anv_buffer_handle_t;
+typedef uintptr_t anv_syncobj_handle_t;
 
 struct anv_bo {
    anv_buffer_handle_t gem_handle;
@@ -970,18 +971,18 @@
 int anv_gem_set_domain(struct anv_device *device, uint32_t gem_handle,
                        uint32_t read_domains, uint32_t write_domain);
 int anv_gem_sync_file_merge(struct anv_device *device, int fd1, int fd2);
-uint32_t anv_gem_syncobj_create(struct anv_device *device, uint32_t flags);
-void anv_gem_syncobj_destroy(struct anv_device *device, uint32_t handle);
-int anv_gem_syncobj_handle_to_fd(struct anv_device *device, uint32_t handle);
-uint32_t anv_gem_syncobj_fd_to_handle(struct anv_device *device, int fd);
+anv_syncobj_handle_t anv_gem_syncobj_create(struct anv_device *device, uint32_t flags);
+void anv_gem_syncobj_destroy(struct anv_device *device, anv_syncobj_handle_t handle);
+int anv_gem_syncobj_handle_to_fd(struct anv_device *device, anv_syncobj_handle_t handle);
+anv_syncobj_handle_t anv_gem_syncobj_fd_to_handle(struct anv_device *device, int fd);
 int anv_gem_syncobj_export_sync_file(struct anv_device *device,
-                                     uint32_t handle);
+                                     anv_syncobj_handle_t handle);
 int anv_gem_syncobj_import_sync_file(struct anv_device *device,
-                                     uint32_t handle, int fd);
-void anv_gem_syncobj_reset(struct anv_device *device, uint32_t handle);
+                                     anv_syncobj_handle_t handle, int fd);
+void anv_gem_syncobj_reset(struct anv_device *device, anv_syncobj_handle_t handle);
 bool anv_gem_supports_syncobj_wait(int fd);
 int anv_gem_syncobj_wait(struct anv_device *device,
-                         uint32_t *handles, uint32_t num_handles,
+                         anv_syncobj_handle_t *handles, uint32_t num_handles,
                          int64_t abs_timeout_ns, bool wait_all);
 
 int anv_platform_futex_wake(uint32_t *addr, int count);
@@ -1950,7 +1951,7 @@
       } bo;
 
       /** DRM syncobj handle for syncobj-based fences */
-      uint32_t syncobj;
+      anv_syncobj_handle_t syncobj;
    };
 };
 
@@ -2005,7 +2006,7 @@
        * Unlike GEM BOs, DRM sync objects aren't deduplicated by the kernel on
        * import so we don't need to bother with a userspace cache.
        */
-      uint32_t syncobj;
+      anv_syncobj_handle_t syncobj;
    };
 };
 
diff --git a/src/intel/vulkan/anv_queue.c b/src/intel/vulkan/anv_queue.c
index 51c37cf..6b35c94 100644
--- a/src/intel/vulkan/anv_queue.c
+++ b/src/intel/vulkan/anv_queue.c
@@ -462,7 +462,7 @@
                             bool waitAll,
                             uint64_t _timeout)
 {
-   uint32_t *syncobjs = vk_zalloc(&device->alloc,
+   anv_syncobj_handle_t *syncobjs = vk_zalloc(&device->alloc,
                                   sizeof(*syncobjs) * fenceCount, 8,
                                   VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
    if (!syncobjs)
diff --git a/src/intel/vulkan/anv_wsi_magma.cc b/src/intel/vulkan/anv_wsi_magma.cc
index 6e58892..34922d1 100644
--- a/src/intel/vulkan/anv_wsi_magma.cc
+++ b/src/intel/vulkan/anv_wsi_magma.cc
@@ -144,10 +144,7 @@
 {
    anv_device* device = anv_device_from_handle(vk_device);
    ANV_FROM_HANDLE(anv_semaphore, semaphore, vk_semaphore);
-   magma_semaphore_t magma_semaphore;
-   if (!static_cast<Connection*>(device->connection)->find_semaphores(&semaphore, &magma_semaphore, 1)) {
-      DLOG("failed to find semaphore");
-      return 0;
-   }
-   return magma_semaphore;
+   anv_semaphore_impl* impl = semaphore->temporary.type != ANV_SEMAPHORE_TYPE_NONE
+                              ? &semaphore->temporary : &semaphore->permanent;
+   return impl->syncobj;
 }