xref: /freebsd/sys/arm64/include/counter.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
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 
27*d5d97bedSMike Karels #ifdef __arm__
28*d5d97bedSMike Karels #include <arm/counter.h>
29*d5d97bedSMike Karels #else /* !__arm__ */
30*d5d97bedSMike Karels 
31e5acd89cSAndrew Turner #ifndef _MACHINE_COUNTER_H_
32e5acd89cSAndrew Turner #define	_MACHINE_COUNTER_H_
33e5acd89cSAndrew Turner 
34e5acd89cSAndrew Turner #include <sys/pcpu.h>
353b7a388bSKonstantin Belousov #include <machine/atomic.h>
36e5acd89cSAndrew Turner 
37a90ebeb5SAndrew Turner #define	EARLY_COUNTER	&pcpu0.pc_early_dummy_counter
3883c9dea1SGleb Smirnoff 
393b7a388bSKonstantin Belousov #define	counter_enter()	do {} while (0)
403b7a388bSKonstantin Belousov #define	counter_exit()	do {} while (0)
41e5acd89cSAndrew Turner 
42e5acd89cSAndrew Turner #ifdef IN_SUBR_COUNTER_C
43e5acd89cSAndrew Turner static inline uint64_t
counter_u64_read_one(uint64_t * p,int cpu)44e5acd89cSAndrew Turner counter_u64_read_one(uint64_t *p, int cpu)
45e5acd89cSAndrew Turner {
46e5acd89cSAndrew Turner 
47ab3059a8SMatt Macy 	return (*(uint64_t *)((char *)p + UMA_PCPU_ALLOC_SIZE * cpu));
48e5acd89cSAndrew Turner }
49e5acd89cSAndrew Turner 
50e5acd89cSAndrew Turner static inline uint64_t
counter_u64_fetch_inline(uint64_t * p)51e5acd89cSAndrew Turner counter_u64_fetch_inline(uint64_t *p)
52e5acd89cSAndrew Turner {
53e5acd89cSAndrew Turner 	uint64_t r;
54e5acd89cSAndrew Turner 	int i;
55e5acd89cSAndrew Turner 
56e5acd89cSAndrew Turner 	r = 0;
573b7a388bSKonstantin Belousov 	CPU_FOREACH(i)
58e5acd89cSAndrew Turner 		r += counter_u64_read_one((uint64_t *)p, i);
59e5acd89cSAndrew Turner 
60e5acd89cSAndrew Turner 	return (r);
61e5acd89cSAndrew Turner }
62e5acd89cSAndrew Turner 
63e5acd89cSAndrew Turner static void
counter_u64_zero_one_cpu(void * arg)64e5acd89cSAndrew Turner counter_u64_zero_one_cpu(void *arg)
65e5acd89cSAndrew Turner {
66e5acd89cSAndrew Turner 
67ab3059a8SMatt Macy 	*((uint64_t *)((char *)arg + UMA_PCPU_ALLOC_SIZE *
68e5acd89cSAndrew Turner 	    PCPU_GET(cpuid))) = 0;
69e5acd89cSAndrew Turner }
70e5acd89cSAndrew Turner 
71e5acd89cSAndrew Turner static inline void
counter_u64_zero_inline(counter_u64_t c)72e5acd89cSAndrew Turner counter_u64_zero_inline(counter_u64_t c)
73e5acd89cSAndrew Turner {
74e5acd89cSAndrew Turner 
7567d955aaSPatrick Kelsey 	smp_rendezvous(smp_no_rendezvous_barrier, counter_u64_zero_one_cpu,
7667d955aaSPatrick Kelsey 	    smp_no_rendezvous_barrier, c);
77e5acd89cSAndrew Turner }
78e5acd89cSAndrew Turner #endif
79e5acd89cSAndrew Turner 
803b7a388bSKonstantin Belousov #define	counter_u64_add_protected(c, inc)	counter_u64_add(c, inc)
81e5acd89cSAndrew Turner 
82e5acd89cSAndrew Turner static inline void
counter_u64_add(counter_u64_t c,int64_t inc)83e5acd89cSAndrew Turner counter_u64_add(counter_u64_t c, int64_t inc)
84e5acd89cSAndrew Turner {
85e5acd89cSAndrew Turner 
863b7a388bSKonstantin Belousov 	atomic_add_64((uint64_t *)zpcpu_get(c), inc);
87e5acd89cSAndrew Turner }
88e5acd89cSAndrew Turner 
89e5acd89cSAndrew Turner #endif	/* ! _MACHINE_COUNTER_H_ */
90*d5d97bedSMike Karels 
91*d5d97bedSMike Karels #endif /* !__arm__ */
92