blob: 281d26784d251107bc6b158ba3ce5f5904a157fd [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>
11
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010012#include "base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14struct sysdev_class cpu_sysdev_class = {
15 set_kset_name("cpu"),
16};
17EXPORT_SYMBOL(cpu_sysdev_class);
18
Ashok Rajad745572005-10-30 14:59:49 -080019static struct sys_device *cpu_sys_devices[NR_CPUS];
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#ifdef CONFIG_HOTPLUG_CPU
Ashok Raj52a119f2005-06-25 14:54:57 -070022int __attribute__((weak)) smp_prepare_cpu (int cpu)
23{
24 return 0;
25}
Li Shaohuae1367da2005-06-25 14:54:56 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static ssize_t show_online(struct sys_device *dev, char *buf)
28{
29 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
30
31 return sprintf(buf, "%u\n", !!cpu_online(cpu->sysdev.id));
32}
33
34static ssize_t store_online(struct sys_device *dev, const char *buf,
35 size_t count)
36{
37 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
38 ssize_t ret;
39
40 switch (buf[0]) {
41 case '0':
42 ret = cpu_down(cpu->sysdev.id);
43 if (!ret)
Kay Sievers312c0042005-11-16 09:00:00 +010044 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 break;
46 case '1':
Li Shaohuae1367da2005-06-25 14:54:56 -070047 ret = smp_prepare_cpu(cpu->sysdev.id);
Ashok Raj52a119f2005-06-25 14:54:57 -070048 if (!ret)
Li Shaohuae1367da2005-06-25 14:54:56 -070049 ret = cpu_up(cpu->sysdev.id);
Nathan Lynchfb69c392005-06-25 14:55:05 -070050 if (!ret)
Kay Sievers312c0042005-11-16 09:00:00 +010051 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 break;
53 default:
54 ret = -EINVAL;
55 }
56
57 if (ret >= 0)
58 ret = count;
59 return ret;
60}
61static SYSDEV_ATTR(online, 0600, show_online, store_online);
62
63static void __devinit register_cpu_control(struct cpu *cpu)
64{
65 sysdev_create_file(&cpu->sysdev, &attr_online);
66}
67void unregister_cpu(struct cpu *cpu, struct node *root)
68{
Ashok Rajad745572005-10-30 14:59:49 -080069 int logical_cpu = cpu->sysdev.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 if (root)
72 sysfs_remove_link(&root->sysdev.kobj,
73 kobject_name(&cpu->sysdev.kobj));
74 sysdev_remove_file(&cpu->sysdev, &attr_online);
75
76 sysdev_unregister(&cpu->sysdev);
Ashok Rajad745572005-10-30 14:59:49 -080077 cpu_sys_devices[logical_cpu] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return;
79}
80#else /* ... !CONFIG_HOTPLUG_CPU */
81static inline void register_cpu_control(struct cpu *cpu)
82{
83}
84#endif /* CONFIG_HOTPLUG_CPU */
85
86/*
87 * register_cpu - Setup a driverfs device for a CPU.
88 * @cpu - Callers can set the cpu->no_control field to 1, to indicate not to
89 * generate a control file in sysfs for this CPU.
90 * @num - CPU number to use when creating the device.
91 *
92 * Initialize and register the CPU device.
93 */
94int __devinit register_cpu(struct cpu *cpu, int num, struct node *root)
95{
96 int error;
97
98 cpu->node_id = cpu_to_node(num);
99 cpu->sysdev.id = num;
100 cpu->sysdev.cls = &cpu_sysdev_class;
101
102 error = sysdev_register(&cpu->sysdev);
103 if (!error && root)
104 error = sysfs_create_link(&root->sysdev.kobj,
105 &cpu->sysdev.kobj,
106 kobject_name(&cpu->sysdev.kobj));
107 if (!error && !cpu->no_control)
108 register_cpu_control(cpu);
Ashok Rajad745572005-10-30 14:59:49 -0800109 if (!error)
110 cpu_sys_devices[num] = &cpu->sysdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return error;
112}
113
Ashok Rajad745572005-10-30 14:59:49 -0800114struct sys_device *get_cpu_sysdev(int cpu)
115{
116 if (cpu < NR_CPUS)
117 return cpu_sys_devices[cpu];
118 else
119 return NULL;
120}
121EXPORT_SYMBOL_GPL(get_cpu_sysdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123int __init cpu_dev_init(void)
124{
125 return sysdev_class_register(&cpu_sysdev_class);
126}