[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

xserver-xorg-video-intel: Changes to 'debian-experimental'



 configure.ac                       |   87 ++
 debian/changelog                   |    6 
 man/intel.man                      |   13 
 src/Makefile.am                    |    4 
 src/intel_device.c                 |  111 +++
 src/intel_driver.h                 |    2 
 src/intel_options.c                |    1 
 src/intel_options.h                |    1 
 src/legacy/i810/Makefile.am        |    3 
 src/sna/Makefile.am                |   14 
 src/sna/gen8_render.c              |   20 
 src/sna/kgem.c                     |  273 ++++++---
 src/sna/kgem.h                     |   23 
 src/sna/sna.h                      |   91 ++-
 src/sna/sna_accel.c                |  654 +++++++++++----------
 src/sna/sna_composite.c            |   17 
 src/sna/sna_damage.h               |   20 
 src/sna/sna_display.c              |  841 +++++++++++++++++++---------
 src/sna/sna_dri2.c                 |  744 +++++++++++++------------
 src/sna/sna_dri3.c                 |  379 ++++++++++++
 src/sna/sna_driver.c               |  178 ++++-
 src/sna/sna_io.c                   |   39 -
 src/sna/sna_present.c              |  465 +++++++++++++++
 src/sna/sna_render.c               |    3 
 src/sna/sna_trapezoids.c           |    4 
 src/sna/sna_trapezoids_imprecise.c |    4 
 src/uxa/Makefile.am                |   13 
 src/uxa/intel.h                    |   78 ++
 src/uxa/intel_display.c            |  419 ++++++++++++--
 src/uxa/intel_dri.c                |  212 ++++---
 src/uxa/intel_dri3.c               |  140 ++++
 src/uxa/intel_driver.c             |   89 ++
 src/uxa/intel_present.c            |  401 +++++++++++++
 src/uxa/intel_sync.c               |  111 +++
 src/uxa/intel_uxa.c                |   67 +-
 test/.gitignore                    |    2 
 test/Makefile.am                   |   15 
 test/dri3-test.c                   | 1100 +++++++++++++++++++++++++++++++++++++
 test/dri3.c                        |  133 ++++
 test/dri3.h                        |   50 +
 test/present-test.c                |  726 ++++++++++++++++++++++++
 test/test.h                        |   14 
 tools/virtual.c                    |  343 +++++++++--
 43 files changed, 6536 insertions(+), 1374 deletions(-)

New commits:
commit 904e8540fa21e9aa0f0870d08136557e7f3410bb
Author: maximilian attems <maks@debian.org>
Date:   Sat Jun 7 00:55:06 2014 +0200

    update to latest git
    
    Signed-off-by: maximilian attems <maks@debian.org>

diff --git a/debian/changelog b/debian/changelog
index 25c5f42..9f41a4f 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,9 @@
+xserver-xorg-video-intel (2:2.99.911+git20140607-1~exp1) UNRELEASED; urgency=medium
+
+  * New upstream prerelease.
+
+ -- maximilian attems <maks@debian.org>  Sat, 07 Jun 2014 00:54:37 +0200
+
 xserver-xorg-video-intel (2:2.99.911+git20140529-1~exp2) experimental; urgency=medium
 
   * Add myself to Uploaders.

commit 23840bd329cf4da3a4bdd5a1a466125a95473534
Author: Chris Wilson <chris@chris-wilson.co.uk>
Date:   Fri Jun 6 16:10:29 2014 +0100

    sna: Only allow the inplace copy to replace a complete overwritten GPU bo
    
    And double check after recreating the new GPU bo that is still mappable.
    
    Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>

diff --git a/src/sna/sna_accel.c b/src/sna/sna_accel.c
index 50e0b2c..47a346c 100644
--- a/src/sna/sna_accel.c
+++ b/src/sna/sna_accel.c
@@ -5624,7 +5624,8 @@ static bool
 sna_copy_boxes__inplace(struct sna *sna, RegionPtr region, int alu,
 			PixmapPtr src_pixmap, struct sna_pixmap *src_priv,
 			int dx, int dy,
-			PixmapPtr dst_pixmap, struct sna_pixmap *dst_priv)
+			PixmapPtr dst_pixmap, struct sna_pixmap *dst_priv,
+			bool replaces)
 {
 	const BoxRec *box;
 	char *ptr;
@@ -5765,18 +5766,14 @@ upload_inplace:
 		return false;
 	}
 
