v3dv: don't free BOs from imported memory objects

Only free the underlying BO when the exported memory object is freed
to avoid multiple frees of the same memory.

The only exception is winsys BOs where we import a BO created in the
display device into the render device. In this case, we only have one
memory object referencing the BO and we want to destroy it with that
memory object.

Fixes:
dEQP-VK.api.external.memory.dma_buf.*
dEQP-VK.api.external.memory.opaque_fd.*

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6766>
diff --git a/src/broadcom/vulkan/v3dv_device.c b/src/broadcom/vulkan/v3dv_device.c
index c9d32a9..c897e6e 100644
--- a/src/broadcom/vulkan/v3dv_device.c
+++ b/src/broadcom/vulkan/v3dv_device.c
@@ -1373,7 +1373,10 @@
 static void
 device_free(struct v3dv_device *device, struct v3dv_device_memory *mem)
 {
-   v3dv_bo_free(device, mem->bo);
+   if (mem->has_bo_ownership)
+      v3dv_bo_free(device, mem->bo);
+   else if (mem->bo)
+      vk_free(&device->alloc, mem->bo);
 }
 
 static void
@@ -1457,6 +1460,7 @@
    (*bo)->offset = get_offset.offset;
    (*bo)->map = NULL;
    (*bo)->map_size = 0;
+   (*bo)->private = false;
 
    return VK_SUCCESS;
 
@@ -1506,7 +1510,6 @@
    if (result != VK_SUCCESS)
       goto fail_import;
 
-
    return VK_SUCCESS;
 
 fail_import:
@@ -1544,6 +1547,7 @@
 
    assert(pAllocateInfo->memoryTypeIndex < pdevice->memory.memoryTypeCount);
    mem->type = &pdevice->memory.memoryTypes[pAllocateInfo->memoryTypeIndex];
+   mem->has_bo_ownership = true;
 
    const struct wsi_memory_allocate_info *wsi_info = NULL;
    const VkImportMemoryFdInfoKHR *fd_info = NULL;
@@ -1571,6 +1575,7 @@
       result = device_import_bo(device, pAllocator,
                                 fd_info->fd, pAllocateInfo->allocationSize,
                                 &mem->bo);
+      mem->has_bo_ownership = false;
       if (result == VK_SUCCESS)
          close(fd_info->fd);
    } else {
diff --git a/src/broadcom/vulkan/v3dv_private.h b/src/broadcom/vulkan/v3dv_private.h
index b21c11e..276fc1a 100644
--- a/src/broadcom/vulkan/v3dv_private.h
+++ b/src/broadcom/vulkan/v3dv_private.h
@@ -312,6 +312,7 @@
 struct v3dv_device_memory {
    struct v3dv_bo *bo;
    const VkMemoryType *type;
+   bool has_bo_ownership;
 };
 
 #define V3D_OUTPUT_IMAGE_FORMAT_NO 255