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$ 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 41252434Skibstatic inline uint64_t 42252434Skibcounter_u64_read_one(uint64_t *p, int cpu) 43252434Skib{ 44252434Skib 45252434Skib return (*(uint64_t *)((char *)p + sizeof(struct pcpu) * 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; 55252434Skib for (i = 0; i < mp_ncpus; i++) 56252434Skib r += counter_u64_read_one((uint64_t *)p, i); 57252434Skib 58252434Skib return (r); 59252434Skib} 60252434Skib 61252434Skib/* XXXKIB might interrupt increment */ 62252434Skibstatic void 63252434Skibcounter_u64_zero_one_cpu(void *arg) 64252434Skib{ 65252434Skib 66252434Skib *((uint64_t *)((char *)arg + sizeof(struct pcpu) * 67252434Skib PCPU_GET(cpuid))) = 0; 68252434Skib} 69252434Skib 70252434Skibstatic inline void 71252434Skibcounter_u64_zero_inline(counter_u64_t c) 72252434Skib{ 73252434Skib 74252434Skib smp_rendezvous(smp_no_rendevous_barrier, counter_u64_zero_one_cpu, 75252434Skib smp_no_rendevous_barrier, c); 76252434Skib} 77252434Skib#endif 78252434Skib 79249268Sglebius#define counter_u64_add_protected(c, inc) do { \ 80249268Sglebius CRITICAL_ASSERT(curthread); \ 81249268Sglebius *(uint64_t *)zpcpu_get(c) += (inc); \ 82249268Sglebius} while (0) 83249268Sglebius 84249268Sglebiusstatic inline void 85249268Sglebiuscounter_u64_add(counter_u64_t c, int64_t inc) 86249268Sglebius{ 87249268Sglebius 88249268Sglebius counter_enter(); 89249268Sglebius counter_u64_add_protected(c, inc); 90249268Sglebius counter_exit(); 91249268Sglebius} 92249268Sglebius 93249268Sglebius#endif /* ! __MACHINE_COUNTER_H__ */ 94