Separate out c++ implementation

anv_magma_connection.cc doesn't include anv_private.h,
so we can revert the modifications made to that header
and other headers included by it.

Change-Id: I6a40d35bb2533680b56548c613b253b33fc5abcc
diff --git a/src/intel/common/gen_clflush.h b/src/intel/common/gen_clflush.h
index 806564f..d39f158 100644
--- a/src/intel/common/gen_clflush.h
+++ b/src/intel/common/gen_clflush.h
@@ -30,8 +30,8 @@
 static inline void
 gen_clflush_range(void *start, size_t size)
 {
-   char *p = (char *) (((uintptr_t) start) & ~CACHELINE_MASK);
-   char *end = (char*) start + size;
+   void *p = (void *) (((uintptr_t) start) & ~CACHELINE_MASK);
+   void *end = start + size;
 
    while (p < end) {
       __builtin_ia32_clflush(p);
@@ -61,7 +61,7 @@
     * ("drm: Restore double clflush on the last partial cacheline")
     * and https://bugs.freedesktop.org/show_bug.cgi?id=92845.
     */
-   __builtin_ia32_clflush((char*) start + size - 1);
+   __builtin_ia32_clflush(start + size - 1);
    __builtin_ia32_mfence();
 }
 
diff --git a/src/intel/vulkan/BUILD.gn b/src/intel/vulkan/BUILD.gn
index 8072175..e975186 100644
--- a/src/intel/vulkan/BUILD.gn
+++ b/src/intel/vulkan/BUILD.gn
@@ -100,7 +100,8 @@
     "anv_genX.h",
     "anv_image.c",
     "anv_intel.c",
-    "anv_magma.cc",
+    "anv_magma.c",
+    "anv_magma_connection.cc",
     "anv_nir.h",
     "anv_nir_add_base_work_group_id.c",
     "anv_nir_apply_pipeline_layout.c",
diff --git a/src/intel/vulkan/anv_magma.c b/src/intel/vulkan/anv_magma.c
new file mode 100644
index 0000000..01a4246
--- /dev/null
+++ b/src/intel/vulkan/anv_magma.c
@@ -0,0 +1,467 @@
+// Copyright 2016 The Fuchsia Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "anv_magma.h"
+#include "anv_private.h"
+#include "msd_intel_gen_query.h"
+#include "magma_util/dlog.h"
+
+static magma_connection_t* magma_connection(struct anv_device* device)
+{
+   assert(device);
+   assert(device->connection);
+   return device->connection->connection;
+}
+
+
+int anv_gem_connect(struct anv_device* device)
+{
+   magma_connection_t* connection = magma_create_connection(device->fd, 0);
+   if (!connection) {
+      DLOG("magma_create_connection failed");
+      return -1;
+   }
+
+   device->connection = AnvMagmaCreateConnection(connection);
+
+   DLOG("created magma connection");
+   return 0;
+}
+
+void anv_gem_disconnect(struct anv_device* device)
+{
+   AnvMagmaReleaseConnection(device->connection);
+   DLOG("released magma connection");
+}
+
+// Return handle, or 0 on failure. Gem handles are never 0.
+anv_buffer_handle_t anv_gem_create(struct anv_device* device, size_t size)
+{
+   magma_buffer_t buffer;
+   uint64_t magma_size = size;
+   magma_status_t status = magma_create_buffer(magma_connection(device), magma_size, &magma_size, &buffer);
+   if (status != MAGMA_STATUS_OK) {
+      DLOG("magma_create_buffer failed (%d) size 0x%zx", status, magma_size);
+      return 0;
+   }
+   DLOG("magma_create_buffer size 0x%zx returning buffer 0x%lx", magma_size, buffer);
+
+   assert(buffer != 0);
+   return buffer;
+}
+
+void anv_gem_close(struct anv_device* device, anv_buffer_handle_t handle)
+{
+   DLOG("anv_gem_close handle 0x%lx", handle);
+   magma_release_buffer(magma_connection(device), handle);
+}
+
+void* anv_gem_mmap(struct anv_device* device, anv_buffer_handle_t handle, uint64_t offset, uint64_t size,
+                   uint32_t flags)
+{
+   assert(flags == 0);
+   void* addr;
+   magma_status_t status = magma_map(magma_connection(device), handle, &addr);
+   if (status != MAGMA_STATUS_OK) {
+      DLOG("magma_map failed: status %d", status);
+      return MAP_FAILED;
+   }
+   DLOG("magma_system_map handle 0x%lx size 0x%zx returning %p", handle, size, addr);
+   return (uint8_t*) addr + offset;
+}
+
+void anv_gem_munmap(struct anv_device* device, anv_buffer_handle_t gem_handle, void* addr, uint64_t size)
+{
+   if (!addr)
+      return;
+
+   if (magma_unmap(magma_connection(device), gem_handle) != 0) {
+      DLOG("magma_unmap failed");
+      return;
+   }
+
+   DLOG("magma_unmap handle 0x%lx", gem_handle);
+}
+
+uint32_t anv_gem_userptr(struct anv_device* device, void* mem, size_t size)
+{
+   DLOG("anv_gem_userptr - STUB");
+   assert(false);
+   return 0;
+}
+
+int anv_gem_set_caching(struct anv_device* device, anv_buffer_handle_t gem_handle, uint32_t caching)
+{
+   DLOG("anv_get_set_caching - STUB");
+   return 0;
+}
+
+int anv_gem_set_domain(struct anv_device* device, anv_buffer_handle_t gem_handle, uint32_t read_domains,
+                       uint32_t write_domain)
+{
+   DLOG("anv_gem_set_domain - STUB");
+   return 0;
+}
+
+/**
+ * On error, \a timeout_ns holds the remaining time.
+ */
+int anv_gem_wait(struct anv_device* device, anv_buffer_handle_t handle, int64_t* timeout_ns)
+{
+   DLOG("anv_gem_wait buffer_id %lu timeout_ns %lu", magma_get_buffer_id(handle), *timeout_ns);
+   uint64_t buffer_id = magma_get_buffer_id(handle);
+   AnvMagmaConnectionWait(device->connection, buffer_id, timeout_ns);
+   return 0;
+}
+
+/**
+ * Returns 0, 1, or negative to indicate error
+ */
+int anv_gem_busy(struct anv_device* device, anv_buffer_handle_t handle)
+{
+   DLOG("anv_gem_busy");
+   return AnvMagmaConnectionIsBusy(device->connection, magma_get_buffer_id(handle));
+}
+
+bool anv_gem_supports_48b_addresses(int fd)
+{
+   uint64_t gtt_size;
+   magma_status_t status = magma_query(fd, kMsdIntelGenQueryGttSize, &gtt_size);
+   if (status != MAGMA_STATUS_OK) {
+      DLOG("magma_query failed: %d", status);
+      return false;
+   }
+   return gtt_size >= 1ul << 48;
+}
+
+int anv_gem_get_context_param(int fd, int context, uint32_t param, uint64_t* value)
+{
+   magma_status_t status;
+   switch (param) {
+   case I915_CONTEXT_PARAM_GTT_SIZE:
+      status = magma_query(fd, kMsdIntelGenQueryGttSize, value);
+      if (status != MAGMA_STATUS_OK) {
+         DLOG("magma_query failed: %d", status);
+         return -1;
+      }
+      return 0;
+   default:
+      DLOG("anv_gem_get_context_param: unhandled param 0x%x", param);
+      return -1;
+   }
+}
+
+int anv_gem_execbuffer(struct anv_device* device, struct drm_i915_gem_execbuffer2* execbuf)
+{
+   DLOG("anv_gem_execbuffer");
+   return AnvMagmaConnectionExec(device->connection, device->context_id, execbuf);
+}
+
+int anv_gem_set_tiling(struct anv_device* device, anv_buffer_handle_t gem_handle, uint32_t stride,
+                       uint32_t tiling)
+{
+   DLOG("anv_gem_set_tiling - STUB");
+   return 0;
+}
+
+int anv_gem_get_param(int fd, uint32_t param)
+{
+   magma_status_t status = MAGMA_STATUS_OK;
+   uint64_t value;
+
+   switch (param) {
+   case I915_PARAM_CHIPSET_ID:
+      status = magma_query(fd, MAGMA_QUERY_DEVICE_ID, &value);
+      break;
+   case I915_PARAM_SUBSLICE_TOTAL:
+      status = magma_query(fd, kMsdIntelGenQuerySubsliceAndEuTotal, &value);
+      value >>= 32;
+      break;
+   case I915_PARAM_EU_TOTAL:
+      status = magma_query(fd, kMsdIntelGenQuerySubsliceAndEuTotal, &value);
+      value = (uint32_t) value;
+      break;
+   case I915_PARAM_HAS_WAIT_TIMEOUT:
+   case I915_PARAM_HAS_EXECBUF2:
+      value = 1;
+      break;
+   case I915_PARAM_HAS_EXEC_FENCE_ARRAY: // Used for semaphores
+      value = 1;
+      break;
+   default:
+      status = MAGMA_STATUS_INVALID_ARGS;
+   }
+
+   if (status != MAGMA_STATUS_OK)
+      value = 0;
+
+   uint32_t result = (uint32_t) value;
+   assert(result == value);
+   DLOG("anv_gem_get_param(%u, %u) returning %d", fd, param, result);
+   return result;
+}
+
+bool anv_gem_get_bit6_swizzle(int fd, uint32_t tiling)
+{
+   DLOG("anv_gem_get_bit6_swizzle - STUB");
+   return 0;
+}
+
+int anv_gem_create_context(struct anv_device* device)
+{
+   uint32_t context_id;
+   magma_create_context(magma_connection(device), &context_id);
+   DLOG("magma_create_context returned context_id %u", context_id);
+   return context_id;
+}
+
+int anv_gem_destroy_context(struct anv_device* device, int context_id)
+{
+   magma_release_context(magma_connection(device), context_id);
+   return 0;
+}
+
+int anv_gem_get_aperture(int fd, uint64_t* size)
+{
+   DLOG("anv_gem_get_aperture - STUB");
+   return 0;
+}
+
+int anv_gem_handle_to_fd(struct anv_device* device, anv_buffer_handle_t gem_handle)
+{
+   assert(false);
+   return -1;
+}
+
+anv_buffer_handle_t anv_gem_fd_to_handle(struct anv_device* device, int fd)
+{
+   assert(false);
+   return 0;
+}
+
+int anv_gem_gpu_get_reset_stats(struct anv_device* device, uint32_t* active, uint32_t* pending)
+{
+   DLOG("anv_gem_gpu_get_reset_stats - STUB");
+   *active = 0;
+   *pending = 0;
+   return 0;
+}
+
+int anv_gem_import_fuchsia_buffer(struct anv_device* device, uint32_t handle,
+                                  anv_buffer_handle_t* buffer_out, uint64_t* size_out)
+{
+   magma_status_t status = magma_import(magma_connection(device), handle, buffer_out);
+   if (status != MAGMA_STATUS_OK) {
+      DLOG("magma_import failed: %d", status);
+      return -EINVAL;
+   }
+
+   *size_out = magma_get_buffer_size(*buffer_out);
+   return 0;
+}
+
+#if VK_USE_PLATFORM_MAGMA_KHR
+VkResult anv_GetMemoryFuchsiaHandleKHR(VkDevice vk_device,
+                                       const VkMemoryGetFuchsiaHandleInfoKHR* pGetFuchsiaHandleInfo,
+                                       uint32_t* pHandle)
+{
+   ANV_FROM_HANDLE(anv_device, device, vk_device);
+   ANV_FROM_HANDLE(anv_device_memory, memory, pGetFuchsiaHandleInfo->memory);
+
+   assert(pGetFuchsiaHandleInfo->sType == VK_STRUCTURE_TYPE_MEMORY_GET_FUCHSIA_HANDLE_INFO_KHR);
+   assert(pGetFuchsiaHandleInfo->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_FUCHSIA_VMO_BIT_KHR);
+
+   magma_status_t status = magma_export(magma_connection(device), memory->bo->gem_handle, pHandle);
+   assert(status == MAGMA_STATUS_OK);
+
+   return VK_SUCCESS;
+}
+
+VkResult anv_GetMemoryFuchsiaHandlePropertiesKHR(
+    VkDevice vk_device, VkExternalMemoryHandleTypeFlagBitsKHR handleType, uint32_t handle,
+    VkMemoryFuchsiaHandlePropertiesKHR* pMemoryFuchsiaHandleProperties)
+{
+   ANV_FROM_HANDLE(anv_device, device, vk_device);
+
+   assert(handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_FUCHSIA_VMO_BIT_KHR);
+   assert(pMemoryFuchsiaHandleProperties->sType ==
+          VK_STRUCTURE_TYPE_MEMORY_FUCHSIA_HANDLE_PROPERTIES_KHR);
+
+   struct anv_physical_device* pdevice = &device->instance->physicalDevice;
+   // All memory types supported
+   pMemoryFuchsiaHandleProperties->memoryTypeBits = (1ull << pdevice->memory.type_count) - 1;
+
+   return VK_SUCCESS;
+}
+
+/* Similar to anv_ImportSemaphoreFdKHR */
+VkResult anv_ImportSemaphoreFuchsiaHandleKHR(VkDevice vk_device,
+                                             const VkImportSemaphoreFuchsiaHandleInfoKHR* info)
+{
+   assert(info->sType == VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FUCHSIA_HANDLE_INFO_KHR);
+   assert(info->handleType == VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_FUCHSIA_FENCE_BIT_KHR);
+
+   ANV_FROM_HANDLE(anv_device, device, vk_device);
+   ANV_FROM_HANDLE(anv_semaphore, semaphore, info->semaphore);
+
+   magma_semaphore_t magma_semaphore;
+   magma_status_t status =
+       magma_import_semaphore(magma_connection(device), info->handle, &magma_semaphore);
+   if (status != MAGMA_STATUS_OK)
+      return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR);
+
+   struct anv_semaphore_impl new_impl = {.type = ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ};
+   new_impl.syncobj = magma_semaphore;
+
+   if (info->flags & VK_SEMAPHORE_IMPORT_TEMPORARY_BIT_KHR) {
+      anv_semaphore_impl_cleanup(device, &semaphore->temporary);
+      semaphore->temporary = new_impl;
+   } else {
+      anv_semaphore_impl_cleanup(device, &semaphore->permanent);
+      semaphore->permanent = new_impl;
+   }
+
+   return VK_SUCCESS;
+}
+
+/* Similar to anv_GetSemaphoreFdKHR */
+VkResult anv_GetSemaphoreFuchsiaHandleKHR(VkDevice vk_device,
+                                          const VkSemaphoreGetFuchsiaHandleInfoKHR* info,
+                                          uint32_t* pFuchsiaHandle)
+{
+   ANV_FROM_HANDLE(anv_device, device, vk_device);
+   ANV_FROM_HANDLE(anv_semaphore, semaphore, info->semaphore);
+
+   assert(info->sType == VK_STRUCTURE_TYPE_SEMAPHORE_GET_FUCHSIA_HANDLE_INFO_KHR);
+
+   if (info->handleType != VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_FUCHSIA_FENCE_BIT_KHR)
+      return VK_SUCCESS;
+
+   struct 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
+    *    type’s import operations. [...] If the semaphore was using a
+    *    temporarily imported payload, the semaphore’s prior permanent payload
+    *    will be restored.
+    */
+   anv_semaphore_reset_temporary(device, semaphore);
+
+   *pFuchsiaHandle = handle;
+   return VK_SUCCESS;
+}
+#endif // VK_USE_PLATFORM_MAGMA_KHR
+
+bool anv_gem_supports_syncobj_wait(int fd) { return true; }
+
+anv_syncobj_handle_t anv_gem_syncobj_create(struct anv_device* device, uint32_t flags)
+{
+   magma_semaphore_t semaphore;
+   magma_status_t status = magma_create_semaphore(magma_connection(device), &semaphore);
+   if (status != MAGMA_STATUS_OK) {
+      DLOG("magma_create_semaphore failed: %d", status);
+      return 0;
+   }
+   if (flags & DRM_SYNCOBJ_CREATE_SIGNALED)
+      magma_signal_semaphore(semaphore);
+   return semaphore;
+}
+
+void anv_gem_syncobj_destroy(struct anv_device* device, anv_syncobj_handle_t semaphore)
+{
+   magma_release_semaphore(magma_connection(device), semaphore);
+}
+
+void anv_gem_syncobj_reset(struct anv_device* device, anv_syncobj_handle_t fence)
+{
+   magma_reset_semaphore(fence);
+}
+
+static uint64_t gettime_ns(void)
+{
+   struct timespec current;
+   clock_gettime(CLOCK_MONOTONIC, &current);
+#define NSEC_PER_SEC 1000000000
+   return (uint64_t)current.tv_sec * NSEC_PER_SEC + current.tv_nsec;
+#undef NSEC_PER_SEC   
+}
+
+static int64_t anv_get_relative_timeout(uint64_t abs_timeout)
+{
+   uint64_t now = gettime_ns();
+
+   if (abs_timeout < now)
+      return 0;
+   return abs_timeout - now;
+}
+
+static inline uint64_t ns_to_ms(uint64_t ns) { return ns / 1000000ull; }
+
+int anv_gem_syncobj_wait(struct anv_device* device, anv_syncobj_handle_t* fences, uint32_t fence_count,
+                         int64_t abs_timeout_ns, bool wait_all)
+{
+   int64_t timeout_ns = anv_get_relative_timeout(abs_timeout_ns);
+   magma_status_t status =
+       magma_wait_semaphores(fences, fence_count, ns_to_ms(timeout_ns), wait_all);
+   switch (status) {
+   case MAGMA_STATUS_OK:
+      break;
+   case MAGMA_STATUS_TIMED_OUT:
+      errno = ETIME;
+      // fall through
+   default:
+      return -1;
+   }
+   return 0;
+}
+
+anv_syncobj_handle_t anv_gem_syncobj_fd_to_handle(struct anv_device* device, int fd)
+{
+   assert(false);
+   return 0;
+}
+
+int anv_gem_syncobj_handle_to_fd(struct anv_device* device, anv_syncobj_handle_t handle)
+{
+   assert(false);
+   return -1;
+}
+
+int anv_gem_syncobj_export_sync_file(struct anv_device* device, anv_syncobj_handle_t handle)
+{
+   assert(false);
+   return -1;
+}
+
+int anv_gem_syncobj_import_sync_file(struct anv_device* device, anv_syncobj_handle_t handle, int fd)
+{
+   assert(false);
+   return -1;
+}
+
+int anv_gem_sync_file_merge(struct anv_device* device, int fd1, int fd2)
+{
+   assert(false);
+   return -1;
+}
+
+int anv_gem_set_context_param(int fd, int context, uint32_t param, uint64_t value)
+{
+   assert(false);
+   return -1;
+}
+
+bool anv_gem_has_context_priority(int fd)
+{
+   return false;
+}
diff --git a/src/intel/vulkan/anv_magma.cc b/src/intel/vulkan/anv_magma.cc
deleted file mode 100644
index 982f58b..0000000
--- a/src/intel/vulkan/anv_magma.cc
+++ /dev/null
@@ -1,527 +0,0 @@
-// Copyright 2016 The Fuchsia Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "anv_magma.h"
-#include "drm_command_buffer.h"
-#include "msd_intel_gen_query.h"
-#include <chrono>
-
-int anv_gem_connect(anv_device* device)
-{
-   magma_connection_t* connection = magma_create_connection(device->fd, 0);
-   if (!connection)
-      return DRET_MSG(-1, "magma_system_open failed");
-
-   device->connection = new Connection(connection);
-
-   DLOG("opened a magma system connection");
-   return 0;
-}
-
-void anv_gem_disconnect(anv_device* device)
-{
-   delete static_cast<Connection*>(device->connection);
-   DLOG("closed the magma system connection");
-}
-
-// Return handle, or 0 on failure. Gem handles are never 0.
-anv_buffer_handle_t anv_gem_create(anv_device* device, size_t size)
-{
-   magma_buffer_t buffer;
-   uint64_t magma_size = size;
-   if (magma_create_buffer(magma_connection(device), magma_size, &magma_size, &buffer) != 0) {
-      DLOG("magma_system_alloc failed size 0x%zx", magma_size);
-      return 0;
-   }
-   DLOG("magma_system_alloc size 0x%zx returning buffer 0x%lx", magma_size, buffer);
-
-   DASSERT(buffer != 0);
-   return buffer;
-}
-
-void anv_gem_close(anv_device* device, anv_buffer_handle_t handle)
-{
-   DLOG("anv_gem_close handle 0x%lx", handle);
-   magma_release_buffer(magma_connection(device), handle);
-}
-
-void* anv_gem_mmap(anv_device* device, anv_buffer_handle_t handle, uint64_t offset, uint64_t size,
-                   uint32_t flags)
-{
-   DASSERT(flags == 0);
-   void* addr;
-   magma_status_t status = magma_map(magma_connection(device), handle, &addr);
-   if (status != MAGMA_STATUS_OK) {
-      DLOG("magma_system_map failed: status %d", status);
-      return MAP_FAILED;
-   }
-   DLOG("magma_system_map handle 0x%lx size 0x%zx returning %p", handle, size, addr);
-   return reinterpret_cast<uint8_t*>(addr) + offset;
-}
-
-void anv_gem_munmap(anv_device* device, anv_buffer_handle_t gem_handle, void* addr, uint64_t size)
-{
-   if (!addr)
-      return;
-
-   if (magma_unmap(magma_connection(device), gem_handle) != 0) {
-      DLOG("magma_system_unmap failed");
-      return;
-   }
-
-   DLOG("magma_system_unmap handle 0x%lx", gem_handle);
-}
-
-uint32_t anv_gem_userptr(anv_device* device, void* mem, size_t size)
-{
-   DLOG("anv_gem_userptr - STUB");
-   DASSERT(false);
-   return 0;
-}
-
-int anv_gem_set_caching(anv_device* device, anv_buffer_handle_t gem_handle, uint32_t caching)
-{
-   DLOG("anv_get_set_caching - STUB");
-   return 0;
-}
-
-int anv_gem_set_domain(anv_device* device, anv_buffer_handle_t gem_handle, uint32_t read_domains,
-                       uint32_t write_domain)
-{
-   DLOG("anv_gem_set_domain - STUB");
-   return 0;
-}
-
-/**
- * On error, \a timeout_ns holds the remaining time.
- */
-int anv_gem_wait(anv_device* device, anv_buffer_handle_t handle, int64_t* timeout_ns)
-{
-   DLOG("anv_gem_wait buffer_id %lu timeout_ns %lu\n", magma_get_buffer_id(handle), *timeout_ns);
-
-   magma::InflightList* inflight_list =
-       static_cast<Connection*>(device->connection)->inflight_list();
-
-   auto start = std::chrono::high_resolution_clock::now();
-
-   while (inflight_list->is_inflight(magma_get_buffer_id(handle)) &&
-          std::chrono::duration_cast<std::chrono::nanoseconds>(
-              std::chrono::high_resolution_clock::now() - start)
-                  .count() < *timeout_ns) {
-
-      if (inflight_list->WaitForCompletion(magma::ns_to_ms(*timeout_ns))) {
-         inflight_list->ServiceCompletions(magma_connection(device));
-      }
-   }
-
-   return 0;
-}
-
-/**
- * Returns 0, 1, or negative to indicate error
- */
-int anv_gem_busy(anv_device* device, anv_buffer_handle_t handle)
-{
-   DLOG("anv_gem_busy\n");
-
-   magma::InflightList* inflight_list =
-       static_cast<Connection*>(device->connection)->inflight_list();
-
-   inflight_list->ServiceCompletions(magma_connection(device));
-
-   return inflight_list->is_inflight(magma_get_buffer_id(handle)) ? 1 : 0;
-}
-
-bool anv_gem_supports_48b_addresses(int fd)
-{
-   uint64_t gtt_size;
-   magma_status_t status = magma_query(fd, kMsdIntelGenQueryGttSize, &gtt_size);
-   if (status != MAGMA_STATUS_OK)
-      return DRETF(false, "magma_query failed: %d", status);
-   return gtt_size >= 1ul << 48;
-}
-
-int anv_gem_get_context_param(int fd, int context, uint32_t param, uint64_t* value)
-{
-   magma_status_t status;
-   switch (param) {
-   case I915_CONTEXT_PARAM_GTT_SIZE:
-      status = magma_query(fd, kMsdIntelGenQueryGttSize, value);
-      if (status != MAGMA_STATUS_OK)
-         return DRET_MSG(-1, "magma_query failed: %d", status);
-      return 0;
-   default:
-      return DRET_MSG(-1, "anv_gem_get_context_param: unhandled param 0x%x", param);
-   }
-}
-
-int anv_gem_execbuffer(anv_device* device, drm_i915_gem_execbuffer2* execbuf)
-{
-   DLOG("anv_gem_execbuffer");
-
-   if (execbuf->buffer_count == 0)
-      return 0;
-
-   uint32_t syncobj_count = execbuf->num_cliprects;
-
-   uint64_t required_size = DrmCommandBuffer::RequiredSize(execbuf, syncobj_count);
-
-   uint64_t cmd_buf_id;
-   magma_status_t status =
-       magma_create_command_buffer(magma_connection(device), required_size, &cmd_buf_id);
-   if (status != MAGMA_STATUS_OK)
-      return DRET_MSG(-1, "magma_alloc_command_buffer failed size 0x%" PRIx64 " : %d",
-                      required_size, status);
-
-   void* cmd_buf_data;
-   status = magma_map(magma_connection(device), cmd_buf_id, &cmd_buf_data);
-   if (status != MAGMA_STATUS_OK) {
-      magma_release_command_buffer(magma_connection(device), cmd_buf_id);
-      return DRET_MSG(-1, "magma_system_map failed: %d", status);
-   }
-
-   std::vector<uint64_t> wait_semaphore_ids;
-   std::vector<uint64_t> signal_semaphore_ids;
-
-   for (uint32_t i = 0; i < syncobj_count; i++) {
-      auto& syncobj = reinterpret_cast<drm_i915_gem_exec_fence*>(execbuf->cliprects_ptr)[i];
-      if (syncobj.flags & I915_EXEC_FENCE_WAIT) {
-         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(syncobj.handle));
-      } else {
-         return DRET_MSG(-1, "syncobj not wait or signal");
-      }
-   }
-
-   if (!DrmCommandBuffer::Translate(execbuf, std::move(wait_semaphore_ids),
-                                    std::move(signal_semaphore_ids), cmd_buf_data)) {
-      status = magma_unmap(magma_connection(device), cmd_buf_id);
-      DASSERT(status == MAGMA_STATUS_OK);
-      magma_release_command_buffer(magma_connection(device), cmd_buf_id);
-      return DRET_MSG(-1, "DrmCommandBuffer::Translate failed");
-   }
-
-   status = magma_unmap(magma_connection(device), cmd_buf_id);
-   DASSERT(status == MAGMA_STATUS_OK);
-
-   magma_submit_command_buffer(magma_connection(device), cmd_buf_id, device->context_id);
-
-   magma::InflightList* inflight_list =
-       static_cast<Connection*>(device->connection)->inflight_list();
-
-   for (uint32_t i = 0; i < execbuf->buffer_count; i++) {
-      inflight_list->add(magma_get_buffer_id(
-          reinterpret_cast<drm_i915_gem_exec_object2*>(execbuf->buffers_ptr)[i].handle));
-   }
-
-   inflight_list->ServiceCompletions(magma_connection(device));
-
-   return 0;
-}
-
-int anv_gem_set_tiling(anv_device* device, anv_buffer_handle_t gem_handle, uint32_t stride,
-                       uint32_t tiling)
-{
-   DLOG("anv_gem_set_tiling - STUB");
-   return 0;
-}
-
-int anv_gem_get_param(int fd, uint32_t param)
-{
-   magma_status_t status = MAGMA_STATUS_OK;
-   uint64_t value;
-
-   switch (param) {
-   case I915_PARAM_CHIPSET_ID:
-      status = magma_query(fd, MAGMA_QUERY_DEVICE_ID, &value);
-      break;
-   case I915_PARAM_SUBSLICE_TOTAL:
-      status = magma_query(fd, kMsdIntelGenQuerySubsliceAndEuTotal, &value);
-      value >>= 32;
-      break;
-   case I915_PARAM_EU_TOTAL:
-      status = magma_query(fd, kMsdIntelGenQuerySubsliceAndEuTotal, &value);
-      value = static_cast<uint32_t>(value);
-      break;
-   case I915_PARAM_HAS_WAIT_TIMEOUT:
-   case I915_PARAM_HAS_EXECBUF2:
-      value = 1;
-      break;
-   case I915_PARAM_HAS_EXEC_FENCE_ARRAY: // Used for semaphores
-      value = 1;
-      break;
-   default:
-      status = MAGMA_STATUS_INVALID_ARGS;
-   }
-
-   if (status != MAGMA_STATUS_OK)
-      value = 0;
-
-   uint32_t result = static_cast<uint32_t>(value);
-   DASSERT(result == value);
-   DLOG("anv_gem_get_param(%u, %u) returning %d", fd, param, result);
-   return result;
-}
-
-bool anv_gem_get_bit6_swizzle(int fd, uint32_t tiling)
-{
-   DLOG("anv_gem_get_bit6_swizzle - STUB");
-   return 0;
-}
-
-int anv_gem_create_context(anv_device* device)
-{
-   uint32_t context_id;
-   magma_create_context(magma_connection(device), &context_id);
-   DLOG("magma_system_create_context returned context_id %u", context_id);
-
-   return static_cast<int>(context_id);
-}
-
-int anv_gem_destroy_context(anv_device* device, int context_id)
-{
-   magma_release_context(magma_connection(device), context_id);
-   return 0;
-}
-
-int anv_gem_get_aperture(int fd, uint64_t* size)
-{
-   DLOG("anv_gem_get_aperture - STUB");
-   return 0;
-}
-
-int anv_gem_handle_to_fd(anv_device* device, anv_buffer_handle_t gem_handle)
-{
-   DASSERT(false);
-   return -1;
-}
-
-anv_buffer_handle_t anv_gem_fd_to_handle(anv_device* device, int fd)
-{
-   DASSERT(false);
-   return 0;
-}
-
-int anv_gem_gpu_get_reset_stats(anv_device* device, uint32_t* active, uint32_t* pending)
-{
-   DLOG("anv_gem_gpu_get_reset_stats - STUB");
-   *active = 0;
-   *pending = 0;
-   return 0;
-}
-
-int anv_gem_import_fuchsia_buffer(anv_device* device, uint32_t handle,
-                                  anv_buffer_handle_t* buffer_out, uint64_t* size_out)
-{
-   magma_status_t status = magma_import(magma_connection(device), handle, buffer_out);
-   if (status != MAGMA_STATUS_OK)
-      return DRET_MSG(-EINVAL, "magma_import failed: %d", status);
-
-   *size_out = magma_get_buffer_size(*buffer_out);
-   return 0;
-}
-
-#if VK_USE_PLATFORM_MAGMA_KHR
-VkResult anv_GetMemoryFuchsiaHandleKHR(VkDevice vk_device,
-                                       const VkMemoryGetFuchsiaHandleInfoKHR* pGetFuchsiaHandleInfo,
-                                       uint32_t* pHandle)
-{
-   ANV_FROM_HANDLE(anv_device, device, vk_device);
-   ANV_FROM_HANDLE(anv_device_memory, memory, pGetFuchsiaHandleInfo->memory);
-
-   assert(pGetFuchsiaHandleInfo->sType == VK_STRUCTURE_TYPE_MEMORY_GET_FUCHSIA_HANDLE_INFO_KHR);
-   assert(pGetFuchsiaHandleInfo->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_FUCHSIA_VMO_BIT_KHR);
-
-   auto result = magma_export(magma_connection(device), memory->bo->gem_handle, pHandle);
-   DASSERT(result == MAGMA_STATUS_OK);
-
-   return VK_SUCCESS;
-}
-
-VkResult anv_GetMemoryFuchsiaHandlePropertiesKHR(
-    VkDevice vk_device, VkExternalMemoryHandleTypeFlagBitsKHR handleType, uint32_t handle,
-    VkMemoryFuchsiaHandlePropertiesKHR* pMemoryFuchsiaHandleProperties)
-{
-   ANV_FROM_HANDLE(anv_device, device, vk_device);
-
-   assert(handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_FUCHSIA_VMO_BIT_KHR);
-   assert(pMemoryFuchsiaHandleProperties->sType ==
-          VK_STRUCTURE_TYPE_MEMORY_FUCHSIA_HANDLE_PROPERTIES_KHR);
-
-   struct anv_physical_device* pdevice = &device->instance->physicalDevice;
-   // All memory types supported
-   pMemoryFuchsiaHandleProperties->memoryTypeBits = (1ull << pdevice->memory.type_count) - 1;
-
-   return VK_SUCCESS;
-}
-
-/* Similar to anv_ImportSemaphoreFdKHR */
-VkResult anv_ImportSemaphoreFuchsiaHandleKHR(VkDevice vk_device,
-                                             const VkImportSemaphoreFuchsiaHandleInfoKHR* info)
-{
-   assert(info->sType == VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FUCHSIA_HANDLE_INFO_KHR);
-   assert(info->handleType == VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_FUCHSIA_FENCE_BIT_KHR);
-
-   ANV_FROM_HANDLE(anv_device, device, vk_device);
-   ANV_FROM_HANDLE(anv_semaphore, semaphore, info->semaphore);
-
-   magma_semaphore_t magma_semaphore;
-   magma_status_t status =
-       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 = magma_semaphore;
-
-   if (info->flags & VK_SEMAPHORE_IMPORT_TEMPORARY_BIT_KHR) {
-      anv_semaphore_impl_cleanup(device, &semaphore->temporary);
-      semaphore->temporary = new_impl;
-   } else {
-      anv_semaphore_impl_cleanup(device, &semaphore->permanent);
-      semaphore->permanent = new_impl;
-   }
-
-   return VK_SUCCESS;
-}
-
-/* Similar to anv_GetSemaphoreFdKHR */
-VkResult anv_GetSemaphoreFuchsiaHandleKHR(VkDevice vk_device,
-                                          const VkSemaphoreGetFuchsiaHandleInfoKHR* info,
-                                          uint32_t* pFuchsiaHandle)
-{
-   ANV_FROM_HANDLE(anv_device, device, vk_device);
-   ANV_FROM_HANDLE(anv_semaphore, semaphore, info->semaphore);
-
-   assert(info->sType == VK_STRUCTURE_TYPE_SEMAPHORE_GET_FUCHSIA_HANDLE_INFO_KHR);
-
-   if (info->handleType != VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_FUCHSIA_FENCE_BIT_KHR)
-      return VK_SUCCESS;
-
-   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
-    *    type’s import operations. [...] If the semaphore was using a
-    *    temporarily imported payload, the semaphore’s prior permanent payload
-    *    will be restored.
-    */
-   anv_semaphore_reset_temporary(device, semaphore);
-
-   *pFuchsiaHandle = handle;
-   return VK_SUCCESS;
-}
-#endif // VK_USE_PLATFORM_MAGMA_KHR
-
-bool anv_gem_supports_syncobj_wait(int fd) { return true; }
-
-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);
-   if (status != MAGMA_STATUS_OK) {
-      DLOG("magma_create_semaphore failed: %d", status);
-      return 0;
-   }
-   if (flags & DRM_SYNCOBJ_CREATE_SIGNALED)
-      magma_signal_semaphore(semaphore);
-   return semaphore;
-}
-
-void anv_gem_syncobj_destroy(anv_device* device, anv_syncobj_handle_t semaphore)
-{
-   magma_release_semaphore(magma_connection(device), semaphore);
-}
-
-void anv_gem_syncobj_reset(anv_device* device, anv_syncobj_handle_t fence)
-{
-   magma_reset_semaphore(fence);
-}
-
-static uint64_t gettime_ns(void)
-{
-   struct timespec current;
-   clock_gettime(CLOCK_MONOTONIC, &current);
-#define NSEC_PER_SEC 1000000000
-   return (uint64_t)current.tv_sec * NSEC_PER_SEC + current.tv_nsec;
-#undef NSEC_PER_SEC   
-}
-
-static int64_t anv_get_relative_timeout(uint64_t abs_timeout)
-{
-   uint64_t now = gettime_ns();
-
-   if (abs_timeout < now)
-      return 0;
-   return abs_timeout - now;
-}
-
-int anv_gem_syncobj_wait(anv_device* device, anv_syncobj_handle_t* fences, uint32_t fence_count,
-                         int64_t abs_timeout_ns, bool wait_all)
-{
-   int64_t timeout_ns = anv_get_relative_timeout(abs_timeout_ns);
-   magma_status_t status =
-       magma_wait_semaphores(fences, fence_count, magma::ns_to_ms(timeout_ns), wait_all);
-   switch (status) {
-   case MAGMA_STATUS_OK:
-      break;
-   case MAGMA_STATUS_TIMED_OUT:
-      errno = ETIME;
-      // fall through
-   default:
-      return -1;
-   }
-   return 0;
-}
-
-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, anv_syncobj_handle_t handle)
-{
-   DASSERT(false);
-   return -1;
-}
-
-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, anv_syncobj_handle_t handle, int fd)
-{
-   DASSERT(false);
-   return -1;
-}
-
-int anv_gem_sync_file_merge(anv_device* device, int fd1, int fd2)
-{
-   DASSERT(false);
-   return -1;
-}
-
-int anv_gem_set_context_param(int fd, int context, uint32_t param, uint64_t value)
-{
-   DASSERT(false);
-   return -1;
-}
-
-bool anv_gem_has_context_priority(int fd)
-{
-   return false;
-}
diff --git a/src/intel/vulkan/anv_magma.h b/src/intel/vulkan/anv_magma.h
index ebdd8d0..cad49fe 100644
--- a/src/intel/vulkan/anv_magma.h
+++ b/src/intel/vulkan/anv_magma.h
@@ -5,37 +5,32 @@
 #ifndef ANV_MAGMA_H
 #define ANV_MAGMA_H
 
