Merge git://git.infradead.org/battery-2.6
[linux-2.6] / drivers / base / node.c
1 /*
2  * drivers/base/node.c - basic Node class support
3  */
4
5 #include <linux/sysdev.h>
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/mm.h>
9 #include <linux/node.h>
10 #include <linux/hugetlb.h>
11 #include <linux/cpumask.h>
12 #include <linux/topology.h>
13 #include <linux/nodemask.h>
14 #include <linux/cpu.h>
15 #include <linux/device.h>
16
17 static struct sysdev_class node_class = {
18         set_kset_name("node"),
19 };
20
21
22 static ssize_t node_read_cpumap(struct sys_device * dev, char * buf)
23 {
24         struct node *node_dev = to_node(dev);
25         cpumask_t mask = node_to_cpumask(node_dev->sysdev.id);
26         int len;
27
28         /* 2004/06/03: buf currently PAGE_SIZE, need > 1 char per 4 bits. */
29         BUILD_BUG_ON(MAX_NUMNODES/4 > PAGE_SIZE/2);
30
31         len = cpumask_scnprintf(buf, PAGE_SIZE-1, mask);
32         len += sprintf(buf + len, "\n");
33         return len;
34 }
35
36 static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumap, NULL);
37
38 #define K(x) ((x) << (PAGE_SHIFT - 10))
39 static ssize_t node_read_meminfo(struct sys_device * dev, char * buf)
40 {
41         int n;
42         int nid = dev->id;
43         struct sysinfo i;
44
45         si_meminfo_node(&i, nid);
46
47         n = sprintf(buf, "\n"
48                        "Node %d MemTotal:     %8lu kB\n"
49                        "Node %d MemFree:      %8lu kB\n"
50                        "Node %d MemUsed:      %8lu kB\n"
51                        "Node %d Active:       %8lu kB\n"
52                        "Node %d Inactive:     %8lu kB\n"
53 #ifdef CONFIG_HIGHMEM
54                        "Node %d HighTotal:    %8lu kB\n"
55                        "Node %d HighFree:     %8lu kB\n"
56                        "Node %d LowTotal:     %8lu kB\n"
57                        "Node %d LowFree:      %8lu kB\n"
58 #endif
59                        "Node %d Dirty:        %8lu kB\n"
60                        "Node %d Writeback:    %8lu kB\n"
61                        "Node %d FilePages:    %8lu kB\n"
62                        "Node %d Mapped:       %8lu kB\n"
63                        "Node %d AnonPages:    %8lu kB\n"
64                        "Node %d PageTables:   %8lu kB\n"
65                        "Node %d NFS_Unstable: %8lu kB\n"
66                        "Node %d Bounce:       %8lu kB\n"
67                        "Node %d Slab:         %8lu kB\n"
68                        "Node %d SReclaimable: %8lu kB\n"
69                        "Node %d SUnreclaim:   %8lu kB\n",
70                        nid, K(i.totalram),
71                        nid, K(i.freeram),
72                        nid, K(i.totalram - i.freeram),
73                        nid, node_page_state(nid, NR_ACTIVE),
74                        nid, node_page_state(nid, NR_INACTIVE),
75 #ifdef CONFIG_HIGHMEM
76                        nid, K(i.totalhigh),
77                        nid, K(i.freehigh),
78                        nid, K(i.totalram - i.totalhigh),
79                        nid, K(i.freeram - i.freehigh),
80 #endif
81                        nid, K(node_page_state(nid, NR_FILE_DIRTY)),
82                        nid, K(node_page_state(nid, NR_WRITEBACK)),
83                        nid, K(node_page_state(nid, NR_FILE_PAGES)),
84                        nid, K(node_page_state(nid, NR_FILE_MAPPED)),
85                        nid, K(node_page_state(nid, NR_ANON_PAGES)),
86                        nid, K(node_page_state(nid, NR_PAGETABLE)),
87                        nid, K(node_page_state(nid, NR_UNSTABLE_NFS)),
88                        nid, K(node_page_state(nid, NR_BOUNCE)),
89                        nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE) +
90                                 node_page_state(nid, NR_SLAB_UNRECLAIMABLE)),
91                        nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE)),
92                        nid, K(node_page_state(nid, NR_SLAB_UNRECLAIMABLE)));
93         n += hugetlb_report_node_meminfo(nid, buf + n);
94         return n;
95 }
96
97 #undef K
98 static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
99
100 static ssize_t node_read_numastat(struct sys_device * dev, char * buf)
101 {
102         return sprintf(buf,
103                        "numa_hit %lu\n"
104                        "numa_miss %lu\n"
105                        "numa_foreign %lu\n"
106                        "interleave_hit %lu\n"
107                        "local_node %lu\n"
108                        "other_node %lu\n",
109                        node_page_state(dev->id, NUMA_HIT),
110                        node_page_state(dev->id, NUMA_MISS),
111                        node_page_state(dev->id, NUMA_FOREIGN),
112                        node_page_state(dev->id, NUMA_INTERLEAVE_HIT),
113                        node_page_state(dev->id, NUMA_LOCAL),
114                        node_page_state(dev->id, NUMA_OTHER));
115 }
116 static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
117
118 static ssize_t node_read_distance(struct sys_device * dev, char * buf)
119 {
120         int nid = dev->id;
121         int len = 0;
122         int i;
123
124         /* buf currently PAGE_SIZE, need ~4 chars per node */
125         BUILD_BUG_ON(MAX_NUMNODES*4 > PAGE_SIZE/2);
126
127         for_each_online_node(i)
128                 len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
129
130         len += sprintf(buf + len, "\n");
131         return len;
132 }
133 static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
134
135
136 /*
137  * register_node - Setup a sysfs device for a node.
138  * @num - Node number to use when creating the device.
139  *
140  * Initialize and register the node device.
141  */
142 int register_node(struct node *node, int num, struct node *parent)
143 {
144         int error;
145
146         node->sysdev.id = num;
147         node->sysdev.cls = &node_class;
148         error = sysdev_register(&node->sysdev);
149
150         if (!error){
151                 sysdev_create_file(&node->sysdev, &attr_cpumap);
152                 sysdev_create_file(&node->sysdev, &attr_meminfo);
153                 sysdev_create_file(&node->sysdev, &attr_numastat);
154                 sysdev_create_file(&node->sysdev, &attr_distance);
155         }
156         return error;
157 }
158
159 /**
160  * unregister_node - unregister a node device
161  * @node: node going away
162  *
163  * Unregisters a node device @node.  All the devices on the node must be
164  * unregistered before calling this function.
165  */
166 void unregister_node(struct node *node)
167 {
168         sysdev_remove_file(&node->sysdev, &attr_cpumap);
169         sysdev_remove_file(&node->sysdev, &attr_meminfo);
170         sysdev_remove_file(&node->sysdev, &attr_numastat);
171         sysdev_remove_file(&node->sysdev, &attr_distance);
172
173         sysdev_unregister(&node->sysdev);
174 }
175
176 struct node node_devices[MAX_NUMNODES];
177
178 /*
179  * register cpu under node
180  */
181 int register_cpu_under_node(unsigned int cpu, unsigned int nid)
182 {
183         if (node_online(nid)) {
184                 struct sys_device *obj = get_cpu_sysdev(cpu);
185                 if (!obj)
186                         return 0;
187                 return sysfs_create_link(&node_devices[nid].sysdev.kobj,
188                                          &obj->kobj,
189                                          kobject_name(&obj->kobj));
190          }
191
192         return 0;
193 }
194
195 int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
196 {
197         if (node_online(nid)) {
198                 struct sys_device *obj = get_cpu_sysdev(cpu);
199                 if (obj)
200                         sysfs_remove_link(&node_devices[nid].sysdev.kobj,
201                                          kobject_name(&obj->kobj));
202         }
203         return 0;
204 }
205
206 int register_one_node(int nid)
207 {
208         int error = 0;
209         int cpu;
210
211         if (node_online(nid)) {
212                 int p_node = parent_node(nid);
213                 struct node *parent = NULL;
214
215                 if (p_node != nid)
216                         parent = &node_devices[p_node];
217
218                 error = register_node(&node_devices[nid], nid, parent);
219
220                 /* link cpu under this node */
221                 for_each_present_cpu(cpu) {
222                         if (cpu_to_node(cpu) == nid)
223                                 register_cpu_under_node(cpu, nid);
224                 }
225         }
226
227         return error;
228
229 }
230
231 void unregister_one_node(int nid)
232 {
233         unregister_node(&node_devices[nid]);
234 }
235
236 /*
237  * node states attributes
238  */
239
240 static ssize_t print_nodes_state(enum node_states state, char *buf)
241 {
242         int n;
243
244         n = nodelist_scnprintf(buf, PAGE_SIZE, node_states[state]);
245         if (n > 0 && PAGE_SIZE > n + 1) {
246                 *(buf + n++) = '\n';
247                 *(buf + n++) = '\0';
248         }
249         return n;
250 }
251
252 static ssize_t print_nodes_possible(struct sysdev_class *class, char *buf)
253 {
254         return print_nodes_state(N_POSSIBLE, buf);
255 }
256
257 static ssize_t print_nodes_online(struct sysdev_class *class, char *buf)
258 {
259         return print_nodes_state(N_ONLINE, buf);
260 }
261
262 static ssize_t print_nodes_has_normal_memory(struct sysdev_class *class,
263                                                 char *buf)
264 {
265         return print_nodes_state(N_NORMAL_MEMORY, buf);
266 }
267
268 static ssize_t print_nodes_has_cpu(struct sysdev_class *class, char *buf)
269 {
270         return print_nodes_state(N_CPU, buf);
271 }
272
273 static SYSDEV_CLASS_ATTR(possible, 0444, print_nodes_possible, NULL);
274 static SYSDEV_CLASS_ATTR(online, 0444, print_nodes_online, NULL);
275 static SYSDEV_CLASS_ATTR(has_normal_memory, 0444, print_nodes_has_normal_memory,
276                                                                         NULL);
277 static SYSDEV_CLASS_ATTR(has_cpu, 0444, print_nodes_has_cpu, NULL);
278
279 #ifdef CONFIG_HIGHMEM
280 static ssize_t print_nodes_has_high_memory(struct sysdev_class *class,
281                                                  char *buf)
282 {
283         return print_nodes_state(N_HIGH_MEMORY, buf);
284 }
285
286 static SYSDEV_CLASS_ATTR(has_high_memory, 0444, print_nodes_has_high_memory,
287                                                                          NULL);
288 #endif
289
290 struct sysdev_class_attribute *node_state_attr[] = {
291         &attr_possible,
292         &attr_online,
293         &attr_has_normal_memory,
294 #ifdef CONFIG_HIGHMEM
295         &attr_has_high_memory,
296 #endif
297         &attr_has_cpu,
298 };
299
300 static int node_states_init(void)
301 {
302         int i;
303         int err = 0;
304
305         for (i = 0;  i < NR_NODE_STATES; i++) {
306                 int ret;
307                 ret = sysdev_class_create_file(&node_class, node_state_attr[i]);
308                 if (!err)
309                         err = ret;
310         }
311         return err;
312 }
313
314 static int __init register_node_type(void)
315 {
316         int ret;
317
318         ret = sysdev_class_register(&node_class);
319         if (!ret)
320                 ret = node_states_init();
321
322         /*
323          * Note:  we're not going to unregister the node class if we fail
324          * to register the node state class attribute files.
325          */
326         return ret;
327 }
328 postcore_initcall(register_node_type);