blob: a2484fc1a06c43482e575888ca1991d45faf43e7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * This file contains NUMA specific variables and functions which can
7 * be split away from DISCONTIGMEM and are used on NUMA machines with
8 * contiguous memory.
9 * 2002/08/07 Erich Focht <efocht@ess.nec.de>
10 * Populate cpu entries in sysfs for non-numa systems as well
11 * Intel Corporation - Ashok Raj
Zhang, Yanminf1918002006-02-27 11:37:45 +080012 * 02/27/2006 Zhang, Yanmin
13 * Populate cpu cache entries in sysfs for cpu cache info
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/cpu.h>
17#include <linux/kernel.h>
18#include <linux/mm.h>
19#include <linux/node.h>
20#include <linux/init.h>
21#include <linux/bootmem.h>
22#include <linux/nodemask.h>
Zhang, Yanminf1918002006-02-27 11:37:45 +080023#include <linux/notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/mmzone.h>
25#include <asm/numa.h>
26#include <asm/cpu.h>
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static struct ia64_cpu *sysfs_cpus;
29
30int arch_register_cpu(int num)
31{
Ashok Rajb88e9262006-01-19 16:18:47 -080032#if defined (CONFIG_ACPI) && defined (CONFIG_HOTPLUG_CPU)
Ashok Raj55e59c52005-03-31 22:51:10 -050033 /*
Siddha, Suresh B72486f12006-12-07 02:14:10 +010034 * If CPEI can be re-targetted or if this is not
35 * CPEI target, then it is hotpluggable
Ashok Raj55e59c52005-03-31 22:51:10 -050036 */
Siddha, Suresh B72486f12006-12-07 02:14:10 +010037 if (can_cpei_retarget() || !is_cpu_cpei_target(num))
38 sysfs_cpus[num].cpu.hotpluggable = 1;
KAMEZAWA Hiroyuki3212fe12006-09-25 16:25:31 -070039 map_cpu_to_node(num, node_cpuid[num].nid);
40#endif
Ashok Raj55e59c52005-03-31 22:51:10 -050041
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -070042 return register_cpu(&sysfs_cpus[num].cpu, num);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
44
45#ifdef CONFIG_HOTPLUG_CPU
46
47void arch_unregister_cpu(int num)
48{
KAMEZAWA Hiroyuki3212fe12006-09-25 16:25:31 -070049 unregister_cpu(&sysfs_cpus[num].cpu);
50 unmap_cpu_from_node(num, cpu_to_node(num));
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52EXPORT_SYMBOL(arch_register_cpu);
53EXPORT_SYMBOL(arch_unregister_cpu);
54#endif /*CONFIG_HOTPLUG_CPU*/
55
56
57static int __init topology_init(void)
58{
59 int i, err = 0;
60
61#ifdef CONFIG_NUMA
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080062 /*
63 * MCD - Do we want to register all ONLINE nodes, or all POSSIBLE nodes?
64 */
65 for_each_online_node(i) {
Yasunori Goto0fc44152006-06-27 02:53:38 -070066 if ((err = register_one_node(i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 goto out;
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#endif
70
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080071 sysfs_cpus = kzalloc(sizeof(struct ia64_cpu) * NR_CPUS, GFP_KERNEL);
Paul Jacksona8132132006-08-14 22:45:49 -070072 if (!sysfs_cpus)
73 panic("kzalloc in topology_init failed - NR_CPUS too big?");
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080075 for_each_present_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if((err = arch_register_cpu(i)))
77 goto out;
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070079out:
80 return err;
81}
82
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080083subsys_initcall(topology_init);
Zhang, Yanminf1918002006-02-27 11:37:45 +080084
85
86/*
87 * Export cpu cache information through sysfs
88 */
89
90/*
91 * A bunch of string array to get pretty printing
92 */
93static const char *cache_types[] = {
94 "", /* not used */
95 "Instruction",
96 "Data",
97 "Unified" /* unified */
98};
99
100static const char *cache_mattrib[]={
101 "WriteThrough",
102 "WriteBack",
103 "", /* reserved */
104 "" /* reserved */
105};
106
107struct cache_info {
108 pal_cache_config_info_t cci;
109 cpumask_t shared_cpu_map;
110 int level;
111 int type;
112 struct kobject kobj;
113};
114
115struct cpu_cache_info {
116 struct cache_info *cache_leaves;
117 int num_cache_leaves;
118 struct kobject kobj;
119};
120
Satyam Sharmadb6a5ce2007-10-02 13:39:45 -0700121static struct cpu_cache_info all_cpu_cache_info[NR_CPUS] __cpuinitdata;
Zhang, Yanminf1918002006-02-27 11:37:45 +0800122#define LEAF_KOBJECT_PTR(x,y) (&all_cpu_cache_info[x].cache_leaves[y])
123
124#ifdef CONFIG_SMP
Satyam Sharmadb6a5ce2007-10-02 13:39:45 -0700125static void __cpuinit cache_shared_cpu_map_setup( unsigned int cpu,
Zhang, Yanminf1918002006-02-27 11:37:45 +0800126 struct cache_info * this_leaf)
127{
128 pal_cache_shared_info_t csi;
129 int num_shared, i = 0;
130 unsigned int j;
131
132 if (cpu_data(cpu)->threads_per_core <= 1 &&
133 cpu_data(cpu)->cores_per_socket <= 1) {
134 cpu_set(cpu, this_leaf->shared_cpu_map);
135 return;
136 }
137
138 if (ia64_pal_cache_shared_info(this_leaf->level,
139 this_leaf->type,
140 0,
141 &csi) != PAL_STATUS_SUCCESS)
142 return;
143
144 num_shared = (int) csi.num_shared;
145 do {
Andrew Mortonfb1bb342006-06-25 05:46:43 -0700146 for_each_possible_cpu(j)
Zhang, Yanminf1918002006-02-27 11:37:45 +0800147 if (cpu_data(cpu)->socket_id == cpu_data(j)->socket_id
148 && cpu_data(j)->core_id == csi.log1_cid
149 && cpu_data(j)->thread_id == csi.log1_tid)
150 cpu_set(j, this_leaf->shared_cpu_map);
151
152 i++;
153 } while (i < num_shared &&
154 ia64_pal_cache_shared_info(this_leaf->level,
155 this_leaf->type,
156 i,
157 &csi) == PAL_STATUS_SUCCESS);
158}
159#else
Satyam Sharmadb6a5ce2007-10-02 13:39:45 -0700160static void __cpuinit cache_shared_cpu_map_setup(unsigned int cpu,
Zhang, Yanminf1918002006-02-27 11:37:45 +0800161 struct cache_info * this_leaf)
162{
163 cpu_set(cpu, this_leaf->shared_cpu_map);
164 return;
165}
166#endif
167
168static ssize_t show_coherency_line_size(struct cache_info *this_leaf,
169 char *buf)
170{
171 return sprintf(buf, "%u\n", 1 << this_leaf->cci.pcci_line_size);
172}
173
174static ssize_t show_ways_of_associativity(struct cache_info *this_leaf,
175 char *buf)
176{
177 return sprintf(buf, "%u\n", this_leaf->cci.pcci_assoc);
178}
179
180static ssize_t show_attributes(struct cache_info *this_leaf, char *buf)
181{
182 return sprintf(buf,
183 "%s\n",
184 cache_mattrib[this_leaf->cci.pcci_cache_attr]);
185}
186
187static ssize_t show_size(struct cache_info *this_leaf, char *buf)
188{
189 return sprintf(buf, "%uK\n", this_leaf->cci.pcci_cache_size / 1024);
190}
191
192static ssize_t show_number_of_sets(struct cache_info *this_leaf, char *buf)
193{
194 unsigned number_of_sets = this_leaf->cci.pcci_cache_size;
195 number_of_sets /= this_leaf->cci.pcci_assoc;
196 number_of_sets /= 1 << this_leaf->cci.pcci_line_size;
197
198 return sprintf(buf, "%u\n", number_of_sets);
199}
200
201static ssize_t show_shared_cpu_map(struct cache_info *this_leaf, char *buf)
202{
203 ssize_t len;
204 cpumask_t shared_cpu_map;
205
206 cpus_and(shared_cpu_map, this_leaf->shared_cpu_map, cpu_online_map);
207 len = cpumask_scnprintf(buf, NR_CPUS+1, shared_cpu_map);
208 len += sprintf(buf+len, "\n");
209 return len;
210}
211
212static ssize_t show_type(struct cache_info *this_leaf, char *buf)
213{
214 int type = this_leaf->type + this_leaf->cci.pcci_unified;
215 return sprintf(buf, "%s\n", cache_types[type]);
216}
217
218static ssize_t show_level(struct cache_info *this_leaf, char *buf)
219{
220 return sprintf(buf, "%u\n", this_leaf->level);
221}
222
223struct cache_attr {
224 struct attribute attr;
225 ssize_t (*show)(struct cache_info *, char *);
226 ssize_t (*store)(struct cache_info *, const char *, size_t count);
227};
228
229#ifdef define_one_ro
230 #undef define_one_ro
231#endif
232#define define_one_ro(_name) \
233 static struct cache_attr _name = \
234__ATTR(_name, 0444, show_##_name, NULL)
235
236define_one_ro(level);
237define_one_ro(type);
238define_one_ro(coherency_line_size);
239define_one_ro(ways_of_associativity);
240define_one_ro(size);
241define_one_ro(number_of_sets);
242define_one_ro(shared_cpu_map);
243define_one_ro(attributes);
244
245static struct attribute * cache_default_attrs[] = {
246 &type.attr,
247 &level.attr,
248 &coherency_line_size.attr,
249 &ways_of_associativity.attr,
250 &attributes.attr,
251 &size.attr,
252 &number_of_sets.attr,
253 &shared_cpu_map.attr,
254 NULL
255};
256
257#define to_object(k) container_of(k, struct cache_info, kobj)
258#define to_attr(a) container_of(a, struct cache_attr, attr)
259
260static ssize_t cache_show(struct kobject * kobj, struct attribute * attr, char * buf)
261{
262 struct cache_attr *fattr = to_attr(attr);
263 struct cache_info *this_leaf = to_object(kobj);
264 ssize_t ret;
265
266 ret = fattr->show ? fattr->show(this_leaf, buf) : 0;
267 return ret;
268}
269
270static struct sysfs_ops cache_sysfs_ops = {
271 .show = cache_show
272};
273
274static struct kobj_type cache_ktype = {
275 .sysfs_ops = &cache_sysfs_ops,
276 .default_attrs = cache_default_attrs,
277};
278
279static struct kobj_type cache_ktype_percpu_entry = {
280 .sysfs_ops = &cache_sysfs_ops,
281};
282
283static void __cpuinit cpu_cache_sysfs_exit(unsigned int cpu)
284{
Jesper Juhlcbf283c2006-04-20 10:11:09 -0700285 kfree(all_cpu_cache_info[cpu].cache_leaves);
286 all_cpu_cache_info[cpu].cache_leaves = NULL;
Zhang, Yanminf1918002006-02-27 11:37:45 +0800287 all_cpu_cache_info[cpu].num_cache_leaves = 0;
288 memset(&all_cpu_cache_info[cpu].kobj, 0, sizeof(struct kobject));
Zhang, Yanminf1918002006-02-27 11:37:45 +0800289 return;
290}
291
292static int __cpuinit cpu_cache_sysfs_init(unsigned int cpu)
293{
294 u64 i, levels, unique_caches;
295 pal_cache_config_info_t cci;
296 int j;
297 s64 status;
298 struct cache_info *this_cache;
299 int num_cache_leaves = 0;
300
301 if ((status = ia64_pal_cache_summary(&levels, &unique_caches)) != 0) {
302 printk(KERN_ERR "ia64_pal_cache_summary=%ld\n", status);
303 return -1;
304 }
305
306 this_cache=kzalloc(sizeof(struct cache_info)*unique_caches,
307 GFP_KERNEL);
308 if (this_cache == NULL)
309 return -ENOMEM;
310
311 for (i=0; i < levels; i++) {
312 for (j=2; j >0 ; j--) {
313 if ((status=ia64_pal_cache_config_info(i,j, &cci)) !=
314 PAL_STATUS_SUCCESS)
315 continue;
316
317 this_cache[num_cache_leaves].cci = cci;
318 this_cache[num_cache_leaves].level = i + 1;
319 this_cache[num_cache_leaves].type = j;
320
321 cache_shared_cpu_map_setup(cpu,
322 &this_cache[num_cache_leaves]);
323 num_cache_leaves ++;
324 }
325 }
326
327 all_cpu_cache_info[cpu].cache_leaves = this_cache;
328 all_cpu_cache_info[cpu].num_cache_leaves = num_cache_leaves;
329
330 memset(&all_cpu_cache_info[cpu].kobj, 0, sizeof(struct kobject));
331
332 return 0;
333}
334
335/* Add cache interface for CPU device */
336static int __cpuinit cache_add_dev(struct sys_device * sys_dev)
337{
338 unsigned int cpu = sys_dev->id;
339 unsigned long i, j;
340 struct cache_info *this_object;
341 int retval = 0;
342 cpumask_t oldmask;
343
344 if (all_cpu_cache_info[cpu].kobj.parent)
345 return 0;
346
347 oldmask = current->cpus_allowed;
348 retval = set_cpus_allowed(current, cpumask_of_cpu(cpu));
349 if (unlikely(retval))
350 return retval;
351
352 retval = cpu_cache_sysfs_init(cpu);
353 set_cpus_allowed(current, oldmask);
354 if (unlikely(retval < 0))
355 return retval;
356
Greg Kroah-Hartmanc1997902007-12-17 15:54:39 -0400357 retval = kobject_init_and_add(&all_cpu_cache_info[cpu].kobj,
358 &cache_ktype_percpu_entry, &sys_dev->kobj,
359 "%s", "cache");
Zhang, Yanminf1918002006-02-27 11:37:45 +0800360
361 for (i = 0; i < all_cpu_cache_info[cpu].num_cache_leaves; i++) {
362 this_object = LEAF_KOBJECT_PTR(cpu,i);
Greg Kroah-Hartmanc1997902007-12-17 15:54:39 -0400363 retval = kobject_init_and_add(&(this_object->kobj),
364 &cache_ktype,
365 &all_cpu_cache_info[cpu].kobj,
366 "index%1lu", i);
Zhang, Yanminf1918002006-02-27 11:37:45 +0800367 if (unlikely(retval)) {
368 for (j = 0; j < i; j++) {
Greg Kroah-Hartman38a382a2007-12-20 08:13:05 -0800369 kobject_put(&(LEAF_KOBJECT_PTR(cpu,j)->kobj));
Zhang, Yanminf1918002006-02-27 11:37:45 +0800370 }
Greg Kroah-Hartman38a382a2007-12-20 08:13:05 -0800371 kobject_put(&all_cpu_cache_info[cpu].kobj);
Zhang, Yanminf1918002006-02-27 11:37:45 +0800372 cpu_cache_sysfs_exit(cpu);
373 break;
374 }
Greg Kroah-Hartmanc1997902007-12-17 15:54:39 -0400375 kobject_uevent(&(this_object->kobj), KOBJ_ADD);
Zhang, Yanminf1918002006-02-27 11:37:45 +0800376 }
Greg Kroah-Hartmanc1997902007-12-17 15:54:39 -0400377 kobject_uevent(&all_cpu_cache_info[cpu].kobj, KOBJ_ADD);
Zhang, Yanminf1918002006-02-27 11:37:45 +0800378 return retval;
379}
380
381/* Remove cache interface for CPU device */
382static int __cpuinit cache_remove_dev(struct sys_device * sys_dev)
383{
384 unsigned int cpu = sys_dev->id;
385 unsigned long i;
386
387 for (i = 0; i < all_cpu_cache_info[cpu].num_cache_leaves; i++)
Greg Kroah-Hartman38a382a2007-12-20 08:13:05 -0800388 kobject_put(&(LEAF_KOBJECT_PTR(cpu,i)->kobj));
Zhang, Yanminf1918002006-02-27 11:37:45 +0800389
390 if (all_cpu_cache_info[cpu].kobj.parent) {
Greg Kroah-Hartman38a382a2007-12-20 08:13:05 -0800391 kobject_put(&all_cpu_cache_info[cpu].kobj);
Zhang, Yanminf1918002006-02-27 11:37:45 +0800392 memset(&all_cpu_cache_info[cpu].kobj,
393 0,
394 sizeof(struct kobject));
395 }
396
397 cpu_cache_sysfs_exit(cpu);
398
399 return 0;
400}
401
402/*
403 * When a cpu is hot-plugged, do a check and initiate
404 * cache kobject if necessary
405 */
Chandra Seetharaman9c7b2162006-06-27 02:54:07 -0700406static int __cpuinit cache_cpu_callback(struct notifier_block *nfb,
Zhang, Yanminf1918002006-02-27 11:37:45 +0800407 unsigned long action, void *hcpu)
408{
409 unsigned int cpu = (unsigned long)hcpu;
410 struct sys_device *sys_dev;
411
412 sys_dev = get_cpu_sysdev(cpu);
413 switch (action) {
414 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700415 case CPU_ONLINE_FROZEN:
Zhang, Yanminf1918002006-02-27 11:37:45 +0800416 cache_add_dev(sys_dev);
417 break;
418 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700419 case CPU_DEAD_FROZEN:
Zhang, Yanminf1918002006-02-27 11:37:45 +0800420 cache_remove_dev(sys_dev);
421 break;
422 }
423 return NOTIFY_OK;
424}
425
Chandra Seetharaman74b85f32006-06-27 02:54:09 -0700426static struct notifier_block __cpuinitdata cache_cpu_notifier =
Zhang, Yanminf1918002006-02-27 11:37:45 +0800427{
428 .notifier_call = cache_cpu_callback
429};
430
Satyam Sharmadb6a5ce2007-10-02 13:39:45 -0700431static int __init cache_sysfs_init(void)
Zhang, Yanminf1918002006-02-27 11:37:45 +0800432{
433 int i;
434
435 for_each_online_cpu(i) {
Satyam Sharmadb6a5ce2007-10-02 13:39:45 -0700436 struct sys_device *sys_dev = get_cpu_sysdev((unsigned int)i);
437 cache_add_dev(sys_dev);
Zhang, Yanminf1918002006-02-27 11:37:45 +0800438 }
439
Chandra Seetharamanbe6b5a32006-07-30 03:03:37 -0700440 register_hotcpu_notifier(&cache_cpu_notifier);
Zhang, Yanminf1918002006-02-27 11:37:45 +0800441
442 return 0;
443}
444
445device_initcall(cache_sysfs_init);
446