1/*
2 * Copyright © 2018 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#include "gen_perf.h"
25#include "gen_perf_mdapi.h"
26
27#include "dev/gen_device_info.h"
28
29int
30gen_perf_query_result_write_mdapi(void *data, uint32_t data_size,
31                                  const struct gen_device_info *devinfo,
32                                  const struct gen_perf_query_result *result,
33                                  uint64_t freq_start, uint64_t freq_end)
34{
35   switch (devinfo->gen) {
36   case 7: {
37      struct gen7_mdapi_metrics *mdapi_data = (struct gen7_mdapi_metrics *) data;
38
39      if (data_size < sizeof(*mdapi_data))
40         return 0;
41
42      assert(devinfo->is_haswell);
43
44      for (int i = 0; i < ARRAY_SIZE(mdapi_data->ACounters); i++)
45         mdapi_data->ACounters[i] = result->accumulator[1 + i];
46
47      for (int i = 0; i < ARRAY_SIZE(mdapi_data->NOACounters); i++) {
48         mdapi_data->NOACounters[i] =
49            result->accumulator[1 + ARRAY_SIZE(mdapi_data->ACounters) + i];
50      }
51
52      mdapi_data->ReportsCount = result->reports_accumulated;
53      mdapi_data->TotalTime =
54         gen_device_info_timebase_scale(devinfo, result->accumulator[0]);
55      mdapi_data->CoreFrequency = freq_end;
56      mdapi_data->CoreFrequencyChanged = freq_end != freq_start;
57      return sizeof(*mdapi_data);
58   }
59   case 8: {
60      struct gen8_mdapi_metrics *mdapi_data = (struct gen8_mdapi_metrics *) data;
61
62      if (data_size < sizeof(*mdapi_data))
63         return 0;
64
65      for (int i = 0; i < ARRAY_SIZE(mdapi_data->OaCntr); i++)
66         mdapi_data->OaCntr[i] = result->accumulator[2 + i];
67      for (int i = 0; i < ARRAY_SIZE(mdapi_data->NoaCntr); i++) {
68         mdapi_data->NoaCntr[i] =
69            result->accumulator[2 + ARRAY_SIZE(mdapi_data->OaCntr) + i];
70      }
71
72      mdapi_data->ReportId = result->hw_id;
73      mdapi_data->ReportsCount = result->reports_accumulated;
74      mdapi_data->TotalTime =
75         gen_device_info_timebase_scale(devinfo, result->accumulator[0]);
76      mdapi_data->GPUTicks = result->accumulator[1];
77      mdapi_data->CoreFrequency = freq_end;
78      mdapi_data->CoreFrequencyChanged = freq_end != freq_start;
79      mdapi_data->SliceFrequency =
80         (result->slice_frequency[0] + result->slice_frequency[1]) / 2ULL;
81      mdapi_data->UnsliceFrequency =
82         (result->unslice_frequency[0] + result->unslice_frequency[1]) / 2ULL;
83      return sizeof(*mdapi_data);
84   }
85   case 9:
86   case 10:
87   case 11: {
88      struct gen9_mdapi_metrics *mdapi_data = (struct gen9_mdapi_metrics *) data;
89
90      if (data_size < sizeof(*mdapi_data))
91         return 0;
92
93      for (int i = 0; i < ARRAY_SIZE(mdapi_data->OaCntr); i++)
94         mdapi_data->OaCntr[i] = result->accumulator[2 + i];
95      for (int i = 0; i < ARRAY_SIZE(mdapi_data->NoaCntr); i++) {
96         mdapi_data->NoaCntr[i] =
97            result->accumulator[2 + ARRAY_SIZE(mdapi_data->OaCntr) + i];
98      }
99
100      mdapi_data->ReportId = result->hw_id;
101      mdapi_data->ReportsCount = result->reports_accumulated;
102      mdapi_data->TotalTime =
103         gen_device_info_timebase_scale(devinfo, result->accumulator[0]);
104      mdapi_data->GPUTicks = result->accumulator[1];
105      mdapi_data->CoreFrequency = freq_end;
106      mdapi_data->CoreFrequencyChanged = freq_end != freq_start;
107      mdapi_data->SliceFrequency =
108         (result->slice_frequency[0] + result->slice_frequency[1]) / 2ULL;
109      mdapi_data->UnsliceFrequency =
110         (result->unslice_frequency[0] + result->unslice_frequency[1]) / 2ULL;
111      return sizeof(*mdapi_data);
112   }
113   default:
114      unreachable("unexpected gen");
115   }
116}
117