1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2012 Fabien Thomas 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/pmc.h> 34 #include <sys/pmckern.h> 35 #include <sys/systm.h> 36 #include <sys/mutex.h> 37 38 #include <machine/cpu.h> 39 #include <machine/cpufunc.h> 40 41 #include "hwpmc_soft.h" 42 43 /* 44 * Software PMC support. 45 */ 46 47 #define SOFT_CAPS (PMC_CAP_READ | PMC_CAP_WRITE | PMC_CAP_INTERRUPT | \ 48 PMC_CAP_USER | PMC_CAP_SYSTEM) 49 50 struct soft_descr { 51 struct pmc_descr pm_descr; /* "base class" */ 52 }; 53 54 static struct soft_descr soft_pmcdesc[SOFT_NPMCS] = 55 { 56 #define SOFT_PMCDESCR(N) \ 57 { \ 58 .pm_descr = \ 59 { \ 60 .pd_name = #N, \ 61 .pd_class = PMC_CLASS_SOFT, \ 62 .pd_caps = SOFT_CAPS, \ 63 .pd_width = 64 \ 64 }, \ 65 } 66 67 SOFT_PMCDESCR(SOFT0), 68 SOFT_PMCDESCR(SOFT1), 69 SOFT_PMCDESCR(SOFT2), 70 SOFT_PMCDESCR(SOFT3), 71 SOFT_PMCDESCR(SOFT4), 72 SOFT_PMCDESCR(SOFT5), 73 SOFT_PMCDESCR(SOFT6), 74 SOFT_PMCDESCR(SOFT7), 75 SOFT_PMCDESCR(SOFT8), 76 SOFT_PMCDESCR(SOFT9), 77 SOFT_PMCDESCR(SOFT10), 78 SOFT_PMCDESCR(SOFT11), 79 SOFT_PMCDESCR(SOFT12), 80 SOFT_PMCDESCR(SOFT13), 81 SOFT_PMCDESCR(SOFT14), 82 SOFT_PMCDESCR(SOFT15) 83 }; 84 85 /* 86 * Per-CPU data structure. 87 */ 88 89 struct soft_cpu { 90 struct pmc_hw soft_hw[SOFT_NPMCS]; 91 pmc_value_t soft_values[SOFT_NPMCS]; 92 }; 93 94 95 static struct soft_cpu **soft_pcpu; 96 97 static int 98 soft_allocate_pmc(int cpu, int ri, struct pmc *pm, 99 const struct pmc_op_pmcallocate *a) 100 { 101 enum pmc_event ev; 102 struct pmc_soft *ps; 103 104 (void) cpu; 105 106 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 107 ("[soft,%d] illegal CPU value %d", __LINE__, cpu)); 108 KASSERT(ri >= 0 && ri < SOFT_NPMCS, 109 ("[soft,%d] illegal row-index %d", __LINE__, ri)); 110 111 if (a->pm_class != PMC_CLASS_SOFT) 112 return (EINVAL); 113 114 if ((pm->pm_caps & SOFT_CAPS) == 0) 115 return (EINVAL); 116 117 if ((pm->pm_caps & ~SOFT_CAPS) != 0) 118 return (EPERM); 119 120 ev = pm->pm_event; 121 if ((int)ev < PMC_EV_SOFT_FIRST || (int)ev > PMC_EV_SOFT_LAST) 122 return (EINVAL); 123 124 /* Check if event is registered. */ 125 ps = pmc_soft_ev_acquire(ev); 126 if (ps == NULL) 127 return (EINVAL); 128 pmc_soft_ev_release(ps); 129 /* Module unload is protected by pmc SX lock. */ 130 if (ps->ps_alloc != NULL) 131 ps->ps_alloc(); 132 133 return (0); 134 } 135 136 static int 137 soft_config_pmc(int cpu, int ri, struct pmc *pm) 138 { 139 struct pmc_hw *phw; 140 141 PMCDBG3(MDP,CFG,1, "cpu=%d ri=%d pm=%p", cpu, ri, pm); 142 143 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 144 ("[soft,%d] illegal CPU value %d", __LINE__, cpu)); 145 KASSERT(ri >= 0 && ri < SOFT_NPMCS, 146 ("[soft,%d] illegal row-index %d", __LINE__, ri)); 147 148 phw = &soft_pcpu[cpu]->soft_hw[ri]; 149 150 KASSERT(pm == NULL || phw->phw_pmc == NULL, 151 ("[soft,%d] pm=%p phw->pm=%p hwpmc not unconfigured", __LINE__, 152 pm, phw->phw_pmc)); 153 154 phw->phw_pmc = pm; 155 156 return (0); 157 } 158 159 static int 160 soft_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc) 161 { 162 const struct soft_descr *pd; 163 struct pmc_hw *phw; 164 165 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 166 ("[soft,%d] illegal CPU %d", __LINE__, cpu)); 167 KASSERT(ri >= 0 && ri < SOFT_NPMCS, 168 ("[soft,%d] illegal row-index %d", __LINE__, ri)); 169 170 phw = &soft_pcpu[cpu]->soft_hw[ri]; 171 pd = &soft_pmcdesc[ri]; 172 173 strlcpy(pi->pm_name, pd->pm_descr.pd_name, sizeof(pi->pm_name)); 174 pi->pm_class = pd->pm_descr.pd_class; 175 176 if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) { 177 pi->pm_enabled = TRUE; 178 *ppmc = phw->phw_pmc; 179 } else { 180 pi->pm_enabled = FALSE; 181 *ppmc = NULL; 182 } 183 184 return (0); 185 } 186 187 static int 188 soft_get_config(int cpu, int ri, struct pmc **ppm) 189 { 190 (void) ri; 191 192 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 193 ("[soft,%d] illegal CPU %d", __LINE__, cpu)); 194 KASSERT(ri >= 0 && ri < SOFT_NPMCS, 195 ("[soft,%d] illegal row-index %d", __LINE__, ri)); 196 197 *ppm = soft_pcpu[cpu]->soft_hw[ri].phw_pmc; 198 return (0); 199 } 200 201 static int 202 soft_pcpu_fini(struct pmc_mdep *md, int cpu) 203 { 204 int ri; 205 struct pmc_cpu *pc; 206 207 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 208 ("[soft,%d] illegal cpu %d", __LINE__, cpu)); 209 KASSERT(soft_pcpu[cpu] != NULL, ("[soft,%d] null pcpu", __LINE__)); 210 211 free(soft_pcpu[cpu], M_PMC); 212 soft_pcpu[cpu] = NULL; 213 214 ri = md->pmd_classdep[PMC_CLASS_INDEX_SOFT].pcd_ri; 215 216 KASSERT(ri >= 0 && ri < SOFT_NPMCS, 217 ("[soft,%d] ri=%d", __LINE__, ri)); 218 219 pc = pmc_pcpu[cpu]; 220 pc->pc_hwpmcs[ri] = NULL; 221 222 return (0); 223 } 224 225 static int 226 soft_pcpu_init(struct pmc_mdep *md, int cpu) 227 { 228 int first_ri, n; 229 struct pmc_cpu *pc; 230 struct soft_cpu *soft_pc; 231 struct pmc_hw *phw; 232 233 234 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 235 ("[soft,%d] illegal cpu %d", __LINE__, cpu)); 236 KASSERT(soft_pcpu, ("[soft,%d] null pcpu", __LINE__)); 237 KASSERT(soft_pcpu[cpu] == NULL, ("[soft,%d] non-null per-cpu", 238 __LINE__)); 239 240 soft_pc = malloc(sizeof(struct soft_cpu), M_PMC, M_WAITOK|M_ZERO); 241 pc = pmc_pcpu[cpu]; 242 243 KASSERT(pc != NULL, ("[soft,%d] cpu %d null per-cpu", __LINE__, cpu)); 244 245 soft_pcpu[cpu] = soft_pc; 246 phw = soft_pc->soft_hw; 247 first_ri = md->pmd_classdep[PMC_CLASS_INDEX_SOFT].pcd_ri; 248 249 for (n = 0; n < SOFT_NPMCS; n++, phw++) { 250 phw->phw_state = PMC_PHW_FLAG_IS_ENABLED | 251 PMC_PHW_CPU_TO_STATE(cpu) | PMC_PHW_INDEX_TO_STATE(n); 252 phw->phw_pmc = NULL; 253 pc->pc_hwpmcs[n + first_ri] = phw; 254 } 255 256 return (0); 257 } 258 259 static int 260 soft_read_pmc(int cpu, int ri, struct pmc *pm __unused, pmc_value_t *v) 261 { 262 263 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 264 ("[soft,%d] illegal CPU value %d", __LINE__, cpu)); 265 KASSERT(ri >= 0 && ri < SOFT_NPMCS, 266 ("[soft,%d] illegal row-index %d", __LINE__, ri)); 267 268 PMCDBG1(MDP,REA,1,"soft-read id=%d", ri); 269 270 *v = soft_pcpu[cpu]->soft_values[ri]; 271 272 return (0); 273 } 274 275 static int 276 soft_write_pmc(int cpu, int ri, struct pmc *pm __unused, pmc_value_t v) 277 { 278 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 279 ("[soft,%d] illegal cpu value %d", __LINE__, cpu)); 280 KASSERT(ri >= 0 && ri < SOFT_NPMCS, 281 ("[soft,%d] illegal row-index %d", __LINE__, ri)); 282 283 PMCDBG3(MDP,WRI,1, "soft-write cpu=%d ri=%d v=%jx", cpu, ri, v); 284 285 soft_pcpu[cpu]->soft_values[ri] = v; 286 287 return (0); 288 } 289 290 static int 291 soft_release_pmc(int cpu, int ri, struct pmc *pmc) 292 { 293 struct pmc_hw *phw __diagused; 294 enum pmc_event ev; 295 struct pmc_soft *ps; 296 297 (void) pmc; 298 299 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 300 ("[soft,%d] illegal CPU value %d", __LINE__, cpu)); 301 KASSERT(ri >= 0 && ri < SOFT_NPMCS, 302 ("[soft,%d] illegal row-index %d", __LINE__, ri)); 303 304 phw = &soft_pcpu[cpu]->soft_hw[ri]; 305 306 KASSERT(phw->phw_pmc == NULL, 307 ("[soft,%d] PHW pmc %p non-NULL", __LINE__, phw->phw_pmc)); 308 309 ev = pmc->pm_event; 310 311 /* Check if event is registered. */ 312 ps = pmc_soft_ev_acquire(ev); 313 KASSERT(ps != NULL, 314 ("[soft,%d] unregistered event %d", __LINE__, ev)); 315 pmc_soft_ev_release(ps); 316 /* Module unload is protected by pmc SX lock. */ 317 if (ps->ps_release != NULL) 318 ps->ps_release(); 319 return (0); 320 } 321 322 static int 323 soft_start_pmc(int cpu, int ri, struct pmc *pm) 324 { 325 struct pmc_soft *ps; 326 327 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 328 ("[soft,%d] illegal CPU value %d", __LINE__, cpu)); 329 KASSERT(ri >= 0 && ri < SOFT_NPMCS, 330 ("[soft,%d] illegal row-index %d", __LINE__, ri)); 331 332 ps = pmc_soft_ev_acquire(pm->pm_event); 333 if (ps == NULL) 334 return (EINVAL); 335 atomic_add_int(&ps->ps_running, 1); 336 pmc_soft_ev_release(ps); 337 338 return (0); 339 } 340 341 static int 342 soft_stop_pmc(int cpu, int ri, struct pmc *pm) 343 { 344 struct pmc_soft *ps; 345 346 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 347 ("[soft,%d] illegal CPU value %d", __LINE__, cpu)); 348 KASSERT(ri >= 0 && ri < SOFT_NPMCS, 349 ("[soft,%d] illegal row-index %d", __LINE__, ri)); 350 351 ps = pmc_soft_ev_acquire(pm->pm_event); 352 /* event unregistered ? */ 353 if (ps != NULL) { 354 atomic_subtract_int(&ps->ps_running, 1); 355 pmc_soft_ev_release(ps); 356 } 357 358 return (0); 359 } 360 361 int 362 pmc_soft_intr(struct pmckern_soft *ks) 363 { 364 struct pmc *pm; 365 struct soft_cpu *pc; 366 int ri, processed, error, user_mode; 367 368 KASSERT(ks->pm_cpu >= 0 && ks->pm_cpu < pmc_cpu_max(), 369 ("[soft,%d] CPU %d out of range", __LINE__, ks->pm_cpu)); 370 371 processed = 0; 372 pc = soft_pcpu[ks->pm_cpu]; 373 374 for (ri = 0; ri < SOFT_NPMCS; ri++) { 375 376 pm = pc->soft_hw[ri].phw_pmc; 377 if (pm == NULL || 378 pm->pm_state != PMC_STATE_RUNNING || 379 pm->pm_event != ks->pm_ev) { 380 continue; 381 } 382 383 processed = 1; 384 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm))) { 385 if ((pc->soft_values[ri]--) <= 0) 386 pc->soft_values[ri] += pm->pm_sc.pm_reloadcount; 387 else 388 continue; 389 user_mode = TRAPF_USERMODE(ks->pm_tf); 390 error = pmc_process_interrupt(PMC_SR, pm, ks->pm_tf); 391 if (error) { 392 soft_stop_pmc(ks->pm_cpu, ri, pm); 393 continue; 394 } 395 396 if (user_mode) { 397 /* 398 * If in user mode setup AST to process 399 * callchain out of interrupt context. 400 */ 401 ast_sched(curthread, TDA_HWPMC); 402 } 403 } else 404 pc->soft_values[ri]++; 405 } 406 if (processed) 407 counter_u64_add(pmc_stats.pm_intr_processed, 1); 408 else 409 counter_u64_add(pmc_stats.pm_intr_ignored, 1); 410 411 return (processed); 412 } 413 414 static void 415 ast_hwpmc(struct thread *td, int tda __unused) 416 { 417 /* Handle Software PMC callchain capture. */ 418 if (PMC_IS_PENDING_CALLCHAIN(td)) 419 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_USER_CALLCHAIN_SOFT, 420 (void *)td->td_frame); 421 } 422 423 void 424 pmc_soft_initialize(struct pmc_mdep *md) 425 { 426 struct pmc_classdep *pcd; 427 428 /* Add SOFT PMCs. */ 429 soft_pcpu = malloc(sizeof(struct soft_cpu *) * pmc_cpu_max(), M_PMC, 430 M_ZERO|M_WAITOK); 431 432 pcd = &md->pmd_classdep[PMC_CLASS_INDEX_SOFT]; 433 434 pcd->pcd_caps = SOFT_CAPS; 435 pcd->pcd_class = PMC_CLASS_SOFT; 436 pcd->pcd_num = SOFT_NPMCS; 437 pcd->pcd_ri = md->pmd_npmc; 438 pcd->pcd_width = 64; 439 440 pcd->pcd_allocate_pmc = soft_allocate_pmc; 441 pcd->pcd_config_pmc = soft_config_pmc; 442 pcd->pcd_describe = soft_describe; 443 pcd->pcd_get_config = soft_get_config; 444 pcd->pcd_get_msr = NULL; 445 pcd->pcd_pcpu_init = soft_pcpu_init; 446 pcd->pcd_pcpu_fini = soft_pcpu_fini; 447 pcd->pcd_read_pmc = soft_read_pmc; 448 pcd->pcd_write_pmc = soft_write_pmc; 449 pcd->pcd_release_pmc = soft_release_pmc; 450 pcd->pcd_start_pmc = soft_start_pmc; 451 pcd->pcd_stop_pmc = soft_stop_pmc; 452 453 md->pmd_npmc += SOFT_NPMCS; 454 455 ast_register(TDA_HWPMC, ASTR_UNCOND, 0, ast_hwpmc); 456 } 457 458 void 459 pmc_soft_finalize(struct pmc_mdep *md) 460 { 461 #ifdef INVARIANTS 462 int i, ncpus; 463 464 ncpus = pmc_cpu_max(); 465 for (i = 0; i < ncpus; i++) 466 KASSERT(soft_pcpu[i] == NULL, ("[soft,%d] non-null pcpu cpu %d", 467 __LINE__, i)); 468 469 KASSERT(md->pmd_classdep[PMC_CLASS_INDEX_SOFT].pcd_class == 470 PMC_CLASS_SOFT, ("[soft,%d] class mismatch", __LINE__)); 471 #endif 472 ast_deregister(TDA_HWPMC); 473 free(soft_pcpu, M_PMC); 474 soft_pcpu = NULL; 475 } 476