-	if (!kgem_bo_can_map__cpu(&sna->kgem, dst_priv->gpu_bo, true)) {
-		DBG(("%s - no, cannot map dst for reads into the CPU\n", __FUNCTION__));
-		return false;
-	}
-
-	if (__kgem_bo_is_busy(&sna->kgem, dst_priv->gpu_bo)) {
-		if (!dst_priv->pinned) {
+	if (!kgem_bo_can_map__cpu(&sna->kgem, dst_priv->gpu_bo, true) ||
+	    __kgem_bo_is_busy(&sna->kgem, dst_priv->gpu_bo)) {
+		if (replaces && !dst_priv->pinned) {
 			unsigned create;
 			struct kgem_bo *bo;
 
 			create = CREATE_CPU_MAP | CREATE_INACTIVE;
-			if (dst_pixmap->usage_hint == SNA_CREATE_FB)
+			if (dst_priv->gpu_bo->scanout)
 				create |= CREATE_SCANOUT;
 
 			bo = kgem_create_2d(&sna->kgem,
@@ -5795,6 +5792,11 @@ upload_inplace:
 			DBG(("%s - no, dst is busy\n", __FUNCTION__));
 			return false;
 		}
+
+		if (!kgem_bo_can_map__cpu(&sna->kgem, dst_priv->gpu_bo, true)) {
+			DBG(("%s - no, cannot map dst for reads into the CPU\n", __FUNCTION__));
+			return false;
+		}
 	}
 
 	if (src_priv &&
@@ -6373,7 +6375,8 @@ fallback:
 	} else if (!sna_copy_boxes__inplace(sna, region, alu,
 					    src_pixmap, src_priv,
 					    src_dx, src_dy,
-					    dst_pixmap, dst_priv)) {
+					    dst_pixmap, dst_priv,
+					    replaces)) {
 		FbBits *dst_bits, *src_bits;
 		int dst_stride, src_stride;
 

commit 283ff8cbe4137f46cc027dd6aadc69d0270cb33d
Author: Chris Wilson <chris@chris-wilson.co.uk>
Date:   Fri Jun 6 15:04:25 2014 +0100

    sna: Rearrange scanout_flush checking to avoid potential crash
    
    With a shadow, we may never attach a GPU bo to the frontbuffer, so be
    careful when checking the scanout not to dereference that NULL pointer.
    
    Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>

diff --git a/src/sna/sna_accel.c b/src/sna/sna_accel.c
index a307d9e..50e0b2c 100644
--- a/src/sna/sna_accel.c
+++ b/src/sna/sna_accel.c
@@ -16565,7 +16565,8 @@ static struct sna_pixmap *sna_accel_scanout(struct sna *sna)
 	assert(sna->front);
 
 	priv = sna_pixmap(sna->front);
-	assert(priv->gpu_bo);
+	if (priv->gpu_bo == NULL)
+		return NULL;
 
 	return priv;
 }
@@ -16607,21 +16608,21 @@ static bool has_shadow(struct sna *sna)
 	return RegionNotEmpty(DamageRegion(damage));
 }
 
-static bool start_flush(struct sna *sna, struct sna_pixmap *scanout)
+static bool start_flush(struct sna *sna)
 {
-	DBG(("%s: scanout=%d shadow?=%d, slaves?=%d, (cpu?=%d || gpu?=%d))\n",
-	     __FUNCTION__,
-	     scanout && scanout->gpu_bo ? scanout->gpu_bo->handle : 0,
-	     has_shadow(sna), has_offload_slaves(sna),
-	     scanout && scanout->cpu_damage != NULL,
-	     scanout && scanout->gpu_bo && scanout->gpu_bo->exec != NULL));
+	struct sna_pixmap *scanout;
 
-	if (has_offload_slaves(sna))
+	if (has_offload_slaves(sna)) {
+		DBG(("%s: has offload slaves\n", __FUNCTION__));
 		return true;
+	}
 
-	if (has_shadow(sna))
+	if (has_shadow(sna)) {
+		DBG(("%s: has dirty shadow\n", __FUNCTION__));
 		return true;
+	}
 
+	scanout = sna_accel_scanout(sna);
 	if (!scanout)
 		return false;
 
@@ -16630,7 +16631,11 @@ static bool start_flush(struct sna *sna, struct sna_pixmap *scanout)
 		return true;
 	}
 
-	return scanout->cpu_damage || scanout->gpu_bo->exec;
+	if (scanout->cpu_damage || scanout->gpu_bo->exec)
+		return true;
+
+	kgem_scanout_flush(&sna->kgem, scanout->gpu_bo);
+	return false;
 }
 
 static bool stop_flush(struct sna *sna, struct sna_pixmap *scanout)
@@ -16670,17 +16675,7 @@ static void timer_enable(struct sna *sna, int whom, int interval)
 
 static bool sna_scanout_do_flush(struct sna *sna)
 {
-	struct sna_pixmap *priv;
-	int interval;
-
-	priv = sna_accel_scanout(sna);
-	if (priv == NULL && !sna->mode.shadow_damage && !has_offload_slaves(sna)) {
-		DBG(("%s -- no scanout attached\n", __FUNCTION__));
-		sna_accel_disarm_timer(sna, FLUSH_TIMER);
-		return false;
-	}
-
-	interval = sna->vblank_interval ?: 20;
+	int interval = sna->vblank_interval ?: 20;
 	if (sna->timer_active & (1<<(FLUSH_TIMER))) {
 		int32_t delta = sna->timer_expire[FLUSH_TIMER] - TIME;
 		DBG(("%s: flush timer active: delta=%d\n",
@@ -16690,12 +16685,10 @@ static bool sna_scanout_do_flush(struct sna *sna)
 			sna->timer_expire[FLUSH_TIMER] = TIME + interval;
 			return true;
 		}
-	} else if (!start_flush(sna, priv)) {
-		DBG(("%s -- no pending write to scanout\n", __FUNCTION__));
-		if (priv)
-			kgem_scanout_flush(&sna->kgem, priv->gpu_bo);
-	} else
-		timer_enable(sna, FLUSH_TIMER, interval/2);
+	} else {
+		if (start_flush(sna))
+			timer_enable(sna, FLUSH_TIMER, interval/2);
+	}
 
 	return false;
 }