+// Don't include anv_private.h here; this header is included by the
+// c++ implementation anv_magma_connection.cc.
+#include "i915_drm.h"
 #include "magma.h"
-#include "magma_util/dlog.h"
-#include "magma_util/inflight_list.h"
-#include "magma_util/macros.h"
-// clang-format off
-#include "anv_private.h"
-// clang-format on
 
-class Connection : public anv_connection {
-public:
-   Connection(magma_connection_t* magma_connection)
-       : magma_connection_(magma_connection), inflight_list_(magma_connection)
-   {
-   }
-
-   ~Connection() { magma_release_connection(magma_connection_); }
-
-   magma_connection_t* magma_connection() { return magma_connection_; }
-
-   magma::InflightList* inflight_list() { return &inflight_list_; }
-
-private:
-   magma_connection_t* magma_connection_;
-   magma::InflightList inflight_list_;
+struct anv_connection {
+   magma_connection_t* connection;
 };
 
-static magma_connection_t* magma_connection(anv_device* device)
-{
-   DASSERT(device);
-   DASSERT(device->connection);
-   return static_cast<Connection*>(device->connection)->magma_connection();
-}
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// Transfer ownership of the |connection|.
+struct anv_connection* AnvMagmaCreateConnection(magma_connection_t* connection);
+
+void AnvMagmaReleaseConnection(struct anv_connection* connection);
+
+void AnvMagmaConnectionWait(struct anv_connection* connection, uint64_t buffer_id, int64_t* timeout_ns);
+
+int AnvMagmaConnectionIsBusy(struct anv_connection* connection, uint64_t buffer_id);
+
+int AnvMagmaConnectionExec(struct anv_connection* connection, uint32_t context_id, struct drm_i915_gem_execbuffer2* execbuf);
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
 
 #endif // ANV_MAGMA_H
diff --git a/src/intel/vulkan/anv_magma_connection.cc b/src/intel/vulkan/anv_magma_connection.cc
new file mode 100644
index 0000000..53723d4
--- /dev/null
+++ b/src/intel/vulkan/anv_magma_connection.cc
@@ -0,0 +1,127 @@
+// Copyright 2018 The Fuchsia Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "anv_magma.h"
+#include "drm_command_buffer.h"
+#include "magma_util/inflight_list.h"
+#include "magma_util/macros.h"
+#include <chrono>
+
+class Connection : public anv_connection {
+public:
+   Connection(magma_connection_t* magma_connection)
+       : inflight_list_(magma_connection)
+   {
+      anv_connection::connection = magma_connection;
+   }
+
+   ~Connection() { magma_release_connection(magma_connection()); }
+
+   magma_connection_t* magma_connection() { return anv_connection::connection; }
+
+   magma::InflightList* inflight_list() { return &inflight_list_; }
+
+   static Connection* cast(anv_connection* connection) { return static_cast<Connection*>(connection); }
+
+private:
+   magma::InflightList inflight_list_;
+};
+
+anv_connection* AnvMagmaCreateConnection(magma_connection_t* connection)
+{
+   return new Connection(connection);
+}
+
+void AnvMagmaReleaseConnection(anv_connection* connection)
+{
+   delete static_cast<Connection*>(connection);
+}
+
+void AnvMagmaConnectionWait(anv_connection* connection, uint64_t buffer_id, int64_t* timeout_ns)
+{
+   magma::InflightList* inflight_list = Connection::cast(connection)->inflight_list();
+
+   auto start = std::chrono::high_resolution_clock::now();
+
+   while (inflight_list->is_inflight(buffer_id) &&
+          std::chrono::duration_cast<std::chrono::nanoseconds>(
+              std::chrono::high_resolution_clock::now() - start)
+                  .count() < *timeout_ns) {
+
+      if (inflight_list->WaitForCompletion(magma::ns_to_ms(*timeout_ns))) {
+         inflight_list->ServiceCompletions(Connection::cast(connection)->magma_connection());
+      }
+   }
+}
+
+int AnvMagmaConnectionIsBusy(anv_connection* connection, uint64_t buffer_id)
+{
+   magma::InflightList* inflight_list = Connection::cast(connection)->inflight_list();
+
+   inflight_list->ServiceCompletions(Connection::cast(connection)->magma_connection());
+
+   return inflight_list->is_inflight(buffer_id) ? 1 : 0;
+}
+
+int AnvMagmaConnectionExec(anv_connection* connection, uint32_t context_id, struct drm_i915_gem_execbuffer2* execbuf)
+{
+   if (execbuf->buffer_count == 0)
+      return 0;
+
+   uint32_t syncobj_count = execbuf->num_cliprects;
+
+   uint64_t required_size = DrmCommandBuffer::RequiredSize(execbuf, syncobj_count);
+
+   uint64_t cmd_buf_id;
+   magma_status_t status =
+       magma_create_command_buffer(Connection::cast(connection)->magma_connection(), required_size, &cmd_buf_id);
+   if (status != MAGMA_STATUS_OK)
+      return DRET_MSG(-1, "magma_alloc_command_buffer failed size 0x%" PRIx64 " : %d",
+                      required_size, status);
+
+   void* cmd_buf_data;
+   status = magma_map(Connection::cast(connection)->magma_connection(), cmd_buf_id, &cmd_buf_data);
+   if (status != MAGMA_STATUS_OK) {
+      magma_release_command_buffer(Connection::cast(connection)->magma_connection(), cmd_buf_id);
+      return DRET_MSG(-1, "magma_system_map failed: %d", status);
+   }
+
+   std::vector<uint64_t> wait_semaphore_ids;
+   std::vector<uint64_t> signal_semaphore_ids;
+
+   for (uint32_t i = 0; i < syncobj_count; i++) {
+      auto& syncobj = reinterpret_cast<drm_i915_gem_exec_fence*>(execbuf->cliprects_ptr)[i];
+      if (syncobj.flags & I915_EXEC_FENCE_WAIT) {
+         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(syncobj.handle));
+      } else {
+         return DRET_MSG(-1, "syncobj not wait or signal");
+      }
+   }
+
+   if (!DrmCommandBuffer::Translate(execbuf, std::move(wait_semaphore_ids),
+                                    std::move(signal_semaphore_ids), cmd_buf_data)) {
+      status = magma_unmap(Connection::cast(connection)->magma_connection(), cmd_buf_id);
+      DASSERT(status == MAGMA_STATUS_OK);
+      magma_release_command_buffer(Connection::cast(connection)->magma_connection(), cmd_buf_id);
+      return DRET_MSG(-1, "DrmCommandBuffer::Translate failed");
+   }
+
+   status = magma_unmap(Connection::cast(connection)->magma_connection(), cmd_buf_id);
+   DASSERT(status == MAGMA_STATUS_OK);
+
+   magma_submit_command_buffer(Connection::cast(connection)->magma_connection(), cmd_buf_id, context_id);
+
+   magma::InflightList* inflight_list = Connection::cast(connection)->inflight_list();
+
+   for (uint32_t i = 0; i < execbuf->buffer_count; i++) {
+      inflight_list->add(magma_get_buffer_id(
+          reinterpret_cast<drm_i915_gem_exec_object2*>(execbuf->buffers_ptr)[i].handle));
+   }
+
+   inflight_list->ServiceCompletions(Connection::cast(connection)->magma_connection());
+
+   return 0;
+}
diff --git a/src/intel/vulkan/anv_private.h b/src/intel/vulkan/anv_private.h
index f5ec764..50df89c 100644
--- a/src/intel/vulkan/anv_private.h
+++ b/src/intel/vulkan/anv_private.h
@@ -78,10 +78,6 @@
 #include <vulkan/vk_icd.h>
 #include <vulkan/vk_android_native_buffer.h>
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
 #include "anv_entrypoints.h"
 #include "anv_extensions.h"
 #include "isl/isl.h"
