diff mbox

[v3] tests/gem_reg_read: Extend and check for valid 36b counter

Message ID 1437045549-15455-1-git-send-email-michal.winiarski@intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

MichaƂ Winiarski July 16, 2015, 11:19 a.m. UTC
When reading the timestamp register with single 64b read, we are observing
invalid values on x86_64:

    [f = valid counter value | X = garbage]

    i386:   0x0000000fffffffff
    x86_64: 0xffffffffXXXXXXXX

Test checks if the counter is moving and increasing.
Add a check to see if we can use (reg | 1) flag to get a proper 36b timestamp,
shifting the value on x86_64 if we can't.

v2: More iterations of monotonic test, comments, minor fixups (Chris)
v3: Skip tests if reg_read is not supported

Cc: Chris Wilson <chris@chris-wilson.co.uk>
Signed-off-by: Micha? Winiarski <michal.winiarski@intel.com>
---
 tests/gem_reg_read.c | 141 +++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 120 insertions(+), 21 deletions(-)

Comments

Chris Wilson July 16, 2015, 11:24 a.m. UTC | #1
On Thu, Jul 16, 2015 at 01:19:09PM +0200, Micha? Winiarski wrote:
> When reading the timestamp register with single 64b read, we are observing
> invalid values on x86_64:
> 
>     [f = valid counter value | X = garbage]
> 
>     i386:   0x0000000fffffffff
>     x86_64: 0xffffffffXXXXXXXX
> 
> Test checks if the counter is moving and increasing.
> Add a check to see if we can use (reg | 1) flag to get a proper 36b timestamp,
> shifting the value on x86_64 if we can't.
> 
> v2: More iterations of monotonic test, comments, minor fixups (Chris)
> v3: Skip tests if reg_read is not supported
> 
> Cc: Chris Wilson <chris@chris-wilson.co.uk>
> Signed-off-by: Micha? Winiarski <michal.winiarski@intel.com>

Lgtm,
Reviewed-by: Chris Wilson <chris@chris-wilson.co.uk>
-Chris
Lespiau, Damien July 16, 2015, 12:04 p.m. UTC | #2
On Thu, Jul 16, 2015 at 12:24:19PM +0100, Chris Wilson wrote:
> On Thu, Jul 16, 2015 at 01:19:09PM +0200, Micha? Winiarski wrote:
> > When reading the timestamp register with single 64b read, we are observing
> > invalid values on x86_64:
> > 
> >     [f = valid counter value | X = garbage]
> > 
> >     i386:   0x0000000fffffffff
> >     x86_64: 0xffffffffXXXXXXXX
> > 
> > Test checks if the counter is moving and increasing.
> > Add a check to see if we can use (reg | 1) flag to get a proper 36b timestamp,
> > shifting the value on x86_64 if we can't.
> > 
> > v2: More iterations of monotonic test, comments, minor fixups (Chris)
> > v3: Skip tests if reg_read is not supported
> > 
> > Cc: Chris Wilson <chris@chris-wilson.co.uk>
> > Signed-off-by: Micha? Winiarski <michal.winiarski@intel.com>
> 
> Lgtm,
> Reviewed-by: Chris Wilson <chris@chris-wilson.co.uk>

Pushed! thanks for the patch and review.
Daniel Vetter July 21, 2015, 6:48 a.m. UTC | #3
On Thu, Jul 16, 2015 at 2:04 PM, Damien Lespiau
<damien.lespiau@intel.com> wrote:
> On Thu, Jul 16, 2015 at 12:24:19PM +0100, Chris Wilson wrote:
>> On Thu, Jul 16, 2015 at 01:19:09PM +0200, Micha? Winiarski wrote:
>> > When reading the timestamp register with single 64b read, we are observing
>> > invalid values on x86_64:
>> >
>> >     [f = valid counter value | X = garbage]
>> >
>> >     i386:   0x0000000fffffffff
>> >     x86_64: 0xffffffffXXXXXXXX
>> >
>> > Test checks if the counter is moving and increasing.
>> > Add a check to see if we can use (reg | 1) flag to get a proper 36b timestamp,
>> > shifting the value on x86_64 if we can't.
>> >
>> > v2: More iterations of monotonic test, comments, minor fixups (Chris)
>> > v3: Skip tests if reg_read is not supported
>> >
>> > Cc: Chris Wilson <chris@chris-wilson.co.uk>
>> > Signed-off-by: Micha? Winiarski <michal.winiarski@intel.com>
>>
>> Lgtm,
>> Reviewed-by: Chris Wilson <chris@chris-wilson.co.uk>
>
> Pushed! thanks for the patch and review.