commit 694ff54aca16ccf39e94d505942eb857b3101d7d
Author: Chris Wilson <chris@chris-wilson.co.uk>
Date:   Fri Jun 6 13:34:18 2014 +0100

    sna: Report KMS driver version
    
    Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>

diff --git a/src/intel_device.c b/src/intel_device.c
index c1c6d84..610b223 100644
--- a/src/intel_device.c
+++ b/src/intel_device.c
@@ -376,6 +376,16 @@ err_path:
 	return -1;
 }
 
+int __intel_peek_fd(ScrnInfoPtr scrn)
+{
+	struct intel_device *dev;
+
+	dev = intel_device(scrn);
+	assert(dev && dev->fd != -1);
+
+	return dev->fd;
+}
+
 int intel_get_device(ScrnInfoPtr scrn)
 {
 	struct intel_device *dev;
diff --git a/src/intel_driver.h b/src/intel_driver.h
index 3dc640c..d295250 100644
--- a/src/intel_driver.h
+++ b/src/intel_driver.h
@@ -125,6 +125,7 @@ void intel_detect_chipset(ScrnInfoPtr scrn, EntityInfoPtr ent);
 int intel_open_device(int entity_num,
 		      const struct pci_device *pci,
 		      struct xf86_platform_device *dev);
+int __intel_peek_fd(ScrnInfoPtr scrn);
 int intel_get_device(ScrnInfoPtr scrn);
 const char *intel_get_client_name(ScrnInfoPtr scrn);
 int intel_get_client_fd(ScrnInfoPtr scrn);
diff --git a/src/sna/sna_driver.c b/src/sna/sna_driver.c
index b923236..3810090 100644
--- a/src/sna/sna_driver.c
+++ b/src/sna/sna_driver.c
@@ -1276,9 +1276,31 @@ static void sna_leave_vt__hosted(VT_FUNC_ARGS_DECL)
 {
 }
 
-Bool sna_init_scrn(ScrnInfoPtr scrn, int entity_num)
+static void describe_kms(ScrnInfoPtr scrn)
+{
+	int fd = __intel_peek_fd(scrn);
+	drm_version_t version;
+	char name[128] = "";
+	char date[128] = "";
+
+	memset(&version, 0, sizeof(version));
+	version.name_len = sizeof(name) - 1;
+	version.name = name;
+	version.date_len = sizeof(date) - 1;
+	version.date = date;
+
+	if (drmIoctl(fd, DRM_IOCTL_VERSION, &version))
+		return;
+
+	xf86DrvMsg(scrn->scrnIndex, X_INFO,
+		   "Using Kernel Mode Setting driver: %s, version %d.%d.%d %s\n",
+		   version.name,
+		   version.version_major, version.version_minor, version.version_patchlevel,
+		   version.date);
+}
+
+static void describe_sna(ScrnInfoPtr scrn)
 {
-	DBG(("%s: entity_num=%d\n", __FUNCTION__, entity_num));
 #if defined(USE_GIT_DESCRIBE)
 	xf86DrvMsg(scrn->scrnIndex, X_INFO,
 		   "SNA compiled from %s\n", git_version);
@@ -1303,6 +1325,13 @@ Bool sna_init_scrn(ScrnInfoPtr scrn, int entity_num)
 		   "SNA compiled with extra pixmap/damage validation\n");
 #endif
 	DBG(("pixman version: %s\n", pixman_version_string()));
+}
+
+Bool sna_init_scrn(ScrnInfoPtr scrn, int entity_num)
+{
+	DBG(("%s: entity_num=%d\n", __FUNCTION__, entity_num));
+	describe_kms(scrn);
+	describe_sna(scrn);
 
 	scrn->PreInit = sna_pre_init;
 	scrn->ScreenInit = sna_screen_init;

commit 0ebcef4f2ebe82e9b90589aa4266a9228e2b5fdb
Author: Chris Wilson <chris@chris-wilson.co.uk>
Date:   Fri Jun 6 11:39:57 2014 +0100

    man: Update notes about TearFree
    
    Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>

diff --git a/man/intel.man b/man/intel.man
index 27206e3..9deac41 100644
--- a/man/intel.man
+++ b/man/intel.man
@@ -150,9 +150,11 @@ the screen is then performed synchronously with the vertical refresh of the
 display so that the entire update is completed before the display starts its
 refresh. That is only one frame is ever visible, preventing an unsightly tear
 between two visible and differing frames. Note that this replicates what the
-compositing manager should be doing, so it is not advisable to enable both.
-However, some compositing managers do cause tearing, and if the outputs are
-rotated, there may will still be tearing without TearFree enabled.
+compositing manager should be doing, however TearFree will redirect the
+compositor updates (and those of fullscreen games) directly on to the scanout
+thus incurring no additional overhead in the composited case. Also note that
+not all compositing managers prevent tearing, and if the outputs are
+rotated, there will still be tearing without TearFree enabled.
 .IP
 Default: TearFree is disabled.
 .TP

commit 1f943a7d92d0e3a8895ba7d539cfc5e726bcc8b7
Author: Chris Wilson <chris@chris-wilson.co.uk>
Date:   Fri Jun 6 11:25:47 2014 +0100

    sna/dri2: Restore flush on front buffers after swapping bo
    
    Fallout from commit 8369166349c92a20d9a2e7d0256e63f66fe2682b
    Author: Chris Wilson <chris@chris-wilson.co.uk>
    Date:   Wed Jun 4 08:29:51 2014 +0100
    
        sna/dri2: Enable immediate buffer exchanges
    
    We set the flush hint too early, as we may need to swap the GPU bo for a
    more appropriate buffer for DRI.
    
    Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>

diff --git a/src/sna/sna_dri2.c b/src/sna/sna_dri2.c
index 9e591be..e13e626 100644
--- a/src/sna/sna_dri2.c
+++ b/src/sna/sna_dri2.c
@@ -327,13 +327,6 @@ static struct kgem_bo *sna_pixmap_set_dri(struct sna *sna,
 	assert(priv->gpu_bo->proxy == NULL);
 	assert(priv->gpu_bo->flush == false);
 
-	/* Henceforth, we need to broadcast all updates to clients and
-	 * flush our rendering before doing so.
-	 */
-	priv->gpu_bo->flush = true;
-	if (priv->gpu_bo->exec)
-		sna->kgem.flush = 1;
-
 	tiling = color_tiling(sna, &pixmap->drawable);
 	if (tiling < 0)
 		tiling = -tiling;
@@ -377,12 +370,14 @@ sna_dri2_pixmap_update_bo(struct sna *sna, PixmapPtr pixmap, struct kgem_bo *bo)
 	if (private->bo == bo)
 		return;
 
+	DBG(("%s: dropping flush hint from handle=%d\n", __FUNCTION__, private->bo->handle));
 	private->bo->flush = false;
 	kgem_bo_destroy(&sna->kgem, private->bo);
 
 	buffer->name = kgem_bo_flink(&sna->kgem, bo);
 	private->bo = ref(bo);
 
+	DBG(("%s: adding flush hint to handle=%d\n", __FUNCTION__, bo->handle));
 	bo->flush = true;
 	assert(sna_pixmap(pixmap)->flush);
 
@@ -422,6 +417,7 @@ sna_dri2_create_buffer(DrawablePtr draw,
 			     private->bo->handle, buffer->name));
 
 			assert(private->pixmap == pixmap);
+			assert(private->bo->flush);
 			assert(sna_pixmap(pixmap)->flush);
 			assert(sna_pixmap(pixmap)->gpu_bo == private->bo);
 			assert(sna_pixmap(pixmap)->pinned & PIN_DRI2);
@@ -564,6 +560,11 @@ sna_dri2_create_buffer(DrawablePtr draw,
 		 *
 		 * As we don't track which Client, we flush for all.
 		 */
+		DBG(("%s: adding flush hint to handle=%d\n", __FUNCTION__, priv->gpu_bo->handle));
+		priv->gpu_bo->flush = true;
+		if (priv->gpu_bo->exec)
+			sna->kgem.flush = 1;
+
 		priv->flush = true;
 		sna_accel_watch_flush(sna, 1);
 	}
@@ -609,6 +610,7 @@ static void _sna_dri2_destroy_buffer(struct sna *sna, DRI2Buffer2Ptr buffer)
 
 		list_del(&priv->flush_list);
 
+		DBG(("%s: dropping flush hint from handle=%d\n", __FUNCTION__, private->bo->handle));
 		priv->gpu_bo->flush = false;
 		priv->pinned &= ~PIN_DRI2;
 
@@ -617,8 +619,8 @@ static void _sna_dri2_destroy_buffer(struct sna *sna, DRI2Buffer2Ptr buffer)
 
 		sna_pixmap_set_buffer(pixmap, NULL);
 		pixmap->drawable.pScreen->DestroyPixmap(pixmap);
-	} else
-		private->bo->flush = false;
+	}
+	assert(private->bo->flush == false);
 
 	kgem_bo_destroy(&sna->kgem, private->bo);
 	free(buffer);
