summaryrefslogtreecommitdiff
path: root/usr/src/compat/freebsd/sys/cpuset.h
blob: dadadf15b237f6a473c422b83d3be493f7e1aec9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2014 Pluribus Networks Inc.
 * Copyright 2017 Joyent, Inc.
 */

#ifndef _COMPAT_FREEBSD_SYS_CPUSET_H_
#define	_COMPAT_FREEBSD_SYS_CPUSET_H_

#define	NOCPU			-1

#ifdef	_KERNEL

#include <sys/_cpuset.h>

#define	CPU_SET(cpu, set)		cpuset_add((set), (cpu))
#define	CPU_SETOF(cpu, set)		cpuset_only((set), (cpu))
#define	CPU_ZERO(set)			cpuset_zero((cpuset_t *)(set))
#define	CPU_CLR(cpu, set)		cpuset_del((set), (cpu))
#define	CPU_FFS(set)			cpusetobj_ffs(set)
#define	CPU_ISSET(cpu, set)		cpu_in_set((cpuset_t *)(set), (cpu))
#define	CPU_AND(dst, src)		cpuset_and(			\
						(cpuset_t *)(dst),	\
						(cpuset_t *)(src))
#define	CPU_CMP(set1, set2)		(cpuset_isequal(		\
						(cpuset_t *)(set1),	\
						(cpuset_t *)(set2)) == 0)
#define	CPU_SET_ATOMIC(cpu, set)	cpuset_atomic_add(		\
						(cpuset_t *)(set),	\
						(cpu))
#define	CPU_CLR_ATOMIC(cpu, set)	cpuset_atomic_del(		\
						(cpuset_t *)(set),	\
						(cpu))

/* XXXJOY: The _ACQ variants appear to imply a membar too. Is that an issue? */
#define	CPU_SET_ATOMIC_ACQ(cpu, set)	cpuset_atomic_add((set), (cpu))


int	cpusetobj_ffs(const cpuset_t *set);

#else

#include <sys/bitmap.h>
#include <machine/atomic.h>
#include <machine/cpufunc.h>

/* For now, assume NCPU of 256 */
#define	CPU_SETSIZE			(256)

typedef struct {
	ulong_t _bits[BT_BITOUL(CPU_SETSIZE)];
} cpuset_t;

static __inline int
cpuset_isempty(const cpuset_t *set)
{
	uint_t i;

	for (i = 0; i < BT_BITOUL(CPU_SETSIZE); i++) {
		if (set->_bits[i] != 0)
			return (0);
	}
	return (1);
}

static __inline void
cpuset_zero(cpuset_t *dst)
{
	uint_t i;

	for (i = 0; i < BT_BITOUL(CPU_SETSIZE); i++) {
		dst->_bits[i] = 0;
	}
}

static __inline int
cpuset_isequal(cpuset_t *s1, cpuset_t *s2)
{
	uint_t i;

	for (i = 0; i < BT_BITOUL(CPU_SETSIZE); i++) {
		if (s1->_bits[i] != s2->_bits[i])
			return (0);
	}
	return (1);
}

static __inline uint_t
cpusetobj_ffs(const cpuset_t *set)
{
	uint_t i, cbit;

	cbit = 0;
	for (i = 0; i < BT_BITOUL(CPU_SETSIZE); i++) {
		if (set->_bits[i] != 0) {
			cbit = ffsl(set->_bits[i]);
			cbit += i * sizeof (set->_bits[0]);
			break;
		}
	}
	return (cbit);
}


#define	CPU_SET(cpu, setp)		BT_SET((setp)->_bits, cpu)
#define	CPU_CLR(cpu, setp)		BT_CLEAR((setp)->_bits, cpu)
#define	CPU_ZERO(setp)			cpuset_zero((setp))
#define	CPU_CMP(set1, set2)		(cpuset_isequal(		\
						(cpuset_t *)(set1),	\
						(cpuset_t *)(set2)) == 0)
#define	CPU_FFS(set)			cpusetobj_ffs(set)
#define	CPU_ISSET(cpu, setp)		BT_TEST((setp)->_bits, cpu)
#define	CPU_EMPTY(setp)			cpuset_isempty((setp))
#define	CPU_SET_ATOMIC(cpu, setp)	\
	atomic_set_long(&(BT_WIM((setp)->_bits, cpu)), BT_BIW(cpu))
#define	CPU_CLR_ATOMIC(cpu, setp)	\
	atomic_clear_long(&(BT_WIM((setp)->_bits, cpu)), BT_BIW(cpu))

#endif

#endif	/* _COMPAT_FREEBSD_SYS_CPUSET_H_ */