Skip to contentSkip to conversation

drm/panthor: extend timestamp query with flags

PATCH Superseded Marcin Slusarz 4 participants 23 messages Mar 18 – Mar 24

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/panthor: extend timestamp query with flags

Flags now control which data user space wants to query, there is more information sources, and there's ability to query duration of multiple timestamp reads.

New sources:
- CPU's monotonic,
- CPU's monotonic raw,
- GPU's cycle count

These changes should make the implementation of VK_KHR_calibrated_timestamps more accurate and much simpler.

---
This is counter proposal to https://lore.kernel.org/all/[email protected]/

---
 drivers/gpu/drm/panthor/panthor_drv.c | 124 ++++++++++++++++++++++++--
 include/uapi/drm/panthor_drm.h        |  51 ++++++++++-
 2 files changed, 166 insertions(+), 9 deletions(-)
drivers/gpu/drm/panthor/panthor_drv.c +117 -7
index 165dddfde6ca..19ede20a578e 100644
@@ -13,7 +13,9 @@
 #include <linux/pagemap.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
+#include <linux/sched/clock.h>
 #include <linux/time64.h>
+#include <linux/time_namespace.h>
 
 #include <drm/drm_auth.h>
 #include <drm/drm_debugfs.h>
@@ -762,21 +764,123 @@ static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx,
 }
 
 static int panthor_query_timestamp_info(struct panthor_device *ptdev,
-					struct drm_panthor_timestamp_info *arg)
+					struct drm_panthor_timestamp_info *arg,
+					u32 size)
 {
 	int ret;
+	u32 flags;
+	unsigned long irq_flags;
+	struct timespec64 cpu_ts;
+	u64 query_start_time;
+	bool minimize_interruption;
+	u32 timestamp_types = 0;
+
+	if (size >= offsetof(struct drm_panthor_timestamp_info, pad1) + sizeof(arg->pad1) &&
+			arg->pad1 != 0)
+		return -EINVAL;
+
+	if (size >= offsetof(struct drm_panthor_timestamp_info, flags) + sizeof(arg->flags))
+		flags = arg->flags;
+	else
+		flags = DRM_PANTHOR_TIMESTAMP_GPU |
+			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+			DRM_PANTHOR_TIMESTAMP_FREQ;
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case 0:
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		timestamp_types++;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	if (flags & ~(DRM_PANTHOR_TIMESTAMP_GPU |
+		      DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK |
+		      DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+		      DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT |
+		      DRM_PANTHOR_TIMESTAMP_FREQ |
+		      DRM_PANTHOR_TIMESTAMP_DURATION))
+		return -EINVAL;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		timestamp_types++;
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		timestamp_types++;
+
+	minimize_interruption =
+		(flags & DRM_PANTHOR_TIMESTAMP_DURATION) ||
+		(timestamp_types >= 2);
 
 	ret = panthor_device_resume_and_get(ptdev);
 	if (ret)
 		return ret;
 
+	if (flags & DRM_PANTHOR_TIMESTAMP_FREQ) {
 #ifdef CONFIG_ARM_ARCH_TIMER
-	arg->timestamp_frequency = arch_timer_get_cntfrq();
+		arg->timestamp_frequency = arch_timer_get_cntfrq();
 #else
-	arg->timestamp_frequency = 0;
+		arg->timestamp_frequency = 0;
 #endif
-	arg->current_timestamp = gpu_read64_counter(ptdev, GPU_TIMESTAMP);
-	arg->timestamp_offset = gpu_read64(ptdev, GPU_TIMESTAMP_OFFSET);
+	} else {
+		arg->timestamp_frequency = 0;
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_OFFSET)
+		arg->timestamp_offset = gpu_read64(ptdev, GPU_TIMESTAMP_OFFSET);
+	else
+		arg->timestamp_offset = 0;
+
+	if (minimize_interruption) {
+		preempt_disable();
+		local_irq_save(irq_flags);
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_DURATION)
+		query_start_time = local_clock();
+	else
+		query_start_time = 0;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		arg->current_timestamp = gpu_read64_counter(ptdev, GPU_TIMESTAMP);
+	else
+		arg->current_timestamp = 0;
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+		ktime_get_ts64(&cpu_ts);
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		ktime_get_raw_ts64(&cpu_ts);
+		break;
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		arg->cycle_count = gpu_read64_counter(ptdev, GPU_CYCLE_COUNT);
+	else
+		arg->cycle_count = 0;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_DURATION)
+		arg->duration_nsec = local_clock() - query_start_time;
+	else
+		arg->duration_nsec = 0;
+
+	if (minimize_interruption) {
+		local_irq_restore(irq_flags);
+		preempt_enable();
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+		timens_add_monotonic(&cpu_ts);
+
+		arg->cpu_timestamp_sec = cpu_ts.tv_sec;
+		arg->cpu_timestamp_nsec = cpu_ts.tv_nsec;
+	} else {
+		arg->cpu_timestamp_sec = 0;
+		arg->cpu_timestamp_nsec = 0;
+	}
 
 	pm_runtime_put(ptdev->base.dev);
 	return 0;
@@ -851,7 +955,12 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d
 		return PANTHOR_UOBJ_SET(args->pointer, args->size, ptdev->csif_info);
 
 	case DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO:
-		ret = panthor_query_timestamp_info(ptdev, &timestamp_info);
+		if (copy_from_user(&timestamp_info,
+				   (const void __user __force *) args->pointer,
+				   args->size))
+			return -EFAULT;
+
+		ret = panthor_query_timestamp_info(ptdev, &timestamp_info, args->size);
 
 		if (ret)
 			return ret;
@@ -1680,6 +1789,7 @@ static void panthor_debugfs_init(struct drm_minor *minor)
  *       - adds DRM_IOCTL_PANTHOR_BO_SYNC ioctl
  *       - adds DRM_IOCTL_PANTHOR_BO_QUERY_INFO ioctl
  *       - adds drm_panthor_gpu_info::selected_coherency
+ * - 1.8 - extends DEV_QUERY_TIMESTAMP_INFO with flags
  */
 static const struct drm_driver panthor_drm_driver = {
 	.driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ |
@@ -1693,7 +1803,7 @@ static const struct drm_driver panthor_drm_driver = {
 	.name = "panthor",
 	.desc = "Panthor DRM driver",
 	.major = 1,
-	.minor = 7,
+	.minor = 8,
 
 	.gem_create_object = panthor_gem_create_object,
 	.gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table,
include/uapi/drm/panthor_drm.h +49 -2

66 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 50 lines · On Wed, 18 Mar 2026 12:29:52 +0100 Marcin Slusarz <[email protected]>…

Flags now control which data user space wants to query, there is more information sources, and there's ability to query duration of multiple timestamp reads.

New sources:
- CPU's monotonic,
- CPU's monotonic raw,
- GPU's cycle count

These changes should make the implementation of VK_KHR_calibrated_timestamps more accurate and much simpler.

---
This is counter proposal to https://lore.kernel.org/all/[email protected]/

---
 drivers/gpu/drm/panthor/panthor_drv.c | 124 ++++++++++++++++++++++++--
 include/uapi/drm/panthor_drm.h        |  51 ++++++++++-
 2 files changed, 166 insertions(+), 9 deletions(-)
drivers/gpu/drm/panthor/panthor_drv.c +11 -1
index 165dddfde6ca..19ede20a578e 100644
@@ -13,7 +13,9 @@
 #include <linux/pagemap.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
+#include <linux/sched/clock.h>
 #include <linux/time64.h>
+#include <linux/time_namespace.h>
 
 #include <drm/drm_auth.h>
 #include <drm/drm_debugfs.h>
@@ -762,21 +764,123 @@ static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx,
 }
 
 static int panthor_query_timestamp_info(struct panthor_device *ptdev,
-					struct drm_panthor_timestamp_info *arg)
+					struct drm_panthor_timestamp_info *arg,
+					u32 size)
 {
 	int ret;
+	u32 flags;
+	unsigned long irq_flags;
+	struct timespec64 cpu_ts;
+	u64 query_start_time;
+	bool minimize_interruption;
+	u32 timestamp_types = 0;
+

drivers/gpu/drm/panthor/panthor_drv.c static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx, · line 787

Quoted 10 lines
drivers/gpu/drm/panthor/panthor_drv.c +10 -0
+	if (size >= offsetof(struct drm_panthor_timestamp_info, pad1) + sizeof(arg->pad1) &&
+			arg->pad1 != 0)
+		return -EINVAL;
+
+	if (size >= offsetof(struct drm_panthor_timestamp_info, flags) + sizeof(arg->flags))
+		flags = arg->flags;
+	else
+		flags = DRM_PANTHOR_TIMESTAMP_GPU |
+			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+			DRM_PANTHOR_TIMESTAMP_FREQ;

How about we add a DRM_PANTHOR_TIMESTAMP_ADVANCED_QUERY flag that tells the driver whether the default should be picked or not instead of this weird is-this-the-new-or-old-struct detection based on the size.

	if (args->flags & DRM_PANTHOR_TIMESTAMP_ADVANCED_QUERY)
		flags = args->flags;
	else
		flags = DRM_PANTHOR_TIMESTAMP_GPU |
			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
			DRM_PANTHOR_TIMESTAMP_FREQ;
Quoted 18 lines
drivers/gpu/drm/panthor/panthor_drv.c +18 -0
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case 0:
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		timestamp_types++;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	if (flags & ~(DRM_PANTHOR_TIMESTAMP_GPU |
+		      DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK |
+		      DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+		      DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT |
+		      DRM_PANTHOR_TIMESTAMP_FREQ |
+		      DRM_PANTHOR_TIMESTAMP_DURATION))

drivers/gpu/drm/panthor/panthor_drv.c static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx, · line 815

Quoted 10 lines
drivers/gpu/drm/panthor/panthor_drv.c +10 -0
+		return -EINVAL;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		timestamp_types++;
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		timestamp_types++;
+
+	minimize_interruption =
+		(flags & DRM_PANTHOR_TIMESTAMP_DURATION) ||
+		(timestamp_types >= 2);

It's probably worth a comment explaining where the >= 2 comes from and why query duration measurement needs to be done with interrupts disabled (though the latter is a bit more obvious than the former).

Quoted 36 lines
drivers/gpu/drm/panthor/panthor_drv.c +24 -4
 	ret = panthor_device_resume_and_get(ptdev);
 	if (ret)
 		return ret;
 
+	if (flags & DRM_PANTHOR_TIMESTAMP_FREQ) {
 #ifdef CONFIG_ARM_ARCH_TIMER
-	arg->timestamp_frequency = arch_timer_get_cntfrq();
+		arg->timestamp_frequency = arch_timer_get_cntfrq();
 #else
-	arg->timestamp_frequency = 0;
+		arg->timestamp_frequency = 0;
 #endif
-	arg->current_timestamp = gpu_read64_counter(ptdev, GPU_TIMESTAMP);
-	arg->timestamp_offset = gpu_read64(ptdev, GPU_TIMESTAMP_OFFSET);
+	} else {
+		arg->timestamp_frequency = 0;
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_OFFSET)
+		arg->timestamp_offset = gpu_read64(ptdev, GPU_TIMESTAMP_OFFSET);
+	else
+		arg->timestamp_offset = 0;
+
+	if (minimize_interruption) {
+		preempt_disable();
+		local_irq_save(irq_flags);
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_DURATION)
+		query_start_time = local_clock();
+	else
+		query_start_time = 0;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		arg->current_timestamp = gpu_read64_counter(ptdev, GPU_TIMESTAMP);

drivers/gpu/drm/panthor/panthor_drv.c static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx, · line 857

Quoted 10 lines
drivers/gpu/drm/panthor/panthor_drv.c +10 -0
+	else
+		arg->current_timestamp = 0;
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+		ktime_get_ts64(&cpu_ts);
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		ktime_get_raw_ts64(&cpu_ts);
+		break;
	default:
		break;
Quoted 31 lines
drivers/gpu/drm/panthor/panthor_drv.c +26 -0
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		arg->cycle_count = gpu_read64_counter(ptdev, GPU_CYCLE_COUNT);
+	else
+		arg->cycle_count = 0;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_DURATION)
+		arg->duration_nsec = local_clock() - query_start_time;
+	else
+		arg->duration_nsec = 0;
+
+	if (minimize_interruption) {
+		local_irq_restore(irq_flags);
+		preempt_enable();
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+		timens_add_monotonic(&cpu_ts);
+
+		arg->cpu_timestamp_sec = cpu_ts.tv_sec;
+		arg->cpu_timestamp_nsec = cpu_ts.tv_nsec;
+	} else {
+		arg->cpu_timestamp_sec = 0;
+		arg->cpu_timestamp_nsec = 0;
+	}
 
 	pm_runtime_put(ptdev->base.dev);
 	return 0;
@@ -851,7 +955,12 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d
 		return PANTHOR_UOBJ_SET(args->pointer, args->size, ptdev->csif_info);

drivers/gpu/drm/panthor/panthor_drv.c static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d · line 964

Quoted 10 lines
drivers/gpu/drm/panthor/panthor_drv.c +6 -1
 
 	case DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO:
-		ret = panthor_query_timestamp_info(ptdev, &timestamp_info);
+		if (copy_from_user(&timestamp_info,
+				   (const void __user __force *) args->pointer,
+				   args->size))
+			return -EFAULT;
+
+		ret = panthor_query_timestamp_info(ptdev, &timestamp_info, args->size);
 

nit: drop the blank line.

Quoted 26 lines
drivers/gpu/drm/panthor/panthor_drv.c +2 -1
 		if (ret)
 			return ret;
@@ -1680,6 +1789,7 @@ static void panthor_debugfs_init(struct drm_minor *minor)
  *       - adds DRM_IOCTL_PANTHOR_BO_SYNC ioctl
  *       - adds DRM_IOCTL_PANTHOR_BO_QUERY_INFO ioctl
  *       - adds drm_panthor_gpu_info::selected_coherency
