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