1 /*
2 * Copyright (c) 2017 Etnaviv Project
3 * Copyright (C) 2017 Zodiac Inflight Innovations
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Rob Clark <robclark@freedesktop.org>
26 * Christian Gmeiner <christian.gmeiner@gmail.com>
27 */
28
29 #include "util/u_memory.h"
30
31 #include "etnaviv_context.h"
32 #include "etnaviv_debug.h"
33 #include "etnaviv_emit.h"
34 #include "etnaviv_query_acc.h"
35
36 struct etna_pm_query
37 {
38 struct etna_acc_query base;
39
40 struct etna_perfmon_signal *signal;
41 unsigned sequence;
42 bool multiply_with_8;
43 };
44
45 static inline struct etna_pm_query *
etna_pm_query(struct etna_acc_query * aq)46 etna_pm_query(struct etna_acc_query *aq)
47 {
48 return (struct etna_pm_query *)aq;
49 }
50
51 static inline void
pm_add_signal(struct etna_pm_query * pq,struct etna_perfmon * perfmon,const struct etna_perfmon_config * cfg)52 pm_add_signal(struct etna_pm_query *pq, struct etna_perfmon *perfmon,
53 const struct etna_perfmon_config *cfg)
54 {
55 struct etna_perfmon_signal *signal = etna_pm_query_signal(perfmon, cfg->source);
56
57 pq->signal = signal;
58 }
59
60 static void
pm_query(struct etna_context * ctx,struct etna_acc_query * aq,unsigned flags)61 pm_query(struct etna_context *ctx, struct etna_acc_query *aq, unsigned flags)
62 {
63 struct etna_cmd_stream *stream = ctx->stream;
64 struct etna_pm_query *pq = etna_pm_query(aq);
65 unsigned offset;
66 assert(flags);
67
68 if (aq->samples > 127) {
69 aq->samples = 127;
70 BUG("samples overflow perfmon");
71 }
72
73 /* offset 0 is reserved for seq number */
74 offset = 1 + aq->samples;
75
76 pq->sequence++;
77
78 /* skip seq number of 0 as the buffer got zeroed out */
79 pq->sequence = MAX2(pq->sequence, 1);
80
81 struct etna_perf p = {
82 .flags = flags,
83 .sequence = pq->sequence,
84 .bo = etna_resource(aq->prsc)->bo,
85 .signal = pq->signal,
86 .offset = offset
87 };
88
89 etna_cmd_stream_perf(stream, &p);
90 resource_written(ctx, aq->prsc);
91
92 /* force a flush in !wait case in etna_acc_get_query_result(..) */
93 aq->no_wait_cnt = 10;
94 }
95
96 static bool
perfmon_supports(unsigned query_type)97 perfmon_supports(unsigned query_type)
98 {
99 return !!etna_pm_query_config(query_type);
100 }
101
102 static struct etna_acc_query *
perfmon_allocate(struct etna_context * ctx,unsigned query_type)103 perfmon_allocate(struct etna_context *ctx, unsigned query_type)
104 {
105 struct etna_pm_query *pq;
106 const struct etna_perfmon_config *cfg;
107
108 cfg = etna_pm_query_config(query_type);
109 if (!cfg)
110 return false;
111
112 if (!etna_pm_cfg_supported(ctx->screen->perfmon, cfg))
113 return false;
114
115 pq = CALLOC_STRUCT(etna_pm_query);
116 if (!pq)
117 return NULL;
118
119 pm_add_signal(pq, ctx->screen->perfmon, cfg);
120 pq->multiply_with_8 = cfg->multiply_with_8;
121
122 return &pq->base;
123 }
124
125 static void
perfmon_resume(struct etna_acc_query * aq,struct etna_context * ctx)126 perfmon_resume(struct etna_acc_query *aq, struct etna_context *ctx)
127 {
128 pm_query(ctx, aq, ETNA_PM_PROCESS_PRE);
129 }
130
131 static void
perfmon_suspend(struct etna_acc_query * aq,struct etna_context * ctx)132 perfmon_suspend(struct etna_acc_query *aq, struct etna_context *ctx)
133 {
134 pm_query(ctx, aq, ETNA_PM_PROCESS_POST);
135 }
136
137 static bool
perfmon_result(struct etna_acc_query * aq,void * buf,union pipe_query_result * result)138 perfmon_result(struct etna_acc_query *aq, void *buf,
139 union pipe_query_result *result)
140 {
141 const struct etna_pm_query *pq = etna_pm_query(aq);
142 uint32_t sum = 0;
143 uint32_t *ptr = (uint32_t *)buf;
144
145 /* check seq number */
146 if (pq->sequence > ptr[0])
147 return false;
148
149 /* jump over seq number */
150 ptr++;
151
152 assert(aq->samples % 2 == 0);
153
154 /* each pair has a start and end value */
155 for (unsigned i = 0; i < aq->samples; i += 2)
156 sum += *(ptr + i + 1) - *(ptr + i);
157
158 result->u32 = sum;
159
160 if (pq->multiply_with_8)
161 result->u32 *= 8;
162
163 return true;
164 }
165
166 const struct etna_acc_sample_provider perfmon_provider = {
167 .supports = perfmon_supports,
168 .allocate = perfmon_allocate,
169 .resume = perfmon_resume,
170 .suspend = perfmon_suspend,
171 .result = perfmon_result,
172 };
173