1 /*
2  * Populate sysfs with topology information
3  *
4  * Written by: Matthew Dobson, IBM Corporation
5  * Original Code: Paul Dorwin, IBM Corporation, Patrick Mochel, OSDL
6  *
7  * Copyright (C) 2002, IBM Corp.
8  *
9  * All rights reserved.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
19  * NON INFRINGEMENT.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  *
26  * Send feedback to <colpatch@us.ibm.com>
27  */
28 #include <linux/nodemask.h>
29 #include <linux/export.h>
30 #include <linux/mmzone.h>
31 #include <linux/init.h>
32 #include <linux/smp.h>
33 #include <linux/irq.h>
34 #include <asm/cpu.h>
35 
36 static DEFINE_PER_CPU(struct x86_cpu, cpu_devices);
37 
38 #ifdef CONFIG_HOTPLUG_CPU
39 
40 #ifdef CONFIG_BOOTPARAM_HOTPLUG_CPU0
41 static int cpu0_hotpluggable = 1;
42 #else
43 static int cpu0_hotpluggable;
enable_cpu0_hotplug(char * str)44 static int __init enable_cpu0_hotplug(char *str)
45 {
46 	cpu0_hotpluggable = 1;
47 	return 1;
48 }
49 
50 __setup("cpu0_hotplug", enable_cpu0_hotplug);
51 #endif
52 
53 #ifdef CONFIG_DEBUG_HOTPLUG_CPU0
54 /*
55  * This function offlines a CPU as early as possible and allows userspace to
56  * boot up without the CPU. The CPU can be onlined back by user after boot.
57  *
58  * This is only called for debugging CPU offline/online feature.
59  */
_debug_hotplug_cpu(int cpu,int action)60 int _debug_hotplug_cpu(int cpu, int action)
61 {
62 	struct device *dev = get_cpu_device(cpu);
63 	int ret;
64 
65 	if (!cpu_is_hotpluggable(cpu))
66 		return -EINVAL;
67 
68 	lock_device_hotplug();
69 
70 	switch (action) {
71 	case 0:
72 		ret = cpu_down(cpu);
73 		if (!ret) {
74 			pr_info("CPU %u is now offline\n", cpu);
75 			dev->offline = true;
76 			kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
77 		} else
78 			pr_debug("Can't offline CPU%d.\n", cpu);
79 		break;
80 	case 1:
81 		ret = cpu_up(cpu);
82 		if (!ret) {
83 			dev->offline = false;
84 			kobject_uevent(&dev->kobj, KOBJ_ONLINE);
85 		} else {
86 			pr_debug("Can't online CPU%d.\n", cpu);
87 		}
88 		break;
89 	default:
90 		ret = -EINVAL;
91 	}
92 
93 	unlock_device_hotplug();
94 
95 	return ret;
96 }
97 
debug_hotplug_cpu(void)98 static int __init debug_hotplug_cpu(void)
99 {
100 	_debug_hotplug_cpu(0, 0);
101 	return 0;
102 }
103 
104 late_initcall_sync(debug_hotplug_cpu);
105 #endif /* CONFIG_DEBUG_HOTPLUG_CPU0 */
106 
arch_register_cpu(int num)107 int arch_register_cpu(int num)
108 {
109 	struct cpuinfo_x86 *c = &cpu_data(num);
110 
111 	/*
112 	 * Currently CPU0 is only hotpluggable on Intel platforms. Other
113 	 * vendors can add hotplug support later.
114 	 * Xen PV guests don't support CPU0 hotplug at all.
115 	 */
116 	if (c->x86_vendor != X86_VENDOR_INTEL ||
117 	    boot_cpu_has(X86_FEATURE_XENPV))
118 		cpu0_hotpluggable = 0;
119 
120 	/*
121 	 * Two known BSP/CPU0 dependencies: Resume from suspend/hibernate
122 	 * depends on BSP. PIC interrupts depend on BSP.
123 	 *
124 	 * If the BSP depencies are under control, one can tell kernel to
125 	 * enable BSP hotplug. This basically adds a control file and
126 	 * one can attempt to offline BSP.
127 	 */
128 	if (num == 0 && cpu0_hotpluggable) {
129 		unsigned int irq;
130 		/*
131 		 * We won't take down the boot processor on i386 if some
132 		 * interrupts only are able to be serviced by the BSP in PIC.
133 		 */
134 		for_each_active_irq(irq) {
135 			if (!IO_APIC_IRQ(irq) && irq_has_action(irq)) {
136 				cpu0_hotpluggable = 0;
137 				break;
138 			}
139 		}
140 	}
141 	if (num || cpu0_hotpluggable)
142 		per_cpu(cpu_devices, num).cpu.hotpluggable = 1;
143 
144 	return register_cpu(&per_cpu(cpu_devices, num).cpu, num);
145 }
146 EXPORT_SYMBOL(arch_register_cpu);
147 
arch_unregister_cpu(int num)148 void arch_unregister_cpu(int num)
149 {
150 	unregister_cpu(&per_cpu(cpu_devices, num).cpu);
151 }
152 EXPORT_SYMBOL(arch_unregister_cpu);
153 #else /* CONFIG_HOTPLUG_CPU */
154 
arch_register_cpu(int num)155 static int __init arch_register_cpu(int num)
156 {
157 	return register_cpu(&per_cpu(cpu_devices, num).cpu, num);
158 }
159 #endif /* CONFIG_HOTPLUG_CPU */
160 
topology_init(void)161 static int __init topology_init(void)
162 {
163 	int i;
164 
165 #ifdef CONFIG_NUMA
166 	for_each_online_node(i)
167 		register_one_node(i);
168 #endif
169 
170 	for_each_present_cpu(i)
171 		arch_register_cpu(i);
172 
173 	return 0;
174 }
175 subsys_initcall(topology_init);
176