This is a testcase for new abi and the kernel side hasn't landed yet.
Intentional breach of procedures?
-Daniel
Lespiau, Damien July 21, 2015, 7:10 a.m. UTC | #4
On Tue, Jul 21, 2015 at 08:48:05AM +0200, Daniel Vetter wrote:
> On Thu, Jul 16, 2015 at 2:04 PM, Damien Lespiau
> <damien.lespiau@intel.com> wrote:
> > On Thu, Jul 16, 2015 at 12:24:19PM +0100, Chris Wilson wrote:
> >> On Thu, Jul 16, 2015 at 01:19:09PM +0200, Micha? Winiarski wrote:
> >> > When reading the timestamp register with single 64b read, we are observing
> >> > invalid values on x86_64:
> >> >
> >> >     [f = valid counter value | X = garbage]
> >> >
> >> >     i386:   0x0000000fffffffff
> >> >     x86_64: 0xffffffffXXXXXXXX
> >> >
> >> > Test checks if the counter is moving and increasing.
> >> > Add a check to see if we can use (reg | 1) flag to get a proper 36b timestamp,
> >> > shifting the value on x86_64 if we can't.
> >> >
> >> > v2: More iterations of monotonic test, comments, minor fixups (Chris)
> >> > v3: Skip tests if reg_read is not supported
> >> >
> >> > Cc: Chris Wilson <chris@chris-wilson.co.uk>
> >> > Signed-off-by: Micha? Winiarski <michal.winiarski@intel.com>
> >>
> >> Lgtm,
> >> Reviewed-by: Chris Wilson <chris@chris-wilson.co.uk>
> >
> > Pushed! thanks for the patch and review.
> 
> This is a testcase for new abi and the kernel side hasn't landed yet.
> Intentional breach of procedures?

Nop, was just overlooked.
diff mbox

Patch

diff --git a/tests/gem_reg_read.c b/tests/gem_reg_read.c
index d3e68d9..28ecdf5 100644
--- a/tests/gem_reg_read.c
+++ b/tests/gem_reg_read.c
@@ -28,10 +28,15 @@ 
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
+#include <sys/utsname.h>
+#include <time.h>
 
 #include "ioctl_wrappers.h"
 #include "drmtest.h"
 
