[PATCH xf86-video-amdgpu 3/3] Add RandR leases with modesetting driver support [v7]
Keith Packard
keithp at keithp.com
Sat Jul 7 00:36:47 UTC 2018
This adds support for RandR CRTC/Output leases through the modesetting
driver, creating a lease using new kernel infrastructure and returning
that to a client through an fd which will have access to only those
resources.
v2: Restore CRTC mode when leases terminate
When a lease terminates for a crtc we have saved data for, go
ahead and restore the saved mode.
v3: Report RR_Rotate_0 rotations for leased crtcs.
Ignore leased CRTCs when selecting screen size.
Stop leasing encoders, the kernel doesn't do that anymore.
Turn off crtc->enabled while leased so that modesetting
ignores them.
Check lease status before calling any driver mode functions
When starting a lease, mark leased CRTCs as disabled and hide
their cursors. Also, check to see if there are other
non-leased CRTCs which are driving leased Outputs and mark
them as disabled as well. Sometimes an application will lease
an idle crtc instead of the one already associated with the
leased output.
When terminating a lease, reset any CRTCs which are driving
outputs that are no longer leased so that they start working
again.
This required splitting the DIX level lease termination code
into two pieces, one to remove the lease from the system
(RRLeaseTerminated) and a new function that frees the lease
data structure (RRLeaseFree).
v4: Report RR_Rotate_0 rotation for leased crtcs.
v5: Terminate all leases on server reset.
Leases hang around after the associated client exits so that
the client doesn't need to occupy an X server client slot and
consume a file descriptor once it has gotten the output
resources necessary.
Any leases still hanging around when the X server resets or
shuts down need to be cleaned up by calling the kernel to
terminate the lease and freeing any DIX structures.
Note that we cannot simply use the existing
drmmode_terminate_lease function on each lease as that wants
to also reset the video mode, and during server shut down that
modesetting: Validate leases on VT enter
The kernel doesn't allow any master ioctls to run when another
VT is active, including simple things like listing the active
leases. To deal with that, we check the list of leases
whenever the X server VT is activated.
xfree86: hide disabled cursors when resetting after lease termination
The lessee may well have played with cursors and left one
active on our screen. Just tell the kernel to turn it off.
v6: Add meson build infrastructure
v7:
Make it build on X server 1.13
Remove drmmode_terminate_leases
Signed-off-by: Keith Packard <keithp at keithp.com>
Reviewed-by: Adam Jackson <ajax at redhat.com>
---
configure.ac | 18 ++++++
src/drmmode_display.c | 137 +++++++++++++++++++++++++++++++++++++++++-
src/drmmode_display.h | 5 ++
3 files changed, 159 insertions(+), 1 deletion(-)
diff --git a/configure.ac b/configure.ac
index 378b5aa..d56e519 100644
--- a/configure.ac
+++ b/configure.ac
@@ -54,6 +54,14 @@ if test "x$GCC" = "xyes"; then
CPPFLAGS="$CPPFLAGS -Wall"
fi
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
+#include <features.h>
+#ifndef __GLIBC__
+#error not glibc
+#endif
+]], [])], [AC_DEFINE(_GNU_SOURCE, 1,
+ [ Enable GNU and other extensions to the C environment for glibc])])
+
AH_TOP([#include "xorg-server.h"])
# Define a configure option for an alternate module directory
@@ -170,6 +178,12 @@ AC_CHECK_DECL(RROutputSetNonDesktop,
[#include <xorg-server.h>
#include <randrstr.h>])
+AC_CHECK_DECL(RRDeliverLeaseEvent,
+ [AC_DEFINE(HAVE_RRLEASE, 1,
+ [Have RandR lease support API])], [],
+ [#include <xorg-server.h>
+ #include <randrstr.h>])
+
AC_CHECK_DECL(fbGlyphs,
[AC_DEFINE(HAVE_FBGLYPHS, 1, [Have fbGlyphs API])], [],
[#include <X11/Xmd.h>
@@ -209,6 +223,8 @@ AC_CHECK_HEADERS([dri3.h], [], [],
CPPFLAGS="$SAVE_CPPFLAGS"
+AC_CHECK_LIB([bsd], [arc4random_buf])
+
# Checks for headers/macros for byte swapping
# Known variants:
# <byteswap.h> bswap_16, bswap_32, bswap_64 (glibc)
@@ -219,6 +235,8 @@ CPPFLAGS="$SAVE_CPPFLAGS"
# if <byteswap.h> is found, assume it's the correct version
AC_CHECK_HEADERS([byteswap.h])
+AC_REPLACE_FUNCS([reallocarray])
+
# if <sys/endian.h> is found, have to check which version
AC_CHECK_HEADER([sys/endian.h], [HAVE_SYS_ENDIAN_H="yes"], [HAVE_SYS_ENDIAN_H="no"])
diff --git a/src/drmmode_display.c b/src/drmmode_display.c
index 914086d..84a355c 100644
--- a/src/drmmode_display.c
+++ b/src/drmmode_display.c
@@ -2934,8 +2934,138 @@ fail:
return FALSE;
}
+#ifdef HAVE_RRLEASE
+
+static void
+drmmode_validate_leases(ScrnInfoPtr scrn)
+{
+ ScreenPtr screen = scrn->pScreen;
+ rrScrPrivPtr scr_priv = rrGetScrPriv(screen);
+ AMDGPUEntPtr pAMDGPUEnt = AMDGPUEntPriv(scrn);
+ drmModeLesseeListPtr lessees;
+ RRLeasePtr lease, next;
+ int l;
+
+ /* We can't talk to the kernel about leases when VT switched */
+ if (!scrn->vtSema)
+ return;
+
+ lessees = drmModeListLessees(pAMDGPUEnt->fd);
+ if (!lessees)
+ return;
+
+ xorg_list_for_each_entry_safe(lease, next, &scr_priv->leases, list) {
+ drmmode_lease_private_ptr lease_private = lease->devPrivate;
+
+ for (l = 0; l < lessees->count; l++) {
+ if (lessees->lessees[l] == lease_private->lessee_id)
+ break;
+ }
+
+ /* check to see if the lease has gone away */
+ if (l == lessees->count) {
+ free(lease_private);
+ lease->devPrivate = NULL;
+ xf86CrtcLeaseTerminated(lease);
+ }
+ }
+
+ free(lessees);
+}
+
+static int
+drmmode_create_lease(RRLeasePtr lease, int *fd)
+{
+ ScreenPtr screen = lease->screen;
+ ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
+ AMDGPUEntPtr pAMDGPUEnt = AMDGPUEntPriv(scrn);
+ int ncrtc = lease->numCrtcs;
+ int noutput = lease->numOutputs;
+ int nobjects;
+ int c, o;
+ int i;
+ int lease_fd;
+ uint32_t *objects;
+ drmmode_lease_private_ptr lease_private;
+
+ nobjects = ncrtc + noutput;
+
+ if (nobjects == 0)
+ return BadValue;
+
+ lease_private = calloc(1, sizeof (drmmode_lease_private_rec));
+ if (!lease_private)
+ return BadAlloc;
+
+ objects = xallocarray(nobjects, sizeof (uint32_t));
+
+ if (!objects) {
+ free(lease_private);
+ return BadAlloc;
+ }
+
+ i = 0;
+
+ /* Add CRTC ids */
+ for (c = 0; c < ncrtc; c++) {
+ xf86CrtcPtr crtc = lease->crtcs[c]->devPrivate;
+ drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
+
+ objects[i++] = drmmode_crtc->mode_crtc->crtc_id;
+ }
+
+ /* Add connector ids */
+
+ for (o = 0; o < noutput; o++) {
+ xf86OutputPtr output = lease->outputs[o]->devPrivate;
+ drmmode_output_private_ptr drmmode_output = output->driver_private;
+
+ objects[i++] = drmmode_output->mode_output->connector_id;
+ }
+
+ /* call kernel to create lease */
+ assert (i == nobjects);
+
+ lease_fd = drmModeCreateLease(pAMDGPUEnt->fd, objects, nobjects, 0, &lease_private->lessee_id);
+
+ free(objects);
+
+ if (lease_fd < 0) {
+ free(lease_private);
+ return BadMatch;
+ }
+
+ lease->devPrivate = lease_private;
+
+ xf86CrtcLeaseStarted(lease);
+
+ *fd = lease_fd;
+ return Success;
+}
+
+static void
+drmmode_terminate_lease(RRLeasePtr lease)
+{
+ ScreenPtr screen = lease->screen;
+ ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
+ AMDGPUEntPtr pAMDGPUEnt = AMDGPUEntPriv(scrn);
+ drmmode_lease_private_ptr lease_private = lease->devPrivate;
+
+ if (drmModeRevokeLease(pAMDGPUEnt->fd, lease_private->lessee_id) == 0) {
+ free(lease_private);
+ lease->devPrivate = NULL;
+ xf86CrtcLeaseTerminated(lease);
+ }
+}
+
+#endif
+
static const xf86CrtcConfigFuncsRec drmmode_xf86crtc_config_funcs = {
- drmmode_xf86crtc_resize
+ .resize = drmmode_xf86crtc_resize,
+#ifdef HAVE_RRLEASE
+ .create_lease = drmmode_create_lease,
+ .terminate_lease = drmmode_terminate_lease
+#endif
};
static void
@@ -3640,6 +3770,11 @@ restart_destroy:
changed = TRUE;
}
+#ifdef HAVE_RRLEASE
+ /* Check to see if a lessee has disappeared */
+ drmmode_validate_leases(scrn);
+#endif
+
if (changed && dixPrivateKeyRegistered(rrPrivKey)) {
#if XORG_VERSION_CURRENT >= XORG_VERSION_NUMERIC(1,14,99,2,0)
RRSetChanged(xf86ScrnToScreen(scrn));
diff --git a/src/drmmode_display.h b/src/drmmode_display.h
index 0f0227c..8b3482f 100644
--- a/src/drmmode_display.h
+++ b/src/drmmode_display.h
@@ -157,6 +157,11 @@ typedef struct {
int tear_free;
} drmmode_output_private_rec, *drmmode_output_private_ptr;
+#ifdef HAVE_RRLEASE
+typedef struct {
+ uint32_t lessee_id;
+} drmmode_lease_private_rec, *drmmode_lease_private_ptr;
+#endif
enum drmmode_flip_sync {
FLIP_VSYNC,
--
2.17.1
More information about the xorg-devel
mailing list