xref: /freebsd/sys/dev/hwpmc/hwpmc_soft.c (revision fdafd315)
1f5f9340bSFabien Thomas /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
30f5f9340bSFabien Thomas #include <sys/pmc.h>
31f5f9340bSFabien Thomas #include <sys/pmckern.h>
32f5f9340bSFabien Thomas #include <sys/systm.h>
33f5f9340bSFabien Thomas #include <sys/mutex.h>
34f5f9340bSFabien Thomas 
35f5f9340bSFabien Thomas #include <machine/cpu.h>
36f5f9340bSFabien Thomas #include <machine/cpufunc.h>
37f5f9340bSFabien Thomas 
38f5f9340bSFabien Thomas #include "hwpmc_soft.h"
39f5f9340bSFabien Thomas 
40f5f9340bSFabien Thomas /*
41f5f9340bSFabien Thomas  * Software PMC support.
42f5f9340bSFabien Thomas  */
43f5f9340bSFabien Thomas 
44f5f9340bSFabien Thomas #define	SOFT_CAPS (PMC_CAP_READ | PMC_CAP_WRITE | PMC_CAP_INTERRUPT | \
45f5f9340bSFabien Thomas     PMC_CAP_USER | PMC_CAP_SYSTEM)
46f5f9340bSFabien Thomas 
47f5f9340bSFabien Thomas struct soft_descr {
48f5f9340bSFabien Thomas 	struct pmc_descr pm_descr;  /* "base class" */
49f5f9340bSFabien Thomas };
50f5f9340bSFabien Thomas 
51f5f9340bSFabien Thomas static struct soft_descr soft_pmcdesc[SOFT_NPMCS] =
52f5f9340bSFabien Thomas {
53f5f9340bSFabien Thomas #define	SOFT_PMCDESCR(N)				\
54f5f9340bSFabien Thomas 	{						\
55f5f9340bSFabien Thomas 		.pm_descr =				\
56f5f9340bSFabien Thomas 		{					\
57f5f9340bSFabien Thomas 			.pd_name = #N,			\
58f5f9340bSFabien Thomas 			.pd_class = PMC_CLASS_SOFT,	\
59f5f9340bSFabien Thomas 			.pd_caps = SOFT_CAPS,		\
60f5f9340bSFabien Thomas 			.pd_width = 64			\
61f5f9340bSFabien Thomas 		},					\
62f5f9340bSFabien Thomas 	}
63f5f9340bSFabien Thomas 
64f5f9340bSFabien Thomas 	SOFT_PMCDESCR(SOFT0),
65f5f9340bSFabien Thomas 	SOFT_PMCDESCR(SOFT1),
66f5f9340bSFabien Thomas 	SOFT_PMCDESCR(SOFT2),
67f5f9340bSFabien Thomas 	SOFT_PMCDESCR(SOFT3),
68f5f9340bSFabien Thomas 	SOFT_PMCDESCR(SOFT4),
69f5f9340bSFabien Thomas 	SOFT_PMCDESCR(SOFT5),
70f5f9340bSFabien Thomas 	SOFT_PMCDESCR(SOFT6),
71f5f9340bSFabien Thomas 	SOFT_PMCDESCR(SOFT7),
72f5f9340bSFabien Thomas 	SOFT_PMCDESCR(SOFT8),
73f5f9340bSFabien Thomas 	SOFT_PMCDESCR(SOFT9),
74f5f9340bSFabien Thomas 	SOFT_PMCDESCR(SOFT10),
75f5f9340bSFabien Thomas 	SOFT_PMCDESCR(SOFT11),
76f5f9340bSFabien Thomas 	SOFT_PMCDESCR(SOFT12),
77f5f9340bSFabien Thomas 	SOFT_PMCDESCR(SOFT13),
78f5f9340bSFabien Thomas 	SOFT_PMCDESCR(SOFT14),
79f5f9340bSFabien Thomas 	SOFT_PMCDESCR(SOFT15)
80f5f9340bSFabien Thomas };
81f5f9340bSFabien Thomas 
82f5f9340bSFabien Thomas /*
83f5f9340bSFabien Thomas  * Per-CPU data structure.
84f5f9340bSFabien Thomas  */
85f5f9340bSFabien Thomas 
86f5f9340bSFabien Thomas struct soft_cpu {
87f5f9340bSFabien Thomas 	struct pmc_hw	soft_hw[SOFT_NPMCS];
88f5f9340bSFabien Thomas 	pmc_value_t	soft_values[SOFT_NPMCS];
89f5f9340bSFabien Thomas };
90f5f9340bSFabien Thomas 
91f5f9340bSFabien Thomas 
92f5f9340bSFabien Thomas static struct soft_cpu **soft_pcpu;
93f5f9340bSFabien Thomas 
94f5f9340bSFabien Thomas static int
soft_allocate_pmc(int cpu,int ri,struct pmc * pm,const struct pmc_op_pmcallocate * a)95f5f9340bSFabien Thomas soft_allocate_pmc(int cpu, int ri, struct pmc *pm,
96f5f9340bSFabien Thomas     const struct pmc_op_pmcallocate *a)
97f5f9340bSFabien Thomas {
98f5f9340bSFabien Thomas 	enum pmc_event ev;
99f5f9340bSFabien Thomas 	struct pmc_soft *ps;
100f5f9340bSFabien Thomas 
101f5f9340bSFabien Thomas 	(void) cpu;
102f5f9340bSFabien Thomas 
103f5f9340bSFabien Thomas 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
104f5f9340bSFabien Thomas 	    ("[soft,%d] illegal CPU value %d", __LINE__, cpu));
105f5f9340bSFabien Thomas 	KASSERT(ri >= 0 && ri < SOFT_NPMCS,
106f5f9340bSFabien Thomas 	    ("[soft,%d] illegal row-index %d", __LINE__, ri));
107f5f9340bSFabien Thomas 
108f5f9340bSFabien Thomas 	if (a->pm_class != PMC_CLASS_SOFT)
109f5f9340bSFabien Thomas 		return (EINVAL);
110f5f9340bSFabien Thomas 
111f5f9340bSFabien Thomas 	if ((pm->pm_caps & SOFT_CAPS) == 0)
112f5f9340bSFabien Thomas 		return (EINVAL);
113f5f9340bSFabien Thomas 
114f5f9340bSFabien Thomas 	if ((pm->pm_caps & ~SOFT_CAPS) != 0)
115f5f9340bSFabien Thomas 		return (EPERM);
116f5f9340bSFabien Thomas 
117f5f9340bSFabien Thomas 	ev = pm->pm_event;
118ca57f64fSSean Bruno 	if ((int)ev < PMC_EV_SOFT_FIRST || (int)ev > PMC_EV_SOFT_LAST)
119f5f9340bSFabien Thomas 		return (EINVAL);
120f5f9340bSFabien Thomas 
121f5f9340bSFabien Thomas 	/* Check if event is registered. */
122f5f9340bSFabien Thomas 	ps = pmc_soft_ev_acquire(ev);
123f5f9340bSFabien Thomas 	if (ps == NULL)
124f5f9340bSFabien Thomas 		return (EINVAL);
125f5f9340bSFabien Thomas 	pmc_soft_ev_release(ps);
126d49302aeSFabien Thomas 	/* Module unload is protected by pmc SX lock. */
127d49302aeSFabien Thomas 	if (ps->ps_alloc != NULL)
128d49302aeSFabien Thomas 		ps->ps_alloc();
129f5f9340bSFabien Thomas 
130f5f9340bSFabien Thomas 	return (0);
131f5f9340bSFabien Thomas }
132f5f9340bSFabien Thomas 
133f5f9340bSFabien Thomas static int
soft_config_pmc(int cpu,int ri,struct pmc * pm)134f5f9340bSFabien Thomas soft_config_pmc(int cpu, int ri, struct pmc *pm)
135f5f9340bSFabien Thomas {
136f5f9340bSFabien Thomas 	struct pmc_hw *phw;
137f5f9340bSFabien Thomas 
1384a3690dfSJohn Baldwin 	PMCDBG3(MDP,CFG,1, "cpu=%d ri=%d pm=%p", cpu, ri, pm);
139f5f9340bSFabien Thomas 
140f5f9340bSFabien Thomas 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
141f5f9340bSFabien Thomas 	    ("[soft,%d] illegal CPU value %d", __LINE__, cpu));
142f5f9340bSFabien Thomas 	KASSERT(ri >= 0 && ri < SOFT_NPMCS,
143f5f9340bSFabien Thomas 	    ("[soft,%d] illegal row-index %d", __LINE__, ri));
144f5f9340bSFabien Thomas 
145f5f9340bSFabien Thomas 	phw = &soft_pcpu[cpu]->soft_hw[ri];
146f5f9340bSFabien Thomas 
147f5f9340bSFabien Thomas 	KASSERT(pm == NULL || phw->phw_pmc == NULL,
148f5f9340bSFabien Thomas 	    ("[soft,%d] pm=%p phw->pm=%p hwpmc not unconfigured", __LINE__,
149f5f9340bSFabien Thomas 	    pm, phw->phw_pmc));
150f5f9340bSFabien Thomas 
151f5f9340bSFabien Thomas 	phw->phw_pmc = pm;
152f5f9340bSFabien Thomas 
153f5f9340bSFabien Thomas 	return (0);
154f5f9340bSFabien Thomas }
155f5f9340bSFabien Thomas 
156f5f9340bSFabien Thomas static int
soft_describe(int cpu,int ri,struct pmc_info * pi,struct pmc ** ppmc)157f5f9340bSFabien Thomas soft_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
158f5f9340bSFabien Thomas {
159f5f9340bSFabien Thomas 	const struct soft_descr *pd;
160f5f9340bSFabien Thomas 	struct pmc_hw *phw;
161f5f9340bSFabien Thomas 
162f5f9340bSFabien Thomas 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
163f5f9340bSFabien Thomas 	    ("[soft,%d] illegal CPU %d", __LINE__, cpu));
164f5f9340bSFabien Thomas 	KASSERT(ri >= 0 && ri < SOFT_NPMCS,
165f5f9340bSFabien Thomas 	    ("[soft,%d] illegal row-index %d", __LINE__, ri));
166f5f9340bSFabien Thomas 
167f5f9340bSFabien Thomas 	phw = &soft_pcpu[cpu]->soft_hw[ri];
168f5f9340bSFabien Thomas 	pd  = &soft_pmcdesc[ri];
169f5f9340bSFabien Thomas 
17031610e34SMitchell Horne 	strlcpy(pi->pm_name, pd->pm_descr.pd_name, sizeof(pi->pm_name));
171f5f9340bSFabien Thomas 	pi->pm_class = pd->pm_descr.pd_class;
172f5f9340bSFabien Thomas 
173f5f9340bSFabien Thomas 	if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) {
174f5f9340bSFabien Thomas 		pi->pm_enabled = TRUE;
175f5f9340bSFabien Thomas 		*ppmc          = phw->phw_pmc;
176f5f9340bSFabien Thomas 	} else {
177f5f9340bSFabien Thomas 		pi->pm_enabled = FALSE;
178f5f9340bSFabien Thomas 		*ppmc          = NULL;
179f5f9340bSFabien Thomas 	}
180f5f9340bSFabien Thomas 
181f5f9340bSFabien Thomas 	return (0);
182f5f9340bSFabien Thomas }
183f5f9340bSFabien Thomas 
184f5f9340bSFabien Thomas static int
soft_get_config(int cpu,int ri,struct pmc ** ppm)185f5f9340bSFabien Thomas soft_get_config(int cpu, int ri, struct pmc **ppm)
186f5f9340bSFabien Thomas {
187f5f9340bSFabien Thomas 	(void) ri;
188f5f9340bSFabien Thomas 
189f5f9340bSFabien Thomas 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
190f5f9340bSFabien Thomas 	    ("[soft,%d] illegal CPU %d", __LINE__, cpu));
191f5f9340bSFabien Thomas 	KASSERT(ri >= 0 && ri < SOFT_NPMCS,
192f5f9340bSFabien Thomas 	    ("[soft,%d] illegal row-index %d", __LINE__, ri));
193f5f9340bSFabien Thomas 
194f5f9340bSFabien Thomas 	*ppm = soft_pcpu[cpu]->soft_hw[ri].phw_pmc;
195f5f9340bSFabien Thomas 	return (0);
196f5f9340bSFabien Thomas }
197f5f9340bSFabien Thomas 
198f5f9340bSFabien Thomas static int
soft_pcpu_fini(struct pmc_mdep * md,int cpu)199f5f9340bSFabien Thomas soft_pcpu_fini(struct pmc_mdep *md, int cpu)
200f5f9340bSFabien Thomas {
201f5f9340bSFabien Thomas 	int ri;
202f5f9340bSFabien Thomas 	struct pmc_cpu *pc;
203f5f9340bSFabien Thomas 
204f5f9340bSFabien Thomas 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
205f5f9340bSFabien Thomas 	    ("[soft,%d] illegal cpu %d", __LINE__, cpu));
206f5f9340bSFabien Thomas 	KASSERT(soft_pcpu[cpu] != NULL, ("[soft,%d] null pcpu", __LINE__));
207f5f9340bSFabien Thomas 
208f5f9340bSFabien Thomas 	free(soft_pcpu[cpu], M_PMC);
209f5f9340bSFabien Thomas 	soft_pcpu[cpu] = NULL;
210f5f9340bSFabien Thomas 
211f5f9340bSFabien Thomas 	ri = md->pmd_classdep[PMC_CLASS_INDEX_SOFT].pcd_ri;
212f5f9340bSFabien Thomas 
213f5f9340bSFabien Thomas 	KASSERT(ri >= 0 && ri < SOFT_NPMCS,
214f5f9340bSFabien Thomas 	    ("[soft,%d] ri=%d", __LINE__, ri));
215f5f9340bSFabien Thomas 
216f5f9340bSFabien Thomas 	pc = pmc_pcpu[cpu];
217f5f9340bSFabien Thomas 	pc->pc_hwpmcs[ri] = NULL;
218f5f9340bSFabien Thomas 
219f5f9340bSFabien Thomas 	return (0);
220f5f9340bSFabien Thomas }
221f5f9340bSFabien Thomas 
222f5f9340bSFabien Thomas static int
soft_pcpu_init(struct pmc_mdep * md,int cpu)223f5f9340bSFabien Thomas soft_pcpu_init(struct pmc_mdep *md, int cpu)
224f5f9340bSFabien Thomas {
225f5f9340bSFabien Thomas 	int first_ri, n;
226f5f9340bSFabien Thomas 	struct pmc_cpu *pc;
227f5f9340bSFabien Thomas 	struct soft_cpu *soft_pc;
228f5f9340bSFabien Thomas 	struct pmc_hw *phw;
229f5f9340bSFabien Thomas 
230f5f9340bSFabien Thomas 
231f5f9340bSFabien Thomas 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
232f5f9340bSFabien Thomas 	    ("[soft,%d] illegal cpu %d", __LINE__, cpu));
233f5f9340bSFabien Thomas 	KASSERT(soft_pcpu, ("[soft,%d] null pcpu", __LINE__));
234f5f9340bSFabien Thomas 	KASSERT(soft_pcpu[cpu] == NULL, ("[soft,%d] non-null per-cpu",
235f5f9340bSFabien Thomas 	    __LINE__));
236f5f9340bSFabien Thomas 
237f5f9340bSFabien Thomas 	soft_pc = malloc(sizeof(struct soft_cpu), M_PMC, M_WAITOK|M_ZERO);
238f5f9340bSFabien Thomas 	pc = pmc_pcpu[cpu];
239f5f9340bSFabien Thomas 
240f5f9340bSFabien Thomas 	KASSERT(pc != NULL, ("[soft,%d] cpu %d null per-cpu", __LINE__, cpu));
241f5f9340bSFabien Thomas 
242f5f9340bSFabien Thomas 	soft_pcpu[cpu] = soft_pc;
243f5f9340bSFabien Thomas 	phw = soft_pc->soft_hw;
244f5f9340bSFabien Thomas 	first_ri = md->pmd_classdep[PMC_CLASS_INDEX_SOFT].pcd_ri;
245f5f9340bSFabien Thomas 
246f5f9340bSFabien Thomas 	for (n = 0; n < SOFT_NPMCS; n++, phw++) {
247f5f9340bSFabien Thomas 		phw->phw_state = PMC_PHW_FLAG_IS_ENABLED |
248f5f9340bSFabien Thomas 		    PMC_PHW_CPU_TO_STATE(cpu) | PMC_PHW_INDEX_TO_STATE(n);
249f5f9340bSFabien Thomas 		phw->phw_pmc = NULL;
250f5f9340bSFabien Thomas 		pc->pc_hwpmcs[n + first_ri] = phw;
251f5f9340bSFabien Thomas 	}
252f5f9340bSFabien Thomas 
253f5f9340bSFabien Thomas 	return (0);
254f5f9340bSFabien Thomas }
255f5f9340bSFabien Thomas 
256f5f9340bSFabien Thomas static int
soft_read_pmc(int cpu,int ri,struct pmc * pm __unused,pmc_value_t * v)25739f92a76SMitchell Horne soft_read_pmc(int cpu, int ri, struct pmc *pm __unused, pmc_value_t *v)
258f5f9340bSFabien Thomas {
259f5f9340bSFabien Thomas 
260f5f9340bSFabien Thomas 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
261f5f9340bSFabien Thomas 	    ("[soft,%d] illegal CPU value %d", __LINE__, cpu));
262f5f9340bSFabien Thomas 	KASSERT(ri >= 0 && ri < SOFT_NPMCS,
263f5f9340bSFabien Thomas 	    ("[soft,%d] illegal row-index %d", __LINE__, ri));
264f5f9340bSFabien Thomas 
2654a3690dfSJohn Baldwin 	PMCDBG1(MDP,REA,1,"soft-read id=%d", ri);
266f5f9340bSFabien Thomas 
267f5f9340bSFabien Thomas 	*v = soft_pcpu[cpu]->soft_values[ri];
268f5f9340bSFabien Thomas 
269f5f9340bSFabien Thomas 	return (0);
270f5f9340bSFabien Thomas }
271f5f9340bSFabien Thomas 
272f5f9340bSFabien Thomas static int
soft_write_pmc(int cpu,int ri,struct pmc * pm __unused,pmc_value_t v)27339f92a76SMitchell Horne soft_write_pmc(int cpu, int ri, struct pmc *pm __unused, pmc_value_t v)
274f5f9340bSFabien Thomas {
275f5f9340bSFabien Thomas 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
276f5f9340bSFabien Thomas 	    ("[soft,%d] illegal cpu value %d", __LINE__, cpu));
277f5f9340bSFabien Thomas 	KASSERT(ri >= 0 && ri < SOFT_NPMCS,
278f5f9340bSFabien Thomas 	    ("[soft,%d] illegal row-index %d", __LINE__, ri));
279f5f9340bSFabien Thomas 
2804a3690dfSJohn Baldwin 	PMCDBG3(MDP,WRI,1, "soft-write cpu=%d ri=%d v=%jx", cpu, ri, v);
281f5f9340bSFabien Thomas 
282f5f9340bSFabien Thomas 	soft_pcpu[cpu]->soft_values[ri] = v;
283f5f9340bSFabien Thomas 
284f5f9340bSFabien Thomas 	return (0);
285f5f9340bSFabien Thomas }
286f5f9340bSFabien Thomas 
287f5f9340bSFabien Thomas static int
soft_release_pmc(int cpu,int ri,struct pmc * pmc)288f5f9340bSFabien Thomas soft_release_pmc(int cpu, int ri, struct pmc *pmc)
289f5f9340bSFabien Thomas {
290aee6e7dcSMateusz Guzik 	struct pmc_hw *phw __diagused;
291d49302aeSFabien Thomas 	enum pmc_event ev;
292d49302aeSFabien Thomas 	struct pmc_soft *ps;
293f5f9340bSFabien Thomas 
294f5f9340bSFabien Thomas 	(void) pmc;
295f5f9340bSFabien Thomas 
296f5f9340bSFabien Thomas 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
297f5f9340bSFabien Thomas 	    ("[soft,%d] illegal CPU value %d", __LINE__, cpu));
298f5f9340bSFabien Thomas 	KASSERT(ri >= 0 && ri < SOFT_NPMCS,
299f5f9340bSFabien Thomas 	    ("[soft,%d] illegal row-index %d", __LINE__, ri));
300f5f9340bSFabien Thomas 
301f5f9340bSFabien Thomas 	phw = &soft_pcpu[cpu]->soft_hw[ri];
302f5f9340bSFabien Thomas 
303f5f9340bSFabien Thomas 	KASSERT(phw->phw_pmc == NULL,
304f5f9340bSFabien Thomas 	    ("[soft,%d] PHW pmc %p non-NULL", __LINE__, phw->phw_pmc));
305f5f9340bSFabien Thomas 
306d49302aeSFabien Thomas 	ev = pmc->pm_event;
307d49302aeSFabien Thomas 
308d49302aeSFabien Thomas 	/* Check if event is registered. */
309d49302aeSFabien Thomas 	ps = pmc_soft_ev_acquire(ev);
310d49302aeSFabien Thomas 	KASSERT(ps != NULL,
311d49302aeSFabien Thomas 	    ("[soft,%d] unregistered event %d", __LINE__, ev));
312d49302aeSFabien Thomas 	pmc_soft_ev_release(ps);
313d49302aeSFabien Thomas 	/* Module unload is protected by pmc SX lock. */
314d49302aeSFabien Thomas 	if (ps->ps_release != NULL)
315d49302aeSFabien Thomas 		ps->ps_release();
316f5f9340bSFabien Thomas 	return (0);
317f5f9340bSFabien Thomas }
318f5f9340bSFabien Thomas 
319f5f9340bSFabien Thomas static int
soft_start_pmc(int cpu,int ri,struct pmc * pm)32039f92a76SMitchell Horne soft_start_pmc(int cpu, int ri, struct pmc *pm)
321f5f9340bSFabien Thomas {
322f5f9340bSFabien Thomas 	struct pmc_soft *ps;
323f5f9340bSFabien Thomas 
324f5f9340bSFabien Thomas 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
325f5f9340bSFabien Thomas 	    ("[soft,%d] illegal CPU value %d", __LINE__, cpu));
326f5f9340bSFabien Thomas 	KASSERT(ri >= 0 && ri < SOFT_NPMCS,
327f5f9340bSFabien Thomas 	    ("[soft,%d] illegal row-index %d", __LINE__, ri));
328f5f9340bSFabien Thomas 
329f5f9340bSFabien Thomas 	ps = pmc_soft_ev_acquire(pm->pm_event);
330f5f9340bSFabien Thomas 	if (ps == NULL)
331f5f9340bSFabien Thomas 		return (EINVAL);
332f5f9340bSFabien Thomas 	atomic_add_int(&ps->ps_running, 1);
333f5f9340bSFabien Thomas 	pmc_soft_ev_release(ps);
334f5f9340bSFabien Thomas 
335f5f9340bSFabien Thomas 	return (0);
336f5f9340bSFabien Thomas }
337f5f9340bSFabien Thomas 
338f5f9340bSFabien Thomas static int
soft_stop_pmc(int cpu,int ri,struct pmc * pm)33939f92a76SMitchell Horne soft_stop_pmc(int cpu, int ri, struct pmc *pm)
340f5f9340bSFabien Thomas {
341f5f9340bSFabien Thomas 	struct pmc_soft *ps;
342f5f9340bSFabien Thomas 
343f5f9340bSFabien Thomas 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
344f5f9340bSFabien Thomas 	    ("[soft,%d] illegal CPU value %d", __LINE__, cpu));
345f5f9340bSFabien Thomas 	KASSERT(ri >= 0 && ri < SOFT_NPMCS,
346f5f9340bSFabien Thomas 	    ("[soft,%d] illegal row-index %d", __LINE__, ri));
347f5f9340bSFabien Thomas 
348f5f9340bSFabien Thomas 	ps = pmc_soft_ev_acquire(pm->pm_event);
349f5f9340bSFabien Thomas 	/* event unregistered ? */
350f5f9340bSFabien Thomas 	if (ps != NULL) {
351f5f9340bSFabien Thomas 		atomic_subtract_int(&ps->ps_running, 1);
352f5f9340bSFabien Thomas 		pmc_soft_ev_release(ps);
353f5f9340bSFabien Thomas 	}
354f5f9340bSFabien Thomas 
355f5f9340bSFabien Thomas 	return (0);
356f5f9340bSFabien Thomas }
357f5f9340bSFabien Thomas 
358f5f9340bSFabien Thomas int
pmc_soft_intr(struct pmckern_soft * ks)359f5f9340bSFabien Thomas pmc_soft_intr(struct pmckern_soft *ks)
360f5f9340bSFabien Thomas {
361f5f9340bSFabien Thomas 	struct pmc *pm;
362f5f9340bSFabien Thomas 	struct soft_cpu *pc;
363f5f9340bSFabien Thomas 	int ri, processed, error, user_mode;
364f5f9340bSFabien Thomas 
365f5f9340bSFabien Thomas 	KASSERT(ks->pm_cpu >= 0 && ks->pm_cpu < pmc_cpu_max(),
366f5f9340bSFabien Thomas 	    ("[soft,%d] CPU %d out of range", __LINE__, ks->pm_cpu));
367f5f9340bSFabien Thomas 
368f5f9340bSFabien Thomas 	processed = 0;
369f5f9340bSFabien Thomas 	pc = soft_pcpu[ks->pm_cpu];
370f5f9340bSFabien Thomas 
371f5f9340bSFabien Thomas 	for (ri = 0; ri < SOFT_NPMCS; ri++) {
372f5f9340bSFabien Thomas 
373f5f9340bSFabien Thomas 		pm = pc->soft_hw[ri].phw_pmc;
374f5f9340bSFabien Thomas 		if (pm == NULL ||
375f5f9340bSFabien Thomas 		    pm->pm_state != PMC_STATE_RUNNING ||
376f5f9340bSFabien Thomas 		    pm->pm_event != ks->pm_ev) {
377f5f9340bSFabien Thomas 				continue;
378f5f9340bSFabien Thomas 		}
379f5f9340bSFabien Thomas 
380f5f9340bSFabien Thomas 		processed = 1;
381f5f9340bSFabien Thomas 		if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm))) {
3828a4f65bcSAlexander Motin 			if ((pc->soft_values[ri]--) <= 0)
3838a4f65bcSAlexander Motin 				pc->soft_values[ri] += pm->pm_sc.pm_reloadcount;
3848a4f65bcSAlexander Motin 			else
3858a4f65bcSAlexander Motin 				continue;
386f5f9340bSFabien Thomas 			user_mode = TRAPF_USERMODE(ks->pm_tf);
387eb7c9019SMatt Macy 			error = pmc_process_interrupt(PMC_SR, pm, ks->pm_tf);
388f5f9340bSFabien Thomas 			if (error) {
38939f92a76SMitchell Horne 				soft_stop_pmc(ks->pm_cpu, ri, pm);
390f5f9340bSFabien Thomas 				continue;
391f5f9340bSFabien Thomas 			}
392f5f9340bSFabien Thomas 
393f5f9340bSFabien Thomas 			if (user_mode) {
394c6d31b83SKonstantin Belousov 				/*
395c6d31b83SKonstantin Belousov 				 * If in user mode setup AST to process
396f5f9340bSFabien Thomas 				 * callchain out of interrupt context.
397f5f9340bSFabien Thomas 				 */
398c6d31b83SKonstantin Belousov 				ast_sched(curthread, TDA_HWPMC);
399f5f9340bSFabien Thomas 			}
4008a4f65bcSAlexander Motin 		} else
4018a4f65bcSAlexander Motin 			pc->soft_values[ri]++;
402f5f9340bSFabien Thomas 	}
403e6b475e0SMatt Macy 	if (processed)
404e6b475e0SMatt Macy 		counter_u64_add(pmc_stats.pm_intr_processed, 1);
405e6b475e0SMatt Macy 	else
406e6b475e0SMatt Macy 		counter_u64_add(pmc_stats.pm_intr_ignored, 1);
407f5f9340bSFabien Thomas 
408f5f9340bSFabien Thomas 	return (processed);
409f5f9340bSFabien Thomas }
410f5f9340bSFabien Thomas 
411c6d31b83SKonstantin Belousov static void
ast_hwpmc(struct thread * td,int tda __unused)412c6d31b83SKonstantin Belousov ast_hwpmc(struct thread *td, int tda __unused)
413c6d31b83SKonstantin Belousov {
414c6d31b83SKonstantin Belousov 	/* Handle Software PMC callchain capture. */
415c6d31b83SKonstantin Belousov 	if (PMC_IS_PENDING_CALLCHAIN(td))
416c6d31b83SKonstantin Belousov 		PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_USER_CALLCHAIN_SOFT,
417c6d31b83SKonstantin Belousov 		    (void *)td->td_frame);
418c6d31b83SKonstantin Belousov }
419c6d31b83SKonstantin Belousov 
420f5f9340bSFabien Thomas void
pmc_soft_initialize(struct pmc_mdep * md)421f5f9340bSFabien Thomas pmc_soft_initialize(struct pmc_mdep *md)
422f5f9340bSFabien Thomas {
423f5f9340bSFabien Thomas 	struct pmc_classdep *pcd;
424f5f9340bSFabien Thomas 
425f5f9340bSFabien Thomas 	/* Add SOFT PMCs. */
426f5f9340bSFabien Thomas 	soft_pcpu = malloc(sizeof(struct soft_cpu *) * pmc_cpu_max(), M_PMC,
427f5f9340bSFabien Thomas 	    M_ZERO|M_WAITOK);
428f5f9340bSFabien Thomas 
429f5f9340bSFabien Thomas 	pcd = &md->pmd_classdep[PMC_CLASS_INDEX_SOFT];
430f5f9340bSFabien Thomas 
431f5f9340bSFabien Thomas 	pcd->pcd_caps	= SOFT_CAPS;
432f5f9340bSFabien Thomas 	pcd->pcd_class	= PMC_CLASS_SOFT;
433f5f9340bSFabien Thomas 	pcd->pcd_num	= SOFT_NPMCS;
434f5f9340bSFabien Thomas 	pcd->pcd_ri	= md->pmd_npmc;
435f5f9340bSFabien Thomas 	pcd->pcd_width	= 64;
436f5f9340bSFabien Thomas 
437f5f9340bSFabien Thomas 	pcd->pcd_allocate_pmc = soft_allocate_pmc;
438f5f9340bSFabien Thomas 	pcd->pcd_config_pmc   = soft_config_pmc;
439f5f9340bSFabien Thomas 	pcd->pcd_describe     = soft_describe;
440f5f9340bSFabien Thomas 	pcd->pcd_get_config   = soft_get_config;
441f5f9340bSFabien Thomas 	pcd->pcd_get_msr      = NULL;
442f5f9340bSFabien Thomas 	pcd->pcd_pcpu_init    = soft_pcpu_init;
443f5f9340bSFabien Thomas 	pcd->pcd_pcpu_fini    = soft_pcpu_fini;
444f5f9340bSFabien Thomas 	pcd->pcd_read_pmc     = soft_read_pmc;
445f5f9340bSFabien Thomas 	pcd->pcd_write_pmc    = soft_write_pmc;
446f5f9340bSFabien Thomas 	pcd->pcd_release_pmc  = soft_release_pmc;
447f5f9340bSFabien Thomas 	pcd->pcd_start_pmc    = soft_start_pmc;
448f5f9340bSFabien Thomas 	pcd->pcd_stop_pmc     = soft_stop_pmc;
449f5f9340bSFabien Thomas 
450f5f9340bSFabien Thomas 	md->pmd_npmc += SOFT_NPMCS;
451c6d31b83SKonstantin Belousov 
452c6d31b83SKonstantin Belousov 	ast_register(TDA_HWPMC, ASTR_UNCOND, 0, ast_hwpmc);
453f5f9340bSFabien Thomas }
454f5f9340bSFabien Thomas 
455f5f9340bSFabien Thomas void
pmc_soft_finalize(struct pmc_mdep * md)456f5f9340bSFabien Thomas pmc_soft_finalize(struct pmc_mdep *md)
457f5f9340bSFabien Thomas {
458*90a6ea5cSMitchell Horne 	PMCDBG0(MDP, INI, 1, "soft-finalize");
459f5f9340bSFabien Thomas 
460*90a6ea5cSMitchell Horne 	for (int i = 0; i < pmc_cpu_max(); i++)
461f5f9340bSFabien Thomas 		KASSERT(soft_pcpu[i] == NULL, ("[soft,%d] non-null pcpu cpu %d",
462f5f9340bSFabien Thomas 		    __LINE__, i));
463f5f9340bSFabien Thomas 
464c6d31b83SKonstantin Belousov 	ast_deregister(TDA_HWPMC);
465f5f9340bSFabien Thomas 	free(soft_pcpu, M_PMC);
466f5f9340bSFabien Thomas 	soft_pcpu = NULL;
467f5f9340bSFabien Thomas }
468