xref: /freebsd/sys/compat/linuxkpi/common/include/linux/cpu.h (revision 5fce802722cd4435a748d89043615324db4efe5e)
1*5fce8027SBjoern A. Zeeb /*-
2*5fce8027SBjoern A. Zeeb  * SPDX-License-Identifier: BSD-2-Clause
3*5fce8027SBjoern A. Zeeb  *
4*5fce8027SBjoern A. Zeeb  * Copyright (c) 2020-2021 The FreeBSD Foundation
5*5fce8027SBjoern A. Zeeb  *
6*5fce8027SBjoern A. Zeeb  * This software was developed by Björn Zeeb under sponsorship from
7*5fce8027SBjoern A. Zeeb  * the FreeBSD Foundation.
8*5fce8027SBjoern A. Zeeb  *
9*5fce8027SBjoern A. Zeeb  * Redistribution and use in source and binary forms, with or without
10*5fce8027SBjoern A. Zeeb  * modification, are permitted provided that the following conditions
11*5fce8027SBjoern A. Zeeb  * are met:
12*5fce8027SBjoern A. Zeeb  * 1. Redistributions of source code must retain the above copyright
13*5fce8027SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer.
14*5fce8027SBjoern A. Zeeb  * 2. Redistributions in binary form must reproduce the above copyright
15*5fce8027SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer in the
16*5fce8027SBjoern A. Zeeb  *    documentation and/or other materials provided with the distribution.
17*5fce8027SBjoern A. Zeeb  *
18*5fce8027SBjoern A. Zeeb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19*5fce8027SBjoern A. Zeeb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*5fce8027SBjoern A. Zeeb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*5fce8027SBjoern A. Zeeb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22*5fce8027SBjoern A. Zeeb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*5fce8027SBjoern A. Zeeb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*5fce8027SBjoern A. Zeeb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*5fce8027SBjoern A. Zeeb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*5fce8027SBjoern A. Zeeb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*5fce8027SBjoern A. Zeeb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*5fce8027SBjoern A. Zeeb  * SUCH DAMAGE.
29*5fce8027SBjoern A. Zeeb  *
30*5fce8027SBjoern A. Zeeb  * $FreeBSD$
31*5fce8027SBjoern A. Zeeb  */
32*5fce8027SBjoern A. Zeeb 
33*5fce8027SBjoern A. Zeeb #ifndef	__LKPI_LINUX_CPU_H
34*5fce8027SBjoern A. Zeeb #define	__LKPI_LINUX_CPU_H
35*5fce8027SBjoern A. Zeeb 
36*5fce8027SBjoern A. Zeeb #include <sys/types.h>
37*5fce8027SBjoern A. Zeeb #include <sys/systm.h>
38*5fce8027SBjoern A. Zeeb #include <sys/cpuset.h>
39*5fce8027SBjoern A. Zeeb #include <sys/smp.h>
40*5fce8027SBjoern A. Zeeb #include <linux/compiler.h>
41*5fce8027SBjoern A. Zeeb #include <linux/slab.h>
42*5fce8027SBjoern A. Zeeb 
43*5fce8027SBjoern A. Zeeb typedef	cpuset_t	cpumask_t;
44*5fce8027SBjoern A. Zeeb 
45*5fce8027SBjoern A. Zeeb extern cpumask_t cpu_online_mask;
46*5fce8027SBjoern A. Zeeb 
47*5fce8027SBjoern A. Zeeb static __inline int
48*5fce8027SBjoern A. Zeeb cpumask_next(int cpuid, cpumask_t mask)
49*5fce8027SBjoern A. Zeeb {
50*5fce8027SBjoern A. Zeeb 
51*5fce8027SBjoern A. Zeeb 	/*
52*5fce8027SBjoern A. Zeeb 	 * -1 can be an input to cpuid according to logic in drivers
53*5fce8027SBjoern A. Zeeb 	 * but is never a valid cpuid in a set!
54*5fce8027SBjoern A. Zeeb 	 */
55*5fce8027SBjoern A. Zeeb 	KASSERT((cpuid >= -1 && cpuid <= MAXCPU), ("%s: invalid cpuid %d\n",
56*5fce8027SBjoern A. Zeeb 	    __func__, cpuid));
57*5fce8027SBjoern A. Zeeb 	KASSERT(!CPU_EMPTY(&mask), ("%s: empty CPU mask", __func__));
58*5fce8027SBjoern A. Zeeb 
59*5fce8027SBjoern A. Zeeb 	do {
60*5fce8027SBjoern A. Zeeb 		cpuid++;
61*5fce8027SBjoern A. Zeeb #ifdef SMP
62*5fce8027SBjoern A. Zeeb 		if (cpuid > mp_maxid)
63*5fce8027SBjoern A. Zeeb #endif
64*5fce8027SBjoern A. Zeeb 			cpuid = 0;
65*5fce8027SBjoern A. Zeeb 	} while (!CPU_ISSET(cpuid, &mask));
66*5fce8027SBjoern A. Zeeb 	return (cpuid);
67*5fce8027SBjoern A. Zeeb }
68*5fce8027SBjoern A. Zeeb 
69*5fce8027SBjoern A. Zeeb static __inline void
70*5fce8027SBjoern A. Zeeb cpumask_set_cpu(int cpu, cpumask_t *mask)
71*5fce8027SBjoern A. Zeeb {
72*5fce8027SBjoern A. Zeeb 
73*5fce8027SBjoern A. Zeeb 	CPU_SET(cpu, mask);
74*5fce8027SBjoern A. Zeeb }
75*5fce8027SBjoern A. Zeeb 
76*5fce8027SBjoern A. Zeeb #endif	/* __LKPI_LINUX_CPU_H */
77