| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #include <linux/config.h> | 
 | 2 | #include <linux/sysdev.h> | 
 | 3 | #include <linux/cpu.h> | 
 | 4 | #include <linux/smp.h> | 
 | 5 | #include <linux/percpu.h> | 
 | 6 | #include <linux/init.h> | 
 | 7 | #include <linux/sched.h> | 
 | 8 | #include <linux/module.h> | 
 | 9 | #include <linux/nodemask.h> | 
 | 10 | #include <linux/cpumask.h> | 
 | 11 | #include <linux/notifier.h> | 
 | 12 |  | 
 | 13 | #include <asm/current.h> | 
 | 14 | #include <asm/processor.h> | 
 | 15 | #include <asm/cputable.h> | 
| Stephen Rothwell | 1ababe11 | 2005-08-03 14:35:25 +1000 | [diff] [blame] | 16 | #include <asm/firmware.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <asm/hvcall.h> | 
 | 18 | #include <asm/prom.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/paca.h> | 
 | 20 | #include <asm/lppaca.h> | 
 | 21 | #include <asm/machdep.h> | 
| Paul Mackerras | 2249ca9 | 2005-11-07 13:18:13 +1100 | [diff] [blame] | 22 | #include <asm/smp.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 |  | 
 | 24 | static DEFINE_PER_CPU(struct cpu, cpu_devices); | 
 | 25 |  | 
 | 26 | /* SMT stuff */ | 
 | 27 |  | 
 | 28 | #ifdef CONFIG_PPC_MULTIPLATFORM | 
 | 29 | /* default to snooze disabled */ | 
 | 30 | DEFINE_PER_CPU(unsigned long, smt_snooze_delay); | 
 | 31 |  | 
 | 32 | static ssize_t store_smt_snooze_delay(struct sys_device *dev, const char *buf, | 
 | 33 | 				      size_t count) | 
 | 34 | { | 
 | 35 | 	struct cpu *cpu = container_of(dev, struct cpu, sysdev); | 
 | 36 | 	ssize_t ret; | 
 | 37 | 	unsigned long snooze; | 
 | 38 |  | 
 | 39 | 	ret = sscanf(buf, "%lu", &snooze); | 
 | 40 | 	if (ret != 1) | 
 | 41 | 		return -EINVAL; | 
 | 42 |  | 
 | 43 | 	per_cpu(smt_snooze_delay, cpu->sysdev.id) = snooze; | 
 | 44 |  | 
 | 45 | 	return count; | 
 | 46 | } | 
 | 47 |  | 
 | 48 | static ssize_t show_smt_snooze_delay(struct sys_device *dev, char *buf) | 
 | 49 | { | 
 | 50 | 	struct cpu *cpu = container_of(dev, struct cpu, sysdev); | 
 | 51 |  | 
 | 52 | 	return sprintf(buf, "%lu\n", per_cpu(smt_snooze_delay, cpu->sysdev.id)); | 
 | 53 | } | 
 | 54 |  | 
 | 55 | static SYSDEV_ATTR(smt_snooze_delay, 0644, show_smt_snooze_delay, | 
 | 56 | 		   store_smt_snooze_delay); | 
 | 57 |  | 
 | 58 | /* Only parse OF options if the matching cmdline option was not specified */ | 
 | 59 | static int smt_snooze_cmdline; | 
 | 60 |  | 
 | 61 | static int __init smt_setup(void) | 
 | 62 | { | 
 | 63 | 	struct device_node *options; | 
 | 64 | 	unsigned int *val; | 
 | 65 | 	unsigned int cpu; | 
 | 66 |  | 
 | 67 | 	if (!cpu_has_feature(CPU_FTR_SMT)) | 
 | 68 | 		return 1; | 
 | 69 |  | 
 | 70 | 	options = find_path_device("/options"); | 
 | 71 | 	if (!options) | 
 | 72 | 		return 1; | 
 | 73 |  | 
 | 74 | 	val = (unsigned int *)get_property(options, "ibm,smt-snooze-delay", | 
 | 75 | 					   NULL); | 
 | 76 | 	if (!smt_snooze_cmdline && val) { | 
 | 77 | 		for_each_cpu(cpu) | 
 | 78 | 			per_cpu(smt_snooze_delay, cpu) = *val; | 
 | 79 | 	} | 
 | 80 |  | 
 | 81 | 	return 1; | 
 | 82 | } | 
 | 83 | __initcall(smt_setup); | 
 | 84 |  | 
 | 85 | static int __init setup_smt_snooze_delay(char *str) | 
 | 86 | { | 
 | 87 | 	unsigned int cpu; | 
 | 88 | 	int snooze; | 
 | 89 |  | 
 | 90 | 	if (!cpu_has_feature(CPU_FTR_SMT)) | 
 | 91 | 		return 1; | 
 | 92 |  | 
 | 93 | 	smt_snooze_cmdline = 1; | 
 | 94 |  | 
 | 95 | 	if (get_option(&str, &snooze)) { | 
 | 96 | 		for_each_cpu(cpu) | 
 | 97 | 			per_cpu(smt_snooze_delay, cpu) = snooze; | 
 | 98 | 	} | 
 | 99 |  | 
 | 100 | 	return 1; | 
 | 101 | } | 
 | 102 | __setup("smt-snooze-delay=", setup_smt_snooze_delay); | 
 | 103 |  | 
