xref: /openbmc/linux/drivers/base/cpu.c (revision 77018fb9)
1989d42e8SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
38a25a2fdSKay Sievers  * CPU subsystem support
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
6024f7846SBen Hutchings #include <linux/kernel.h>
71da177e4SLinus Torvalds #include <linux/module.h>
81da177e4SLinus Torvalds #include <linux/init.h>
9f6a57033SAl Viro #include <linux/sched.h>
101da177e4SLinus Torvalds #include <linux/cpu.h>
111da177e4SLinus Torvalds #include <linux/topology.h>
121da177e4SLinus Torvalds #include <linux/device.h>
1376b67ed9SKAMEZAWA Hiroyuki #include <linux/node.h>
145a0e3ad6STejun Heo #include <linux/gfp.h>
15fad12ac8SThomas Renninger #include <linux/slab.h>
169f13a1fdSBen Hutchings #include <linux/percpu.h>
17ac212b69SRafael J. Wysocki #include <linux/acpi.h>
18f86e4718SSudeep KarkadaNagesha #include <linux/of.h>
1967bad2fdSArd Biesheuvel #include <linux/cpufeature.h>
206570a9a1SRik van Riel #include <linux/tick.h>
2137efa4b4SAlex Shi #include <linux/pm_qos.h>
221fd7ab3fSWaiman Long #include <linux/delay.h>
23edb93821SFrederic Weisbecker #include <linux/sched/isolation.h>
241da177e4SLinus Torvalds 
25a1bdc7aaSBen Dooks #include "base.h"
261da177e4SLinus Torvalds 
278a25a2fdSKay Sievers static DEFINE_PER_CPU(struct device *, cpu_sys_devices);
28ad74557aSAshok Raj 
cpu_subsys_match(struct device * dev,struct device_driver * drv)29ac212b69SRafael J. Wysocki static int cpu_subsys_match(struct device *dev, struct device_driver *drv)
30ac212b69SRafael J. Wysocki {
31ac212b69SRafael J. Wysocki 	/* ACPI style match is the only one that may succeed. */
32ac212b69SRafael J. Wysocki 	if (acpi_driver_match_device(dev, drv))
33ac212b69SRafael J. Wysocki 		return 1;
34ac212b69SRafael J. Wysocki 
35ac212b69SRafael J. Wysocki 	return 0;
36ac212b69SRafael J. Wysocki }
37ac212b69SRafael J. Wysocki 
381da177e4SLinus Torvalds #ifdef CONFIG_HOTPLUG_CPU
change_cpu_under_node(struct cpu * cpu,unsigned int from_nid,unsigned int to_nid)3934640468SYasuaki Ishimatsu static void change_cpu_under_node(struct cpu *cpu,
4034640468SYasuaki Ishimatsu 			unsigned int from_nid, unsigned int to_nid)
4134640468SYasuaki Ishimatsu {
4234640468SYasuaki Ishimatsu 	int cpuid = cpu->dev.id;
4334640468SYasuaki Ishimatsu 	unregister_cpu_under_node(cpuid, from_nid);
4434640468SYasuaki Ishimatsu 	register_cpu_under_node(cpuid, to_nid);
4534640468SYasuaki Ishimatsu 	cpu->node_id = to_nid;
4634640468SYasuaki Ishimatsu }
4734640468SYasuaki Ishimatsu 
cpu_subsys_online(struct device * dev)48eda5867bSMathias Krause static int cpu_subsys_online(struct device *dev)
491da177e4SLinus Torvalds {
508a25a2fdSKay Sievers 	struct cpu *cpu = container_of(dev, struct cpu, dev);
510902a904SRafael J. Wysocki 	int cpuid = dev->id;
5234640468SYasuaki Ishimatsu 	int from_nid, to_nid;
536dedcca6SToshi Kani 	int ret;
541fd7ab3fSWaiman Long 	int retries = 0;
550902a904SRafael J. Wysocki 
5634640468SYasuaki Ishimatsu 	from_nid = cpu_to_node(cpuid);
57c7991b0bSRafael J. Wysocki 	if (from_nid == NUMA_NO_NODE)
586dedcca6SToshi Kani 		return -ENODEV;
59c7991b0bSRafael J. Wysocki 
601fd7ab3fSWaiman Long retry:
6133c3736eSQais Yousef 	ret = cpu_device_up(dev);
621fd7ab3fSWaiman Long 
631fd7ab3fSWaiman Long 	/*
641fd7ab3fSWaiman Long 	 * If -EBUSY is returned, it is likely that hotplug is temporarily
651fd7ab3fSWaiman Long 	 * disabled when cpu_hotplug_disable() was called. This condition is
661fd7ab3fSWaiman Long 	 * transient. So we retry after waiting for an exponentially
671fd7ab3fSWaiman Long 	 * increasing delay up to a total of at least 620ms as some PCI
681fd7ab3fSWaiman Long 	 * device initialization can take quite a while.
691fd7ab3fSWaiman Long 	 */
701fd7ab3fSWaiman Long 	if (ret == -EBUSY) {
711fd7ab3fSWaiman Long 		retries++;
721fd7ab3fSWaiman Long 		if (retries > 5)
731fd7ab3fSWaiman Long 			return ret;
741fd7ab3fSWaiman Long 		msleep(10 * (1 << retries));
751fd7ab3fSWaiman Long 		goto retry;
761fd7ab3fSWaiman Long 	}
771fd7ab3fSWaiman Long 
7834640468SYasuaki Ishimatsu 	/*
7934640468SYasuaki Ishimatsu 	 * When hot adding memory to memoryless node and enabling a cpu
8034640468SYasuaki Ishimatsu 	 * on the node, node number of the cpu may internally change.
8134640468SYasuaki Ishimatsu 	 */
8234640468SYasuaki Ishimatsu 	to_nid = cpu_to_node(cpuid);
8334640468SYasuaki Ishimatsu 	if (from_nid != to_nid)
8434640468SYasuaki Ishimatsu 		change_cpu_under_node(cpu, from_nid, to_nid);
8534640468SYasuaki Ishimatsu 
861da177e4SLinus Torvalds 	return ret;
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds 
cpu_subsys_offline(struct device * dev)890902a904SRafael J. Wysocki static int cpu_subsys_offline(struct device *dev)
901da177e4SLinus Torvalds {
9133c3736eSQais Yousef 	return cpu_device_down(dev);
921da177e4SLinus Torvalds }
931c4e2d70SIgor Mammedov 
unregister_cpu(struct cpu * cpu)9476b67ed9SKAMEZAWA Hiroyuki void unregister_cpu(struct cpu *cpu)
951da177e4SLinus Torvalds {
968a25a2fdSKay Sievers 	int logical_cpu = cpu->dev.id;
971da177e4SLinus Torvalds 
9876b67ed9SKAMEZAWA Hiroyuki 	unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu));
9976b67ed9SKAMEZAWA Hiroyuki 
1008a25a2fdSKay Sievers 	device_unregister(&cpu->dev);
101e37d05daSMike Travis 	per_cpu(cpu_sys_devices, logical_cpu) = NULL;
1021da177e4SLinus Torvalds 	return;
1031da177e4SLinus Torvalds }
10412633e80SNathan Fontenot 
10512633e80SNathan Fontenot #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
cpu_probe_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1068a25a2fdSKay Sievers static ssize_t cpu_probe_store(struct device *dev,
1078a25a2fdSKay Sievers 			       struct device_attribute *attr,
10828812fe1SAndi Kleen 			       const char *buf,
10912633e80SNathan Fontenot 			       size_t count)
11012633e80SNathan Fontenot {
111574b851eSToshi Kani 	ssize_t cnt;
112574b851eSToshi Kani 	int ret;
113574b851eSToshi Kani 
114574b851eSToshi Kani 	ret = lock_device_hotplug_sysfs();
115574b851eSToshi Kani 	if (ret)
116574b851eSToshi Kani 		return ret;
117574b851eSToshi Kani 
118574b851eSToshi Kani 	cnt = arch_cpu_probe(buf, count);
119574b851eSToshi Kani 
120574b851eSToshi Kani 	unlock_device_hotplug();
121574b851eSToshi Kani 	return cnt;
12212633e80SNathan Fontenot }
12312633e80SNathan Fontenot 
cpu_release_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1248a25a2fdSKay Sievers static ssize_t cpu_release_store(struct device *dev,
1258a25a2fdSKay Sievers 				 struct device_attribute *attr,
12628812fe1SAndi Kleen 				 const char *buf,
12712633e80SNathan Fontenot 				 size_t count)
12812633e80SNathan Fontenot {
129574b851eSToshi Kani 	ssize_t cnt;
130574b851eSToshi Kani 	int ret;
131574b851eSToshi Kani 
132574b851eSToshi Kani 	ret = lock_device_hotplug_sysfs();
133574b851eSToshi Kani 	if (ret)
134574b851eSToshi Kani 		return ret;
135574b851eSToshi Kani 
136574b851eSToshi Kani 	cnt = arch_cpu_release(buf, count);
137574b851eSToshi Kani 
138574b851eSToshi Kani 	unlock_device_hotplug();
139574b851eSToshi Kani 	return cnt;
14012633e80SNathan Fontenot }
14112633e80SNathan Fontenot 
1428a25a2fdSKay Sievers static DEVICE_ATTR(probe, S_IWUSR, NULL, cpu_probe_store);
1438a25a2fdSKay Sievers static DEVICE_ATTR(release, S_IWUSR, NULL, cpu_release_store);
14412633e80SNathan Fontenot #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
1451da177e4SLinus Torvalds #endif /* CONFIG_HOTPLUG_CPU */
1461da177e4SLinus Torvalds 
14783dd18e0SBaoquan He #ifdef CONFIG_KEXEC_CORE
14851be5606SVivek Goyal #include <linux/kexec.h>
14951be5606SVivek Goyal 
crash_notes_show(struct device * dev,struct device_attribute * attr,char * buf)150948b3edbSJoe Perches static ssize_t crash_notes_show(struct device *dev,
151948b3edbSJoe Perches 				struct device_attribute *attr,
1524a0b2b4dSAndi Kleen 				char *buf)
15351be5606SVivek Goyal {
1548a25a2fdSKay Sievers 	struct cpu *cpu = container_of(dev, struct cpu, dev);
15551be5606SVivek Goyal 	unsigned long long addr;
15651be5606SVivek Goyal 	int cpunum;
15751be5606SVivek Goyal 
1588a25a2fdSKay Sievers 	cpunum = cpu->dev.id;
15951be5606SVivek Goyal 
16051be5606SVivek Goyal 	/*
16151be5606SVivek Goyal 	 * Might be reading other cpu's data based on which cpu read thread
16251be5606SVivek Goyal 	 * has been scheduled. But cpu data (memory) is allocated once during
16351be5606SVivek Goyal 	 * boot up and this data does not change there after. Hence this
16451be5606SVivek Goyal 	 * operation should be safe. No locking required.
16551be5606SVivek Goyal 	 */
1663b034b0dSVivek Goyal 	addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpunum));
167eca4549fSZhang Yanfei 
168948b3edbSJoe Perches 	return sysfs_emit(buf, "%llx\n", addr);
169948b3edbSJoe Perches }
170948b3edbSJoe Perches static DEVICE_ATTR_ADMIN_RO(crash_notes);
171948b3edbSJoe Perches 
crash_notes_size_show(struct device * dev,struct device_attribute * attr,char * buf)172948b3edbSJoe Perches static ssize_t crash_notes_size_show(struct device *dev,
173eca4549fSZhang Yanfei 				     struct device_attribute *attr,
174eca4549fSZhang Yanfei 				     char *buf)
175eca4549fSZhang Yanfei {
176948b3edbSJoe Perches 	return sysfs_emit(buf, "%zu\n", sizeof(note_buf_t));
177eca4549fSZhang Yanfei }
178948b3edbSJoe Perches static DEVICE_ATTR_ADMIN_RO(crash_notes_size);
179c055da9fSIgor Mammedov 
180c055da9fSIgor Mammedov static struct attribute *crash_note_cpu_attrs[] = {
181c055da9fSIgor Mammedov 	&dev_attr_crash_notes.attr,
182c055da9fSIgor Mammedov 	&dev_attr_crash_notes_size.attr,
183c055da9fSIgor Mammedov 	NULL
184c055da9fSIgor Mammedov };
185c055da9fSIgor Mammedov 
1865a576764SRikard Falkeborn static const struct attribute_group crash_note_cpu_attr_group = {
187c055da9fSIgor Mammedov 	.attrs = crash_note_cpu_attrs,
188c055da9fSIgor Mammedov };
18951be5606SVivek Goyal #endif
19051be5606SVivek Goyal 
191c055da9fSIgor Mammedov static const struct attribute_group *common_cpu_attr_groups[] = {
19283dd18e0SBaoquan He #ifdef CONFIG_KEXEC_CORE
193c055da9fSIgor Mammedov 	&crash_note_cpu_attr_group,
194c055da9fSIgor Mammedov #endif
195c055da9fSIgor Mammedov 	NULL
196c055da9fSIgor Mammedov };
197c055da9fSIgor Mammedov 
1981c4e2d70SIgor Mammedov static const struct attribute_group *hotplugable_cpu_attr_groups[] = {
19983dd18e0SBaoquan He #ifdef CONFIG_KEXEC_CORE
2001c4e2d70SIgor Mammedov 	&crash_note_cpu_attr_group,
2011c4e2d70SIgor Mammedov #endif
2021c4e2d70SIgor Mammedov 	NULL
2031c4e2d70SIgor Mammedov };
2041c4e2d70SIgor Mammedov 
2051da177e4SLinus Torvalds /*
2069d1fe323SMike Travis  * Print cpu online, possible, present, and system maps
2079d1fe323SMike Travis  */
208265d2e2eSAndi Kleen 
209265d2e2eSAndi Kleen struct cpu_attr {
2108a25a2fdSKay Sievers 	struct device_attribute attr;
211848e2391SRasmus Villemoes 	const struct cpumask *const map;
212265d2e2eSAndi Kleen };
213265d2e2eSAndi Kleen 
show_cpus_attr(struct device * dev,struct device_attribute * attr,char * buf)2148a25a2fdSKay Sievers static ssize_t show_cpus_attr(struct device *dev,
2158a25a2fdSKay Sievers 			      struct device_attribute *attr,
216265d2e2eSAndi Kleen 			      char *buf)
2179d1fe323SMike Travis {
218265d2e2eSAndi Kleen 	struct cpu_attr *ca = container_of(attr, struct cpu_attr, attr);
2199d1fe323SMike Travis 
220848e2391SRasmus Villemoes 	return cpumap_print_to_pagebuf(true, buf, ca->map);
2219d1fe323SMike Travis }
2229d1fe323SMike Travis 
223265d2e2eSAndi Kleen #define _CPU_ATTR(name, map) \
2248a25a2fdSKay Sievers 	{ __ATTR(name, 0444, show_cpus_attr, NULL), map }
2259d1fe323SMike Travis 
2268a25a2fdSKay Sievers /* Keep in sync with cpu_subsys_attrs */
227265d2e2eSAndi Kleen static struct cpu_attr cpu_attrs[] = {
228848e2391SRasmus Villemoes 	_CPU_ATTR(online, &__cpu_online_mask),
229848e2391SRasmus Villemoes 	_CPU_ATTR(possible, &__cpu_possible_mask),
230848e2391SRasmus Villemoes 	_CPU_ATTR(present, &__cpu_present_mask),
231265d2e2eSAndi Kleen };
2329d1fe323SMike Travis 
233e057d7aeSMike Travis /*
234e057d7aeSMike Travis  * Print values for NR_CPUS and offlined cpus
235e057d7aeSMike Travis  */
print_cpus_kernel_max(struct device * dev,struct device_attribute * attr,char * buf)2368a25a2fdSKay Sievers static ssize_t print_cpus_kernel_max(struct device *dev,
2378a25a2fdSKay Sievers 				     struct device_attribute *attr, char *buf)
238e057d7aeSMike Travis {
239aa838896SJoe Perches 	return sysfs_emit(buf, "%d\n", NR_CPUS - 1);
240e057d7aeSMike Travis }
2418a25a2fdSKay Sievers static DEVICE_ATTR(kernel_max, 0444, print_cpus_kernel_max, NULL);
242e057d7aeSMike Travis 
243e057d7aeSMike Travis /* arch-optional setting to enable display of offline cpus >= nr_cpu_ids */
244e057d7aeSMike Travis unsigned int total_cpus;
245e057d7aeSMike Travis 
print_cpus_offline(struct device * dev,struct device_attribute * attr,char * buf)2468a25a2fdSKay Sievers static ssize_t print_cpus_offline(struct device *dev,
2478a25a2fdSKay Sievers 				  struct device_attribute *attr, char *buf)
248e057d7aeSMike Travis {
249948b3edbSJoe Perches 	int len = 0;
250e057d7aeSMike Travis 	cpumask_var_t offline;
251e057d7aeSMike Travis 
252e057d7aeSMike Travis 	/* display offline cpus < nr_cpu_ids */
253e057d7aeSMike Travis 	if (!alloc_cpumask_var(&offline, GFP_KERNEL))
254e057d7aeSMike Travis 		return -ENOMEM;
255cdc6e3d3SJan Beulich 	cpumask_andnot(offline, cpu_possible_mask, cpu_online_mask);
256948b3edbSJoe Perches 	len += sysfs_emit_at(buf, len, "%*pbl", cpumask_pr_args(offline));
257e057d7aeSMike Travis 	free_cpumask_var(offline);
258e057d7aeSMike Travis 
259e057d7aeSMike Travis 	/* display offline cpus >= nr_cpu_ids */
260e057d7aeSMike Travis 	if (total_cpus && nr_cpu_ids < total_cpus) {
261948b3edbSJoe Perches 		len += sysfs_emit_at(buf, len, ",");
262e057d7aeSMike Travis 
263e057d7aeSMike Travis 		if (nr_cpu_ids == total_cpus-1)
264948b3edbSJoe Perches 			len += sysfs_emit_at(buf, len, "%u", nr_cpu_ids);
265e057d7aeSMike Travis 		else
266948b3edbSJoe Perches 			len += sysfs_emit_at(buf, len, "%u-%d",
267e057d7aeSMike Travis 					     nr_cpu_ids, total_cpus - 1);
268e057d7aeSMike Travis 	}
269e057d7aeSMike Travis 
270948b3edbSJoe Perches 	len += sysfs_emit_at(buf, len, "\n");
271948b3edbSJoe Perches 
272948b3edbSJoe Perches 	return len;
273e057d7aeSMike Travis }
2748a25a2fdSKay Sievers static DEVICE_ATTR(offline, 0444, print_cpus_offline, NULL);
275e057d7aeSMike Travis 
print_cpus_isolated(struct device * dev,struct device_attribute * attr,char * buf)27659f30abeSRik van Riel static ssize_t print_cpus_isolated(struct device *dev,
27759f30abeSRik van Riel 				  struct device_attribute *attr, char *buf)
27859f30abeSRik van Riel {
279948b3edbSJoe Perches 	int len;
280edb93821SFrederic Weisbecker 	cpumask_var_t isolated;
28159f30abeSRik van Riel 
282edb93821SFrederic Weisbecker 	if (!alloc_cpumask_var(&isolated, GFP_KERNEL))
283edb93821SFrederic Weisbecker 		return -ENOMEM;
284edb93821SFrederic Weisbecker 
285edb93821SFrederic Weisbecker 	cpumask_andnot(isolated, cpu_possible_mask,
28604d4e665SFrederic Weisbecker 		       housekeeping_cpumask(HK_TYPE_DOMAIN));
287948b3edbSJoe Perches 	len = sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(isolated));
288edb93821SFrederic Weisbecker 
289edb93821SFrederic Weisbecker 	free_cpumask_var(isolated);
29059f30abeSRik van Riel 
291948b3edbSJoe Perches 	return len;
29259f30abeSRik van Riel }
29359f30abeSRik van Riel static DEVICE_ATTR(isolated, 0444, print_cpus_isolated, NULL);
29459f30abeSRik van Riel 
2956570a9a1SRik van Riel #ifdef CONFIG_NO_HZ_FULL
print_cpus_nohz_full(struct device * dev,struct device_attribute * attr,char * buf)2966570a9a1SRik van Riel static ssize_t print_cpus_nohz_full(struct device *dev,
2976570a9a1SRik van Riel 				    struct device_attribute *attr, char *buf)
2986570a9a1SRik van Riel {
299aa838896SJoe Perches 	return sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(tick_nohz_full_mask));
3006570a9a1SRik van Riel }
3016570a9a1SRik van Riel static DEVICE_ATTR(nohz_full, 0444, print_cpus_nohz_full, NULL);
3026570a9a1SRik van Riel #endif
3036570a9a1SRik van Riel 
30488a6f899SEric DeVolder #ifdef CONFIG_CRASH_HOTPLUG
crash_hotplug_show(struct device * dev,struct device_attribute * attr,char * buf)30588a6f899SEric DeVolder static ssize_t crash_hotplug_show(struct device *dev,
30688a6f899SEric DeVolder 				     struct device_attribute *attr,
30788a6f899SEric DeVolder 				     char *buf)
30888a6f899SEric DeVolder {
30988a6f899SEric DeVolder 	return sysfs_emit(buf, "%d\n", crash_hotplug_cpu_support());
31088a6f899SEric DeVolder }
31188a6f899SEric DeVolder static DEVICE_ATTR_ADMIN_RO(crash_hotplug);
31288a6f899SEric DeVolder #endif
31388a6f899SEric DeVolder 
cpu_device_release(struct device * dev)3142885e25cSGreg Kroah-Hartman static void cpu_device_release(struct device *dev)
3152885e25cSGreg Kroah-Hartman {
3162885e25cSGreg Kroah-Hartman 	/*
3172885e25cSGreg Kroah-Hartman 	 * This is an empty function to prevent the driver core from spitting a
3182885e25cSGreg Kroah-Hartman 	 * warning at us.  Yes, I know this is directly opposite of what the
3192885e25cSGreg Kroah-Hartman 	 * documentation for the driver core and kobjects say, and the author
3202885e25cSGreg Kroah-Hartman 	 * of this code has already been publically ridiculed for doing
3212885e25cSGreg Kroah-Hartman 	 * something as foolish as this.  However, at this point in time, it is
3222885e25cSGreg Kroah-Hartman 	 * the only way to handle the issue of statically allocated cpu
3232885e25cSGreg Kroah-Hartman 	 * devices.  The different architectures will have their cpu device
3242885e25cSGreg Kroah-Hartman 	 * code reworked to properly handle this in the near future, so this
3252885e25cSGreg Kroah-Hartman 	 * function will then be changed to correctly free up the memory held
3262885e25cSGreg Kroah-Hartman 	 * by the cpu device.
3272885e25cSGreg Kroah-Hartman 	 *
3282885e25cSGreg Kroah-Hartman 	 * Never copy this way of doing things, or you too will be made fun of
32930a4840aSRalf Baechle 	 * on the linux-kernel list, you have been warned.
3302885e25cSGreg Kroah-Hartman 	 */
3312885e25cSGreg Kroah-Hartman }
3322885e25cSGreg Kroah-Hartman 
33367bad2fdSArd Biesheuvel #ifdef CONFIG_GENERIC_CPU_AUTOPROBE
print_cpu_modalias(struct device * dev,struct device_attribute * attr,char * buf)33467bad2fdSArd Biesheuvel static ssize_t print_cpu_modalias(struct device *dev,
33567bad2fdSArd Biesheuvel 				  struct device_attribute *attr,
33667bad2fdSArd Biesheuvel 				  char *buf)
33767bad2fdSArd Biesheuvel {
338948b3edbSJoe Perches 	int len = 0;
33967bad2fdSArd Biesheuvel 	u32 i;
34067bad2fdSArd Biesheuvel 
341948b3edbSJoe Perches 	len += sysfs_emit_at(buf, len,
342948b3edbSJoe Perches 			     "cpu:type:" CPU_FEATURE_TYPEFMT ":feature:",
34367bad2fdSArd Biesheuvel 			     CPU_FEATURE_TYPEVAL);
34467bad2fdSArd Biesheuvel 
34567bad2fdSArd Biesheuvel 	for (i = 0; i < MAX_CPU_FEATURES; i++)
34667bad2fdSArd Biesheuvel 		if (cpu_have_feature(i)) {
347948b3edbSJoe Perches 			if (len + sizeof(",XXXX\n") >= PAGE_SIZE) {
34867bad2fdSArd Biesheuvel 				WARN(1, "CPU features overflow page\n");
34967bad2fdSArd Biesheuvel 				break;
35067bad2fdSArd Biesheuvel 			}
351948b3edbSJoe Perches 			len += sysfs_emit_at(buf, len, ",%04X", i);
35267bad2fdSArd Biesheuvel 		}
353948b3edbSJoe Perches 	len += sysfs_emit_at(buf, len, "\n");
354948b3edbSJoe Perches 	return len;
35567bad2fdSArd Biesheuvel }
35667bad2fdSArd Biesheuvel 
cpu_uevent(const struct device * dev,struct kobj_uevent_env * env)3572a81ada3SGreg Kroah-Hartman static int cpu_uevent(const struct device *dev, struct kobj_uevent_env *env)
35867bad2fdSArd Biesheuvel {
35967bad2fdSArd Biesheuvel 	char *buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
36067bad2fdSArd Biesheuvel 	if (buf) {
36167bad2fdSArd Biesheuvel 		print_cpu_modalias(NULL, NULL, buf);
36267bad2fdSArd Biesheuvel 		add_uevent_var(env, "MODALIAS=%s", buf);
36367bad2fdSArd Biesheuvel 		kfree(buf);
36467bad2fdSArd Biesheuvel 	}
36567bad2fdSArd Biesheuvel 	return 0;
36667bad2fdSArd Biesheuvel }
36767bad2fdSArd Biesheuvel #endif
36867bad2fdSArd Biesheuvel 
3692bc19066SGreg Kroah-Hartman struct bus_type cpu_subsys = {
3702bc19066SGreg Kroah-Hartman 	.name = "cpu",
3712bc19066SGreg Kroah-Hartman 	.dev_name = "cpu",
3722bc19066SGreg Kroah-Hartman 	.match = cpu_subsys_match,
3732bc19066SGreg Kroah-Hartman #ifdef CONFIG_HOTPLUG_CPU
3742bc19066SGreg Kroah-Hartman 	.online = cpu_subsys_online,
3752bc19066SGreg Kroah-Hartman 	.offline = cpu_subsys_offline,
3762bc19066SGreg Kroah-Hartman #endif
3772bc19066SGreg Kroah-Hartman #ifdef CONFIG_GENERIC_CPU_AUTOPROBE
3782bc19066SGreg Kroah-Hartman 	.uevent = cpu_uevent,
3792bc19066SGreg Kroah-Hartman #endif
3802bc19066SGreg Kroah-Hartman };
3812bc19066SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(cpu_subsys);
3822bc19066SGreg Kroah-Hartman 
3839d1fe323SMike Travis /*
384405ae7d3SRobert P. J. Day  * register_cpu - Setup a sysfs device for a CPU.
38572486f1fSSiddha, Suresh B  * @cpu - cpu->hotpluggable field set to 1 will generate a control file in
38672486f1fSSiddha, Suresh B  *	  sysfs for this CPU.
3871da177e4SLinus Torvalds  * @num - CPU number to use when creating the device.
3881da177e4SLinus Torvalds  *
3891da177e4SLinus Torvalds  * Initialize and register the CPU device.
3901da177e4SLinus Torvalds  */
register_cpu(struct cpu * cpu,int num)391a83048ebSPaul Gortmaker int register_cpu(struct cpu *cpu, int num)
3921da177e4SLinus Torvalds {
3931da177e4SLinus Torvalds 	int error;
3948a25a2fdSKay Sievers 
3951da177e4SLinus Torvalds 	cpu->node_id = cpu_to_node(num);
39629bb5d4fSGreg Kroah-Hartman 	memset(&cpu->dev, 0x00, sizeof(struct device));
3978a25a2fdSKay Sievers 	cpu->dev.id = num;
3988a25a2fdSKay Sievers 	cpu->dev.bus = &cpu_subsys;
3992885e25cSGreg Kroah-Hartman 	cpu->dev.release = cpu_device_release;
4000902a904SRafael J. Wysocki 	cpu->dev.offline_disabled = !cpu->hotpluggable;
4011001b4d4SToshi Kani 	cpu->dev.offline = !cpu_online(num);
402f86e4718SSudeep KarkadaNagesha 	cpu->dev.of_node = of_get_cpu_node(num, NULL);
403c055da9fSIgor Mammedov 	cpu->dev.groups = common_cpu_attr_groups;
4041c4e2d70SIgor Mammedov 	if (cpu->hotpluggable)
4051c4e2d70SIgor Mammedov 		cpu->dev.groups = hotplugable_cpu_attr_groups;
4068a25a2fdSKay Sievers 	error = device_register(&cpu->dev);
4073aaba245SArvind Yadav 	if (error) {
4083aaba245SArvind Yadav 		put_device(&cpu->dev);
40959fffa34SAlex Shi 		return error;
4103aaba245SArvind Yadav 	}
41159fffa34SAlex Shi 
4128a25a2fdSKay Sievers 	per_cpu(cpu_sys_devices, num) = &cpu->dev;
41376b67ed9SKAMEZAWA Hiroyuki 	register_cpu_under_node(num, cpu_to_node(num));
4140759e80bSRafael J. Wysocki 	dev_pm_qos_expose_latency_limit(&cpu->dev,
4150759e80bSRafael J. Wysocki 					PM_QOS_RESUME_LATENCY_NO_CONSTRAINT);
41651be5606SVivek Goyal 
41759fffa34SAlex Shi 	return 0;
4181da177e4SLinus Torvalds }
4191da177e4SLinus Torvalds 
get_cpu_device(unsigned int cpu)420e7deeb9dSJinchao Wang struct device *get_cpu_device(unsigned int cpu)
421ad74557aSAshok Raj {
422e37d05daSMike Travis 	if (cpu < nr_cpu_ids && cpu_possible(cpu))
423e37d05daSMike Travis 		return per_cpu(cpu_sys_devices, cpu);
424ad74557aSAshok Raj 	else
425ad74557aSAshok Raj 		return NULL;
426ad74557aSAshok Raj }
4278a25a2fdSKay Sievers EXPORT_SYMBOL_GPL(get_cpu_device);
4288a25a2fdSKay Sievers 
device_create_release(struct device * dev)4293d52943bSSudeep Holla static void device_create_release(struct device *dev)
4303d52943bSSudeep Holla {
4313d52943bSSudeep Holla 	kfree(dev);
4323d52943bSSudeep Holla }
4333d52943bSSudeep Holla 
434fa548d79SMathieu Malaterre __printf(4, 0)
4353d52943bSSudeep Holla static struct device *
__cpu_device_create(struct device * parent,void * drvdata,const struct attribute_group ** groups,const char * fmt,va_list args)4363d52943bSSudeep Holla __cpu_device_create(struct device *parent, void *drvdata,
4373d52943bSSudeep Holla 		    const struct attribute_group **groups,
4383d52943bSSudeep Holla 		    const char *fmt, va_list args)
4393d52943bSSudeep Holla {
4403d52943bSSudeep Holla 	struct device *dev = NULL;
4416b72cf12SColin Ian King 	int retval = -ENOMEM;
4423d52943bSSudeep Holla 
4433d52943bSSudeep Holla 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
4446b72cf12SColin Ian King 	if (!dev)
4453d52943bSSudeep Holla 		goto error;
4463d52943bSSudeep Holla 
4473d52943bSSudeep Holla 	device_initialize(dev);
4483d52943bSSudeep Holla 	dev->parent = parent;
4493d52943bSSudeep Holla 	dev->groups = groups;
4503d52943bSSudeep Holla 	dev->release = device_create_release;
45185945c28SSudeep Holla 	device_set_pm_not_required(dev);
4523d52943bSSudeep Holla 	dev_set_drvdata(dev, drvdata);
4533d52943bSSudeep Holla 
4543d52943bSSudeep Holla 	retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
4553d52943bSSudeep Holla 	if (retval)
4563d52943bSSudeep Holla 		goto error;
4573d52943bSSudeep Holla 
4583d52943bSSudeep Holla 	retval = device_add(dev);
4593d52943bSSudeep Holla 	if (retval)
4603d52943bSSudeep Holla 		goto error;
4613d52943bSSudeep Holla 
4623d52943bSSudeep Holla 	return dev;
4633d52943bSSudeep Holla 
4643d52943bSSudeep Holla error:
4653d52943bSSudeep Holla 	put_device(dev);
4663d52943bSSudeep Holla 	return ERR_PTR(retval);
4673d52943bSSudeep Holla }
4683d52943bSSudeep Holla 
cpu_device_create(struct device * parent,void * drvdata,const struct attribute_group ** groups,const char * fmt,...)4693d52943bSSudeep Holla struct device *cpu_device_create(struct device *parent, void *drvdata,
4703d52943bSSudeep Holla 				 const struct attribute_group **groups,
4713d52943bSSudeep Holla 				 const char *fmt, ...)
4723d52943bSSudeep Holla {
4733d52943bSSudeep Holla 	va_list vargs;
4743d52943bSSudeep Holla 	struct device *dev;
4753d52943bSSudeep Holla 
4763d52943bSSudeep Holla 	va_start(vargs, fmt);
4773d52943bSSudeep Holla 	dev = __cpu_device_create(parent, drvdata, groups, fmt, vargs);
4783d52943bSSudeep Holla 	va_end(vargs);
4793d52943bSSudeep Holla 	return dev;
4803d52943bSSudeep Holla }
4813d52943bSSudeep Holla EXPORT_SYMBOL_GPL(cpu_device_create);
4823d52943bSSudeep Holla 
4832b9c1f03SArd Biesheuvel #ifdef CONFIG_GENERIC_CPU_AUTOPROBE
48467bad2fdSArd Biesheuvel static DEVICE_ATTR(modalias, 0444, print_cpu_modalias, NULL);
485fad12ac8SThomas Renninger #endif
486fad12ac8SThomas Renninger 
4878a25a2fdSKay Sievers static struct attribute *cpu_root_attrs[] = {
4888a25a2fdSKay Sievers #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
4898a25a2fdSKay Sievers 	&dev_attr_probe.attr,
4908a25a2fdSKay Sievers 	&dev_attr_release.attr,
4918a25a2fdSKay Sievers #endif
4928a25a2fdSKay Sievers 	&cpu_attrs[0].attr.attr,
4938a25a2fdSKay Sievers 	&cpu_attrs[1].attr.attr,
4948a25a2fdSKay Sievers 	&cpu_attrs[2].attr.attr,
4958a25a2fdSKay Sievers 	&dev_attr_kernel_max.attr,
4968a25a2fdSKay Sievers 	&dev_attr_offline.attr,
49759f30abeSRik van Riel 	&dev_attr_isolated.attr,
4986570a9a1SRik van Riel #ifdef CONFIG_NO_HZ_FULL
4996570a9a1SRik van Riel 	&dev_attr_nohz_full.attr,
5006570a9a1SRik van Riel #endif
50188a6f899SEric DeVolder #ifdef CONFIG_CRASH_HOTPLUG
50288a6f899SEric DeVolder 	&dev_attr_crash_hotplug.attr,
50388a6f899SEric DeVolder #endif
5042b9c1f03SArd Biesheuvel #ifdef CONFIG_GENERIC_CPU_AUTOPROBE
505fad12ac8SThomas Renninger 	&dev_attr_modalias.attr,
506fad12ac8SThomas Renninger #endif
5078a25a2fdSKay Sievers 	NULL
5088a25a2fdSKay Sievers };
5098a25a2fdSKay Sievers 
5105a576764SRikard Falkeborn static const struct attribute_group cpu_root_attr_group = {
5118a25a2fdSKay Sievers 	.attrs = cpu_root_attrs,
5128a25a2fdSKay Sievers };
5138a25a2fdSKay Sievers 
5148a25a2fdSKay Sievers static const struct attribute_group *cpu_root_attr_groups[] = {
5158a25a2fdSKay Sievers 	&cpu_root_attr_group,
5168a25a2fdSKay Sievers 	NULL,
5178a25a2fdSKay Sievers };
5181da177e4SLinus Torvalds 
cpu_is_hotpluggable(unsigned int cpu)519e7deeb9dSJinchao Wang bool cpu_is_hotpluggable(unsigned int cpu)
5202987557fSJosh Triplett {
5217affca35SLinus Torvalds 	struct device *dev = get_cpu_device(cpu);
52258d76682SJoel Fernandes (Google) 	return dev && container_of(dev, struct cpu, dev)->hotpluggable
52358d76682SJoel Fernandes (Google) 		&& tick_nohz_cpu_hotpluggable(cpu);
5242987557fSJosh Triplett }
5252987557fSJosh Triplett EXPORT_SYMBOL_GPL(cpu_is_hotpluggable);
5262987557fSJosh Triplett 
5279f13a1fdSBen Hutchings #ifdef CONFIG_GENERIC_CPU_DEVICES
5289f13a1fdSBen Hutchings static DEFINE_PER_CPU(struct cpu, cpu_devices);
5299f13a1fdSBen Hutchings #endif
5309f13a1fdSBen Hutchings 
cpu_dev_register_generic(void)5319f13a1fdSBen Hutchings static void __init cpu_dev_register_generic(void)
5329f13a1fdSBen Hutchings {
5339f13a1fdSBen Hutchings #ifdef CONFIG_GENERIC_CPU_DEVICES
5349f13a1fdSBen Hutchings 	int i;
5359f13a1fdSBen Hutchings 
5369f13a1fdSBen Hutchings 	for_each_possible_cpu(i) {
5379f13a1fdSBen Hutchings 		if (register_cpu(&per_cpu(cpu_devices, i), i))
5389f13a1fdSBen Hutchings 			panic("Failed to register CPU device");
5399f13a1fdSBen Hutchings 	}
5409f13a1fdSBen Hutchings #endif
5419f13a1fdSBen Hutchings }
5429f13a1fdSBen Hutchings 
54387590ce6SThomas Gleixner #ifdef CONFIG_GENERIC_CPU_VULNERABILITIES
cpu_show_not_affected(struct device * dev,struct device_attribute * attr,char * buf)5446524c798SBorislav Petkov (AMD) static ssize_t cpu_show_not_affected(struct device *dev,
54587590ce6SThomas Gleixner 			      struct device_attribute *attr, char *buf)
54687590ce6SThomas Gleixner {
547aa838896SJoe Perches 	return sysfs_emit(buf, "Not affected\n");
54887590ce6SThomas Gleixner }
54987590ce6SThomas Gleixner 
5500fddfe33SBorislav Petkov (AMD) #define CPU_SHOW_VULN_FALLBACK(func)					\
5510fddfe33SBorislav Petkov (AMD) 	ssize_t cpu_show_##func(struct device *,			\
5520fddfe33SBorislav Petkov (AMD) 				  struct device_attribute *, char *)	\
5530fddfe33SBorislav Petkov (AMD) 		 __attribute__((weak, alias("cpu_show_not_affected")))
55487590ce6SThomas Gleixner 
5550fddfe33SBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(meltdown);
5560fddfe33SBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(spectre_v1);
5570fddfe33SBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(spectre_v2);
5580fddfe33SBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(spec_store_bypass);
5590fddfe33SBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(l1tf);
5600fddfe33SBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(mds);
5610fddfe33SBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(tsx_async_abort);
5620fddfe33SBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(itlb_multihit);
5630fddfe33SBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(srbds);
5640fddfe33SBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(mmio_stale_data);
5650fddfe33SBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(retbleed);
5660fddfe33SBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(spec_rstack_overflow);
5673477144cSBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(gds);
568*77018fb9SPawan Gupta CPU_SHOW_VULN_FALLBACK(reg_file_data_sampling);
569fb3bd914SBorislav Petkov (AMD) 
57087590ce6SThomas Gleixner static DEVICE_ATTR(meltdown, 0444, cpu_show_meltdown, NULL);
57187590ce6SThomas Gleixner static DEVICE_ATTR(spectre_v1, 0444, cpu_show_spectre_v1, NULL);
57287590ce6SThomas Gleixner static DEVICE_ATTR(spectre_v2, 0444, cpu_show_spectre_v2, NULL);
573c456442cSKonrad Rzeszutek Wilk static DEVICE_ATTR(spec_store_bypass, 0444, cpu_show_spec_store_bypass, NULL);
57417dbca11SAndi Kleen static DEVICE_ATTR(l1tf, 0444, cpu_show_l1tf, NULL);
5758a4b06d3SThomas Gleixner static DEVICE_ATTR(mds, 0444, cpu_show_mds, NULL);
5766608b45aSPawan Gupta static DEVICE_ATTR(tsx_async_abort, 0444, cpu_show_tsx_async_abort, NULL);
577db4d30fbSVineela Tummalapalli static DEVICE_ATTR(itlb_multihit, 0444, cpu_show_itlb_multihit, NULL);
5787e5b3c26SMark Gross static DEVICE_ATTR(srbds, 0444, cpu_show_srbds, NULL);
5798d50cdf8SPawan Gupta static DEVICE_ATTR(mmio_stale_data, 0444, cpu_show_mmio_stale_data, NULL);
5806b80b59bSAlexandre Chartre static DEVICE_ATTR(retbleed, 0444, cpu_show_retbleed, NULL);
581fb3bd914SBorislav Petkov (AMD) static DEVICE_ATTR(spec_rstack_overflow, 0444, cpu_show_spec_rstack_overflow, NULL);
5823477144cSBorislav Petkov (AMD) static DEVICE_ATTR(gather_data_sampling, 0444, cpu_show_gds, NULL);
583*77018fb9SPawan Gupta static DEVICE_ATTR(reg_file_data_sampling, 0444, cpu_show_reg_file_data_sampling, NULL);
58487590ce6SThomas Gleixner 
58587590ce6SThomas Gleixner static struct attribute *cpu_root_vulnerabilities_attrs[] = {
58687590ce6SThomas Gleixner 	&dev_attr_meltdown.attr,
58787590ce6SThomas Gleixner 	&dev_attr_spectre_v1.attr,
58887590ce6SThomas Gleixner 	&dev_attr_spectre_v2.attr,
589c456442cSKonrad Rzeszutek Wilk 	&dev_attr_spec_store_bypass.attr,
59017dbca11SAndi Kleen 	&dev_attr_l1tf.attr,
5918a4b06d3SThomas Gleixner 	&dev_attr_mds.attr,
5926608b45aSPawan Gupta 	&dev_attr_tsx_async_abort.attr,
593db4d30fbSVineela Tummalapalli 	&dev_attr_itlb_multihit.attr,
5947e5b3c26SMark Gross 	&dev_attr_srbds.attr,
5958d50cdf8SPawan Gupta 	&dev_attr_mmio_stale_data.attr,
5966b80b59bSAlexandre Chartre 	&dev_attr_retbleed.attr,
597fb3bd914SBorislav Petkov (AMD) 	&dev_attr_spec_rstack_overflow.attr,
5988974eb58SDaniel Sneddon 	&dev_attr_gather_data_sampling.attr,
599*77018fb9SPawan Gupta 	&dev_attr_reg_file_data_sampling.attr,
60087590ce6SThomas Gleixner 	NULL
60187590ce6SThomas Gleixner };
60287590ce6SThomas Gleixner 
60387590ce6SThomas Gleixner static const struct attribute_group cpu_root_vulnerabilities_group = {
60487590ce6SThomas Gleixner 	.name  = "vulnerabilities",
60587590ce6SThomas Gleixner 	.attrs = cpu_root_vulnerabilities_attrs,
60687590ce6SThomas Gleixner };
60787590ce6SThomas Gleixner 
cpu_register_vulnerabilities(void)60887590ce6SThomas Gleixner static void __init cpu_register_vulnerabilities(void)
60987590ce6SThomas Gleixner {
6108c99377eSGreg Kroah-Hartman 	struct device *dev = bus_get_dev_root(&cpu_subsys);
6118c99377eSGreg Kroah-Hartman 
6128c99377eSGreg Kroah-Hartman 	if (dev) {
6138c99377eSGreg Kroah-Hartman 		if (sysfs_create_group(&dev->kobj, &cpu_root_vulnerabilities_group))
61487590ce6SThomas Gleixner 			pr_err("Unable to register CPU vulnerabilities\n");
6158c99377eSGreg Kroah-Hartman 		put_device(dev);
6168c99377eSGreg Kroah-Hartman 	}
61787590ce6SThomas Gleixner }
61887590ce6SThomas Gleixner 
61987590ce6SThomas Gleixner #else
cpu_register_vulnerabilities(void)62087590ce6SThomas Gleixner static inline void cpu_register_vulnerabilities(void) { }
62187590ce6SThomas Gleixner #endif
62287590ce6SThomas Gleixner 
cpu_dev_init(void)623024f7846SBen Hutchings void __init cpu_dev_init(void)
6241da177e4SLinus Torvalds {
625024f7846SBen Hutchings 	if (subsys_system_register(&cpu_subsys, cpu_root_attr_groups))
626024f7846SBen Hutchings 		panic("Failed to register CPU subsystem");
6275c45bf27SSiddha, Suresh B 
6289f13a1fdSBen Hutchings 	cpu_dev_register_generic();
62987590ce6SThomas Gleixner 	cpu_register_vulnerabilities();
6301da177e4SLinus Torvalds }
631