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#include "ac_spm.h"
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "util/bitscan.h"
28bf215546Sopenharmony_ci#include "util/u_memory.h"
29bf215546Sopenharmony_ci#include "ac_perfcounter.h"
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_cistatic struct ac_spm_block_select *
32bf215546Sopenharmony_ciac_spm_get_block_select(struct ac_spm_trace_data *spm_trace,
33bf215546Sopenharmony_ci                        const struct ac_pc_block *block)
34bf215546Sopenharmony_ci{
35bf215546Sopenharmony_ci   struct ac_spm_block_select *block_sel, *new_block_sel;
36bf215546Sopenharmony_ci   uint32_t num_block_sel;
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci   for (uint32_t i = 0; i < spm_trace->num_block_sel; i++) {
39bf215546Sopenharmony_ci      if (spm_trace->block_sel[i].b->b->b->gpu_block == block->b->b->gpu_block)
40bf215546Sopenharmony_ci         return &spm_trace->block_sel[i];
41bf215546Sopenharmony_ci   }
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci   /* Allocate a new select block if it doesn't already exist. */
44bf215546Sopenharmony_ci   num_block_sel = spm_trace->num_block_sel + 1;
45bf215546Sopenharmony_ci   block_sel = realloc(spm_trace->block_sel, num_block_sel * sizeof(*block_sel));
46bf215546Sopenharmony_ci   if (!block_sel)
47bf215546Sopenharmony_ci      return NULL;
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci   spm_trace->num_block_sel = num_block_sel;
50bf215546Sopenharmony_ci   spm_trace->block_sel = block_sel;
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci   /* Initialize the new select block. */
53bf215546Sopenharmony_ci   new_block_sel = &spm_trace->block_sel[spm_trace->num_block_sel - 1];
54bf215546Sopenharmony_ci   memset(new_block_sel, 0, sizeof(*new_block_sel));
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   new_block_sel->b = block;
57bf215546Sopenharmony_ci   new_block_sel->num_counters = block->b->b->num_spm_counters;
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci   /* Broadcast global block writes to SEs and SAs */
60bf215546Sopenharmony_ci   if (!(block->b->b->flags & (AC_PC_BLOCK_SE | AC_PC_BLOCK_SHADER)))
61bf215546Sopenharmony_ci      new_block_sel->grbm_gfx_index = S_030800_SE_BROADCAST_WRITES(1) |
62bf215546Sopenharmony_ci                                      S_030800_SH_BROADCAST_WRITES(1);
63bf215546Sopenharmony_ci   /* Broadcast per SE block writes to SAs */
64bf215546Sopenharmony_ci   else if (block->b->b->flags & AC_PC_BLOCK_SE)
65bf215546Sopenharmony_ci      new_block_sel->grbm_gfx_index = S_030800_SH_BROADCAST_WRITES(1);
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci   return new_block_sel;
68bf215546Sopenharmony_ci}
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_cistatic void
71bf215546Sopenharmony_ciac_spm_init_muxsel(const struct ac_pc_block *block,
72bf215546Sopenharmony_ci                   struct ac_spm_counter_info *counter,
73bf215546Sopenharmony_ci                   uint32_t spm_wire)
74bf215546Sopenharmony_ci{
75bf215546Sopenharmony_ci   struct ac_spm_muxsel *muxsel = &counter->muxsel;
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci   muxsel->counter = 2 * spm_wire + (counter->is_even ? 0 : 1);
78bf215546Sopenharmony_ci   muxsel->block = block->b->b->spm_block_select;
79bf215546Sopenharmony_ci   muxsel->shader_array = 0;
80bf215546Sopenharmony_ci   muxsel->instance = 0;
81bf215546Sopenharmony_ci}
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_cistatic bool
84bf215546Sopenharmony_ciac_spm_map_counter(struct ac_spm_trace_data *spm_trace,
85bf215546Sopenharmony_ci                   struct ac_spm_block_select *block_sel,
86bf215546Sopenharmony_ci                   struct ac_spm_counter_info *counter,
87bf215546Sopenharmony_ci                   uint32_t *spm_wire)
88bf215546Sopenharmony_ci{
89bf215546Sopenharmony_ci   if (block_sel->b->b->b->gpu_block == SQ) {
90bf215546Sopenharmony_ci      for (unsigned i = 0; i < ARRAY_SIZE(spm_trace->sq_block_sel); i++) {
91bf215546Sopenharmony_ci         struct ac_spm_block_select *sq_block_sel = &spm_trace->sq_block_sel[i];
92bf215546Sopenharmony_ci         struct ac_spm_counter_select *cntr_sel = &sq_block_sel->counters[0];
93bf215546Sopenharmony_ci         if (i < spm_trace->num_used_sq_block_sel)
94bf215546Sopenharmony_ci            continue;
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci         /* SQ doesn't support 16-bit counters. */
97bf215546Sopenharmony_ci         cntr_sel->sel0 |= S_036700_PERF_SEL(counter->event_id) |
98bf215546Sopenharmony_ci                           S_036700_SPM_MODE(3) | /* 32-bit clamp */
99bf215546Sopenharmony_ci                           S_036700_PERF_MODE(0);
100bf215546Sopenharmony_ci         cntr_sel->active |= 0x3;
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci         /* 32-bits counter are always even. */
103bf215546Sopenharmony_ci         counter->is_even = true;
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci         /* One wire per SQ module. */
106bf215546Sopenharmony_ci         *spm_wire = i;
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci         spm_trace->num_used_sq_block_sel++;
109bf215546Sopenharmony_ci         return true;
110bf215546Sopenharmony_ci      }
111bf215546Sopenharmony_ci   } else {
112bf215546Sopenharmony_ci      /* Generic blocks. */
113bf215546Sopenharmony_ci      for (unsigned i = 0; i < block_sel->num_counters; i++) {
114bf215546Sopenharmony_ci         struct ac_spm_counter_select *cntr_sel = &block_sel->counters[i];
115bf215546Sopenharmony_ci         int index = ffs(~cntr_sel->active) - 1;
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci         switch (index) {
118bf215546Sopenharmony_ci         case 0: /* use S_037004_PERF_SEL */
119bf215546Sopenharmony_ci            cntr_sel->sel0 |= S_037004_PERF_SEL(counter->event_id) |
120bf215546Sopenharmony_ci                              S_037004_CNTR_MODE(1) | /* 16-bit clamp */
121bf215546Sopenharmony_ci                              S_037004_PERF_MODE(0); /* accum */
122bf215546Sopenharmony_ci            break;
123bf215546Sopenharmony_ci         case 1: /* use S_037004_PERF_SEL1 */
124bf215546Sopenharmony_ci            cntr_sel->sel0 |= S_037004_PERF_SEL1(counter->event_id) |
125bf215546Sopenharmony_ci                              S_037004_PERF_MODE1(0);
126bf215546Sopenharmony_ci            break;
127bf215546Sopenharmony_ci         case 2: /* use S_037004_PERF_SEL2 */
128bf215546Sopenharmony_ci            cntr_sel->sel1 |= S_037008_PERF_SEL2(counter->event_id) |
129bf215546Sopenharmony_ci                              S_037008_PERF_MODE2(0);
130bf215546Sopenharmony_ci            break;
131bf215546Sopenharmony_ci         case 3: /* use S_037004_PERF_SEL3 */
132bf215546Sopenharmony_ci            cntr_sel->sel1 |= S_037008_PERF_SEL3(counter->event_id) |
133bf215546Sopenharmony_ci                              S_037008_PERF_MODE3(0);
134bf215546Sopenharmony_ci            break;
135bf215546Sopenharmony_ci         default:
136bf215546Sopenharmony_ci            return false;
137bf215546Sopenharmony_ci         }
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_ci         /* Mark this 16-bit counter as used. */
140bf215546Sopenharmony_ci         cntr_sel->active |= 1 << index;
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_ci         /* Determine if the counter is even or odd. */
143bf215546Sopenharmony_ci         counter->is_even = !(index % 2);
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci         /* Determine the SPM wire (one wire holds two 16-bit counters). */
146bf215546Sopenharmony_ci         *spm_wire = !!(index >= 2);
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_ci         return true;
149bf215546Sopenharmony_ci      }
150bf215546Sopenharmony_ci   }
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ci   return false;
153bf215546Sopenharmony_ci}
154bf215546Sopenharmony_ci
155bf215546Sopenharmony_cistatic bool
156bf215546Sopenharmony_ciac_spm_add_counter(const struct ac_perfcounters *pc,
157bf215546Sopenharmony_ci                   struct ac_spm_trace_data *spm_trace,
158bf215546Sopenharmony_ci                   const struct ac_spm_counter_create_info *info)
159bf215546Sopenharmony_ci{
160bf215546Sopenharmony_ci   struct ac_spm_counter_info *counter;
161bf215546Sopenharmony_ci   struct ac_spm_block_select *block_sel;
162bf215546Sopenharmony_ci   struct ac_pc_block *block;
163bf215546Sopenharmony_ci   uint32_t spm_wire;
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci   /* Check if the GPU block is valid. */
166bf215546Sopenharmony_ci   block = ac_pc_get_block(pc, info->gpu_block);
167bf215546Sopenharmony_ci   if (!block) {
168bf215546Sopenharmony_ci      fprintf(stderr, "ac/spm: Invalid GPU block.\n");
169bf215546Sopenharmony_ci      return false;
170bf215546Sopenharmony_ci   }
171bf215546Sopenharmony_ci
172bf215546Sopenharmony_ci   /* Check if the number of instances is valid. */
173bf215546Sopenharmony_ci   if (info->instance > block->num_instances) {
174bf215546Sopenharmony_ci      fprintf(stderr, "ac/spm: Invalid instance ID.\n");
175bf215546Sopenharmony_ci      return false;
176bf215546Sopenharmony_ci   }
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_ci   /* Check if the event ID is valid. */
179bf215546Sopenharmony_ci   if (info->event_id > block->b->selectors) {
180bf215546Sopenharmony_ci      fprintf(stderr, "ac/spm: Invalid event ID.\n");
181bf215546Sopenharmony_ci      return false;
182bf215546Sopenharmony_ci   }
183bf215546Sopenharmony_ci
184bf215546Sopenharmony_ci   counter = &spm_trace->counters[spm_trace->num_counters];
185bf215546Sopenharmony_ci   spm_trace->num_counters++;
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_ci   counter->gpu_block = info->gpu_block;
188bf215546Sopenharmony_ci   counter->instance = info->instance;
189bf215546Sopenharmony_ci   counter->event_id = info->event_id;
190bf215546Sopenharmony_ci
191bf215546Sopenharmony_ci   /* Get the select block used to configure the counter. */
192bf215546Sopenharmony_ci   block_sel = ac_spm_get_block_select(spm_trace, block);
193bf215546Sopenharmony_ci   if (!block_sel)
194bf215546Sopenharmony_ci      return false;
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_ci   /* Map the counter to the select block. */
197bf215546Sopenharmony_ci   if (!ac_spm_map_counter(spm_trace, block_sel, counter, &spm_wire)) {
198bf215546Sopenharmony_ci      fprintf(stderr, "ac/spm: No free slots available!\n");
199bf215546Sopenharmony_ci      return false;
200bf215546Sopenharmony_ci   }
201bf215546Sopenharmony_ci
202bf215546Sopenharmony_ci   /* Determine the counter segment type. */
203bf215546Sopenharmony_ci   if (block->b->b->flags & AC_PC_BLOCK_SE) {
204bf215546Sopenharmony_ci      counter->segment_type = AC_SPM_SEGMENT_TYPE_SE0; // XXX
205bf215546Sopenharmony_ci   } else {
206bf215546Sopenharmony_ci      counter->segment_type = AC_SPM_SEGMENT_TYPE_GLOBAL;
207bf215546Sopenharmony_ci   }
208bf215546Sopenharmony_ci
209bf215546Sopenharmony_ci   /* Configure the muxsel for SPM. */
210bf215546Sopenharmony_ci   ac_spm_init_muxsel(block, counter, spm_wire);
211bf215546Sopenharmony_ci
212bf215546Sopenharmony_ci   return true;
213bf215546Sopenharmony_ci}
214bf215546Sopenharmony_ci
215bf215546Sopenharmony_cibool ac_init_spm(const struct radeon_info *info,
216bf215546Sopenharmony_ci                 const struct ac_perfcounters *pc,
217bf215546Sopenharmony_ci                 unsigned num_counters,
218bf215546Sopenharmony_ci                 const struct ac_spm_counter_create_info *counters,
219bf215546Sopenharmony_ci                 struct ac_spm_trace_data *spm_trace)
220bf215546Sopenharmony_ci{
221bf215546Sopenharmony_ci   spm_trace->counters = CALLOC(num_counters, sizeof(*spm_trace->counters));
222bf215546Sopenharmony_ci   if (!spm_trace->counters)
223bf215546Sopenharmony_ci      return false;
224bf215546Sopenharmony_ci
225bf215546Sopenharmony_ci   for (unsigned i = 0; i < num_counters; i++) {
226bf215546Sopenharmony_ci      if (!ac_spm_add_counter(pc, spm_trace, &counters[i])) {
227bf215546Sopenharmony_ci         fprintf(stderr, "ac/spm: Failed to add SPM counter (%d).\n", i);
228bf215546Sopenharmony_ci         return false;
229bf215546Sopenharmony_ci      }
230bf215546Sopenharmony_ci   }
231bf215546Sopenharmony_ci
232bf215546Sopenharmony_ci   /* Determine the segment size and create a muxsel ram for every segment. */
233bf215546Sopenharmony_ci   for (unsigned s = 0; s < AC_SPM_SEGMENT_TYPE_COUNT; s++) {
234bf215546Sopenharmony_ci      unsigned num_even_counters = 0, num_odd_counters = 0;
235bf215546Sopenharmony_ci
236bf215546Sopenharmony_ci      if (s == AC_SPM_SEGMENT_TYPE_GLOBAL) {
237bf215546Sopenharmony_ci         /* The global segment always start with a 64-bit timestamp. */
238bf215546Sopenharmony_ci         num_even_counters += AC_SPM_GLOBAL_TIMESTAMP_COUNTERS;
239bf215546Sopenharmony_ci      }
240bf215546Sopenharmony_ci
241bf215546Sopenharmony_ci      /* Count the number of even/odd counters for this segment. */
242bf215546Sopenharmony_ci      for (unsigned c = 0; c < spm_trace->num_counters; c++) {
243bf215546Sopenharmony_ci         struct ac_spm_counter_info *counter = &spm_trace->counters[c];
244bf215546Sopenharmony_ci
245bf215546Sopenharmony_ci         if (counter->segment_type != s)
246bf215546Sopenharmony_ci            continue;
247bf215546Sopenharmony_ci
248bf215546Sopenharmony_ci         if (counter->is_even) {
249bf215546Sopenharmony_ci            num_even_counters++;
250bf215546Sopenharmony_ci         } else {
251bf215546Sopenharmony_ci            num_odd_counters++;
252bf215546Sopenharmony_ci         }
253bf215546Sopenharmony_ci      }
254bf215546Sopenharmony_ci
255bf215546Sopenharmony_ci      /* Compute the number of lines. */
256bf215546Sopenharmony_ci      unsigned even_lines =
257bf215546Sopenharmony_ci         DIV_ROUND_UP(num_even_counters, AC_SPM_NUM_COUNTER_PER_MUXSEL);
258bf215546Sopenharmony_ci      unsigned odd_lines =
259bf215546Sopenharmony_ci         DIV_ROUND_UP(num_odd_counters, AC_SPM_NUM_COUNTER_PER_MUXSEL);
260bf215546Sopenharmony_ci      unsigned num_lines = (even_lines > odd_lines) ? (2 * even_lines - 1) : (2 * odd_lines);
261bf215546Sopenharmony_ci
262bf215546Sopenharmony_ci      spm_trace->muxsel_lines[s] = CALLOC(num_lines, sizeof(*spm_trace->muxsel_lines[s]));
263bf215546Sopenharmony_ci      if (!spm_trace->muxsel_lines[s])
264bf215546Sopenharmony_ci         return false;
265bf215546Sopenharmony_ci      spm_trace->num_muxsel_lines[s] = num_lines;
266bf215546Sopenharmony_ci   }
267bf215546Sopenharmony_ci
268bf215546Sopenharmony_ci   /* RLC uses the following order: Global, SE0, SE1, SE2, SE3. */
269bf215546Sopenharmony_ci   const enum ac_spm_segment_type ordered_segment[AC_SPM_SEGMENT_TYPE_COUNT] =
270bf215546Sopenharmony_ci   {
271bf215546Sopenharmony_ci      AC_SPM_SEGMENT_TYPE_GLOBAL,
272bf215546Sopenharmony_ci      AC_SPM_SEGMENT_TYPE_SE0,
273bf215546Sopenharmony_ci      AC_SPM_SEGMENT_TYPE_SE1,
274bf215546Sopenharmony_ci      AC_SPM_SEGMENT_TYPE_SE2,
275bf215546Sopenharmony_ci      AC_SPM_SEGMENT_TYPE_SE3,
276bf215546Sopenharmony_ci   };
277bf215546Sopenharmony_ci
278bf215546Sopenharmony_ci   for (unsigned s = 0; s < AC_SPM_SEGMENT_TYPE_COUNT; s++) {
279bf215546Sopenharmony_ci      if (!spm_trace->muxsel_lines[s])
280bf215546Sopenharmony_ci         continue;
281bf215546Sopenharmony_ci
282bf215546Sopenharmony_ci      uint32_t segment_offset = 0;
283bf215546Sopenharmony_ci      for (unsigned i = 0; s != ordered_segment[i]; i++) {
284bf215546Sopenharmony_ci         segment_offset += spm_trace->num_muxsel_lines[ordered_segment[i]] *
285bf215546Sopenharmony_ci                           AC_SPM_NUM_COUNTER_PER_MUXSEL;
286bf215546Sopenharmony_ci      }
287bf215546Sopenharmony_ci
288bf215546Sopenharmony_ci      uint32_t even_counter_idx = 0, even_line_idx = 0;
289bf215546Sopenharmony_ci      uint32_t odd_counter_idx = 0, odd_line_idx = 1;
290bf215546Sopenharmony_ci
291bf215546Sopenharmony_ci      /* Add the global timestamps first. */
292bf215546Sopenharmony_ci      if (s == AC_SPM_SEGMENT_TYPE_GLOBAL) {
293bf215546Sopenharmony_ci         struct ac_spm_muxsel global_timestamp_muxsel = {
294bf215546Sopenharmony_ci            .counter = 0x30,
295bf215546Sopenharmony_ci            .block = 0x3,
296bf215546Sopenharmony_ci            .shader_array = 0,
297bf215546Sopenharmony_ci            .instance = 0x1e,
298bf215546Sopenharmony_ci         };
299bf215546Sopenharmony_ci
300bf215546Sopenharmony_ci         for (unsigned i = 0; i < 4; i++) {
301bf215546Sopenharmony_ci            spm_trace->muxsel_lines[s][even_line_idx].muxsel[even_counter_idx++] = global_timestamp_muxsel;
302bf215546Sopenharmony_ci         }
303bf215546Sopenharmony_ci      }
304bf215546Sopenharmony_ci
305bf215546Sopenharmony_ci      for (unsigned i = 0; i < spm_trace->num_counters; i++) {
306bf215546Sopenharmony_ci         struct ac_spm_counter_info *counter = &spm_trace->counters[i];
307bf215546Sopenharmony_ci
308bf215546Sopenharmony_ci         if (counter->segment_type != s)
309bf215546Sopenharmony_ci            continue;
310bf215546Sopenharmony_ci
311bf215546Sopenharmony_ci         if (counter->is_even) {
312bf215546Sopenharmony_ci            counter->offset = segment_offset + even_line_idx *
313bf215546Sopenharmony_ci                              AC_SPM_NUM_COUNTER_PER_MUXSEL + even_counter_idx;
314bf215546Sopenharmony_ci
315bf215546Sopenharmony_ci            spm_trace->muxsel_lines[s][even_line_idx].muxsel[even_counter_idx] = spm_trace->counters[i].muxsel;
316bf215546Sopenharmony_ci            if (++even_counter_idx == AC_SPM_NUM_COUNTER_PER_MUXSEL) {
317bf215546Sopenharmony_ci               even_counter_idx = 0;
318bf215546Sopenharmony_ci               even_line_idx += 2;
319bf215546Sopenharmony_ci            }
320bf215546Sopenharmony_ci         } else {
321bf215546Sopenharmony_ci            counter->offset = segment_offset + odd_line_idx *
322bf215546Sopenharmony_ci                              AC_SPM_NUM_COUNTER_PER_MUXSEL + odd_counter_idx;
323bf215546Sopenharmony_ci
324bf215546Sopenharmony_ci            spm_trace->muxsel_lines[s][odd_line_idx].muxsel[odd_counter_idx] = spm_trace->counters[i].muxsel;
325bf215546Sopenharmony_ci            if (++odd_counter_idx == AC_SPM_NUM_COUNTER_PER_MUXSEL) {
326bf215546Sopenharmony_ci               odd_counter_idx = 0;
327bf215546Sopenharmony_ci               odd_line_idx += 2;
328bf215546Sopenharmony_ci            }
329bf215546Sopenharmony_ci         }
330bf215546Sopenharmony_ci      }
331bf215546Sopenharmony_ci   }
332bf215546Sopenharmony_ci
333bf215546Sopenharmony_ci   return true;
334bf215546Sopenharmony_ci}
335bf215546Sopenharmony_ci
336bf215546Sopenharmony_civoid ac_destroy_spm(struct ac_spm_trace_data *spm_trace)
337bf215546Sopenharmony_ci{
338bf215546Sopenharmony_ci   for (unsigned s = 0; s < AC_SPM_SEGMENT_TYPE_COUNT; s++) {
339bf215546Sopenharmony_ci      FREE(spm_trace->muxsel_lines[s]);
340bf215546Sopenharmony_ci   }
341bf215546Sopenharmony_ci   FREE(spm_trace->block_sel);
342bf215546Sopenharmony_ci   FREE(spm_trace->counters);
343bf215546Sopenharmony_ci}
344bf215546Sopenharmony_ci
345bf215546Sopenharmony_ciuint32_t ac_spm_get_sample_size(const struct ac_spm_trace_data *spm_trace)
346bf215546Sopenharmony_ci{
347bf215546Sopenharmony_ci   uint32_t sample_size = 0; /* in bytes */
348bf215546Sopenharmony_ci
349bf215546Sopenharmony_ci   for (unsigned s = 0; s < AC_SPM_SEGMENT_TYPE_COUNT; s++) {
350bf215546Sopenharmony_ci      sample_size += spm_trace->num_muxsel_lines[s] * AC_SPM_MUXSEL_LINE_SIZE * 4;
351bf215546Sopenharmony_ci   }
352bf215546Sopenharmony_ci
353bf215546Sopenharmony_ci   return sample_size;
354bf215546Sopenharmony_ci}
355bf215546Sopenharmony_ci
356bf215546Sopenharmony_ciuint32_t ac_spm_get_num_samples(const struct ac_spm_trace_data *spm_trace)
357bf215546Sopenharmony_ci{
358bf215546Sopenharmony_ci   uint32_t sample_size = ac_spm_get_sample_size(spm_trace);
359bf215546Sopenharmony_ci   uint32_t *ptr = (uint32_t *)spm_trace->ptr;
360bf215546Sopenharmony_ci   uint32_t data_size, num_lines_written;
361bf215546Sopenharmony_ci   uint32_t num_samples = 0;
362bf215546Sopenharmony_ci
363bf215546Sopenharmony_ci   /* Get the data size (in bytes) written by the hw to the ring buffer. */
364bf215546Sopenharmony_ci   data_size = ptr[0];
365bf215546Sopenharmony_ci
366bf215546Sopenharmony_ci   /* Compute the number of 256 bits (16 * 16-bits counters) lines written. */
367bf215546Sopenharmony_ci   num_lines_written = data_size / (2 * AC_SPM_NUM_COUNTER_PER_MUXSEL);
368bf215546Sopenharmony_ci
369bf215546Sopenharmony_ci   /* Check for overflow. */
370bf215546Sopenharmony_ci   if (num_lines_written % (sample_size / 32)) {
371bf215546Sopenharmony_ci      abort();
372bf215546Sopenharmony_ci   } else {
373bf215546Sopenharmony_ci      num_samples = num_lines_written / (sample_size / 32);
374bf215546Sopenharmony_ci   }
375bf215546Sopenharmony_ci
376bf215546Sopenharmony_ci   return num_samples;
377bf215546Sopenharmony_ci}
378