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/mips/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> 33249268Sglebius#ifdef INVARIANTS 34249268Sglebius#include <sys/proc.h> 35249268Sglebius#endif 36249268Sglebius 37249268Sglebius#define counter_enter() critical_enter() 38249268Sglebius#define counter_exit() critical_exit() 39249268Sglebius 40252434Skib#ifdef IN_SUBR_COUNTER_C 41252434Skib/* XXXKIB non-atomic 64bit read on 32bit */ 42252434Skibstatic inline uint64_t 43252434Skibcounter_u64_read_one(uint64_t *p, int cpu) 44252434Skib{ 45252434Skib 46252434Skib return (*(uint64_t *)((char *)p + sizeof(struct pcpu) * cpu)); 47252434Skib} 48252434Skib 49252434Skibstatic inline uint64_t 50252434Skibcounter_u64_fetch_inline(uint64_t *p) 51252434Skib{ 52252434Skib uint64_t r; 53252434Skib int i; 54252434Skib 55252434Skib r = 0; 56252434Skib for (i = 0; i < mp_ncpus; i++) 57252434Skib r += counter_u64_read_one((uint64_t *)p, i); 58252434Skib 59252434Skib return (r); 60252434Skib} 61252434Skib 62252434Skib/* XXXKIB non-atomic 64bit store on 32bit, might interrupt increment */ 63252434Skibstatic void 64252434Skibcounter_u64_zero_one_cpu(void *arg) 65252434Skib{ 66252434Skib 67252434Skib *((uint64_t *)((char *)arg + sizeof(struct pcpu) * 68252434Skib PCPU_GET(cpuid))) = 0; 69252434Skib} 70252434Skib 71252434Skibstatic inline void 72252434Skibcounter_u64_zero_inline(counter_u64_t c) 73252434Skib{ 74252434Skib 75328386Spkelsey smp_rendezvous(smp_no_rendezvous_barrier, counter_u64_zero_one_cpu, 76328386Spkelsey smp_no_rendezvous_barrier, c); 77252434Skib} 78252434Skib#endif 79252434Skib 80249268Sglebius#define counter_u64_add_protected(c, inc) do { \ 81249268Sglebius CRITICAL_ASSERT(curthread); \ 82249268Sglebius *(uint64_t *)zpcpu_get(c) += (inc); \ 83249268Sglebius} while (0) 84249268Sglebius 85249268Sglebiusstatic inline void 86249268Sglebiuscounter_u64_add(counter_u64_t c, int64_t inc) 87249268Sglebius{ 88249268Sglebius 89249268Sglebius counter_enter(); 90249268Sglebius counter_u64_add_protected(c, inc); 91249268Sglebius counter_exit(); 92249268Sglebius} 93249268Sglebius 94249268Sglebius#endif /* ! __MACHINE_COUNTER_H__ */ 95