+static bool is_x86_64;
+static bool has_proper_timestamp;
+
 struct local_drm_i915_reg_read {
 	__u64 offset;
 	__u64 val; /* Return value */
@@ -39,39 +44,133 @@  struct local_drm_i915_reg_read {
 
 #define REG_READ_IOCTL DRM_IOWR(DRM_COMMAND_BASE + 0x31, struct local_drm_i915_reg_read)
 
-static uint64_t timer_query(int fd)
+#define RENDER_RING_TIMESTAMP 0x2358
+
+static int read_register(int fd, uint64_t offset, uint64_t * val)
 {
+	int ret = 0;
 	struct local_drm_i915_reg_read reg_read;
+	reg_read.offset = offset;
+
+	if (drmIoctl(fd, REG_READ_IOCTL, &reg_read))
+		ret = -errno;
 
-	reg_read.offset = 0x2358;
-	igt_fail_on_f(drmIoctl(fd, REG_READ_IOCTL, &reg_read),
-		      "positive test case failed: ");
+	*val = reg_read.val;
 
-	return reg_read.val;
+	return ret;
 }
 
-igt_simple_main
+static bool check_kernel_x86_64(void)
 {
-	struct local_drm_i915_reg_read reg_read;
-	int fd, ret;
+	int ret;
+	struct utsname uts;
+
+	ret = uname(&uts);
+	igt_assert(ret == 0);
+
+	if (!strcmp(uts.machine, "x86_64"))
+		return true;
+
+	return false;
+}
+
+static bool check_timestamp(int fd)
+{
+	int ret;
+	uint64_t val;
+
+	ret = read_register(fd, RENDER_RING_TIMESTAMP | 1, &val);
+
+	return ret == 0;
+}
+
+static int timer_query(int fd, uint64_t * val)
+{
+	uint64_t offset;
+	int ret;
+
+	offset = RENDER_RING_TIMESTAMP;
+	if (has_proper_timestamp)
+		offset |= 1;
+
+	ret = read_register(fd, offset, val);
+
+/*
+ * When reading the timestamp register with single 64b read, we are observing
+ * invalid values on x86_64:
+ *
+ *      [f = valid counter value | X = garbage]
+ *
+ *      i386:   0x0000000fffffffff
+ *      x86_64: 0xffffffffXXXXXXXX
+ *
+ * In the absence of a corrected register read ioctl, attempt
+ * to fix up the return value to be vaguely useful.
+ */
 
-	fd = drm_open_any();
+	if (is_x86_64 && !has_proper_timestamp)
+		*val >>= 32;
 
-	reg_read.offset = 0x2358;
-	ret = drmIoctl(fd, REG_READ_IOCTL, &reg_read);
-	igt_assert(ret == 0 || errno == EINVAL);
-	igt_require(ret == 0);
+	return ret;
+}
+
+static void test_timestamp_moving(int fd)
+{
+	uint64_t first_val, second_val;
 
-	reg_read.val = timer_query(fd);
+	igt_fail_on(timer_query(fd, &first_val) != 0);
 	sleep(1);
-	/* Check that timer is moving and isn't busted. */
-	igt_assert(timer_query(fd) != reg_read.val);
+	igt_fail_on(timer_query(fd, &second_val) != 0);
+	igt_assert(second_val != first_val);
+}
+
+static void test_timestamp_monotonic(int fd)
+{
+	uint64_t first_val, second_val;
+	time_t start;
+	bool retry = true;
+
+	igt_fail_on(timer_query(fd, &first_val) != 0);
+	time(&start);
+	do {
+retry:
+		igt_fail_on(timer_query(fd, &second_val) != 0);
+		if (second_val < first_val && retry) {
+		/* We may hit timestamp overflow once */
+			retry = false;
+			first_val = second_val;
+			goto retry;
+		}
+		igt_assert(second_val >= first_val);
+	} while(difftime(time(NULL), start) < 5);
+
+}
+
+igt_main
+{
+	uint64_t val = 0;
+	int fd = -1;
+
+	igt_fixture {
+		fd = drm_open_any();
+		is_x86_64 = check_kernel_x86_64();
+		has_proper_timestamp = check_timestamp(fd);
+	}
+
+	igt_subtest("bad-register")
+		igt_assert_eq(read_register(fd, 0x12345678, &val), -EINVAL);
 
-	/* bad reg */
-	reg_read.offset = 0x12345678;
-	ret = drmIoctl(fd, REG_READ_IOCTL, &reg_read);
+	igt_subtest("timestamp-moving") {
+		igt_skip_on(timer_query(fd, &val) != 0);
+		test_timestamp_moving(fd);
+	}
 
-	igt_assert(ret != 0 && errno == EINVAL);
+	igt_subtest("timestamp-monotonic") {
+		igt_skip_on(timer_query(fd, &val) != 0);
+		test_timestamp_monotonic(fd);
+	}
 
-	close(fd);
+	igt_fixture {
+		close(fd);
+	}
 }