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