blob: 3972d8ac9786872bfc2890152efbc52dd196bb9a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/base/cpu.c - basic CPU class support
3 */
4
5#include <linux/sysdev.h>
6#include <linux/module.h>
7#include <linux/init.h>
8#include <linux/cpu.h>
9#include <linux/topology.h>
10#include <linux/device.h>
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -070011#include <linux/node.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010013#include "base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15struct sysdev_class cpu_sysdev_class = {
16 set_kset_name("cpu"),
17};
18EXPORT_SYMBOL(cpu_sysdev_class);
19
Ashok Rajad745572005-10-30 14:59:49 -080020static struct sys_device *cpu_sys_devices[NR_CPUS];
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#ifdef CONFIG_HOTPLUG_CPU
23static ssize_t show_online(struct sys_device *dev, char *buf)
24{
25 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
26
27 return sprintf(buf, "%u\n", !!cpu_online(cpu->sysdev.id));
28}
29
30static ssize_t store_online(struct sys_device *dev, const char *buf,
31 size_t count)
32{
33 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
34 ssize_t ret;
35
36 switch (buf[0]) {
37 case '0':
38 ret = cpu_down(cpu->sysdev.id);
39 if (!ret)
Kay Sievers312c0042005-11-16 09:00:00 +010040 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 break;
42 case '1':
Ashok Raj34f361a2006-03-25 03:08:18 -080043 ret = cpu_up(cpu->sysdev.id);
Nathan Lynchfb69c392005-06-25 14:55:05 -070044 if (!ret)
Kay Sievers312c0042005-11-16 09:00:00 +010045 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 break;
47 default:
48 ret = -EINVAL;
49 }
50
51 if (ret >= 0)
52 ret = count;
53 return ret;
54}
55static SYSDEV_ATTR(online, 0600, show_online, store_online);
56
57static void __devinit register_cpu_control(struct cpu *cpu)
58{
59 sysdev_create_file(&cpu->sysdev, &attr_online);
60}
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -070061void unregister_cpu(struct cpu *cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Ashok Rajad745572005-10-30 14:59:49 -080063 int logical_cpu = cpu->sysdev.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -070065 unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu));
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 sysdev_remove_file(&cpu->sysdev, &attr_online);
68
69 sysdev_unregister(&cpu->sysdev);
Ashok Rajad745572005-10-30 14:59:49 -080070 cpu_sys_devices[logical_cpu] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return;
72}
73#else /* ... !CONFIG_HOTPLUG_CPU */
74static inline void register_cpu_control(struct cpu *cpu)
75{
76}
77#endif /* CONFIG_HOTPLUG_CPU */
78
Vivek Goyal51be5602006-01-09 20:51:42 -080079#ifdef CONFIG_KEXEC
80#include <linux/kexec.h>
81
82static ssize_t show_crash_notes(struct sys_device *dev, char *buf)
83{
84 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
85 ssize_t rc;
86 unsigned long long addr;
87 int cpunum;
88
89 cpunum = cpu->sysdev.id;
90
91 /*
92 * Might be reading other cpu's data based on which cpu read thread
93 * has been scheduled. But cpu data (memory) is allocated once during
94 * boot up and this data does not change there after. Hence this
95 * operation should be safe. No locking required.
96 */
Vivek Goyal51be5602006-01-09 20:51:42 -080097 addr = __pa(per_cpu_ptr(crash_notes, cpunum));
98 rc = sprintf(buf, "%Lx\n", addr);
Vivek Goyal51be5602006-01-09 20:51:42 -080099 return rc;
100}
101static SYSDEV_ATTR(crash_notes, 0400, show_crash_notes, NULL);
102#endif
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/*
105 * register_cpu - Setup a driverfs device for a CPU.
106 * @cpu - Callers can set the cpu->no_control field to 1, to indicate not to
107 * generate a control file in sysfs for this CPU.
108 * @num - CPU number to use when creating the device.
109 *
110 * Initialize and register the CPU device.
111 */
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700112int __devinit register_cpu(struct cpu *cpu, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 cpu->node_id = cpu_to_node(num);
116 cpu->sysdev.id = num;
117 cpu->sysdev.cls = &cpu_sysdev_class;
118
119 error = sysdev_register(&cpu->sysdev);
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (!error && !cpu->no_control)
122 register_cpu_control(cpu);
Ashok Rajad745572005-10-30 14:59:49 -0800123 if (!error)
124 cpu_sys_devices[num] = &cpu->sysdev;
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700125 if (!error)
126 register_cpu_under_node(num, cpu_to_node(num));
Vivek Goyal51be5602006-01-09 20:51:42 -0800127
128#ifdef CONFIG_KEXEC
129 if (!error)
130 error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes);
131#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return error;
133}
134
Andrew Mortona29d6422006-03-07 23:53:25 -0800135struct sys_device *get_cpu_sysdev(unsigned cpu)
Ashok Rajad745572005-10-30 14:59:49 -0800136{
137 if (cpu < NR_CPUS)
138 return cpu_sys_devices[cpu];
139 else
140 return NULL;
141}
142EXPORT_SYMBOL_GPL(get_cpu_sysdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144int __init cpu_dev_init(void)
145{
146 return sysdev_class_register(&cpu_sysdev_class);
147}