]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - arch/ia64/kernel/topology.c
Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux...
[mirror_ubuntu-zesty-kernel.git] / arch / ia64 / kernel / topology.c
1 /*
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
12 * 02/27/2006 Zhang, Yanmin
13 * Populate cpu cache entries in sysfs for cpu cache info
14 */
15
16 #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>
23 #include <linux/notifier.h>
24 #include <asm/mmzone.h>
25 #include <asm/numa.h>
26 #include <asm/cpu.h>
27
28 static struct ia64_cpu *sysfs_cpus;
29
30 void 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 }
37 EXPORT_SYMBOL_GPL(arch_fix_phys_package_id);
38
39 int arch_register_cpu(int num)
40 {
41 #if defined (CONFIG_ACPI) && defined (CONFIG_HOTPLUG_CPU)
42 /*
43 * If CPEI can be re-targetted or if this is not
44 * CPEI target, then it is hotpluggable
45 */
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);
49 #endif
50
51 return register_cpu(&sysfs_cpus[num].cpu, num);
52 }
53
54 #ifdef CONFIG_HOTPLUG_CPU
55
56 void arch_unregister_cpu(int num)
57 {
58 unregister_cpu(&sysfs_cpus[num].cpu);
59 unmap_cpu_from_node(num, cpu_to_node(num));
60 }
61 EXPORT_SYMBOL(arch_register_cpu);
62 EXPORT_SYMBOL(arch_unregister_cpu);
63 #endif /*CONFIG_HOTPLUG_CPU*/
64
65
66 static int __init topology_init(void)
67 {
68 int i, err = 0;
69
70 #ifdef CONFIG_NUMA
71 /*
72 * MCD - Do we want to register all ONLINE nodes, or all POSSIBLE nodes?
73 */
74 for_each_online_node(i) {
75 if ((err = register_one_node(i)))
76 goto out;
77 }
78 #endif
79
80 sysfs_cpus = kzalloc(sizeof(struct ia64_cpu) * NR_CPUS, GFP_KERNEL);
81 if (!sysfs_cpus)
82 panic("kzalloc in topology_init failed - NR_CPUS too big?");
83
84 for_each_present_cpu(i) {
85 if((err = arch_register_cpu(i)))
86 goto out;
87 }
88 out:
89 return err;
90 }
91
92 subsys_initcall(topology_init);
93
94
95 /*
96 * Export cpu cache information through sysfs
97 */
98
99 /*
100 * A bunch of string array to get pretty printing
101 */
102 static const char *cache_types[] = {
103 "", /* not used */
104 "Instruction",
105 "Data",
106 "Unified" /* unified */
107 };
108
109 static const char *cache_mattrib[]={
110 "WriteThrough",
111 "WriteBack",
112 "", /* reserved */
113 "" /* reserved */
114 };
115
116 struct 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
124 struct cpu_cache_info {
125 struct cache_info *cache_leaves;
126 int num_cache_leaves;
127 struct kobject kobj;
128 };
129
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])
132
133 #ifdef CONFIG_SMP
134 static void __cpuinit cache_shared_cpu_map_setup( unsigned int cpu,
135 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 {
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);
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
169 static void __cpuinit cache_shared_cpu_map_setup(unsigned int cpu,
170 struct cache_info * this_leaf)
171 {
172 cpu_set(cpu, this_leaf->shared_cpu_map);
173 return;
174 }
175 #endif
176
177 static 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
183 static 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
189 static 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
196 static 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
201 static 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
210 static 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
221 static 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
227 static ssize_t show_level(struct cache_info *this_leaf, char *buf)
228 {
229 return sprintf(buf, "%u\n", this_leaf->level);
230 }
231
232 struct 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
245 define_one_ro(level);
246 define_one_ro(type);
247 define_one_ro(coherency_line_size);
248 define_one_ro(ways_of_associativity);
249 define_one_ro(size);
250 define_one_ro(number_of_sets);
251 define_one_ro(shared_cpu_map);
252 define_one_ro(attributes);
253
254 static 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
269 static 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
279 static struct sysfs_ops cache_sysfs_ops = {
280 .show = cache_show
281 };
282
283 static struct kobj_type cache_ktype = {
284 .sysfs_ops = &cache_sysfs_ops,
285 .default_attrs = cache_default_attrs,
286 };
287
288 static struct kobj_type cache_ktype_percpu_entry = {
289 .sysfs_ops = &cache_sysfs_ops,
290 };
291
292 static void __cpuinit cpu_cache_sysfs_exit(unsigned int cpu)
293 {
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));
298 return;
299 }
300
301 static 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 */
345 static 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
366 retval = kobject_init_and_add(&all_cpu_cache_info[cpu].kobj,
367 &cache_ktype_percpu_entry, &sys_dev->kobj,
368 "%s", "cache");
369
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),
373 &cache_ktype,
374 &all_cpu_cache_info[cpu].kobj,
375 "index%1lu", i);
376 if (unlikely(retval)) {
377 for (j = 0; j < i; j++) {
378 kobject_put(&(LEAF_KOBJECT_PTR(cpu,j)->kobj));
379 }
380 kobject_put(&all_cpu_cache_info[cpu].kobj);
381 cpu_cache_sysfs_exit(cpu);
382 break;
383 }
384 kobject_uevent(&(this_object->kobj), KOBJ_ADD);
385 }
386 kobject_uevent(&all_cpu_cache_info[cpu].kobj, KOBJ_ADD);
387 return retval;
388 }
389
390 /* Remove cache interface for CPU device */
391 static 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++)
397 kobject_put(&(LEAF_KOBJECT_PTR(cpu,i)->kobj));
398
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,
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 */
415 static int __cpuinit cache_cpu_callback(struct notifier_block *nfb,
416 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:
424 case CPU_ONLINE_FROZEN:
425 cache_add_dev(sys_dev);
426 break;
427 case CPU_DEAD:
428 case CPU_DEAD_FROZEN:
429 cache_remove_dev(sys_dev);
430 break;
431 }
432 return NOTIFY_OK;
433 }
434
435 static struct notifier_block __cpuinitdata cache_cpu_notifier =
436 {
437 .notifier_call = cache_cpu_callback
438 };
439
440 static int __init cache_sysfs_init(void)
441 {
442 int i;
443
444 for_each_online_cpu(i) {
445 struct sys_device *sys_dev = get_cpu_sysdev((unsigned int)i);
446 cache_add_dev(sys_dev);
447 }
448
449 register_hotcpu_notifier(&cache_cpu_notifier);
450
451 return 0;
452 }
453
454 device_initcall(cache_sysfs_init);
455