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,const struct device_driver * drv)29d69d8048SGreg Kroah-Hartman static int cpu_subsys_match(struct device *dev, const 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
984e1a7df4SJames Morse set_cpu_enabled(logical_cpu, false);
9976b67ed9SKAMEZAWA Hiroyuki unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu));
10076b67ed9SKAMEZAWA Hiroyuki
1018a25a2fdSKay Sievers device_unregister(&cpu->dev);
102e37d05daSMike Travis per_cpu(cpu_sys_devices, logical_cpu) = NULL;
1031da177e4SLinus Torvalds return;
1041da177e4SLinus Torvalds }
10512633e80SNathan Fontenot
10612633e80SNathan Fontenot #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
cpu_probe_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1078a25a2fdSKay Sievers static ssize_t cpu_probe_store(struct device *dev,
1088a25a2fdSKay Sievers struct device_attribute *attr,
10928812fe1SAndi Kleen const char *buf,
11012633e80SNathan Fontenot size_t count)
11112633e80SNathan Fontenot {
112574b851eSToshi Kani ssize_t cnt;
113574b851eSToshi Kani int ret;
114574b851eSToshi Kani
115574b851eSToshi Kani ret = lock_device_hotplug_sysfs();
116574b851eSToshi Kani if (ret)
117574b851eSToshi Kani return ret;
118574b851eSToshi Kani
119574b851eSToshi Kani cnt = arch_cpu_probe(buf, count);
120574b851eSToshi Kani
121574b851eSToshi Kani unlock_device_hotplug();
122574b851eSToshi Kani return cnt;
12312633e80SNathan Fontenot }
12412633e80SNathan Fontenot
cpu_release_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1258a25a2fdSKay Sievers static ssize_t cpu_release_store(struct device *dev,
1268a25a2fdSKay Sievers struct device_attribute *attr,
12728812fe1SAndi Kleen const char *buf,
12812633e80SNathan Fontenot size_t count)
12912633e80SNathan Fontenot {
130574b851eSToshi Kani ssize_t cnt;
131574b851eSToshi Kani int ret;
132574b851eSToshi Kani
133574b851eSToshi Kani ret = lock_device_hotplug_sysfs();
134574b851eSToshi Kani if (ret)
135574b851eSToshi Kani return ret;
136574b851eSToshi Kani
137574b851eSToshi Kani cnt = arch_cpu_release(buf, count);
138574b851eSToshi Kani
139574b851eSToshi Kani unlock_device_hotplug();
140574b851eSToshi Kani return cnt;
14112633e80SNathan Fontenot }
14212633e80SNathan Fontenot
1438a25a2fdSKay Sievers static DEVICE_ATTR(probe, S_IWUSR, NULL, cpu_probe_store);
1448a25a2fdSKay Sievers static DEVICE_ATTR(release, S_IWUSR, NULL, cpu_release_store);
14512633e80SNathan Fontenot #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
1461da177e4SLinus Torvalds #endif /* CONFIG_HOTPLUG_CPU */
1471da177e4SLinus Torvalds
14802aff848SBaoquan He #ifdef CONFIG_CRASH_DUMP
14951be5606SVivek Goyal #include <linux/kexec.h>
15051be5606SVivek Goyal
crash_notes_show(struct device * dev,struct device_attribute * attr,char * buf)151948b3edbSJoe Perches static ssize_t crash_notes_show(struct device *dev,
152948b3edbSJoe Perches struct device_attribute *attr,
1534a0b2b4dSAndi Kleen char *buf)
15451be5606SVivek Goyal {
1558a25a2fdSKay Sievers struct cpu *cpu = container_of(dev, struct cpu, dev);
15651be5606SVivek Goyal unsigned long long addr;
15751be5606SVivek Goyal int cpunum;
15851be5606SVivek Goyal
1598a25a2fdSKay Sievers cpunum = cpu->dev.id;
16051be5606SVivek Goyal
16151be5606SVivek Goyal /*
16251be5606SVivek Goyal * Might be reading other cpu's data based on which cpu read thread
16351be5606SVivek Goyal * has been scheduled. But cpu data (memory) is allocated once during
16451be5606SVivek Goyal * boot up and this data does not change there after. Hence this
16551be5606SVivek Goyal * operation should be safe. No locking required.
16651be5606SVivek Goyal */
1673b034b0dSVivek Goyal addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpunum));
168eca4549fSZhang Yanfei
169948b3edbSJoe Perches return sysfs_emit(buf, "%llx\n", addr);
170948b3edbSJoe Perches }
171948b3edbSJoe Perches static DEVICE_ATTR_ADMIN_RO(crash_notes);
172948b3edbSJoe Perches
crash_notes_size_show(struct device * dev,struct device_attribute * attr,char * buf)173948b3edbSJoe Perches static ssize_t crash_notes_size_show(struct device *dev,
174eca4549fSZhang Yanfei struct device_attribute *attr,
175eca4549fSZhang Yanfei char *buf)
176eca4549fSZhang Yanfei {
177948b3edbSJoe Perches return sysfs_emit(buf, "%zu\n", sizeof(note_buf_t));
178eca4549fSZhang Yanfei }
179948b3edbSJoe Perches static DEVICE_ATTR_ADMIN_RO(crash_notes_size);
180c055da9fSIgor Mammedov
181c055da9fSIgor Mammedov static struct attribute *crash_note_cpu_attrs[] = {
182c055da9fSIgor Mammedov &dev_attr_crash_notes.attr,
183c055da9fSIgor Mammedov &dev_attr_crash_notes_size.attr,
184c055da9fSIgor Mammedov NULL
185c055da9fSIgor Mammedov };
186c055da9fSIgor Mammedov
1875a576764SRikard Falkeborn static const struct attribute_group crash_note_cpu_attr_group = {
188c055da9fSIgor Mammedov .attrs = crash_note_cpu_attrs,
189c055da9fSIgor Mammedov };
19051be5606SVivek Goyal #endif
19151be5606SVivek Goyal
192c055da9fSIgor Mammedov static const struct attribute_group *common_cpu_attr_groups[] = {
19302aff848SBaoquan He #ifdef CONFIG_CRASH_DUMP
194c055da9fSIgor Mammedov &crash_note_cpu_attr_group,
195c055da9fSIgor Mammedov #endif
196c055da9fSIgor Mammedov NULL
197c055da9fSIgor Mammedov };
198c055da9fSIgor Mammedov
1991c4e2d70SIgor Mammedov static const struct attribute_group *hotplugable_cpu_attr_groups[] = {
20002aff848SBaoquan He #ifdef CONFIG_CRASH_DUMP
2011c4e2d70SIgor Mammedov &crash_note_cpu_attr_group,
2021c4e2d70SIgor Mammedov #endif
2031c4e2d70SIgor Mammedov NULL
2041c4e2d70SIgor Mammedov };
2051c4e2d70SIgor Mammedov
2061da177e4SLinus Torvalds /*
2079d1fe323SMike Travis * Print cpu online, possible, present, and system maps
2089d1fe323SMike Travis */
209265d2e2eSAndi Kleen
210265d2e2eSAndi Kleen struct cpu_attr {
2118a25a2fdSKay Sievers struct device_attribute attr;
212848e2391SRasmus Villemoes const struct cpumask *const map;
213265d2e2eSAndi Kleen };
214265d2e2eSAndi Kleen
show_cpus_attr(struct device * dev,struct device_attribute * attr,char * buf)2158a25a2fdSKay Sievers static ssize_t show_cpus_attr(struct device *dev,
2168a25a2fdSKay Sievers struct device_attribute *attr,
217265d2e2eSAndi Kleen char *buf)
2189d1fe323SMike Travis {
219265d2e2eSAndi Kleen struct cpu_attr *ca = container_of(attr, struct cpu_attr, attr);
2209d1fe323SMike Travis
221848e2391SRasmus Villemoes return cpumap_print_to_pagebuf(true, buf, ca->map);
2229d1fe323SMike Travis }
2239d1fe323SMike Travis
224265d2e2eSAndi Kleen #define _CPU_ATTR(name, map) \
2258a25a2fdSKay Sievers { __ATTR(name, 0444, show_cpus_attr, NULL), map }
2269d1fe323SMike Travis
2278a25a2fdSKay Sievers /* Keep in sync with cpu_subsys_attrs */
228265d2e2eSAndi Kleen static struct cpu_attr cpu_attrs[] = {
229848e2391SRasmus Villemoes _CPU_ATTR(online, &__cpu_online_mask),
230848e2391SRasmus Villemoes _CPU_ATTR(possible, &__cpu_possible_mask),
231848e2391SRasmus Villemoes _CPU_ATTR(present, &__cpu_present_mask),
232265d2e2eSAndi Kleen };
2339d1fe323SMike Travis
234e057d7aeSMike Travis /*
235e057d7aeSMike Travis * Print values for NR_CPUS and offlined cpus
236e057d7aeSMike Travis */
print_cpus_kernel_max(struct device * dev,struct device_attribute * attr,char * buf)2378a25a2fdSKay Sievers static ssize_t print_cpus_kernel_max(struct device *dev,
2388a25a2fdSKay Sievers struct device_attribute *attr, char *buf)
239e057d7aeSMike Travis {
240aa838896SJoe Perches return sysfs_emit(buf, "%d\n", NR_CPUS - 1);
241e057d7aeSMike Travis }
2428a25a2fdSKay Sievers static DEVICE_ATTR(kernel_max, 0444, print_cpus_kernel_max, NULL);
243e057d7aeSMike Travis
244e057d7aeSMike Travis /* arch-optional setting to enable display of offline cpus >= nr_cpu_ids */
245e057d7aeSMike Travis unsigned int total_cpus;
246e057d7aeSMike Travis
print_cpus_offline(struct device * dev,struct device_attribute * attr,char * buf)2478a25a2fdSKay Sievers static ssize_t print_cpus_offline(struct device *dev,
2488a25a2fdSKay Sievers struct device_attribute *attr, char *buf)
249e057d7aeSMike Travis {
250948b3edbSJoe Perches int len = 0;
251e057d7aeSMike Travis cpumask_var_t offline;
252e057d7aeSMike Travis
253e057d7aeSMike Travis /* display offline cpus < nr_cpu_ids */
254e057d7aeSMike Travis if (!alloc_cpumask_var(&offline, GFP_KERNEL))
255e057d7aeSMike Travis return -ENOMEM;
256cdc6e3d3SJan Beulich cpumask_andnot(offline, cpu_possible_mask, cpu_online_mask);
257948b3edbSJoe Perches len += sysfs_emit_at(buf, len, "%*pbl", cpumask_pr_args(offline));
258e057d7aeSMike Travis free_cpumask_var(offline);
259e057d7aeSMike Travis
260e057d7aeSMike Travis /* display offline cpus >= nr_cpu_ids */
261e057d7aeSMike Travis if (total_cpus && nr_cpu_ids < total_cpus) {
262948b3edbSJoe Perches len += sysfs_emit_at(buf, len, ",");
263e057d7aeSMike Travis
264e057d7aeSMike Travis if (nr_cpu_ids == total_cpus-1)
265948b3edbSJoe Perches len += sysfs_emit_at(buf, len, "%u", nr_cpu_ids);
266e057d7aeSMike Travis else
267948b3edbSJoe Perches len += sysfs_emit_at(buf, len, "%u-%d",
268e057d7aeSMike Travis nr_cpu_ids, total_cpus - 1);
269e057d7aeSMike Travis }
270e057d7aeSMike Travis
271948b3edbSJoe Perches len += sysfs_emit_at(buf, len, "\n");
272948b3edbSJoe Perches
273948b3edbSJoe Perches return len;
274e057d7aeSMike Travis }
2758a25a2fdSKay Sievers static DEVICE_ATTR(offline, 0444, print_cpus_offline, NULL);
276e057d7aeSMike Travis
print_cpus_enabled(struct device * dev,struct device_attribute * attr,char * buf)2774e1a7df4SJames Morse static ssize_t print_cpus_enabled(struct device *dev,
2784e1a7df4SJames Morse struct device_attribute *attr, char *buf)
2794e1a7df4SJames Morse {
2804e1a7df4SJames Morse return sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(cpu_enabled_mask));
2814e1a7df4SJames Morse }
2824e1a7df4SJames Morse static DEVICE_ATTR(enabled, 0444, print_cpus_enabled, NULL);
2834e1a7df4SJames Morse
print_cpus_isolated(struct device * dev,struct device_attribute * attr,char * buf)28459f30abeSRik van Riel static ssize_t print_cpus_isolated(struct device *dev,
28559f30abeSRik van Riel struct device_attribute *attr, char *buf)
28659f30abeSRik van Riel {
287948b3edbSJoe Perches int len;
288edb93821SFrederic Weisbecker cpumask_var_t isolated;
28959f30abeSRik van Riel
290edb93821SFrederic Weisbecker if (!alloc_cpumask_var(&isolated, GFP_KERNEL))
291edb93821SFrederic Weisbecker return -ENOMEM;
292edb93821SFrederic Weisbecker
293edb93821SFrederic Weisbecker cpumask_andnot(isolated, cpu_possible_mask,
29404d4e665SFrederic Weisbecker housekeeping_cpumask(HK_TYPE_DOMAIN));
295948b3edbSJoe Perches len = sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(isolated));
296edb93821SFrederic Weisbecker
297edb93821SFrederic Weisbecker free_cpumask_var(isolated);
29859f30abeSRik van Riel
299948b3edbSJoe Perches return len;
30059f30abeSRik van Riel }
30159f30abeSRik van Riel static DEVICE_ATTR(isolated, 0444, print_cpus_isolated, NULL);
30259f30abeSRik van Riel
3036570a9a1SRik van Riel #ifdef CONFIG_NO_HZ_FULL
print_cpus_nohz_full(struct device * dev,struct device_attribute * attr,char * buf)3046570a9a1SRik van Riel static ssize_t print_cpus_nohz_full(struct device *dev,
3056570a9a1SRik van Riel struct device_attribute *attr, char *buf)
3066570a9a1SRik van Riel {
307aa838896SJoe Perches return sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(tick_nohz_full_mask));
3086570a9a1SRik van Riel }
3096570a9a1SRik van Riel static DEVICE_ATTR(nohz_full, 0444, print_cpus_nohz_full, NULL);
3106570a9a1SRik van Riel #endif
3116570a9a1SRik van Riel
31288a6f899SEric DeVolder #ifdef CONFIG_CRASH_HOTPLUG
crash_hotplug_show(struct device * dev,struct device_attribute * attr,char * buf)31388a6f899SEric DeVolder static ssize_t crash_hotplug_show(struct device *dev,
31488a6f899SEric DeVolder struct device_attribute *attr,
31588a6f899SEric DeVolder char *buf)
31688a6f899SEric DeVolder {
31779365026SSourabh Jain return sysfs_emit(buf, "%d\n", crash_check_hotplug_support());
31888a6f899SEric DeVolder }
319e777798eSPetr Tesarik static DEVICE_ATTR_RO(crash_hotplug);
32088a6f899SEric DeVolder #endif
32188a6f899SEric DeVolder
cpu_device_release(struct device * dev)3222885e25cSGreg Kroah-Hartman static void cpu_device_release(struct device *dev)
3232885e25cSGreg Kroah-Hartman {
3242885e25cSGreg Kroah-Hartman /*
3252885e25cSGreg Kroah-Hartman * This is an empty function to prevent the driver core from spitting a
3262885e25cSGreg Kroah-Hartman * warning at us. Yes, I know this is directly opposite of what the
3272885e25cSGreg Kroah-Hartman * documentation for the driver core and kobjects say, and the author
3282885e25cSGreg Kroah-Hartman * of this code has already been publically ridiculed for doing
3292885e25cSGreg Kroah-Hartman * something as foolish as this. However, at this point in time, it is
3302885e25cSGreg Kroah-Hartman * the only way to handle the issue of statically allocated cpu
3312885e25cSGreg Kroah-Hartman * devices. The different architectures will have their cpu device
3322885e25cSGreg Kroah-Hartman * code reworked to properly handle this in the near future, so this
3332885e25cSGreg Kroah-Hartman * function will then be changed to correctly free up the memory held
3342885e25cSGreg Kroah-Hartman * by the cpu device.
3352885e25cSGreg Kroah-Hartman *
3362885e25cSGreg Kroah-Hartman * Never copy this way of doing things, or you too will be made fun of
33730a4840aSRalf Baechle * on the linux-kernel list, you have been warned.
3382885e25cSGreg Kroah-Hartman */
3392885e25cSGreg Kroah-Hartman }
3402885e25cSGreg Kroah-Hartman
34167bad2fdSArd Biesheuvel #ifdef CONFIG_GENERIC_CPU_AUTOPROBE
print_cpu_modalias(struct device * dev,struct device_attribute * attr,char * buf)34267bad2fdSArd Biesheuvel static ssize_t print_cpu_modalias(struct device *dev,
34367bad2fdSArd Biesheuvel struct device_attribute *attr,
34467bad2fdSArd Biesheuvel char *buf)
34567bad2fdSArd Biesheuvel {
346948b3edbSJoe Perches int len = 0;
34767bad2fdSArd Biesheuvel u32 i;
34867bad2fdSArd Biesheuvel
349948b3edbSJoe Perches len += sysfs_emit_at(buf, len,
350948b3edbSJoe Perches "cpu:type:" CPU_FEATURE_TYPEFMT ":feature:",
35167bad2fdSArd Biesheuvel CPU_FEATURE_TYPEVAL);
35267bad2fdSArd Biesheuvel
35367bad2fdSArd Biesheuvel for (i = 0; i < MAX_CPU_FEATURES; i++)
35467bad2fdSArd Biesheuvel if (cpu_have_feature(i)) {
355948b3edbSJoe Perches if (len + sizeof(",XXXX\n") >= PAGE_SIZE) {
35667bad2fdSArd Biesheuvel WARN(1, "CPU features overflow page\n");
35767bad2fdSArd Biesheuvel break;
35867bad2fdSArd Biesheuvel }
359948b3edbSJoe Perches len += sysfs_emit_at(buf, len, ",%04X", i);
36067bad2fdSArd Biesheuvel }
361948b3edbSJoe Perches len += sysfs_emit_at(buf, len, "\n");
362948b3edbSJoe Perches return len;
36367bad2fdSArd Biesheuvel }
36467bad2fdSArd Biesheuvel
cpu_uevent(const struct device * dev,struct kobj_uevent_env * env)3652a81ada3SGreg Kroah-Hartman static int cpu_uevent(const struct device *dev, struct kobj_uevent_env *env)
36667bad2fdSArd Biesheuvel {
36767bad2fdSArd Biesheuvel char *buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
36867bad2fdSArd Biesheuvel if (buf) {
36967bad2fdSArd Biesheuvel print_cpu_modalias(NULL, NULL, buf);
37067bad2fdSArd Biesheuvel add_uevent_var(env, "MODALIAS=%s", buf);
37167bad2fdSArd Biesheuvel kfree(buf);
37267bad2fdSArd Biesheuvel }
37367bad2fdSArd Biesheuvel return 0;
37467bad2fdSArd Biesheuvel }
37567bad2fdSArd Biesheuvel #endif
37667bad2fdSArd Biesheuvel
3773a480d4bSGreg Kroah-Hartman const struct bus_type cpu_subsys = {
3782bc19066SGreg Kroah-Hartman .name = "cpu",
3792bc19066SGreg Kroah-Hartman .dev_name = "cpu",
3802bc19066SGreg Kroah-Hartman .match = cpu_subsys_match,
3812bc19066SGreg Kroah-Hartman #ifdef CONFIG_HOTPLUG_CPU
3822bc19066SGreg Kroah-Hartman .online = cpu_subsys_online,
3832bc19066SGreg Kroah-Hartman .offline = cpu_subsys_offline,
3842bc19066SGreg Kroah-Hartman #endif
3852bc19066SGreg Kroah-Hartman #ifdef CONFIG_GENERIC_CPU_AUTOPROBE
3862bc19066SGreg Kroah-Hartman .uevent = cpu_uevent,
3872bc19066SGreg Kroah-Hartman #endif
3882bc19066SGreg Kroah-Hartman };
3892bc19066SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(cpu_subsys);
3902bc19066SGreg Kroah-Hartman
3919d1fe323SMike Travis /*
392405ae7d3SRobert P. J. Day * register_cpu - Setup a sysfs device for a CPU.
39372486f1fSSiddha, Suresh B * @cpu - cpu->hotpluggable field set to 1 will generate a control file in
39472486f1fSSiddha, Suresh B * sysfs for this CPU.
3951da177e4SLinus Torvalds * @num - CPU number to use when creating the device.
3961da177e4SLinus Torvalds *
3971da177e4SLinus Torvalds * Initialize and register the CPU device.
3981da177e4SLinus Torvalds */
register_cpu(struct cpu * cpu,int num)399a83048ebSPaul Gortmaker int register_cpu(struct cpu *cpu, int num)
4001da177e4SLinus Torvalds {
4011da177e4SLinus Torvalds int error;
4028a25a2fdSKay Sievers
4031da177e4SLinus Torvalds cpu->node_id = cpu_to_node(num);
40429bb5d4fSGreg Kroah-Hartman memset(&cpu->dev, 0x00, sizeof(struct device));
4058a25a2fdSKay Sievers cpu->dev.id = num;
4068a25a2fdSKay Sievers cpu->dev.bus = &cpu_subsys;
4072885e25cSGreg Kroah-Hartman cpu->dev.release = cpu_device_release;
4080902a904SRafael J. Wysocki cpu->dev.offline_disabled = !cpu->hotpluggable;
4091001b4d4SToshi Kani cpu->dev.offline = !cpu_online(num);
410f86e4718SSudeep KarkadaNagesha cpu->dev.of_node = of_get_cpu_node(num, NULL);
411c055da9fSIgor Mammedov cpu->dev.groups = common_cpu_attr_groups;
4121c4e2d70SIgor Mammedov if (cpu->hotpluggable)
4131c4e2d70SIgor Mammedov cpu->dev.groups = hotplugable_cpu_attr_groups;
4148a25a2fdSKay Sievers error = device_register(&cpu->dev);
4153aaba245SArvind Yadav if (error) {
4163aaba245SArvind Yadav put_device(&cpu->dev);
41759fffa34SAlex Shi return error;
4183aaba245SArvind Yadav }
41959fffa34SAlex Shi
4208a25a2fdSKay Sievers per_cpu(cpu_sys_devices, num) = &cpu->dev;
42176b67ed9SKAMEZAWA Hiroyuki register_cpu_under_node(num, cpu_to_node(num));
4220759e80bSRafael J. Wysocki dev_pm_qos_expose_latency_limit(&cpu->dev,
4230759e80bSRafael J. Wysocki PM_QOS_RESUME_LATENCY_NO_CONSTRAINT);
4244e1a7df4SJames Morse set_cpu_enabled(num, true);
42551be5606SVivek Goyal
42659fffa34SAlex Shi return 0;
4271da177e4SLinus Torvalds }
4281da177e4SLinus Torvalds
get_cpu_device(unsigned int cpu)429e7deeb9dSJinchao Wang struct device *get_cpu_device(unsigned int cpu)
430ad74557aSAshok Raj {
431e37d05daSMike Travis if (cpu < nr_cpu_ids && cpu_possible(cpu))
432e37d05daSMike Travis return per_cpu(cpu_sys_devices, cpu);
433ad74557aSAshok Raj else
434ad74557aSAshok Raj return NULL;
435ad74557aSAshok Raj }
4368a25a2fdSKay Sievers EXPORT_SYMBOL_GPL(get_cpu_device);
4378a25a2fdSKay Sievers
device_create_release(struct device * dev)4383d52943bSSudeep Holla static void device_create_release(struct device *dev)
4393d52943bSSudeep Holla {
4403d52943bSSudeep Holla kfree(dev);
4413d52943bSSudeep Holla }
4423d52943bSSudeep Holla
443fa548d79SMathieu Malaterre __printf(4, 0)
4443d52943bSSudeep Holla static struct device *
__cpu_device_create(struct device * parent,void * drvdata,const struct attribute_group ** groups,const char * fmt,va_list args)4453d52943bSSudeep Holla __cpu_device_create(struct device *parent, void *drvdata,
4463d52943bSSudeep Holla const struct attribute_group **groups,
4473d52943bSSudeep Holla const char *fmt, va_list args)
4483d52943bSSudeep Holla {
4493d52943bSSudeep Holla struct device *dev = NULL;
4506b72cf12SColin Ian King int retval = -ENOMEM;
4513d52943bSSudeep Holla
4523d52943bSSudeep Holla dev = kzalloc(sizeof(*dev), GFP_KERNEL);
4536b72cf12SColin Ian King if (!dev)
4543d52943bSSudeep Holla goto error;
4553d52943bSSudeep Holla
4563d52943bSSudeep Holla device_initialize(dev);
4573d52943bSSudeep Holla dev->parent = parent;
4583d52943bSSudeep Holla dev->groups = groups;
4593d52943bSSudeep Holla dev->release = device_create_release;
46085945c28SSudeep Holla device_set_pm_not_required(dev);
4613d52943bSSudeep Holla dev_set_drvdata(dev, drvdata);
4623d52943bSSudeep Holla
4633d52943bSSudeep Holla retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
4643d52943bSSudeep Holla if (retval)
4653d52943bSSudeep Holla goto error;
4663d52943bSSudeep Holla
4673d52943bSSudeep Holla retval = device_add(dev);
4683d52943bSSudeep Holla if (retval)
4693d52943bSSudeep Holla goto error;
4703d52943bSSudeep Holla
4713d52943bSSudeep Holla return dev;
4723d52943bSSudeep Holla
4733d52943bSSudeep Holla error:
4743d52943bSSudeep Holla put_device(dev);
4753d52943bSSudeep Holla return ERR_PTR(retval);
4763d52943bSSudeep Holla }
4773d52943bSSudeep Holla
cpu_device_create(struct device * parent,void * drvdata,const struct attribute_group ** groups,const char * fmt,...)4783d52943bSSudeep Holla struct device *cpu_device_create(struct device *parent, void *drvdata,
4793d52943bSSudeep Holla const struct attribute_group **groups,
4803d52943bSSudeep Holla const char *fmt, ...)
4813d52943bSSudeep Holla {
4823d52943bSSudeep Holla va_list vargs;
4833d52943bSSudeep Holla struct device *dev;
4843d52943bSSudeep Holla
4853d52943bSSudeep Holla va_start(vargs, fmt);
4863d52943bSSudeep Holla dev = __cpu_device_create(parent, drvdata, groups, fmt, vargs);
4873d52943bSSudeep Holla va_end(vargs);
4883d52943bSSudeep Holla return dev;
4893d52943bSSudeep Holla }
4903d52943bSSudeep Holla EXPORT_SYMBOL_GPL(cpu_device_create);
4913d52943bSSudeep Holla
4922b9c1f03SArd Biesheuvel #ifdef CONFIG_GENERIC_CPU_AUTOPROBE
49367bad2fdSArd Biesheuvel static DEVICE_ATTR(modalias, 0444, print_cpu_modalias, NULL);
494fad12ac8SThomas Renninger #endif
495fad12ac8SThomas Renninger
4968a25a2fdSKay Sievers static struct attribute *cpu_root_attrs[] = {
4978a25a2fdSKay Sievers #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
4988a25a2fdSKay Sievers &dev_attr_probe.attr,
4998a25a2fdSKay Sievers &dev_attr_release.attr,
5008a25a2fdSKay Sievers #endif
5018a25a2fdSKay Sievers &cpu_attrs[0].attr.attr,
5028a25a2fdSKay Sievers &cpu_attrs[1].attr.attr,
5038a25a2fdSKay Sievers &cpu_attrs[2].attr.attr,
5048a25a2fdSKay Sievers &dev_attr_kernel_max.attr,
5058a25a2fdSKay Sievers &dev_attr_offline.attr,
5064e1a7df4SJames Morse &dev_attr_enabled.attr,
50759f30abeSRik van Riel &dev_attr_isolated.attr,
5086570a9a1SRik van Riel #ifdef CONFIG_NO_HZ_FULL
5096570a9a1SRik van Riel &dev_attr_nohz_full.attr,
5106570a9a1SRik van Riel #endif
51188a6f899SEric DeVolder #ifdef CONFIG_CRASH_HOTPLUG
51288a6f899SEric DeVolder &dev_attr_crash_hotplug.attr,
51388a6f899SEric DeVolder #endif
5142b9c1f03SArd Biesheuvel #ifdef CONFIG_GENERIC_CPU_AUTOPROBE
515fad12ac8SThomas Renninger &dev_attr_modalias.attr,
516fad12ac8SThomas Renninger #endif
5178a25a2fdSKay Sievers NULL
5188a25a2fdSKay Sievers };
5198a25a2fdSKay Sievers
5205a576764SRikard Falkeborn static const struct attribute_group cpu_root_attr_group = {
5218a25a2fdSKay Sievers .attrs = cpu_root_attrs,
5228a25a2fdSKay Sievers };
5238a25a2fdSKay Sievers
5248a25a2fdSKay Sievers static const struct attribute_group *cpu_root_attr_groups[] = {
5258a25a2fdSKay Sievers &cpu_root_attr_group,
5268a25a2fdSKay Sievers NULL,
5278a25a2fdSKay Sievers };
5281da177e4SLinus Torvalds
cpu_is_hotpluggable(unsigned int cpu)529e7deeb9dSJinchao Wang bool cpu_is_hotpluggable(unsigned int cpu)
5302987557fSJosh Triplett {
5317affca35SLinus Torvalds struct device *dev = get_cpu_device(cpu);
53258d76682SJoel Fernandes (Google) return dev && container_of(dev, struct cpu, dev)->hotpluggable
53358d76682SJoel Fernandes (Google) && tick_nohz_cpu_hotpluggable(cpu);
5342987557fSJosh Triplett }
5352987557fSJosh Triplett EXPORT_SYMBOL_GPL(cpu_is_hotpluggable);
5362987557fSJosh Triplett
5379f13a1fdSBen Hutchings #ifdef CONFIG_GENERIC_CPU_DEVICES
5380949dd96SJames Morse DEFINE_PER_CPU(struct cpu, cpu_devices);
5390949dd96SJames Morse
arch_cpu_is_hotpluggable(int cpu)540bb5e44fbSRussell King (Oracle) bool __weak arch_cpu_is_hotpluggable(int cpu)
541bb5e44fbSRussell King (Oracle) {
542bb5e44fbSRussell King (Oracle) return false;
543bb5e44fbSRussell King (Oracle) }
544bb5e44fbSRussell King (Oracle)
arch_register_cpu(int cpu)5450949dd96SJames Morse int __weak arch_register_cpu(int cpu)
5460949dd96SJames Morse {
547bb5e44fbSRussell King (Oracle) struct cpu *c = &per_cpu(cpu_devices, cpu);
548bb5e44fbSRussell King (Oracle)
549bb5e44fbSRussell King (Oracle) c->hotpluggable = arch_cpu_is_hotpluggable(cpu);
550bb5e44fbSRussell King (Oracle)
551bb5e44fbSRussell King (Oracle) return register_cpu(c, cpu);
5520949dd96SJames Morse }
553866ec300SJames Morse
554866ec300SJames Morse #ifdef CONFIG_HOTPLUG_CPU
arch_unregister_cpu(int num)555866ec300SJames Morse void __weak arch_unregister_cpu(int num)
556866ec300SJames Morse {
557866ec300SJames Morse unregister_cpu(&per_cpu(cpu_devices, num));
558866ec300SJames Morse }
559866ec300SJames Morse #endif /* CONFIG_HOTPLUG_CPU */
560866ec300SJames Morse #endif /* CONFIG_GENERIC_CPU_DEVICES */
5619f13a1fdSBen Hutchings
cpu_dev_register_generic(void)5629f13a1fdSBen Hutchings static void __init cpu_dev_register_generic(void)
5639f13a1fdSBen Hutchings {
564ca00f7d9SJames Morse int i, ret;
5659f13a1fdSBen Hutchings
5660949dd96SJames Morse if (!IS_ENABLED(CONFIG_GENERIC_CPU_DEVICES))
5670949dd96SJames Morse return;
5680949dd96SJames Morse
569b0c69e12SJames Morse for_each_present_cpu(i) {
570ca00f7d9SJames Morse ret = arch_register_cpu(i);
571d830ef3aSJonathan Cameron if (ret && ret != -EPROBE_DEFER)
572ca00f7d9SJames Morse pr_warn("register_cpu %d failed (%d)\n", i, ret);
5739f13a1fdSBen Hutchings }
5749f13a1fdSBen Hutchings }
5759f13a1fdSBen Hutchings
57687590ce6SThomas Gleixner #ifdef CONFIG_GENERIC_CPU_VULNERABILITIES
cpu_show_not_affected(struct device * dev,struct device_attribute * attr,char * buf)5776524c798SBorislav Petkov (AMD) static ssize_t cpu_show_not_affected(struct device *dev,
57887590ce6SThomas Gleixner struct device_attribute *attr, char *buf)
57987590ce6SThomas Gleixner {
580aa838896SJoe Perches return sysfs_emit(buf, "Not affected\n");
58187590ce6SThomas Gleixner }
58287590ce6SThomas Gleixner
5830fddfe33SBorislav Petkov (AMD) #define CPU_SHOW_VULN_FALLBACK(func) \
5840fddfe33SBorislav Petkov (AMD) ssize_t cpu_show_##func(struct device *, \
5850fddfe33SBorislav Petkov (AMD) struct device_attribute *, char *) \
5860fddfe33SBorislav Petkov (AMD) __attribute__((weak, alias("cpu_show_not_affected")))
58787590ce6SThomas Gleixner
5880fddfe33SBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(meltdown);
5890fddfe33SBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(spectre_v1);
5900fddfe33SBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(spectre_v2);
5910fddfe33SBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(spec_store_bypass);
5920fddfe33SBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(l1tf);
5930fddfe33SBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(mds);
5940fddfe33SBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(tsx_async_abort);
5950fddfe33SBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(itlb_multihit);
5960fddfe33SBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(srbds);
5970fddfe33SBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(mmio_stale_data);
5980fddfe33SBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(retbleed);
5990fddfe33SBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(spec_rstack_overflow);
6003477144cSBorislav Petkov (AMD) CPU_SHOW_VULN_FALLBACK(gds);
6018076fcdeSPawan Gupta CPU_SHOW_VULN_FALLBACK(reg_file_data_sampling);
6024bf97069SCharlie Jenkins CPU_SHOW_VULN_FALLBACK(ghostwrite);
603*f4818881SPawan Gupta CPU_SHOW_VULN_FALLBACK(indirect_target_selection);
604fb3bd914SBorislav Petkov (AMD)
60587590ce6SThomas Gleixner static DEVICE_ATTR(meltdown, 0444, cpu_show_meltdown, NULL);
60687590ce6SThomas Gleixner static DEVICE_ATTR(spectre_v1, 0444, cpu_show_spectre_v1, NULL);
60787590ce6SThomas Gleixner static DEVICE_ATTR(spectre_v2, 0444, cpu_show_spectre_v2, NULL);
608c456442cSKonrad Rzeszutek Wilk static DEVICE_ATTR(spec_store_bypass, 0444, cpu_show_spec_store_bypass, NULL);
60917dbca11SAndi Kleen static DEVICE_ATTR(l1tf, 0444, cpu_show_l1tf, NULL);
6108a4b06d3SThomas Gleixner static DEVICE_ATTR(mds, 0444, cpu_show_mds, NULL);
6116608b45aSPawan Gupta static DEVICE_ATTR(tsx_async_abort, 0444, cpu_show_tsx_async_abort, NULL);
612db4d30fbSVineela Tummalapalli static DEVICE_ATTR(itlb_multihit, 0444, cpu_show_itlb_multihit, NULL);
6137e5b3c26SMark Gross static DEVICE_ATTR(srbds, 0444, cpu_show_srbds, NULL);
6148d50cdf8SPawan Gupta static DEVICE_ATTR(mmio_stale_data, 0444, cpu_show_mmio_stale_data, NULL);
6156b80b59bSAlexandre Chartre static DEVICE_ATTR(retbleed, 0444, cpu_show_retbleed, NULL);
616fb3bd914SBorislav Petkov (AMD) static DEVICE_ATTR(spec_rstack_overflow, 0444, cpu_show_spec_rstack_overflow, NULL);
6173477144cSBorislav Petkov (AMD) static DEVICE_ATTR(gather_data_sampling, 0444, cpu_show_gds, NULL);
6188076fcdeSPawan Gupta static DEVICE_ATTR(reg_file_data_sampling, 0444, cpu_show_reg_file_data_sampling, NULL);
6194bf97069SCharlie Jenkins static DEVICE_ATTR(ghostwrite, 0444, cpu_show_ghostwrite, NULL);
620*f4818881SPawan Gupta static DEVICE_ATTR(indirect_target_selection, 0444, cpu_show_indirect_target_selection, NULL);
62187590ce6SThomas Gleixner
62287590ce6SThomas Gleixner static struct attribute *cpu_root_vulnerabilities_attrs[] = {
62387590ce6SThomas Gleixner &dev_attr_meltdown.attr,
62487590ce6SThomas Gleixner &dev_attr_spectre_v1.attr,
62587590ce6SThomas Gleixner &dev_attr_spectre_v2.attr,
626c456442cSKonrad Rzeszutek Wilk &dev_attr_spec_store_bypass.attr,
62717dbca11SAndi Kleen &dev_attr_l1tf.attr,
6288a4b06d3SThomas Gleixner &dev_attr_mds.attr,
6296608b45aSPawan Gupta &dev_attr_tsx_async_abort.attr,
630db4d30fbSVineela Tummalapalli &dev_attr_itlb_multihit.attr,
6317e5b3c26SMark Gross &dev_attr_srbds.attr,
6328d50cdf8SPawan Gupta &dev_attr_mmio_stale_data.attr,
6336b80b59bSAlexandre Chartre &dev_attr_retbleed.attr,
634fb3bd914SBorislav Petkov (AMD) &dev_attr_spec_rstack_overflow.attr,
6358974eb58SDaniel Sneddon &dev_attr_gather_data_sampling.attr,
6368076fcdeSPawan Gupta &dev_attr_reg_file_data_sampling.attr,
6374bf97069SCharlie Jenkins &dev_attr_ghostwrite.attr,
638*f4818881SPawan Gupta &dev_attr_indirect_target_selection.attr,
63987590ce6SThomas Gleixner NULL
64087590ce6SThomas Gleixner };
64187590ce6SThomas Gleixner
64287590ce6SThomas Gleixner static const struct attribute_group cpu_root_vulnerabilities_group = {
64387590ce6SThomas Gleixner .name = "vulnerabilities",
64487590ce6SThomas Gleixner .attrs = cpu_root_vulnerabilities_attrs,
64587590ce6SThomas Gleixner };
64687590ce6SThomas Gleixner
cpu_register_vulnerabilities(void)64787590ce6SThomas Gleixner static void __init cpu_register_vulnerabilities(void)
64887590ce6SThomas Gleixner {
6498c99377eSGreg Kroah-Hartman struct device *dev = bus_get_dev_root(&cpu_subsys);
6508c99377eSGreg Kroah-Hartman
6518c99377eSGreg Kroah-Hartman if (dev) {
6528c99377eSGreg Kroah-Hartman if (sysfs_create_group(&dev->kobj, &cpu_root_vulnerabilities_group))
65387590ce6SThomas Gleixner pr_err("Unable to register CPU vulnerabilities\n");
6548c99377eSGreg Kroah-Hartman put_device(dev);
6558c99377eSGreg Kroah-Hartman }
65687590ce6SThomas Gleixner }
65787590ce6SThomas Gleixner
65887590ce6SThomas Gleixner #else
cpu_register_vulnerabilities(void)65987590ce6SThomas Gleixner static inline void cpu_register_vulnerabilities(void) { }
66087590ce6SThomas Gleixner #endif
66187590ce6SThomas Gleixner
cpu_dev_init(void)662024f7846SBen Hutchings void __init cpu_dev_init(void)
6631da177e4SLinus Torvalds {
664024f7846SBen Hutchings if (subsys_system_register(&cpu_subsys, cpu_root_attr_groups))
665024f7846SBen Hutchings panic("Failed to register CPU subsystem");
6665c45bf27SSiddha, Suresh B
6679f13a1fdSBen Hutchings cpu_dev_register_generic();
66887590ce6SThomas Gleixner cpu_register_vulnerabilities();
6691da177e4SLinus Torvalds }
670