From 04f104e8f3efb422223a6a13782e9a49d920159c Mon Sep 17 00:00:00 2001 From: Jian-Hong Pan Date: Tue, 28 Jul 2020 17:37:18 +0800 Subject: [PATCH] xfree86: Detect the primary device by checking outputs Before this patch, X server detects/sets the primary device by: 1. The "PrimaryGPU" option in extra X configuration 2. pci_device_is_boot_vga() for PCI devices 3. Set the first (0 index) device as the primary device, if it is not found yet. However, the other display controllers like Amlogic's meson cannot be detected as the primary device by pci_device_is_boot_vga(). Thus, it has to set the extra X configuration for the "PrimaryGPU" option. Otherwise, X server will set the first (0 index) device as the primary device. But it may not be the correct one, because it has no output. For example, Amlogic puts the GPU and display controller as different devices: (II) xfree86: Adding drm device (/dev/dri/card0) (II) Platform probe for /sys/devices/platform/soc/d0000000.apb/d00c0000.gpu/drm/card0 (II) xfree86: Adding drm device (/dev/dri/card1) (II) Platform probe for /sys/devices/platform/soc/d0100000.vpu/drm/card1 This patch introduces a new member num_connectors into OdevAttributes to hold the number of display connectors for each DRM device. It gets the number of the device's connectors by detecting the output connecters of devices in platform dev driver, that refers to the check_outputs() function in modesetting driver. Then, adds a new way to set the primary device by checking the number of display connectors for drm devices, before use the first platform device as a fallback. Buglink: https://gitlab.freedesktop.org/xorg/xserver/-/issues/1023 Signed-off-by: Jian-Hong Pan --- hw/xfree86/common/xf86platformBus.c | 35 ++++++++++++++++++---- hw/xfree86/common/xf86platformBus.h | 20 ++++++++----- hw/xfree86/os-support/linux/lnx_platform.c | 21 +++++++++++++ include/hotplug.h | 5 +++- 4 files changed, 66 insertions(+), 15 deletions(-) diff --git a/hw/xfree86/common/xf86platformBus.c b/hw/xfree86/common/xf86platformBus.c index ee2f3f86a..b92db74a8 100644 --- a/hw/xfree86/common/xf86platformBus.c +++ b/hw/xfree86/common/xf86platformBus.c @@ -746,16 +746,39 @@ void xf86platformVTProbe(void) void xf86platformPrimary(void) { - /* use the first platform device as a fallback */ - if (primaryBus.type == BUS_NONE) { - xf86Msg(X_INFO, "no primary bus or device found\n"); + struct xf86_platform_device *dev; + struct OdevAttributes *attribs; + int i; - if (xf86_num_platform_devices > 0) { - primaryBus.id.plat = &xf86_platform_devices[0]; + /* only deal when no primary device has been detected */ + if (primaryBus.type != BUS_NONE) + return; + + /* set it to the first platform device detected with output connector capabilities */ + for (i = 0; i < xf86_num_platform_devices; i++) { + dev = &xf86_platform_devices[i]; + attribs = xf86_platform_device_odev_attributes(dev); + + if (attribs->num_connectors > 0) { + primaryBus.id.plat = dev; primaryBus.type = BUS_PLATFORM; - xf86Msg(X_NONE, "\tfalling back to %s\n", primaryBus.id.plat->attribs->syspath); + xf86Msg(X_INFO, "Choose %s as primary device\n", attribs->syspath); + return; } } + + /* finally, use the first platform device as a fallback */ + xf86Msg(X_INFO, "no primary bus or device found\n"); + + if (xf86_num_platform_devices > 0) { + dev = &xf86_platform_devices[0]; + attribs = xf86_platform_device_odev_attributes(dev); + + primaryBus.id.plat = dev; + primaryBus.type = BUS_PLATFORM; + + xf86Msg(X_NONE, "\tfalling back to %s\n", attribs->syspath); + } } #endif diff --git a/hw/xfree86/common/xf86platformBus.h b/hw/xfree86/common/xf86platformBus.h index 1e75e6352..65b0b96bd 100644 --- a/hw/xfree86/common/xf86platformBus.h +++ b/hw/xfree86/common/xf86platformBus.h @@ -80,19 +80,21 @@ xf86_platform_odev_attributes(int index) */ /* path to kernel device node - Linux e.g. /dev/dri/card0 */ -#define ODEV_ATTRIB_PATH 1 +#define ODEV_ATTRIB_PATH 1 /* system device path - Linux e.g. /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0/drm/card1 */ -#define ODEV_ATTRIB_SYSPATH 2 +#define ODEV_ATTRIB_SYSPATH 2 /* DRI-style bus id */ -#define ODEV_ATTRIB_BUSID 3 +#define ODEV_ATTRIB_BUSID 3 /* Server managed FD */ -#define ODEV_ATTRIB_FD 4 +#define ODEV_ATTRIB_FD 4 /* Major number of the device node pointed to by ODEV_ATTRIB_PATH */ -#define ODEV_ATTRIB_MAJOR 5 +#define ODEV_ATTRIB_MAJOR 5 /* Minor number of the device node pointed to by ODEV_ATTRIB_PATH */ -#define ODEV_ATTRIB_MINOR 6 +#define ODEV_ATTRIB_MINOR 6 /* kernel driver name */ -#define ODEV_ATTRIB_DRIVER 7 +#define ODEV_ATTRIB_DRIVER 7 +/* Amount of display connectors */ +#define ODEV_ATTRIB_NUM_CONNECTORS 8 /* Protect against a mismatch attribute type by generating a compiler * error using a negative array size when an incorrect attribute is @@ -126,7 +128,7 @@ _xf86_get_platform_device_attrib(struct xf86_platform_device *device, int attrib #define xf86_get_platform_device_attrib(device, attrib) _xf86_get_platform_device_attrib(device,attrib,_ODEV_ATTRIB_STRING_CHECK(attrib)) -#define _ODEV_ATTRIB_IS_INT(x) ((x) == ODEV_ATTRIB_FD || (x) == ODEV_ATTRIB_MAJOR || (x) == ODEV_ATTRIB_MINOR) +#define _ODEV_ATTRIB_IS_INT(x) ((x) == ODEV_ATTRIB_FD || (x) == ODEV_ATTRIB_MAJOR || (x) == ODEV_ATTRIB_MINOR || (x) == ODEV_ATTRIB_NUM_CONNECTORS) #define _ODEV_ATTRIB_INT_DEFAULT(x) ((x) == ODEV_ATTRIB_FD ? -1 : 0) #define _ODEV_ATTRIB_DEFAULT_CHECK(x,def) (_ODEV_ATTRIB_INT_DEFAULT(x) == (def)) #define _ODEV_ATTRIB_INT_CHECK(x,def) ((int (*)[_ODEV_ATTRIB_IS_INT(x)*_ODEV_ATTRIB_DEFAULT_CHECK(x,def)-1]) 0) @@ -141,6 +143,8 @@ _xf86_get_platform_device_int_attrib(struct xf86_platform_device *device, int at return xf86_platform_device_odev_attributes(device)->major; case ODEV_ATTRIB_MINOR: return xf86_platform_device_odev_attributes(device)->minor; + case ODEV_ATTRIB_NUM_CONNECTORS: + return xf86_platform_device_odev_attributes(device)->num_connectors; default: assert(FALSE); return 0; diff --git a/hw/xfree86/os-support/linux/lnx_platform.c b/hw/xfree86/os-support/linux/lnx_platform.c index e62306219..132b59f0b 100644 --- a/hw/xfree86/os-support/linux/lnx_platform.c +++ b/hw/xfree86/os-support/linux/lnx_platform.c @@ -5,6 +5,7 @@ #ifdef XSERVER_PLATFORM_BUS #include +#include #include #include #include @@ -20,6 +21,22 @@ #include "hotplug.h" #include "systemd-logind.h" +static int +CountConnectors(int fd) +{ + int connectors = 0; + drmModeResPtr res; + + res = drmModeGetResources(fd); + if (!res) + return 0; + + connectors = res->count_connectors; + drmModeFreeResources(res); + + return connectors; +} + static Bool get_drm_info(struct OdevAttributes *attribs, char *path, int delayed_index) { @@ -66,6 +83,10 @@ get_drm_info(struct OdevAttributes *attribs, char *path, int delayed_index) xf86_platform_odev_attributes(delayed_index)->driver = XNFstrdup(v->name); drmFreeVersion(v); + xf86_platform_odev_attributes(delayed_index)->num_connectors = CountConnectors(fd); + xf86DrvMsg(-1, X_DEBUG, "%s has %d connectors\n", + path, xf86_platform_odev_attributes(delayed_index)->num_connectors); + out: if (!server_fd) close(fd); diff --git a/include/hotplug.h b/include/hotplug.h index 6fe76c806..f6b07c4b8 100644 --- a/include/hotplug.h +++ b/include/hotplug.h @@ -35,7 +35,7 @@ extern _X_EXPORT void config_fini(void); /* Bump this each time you add something to the struct * so that drivers can easily tell what is available */ -#define ODEV_ATTRIBUTES_VERSION 1 +#define ODEV_ATTRIBUTES_VERSION 2 struct OdevAttributes { /* path to kernel device node - Linux e.g. /dev/dri/card0 */ @@ -58,6 +58,9 @@ struct OdevAttributes { /* kernel driver name */ char *driver; + + /* Amount of display connectors */ + int num_connectors; }; /* Note starting with xserver 1.16 this function never fails */