Implement Vulkan API for reading the display's size

 * magma: Add Magma APIs for reading the display size.  Add public API
   in magma.h and add driver API in msd.h.  Add IPC plumbing (an
   ioctl) for reading the display size.

 * drivers/gpu/msd-intel-gen: Implement the driver API by reading the
   display size from the register state.

 * third_party/mesa: Implement the Vulkan API --
   vkGetPhysicalDeviceSurfacePresentModesKHR() -- in terms of the
   Magma public API.

Note that the vkcube demo already uses that Vulkan API, so it will
start using the actual display size (rather than a default) after this
change.

MA-214

Change-Id: Ifc0e75104205d16eb1e0c968d0624c21c5e0484d
diff --git a/include/vulkan/vk_icd.h b/include/vulkan/vk_icd.h
index f06610b..a06e7c8 100644
--- a/include/vulkan/vk_icd.h
+++ b/include/vulkan/vk_icd.h
@@ -86,6 +86,7 @@
 #ifdef VK_USE_PLATFORM_MAGMA_KHR
 typedef struct _VkIcdSurfaceMagma {
    VkIcdSurfaceBase base;
+   VkExtent2D       display_size;
 } VkIcdSurfaceMagma;
 #endif // VK_USE_PLATFORM_MAGMA_KHR
 
diff --git a/src/intel/vulkan/anv_device.c b/src/intel/vulkan/anv_device.c
index 12be23b..59d7286 100644
--- a/src/intel/vulkan/anv_device.c
+++ b/src/intel/vulkan/anv_device.c
@@ -67,6 +67,7 @@
    fd = open(path, O_RDONLY);
    if (fd < 0)
       return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
+   device->device_fd = fd;
 
    device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
    device->instance = instance;
@@ -186,7 +187,6 @@
    /* XXX: Actually detect bit6 swizzling */
    isl_device_init(&device->isl_dev, &device->info, swizzled);
 
-   close(fd);
    return VK_SUCCESS;
 
 fail:
@@ -199,6 +199,8 @@
 {
    anv_finish_wsi(device);
    ralloc_free(device->compiler);
+   int result = close(device->device_fd);
+   assert(result == 0);
 }
 
 static const VkExtensionProperties global_extensions[] = {
diff --git a/src/intel/vulkan/anv_private.h b/src/intel/vulkan/anv_private.h
index 521a34b..98e7090 100644
--- a/src/intel/vulkan/anv_private.h
+++ b/src/intel/vulkan/anv_private.h
@@ -493,6 +493,7 @@
     struct anv_instance *                       instance;
     uint32_t                                    chipset_id;
     char                                        path[64];
+    int                                         device_fd;
     const char *                                name;
     struct gen_device_info                      info;
     uint64_t                                    aperture_size;
diff --git a/src/intel/vulkan/anv_wsi_magma.cc b/src/intel/vulkan/anv_wsi_magma.cc
index 1fb85f0ab..80d638e 100644
--- a/src/intel/vulkan/anv_wsi_magma.cc
+++ b/src/intel/vulkan/anv_wsi_magma.cc
@@ -26,6 +26,10 @@
 
    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_MAGMA_SURFACE_CREATE_INFO_KHR);
 
+   magma_display_size display_size;
+   if (magma_display_get_size(instance->physicalDevice.device_fd, &display_size) != MAGMA_STATUS_OK)
+      return VK_ERROR_INITIALIZATION_FAILED;
+
    if (!pAllocator)
       pAllocator = &instance->alloc;
 
@@ -35,6 +39,8 @@
       return VK_ERROR_OUT_OF_HOST_MEMORY;
 
    surface->base.platform = VK_ICD_WSI_PLATFORM_MAGMA;
+   surface->display_size.width = display_size.width;
+   surface->display_size.height = display_size.height;
 
    *pSurface = _VkIcdSurfaceBase_to_handle(&surface->base);
 
diff --git a/src/vulkan/wsi/wsi_common_magma.cc b/src/vulkan/wsi/wsi_common_magma.cc
index 780c704..f0c583b 100644
--- a/src/vulkan/wsi/wsi_common_magma.cc
+++ b/src/vulkan/wsi/wsi_common_magma.cc
@@ -343,11 +343,11 @@
 {
    DLOG("magma_surface_get_capabilities");
 
-   VkExtent2D any_extent = {0xFFFFFFFF, 0xFFFFFFFF};
+   auto* magma_surface = reinterpret_cast<VkIcdSurfaceMagma*>(icd_surface);
 
    caps->minImageExtent = {1, 1};
-   caps->maxImageExtent = any_extent;
-   caps->currentExtent = any_extent;
+   caps->maxImageExtent = magma_surface->display_size;
+   caps->currentExtent = magma_surface->display_size;
 
    caps->supportedCompositeAlpha =
        VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR | VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;