| Michael Ellerman | 180a336 | 2005-08-09 11:13:36 +1000 | [diff] [blame] | 104 | #endif /* CONFIG_PPC_MULTIPLATFORM */ | 
 | 105 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | /* | 
 | 107 |  * Enabling PMCs will slow partition context switch times so we only do | 
 | 108 |  * it the first time we write to the PMCs. | 
 | 109 |  */ | 
 | 110 |  | 
 | 111 | static DEFINE_PER_CPU(char, pmcs_enabled); | 
 | 112 |  | 
 | 113 | void ppc64_enable_pmcs(void) | 
 | 114 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | 	/* Only need to enable them once */ | 
 | 116 | 	if (__get_cpu_var(pmcs_enabled)) | 
 | 117 | 		return; | 
 | 118 |  | 
 | 119 | 	__get_cpu_var(pmcs_enabled) = 1; | 
 | 120 |  | 
| Michael Ellerman | 180a336 | 2005-08-09 11:13:36 +1000 | [diff] [blame] | 121 | 	if (ppc_md.enable_pmcs) | 
 | 122 | 		ppc_md.enable_pmcs(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | EXPORT_SYMBOL(ppc64_enable_pmcs); | 
 | 125 |  | 
 | 126 | /* XXX convert to rusty's on_one_cpu */ | 
 | 127 | static unsigned long run_on_cpu(unsigned long cpu, | 
 | 128 | 			        unsigned long (*func)(unsigned long), | 
 | 129 | 				unsigned long arg) | 
 | 130 | { | 
 | 131 | 	cpumask_t old_affinity = current->cpus_allowed; | 
 | 132 | 	unsigned long ret; | 
 | 133 |  | 
 | 134 | 	/* should return -EINVAL to userspace */ | 
 | 135 | 	if (set_cpus_allowed(current, cpumask_of_cpu(cpu))) | 
 | 136 | 		return 0; | 
 | 137 |  | 
 | 138 | 	ret = func(arg); | 
 | 139 |  | 
 | 140 | 	set_cpus_allowed(current, old_affinity); | 
 | 141 |  | 
 | 142 | 	return ret; | 
 | 143 | } | 
 | 144 |  | 
 | 145 | #define SYSFS_PMCSETUP(NAME, ADDRESS) \ | 
 | 146 | static unsigned long read_##NAME(unsigned long junk) \ | 
 | 147 | { \ | 
 | 148 | 	return mfspr(ADDRESS); \ | 
 | 149 | } \ | 
 | 150 | static unsigned long write_##NAME(unsigned long val) \ | 
 | 151 | { \ | 
 | 152 | 	ppc64_enable_pmcs(); \ | 
 | 153 | 	mtspr(ADDRESS, val); \ | 
 | 154 | 	return 0; \ | 
 | 155 | } \ | 
 | 156 | static ssize_t show_##NAME(struct sys_device *dev, char *buf) \ | 
 | 157 | { \ | 
 | 158 | 	struct cpu *cpu = container_of(dev, struct cpu, sysdev); \ | 
 | 159 | 	unsigned long val = run_on_cpu(cpu->sysdev.id, read_##NAME, 0); \ | 
 | 160 | 	return sprintf(buf, "%lx\n", val); \ | 
 | 161 | } \ | 
 | 162 | static ssize_t __attribute_used__ \ | 
 | 163 | 	store_##NAME(struct sys_device *dev, const char *buf, size_t count) \ | 
 | 164 | { \ | 
 | 165 | 	struct cpu *cpu = container_of(dev, struct cpu, sysdev); \ | 
 | 166 | 	unsigned long val; \ | 
 | 167 | 	int ret = sscanf(buf, "%lx", &val); \ | 
 | 168 | 	if (ret != 1) \ | 
 | 169 | 		return -EINVAL; \ | 
 | 170 | 	run_on_cpu(cpu->sysdev.id, write_##NAME, val); \ | 
 | 171 | 	return count; \ | 
 | 172 | } | 
 | 173 |  | 
 | 174 | SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0); | 
 | 175 | SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1); | 
 | 176 | SYSFS_PMCSETUP(mmcra, SPRN_MMCRA); | 
 | 177 | SYSFS_PMCSETUP(pmc1, SPRN_PMC1); | 
 | 178 | SYSFS_PMCSETUP(pmc2, SPRN_PMC2); | 
 | 179 | SYSFS_PMCSETUP(pmc3, SPRN_PMC3); | 
 | 180 | SYSFS_PMCSETUP(pmc4, SPRN_PMC4); | 
 | 181 | SYSFS_PMCSETUP(pmc5, SPRN_PMC5); | 
 | 182 | SYSFS_PMCSETUP(pmc6, SPRN_PMC6); | 
 | 183 | SYSFS_PMCSETUP(pmc7, SPRN_PMC7); | 
 | 184 | SYSFS_PMCSETUP(pmc8, SPRN_PMC8); | 
 | 185 | SYSFS_PMCSETUP(purr, SPRN_PURR); | 
 | 186 |  | 
 | 187 | static SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0); | 
 | 188 | static SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1); | 
 | 189 | static SYSDEV_ATTR(mmcra, 0600, show_mmcra, store_mmcra); | 
 | 190 | static SYSDEV_ATTR(pmc1, 0600, show_pmc1, store_pmc1); | 
 | 191 | static SYSDEV_ATTR(pmc2, 0600, show_pmc2, store_pmc2); | 
 | 192 | static SYSDEV_ATTR(pmc3, 0600, show_pmc3, store_pmc3); | 
 | 193 | static SYSDEV_ATTR(pmc4, 0600, show_pmc4, store_pmc4); | 
 | 194 | static SYSDEV_ATTR(pmc5, 0600, show_pmc5, store_pmc5); | 
 | 195 | static SYSDEV_ATTR(pmc6, 0600, show_pmc6, store_pmc6); | 
 | 196 | static SYSDEV_ATTR(pmc7, 0600, show_pmc7, store_pmc7); | 
 | 197 | static SYSDEV_ATTR(pmc8, 0600, show_pmc8, store_pmc8); | 
 | 198 | static SYSDEV_ATTR(purr, 0600, show_purr, NULL); | 
 | 199 |  | 
 | 200 | static void register_cpu_online(unsigned int cpu) | 
 | 201 | { | 
 | 202 | 	struct cpu *c = &per_cpu(cpu_devices, cpu); | 
 | 203 | 	struct sys_device *s = &c->sysdev; | 
 | 204 |  | 
 | 205 | #ifndef CONFIG_PPC_ISERIES | 
 | 206 | 	if (cpu_has_feature(CPU_FTR_SMT)) | 
 | 207 | 		sysdev_create_file(s, &attr_smt_snooze_delay); | 
 | 208 | #endif | 
 | 209 |  | 
 | 210 | 	/* PMC stuff */ | 
 | 211 |  | 
 | 212 | 	sysdev_create_file(s, &attr_mmcr0); | 
 | 213 | 	sysdev_create_file(s, &attr_mmcr1); | 
 | 214 |  | 
 | 215 | 	if (cpu_has_feature(CPU_FTR_MMCRA)) | 
 | 216 | 		sysdev_create_file(s, &attr_mmcra); | 
 | 217 |  | 
| Anton Blanchard | fd5b437 | 2005-09-06 14:47:49 +1000 | [diff] [blame] | 218 | 	if (cur_cpu_spec->num_pmcs >= 1) | 
 | 219 | 		sysdev_create_file(s, &attr_pmc1); | 
 | 220 | 	if (cur_cpu_spec->num_pmcs >= 2) | 
 | 221 | 		sysdev_create_file(s, &attr_pmc2); | 
 | 222 | 	if (cur_cpu_spec->num_pmcs >= 3) | 
 | 223 | 		sysdev_create_file(s, &attr_pmc3); | 
 | 224 | 	if (cur_cpu_spec->num_pmcs >= 4) | 
 | 225 | 		sysdev_create_file(s, &attr_pmc4); | 
 | 226 | 	if (cur_cpu_spec->num_pmcs >= 5) | 
 | 227 | 		sysdev_create_file(s, &attr_pmc5); | 
 | 228 | 	if (cur_cpu_spec->num_pmcs >= 6) | 
 | 229 | 		sysdev_create_file(s, &attr_pmc6); | 
 | 230 | 	if (cur_cpu_spec->num_pmcs >= 7) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | 		sysdev_create_file(s, &attr_pmc7); | 
| Anton Blanchard | fd5b437 | 2005-09-06 14:47:49 +1000 | [diff] [blame] | 232 | 	if (cur_cpu_spec->num_pmcs >= 8) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | 		sysdev_create_file(s, &attr_pmc8); | 
| David Gibson | d3d2176 | 2005-11-10 15:26:20 +1100 | [diff] [blame] | 234 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | 	if (cpu_has_feature(CPU_FTR_SMT)) | 
 | 236 | 		sysdev_create_file(s, &attr_purr); | 
 | 237 | } | 
 | 238 |  | 
 | 239 | #ifdef CONFIG_HOTPLUG_CPU | 
 | 240 | static void unregister_cpu_online(unsigned int cpu) | 
 | 241 | { | 
 | 242 | 	struct cpu *c = &per_cpu(cpu_devices, cpu); | 
 | 243 | 	struct sys_device *s = &c->sysdev; | 
 | 244 |  | 
 | 245 | 	BUG_ON(c->no_control); | 
 | 246 |  | 
 | 247 | #ifndef CONFIG_PPC_ISERIES | 
 | 248 | 	if (cpu_has_feature(CPU_FTR_SMT)) | 
 | 249 | 		sysdev_remove_file(s, &attr_smt_snooze_delay); | 
 | 250 | #endif | 
 | 251 |  | 
 | 252 | 	/* PMC stuff */ | 
 | 253 |  | 
 | 254 | 	sysdev_remove_file(s, &attr_mmcr0); | 
 | 255 | 	sysdev_remove_file(s, &attr_mmcr1); | 
 | 256 |  | 
 | 257 | 	if (cpu_has_feature(CPU_FTR_MMCRA)) | 
 | 258 | 		sysdev_remove_file(s, &attr_mmcra); | 
 | 259 |  | 
| Anton Blanchard | fd5b437 | 2005-09-06 14:47:49 +1000 | [diff] [blame] | 260 | 	if (cur_cpu_spec->num_pmcs >= 1) | 
 | 261 | 		sysdev_remove_file(s, &attr_pmc1); | 
 | 262 | 	if (cur_cpu_spec->num_pmcs >= 2) | 
 | 263 | 		sysdev_remove_file(s, &attr_pmc2); | 
 | 264 | 	if (cur_cpu_spec->num_pmcs >= 3) | 
 | 265 | 		sysdev_remove_file(s, &attr_pmc3); | 
 | 266 | 	if (cur_cpu_spec->num_pmcs >= 4) | 
 | 267 | 		sysdev_remove_file(s, &attr_pmc4); | 
 | 268 | 	if (cur_cpu_spec->num_pmcs >= 5) | 
 | 269 | 		sysdev_remove_file(s, &attr_pmc5); | 
 | 270 | 	if (cur_cpu_spec->num_pmcs >= 6) | 
 | 271 | 		sysdev_remove_file(s, &attr_pmc6); | 
 | 272 | 	if (cur_cpu_spec->num_pmcs >= 7) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | 		sysdev_remove_file(s, &attr_pmc7); | 
| Anton Blanchard | fd5b437 | 2005-09-06 14:47:49 +1000 | [diff] [blame] | 274 | 	if (cur_cpu_spec->num_pmcs >= 8) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | 		sysdev_remove_file(s, &attr_pmc8); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 |  | 
 | 277 | 	if (cpu_has_feature(CPU_FTR_SMT)) | 
 | 278 | 		sysdev_remove_file(s, &attr_purr); | 
 | 279 | } | 
 | 280 | #endif /* CONFIG_HOTPLUG_CPU */ | 
 | 281 |  | 
 | 282 | static int __devinit sysfs_cpu_notify(struct notifier_block *self, | 
 | 283 | 				      unsigned long action, void *hcpu) | 
 | 284 | { | 
 | 285 | 	unsigned int cpu = (unsigned int)(long)hcpu; | 
 | 286 |  | 
 | 287 | 	switch (action) { | 
 | 288 | 	case CPU_ONLINE: | 
 | 289 | 		register_cpu_online(cpu); | 
 | 290 | 		break; | 
 | 291 | #ifdef CONFIG_HOTPLUG_CPU | 
 | 292 | 	case CPU_DEAD: | 
 | 293 | 		unregister_cpu_online(cpu); | 
 | 294 | 		break; | 
 | 295 | #endif | 
 | 296 | 	} | 
 | 297 | 	return NOTIFY_OK; | 
 | 298 | } | 
 | 299 |  | 
 | 300 | static struct notifier_block __devinitdata sysfs_cpu_nb = { | 
 | 301 | 	.notifier_call	= sysfs_cpu_notify, | 
 | 302 | }; | 
 | 303 |  | 
 | 304 | /* NUMA stuff */ | 
 | 305 |  | 
 | 306 | #ifdef CONFIG_NUMA | 
 | 307 | static struct node node_devices[MAX_NUMNODES]; | 
 | 308 |  | 
 | 309 | static void register_nodes(void) | 
 | 310 | { | 
 | 311 | 	int i; | 
 | 312 |  | 
 | 313 | 	for (i = 0; i < MAX_NUMNODES; i++) { | 
 | 314 | 		if (node_online(i)) { | 
 | 315 | 			int p_node = parent_node(i); | 
 | 316 | 			struct node *parent = NULL; | 
 | 317 |  | 
 | 318 | 			if (p_node != i) | 
 | 319 | 				parent = &node_devices[p_node]; | 
 | 320 |  | 
 | 321 | 			register_node(&node_devices[i], i, parent); | 
 | 322 | 		} | 
 | 323 | 	} | 
 | 324 | } | 
 | 325 | #else | 
 | 326 | static void register_nodes(void) | 
 | 327 | { | 
 | 328 | 	return; | 
 | 329 | } | 
 | 330 | #endif | 
 | 331 |  | 
 | 332 | /* Only valid if CPU is present. */ | 
 | 333 | static ssize_t show_physical_id(struct sys_device *dev, char *buf) | 
 | 334 | { | 
 | 335 | 	struct cpu *cpu = container_of(dev, struct cpu, sysdev); | 
 | 336 |  | 
 | 337 | 	return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->sysdev.id)); | 
 | 338 | } | 
 | 339 | static SYSDEV_ATTR(physical_id, 0444, show_physical_id, NULL); | 
 | 340 |  | 
 | 341 | static int __init topology_init(void) | 
 | 342 | { | 
 | 343 | 	int cpu; | 
 | 344 | 	struct node *parent = NULL; | 
 | 345 |  | 
 | 346 | 	register_nodes(); | 
 | 347 |  | 
 | 348 | 	register_cpu_notifier(&sysfs_cpu_nb); | 
 | 349 |  | 
 | 350 | 	for_each_cpu(cpu) { | 
 | 351 | 		struct cpu *c = &per_cpu(cpu_devices, cpu); | 
 | 352 |  | 
 | 353 | #ifdef CONFIG_NUMA | 
| Nathan Lynch | f5f1cc5 | 2005-06-28 20:55:25 +1000 | [diff] [blame] | 354 | 		/* The node to which a cpu belongs can't be known | 
 | 355 | 		 * until the cpu is made present. | 
 | 356 | 		 */ | 
 | 357 | 		parent = NULL; | 
 | 358 | 		if (cpu_present(cpu)) | 
 | 359 | 			parent = &node_devices[cpu_to_node(cpu)]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | #endif | 
 | 361 | 		/* | 
 | 362 | 		 * For now, we just see if the system supports making | 
 | 363 | 		 * the RTAS calls for CPU hotplug.  But, there may be a | 
 | 364 | 		 * more comprehensive way to do this for an individual | 
 | 365 | 		 * CPU.  For instance, the boot cpu might never be valid | 
 | 366 | 		 * for hotplugging. | 
 | 367 | 		 */ | 
 | 368 | 		if (!ppc_md.cpu_die) | 
 | 369 | 			c->no_control = 1; | 
 | 370 |  | 
 | 371 | 		if (cpu_online(cpu) || (c->no_control == 0)) { | 
 | 372 | 			register_cpu(c, cpu, parent); | 
 | 373 |  | 
 | 374 | 			sysdev_create_file(&c->sysdev, &attr_physical_id); | 
 | 375 | 		} | 
 | 376 |  | 
 | 377 | 		if (cpu_online(cpu)) | 
 | 378 | 			register_cpu_online(cpu); | 
 | 379 | 	} | 
 | 380 |  | 
 | 381 | 	return 0; | 
 | 382 | } | 
 | 383 | __initcall(topology_init); |