gallium/radeon: undef the very specific UPDATE_COUNTER macro
[mesa.git] / src / gallium / drivers / radeon / r600_gpu_load.c
1 /*
2 * Copyright 2015 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors: Marek Olšák <maraeo@gmail.com>
24 *
25 */
26
27 /* The GPU load is measured as follows.
28 *
29 * There is a thread which samples the GRBM_STATUS register at a certain
30 * frequency and the "busy" or "idle" counter is incremented based on
31 * whether the GUI_ACTIVE bit is set or not.
32 *
33 * Then, the user can sample the counters twice and calculate the average
34 * GPU load between the two samples.
35 */
36
37 #include "r600_pipe_common.h"
38 #include "r600_query.h"
39 #include "os/os_time.h"
40
41 /* For good accuracy at 1000 fps or lower. This will be inaccurate for higher
42 * fps (there are too few samples per frame). */
43 #define SAMPLES_PER_SEC 10000
44
45 #define GRBM_STATUS 0x8010
46 #define TA_BUSY(x) (((x) >> 14) & 0x1)
47 #define GDS_BUSY(x) (((x) >> 15) & 0x1)
48 #define VGT_BUSY(x) (((x) >> 17) & 0x1)
49 #define IA_BUSY(x) (((x) >> 19) & 0x1)
50 #define SX_BUSY(x) (((x) >> 20) & 0x1)
51 #define WD_BUSY(x) (((x) >> 21) & 0x1)
52 #define SPI_BUSY(x) (((x) >> 22) & 0x1)
53 #define BCI_BUSY(x) (((x) >> 23) & 0x1)
54 #define SC_BUSY(x) (((x) >> 24) & 0x1)
55 #define PA_BUSY(x) (((x) >> 25) & 0x1)
56 #define DB_BUSY(x) (((x) >> 26) & 0x1)
57 #define CP_BUSY(x) (((x) >> 29) & 0x1)
58 #define CB_BUSY(x) (((x) >> 30) & 0x1)
59 #define GUI_ACTIVE(x) (((x) >> 31) & 0x1)
60
61 #define UPDATE_COUNTER(field, mask) \
62 do { \
63 if (mask(value)) \
64 p_atomic_inc(&counters->named.field.busy); \
65 else \
66 p_atomic_inc(&counters->named.field.idle); \
67 } while (0)
68
69 static void r600_update_grbm_counters(struct r600_common_screen *rscreen,
70 union r600_grbm_counters *counters)
71 {
72 uint32_t value = 0;
73
74 rscreen->ws->read_registers(rscreen->ws, GRBM_STATUS, 1, &value);
75
76 UPDATE_COUNTER(ta, TA_BUSY);
77 UPDATE_COUNTER(gds, GDS_BUSY);
78 UPDATE_COUNTER(vgt, VGT_BUSY);
79 UPDATE_COUNTER(ia, IA_BUSY);
80 UPDATE_COUNTER(sx, SX_BUSY);
81 UPDATE_COUNTER(wd, WD_BUSY);
82 UPDATE_COUNTER(spi, SPI_BUSY);
83 UPDATE_COUNTER(bci, BCI_BUSY);
84 UPDATE_COUNTER(sc, SC_BUSY);
85 UPDATE_COUNTER(pa, PA_BUSY);
86 UPDATE_COUNTER(db, DB_BUSY);
87 UPDATE_COUNTER(cp, CP_BUSY);
88 UPDATE_COUNTER(cb, CB_BUSY);
89 UPDATE_COUNTER(gui, GUI_ACTIVE);
90 }
91
92 #undef UPDATE_COUNTER
93
94 static PIPE_THREAD_ROUTINE(r600_gpu_load_thread, param)
95 {
96 struct r600_common_screen *rscreen = (struct r600_common_screen*)param;
97 const int period_us = 1000000 / SAMPLES_PER_SEC;
98 int sleep_us = period_us;
99 int64_t cur_time, last_time = os_time_get();
100
101 while (!p_atomic_read(&rscreen->gpu_load_stop_thread)) {
102 if (sleep_us)
103 os_time_sleep(sleep_us);
104
105 /* Make sure we sleep the ideal amount of time to match
106 * the expected frequency. */
107 cur_time = os_time_get();
108
109 if (os_time_timeout(last_time, last_time + period_us,
110 cur_time))
111 sleep_us = MAX2(sleep_us - 1, 1);
112 else
113 sleep_us += 1;
114
115 /*printf("Hz: %.1f\n", 1000000.0 / (cur_time - last_time));*/
116 last_time = cur_time;
117
118 /* Update the counters. */
119 r600_update_grbm_counters(rscreen, &rscreen->grbm_counters);
120 }
121 p_atomic_dec(&rscreen->gpu_load_stop_thread);
122 return 0;
123 }
124
125 void r600_gpu_load_kill_thread(struct r600_common_screen *rscreen)
126 {
127 if (!rscreen->gpu_load_thread)
128 return;
129
130 p_atomic_inc(&rscreen->gpu_load_stop_thread);
131 pipe_thread_wait(rscreen->gpu_load_thread);
132 rscreen->gpu_load_thread = 0;
133 }
134
135 static uint64_t r600_read_grbm_counter(struct r600_common_screen *rscreen,
136 unsigned busy_index)
137 {
138 /* Start the thread if needed. */
139 if (!rscreen->gpu_load_thread) {
140 pipe_mutex_lock(rscreen->gpu_load_mutex);
141 /* Check again inside the mutex. */
142 if (!rscreen->gpu_load_thread)
143 rscreen->gpu_load_thread =
144 pipe_thread_create(r600_gpu_load_thread, rscreen);
145 pipe_mutex_unlock(rscreen->gpu_load_mutex);
146 }
147
148 unsigned busy = p_atomic_read(&rscreen->grbm_counters.array[busy_index]);
149 unsigned idle = p_atomic_read(&rscreen->grbm_counters.array[busy_index + 1]);
150
151 return busy | ((uint64_t)idle << 32);
152 }
153
154 static unsigned r600_end_grbm_counter(struct r600_common_screen *rscreen,
155 uint64_t begin, unsigned busy_index)
156 {
157 uint64_t end = r600_read_grbm_counter(rscreen, busy_index);
158 unsigned busy = (end & 0xffffffff) - (begin & 0xffffffff);
159 unsigned idle = (end >> 32) - (begin >> 32);
160
161 /* Calculate the % of time the busy counter was being incremented.
162 *
163 * If no counters were incremented, return the current counter status.
164 * It's for the case when the load is queried faster than
165 * the counters are updated.
166 */
167 if (idle || busy) {
168 return busy*100 / (busy + idle);
169 } else {
170 union r600_grbm_counters counters;
171
172 memset(&counters, 0, sizeof(counters));
173 r600_update_grbm_counters(rscreen, &counters);
174 return counters.array[busy_index] ? 100 : 0;
175 }
176 }
177
178 #define BUSY_INDEX(rscreen, field) (&rscreen->grbm_counters.named.field.busy - \
179 rscreen->grbm_counters.array)
180
181 static unsigned busy_index_from_type(struct r600_common_screen *rscreen,
182 unsigned type)
183 {
184 switch (type) {
185 case R600_QUERY_GPU_LOAD:
186 return BUSY_INDEX(rscreen, gui);
187 case R600_QUERY_GPU_SHADERS_BUSY:
188 return BUSY_INDEX(rscreen, spi);
189 case R600_QUERY_GPU_TA_BUSY:
190 return BUSY_INDEX(rscreen, ta);
191 case R600_QUERY_GPU_GDS_BUSY:
192 return BUSY_INDEX(rscreen, gds);
193 case R600_QUERY_GPU_VGT_BUSY:
194 return BUSY_INDEX(rscreen, vgt);
195 case R600_QUERY_GPU_IA_BUSY:
196 return BUSY_INDEX(rscreen, ia);
197 case R600_QUERY_GPU_SX_BUSY:
198 return BUSY_INDEX(rscreen, sx);
199 case R600_QUERY_GPU_WD_BUSY:
200 return BUSY_INDEX(rscreen, wd);
201 case R600_QUERY_GPU_BCI_BUSY:
202 return BUSY_INDEX(rscreen, bci);
203 case R600_QUERY_GPU_SC_BUSY:
204 return BUSY_INDEX(rscreen, sc);
205 case R600_QUERY_GPU_PA_BUSY:
206 return BUSY_INDEX(rscreen, pa);
207 case R600_QUERY_GPU_DB_BUSY:
208 return BUSY_INDEX(rscreen, db);
209 case R600_QUERY_GPU_CP_BUSY:
210 return BUSY_INDEX(rscreen, cp);
211 case R600_QUERY_GPU_CB_BUSY:
212 return BUSY_INDEX(rscreen, cb);
213 default:
214 unreachable("query type does not correspond to grbm id");
215 }
216 }
217
218 uint64_t r600_begin_counter(struct r600_common_screen *rscreen, unsigned type)
219 {
220 unsigned busy_index = busy_index_from_type(rscreen, type);
221 return r600_read_grbm_counter(rscreen, busy_index);
222 }
223
224 unsigned r600_end_counter(struct r600_common_screen *rscreen, unsigned type,
225 uint64_t begin)
226 {
227 unsigned busy_index = busy_index_from_type(rscreen, type);
228 return r600_end_grbm_counter(rscreen, begin, busy_index);
229 }