Commit | Line | Data |
---|---|---|
69dcc991 ZY |
1 | /* |
2 | * driver/base/topology.c - Populate sysfs with cpu topology information | |
3 | * | |
4 | * Written by: Zhang Yanmin, Intel Corporation | |
5 | * | |
6 | * Copyright (C) 2006, Intel Corp. | |
7 | * | |
8 | * All rights reserved. | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, but | |
16 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or | |
18 | * NON INFRINGEMENT. See the GNU General Public License for more | |
19 | * details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
24 | * | |
25 | */ | |
69dcc991 ZY |
26 | #include <linux/init.h> |
27 | #include <linux/mm.h> | |
28 | #include <linux/cpu.h> | |
29 | #include <linux/module.h> | |
b13d3720 | 30 | #include <linux/hardirq.h> |
69dcc991 ZY |
31 | #include <linux/topology.h> |
32 | ||
fbd59a8d | 33 | #define define_one_ro_named(_name, _func) \ |
8a25a2fd | 34 | static DEVICE_ATTR(_name, 0444, _func, NULL) |
fbd59a8d RR |
35 | |
36 | #define define_one_ro(_name) \ | |
8a25a2fd | 37 | static DEVICE_ATTR(_name, 0444, show_##_name, NULL) |
69dcc991 ZY |
38 | |
39 | #define define_id_show_func(name) \ | |
8a25a2fd KS |
40 | static ssize_t show_##name(struct device *dev, \ |
41 | struct device_attribute *attr, char *buf) \ | |
69dcc991 | 42 | { \ |
ed04ce19 | 43 | return sprintf(buf, "%d\n", topology_##name(dev->id)); \ |
69dcc991 | 44 | } |
6fa3eb70 S |
45 | #define define_id_lu_show_func(name) \ |
46 | static ssize_t show_##name(struct device *dev, \ | |
47 | struct device_attribute *attr, char *buf) \ | |
48 | { \ | |
49 | unsigned int cpu = dev->id; \ | |
50 | return sprintf(buf, "%lu\n", topology_##name(cpu)); \ | |
51 | } | |
69dcc991 | 52 | |
b40d8ed4 HC |
53 | #if defined(topology_thread_cpumask) || defined(topology_core_cpumask) || \ |
54 | defined(topology_book_cpumask) | |
fbd59a8d | 55 | static ssize_t show_cpumap(int type, const struct cpumask *mask, char *buf) |
39106dcf MT |
56 | { |
57 | ptrdiff_t len = PTR_ALIGN(buf + PAGE_SIZE - 1, PAGE_SIZE) - buf; | |
58 | int n = 0; | |
59 | ||
60 | if (len > 1) { | |
61 | n = type? | |
29c0177e RR |
62 | cpulist_scnprintf(buf, len-2, mask) : |
63 | cpumask_scnprintf(buf, len-2, mask); | |
39106dcf MT |
64 | buf[n++] = '\n'; |
65 | buf[n] = '\0'; | |
66 | } | |
67 | return n; | |
68 | } | |
23ca4bba | 69 | #endif |
39106dcf | 70 | |
23ca4bba | 71 | #ifdef arch_provides_topology_pointers |
39106dcf | 72 | #define define_siblings_show_map(name) \ |
8a25a2fd KS |
73 | static ssize_t show_##name(struct device *dev, \ |
74 | struct device_attribute *attr, char *buf) \ | |
39106dcf MT |
75 | { \ |
76 | unsigned int cpu = dev->id; \ | |
fbd59a8d | 77 | return show_cpumap(0, topology_##name(cpu), buf); \ |
39106dcf MT |
78 | } |
79 | ||
80 | #define define_siblings_show_list(name) \ | |
8a25a2fd KS |
81 | static ssize_t show_##name##_list(struct device *dev, \ |
82 | struct device_attribute *attr, \ | |
4a0b2b4d | 83 | char *buf) \ |
69dcc991 | 84 | { \ |
69dcc991 | 85 | unsigned int cpu = dev->id; \ |
fbd59a8d | 86 | return show_cpumap(1, topology_##name(cpu), buf); \ |
69dcc991 ZY |
87 | } |
88 | ||
23ca4bba MT |
89 | #else |
90 | #define define_siblings_show_map(name) \ | |
8a25a2fd KS |
91 | static ssize_t show_##name(struct device *dev, \ |
92 | struct device_attribute *attr, char *buf) \ | |
23ca4bba | 93 | { \ |
fbd59a8d | 94 | return show_cpumap(0, topology_##name(dev->id), buf); \ |
23ca4bba MT |
95 | } |
96 | ||
97 | #define define_siblings_show_list(name) \ | |
8a25a2fd KS |
98 | static ssize_t show_##name##_list(struct device *dev, \ |
99 | struct device_attribute *attr, \ | |
4a0b2b4d | 100 | char *buf) \ |
23ca4bba | 101 | { \ |
fbd59a8d | 102 | return show_cpumap(1, topology_##name(dev->id), buf); \ |
23ca4bba MT |
103 | } |
104 | #endif | |
105 | ||
39106dcf MT |
106 | #define define_siblings_show_func(name) \ |
107 | define_siblings_show_map(name); define_siblings_show_list(name) | |
108 | ||
69dcc991 ZY |
109 | define_id_show_func(physical_package_id); |
110 | define_one_ro(physical_package_id); | |
69dcc991 | 111 | |
69dcc991 ZY |
112 | define_id_show_func(core_id); |
113 | define_one_ro(core_id); | |
69dcc991 | 114 | |
fbd59a8d RR |
115 | define_siblings_show_func(thread_cpumask); |
116 | define_one_ro_named(thread_siblings, show_thread_cpumask); | |
117 | define_one_ro_named(thread_siblings_list, show_thread_cpumask_list); | |
69dcc991 | 118 | |
fbd59a8d RR |
119 | define_siblings_show_func(core_cpumask); |
120 | define_one_ro_named(core_siblings, show_core_cpumask); | |
121 | define_one_ro_named(core_siblings_list, show_core_cpumask_list); | |
69dcc991 | 122 | |
b40d8ed4 HC |
123 | #ifdef CONFIG_SCHED_BOOK |
124 | define_id_show_func(book_id); | |
125 | define_one_ro(book_id); | |
126 | define_siblings_show_func(book_cpumask); | |
127 | define_one_ro_named(book_siblings, show_book_cpumask); | |
128 | define_one_ro_named(book_siblings_list, show_book_cpumask_list); | |
129 | #endif | |
130 | ||
6fa3eb70 S |
131 | #ifdef CONFIG_ARCH_SCALE_INVARIANT_CPU_CAPACITY |
132 | define_id_lu_show_func(max_cpu_capacity); | |
133 | define_one_ro(max_cpu_capacity); | |
134 | define_id_lu_show_func(cpu_capacity); | |
135 | define_one_ro(cpu_capacity); | |
136 | #endif | |
137 | ||
69dcc991 | 138 | static struct attribute *default_attrs[] = { |
8a25a2fd KS |
139 | &dev_attr_physical_package_id.attr, |
140 | &dev_attr_core_id.attr, | |
141 | &dev_attr_thread_siblings.attr, | |
142 | &dev_attr_thread_siblings_list.attr, | |
143 | &dev_attr_core_siblings.attr, | |
144 | &dev_attr_core_siblings_list.attr, | |
b40d8ed4 | 145 | #ifdef CONFIG_SCHED_BOOK |
8a25a2fd KS |
146 | &dev_attr_book_id.attr, |
147 | &dev_attr_book_siblings.attr, | |
148 | &dev_attr_book_siblings_list.attr, | |
6fa3eb70 S |
149 | #endif |
150 | #ifdef CONFIG_ARCH_SCALE_INVARIANT_CPU_CAPACITY | |
151 | &dev_attr_max_cpu_capacity.attr, | |
152 | &dev_attr_cpu_capacity.attr, | |
b40d8ed4 | 153 | #endif |
69dcc991 ZY |
154 | NULL |
155 | }; | |
156 | ||
157 | static struct attribute_group topology_attr_group = { | |
158 | .attrs = default_attrs, | |
159 | .name = "topology" | |
160 | }; | |
161 | ||
162 | /* Add/Remove cpu_topology interface for CPU device */ | |
06a4bcae | 163 | static int __cpuinit topology_add_dev(unsigned int cpu) |
69dcc991 | 164 | { |
8a25a2fd | 165 | struct device *dev = get_cpu_device(cpu); |
06a4bcae | 166 | |
8a25a2fd | 167 | return sysfs_create_group(&dev->kobj, &topology_attr_group); |
69dcc991 ZY |
168 | } |
169 | ||
06a4bcae | 170 | static void __cpuinit topology_remove_dev(unsigned int cpu) |
69dcc991 | 171 | { |
8a25a2fd | 172 | struct device *dev = get_cpu_device(cpu); |
06a4bcae | 173 | |
8a25a2fd | 174 | sysfs_remove_group(&dev->kobj, &topology_attr_group); |
69dcc991 ZY |
175 | } |
176 | ||
9c7b216d | 177 | static int __cpuinit topology_cpu_callback(struct notifier_block *nfb, |
06a4bcae | 178 | unsigned long action, void *hcpu) |
69dcc991 ZY |
179 | { |
180 | unsigned int cpu = (unsigned long)hcpu; | |
06a4bcae | 181 | int rc = 0; |
69dcc991 | 182 | |
69dcc991 | 183 | switch (action) { |
06a4bcae | 184 | case CPU_UP_PREPARE: |
8bb78442 | 185 | case CPU_UP_PREPARE_FROZEN: |
06a4bcae | 186 | rc = topology_add_dev(cpu); |
69dcc991 | 187 | break; |
06a4bcae | 188 | case CPU_UP_CANCELED: |
8bb78442 | 189 | case CPU_UP_CANCELED_FROZEN: |
69dcc991 | 190 | case CPU_DEAD: |
8bb78442 | 191 | case CPU_DEAD_FROZEN: |
06a4bcae | 192 | topology_remove_dev(cpu); |
69dcc991 ZY |
193 | break; |
194 | } | |
ad84bb5b | 195 | return notifier_from_errno(rc); |
69dcc991 | 196 | } |
69dcc991 ZY |
197 | |
198 | static int __cpuinit topology_sysfs_init(void) | |
199 | { | |
06a4bcae HC |
200 | int cpu; |
201 | int rc; | |
69dcc991 | 202 | |
06a4bcae HC |
203 | for_each_online_cpu(cpu) { |
204 | rc = topology_add_dev(cpu); | |
205 | if (rc) | |
206 | return rc; | |
69dcc991 | 207 | } |
06a4bcae | 208 | hotcpu_notifier(topology_cpu_callback, 0); |
69dcc991 ZY |
209 | |
210 | return 0; | |
211 | } | |
212 | ||
213 | device_initcall(topology_sysfs_init); |