• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
24 /* The GPU load is measured as follows.
25  *
26  * There is a thread which samples the GRBM_STATUS register at a certain
27  * frequency and the "busy" or "idle" counter is incremented based on
28  * whether the GUI_ACTIVE bit is set or not.
29  *
30  * Then, the user can sample the counters twice and calculate the average
31  * GPU load between the two samples.
32  */
33 
34 #include "radeonsi/si_pipe.h"
35 #include "r600_query.h"
36 #include "util/os_time.h"
37 
38 /* For good accuracy at 1000 fps or lower. This will be inaccurate for higher
39  * fps (there are too few samples per frame). */
40 #define SAMPLES_PER_SEC 10000
41 
42 #define GRBM_STATUS		0x8010
43 #define TA_BUSY(x)		(((x) >> 14) & 0x1)
44 #define GDS_BUSY(x)		(((x) >> 15) & 0x1)
45 #define VGT_BUSY(x)		(((x) >> 17) & 0x1)
46 #define IA_BUSY(x)		(((x) >> 19) & 0x1)
47 #define SX_BUSY(x)		(((x) >> 20) & 0x1)
48 #define WD_BUSY(x)		(((x) >> 21) & 0x1)
49 #define SPI_BUSY(x)		(((x) >> 22) & 0x1)
50 #define BCI_BUSY(x)		(((x) >> 23) & 0x1)
51 #define SC_BUSY(x)		(((x) >> 24) & 0x1)
52 #define PA_BUSY(x)		(((x) >> 25) & 0x1)
53 #define DB_BUSY(x)		(((x) >> 26) & 0x1)
54 #define CP_BUSY(x)		(((x) >> 29) & 0x1)
55 #define CB_BUSY(x)		(((x) >> 30) & 0x1)
56 #define GUI_ACTIVE(x)		(((x) >> 31) & 0x1)
57 
58 #define SRBM_STATUS2		0x0e4c
59 #define SDMA_BUSY(x)		(((x) >> 5) & 0x1)
60 
61 #define CP_STAT                 0x8680
62 #define PFP_BUSY(x)		(((x) >> 15) & 0x1)
63 #define MEQ_BUSY(x)		(((x) >> 16) & 0x1)
64 #define ME_BUSY(x)		(((x) >> 17) & 0x1)
65 #define SURFACE_SYNC_BUSY(x)	(((x) >> 21) & 0x1)
66 #define DMA_BUSY(x)		(((x) >> 22) & 0x1)
67 #define SCRATCH_RAM_BUSY(x)	(((x) >> 24) & 0x1)
68 
69 #define IDENTITY(x) x
70 
71 #define UPDATE_COUNTER(field, mask)					\
72 	do {								\
73 		if (mask(value))					\
74 			p_atomic_inc(&counters->named.field.busy);	\
75 		else							\
76 			p_atomic_inc(&counters->named.field.idle);	\
77 	} while (0)
78 
r600_update_mmio_counters(struct si_screen * sscreen,union r600_mmio_counters * counters)79 static void r600_update_mmio_counters(struct si_screen *sscreen,
80 				      union r600_mmio_counters *counters)
81 {
82 	uint32_t value = 0;
83 	bool gui_busy, sdma_busy = false;
84 
85 	/* GRBM_STATUS */
86 	sscreen->ws->read_registers(sscreen->ws, GRBM_STATUS, 1, &value);
87 
88 	UPDATE_COUNTER(ta, TA_BUSY);
89 	UPDATE_COUNTER(gds, GDS_BUSY);
90 	UPDATE_COUNTER(vgt, VGT_BUSY);
91 	UPDATE_COUNTER(ia, IA_BUSY);
92 	UPDATE_COUNTER(sx, SX_BUSY);
93 	UPDATE_COUNTER(wd, WD_BUSY);
94 	UPDATE_COUNTER(spi, SPI_BUSY);
95 	UPDATE_COUNTER(bci, BCI_BUSY);
96 	UPDATE_COUNTER(sc, SC_BUSY);
97 	UPDATE_COUNTER(pa, PA_BUSY);
98 	UPDATE_COUNTER(db, DB_BUSY);
99 	UPDATE_COUNTER(cp, CP_BUSY);
100 	UPDATE_COUNTER(cb, CB_BUSY);
101 	UPDATE_COUNTER(gui, GUI_ACTIVE);
102 	gui_busy = GUI_ACTIVE(value);
103 
104 	if (sscreen->info.chip_class == CIK || sscreen->info.chip_class == VI) {
105 		/* SRBM_STATUS2 */
106 		sscreen->ws->read_registers(sscreen->ws, SRBM_STATUS2, 1, &value);
107 
108 		UPDATE_COUNTER(sdma, SDMA_BUSY);
109 		sdma_busy = SDMA_BUSY(value);
110 	}
111 
112 	if (sscreen->info.chip_class >= VI) {
113 		/* CP_STAT */
114 		sscreen->ws->read_registers(sscreen->ws, CP_STAT, 1, &value);
115 
116 		UPDATE_COUNTER(pfp, PFP_BUSY);
117 		UPDATE_COUNTER(meq, MEQ_BUSY);
118 		UPDATE_COUNTER(me, ME_BUSY);
119 		UPDATE_COUNTER(surf_sync, SURFACE_SYNC_BUSY);
120 		UPDATE_COUNTER(cp_dma, DMA_BUSY);
121 		UPDATE_COUNTER(scratch_ram, SCRATCH_RAM_BUSY);
122 	}
123 
124 	value = gui_busy || sdma_busy;
125 	UPDATE_COUNTER(gpu, IDENTITY);
126 }
127 
128 #undef UPDATE_COUNTER
129 
130 static int
r600_gpu_load_thread(void * param)131 r600_gpu_load_thread(void *param)
132 {
133 	struct si_screen *sscreen = (struct si_screen*)param;
134 	const int period_us = 1000000 / SAMPLES_PER_SEC;
135 	int sleep_us = period_us;
136 	int64_t cur_time, last_time = os_time_get();
137 
138 	while (!p_atomic_read(&sscreen->gpu_load_stop_thread)) {
139 		if (sleep_us)
140 			os_time_sleep(sleep_us);
141 
142 		/* Make sure we sleep the ideal amount of time to match
143 		 * the expected frequency. */
144 		cur_time = os_time_get();
145 
146 		if (os_time_timeout(last_time, last_time + period_us,
147 				    cur_time))
148 			sleep_us = MAX2(sleep_us - 1, 1);
149 		else
150 			sleep_us += 1;
151 
152 		/*printf("Hz: %.1f\n", 1000000.0 / (cur_time - last_time));*/
153 		last_time = cur_time;
154 
155 		/* Update the counters. */
156 		r600_update_mmio_counters(sscreen, &sscreen->mmio_counters);
157 	}
158 	p_atomic_dec(&sscreen->gpu_load_stop_thread);
159 	return 0;
160 }
161 
si_gpu_load_kill_thread(struct si_screen * sscreen)162 void si_gpu_load_kill_thread(struct si_screen *sscreen)
163 {
164 	if (!sscreen->gpu_load_thread)
165 		return;
166 
167 	p_atomic_inc(&sscreen->gpu_load_stop_thread);
168 	thrd_join(sscreen->gpu_load_thread, NULL);
169 	sscreen->gpu_load_thread = 0;
170 }
171 
r600_read_mmio_counter(struct si_screen * sscreen,unsigned busy_index)172 static uint64_t r600_read_mmio_counter(struct si_screen *sscreen,
173 				       unsigned busy_index)
174 {
175 	/* Start the thread if needed. */
176 	if (!sscreen->gpu_load_thread) {
177 		mtx_lock(&sscreen->gpu_load_mutex);
178 		/* Check again inside the mutex. */
179 		if (!sscreen->gpu_load_thread)
180 			sscreen->gpu_load_thread =
181 				u_thread_create(r600_gpu_load_thread, sscreen);
182 		mtx_unlock(&sscreen->gpu_load_mutex);
183 	}
184 
185 	unsigned busy = p_atomic_read(&sscreen->mmio_counters.array[busy_index]);
186 	unsigned idle = p_atomic_read(&sscreen->mmio_counters.array[busy_index + 1]);
187 
188 	return busy | ((uint64_t)idle << 32);
189 }
190 
r600_end_mmio_counter(struct si_screen * sscreen,uint64_t begin,unsigned busy_index)191 static unsigned r600_end_mmio_counter(struct si_screen *sscreen,
192 				      uint64_t begin, unsigned busy_index)
193 {
194 	uint64_t end = r600_read_mmio_counter(sscreen, busy_index);
195 	unsigned busy = (end & 0xffffffff) - (begin & 0xffffffff);
196 	unsigned idle = (end >> 32) - (begin >> 32);
197 
198 	/* Calculate the % of time the busy counter was being incremented.
199 	 *
200 	 * If no counters were incremented, return the current counter status.
201 	 * It's for the case when the load is queried faster than
202 	 * the counters are updated.
203 	 */
204 	if (idle || busy) {
205 		return busy*100 / (busy + idle);
206 	} else {
207 		union r600_mmio_counters counters;
208 
209 		memset(&counters, 0, sizeof(counters));
210 		r600_update_mmio_counters(sscreen, &counters);
211 		return counters.array[busy_index] ? 100 : 0;
212 	}
213 }
214 
215 #define BUSY_INDEX(rscreen, field) (&rscreen->mmio_counters.named.field.busy - \
216 				    rscreen->mmio_counters.array)
217 
busy_index_from_type(struct si_screen * sscreen,unsigned type)218 static unsigned busy_index_from_type(struct si_screen *sscreen,
219 				     unsigned type)
220 {
221 	switch (type) {
222 	case R600_QUERY_GPU_LOAD:
223 		return BUSY_INDEX(sscreen, gpu);
224 	case R600_QUERY_GPU_SHADERS_BUSY:
225 		return BUSY_INDEX(sscreen, spi);
226 	case R600_QUERY_GPU_TA_BUSY:
227 		return BUSY_INDEX(sscreen, ta);
228 	case R600_QUERY_GPU_GDS_BUSY:
229 		return BUSY_INDEX(sscreen, gds);
230 	case R600_QUERY_GPU_VGT_BUSY:
231 		return BUSY_INDEX(sscreen, vgt);
232 	case R600_QUERY_GPU_IA_BUSY:
233 		return BUSY_INDEX(sscreen, ia);
234 	case R600_QUERY_GPU_SX_BUSY:
235 		return BUSY_INDEX(sscreen, sx);
236 	case R600_QUERY_GPU_WD_BUSY:
237 		return BUSY_INDEX(sscreen, wd);
238 	case R600_QUERY_GPU_BCI_BUSY:
239 		return BUSY_INDEX(sscreen, bci);
240 	case R600_QUERY_GPU_SC_BUSY:
241 		return BUSY_INDEX(sscreen, sc);
242 	case R600_QUERY_GPU_PA_BUSY:
243 		return BUSY_INDEX(sscreen, pa);
244 	case R600_QUERY_GPU_DB_BUSY:
245 		return BUSY_INDEX(sscreen, db);
246 	case R600_QUERY_GPU_CP_BUSY:
247 		return BUSY_INDEX(sscreen, cp);
248 	case R600_QUERY_GPU_CB_BUSY:
249 		return BUSY_INDEX(sscreen, cb);
250 	case R600_QUERY_GPU_SDMA_BUSY:
251 		return BUSY_INDEX(sscreen, sdma);
252 	case R600_QUERY_GPU_PFP_BUSY:
253 		return BUSY_INDEX(sscreen, pfp);
254 	case R600_QUERY_GPU_MEQ_BUSY:
255 		return BUSY_INDEX(sscreen, meq);
256 	case R600_QUERY_GPU_ME_BUSY:
257 		return BUSY_INDEX(sscreen, me);
258 	case R600_QUERY_GPU_SURF_SYNC_BUSY:
259 		return BUSY_INDEX(sscreen, surf_sync);
260 	case R600_QUERY_GPU_CP_DMA_BUSY:
261 		return BUSY_INDEX(sscreen, cp_dma);
262 	case R600_QUERY_GPU_SCRATCH_RAM_BUSY:
263 		return BUSY_INDEX(sscreen, scratch_ram);
264 	default:
265 		unreachable("invalid query type");
266 	}
267 }
268 
si_begin_counter(struct si_screen * sscreen,unsigned type)269 uint64_t si_begin_counter(struct si_screen *sscreen, unsigned type)
270 {
271 	unsigned busy_index = busy_index_from_type(sscreen, type);
272 	return r600_read_mmio_counter(sscreen, busy_index);
273 }
274 
si_end_counter(struct si_screen * sscreen,unsigned type,uint64_t begin)275 unsigned si_end_counter(struct si_screen *sscreen, unsigned type,
276 			uint64_t begin)
277 {
278 	unsigned busy_index = busy_index_from_type(sscreen, type);
279 	return r600_end_mmio_counter(sscreen, begin, busy_index);
280 }
281