@@ -691,6 +693,7 @@ static void set_bo(PixmapPtr pixmap, struct kgem_bo *bo)
 	if (priv->move_to_gpu)
 		priv->move_to_gpu(sna, priv, 0);
 	if (priv->gpu_bo != bo) {
+		DBG(("%s: dropping flush hint from handle=%d\n", __FUNCTION__, priv->gpu_bo->handle));
 		priv->gpu_bo->flush = false;
 		if (priv->cow)
 			sna_pixmap_undo_cow(sna, priv, 0);
@@ -698,6 +701,7 @@ static void set_bo(PixmapPtr pixmap, struct kgem_bo *bo)
 			sna_pixmap_unmap(pixmap, priv);
 			kgem_bo_destroy(&sna->kgem, priv->gpu_bo);
 		}
+		DBG(("%s: adding flush hint to handle=%d\n", __FUNCTION__, bo->handle));
 		bo->flush = true;
 		if (bo->exec)
 			sna->kgem.flush = 1;
@@ -705,6 +709,7 @@ static void set_bo(PixmapPtr pixmap, struct kgem_bo *bo)
 	}
 	if (bo->domain != DOMAIN_GPU)
 		bo->domain = DOMAIN_NONE;
+	assert(bo->flush);
 
 	DamageRegionProcessPending(&pixmap->drawable);
 }

