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#include <machine/md_var.h>
37249268Sglebius#include <machine/specialreg.h>
38249268Sglebius
39249268Sglebius#define	counter_enter()	do {				\
40249268Sglebius	if ((cpu_feature & CPUID_CX8) == 0)		\
41249268Sglebius		critical_enter();			\
42249268Sglebius} while (0)
43249268Sglebius
44249268Sglebius#define	counter_exit()	do {				\
45249268Sglebius	if ((cpu_feature & CPUID_CX8) == 0)		\
46249268Sglebius		critical_exit();			\
47249268Sglebius} while (0)
48249268Sglebius
49249314Skibextern struct pcpu __pcpu[MAXCPU];
50249314Skib
51249268Sglebiusstatic inline void
52249268Sglebiuscounter_64_inc_8b(uint64_t *p, int64_t inc)
53249268Sglebius{
54249268Sglebius
55249268Sglebius	__asm __volatile(
56249268Sglebius	"movl	%%fs:(%%esi),%%eax\n\t"
57249268Sglebius	"movl	%%fs:4(%%esi),%%edx\n"
58249268Sglebius"1:\n\t"
59249268Sglebius	"movl	%%eax,%%ebx\n\t"
60249268Sglebius	"movl	%%edx,%%ecx\n\t"
61249268Sglebius	"addl	(%%edi),%%ebx\n\t"
62249268Sglebius	"adcl	4(%%edi),%%ecx\n\t"
63249268Sglebius	"cmpxchg8b %%fs:(%%esi)\n\t"
64249268Sglebius	"jnz	1b"
65249268Sglebius	:
66249314Skib	: "S" ((char *)p - (char *)&__pcpu[0]), "D" (&inc)
67249268Sglebius	: "memory", "cc", "eax", "edx", "ebx", "ecx");
68249268Sglebius}
69249268Sglebius
70252434Skib#ifdef IN_SUBR_COUNTER_C
71252434Skibstatic inline uint64_t
72252434Skibcounter_u64_read_one_8b(uint64_t *p)
73252434Skib{
74252434Skib	uint32_t res_lo, res_high;
75252434Skib
76252434Skib	__asm __volatile(
77252434Skib	"movl	%%eax,%%ebx\n\t"
78252434Skib	"movl	%%edx,%%ecx\n\t"
79252434Skib	"cmpxchg8b	(%2)"
80252434Skib	: "=a" (res_lo), "=d"(res_high)
81252434Skib	: "SD" (p)
82252434Skib	: "cc", "ebx", "ecx");
83252434Skib	return (res_lo + ((uint64_t)res_high << 32));
84252434Skib}
85252434Skib
86252434Skibstatic inline uint64_t
87252434Skibcounter_u64_fetch_inline(uint64_t *p)
88252434Skib{
89252434Skib	uint64_t res;
90252434Skib	int i;
91252434Skib
92252434Skib	res = 0;
93252434Skib	if ((cpu_feature & CPUID_CX8) == 0) {
94252434Skib		/*
95252434Skib		 * The machines without cmpxchg8b are not SMP.
96252434Skib		 * Disabling the preemption provides atomicity of the
97252434Skib		 * counter reading, since update is done in the
98252434Skib		 * critical section as well.
99252434Skib		 */
100252434Skib		critical_enter();
101252434Skib		for (i = 0; i < mp_ncpus; i++) {
102252434Skib			res += *(uint64_t *)((char *)p +
103252434Skib			    sizeof(struct pcpu) * i);
104252434Skib		}
105252434Skib		critical_exit();
106252434Skib	} else {
107252434Skib		for (i = 0; i < mp_ncpus; i++)
108252434Skib			res += counter_u64_read_one_8b((uint64_t *)((char *)p +
109252434Skib			    sizeof(struct pcpu) * i));
110252434Skib	}
111252434Skib	return (res);
112252434Skib}
113252434Skib
114252434Skibstatic inline void
115252434Skibcounter_u64_zero_one_8b(uint64_t *p)
116252434Skib{
117252434Skib
118252434Skib	__asm __volatile(
119252434Skib	"movl	(%0),%%eax\n\t"
120252434Skib	"movl	4(%0),%%edx\n"
121252434Skib	"xorl	%%ebx,%%ebx\n\t"
122252434Skib	"xorl	%%ecx,%%ecx\n\t"
123252434Skib"1:\n\t"
124252434Skib	"cmpxchg8b	(%0)\n\t"
125252434Skib	"jnz	1b"
126252434Skib	:
127252434Skib	: "SD" (p)
128252434Skib	: "memory", "cc", "eax", "edx", "ebx", "ecx");
129252434Skib}
130252434Skib
131252434Skibstatic void
132252434Skibcounter_u64_zero_one_cpu(void *arg)
133252434Skib{
134252434Skib	uint64_t *p;
135252434Skib
136252434Skib	p = (uint64_t *)((char *)arg + sizeof(struct pcpu) * PCPU_GET(cpuid));
137252434Skib	counter_u64_zero_one_8b(p);
138252434Skib}
139252434Skib
140252434Skibstatic inline void
141252434Skibcounter_u64_zero_inline(counter_u64_t c)
142252434Skib{
143252434Skib	int i;
144252434Skib
145252434Skib	if ((cpu_feature & CPUID_CX8) == 0) {
146252434Skib		critical_enter();
147252434Skib		for (i = 0; i < mp_ncpus; i++)
148252434Skib			*(uint64_t *)((char *)c + sizeof(struct pcpu) * i) = 0;
149252434Skib		critical_exit();
150252434Skib	} else {
151252434Skib		smp_rendezvous(smp_no_rendevous_barrier,
152252434Skib		    counter_u64_zero_one_cpu, smp_no_rendevous_barrier, c);
153252434Skib	}
154252434Skib}
155252434Skib#endif
156252434Skib
157249268Sglebius#define	counter_u64_add_protected(c, inc)	do {	\
158249268Sglebius	if ((cpu_feature & CPUID_CX8) == 0) {		\
159249268Sglebius		CRITICAL_ASSERT(curthread);		\
160249268Sglebius		*(uint64_t *)zpcpu_get(c) += (inc);	\
161249268Sglebius	} else						\
162249268Sglebius		counter_64_inc_8b((c), (inc));		\
163249268Sglebius} while (0)
164249268Sglebius
165249268Sglebiusstatic inline void
166249268Sglebiuscounter_u64_add(counter_u64_t c, int64_t inc)
167249268Sglebius{
168249268Sglebius
169249268Sglebius	if ((cpu_feature & CPUID_CX8) == 0) {
170249268Sglebius		critical_enter();
171249268Sglebius		*(uint64_t *)zpcpu_get(c) += inc;
172249268Sglebius		critical_exit();
173249268Sglebius	} else {
174249268Sglebius		counter_64_inc_8b(c, inc);
175249268Sglebius	}
176249268Sglebius}
177249268Sglebius
178249268Sglebius#endif	/* ! __MACHINE_COUNTER_H__ */
179