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 * on the rights to use, copy, modify, merge, publish, distribute, sub
9bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom
10bf215546Sopenharmony_ci * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci#ifndef AC_PERFCOUNTER_H
26bf215546Sopenharmony_ci#define AC_PERFCOUNTER_H
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include <stdbool.h>
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "sid.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#include "ac_gpu_info.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci/* Max counters per HW block */
35bf215546Sopenharmony_ci#define AC_QUERY_MAX_COUNTERS 16
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci#define AC_PC_SHADERS_WINDOWING (1u << 31)
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_cienum ac_pc_block_flags
40bf215546Sopenharmony_ci{
41bf215546Sopenharmony_ci   /* This block is part of the shader engine */
42bf215546Sopenharmony_ci   AC_PC_BLOCK_SE = (1 << 0),
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci   /* Expose per-instance groups instead of summing all instances (within
45bf215546Sopenharmony_ci    * an SE). */
46bf215546Sopenharmony_ci   AC_PC_BLOCK_INSTANCE_GROUPS = (1 << 1),
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci   /* Expose per-SE groups instead of summing instances across SEs. */
49bf215546Sopenharmony_ci   AC_PC_BLOCK_SE_GROUPS = (1 << 2),
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci   /* Shader block */
52bf215546Sopenharmony_ci   AC_PC_BLOCK_SHADER = (1 << 3),
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci   /* Non-shader block with perfcounters windowed by shaders. */
55bf215546Sopenharmony_ci   AC_PC_BLOCK_SHADER_WINDOWED = (1 << 4),
56bf215546Sopenharmony_ci};
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_cienum ac_pc_gpu_block {
59bf215546Sopenharmony_ci   CPF     = 0x0,
60bf215546Sopenharmony_ci   IA      = 0x1,
61bf215546Sopenharmony_ci   VGT     = 0x2,
62bf215546Sopenharmony_ci   PA_SU   = 0x3,
63bf215546Sopenharmony_ci   PA_SC   = 0x4,
64bf215546Sopenharmony_ci   SPI     = 0x5,
65bf215546Sopenharmony_ci   SQ      = 0x6,
66bf215546Sopenharmony_ci   SX      = 0x7,
67bf215546Sopenharmony_ci   TA      = 0x8,
68bf215546Sopenharmony_ci   TD      = 0x9,
69bf215546Sopenharmony_ci   TCP     = 0xA,
70bf215546Sopenharmony_ci   TCC     = 0xB,
71bf215546Sopenharmony_ci   TCA     = 0xC,
72bf215546Sopenharmony_ci   DB      = 0xD,
73bf215546Sopenharmony_ci   CB      = 0xE,
74bf215546Sopenharmony_ci   GDS     = 0xF,
75bf215546Sopenharmony_ci   SRBM    = 0x10,
76bf215546Sopenharmony_ci   GRBM    = 0x11,
77bf215546Sopenharmony_ci   GRBMSE  = 0x12,
78bf215546Sopenharmony_ci   RLC     = 0x13,
79bf215546Sopenharmony_ci   DMA     = 0x14,
80bf215546Sopenharmony_ci   MC      = 0x15,
81bf215546Sopenharmony_ci   CPG     = 0x16,
82bf215546Sopenharmony_ci   CPC     = 0x17,
83bf215546Sopenharmony_ci   WD      = 0x18,
84bf215546Sopenharmony_ci   TCS     = 0x19,
85bf215546Sopenharmony_ci   ATC     = 0x1A,
86bf215546Sopenharmony_ci   ATCL2   = 0x1B,
87bf215546Sopenharmony_ci   MCVML2  = 0x1C,
88bf215546Sopenharmony_ci   EA      = 0x1D,
89bf215546Sopenharmony_ci   RPB     = 0x1E,
90bf215546Sopenharmony_ci   RMI     = 0x1F,
91bf215546Sopenharmony_ci   UMCCH   = 0x20,
92bf215546Sopenharmony_ci   GE      = 0x21,
93bf215546Sopenharmony_ci   GE1     = GE,
94bf215546Sopenharmony_ci   GL1A    = 0x22,
95bf215546Sopenharmony_ci   GL1C    = 0x23,
96bf215546Sopenharmony_ci   GL1CG   = 0x24,
97bf215546Sopenharmony_ci   GL2A    = 0x25,
98bf215546Sopenharmony_ci   GL2C    = 0x26,
99bf215546Sopenharmony_ci   CHA     = 0x27,
100bf215546Sopenharmony_ci   CHC     = 0x28,
101bf215546Sopenharmony_ci   CHCG    = 0x29,
102bf215546Sopenharmony_ci   GUS     = 0x2A,
103bf215546Sopenharmony_ci   GCR     = 0x2B,
104bf215546Sopenharmony_ci   PA_PH   = 0x2C,
105bf215546Sopenharmony_ci   UTCL1   = 0x2D,
106bf215546Sopenharmony_ci   GEDIST  = 0x2E,
107bf215546Sopenharmony_ci   GESE    = 0x2F,
108bf215546Sopenharmony_ci   DF      = 0x30,
109bf215546Sopenharmony_ci   NUM_GPU_BLOCK,
110bf215546Sopenharmony_ci};
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_cistruct ac_pc_block_base {
113bf215546Sopenharmony_ci   enum ac_pc_gpu_block gpu_block;
114bf215546Sopenharmony_ci   const char *name;
115bf215546Sopenharmony_ci   unsigned num_counters;
116bf215546Sopenharmony_ci   unsigned flags;
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_ci   unsigned select_or;
119bf215546Sopenharmony_ci   unsigned *select0;
120bf215546Sopenharmony_ci   unsigned counter0_lo;
121bf215546Sopenharmony_ci   unsigned *counters;
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_ci   /* SPM */
124bf215546Sopenharmony_ci   unsigned num_spm_counters;
125bf215546Sopenharmony_ci   unsigned num_spm_wires;
126bf215546Sopenharmony_ci   unsigned *select1;
127bf215546Sopenharmony_ci   unsigned spm_block_select;
128bf215546Sopenharmony_ci};
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_cistruct ac_pc_block_gfxdescr {
131bf215546Sopenharmony_ci   struct ac_pc_block_base *b;
132bf215546Sopenharmony_ci   unsigned selectors;
133bf215546Sopenharmony_ci   unsigned instances;
134bf215546Sopenharmony_ci};
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_cistruct ac_pc_block {
137bf215546Sopenharmony_ci   const struct ac_pc_block_gfxdescr *b;
138bf215546Sopenharmony_ci   unsigned num_instances;
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ci   unsigned num_groups;
141bf215546Sopenharmony_ci   char *group_names;
142bf215546Sopenharmony_ci   unsigned group_name_stride;
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci   char *selector_names;
145bf215546Sopenharmony_ci   unsigned selector_name_stride;
146bf215546Sopenharmony_ci};
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_cistruct ac_perfcounters {
149bf215546Sopenharmony_ci   unsigned num_groups;
150bf215546Sopenharmony_ci   unsigned num_blocks;
151bf215546Sopenharmony_ci   struct ac_pc_block *blocks;
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_ci   bool separate_se;
154bf215546Sopenharmony_ci   bool separate_instance;
155bf215546Sopenharmony_ci};
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_ci/* The order is chosen to be compatible with GPUPerfStudio's hardcoding of
158bf215546Sopenharmony_ci * performance counter group IDs.
159bf215546Sopenharmony_ci */
160bf215546Sopenharmony_cistatic const char *const ac_pc_shader_type_suffixes[] = {"",    "_ES", "_GS", "_VS",
161bf215546Sopenharmony_ci                                                         "_PS", "_LS", "_HS", "_CS"};
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_cistatic const unsigned ac_pc_shader_type_bits[] = {
164bf215546Sopenharmony_ci   0x7f,
165bf215546Sopenharmony_ci   S_036780_ES_EN(1),
166bf215546Sopenharmony_ci   S_036780_GS_EN(1),
167bf215546Sopenharmony_ci   S_036780_VS_EN(1),
168bf215546Sopenharmony_ci   S_036780_PS_EN(1),
169bf215546Sopenharmony_ci   S_036780_LS_EN(1),
170bf215546Sopenharmony_ci   S_036780_HS_EN(1),
171bf215546Sopenharmony_ci   S_036780_CS_EN(1),
172bf215546Sopenharmony_ci};
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_cistatic inline bool
175bf215546Sopenharmony_ciac_pc_block_has_per_se_groups(const struct ac_perfcounters *pc,
176bf215546Sopenharmony_ci                              const struct ac_pc_block *block)
177bf215546Sopenharmony_ci{
178bf215546Sopenharmony_ci   return block->b->b->flags & AC_PC_BLOCK_SE_GROUPS ||
179bf215546Sopenharmony_ci          (block->b->b->flags & AC_PC_BLOCK_SE && pc->separate_se);
180bf215546Sopenharmony_ci}
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_cistatic inline bool
183bf215546Sopenharmony_ciac_pc_block_has_per_instance_groups(const struct ac_perfcounters *pc,
184bf215546Sopenharmony_ci                                    const struct ac_pc_block *block)
185bf215546Sopenharmony_ci{
186bf215546Sopenharmony_ci   return block->b->b->flags & AC_PC_BLOCK_INSTANCE_GROUPS ||
187bf215546Sopenharmony_ci          (block->num_instances > 1 && pc->separate_instance);
188bf215546Sopenharmony_ci}
189bf215546Sopenharmony_ci
190bf215546Sopenharmony_cistruct ac_pc_block *ac_lookup_counter(const struct ac_perfcounters *pc,
191bf215546Sopenharmony_ci                                      unsigned index, unsigned *base_gid,
192bf215546Sopenharmony_ci                                      unsigned *sub_index);
193bf215546Sopenharmony_cistruct ac_pc_block *ac_lookup_group(const struct ac_perfcounters *pc,
194bf215546Sopenharmony_ci                                    unsigned *index);
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_cistruct ac_pc_block *ac_pc_get_block(const struct ac_perfcounters *pc,
197bf215546Sopenharmony_ci                                    enum ac_pc_gpu_block gpu_block);
198bf215546Sopenharmony_ci
199bf215546Sopenharmony_cibool ac_init_block_names(const struct radeon_info *info,
200bf215546Sopenharmony_ci                         const struct ac_perfcounters *pc,
201bf215546Sopenharmony_ci                         struct ac_pc_block *block);
202bf215546Sopenharmony_ci
203bf215546Sopenharmony_cibool ac_init_perfcounters(const struct radeon_info *info,
204bf215546Sopenharmony_ci                          bool separate_se,
205bf215546Sopenharmony_ci                          bool separate_instance,
206bf215546Sopenharmony_ci                          struct ac_perfcounters *pc);
207bf215546Sopenharmony_civoid ac_destroy_perfcounters(struct ac_perfcounters *pc);
208bf215546Sopenharmony_ci
209bf215546Sopenharmony_ci#endif
210