Whamcloud - gitweb
124523feffcec72dec6e2cd5c690d413d701e15b
[fs/lustre-release.git] / libcfs / include / libcfs / linux / linux-cpu.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 021110-1307, USA
20  *
21  * GPL HEADER END
22  */
23 /*
24  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
25  * Copyright (c) 2011, 2012, Whamcloud, Inc.
26  */
27 /*
28  * This file is part of Lustre, http://www.lustre.org/
29  * Lustre is a trademark of Sun Microsystems, Inc.
30  *
31  * libcfs/include/libcfs/linux/linux-mem.h
32  *
33  * Basic library routines.
34  *
35  * Author: liang@whamcloud.com
36  */
37
38 #ifndef __LIBCFS_LINUX_CPU_H__
39 #define __LIBCFS_LINUX_CPU_H__
40
41 #ifndef __LIBCFS_LIBCFS_H__
42 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
43 #endif
44
45 #ifndef __KERNEL__
46 #error This include is only for kernel use.
47 #endif
48
49 #include <linux/cpu.h>
50 #include <linux/cpuset.h>
51 #include <linux/topology.h>
52 #include <linux/version.h>
53
54 #ifndef HAVE_CPUMASK_SIZE
55 #define cpumask_size()          sizeof(cpumask_t)
56 #endif
57
58 #ifdef CONFIG_SMP
59
60 #define HAVE_LIBCFS_CPT
61
62 /** virtual processing unit */
63 struct cfs_cpu_partition {
64         /* CPUs mask for this partition */
65         cpumask_t                       *cpt_cpumask;
66         /* nodes mask for this partition */
67         nodemask_t                      *cpt_nodemask;
68         /* spread rotor for NUMA allocator */
69         unsigned                        cpt_spread_rotor;
70 };
71
72 /** descriptor for CPU partitions */
73 struct cfs_cpt_table {
74         /* version, reserved for hotplug */
75         unsigned                        ctb_version;
76         /* spread rotor for NUMA allocator */
77         unsigned                        ctb_spread_rotor;
78         /* # of CPU partitions */
79         unsigned                        ctb_nparts;
80         /* partitions tables */
81         struct cfs_cpu_partition        *ctb_parts;
82         /* shadow HW CPU to CPU partition ID */
83         int                             *ctb_cpu2cpt;
84         /* all cpus in this partition table */
85         cpumask_t                       *ctb_cpumask;
86         /* all nodes in this partition table */
87         nodemask_t                      *ctb_nodemask;
88 };
89
90 void cfs_cpu_core_siblings(int cpu, cpumask_t *mask);
91 void cfs_cpu_ht_siblings(int cpu, cpumask_t *mask);
92 void cfs_node_to_cpumask(int node, cpumask_t *mask);
93 int cfs_cpu_core_nsiblings(int cpu);
94 int cfs_cpu_ht_nsiblings(int cpu);
95
96 /**
97  * comment out definitions for compatible layer
98  * #define CFS_CPU_NR                          NR_CPUS
99  *
100  * typedef cpumask_t                           cfs_cpumask_t;
101  *
102  * #define cfs_cpu_current()                   smp_processor_id()
103  * #define cfs_cpu_online(i)                   cpu_online(i)
104  * #define cfs_cpu_online_num()                num_online_cpus()
105  * #define cfs_cpu_online_for_each(i)          for_each_online_cpu(i)
106  * #define cfs_cpu_possible_num()              num_possible_cpus()
107  * #define cfs_cpu_possible_for_each(i)        for_each_possible_cpu(i)
108  *
109  * #ifdef CONFIG_CPUMASK_SIZE
110  * #define cfs_cpu_mask_size()                 cpumask_size()
111  * #else
112  * #define cfs_cpu_mask_size()                 sizeof(cfs_cpumask_t)
113  * #endif
114  *
115  * #define cfs_cpu_mask_set(i, mask)           cpu_set(i, mask)
116  * #define cfs_cpu_mask_unset(i, mask)         cpu_clear(i, mask)
117  * #define cfs_cpu_mask_isset(i, mask)         cpu_isset(i, mask)
118  * #define cfs_cpu_mask_clear(mask)            cpus_clear(mask)
119  * #define cfs_cpu_mask_empty(mask)            cpus_empty(mask)
120  * #define cfs_cpu_mask_weight(mask)           cpus_weight(mask)
121  * #define cfs_cpu_mask_first(mask)            first_cpu(mask)
122  * #define cfs_cpu_mask_any_online(mask)      (any_online_cpu(mask) != NR_CPUS)
123  * #define cfs_cpu_mask_for_each(i, mask)      for_each_cpu_mask(i, mask)
124  * #define cfs_cpu_mask_bind(t, mask)          set_cpus_allowed(t, mask)
125  *
126  * #ifdef HAVE_CPUMASK_COPY
127  * #define cfs_cpu_mask_copy(dst, src)         cpumask_copy(dst, src)
128  * #else
129  * #define cfs_cpu_mask_copy(dst, src)         memcpy(dst, src, sizeof(*src))
130  * #endif
131  *
132  * static inline void
133  * cfs_cpu_mask_of_online(cfs_cpumask_t *mask)
134  * {
135  * cfs_cpu_mask_copy(mask, &cpu_online_map);
136  * }
137  *
138  * #ifdef CONFIG_NUMA
139  *
140  * #define CFS_NODE_NR                         MAX_NUMNODES
141  *
142  * typedef nodemask_t                          cfs_node_mask_t;
143  *
144  * #define cfs_node_of_cpu(cpu)                cpu_to_node(cpu)
145  * #define cfs_node_online(i)                  node_online(i)
146  * #define cfs_node_online_num()               num_online_nodes()
147  * #define cfs_node_online_for_each(i)         for_each_online_node(i)
148  * #define cfs_node_possible_num()             num_possible_nodes()
149  * #define cfs_node_possible_for_each(i)       for_each_node(i)
150  *
151  * static inline void cfs_node_to_cpumask(int node, cfs_cpumask_t *mask)
152  * {
153  * #if defined(HAVE_NODE_TO_CPUMASK)
154  *      *mask = node_to_cpumask(node);
155  * #elif defined(HAVE_CPUMASK_OF_NODE)
156  *      cfs_cpu_mask_copy(mask, cpumask_of_node(node));
157  * #else
158  * # error "Needs node_to_cpumask or cpumask_of_node"
159  * #endif
160  * }
161  *
162  * #define cfs_node_mask_set(i, mask)          node_set(i, mask)
163  * #define cfs_node_mask_unset(i, mask)        node_clear(i, mask)
164  * #define cfs_node_mask_isset(i, mask)        node_isset(i, mask)
165  * #define cfs_node_mask_clear(mask)           nodes_reset(mask)
166  * #define cfs_node_mask_empty(mask)           nodes_empty(mask)
167  * #define cfs_node_mask_weight(mask)          nodes_weight(mask)
168  * #define cfs_node_mask_for_each(i, mask)     for_each_node_mask(i, mask)
169  * #define cfs_node_mask_copy(dst, src)        memcpy(dst, src, sizeof(*src))
170  *
171  * static inline void
172  * cfs_node_mask_of_online(cfs_node_mask_t *mask)
173  * {
174  *       cfs_node_mask_copy(mask, &node_online_map);
175  * }
176  *
177  * #endif
178  */
179
180 #endif /* CONFIG_SMP */
181 #endif /* __LIBCFS_LINUX_CPU_H__ */