17ec681f3Smrg/*
27ec681f3Smrg * Copyright © 2021 Collabora, Ltd.
37ec681f3Smrg * Author: Antonio Caggiano <antonio.caggiano@collabora.com>
47ec681f3Smrg *
57ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a copy
67ec681f3Smrg * of this software and associated documentation files (the "Software"), to deal
77ec681f3Smrg * in the Software without restriction, including without limitation the rights
87ec681f3Smrg * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
97ec681f3Smrg * copies of the Software, and to permit persons to whom the Software is
107ec681f3Smrg * furnished to do so, subject to the following conditions:
117ec681f3Smrg *
127ec681f3Smrg * The above copyright notice and this permission notice shall be included in
137ec681f3Smrg * all copies or substantial portions of the Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
187ec681f3Smrg * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
207ec681f3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
217ec681f3Smrg * THE SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg#include "pan_perf.h"
257ec681f3Smrg
267ec681f3Smrg#include <pan_perf_metrics.h>
277ec681f3Smrg#include <lib/pan_device.h>
287ec681f3Smrg#include <drm-uapi/panfrost_drm.h>
297ec681f3Smrg
307ec681f3Smrg#define PAN_COUNTERS_PER_CATEGORY 64
317ec681f3Smrg#define PAN_SHADER_CORE_INDEX 2
327ec681f3Smrg
337ec681f3Smrguint32_t
347ec681f3Smrgpanfrost_perf_counter_read(const struct panfrost_perf_counter *counter,
357ec681f3Smrg                           const struct panfrost_perf *perf)
367ec681f3Smrg{
377ec681f3Smrg   assert(counter->offset < perf->n_counter_values);
387ec681f3Smrg   uint32_t ret = perf->counter_values[counter->offset];
397ec681f3Smrg
407ec681f3Smrg   // If counter belongs to shader core, accumulate values for all other cores
417ec681f3Smrg   if (counter->category == &perf->cfg->categories[PAN_SHADER_CORE_INDEX]) {
427ec681f3Smrg      for (uint32_t core = 1; core < perf->dev->core_count; ++core) {
437ec681f3Smrg         ret += perf->counter_values[counter->offset + PAN_COUNTERS_PER_CATEGORY * core];
447ec681f3Smrg      }
457ec681f3Smrg   }
467ec681f3Smrg
477ec681f3Smrg   return ret;
487ec681f3Smrg}
497ec681f3Smrg
507ec681f3Smrgstatic const struct panfrost_perf_config*
517ec681f3Smrgget_perf_config(unsigned int gpu_id)
527ec681f3Smrg{
537ec681f3Smrg   switch (gpu_id) {
547ec681f3Smrg   case 0x720:
557ec681f3Smrg      return &panfrost_perf_config_t72x;
567ec681f3Smrg   case 0x750:
577ec681f3Smrg      return &panfrost_perf_config_t76x;
587ec681f3Smrg   case 0x820:
597ec681f3Smrg      return &panfrost_perf_config_t82x;
607ec681f3Smrg   case 0x830:
617ec681f3Smrg      return &panfrost_perf_config_t83x;
627ec681f3Smrg   case 0x860:
637ec681f3Smrg      return &panfrost_perf_config_t86x;
647ec681f3Smrg   case 0x880:
657ec681f3Smrg      return &panfrost_perf_config_t88x;
667ec681f3Smrg   case 0x6221:
677ec681f3Smrg      return &panfrost_perf_config_thex;
687ec681f3Smrg   case 0x7093:
697ec681f3Smrg      return &panfrost_perf_config_tdvx;
707ec681f3Smrg   case 0x7212:
717ec681f3Smrg   case 0x7402:
727ec681f3Smrg      return &panfrost_perf_config_tgox;
737ec681f3Smrg   default:
747ec681f3Smrg      unreachable("Invalid GPU ID");
757ec681f3Smrg   }
767ec681f3Smrg}
777ec681f3Smrg
787ec681f3Smrgvoid
797ec681f3Smrgpanfrost_perf_init(struct panfrost_perf *perf, struct panfrost_device *dev)
807ec681f3Smrg{
817ec681f3Smrg   perf->dev = dev;
827ec681f3Smrg   perf->cfg = get_perf_config(dev->gpu_id);
837ec681f3Smrg
847ec681f3Smrg   // Generally counter blocks are laid out in the following order:
857ec681f3Smrg   // Job manager, tiler, L2 cache, and one or more shader cores.
867ec681f3Smrg   uint32_t n_blocks = 3 + dev->core_count;
877ec681f3Smrg   perf->n_counter_values = PAN_COUNTERS_PER_CATEGORY * n_blocks;
887ec681f3Smrg   perf->counter_values = ralloc_array(perf, uint32_t, perf->n_counter_values);
897ec681f3Smrg}
907ec681f3Smrg
917ec681f3Smrgstatic int
927ec681f3Smrgpanfrost_perf_query(struct panfrost_perf *perf, uint32_t enable)
937ec681f3Smrg{
947ec681f3Smrg   struct drm_panfrost_perfcnt_enable perfcnt_enable = {enable, 0};
957ec681f3Smrg   return drmIoctl(perf->dev->fd, DRM_IOCTL_PANFROST_PERFCNT_ENABLE, &perfcnt_enable);
967ec681f3Smrg}
977ec681f3Smrg
987ec681f3Smrgint
997ec681f3Smrgpanfrost_perf_enable(struct panfrost_perf *perf)
1007ec681f3Smrg{
1017ec681f3Smrg   return panfrost_perf_query(perf, 1 /* enable */);
1027ec681f3Smrg}
1037ec681f3Smrg
1047ec681f3Smrgint
1057ec681f3Smrgpanfrost_perf_disable(struct panfrost_perf *perf)
1067ec681f3Smrg{
1077ec681f3Smrg   return panfrost_perf_query(perf, 0 /* disable */);
1087ec681f3Smrg}
1097ec681f3Smrg
1107ec681f3Smrgint
1117ec681f3Smrgpanfrost_perf_dump(struct panfrost_perf *perf)
1127ec681f3Smrg{
1137ec681f3Smrg   // Dump performance counter values to the memory buffer pointed to by counter_values
1147ec681f3Smrg   struct drm_panfrost_perfcnt_dump perfcnt_dump = {(uint64_t)(uintptr_t)perf->counter_values};
1157ec681f3Smrg   return drmIoctl(perf->dev->fd, DRM_IOCTL_PANFROST_PERFCNT_DUMP, &perfcnt_dump);
1167ec681f3Smrg}
117