xref: /freebsd/sys/arm64/include/counter.h (revision a90ebeb5feea63c9d8377cbc74e0442156a0ed15)
1e5acd89cSAndrew Turner /*-
2e5acd89cSAndrew Turner  * Copyright (c) 2012 Konstantin Belousov <kib@FreeBSD.org>
3e5acd89cSAndrew Turner  * All rights reserved.
4e5acd89cSAndrew Turner  *
5e5acd89cSAndrew Turner  * Redistribution and use in source and binary forms, with or without
6e5acd89cSAndrew Turner  * modification, are permitted provided that the following conditions
7e5acd89cSAndrew Turner  * are met:
8e5acd89cSAndrew Turner  * 1. Redistributions of source code must retain the above copyright
9e5acd89cSAndrew Turner  *    notice, this list of conditions and the following disclaimer.
10e5acd89cSAndrew Turner  * 2. Redistributions in binary form must reproduce the above copyright
11e5acd89cSAndrew Turner  *    notice, this list of conditions and the following disclaimer in the
12e5acd89cSAndrew Turner  *    documentation and/or other materials provided with the distribution.
13e5acd89cSAndrew Turner  *
14e5acd89cSAndrew Turner  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15e5acd89cSAndrew Turner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16e5acd89cSAndrew Turner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17e5acd89cSAndrew Turner  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18e5acd89cSAndrew Turner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19e5acd89cSAndrew Turner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20e5acd89cSAndrew Turner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21e5acd89cSAndrew Turner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22e5acd89cSAndrew Turner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23e5acd89cSAndrew Turner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24e5acd89cSAndrew Turner  * SUCH DAMAGE.
25e5acd89cSAndrew Turner  *
26e5acd89cSAndrew Turner  * $FreeBSD$
27e5acd89cSAndrew Turner  */
28e5acd89cSAndrew Turner 
29e5acd89cSAndrew Turner #ifndef _MACHINE_COUNTER_H_
30e5acd89cSAndrew Turner #define	_MACHINE_COUNTER_H_
31e5acd89cSAndrew Turner 
32e5acd89cSAndrew Turner #include <sys/pcpu.h>
333b7a388bSKonstantin Belousov #include <machine/atomic.h>
34e5acd89cSAndrew Turner 
35*a90ebeb5SAndrew Turner #define	EARLY_COUNTER	&pcpu0.pc_early_dummy_counter
3683c9dea1SGleb Smirnoff 
373b7a388bSKonstantin Belousov #define	counter_enter()	do {} while (0)
383b7a388bSKonstantin Belousov #define	counter_exit()	do {} while (0)
39e5acd89cSAndrew Turner 
40e5acd89cSAndrew Turner #ifdef IN_SUBR_COUNTER_C
41e5acd89cSAndrew Turner static inline uint64_t
42e5acd89cSAndrew Turner counter_u64_read_one(uint64_t *p, int cpu)
43e5acd89cSAndrew Turner {
44e5acd89cSAndrew Turner 
45ab3059a8SMatt Macy 	return (*(uint64_t *)((char *)p + UMA_PCPU_ALLOC_SIZE * cpu));
46e5acd89cSAndrew Turner }
47e5acd89cSAndrew Turner 
48e5acd89cSAndrew Turner static inline uint64_t
49e5acd89cSAndrew Turner counter_u64_fetch_inline(uint64_t *p)
50e5acd89cSAndrew Turner {
51e5acd89cSAndrew Turner 	uint64_t r;
52e5acd89cSAndrew Turner 	int i;
53e5acd89cSAndrew Turner 
54e5acd89cSAndrew Turner 	r = 0;
553b7a388bSKonstantin Belousov 	CPU_FOREACH(i)
56e5acd89cSAndrew Turner 		r += counter_u64_read_one((uint64_t *)p, i);
57e5acd89cSAndrew Turner 
58e5acd89cSAndrew Turner 	return (r);
59e5acd89cSAndrew Turner }
60e5acd89cSAndrew Turner 
61e5acd89cSAndrew Turner static void
62e5acd89cSAndrew Turner counter_u64_zero_one_cpu(void *arg)
63e5acd89cSAndrew Turner {
64e5acd89cSAndrew Turner 
65ab3059a8SMatt Macy 	*((uint64_t *)((char *)arg + UMA_PCPU_ALLOC_SIZE *
66e5acd89cSAndrew Turner 	    PCPU_GET(cpuid))) = 0;
67e5acd89cSAndrew Turner }
68e5acd89cSAndrew Turner 
69e5acd89cSAndrew Turner static inline void
70e5acd89cSAndrew Turner counter_u64_zero_inline(counter_u64_t c)
71e5acd89cSAndrew Turner {
72e5acd89cSAndrew Turner 
7367d955aaSPatrick Kelsey 	smp_rendezvous(smp_no_rendezvous_barrier, counter_u64_zero_one_cpu,
7467d955aaSPatrick Kelsey 	    smp_no_rendezvous_barrier, c);
75e5acd89cSAndrew Turner }
76e5acd89cSAndrew Turner #endif
77e5acd89cSAndrew Turner 
783b7a388bSKonstantin Belousov #define	counter_u64_add_protected(c, inc)	counter_u64_add(c, inc)
79e5acd89cSAndrew Turner 
80e5acd89cSAndrew Turner static inline void
81e5acd89cSAndrew Turner counter_u64_add(counter_u64_t c, int64_t inc)
82e5acd89cSAndrew Turner {
83e5acd89cSAndrew Turner 
843b7a388bSKonstantin Belousov 	atomic_add_64((uint64_t *)zpcpu_get(c), inc);
85e5acd89cSAndrew Turner }
86e5acd89cSAndrew Turner 
87e5acd89cSAndrew Turner #endif	/* ! _MACHINE_COUNTER_H_ */
88