1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2021 Valve Corporation
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_SPM_H
26bf215546Sopenharmony_ci#define AC_SPM_H
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include <stdint.h>
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "ac_perfcounter.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#define AC_SPM_MAX_COUNTER_PER_BLOCK 16
33bf215546Sopenharmony_ci#define AC_SPM_GLOBAL_TIMESTAMP_COUNTERS 4 /* in unit of 16-bit counters*/
34bf215546Sopenharmony_ci#define AC_SPM_NUM_COUNTER_PER_MUXSEL 16 /* 16 16-bit counters per muxsel */
35bf215546Sopenharmony_ci#define AC_SPM_MUXSEL_LINE_SIZE ((AC_SPM_NUM_COUNTER_PER_MUXSEL * 2) / 4) /* in dwords */
36bf215546Sopenharmony_ci#define AC_SPM_NUM_PERF_SEL 4
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_cienum ac_spm_segment_type {
39bf215546Sopenharmony_ci   AC_SPM_SEGMENT_TYPE_SE0,
40bf215546Sopenharmony_ci   AC_SPM_SEGMENT_TYPE_SE1,
41bf215546Sopenharmony_ci   AC_SPM_SEGMENT_TYPE_SE2,
42bf215546Sopenharmony_ci   AC_SPM_SEGMENT_TYPE_SE3,
43bf215546Sopenharmony_ci   AC_SPM_SEGMENT_TYPE_GLOBAL,
44bf215546Sopenharmony_ci   AC_SPM_SEGMENT_TYPE_COUNT,
45bf215546Sopenharmony_ci};
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_cistruct ac_spm_counter_create_info {
48bf215546Sopenharmony_ci   enum ac_pc_gpu_block gpu_block;
49bf215546Sopenharmony_ci   uint32_t instance;
50bf215546Sopenharmony_ci   uint32_t event_id;
51bf215546Sopenharmony_ci};
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_cistruct ac_spm_muxsel {
54bf215546Sopenharmony_ci   uint16_t counter      : 6;
55bf215546Sopenharmony_ci   uint16_t block        : 4;
56bf215546Sopenharmony_ci   uint16_t shader_array : 1; /* 0: SA0, 1: SA1 */
57bf215546Sopenharmony_ci   uint16_t instance     : 5;
58bf215546Sopenharmony_ci};
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_cistruct ac_spm_muxsel_line {
61bf215546Sopenharmony_ci   struct ac_spm_muxsel muxsel[AC_SPM_NUM_COUNTER_PER_MUXSEL];
62bf215546Sopenharmony_ci};
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_cistruct ac_spm_counter_info {
65bf215546Sopenharmony_ci   /* General info. */
66bf215546Sopenharmony_ci   enum ac_pc_gpu_block gpu_block;
67bf215546Sopenharmony_ci   uint32_t instance;
68bf215546Sopenharmony_ci   uint32_t event_id;
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci   /* Muxsel info. */
71bf215546Sopenharmony_ci   enum ac_spm_segment_type segment_type;
72bf215546Sopenharmony_ci   bool is_even;
73bf215546Sopenharmony_ci   struct ac_spm_muxsel muxsel;
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci   /* Output info. */
76bf215546Sopenharmony_ci   uint64_t offset;
77bf215546Sopenharmony_ci};
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_cistruct ac_spm_counter_select {
80bf215546Sopenharmony_ci   uint8_t active; /* mask of used 16-bit counters. */
81bf215546Sopenharmony_ci   uint32_t sel0;
82bf215546Sopenharmony_ci   uint32_t sel1;
83bf215546Sopenharmony_ci};
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_cistruct ac_spm_block_select {
86bf215546Sopenharmony_ci   const struct ac_pc_block *b;
87bf215546Sopenharmony_ci   uint32_t grbm_gfx_index;
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci   uint32_t num_counters;
90bf215546Sopenharmony_ci   struct ac_spm_counter_select counters[AC_SPM_MAX_COUNTER_PER_BLOCK];
91bf215546Sopenharmony_ci};
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_cistruct ac_spm_trace_data {
94bf215546Sopenharmony_ci   /* struct radeon_winsys_bo or struct pb_buffer */
95bf215546Sopenharmony_ci   void *bo;
96bf215546Sopenharmony_ci   void *ptr;
97bf215546Sopenharmony_ci   uint32_t buffer_size;
98bf215546Sopenharmony_ci   uint16_t sample_interval;
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci   /* Enabled counters. */
101bf215546Sopenharmony_ci   unsigned num_counters;
102bf215546Sopenharmony_ci   struct ac_spm_counter_info *counters;
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci   /* Block/counters selection. */
105bf215546Sopenharmony_ci   uint32_t num_block_sel;
106bf215546Sopenharmony_ci   struct ac_spm_block_select *block_sel;
107bf215546Sopenharmony_ci   uint32_t num_used_sq_block_sel;
108bf215546Sopenharmony_ci   struct ac_spm_block_select sq_block_sel[16];
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci   /* Muxsel lines. */
111bf215546Sopenharmony_ci   unsigned num_muxsel_lines[AC_SPM_SEGMENT_TYPE_COUNT];
112bf215546Sopenharmony_ci   struct ac_spm_muxsel_line *muxsel_lines[AC_SPM_SEGMENT_TYPE_COUNT];
113bf215546Sopenharmony_ci};
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_cibool ac_init_spm(const struct radeon_info *info,
116bf215546Sopenharmony_ci                 const struct ac_perfcounters *pc,
117bf215546Sopenharmony_ci                 unsigned num_counters,
118bf215546Sopenharmony_ci                 const struct ac_spm_counter_create_info *counters,
119bf215546Sopenharmony_ci                 struct ac_spm_trace_data *spm_trace);
120bf215546Sopenharmony_civoid ac_destroy_spm(struct ac_spm_trace_data *spm_trace);
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ciuint32_t ac_spm_get_sample_size(const struct ac_spm_trace_data *spm_trace);
123bf215546Sopenharmony_ciuint32_t ac_spm_get_num_samples(const struct ac_spm_trace_data *spm_trace);
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci#endif
126