commit c6cd10f536e099277cdc46643725a5a50ea8b525
Author: Chris Wilson <chris@chris-wilson.co.uk>
Date:   Thu Jun 5 22:43:37 2014 +0100

    sna: Hook up a backlight udev monitor for external changes
    
    Changes to the backlights are notified through uevents. Hooking up a
    udev monitor to listen out for external changes to the backlight (e.g.
    through ACPI function keys, or by the user writing to
    /sys/class/backlight directly) is easier than enabling polling on the
    backlight sysfs file using X's select() mechanism.
    
    Since we listen to backlight changes, we have to be careful not to
    confuse the side-effects of disabling connectors (which may cause either
    ourselves or the kernel to turn off the backlight) with the user value.
    
    Many thanks to Alexander Mezin for the suggestion to use udev for
    tracking the notifications for external changes to the backlight.
    
    Reported-by: Alexander Mezin <mezin.alexander@gmail.com>
    Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=79699
    Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>

diff --git a/src/sna/sna.h b/src/sna/sna.h
index 7845cd1..0739f7b 100644
--- a/src/sna/sna.h
+++ b/src/sna/sna.h
@@ -298,6 +298,11 @@ struct sna {
 		unsigned serial;
 
 		uint32_t *encoders;
+
+#if HAVE_UDEV
+		struct udev_monitor *backlight_monitor;
+		pointer backlight_handler;
+#endif
 	} mode;
 
 	struct {
diff --git a/src/sna/sna_display.c b/src/sna/sna_display.c
index 2dbe3f3..c6bb9cd 100644
--- a/src/sna/sna_display.c
+++ b/src/sna/sna_display.c
@@ -399,6 +399,129 @@ static void gem_close(int fd, uint32_t handle)
 #define BACKLIGHT_DEPRECATED_NAME  "BACKLIGHT"
 static Atom backlight_atom, backlight_deprecated_atom;
 
+#if HAVE_UDEV
+static void
+sna_backlight_uevent(int fd, void *closure)
+{
+	struct sna *sna = closure;
+	xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(sna->scrn);
+	int i;
+
+	DBG(("%s()\n", __FUNCTION__));
+
+	/* Drain the event queue */
+	do {
+		struct udev_device *dev;
+
+		dev = udev_monitor_receive_device(sna->mode.backlight_monitor);
+		if (dev == NULL)
+			break;
+
+		udev_device_unref(dev);
+	} while (1);
+
+	/* Query all backlights for any changes */
+	for (i = 0; i < sna->mode.num_real_output; i++) {
+		xf86OutputPtr output = config->output[i];
+		struct sna_output *sna_output = to_sna_output(output);
+		int val;
+
+		if (sna_output->dpms_mode != DPMSModeOn)
+			continue;
+
+		assert(output->randr_output);
+
+		val = backlight_get(&sna_output->backlight);
+		if (val < 0)
+			continue;
+		DBG(("%s(%s): backlight '%s' was %d, now %d\n",
+		     __FUNCTION__, output->name, sna_output->backlight.iface,
+		     sna_output->backlight_active_level, val));
+
+		if (val == sna_output->backlight_active_level)
+			continue;
+
+		sna_output->backlight_active_level = val;
+
+		DBG(("%s(%s): sending change notification\n", __FUNCTION__, output->name));
+		RRChangeOutputProperty(output->randr_output,
+				       backlight_atom, XA_INTEGER,
+				       32, PropModeReplace, 1, &val,
+				       TRUE, FALSE);
+		RRChangeOutputProperty(output->randr_output,
+				       backlight_deprecated_atom, XA_INTEGER,
+				       32, PropModeReplace, 1, &val,
+				       TRUE, FALSE);
+	}
+}
+
+static void sna_backlight_pre_init(struct sna *sna)
+{
+	struct udev *u;
+	struct udev_monitor *mon;
+
+	u = udev_new();
+	if (!u)
+		return;
+
+	mon = udev_monitor_new_from_netlink(u, "udev");
+	if (!mon)
+		goto free_udev;
+
+	if (udev_monitor_filter_add_match_subsystem_devtype(mon, "backlight", NULL))
+		goto free_monitor;
+
+	if (udev_monitor_enable_receiving(mon))
+		goto free_monitor;
+
+	sna->mode.backlight_handler =
+		xf86AddGeneralHandler(udev_monitor_get_fd(mon),
+				      sna_backlight_uevent, sna);
+	if (!sna->mode.backlight_handler)
+		goto free_monitor;
+
+	DBG(("%s: installed backlight monitor\n", __FUNCTION__));
+	sna->mode.backlight_monitor = mon;
+
+	return;
+
+free_monitor:
+	udev_monitor_unref(mon);
+free_udev:
+	udev_unref(u);
+}
+
+static void sna_backlight_drain_uevents(struct sna *sna)
+{
+	if (sna->mode.backlight_monitor == NULL)
+		return;
+
+	sna_backlight_uevent(udev_monitor_get_fd(sna->mode.backlight_monitor),
+			     sna);
+}
+
+static void sna_backlight_close(struct sna *sna)
+{
+	struct udev *u;
+
+	if (sna->mode.backlight_handler == NULL)
+		return;
+
+	xf86RemoveGeneralHandler(sna->mode.backlight_handler);
+
+	u = udev_monitor_get_udev(sna->mode.backlight_monitor);
+	udev_monitor_unref(sna->mode.backlight_monitor);
+	udev_unref(u);
+
+	sna->mode.backlight_handler = NULL;
+	sna->mode.backlight_monitor = NULL;
+}
+#else
+static void sna_backlight_pre_init(struct sna *sna) { }
+static void sna_backlight_drain_uevents(struct sna *sna) { }
+static void sna_backlight_close(struct sna *sna) { }
+#endif
+
 static void
 sna_output_backlight_set(xf86OutputPtr output, int level)
 {
@@ -417,6 +540,12 @@ sna_output_backlight_set(xf86OutputPtr output, int level)
 			RRDeleteOutputProperty(output->randr_output, backlight_deprecated_atom);
 		}
 	}
