Re: [PATCH v3 16/17] drm/v3d: Create a CPU job extension for the reset performance query job

2023-11-28 Thread Iago Toral
El lun, 27-11-2023 a las 15:48 -0300, Maíra Canal escribió:
(...)
> diff --git a/include/uapi/drm/v3d_drm.h b/include/uapi/drm/v3d_drm.h
> index a3ae1f220291..76a02d2c01e6 100644
> --- a/include/uapi/drm/v3d_drm.h
> +++ b/include/uapi/drm/v3d_drm.h
> @@ -76,6 +76,7 @@ struct drm_v3d_extension {
>  #define DRM_V3D_EXT_ID_CPU_TIMESTAMP_QUERY 0x03
>  #define DRM_V3D_EXT_ID_CPU_RESET_TIMESTAMP_QUERY   0x04
>  #define DRM_V3D_EXT_ID_CPU_COPY_TIMESTAMP_QUERY0x05
> +#define DRM_V3D_EXT_ID_CPU_RESET_PERFORMANCE_QUERY 0x06
> __u32 flags; /* mbz */
>  };
>  
> @@ -492,6 +493,32 @@ struct drm_v3d_copy_timestamp_query {
> __u64 syncs;
>  };
>  
> +/**
> + * struct drm_v3d_reset_performance_query - ioctl extension for the
> CPU job to
> + * reset performance queries
> + *
> + * When an extension DRM_V3D_EXT_ID_CPU_RESET_PERFORMANCE_QUERY is
> defined, it
> + * points to this extension to define a reset performance
> submission. This CPU
> + * job will reset the performance queries by resetting the values of
> the
> + * performance monitors. Moreover, it will reset the syncobj to
> reset query
> + * availability.
> + */
> +struct drm_v3d_reset_performance_query {
> +   struct drm_v3d_extension base;
> +
> +   /* Array of performance queries's syncobjs to indicate its
> availability */
> +   __u64 syncs;
> +
> +   /* Number of queries */
> +   __u32 count;
> +
> +   /* Number of performance monitors */
> +   __u32 nperfmons;
> +
> +   /* Array of u64 user-pointers that point to an array of
> kperfmon_ids */
> +   __u64 kperfmon_ids;
> +};
> +
>  struct drm_v3d_submit_cpu {
> /* Pointer to a u32 array of the BOs that are referenced by
> the job.
>  *
> @@ -507,6 +534,9 @@ struct drm_v3d_submit_cpu {
>  * For DRM_V3D_EXT_ID_CPU_COPY_TIMESTAMP_QUERY, it must
> contain two
>  * BOs. The first is the BO for which the timestamp queries
> results
>  * will be written to. The second is the BO that contains the
> timestamp.
> +    *
> +    * For DRM_V3D_EXT_ID_CPU_RESET_PERFORMANCE_QUERY, it must
> contain no
> +    * BOs.
>  */
(...) The first is the BO where the timestamps queries will be written.
(...)

Iago

> __u64 bo_handles;
>  



[PATCH v3 16/17] drm/v3d: Create a CPU job extension for the reset performance query job

2023-11-27 Thread Maíra Canal
A CPU job is a type of job that performs operations that requires CPU
intervention. A reset performance query job is a job that resets the
performance queries by resetting the values of the perfmons. Moreover,
we also reset the syncobjs related to the availability of the query.

So, create a user extension for the CPU job that enables the creation
of a reset performance job. This user extension will allow the creation of
a CPU job that resets the perfmons values and resets the availability syncobj.

Signed-off-by: Maíra Canal 
---
 drivers/gpu/drm/v3d/v3d_drv.h| 28 
 drivers/gpu/drm/v3d/v3d_sched.c  | 37 
 drivers/gpu/drm/v3d/v3d_submit.c | 73 
 include/uapi/drm/v3d_drm.h   | 30 +
 4 files changed, 168 insertions(+)

diff --git a/drivers/gpu/drm/v3d/v3d_drv.h b/drivers/gpu/drm/v3d/v3d_drv.h
index 5058a354fffd..0f7f80ad8d88 100644
--- a/drivers/gpu/drm/v3d/v3d_drv.h
+++ b/drivers/gpu/drm/v3d/v3d_drv.h
@@ -321,6 +321,7 @@ enum v3d_cpu_job_type {
V3D_CPU_JOB_TYPE_TIMESTAMP_QUERY,
V3D_CPU_JOB_TYPE_RESET_TIMESTAMP_QUERY,
V3D_CPU_JOB_TYPE_COPY_TIMESTAMP_QUERY,
+   V3D_CPU_JOB_TYPE_RESET_PERFORMANCE_QUERY,
 };
 
 struct v3d_timestamp_query {
@@ -331,6 +332,18 @@ struct v3d_timestamp_query {
struct drm_syncobj *syncobj;
 };
 
+/* Number of perfmons required to handle all supported performance counters */
+#define V3D_MAX_PERFMONS DIV_ROUND_UP(V3D_PERFCNT_NUM, \
+ DRM_V3D_MAX_PERF_COUNTERS)
+
+struct v3d_performance_query {
+   /* Performance monitor IDs for this query */
+   u32 kperfmon_ids[V3D_MAX_PERFMONS];
+
+   /* Syncobj that indicates the query availability */
+   struct drm_syncobj *syncobj;
+};
+
 struct v3d_indirect_csd_info {
/* Indirect CSD */
struct v3d_csd_job *job;
@@ -362,6 +375,19 @@ struct v3d_timestamp_query_info {
u32 count;
 };
 
+struct v3d_performance_query_info {
+   struct v3d_performance_query *queries;
+
+   /* Number of performance queries */
+   u32 count;
+
+   /* Number of performance monitors related to that query pool */
+   u32 nperfmons;
+
+   /* Number of performance counters related to that query pool */
+   u32 ncounters;
+};
+
 struct v3d_copy_query_results_info {
/* Define if should write to buffer using 64 or 32 bits */
bool do_64bit;
@@ -389,6 +415,8 @@ struct v3d_cpu_job {
struct v3d_timestamp_query_info timestamp_query;
 
struct v3d_copy_query_results_info copy;
+
+   struct v3d_performance_query_info performance_query;
 };
 
 typedef void (*v3d_cpu_job_fn)(struct v3d_cpu_job *);
diff --git a/drivers/gpu/drm/v3d/v3d_sched.c b/drivers/gpu/drm/v3d/v3d_sched.c
index 07c897cd3423..452c4a1db52e 100644
--- a/drivers/gpu/drm/v3d/v3d_sched.c
+++ b/drivers/gpu/drm/v3d/v3d_sched.c
@@ -78,6 +78,7 @@ v3d_cpu_job_free(struct drm_sched_job *sched_job)
 {
struct v3d_cpu_job *job = to_cpu_job(sched_job);
struct v3d_timestamp_query_info *timestamp_query = 
>timestamp_query;
+   struct v3d_performance_query_info *performance_query = 
>performance_query;
 
if (timestamp_query->queries) {
for (int i = 0; i < timestamp_query->count; i++)
@@ -85,6 +86,12 @@ v3d_cpu_job_free(struct drm_sched_job *sched_job)
kvfree(timestamp_query->queries);
}
 
+   if (performance_query->queries) {
+   for (int i = 0; i < performance_query->count; i++)
+   drm_syncobj_put(performance_query->queries[i].syncobj);
+   kvfree(performance_query->queries);
+   }
+
v3d_job_cleanup(>base);
 }
 
@@ -361,6 +368,7 @@ v3d_reset_timestamp_queries(struct v3d_cpu_job *job)
 
v3d_put_bo_vaddr(bo);
 }
+
 static void
 write_to_buffer(void *dst, u32 idx, bool do_64bit, u64 value)
 {
@@ -414,11 +422,40 @@ v3d_copy_query_results(struct v3d_cpu_job *job)
v3d_put_bo_vaddr(bo);
 }
 
+static void
+v3d_reset_performance_queries(struct v3d_cpu_job *job)
+{
+   struct v3d_performance_query_info *performance_query = 
>performance_query;
+   struct v3d_file_priv *v3d_priv = job->base.file->driver_priv;
+   struct v3d_dev *v3d = job->base.v3d;
+   struct v3d_perfmon *perfmon;
+
+   for (int i = 0; i < performance_query->count; i++) {
+   for (int j = 0; j < performance_query->nperfmons; j++) {
+   perfmon = v3d_perfmon_find(v3d_priv,
+  
performance_query->queries[i].kperfmon_ids[j]);
+   if (!perfmon) {
+   DRM_DEBUG("Failed to find perfmon.");
+   continue;
+   }
+
+   v3d_perfmon_stop(v3d, perfmon, false);
+
+   memset(perfmon->values, 0, perfmon->ncounters * 
sizeof(u64));
+
+