+ * - 1.8 - extends DEV_QUERY_TIMESTAMP_INFO with flags
  */
 static const struct drm_driver panthor_drm_driver = {
 	.driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ |
@@ -1693,7 +1803,7 @@ static const struct drm_driver panthor_drm_driver = {
 	.name = "panthor",
 	.desc = "Panthor DRM driver",
 	.major = 1,
-	.minor = 7,
+	.minor = 8,
 
 	.gem_create_object = panthor_gem_create_object,
 	.gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table,
include/uapi/drm/panthor_drm.h +0 -0
index b401ac585d6a..c869e8b95ecd 100644
@@ -409,6 +409,35 @@ struct drm_panthor_csif_info {
 	__u32 pad;
 };

include/uapi/drm/panthor_drm.h struct drm_panthor_csif_info { · line 420

Quoted 10 lines
include/uapi/drm/panthor_drm.h +9 -0
 
+/**
+ * enum drm_panthor_timestamp_info_flags - drm_panthor_timestamp_info.flags
+ */
+enum drm_panthor_timestamp_info_flags {
+	/** @DRM_PANTHOR_TIMESTAMP_GPU: Query GPU time. */
+	DRM_PANTHOR_TIMESTAMP_GPU = 1 << 0,
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC: Query CPU time using CLOCK_MONOTONIC. */
+	DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC = 1 << 1,

Can we define

DRM_PANTHOR_TIMESTAMP_CPU_NONE = 0 << 1,

for completeness.

Quoted 8 lines
include/uapi/drm/panthor_drm.h +8 -0
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW: Query CPU time using CLOCK_MONOTONIC_RAW. */
+	DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW = 2 << 1,
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK: Space reserved for CPU clock type. */
+	DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK = 7 << 1,
+
+	/** @DRM_PANTHOR_TIMESTAMP_GPU_OFFSET: Query GPU offset. */

include/uapi/drm/panthor_drm.h struct drm_panthor_csif_info { · line 438

Quoted 10 lines
include/uapi/drm/panthor_drm.h +10 -0
+	DRM_PANTHOR_TIMESTAMP_GPU_OFFSET = 1 << 4,
+
+	/** @DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT: Query GPU cycle count. */
+	DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT = 1 << 5,
+
+	/** @DRM_PANTHOR_TIMESTAMP_FREQ: Query timestamp frequency. */
+	DRM_PANTHOR_TIMESTAMP_FREQ = 1 << 6,
+
+	/** @DRM_PANTHOR_TIMESTAMP_DURATION: Return duration of time query. */
+	DRM_PANTHOR_TIMESTAMP_DURATION = 1 << 7,

/**
* @DRM_PANTHOR_TIMESTAMP_ADVANCED_QUERY: Advanced query requested. *
* When missing, flags should be 0, which is equivalent to
* DRM_PANTHOR_TIMESTAMP_ADVANCED_QUERY |
* DRM_PANTHOR_TIMESTAMP_GPU |
* DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
* DRM_PANTHOR_TIMESTAMP_FREQ
*/
DRM_PANTHOR_TIMESTAMP_ADVANCED_QUERY = 1 << 30,

Quoted 24 lines
include/uapi/drm/panthor_drm.h +12 -2
+};
+
 /**
  * struct drm_panthor_timestamp_info - Timestamp information
  *
@@ -421,11 +450,29 @@ struct drm_panthor_timestamp_info {
 	 */
 	__u64 timestamp_frequency;
 
-	/** @current_timestamp: The current timestamp. */
+	/** @current_timestamp: The current GPU timestamp. */
 	__u64 current_timestamp;
 
-	/** @timestamp_offset: The offset of the timestamp timer. */
+	/** @timestamp_offset: The offset of the GPU timestamp timer. */
 	__u64 timestamp_offset;
+
+	/** @flags: Bitmask of drm_panthor_timestamp_info_flags. */
+	__u32 flags;
+
+	/** @duration_nsec: Duration of time query. */
+	__u32 duration_nsec;
+
+	/** @cycle_count: Value of GPU_CYCLE_COUNT. */

include/uapi/drm/panthor_drm.h struct drm_panthor_timestamp_info { · line 475

Quoted 10 lines
include/uapi/drm/panthor_drm.h +10 -0
+	__u64 cycle_count;
+
+	/** @cpu_timestamp_sec: Seconds part of CPU timestamp. */
+	__u64 cpu_timestamp_sec;
+
+	/** @cpu_timestamp_nsec: Nanseconds part of CPU timestamp. */
+	__u32 cpu_timestamp_nsec;
+
+	/** @pad1: Padding, MBZ. */
+	__u32 pad1;

Let's re-purpose the existing pad field into flags, move duration_nsec after cpu_timestamp_nsec, and get rid of this pad1.

Quoted 3 lines
include/uapi/drm/panthor_drm.h +0 -0
 };
 
 /**
#2 lore raw
Quoted 59 lines · Boris Brezillon
Quoted 56 lines · On Wed, 18 Mar 2026 12:29:52 +0100 Marcin Slusarz <[email protected]>…

Flags now control which data user space wants to query, there is more information sources, and there's ability to query duration of multiple timestamp reads.

New sources:
- CPU's monotonic,
- CPU's monotonic raw,
- GPU's cycle count

These changes should make the implementation of VK_KHR_calibrated_timestamps more accurate and much simpler.

---
This is counter proposal to https://lore.kernel.org/all/[email protected]/

---
 drivers/gpu/drm/panthor/panthor_drv.c | 124 ++++++++++++++++++++++++--
 include/uapi/drm/panthor_drm.h        |  51 ++++++++++-
 2 files changed, 166 insertions(+), 9 deletions(-)
drivers/gpu/drm/panthor/panthor_drv.c +17 -1
index 165dddfde6ca..19ede20a578e 100644
@@ -13,7 +13,9 @@
 #include <linux/pagemap.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
+#include <linux/sched/clock.h>
 #include <linux/time64.h>
+#include <linux/time_namespace.h>
 
 #include <drm/drm_auth.h>
 #include <drm/drm_debugfs.h>
@@ -762,21 +764,123 @@ static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx,
 }
 
 static int panthor_query_timestamp_info(struct panthor_device *ptdev,
-					struct drm_panthor_timestamp_info *arg)
+					struct drm_panthor_timestamp_info *arg,
+					u32 size)
 {
 	int ret;
+	u32 flags;
+	unsigned long irq_flags;
+	struct timespec64 cpu_ts;
+	u64 query_start_time;
+	bool minimize_interruption;
+	u32 timestamp_types = 0;
+
+	if (size >= offsetof(struct drm_panthor_timestamp_info, pad1) + sizeof(arg->pad1) &&
+			arg->pad1 != 0)
+		return -EINVAL;
+
+	if (size >= offsetof(struct drm_panthor_timestamp_info, flags) + sizeof(arg->flags))
+		flags = arg->flags;
Quoted 8 lines

drivers/gpu/drm/panthor/panthor_drv.c static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx, · line 787

Quoted 4 lines
drivers/gpu/drm/panthor/panthor_drv.c +4 -0
+	else
+		flags = DRM_PANTHOR_TIMESTAMP_GPU |
+			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+			DRM_PANTHOR_TIMESTAMP_FREQ;

How about we add a DRM_PANTHOR_TIMESTAMP_ADVANCED_QUERY flag that tells the driver whether the default should be picked or not instead of this weird is-this-the-new-or-old-struct detection based on the size.

Well, as is, we would read uninitialized data from kernel stack if user passed old struct with the original size. It's fixable, but I'm not sure why you think checking size to detect the use of new interface is weird. I thought it's a pretty standard thing.

If the conclusion will be that checking size must be dropped, then I think looking at flags being non-zero would be enough - there's no need for new special flag that says other bits mean something.

Quoted 31 lines
	if (args->flags & DRM_PANTHOR_TIMESTAMP_ADVANCED_QUERY)
		flags = args->flags;
	else
		flags = DRM_PANTHOR_TIMESTAMP_GPU |
			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
			DRM_PANTHOR_TIMESTAMP_FREQ;
Quoted 24 lines
drivers/gpu/drm/panthor/panthor_drv.c +24 -0
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case 0:
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		timestamp_types++;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	if (flags & ~(DRM_PANTHOR_TIMESTAMP_GPU |
+		      DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK |
+		      DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+		      DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT |
+		      DRM_PANTHOR_TIMESTAMP_FREQ |
+		      DRM_PANTHOR_TIMESTAMP_DURATION))
+		return -EINVAL;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		timestamp_types++;
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		timestamp_types++;
Quoted 8 lines

drivers/gpu/drm/panthor/panthor_drv.c static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx, · line 815

Quoted 4 lines
drivers/gpu/drm/panthor/panthor_drv.c +4 -0
+
+	minimize_interruption =
+		(flags & DRM_PANTHOR_TIMESTAMP_DURATION) ||
+		(timestamp_types >= 2);

It's probably worth a comment explaining where the >= 2 comes from and why query duration measurement needs to be done with interrupts disabled (though the latter is a bit more obvious than the former).

ack

Quoted 41 lines
Quoted 41 lines
drivers/gpu/drm/panthor/panthor_drv.c +29 -4
 	ret = panthor_device_resume_and_get(ptdev);
 	if (ret)
 		return ret;
 
+	if (flags & DRM_PANTHOR_TIMESTAMP_FREQ) {
 #ifdef CONFIG_ARM_ARCH_TIMER
-	arg->timestamp_frequency = arch_timer_get_cntfrq();
+		arg->timestamp_frequency = arch_timer_get_cntfrq();
 #else
-	arg->timestamp_frequency = 0;
+		arg->timestamp_frequency = 0;
 #endif
-	arg->current_timestamp = gpu_read64_counter(ptdev, GPU_TIMESTAMP);
-	arg->timestamp_offset = gpu_read64(ptdev, GPU_TIMESTAMP_OFFSET);
+	} else {
+		arg->timestamp_frequency = 0;
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_OFFSET)
+		arg->timestamp_offset = gpu_read64(ptdev, GPU_TIMESTAMP_OFFSET);
+	else
+		arg->timestamp_offset = 0;
+
+	if (minimize_interruption) {
+		preempt_disable();
+		local_irq_save(irq_flags);
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_DURATION)
+		query_start_time = local_clock();
+	else
+		query_start_time = 0;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		arg->current_timestamp = gpu_read64_counter(ptdev, GPU_TIMESTAMP);
+	else
+		arg->current_timestamp = 0;
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
Quoted 8 lines

drivers/gpu/drm/panthor/panthor_drv.c static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx, · line 857

Quoted 5 lines
drivers/gpu/drm/panthor/panthor_drv.c +5 -0
+		ktime_get_ts64(&cpu_ts);
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		ktime_get_raw_ts64(&cpu_ts);
+		break;
	default:
		break;

ack

Quoted 32 lines
Quoted 32 lines
drivers/gpu/drm/panthor/panthor_drv.c +26 -0
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		arg->cycle_count = gpu_read64_counter(ptdev, GPU_CYCLE_COUNT);
+	else
+		arg->cycle_count = 0;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_DURATION)
+		arg->duration_nsec = local_clock() - query_start_time;
+	else
+		arg->duration_nsec = 0;
+
+	if (minimize_interruption) {
+		local_irq_restore(irq_flags);
+		preempt_enable();
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+		timens_add_monotonic(&cpu_ts);
+
+		arg->cpu_timestamp_sec = cpu_ts.tv_sec;
+		arg->cpu_timestamp_nsec = cpu_ts.tv_nsec;
+	} else {
+		arg->cpu_timestamp_sec = 0;
+		arg->cpu_timestamp_nsec = 0;
+	}
 
 	pm_runtime_put(ptdev->base.dev);
 	return 0;
@@ -851,7 +955,12 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d
 		return PANTHOR_UOBJ_SET(args->pointer, args->size, ptdev->csif_info);
 
Quoted 5 lines

drivers/gpu/drm/panthor/panthor_drv.c static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d · line 960

Quoted 5 lines
drivers/gpu/drm/panthor/panthor_drv.c +3 -1
 	case DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO:
-		ret = panthor_query_timestamp_info(ptdev, &timestamp_info);
+		if (copy_from_user(&timestamp_info,
+				   (const void __user __force *) args->pointer,
+				   args->size))

Umm, now I see that I forgot to explicitly validate args->size. It's done inside of copy_from_user, but it relies on __builtin_object_size doing the right thing. Will do that in v2.

Quoted 6 lines

drivers/gpu/drm/panthor/panthor_drv.c static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d · line 964

Quoted 4 lines
drivers/gpu/drm/panthor/panthor_drv.c +3 -0
+			return -EFAULT;
+
+		ret = panthor_query_timestamp_info(ptdev, &timestamp_info, args->size);
 

nit: drop the blank line.

ack

Quoted 39 lines
Quoted 26 lines
drivers/gpu/drm/panthor/panthor_drv.c +2 -1
 		if (ret)
 			return ret;
@@ -1680,6 +1789,7 @@ static void panthor_debugfs_init(struct drm_minor *minor)
  *       - adds DRM_IOCTL_PANTHOR_BO_SYNC ioctl
  *       - adds DRM_IOCTL_PANTHOR_BO_QUERY_INFO ioctl
  *       - adds drm_panthor_gpu_info::selected_coherency
+ * - 1.8 - extends DEV_QUERY_TIMESTAMP_INFO with flags
  */
 static const struct drm_driver panthor_drm_driver = {
 	.driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ |
@@ -1693,7 +1803,7 @@ static const struct drm_driver panthor_drm_driver = {
 	.name = "panthor",
 	.desc = "Panthor DRM driver",
 	.major = 1,
-	.minor = 7,
+	.minor = 8,
 
 	.gem_create_object = panthor_gem_create_object,
 	.gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table,
include/uapi/drm/panthor_drm.h +0 -0
index b401ac585d6a..c869e8b95ecd 100644
@@ -409,6 +409,35 @@ struct drm_panthor_csif_info {
 	__u32 pad;
 };

include/uapi/drm/panthor_drm.h struct drm_panthor_csif_info { · line 420

Quoted 10 lines
include/uapi/drm/panthor_drm.h +9 -0
 
+/**
+ * enum drm_panthor_timestamp_info_flags - drm_panthor_timestamp_info.flags
+ */
+enum drm_panthor_timestamp_info_flags {
+	/** @DRM_PANTHOR_TIMESTAMP_GPU: Query GPU time. */
+	DRM_PANTHOR_TIMESTAMP_GPU = 1 << 0,
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC: Query CPU time using CLOCK_MONOTONIC. */
+	DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC = 1 << 1,

Can we define

Quoted 3 lines

DRM_PANTHOR_TIMESTAMP_CPU_NONE = 0 << 1,

for completeness.

Yeah, good idea.

Quoted 60 lines
Quoted 9 lines
include/uapi/drm/panthor_drm.h +8 -0
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW: Query CPU time using CLOCK_MONOTONIC_RAW. */
+	DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW = 2 << 1,
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK: Space reserved for CPU clock type. */
+	DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK = 7 << 1,
+
+	/** @DRM_PANTHOR_TIMESTAMP_GPU_OFFSET: Query GPU offset. */

include/uapi/drm/panthor_drm.h struct drm_panthor_csif_info { · line 438

Quoted 10 lines
include/uapi/drm/panthor_drm.h +10 -0
+	DRM_PANTHOR_TIMESTAMP_GPU_OFFSET = 1 << 4,
+
+	/** @DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT: Query GPU cycle count. */
+	DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT = 1 << 5,
+
+	/** @DRM_PANTHOR_TIMESTAMP_FREQ: Query timestamp frequency. */
+	DRM_PANTHOR_TIMESTAMP_FREQ = 1 << 6,
+
+	/** @DRM_PANTHOR_TIMESTAMP_DURATION: Return duration of time query. */
+	DRM_PANTHOR_TIMESTAMP_DURATION = 1 << 7,

/**
* @DRM_PANTHOR_TIMESTAMP_ADVANCED_QUERY: Advanced query requested. *
* When missing, flags should be 0, which is equivalent to
* DRM_PANTHOR_TIMESTAMP_ADVANCED_QUERY |
* DRM_PANTHOR_TIMESTAMP_GPU |
* DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
* DRM_PANTHOR_TIMESTAMP_FREQ
*/
DRM_PANTHOR_TIMESTAMP_ADVANCED_QUERY = 1 << 30,

Quoted 29 lines
include/uapi/drm/panthor_drm.h +17 -2
+};
+
 /**
  * struct drm_panthor_timestamp_info - Timestamp information
  *
@@ -421,11 +450,29 @@ struct drm_panthor_timestamp_info {
 	 */
 	__u64 timestamp_frequency;
 
-	/** @current_timestamp: The current timestamp. */
+	/** @current_timestamp: The current GPU timestamp. */
 	__u64 current_timestamp;
 
-	/** @timestamp_offset: The offset of the timestamp timer. */
+	/** @timestamp_offset: The offset of the GPU timestamp timer. */
 	__u64 timestamp_offset;
+
+	/** @flags: Bitmask of drm_panthor_timestamp_info_flags. */
+	__u32 flags;
+
+	/** @duration_nsec: Duration of time query. */
+	__u32 duration_nsec;
+
+	/** @cycle_count: Value of GPU_CYCLE_COUNT. */
+	__u64 cycle_count;
+
+	/** @cpu_timestamp_sec: Seconds part of CPU timestamp. */
+	__u64 cpu_timestamp_sec;
+
Quoted 8 lines

include/uapi/drm/panthor_drm.h struct drm_panthor_timestamp_info { · line 475

Quoted 5 lines
include/uapi/drm/panthor_drm.h +5 -0
+	/** @cpu_timestamp_nsec: Nanseconds part of CPU timestamp. */
+	__u32 cpu_timestamp_nsec;
+
+	/** @pad1: Padding, MBZ. */
+	__u32 pad1;

Let's re-purpose the existing pad field into flags, move duration_nsec after cpu_timestamp_nsec, and get rid of this pad1.

I'm not sure I understand. Do you want me to extend flags to u64? What's the point of that?

Quoted 5 lines
Quoted 4 lines
include/uapi/drm/panthor_drm.h +0 -0
 };
 
 /**
maintainer
#3 lore raw
Quoted 69 lines · Marcin Ślusarz
Quoted 59 lines · Boris Brezillon
Quoted 56 lines · On Wed, 18 Mar 2026 12:29:52 +0100 Marcin Slusarz <[email protected]>…

Flags now control which data user space wants to query, there is more information sources, and there's ability to query duration of multiple timestamp reads.

New sources:
- CPU's monotonic,
- CPU's monotonic raw,
- GPU's cycle count

These changes should make the implementation of VK_KHR_calibrated_timestamps more accurate and much simpler.

---
This is counter proposal to https://lore.kernel.org/all/[email protected]/

---
 drivers/gpu/drm/panthor/panthor_drv.c | 124 ++++++++++++++++++++++++--
 include/uapi/drm/panthor_drm.h        |  51 ++++++++++-
 2 files changed, 166 insertions(+), 9 deletions(-)
drivers/gpu/drm/panthor/panthor_drv.c +17 -1
index 165dddfde6ca..19ede20a578e 100644
@@ -13,7 +13,9 @@
 #include <linux/pagemap.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
+#include <linux/sched/clock.h>
 #include <linux/time64.h>
+#include <linux/time_namespace.h>
 
 #include <drm/drm_auth.h>
 #include <drm/drm_debugfs.h>
@@ -762,21 +764,123 @@ static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx,
 }
 
 static int panthor_query_timestamp_info(struct panthor_device *ptdev,
-					struct drm_panthor_timestamp_info *arg)
+					struct drm_panthor_timestamp_info *arg,
+					u32 size)
 {
 	int ret;
+	u32 flags;
+	unsigned long irq_flags;
+	struct timespec64 cpu_ts;
+	u64 query_start_time;
+	bool minimize_interruption;
+	u32 timestamp_types = 0;
+
+	if (size >= offsetof(struct drm_panthor_timestamp_info, pad1) + sizeof(arg->pad1) &&
+			arg->pad1 != 0)
+		return -EINVAL;
+
+	if (size >= offsetof(struct drm_panthor_timestamp_info, flags) + sizeof(arg->flags))
+		flags = arg->flags;
Quoted 8 lines

drivers/gpu/drm/panthor/panthor_drv.c static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx, · line 787

Quoted 4 lines
drivers/gpu/drm/panthor/panthor_drv.c +4 -0
+	else
+		flags = DRM_PANTHOR_TIMESTAMP_GPU |
+			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+			DRM_PANTHOR_TIMESTAMP_FREQ;

How about we add a DRM_PANTHOR_TIMESTAMP_ADVANCED_QUERY flag that tells the driver whether the default should be picked or not instead of this weird is-this-the-new-or-old-struct detection based on the size.

Quoted 4 lines

Well, as is, we would read uninitialized data from kernel stack if user passed old struct with the original size. It's fixable, but I'm not sure why you think checking size to detect the use of new interface is weird. I thought it's a pretty standard thing.

What you need is copy_struct_from_user() - it will zero any fields that user space didn't provide. So adding a flags field to the end of the struct will be guaranteed to be zero with old (binary of) user space.

This is the standard way of extending an API. If user space is recompiled with new headers then user space will pass in the larger size (because it uses sizeof()), but will zero initialise any fields that it doesn't know about. If you look purely at the size passed by userspace then the sizeof() will be wrong and no flags will get set.

Quoted 3 lines

If the conclusion will be that checking size must be dropped, then I think looking at flags being non-zero would be enough - there's no need for new special flag that says other bits mean something.

That would be fine if we don't want the 'default' flags behaviour you have above. So either GPU/GPU_OFFSET/FREQ are unconditionally enabled or you need to reverse the meaning of those flags. Or of course go with Boris's suggestion of a flag to enable the new behaviour.

[...]

Quoted 36 lines
Quoted 27 lines
Quoted 27 lines
drivers/gpu/drm/panthor/panthor_drv.c +15 -2
 /**
  * struct drm_panthor_timestamp_info - Timestamp information
  *
@@ -421,11 +450,29 @@ struct drm_panthor_timestamp_info {
 	 */
 	__u64 timestamp_frequency;
 
-	/** @current_timestamp: The current timestamp. */
+	/** @current_timestamp: The current GPU timestamp. */
 	__u64 current_timestamp;
 
-	/** @timestamp_offset: The offset of the timestamp timer. */
+	/** @timestamp_offset: The offset of the GPU timestamp timer. */
 	__u64 timestamp_offset;
+
+	/** @flags: Bitmask of drm_panthor_timestamp_info_flags. */
+	__u32 flags;
+
+	/** @duration_nsec: Duration of time query. */
+	__u32 duration_nsec;
+
+	/** @cycle_count: Value of GPU_CYCLE_COUNT. */
+	__u64 cycle_count;
+
+	/** @cpu_timestamp_sec: Seconds part of CPU timestamp. */
+	__u64 cpu_timestamp_sec;
+
Quoted 8 lines

drivers/gpu/drm/panthor/panthor_drv.c struct drm_panthor_timestamp_info { · line 475

Quoted 5 lines
drivers/gpu/drm/panthor/panthor_drv.c +5 -0
+	/** @cpu_timestamp_nsec: Nanseconds part of CPU timestamp. */
+	__u32 cpu_timestamp_nsec;
+
+	/** @pad1: Padding, MBZ. */
+	__u32 pad1;

Let's re-purpose the existing pad field into flags, move duration_nsec after cpu_timestamp_nsec, and get rid of this pad1.

Quoted 2 lines

I'm not sure I understand. Do you want me to extend flags to u64? What's the point of that?

I'm not sure I necessarily understand Boris's comment either, but I would suggest making flags u64 would be better.

By shuffling things around to have a u64 flags you no longer have any padding fields. And the unused part of the flags will be naturally checked for being 0 rather than the explicit check for pad1 you currently have.

Not a big deal to me - but it's easier to just avoid padding fields where possible as they often get overlooked in the validation.

Thanks,
Steve

maintainer
#4 lore raw
Quoted 70 lines · On Wed, 18 Mar 2026 15:20:18 +0000 Steven Price <[email protected]> wrote:
Quoted 69 lines · Marcin Ślusarz
Quoted 67 lines · Boris Brezillon
Quoted 50 lines · On Wed, 18 Mar 2026 12:29:52 +0100 Marcin Slusarz <[email protected]>…

Flags now control which data user space wants to query, there is more information sources, and there's ability to query duration of multiple timestamp reads.

New sources:
- CPU's monotonic,
- CPU's monotonic raw,
- GPU's cycle count

These changes should make the implementation of VK_KHR_calibrated_timestamps more accurate and much simpler.

---
This is counter proposal to https://lore.kernel.org/all/[email protected]/

---
 drivers/gpu/drm/panthor/panthor_drv.c | 124 ++++++++++++++++++++++++--
 include/uapi/drm/panthor_drm.h        |  51 ++++++++++-
 2 files changed, 166 insertions(+), 9 deletions(-)
drivers/gpu/drm/panthor/panthor_drv.c +11 -1
index 165dddfde6ca..19ede20a578e 100644
@@ -13,7 +13,9 @@
 #include <linux/pagemap.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
+#include <linux/sched/clock.h>
 #include <linux/time64.h>
+#include <linux/time_namespace.h>
 
 #include <drm/drm_auth.h>
 #include <drm/drm_debugfs.h>
@@ -762,21 +764,123 @@ static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx,
 }
 
 static int panthor_query_timestamp_info(struct panthor_device *ptdev,
-					struct drm_panthor_timestamp_info *arg)
+					struct drm_panthor_timestamp_info *arg,
+					u32 size)
 {
 	int ret;
+	u32 flags;
+	unsigned long irq_flags;
+	struct timespec64 cpu_ts;
+	u64 query_start_time;
+	bool minimize_interruption;
+	u32 timestamp_types = 0;
+

drivers/gpu/drm/panthor/panthor_drv.c static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx, · line 787

Quoted 10 lines
drivers/gpu/drm/panthor/panthor_drv.c +10 -0
+	if (size >= offsetof(struct drm_panthor_timestamp_info, pad1) + sizeof(arg->pad1) &&
+			arg->pad1 != 0)
+		return -EINVAL;
+
+	if (size >= offsetof(struct drm_panthor_timestamp_info, flags) + sizeof(arg->flags))
+		flags = arg->flags;
+	else
+		flags = DRM_PANTHOR_TIMESTAMP_GPU |
+			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+			DRM_PANTHOR_TIMESTAMP_FREQ;  

How about we add a DRM_PANTHOR_TIMESTAMP_ADVANCED_QUERY flag that tells the driver whether the default should be picked or not instead of this weird is-this-the-new-or-old-struct detection based on the size.

Quoted 8 lines
Quoted 4 lines

Well, as is, we would read uninitialized data from kernel stack if user passed old struct with the original size. It's fixable, but I'm not sure why you think checking size to detect the use of new interface is weird. I thought it's a pretty standard thing.

What you need is copy_struct_from_user() - it will zero any fields that user space didn't provide. So adding a flags field to the end of the struct will be guaranteed to be zero with old (binary of) user space.

This ^.

Quoted 7 lines

This is the standard way of extending an API. If user space is recompiled with new headers then user space will pass in the larger size (because it uses sizeof()), but will zero initialise any fields that it doesn't know about. If you look purely at the size passed by userspace then the sizeof() will be wrong and no flags will get set.

Quoted 7 lines
Quoted 3 lines

If the conclusion will be that checking size must be dropped, then I think looking at flags being non-zero would be enough - there's no need for new special flag that says other bits mean something.

That would be fine if we don't want the 'default' flags behaviour you have above. So either GPU/GPU_OFFSET/FREQ are unconditionally enabled or you need to reverse the meaning of those flags.

Right, if you don't want the extra ADVANCED_QUERY flag, the GPU, GPU_OFFSET and FREQ flags need to be opt-out, but that's a bit confusing if the other flags are opt-in.

Quoted 41 lines

Or of course go with
Boris's suggestion of a flag to enable the new behaviour.

[...]

Quoted 36 lines
Quoted 35 lines
Quoted 22 lines
drivers/gpu/drm/panthor/panthor_drv.c +10 -2
 /**
  * struct drm_panthor_timestamp_info - Timestamp information
  *
@@ -421,11 +450,29 @@ struct drm_panthor_timestamp_info {
 	 */
 	__u64 timestamp_frequency;
 
-	/** @current_timestamp: The current timestamp. */
+	/** @current_timestamp: The current GPU timestamp. */
 	__u64 current_timestamp;
 
-	/** @timestamp_offset: The offset of the timestamp timer. */
+	/** @timestamp_offset: The offset of the GPU timestamp timer. */
 	__u64 timestamp_offset;
+
+	/** @flags: Bitmask of drm_panthor_timestamp_info_flags. */
+	__u32 flags;
+
+	/** @duration_nsec: Duration of time query. */
+	__u32 duration_nsec;
+
+	/** @cycle_count: Value of GPU_CYCLE_COUNT. */

drivers/gpu/drm/panthor/panthor_drv.c struct drm_panthor_timestamp_info { · line 475

Quoted 10 lines
drivers/gpu/drm/panthor/panthor_drv.c +10 -0
+	__u64 cycle_count;
+
+	/** @cpu_timestamp_sec: Seconds part of CPU timestamp. */
+	__u64 cpu_timestamp_sec;
+
+	/** @cpu_timestamp_nsec: Nanseconds part of CPU timestamp. */
+	__u32 cpu_timestamp_nsec;
+
+	/** @pad1: Padding, MBZ. */
+	__u32 pad1;  

Let's re-purpose the existing pad field into flags, move duration_nsec after cpu_timestamp_nsec, and get rid of this pad1.

Quoted 5 lines
Quoted 2 lines

I'm not sure I understand. Do you want me to extend flags to u64? What's the point of that?

I'm not sure I necessarily understand Boris's comment either, but I would suggest making flags u64 would be better.

I was confused by the fact the field was named pad1, and I assumed there was a pad field already present in the struct, which is why I suggested re-purposing that one instead of adding a new field that would in turn require extra padding. Given there's no pre-existing padding, I'd rename pad1 into pad and call it a day.

Quoted 8 lines

By shuffling things around to have a u64 flags you no longer have any padding fields. And the unused part of the flags will be naturally checked for being 0 rather than the explicit check for pad1 you currently have.

Not a big deal to me - but it's easier to just avoid padding fields where possible as they often get overlooked in the validation.

We certainly want to ensure they are, this way we can re-purpose existing padding fields instead of adding new ones when we need to extend the logic.

#5 lore raw
Quoted 78 lines · Boris Brezillon
Quoted 75 lines · On Wed, 18 Mar 2026 15:20:18 +0000 Steven Price <[email protected]> wrote:
Quoted 73 lines · Marcin Ślusarz
Quoted 59 lines · Boris Brezillon
Quoted 56 lines · On Wed, 18 Mar 2026 12:29:52 +0100 Marcin Slusarz <[email protected]>…

Flags now control which data user space wants to query, there is more information sources, and there's ability to query duration of multiple timestamp reads.

New sources:
- CPU's monotonic,
- CPU's monotonic raw,
- GPU's cycle count

These changes should make the implementation of VK_KHR_calibrated_timestamps more accurate and much simpler.

---
This is counter proposal to https://lore.kernel.org/all/[email protected]/

---
 drivers/gpu/drm/panthor/panthor_drv.c | 124 ++++++++++++++++++++++++--
 include/uapi/drm/panthor_drm.h        |  51 ++++++++++-
 2 files changed, 166 insertions(+), 9 deletions(-)
drivers/gpu/drm/panthor/panthor_drv.c +17 -1
index 165dddfde6ca..19ede20a578e 100644
@@ -13,7 +13,9 @@
 #include <linux/pagemap.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
+#include <linux/sched/clock.h>
 #include <linux/time64.h>
+#include <linux/time_namespace.h>
 
 #include <drm/drm_auth.h>
 #include <drm/drm_debugfs.h>
@@ -762,21 +764,123 @@ static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx,
 }
 
 static int panthor_query_timestamp_info(struct panthor_device *ptdev,
-					struct drm_panthor_timestamp_info *arg)
+					struct drm_panthor_timestamp_info *arg,
+					u32 size)
 {
 	int ret;
+	u32 flags;
+	unsigned long irq_flags;
+	struct timespec64 cpu_ts;
+	u64 query_start_time;
+	bool minimize_interruption;
+	u32 timestamp_types = 0;
+
+	if (size >= offsetof(struct drm_panthor_timestamp_info, pad1) + sizeof(arg->pad1) &&
+			arg->pad1 != 0)
+		return -EINVAL;
+
+	if (size >= offsetof(struct drm_panthor_timestamp_info, flags) + sizeof(arg->flags))
+		flags = arg->flags;
Quoted 8 lines

drivers/gpu/drm/panthor/panthor_drv.c static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx, · line 787

Quoted 4 lines
drivers/gpu/drm/panthor/panthor_drv.c +4 -0
+	else
+		flags = DRM_PANTHOR_TIMESTAMP_GPU |
+			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+			DRM_PANTHOR_TIMESTAMP_FREQ;  

How about we add a DRM_PANTHOR_TIMESTAMP_ADVANCED_QUERY flag that tells the driver whether the default should be picked or not instead of this weird is-this-the-new-or-old-struct detection based on the size.

Well, as is, we would read uninitialized data from kernel stack if user passed old struct with the original size. It's fixable, but I'm not sure why you think checking size to detect the use of new interface is weird. I thought it's a pretty standard thing.

Quoted 5 lines
Quoted 3 lines

What you need is copy_struct_from_user() - it will zero any fields that user space didn't provide. So adding a flags field to the end of the struct will be guaranteed to be zero with old (binary of) user space.

This ^.

Ok, I'm convinced. Will do that in the next version.

Quoted 11 lines
Quoted 11 lines

This is the standard way of extending an API. If user space is recompiled with new headers then user space will pass in the larger size (because it uses sizeof()), but will zero initialise any fields that it doesn't know about. If you look purely at the size passed by userspace then the sizeof() will be wrong and no flags will get set.

Quoted 3 lines

If the conclusion will be that checking size must be dropped, then I think looking at flags being non-zero would be enough - there's no need for new special flag that says other bits mean something.

Quoted 7 lines
Quoted 3 lines

That would be fine if we don't want the 'default' flags behaviour you have above. So either GPU/GPU_OFFSET/FREQ are unconditionally enabled or you need to reverse the meaning of those flags.

Right, if you don't want the extra ADVANCED_QUERY flag, the GPU, GPU_OFFSET and FREQ flags need to be opt-out, but that's a bit confusing if the other flags are opt-in.

Flags == 0 doesn't make any sense, so we can translate 0 to the combination of flags that matches previous behavior.

Quoted 44 lines
Quoted 44 lines

Or of course go with
Boris's suggestion of a flag to enable the new behaviour.

[...]

Quoted 38 lines
Quoted 27 lines
Quoted 27 lines
drivers/gpu/drm/panthor/panthor_drv.c +15 -2
 /**
  * struct drm_panthor_timestamp_info - Timestamp information
  *
@@ -421,11 +450,29 @@ struct drm_panthor_timestamp_info {
 	 */
 	__u64 timestamp_frequency;
 
-	/** @current_timestamp: The current timestamp. */
+	/** @current_timestamp: The current GPU timestamp. */
 	__u64 current_timestamp;
 
-	/** @timestamp_offset: The offset of the timestamp timer. */
+	/** @timestamp_offset: The offset of the GPU timestamp timer. */
 	__u64 timestamp_offset;
+
+	/** @flags: Bitmask of drm_panthor_timestamp_info_flags. */
+	__u32 flags;
+
+	/** @duration_nsec: Duration of time query. */
+	__u32 duration_nsec;
+
+	/** @cycle_count: Value of GPU_CYCLE_COUNT. */
+	__u64 cycle_count;
+
+	/** @cpu_timestamp_sec: Seconds part of CPU timestamp. */
+	__u64 cpu_timestamp_sec;
+
Quoted 8 lines

drivers/gpu/drm/panthor/panthor_drv.c struct drm_panthor_timestamp_info { · line 475

Quoted 5 lines
drivers/gpu/drm/panthor/panthor_drv.c +5 -0
+	/** @cpu_timestamp_nsec: Nanseconds part of CPU timestamp. */
+	__u32 cpu_timestamp_nsec;
+
+	/** @pad1: Padding, MBZ. */
+	__u32 pad1;  

Let's re-purpose the existing pad field into flags, move duration_nsec after cpu_timestamp_nsec, and get rid of this pad1.

I'm not sure I understand. Do you want me to extend flags to u64? What's the point of that?

Quoted 8 lines
Quoted 2 lines

I'm not sure I necessarily understand Boris's comment either, but I would suggest making flags u64 would be better.

I was confused by the fact the field was named pad1, and I assumed there was a pad field already present in the struct, which is why I suggested re-purposing that one instead of adding a new field that would in turn require extra padding. Given there's no pre-existing padding, I'd rename pad1 into pad and call it a day.

I named it that way to make sure that future padding fields are named consistently.

Quoted 11 lines
Quoted 7 lines

By shuffling things around to have a u64 flags you no longer have any padding fields. And the unused part of the flags will be naturally checked for being 0 rather than the explicit check for pad1 you currently have.

Not a big deal to me - but it's easier to just avoid padding fields where possible as they often get overlooked in the validation.

We certainly want to ensure they are, this way we can re-purpose existing padding fields instead of adding new ones when we need to extend the logic.

I don't know why, but https://docs.kernel.org/process/botching-up-ioctls.html
suggests that both seconds and nanoseconds should be 64-bit, so maybe we could extend cpu_timestamp_nsec and forget about this?

maintainer
#6 lore raw
Quoted 102 lines · On Wed, 18 Mar 2026 17:27:26 +0100 Marcin Ślusarz <[email protected]>…
Quoted 78 lines · Boris Brezillon
Quoted 75 lines · On Wed, 18 Mar 2026 15:20:18 +0000 Steven Price <[email protected]> wrote:
Quoted 73 lines · Marcin Ślusarz
Quoted 59 lines · Boris Brezillon
Quoted 56 lines · On Wed, 18 Mar 2026 12:29:52 +0100 Marcin Slusarz <[email protected]>…

Flags now control which data user space wants to query, there is more information sources, and there's ability to query duration of multiple timestamp reads.

New sources:
- CPU's monotonic,
- CPU's monotonic raw,
- GPU's cycle count

These changes should make the implementation of VK_KHR_calibrated_timestamps more accurate and much simpler.

---
This is counter proposal to https://lore.kernel.org/all/[email protected]/

---
 drivers/gpu/drm/panthor/panthor_drv.c | 124 ++++++++++++++++++++++++--
 include/uapi/drm/panthor_drm.h        |  51 ++++++++++-
 2 files changed, 166 insertions(+), 9 deletions(-)
drivers/gpu/drm/panthor/panthor_drv.c +17 -1
index 165dddfde6ca..19ede20a578e 100644
@@ -13,7 +13,9 @@
 #include <linux/pagemap.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
+#include <linux/sched/clock.h>
 #include <linux/time64.h>
+#include <linux/time_namespace.h>
 
 #include <drm/drm_auth.h>
 #include <drm/drm_debugfs.h>
@@ -762,21 +764,123 @@ static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx,
 }
 
 static int panthor_query_timestamp_info(struct panthor_device *ptdev,
-					struct drm_panthor_timestamp_info *arg)
+					struct drm_panthor_timestamp_info *arg,
+					u32 size)
 {
 	int ret;
+	u32 flags;
+	unsigned long irq_flags;
+	struct timespec64 cpu_ts;
+	u64 query_start_time;
+	bool minimize_interruption;
+	u32 timestamp_types = 0;
+
+	if (size >= offsetof(struct drm_panthor_timestamp_info, pad1) + sizeof(arg->pad1) &&
+			arg->pad1 != 0)
+		return -EINVAL;
+
+	if (size >= offsetof(struct drm_panthor_timestamp_info, flags) + sizeof(arg->flags))
+		flags = arg->flags;
Quoted 8 lines

drivers/gpu/drm/panthor/panthor_drv.c static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx, · line 787

Quoted 4 lines
drivers/gpu/drm/panthor/panthor_drv.c +4 -0
+	else
+		flags = DRM_PANTHOR_TIMESTAMP_GPU |
+			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+			DRM_PANTHOR_TIMESTAMP_FREQ;    

How about we add a DRM_PANTHOR_TIMESTAMP_ADVANCED_QUERY flag that tells the driver whether the default should be picked or not instead of this weird is-this-the-new-or-old-struct detection based on the size.

Well, as is, we would read uninitialized data from kernel stack if user passed old struct with the original size. It's fixable, but I'm not sure why you think checking size to detect the use of new interface is weird. I thought it's a pretty standard thing.

Quoted 5 lines
Quoted 3 lines

What you need is copy_struct_from_user() - it will zero any fields that user space didn't provide. So adding a flags field to the end of the struct will be guaranteed to be zero with old (binary of) user space.

This ^.

Ok, I'm convinced. Will do that in the next version.

Quoted 15 lines
Quoted 14 lines

This is the standard way of extending an API. If user space is recompiled with new headers then user space will pass in the larger size (because it uses sizeof()), but will zero initialise any fields that it doesn't know about. If you look purely at the size passed by userspace then the sizeof() will be wrong and no flags will get set.

Quoted 3 lines

If the conclusion will be that checking size must be dropped, then I think looking at flags being non-zero would be enough - there's no need for new special flag that says other bits mean something.

That would be fine if we don't want the 'default' flags behaviour you have above. So either GPU/GPU_OFFSET/FREQ are unconditionally enabled or you need to reverse the meaning of those flags.

Quoted 6 lines
Quoted 3 lines

Right, if you don't want the extra ADVANCED_QUERY flag, the GPU, GPU_OFFSET and FREQ flags need to be opt-out, but that's a bit confusing if the other flags are opt-in.

Flags == 0 doesn't make any sense, so we can translate 0 to the combination of flags that matches previous behavior.

Works too. If zero is not a valid combination, it can be used to encode the previous default behavior. I'm fine with that as long as it's documented in the uAPI doc.

Quoted 72 lines
Quoted 45 lines
Quoted 45 lines

Or of course go with
Boris's suggestion of a flag to enable the new behaviour.

[...]

Quoted 38 lines
Quoted 27 lines
Quoted 27 lines
drivers/gpu/drm/panthor/panthor_drv.c +15 -2
 /**
  * struct drm_panthor_timestamp_info - Timestamp information
  *
@@ -421,11 +450,29 @@ struct drm_panthor_timestamp_info {
 	 */
 	__u64 timestamp_frequency;
 
-	/** @current_timestamp: The current timestamp. */
+	/** @current_timestamp: The current GPU timestamp. */
 	__u64 current_timestamp;
 
-	/** @timestamp_offset: The offset of the timestamp timer. */
+	/** @timestamp_offset: The offset of the GPU timestamp timer. */
 	__u64 timestamp_offset;
+
+	/** @flags: Bitmask of drm_panthor_timestamp_info_flags. */
+	__u32 flags;
+
+	/** @duration_nsec: Duration of time query. */
+	__u32 duration_nsec;
+
+	/** @cycle_count: Value of GPU_CYCLE_COUNT. */
+	__u64 cycle_count;
+
+	/** @cpu_timestamp_sec: Seconds part of CPU timestamp. */
+	__u64 cpu_timestamp_sec;
+
Quoted 8 lines

drivers/gpu/drm/panthor/panthor_drv.c struct drm_panthor_timestamp_info { · line 475

Quoted 5 lines
drivers/gpu/drm/panthor/panthor_drv.c +5 -0
+	/** @cpu_timestamp_nsec: Nanseconds part of CPU timestamp. */
+	__u32 cpu_timestamp_nsec;
+
+	/** @pad1: Padding, MBZ. */
+	__u32 pad1;    

Let's re-purpose the existing pad field into flags, move duration_nsec after cpu_timestamp_nsec, and get rid of this pad1.

I'm not sure I understand. Do you want me to extend flags to u64? What's the point of that?

Quoted 8 lines
Quoted 2 lines

I'm not sure I necessarily understand Boris's comment either, but I would suggest making flags u64 would be better.

I was confused by the fact the field was named pad1, and I assumed there was a pad field already present in the struct, which is why I suggested re-purposing that one instead of adding a new field that would in turn require extra padding. Given there's no pre-existing padding, I'd rename pad1 into pad and call it a day.

I named it that way to make sure that future padding fields are named consistently.

Quoted 11 lines
Quoted 7 lines

By shuffling things around to have a u64 flags you no longer have any padding fields. And the unused part of the flags will be naturally checked for being 0 rather than the explicit check for pad1 you currently have.

Not a big deal to me - but it's easier to just avoid padding fields where possible as they often get overlooked in the validation.

We certainly want to ensure they are, this way we can re-purpose existing padding fields instead of adding new ones when we need to extend the logic.

I don't know why, but https://docs.kernel.org/process/botching-up-ioctls.html
suggests that both seconds and nanoseconds should be 64-bit, so maybe we could extend cpu_timestamp_nsec and forget about this?

maintainer
#7 lore raw
In reply to Steven Price
Quoted 75 lines · On Wed, 18 Mar 2026 15:20:18 +0000 Steven Price <[email protected]> wrote:
Quoted 69 lines · Marcin Ślusarz
Quoted 59 lines · Boris Brezillon
Quoted 56 lines · On Wed, 18 Mar 2026 12:29:52 +0100 Marcin Slusarz <[email protected]>…

Flags now control which data user space wants to query, there is more information sources, and there's ability to query duration of multiple timestamp reads.

New sources:
- CPU's monotonic,
- CPU's monotonic raw,
- GPU's cycle count

These changes should make the implementation of VK_KHR_calibrated_timestamps more accurate and much simpler.

---
This is counter proposal to https://lore.kernel.org/all/[email protected]/

---
 drivers/gpu/drm/panthor/panthor_drv.c | 124 ++++++++++++++++++++++++--
 include/uapi/drm/panthor_drm.h        |  51 ++++++++++-
 2 files changed, 166 insertions(+), 9 deletions(-)
drivers/gpu/drm/panthor/panthor_drv.c +17 -1
index 165dddfde6ca..19ede20a578e 100644
@@ -13,7 +13,9 @@
 #include <linux/pagemap.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
+#include <linux/sched/clock.h>
 #include <linux/time64.h>
+#include <linux/time_namespace.h>
 
 #include <drm/drm_auth.h>
 #include <drm/drm_debugfs.h>
@@ -762,21 +764,123 @@ static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx,
 }
 
 static int panthor_query_timestamp_info(struct panthor_device *ptdev,
-					struct drm_panthor_timestamp_info *arg)
+					struct drm_panthor_timestamp_info *arg,
+					u32 size)
 {
 	int ret;
+	u32 flags;
+	unsigned long irq_flags;
+	struct timespec64 cpu_ts;
+	u64 query_start_time;
+	bool minimize_interruption;
+	u32 timestamp_types = 0;
+
+	if (size >= offsetof(struct drm_panthor_timestamp_info, pad1) + sizeof(arg->pad1) &&
+			arg->pad1 != 0)
+		return -EINVAL;
+
+	if (size >= offsetof(struct drm_panthor_timestamp_info, flags) + sizeof(arg->flags))
+		flags = arg->flags;
Quoted 8 lines

drivers/gpu/drm/panthor/panthor_drv.c static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx, · line 787

Quoted 4 lines
drivers/gpu/drm/panthor/panthor_drv.c +4 -0
+	else
+		flags = DRM_PANTHOR_TIMESTAMP_GPU |
+			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+			DRM_PANTHOR_TIMESTAMP_FREQ;  

How about we add a DRM_PANTHOR_TIMESTAMP_ADVANCED_QUERY flag that tells the driver whether the default should be picked or not instead of this weird is-this-the-new-or-old-struct detection based on the size.

Quoted 4 lines

Well, as is, we would read uninitialized data from kernel stack if user passed old struct with the original size. It's fixable, but I'm not sure why you think checking size to detect the use of new interface is weird. I thought it's a pretty standard thing.

Quoted 4 lines

What you need is copy_struct_from_user() - it will zero any fields that user space didn't provide.
So adding a flags field to the end of the
struct will be guaranteed to be zero with old (binary of) user space.

Okay, so the problem seems to be that we've always treated DEV_QUERY args as write-only, and we don't have this copy_struct_from_user() trick on the args yet. Given the opt-out/in discussion, and the fact we want something that's read-write (reads flags, outputs timestamps and other props), I'd be tempted to make that a separate DEV_QUERY (DRM_PANTHOR_DEV_QUERY_ADVANCED_TIMESTAMP_INFO?).

#8 lore raw

PATCH v2 1/1 drm/panthor: extend timestamp query with flags

In reply to Marcin Slusarz

Flags now control which data user space wants to query, there is more information sources, and there's ability to query duration of multiple timestamp reads.

New sources:
- CPU's monotonic,
- CPU's monotonic raw,
- GPU's cycle count

These changes should make the implementation of VK_KHR_calibrated_timestamps more accurate and much simpler.

---
Changes in v2:
- added DRM_PANTHOR_TIMESTAMP_CPU_NONE
- cpu_timestamp_nsec extended to u64
- pad1 removed
- copy_from_user -> copy_struct_from_user
- looking at size replaced by looking at flags
- more comments

---
 drivers/gpu/drm/panthor/panthor_drv.c | 125 ++++++++++++++++++++++++--
 include/uapi/drm/panthor_drm.h        |  58 +++++++++++-
 2 files changed, 175 insertions(+), 8 deletions(-)
drivers/gpu/drm/panthor/panthor_drv.c +119 -6
index 165dddfde6ca..b6a100ce389d 100644
@@ -13,7 +13,9 @@
 #include <linux/pagemap.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
+#include <linux/sched/clock.h>
 #include <linux/time64.h>
+#include <linux/time_namespace.h>
 
 #include <drm/drm_auth.h>
 #include <drm/drm_debugfs.h>
@@ -765,18 +767,123 @@ static int panthor_query_timestamp_info(struct panthor_device *ptdev,
 					struct drm_panthor_timestamp_info *arg)
 {
 	int ret;
+	u32 flags;
+	unsigned long irq_flags;
+	struct timespec64 cpu_ts;
+	u64 query_start_time;
+	bool minimize_interruption;
+	u32 timestamp_types = 0;
+
+	if (arg->flags != 0)
+		flags = arg->flags;
+	else
+		flags = DRM_PANTHOR_TIMESTAMP_GPU |
+			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+			DRM_PANTHOR_TIMESTAMP_FREQ;
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case 0:
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		timestamp_types++;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	if (flags & ~(DRM_PANTHOR_TIMESTAMP_GPU |
+		      DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK |
+		      DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+		      DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT |
+		      DRM_PANTHOR_TIMESTAMP_FREQ |
+		      DRM_PANTHOR_TIMESTAMP_DURATION))
+		return -EINVAL;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		timestamp_types++;
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		timestamp_types++;
+
+	/* If user asked to obtain timestamps from more than one source,
+	 * then it very likely means they want them to be as close as possible.
+	 * If they asked for duration, then that likely means that they
+	 * want to know how long obtaining timestamp takes, without random
+	 * events, like process scheduling or interrupts.
+	 */
+	minimize_interruption =
+		(flags & DRM_PANTHOR_TIMESTAMP_DURATION) ||
+		(timestamp_types >= 2);
 
 	ret = panthor_device_resume_and_get(ptdev);
 	if (ret)
 		return ret;
 
+	if (flags & DRM_PANTHOR_TIMESTAMP_FREQ) {
 #ifdef CONFIG_ARM_ARCH_TIMER
-	arg->timestamp_frequency = arch_timer_get_cntfrq();
+		arg->timestamp_frequency = arch_timer_get_cntfrq();
 #else
-	arg->timestamp_frequency = 0;
+		arg->timestamp_frequency = 0;
 #endif
-	arg->current_timestamp = gpu_read64_counter(ptdev, GPU_TIMESTAMP);
-	arg->timestamp_offset = gpu_read64(ptdev, GPU_TIMESTAMP_OFFSET);
+	} else {
+		arg->timestamp_frequency = 0;
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_OFFSET)
+		arg->timestamp_offset = gpu_read64(ptdev, GPU_TIMESTAMP_OFFSET);
+	else
+		arg->timestamp_offset = 0;
+
+	if (minimize_interruption) {
+		preempt_disable();
+		local_irq_save(irq_flags);
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_DURATION)
+		query_start_time = local_clock();
+	else
+		query_start_time = 0;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		arg->current_timestamp = gpu_read64_counter(ptdev, GPU_TIMESTAMP);
+	else
+		arg->current_timestamp = 0;
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+		ktime_get_ts64(&cpu_ts);
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		ktime_get_raw_ts64(&cpu_ts);
+		break;
+	default:
+		break;
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		arg->cycle_count = gpu_read64_counter(ptdev, GPU_CYCLE_COUNT);
+	else
+		arg->cycle_count = 0;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_DURATION)
+		arg->duration_nsec = local_clock() - query_start_time;
+	else
+		arg->duration_nsec = 0;
+
+	if (minimize_interruption) {
+		local_irq_restore(irq_flags);
+		preempt_enable();
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+		timens_add_monotonic(&cpu_ts);
+
+		arg->cpu_timestamp_sec = cpu_ts.tv_sec;
+		arg->cpu_timestamp_nsec = cpu_ts.tv_nsec;
+	} else {
+		arg->cpu_timestamp_sec = 0;
+		arg->cpu_timestamp_nsec = 0;
+	}
 
 	pm_runtime_put(ptdev->base.dev);
 	return 0;
@@ -851,8 +958,13 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d
 		return PANTHOR_UOBJ_SET(args->pointer, args->size, ptdev->csif_info);
 
 	case DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO:
-		ret = panthor_query_timestamp_info(ptdev, &timestamp_info);
+		ret = copy_struct_from_user(&timestamp_info, sizeof(timestamp_info),
+					    (const void __user __force *) args->pointer,
+					    args->size);
+		if (ret)
+			return ret;
 
+		ret = panthor_query_timestamp_info(ptdev, &timestamp_info);
 		if (ret)
 			return ret;
 
@@ -1680,6 +1792,7 @@ static void panthor_debugfs_init(struct drm_minor *minor)
  *       - adds DRM_IOCTL_PANTHOR_BO_SYNC ioctl
  *       - adds DRM_IOCTL_PANTHOR_BO_QUERY_INFO ioctl
  *       - adds drm_panthor_gpu_info::selected_coherency
+ * - 1.8 - extends DEV_QUERY_TIMESTAMP_INFO with flags
  */
 static const struct drm_driver panthor_drm_driver = {
 	.driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ |
@@ -1693,7 +1806,7 @@ static const struct drm_driver panthor_drm_driver = {
 	.name = "panthor",
 	.desc = "Panthor DRM driver",
 	.major = 1,
-	.minor = 7,
+	.minor = 8,
 
 	.gem_create_object = panthor_gem_create_object,
 	.gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table,
include/uapi/drm/panthor_drm.h +56 -2

73 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer Reviewed-by on v2 1/1
#9 lore raw
Quoted 13 lines · On Thu, 19 Mar 2026 09:25:51 +0100 Marcin Slusarz <[email protected]>…

Flags now control which data user space wants to query, there is more information sources, and there's ability to query duration of multiple timestamp reads.

New sources:
- CPU's monotonic,
- CPU's monotonic raw,
- GPU's cycle count

These changes should make the implementation of VK_KHR_calibrated_timestamps more accurate and much simpler.

Quoted 8 lines

---
Changes in v2:
- added DRM_PANTHOR_TIMESTAMP_CPU_NONE
- cpu_timestamp_nsec extended to u64
- pad1 removed
- copy_from_user -> copy_struct_from_user
- looking at size replaced by looking at flags
- more comments

Looks good to me, just a few minor things, but once addressed, this is

Quoted 26 lines
---
 drivers/gpu/drm/panthor/panthor_drv.c | 125 ++++++++++++++++++++++++--
 include/uapi/drm/panthor_drm.h        |  58 +++++++++++-
 2 files changed, 175 insertions(+), 8 deletions(-)
drivers/gpu/drm/panthor/panthor_drv.c +5 -0
index 165dddfde6ca..b6a100ce389d 100644
@@ -13,7 +13,9 @@
 #include <linux/pagemap.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
+#include <linux/sched/clock.h>
 #include <linux/time64.h>
+#include <linux/time_namespace.h>
 
 #include <drm/drm_auth.h>
 #include <drm/drm_debugfs.h>
@@ -765,18 +767,123 @@ static int panthor_query_timestamp_info(struct panthor_device *ptdev,
 					struct drm_panthor_timestamp_info *arg)
 {
 	int ret;
+	u32 flags;
+	unsigned long irq_flags;
+	struct timespec64 cpu_ts;

drivers/gpu/drm/panthor/panthor_drv.c static int panthor_query_timestamp_info(struct panthor_device *ptdev, · line 782

Quoted 10 lines
drivers/gpu/drm/panthor/panthor_drv.c +10 -0
+	u64 query_start_time;
+	bool minimize_interruption;
+	u32 timestamp_types = 0;
+
+	if (arg->flags != 0)
+		flags = arg->flags;
+	else
+		flags = DRM_PANTHOR_TIMESTAMP_GPU |
+			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+			DRM_PANTHOR_TIMESTAMP_FREQ;

nit: I like to add explicit {} for multi-line statements under conditional blocks.

	if (arg->flags != 0) {
		flags = arg->flags;
	} else {
		flags = DRM_PANTHOR_TIMESTAMP_GPU |
			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
			DRM_PANTHOR_TIMESTAMP_FREQ;
	}

and maybe add a comment to repeat what the uAPI doc says.

Quoted 8 lines
drivers/gpu/drm/panthor/panthor_drv.c +8 -0
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case 0:
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		timestamp_types++;
+		break;

drivers/gpu/drm/panthor/panthor_drv.c static int panthor_query_timestamp_info(struct panthor_device *ptdev, · line 800

Quoted 10 lines
drivers/gpu/drm/panthor/panthor_drv.c +10 -0
+	default:
+		return -EINVAL;
+	}
+
+	if (flags & ~(DRM_PANTHOR_TIMESTAMP_GPU |
+		      DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK |
+		      DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+		      DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT |
+		      DRM_PANTHOR_TIMESTAMP_FREQ |
+		      DRM_PANTHOR_TIMESTAMP_DURATION))

Let's define a VALID_TIMESTAMP_QUERY_FLAGS like we do for other ioctls.

Quoted 94 lines
drivers/gpu/drm/panthor/panthor_drv.c +78 -4
+		return -EINVAL;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		timestamp_types++;
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		timestamp_types++;
+
+	/* If user asked to obtain timestamps from more than one source,
+	 * then it very likely means they want them to be as close as possible.
+	 * If they asked for duration, then that likely means that they
+	 * want to know how long obtaining timestamp takes, without random
+	 * events, like process scheduling or interrupts.
+	 */
+	minimize_interruption =
+		(flags & DRM_PANTHOR_TIMESTAMP_DURATION) ||
+		(timestamp_types >= 2);
 
 	ret = panthor_device_resume_and_get(ptdev);
 	if (ret)
 		return ret;
 
+	if (flags & DRM_PANTHOR_TIMESTAMP_FREQ) {
 #ifdef CONFIG_ARM_ARCH_TIMER
-	arg->timestamp_frequency = arch_timer_get_cntfrq();
+		arg->timestamp_frequency = arch_timer_get_cntfrq();
 #else
-	arg->timestamp_frequency = 0;
+		arg->timestamp_frequency = 0;
 #endif
-	arg->current_timestamp = gpu_read64_counter(ptdev, GPU_TIMESTAMP);
-	arg->timestamp_offset = gpu_read64(ptdev, GPU_TIMESTAMP_OFFSET);
+	} else {
+		arg->timestamp_frequency = 0;
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_OFFSET)
+		arg->timestamp_offset = gpu_read64(ptdev, GPU_TIMESTAMP_OFFSET);
+	else
+		arg->timestamp_offset = 0;
+
+	if (minimize_interruption) {
+		preempt_disable();
+		local_irq_save(irq_flags);
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_DURATION)
+		query_start_time = local_clock();
+	else
+		query_start_time = 0;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		arg->current_timestamp = gpu_read64_counter(ptdev, GPU_TIMESTAMP);
+	else
+		arg->current_timestamp = 0;
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+		ktime_get_ts64(&cpu_ts);
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		ktime_get_raw_ts64(&cpu_ts);
+		break;
+	default:
+		break;
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		arg->cycle_count = gpu_read64_counter(ptdev, GPU_CYCLE_COUNT);
+	else
+		arg->cycle_count = 0;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_DURATION)
+		arg->duration_nsec = local_clock() - query_start_time;
+	else
+		arg->duration_nsec = 0;
+
+	if (minimize_interruption) {
+		local_irq_restore(irq_flags);
+		preempt_enable();
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+		timens_add_monotonic(&cpu_ts);
+
+		arg->cpu_timestamp_sec = cpu_ts.tv_sec;
+		arg->cpu_timestamp_nsec = cpu_ts.tv_nsec;
+	} else {
+		arg->cpu_timestamp_sec = 0;
+		arg->cpu_timestamp_nsec = 0;
+	}
 
 	pm_runtime_put(ptdev->base.dev);
 	return 0;

drivers/gpu/drm/panthor/panthor_drv.c static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d · line 962

Quoted 6 lines
drivers/gpu/drm/panthor/panthor_drv.c +2 -1
@@ -851,8 +958,13 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d
 		return PANTHOR_UOBJ_SET(args->pointer, args->size, ptdev->csif_info);
 
 	case DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO:
-		ret = panthor_query_timestamp_info(ptdev, &timestamp_info);
+		ret = copy_struct_from_user(&timestamp_info, sizeof(timestamp_info),
+					    (const void __user __force *) args->pointer,
					    u64_to_user_ptr(args->pointer),

drivers/gpu/drm/panthor/panthor_drv.c static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d · line 963

Quoted 1 line
drivers/gpu/drm/panthor/panthor_drv.c +1 -0
+					    args->size);

I wonder if we should have a PANTHOR_UOBJ_GET() that checks the input size against the minimum struct size ever exposed by the driver. Here we are covered by the following PANTHOR_UOBJ_SET() so that's probably not a huge issue, dunno.

Quoted 103 lines
drivers/gpu/drm/panthor/panthor_drv.c +5 -1
+		if (ret)
+			return ret;
 
+		ret = panthor_query_timestamp_info(ptdev, &timestamp_info);
 		if (ret)
 			return ret;
 
@@ -1680,6 +1792,7 @@ static void panthor_debugfs_init(struct drm_minor *minor)
  *       - adds DRM_IOCTL_PANTHOR_BO_SYNC ioctl
  *       - adds DRM_IOCTL_PANTHOR_BO_QUERY_INFO ioctl
  *       - adds drm_panthor_gpu_info::selected_coherency
+ * - 1.8 - extends DEV_QUERY_TIMESTAMP_INFO with flags
  */
 static const struct drm_driver panthor_drm_driver = {
 	.driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ |
@@ -1693,7 +1806,7 @@ static const struct drm_driver panthor_drm_driver = {
 	.name = "panthor",
 	.desc = "Panthor DRM driver",
 	.major = 1,
-	.minor = 7,
+	.minor = 8,
 
 	.gem_create_object = panthor_gem_create_object,
 	.gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table,
include/uapi/drm/panthor_drm.h +56 -2
index b401ac585d6a..8a46ef040c3d 100644
@@ -409,6 +409,38 @@ struct drm_panthor_csif_info {
 	__u32 pad;
 };
 
+/**
+ * enum drm_panthor_timestamp_info_flags - drm_panthor_timestamp_info.flags
+ */
+enum drm_panthor_timestamp_info_flags {
+	/** @DRM_PANTHOR_TIMESTAMP_GPU: Query GPU time. */
+	DRM_PANTHOR_TIMESTAMP_GPU = 1 << 0,
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_NONE: Don't query CPU time. */
+	DRM_PANTHOR_TIMESTAMP_CPU_NONE = 0 << 1,
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC: Query CPU time using CLOCK_MONOTONIC. */
+	DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC = 1 << 1,
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW: Query CPU time using CLOCK_MONOTONIC_RAW. */
+	DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW = 2 << 1,
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK: Space reserved for CPU clock type. */
+	DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK = 7 << 1,
+
+	/** @DRM_PANTHOR_TIMESTAMP_GPU_OFFSET: Query GPU offset. */
+	DRM_PANTHOR_TIMESTAMP_GPU_OFFSET = 1 << 4,
+
+	/** @DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT: Query GPU cycle count. */
+	DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT = 1 << 5,
+
+	/** @DRM_PANTHOR_TIMESTAMP_FREQ: Query timestamp frequency. */
+	DRM_PANTHOR_TIMESTAMP_FREQ = 1 << 6,
+
+	/** @DRM_PANTHOR_TIMESTAMP_DURATION: Return duration of time query. */
+	DRM_PANTHOR_TIMESTAMP_DURATION = 1 << 7,
+};
+
 /**
  * struct drm_panthor_timestamp_info - Timestamp information
  *
@@ -421,11 +453,33 @@ struct drm_panthor_timestamp_info {
 	 */
 	__u64 timestamp_frequency;
 
-	/** @current_timestamp: The current timestamp. */
+	/** @current_timestamp: The current GPU timestamp. */
 	__u64 current_timestamp;
 
-	/** @timestamp_offset: The offset of the timestamp timer. */
+	/** @timestamp_offset: The offset of the GPU timestamp timer. */
 	__u64 timestamp_offset;
+
+	/**
+	 * @flags: Bitmask of drm_panthor_timestamp_info_flags.
+	 *
+	 * If set to 0, then it is interpreted as:
+	 *  DRM_PANTHOR_TIMESTAMP_GPU |
+	 *  DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+	 *  DRM_PANTHOR_TIMESTAMP_FREQ
+	 */
+	__u32 flags;
+
+	/** @duration_nsec: Duration of time query. */
+	__u32 duration_nsec;
+
+	/** @cycle_count: Value of GPU_CYCLE_COUNT. */
+	__u64 cycle_count;
+
+	/** @cpu_timestamp_sec: Seconds part of CPU timestamp. */
+	__u64 cpu_timestamp_sec;
+
+	/** @cpu_timestamp_nsec: Nanseconds part of CPU timestamp. */
+	__u64 cpu_timestamp_nsec;
 };
 
 /**
#10 lore raw

PATCH v3 1/1 drm/panthor: extend timestamp query with flags

In reply to Marcin Slusarz

Flags now control which data user space wants to query, there is more information sources, and there's ability to query duration of multiple timestamp reads.

New sources:
- CPU's monotonic,
- CPU's monotonic raw,
- GPU's cycle count

These changes should make the implementation of VK_KHR_calibrated_timestamps more accurate and much simpler.

---
Changes in v3:
- {} around multiline statements
- added comment about default flags
- added VALID_TIMESTAMP_QUERY_FLAGS
- used u64_to_user_ptr
Changes in v2:
- added DRM_PANTHOR_TIMESTAMP_CPU_NONE
- cpu_timestamp_nsec extended to u64
- pad1 removed
- copy_from_user -> copy_struct_from_user
- looking at size replaced by looking at flags
- more comments

---
 drivers/gpu/drm/panthor/panthor_drv.c | 134 ++++++++++++++++++++++++--
 include/uapi/drm/panthor_drm.h        |  58 ++++++++++-
 2 files changed, 184 insertions(+), 8 deletions(-)
drivers/gpu/drm/panthor/panthor_drv.c +128 -6
index 165dddfde6ca..029ef3295b06 100644
@@ -13,7 +13,9 @@
 #include <linux/pagemap.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
+#include <linux/sched/clock.h>
 #include <linux/time64.h>
+#include <linux/time_namespace.h>
 
 #include <drm/drm_auth.h>
 #include <drm/drm_debugfs.h>
@@ -761,22 +763,135 @@ static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx,
 	kvfree(ctx->jobs);
 }
 
+#define VALID_TIMESTAMP_QUERY_FLAGS \
+		(DRM_PANTHOR_TIMESTAMP_GPU | \
+		 DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_OFFSET | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT | \
+		 DRM_PANTHOR_TIMESTAMP_FREQ | \
+		 DRM_PANTHOR_TIMESTAMP_DURATION)
+
 static int panthor_query_timestamp_info(struct panthor_device *ptdev,
 					struct drm_panthor_timestamp_info *arg)
 {
 	int ret;
+	u32 flags;
+	unsigned long irq_flags;
+	struct timespec64 cpu_ts;
+	u64 query_start_time;
+	bool minimize_interruption;
+	u32 timestamp_types = 0;
+
+	if (arg->flags != 0) {
+		flags = arg->flags;
+	} else {
+		/*
+		 * If flags are 0, then ask for the same things that we asked
+		 * for before flags were added.
+		 */
+		flags = DRM_PANTHOR_TIMESTAMP_GPU |
+			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+			DRM_PANTHOR_TIMESTAMP_FREQ;
+	}
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case 0:
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		timestamp_types++;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	if (flags & ~VALID_TIMESTAMP_QUERY_FLAGS)
+		return -EINVAL;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		timestamp_types++;
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		timestamp_types++;
+
+	/* If user asked to obtain timestamps from more than one source,
+	 * then it very likely means they want them to be as close as possible.
+	 * If they asked for duration, then that likely means that they
+	 * want to know how long obtaining timestamp takes, without random
+	 * events, like process scheduling or interrupts.
+	 */
+	minimize_interruption =
+		(flags & DRM_PANTHOR_TIMESTAMP_DURATION) ||
+		(timestamp_types >= 2);
 
 	ret = panthor_device_resume_and_get(ptdev);
 	if (ret)
 		return ret;
 
+	if (flags & DRM_PANTHOR_TIMESTAMP_FREQ) {
 #ifdef CONFIG_ARM_ARCH_TIMER
-	arg->timestamp_frequency = arch_timer_get_cntfrq();
+		arg->timestamp_frequency = arch_timer_get_cntfrq();
 #else
-	arg->timestamp_frequency = 0;
+		arg->timestamp_frequency = 0;
 #endif
-	arg->current_timestamp = gpu_read64_counter(ptdev, GPU_TIMESTAMP);
-	arg->timestamp_offset = gpu_read64(ptdev, GPU_TIMESTAMP_OFFSET);
+	} else {
+		arg->timestamp_frequency = 0;
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_OFFSET)
+		arg->timestamp_offset = gpu_read64(ptdev, GPU_TIMESTAMP_OFFSET);
+	else
+		arg->timestamp_offset = 0;
+
+	if (minimize_interruption) {
+		preempt_disable();
+		local_irq_save(irq_flags);
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_DURATION)
+		query_start_time = local_clock();
+	else
+		query_start_time = 0;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		arg->current_timestamp = gpu_read64_counter(ptdev, GPU_TIMESTAMP);
+	else
+		arg->current_timestamp = 0;
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+		ktime_get_ts64(&cpu_ts);
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		ktime_get_raw_ts64(&cpu_ts);
+		break;
+	default:
+		break;
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		arg->cycle_count = gpu_read64_counter(ptdev, GPU_CYCLE_COUNT);
+	else
+		arg->cycle_count = 0;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_DURATION)
+		arg->duration_nsec = local_clock() - query_start_time;
+	else
+		arg->duration_nsec = 0;
+
+	if (minimize_interruption) {
+		local_irq_restore(irq_flags);
+		preempt_enable();
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+		timens_add_monotonic(&cpu_ts);
+
+		arg->cpu_timestamp_sec = cpu_ts.tv_sec;
+		arg->cpu_timestamp_nsec = cpu_ts.tv_nsec;
+	} else {
+		arg->cpu_timestamp_sec = 0;
+		arg->cpu_timestamp_nsec = 0;
+	}
 
 	pm_runtime_put(ptdev->base.dev);
 	return 0;
@@ -851,8 +966,14 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d
 		return PANTHOR_UOBJ_SET(args->pointer, args->size, ptdev->csif_info);
 
 	case DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO:
-		ret = panthor_query_timestamp_info(ptdev, &timestamp_info);
+		ret = copy_struct_from_user(&timestamp_info,
+					    sizeof(timestamp_info),
+					    u64_to_user_ptr(args->pointer),
+					    args->size);
+		if (ret)
+			return ret;
 
+		ret = panthor_query_timestamp_info(ptdev, &timestamp_info);
 		if (ret)
 			return ret;
 
@@ -1680,6 +1801,7 @@ static void panthor_debugfs_init(struct drm_minor *minor)
  *       - adds DRM_IOCTL_PANTHOR_BO_SYNC ioctl
  *       - adds DRM_IOCTL_PANTHOR_BO_QUERY_INFO ioctl
  *       - adds drm_panthor_gpu_info::selected_coherency
+ * - 1.8 - extends DEV_QUERY_TIMESTAMP_INFO with flags
  */
 static const struct drm_driver panthor_drm_driver = {
 	.driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ |
@@ -1693,7 +1815,7 @@ static const struct drm_driver panthor_drm_driver = {
 	.name = "panthor",
 	.desc = "Panthor DRM driver",
 	.major = 1,
-	.minor = 7,
+	.minor = 8,
 
 	.gem_create_object = panthor_gem_create_object,
 	.gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table,
include/uapi/drm/panthor_drm.h +56 -2

73 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer Reviewed-by on v3 1/1
#11 lore raw
Quoted 9 lines · On Thu, 19 Mar 2026 12:00:53 +0100 Marcin Slusarz <[email protected]>…

Flags now control which data user space wants to query, there is more information sources, and there's ability to query duration of multiple timestamp reads.

New sources:
- CPU's monotonic,
- CPU's monotonic raw,
- GPU's cycle count

Quoted 4 lines

These changes should make the implementation of VK_KHR_calibrated_timestamps more accurate and much simpler.

Still

Quoted 284 lines

---
Changes in v3:
- {} around multiline statements
- added comment about default flags
- added VALID_TIMESTAMP_QUERY_FLAGS
- used u64_to_user_ptr
Changes in v2:
- added DRM_PANTHOR_TIMESTAMP_CPU_NONE
- cpu_timestamp_nsec extended to u64
- pad1 removed
- copy_from_user -> copy_struct_from_user
- looking at size replaced by looking at flags
- more comments

---
 drivers/gpu/drm/panthor/panthor_drv.c | 134 ++++++++++++++++++++++++--
 include/uapi/drm/panthor_drm.h        |  58 ++++++++++-
 2 files changed, 184 insertions(+), 8 deletions(-)
drivers/gpu/drm/panthor/panthor_drv.c +128 -6
index 165dddfde6ca..029ef3295b06 100644
@@ -13,7 +13,9 @@
 #include <linux/pagemap.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
+#include <linux/sched/clock.h>
 #include <linux/time64.h>
+#include <linux/time_namespace.h>
 
 #include <drm/drm_auth.h>
 #include <drm/drm_debugfs.h>
@@ -761,22 +763,135 @@ static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx,
 	kvfree(ctx->jobs);
 }
 
+#define VALID_TIMESTAMP_QUERY_FLAGS \
+		(DRM_PANTHOR_TIMESTAMP_GPU | \
+		 DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_OFFSET | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT | \
+		 DRM_PANTHOR_TIMESTAMP_FREQ | \
+		 DRM_PANTHOR_TIMESTAMP_DURATION)
+
 static int panthor_query_timestamp_info(struct panthor_device *ptdev,
 					struct drm_panthor_timestamp_info *arg)
 {
 	int ret;
+	u32 flags;
+	unsigned long irq_flags;
+	struct timespec64 cpu_ts;
+	u64 query_start_time;
+	bool minimize_interruption;
+	u32 timestamp_types = 0;
+
+	if (arg->flags != 0) {
+		flags = arg->flags;
+	} else {
+		/*
+		 * If flags are 0, then ask for the same things that we asked
+		 * for before flags were added.
+		 */
+		flags = DRM_PANTHOR_TIMESTAMP_GPU |
+			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+			DRM_PANTHOR_TIMESTAMP_FREQ;
+	}
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case 0:
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		timestamp_types++;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	if (flags & ~VALID_TIMESTAMP_QUERY_FLAGS)
+		return -EINVAL;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		timestamp_types++;
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		timestamp_types++;
+
+	/* If user asked to obtain timestamps from more than one source,
+	 * then it very likely means they want them to be as close as possible.
+	 * If they asked for duration, then that likely means that they
+	 * want to know how long obtaining timestamp takes, without random
+	 * events, like process scheduling or interrupts.
+	 */
+	minimize_interruption =
+		(flags & DRM_PANTHOR_TIMESTAMP_DURATION) ||
+		(timestamp_types >= 2);
 
 	ret = panthor_device_resume_and_get(ptdev);
 	if (ret)
 		return ret;
 
+	if (flags & DRM_PANTHOR_TIMESTAMP_FREQ) {
 #ifdef CONFIG_ARM_ARCH_TIMER
-	arg->timestamp_frequency = arch_timer_get_cntfrq();
+		arg->timestamp_frequency = arch_timer_get_cntfrq();
 #else
-	arg->timestamp_frequency = 0;
+		arg->timestamp_frequency = 0;
 #endif
-	arg->current_timestamp = gpu_read64_counter(ptdev, GPU_TIMESTAMP);
-	arg->timestamp_offset = gpu_read64(ptdev, GPU_TIMESTAMP_OFFSET);
+	} else {
+		arg->timestamp_frequency = 0;
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_OFFSET)
+		arg->timestamp_offset = gpu_read64(ptdev, GPU_TIMESTAMP_OFFSET);
+	else
+		arg->timestamp_offset = 0;
+
+	if (minimize_interruption) {
+		preempt_disable();
+		local_irq_save(irq_flags);
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_DURATION)
+		query_start_time = local_clock();
+	else
+		query_start_time = 0;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		arg->current_timestamp = gpu_read64_counter(ptdev, GPU_TIMESTAMP);
+	else
+		arg->current_timestamp = 0;
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+		ktime_get_ts64(&cpu_ts);
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		ktime_get_raw_ts64(&cpu_ts);
+		break;
+	default:
+		break;
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		arg->cycle_count = gpu_read64_counter(ptdev, GPU_CYCLE_COUNT);
+	else
+		arg->cycle_count = 0;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_DURATION)
+		arg->duration_nsec = local_clock() - query_start_time;
+	else
+		arg->duration_nsec = 0;
+
+	if (minimize_interruption) {
+		local_irq_restore(irq_flags);
+		preempt_enable();
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+		timens_add_monotonic(&cpu_ts);
+
+		arg->cpu_timestamp_sec = cpu_ts.tv_sec;
+		arg->cpu_timestamp_nsec = cpu_ts.tv_nsec;
+	} else {
+		arg->cpu_timestamp_sec = 0;
+		arg->cpu_timestamp_nsec = 0;
+	}
 
 	pm_runtime_put(ptdev->base.dev);
 	return 0;
@@ -851,8 +966,14 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d
 		return PANTHOR_UOBJ_SET(args->pointer, args->size, ptdev->csif_info);
 
 	case DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO:
-		ret = panthor_query_timestamp_info(ptdev, &timestamp_info);
+		ret = copy_struct_from_user(&timestamp_info,
+					    sizeof(timestamp_info),
+					    u64_to_user_ptr(args->pointer),
+					    args->size);
+		if (ret)
+			return ret;
 
+		ret = panthor_query_timestamp_info(ptdev, &timestamp_info);
 		if (ret)
 			return ret;
 
@@ -1680,6 +1801,7 @@ static void panthor_debugfs_init(struct drm_minor *minor)
  *       - adds DRM_IOCTL_PANTHOR_BO_SYNC ioctl
  *       - adds DRM_IOCTL_PANTHOR_BO_QUERY_INFO ioctl
  *       - adds drm_panthor_gpu_info::selected_coherency
+ * - 1.8 - extends DEV_QUERY_TIMESTAMP_INFO with flags
  */
 static const struct drm_driver panthor_drm_driver = {
 	.driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ |
@@ -1693,7 +1815,7 @@ static const struct drm_driver panthor_drm_driver = {
 	.name = "panthor",
 	.desc = "Panthor DRM driver",
 	.major = 1,
-	.minor = 7,
+	.minor = 8,
 
 	.gem_create_object = panthor_gem_create_object,
 	.gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table,
include/uapi/drm/panthor_drm.h +56 -2
index b401ac585d6a..8a46ef040c3d 100644
@@ -409,6 +409,38 @@ struct drm_panthor_csif_info {
 	__u32 pad;
 };
 
+/**
+ * enum drm_panthor_timestamp_info_flags - drm_panthor_timestamp_info.flags
+ */
+enum drm_panthor_timestamp_info_flags {
+	/** @DRM_PANTHOR_TIMESTAMP_GPU: Query GPU time. */
+	DRM_PANTHOR_TIMESTAMP_GPU = 1 << 0,
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_NONE: Don't query CPU time. */
+	DRM_PANTHOR_TIMESTAMP_CPU_NONE = 0 << 1,
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC: Query CPU time using CLOCK_MONOTONIC. */
+	DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC = 1 << 1,
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW: Query CPU time using CLOCK_MONOTONIC_RAW. */
+	DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW = 2 << 1,
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK: Space reserved for CPU clock type. */
+	DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK = 7 << 1,
+
+	/** @DRM_PANTHOR_TIMESTAMP_GPU_OFFSET: Query GPU offset. */
+	DRM_PANTHOR_TIMESTAMP_GPU_OFFSET = 1 << 4,
+
+	/** @DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT: Query GPU cycle count. */
+	DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT = 1 << 5,
+
+	/** @DRM_PANTHOR_TIMESTAMP_FREQ: Query timestamp frequency. */
+	DRM_PANTHOR_TIMESTAMP_FREQ = 1 << 6,
+
+	/** @DRM_PANTHOR_TIMESTAMP_DURATION: Return duration of time query. */
+	DRM_PANTHOR_TIMESTAMP_DURATION = 1 << 7,
+};
+
 /**
  * struct drm_panthor_timestamp_info - Timestamp information
  *
@@ -421,11 +453,33 @@ struct drm_panthor_timestamp_info {
 	 */
 	__u64 timestamp_frequency;
 
-	/** @current_timestamp: The current timestamp. */
+	/** @current_timestamp: The current GPU timestamp. */
 	__u64 current_timestamp;
 
-	/** @timestamp_offset: The offset of the timestamp timer. */
+	/** @timestamp_offset: The offset of the GPU timestamp timer. */
 	__u64 timestamp_offset;
+
+	/**
+	 * @flags: Bitmask of drm_panthor_timestamp_info_flags.
+	 *
+	 * If set to 0, then it is interpreted as:
+	 *  DRM_PANTHOR_TIMESTAMP_GPU |
+	 *  DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+	 *  DRM_PANTHOR_TIMESTAMP_FREQ
+	 */
+	__u32 flags;
+
+	/** @duration_nsec: Duration of time query. */
+	__u32 duration_nsec;
+
+	/** @cycle_count: Value of GPU_CYCLE_COUNT. */
+	__u64 cycle_count;
+
+	/** @cpu_timestamp_sec: Seconds part of CPU timestamp. */
+	__u64 cpu_timestamp_sec;
+
+	/** @cpu_timestamp_nsec: Nanseconds part of CPU timestamp. */
+	__u64 cpu_timestamp_nsec;
 };
 
 /**
maintainer Reviewed-by on v3 1/1
#12 lore raw
In reply to Marcin Slusarz

Hi Marcin,

Quoted 83 lines · Marcin Slusarz

Flags now control which data user space wants to query, there is more information sources, and there's ability to query duration of multiple timestamp reads.

New sources:
- CPU's monotonic,
- CPU's monotonic raw,
- GPU's cycle count

These changes should make the implementation of VK_KHR_calibrated_timestamps more accurate and much simpler.

---
Changes in v3:
- {} around multiline statements
- added comment about default flags
- added VALID_TIMESTAMP_QUERY_FLAGS
- used u64_to_user_ptr
Changes in v2:
- added DRM_PANTHOR_TIMESTAMP_CPU_NONE
- cpu_timestamp_nsec extended to u64
- pad1 removed
- copy_from_user -> copy_struct_from_user
- looking at size replaced by looking at flags
- more comments

---
 drivers/gpu/drm/panthor/panthor_drv.c | 134 ++++++++++++++++++++++++--
 include/uapi/drm/panthor_drm.h        |  58 ++++++++++-
 2 files changed, 184 insertions(+), 8 deletions(-)
drivers/gpu/drm/panthor/panthor_drv.c +32 -0
index 165dddfde6ca..029ef3295b06 100644
@@ -13,7 +13,9 @@
 #include <linux/pagemap.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
+#include <linux/sched/clock.h>
 #include <linux/time64.h>
+#include <linux/time_namespace.h>
 
 #include <drm/drm_auth.h>
 #include <drm/drm_debugfs.h>
@@ -761,22 +763,135 @@ static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx,
 	kvfree(ctx->jobs);
 }
 
+#define VALID_TIMESTAMP_QUERY_FLAGS \
+		(DRM_PANTHOR_TIMESTAMP_GPU | \
+		 DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_OFFSET | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT | \
+		 DRM_PANTHOR_TIMESTAMP_FREQ | \
+		 DRM_PANTHOR_TIMESTAMP_DURATION)
+
 static int panthor_query_timestamp_info(struct panthor_device *ptdev,
 					struct drm_panthor_timestamp_info *arg)
 {
 	int ret;
+	u32 flags;
+	unsigned long irq_flags;
+	struct timespec64 cpu_ts;
+	u64 query_start_time;
+	bool minimize_interruption;
+	u32 timestamp_types = 0;
+
+	if (arg->flags != 0) {
+		flags = arg->flags;
+	} else {
+		/*
+		 * If flags are 0, then ask for the same things that we asked
+		 * for before flags were added.
+		 */
+		flags = DRM_PANTHOR_TIMESTAMP_GPU |
+			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+			DRM_PANTHOR_TIMESTAMP_FREQ;
+	}
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case 0:
+		break;

drivers/gpu/drm/panthor/panthor_drv.c static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx, · line 809

Quoted 10 lines
drivers/gpu/drm/panthor/panthor_drv.c +10 -0
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		timestamp_types++;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	if (flags & ~VALID_TIMESTAMP_QUERY_FLAGS)
+		return -EINVAL;

Can we move this check before the switch and simplify the switch itself to only do the timestamp_types increment?

drivers/gpu/drm/panthor/panthor_drv.c static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx, · line 821

Quoted 12 lines
drivers/gpu/drm/panthor/panthor_drv.c +12 -0
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		timestamp_types++;
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		timestamp_types++;
+
+	/* If user asked to obtain timestamps from more than one source,
+	 * then it very likely means they want them to be as close as possible.
+	 * If they asked for duration, then that likely means that they
+	 * want to know how long obtaining timestamp takes, without random
+	 * events, like process scheduling or interrupts.
+	 */

This comment makes me think that user can ask for both CPU_MONOTONIC and CPU_MONOTONIC_RAW timestamps, but the code is built to make them exclusive. Can we document better what sources can be requested simultaneously?

Quoted 195 lines
drivers/gpu/drm/panthor/panthor_drv.c +74 -6
+	minimize_interruption =
+		(flags & DRM_PANTHOR_TIMESTAMP_DURATION) ||
+		(timestamp_types >= 2);
 
 	ret = panthor_device_resume_and_get(ptdev);
 	if (ret)
 		return ret;
 
+	if (flags & DRM_PANTHOR_TIMESTAMP_FREQ) {
 #ifdef CONFIG_ARM_ARCH_TIMER
-	arg->timestamp_frequency = arch_timer_get_cntfrq();
+		arg->timestamp_frequency = arch_timer_get_cntfrq();
 #else
-	arg->timestamp_frequency = 0;
+		arg->timestamp_frequency = 0;
 #endif
-	arg->current_timestamp = gpu_read64_counter(ptdev, GPU_TIMESTAMP);
-	arg->timestamp_offset = gpu_read64(ptdev, GPU_TIMESTAMP_OFFSET);
+	} else {
+		arg->timestamp_frequency = 0;
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_OFFSET)
+		arg->timestamp_offset = gpu_read64(ptdev, GPU_TIMESTAMP_OFFSET);
+	else
+		arg->timestamp_offset = 0;
+
+	if (minimize_interruption) {
+		preempt_disable();
+		local_irq_save(irq_flags);
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_DURATION)
+		query_start_time = local_clock();
+	else
+		query_start_time = 0;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		arg->current_timestamp = gpu_read64_counter(ptdev, GPU_TIMESTAMP);
+	else
+		arg->current_timestamp = 0;
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+		ktime_get_ts64(&cpu_ts);
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		ktime_get_raw_ts64(&cpu_ts);
+		break;
+	default:
+		break;
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		arg->cycle_count = gpu_read64_counter(ptdev, GPU_CYCLE_COUNT);
+	else
+		arg->cycle_count = 0;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_DURATION)
+		arg->duration_nsec = local_clock() - query_start_time;
+	else
+		arg->duration_nsec = 0;
+
+	if (minimize_interruption) {
+		local_irq_restore(irq_flags);
+		preempt_enable();
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+		timens_add_monotonic(&cpu_ts);
+
+		arg->cpu_timestamp_sec = cpu_ts.tv_sec;
+		arg->cpu_timestamp_nsec = cpu_ts.tv_nsec;
+	} else {
+		arg->cpu_timestamp_sec = 0;
+		arg->cpu_timestamp_nsec = 0;
+	}
 
 	pm_runtime_put(ptdev->base.dev);
 	return 0;
@@ -851,8 +966,14 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d
 		return PANTHOR_UOBJ_SET(args->pointer, args->size, ptdev->csif_info);
 
 	case DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO:
-		ret = panthor_query_timestamp_info(ptdev, &timestamp_info);
+		ret = copy_struct_from_user(&timestamp_info,
+					    sizeof(timestamp_info),
+					    u64_to_user_ptr(args->pointer),
+					    args->size);
+		if (ret)
+			return ret;
 
+		ret = panthor_query_timestamp_info(ptdev, &timestamp_info);
 		if (ret)
 			return ret;
 
@@ -1680,6 +1801,7 @@ static void panthor_debugfs_init(struct drm_minor *minor)
  *       - adds DRM_IOCTL_PANTHOR_BO_SYNC ioctl
  *       - adds DRM_IOCTL_PANTHOR_BO_QUERY_INFO ioctl
  *       - adds drm_panthor_gpu_info::selected_coherency
+ * - 1.8 - extends DEV_QUERY_TIMESTAMP_INFO with flags
  */
 static const struct drm_driver panthor_drm_driver = {
 	.driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ |
@@ -1693,7 +1815,7 @@ static const struct drm_driver panthor_drm_driver = {
 	.name = "panthor",
 	.desc = "Panthor DRM driver",
 	.major = 1,
-	.minor = 7,
+	.minor = 8,
 
 	.gem_create_object = panthor_gem_create_object,
 	.gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table,
include/uapi/drm/panthor_drm.h +56 -2
index b401ac585d6a..8a46ef040c3d 100644
@@ -409,6 +409,38 @@ struct drm_panthor_csif_info {
 	__u32 pad;
 };
 
+/**
+ * enum drm_panthor_timestamp_info_flags - drm_panthor_timestamp_info.flags
+ */
+enum drm_panthor_timestamp_info_flags {
+	/** @DRM_PANTHOR_TIMESTAMP_GPU: Query GPU time. */
+	DRM_PANTHOR_TIMESTAMP_GPU = 1 << 0,
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_NONE: Don't query CPU time. */
+	DRM_PANTHOR_TIMESTAMP_CPU_NONE = 0 << 1,
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC: Query CPU time using CLOCK_MONOTONIC. */
+	DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC = 1 << 1,
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW: Query CPU time using CLOCK_MONOTONIC_RAW. */
+	DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW = 2 << 1,
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK: Space reserved for CPU clock type. */
+	DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK = 7 << 1,
+
+	/** @DRM_PANTHOR_TIMESTAMP_GPU_OFFSET: Query GPU offset. */
+	DRM_PANTHOR_TIMESTAMP_GPU_OFFSET = 1 << 4,
+
+	/** @DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT: Query GPU cycle count. */
+	DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT = 1 << 5,
+
+	/** @DRM_PANTHOR_TIMESTAMP_FREQ: Query timestamp frequency. */
+	DRM_PANTHOR_TIMESTAMP_FREQ = 1 << 6,
+
+	/** @DRM_PANTHOR_TIMESTAMP_DURATION: Return duration of time query. */
+	DRM_PANTHOR_TIMESTAMP_DURATION = 1 << 7,
+};
+
 /**
  * struct drm_panthor_timestamp_info - Timestamp information
  *
@@ -421,11 +453,33 @@ struct drm_panthor_timestamp_info {
 	 */
 	__u64 timestamp_frequency;
 
-	/** @current_timestamp: The current timestamp. */
+	/** @current_timestamp: The current GPU timestamp. */
 	__u64 current_timestamp;
 
-	/** @timestamp_offset: The offset of the timestamp timer. */
+	/** @timestamp_offset: The offset of the GPU timestamp timer. */
 	__u64 timestamp_offset;
+
+	/**
+	 * @flags: Bitmask of drm_panthor_timestamp_info_flags.
+	 *
+	 * If set to 0, then it is interpreted as:
+	 *  DRM_PANTHOR_TIMESTAMP_GPU |
+	 *  DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+	 *  DRM_PANTHOR_TIMESTAMP_FREQ
+	 */
+	__u32 flags;
+
+	/** @duration_nsec: Duration of time query. */
+	__u32 duration_nsec;
+
+	/** @cycle_count: Value of GPU_CYCLE_COUNT. */
+	__u64 cycle_count;
+
+	/** @cpu_timestamp_sec: Seconds part of CPU timestamp. */
+	__u64 cpu_timestamp_sec;
+
+	/** @cpu_timestamp_nsec: Nanseconds part of CPU timestamp. */
+	__u64 cpu_timestamp_nsec;
 };
 
 /**
Signature · 1 line
2.34.1

With those addressed,

Best regards,
Liviu

Signature · 8 lines
====================
| I would like to |
| fix the world,  |
| but they're not |
| giving me the   |
 \ source code!  /
  ---------------
    ¯\_(ツ)_/¯
#13 lore raw
Quoted 29 lines · Liviu Dudau

Hi Marcin,

Quoted 26 lines · Marcin Slusarz

...

(quoted diff) +21 -0
+#define VALID_TIMESTAMP_QUERY_FLAGS \
+		(DRM_PANTHOR_TIMESTAMP_GPU | \
+		 DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_OFFSET | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT | \
+		 DRM_PANTHOR_TIMESTAMP_FREQ | \
+		 DRM_PANTHOR_TIMESTAMP_DURATION)
+
 static int panthor_query_timestamp_info(struct panthor_device *ptdev,
 					struct drm_panthor_timestamp_info *arg)
 {
 	int ret;
+	u32 flags;
+	unsigned long irq_flags;
+	struct timespec64 cpu_ts;
+	u64 query_start_time;
+	bool minimize_interruption;
+	u32 timestamp_types = 0;
+
+	if (arg->flags != 0) {
+		flags = arg->flags;
+	} else {
+		/*
+		 * If flags are 0, then ask for the same things that we asked
+		 * for before flags were added.
Quoted 8 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+		 */
+		flags = DRM_PANTHOR_TIMESTAMP_GPU |
+			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+			DRM_PANTHOR_TIMESTAMP_FREQ;
+	}
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case 0:

Umm, this should be DRM_PANTHOR_TIMESTAMP_CPU_NONE.

Quoted 5 lines
Quoted 5 lines
(quoted diff) +5 -0
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		timestamp_types++;
+		break;
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+	default:
+		return -EINVAL;
+	}
+
+	if (flags & ~VALID_TIMESTAMP_QUERY_FLAGS)
+		return -EINVAL;

Can we move this check before the switch and simplify the switch itself to only do the timestamp_types increment?

DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK is bit field that holds individual clock type values, so we still need to validate the bit field.

Quoted 8 lines
Quoted 8 lines
(quoted diff) +8 -0
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		timestamp_types++;
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		timestamp_types++;
+
+	/* If user asked to obtain timestamps from more than one source,
+	 * then it very likely means they want them to be as close as possible.
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+	 * If they asked for duration, then that likely means that they
+	 * want to know how long obtaining timestamp takes, without random
+	 * events, like process scheduling or interrupts.
+	 */

This comment makes me think that user can ask for both CPU_MONOTONIC and CPU_MONOTONIC_RAW timestamps, but the code is built to make them exclusive. Can we document better what sources can be requested simultaneously?

Somethine like this?

include/uapi/drm/panthor_drm.h +5 -0
index 8a46ef040c3d..0e455d91e77d 100644
@@ -466,6 +466,11 @@ struct drm_panthor_timestamp_info {
         *  DRM_PANTHOR_TIMESTAMP_GPU |
         *  DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
         *  DRM_PANTHOR_TIMESTAMP_FREQ
+        *
+        * Note: these flags are exclusive to each other (only one can be used):
+        * - DRM_PANTHOR_TIMESTAMP_CPU_NONE
+        * - DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC
+        * - DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW
         */
        __u32 flags;
 
maintainer
#14 lore raw
Quoted 55 lines · Marcin Ślusarz
Quoted 29 lines · Liviu Dudau

Hi Marcin,

Quoted 26 lines · Marcin Slusarz

...

(quoted diff) +21 -0
+#define VALID_TIMESTAMP_QUERY_FLAGS \
+		(DRM_PANTHOR_TIMESTAMP_GPU | \
+		 DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_OFFSET | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT | \
+		 DRM_PANTHOR_TIMESTAMP_FREQ | \
+		 DRM_PANTHOR_TIMESTAMP_DURATION)
+
 static int panthor_query_timestamp_info(struct panthor_device *ptdev,
 					struct drm_panthor_timestamp_info *arg)
 {
 	int ret;
+	u32 flags;
+	unsigned long irq_flags;
+	struct timespec64 cpu_ts;
+	u64 query_start_time;
+	bool minimize_interruption;
+	u32 timestamp_types = 0;
+
+	if (arg->flags != 0) {
+		flags = arg->flags;
+	} else {
+		/*
+		 * If flags are 0, then ask for the same things that we asked
+		 * for before flags were added.
Quoted 8 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+		 */
+		flags = DRM_PANTHOR_TIMESTAMP_GPU |
+			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+			DRM_PANTHOR_TIMESTAMP_FREQ;
+	}
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case 0:

Umm, this should be DRM_PANTHOR_TIMESTAMP_CPU_NONE.

Quoted 5 lines
Quoted 5 lines
(quoted diff) +5 -0
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		timestamp_types++;
+		break;
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+	default:
+		return -EINVAL;
+	}
+
+	if (flags & ~VALID_TIMESTAMP_QUERY_FLAGS)
+		return -EINVAL;

Can we move this check before the switch and simplify the switch itself to only do the timestamp_types increment?

Quoted 2 lines

DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK is bit field that holds individual clock type values, so we still need to validate the bit field.

The if () test eliminates the default case, and if you change the switch to:

	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_NONE) {
	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
		timestamp_types++;
		break;
	}

then it should be equivalent, right?

Quoted 25 lines
Quoted 17 lines

Review

Quoted 13 lines
(quoted diff) +12 -0
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		timestamp_types++;
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		timestamp_types++;
+
+	/* If user asked to obtain timestamps from more than one source,
+	 * then it very likely means they want them to be as close as possible.
+	 * If they asked for duration, then that likely means that they
+	 * want to know how long obtaining timestamp takes, without random
+	 * events, like process scheduling or interrupts.
+	 */

This comment makes me think that user can ask for both CPU_MONOTONIC and CPU_MONOTONIC_RAW timestamps, but the code is built to make them exclusive. Can we document better what sources can be requested simultaneously?

Somethine like this?

include/uapi/drm/panthor_drm.h +0 -0
index 8a46ef040c3d..0e455d91e77d 100644

include/uapi/drm/panthor_drm.h struct drm_panthor_timestamp_info { · line 473

Quoted 8 lines
include/uapi/drm/panthor_drm.h +5 -0
@@ -466,6 +466,11 @@ struct drm_panthor_timestamp_info {
         *  DRM_PANTHOR_TIMESTAMP_GPU |
         *  DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
         *  DRM_PANTHOR_TIMESTAMP_FREQ
+        *
+        * Note: these flags are exclusive to each other (only one can be used):
+        * - DRM_PANTHOR_TIMESTAMP_CPU_NONE
+        * - DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC
+        * - DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW

Yes, looks good to me.

Best regards,
Liviu

Quoted 3 lines
include/uapi/drm/panthor_drm.h +0 -0
         */
        __u32 flags;
 
Signature · 8 lines
====================
| I would like to |
| fix the world,  |
| but they're not |
| giving me the   |
 \ source code!  /
  ---------------
    ¯\_(ツ)_/¯
#15 lore raw
Quoted 61 lines · Liviu Dudau
Quoted 55 lines · Marcin Ślusarz
Quoted 29 lines · Liviu Dudau

Hi Marcin,

Quoted 26 lines · Marcin Slusarz

...

(quoted diff) +21 -0
+#define VALID_TIMESTAMP_QUERY_FLAGS \
+		(DRM_PANTHOR_TIMESTAMP_GPU | \
+		 DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_OFFSET | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT | \
+		 DRM_PANTHOR_TIMESTAMP_FREQ | \
+		 DRM_PANTHOR_TIMESTAMP_DURATION)
+
 static int panthor_query_timestamp_info(struct panthor_device *ptdev,
 					struct drm_panthor_timestamp_info *arg)
 {
 	int ret;
+	u32 flags;
+	unsigned long irq_flags;
+	struct timespec64 cpu_ts;
+	u64 query_start_time;
+	bool minimize_interruption;
+	u32 timestamp_types = 0;
+
+	if (arg->flags != 0) {
+		flags = arg->flags;
+	} else {
+		/*
+		 * If flags are 0, then ask for the same things that we asked
+		 * for before flags were added.
Quoted 8 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+		 */
+		flags = DRM_PANTHOR_TIMESTAMP_GPU |
+			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+			DRM_PANTHOR_TIMESTAMP_FREQ;
+	}
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case 0:

Umm, this should be DRM_PANTHOR_TIMESTAMP_CPU_NONE.

Quoted 5 lines
Quoted 5 lines
(quoted diff) +5 -0
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		timestamp_types++;
+		break;
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+	default:
+		return -EINVAL;
+	}
+
+	if (flags & ~VALID_TIMESTAMP_QUERY_FLAGS)
+		return -EINVAL;

Can we move this check before the switch and simplify the switch itself to only do the timestamp_types increment?

Quoted 2 lines

DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK is bit field that holds individual clock type values, so we still need to validate the bit field.

The if () test eliminates the default case, and if you change the switch to:

Quoted 8 lines
	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_NONE) {
	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
		timestamp_types++;
		break;
	}

then it should be equivalent, right?

We need the default case to detect garbage values in the part of flags that ands with DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK.

DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK is 7 << 1, DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC is 1 << 1, DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW is 2 << 1,
so 3 << 1, 4 << 1, 5 << 1, 6 << 1, 7 << 1 are all invalid values that need to be rejected.

And since DRM_PANTHOR_TIMESTAMP_CPU_NONE is 0 << 1, we need it too in the switch to not be caught by the default case.

Quoted 52 lines
Quoted 25 lines
Quoted 17 lines

Review

Quoted 13 lines
(quoted diff) +12 -0
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		timestamp_types++;
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		timestamp_types++;
+
+	/* If user asked to obtain timestamps from more than one source,
+	 * then it very likely means they want them to be as close as possible.
+	 * If they asked for duration, then that likely means that they
+	 * want to know how long obtaining timestamp takes, without random
+	 * events, like process scheduling or interrupts.
+	 */

This comment makes me think that user can ask for both CPU_MONOTONIC and CPU_MONOTONIC_RAW timestamps, but the code is built to make them exclusive. Can we document better what sources can be requested simultaneously?

Somethine like this?

include/uapi/drm/panthor_drm.h +0 -0
index 8a46ef040c3d..0e455d91e77d 100644

include/uapi/drm/panthor_drm.h struct drm_panthor_timestamp_info { · line 473

Quoted 8 lines
include/uapi/drm/panthor_drm.h +5 -0
@@ -466,6 +466,11 @@ struct drm_panthor_timestamp_info {
         *  DRM_PANTHOR_TIMESTAMP_GPU |
         *  DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
         *  DRM_PANTHOR_TIMESTAMP_FREQ
+        *
+        * Note: these flags are exclusive to each other (only one can be used):
+        * - DRM_PANTHOR_TIMESTAMP_CPU_NONE
+        * - DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC
+        * - DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW

Yes, looks good to me.

Best regards,
Liviu

Quoted 3 lines
include/uapi/drm/panthor_drm.h +0 -0
         */
        __u32 flags;
 
Signature · 8 lines
====================
| I would like to |
| fix the world,  |
| but they're not |
| giving me the   |
 \ source code!  /
  ---------------
    ¯\_(ツ)_/¯
maintainer
#16 lore raw
Quoted 34 lines · Marcin Ślusarz
Quoted 33 lines · Liviu Dudau
Quoted 32 lines · Marcin Ślusarz
Quoted 31 lines · Liviu Dudau

Hi Marcin,

Quoted 28 lines · Marcin Slusarz

...

(quoted diff) +23 -0
+#define VALID_TIMESTAMP_QUERY_FLAGS \
+		(DRM_PANTHOR_TIMESTAMP_GPU | \
+		 DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_OFFSET | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT | \
+		 DRM_PANTHOR_TIMESTAMP_FREQ | \
+		 DRM_PANTHOR_TIMESTAMP_DURATION)
+
 static int panthor_query_timestamp_info(struct panthor_device *ptdev,
 					struct drm_panthor_timestamp_info *arg)
 {
 	int ret;
+	u32 flags;
+	unsigned long irq_flags;
+	struct timespec64 cpu_ts;
+	u64 query_start_time;
+	bool minimize_interruption;
+	u32 timestamp_types = 0;
+
+	if (arg->flags != 0) {
+		flags = arg->flags;
+	} else {
+		/*
+		 * If flags are 0, then ask for the same things that we asked
+		 * for before flags were added.
+		 */
+		flags = DRM_PANTHOR_TIMESTAMP_GPU |
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+			DRM_PANTHOR_TIMESTAMP_FREQ;
+	}
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case 0:

Umm, this should be DRM_PANTHOR_TIMESTAMP_CPU_NONE.

OK, are you going to re-spin?

Quoted 27 lines
Quoted 27 lines
Quoted 15 lines
Quoted 14 lines

Review

Quoted 12 lines
(quoted diff) +11 -0
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		timestamp_types++;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	if (flags & ~VALID_TIMESTAMP_QUERY_FLAGS)
+		return -EINVAL;

Can we move this check before the switch and simplify the switch itself to only do the timestamp_types increment?

Quoted 2 lines

DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK is bit field that holds individual clock type values, so we still need to validate the bit field.

The if () test eliminates the default case, and if you change the switch to:

	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_NONE) {
	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
		timestamp_types++;
		break;
	}
Quoted 6 lines
Quoted 1 line

then it should be equivalent, right?

We need the default case to detect garbage values in the part of flags that ands with DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK.

DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK is 7 << 1,

I understand that you want to make sure that user space doesn't insert values into the flags and then pretends that because we didn't return an error that's now part of the ABI. But then maybe you should not reserve the extra bit now if you don't know how it's going to be used (in other words, why not make TIMESTAMP_CPU_TYPE_MASK 3 << 1?).

Quoted 4 lines

DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC is 1 << 1, DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW is 2 << 1,
so 3 << 1, 4 << 1, 5 << 1, 6 << 1, 7 << 1 are all invalid values that need to be rejected.

I am confused about why TIMESTAMP_CPU is a continous range [0-7] while the rest of the flags are bits in a bitmask. A note should be added to the panthor_drm.h file to explain this.

Quoted 3 lines

And since DRM_PANTHOR_TIMESTAMP_CPU_NONE is 0 << 1, we need it too in the switch to not be caught by the default case.

It only makes sense if you explain that TIMESTAMP_CPU values are in a range. I kept reading the different versions of the patch thinking that they are bits in a bitmask, with TIMESTAMP_CPU_NONE being the logical state of not selecting any of the sources.

Best regards,
Liviu

Quoted 53 lines
Quoted 53 lines
Quoted 26 lines
Quoted 18 lines
Quoted 4 lines
(quoted diff) +2 -0
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)

Review

Quoted 10 lines
(quoted diff) +10 -0
+		timestamp_types++;
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		timestamp_types++;
+
+	/* If user asked to obtain timestamps from more than one source,
+	 * then it very likely means they want them to be as close as possible.
+	 * If they asked for duration, then that likely means that they
+	 * want to know how long obtaining timestamp takes, without random
+	 * events, like process scheduling or interrupts.
+	 */

This comment makes me think that user can ask for both CPU_MONOTONIC and CPU_MONOTONIC_RAW timestamps, but the code is built to make them exclusive. Can we document better what sources can be requested simultaneously?

Somethine like this?

include/uapi/drm/panthor_drm.h +0 -0
index 8a46ef040c3d..0e455d91e77d 100644

include/uapi/drm/panthor_drm.h struct drm_panthor_timestamp_info { · line 473

Quoted 8 lines
include/uapi/drm/panthor_drm.h +5 -0
@@ -466,6 +466,11 @@ struct drm_panthor_timestamp_info {
         *  DRM_PANTHOR_TIMESTAMP_GPU |
         *  DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
         *  DRM_PANTHOR_TIMESTAMP_FREQ
+        *
+        * Note: these flags are exclusive to each other (only one can be used):
+        * - DRM_PANTHOR_TIMESTAMP_CPU_NONE
+        * - DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC
+        * - DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW

Yes, looks good to me.

Best regards,
Liviu

Quoted 3 lines
include/uapi/drm/panthor_drm.h +0 -0
         */
        __u32 flags;
 
Signature · 8 lines
====================
| I would like to |
| fix the world,  |
| but they're not |
| giving me the   |
 \ source code!  /
  ---------------
    ¯\_(ツ)_/¯
Signature · 8 lines
====================
| I would like to |
| fix the world,  |
| but they're not |
| giving me the   |
 \ source code!  /
  ---------------
    ¯\_(ツ)_/¯
#17 lore raw
Quoted 44 lines · Liviu Dudau
Quoted 34 lines · Marcin Ślusarz
Quoted 33 lines · Liviu Dudau
Quoted 32 lines · Marcin Ślusarz
Quoted 31 lines · Liviu Dudau

Hi Marcin,

Quoted 28 lines · Marcin Slusarz

...

(quoted diff) +23 -0
+#define VALID_TIMESTAMP_QUERY_FLAGS \
+		(DRM_PANTHOR_TIMESTAMP_GPU | \
+		 DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_OFFSET | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT | \
+		 DRM_PANTHOR_TIMESTAMP_FREQ | \
+		 DRM_PANTHOR_TIMESTAMP_DURATION)
+
 static int panthor_query_timestamp_info(struct panthor_device *ptdev,
 					struct drm_panthor_timestamp_info *arg)
 {
 	int ret;
+	u32 flags;
+	unsigned long irq_flags;
+	struct timespec64 cpu_ts;
+	u64 query_start_time;
+	bool minimize_interruption;
+	u32 timestamp_types = 0;
+
+	if (arg->flags != 0) {
+		flags = arg->flags;
+	} else {
+		/*
+		 * If flags are 0, then ask for the same things that we asked
+		 * for before flags were added.
+		 */
+		flags = DRM_PANTHOR_TIMESTAMP_GPU |
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+			DRM_PANTHOR_TIMESTAMP_FREQ;
+	}
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case 0:

Umm, this should be DRM_PANTHOR_TIMESTAMP_CPU_NONE.

Quoted 1 line

OK, are you going to re-spin?

Yes, as soon as we get to some conclusion.

Quoted 32 lines
Quoted 32 lines
Quoted 20 lines
Quoted 15 lines
Quoted 14 lines

Review

Quoted 12 lines
(quoted diff) +11 -0
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		timestamp_types++;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	if (flags & ~VALID_TIMESTAMP_QUERY_FLAGS)
+		return -EINVAL;

Can we move this check before the switch and simplify the switch itself to only do the timestamp_types increment?

Quoted 2 lines

DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK is bit field that holds individual clock type values, so we still need to validate the bit field.

The if () test eliminates the default case, and if you change the switch to:

Quoted 8 lines
	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_NONE) {
	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
		timestamp_types++;
		break;
	}

then it should be equivalent, right?

We need the default case to detect garbage values in the part of flags that ands with DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK.

Quoted 6 lines
Quoted 1 line

DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK is 7 << 1,

I understand that you want to make sure that user space doesn't insert values into the flags and then pretends that because we didn't return an error that's now part of the ABI. But then maybe you should not reserve the extra bit now if you don't know how it's going to be used (in other words, why not make TIMESTAMP_CPU_TYPE_MASK 3 << 1?).

I think it makes sense to reserve this bit now to make it easier to extend the interface if we ever will need to. Changing uapi definition is painful enough, so reserving this will rule out misuses of the current value. Another point is that we would have to validate that the bit is 0 anyway, so I don't see why we can't include that bit in the field that is supposed to go with.

Quoted 8 lines
Quoted 4 lines

DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC is 1 << 1, DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW is 2 << 1,
so 3 << 1, 4 << 1, 5 << 1, 6 << 1, 7 << 1 are all invalid values that need to be rejected.

I am confused about why TIMESTAMP_CPU is a continous range [0-7] while the rest of the flags are bits in a bitmask. A note should be added to the panthor_drm.h file to explain this.

Well, just look at the definition of struct drm_panthor_timestamp_info - there's one to one relation between field and a DRM_PANTHOR_TIMESTAMP_* flag, with an exception that CPU timestamp types have only one field (well, a pair of "seconds" and "nanoseconds", but from logical perspective it's only one value). There's no known reason why anyone would want to query multiple CPU timestamps together with various GPU counters from _GPU_ query.

Quoted 8 lines
Quoted 4 lines

And since DRM_PANTHOR_TIMESTAMP_CPU_NONE is 0 << 1, we need it too in the switch to not be caught by the default case.

It only makes sense if you explain that TIMESTAMP_CPU values are in a range. I kept reading the different versions of the patch thinking that they are bits in a bitmask, with TIMESTAMP_CPU_NONE being the logical state of not selecting any of the sources.

That should be resolved with the documentation update below, right?

Quoted 48 lines

Best regards,
Liviu

Quoted 43 lines
Quoted 43 lines
Quoted 26 lines
Quoted 18 lines
Quoted 4 lines
(quoted diff) +2 -0
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)

Review

Quoted 10 lines
(quoted diff) +10 -0
+		timestamp_types++;
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		timestamp_types++;
+
+	/* If user asked to obtain timestamps from more than one source,
+	 * then it very likely means they want them to be as close as possible.
+	 * If they asked for duration, then that likely means that they
+	 * want to know how long obtaining timestamp takes, without random
+	 * events, like process scheduling or interrupts.
+	 */

This comment makes me think that user can ask for both CPU_MONOTONIC and CPU_MONOTONIC_RAW timestamps, but the code is built to make them exclusive. Can we document better what sources can be requested simultaneously?

Somethine like this?

include/uapi/drm/panthor_drm.h +0 -0
index 8a46ef040c3d..0e455d91e77d 100644

include/uapi/drm/panthor_drm.h struct drm_panthor_timestamp_info { · line 473

Quoted 8 lines
include/uapi/drm/panthor_drm.h +5 -0
@@ -466,6 +466,11 @@ struct drm_panthor_timestamp_info {
         *  DRM_PANTHOR_TIMESTAMP_GPU |
         *  DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
         *  DRM_PANTHOR_TIMESTAMP_FREQ
+        *
+        * Note: these flags are exclusive to each other (only one can be used):
+        * - DRM_PANTHOR_TIMESTAMP_CPU_NONE
+        * - DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC
+        * - DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW

Yes, looks good to me.

Best regards,
Liviu

Quoted 3 lines
include/uapi/drm/panthor_drm.h +0 -0
         */
        __u32 flags;
 
maintainer
#18 lore raw
Quoted 88 lines · Marcin Ślusarz
Quoted 44 lines · Liviu Dudau
Quoted 34 lines · Marcin Ślusarz
Quoted 33 lines · Liviu Dudau
Quoted 32 lines · Marcin Ślusarz
Quoted 31 lines · Liviu Dudau

Hi Marcin,

Quoted 28 lines · Marcin Slusarz

...

(quoted diff) +23 -0
+#define VALID_TIMESTAMP_QUERY_FLAGS \
+		(DRM_PANTHOR_TIMESTAMP_GPU | \
+		 DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_OFFSET | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT | \
+		 DRM_PANTHOR_TIMESTAMP_FREQ | \
+		 DRM_PANTHOR_TIMESTAMP_DURATION)
+
 static int panthor_query_timestamp_info(struct panthor_device *ptdev,
 					struct drm_panthor_timestamp_info *arg)
 {
 	int ret;
+	u32 flags;
+	unsigned long irq_flags;
+	struct timespec64 cpu_ts;
+	u64 query_start_time;
+	bool minimize_interruption;
+	u32 timestamp_types = 0;
+
+	if (arg->flags != 0) {
+		flags = arg->flags;
+	} else {
+		/*
+		 * If flags are 0, then ask for the same things that we asked
+		 * for before flags were added.
+		 */
+		flags = DRM_PANTHOR_TIMESTAMP_GPU |
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+			DRM_PANTHOR_TIMESTAMP_FREQ;
+	}
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case 0:

Umm, this should be DRM_PANTHOR_TIMESTAMP_CPU_NONE.

Quoted 1 line

OK, are you going to re-spin?

Yes, as soon as we get to some conclusion.

Quoted 32 lines
Quoted 32 lines
Quoted 20 lines
Quoted 15 lines
Quoted 14 lines

Review

Quoted 12 lines
(quoted diff) +11 -0
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		timestamp_types++;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	if (flags & ~VALID_TIMESTAMP_QUERY_FLAGS)
+		return -EINVAL;

Can we move this check before the switch and simplify the switch itself to only do the timestamp_types increment?

Quoted 2 lines

DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK is bit field that holds individual clock type values, so we still need to validate the bit field.

The if () test eliminates the default case, and if you change the switch to:

Quoted 8 lines
	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_NONE) {
	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
		timestamp_types++;
		break;
	}

then it should be equivalent, right?

We need the default case to detect garbage values in the part of flags that ands with DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK.

Quoted 6 lines
Quoted 1 line

DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK is 7 << 1,

I understand that you want to make sure that user space doesn't insert values into the flags and then pretends that because we didn't return an error that's now part of the ABI. But then maybe you should not reserve the extra bit now if you don't know how it's going to be used (in other words, why not make TIMESTAMP_CPU_TYPE_MASK 3 << 1?).

Quoted 6 lines

I think it makes sense to reserve this bit now to make it easier to extend the interface if we ever will need to. Changing uapi definition is painful enough, so reserving this will rule out misuses of the current value. Another point is that we would have to validate that the bit is 0 anyway, so I don't see why we can't include that bit in the field that is supposed to go with.

You do validate the bits in the if (flags & ~VALID_TIMESTAMP_QUERY_FLAGS) test if the QUERY_FLAGS bitfield is updated.

Quoted 10 lines
Quoted 9 lines
Quoted 5 lines

DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC is 1 << 1, DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW is 2 << 1,
so 3 << 1, 4 << 1, 5 << 1, 6 << 1, 7 << 1 are all invalid values that need to be rejected.

I am confused about why TIMESTAMP_CPU is a continous range [0-7] while the rest of the flags are bits in a bitmask. A note should be added to the panthor_drm.h file to explain this.

Quoted 6 lines

Well, just look at the definition of struct drm_panthor_timestamp_info - there's one to one relation between field and a DRM_PANTHOR_TIMESTAMP_* flag, with an exception that CPU timestamp types have only one field (well, a pair of "seconds" and "nanoseconds", but from logical perspective it's only one value). There's no known reason why anyone would want to query multiple CPU timestamps together with various GPU counters from _GPU_ query.

OK, so what I'm hearing is: we're reserving space for up to 6 CPU clock sources (zero is no CPU clock) but you can have only one active at any time (because we can't think of any reason why you would want more at the same time) and we currently only define two sources because that's what user space cares about.

If you agree with this then this will be recorded in the thread and we can go back in the future and reference it.

Quoted 11 lines
Quoted 9 lines
Quoted 5 lines

And since DRM_PANTHOR_TIMESTAMP_CPU_NONE is 0 << 1, we need it too in the switch to not be caught by the default case.

It only makes sense if you explain that TIMESTAMP_CPU values are in a range. I kept reading the different versions of the patch thinking that they are bits in a bitmask, with TIMESTAMP_CPU_NONE being the logical state of not selecting any of the sources.

That should be resolved with the documentation update below, right?

The documentation doesn't say anything about the intent for future values for CPU sources, but if you don't want to add more in the documentation at least we have this thread as reference.

I don't want to turn this into a bikeshedding thread, so please do a v4 with the switch case updated and the documentation addition and I will ACK that.

Best regards,
Liviu

Quoted 49 lines
Quoted 49 lines

Best regards,
Liviu

Quoted 43 lines
Quoted 43 lines
Quoted 26 lines
Quoted 18 lines
Quoted 4 lines
(quoted diff) +2 -0
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)

Review

Quoted 10 lines
(quoted diff) +10 -0
+		timestamp_types++;
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		timestamp_types++;
+
+	/* If user asked to obtain timestamps from more than one source,
+	 * then it very likely means they want them to be as close as possible.
+	 * If they asked for duration, then that likely means that they
+	 * want to know how long obtaining timestamp takes, without random
+	 * events, like process scheduling or interrupts.
+	 */

This comment makes me think that user can ask for both CPU_MONOTONIC and CPU_MONOTONIC_RAW timestamps, but the code is built to make them exclusive. Can we document better what sources can be requested simultaneously?

Somethine like this?

include/uapi/drm/panthor_drm.h +0 -0
index 8a46ef040c3d..0e455d91e77d 100644

include/uapi/drm/panthor_drm.h struct drm_panthor_timestamp_info { · line 473

Quoted 8 lines
include/uapi/drm/panthor_drm.h +5 -0
@@ -466,6 +466,11 @@ struct drm_panthor_timestamp_info {
         *  DRM_PANTHOR_TIMESTAMP_GPU |
         *  DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
         *  DRM_PANTHOR_TIMESTAMP_FREQ
+        *
+        * Note: these flags are exclusive to each other (only one can be used):
+        * - DRM_PANTHOR_TIMESTAMP_CPU_NONE
+        * - DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC
+        * - DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW

Yes, looks good to me.

Best regards,
Liviu

Quoted 3 lines
include/uapi/drm/panthor_drm.h +0 -0
         */
        __u32 flags;
 
Signature · 8 lines
====================
| I would like to |
| fix the world,  |
| but they're not |
| giving me the   |
 \ source code!  /
  ---------------
    ¯\_(ツ)_/¯
#19 lore raw
Quoted 116 lines · Liviu Dudau
Quoted 88 lines · Marcin Ślusarz
Quoted 44 lines · Liviu Dudau
Quoted 34 lines · Marcin Ślusarz
Quoted 33 lines · Liviu Dudau
Quoted 32 lines · Marcin Ślusarz
Quoted 31 lines · Liviu Dudau

Hi Marcin,

Quoted 28 lines · Marcin Slusarz

...

(quoted diff) +23 -0
+#define VALID_TIMESTAMP_QUERY_FLAGS \
+		(DRM_PANTHOR_TIMESTAMP_GPU | \
+		 DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_OFFSET | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT | \
+		 DRM_PANTHOR_TIMESTAMP_FREQ | \
+		 DRM_PANTHOR_TIMESTAMP_DURATION)
+
 static int panthor_query_timestamp_info(struct panthor_device *ptdev,
 					struct drm_panthor_timestamp_info *arg)
 {
 	int ret;
+	u32 flags;
+	unsigned long irq_flags;
+	struct timespec64 cpu_ts;
+	u64 query_start_time;
+	bool minimize_interruption;
+	u32 timestamp_types = 0;
+
+	if (arg->flags != 0) {
+		flags = arg->flags;
+	} else {
+		/*
+		 * If flags are 0, then ask for the same things that we asked
+		 * for before flags were added.
+		 */
+		flags = DRM_PANTHOR_TIMESTAMP_GPU |
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+			DRM_PANTHOR_TIMESTAMP_FREQ;
+	}
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case 0:

Umm, this should be DRM_PANTHOR_TIMESTAMP_CPU_NONE.

Quoted 1 line

OK, are you going to re-spin?

Yes, as soon as we get to some conclusion.

Quoted 32 lines
Quoted 32 lines
Quoted 20 lines
Quoted 15 lines
Quoted 14 lines

Review

Quoted 12 lines
(quoted diff) +11 -0
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		timestamp_types++;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	if (flags & ~VALID_TIMESTAMP_QUERY_FLAGS)
+		return -EINVAL;

Can we move this check before the switch and simplify the switch itself to only do the timestamp_types increment?

Quoted 2 lines

DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK is bit field that holds individual clock type values, so we still need to validate the bit field.

The if () test eliminates the default case, and if you change the switch to:

Quoted 8 lines
	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_NONE) {
	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
		timestamp_types++;
		break;
	}

then it should be equivalent, right?

We need the default case to detect garbage values in the part of flags that ands with DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK.

Quoted 6 lines
Quoted 1 line

DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK is 7 << 1,

I understand that you want to make sure that user space doesn't insert values into the flags and then pretends that because we didn't return an error that's now part of the ABI. But then maybe you should not reserve the extra bit now if you don't know how it's going to be used (in other words, why not make TIMESTAMP_CPU_TYPE_MASK 3 << 1?).

Quoted 6 lines

I think it makes sense to reserve this bit now to make it easier to extend the interface if we ever will need to. Changing uapi definition is painful enough, so reserving this will rule out misuses of the current value. Another point is that we would have to validate that the bit is 0 anyway, so I don't see why we can't include that bit in the field that is supposed to go with.

You do validate the bits in the if (flags & ~VALID_TIMESTAMP_QUERY_FLAGS) test if the QUERY_FLAGS bitfield is updated.

Quoted 10 lines
Quoted 9 lines
Quoted 5 lines

DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC is 1 << 1, DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW is 2 << 1,
so 3 << 1, 4 << 1, 5 << 1, 6 << 1, 7 << 1 are all invalid values that need to be rejected.

I am confused about why TIMESTAMP_CPU is a continous range [0-7] while the rest of the flags are bits in a bitmask. A note should be added to the panthor_drm.h file to explain this.

Quoted 6 lines

Well, just look at the definition of struct drm_panthor_timestamp_info - there's one to one relation between field and a DRM_PANTHOR_TIMESTAMP_* flag, with an exception that CPU timestamp types have only one field (well, a pair of "seconds" and "nanoseconds", but from logical perspective it's only one value). There's no known reason why anyone would want to query multiple CPU timestamps together with various GPU counters from _GPU_ query.

Quoted 7 lines

OK, so what I'm hearing is: we're reserving space for up to 6 CPU clock sources (zero is no CPU clock) but you can have only one active at any time (because we can't think of any reason why you would want more at the same time) and we currently only define two sources because that's what user space cares about.

If you agree with this then this will be recorded in the thread and we can go back in the future and reference it.

Up to 7 CPU clock sources and 0 as no CPU clock, but yes, this is the intent.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

And since DRM_PANTHOR_TIMESTAMP_CPU_NONE is 0 << 1, we need it too in the switch to not be caught by the default case.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

It only makes sense if you explain that TIMESTAMP_CPU values are in a range. I kept reading the different versions of the patch thinking that they are bits in a bitmask, with TIMESTAMP_CPU_NONE being the logical state of not selecting any of the sources.

That should be resolved with the documentation update below, right?

The documentation doesn't say anything about the intent for future values for CPU sources, but if you don't want to add more in the documentation at least we have this thread as reference.

It's not that I don't want to add more documentation, I just don't understand what else needs to be explained.

Quoted 3 lines

I don't want to turn this into a bikeshedding thread, so please do a v4 with the switch case updated and the documentation addition and I will ACK that.

Done

#20 lore raw

PATCH v4 1/1 drm/panthor: extend timestamp query with flags

In reply to Marcin Slusarz

Flags now control which data user space wants to query, there is more information sources, and there's ability to query duration of multiple timestamp reads.

New sources:
- CPU's monotonic,
- CPU's monotonic raw,
- GPU's cycle count

These changes should make the implementation of VK_KHR_calibrated_timestamps more accurate and much simpler.

---
Changes in v4:
- 0 replaced by DRM_PANTHOR_TIMESTAMP_CPU_NONE in CPU_TYPE_MASK switch
- extended documentation for flags
Changes in v3:
- {} around multiline statements
- added comment about default flags
- added VALID_TIMESTAMP_QUERY_FLAGS
- used u64_to_user_ptr
Changes in v2:
- added DRM_PANTHOR_TIMESTAMP_CPU_NONE
- cpu_timestamp_nsec extended to u64
- pad1 removed
- copy_from_user -> copy_struct_from_user
- looking at size replaced by looking at flags
- more comments

---
 drivers/gpu/drm/panthor/panthor_drv.c | 134 ++++++++++++++++++++++++--
 include/uapi/drm/panthor_drm.h        |  63 +++++++++++-
 2 files changed, 189 insertions(+), 8 deletions(-)
drivers/gpu/drm/panthor/panthor_drv.c +128 -6
index 165dddfde6ca..9e65af453119 100644
@@ -13,7 +13,9 @@
 #include <linux/pagemap.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
+#include <linux/sched/clock.h>
 #include <linux/time64.h>
+#include <linux/time_namespace.h>
 
 #include <drm/drm_auth.h>
 #include <drm/drm_debugfs.h>
@@ -761,22 +763,135 @@ static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx,
 	kvfree(ctx->jobs);
 }
 
+#define VALID_TIMESTAMP_QUERY_FLAGS \
+		(DRM_PANTHOR_TIMESTAMP_GPU | \
+		 DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_OFFSET | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT | \
+		 DRM_PANTHOR_TIMESTAMP_FREQ | \
+		 DRM_PANTHOR_TIMESTAMP_DURATION)
+
 static int panthor_query_timestamp_info(struct panthor_device *ptdev,
 					struct drm_panthor_timestamp_info *arg)
 {
 	int ret;
+	u32 flags;
+	unsigned long irq_flags;
+	struct timespec64 cpu_ts;
+	u64 query_start_time;
+	bool minimize_interruption;
+	u32 timestamp_types = 0;
+
+	if (arg->flags != 0) {
+		flags = arg->flags;
+	} else {
+		/*
+		 * If flags are 0, then ask for the same things that we asked
+		 * for before flags were added.
+		 */
+		flags = DRM_PANTHOR_TIMESTAMP_GPU |
+			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+			DRM_PANTHOR_TIMESTAMP_FREQ;
+	}
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case DRM_PANTHOR_TIMESTAMP_CPU_NONE:
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		timestamp_types++;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	if (flags & ~VALID_TIMESTAMP_QUERY_FLAGS)
+		return -EINVAL;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		timestamp_types++;
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		timestamp_types++;
+
+	/* If user asked to obtain timestamps from more than one source,
+	 * then it very likely means they want them to be as close as possible.
+	 * If they asked for duration, then that likely means that they
+	 * want to know how long obtaining timestamp takes, without random
+	 * events, like process scheduling or interrupts.
+	 */
+	minimize_interruption =
+		(flags & DRM_PANTHOR_TIMESTAMP_DURATION) ||
+		(timestamp_types >= 2);
 
 	ret = panthor_device_resume_and_get(ptdev);
 	if (ret)
 		return ret;
 
+	if (flags & DRM_PANTHOR_TIMESTAMP_FREQ) {
 #ifdef CONFIG_ARM_ARCH_TIMER
-	arg->timestamp_frequency = arch_timer_get_cntfrq();
+		arg->timestamp_frequency = arch_timer_get_cntfrq();
 #else
-	arg->timestamp_frequency = 0;
+		arg->timestamp_frequency = 0;
 #endif
-	arg->current_timestamp = gpu_read64_counter(ptdev, GPU_TIMESTAMP);
-	arg->timestamp_offset = gpu_read64(ptdev, GPU_TIMESTAMP_OFFSET);
+	} else {
+		arg->timestamp_frequency = 0;
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_OFFSET)
+		arg->timestamp_offset = gpu_read64(ptdev, GPU_TIMESTAMP_OFFSET);
+	else
+		arg->timestamp_offset = 0;
+
+	if (minimize_interruption) {
+		preempt_disable();
+		local_irq_save(irq_flags);
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_DURATION)
+		query_start_time = local_clock();
+	else
+		query_start_time = 0;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		arg->current_timestamp = gpu_read64_counter(ptdev, GPU_TIMESTAMP);
+	else
+		arg->current_timestamp = 0;
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+		ktime_get_ts64(&cpu_ts);
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		ktime_get_raw_ts64(&cpu_ts);
+		break;
+	default:
+		break;
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		arg->cycle_count = gpu_read64_counter(ptdev, GPU_CYCLE_COUNT);
+	else
+		arg->cycle_count = 0;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_DURATION)
+		arg->duration_nsec = local_clock() - query_start_time;
+	else
+		arg->duration_nsec = 0;
+
+	if (minimize_interruption) {
+		local_irq_restore(irq_flags);
+		preempt_enable();
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+		timens_add_monotonic(&cpu_ts);
+
+		arg->cpu_timestamp_sec = cpu_ts.tv_sec;
+		arg->cpu_timestamp_nsec = cpu_ts.tv_nsec;
+	} else {
+		arg->cpu_timestamp_sec = 0;
+		arg->cpu_timestamp_nsec = 0;
+	}
 
 	pm_runtime_put(ptdev->base.dev);
 	return 0;
@@ -851,8 +966,14 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d
 		return PANTHOR_UOBJ_SET(args->pointer, args->size, ptdev->csif_info);
 
 	case DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO:
-		ret = panthor_query_timestamp_info(ptdev, &timestamp_info);
+		ret = copy_struct_from_user(&timestamp_info,
+					    sizeof(timestamp_info),
+					    u64_to_user_ptr(args->pointer),
+					    args->size);
+		if (ret)
+			return ret;
 
+		ret = panthor_query_timestamp_info(ptdev, &timestamp_info);
 		if (ret)
 			return ret;
 
@@ -1680,6 +1801,7 @@ static void panthor_debugfs_init(struct drm_minor *minor)
  *       - adds DRM_IOCTL_PANTHOR_BO_SYNC ioctl
  *       - adds DRM_IOCTL_PANTHOR_BO_QUERY_INFO ioctl
  *       - adds drm_panthor_gpu_info::selected_coherency
+ * - 1.8 - extends DEV_QUERY_TIMESTAMP_INFO with flags
  */
 static const struct drm_driver panthor_drm_driver = {
 	.driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ |
@@ -1693,7 +1815,7 @@ static const struct drm_driver panthor_drm_driver = {
 	.name = "panthor",
 	.desc = "Panthor DRM driver",
 	.major = 1,
-	.minor = 7,
+	.minor = 8,
 
 	.gem_create_object = panthor_gem_create_object,
 	.gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table,
include/uapi/drm/panthor_drm.h +61 -2

78 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer Reviewed-by on v4 1/1
#21 lore raw
Quoted 9 lines · Marcin Slusarz

Flags now control which data user space wants to query, there is more information sources, and there's ability to query duration of multiple timestamp reads.

New sources:
- CPU's monotonic,
- CPU's monotonic raw,
- GPU's cycle count

Quoted 5 lines

These changes should make the implementation of VK_KHR_calibrated_timestamps more accurate and much simpler.

Best regards,
Liviu

Quoted 295 lines

---
Changes in v4:
- 0 replaced by DRM_PANTHOR_TIMESTAMP_CPU_NONE in CPU_TYPE_MASK switch
- extended documentation for flags
Changes in v3:
- {} around multiline statements
- added comment about default flags
- added VALID_TIMESTAMP_QUERY_FLAGS
- used u64_to_user_ptr
Changes in v2:
- added DRM_PANTHOR_TIMESTAMP_CPU_NONE
- cpu_timestamp_nsec extended to u64
- pad1 removed
- copy_from_user -> copy_struct_from_user
- looking at size replaced by looking at flags
- more comments

---
 drivers/gpu/drm/panthor/panthor_drv.c | 134 ++++++++++++++++++++++++--
 include/uapi/drm/panthor_drm.h        |  63 +++++++++++-
 2 files changed, 189 insertions(+), 8 deletions(-)
drivers/gpu/drm/panthor/panthor_drv.c +128 -6
index 165dddfde6ca..9e65af453119 100644
@@ -13,7 +13,9 @@
 #include <linux/pagemap.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
+#include <linux/sched/clock.h>
 #include <linux/time64.h>
+#include <linux/time_namespace.h>
 
 #include <drm/drm_auth.h>
 #include <drm/drm_debugfs.h>
@@ -761,22 +763,135 @@ static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx,
 	kvfree(ctx->jobs);
 }
 
+#define VALID_TIMESTAMP_QUERY_FLAGS \
+		(DRM_PANTHOR_TIMESTAMP_GPU | \
+		 DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_OFFSET | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT | \
+		 DRM_PANTHOR_TIMESTAMP_FREQ | \
+		 DRM_PANTHOR_TIMESTAMP_DURATION)
+
 static int panthor_query_timestamp_info(struct panthor_device *ptdev,
 					struct drm_panthor_timestamp_info *arg)
 {
 	int ret;
+	u32 flags;
+	unsigned long irq_flags;
+	struct timespec64 cpu_ts;
+	u64 query_start_time;
+	bool minimize_interruption;
+	u32 timestamp_types = 0;
+
+	if (arg->flags != 0) {
+		flags = arg->flags;
+	} else {
+		/*
+		 * If flags are 0, then ask for the same things that we asked
+		 * for before flags were added.
+		 */
+		flags = DRM_PANTHOR_TIMESTAMP_GPU |
+			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+			DRM_PANTHOR_TIMESTAMP_FREQ;
+	}
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case DRM_PANTHOR_TIMESTAMP_CPU_NONE:
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		timestamp_types++;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	if (flags & ~VALID_TIMESTAMP_QUERY_FLAGS)
+		return -EINVAL;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		timestamp_types++;
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		timestamp_types++;
+
+	/* If user asked to obtain timestamps from more than one source,
+	 * then it very likely means they want them to be as close as possible.
+	 * If they asked for duration, then that likely means that they
+	 * want to know how long obtaining timestamp takes, without random
+	 * events, like process scheduling or interrupts.
+	 */
+	minimize_interruption =
+		(flags & DRM_PANTHOR_TIMESTAMP_DURATION) ||
+		(timestamp_types >= 2);
 
 	ret = panthor_device_resume_and_get(ptdev);
 	if (ret)
 		return ret;
 
+	if (flags & DRM_PANTHOR_TIMESTAMP_FREQ) {
 #ifdef CONFIG_ARM_ARCH_TIMER
-	arg->timestamp_frequency = arch_timer_get_cntfrq();
+		arg->timestamp_frequency = arch_timer_get_cntfrq();
 #else
-	arg->timestamp_frequency = 0;
+		arg->timestamp_frequency = 0;
 #endif
-	arg->current_timestamp = gpu_read64_counter(ptdev, GPU_TIMESTAMP);
-	arg->timestamp_offset = gpu_read64(ptdev, GPU_TIMESTAMP_OFFSET);
+	} else {
+		arg->timestamp_frequency = 0;
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_OFFSET)
+		arg->timestamp_offset = gpu_read64(ptdev, GPU_TIMESTAMP_OFFSET);
+	else
+		arg->timestamp_offset = 0;
+
+	if (minimize_interruption) {
+		preempt_disable();
+		local_irq_save(irq_flags);
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_DURATION)
+		query_start_time = local_clock();
+	else
+		query_start_time = 0;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		arg->current_timestamp = gpu_read64_counter(ptdev, GPU_TIMESTAMP);
+	else
+		arg->current_timestamp = 0;
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+		ktime_get_ts64(&cpu_ts);
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		ktime_get_raw_ts64(&cpu_ts);
+		break;
+	default:
+		break;
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		arg->cycle_count = gpu_read64_counter(ptdev, GPU_CYCLE_COUNT);
+	else
+		arg->cycle_count = 0;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_DURATION)
+		arg->duration_nsec = local_clock() - query_start_time;
+	else
+		arg->duration_nsec = 0;
+
+	if (minimize_interruption) {
+		local_irq_restore(irq_flags);
+		preempt_enable();
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+		timens_add_monotonic(&cpu_ts);
+
+		arg->cpu_timestamp_sec = cpu_ts.tv_sec;
+		arg->cpu_timestamp_nsec = cpu_ts.tv_nsec;
+	} else {
+		arg->cpu_timestamp_sec = 0;
+		arg->cpu_timestamp_nsec = 0;
+	}
 
 	pm_runtime_put(ptdev->base.dev);
 	return 0;
@@ -851,8 +966,14 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d
 		return PANTHOR_UOBJ_SET(args->pointer, args->size, ptdev->csif_info);
 
 	case DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO:
-		ret = panthor_query_timestamp_info(ptdev, &timestamp_info);
+		ret = copy_struct_from_user(&timestamp_info,
+					    sizeof(timestamp_info),
+					    u64_to_user_ptr(args->pointer),
+					    args->size);
+		if (ret)
+			return ret;
 
+		ret = panthor_query_timestamp_info(ptdev, &timestamp_info);
 		if (ret)
 			return ret;
 
@@ -1680,6 +1801,7 @@ static void panthor_debugfs_init(struct drm_minor *minor)
  *       - adds DRM_IOCTL_PANTHOR_BO_SYNC ioctl
  *       - adds DRM_IOCTL_PANTHOR_BO_QUERY_INFO ioctl
  *       - adds drm_panthor_gpu_info::selected_coherency
+ * - 1.8 - extends DEV_QUERY_TIMESTAMP_INFO with flags
  */
 static const struct drm_driver panthor_drm_driver = {
 	.driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ |
@@ -1693,7 +1815,7 @@ static const struct drm_driver panthor_drm_driver = {
 	.name = "panthor",
 	.desc = "Panthor DRM driver",
 	.major = 1,
-	.minor = 7,
+	.minor = 8,
 
 	.gem_create_object = panthor_gem_create_object,
 	.gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table,
include/uapi/drm/panthor_drm.h +61 -2
index b401ac585d6a..0e455d91e77d 100644
@@ -409,6 +409,38 @@ struct drm_panthor_csif_info {
 	__u32 pad;
 };
 
+/**
+ * enum drm_panthor_timestamp_info_flags - drm_panthor_timestamp_info.flags
+ */
+enum drm_panthor_timestamp_info_flags {
+	/** @DRM_PANTHOR_TIMESTAMP_GPU: Query GPU time. */
+	DRM_PANTHOR_TIMESTAMP_GPU = 1 << 0,
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_NONE: Don't query CPU time. */
+	DRM_PANTHOR_TIMESTAMP_CPU_NONE = 0 << 1,
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC: Query CPU time using CLOCK_MONOTONIC. */
+	DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC = 1 << 1,
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW: Query CPU time using CLOCK_MONOTONIC_RAW. */
+	DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW = 2 << 1,
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK: Space reserved for CPU clock type. */
+	DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK = 7 << 1,
+
+	/** @DRM_PANTHOR_TIMESTAMP_GPU_OFFSET: Query GPU offset. */
+	DRM_PANTHOR_TIMESTAMP_GPU_OFFSET = 1 << 4,
+
+	/** @DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT: Query GPU cycle count. */
+	DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT = 1 << 5,
+
+	/** @DRM_PANTHOR_TIMESTAMP_FREQ: Query timestamp frequency. */
+	DRM_PANTHOR_TIMESTAMP_FREQ = 1 << 6,
+
+	/** @DRM_PANTHOR_TIMESTAMP_DURATION: Return duration of time query. */
+	DRM_PANTHOR_TIMESTAMP_DURATION = 1 << 7,
+};
+
 /**
  * struct drm_panthor_timestamp_info - Timestamp information
  *
@@ -421,11 +453,38 @@ struct drm_panthor_timestamp_info {
 	 */
 	__u64 timestamp_frequency;
 
-	/** @current_timestamp: The current timestamp. */
+	/** @current_timestamp: The current GPU timestamp. */
 	__u64 current_timestamp;
 
-	/** @timestamp_offset: The offset of the timestamp timer. */
+	/** @timestamp_offset: The offset of the GPU timestamp timer. */
 	__u64 timestamp_offset;
+
+	/**
+	 * @flags: Bitmask of drm_panthor_timestamp_info_flags.
+	 *
+	 * If set to 0, then it is interpreted as:
+	 *  DRM_PANTHOR_TIMESTAMP_GPU |
+	 *  DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+	 *  DRM_PANTHOR_TIMESTAMP_FREQ
+	 *
+	 * Note: these flags are exclusive to each other (only one can be used):
+	 * - DRM_PANTHOR_TIMESTAMP_CPU_NONE
+	 * - DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC
+	 * - DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW
+	 */
+	__u32 flags;
+
+	/** @duration_nsec: Duration of time query. */
+	__u32 duration_nsec;
+
+	/** @cycle_count: Value of GPU_CYCLE_COUNT. */
+	__u64 cycle_count;
+
+	/** @cpu_timestamp_sec: Seconds part of CPU timestamp. */
+	__u64 cpu_timestamp_sec;
+
+	/** @cpu_timestamp_nsec: Nanseconds part of CPU timestamp. */
+	__u64 cpu_timestamp_nsec;
 };
 
 /**
Signature · 1 line
2.34.1
Signature · 8 lines
====================
| I would like to |
| fix the world,  |
| but they're not |
| giving me the   |
 \ source code!  /
  ---------------
    ¯\_(ツ)_/¯
maintainer Applied
#22 lore raw
In reply to Marcin Slusarz
Quoted 9 lines · Marcin Slusarz

Flags now control which data user space wants to query, there is more information sources, and there's ability to query duration of multiple timestamp reads.

New sources:
- CPU's monotonic,
- CPU's monotonic raw,
- GPU's cycle count

Quoted 5 lines

These changes should make the implementation of VK_KHR_calibrated_timestamps more accurate and much simpler.

Pushed to drm-misc-next

Best regards,
Liviu

Quoted 295 lines

---
Changes in v4:
- 0 replaced by DRM_PANTHOR_TIMESTAMP_CPU_NONE in CPU_TYPE_MASK switch
- extended documentation for flags
Changes in v3:
- {} around multiline statements
- added comment about default flags
- added VALID_TIMESTAMP_QUERY_FLAGS
- used u64_to_user_ptr
Changes in v2:
- added DRM_PANTHOR_TIMESTAMP_CPU_NONE
- cpu_timestamp_nsec extended to u64
- pad1 removed
- copy_from_user -> copy_struct_from_user
- looking at size replaced by looking at flags
- more comments

---
 drivers/gpu/drm/panthor/panthor_drv.c | 134 ++++++++++++++++++++++++--
 include/uapi/drm/panthor_drm.h        |  63 +++++++++++-
 2 files changed, 189 insertions(+), 8 deletions(-)
drivers/gpu/drm/panthor/panthor_drv.c +128 -6
index 165dddfde6ca..9e65af453119 100644
@@ -13,7 +13,9 @@
 #include <linux/pagemap.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
+#include <linux/sched/clock.h>
 #include <linux/time64.h>
+#include <linux/time_namespace.h>
 
 #include <drm/drm_auth.h>
 #include <drm/drm_debugfs.h>
@@ -761,22 +763,135 @@ static void panthor_submit_ctx_cleanup(struct panthor_submit_ctx *ctx,
 	kvfree(ctx->jobs);
 }
 
+#define VALID_TIMESTAMP_QUERY_FLAGS \
+		(DRM_PANTHOR_TIMESTAMP_GPU | \
+		 DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_OFFSET | \
+		 DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT | \
+		 DRM_PANTHOR_TIMESTAMP_FREQ | \
+		 DRM_PANTHOR_TIMESTAMP_DURATION)
+
 static int panthor_query_timestamp_info(struct panthor_device *ptdev,
 					struct drm_panthor_timestamp_info *arg)
 {
 	int ret;
+	u32 flags;
+	unsigned long irq_flags;
+	struct timespec64 cpu_ts;
+	u64 query_start_time;
+	bool minimize_interruption;
+	u32 timestamp_types = 0;
+
+	if (arg->flags != 0) {
+		flags = arg->flags;
+	} else {
+		/*
+		 * If flags are 0, then ask for the same things that we asked
+		 * for before flags were added.
+		 */
+		flags = DRM_PANTHOR_TIMESTAMP_GPU |
+			DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+			DRM_PANTHOR_TIMESTAMP_FREQ;
+	}
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case DRM_PANTHOR_TIMESTAMP_CPU_NONE:
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		timestamp_types++;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	if (flags & ~VALID_TIMESTAMP_QUERY_FLAGS)
+		return -EINVAL;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		timestamp_types++;
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		timestamp_types++;
+
+	/* If user asked to obtain timestamps from more than one source,
+	 * then it very likely means they want them to be as close as possible.
+	 * If they asked for duration, then that likely means that they
+	 * want to know how long obtaining timestamp takes, without random
+	 * events, like process scheduling or interrupts.
+	 */
+	minimize_interruption =
+		(flags & DRM_PANTHOR_TIMESTAMP_DURATION) ||
+		(timestamp_types >= 2);
 
 	ret = panthor_device_resume_and_get(ptdev);
 	if (ret)
 		return ret;
 
+	if (flags & DRM_PANTHOR_TIMESTAMP_FREQ) {
 #ifdef CONFIG_ARM_ARCH_TIMER
-	arg->timestamp_frequency = arch_timer_get_cntfrq();
+		arg->timestamp_frequency = arch_timer_get_cntfrq();
 #else
-	arg->timestamp_frequency = 0;
+		arg->timestamp_frequency = 0;
 #endif
-	arg->current_timestamp = gpu_read64_counter(ptdev, GPU_TIMESTAMP);
-	arg->timestamp_offset = gpu_read64(ptdev, GPU_TIMESTAMP_OFFSET);
+	} else {
+		arg->timestamp_frequency = 0;
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_OFFSET)
+		arg->timestamp_offset = gpu_read64(ptdev, GPU_TIMESTAMP_OFFSET);
+	else
+		arg->timestamp_offset = 0;
+
+	if (minimize_interruption) {
+		preempt_disable();
+		local_irq_save(irq_flags);
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_DURATION)
+		query_start_time = local_clock();
+	else
+		query_start_time = 0;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU)
+		arg->current_timestamp = gpu_read64_counter(ptdev, GPU_TIMESTAMP);
+	else
+		arg->current_timestamp = 0;
+
+	switch (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC:
+		ktime_get_ts64(&cpu_ts);
+		break;
+	case DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW:
+		ktime_get_raw_ts64(&cpu_ts);
+		break;
+	default:
+		break;
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT)
+		arg->cycle_count = gpu_read64_counter(ptdev, GPU_CYCLE_COUNT);
+	else
+		arg->cycle_count = 0;
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_DURATION)
+		arg->duration_nsec = local_clock() - query_start_time;
+	else
+		arg->duration_nsec = 0;
+
+	if (minimize_interruption) {
+		local_irq_restore(irq_flags);
+		preempt_enable();
+	}
+
+	if (flags & DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK) {
+		timens_add_monotonic(&cpu_ts);
+
+		arg->cpu_timestamp_sec = cpu_ts.tv_sec;
+		arg->cpu_timestamp_nsec = cpu_ts.tv_nsec;
+	} else {
+		arg->cpu_timestamp_sec = 0;
+		arg->cpu_timestamp_nsec = 0;
+	}
 
 	pm_runtime_put(ptdev->base.dev);
 	return 0;
@@ -851,8 +966,14 @@ static int panthor_ioctl_dev_query(struct drm_device *ddev, void *data, struct d
 		return PANTHOR_UOBJ_SET(args->pointer, args->size, ptdev->csif_info);
 
 	case DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO:
-		ret = panthor_query_timestamp_info(ptdev, &timestamp_info);
+		ret = copy_struct_from_user(&timestamp_info,
+					    sizeof(timestamp_info),
+					    u64_to_user_ptr(args->pointer),
+					    args->size);
+		if (ret)
+			return ret;
 
+		ret = panthor_query_timestamp_info(ptdev, &timestamp_info);
 		if (ret)
 			return ret;
 
@@ -1680,6 +1801,7 @@ static void panthor_debugfs_init(struct drm_minor *minor)
  *       - adds DRM_IOCTL_PANTHOR_BO_SYNC ioctl
  *       - adds DRM_IOCTL_PANTHOR_BO_QUERY_INFO ioctl
  *       - adds drm_panthor_gpu_info::selected_coherency
+ * - 1.8 - extends DEV_QUERY_TIMESTAMP_INFO with flags
  */
 static const struct drm_driver panthor_drm_driver = {
 	.driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ |
@@ -1693,7 +1815,7 @@ static const struct drm_driver panthor_drm_driver = {
 	.name = "panthor",
 	.desc = "Panthor DRM driver",
 	.major = 1,
-	.minor = 7,
+	.minor = 8,
 
 	.gem_create_object = panthor_gem_create_object,
 	.gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table,
include/uapi/drm/panthor_drm.h +61 -2
index b401ac585d6a..0e455d91e77d 100644
@@ -409,6 +409,38 @@ struct drm_panthor_csif_info {
 	__u32 pad;
 };
 
+/**
+ * enum drm_panthor_timestamp_info_flags - drm_panthor_timestamp_info.flags
+ */
+enum drm_panthor_timestamp_info_flags {
+	/** @DRM_PANTHOR_TIMESTAMP_GPU: Query GPU time. */
+	DRM_PANTHOR_TIMESTAMP_GPU = 1 << 0,
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_NONE: Don't query CPU time. */
+	DRM_PANTHOR_TIMESTAMP_CPU_NONE = 0 << 1,
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC: Query CPU time using CLOCK_MONOTONIC. */
+	DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC = 1 << 1,
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW: Query CPU time using CLOCK_MONOTONIC_RAW. */
+	DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW = 2 << 1,
+
+	/** @DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK: Space reserved for CPU clock type. */
+	DRM_PANTHOR_TIMESTAMP_CPU_TYPE_MASK = 7 << 1,
+
+	/** @DRM_PANTHOR_TIMESTAMP_GPU_OFFSET: Query GPU offset. */
+	DRM_PANTHOR_TIMESTAMP_GPU_OFFSET = 1 << 4,
+
+	/** @DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT: Query GPU cycle count. */
+	DRM_PANTHOR_TIMESTAMP_GPU_CYCLE_COUNT = 1 << 5,
+
+	/** @DRM_PANTHOR_TIMESTAMP_FREQ: Query timestamp frequency. */
+	DRM_PANTHOR_TIMESTAMP_FREQ = 1 << 6,
+
+	/** @DRM_PANTHOR_TIMESTAMP_DURATION: Return duration of time query. */
+	DRM_PANTHOR_TIMESTAMP_DURATION = 1 << 7,
+};
+
 /**
  * struct drm_panthor_timestamp_info - Timestamp information
  *
@@ -421,11 +453,38 @@ struct drm_panthor_timestamp_info {
 	 */
 	__u64 timestamp_frequency;
 
-	/** @current_timestamp: The current timestamp. */
+	/** @current_timestamp: The current GPU timestamp. */
 	__u64 current_timestamp;
 
-	/** @timestamp_offset: The offset of the timestamp timer. */
+	/** @timestamp_offset: The offset of the GPU timestamp timer. */
 	__u64 timestamp_offset;
+
+	/**
+	 * @flags: Bitmask of drm_panthor_timestamp_info_flags.
+	 *
+	 * If set to 0, then it is interpreted as:
+	 *  DRM_PANTHOR_TIMESTAMP_GPU |
+	 *  DRM_PANTHOR_TIMESTAMP_GPU_OFFSET |
+	 *  DRM_PANTHOR_TIMESTAMP_FREQ
+	 *
+	 * Note: these flags are exclusive to each other (only one can be used):
+	 * - DRM_PANTHOR_TIMESTAMP_CPU_NONE
+	 * - DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC
+	 * - DRM_PANTHOR_TIMESTAMP_CPU_MONOTONIC_RAW
+	 */
+	__u32 flags;
+
+	/** @duration_nsec: Duration of time query. */
+	__u32 duration_nsec;
+
+	/** @cycle_count: Value of GPU_CYCLE_COUNT. */
+	__u64 cycle_count;
+
+	/** @cpu_timestamp_sec: Seconds part of CPU timestamp. */
+	__u64 cpu_timestamp_sec;
+
+	/** @cpu_timestamp_nsec: Nanseconds part of CPU timestamp. */
+	__u64 cpu_timestamp_nsec;
 };
 
 /**
Signature · 1 line
2.34.1
Signature · 8 lines
====================
| I would like to |
| fix the world,  |
| but they're not |
| giving me the   |
 \ source code!  /
  ---------------
    ¯\_(ツ)_/¯