/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2012 Konstantin Belousov <[email protected]>4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#ifndef __MACHINE_COUNTER_H__29#define __MACHINE_COUNTER_H__3031#include <sys/pcpu.h>32#include <machine/atomic.h>3334#define EARLY_COUNTER &__pcpu[0].pc_early_dummy_counter3536#define counter_enter() do {} while (0)37#define counter_exit() do {} while (0)3839#ifdef IN_SUBR_COUNTER_C4041static inline uint64_t42counter_u64_read_one(uint64_t *p, int cpu)43{4445return (atomic_load_64((uint64_t *)zpcpu_get_cpu(p, cpu)));46}4748static inline uint64_t49counter_u64_fetch_inline(uint64_t *p)50{51uint64_t r;52int i;5354r = 0;55CPU_FOREACH(i)56r += counter_u64_read_one((uint64_t *)p, i);5758return (r);59}6061static void62counter_u64_zero_one_cpu(void *arg)63{6465atomic_store_64((uint64_t *)zpcpu_get(arg), 0);66}6768static inline void69counter_u64_zero_inline(counter_u64_t c)70{7172smp_rendezvous(smp_no_rendezvous_barrier, counter_u64_zero_one_cpu,73smp_no_rendezvous_barrier, c);74}75#endif7677#define counter_u64_add_protected(c, inc) counter_u64_add(c, inc)7879static inline void80counter_u64_add(counter_u64_t c, int64_t inc)81{8283atomic_add_64((uint64_t *)zpcpu_get(c), inc);84}8586#endif /* ! __MACHINE_COUNTER_H__ */878889