1 1.1 riastrad /* $NetBSD: amdgpu_pmu.c,v 1.2 2021/12/18 23:44:58 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright 2019 Advanced Micro Devices, Inc. 5 1.1 riastrad * 6 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 7 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 8 1.1 riastrad * to deal in the Software without restriction, including without limitation 9 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 11 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 12 1.1 riastrad * 13 1.1 riastrad * The above copyright notice and this permission notice shall be included in 14 1.1 riastrad * all copies or substantial portions of the Software. 15 1.1 riastrad * 16 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 1.1 riastrad * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE. 23 1.1 riastrad * 24 1.1 riastrad * Author: Jonathan Kim <jonathan.kim (at) amd.com> 25 1.1 riastrad * 26 1.1 riastrad */ 27 1.1 riastrad 28 1.1 riastrad #include <sys/cdefs.h> 29 1.1 riastrad __KERNEL_RCSID(0, "$NetBSD: amdgpu_pmu.c,v 1.2 2021/12/18 23:44:58 riastradh Exp $"); 30 1.1 riastrad 31 1.1 riastrad #include <linux/perf_event.h> 32 1.1 riastrad #include <linux/init.h> 33 1.1 riastrad #include "amdgpu.h" 34 1.1 riastrad #include "amdgpu_pmu.h" 35 1.1 riastrad #include "df_v3_6.h" 36 1.1 riastrad 37 1.1 riastrad #define PMU_NAME_SIZE 32 38 1.1 riastrad 39 1.1 riastrad /* record to keep track of pmu entry per pmu type per device */ 40 1.1 riastrad struct amdgpu_pmu_entry { 41 1.1 riastrad struct list_head entry; 42 1.1 riastrad struct amdgpu_device *adev; 43 1.1 riastrad struct pmu pmu; 44 1.1 riastrad unsigned int pmu_perf_type; 45 1.1 riastrad }; 46 1.1 riastrad 47 1.1 riastrad static LIST_HEAD(amdgpu_pmu_list); 48 1.1 riastrad 49 1.1 riastrad 50 1.1 riastrad /* initialize perf counter */ 51 1.1 riastrad static int amdgpu_perf_event_init(struct perf_event *event) 52 1.1 riastrad { 53 1.1 riastrad struct hw_perf_event *hwc = &event->hw; 54 1.1 riastrad 55 1.1 riastrad /* test the event attr type check for PMU enumeration */ 56 1.1 riastrad if (event->attr.type != event->pmu->type) 57 1.1 riastrad return -ENOENT; 58 1.1 riastrad 59 1.1 riastrad /* update the hw_perf_event struct with config data */ 60 1.1 riastrad hwc->config = event->attr.config; 61 1.1 riastrad 62 1.1 riastrad return 0; 63 1.1 riastrad } 64 1.1 riastrad 65 1.1 riastrad /* start perf counter */ 66 1.1 riastrad static void amdgpu_perf_start(struct perf_event *event, int flags) 67 1.1 riastrad { 68 1.1 riastrad struct hw_perf_event *hwc = &event->hw; 69 1.1 riastrad struct amdgpu_pmu_entry *pe = container_of(event->pmu, 70 1.1 riastrad struct amdgpu_pmu_entry, 71 1.1 riastrad pmu); 72 1.1 riastrad 73 1.1 riastrad if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED))) 74 1.1 riastrad return; 75 1.1 riastrad 76 1.1 riastrad WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE)); 77 1.1 riastrad hwc->state = 0; 78 1.1 riastrad 79 1.1 riastrad switch (pe->pmu_perf_type) { 80 1.1 riastrad case PERF_TYPE_AMDGPU_DF: 81 1.1 riastrad if (!(flags & PERF_EF_RELOAD)) 82 1.1 riastrad pe->adev->df.funcs->pmc_start(pe->adev, hwc->config, 1); 83 1.1 riastrad 84 1.1 riastrad pe->adev->df.funcs->pmc_start(pe->adev, hwc->config, 0); 85 1.1 riastrad break; 86 1.1 riastrad default: 87 1.1 riastrad break; 88 1.1 riastrad } 89 1.1 riastrad 90 1.1 riastrad perf_event_update_userpage(event); 91 1.1 riastrad 92 1.1 riastrad } 93 1.1 riastrad 94 1.1 riastrad /* read perf counter */ 95 1.1 riastrad static void amdgpu_perf_read(struct perf_event *event) 96 1.1 riastrad { 97 1.1 riastrad struct hw_perf_event *hwc = &event->hw; 98 1.1 riastrad struct amdgpu_pmu_entry *pe = container_of(event->pmu, 99 1.1 riastrad struct amdgpu_pmu_entry, 100 1.1 riastrad pmu); 101 1.1 riastrad 102 1.1 riastrad u64 count, prev; 103 1.1 riastrad 104 1.1 riastrad do { 105 1.1 riastrad prev = local64_read(&hwc->prev_count); 106 1.1 riastrad 107 1.1 riastrad switch (pe->pmu_perf_type) { 108 1.1 riastrad case PERF_TYPE_AMDGPU_DF: 109 1.1 riastrad pe->adev->df.funcs->pmc_get_count(pe->adev, hwc->config, 110 1.1 riastrad &count); 111 1.1 riastrad break; 112 1.1 riastrad default: 113 1.1 riastrad count = 0; 114 1.1 riastrad break; 115 1.1 riastrad } 116 1.1 riastrad } while (local64_cmpxchg(&hwc->prev_count, prev, count) != prev); 117 1.1 riastrad 118 1.1 riastrad local64_add(count - prev, &event->count); 119 1.1 riastrad } 120 1.1 riastrad 121 1.1 riastrad /* stop perf counter */ 122 1.1 riastrad static void amdgpu_perf_stop(struct perf_event *event, int flags) 123 1.1 riastrad { 124 1.1 riastrad struct hw_perf_event *hwc = &event->hw; 125 1.1 riastrad struct amdgpu_pmu_entry *pe = container_of(event->pmu, 126 1.1 riastrad struct amdgpu_pmu_entry, 127 1.1 riastrad pmu); 128 1.1 riastrad 129 1.1 riastrad if (hwc->state & PERF_HES_UPTODATE) 130 1.1 riastrad return; 131 1.1 riastrad 132 1.1 riastrad switch (pe->pmu_perf_type) { 133 1.1 riastrad case PERF_TYPE_AMDGPU_DF: 134 1.1 riastrad pe->adev->df.funcs->pmc_stop(pe->adev, hwc->config, 0); 135 1.1 riastrad break; 136 1.1 riastrad default: 137 1.1 riastrad break; 138 1.1 riastrad } 139 1.1 riastrad 140 1.1 riastrad WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED); 141 1.1 riastrad hwc->state |= PERF_HES_STOPPED; 142 1.1 riastrad 143 1.1 riastrad if (hwc->state & PERF_HES_UPTODATE) 144 1.1 riastrad return; 145 1.1 riastrad 146 1.1 riastrad amdgpu_perf_read(event); 147 1.1 riastrad hwc->state |= PERF_HES_UPTODATE; 148 1.1 riastrad } 149 1.1 riastrad 150 1.1 riastrad /* add perf counter */ 151 1.1 riastrad static int amdgpu_perf_add(struct perf_event *event, int flags) 152 1.1 riastrad { 153 1.1 riastrad struct hw_perf_event *hwc = &event->hw; 154 1.1 riastrad int retval; 155 1.1 riastrad 156 1.1 riastrad struct amdgpu_pmu_entry *pe = container_of(event->pmu, 157 1.1 riastrad struct amdgpu_pmu_entry, 158 1.1 riastrad pmu); 159 1.1 riastrad 160 1.1 riastrad event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED; 161 1.1 riastrad 162 1.1 riastrad switch (pe->pmu_perf_type) { 163 1.1 riastrad case PERF_TYPE_AMDGPU_DF: 164 1.1 riastrad retval = pe->adev->df.funcs->pmc_start(pe->adev, 165 1.1 riastrad hwc->config, 1); 166 1.1 riastrad break; 167 1.1 riastrad default: 168 1.1 riastrad return 0; 169 1.1 riastrad } 170 1.1 riastrad 171 1.1 riastrad if (retval) 172 1.1 riastrad return retval; 173 1.1 riastrad 174 1.1 riastrad if (flags & PERF_EF_START) 175 1.1 riastrad amdgpu_perf_start(event, PERF_EF_RELOAD); 176 1.1 riastrad 177 1.1 riastrad return retval; 178 1.1 riastrad 179 1.1 riastrad } 180 1.1 riastrad 181 1.1 riastrad /* delete perf counter */ 182 1.1 riastrad static void amdgpu_perf_del(struct perf_event *event, int flags) 183 1.1 riastrad { 184 1.1 riastrad struct hw_perf_event *hwc = &event->hw; 185 1.1 riastrad struct amdgpu_pmu_entry *pe = container_of(event->pmu, 186 1.1 riastrad struct amdgpu_pmu_entry, 187 1.1 riastrad pmu); 188 1.1 riastrad 189 1.1 riastrad amdgpu_perf_stop(event, PERF_EF_UPDATE); 190 1.1 riastrad 191 1.1 riastrad switch (pe->pmu_perf_type) { 192 1.1 riastrad case PERF_TYPE_AMDGPU_DF: 193 1.1 riastrad pe->adev->df.funcs->pmc_stop(pe->adev, hwc->config, 1); 194 1.1 riastrad break; 195 1.1 riastrad default: 196 1.1 riastrad break; 197 1.1 riastrad } 198 1.1 riastrad 199 1.1 riastrad perf_event_update_userpage(event); 200 1.1 riastrad } 201 1.1 riastrad 202 1.1 riastrad /* vega20 pmus */ 203 1.1 riastrad 204 1.1 riastrad /* init pmu tracking per pmu type */ 205 1.1 riastrad static int init_pmu_by_type(struct amdgpu_device *adev, 206 1.1 riastrad const struct attribute_group *attr_groups[], 207 1.1 riastrad char *pmu_type_name, char *pmu_file_prefix, 208 1.1 riastrad unsigned int pmu_perf_type, 209 1.1 riastrad unsigned int num_counters) 210 1.1 riastrad { 211 1.1 riastrad char pmu_name[PMU_NAME_SIZE]; 212 1.1 riastrad struct amdgpu_pmu_entry *pmu_entry; 213 1.1 riastrad int ret = 0; 214 1.1 riastrad 215 1.1 riastrad pmu_entry = kzalloc(sizeof(struct amdgpu_pmu_entry), GFP_KERNEL); 216 1.1 riastrad 217 1.1 riastrad if (!pmu_entry) 218 1.1 riastrad return -ENOMEM; 219 1.1 riastrad 220 1.1 riastrad pmu_entry->adev = adev; 221 1.1 riastrad pmu_entry->pmu = (struct pmu){ 222 1.1 riastrad .event_init = amdgpu_perf_event_init, 223 1.1 riastrad .add = amdgpu_perf_add, 224 1.1 riastrad .del = amdgpu_perf_del, 225 1.1 riastrad .start = amdgpu_perf_start, 226 1.1 riastrad .stop = amdgpu_perf_stop, 227 1.1 riastrad .read = amdgpu_perf_read, 228 1.1 riastrad .task_ctx_nr = perf_invalid_context, 229 1.1 riastrad }; 230 1.1 riastrad 231 1.1 riastrad pmu_entry->pmu.attr_groups = attr_groups; 232 1.1 riastrad pmu_entry->pmu_perf_type = pmu_perf_type; 233 1.1 riastrad snprintf(pmu_name, PMU_NAME_SIZE, "%s_%d", 234 1.1 riastrad pmu_file_prefix, adev->ddev->primary->index); 235 1.1 riastrad 236 1.1 riastrad ret = perf_pmu_register(&pmu_entry->pmu, pmu_name, -1); 237 1.1 riastrad 238 1.1 riastrad if (ret) { 239 1.1 riastrad kfree(pmu_entry); 240 1.1 riastrad pr_warn("Error initializing AMDGPU %s PMUs.\n", pmu_type_name); 241 1.1 riastrad return ret; 242 1.1 riastrad } 243 1.1 riastrad 244 1.1 riastrad pr_info("Detected AMDGPU %s Counters. # of Counters = %d.\n", 245 1.1 riastrad pmu_type_name, num_counters); 246 1.1 riastrad 247 1.1 riastrad list_add_tail(&pmu_entry->entry, &amdgpu_pmu_list); 248 1.1 riastrad 249 1.1 riastrad return 0; 250 1.1 riastrad } 251 1.1 riastrad 252 1.1 riastrad /* init amdgpu_pmu */ 253 1.1 riastrad int amdgpu_pmu_init(struct amdgpu_device *adev) 254 1.1 riastrad { 255 1.1 riastrad int ret = 0; 256 1.1 riastrad 257 1.1 riastrad switch (adev->asic_type) { 258 1.1 riastrad case CHIP_VEGA20: 259 1.1 riastrad /* init df */ 260 1.1 riastrad ret = init_pmu_by_type(adev, df_v3_6_attr_groups, 261 1.1 riastrad "DF", "amdgpu_df", PERF_TYPE_AMDGPU_DF, 262 1.1 riastrad DF_V3_6_MAX_COUNTERS); 263 1.1 riastrad 264 1.1 riastrad /* other pmu types go here*/ 265 1.1 riastrad break; 266 1.1 riastrad default: 267 1.1 riastrad return 0; 268 1.1 riastrad } 269 1.1 riastrad 270 1.1 riastrad return 0; 271 1.1 riastrad } 272 1.1 riastrad 273 1.1 riastrad 274 1.1 riastrad /* destroy all pmu data associated with target device */ 275 1.1 riastrad void amdgpu_pmu_fini(struct amdgpu_device *adev) 276 1.1 riastrad { 277 1.1 riastrad struct amdgpu_pmu_entry *pe, *temp; 278 1.1 riastrad 279 1.1 riastrad list_for_each_entry_safe(pe, temp, &amdgpu_pmu_list, entry) { 280 1.1 riastrad if (pe->adev == adev) { 281 1.1 riastrad list_del(&pe->entry); 282 1.1 riastrad perf_pmu_unregister(&pe->pmu); 283 1.1 riastrad kfree(pe); 284 1.1 riastrad } 285 1.1 riastrad } 286 1.1 riastrad } 287