+
+	/* Consume the uevent notification now so that we don't misconstrue
+	 * the change latter when we wake up and the output is in a different
+	 * state.
+	 */
+	sna_backlight_drain_uevents(to_sna(output->scrn));
 }
 
 static int
@@ -820,6 +949,14 @@ sna_crtc_apply(xf86CrtcPtr crtc)
 	for (i = 0; i < config->num_output; i++) {
 		xf86OutputPtr output = config->output[i];
 
+		/* Make sure we mark the output as off (and save the backlight)
+		 * before the kernel turns it off due to changing the pipe.
+		 * This is necessary as the kernel may turn off the backlight
+		 * and we lose track of the user settings.
+		 */
+		if (output->crtc == NULL)
+			output->funcs->dpms(output, DPMSModeOff);
+
 		if (output->crtc != crtc)
 			continue;
 
@@ -2374,6 +2511,7 @@ sna_output_dpms(xf86OutputPtr output, int dpms)
 {
 	struct sna *sna = to_sna(output->scrn);
 	struct sna_output *sna_output = output->driver_private;
+	int old_dpms = sna_output->dpms_mode;
 
 	DBG(("%s(%s:%d): dpms=%d (current: %d), active? %d\n",
 	     __FUNCTION__, output->name, sna_output->id,
@@ -2383,7 +2521,7 @@ sna_output_dpms(xf86OutputPtr output, int dpms)
 	if (!sna_output->id)
 		return;
 
-	if (sna_output->dpms_mode == dpms)
+	if (old_dpms == dpms)
 		return;
 
 	/* Record the value of the backlight before turning
@@ -2394,11 +2532,12 @@ sna_output_dpms(xf86OutputPtr output, int dpms)
 	 * and reapply it afterwards.
 	 */
 	if (sna_output->backlight.iface && dpms != DPMSModeOn) {
-		if (sna_output->dpms_mode == DPMSModeOn) {
+		if (old_dpms == DPMSModeOn) {
 			sna_output->backlight_active_level = sna_output_backlight_get(output);
 			DBG(("%s: saving current backlight %d\n",
 			     __FUNCTION__, sna_output->backlight_active_level));
 		}
+		sna_output->dpms_mode = dpms;
 		sna_output_backlight_set(output, 0);
 	}
 
@@ -2407,7 +2546,7 @@ sna_output_dpms(xf86OutputPtr output, int dpms)
 					sna_output->id,
 					sna_output->dpms_id,
 					dpms))
-		dpms = sna_output->dpms_mode;
+		dpms = old_dpms;
 
 	if (sna_output->backlight.iface && dpms == DPMSModeOn) {
 		DBG(("%s: restoring previous backlight %d\n",
@@ -2577,9 +2716,9 @@ sna_output_set_property(xf86OutputPtr output, Atom property,
 		if (val < 0 || val > sna_output->backlight.max)
 			return FALSE;
 
+		sna_output->backlight_active_level = val;
 		if (sna_output->dpms_mode == DPMSModeOn)
 			sna_output_backlight_set(output, val);
-		sna_output->backlight_active_level = val;
 		return TRUE;
 	}
 
@@ -4736,6 +4875,7 @@ bool sna_mode_pre_init(ScrnInfoPtr scrn, struct sna *sna)
 		drmModeFreeResources(res);
 
 		sna_cursor_pre_init(sna);
+		sna_backlight_pre_init(sna);
 	} else {
 		if (num_fake == 0)
 			num_fake = 1;
@@ -4772,6 +4912,7 @@ sna_mode_close(struct sna *sna)
 	if (sna->flags & SNA_IS_HOSTED)
 		return;
 
+	sna_backlight_close(sna);
 	sna_cursor_close(sna);
 
 	for (i = 0; i < sna->mode.num_real_crtc; i++)

commit b545e10c50cbb2dd6f9fd53369667bed0d8f1b51
Author: Chris Wilson <chris@chris-wilson.co.uk>
Date:   Thu Jun 5 15:54:32 2014 +0100

    sna: When the output is off, report the cached backlight value
    
    Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>

diff --git a/src/sna/sna_display.c b/src/sna/sna_display.c
index 8757847..2dbe3f3 100644
--- a/src/sna/sna_display.c
+++ b/src/sna/sna_display.c
@@ -2646,9 +2646,17 @@ sna_output_get_property(xf86OutputPtr output, Atom property)
 		if (!sna_output->backlight.iface)
 			return FALSE;
 
-		val = sna_output_backlight_get(output);
-		if (val < 0)
-			return FALSE;
+		if (sna_output->dpms_mode == DPMSModeOn) {
+			val = sna_output_backlight_get(output);
+			if (val < 0)
+				return FALSE;
+			DBG(("%s(%s): output on, reporting actual backlight value [%d]\n",
+			     __FUNCTION__, output->name, val));
+		} else {
+			val = sna_output->backlight_active_level;
+			DBG(("%s(%s): output off, reporting cached backlight value [%d]\n",
+			     __FUNCTION__, output->name, val));
+		}
 
 		err = RRChangeOutputProperty(output->randr_output, property,
 					     XA_INTEGER, 32, PropModeReplace, 1, &val,

commit f0b8720807d9c1994e6db91a607efe4711b0d1ca
Author: Chris Wilson <chris@chris-wilson.co.uk>
Date:   Thu Jun 5 15:31:54 2014 +0100

    sna: Inline calls to save/restore backlight around output DPMS
    
    The intertwined logic was a little confusing to read mixed between the
    functions, so inline it.
    
    Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>

diff --git a/src/sna/sna_display.c b/src/sna/sna_display.c
index bfc95f6..8757847 100644
--- a/src/sna/sna_display.c
+++ b/src/sna/sna_display.c
@@ -475,6 +475,8 @@ done:
 	     output->name, best_iface ?: "none"));
 	sna_output->backlight_active_level =
 		backlight_open(&sna_output->backlight, best_iface);
+	DBG(("%s(%s): initial backlight value %d\n",
+	     __FUNCTION__, output->name, sna_output->backlight_active_level));
 	if (sna_output->backlight_active_level < 0)
 		return;
 
@@ -2368,29 +2370,6 @@ sna_output_destroy(xf86OutputPtr output)
 }
 
 static void
-sna_output_dpms_backlight(xf86OutputPtr output, int oldmode, int mode)
-{
-	struct sna_output *sna_output = output->driver_private;
-
-	if (!sna_output->backlight.iface)
-		return;
-
-	DBG(("%s(%s:%d) -- %d -> %d\n", __FUNCTION__, output->name, sna_output->id, oldmode, mode));
-
-	if (mode == DPMSModeOn) {
-		/* If we're going from off->on we may need to turn on the backlight. */
-		if (oldmode != DPMSModeOn)
-			sna_output_backlight_set(output,
-						   sna_output->backlight_active_level);
-	} else {
-		/* Only save the current backlight value if we're going from on to off. */
-		if (oldmode == DPMSModeOn)
-			sna_output->backlight_active_level = sna_output_backlight_get(output);
-		sna_output_backlight_set(output, 0);
-	}
-}
-
-static void
 sna_output_dpms(xf86OutputPtr output, int dpms)
 {
 	struct sna *sna = to_sna(output->scrn);
@@ -2414,10 +2393,14 @@ sna_output_dpms(xf86OutputPtr output, int dpms)
 	 * record the value before the kernel modifies it
 	 * and reapply it afterwards.
 	 */
-	if (dpms != DPMSModeOn)
-		sna_output_dpms_backlight(output,
-					  sna_output->dpms_mode,
-					  dpms);
+	if (sna_output->backlight.iface && dpms != DPMSModeOn) {
+		if (sna_output->dpms_mode == DPMSModeOn) {
+			sna_output->backlight_active_level = sna_output_backlight_get(output);
+			DBG(("%s: saving current backlight %d\n",
+			     __FUNCTION__, sna_output->backlight_active_level));
+		}
+		sna_output_backlight_set(output, 0);
+	}
 
 	if (output->crtc &&
 	    drmModeConnectorSetProperty(sna->kgem.fd,
@@ -2426,10 +2409,12 @@ sna_output_dpms(xf86OutputPtr output, int dpms)
 					dpms))
 		dpms = sna_output->dpms_mode;
 
-	if (dpms == DPMSModeOn)
-		sna_output_dpms_backlight(output,
-					  sna_output->dpms_mode,
-					  dpms);
+	if (sna_output->backlight.iface && dpms == DPMSModeOn) {
+		DBG(("%s: restoring previous backlight %d\n",
+		     __FUNCTION__, sna_output->backlight_active_level));
+		sna_output_backlight_set(output,
+					 sna_output->backlight_active_level);
+	}
 
 	sna_output->dpms_mode = dpms;
 }

commit 9f7775158341c8384da870e06cab200deddf48c1
Author: Chris Wilson <chris@chris-wilson.co.uk>
Date:   Thu Jun 5 15:31:27 2014 +0100

    sna: Set initial output DPMS state from kernel
    
    Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>

diff --git a/src/sna/sna_display.c b/src/sna/sna_display.c
index b0edd06..bfc95f6 100644
--- a/src/sna/sna_display.c
+++ b/src/sna/sna_display.c
@@ -2023,13 +2023,6 @@ find_property(struct sna *sna, struct sna_output *output, const char *name)
 	return -1;
 }
 
-static int
-find_property_id(struct sna *sna, struct sna_output *output, const char *name)
-{
-	int idx = find_property(sna, output, name);
-	return idx != -1 ? output->prop_ids[idx] : 0;
-}
-
 static xf86OutputStatus
 sna_output_detect(xf86OutputPtr output)
 {
@@ -2913,7 +2906,7 @@ sna_output_add(struct sna *sna, int id, int serial)
 	char name[32];
 	int len, i;
 
-	DBG(("%s(%d)\n", __FUNCTION__, id));
+	DBG(("%s(%d): serial=%d\n", __FUNCTION__, id, serial));
 
 	COMPILE_TIME_ASSERT(sizeof(struct drm_mode_get_connector) <= sizeof(compat_conn.pad));
 
@@ -3007,7 +3000,6 @@ sna_output_add(struct sna *sna, int id, int serial)
 	sna_output->num_props = compat_conn.conn.count_props;
 	sna_output->prop_ids = malloc(sizeof(uint32_t)*compat_conn.conn.count_props);
 	sna_output->prop_values = malloc(sizeof(uint64_t)*compat_conn.conn.count_props);
-	sna_output->dpms_mode = DPMSModeOff;
 
 	compat_conn.conn.count_encoders = 0;
 
@@ -3059,7 +3051,16 @@ sna_output_add(struct sna *sna, int id, int serial)
 	sna_output->id = compat_conn.conn.connector_id;
 	sna_output->is_panel = is_panel(compat_conn.conn.connector_type);
 	sna_output->edid_idx = find_property(sna, sna_output, "EDID");
-	sna_output->dpms_id = find_property_id(sna, sna_output, "DPMS");
+	i = find_property(sna, sna_output, "DPMS");
+	if (i != -1) {
+		sna_output->dpms_id = sna_output->prop_ids[i];
+		sna_output->dpms_mode = sna_output->prop_values[i];
+		DBG(("%s: found 'DPMS' (idx=%d, id=%d), initial value=%d\n",
+		     __FUNCTION__, i, sna_output->dpms_id, sna_output->dpms_mode));
+	} else {
+		sna_output->dpms_id = -1;
+		sna_output->dpms_mode = DPMSModeOff;
+	}
 
 	sna_output->possible_encoders = possible_encoders;
 	sna_output->attached_encoders = attached_encoders;

commit 9e02fe269dc79ff867d1d2cb3a8673ee2861bd56
Author: Chris Wilson <chris@chris-wilson.co.uk>
Date:   Thu Jun 5 15:49:03 2014 +0100

    sna: Make sure we recompute the vblank interval after reconfiguring the CRTC
    
    Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>

diff --git a/src/sna/sna_display.c b/src/sna/sna_display.c
index 4006efe..b0edd06 100644
--- a/src/sna/sna_display.c
+++ b/src/sna/sna_display.c
@@ -1785,10 +1785,8 @@ sna_crtc_dpms(xf86CrtcPtr crtc, int mode)
 	} else
 		sna_crtc_disable(crtc);
 
-	if (priv->bo != NULL) {
+	if (priv->bo != NULL)
 		priv->dpms_mode = mode;
-		update_flush_interval(to_sna(crtc->scrn));
-	}
 }
 
 void sna_mode_adjust_frame(struct sna *sna, int x, int y)
@@ -4670,6 +4668,7 @@ sna_crtc_config_notify(ScreenPtr screen)
 		return;
 
 	probe_capabilities(sna);
+	update_flush_interval(sna);
 
 	sna_cursors_reload(sna);
 

commit 51909f37ed6405c38f1c46a4012952be79031994
Author: Chris Wilson <chris@chris-wilson.co.uk>
Date:   Thu Jun 5 14:54:55 2014 +0100

    sna: Skip marking the CRTC as damaged for TearFree
    
    As we have reorder the application of damage on a TearFree frontbuffer
    to before we attach the CRTC, we do not need to then schedule the update
    afterwards (until it gets drawn by the client).
    
    Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>

diff --git a/src/sna/sna_display.c b/src/sna/sna_display.c
index 46fc22c..4006efe 100644
--- a/src/sna/sna_display.c
+++ b/src/sna/sna_display.c
@@ -1756,7 +1756,7 @@ retry: /* Attach per-crtc pixmap or direct */
 	}
 
 	sna_crtc_randr(crtc);
-	if (sna_crtc->shadow)
+	if (sna_crtc->transform)
 		sna_crtc_damage(crtc);
 	sna->mode.front_active += saved_bo == NULL;
 	sna->mode.dirty = true;

commit c7efe88a895a692e5f840562a09dac385508db57
Author: Chris Wilson <chris@chris-wilson.co.uk>
Date:   Thu Jun 5 14:50:04 2014 +0100


Reply to: