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
37250395Sattilio#include <sys/bitset.h>
38250395Sattilio
39222813Sattilio#define	CPUSETBUFSIZ	((2 + sizeof(long) * 2) * _NCPUWORDS)
40176730Sjeff
41250395Sattilio#define	CPU_CLR(n, p)			BIT_CLR(CPU_SETSIZE, n, p)
42250395Sattilio#define	CPU_COPY(f, t)			BIT_COPY(CPU_SETSIZE, f, t)
43250395Sattilio#define	CPU_ISSET(n, p)			BIT_ISSET(CPU_SETSIZE, n, p)
44250395Sattilio#define	CPU_SET(n, p)			BIT_SET(CPU_SETSIZE, n, p)
45250395Sattilio#define	CPU_ZERO(p) 			BIT_ZERO(CPU_SETSIZE, p)
46250395Sattilio#define	CPU_FILL(p) 			BIT_FILL(CPU_SETSIZE, p)
47250395Sattilio#define	CPU_SETOF(n, p)			BIT_SETOF(CPU_SETSIZE, n, p)
48250395Sattilio#define	CPU_EMPTY(p)			BIT_EMPTY(CPU_SETSIZE, p)
49250395Sattilio#define	CPU_ISFULLSET(p)		BIT_ISFULLSET(CPU_SETSIZE, p)
50250395Sattilio#define	CPU_SUBSET(p, c)		BIT_SUBSET(CPU_SETSIZE, p, c)
51250395Sattilio#define	CPU_OVERLAP(p, c)		BIT_OVERLAP(CPU_SETSIZE, p, c)
52250395Sattilio#define	CPU_CMP(p, c)			BIT_CMP(CPU_SETSIZE, p, c)
53250395Sattilio#define	CPU_OR(d, s)			BIT_OR(CPU_SETSIZE, d, s)
54250395Sattilio#define	CPU_AND(d, s)			BIT_AND(CPU_SETSIZE, d, s)
55250395Sattilio#define	CPU_NAND(d, s)			BIT_NAND(CPU_SETSIZE, d, s)
56250395Sattilio#define	CPU_CLR_ATOMIC(n, p)		BIT_CLR_ATOMIC(CPU_SETSIZE, n, p)
57250395Sattilio#define	CPU_SET_ATOMIC(n, p)		BIT_SET_ATOMIC(CPU_SETSIZE, n, p)
58255059Skib#define	CPU_AND_ATOMIC(n, p)		BIT_AND_ATOMIC(CPU_SETSIZE, n, p)
59250395Sattilio#define	CPU_OR_ATOMIC(d, s)		BIT_OR_ATOMIC(CPU_SETSIZE, d, s)
60250395Sattilio#define	CPU_COPY_STORE_REL(f, t)	BIT_COPY_STORE_REL(CPU_SETSIZE, f, t)
61251703Sjeff#define	CPU_FFS(p)			BIT_FFS(CPU_SETSIZE, p)
62232852Smav
63176730Sjeff/*
64176730Sjeff * Valid cpulevel_t values.
65176730Sjeff */
66176730Sjeff#define	CPU_LEVEL_ROOT		1	/* All system cpus. */
67176730Sjeff#define	CPU_LEVEL_CPUSET	2	/* Available cpus for which. */
68176730Sjeff#define	CPU_LEVEL_WHICH		3	/* Actual mask/id for which. */
69176730Sjeff
70176730Sjeff/*
71176730Sjeff * Valid cpuwhich_t values.
72176730Sjeff */
73176730Sjeff#define	CPU_WHICH_TID		1	/* Specifies a thread id. */
74176730Sjeff#define	CPU_WHICH_PID		2	/* Specifies a process id. */
75176730Sjeff#define	CPU_WHICH_CPUSET	3	/* Specifies a set id. */
76178092Sjeff#define	CPU_WHICH_IRQ		4	/* Specifies an irq #. */
77185435Sbz#define	CPU_WHICH_JAIL		5	/* Specifies a jail id. */
78176730Sjeff
79176730Sjeff/*
80176730Sjeff * Reserved cpuset identifiers.
81176730Sjeff */
82176730Sjeff#define	CPUSET_INVALID	-1
83176730Sjeff#define	CPUSET_DEFAULT	0
84176730Sjeff
85176730Sjeff#ifdef _KERNEL
86176730SjeffLIST_HEAD(setlist, cpuset);
87176730Sjeff
88176730Sjeff/*
89176730Sjeff * cpusets encapsulate cpu binding information for one or more threads.
90176730Sjeff *
91176730Sjeff * 	a - Accessed with atomics.
92176730Sjeff *	s - Set at creation, never modified.  Only a ref required to read.
93176730Sjeff *	c - Locked internally by a cpuset lock.
94176730Sjeff *
95176730Sjeff * The bitmask is only modified while holding the cpuset lock.  It may be
96176730Sjeff * read while only a reference is held but the consumer must be prepared
97176730Sjeff * to deal with inconsistent results.
98176730Sjeff */
99176730Sjeffstruct cpuset {
100176730Sjeff	cpuset_t		cs_mask;	/* bitmask of valid cpus. */
101176730Sjeff	volatile u_int		cs_ref;		/* (a) Reference count. */
102176730Sjeff	int			cs_flags;	/* (s) Flags from below. */
103176730Sjeff	cpusetid_t		cs_id;		/* (s) Id or INVALID. */
104176730Sjeff	struct cpuset		*cs_parent;	/* (s) Pointer to our parent. */
105176730Sjeff	LIST_ENTRY(cpuset)	cs_link;	/* (c) All identified sets. */
106176730Sjeff	LIST_ENTRY(cpuset)	cs_siblings;	/* (c) Sibling set link. */
107176730Sjeff	struct setlist		cs_children;	/* (c) List of children. */
108176730Sjeff};
109176730Sjeff
110176730Sjeff#define CPU_SET_ROOT    0x0001  /* Set is a root set. */
111176730Sjeff#define CPU_SET_RDONLY  0x0002  /* No modification allowed. */
112176730Sjeff
113177738Sjeffextern cpuset_t *cpuset_root;
114192895Sjamiestruct prison;
115185435Sbzstruct proc;
116177738Sjeff
117176730Sjeffstruct cpuset *cpuset_thread0(void);
118176730Sjeffstruct cpuset *cpuset_ref(struct cpuset *);
119177738Sjeffvoid	cpuset_rel(struct cpuset *);
120177738Sjeffint	cpuset_setthread(lwpid_t id, cpuset_t *);
121192895Sjamieint	cpuset_create_root(struct prison *, struct cpuset **);
122185435Sbzint	cpuset_setproc_update_set(struct proc *, struct cpuset *);
123222813Sattiliochar	*cpusetobj_strprint(char *, const cpuset_t *);
124222813Sattilioint	cpusetobj_strscan(cpuset_t *, const char *);
125252209Sjhb#ifdef DDB
126252209Sjhbvoid	ddb_display_cpuset(const cpuset_t *);
127252209Sjhb#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