@@ -520,7 +516,7 @@
                      const VkAllocationCallbacks *alloc,
                      VkSystemAllocationScope scope)
 {
-   uint8_t *ptr = (uint8_t*) vk_alloc(alloc, ma->size, ma->align, scope);
+   void *ptr = vk_alloc(alloc, ma->size, ma->align, scope);
    if (!ptr)
       return NULL;
 
@@ -986,10 +982,7 @@
                          uint32_t prog_data_size,
                          const struct anv_pipeline_bind_map *bind_map);
 
-/* May be extended by the anv_gem implementation */
-struct anv_connection {
-   uint32_t unused;
-};
+struct anv_connection;
 
 struct anv_device {
     VK_LOADER_DATA                              _loader_data;
@@ -1095,17 +1088,19 @@
 void anv_device_init_blorp(struct anv_device *device);
 void anv_device_finish_blorp(struct anv_device *device);
 
-VkResult anv_device_execbuf(struct anv_device* device, struct drm_i915_gem_execbuffer2* execbuf,
-                            struct anv_bo** execbuf_bos);
+VkResult anv_device_execbuf(struct anv_device *device,
+                            struct drm_i915_gem_execbuffer2 *execbuf,
+                            struct anv_bo **execbuf_bos);
 VkResult anv_device_query_status(struct anv_device *device);
 VkResult anv_device_bo_busy(struct anv_device *device, struct anv_bo *bo);
 VkResult anv_device_wait(struct anv_device *device, struct anv_bo *bo,
-                             int64_t timeout);
+                         int64_t timeout);
     
 int anv_gem_connect(struct anv_device* device);
 void anv_gem_disconnect(struct anv_device* device);
 
