blob: 5ef14db9790478b81979367fecdd94c4eda6f02e [file] [log] [blame]
Greg Kroah-Hartman989d42e2017-11-07 17:30:07 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Kay Sievers8a25a2f2011-12-21 14:29:42 -08003 * CPU subsystem support
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
Ben Hutchings024f7842012-01-10 02:59:49 +00006#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/module.h>
8#include <linux/init.h>
Al Virof6a57032006-10-18 01:47:25 -04009#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/cpu.h>
11#include <linux/topology.h>
12#include <linux/device.h>
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -070013#include <linux/node.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/gfp.h>
Thomas Renningerfad12ac2012-01-26 00:09:14 +010015#include <linux/slab.h>
Ben Hutchings9f13a1f2012-01-10 03:04:32 +000016#include <linux/percpu.h>
Rafael J. Wysockiac212b62013-05-03 00:26:22 +020017#include <linux/acpi.h>
Sudeep KarkadaNageshaf86e4712013-06-17 12:58:45 +010018#include <linux/of.h>
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +010019#include <linux/cpufeature.h>
Rik van Riel6570a9a2015-04-24 15:24:28 -040020#include <linux/tick.h>
Alex Shi37efa4b2017-01-12 21:27:03 +080021#include <linux/pm_qos.h>
Frederic Weisbeckeredb93822017-10-27 04:42:37 +020022#include <linux/sched/isolation.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010024#include "base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Kay Sievers8a25a2f2011-12-21 14:29:42 -080026static DEFINE_PER_CPU(struct device *, cpu_sys_devices);
Ashok Rajad745572005-10-30 14:59:49 -080027
Rafael J. Wysockiac212b62013-05-03 00:26:22 +020028static int cpu_subsys_match(struct device *dev, struct device_driver *drv)
29{
30 /* ACPI style match is the only one that may succeed. */
31 if (acpi_driver_match_device(dev, drv))
32 return 1;
33
34 return 0;
35}
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#ifdef CONFIG_HOTPLUG_CPU
Yasuaki Ishimatsu34640462013-04-29 15:08:50 -070038static void change_cpu_under_node(struct cpu *cpu,
39 unsigned int from_nid, unsigned int to_nid)
40{
41 int cpuid = cpu->dev.id;
42 unregister_cpu_under_node(cpuid, from_nid);
43 register_cpu_under_node(cpuid, to_nid);
44 cpu->node_id = to_nid;
45}
46
Mathias Krauseeda58672015-07-19 20:06:21 +020047static int cpu_subsys_online(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Kay Sievers8a25a2f2011-12-21 14:29:42 -080049 struct cpu *cpu = container_of(dev, struct cpu, dev);
Rafael J. Wysocki0902a902013-05-03 00:25:49 +020050 int cpuid = dev->id;
Yasuaki Ishimatsu34640462013-04-29 15:08:50 -070051 int from_nid, to_nid;
Toshi Kani6dedcca2013-09-25 15:08:27 -060052 int ret;
Yasuaki Ishimatsu34640462013-04-29 15:08:50 -070053
Rafael J. Wysocki0902a902013-05-03 00:25:49 +020054 from_nid = cpu_to_node(cpuid);
Rafael J. Wysockic7991b02013-08-13 02:39:30 +020055 if (from_nid == NUMA_NO_NODE)
Toshi Kani6dedcca2013-09-25 15:08:27 -060056 return -ENODEV;
Rafael J. Wysockic7991b02013-08-13 02:39:30 +020057
Qais Yousef33c37362020-03-23 13:51:10 +000058 ret = cpu_device_up(dev);
Rafael J. Wysocki0902a902013-05-03 00:25:49 +020059 /*
60 * When hot adding memory to memoryless node and enabling a cpu
61 * on the node, node number of the cpu may internally change.
62 */
63 to_nid = cpu_to_node(cpuid);
64 if (from_nid != to_nid)
65 change_cpu_under_node(cpu, from_nid, to_nid);
Yasuaki Ishimatsu34640462013-04-29 15:08:50 -070066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 return ret;
68}
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Rafael J. Wysocki0902a902013-05-03 00:25:49 +020070static int cpu_subsys_offline(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Qais Yousef33c37362020-03-23 13:51:10 +000072 return cpu_device_down(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
Igor Mammedov1c4e2d72013-05-14 16:46:07 +020074
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -070075void unregister_cpu(struct cpu *cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Kay Sievers8a25a2f2011-12-21 14:29:42 -080077 int logical_cpu = cpu->dev.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -070079 unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu));
80
Kay Sievers8a25a2f2011-12-21 14:29:42 -080081 device_unregister(&cpu->dev);
Mike Travise37d05d2008-05-01 04:35:16 -070082 per_cpu(cpu_sys_devices, logical_cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return;
84}
Nathan Fontenot12633e82009-11-25 17:23:25 +000085
86#ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
Kay Sievers8a25a2f2011-12-21 14:29:42 -080087static ssize_t cpu_probe_store(struct device *dev,
88 struct device_attribute *attr,
Stephen Rothwell67fc2332010-03-16 10:33:32 +110089 const char *buf,
Nathan Fontenot12633e82009-11-25 17:23:25 +000090 size_t count)
91{
Toshi Kani574b8512013-08-29 18:22:07 -060092 ssize_t cnt;
93 int ret;
94
95 ret = lock_device_hotplug_sysfs();
96 if (ret)
97 return ret;
98
99 cnt = arch_cpu_probe(buf, count);
100
101 unlock_device_hotplug();
102 return cnt;
Nathan Fontenot12633e82009-11-25 17:23:25 +0000103}
104
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800105static ssize_t cpu_release_store(struct device *dev,
106 struct device_attribute *attr,
Stephen Rothwell67fc2332010-03-16 10:33:32 +1100107 const char *buf,
Nathan Fontenot12633e82009-11-25 17:23:25 +0000108 size_t count)
109{
Toshi Kani574b8512013-08-29 18:22:07 -0600110 ssize_t cnt;
111 int ret;
112
113 ret = lock_device_hotplug_sysfs();
114 if (ret)
115 return ret;
116
117 cnt = arch_cpu_release(buf, count);
118
119 unlock_device_hotplug();
120 return cnt;
Nathan Fontenot12633e82009-11-25 17:23:25 +0000121}
122
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800123static DEVICE_ATTR(probe, S_IWUSR, NULL, cpu_probe_store);
124static DEVICE_ATTR(release, S_IWUSR, NULL, cpu_release_store);
Nathan Fontenot12633e82009-11-25 17:23:25 +0000125#endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#endif /* CONFIG_HOTPLUG_CPU */
127
Rafael J. Wysocki0902a902013-05-03 00:25:49 +0200128struct bus_type cpu_subsys = {
129 .name = "cpu",
130 .dev_name = "cpu",
Rafael J. Wysockiac212b62013-05-03 00:26:22 +0200131 .match = cpu_subsys_match,
Rafael J. Wysocki0902a902013-05-03 00:25:49 +0200132#ifdef CONFIG_HOTPLUG_CPU
133 .online = cpu_subsys_online,
134 .offline = cpu_subsys_offline,
135#endif
136};
137EXPORT_SYMBOL_GPL(cpu_subsys);
138
Vivek Goyal51be5602006-01-09 20:51:42 -0800139#ifdef CONFIG_KEXEC
140#include <linux/kexec.h>
141
Joe Perches948b3ed2020-09-16 13:40:42 -0700142static ssize_t crash_notes_show(struct device *dev,
143 struct device_attribute *attr,
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200144 char *buf)
Vivek Goyal51be5602006-01-09 20:51:42 -0800145{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800146 struct cpu *cpu = container_of(dev, struct cpu, dev);
Vivek Goyal51be5602006-01-09 20:51:42 -0800147 unsigned long long addr;
148 int cpunum;
149
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800150 cpunum = cpu->dev.id;
Vivek Goyal51be5602006-01-09 20:51:42 -0800151
152 /*
153 * Might be reading other cpu's data based on which cpu read thread
154 * has been scheduled. But cpu data (memory) is allocated once during
155 * boot up and this data does not change there after. Hence this
156 * operation should be safe. No locking required.
157 */
Vivek Goyal3b034b02009-11-24 15:50:03 +0900158 addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpunum));
Zhang Yanfeieca45492013-03-28 16:15:35 +0800159
Joe Perches948b3ed2020-09-16 13:40:42 -0700160 return sysfs_emit(buf, "%llx\n", addr);
161}
162static DEVICE_ATTR_ADMIN_RO(crash_notes);
163
164static ssize_t crash_notes_size_show(struct device *dev,
Zhang Yanfeieca45492013-03-28 16:15:35 +0800165 struct device_attribute *attr,
166 char *buf)
167{
Joe Perches948b3ed2020-09-16 13:40:42 -0700168 return sysfs_emit(buf, "%zu\n", sizeof(note_buf_t));
Zhang Yanfeieca45492013-03-28 16:15:35 +0800169}
Joe Perches948b3ed2020-09-16 13:40:42 -0700170static DEVICE_ATTR_ADMIN_RO(crash_notes_size);
Igor Mammedovc055da92013-05-14 16:46:06 +0200171
172static struct attribute *crash_note_cpu_attrs[] = {
173 &dev_attr_crash_notes.attr,
174 &dev_attr_crash_notes_size.attr,
175 NULL
176};
177
Rikard Falkeborn5a576762021-05-28 23:34:08 +0200178static const struct attribute_group crash_note_cpu_attr_group = {
Igor Mammedovc055da92013-05-14 16:46:06 +0200179 .attrs = crash_note_cpu_attrs,
180};
Vivek Goyal51be5602006-01-09 20:51:42 -0800181#endif
182
Igor Mammedovc055da92013-05-14 16:46:06 +0200183static const struct attribute_group *common_cpu_attr_groups[] = {
184#ifdef CONFIG_KEXEC
185 &crash_note_cpu_attr_group,
186#endif
187 NULL
188};
189
Igor Mammedov1c4e2d72013-05-14 16:46:07 +0200190static const struct attribute_group *hotplugable_cpu_attr_groups[] = {
191#ifdef CONFIG_KEXEC
192 &crash_note_cpu_attr_group,
193#endif
Igor Mammedov1c4e2d72013-05-14 16:46:07 +0200194 NULL
195};
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197/*
Mike Travis9d1fe3232008-04-08 11:43:04 -0700198 * Print cpu online, possible, present, and system maps
199 */
Andi Kleen265d2e22010-01-05 12:48:00 +0100200
201struct cpu_attr {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800202 struct device_attribute attr;
Rasmus Villemoes848e2392016-01-20 15:00:22 -0800203 const struct cpumask *const map;
Andi Kleen265d2e22010-01-05 12:48:00 +0100204};
205
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800206static ssize_t show_cpus_attr(struct device *dev,
207 struct device_attribute *attr,
Andi Kleen265d2e22010-01-05 12:48:00 +0100208 char *buf)
Mike Travis9d1fe3232008-04-08 11:43:04 -0700209{
Andi Kleen265d2e22010-01-05 12:48:00 +0100210 struct cpu_attr *ca = container_of(attr, struct cpu_attr, attr);
Mike Travis9d1fe3232008-04-08 11:43:04 -0700211
Rasmus Villemoes848e2392016-01-20 15:00:22 -0800212 return cpumap_print_to_pagebuf(true, buf, ca->map);
Mike Travis9d1fe3232008-04-08 11:43:04 -0700213}
214
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800215#define _CPU_ATTR(name, map) \
216 { __ATTR(name, 0444, show_cpus_attr, NULL), map }
Mike Travis9d1fe3232008-04-08 11:43:04 -0700217
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800218/* Keep in sync with cpu_subsys_attrs */
Andi Kleen265d2e22010-01-05 12:48:00 +0100219static struct cpu_attr cpu_attrs[] = {
Rasmus Villemoes848e2392016-01-20 15:00:22 -0800220 _CPU_ATTR(online, &__cpu_online_mask),
221 _CPU_ATTR(possible, &__cpu_possible_mask),
222 _CPU_ATTR(present, &__cpu_present_mask),
Andi Kleen265d2e22010-01-05 12:48:00 +0100223};
Mike Travis9d1fe3232008-04-08 11:43:04 -0700224
Mike Travise057d7a2008-12-15 20:26:48 -0800225/*
226 * Print values for NR_CPUS and offlined cpus
227 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800228static ssize_t print_cpus_kernel_max(struct device *dev,
229 struct device_attribute *attr, char *buf)
Mike Travise057d7a2008-12-15 20:26:48 -0800230{
Joe Perchesaa838892020-09-16 13:40:39 -0700231 return sysfs_emit(buf, "%d\n", NR_CPUS - 1);
Mike Travise057d7a2008-12-15 20:26:48 -0800232}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800233static DEVICE_ATTR(kernel_max, 0444, print_cpus_kernel_max, NULL);
Mike Travise057d7a2008-12-15 20:26:48 -0800234
235/* arch-optional setting to enable display of offline cpus >= nr_cpu_ids */
236unsigned int total_cpus;
237
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800238static ssize_t print_cpus_offline(struct device *dev,
239 struct device_attribute *attr, char *buf)
Mike Travise057d7a2008-12-15 20:26:48 -0800240{
Joe Perches948b3ed2020-09-16 13:40:42 -0700241 int len = 0;
Mike Travise057d7a2008-12-15 20:26:48 -0800242 cpumask_var_t offline;
243
244 /* display offline cpus < nr_cpu_ids */
245 if (!alloc_cpumask_var(&offline, GFP_KERNEL))
246 return -ENOMEM;
Jan Beulichcdc6e3d2010-04-27 14:01:20 -0700247 cpumask_andnot(offline, cpu_possible_mask, cpu_online_mask);
Joe Perches948b3ed2020-09-16 13:40:42 -0700248 len += sysfs_emit_at(buf, len, "%*pbl", cpumask_pr_args(offline));
Mike Travise057d7a2008-12-15 20:26:48 -0800249 free_cpumask_var(offline);
250
251 /* display offline cpus >= nr_cpu_ids */
252 if (total_cpus && nr_cpu_ids < total_cpus) {
Joe Perches948b3ed2020-09-16 13:40:42 -0700253 len += sysfs_emit_at(buf, len, ",");
Mike Travise057d7a2008-12-15 20:26:48 -0800254
255 if (nr_cpu_ids == total_cpus-1)
Joe Perches948b3ed2020-09-16 13:40:42 -0700256 len += sysfs_emit_at(buf, len, "%u", nr_cpu_ids);
Mike Travise057d7a2008-12-15 20:26:48 -0800257 else
Joe Perches948b3ed2020-09-16 13:40:42 -0700258 len += sysfs_emit_at(buf, len, "%u-%d",
259 nr_cpu_ids, total_cpus - 1);
Mike Travise057d7a2008-12-15 20:26:48 -0800260 }
261
Joe Perches948b3ed2020-09-16 13:40:42 -0700262 len += sysfs_emit_at(buf, len, "\n");
263
264 return len;
Mike Travise057d7a2008-12-15 20:26:48 -0800265}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800266static DEVICE_ATTR(offline, 0444, print_cpus_offline, NULL);
Mike Travise057d7a2008-12-15 20:26:48 -0800267
Rik van Riel59f30ab2015-04-24 15:24:27 -0400268static ssize_t print_cpus_isolated(struct device *dev,
269 struct device_attribute *attr, char *buf)
270{
Joe Perches948b3ed2020-09-16 13:40:42 -0700271 int len;
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200272 cpumask_var_t isolated;
Rik van Riel59f30ab2015-04-24 15:24:27 -0400273
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200274 if (!alloc_cpumask_var(&isolated, GFP_KERNEL))
275 return -ENOMEM;
276
277 cpumask_andnot(isolated, cpu_possible_mask,
278 housekeeping_cpumask(HK_FLAG_DOMAIN));
Joe Perches948b3ed2020-09-16 13:40:42 -0700279 len = sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(isolated));
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200280
281 free_cpumask_var(isolated);
Rik van Riel59f30ab2015-04-24 15:24:27 -0400282
Joe Perches948b3ed2020-09-16 13:40:42 -0700283 return len;
Rik van Riel59f30ab2015-04-24 15:24:27 -0400284}
285static DEVICE_ATTR(isolated, 0444, print_cpus_isolated, NULL);
286
Rik van Riel6570a9a2015-04-24 15:24:28 -0400287#ifdef CONFIG_NO_HZ_FULL
288static ssize_t print_cpus_nohz_full(struct device *dev,
Joe Perches948b3ed2020-09-16 13:40:42 -0700289 struct device_attribute *attr, char *buf)
Rik van Riel6570a9a2015-04-24 15:24:28 -0400290{
Joe Perchesaa838892020-09-16 13:40:39 -0700291 return sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(tick_nohz_full_mask));
Rik van Riel6570a9a2015-04-24 15:24:28 -0400292}
293static DEVICE_ATTR(nohz_full, 0444, print_cpus_nohz_full, NULL);
294#endif
295
Greg Kroah-Hartman2885e252012-02-02 10:36:33 -0800296static void cpu_device_release(struct device *dev)
297{
298 /*
299 * This is an empty function to prevent the driver core from spitting a
300 * warning at us. Yes, I know this is directly opposite of what the
301 * documentation for the driver core and kobjects say, and the author
302 * of this code has already been publically ridiculed for doing
303 * something as foolish as this. However, at this point in time, it is
304 * the only way to handle the issue of statically allocated cpu
305 * devices. The different architectures will have their cpu device
306 * code reworked to properly handle this in the near future, so this
307 * function will then be changed to correctly free up the memory held
308 * by the cpu device.
309 *
310 * Never copy this way of doing things, or you too will be made fun of
Ralf Baechle30a48402013-01-15 15:27:46 +0100311 * on the linux-kernel list, you have been warned.
Greg Kroah-Hartman2885e252012-02-02 10:36:33 -0800312 */
313}
314
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +0100315#ifdef CONFIG_GENERIC_CPU_AUTOPROBE
316static ssize_t print_cpu_modalias(struct device *dev,
317 struct device_attribute *attr,
318 char *buf)
319{
Joe Perches948b3ed2020-09-16 13:40:42 -0700320 int len = 0;
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +0100321 u32 i;
322
Joe Perches948b3ed2020-09-16 13:40:42 -0700323 len += sysfs_emit_at(buf, len,
324 "cpu:type:" CPU_FEATURE_TYPEFMT ":feature:",
325 CPU_FEATURE_TYPEVAL);
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +0100326
327 for (i = 0; i < MAX_CPU_FEATURES; i++)
328 if (cpu_have_feature(i)) {
Joe Perches948b3ed2020-09-16 13:40:42 -0700329 if (len + sizeof(",XXXX\n") >= PAGE_SIZE) {
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +0100330 WARN(1, "CPU features overflow page\n");
331 break;
332 }
Joe Perches948b3ed2020-09-16 13:40:42 -0700333 len += sysfs_emit_at(buf, len, ",%04X", i);
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +0100334 }
Joe Perches948b3ed2020-09-16 13:40:42 -0700335 len += sysfs_emit_at(buf, len, "\n");
336 return len;
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +0100337}
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +0100338
339static int cpu_uevent(struct device *dev, struct kobj_uevent_env *env)
340{
341 char *buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
342 if (buf) {
343 print_cpu_modalias(NULL, NULL, buf);
344 add_uevent_var(env, "MODALIAS=%s", buf);
345 kfree(buf);
346 }
347 return 0;
348}
349#endif
350
Mike Travis9d1fe3232008-04-08 11:43:04 -0700351/*
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100352 * register_cpu - Setup a sysfs device for a CPU.
Siddha, Suresh B72486f12006-12-07 02:14:10 +0100353 * @cpu - cpu->hotpluggable field set to 1 will generate a control file in
354 * sysfs for this CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 * @num - CPU number to use when creating the device.
356 *
357 * Initialize and register the CPU device.
358 */
Paul Gortmakera83048e2013-06-19 15:22:41 -0400359int register_cpu(struct cpu *cpu, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 int error;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 cpu->node_id = cpu_to_node(num);
Greg Kroah-Hartman29bb5d42012-02-08 15:11:17 -0800364 memset(&cpu->dev, 0x00, sizeof(struct device));
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800365 cpu->dev.id = num;
366 cpu->dev.bus = &cpu_subsys;
Greg Kroah-Hartman2885e252012-02-02 10:36:33 -0800367 cpu->dev.release = cpu_device_release;
Rafael J. Wysocki0902a902013-05-03 00:25:49 +0200368 cpu->dev.offline_disabled = !cpu->hotpluggable;
Toshi Kani1001b4d2013-05-30 00:30:05 +0200369 cpu->dev.offline = !cpu_online(num);
Sudeep KarkadaNageshaf86e4712013-06-17 12:58:45 +0100370 cpu->dev.of_node = of_get_cpu_node(num, NULL);
Ard Biesheuvel2b9c1f02014-02-08 13:34:10 +0100371#ifdef CONFIG_GENERIC_CPU_AUTOPROBE
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +0100372 cpu->dev.bus->uevent = cpu_uevent;
Thomas Renningerfad12ac2012-01-26 00:09:14 +0100373#endif
Igor Mammedovc055da92013-05-14 16:46:06 +0200374 cpu->dev.groups = common_cpu_attr_groups;
Igor Mammedov1c4e2d72013-05-14 16:46:07 +0200375 if (cpu->hotpluggable)
376 cpu->dev.groups = hotplugable_cpu_attr_groups;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800377 error = device_register(&cpu->dev);
Arvind Yadav3aaba242018-03-11 11:25:51 +0530378 if (error) {
379 put_device(&cpu->dev);
Alex Shi59fffa32016-08-25 16:42:39 +0800380 return error;
Arvind Yadav3aaba242018-03-11 11:25:51 +0530381 }
Vivek Goyal51be5602006-01-09 20:51:42 -0800382
Alex Shi59fffa32016-08-25 16:42:39 +0800383 per_cpu(cpu_sys_devices, num) = &cpu->dev;
384 register_cpu_under_node(num, cpu_to_node(num));
Rafael J. Wysocki0759e802017-11-07 11:33:49 +0100385 dev_pm_qos_expose_latency_limit(&cpu->dev,
386 PM_QOS_RESUME_LATENCY_NO_CONSTRAINT);
Alex Shi59fffa32016-08-25 16:42:39 +0800387
388 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800391struct device *get_cpu_device(unsigned cpu)
Ashok Rajad745572005-10-30 14:59:49 -0800392{
Mike Travise37d05d2008-05-01 04:35:16 -0700393 if (cpu < nr_cpu_ids && cpu_possible(cpu))
394 return per_cpu(cpu_sys_devices, cpu);
Ashok Rajad745572005-10-30 14:59:49 -0800395 else
396 return NULL;
397}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800398EXPORT_SYMBOL_GPL(get_cpu_device);
399
Sudeep Holla3d529432014-09-30 14:48:24 +0100400static void device_create_release(struct device *dev)
401{
402 kfree(dev);
403}
404
Mathieu Malaterrefa548d72019-01-23 20:42:00 +0100405__printf(4, 0)
Sudeep Holla3d529432014-09-30 14:48:24 +0100406static struct device *
407__cpu_device_create(struct device *parent, void *drvdata,
408 const struct attribute_group **groups,
409 const char *fmt, va_list args)
410{
411 struct device *dev = NULL;
Colin Ian King6b72cf12021-02-18 20:28:37 +0000412 int retval = -ENOMEM;
Sudeep Holla3d529432014-09-30 14:48:24 +0100413
414 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Colin Ian King6b72cf12021-02-18 20:28:37 +0000415 if (!dev)
Sudeep Holla3d529432014-09-30 14:48:24 +0100416 goto error;
Sudeep Holla3d529432014-09-30 14:48:24 +0100417
418 device_initialize(dev);
419 dev->parent = parent;
420 dev->groups = groups;
421 dev->release = device_create_release;
Sudeep Holla85945c22019-02-14 18:29:10 +0000422 device_set_pm_not_required(dev);
Sudeep Holla3d529432014-09-30 14:48:24 +0100423 dev_set_drvdata(dev, drvdata);
424
425 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
426 if (retval)
427 goto error;
428
429 retval = device_add(dev);
430 if (retval)
431 goto error;
432
433 return dev;
434
435error:
436 put_device(dev);
437 return ERR_PTR(retval);
438}
439
440struct device *cpu_device_create(struct device *parent, void *drvdata,
441 const struct attribute_group **groups,
442 const char *fmt, ...)
443{
444 va_list vargs;
445 struct device *dev;
446
447 va_start(vargs, fmt);
448 dev = __cpu_device_create(parent, drvdata, groups, fmt, vargs);
449 va_end(vargs);
450 return dev;
451}
452EXPORT_SYMBOL_GPL(cpu_device_create);
453
Ard Biesheuvel2b9c1f02014-02-08 13:34:10 +0100454#ifdef CONFIG_GENERIC_CPU_AUTOPROBE
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +0100455static DEVICE_ATTR(modalias, 0444, print_cpu_modalias, NULL);
Thomas Renningerfad12ac2012-01-26 00:09:14 +0100456#endif
457
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800458static struct attribute *cpu_root_attrs[] = {
459#ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
460 &dev_attr_probe.attr,
461 &dev_attr_release.attr,
462#endif
463 &cpu_attrs[0].attr.attr,
464 &cpu_attrs[1].attr.attr,
465 &cpu_attrs[2].attr.attr,
466 &dev_attr_kernel_max.attr,
467 &dev_attr_offline.attr,
Rik van Riel59f30ab2015-04-24 15:24:27 -0400468 &dev_attr_isolated.attr,
Rik van Riel6570a9a2015-04-24 15:24:28 -0400469#ifdef CONFIG_NO_HZ_FULL
470 &dev_attr_nohz_full.attr,
471#endif
Ard Biesheuvel2b9c1f02014-02-08 13:34:10 +0100472#ifdef CONFIG_GENERIC_CPU_AUTOPROBE
Thomas Renningerfad12ac2012-01-26 00:09:14 +0100473 &dev_attr_modalias.attr,
474#endif
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800475 NULL
476};
477
Rikard Falkeborn5a576762021-05-28 23:34:08 +0200478static const struct attribute_group cpu_root_attr_group = {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800479 .attrs = cpu_root_attrs,
480};
481
482static const struct attribute_group *cpu_root_attr_groups[] = {
483 &cpu_root_attr_group,
484 NULL,
485};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Josh Triplett29875572011-12-03 13:06:50 -0800487bool cpu_is_hotpluggable(unsigned cpu)
488{
Linus Torvalds7affca32012-01-07 12:03:30 -0800489 struct device *dev = get_cpu_device(cpu);
490 return dev && container_of(dev, struct cpu, dev)->hotpluggable;
Josh Triplett29875572011-12-03 13:06:50 -0800491}
492EXPORT_SYMBOL_GPL(cpu_is_hotpluggable);
493
Ben Hutchings9f13a1f2012-01-10 03:04:32 +0000494#ifdef CONFIG_GENERIC_CPU_DEVICES
495static DEFINE_PER_CPU(struct cpu, cpu_devices);
496#endif
497
498static void __init cpu_dev_register_generic(void)
499{
500#ifdef CONFIG_GENERIC_CPU_DEVICES
501 int i;
502
503 for_each_possible_cpu(i) {
504 if (register_cpu(&per_cpu(cpu_devices, i), i))
505 panic("Failed to register CPU device");
506 }
507#endif
508}
509
Thomas Gleixner87590ce2018-01-07 22:48:00 +0100510#ifdef CONFIG_GENERIC_CPU_VULNERABILITIES
511
512ssize_t __weak cpu_show_meltdown(struct device *dev,
513 struct device_attribute *attr, char *buf)
514{
Joe Perchesaa838892020-09-16 13:40:39 -0700515 return sysfs_emit(buf, "Not affected\n");
Thomas Gleixner87590ce2018-01-07 22:48:00 +0100516}
517
518ssize_t __weak cpu_show_spectre_v1(struct device *dev,
519 struct device_attribute *attr, char *buf)
520{
Joe Perchesaa838892020-09-16 13:40:39 -0700521 return sysfs_emit(buf, "Not affected\n");
Thomas Gleixner87590ce2018-01-07 22:48:00 +0100522}
523
524ssize_t __weak cpu_show_spectre_v2(struct device *dev,
525 struct device_attribute *attr, char *buf)
526{
Joe Perchesaa838892020-09-16 13:40:39 -0700527 return sysfs_emit(buf, "Not affected\n");
Thomas Gleixner87590ce2018-01-07 22:48:00 +0100528}
529
Konrad Rzeszutek Wilkc4564422018-04-25 22:04:20 -0400530ssize_t __weak cpu_show_spec_store_bypass(struct device *dev,
531 struct device_attribute *attr, char *buf)
532{
Joe Perchesaa838892020-09-16 13:40:39 -0700533 return sysfs_emit(buf, "Not affected\n");
Konrad Rzeszutek Wilkc4564422018-04-25 22:04:20 -0400534}
535
Andi Kleen17dbca12018-06-13 15:48:26 -0700536ssize_t __weak cpu_show_l1tf(struct device *dev,
537 struct device_attribute *attr, char *buf)
538{
Joe Perchesaa838892020-09-16 13:40:39 -0700539 return sysfs_emit(buf, "Not affected\n");
Andi Kleen17dbca12018-06-13 15:48:26 -0700540}
541
Thomas Gleixner8a4b06d2019-02-18 22:51:43 +0100542ssize_t __weak cpu_show_mds(struct device *dev,
543 struct device_attribute *attr, char *buf)
544{
Joe Perchesaa838892020-09-16 13:40:39 -0700545 return sysfs_emit(buf, "Not affected\n");
Thomas Gleixner8a4b06d2019-02-18 22:51:43 +0100546}
547
Pawan Gupta6608b452019-10-23 12:19:51 +0200548ssize_t __weak cpu_show_tsx_async_abort(struct device *dev,
549 struct device_attribute *attr,
550 char *buf)
551{
Joe Perchesaa838892020-09-16 13:40:39 -0700552 return sysfs_emit(buf, "Not affected\n");
Pawan Gupta6608b452019-10-23 12:19:51 +0200553}
554
Vineela Tummalapallidb4d30f2019-11-04 12:22:01 +0100555ssize_t __weak cpu_show_itlb_multihit(struct device *dev,
Joe Perches948b3ed2020-09-16 13:40:42 -0700556 struct device_attribute *attr, char *buf)
Vineela Tummalapallidb4d30f2019-11-04 12:22:01 +0100557{
Joe Perchesaa838892020-09-16 13:40:39 -0700558 return sysfs_emit(buf, "Not affected\n");
Vineela Tummalapallidb4d30f2019-11-04 12:22:01 +0100559}
560
Mark Gross7e5b3c22020-04-16 17:54:04 +0200561ssize_t __weak cpu_show_srbds(struct device *dev,
562 struct device_attribute *attr, char *buf)
563{
Joe Perchesaa838892020-09-16 13:40:39 -0700564 return sysfs_emit(buf, "Not affected\n");
Mark Gross7e5b3c22020-04-16 17:54:04 +0200565}
566
Thomas Gleixner87590ce2018-01-07 22:48:00 +0100567static DEVICE_ATTR(meltdown, 0444, cpu_show_meltdown, NULL);
568static DEVICE_ATTR(spectre_v1, 0444, cpu_show_spectre_v1, NULL);
569static DEVICE_ATTR(spectre_v2, 0444, cpu_show_spectre_v2, NULL);
Konrad Rzeszutek Wilkc4564422018-04-25 22:04:20 -0400570static DEVICE_ATTR(spec_store_bypass, 0444, cpu_show_spec_store_bypass, NULL);
Andi Kleen17dbca12018-06-13 15:48:26 -0700571static DEVICE_ATTR(l1tf, 0444, cpu_show_l1tf, NULL);
Thomas Gleixner8a4b06d2019-02-18 22:51:43 +0100572static DEVICE_ATTR(mds, 0444, cpu_show_mds, NULL);
Pawan Gupta6608b452019-10-23 12:19:51 +0200573static DEVICE_ATTR(tsx_async_abort, 0444, cpu_show_tsx_async_abort, NULL);
Vineela Tummalapallidb4d30f2019-11-04 12:22:01 +0100574static DEVICE_ATTR(itlb_multihit, 0444, cpu_show_itlb_multihit, NULL);
Mark Gross7e5b3c22020-04-16 17:54:04 +0200575static DEVICE_ATTR(srbds, 0444, cpu_show_srbds, NULL);
Thomas Gleixner87590ce2018-01-07 22:48:00 +0100576
577static struct attribute *cpu_root_vulnerabilities_attrs[] = {
578 &dev_attr_meltdown.attr,
579 &dev_attr_spectre_v1.attr,
580 &dev_attr_spectre_v2.attr,
Konrad Rzeszutek Wilkc4564422018-04-25 22:04:20 -0400581 &dev_attr_spec_store_bypass.attr,
Andi Kleen17dbca12018-06-13 15:48:26 -0700582 &dev_attr_l1tf.attr,
Thomas Gleixner8a4b06d2019-02-18 22:51:43 +0100583 &dev_attr_mds.attr,
Pawan Gupta6608b452019-10-23 12:19:51 +0200584 &dev_attr_tsx_async_abort.attr,
Vineela Tummalapallidb4d30f2019-11-04 12:22:01 +0100585 &dev_attr_itlb_multihit.attr,
Mark Gross7e5b3c22020-04-16 17:54:04 +0200586 &dev_attr_srbds.attr,
Thomas Gleixner87590ce2018-01-07 22:48:00 +0100587 NULL
588};
589
590static const struct attribute_group cpu_root_vulnerabilities_group = {
591 .name = "vulnerabilities",
592 .attrs = cpu_root_vulnerabilities_attrs,
593};
594
595static void __init cpu_register_vulnerabilities(void)
596{
597 if (sysfs_create_group(&cpu_subsys.dev_root->kobj,
598 &cpu_root_vulnerabilities_group))
599 pr_err("Unable to register CPU vulnerabilities\n");
600}
601
602#else
603static inline void cpu_register_vulnerabilities(void) { }
604#endif
605
Ben Hutchings024f7842012-01-10 02:59:49 +0000606void __init cpu_dev_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Ben Hutchings024f7842012-01-10 02:59:49 +0000608 if (subsys_system_register(&cpu_subsys, cpu_root_attr_groups))
609 panic("Failed to register CPU subsystem");
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700610
Ben Hutchings9f13a1f2012-01-10 03:04:32 +0000611 cpu_dev_register_generic();
Thomas Gleixner87590ce2018-01-07 22:48:00 +0100612 cpu_register_vulnerabilities();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}