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