-void* anv_gem_mmap(struct anv_device* device, anv_buffer_handle_t gem_handle, uint64_t offset,
+void* anv_gem_mmap(struct anv_device* device,
+                   anv_buffer_handle_t gem_handle, uint64_t offset,
                    uint64_t size, uint32_t flags);
 void anv_gem_munmap(struct anv_device* device, anv_buffer_handle_t gem_handle, void *p, uint64_t size);
 anv_buffer_handle_t anv_gem_create(struct anv_device* device, uint64_t size);
@@ -1113,7 +1108,8 @@
 uint32_t anv_gem_userptr(struct anv_device *device, void *mem, size_t size);
 int anv_gem_busy(struct anv_device *device, anv_buffer_handle_t gem_handle);
 int anv_gem_wait(struct anv_device* device, anv_buffer_handle_t gem_handle, int64_t* timeout_ns);
-int anv_gem_execbuffer(struct anv_device* device, struct drm_i915_gem_execbuffer2* execbuf);
+int anv_gem_execbuffer(struct anv_device* device,
+                       struct drm_i915_gem_execbuffer2* execbuf);
 int anv_gem_set_tiling(struct anv_device* device, anv_buffer_handle_t gem_handle,
                        uint32_t stride, uint32_t tiling);
 int anv_gem_create_context(struct anv_device *device);
@@ -1132,7 +1128,7 @@
 int anv_gem_handle_to_fd(struct anv_device *device, anv_buffer_handle_t gem_handle);
 anv_buffer_handle_t anv_gem_fd_to_handle(struct anv_device *device, int fd);
 int anv_gem_set_caching(struct anv_device *device, anv_buffer_handle_t gem_handle, uint32_t caching);
-int anv_gem_set_domain(struct anv_device *device, uint32_t gem_handle,
+int anv_gem_set_domain(struct anv_device *device, anv_buffer_handle_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);
 anv_syncobj_handle_t anv_gem_syncobj_create(struct anv_device *device, uint32_t flags);
@@ -1639,7 +1635,7 @@
 anv_descriptor_set_write_template(struct anv_descriptor_set *set,
                                   struct anv_device *device,
                                   struct anv_state_stream *alloc_stream,
-                                  const struct anv_descriptor_update_template *update_template,
+                                  const struct anv_descriptor_update_template *template,
                                   const void *data);
 
 VkResult
@@ -1772,10 +1768,10 @@
    ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT | \
    ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT)
 
-static inline uint32_t
+static inline enum anv_pipe_bits
 anv_pipe_flush_bits_for_access_flags(VkAccessFlags flags)
 {
-   uint32_t pipe_bits = 0;
+   enum anv_pipe_bits pipe_bits = 0;
 
    unsigned b;
    for_each_bit(b, flags) {
@@ -1804,10 +1800,10 @@
    return pipe_bits;
 }
 
-static inline uint32_t
+static inline enum anv_pipe_bits
 anv_pipe_invalidate_bits_for_access_flags(VkAccessFlags flags)
 {
-   uint32_t pipe_bits = 0;
+   enum anv_pipe_bits pipe_bits = 0;
 
    unsigned b;
    for_each_bit(b, flags) {
@@ -2156,8 +2152,8 @@
 void anv_cmd_buffer_add_secondary(struct anv_cmd_buffer *primary,
                                   struct anv_cmd_buffer *secondary);
 void anv_cmd_buffer_prepare_execbuf(struct anv_cmd_buffer *cmd_buffer);
-VkResult anv_cmd_buffer_execbuf(struct anv_device* device, 
-                                struct anv_cmd_buffer* cmd_buffer,
+VkResult anv_cmd_buffer_execbuf(struct anv_device *device,
+                                struct anv_cmd_buffer *cmd_buffer,
                                 const VkSemaphore *in_semaphores,
                                 uint32_t num_in_semaphores,
                                 const VkSemaphore *out_semaphores,
@@ -2354,23 +2350,17 @@
    char                                         data[0];
 };
 
-#ifdef __cplusplus
-#define ENUM_FROM_INT(type, val) static_cast<type>(val)
-#else
-#define ENUM_FROM_INT(type, val) (val)
-#endif
-
 static inline gl_shader_stage
 vk_to_mesa_shader_stage(VkShaderStageFlagBits vk_stage)
 {
    assert(__builtin_popcount(vk_stage) == 1);
-   return ENUM_FROM_INT(gl_shader_stage, ffs(vk_stage) - 1);
+   return ffs(vk_stage) - 1;
 }
 
 static inline VkShaderStageFlagBits
 mesa_to_vk_shader_stage(gl_shader_stage mesa_stage)
 {
-   return ENUM_FROM_INT(VkShaderStageFlagBits, 1 << mesa_stage);
+   return (1 << mesa_stage);
 }
 
 #define ANV_STAGE_MASK ((1 << MESA_SHADER_STAGES) - 1)
@@ -2653,7 +2643,7 @@
 anv_get_isl_format(const struct gen_device_info *devinfo, VkFormat vk_format,
                    VkImageAspectFlags aspect, VkImageTiling tiling)
 {
-   return anv_get_format_plane(devinfo, vk_format, (VkImageAspectFlagBits)aspect, tiling).isl_format;
+   return anv_get_format_plane(devinfo, vk_format, aspect, tiling).isl_format;
 }
 
 static inline struct isl_swizzle
@@ -3410,8 +3400,4 @@
 #  undef genX
 #endif
 
-#ifdef __cplusplus
-} // extern "C"
-#endif
-
 #endif /* ANV_PRIVATE_H */
diff --git a/src/vulkan/util/vk_alloc.h b/src/vulkan/util/vk_alloc.h
index 69cead7..2e807a9 100644
--- a/src/vulkan/util/vk_alloc.h
+++ b/src/vulkan/util/vk_alloc.h
@@ -75,7 +75,7 @@
       return NULL;
 
    size_t size = strlen(s) + 1;
-   char *copy = (char*) vk_alloc(alloc, size, 1, scope);
+   char *copy = vk_alloc(alloc, size, 1, scope);
    if (copy == NULL)
       return NULL;