vulkan/wsi: Use VK_EXT_pci_bus_info for DRM fd matching
authorJason Ekstrand <jason.ekstrand@intel.com>
Thu, 18 Oct 2018 15:08:32 +0000 (10:08 -0500)
committerJason Ekstrand <jason.ekstrand@intel.com>
Thu, 18 Oct 2018 16:29:00 +0000 (11:29 -0500)
This lets us avoid passing the DRM fd around all over the place and gets
us closer to layer utopia.

Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com>
Reviewed-by: Eric Engestrom <eric.engestrom@intel.com>
src/amd/vulkan/radv_wsi.c
src/amd/vulkan/radv_wsi_x11.c
src/intel/vulkan/anv_wsi.c
src/intel/vulkan/anv_wsi_x11.c
src/vulkan/wsi/wsi_common.c
src/vulkan/wsi/wsi_common.h
src/vulkan/wsi/wsi_common_display.c
src/vulkan/wsi/wsi_common_private.h
src/vulkan/wsi/wsi_common_wayland.c
src/vulkan/wsi/wsi_common_x11.c
src/vulkan/wsi/wsi_common_x11.h

index 43103a4ef85a1335c604382ed29cf8d7793844e5..346fb43d67541f67628e0fc1417382b65749fcce 100644 (file)
@@ -72,7 +72,6 @@ VkResult radv_GetPhysicalDeviceSurfaceSupportKHR(
        RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
 
        return wsi_common_get_surface_support(&device->wsi_device,
-                                             device->local_fd,
                                              queueFamilyIndex,
                                              surface,
                                              pSupported);
@@ -171,7 +170,6 @@ VkResult radv_CreateSwapchainKHR(
 
        return wsi_common_create_swapchain(&device->physical_device->wsi_device,
                                           radv_device_to_handle(device),
-                                          device->physical_device->local_fd,
                                           pCreateInfo,
                                           alloc,
                                           pSwapchain);
@@ -294,7 +292,6 @@ VkResult radv_GetPhysicalDevicePresentRectanglesKHR(
        RADV_FROM_HANDLE(radv_physical_device, device, physicalDevice);
 
        return wsi_common_get_present_rectangles(&device->wsi_device,
-                                                device->local_fd,
                                                 surface,
                                                 pRectCount, pRects);
 }
index 9ef02ccc43591cd8eef349f7f49f95d12026ca44..f3ebc791e2dc8a8fe8d83e5851cb9b6667a84310 100644 (file)
@@ -45,7 +45,7 @@ VkBool32 radv_GetPhysicalDeviceXcbPresentationSupportKHR(
    return wsi_get_physical_device_xcb_presentation_support(
       &device->wsi_device,
       queueFamilyIndex,
-      device->local_fd, true,
+      true,
       connection, visual_id);
 }
 
@@ -60,7 +60,7 @@ VkBool32 radv_GetPhysicalDeviceXlibPresentationSupportKHR(
    return wsi_get_physical_device_xcb_presentation_support(
       &device->wsi_device,
       queueFamilyIndex,
-      device->local_fd, true,
+      true,
       XGetXCBConnection(dpy), visualID);
 }
 
index 5d672c211c471179646bce1d9439d243fe30ecd3..024bc1c245dac420599557a3c91e99fbb12c6c29 100644 (file)
@@ -89,7 +89,6 @@ VkResult anv_GetPhysicalDeviceSurfaceSupportKHR(
    ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
 
    return wsi_common_get_surface_support(&device->wsi_device,
-                                         device->local_fd,
                                          queueFamilyIndex,
                                          surface,
                                          pSupported);
@@ -183,7 +182,7 @@ VkResult anv_CreateSwapchainKHR(
    else
      alloc = &device->alloc;
 
-   return wsi_common_create_swapchain(wsi_device, _device, device->fd,
+   return wsi_common_create_swapchain(wsi_device, _device,
                                       pCreateInfo, alloc, pSwapchain);
 }
 
@@ -303,7 +302,6 @@ VkResult anv_GetPhysicalDevicePresentRectanglesKHR(
    ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
 
    return wsi_common_get_present_rectangles(&device->wsi_device,
-                                            device->local_fd,
                                             surface,
                                             pRectCount, pRects);
 }
index 45c43f6f17fbbcb490dc014976f440758c9574f3..bfa76e88e79937f321ab3a11ca726f9ff35c5889 100644 (file)
@@ -41,7 +41,7 @@ VkBool32 anv_GetPhysicalDeviceXcbPresentationSupportKHR(
    return wsi_get_physical_device_xcb_presentation_support(
       &device->wsi_device,
       queueFamilyIndex,
-      device->local_fd, false,
+      false,
       connection, visual_id);
 }
 
@@ -56,7 +56,7 @@ VkBool32 anv_GetPhysicalDeviceXlibPresentationSupportKHR(
    return wsi_get_physical_device_xcb_presentation_support(
       &device->wsi_device,
       queueFamilyIndex,
-      device->local_fd, false,
+      false,
       XGetXCBConnection(dpy), visualID);
 }
 
index ad4b8c9075eae1f5cc0f7ecbfbcb76897ae67631..51d8655a5a9e8df3acf64a52b8751b4e6085f1c8 100644 (file)
@@ -27,6 +27,7 @@
 #include "vk_util.h"
 
 #include <unistd.h>
+#include <xf86drm.h>
 
 VkResult
 wsi_device_init(struct wsi_device *wsi,
@@ -44,10 +45,19 @@ wsi_device_init(struct wsi_device *wsi,
 
 #define WSI_GET_CB(func) \
    PFN_vk##func func = (PFN_vk##func)proc_addr(pdevice, "vk" #func)
+   WSI_GET_CB(GetPhysicalDeviceProperties2);
    WSI_GET_CB(GetPhysicalDeviceMemoryProperties);
    WSI_GET_CB(GetPhysicalDeviceQueueFamilyProperties);
 #undef WSI_GET_CB
 
+   wsi->pci_bus_info.sType =
+      VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT;
+   VkPhysicalDeviceProperties2 pdp2 = {
+      .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
+      .pNext = &wsi->pci_bus_info,
+   };
+   GetPhysicalDeviceProperties2(pdevice, &pdp2);
+
    GetPhysicalDeviceMemoryProperties(pdevice, &wsi->memory_props);
    GetPhysicalDeviceQueueFamilyProperties(pdevice, &wsi->queue_family_count, NULL);
 
@@ -121,6 +131,32 @@ wsi_device_finish(struct wsi_device *wsi,
 #endif
 }
 
+bool
+wsi_device_matches_drm_fd(const struct wsi_device *wsi, int drm_fd)
+{
+   drmDevicePtr fd_device;
+   int ret = drmGetDevice(drm_fd, &fd_device);
+   if (ret)
+      return false;
+
+   bool match = false;
+   switch (fd_device->bustype) {
+   case DRM_BUS_PCI:
+      match = wsi->pci_bus_info.pciDomain == fd_device->businfo.pci->domain &&
+              wsi->pci_bus_info.pciBus == fd_device->businfo.pci->bus &&
+              wsi->pci_bus_info.pciDevice == fd_device->businfo.pci->dev &&
+              wsi->pci_bus_info.pciFunction == fd_device->businfo.pci->func;
+      break;
+
+   default:
+      break;
+   }
+
+   drmFreeDevice(&fd_device);
+
+   return match;
+}
+
 VkResult
 wsi_swapchain_init(const struct wsi_device *wsi,
                    struct wsi_swapchain *chain,
@@ -675,7 +711,6 @@ wsi_destroy_image(const struct wsi_swapchain *chain,
 
 VkResult
 wsi_common_get_surface_support(struct wsi_device *wsi_device,
-                               int local_fd,
                                uint32_t queueFamilyIndex,
                                VkSurfaceKHR _surface,
                                VkBool32* pSupported)
@@ -684,7 +719,7 @@ wsi_common_get_surface_support(struct wsi_device *wsi_device,
    struct wsi_interface *iface = wsi_device->wsi[surface->platform];
 
    return iface->get_support(surface, wsi_device,
-                             queueFamilyIndex, local_fd, pSupported);
+                             queueFamilyIndex, pSupported);
 }
 
 VkResult
@@ -805,7 +840,6 @@ wsi_common_get_surface_present_modes(struct wsi_device *wsi_device,
 
 VkResult
 wsi_common_get_present_rectangles(struct wsi_device *wsi_device,
-                                  int local_fd,
                                   VkSurfaceKHR _surface,
                                   uint32_t* pRectCount,
                                   VkRect2D* pRects)
@@ -813,14 +847,13 @@ wsi_common_get_present_rectangles(struct wsi_device *wsi_device,
    ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
    struct wsi_interface *iface = wsi_device->wsi[surface->platform];
 
-   return iface->get_present_rectangles(surface, wsi_device, local_fd,
+   return iface->get_present_rectangles(surface, wsi_device,
                                         pRectCount, pRects);
 }
 
 VkResult
 wsi_common_create_swapchain(struct wsi_device *wsi,
                             VkDevice device,
-                            int fd,
                             const VkSwapchainCreateInfoKHR *pCreateInfo,
                             const VkAllocationCallbacks *pAllocator,
                             VkSwapchainKHR *pSwapchain)
@@ -829,7 +862,7 @@ wsi_common_create_swapchain(struct wsi_device *wsi,
    struct wsi_interface *iface = wsi->wsi[surface->platform];
    struct wsi_swapchain *swapchain;
 
-   VkResult result = iface->create_swapchain(surface, device, wsi, fd,
+   VkResult result = iface->create_swapchain(surface, device, wsi,
                                              pCreateInfo, pAllocator,
                                              &swapchain);
    if (result != VK_SUCCESS)
index 5b69c573d9e5e2476db5e260df70cf953d0e5a5d..f6ca013c2a88246e89c159c0e04b6f8b4aea4e1f 100644 (file)
@@ -97,6 +97,8 @@ struct wsi_device {
    VkPhysicalDeviceMemoryProperties memory_props;
    uint32_t queue_family_count;
 
+   VkPhysicalDevicePCIBusInfoPropertiesEXT pci_bus_info;
+
    bool supports_modifiers;
    uint64_t (*image_get_modifier)(VkImage image);
 
@@ -166,7 +168,6 @@ ICD_DEFINE_NONDISP_HANDLE_CASTS(VkIcdSurfaceBase, VkSurfaceKHR)
 
 VkResult
 wsi_common_get_surface_support(struct wsi_device *wsi_device,
-                               int local_fd,
                                uint32_t queueFamilyIndex,
                                VkSurfaceKHR surface,
                                VkBool32* pSupported);
@@ -201,7 +202,6 @@ wsi_common_get_surface_present_modes(struct wsi_device *wsi_device,
 
 VkResult
 wsi_common_get_present_rectangles(struct wsi_device *wsi,
-                                  int local_fd,
                                   VkSurfaceKHR surface,
                                   uint32_t* pRectCount,
                                   VkRect2D* pRects);
@@ -226,7 +226,6 @@ wsi_common_acquire_next_image2(const struct wsi_device *wsi,
 VkResult
 wsi_common_create_swapchain(struct wsi_device *wsi,
                             VkDevice device,
-                            int fd,
                             const VkSwapchainCreateInfoKHR *pCreateInfo,
                             const VkAllocationCallbacks *pAllocator,
                             VkSwapchainKHR *pSwapchain);
index 2315717ef8e00155a3c8b6a2b597b8cdbec28246..fd0d30ad80ca3587fe2a2fa8c26c6e5d21475161 100644 (file)
@@ -803,7 +803,6 @@ static VkResult
 wsi_display_surface_get_support(VkIcdSurfaceBase *surface,
                                 struct wsi_device *wsi_device,
                                 uint32_t queueFamilyIndex,
-                                int local_fd,
                                 VkBool32* pSupported)
 {
    *pSupported = VK_TRUE;
@@ -934,27 +933,9 @@ wsi_display_surface_get_present_modes(VkIcdSurfaceBase *surface,
    return vk_outarray_status(&conn);
 }
 
-static bool
-fds_are_same_gpu(int fd1, int fd2)
-{
-   if (fd1 == -1 || fd2 == -1)
-      return false;
-
-   char *fd1_dev = drmGetRenderDeviceNameFromFd(fd1);
-   char *fd2_dev = drmGetRenderDeviceNameFromFd(fd2);
-
-   int ret = strcmp(fd1_dev, fd2_dev);
-
-   free(fd1_dev);
-   free(fd2_dev);
-
-   return ret == 0;
-}
-
 static VkResult
 wsi_display_surface_get_present_rectangles(VkIcdSurfaceBase *surface_base,
                                            struct wsi_device *wsi_device,
-                                           int local_fd,
                                            uint32_t* pRectCount,
                                            VkRect2D* pRects)
 {
@@ -962,7 +943,7 @@ wsi_display_surface_get_present_rectangles(VkIcdSurfaceBase *surface_base,
    wsi_display_mode *mode = wsi_display_mode_from_handle(surface->displayMode);
    VK_OUTARRAY_MAKE(out, pRects, pRectCount);
 
-   if (fds_are_same_gpu(local_fd, mode->connector->wsi->fd)) {
+   if (wsi_device_matches_drm_fd(wsi_device, mode->connector->wsi->fd)) {
       vk_outarray_append(&out, rect) {
          *rect = (VkRect2D) {
             .offset = { 0, 0 },
@@ -1730,7 +1711,6 @@ wsi_display_surface_create_swapchain(
    VkIcdSurfaceBase *icd_surface,
    VkDevice device,
    struct wsi_device *wsi_device,
-   int local_fd,
    const VkSwapchainCreateInfoKHR *create_info,
    const VkAllocationCallbacks *allocator,
    struct wsi_swapchain **swapchain_out)
index d8290b69331bf4d24731b56dddb56e89d290d7ac..50a78acacb0c26a03d9e1232c132317f9dc2190f 100644 (file)
@@ -69,6 +69,9 @@ struct wsi_swapchain {
                              const VkPresentRegionKHR *damage);
 };
 
+bool
+wsi_device_matches_drm_fd(const struct wsi_device *wsi, int drm_fd);
+
 VkResult
 wsi_swapchain_init(const struct wsi_device *wsi,
                    struct wsi_swapchain *chain,
@@ -101,7 +104,6 @@ struct wsi_interface {
    VkResult (*get_support)(VkIcdSurfaceBase *surface,
                            struct wsi_device *wsi_device,
                            uint32_t queueFamilyIndex,
-                           int local_fd,
                            VkBool32* pSupported);
    VkResult (*get_capabilities2)(VkIcdSurfaceBase *surface,
                                  const void *info_next,
@@ -120,13 +122,11 @@ struct wsi_interface {
                                  VkPresentModeKHR* pPresentModes);
    VkResult (*get_present_rectangles)(VkIcdSurfaceBase *surface,
                                       struct wsi_device *wsi_device,
-                                      int local_fd,
                                       uint32_t* pRectCount,
                                       VkRect2D* pRects);
    VkResult (*create_swapchain)(VkIcdSurfaceBase *surface,
                                 VkDevice device,
                                 struct wsi_device *wsi_device,
-                                int local_fd,
                                 const VkSwapchainCreateInfoKHR* pCreateInfo,
                                 const VkAllocationCallbacks* pAllocator,
                                 struct wsi_swapchain **swapchain);
index dccb530dae39dd9fca316cf200ce475032c47393..e9cc22ec60372e486e8eb6e9a2aedaf1b8f5ddcf 100644 (file)
@@ -465,7 +465,6 @@ static VkResult
 wsi_wl_surface_get_support(VkIcdSurfaceBase *surface,
                            struct wsi_device *wsi_device,
                            uint32_t queueFamilyIndex,
-                           int local_fd,
                            VkBool32* pSupported)
 {
    *pSupported = true;
@@ -604,7 +603,6 @@ wsi_wl_surface_get_present_modes(VkIcdSurfaceBase *surface,
 static VkResult
 wsi_wl_surface_get_present_rectangles(VkIcdSurfaceBase *surface,
                                       struct wsi_device *wsi_device,
-                                      int local_fd,
                                       uint32_t* pRectCount,
                                       VkRect2D* pRects)
 {
@@ -908,7 +906,6 @@ static VkResult
 wsi_wl_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
                                 VkDevice device,
                                 struct wsi_device *wsi_device,
-                                int local_fd,
                                 const VkSwapchainCreateInfoKHR* pCreateInfo,
                                 const VkAllocationCallbacks* pAllocator,
                                 struct wsi_swapchain **swapchain_out)
index 8c621ddf568ad7b22f19744973358ee48a8e6c74..16a544ec775cb612e4c11db53818159e1b822c84 100644 (file)
@@ -99,29 +99,22 @@ wsi_dri3_open(xcb_connection_t *conn,
 }
 
 static bool
-wsi_x11_check_dri3_compatible(xcb_connection_t *conn, int local_fd)
+wsi_x11_check_dri3_compatible(const struct wsi_device *wsi_dev,
+                              xcb_connection_t *conn)
 {
    xcb_screen_iterator_t screen_iter =
       xcb_setup_roots_iterator(xcb_get_setup(conn));
    xcb_screen_t *screen = screen_iter.data;
 
    int dri3_fd = wsi_dri3_open(conn, screen->root, None);
-   if (dri3_fd != -1) {
-      char *local_dev = drmGetRenderDeviceNameFromFd(local_fd);
-      char *dri3_dev = drmGetRenderDeviceNameFromFd(dri3_fd);
-      int ret;
-
-      close(dri3_fd);
+   if (dri3_fd == -1)
+      return true;
 
-      ret = strcmp(local_dev, dri3_dev);
+   bool match = wsi_device_matches_drm_fd(wsi_dev, dri3_fd);
 
-      free(local_dev);
-      free(dri3_dev);
+   close(dri3_fd);
 
-      if (ret != 0)
-         return false;
-   }
-   return true;
+   return match;
 }
 
 static struct wsi_x11_connection *
@@ -382,7 +375,6 @@ visual_has_alpha(xcb_visualtype_t *visual, unsigned depth)
 VkBool32 wsi_get_physical_device_xcb_presentation_support(
     struct wsi_device *wsi_device,
     uint32_t                                    queueFamilyIndex,
-    int fd,
     bool can_handle_different_gpu,
     xcb_connection_t*                           connection,
     xcb_visualid_t                              visual_id)
@@ -397,7 +389,7 @@ VkBool32 wsi_get_physical_device_xcb_presentation_support(
       return false;
 
    if (!can_handle_different_gpu)
-      if (!wsi_x11_check_dri3_compatible(connection, fd))
+      if (!wsi_x11_check_dri3_compatible(wsi_device, connection))
          return false;
 
    unsigned visual_depth;
@@ -432,7 +424,6 @@ static VkResult
 x11_surface_get_support(VkIcdSurfaceBase *icd_surface,
                         struct wsi_device *wsi_device,
                         uint32_t queueFamilyIndex,
-                        int local_fd,
                         VkBool32* pSupported)
 {
    xcb_connection_t *conn = x11_surface_get_connection(icd_surface);
@@ -601,7 +592,6 @@ x11_surface_get_present_modes(VkIcdSurfaceBase *surface,
 
 static bool
 x11_surface_is_local_to_gpu(struct wsi_device *wsi_dev,
-                            int local_fd,
                             xcb_connection_t *conn)
 {
    struct wsi_x11_connection *wsi_conn =
@@ -613,7 +603,7 @@ x11_surface_is_local_to_gpu(struct wsi_device *wsi_dev,
    if (!wsi_x11_check_for_dri3(wsi_conn))
       return false;
 
-   if (!wsi_x11_check_dri3_compatible(conn, local_fd))
+   if (!wsi_x11_check_dri3_compatible(wsi_dev, conn))
       return false;
 
    return true;
@@ -622,7 +612,6 @@ x11_surface_is_local_to_gpu(struct wsi_device *wsi_dev,
 static VkResult
 x11_surface_get_present_rectangles(VkIcdSurfaceBase *icd_surface,
                                    struct wsi_device *wsi_device,
-                                   int local_fd,
                                    uint32_t* pRectCount,
                                    VkRect2D* pRects)
 {
@@ -630,7 +619,7 @@ x11_surface_get_present_rectangles(VkIcdSurfaceBase *icd_surface,
    xcb_window_t window = x11_surface_get_window(icd_surface);
    VK_OUTARRAY_MAKE(out, pRects, pRectCount);
 
-   if (x11_surface_is_local_to_gpu(wsi_device, local_fd, conn)) {
+   if (x11_surface_is_local_to_gpu(wsi_device, conn)) {
       vk_outarray_append(&out, rect) {
          xcb_generic_error_t *err = NULL;
          xcb_get_geometry_cookie_t geom_cookie = xcb_get_geometry(conn, window);
@@ -1321,7 +1310,6 @@ static VkResult
 x11_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
                              VkDevice device,
                              struct wsi_device *wsi_device,
-                             int local_fd,
                              const VkSwapchainCreateInfoKHR *pCreateInfo,
                              const VkAllocationCallbacks* pAllocator,
                              struct wsi_swapchain **swapchain_out)
@@ -1388,7 +1376,7 @@ x11_surface_create_swapchain(VkIcdSurfaceBase *icd_surface,
    else
       chain->last_present_mode = XCB_PRESENT_COMPLETE_MODE_COPY;
 
-   if (!wsi_x11_check_dri3_compatible(conn, local_fd))
+   if (!wsi_x11_check_dri3_compatible(wsi_device, conn))
        chain->base.use_prime_blit = true;
 
    chain->event_id = xcb_generate_id(chain->conn);
index e9b3ecfb2e0834b56e06c20bce21a47085d2653b..99b1bcdc6fd73487b3df20a7674782b24ab4b78c 100644 (file)
@@ -28,7 +28,6 @@
 VkBool32 wsi_get_physical_device_xcb_presentation_support(
     struct wsi_device *wsi_device,
     uint32_t                                    queueFamilyIndex,
-    int local_fd,
     bool can_handle_different_gpu,
     xcb_connection_t*                           connection,
     xcb_visualid_t                              visual_id);