blob: eb9443d5bae1f8f6eb80632640b7c549f83e0043 [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
Rafael J. Wysocki0902a902013-05-03 00:25:49 +020058 ret = cpu_up(cpuid);
59 /*
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{
Toshi Kani6dedcca2013-09-25 15:08:27 -060072 return cpu_down(dev->id);
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
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800142static ssize_t show_crash_notes(struct device *dev, struct device_attribute *attr,
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200143 char *buf)
Vivek Goyal51be5602006-01-09 20:51:42 -0800144{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800145 struct cpu *cpu = container_of(dev, struct cpu, dev);
Vivek Goyal51be5602006-01-09 20:51:42 -0800146 ssize_t rc;
147 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));
Vivek Goyal51be5602006-01-09 20:51:42 -0800159 rc = sprintf(buf, "%Lx\n", addr);
Vivek Goyal51be5602006-01-09 20:51:42 -0800160 return rc;
161}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800162static DEVICE_ATTR(crash_notes, 0400, show_crash_notes, NULL);
Zhang Yanfeieca45492013-03-28 16:15:35 +0800163
164static ssize_t show_crash_notes_size(struct device *dev,
165 struct device_attribute *attr,
166 char *buf)
167{
168 ssize_t rc;
169
Arnd Bergmannbcfb87f2013-04-03 15:18:24 +0000170 rc = sprintf(buf, "%zu\n", sizeof(note_buf_t));
Zhang Yanfeieca45492013-03-28 16:15:35 +0800171 return rc;
172}
173static DEVICE_ATTR(crash_notes_size, 0400, show_crash_notes_size, NULL);
Igor Mammedovc055da92013-05-14 16:46:06 +0200174
175static struct attribute *crash_note_cpu_attrs[] = {
176 &dev_attr_crash_notes.attr,
177 &dev_attr_crash_notes_size.attr,
178 NULL
179};
180
181static struct attribute_group crash_note_cpu_attr_group = {
182 .attrs = crash_note_cpu_attrs,
183};
Vivek Goyal51be5602006-01-09 20:51:42 -0800184#endif
185
Igor Mammedovc055da92013-05-14 16:46:06 +0200186static const struct attribute_group *common_cpu_attr_groups[] = {
187#ifdef CONFIG_KEXEC
188 &crash_note_cpu_attr_group,
189#endif
190 NULL
191};
192
Igor Mammedov1c4e2d72013-05-14 16:46:07 +0200193static const struct attribute_group *hotplugable_cpu_attr_groups[] = {
194#ifdef CONFIG_KEXEC
195 &crash_note_cpu_attr_group,
196#endif
Igor Mammedov1c4e2d72013-05-14 16:46:07 +0200197 NULL
198};
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/*
Mike Travis9d1fe3232008-04-08 11:43:04 -0700201 * Print cpu online, possible, present, and system maps
202 */
Andi Kleen265d2e22010-01-05 12:48:00 +0100203
204struct cpu_attr {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800205 struct device_attribute attr;
Rasmus Villemoes848e2392016-01-20 15:00:22 -0800206 const struct cpumask *const map;
Andi Kleen265d2e22010-01-05 12:48:00 +0100207};
208
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800209static ssize_t show_cpus_attr(struct device *dev,
210 struct device_attribute *attr,
Andi Kleen265d2e22010-01-05 12:48:00 +0100211 char *buf)
Mike Travis9d1fe3232008-04-08 11:43:04 -0700212{
Andi Kleen265d2e22010-01-05 12:48:00 +0100213 struct cpu_attr *ca = container_of(attr, struct cpu_attr, attr);
Mike Travis9d1fe3232008-04-08 11:43:04 -0700214
Rasmus Villemoes848e2392016-01-20 15:00:22 -0800215 return cpumap_print_to_pagebuf(true, buf, ca->map);
Mike Travis9d1fe3232008-04-08 11:43:04 -0700216}
217
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800218#define _CPU_ATTR(name, map) \
219 { __ATTR(name, 0444, show_cpus_attr, NULL), map }
Mike Travis9d1fe3232008-04-08 11:43:04 -0700220
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800221/* Keep in sync with cpu_subsys_attrs */
Andi Kleen265d2e22010-01-05 12:48:00 +0100222static struct cpu_attr cpu_attrs[] = {
Rasmus Villemoes848e2392016-01-20 15:00:22 -0800223 _CPU_ATTR(online, &__cpu_online_mask),
224 _CPU_ATTR(possible, &__cpu_possible_mask),
225 _CPU_ATTR(present, &__cpu_present_mask),
Andi Kleen265d2e22010-01-05 12:48:00 +0100226};
Mike Travis9d1fe3232008-04-08 11:43:04 -0700227
Mike Travise057d7a2008-12-15 20:26:48 -0800228/*
229 * Print values for NR_CPUS and offlined cpus
230 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800231static ssize_t print_cpus_kernel_max(struct device *dev,
232 struct device_attribute *attr, char *buf)
Mike Travise057d7a2008-12-15 20:26:48 -0800233{
Mike Travis8fd2d2d2008-12-31 18:08:48 -0800234 int n = snprintf(buf, PAGE_SIZE-2, "%d\n", NR_CPUS - 1);
Mike Travise057d7a2008-12-15 20:26:48 -0800235 return n;
236}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800237static DEVICE_ATTR(kernel_max, 0444, print_cpus_kernel_max, NULL);
Mike Travise057d7a2008-12-15 20:26:48 -0800238
239/* arch-optional setting to enable display of offline cpus >= nr_cpu_ids */
240unsigned int total_cpus;
241
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800242static ssize_t print_cpus_offline(struct device *dev,
243 struct device_attribute *attr, char *buf)
Mike Travise057d7a2008-12-15 20:26:48 -0800244{
245 int n = 0, len = PAGE_SIZE-2;
246 cpumask_var_t offline;
247
248 /* display offline cpus < nr_cpu_ids */
249 if (!alloc_cpumask_var(&offline, GFP_KERNEL))
250 return -ENOMEM;
Jan Beulichcdc6e3d2010-04-27 14:01:20 -0700251 cpumask_andnot(offline, cpu_possible_mask, cpu_online_mask);
Tejun Heof799b1a2015-02-13 14:37:56 -0800252 n = scnprintf(buf, len, "%*pbl", cpumask_pr_args(offline));
Mike Travise057d7a2008-12-15 20:26:48 -0800253 free_cpumask_var(offline);
254
255 /* display offline cpus >= nr_cpu_ids */
256 if (total_cpus && nr_cpu_ids < total_cpus) {
257 if (n && n < len)
258 buf[n++] = ',';
259
260 if (nr_cpu_ids == total_cpus-1)
Alexey Dobriyan9b130ad2017-09-08 16:14:18 -0700261 n += snprintf(&buf[n], len - n, "%u", nr_cpu_ids);
Mike Travise057d7a2008-12-15 20:26:48 -0800262 else
Alexey Dobriyan9b130ad2017-09-08 16:14:18 -0700263 n += snprintf(&buf[n], len - n, "%u-%d",
Mike Travise057d7a2008-12-15 20:26:48 -0800264 nr_cpu_ids, total_cpus-1);
265 }
266
267 n += snprintf(&buf[n], len - n, "\n");
268 return n;
269}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800270static DEVICE_ATTR(offline, 0444, print_cpus_offline, NULL);
Mike Travise057d7a2008-12-15 20:26:48 -0800271
Rik van Riel59f30ab2015-04-24 15:24:27 -0400272static ssize_t print_cpus_isolated(struct device *dev,
273 struct device_attribute *attr, char *buf)
274{
275 int n = 0, len = PAGE_SIZE-2;
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200276 cpumask_var_t isolated;
Rik van Riel59f30ab2015-04-24 15:24:27 -0400277
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200278 if (!alloc_cpumask_var(&isolated, GFP_KERNEL))
279 return -ENOMEM;
280
281 cpumask_andnot(isolated, cpu_possible_mask,
282 housekeeping_cpumask(HK_FLAG_DOMAIN));
283 n = scnprintf(buf, len, "%*pbl\n", cpumask_pr_args(isolated));
284
285 free_cpumask_var(isolated);
Rik van Riel59f30ab2015-04-24 15:24:27 -0400286
287 return n;
288}
289static DEVICE_ATTR(isolated, 0444, print_cpus_isolated, NULL);
290
Rik van Riel6570a9a2015-04-24 15:24:28 -0400291#ifdef CONFIG_NO_HZ_FULL
292static ssize_t print_cpus_nohz_full(struct device *dev,
293 struct device_attribute *attr, char *buf)
294{
295 int n = 0, len = PAGE_SIZE-2;
296
297 n = scnprintf(buf, len, "%*pbl\n", cpumask_pr_args(tick_nohz_full_mask));
298
299 return n;
300}
301static DEVICE_ATTR(nohz_full, 0444, print_cpus_nohz_full, NULL);
302#endif
303
Greg Kroah-Hartman2885e252012-02-02 10:36:33 -0800304static void cpu_device_release(struct device *dev)
305{
306 /*
307 * This is an empty function to prevent the driver core from spitting a
308 * warning at us. Yes, I know this is directly opposite of what the
309 * documentation for the driver core and kobjects say, and the author
310 * of this code has already been publically ridiculed for doing
311 * something as foolish as this. However, at this point in time, it is
312 * the only way to handle the issue of statically allocated cpu
313 * devices. The different architectures will have their cpu device
314 * code reworked to properly handle this in the near future, so this
315 * function will then be changed to correctly free up the memory held
316 * by the cpu device.
317 *
318 * Never copy this way of doing things, or you too will be made fun of
Ralf Baechle30a48402013-01-15 15:27:46 +0100319 * on the linux-kernel list, you have been warned.
Greg Kroah-Hartman2885e252012-02-02 10:36:33 -0800320 */
321}
322
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +0100323#ifdef CONFIG_GENERIC_CPU_AUTOPROBE
324static ssize_t print_cpu_modalias(struct device *dev,
325 struct device_attribute *attr,
326 char *buf)
327{
328 ssize_t n;
329 u32 i;
330
331 n = sprintf(buf, "cpu:type:" CPU_FEATURE_TYPEFMT ":feature:",
332 CPU_FEATURE_TYPEVAL);
333
334 for (i = 0; i < MAX_CPU_FEATURES; i++)
335 if (cpu_have_feature(i)) {
336 if (PAGE_SIZE < n + sizeof(",XXXX\n")) {
337 WARN(1, "CPU features overflow page\n");
338 break;
339 }
340 n += sprintf(&buf[n], ",%04X", i);
341 }
342 buf[n++] = '\n';
343 return n;
344}
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +0100345
346static int cpu_uevent(struct device *dev, struct kobj_uevent_env *env)
347{
348 char *buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
349 if (buf) {
350 print_cpu_modalias(NULL, NULL, buf);
351 add_uevent_var(env, "MODALIAS=%s", buf);
352 kfree(buf);
353 }
354 return 0;
355}
356#endif
357
Mike Travis9d1fe3232008-04-08 11:43:04 -0700358/*
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100359 * register_cpu - Setup a sysfs device for a CPU.
Siddha, Suresh B72486f12006-12-07 02:14:10 +0100360 * @cpu - cpu->hotpluggable field set to 1 will generate a control file in
361 * sysfs for this CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 * @num - CPU number to use when creating the device.
363 *
364 * Initialize and register the CPU device.
365 */
Paul Gortmakera83048e2013-06-19 15:22:41 -0400366int register_cpu(struct cpu *cpu, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 int error;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 cpu->node_id = cpu_to_node(num);
Greg Kroah-Hartman29bb5d42012-02-08 15:11:17 -0800371 memset(&cpu->dev, 0x00, sizeof(struct device));
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800372 cpu->dev.id = num;
373 cpu->dev.bus = &cpu_subsys;
Greg Kroah-Hartman2885e252012-02-02 10:36:33 -0800374 cpu->dev.release = cpu_device_release;
Rafael J. Wysocki0902a902013-05-03 00:25:49 +0200375 cpu->dev.offline_disabled = !cpu->hotpluggable;
Toshi Kani1001b4d2013-05-30 00:30:05 +0200376 cpu->dev.offline = !cpu_online(num);
Sudeep KarkadaNageshaf86e4712013-06-17 12:58:45 +0100377 cpu->dev.of_node = of_get_cpu_node(num, NULL);
Ard Biesheuvel2b9c1f02014-02-08 13:34:10 +0100378#ifdef CONFIG_GENERIC_CPU_AUTOPROBE
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +0100379 cpu->dev.bus->uevent = cpu_uevent;
Thomas Renningerfad12ac2012-01-26 00:09:14 +0100380#endif
Igor Mammedovc055da92013-05-14 16:46:06 +0200381 cpu->dev.groups = common_cpu_attr_groups;
Igor Mammedov1c4e2d72013-05-14 16:46:07 +0200382 if (cpu->hotpluggable)
383 cpu->dev.groups = hotplugable_cpu_attr_groups;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800384 error = device_register(&cpu->dev);
Arvind Yadav3aaba242018-03-11 11:25:51 +0530385 if (error) {
386 put_device(&cpu->dev);
Alex Shi59fffa32016-08-25 16:42:39 +0800387 return error;
Arvind Yadav3aaba242018-03-11 11:25:51 +0530388 }
Vivek Goyal51be5602006-01-09 20:51:42 -0800389
Alex Shi59fffa32016-08-25 16:42:39 +0800390 per_cpu(cpu_sys_devices, num) = &cpu->dev;
391 register_cpu_under_node(num, cpu_to_node(num));
Rafael J. Wysocki0759e802017-11-07 11:33:49 +0100392 dev_pm_qos_expose_latency_limit(&cpu->dev,
393 PM_QOS_RESUME_LATENCY_NO_CONSTRAINT);
Alex Shi59fffa32016-08-25 16:42:39 +0800394
395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800398struct device *get_cpu_device(unsigned cpu)
Ashok Rajad745572005-10-30 14:59:49 -0800399{
Mike Travise37d05d2008-05-01 04:35:16 -0700400 if (cpu < nr_cpu_ids && cpu_possible(cpu))
401 return per_cpu(cpu_sys_devices, cpu);
Ashok Rajad745572005-10-30 14:59:49 -0800402 else
403 return NULL;
404}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800405EXPORT_SYMBOL_GPL(get_cpu_device);
406
Sudeep Holla3d529432014-09-30 14:48:24 +0100407static void device_create_release(struct device *dev)
408{
409 kfree(dev);
410}
411
412static struct device *
413__cpu_device_create(struct device *parent, void *drvdata,
414 const struct attribute_group **groups,
415 const char *fmt, va_list args)
416{
417 struct device *dev = NULL;
418 int retval = -ENODEV;
419
420 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
421 if (!dev) {
422 retval = -ENOMEM;
423 goto error;
424 }
425
426 device_initialize(dev);
427 dev->parent = parent;
428 dev->groups = groups;
429 dev->release = device_create_release;
430 dev_set_drvdata(dev, drvdata);
431
432 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
433 if (retval)
434 goto error;
435
436 retval = device_add(dev);
437 if (retval)
438 goto error;
439
440 return dev;
441
442error:
443 put_device(dev);
444 return ERR_PTR(retval);
445}
446
447struct device *cpu_device_create(struct device *parent, void *drvdata,
448 const struct attribute_group **groups,
449 const char *fmt, ...)
450{
451 va_list vargs;
452 struct device *dev;
453
454 va_start(vargs, fmt);
455 dev = __cpu_device_create(parent, drvdata, groups, fmt, vargs);
456 va_end(vargs);
457 return dev;
458}
459EXPORT_SYMBOL_GPL(cpu_device_create);
460
Ard Biesheuvel2b9c1f02014-02-08 13:34:10 +0100461#ifdef CONFIG_GENERIC_CPU_AUTOPROBE
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +0100462static DEVICE_ATTR(modalias, 0444, print_cpu_modalias, NULL);
Thomas Renningerfad12ac2012-01-26 00:09:14 +0100463#endif
464
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800465static struct attribute *cpu_root_attrs[] = {
466#ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
467 &dev_attr_probe.attr,
468 &dev_attr_release.attr,
469#endif
470 &cpu_attrs[0].attr.attr,
471 &cpu_attrs[1].attr.attr,
472 &cpu_attrs[2].attr.attr,
473 &dev_attr_kernel_max.attr,
474 &dev_attr_offline.attr,
Rik van Riel59f30ab2015-04-24 15:24:27 -0400475 &dev_attr_isolated.attr,
Rik van Riel6570a9a2015-04-24 15:24:28 -0400476#ifdef CONFIG_NO_HZ_FULL
477 &dev_attr_nohz_full.attr,
478#endif
Ard Biesheuvel2b9c1f02014-02-08 13:34:10 +0100479#ifdef CONFIG_GENERIC_CPU_AUTOPROBE
Thomas Renningerfad12ac2012-01-26 00:09:14 +0100480 &dev_attr_modalias.attr,
481#endif
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800482 NULL
483};
484
485static struct attribute_group cpu_root_attr_group = {
486 .attrs = cpu_root_attrs,
487};
488
489static const struct attribute_group *cpu_root_attr_groups[] = {
490 &cpu_root_attr_group,
491 NULL,
492};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Josh Triplett29875572011-12-03 13:06:50 -0800494bool cpu_is_hotpluggable(unsigned cpu)
495{
Linus Torvalds7affca32012-01-07 12:03:30 -0800496 struct device *dev = get_cpu_device(cpu);
497 return dev && container_of(dev, struct cpu, dev)->hotpluggable;
Josh Triplett29875572011-12-03 13:06:50 -0800498}
499EXPORT_SYMBOL_GPL(cpu_is_hotpluggable);
500
Ben Hutchings9f13a1f2012-01-10 03:04:32 +0000501#ifdef CONFIG_GENERIC_CPU_DEVICES
502static DEFINE_PER_CPU(struct cpu, cpu_devices);
503#endif
504
505static void __init cpu_dev_register_generic(void)
506{
507#ifdef CONFIG_GENERIC_CPU_DEVICES
508 int i;
509
510 for_each_possible_cpu(i) {
511 if (register_cpu(&per_cpu(cpu_devices, i), i))
512 panic("Failed to register CPU device");
513 }
514#endif
515}
516
Thomas Gleixner87590ce2018-01-07 22:48:00 +0100517#ifdef CONFIG_GENERIC_CPU_VULNERABILITIES
518
519ssize_t __weak cpu_show_meltdown(struct device *dev,
520 struct device_attribute *attr, char *buf)
521{
522 return sprintf(buf, "Not affected\n");
523}
524
525ssize_t __weak cpu_show_spectre_v1(struct device *dev,
526 struct device_attribute *attr, char *buf)
527{
528 return sprintf(buf, "Not affected\n");
529}
530
531ssize_t __weak cpu_show_spectre_v2(struct device *dev,
532 struct device_attribute *attr, char *buf)
533{
534 return sprintf(buf, "Not affected\n");
535}
536
Konrad Rzeszutek Wilkc4564422018-04-25 22:04:20 -0400537ssize_t __weak cpu_show_spec_store_bypass(struct device *dev,
538 struct device_attribute *attr, char *buf)
539{
540 return sprintf(buf, "Not affected\n");
541}
542
Andi Kleen17dbca12018-06-13 15:48:26 -0700543ssize_t __weak cpu_show_l1tf(struct device *dev,
544 struct device_attribute *attr, char *buf)
545{
546 return sprintf(buf, "Not affected\n");
547}
548
Thomas Gleixner87590ce2018-01-07 22:48:00 +0100549static DEVICE_ATTR(meltdown, 0444, cpu_show_meltdown, NULL);
550static DEVICE_ATTR(spectre_v1, 0444, cpu_show_spectre_v1, NULL);
551static DEVICE_ATTR(spectre_v2, 0444, cpu_show_spectre_v2, NULL);
Konrad Rzeszutek Wilkc4564422018-04-25 22:04:20 -0400552static DEVICE_ATTR(spec_store_bypass, 0444, cpu_show_spec_store_bypass, NULL);
Andi Kleen17dbca12018-06-13 15:48:26 -0700553static DEVICE_ATTR(l1tf, 0444, cpu_show_l1tf, NULL);
Thomas Gleixner87590ce2018-01-07 22:48:00 +0100554
555static struct attribute *cpu_root_vulnerabilities_attrs[] = {
556 &dev_attr_meltdown.attr,
557 &dev_attr_spectre_v1.attr,
558 &dev_attr_spectre_v2.attr,
Konrad Rzeszutek Wilkc4564422018-04-25 22:04:20 -0400559 &dev_attr_spec_store_bypass.attr,
Andi Kleen17dbca12018-06-13 15:48:26 -0700560 &dev_attr_l1tf.attr,
Thomas Gleixner87590ce2018-01-07 22:48:00 +0100561 NULL
562};
563
564static const struct attribute_group cpu_root_vulnerabilities_group = {
565 .name = "vulnerabilities",
566 .attrs = cpu_root_vulnerabilities_attrs,
567};
568
569static void __init cpu_register_vulnerabilities(void)
570{
571 if (sysfs_create_group(&cpu_subsys.dev_root->kobj,
572 &cpu_root_vulnerabilities_group))
573 pr_err("Unable to register CPU vulnerabilities\n");
574}
575
576#else
577static inline void cpu_register_vulnerabilities(void) { }
578#endif
579
Ben Hutchings024f7842012-01-10 02:59:49 +0000580void __init cpu_dev_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Ben Hutchings024f7842012-01-10 02:59:49 +0000582 if (subsys_system_register(&cpu_subsys, cpu_root_attr_groups))
583 panic("Failed to register CPU subsystem");
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700584
Ben Hutchings9f13a1f2012-01-10 03:04:32 +0000585 cpu_dev_register_generic();
Thomas Gleixner87590ce2018-01-07 22:48:00 +0100586 cpu_register_vulnerabilities();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}