blob: abb17a613b172f2feac86f38981c255d226864ac [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
Alex Chiangfe086a72008-04-29 15:05:29 -070030void arch_fix_phys_package_id(int num, u32 slot)
31{
32#ifdef CONFIG_SMP
33 if (cpu_data(num)->socket_id == -1)
34 cpu_data(num)->socket_id = slot;
35#endif
36}
37EXPORT_SYMBOL_GPL(arch_fix_phys_package_id);
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039int arch_register_cpu(int num)
40{
Ashok Rajb88e9262006-01-19 16:18:47 -080041#if defined (CONFIG_ACPI) && defined (CONFIG_HOTPLUG_CPU)
Ashok Raj55e59c52005-03-31 22:51:10 -050042 /*
Siddha, Suresh B72486f12006-12-07 02:14:10 +010043 * If CPEI can be re-targetted or if this is not
44 * CPEI target, then it is hotpluggable
Ashok Raj55e59c52005-03-31 22:51:10 -050045 */
Siddha, Suresh B72486f12006-12-07 02:14:10 +010046 if (can_cpei_retarget() || !is_cpu_cpei_target(num))
47 sysfs_cpus[num].cpu.hotpluggable = 1;
KAMEZAWA Hiroyuki3212fe12006-09-25 16:25:31 -070048 map_cpu_to_node(num, node_cpuid[num].nid);
49#endif
Ashok Raj55e59c52005-03-31 22:51:10 -050050
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -070051 return register_cpu(&sysfs_cpus[num].cpu, num);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
54#ifdef CONFIG_HOTPLUG_CPU
55
56void arch_unregister_cpu(int num)
57{
KAMEZAWA Hiroyuki3212fe12006-09-25 16:25:31 -070058 unregister_cpu(&sysfs_cpus[num].cpu);
59 unmap_cpu_from_node(num, cpu_to_node(num));
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61EXPORT_SYMBOL(arch_register_cpu);
62EXPORT_SYMBOL(arch_unregister_cpu);
63#endif /*CONFIG_HOTPLUG_CPU*/
64
65
66static int __init topology_init(void)
67{
68 int i, err = 0;
69
70#ifdef CONFIG_NUMA
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080071 /*
72 * MCD - Do we want to register all ONLINE nodes, or all POSSIBLE nodes?
73 */
74 for_each_online_node(i) {
Yasunori Goto0fc44152006-06-27 02:53:38 -070075 if ((err = register_one_node(i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 goto out;
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#endif
79
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080080 sysfs_cpus = kzalloc(sizeof(struct ia64_cpu) * NR_CPUS, GFP_KERNEL);
Paul Jacksona8132132006-08-14 22:45:49 -070081 if (!sysfs_cpus)
82 panic("kzalloc in topology_init failed - NR_CPUS too big?");
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080084 for_each_present_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if((err = arch_register_cpu(i)))
86 goto out;
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070088out:
89 return err;
90}
91
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080092subsys_initcall(topology_init);
Zhang, Yanminf1918002006-02-27 11:37:45 +080093
94
95/*
96 * Export cpu cache information through sysfs
97 */
98
99/*
100 * A bunch of string array to get pretty printing
101 */
102static const char *cache_types[] = {
103 "", /* not used */
104 "Instruction",
105 "Data",
106 "Unified" /* unified */
107};
108
109static const char *cache_mattrib[]={
110 "WriteThrough",
111 "WriteBack",
112 "", /* reserved */
113 "" /* reserved */
114};
115
116struct cache_info {
117 pal_cache_config_info_t cci;
118 cpumask_t shared_cpu_map;
119 int level;
120 int type;
121 struct kobject kobj;
122};
123
124struct cpu_cache_info {
125 struct cache_info *cache_leaves;
126 int num_cache_leaves;
127 struct kobject kobj;
128};
129
Satyam Sharmadb6a5ce2007-10-02 13:39:45 -0700130static struct cpu_cache_info all_cpu_cache_info[NR_CPUS] __cpuinitdata;
Zhang, Yanminf1918002006-02-27 11:37:45 +0800131#define LEAF_KOBJECT_PTR(x,y) (&all_cpu_cache_info[x].cache_leaves[y])
132
133#ifdef CONFIG_SMP
Satyam Sharmadb6a5ce2007-10-02 13:39:45 -0700134static void __cpuinit cache_shared_cpu_map_setup( unsigned int cpu,
Zhang, Yanminf1918002006-02-27 11:37:45 +0800135 struct cache_info * this_leaf)
136{
137 pal_cache_shared_info_t csi;
138 int num_shared, i = 0;
139 unsigned int j;
140
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);
144 return;
145 }
146
147 if (ia64_pal_cache_shared_info(this_leaf->level,
148 this_leaf->type,
149 0,
150 &csi) != PAL_STATUS_SUCCESS)
151 return;
152
153 num_shared = (int) csi.num_shared;
154 do {
Andrew Mortonfb1bb342006-06-25 05:46:43 -0700155 for_each_possible_cpu(j)
Zhang, Yanminf1918002006-02-27 11:37:45 +0800156 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);
160
161 i++;
162 } while (i < num_shared &&
163 ia64_pal_cache_shared_info(this_leaf->level,
164 this_leaf->type,
165 i,
166 &csi) == PAL_STATUS_SUCCESS);
167}
168#else
Satyam Sharmadb6a5ce2007-10-02 13:39:45 -0700169static void __cpuinit cache_shared_cpu_map_setup(unsigned int cpu,
Zhang, Yanminf1918002006-02-27 11:37:45 +0800170 struct cache_info * this_leaf)
171{
172 cpu_set(cpu, this_leaf->shared_cpu_map);
173 return;
174}
175#endif
176
177static ssize_t show_coherency_line_size(struct cache_info *this_leaf,
178 char *buf)
179{
180 return sprintf(buf, "%u\n", 1 << this_leaf->cci.pcci_line_size);
181}
182
183static ssize_t show_ways_of_associativity(struct cache_info *this_leaf,
184 char *buf)
185{
186 return sprintf(buf, "%u\n", this_leaf->cci.pcci_assoc);
187}
188
189static ssize_t show_attributes(struct cache_info *this_leaf, char *buf)
190{
191 return sprintf(buf,
192 "%s\n",
193 cache_mattrib[this_leaf->cci.pcci_cache_attr]);
194}
195
196static ssize_t show_size(struct cache_info *this_leaf, char *buf)
197{
198 return sprintf(buf, "%uK\n", this_leaf->cci.pcci_cache_size / 1024);
199}
200
201static ssize_t show_number_of_sets(struct cache_info *this_leaf, char *buf)
202{
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;
206
207 return sprintf(buf, "%u\n", number_of_sets);
208}
209
210static ssize_t show_shared_cpu_map(struct cache_info *this_leaf, char *buf)
211{
212 ssize_t len;
213 cpumask_t shared_cpu_map;
214
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");
218 return len;
219}
220
221static ssize_t show_type(struct cache_info *this_leaf, char *buf)
222{
223 int type = this_leaf->type + this_leaf->cci.pcci_unified;
224 return sprintf(buf, "%s\n", cache_types[type]);
225}
226
227static ssize_t show_level(struct cache_info *this_leaf, char *buf)
228{
229 return sprintf(buf, "%u\n", this_leaf->level);
230}
231
232struct cache_attr {
233 struct attribute attr;
234 ssize_t (*show)(struct cache_info *, char *);
235 ssize_t (*store)(struct cache_info *, const char *, size_t count);
236};
237
238#ifdef define_one_ro
239 #undef define_one_ro
240#endif
241#define define_one_ro(_name) \
242 static struct cache_attr _name = \
243__ATTR(_name, 0444, show_##_name, NULL)
244
245define_one_ro(level);
246define_one_ro(type);
247define_one_ro(coherency_line_size);
248define_one_ro(ways_of_associativity);
249define_one_ro(size);
250define_one_ro(number_of_sets);
251define_one_ro(shared_cpu_map);
252define_one_ro(attributes);
253
254static struct attribute * cache_default_attrs[] = {
255 &type.attr,
256 &level.attr,
257 &coherency_line_size.attr,
258 &ways_of_associativity.attr,
259 &attributes.attr,
260 &size.attr,
261 &number_of_sets.attr,
262 &shared_cpu_map.attr,
263 NULL
264};
265
266#define to_object(k) container_of(k, struct cache_info, kobj)
267#define to_attr(a) container_of(a, struct cache_attr, attr)
268
269static ssize_t cache_show(struct kobject * kobj, struct attribute * attr, char * buf)
270{
271 struct cache_attr *fattr = to_attr(attr);
272 struct cache_info *this_leaf = to_object(kobj);
273 ssize_t ret;
274
275 ret = fattr->show ? fattr->show(this_leaf, buf) : 0;
276 return ret;
277}
278
279static struct sysfs_ops cache_sysfs_ops = {
280 .show = cache_show
281};
282
283static struct kobj_type cache_ktype = {
284 .sysfs_ops = &cache_sysfs_ops,
285 .default_attrs = cache_default_attrs,
286};
287
288static struct kobj_type cache_ktype_percpu_entry = {
289 .sysfs_ops = &cache_sysfs_ops,
290};
291
292static void __cpuinit cpu_cache_sysfs_exit(unsigned int cpu)
293{
Jesper Juhlcbf283c2006-04-20 10:11:09 -0700294 kfree(all_cpu_cache_info[cpu].cache_leaves);
295 all_cpu_cache_info[cpu].cache_leaves = NULL;
Zhang, Yanminf1918002006-02-27 11:37:45 +0800296 all_cpu_cache_info[cpu].num_cache_leaves = 0;
297 memset(&all_cpu_cache_info[cpu].kobj, 0, sizeof(struct kobject));
Zhang, Yanminf1918002006-02-27 11:37:45 +0800298 return;
299}
300
301static int __cpuinit cpu_cache_sysfs_init(unsigned int cpu)
302{
303 u64 i, levels, unique_caches;
304 pal_cache_config_info_t cci;
305 int j;
306 s64 status;
307 struct cache_info *this_cache;
308 int num_cache_leaves = 0;
309
310 if ((status = ia64_pal_cache_summary(&levels, &unique_caches)) != 0) {
311 printk(KERN_ERR "ia64_pal_cache_summary=%ld\n", status);
312 return -1;
313 }
314
315 this_cache=kzalloc(sizeof(struct cache_info)*unique_caches,
316 GFP_KERNEL);
317 if (this_cache == NULL)
318 return -ENOMEM;
319
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)) !=
323 PAL_STATUS_SUCCESS)
324 continue;
325
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;
329
330 cache_shared_cpu_map_setup(cpu,
331 &this_cache[num_cache_leaves]);
332 num_cache_leaves ++;
333 }
334 }
335
336 all_cpu_cache_info[cpu].cache_leaves = this_cache;
337 all_cpu_cache_info[cpu].num_cache_leaves = num_cache_leaves;
338
339 memset(&all_cpu_cache_info[cpu].kobj, 0, sizeof(struct kobject));
340
341 return 0;
342}
343
344/* Add cache interface for CPU device */
345static int __cpuinit cache_add_dev(struct sys_device * sys_dev)
346{
347 unsigned int cpu = sys_dev->id;
348 unsigned long i, j;
349 struct cache_info *this_object;
350 int retval = 0;
351 cpumask_t oldmask;
352
353 if (all_cpu_cache_info[cpu].kobj.parent)
354 return 0;
355
356 oldmask = current->cpus_allowed;
357 retval = set_cpus_allowed(current, cpumask_of_cpu(cpu));
358 if (unlikely(retval))
359 return retval;
360
361 retval = cpu_cache_sysfs_init(cpu);
362 set_cpus_allowed(current, oldmask);
363 if (unlikely(retval < 0))
364 return retval;
365
Greg Kroah-Hartmanc1997902007-12-17 15:54:39 -0400366 retval = kobject_init_and_add(&all_cpu_cache_info[cpu].kobj,
367 &cache_ktype_percpu_entry, &sys_dev->kobj,
368 "%s", "cache");
Zhang, Yanminf1918002006-02-27 11:37:45 +0800369
370 for (i = 0; i < all_cpu_cache_info[cpu].num_cache_leaves; i++) {
371 this_object = LEAF_KOBJECT_PTR(cpu,i);
Greg Kroah-Hartmanc1997902007-12-17 15:54:39 -0400372 retval = kobject_init_and_add(&(this_object->kobj),
373 &cache_ktype,
374 &all_cpu_cache_info[cpu].kobj,
375 "index%1lu", i);
Zhang, Yanminf1918002006-02-27 11:37:45 +0800376 if (unlikely(retval)) {
377 for (j = 0; j < i; j++) {
Greg Kroah-Hartman38a382a2007-12-20 08:13:05 -0800378 kobject_put(&(LEAF_KOBJECT_PTR(cpu,j)->kobj));
Zhang, Yanminf1918002006-02-27 11:37:45 +0800379 }
Greg Kroah-Hartman38a382a2007-12-20 08:13:05 -0800380 kobject_put(&all_cpu_cache_info[cpu].kobj);
Zhang, Yanminf1918002006-02-27 11:37:45 +0800381 cpu_cache_sysfs_exit(cpu);
382 break;
383 }
Greg Kroah-Hartmanc1997902007-12-17 15:54:39 -0400384 kobject_uevent(&(this_object->kobj), KOBJ_ADD);
Zhang, Yanminf1918002006-02-27 11:37:45 +0800385 }
Greg Kroah-Hartmanc1997902007-12-17 15:54:39 -0400386 kobject_uevent(&all_cpu_cache_info[cpu].kobj, KOBJ_ADD);
Zhang, Yanminf1918002006-02-27 11:37:45 +0800387 return retval;
388}
389
390/* Remove cache interface for CPU device */
391static int __cpuinit cache_remove_dev(struct sys_device * sys_dev)
392{
393 unsigned int cpu = sys_dev->id;
394 unsigned long i;
395
396 for (i = 0; i < all_cpu_cache_info[cpu].num_cache_leaves; i++)
Greg Kroah-Hartman38a382a2007-12-20 08:13:05 -0800397 kobject_put(&(LEAF_KOBJECT_PTR(cpu,i)->kobj));
Zhang, Yanminf1918002006-02-27 11:37:45 +0800398
399 if (all_cpu_cache_info[cpu].kobj.parent) {
Greg Kroah-Hartman38a382a2007-12-20 08:13:05 -0800400 kobject_put(&all_cpu_cache_info[cpu].kobj);
Zhang, Yanminf1918002006-02-27 11:37:45 +0800401 memset(&all_cpu_cache_info[cpu].kobj,
402 0,
403 sizeof(struct kobject));
404 }
405
406 cpu_cache_sysfs_exit(cpu);
407
408 return 0;
409}
410
411/*
412 * When a cpu is hot-plugged, do a check and initiate
413 * cache kobject if necessary
414 */
Chandra Seetharaman9c7b2162006-06-27 02:54:07 -0700415static int __cpuinit cache_cpu_callback(struct notifier_block *nfb,
Zhang, Yanminf1918002006-02-27 11:37:45 +0800416 unsigned long action, void *hcpu)
417{
418 unsigned int cpu = (unsigned long)hcpu;
419 struct sys_device *sys_dev;
420
421 sys_dev = get_cpu_sysdev(cpu);
422 switch (action) {
423 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700424 case CPU_ONLINE_FROZEN:
Zhang, Yanminf1918002006-02-27 11:37:45 +0800425 cache_add_dev(sys_dev);
426 break;
427 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700428 case CPU_DEAD_FROZEN:
Zhang, Yanminf1918002006-02-27 11:37:45 +0800429 cache_remove_dev(sys_dev);
430 break;
431 }
432 return NOTIFY_OK;
433}
434
Chandra Seetharaman74b85f32006-06-27 02:54:09 -0700435static struct notifier_block __cpuinitdata cache_cpu_notifier =
Zhang, Yanminf1918002006-02-27 11:37:45 +0800436{
437 .notifier_call = cache_cpu_callback
438};
439
Satyam Sharmadb6a5ce2007-10-02 13:39:45 -0700440static int __init cache_sysfs_init(void)
Zhang, Yanminf1918002006-02-27 11:37:45 +0800441{
442 int i;
443
444 for_each_online_cpu(i) {
Satyam Sharmadb6a5ce2007-10-02 13:39:45 -0700445 struct sys_device *sys_dev = get_cpu_sysdev((unsigned int)i);
446 cache_add_dev(sys_dev);
Zhang, Yanminf1918002006-02-27 11:37:45 +0800447 }
448
Chandra Seetharamanbe6b5a32006-07-30 03:03:37 -0700449 register_hotcpu_notifier(&cache_cpu_notifier);
Zhang, Yanminf1918002006-02-27 11:37:45 +0800450
451 return 0;
452}
453
454device_initcall(cache_sysfs_init);
455