1176730Sjeff/*-
2176730Sjeff * Copyright (c) 2008,	Jeffrey Roberson <jeff@freebsd.org>
3176730Sjeff * All rights reserved.
4176730Sjeff *
5177904Sjeff * Copyright (c) 2008 Nokia Corporation
6177904Sjeff * All rights reserved.
7177904Sjeff *
8176730Sjeff * Redistribution and use in source and binary forms, with or without
9176730Sjeff * modification, are permitted provided that the following conditions
10176730Sjeff * are met:
11176730Sjeff * 1. Redistributions of source code must retain the above copyright
12176730Sjeff *    notice unmodified, this list of conditions, and the following
13176730Sjeff *    disclaimer.
14176730Sjeff * 2. Redistributions in binary form must reproduce the above copyright
15176730Sjeff *    notice, this list of conditions and the following disclaimer in the
16176730Sjeff *    documentation and/or other materials provided with the distribution.
17176730Sjeff *
18176730Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19176730Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20176730Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21176730Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22176730Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23176730Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24176730Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25176730Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26176730Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27176730Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28176730Sjeff *
29176730Sjeff * $FreeBSD$
30176730Sjeff */
31176730Sjeff
32176730Sjeff#ifndef _SYS_CPUSET_H_
33176730Sjeff#define	_SYS_CPUSET_H_
34176730Sjeff
35222813Sattilio#include <sys/_cpuset.h>
36176730Sjeff
37256206Smav#include <sys/bitset.h>
38256206Smav
39222813Sattilio#define	CPUSETBUFSIZ	((2 + sizeof(long) * 2) * _NCPUWORDS)
40176730Sjeff
41256206Smav#define	CPU_CLR(n, p)			BIT_CLR(CPU_SETSIZE, n, p)
42256206Smav#define	CPU_COPY(f, t)			BIT_COPY(CPU_SETSIZE, f, t)
43256206Smav#define	CPU_ISSET(n, p)			BIT_ISSET(CPU_SETSIZE, n, p)
44256206Smav#define	CPU_SET(n, p)			BIT_SET(CPU_SETSIZE, n, p)
45256206Smav#define	CPU_ZERO(p) 			BIT_ZERO(CPU_SETSIZE, p)
46256206Smav#define	CPU_FILL(p) 			BIT_FILL(CPU_SETSIZE, p)
47256206Smav#define	CPU_SETOF(n, p)			BIT_SETOF(CPU_SETSIZE, n, p)
48256206Smav#define	CPU_EMPTY(p)			BIT_EMPTY(CPU_SETSIZE, p)
49256206Smav#define	CPU_ISFULLSET(p)		BIT_ISFULLSET(CPU_SETSIZE, p)
50256206Smav#define	CPU_SUBSET(p, c)		BIT_SUBSET(CPU_SETSIZE, p, c)
51256206Smav#define	CPU_OVERLAP(p, c)		BIT_OVERLAP(CPU_SETSIZE, p, c)
52256206Smav#define	CPU_CMP(p, c)			BIT_CMP(CPU_SETSIZE, p, c)
53256206Smav#define	CPU_OR(d, s)			BIT_OR(CPU_SETSIZE, d, s)
54256206Smav#define	CPU_AND(d, s)			BIT_AND(CPU_SETSIZE, d, s)
55256206Smav#define	CPU_NAND(d, s)			BIT_NAND(CPU_SETSIZE, d, s)
56256206Smav#define	CPU_CLR_ATOMIC(n, p)		BIT_CLR_ATOMIC(CPU_SETSIZE, n, p)
57256206Smav#define	CPU_SET_ATOMIC(n, p)		BIT_SET_ATOMIC(CPU_SETSIZE, n, p)
58256206Smav#define	CPU_OR_ATOMIC(d, s)		BIT_OR_ATOMIC(CPU_SETSIZE, d, s)
59256206Smav#define	CPU_COPY_STORE_REL(f, t)	BIT_COPY_STORE_REL(CPU_SETSIZE, f, t)
60256207Smav#define	CPU_FFS(p)			BIT_FFS(CPU_SETSIZE, p)
61233598Smav
62176730Sjeff/*
63176730Sjeff * Valid cpulevel_t values.
64176730Sjeff */
65176730Sjeff#define	CPU_LEVEL_ROOT		1	/* All system cpus. */
66176730Sjeff#define	CPU_LEVEL_CPUSET	2	/* Available cpus for which. */
67176730Sjeff#define	CPU_LEVEL_WHICH		3	/* Actual mask/id for which. */
68176730Sjeff
69176730Sjeff/*
70176730Sjeff * Valid cpuwhich_t values.
71176730Sjeff */
72176730Sjeff#define	CPU_WHICH_TID		1	/* Specifies a thread id. */
73176730Sjeff#define	CPU_WHICH_PID		2	/* Specifies a process id. */
74176730Sjeff#define	CPU_WHICH_CPUSET	3	/* Specifies a set id. */
75178092Sjeff#define	CPU_WHICH_IRQ		4	/* Specifies an irq #. */
76185435Sbz#define	CPU_WHICH_JAIL		5	/* Specifies a jail id. */
77176730Sjeff
78176730Sjeff/*
79176730Sjeff * Reserved cpuset identifiers.
80176730Sjeff */
81176730Sjeff#define	CPUSET_INVALID	-1
82176730Sjeff#define	CPUSET_DEFAULT	0
83176730Sjeff
84176730Sjeff#ifdef _KERNEL
85176730SjeffLIST_HEAD(setlist, cpuset);
86176730Sjeff
87176730Sjeff/*
88176730Sjeff * cpusets encapsulate cpu binding information for one or more threads.
89176730Sjeff *
90176730Sjeff * 	a - Accessed with atomics.
91176730Sjeff *	s - Set at creation, never modified.  Only a ref required to read.
92176730Sjeff *	c - Locked internally by a cpuset lock.
93176730Sjeff *
94176730Sjeff * The bitmask is only modified while holding the cpuset lock.  It may be
95176730Sjeff * read while only a reference is held but the consumer must be prepared
96176730Sjeff * to deal with inconsistent results.
97176730Sjeff */
98176730Sjeffstruct cpuset {
99176730Sjeff	cpuset_t		cs_mask;	/* bitmask of valid cpus. */
100176730Sjeff	volatile u_int		cs_ref;		/* (a) Reference count. */
101176730Sjeff	int			cs_flags;	/* (s) Flags from below. */
102176730Sjeff	cpusetid_t		cs_id;		/* (s) Id or INVALID. */
103176730Sjeff	struct cpuset		*cs_parent;	/* (s) Pointer to our parent. */
104176730Sjeff	LIST_ENTRY(cpuset)	cs_link;	/* (c) All identified sets. */
105176730Sjeff	LIST_ENTRY(cpuset)	cs_siblings;	/* (c) Sibling set link. */
106176730Sjeff	struct setlist		cs_children;	/* (c) List of children. */
107176730Sjeff};
108176730Sjeff
109176730Sjeff#define CPU_SET_ROOT    0x0001  /* Set is a root set. */
110176730Sjeff#define CPU_SET_RDONLY  0x0002  /* No modification allowed. */
111176730Sjeff
112177738Sjeffextern cpuset_t *cpuset_root;
113192895Sjamiestruct prison;
114185435Sbzstruct proc;
115177738Sjeff
116176730Sjeffstruct cpuset *cpuset_thread0(void);
117176730Sjeffstruct cpuset *cpuset_ref(struct cpuset *);
118177738Sjeffvoid	cpuset_rel(struct cpuset *);
119177738Sjeffint	cpuset_setthread(lwpid_t id, cpuset_t *);
120192895Sjamieint	cpuset_create_root(struct prison *, struct cpuset **);
121185435Sbzint	cpuset_setproc_update_set(struct proc *, struct cpuset *);
122222813Sattilioint	cpusetobj_ffs(const cpuset_t *);
123222813Sattiliochar	*cpusetobj_strprint(char *, const cpuset_t *);
124222813Sattilioint	cpusetobj_strscan(cpuset_t *, const char *);
125256001Sjhb#ifdef DDB
126256001Sjhbvoid	ddb_display_cpuset(const cpuset_t *);
127256001Sjhb#endif
128177738Sjeff
129176730Sjeff#else
130176730Sjeff__BEGIN_DECLS
131176730Sjeffint	cpuset(cpusetid_t *);
132176730Sjeffint	cpuset_setid(cpuwhich_t, id_t, cpusetid_t);
133176730Sjeffint	cpuset_getid(cpulevel_t, cpuwhich_t, id_t, cpusetid_t *);
134177597Sruint	cpuset_getaffinity(cpulevel_t, cpuwhich_t, id_t, size_t, cpuset_t *);
135177597Sruint	cpuset_setaffinity(cpulevel_t, cpuwhich_t, id_t, size_t, const cpuset_t *);
136176730Sjeff__END_DECLS
137176730Sjeff#endif
138176730Sjeff#endif /* !_SYS_CPUSET_H_ */
139