blob: e885034a6b73a865c049fa5ca62ca5dab3a88fc1 [file] [log] [blame]
David S. Millereff34142007-06-02 14:41:44 -07001/* sysfs.c: Toplogy sysfs support code for sparc64.
2 *
3 * Copyright (C) 2007 David S. Miller <davem@davemloft.net>
4 */
5#include <linux/sysdev.h>
6#include <linux/cpu.h>
7#include <linux/smp.h>
8#include <linux/percpu.h>
9#include <linux/init.h>
10
David Millerd887ab32007-06-03 23:38:09 -070011#include <asm/hypervisor.h>
12#include <asm/spitfire.h>
David S. Millereff34142007-06-02 14:41:44 -070013
David Millerd887ab32007-06-03 23:38:09 -070014static DEFINE_PER_CPU(struct hv_mmu_statistics, mmu_stats) __attribute__((aligned(64)));
15
16#define SHOW_MMUSTAT_ULONG(NAME) \
David S. Millerd1f253e2007-06-02 20:46:36 -070017static ssize_t show_##NAME(struct sys_device *dev, char *buf) \
18{ \
David Millerd887ab32007-06-03 23:38:09 -070019 struct hv_mmu_statistics *p = &per_cpu(mmu_stats, dev->id); \
20 return sprintf(buf, "%lu\n", p->NAME); \
21} \
22static SYSDEV_ATTR(NAME, 0444, show_##NAME, NULL)
23
24SHOW_MMUSTAT_ULONG(immu_tsb_hits_ctx0_8k_tte);
25SHOW_MMUSTAT_ULONG(immu_tsb_ticks_ctx0_8k_tte);
26SHOW_MMUSTAT_ULONG(immu_tsb_hits_ctx0_64k_tte);
27SHOW_MMUSTAT_ULONG(immu_tsb_ticks_ctx0_64k_tte);
28SHOW_MMUSTAT_ULONG(immu_tsb_hits_ctx0_4mb_tte);
29SHOW_MMUSTAT_ULONG(immu_tsb_ticks_ctx0_4mb_tte);
30SHOW_MMUSTAT_ULONG(immu_tsb_hits_ctx0_256mb_tte);
31SHOW_MMUSTAT_ULONG(immu_tsb_ticks_ctx0_256mb_tte);
32SHOW_MMUSTAT_ULONG(immu_tsb_hits_ctxnon0_8k_tte);
33SHOW_MMUSTAT_ULONG(immu_tsb_ticks_ctxnon0_8k_tte);
34SHOW_MMUSTAT_ULONG(immu_tsb_hits_ctxnon0_64k_tte);
35SHOW_MMUSTAT_ULONG(immu_tsb_ticks_ctxnon0_64k_tte);
36SHOW_MMUSTAT_ULONG(immu_tsb_hits_ctxnon0_4mb_tte);
37SHOW_MMUSTAT_ULONG(immu_tsb_ticks_ctxnon0_4mb_tte);
38SHOW_MMUSTAT_ULONG(immu_tsb_hits_ctxnon0_256mb_tte);
39SHOW_MMUSTAT_ULONG(immu_tsb_ticks_ctxnon0_256mb_tte);
40SHOW_MMUSTAT_ULONG(dmmu_tsb_hits_ctx0_8k_tte);
41SHOW_MMUSTAT_ULONG(dmmu_tsb_ticks_ctx0_8k_tte);
42SHOW_MMUSTAT_ULONG(dmmu_tsb_hits_ctx0_64k_tte);
43SHOW_MMUSTAT_ULONG(dmmu_tsb_ticks_ctx0_64k_tte);
44SHOW_MMUSTAT_ULONG(dmmu_tsb_hits_ctx0_4mb_tte);
45SHOW_MMUSTAT_ULONG(dmmu_tsb_ticks_ctx0_4mb_tte);
46SHOW_MMUSTAT_ULONG(dmmu_tsb_hits_ctx0_256mb_tte);
47SHOW_MMUSTAT_ULONG(dmmu_tsb_ticks_ctx0_256mb_tte);
48SHOW_MMUSTAT_ULONG(dmmu_tsb_hits_ctxnon0_8k_tte);
49SHOW_MMUSTAT_ULONG(dmmu_tsb_ticks_ctxnon0_8k_tte);
50SHOW_MMUSTAT_ULONG(dmmu_tsb_hits_ctxnon0_64k_tte);
51SHOW_MMUSTAT_ULONG(dmmu_tsb_ticks_ctxnon0_64k_tte);
52SHOW_MMUSTAT_ULONG(dmmu_tsb_hits_ctxnon0_4mb_tte);
53SHOW_MMUSTAT_ULONG(dmmu_tsb_ticks_ctxnon0_4mb_tte);
54SHOW_MMUSTAT_ULONG(dmmu_tsb_hits_ctxnon0_256mb_tte);
55SHOW_MMUSTAT_ULONG(dmmu_tsb_ticks_ctxnon0_256mb_tte);
56
57static struct attribute *mmu_stat_attrs[] = {
58 &attr_immu_tsb_hits_ctx0_8k_tte.attr,
59 &attr_immu_tsb_ticks_ctx0_8k_tte.attr,
60 &attr_immu_tsb_hits_ctx0_64k_tte.attr,
61 &attr_immu_tsb_ticks_ctx0_64k_tte.attr,
62 &attr_immu_tsb_hits_ctx0_4mb_tte.attr,
63 &attr_immu_tsb_ticks_ctx0_4mb_tte.attr,
64 &attr_immu_tsb_hits_ctx0_256mb_tte.attr,
65 &attr_immu_tsb_ticks_ctx0_256mb_tte.attr,
66 &attr_immu_tsb_hits_ctxnon0_8k_tte.attr,
67 &attr_immu_tsb_ticks_ctxnon0_8k_tte.attr,
68 &attr_immu_tsb_hits_ctxnon0_64k_tte.attr,
69 &attr_immu_tsb_ticks_ctxnon0_64k_tte.attr,
70 &attr_immu_tsb_hits_ctxnon0_4mb_tte.attr,
71 &attr_immu_tsb_ticks_ctxnon0_4mb_tte.attr,
72 &attr_immu_tsb_hits_ctxnon0_256mb_tte.attr,
73 &attr_immu_tsb_ticks_ctxnon0_256mb_tte.attr,
74 &attr_dmmu_tsb_hits_ctx0_8k_tte.attr,
75 &attr_dmmu_tsb_ticks_ctx0_8k_tte.attr,
76 &attr_dmmu_tsb_hits_ctx0_64k_tte.attr,
77 &attr_dmmu_tsb_ticks_ctx0_64k_tte.attr,
78 &attr_dmmu_tsb_hits_ctx0_4mb_tte.attr,
79 &attr_dmmu_tsb_ticks_ctx0_4mb_tte.attr,
80 &attr_dmmu_tsb_hits_ctx0_256mb_tte.attr,
81 &attr_dmmu_tsb_ticks_ctx0_256mb_tte.attr,
82 &attr_dmmu_tsb_hits_ctxnon0_8k_tte.attr,
83 &attr_dmmu_tsb_ticks_ctxnon0_8k_tte.attr,
84 &attr_dmmu_tsb_hits_ctxnon0_64k_tte.attr,
85 &attr_dmmu_tsb_ticks_ctxnon0_64k_tte.attr,
86 &attr_dmmu_tsb_hits_ctxnon0_4mb_tte.attr,
87 &attr_dmmu_tsb_ticks_ctxnon0_4mb_tte.attr,
88 &attr_dmmu_tsb_hits_ctxnon0_256mb_tte.attr,
89 &attr_dmmu_tsb_ticks_ctxnon0_256mb_tte.attr,
90 NULL,
91};
92
93static struct attribute_group mmu_stat_group = {
94 .attrs = mmu_stat_attrs,
95 .name = "mmu_stats",
96};
97
98/* XXX convert to rusty's on_one_cpu */
99static unsigned long run_on_cpu(unsigned long cpu,
100 unsigned long (*func)(unsigned long),
101 unsigned long arg)
102{
103 cpumask_t old_affinity = current->cpus_allowed;
104 unsigned long ret;
105
106 /* should return -EINVAL to userspace */
107 if (set_cpus_allowed(current, cpumask_of_cpu(cpu)))
108 return 0;
109
110 ret = func(arg);
111
112 set_cpus_allowed(current, old_affinity);
113
114 return ret;
115}
116
117static unsigned long read_mmustat_enable(unsigned long junk)
118{
119 unsigned long ra = 0;
120
121 sun4v_mmustat_info(&ra);
122
123 return ra != 0;
124}
125
126static unsigned long write_mmustat_enable(unsigned long val)
127{
128 unsigned long ra, orig_ra;
129
130 if (val)
131 ra = __pa(&per_cpu(mmu_stats, smp_processor_id()));
132 else
133 ra = 0UL;
134
135 return sun4v_mmustat_conf(ra, &orig_ra);
136}
137
138static ssize_t show_mmustat_enable(struct sys_device *s, char *buf)
139{
140 unsigned long val = run_on_cpu(s->id, read_mmustat_enable, 0);
141 return sprintf(buf, "%lx\n", val);
142}
143
144static ssize_t store_mmustat_enable(struct sys_device *s, const char *buf, size_t count)
145{
146 unsigned long val, err;
147 int ret = sscanf(buf, "%ld", &val);
148
149 if (ret != 1)
150 return -EINVAL;
151
152 err = run_on_cpu(s->id, write_mmustat_enable, val);
153 if (err)
154 return -EIO;
155
156 return count;
157}
158
159static SYSDEV_ATTR(mmustat_enable, 0644, show_mmustat_enable, store_mmustat_enable);
160
161static int mmu_stats_supported;
162
163static int register_mmu_stats(struct sys_device *s)
164{
165 if (!mmu_stats_supported)
166 return 0;
167 sysdev_create_file(s, &attr_mmustat_enable);
168 return sysfs_create_group(&s->kobj, &mmu_stat_group);
169}
170
171#ifdef CONFIG_HOTPLUG_CPU
172static void unregister_mmu_stats(struct sys_device *s)
173{
174 if (!mmu_stats_supported)
175 return;
176 sysfs_remove_group(&s->kobj, &mmu_stat_group);
177 sysdev_remove_file(s, &attr_mmustat_enable);
178}
179#endif
180
181#define SHOW_CPUDATA_ULONG_NAME(NAME, MEMBER) \
182static ssize_t show_##NAME(struct sys_device *dev, char *buf) \
183{ \
184 cpuinfo_sparc *c = &cpu_data(dev->id); \
David S. Millerd1f253e2007-06-02 20:46:36 -0700185 return sprintf(buf, "%lu\n", c->MEMBER); \
186}
187
David Millerd887ab32007-06-03 23:38:09 -0700188#define SHOW_CPUDATA_UINT_NAME(NAME, MEMBER) \
David S. Millerd1f253e2007-06-02 20:46:36 -0700189static ssize_t show_##NAME(struct sys_device *dev, char *buf) \
190{ \
David Millerd887ab32007-06-03 23:38:09 -0700191 cpuinfo_sparc *c = &cpu_data(dev->id); \
David S. Millerd1f253e2007-06-02 20:46:36 -0700192 return sprintf(buf, "%u\n", c->MEMBER); \
193}
194
David Millerd887ab32007-06-03 23:38:09 -0700195SHOW_CPUDATA_ULONG_NAME(clock_tick, clock_tick);
David Millerd887ab32007-06-03 23:38:09 -0700196SHOW_CPUDATA_UINT_NAME(l1_dcache_size, dcache_size);
197SHOW_CPUDATA_UINT_NAME(l1_dcache_line_size, dcache_line_size);
198SHOW_CPUDATA_UINT_NAME(l1_icache_size, icache_size);
199SHOW_CPUDATA_UINT_NAME(l1_icache_line_size, icache_line_size);
200SHOW_CPUDATA_UINT_NAME(l2_cache_size, ecache_size);
201SHOW_CPUDATA_UINT_NAME(l2_cache_line_size, ecache_line_size);
David S. Millerd1f253e2007-06-02 20:46:36 -0700202
203static struct sysdev_attribute cpu_core_attrs[] = {
204 _SYSDEV_ATTR(clock_tick, 0444, show_clock_tick, NULL),
David S. Millerd1f253e2007-06-02 20:46:36 -0700205 _SYSDEV_ATTR(l1_dcache_size, 0444, show_l1_dcache_size, NULL),
206 _SYSDEV_ATTR(l1_dcache_line_size, 0444, show_l1_dcache_line_size, NULL),
207 _SYSDEV_ATTR(l1_icache_size, 0444, show_l1_icache_size, NULL),
208 _SYSDEV_ATTR(l1_icache_line_size, 0444, show_l1_icache_line_size, NULL),
209 _SYSDEV_ATTR(l2_cache_size, 0444, show_l2_cache_size, NULL),
210 _SYSDEV_ATTR(l2_cache_line_size, 0444, show_l2_cache_line_size, NULL),
211};
212
David Millerd887ab32007-06-03 23:38:09 -0700213static DEFINE_PER_CPU(struct cpu, cpu_devices);
214
David S. Millerd1f253e2007-06-02 20:46:36 -0700215static void register_cpu_online(unsigned int cpu)
216{
217 struct cpu *c = &per_cpu(cpu_devices, cpu);
218 struct sys_device *s = &c->sysdev;
219 int i;
220
221 for (i = 0; i < ARRAY_SIZE(cpu_core_attrs); i++)
222 sysdev_create_file(s, &cpu_core_attrs[i]);
David Millerd887ab32007-06-03 23:38:09 -0700223
224 register_mmu_stats(s);
David S. Millerd1f253e2007-06-02 20:46:36 -0700225}
226
227#ifdef CONFIG_HOTPLUG_CPU
228static void unregister_cpu_online(unsigned int cpu)
229{
230 struct cpu *c = &per_cpu(cpu_devices, cpu);
231 struct sys_device *s = &c->sysdev;
232 int i;
233
David Millerd887ab32007-06-03 23:38:09 -0700234 unregister_mmu_stats(s);
David S. Millerd1f253e2007-06-02 20:46:36 -0700235 for (i = 0; i < ARRAY_SIZE(cpu_core_attrs); i++)
236 sysdev_remove_file(s, &cpu_core_attrs[i]);
237}
238#endif
239
240static int __cpuinit sysfs_cpu_notify(struct notifier_block *self,
241 unsigned long action, void *hcpu)
242{
243 unsigned int cpu = (unsigned int)(long)hcpu;
244
245 switch (action) {
246 case CPU_ONLINE:
247 case CPU_ONLINE_FROZEN:
248 register_cpu_online(cpu);
249 break;
250#ifdef CONFIG_HOTPLUG_CPU
251 case CPU_DEAD:
252 case CPU_DEAD_FROZEN:
253 unregister_cpu_online(cpu);
254 break;
255#endif
256 }
257 return NOTIFY_OK;
258}
259
260static struct notifier_block __cpuinitdata sysfs_cpu_nb = {
261 .notifier_call = sysfs_cpu_notify,
262};
263
David Millerd887ab32007-06-03 23:38:09 -0700264static void __init check_mmu_stats(void)
265{
266 unsigned long dummy1, err;
267
268 if (tlb_type != hypervisor)
269 return;
270
271 err = sun4v_mmustat_info(&dummy1);
272 if (!err)
273 mmu_stats_supported = 1;
274}
275
David S. Miller919ee672008-04-23 05:40:25 -0700276static void register_nodes(void)
277{
278#ifdef CONFIG_NUMA
279 int i;
280
281 for (i = 0; i < MAX_NUMNODES; i++)
282 register_one_node(i);
283#endif
284}
285
David S. Millereff34142007-06-02 14:41:44 -0700286static int __init topology_init(void)
287{
288 int cpu;
289
David S. Miller919ee672008-04-23 05:40:25 -0700290 register_nodes();
291
David Millerd887ab32007-06-03 23:38:09 -0700292 check_mmu_stats();
293
David S. Millerd1f253e2007-06-02 20:46:36 -0700294 register_cpu_notifier(&sysfs_cpu_nb);
295
David S. Millereff34142007-06-02 14:41:44 -0700296 for_each_possible_cpu(cpu) {
297 struct cpu *c = &per_cpu(cpu_devices, cpu);
298
299 register_cpu(c, cpu);
David S. Millerd1f253e2007-06-02 20:46:36 -0700300 if (cpu_online(cpu))
301 register_cpu_online(cpu);
David S. Millereff34142007-06-02 14:41:44 -0700302 }
303
304 return 0;
305}
306
307subsys_initcall(topology_init);