etnaviv: move generic perfmon functionality into own file
[mesa.git] / src / gallium / drivers / etnaviv / etnaviv_query_pm.c
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 * Christian Gmeiner <christian.gmeiner@gmail.com>
26 */
27
28 #include "util/u_memory.h"
29
30 #include "etnaviv_context.h"
31 #include "etnaviv_query_pm.h"
32 #include "etnaviv_screen.h"
33
34 static inline void
35 etna_pm_add_signal(struct etna_pm_query *pq, struct etna_perfmon *perfmon,
36 const struct etna_perfmon_config *cfg)
37 {
38 struct etna_perfmon_signal *signal = etna_pm_query_signal(perfmon, cfg->source);
39
40 pq->signal = signal;
41 }
42
43 static bool
44 realloc_query_bo(struct etna_context *ctx, struct etna_pm_query *pq)
45 {
46 if (pq->bo)
47 etna_bo_del(pq->bo);
48
49 pq->bo = etna_bo_new(ctx->screen->dev, 64, DRM_ETNA_GEM_CACHE_WC);
50 if (unlikely(!pq->bo))
51 return false;
52
53 pq->data = etna_bo_map(pq->bo);
54
55 return true;
56 }
57
58 static void
59 etna_pm_query_get(struct etna_cmd_stream *stream, struct etna_query *q,
60 unsigned flags)
61 {
62 struct etna_pm_query *pq = etna_pm_query(q);
63 unsigned offset;
64 assert(flags);
65
66 if (flags == ETNA_PM_PROCESS_PRE)
67 offset = 1;
68 else
69 offset = 2;
70
71 struct etna_perf p = {
72 .flags = flags,
73 .sequence = pq->sequence,
74 .bo = pq->bo,
75 .signal = pq->signal,
76 .offset = offset
77 };
78
79 etna_cmd_stream_perf(stream, &p);
80 }
81
82 static inline void
83 etna_pm_query_update(struct etna_query *q)
84 {
85 struct etna_pm_query *pq = etna_pm_query(q);
86
87 if (pq->data[0] == pq->sequence)
88 pq->ready = true;
89 }
90
91 static void
92 etna_pm_destroy_query(struct etna_context *ctx, struct etna_query *q)
93 {
94 struct etna_pm_query *pq = etna_pm_query(q);
95
96 etna_bo_del(pq->bo);
97 FREE(pq);
98 }
99
100 static bool
101 etna_pm_begin_query(struct etna_context *ctx, struct etna_query *q)
102 {
103 struct etna_pm_query *pq = etna_pm_query(q);
104
105 pq->ready = false;
106 pq->sequence++;
107
108 etna_pm_query_get(ctx->stream, q, ETNA_PM_PROCESS_PRE);
109
110 return true;
111 }
112
113 static void
114 etna_pm_end_query(struct etna_context *ctx, struct etna_query *q)
115 {
116 etna_pm_query_get(ctx->stream, q, ETNA_PM_PROCESS_POST);
117 }
118
119 static bool
120 etna_pm_get_query_result(struct etna_context *ctx, struct etna_query *q,
121 bool wait, union pipe_query_result *result)
122 {
123 struct etna_pm_query *pq = etna_pm_query(q);
124
125 etna_pm_query_update(q);
126
127 if (!pq->ready) {
128 if (!wait)
129 return false;
130
131 if (!etna_bo_cpu_prep(pq->bo, DRM_ETNA_PREP_READ))
132 return false;
133
134 pq->ready = true;
135 etna_bo_cpu_fini(pq->bo);
136 }
137
138 result->u32 = pq->data[2] - pq->data[1];
139
140 return true;
141 }
142
143 static const struct etna_query_funcs hw_query_funcs = {
144 .destroy_query = etna_pm_destroy_query,
145 .begin_query = etna_pm_begin_query,
146 .end_query = etna_pm_end_query,
147 .get_query_result = etna_pm_get_query_result,
148 };
149
150 struct etna_query *
151 etna_pm_create_query(struct etna_context *ctx, unsigned query_type)
152 {
153 struct etna_perfmon *perfmon = ctx->screen->perfmon;
154 const struct etna_perfmon_config *cfg;
155 struct etna_pm_query *pq;
156 struct etna_query *q;
157
158 cfg = etna_pm_query_config(query_type);
159 if (!cfg)
160 return NULL;
161
162 if (!etna_pm_cfg_supported(perfmon, cfg))
163 return NULL;
164
165 pq = CALLOC_STRUCT(etna_pm_query);
166 if (!pq)
167 return NULL;
168
169 if (!realloc_query_bo(ctx, pq)) {
170 FREE(pq);
171 return NULL;
172 }
173
174 q = &pq->base;
175 q->funcs = &hw_query_funcs;
176 q->type = query_type;
177
178 etna_pm_add_signal(pq, perfmon, cfg);
179
180 return q;
181 }