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 <[email protected]>
26 * Christian Gmeiner <[email protected]>
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 #define MAX_PERFMON_SAMPLES 1022 /* (4KB / 4Byte/sample) - 1 reserved seqno */
37
38 struct etna_pm_query
39 {
40 struct etna_acc_query base;
41
42 struct etna_perfmon_signal *signal;
43 unsigned sequence;
44 bool multiply_with_8;
45 };
46
47 static inline struct etna_pm_query *
etna_pm_query(struct etna_acc_query * aq)48 etna_pm_query(struct etna_acc_query *aq)
49 {
50 return (struct etna_pm_query *)aq;
51 }
52
53 static inline void
pm_add_signal(struct etna_pm_query * pq,struct etna_perfmon * perfmon,const struct etna_perfmon_config * cfg)54 pm_add_signal(struct etna_pm_query *pq, struct etna_perfmon *perfmon,
55 const struct etna_perfmon_config *cfg)
56 {
57 struct etna_perfmon_signal *signal = etna_pm_query_signal(perfmon, cfg->source);
58
59 pq->signal = signal;
60 }
61
62 static void
pm_query(struct etna_context * ctx,struct etna_acc_query * aq,unsigned flags)63 pm_query(struct etna_context *ctx, struct etna_acc_query *aq, unsigned flags)
64 {
65 struct etna_cmd_stream *stream = ctx->stream;
66 struct etna_pm_query *pq = etna_pm_query(aq);
67 unsigned offset;
68 assert(flags);
69
70 if (aq->samples > MAX_PERFMON_SAMPLES) {
71 aq->samples = MAX_PERFMON_SAMPLES;
72 BUG("samples overflow perfmon");
73 }
74
75 /* offset 0 is reserved for seq number */
76 offset = 1 + aq->samples;
77
78 pq->sequence++;
79
80 /* skip seq number of 0 as the buffer got zeroed out */
81 pq->sequence = MAX2(pq->sequence, 1);
82
83 struct etna_perf p = {
84 .flags = flags,
85 .sequence = pq->sequence,
86 .bo = etna_resource(aq->prsc)->bo,
87 .signal = pq->signal,
88 .offset = offset
89 };
90
91 etna_cmd_stream_perf(stream, &p);
92 resource_written(ctx, aq->prsc);
93 }
94
95 static bool
perfmon_supports(unsigned query_type)96 perfmon_supports(unsigned query_type)
97 {
98 return !!etna_pm_query_config(query_type);
99 }
100
101 static struct etna_acc_query *
perfmon_allocate(struct etna_context * ctx,unsigned query_type)102 perfmon_allocate(struct etna_context *ctx, unsigned query_type)
103 {
104 struct etna_pm_query *pq;
105 const struct etna_perfmon_config *cfg;
106
107 cfg = etna_pm_query_config(query_type);
108 if (!cfg)
109 return false;
110
111 if (!etna_pm_cfg_supported(ctx->screen->perfmon, cfg))
112 return false;
113
114 pq = CALLOC_STRUCT(etna_pm_query);
115 if (!pq)
116 return NULL;
117
118 pm_add_signal(pq, ctx->screen->perfmon, cfg);
119 pq->multiply_with_8 = cfg->multiply_with_8;
120
121 return &pq->base;
122 }
123
124 static void
perfmon_resume(struct etna_acc_query * aq,struct etna_context * ctx)125 perfmon_resume(struct etna_acc_query *aq, struct etna_context *ctx)
126 {
127 pm_query(ctx, aq, ETNA_PM_PROCESS_PRE);
128 aq->samples++;
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 aq->samples++;
136 }
137
138 static bool
perfmon_result(struct etna_acc_query * aq,void * buf,union pipe_query_result * result)139 perfmon_result(struct etna_acc_query *aq, void *buf,
140 union pipe_query_result *result)
141 {
142 const struct etna_pm_query *pq = etna_pm_query(aq);
143 uint32_t sum = 0;
144 uint32_t *ptr = (uint32_t *)buf;
145
146 /* check seq number */
147 if (pq->sequence > ptr[0])
148 return false;
149
150 /* jump over seq number */
151 ptr++;
152
153 assert(aq->samples % 2 == 0);
154
155 /* each pair has a start and end value */
156 for (unsigned i = 0; i < aq->samples; i += 2)
157 sum += *(ptr + i + 1) - *(ptr + i);
158
159 result->u32 = sum;
160
161 if (pq->multiply_with_8)
162 result->u32 *= 8;
163
164 return true;
165 }
166
167 const struct etna_acc_sample_provider perfmon_provider = {
168 .supports = perfmon_supports,
169 .allocate = perfmon_allocate,
170 .resume = perfmon_resume,
171 .suspend = perfmon_suspend,
172 .result = perfmon_result,
173 };
174