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
6 * This file contains NUMA specific variables and functions which can
7 * be split away from DISCONTIGMEM and are used on NUMA machines with
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
12 * 02/27/2006 Zhang, Yanmin
13 * Populate cpu cache entries in sysfs for cpu cache info
16 #include <linux/cpu.h>
17 #include <linux/kernel.h>
19 #include <linux/node.h>
20 #include <linux/init.h>
21 #include <linux/bootmem.h>
22 #include <linux/nodemask.h>
23 #include <linux/notifier.h>
24 #include <asm/mmzone.h>
28 static struct ia64_cpu
*sysfs_cpus
;
30 void arch_fix_phys_package_id(int num
, u32 slot
)
33 if (cpu_data(num
)->socket_id
== -1)
34 cpu_data(num
)->socket_id
= slot
;
37 EXPORT_SYMBOL_GPL(arch_fix_phys_package_id
);
39 int arch_register_cpu(int num
)
41 #if defined (CONFIG_ACPI) && defined (CONFIG_HOTPLUG_CPU)
43 * If CPEI can be re-targetted or if this is not
44 * CPEI target, then it is hotpluggable
46 if (can_cpei_retarget() || !is_cpu_cpei_target(num
))
47 sysfs_cpus
[num
].cpu
.hotpluggable
= 1;
48 map_cpu_to_node(num
, node_cpuid
[num
].nid
);
51 return register_cpu(&sysfs_cpus
[num
].cpu
, num
);
54 #ifdef CONFIG_HOTPLUG_CPU
56 void arch_unregister_cpu(int num
)
58 unregister_cpu(&sysfs_cpus
[num
].cpu
);
59 unmap_cpu_from_node(num
, cpu_to_node(num
));
61 EXPORT_SYMBOL(arch_register_cpu
);
62 EXPORT_SYMBOL(arch_unregister_cpu
);
63 #endif /*CONFIG_HOTPLUG_CPU*/
66 static int __init
topology_init(void)
72 * MCD - Do we want to register all ONLINE nodes, or all POSSIBLE nodes?
74 for_each_online_node(i
) {
75 if ((err
= register_one_node(i
)))
80 sysfs_cpus
= kzalloc(sizeof(struct ia64_cpu
) * NR_CPUS
, GFP_KERNEL
);
82 panic("kzalloc in topology_init failed - NR_CPUS too big?");
84 for_each_present_cpu(i
) {
85 if((err
= arch_register_cpu(i
)))
92 subsys_initcall(topology_init
);
96 * Export cpu cache information through sysfs
100 * A bunch of string array to get pretty printing
102 static const char *cache_types
[] = {
106 "Unified" /* unified */
109 static const char *cache_mattrib
[]={
117 pal_cache_config_info_t cci
;
118 cpumask_t shared_cpu_map
;
124 struct cpu_cache_info
{
125 struct cache_info
*cache_leaves
;
126 int num_cache_leaves
;
130 static struct cpu_cache_info all_cpu_cache_info
[NR_CPUS
] __cpuinitdata
;
131 #define LEAF_KOBJECT_PTR(x,y) (&all_cpu_cache_info[x].cache_leaves[y])
134 static void __cpuinit
cache_shared_cpu_map_setup( unsigned int cpu
,
135 struct cache_info
* this_leaf
)
137 pal_cache_shared_info_t csi
;
138 int num_shared
, i
= 0;
141 if (cpu_data(cpu
)->threads_per_core
<= 1 &&
142 cpu_data(cpu
)->cores_per_socket
<= 1) {
143 cpu_set(cpu
, this_leaf
->shared_cpu_map
);
147 if (ia64_pal_cache_shared_info(this_leaf
->level
,
150 &csi
) != PAL_STATUS_SUCCESS
)
153 num_shared
= (int) csi
.num_shared
;
155 for_each_possible_cpu(j
)
156 if (cpu_data(cpu
)->socket_id
== cpu_data(j
)->socket_id
157 && cpu_data(j
)->core_id
== csi
.log1_cid
158 && cpu_data(j
)->thread_id
== csi
.log1_tid
)
159 cpu_set(j
, this_leaf
->shared_cpu_map
);
162 } while (i
< num_shared
&&
163 ia64_pal_cache_shared_info(this_leaf
->level
,
166 &csi
) == PAL_STATUS_SUCCESS
);
169 static void __cpuinit
cache_shared_cpu_map_setup(unsigned int cpu
,
170 struct cache_info
* this_leaf
)
172 cpu_set(cpu
, this_leaf
->shared_cpu_map
);
177 static ssize_t
show_coherency_line_size(struct cache_info
*this_leaf
,
180 return sprintf(buf
, "%u\n", 1 << this_leaf
->cci
.pcci_line_size
);
183 static ssize_t
show_ways_of_associativity(struct cache_info
*this_leaf
,
186 return sprintf(buf
, "%u\n", this_leaf
->cci
.pcci_assoc
);
189 static ssize_t
show_attributes(struct cache_info
*this_leaf
, char *buf
)
193 cache_mattrib
[this_leaf
->cci
.pcci_cache_attr
]);
196 static ssize_t
show_size(struct cache_info
*this_leaf
, char *buf
)
198 return sprintf(buf
, "%uK\n", this_leaf
->cci
.pcci_cache_size
/ 1024);
201 static ssize_t
show_number_of_sets(struct cache_info
*this_leaf
, char *buf
)
203 unsigned number_of_sets
= this_leaf
->cci
.pcci_cache_size
;
204 number_of_sets
/= this_leaf
->cci
.pcci_assoc
;
205 number_of_sets
/= 1 << this_leaf
->cci
.pcci_line_size
;
207 return sprintf(buf
, "%u\n", number_of_sets
);
210 static ssize_t
show_shared_cpu_map(struct cache_info
*this_leaf
, char *buf
)
213 cpumask_t shared_cpu_map
;
215 cpus_and(shared_cpu_map
, this_leaf
->shared_cpu_map
, cpu_online_map
);
216 len
= cpumask_scnprintf(buf
, NR_CPUS
+1, shared_cpu_map
);
217 len
+= sprintf(buf
+len
, "\n");
221 static ssize_t
show_type(struct cache_info
*this_leaf
, char *buf
)
223 int type
= this_leaf
->type
+ this_leaf
->cci
.pcci_unified
;
224 return sprintf(buf
, "%s\n", cache_types
[type
]);
227 static ssize_t
show_level(struct cache_info
*this_leaf
, char *buf
)
229 return sprintf(buf
, "%u\n", this_leaf
->level
);
233 struct attribute attr
;
234 ssize_t (*show
)(struct cache_info
*, char *);
235 ssize_t (*store
)(struct cache_info
*, const char *, size_t count
);
241 #define define_one_ro(_name) \
242 static struct cache_attr _name = \
243 __ATTR(_name, 0444, show_##_name, NULL)
245 define_one_ro(level
);
247 define_one_ro(coherency_line_size
);
248 define_one_ro(ways_of_associativity
);
250 define_one_ro(number_of_sets
);
251 define_one_ro(shared_cpu_map
);
252 define_one_ro(attributes
);
254 static struct attribute
* cache_default_attrs
[] = {
257 &coherency_line_size
.attr
,
258 &ways_of_associativity
.attr
,
261 &number_of_sets
.attr
,
262 &shared_cpu_map
.attr
,
266 #define to_object(k) container_of(k, struct cache_info, kobj)
267 #define to_attr(a) container_of(a, struct cache_attr, attr)
269 static ssize_t
cache_show(struct kobject
* kobj
, struct attribute
* attr
, char * buf
)
271 struct cache_attr
*fattr
= to_attr(attr
);
272 struct cache_info
*this_leaf
= to_object(kobj
);
275 ret
= fattr
->show
? fattr
->show(this_leaf
, buf
) : 0;
279 static struct sysfs_ops cache_sysfs_ops
= {
283 static struct kobj_type cache_ktype
= {
284 .sysfs_ops
= &cache_sysfs_ops
,
285 .default_attrs
= cache_default_attrs
,
288 static struct kobj_type cache_ktype_percpu_entry
= {
289 .sysfs_ops
= &cache_sysfs_ops
,
292 static void __cpuinit
cpu_cache_sysfs_exit(unsigned int cpu
)
294 kfree(all_cpu_cache_info
[cpu
].cache_leaves
);
295 all_cpu_cache_info
[cpu
].cache_leaves
= NULL
;
296 all_cpu_cache_info
[cpu
].num_cache_leaves
= 0;
297 memset(&all_cpu_cache_info
[cpu
].kobj
, 0, sizeof(struct kobject
));
301 static int __cpuinit
cpu_cache_sysfs_init(unsigned int cpu
)
303 u64 i
, levels
, unique_caches
;
304 pal_cache_config_info_t cci
;
307 struct cache_info
*this_cache
;
308 int num_cache_leaves
= 0;
310 if ((status
= ia64_pal_cache_summary(&levels
, &unique_caches
)) != 0) {
311 printk(KERN_ERR
"ia64_pal_cache_summary=%ld\n", status
);
315 this_cache
=kzalloc(sizeof(struct cache_info
)*unique_caches
,
317 if (this_cache
== NULL
)
320 for (i
=0; i
< levels
; i
++) {
321 for (j
=2; j
>0 ; j
--) {
322 if ((status
=ia64_pal_cache_config_info(i
,j
, &cci
)) !=
326 this_cache
[num_cache_leaves
].cci
= cci
;
327 this_cache
[num_cache_leaves
].level
= i
+ 1;
328 this_cache
[num_cache_leaves
].type
= j
;
330 cache_shared_cpu_map_setup(cpu
,
331 &this_cache
[num_cache_leaves
]);
336 all_cpu_cache_info
[cpu
].cache_leaves
= this_cache
;
337 all_cpu_cache_info
[cpu
].num_cache_leaves
= num_cache_leaves
;
339 memset(&all_cpu_cache_info
[cpu
].kobj
, 0, sizeof(struct kobject
));
344 /* Add cache interface for CPU device */
345 static int __cpuinit
cache_add_dev(struct sys_device
* sys_dev
)
347 unsigned int cpu
= sys_dev
->id
;
349 struct cache_info
*this_object
;
353 if (all_cpu_cache_info
[cpu
].kobj
.parent
)
356 oldmask
= current
->cpus_allowed
;
357 retval
= set_cpus_allowed(current
, cpumask_of_cpu(cpu
));
358 if (unlikely(retval
))
361 retval
= cpu_cache_sysfs_init(cpu
);
362 set_cpus_allowed(current
, oldmask
);
363 if (unlikely(retval
< 0))
366 retval
= kobject_init_and_add(&all_cpu_cache_info
[cpu
].kobj
,
367 &cache_ktype_percpu_entry
, &sys_dev
->kobj
,
370 for (i
= 0; i
< all_cpu_cache_info
[cpu
].num_cache_leaves
; i
++) {
371 this_object
= LEAF_KOBJECT_PTR(cpu
,i
);
372 retval
= kobject_init_and_add(&(this_object
->kobj
),
374 &all_cpu_cache_info
[cpu
].kobj
,
376 if (unlikely(retval
)) {
377 for (j
= 0; j
< i
; j
++) {
378 kobject_put(&(LEAF_KOBJECT_PTR(cpu
,j
)->kobj
));
380 kobject_put(&all_cpu_cache_info
[cpu
].kobj
);
381 cpu_cache_sysfs_exit(cpu
);
384 kobject_uevent(&(this_object
->kobj
), KOBJ_ADD
);
386 kobject_uevent(&all_cpu_cache_info
[cpu
].kobj
, KOBJ_ADD
);
390 /* Remove cache interface for CPU device */
391 static int __cpuinit
cache_remove_dev(struct sys_device
* sys_dev
)
393 unsigned int cpu
= sys_dev
->id
;
396 for (i
= 0; i
< all_cpu_cache_info
[cpu
].num_cache_leaves
; i
++)
397 kobject_put(&(LEAF_KOBJECT_PTR(cpu
,i
)->kobj
));
399 if (all_cpu_cache_info
[cpu
].kobj
.parent
) {
400 kobject_put(&all_cpu_cache_info
[cpu
].kobj
);
401 memset(&all_cpu_cache_info
[cpu
].kobj
,
403 sizeof(struct kobject
));
406 cpu_cache_sysfs_exit(cpu
);
412 * When a cpu is hot-plugged, do a check and initiate
413 * cache kobject if necessary
415 static int __cpuinit
cache_cpu_callback(struct notifier_block
*nfb
,
416 unsigned long action
, void *hcpu
)
418 unsigned int cpu
= (unsigned long)hcpu
;
419 struct sys_device
*sys_dev
;
421 sys_dev
= get_cpu_sysdev(cpu
);
424 case CPU_ONLINE_FROZEN
:
425 cache_add_dev(sys_dev
);
428 case CPU_DEAD_FROZEN
:
429 cache_remove_dev(sys_dev
);
435 static struct notifier_block __cpuinitdata cache_cpu_notifier
=
437 .notifier_call
= cache_cpu_callback
440 static int __init
cache_sysfs_init(void)
444 for_each_online_cpu(i
) {
445 struct sys_device
*sys_dev
= get_cpu_sysdev((unsigned int)i
);
446 cache_add_dev(sys_dev
);
449 register_hotcpu_notifier(&cache_cpu_notifier
);
454 device_initcall(cache_sysfs_init
);