1249268Sglebius/*-
2249268Sglebius * Copyright (c) 2012 Konstantin Belousov <kib@FreeBSD.org>
3249268Sglebius * All rights reserved.
4249268Sglebius *
5249268Sglebius * Redistribution and use in source and binary forms, with or without
6249268Sglebius * modification, are permitted provided that the following conditions
7249268Sglebius * are met:
8249268Sglebius * 1. Redistributions of source code must retain the above copyright
9249268Sglebius *    notice, this list of conditions and the following disclaimer.
10249268Sglebius * 2. Redistributions in binary form must reproduce the above copyright
11249268Sglebius *    notice, this list of conditions and the following disclaimer in the
12249268Sglebius *    documentation and/or other materials provided with the distribution.
13249268Sglebius *
14249268Sglebius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15249268Sglebius * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16249268Sglebius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17249268Sglebius * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18249268Sglebius * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19249268Sglebius * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20249268Sglebius * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21249268Sglebius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22249268Sglebius * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23249268Sglebius * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24249268Sglebius * SUCH DAMAGE.
25249268Sglebius *
26249268Sglebius * $FreeBSD: stable/11/sys/arm/include/counter.h 328386 2018-01-25 02:45:21Z pkelsey $
27249268Sglebius */
28249268Sglebius
29249268Sglebius#ifndef __MACHINE_COUNTER_H__
30249268Sglebius#define __MACHINE_COUNTER_H__
31249268Sglebius
32249268Sglebius#include <sys/pcpu.h>
33269406Sian#include <machine/atomic.h>
34249268Sglebius
35313989Skib#define	counter_enter()	do {} while (0)
36313989Skib#define	counter_exit()	do {} while (0)
37249268Sglebius
38252434Skib#ifdef IN_SUBR_COUNTER_C
39269406Sian
40252434Skibstatic inline uint64_t
41252434Skibcounter_u64_read_one(uint64_t *p, int cpu)
42252434Skib{
43252434Skib
44269406Sian	return (atomic_load_64((uint64_t *)((char *)p + sizeof(struct pcpu) *
45269406Sian	    cpu)));
46252434Skib}
47252434Skib
48252434Skibstatic inline uint64_t
49252434Skibcounter_u64_fetch_inline(uint64_t *p)
50252434Skib{
51252434Skib	uint64_t r;
52252434Skib	int i;
53252434Skib
54252434Skib	r = 0;
55313989Skib	CPU_FOREACH(i)
56252434Skib		r += counter_u64_read_one((uint64_t *)p, i);
57252434Skib
58252434Skib	return (r);
59252434Skib}
60252434Skib
61252434Skibstatic void
62252434Skibcounter_u64_zero_one_cpu(void *arg)
63252434Skib{
64252434Skib
65269406Sian	atomic_store_64((uint64_t *)((char *)arg + sizeof(struct pcpu) *
66269406Sian	    PCPU_GET(cpuid)), 0);
67252434Skib}
68252434Skib
69252434Skibstatic inline void
70252434Skibcounter_u64_zero_inline(counter_u64_t c)
71252434Skib{
72252434Skib
73328386Spkelsey	smp_rendezvous(smp_no_rendezvous_barrier, counter_u64_zero_one_cpu,
74328386Spkelsey	    smp_no_rendezvous_barrier, c);
75252434Skib}
76252434Skib#endif
77252434Skib
78313989Skib#define	counter_u64_add_protected(c, inc)	counter_u64_add(c, inc)
79249268Sglebius
80249268Sglebiusstatic inline void
81249268Sglebiuscounter_u64_add(counter_u64_t c, int64_t inc)
82249268Sglebius{
83249268Sglebius
84313989Skib	atomic_add_64((uint64_t *)zpcpu_get(c), inc);
85249268Sglebius}
86249268Sglebius
87249268Sglebius#endif	/* ! __MACHINE_COUNTER_H__ */
88