blob: fe7ef339414441afc3592994d00dc97a99e0d0d5 [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>
Al Virof6a57032006-10-18 01:47:25 -04008#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/cpu.h>
10#include <linux/topology.h>
11#include <linux/device.h>
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -070012#include <linux/node.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010014#include "base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16struct sysdev_class cpu_sysdev_class = {
17 set_kset_name("cpu"),
18};
19EXPORT_SYMBOL(cpu_sysdev_class);
20
Ashok Rajad745572005-10-30 14:59:49 -080021static struct sys_device *cpu_sys_devices[NR_CPUS];
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#ifdef CONFIG_HOTPLUG_CPU
24static ssize_t show_online(struct sys_device *dev, char *buf)
25{
26 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
27
28 return sprintf(buf, "%u\n", !!cpu_online(cpu->sysdev.id));
29}
30
31static ssize_t store_online(struct sys_device *dev, const char *buf,
32 size_t count)
33{
34 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
35 ssize_t ret;
36
37 switch (buf[0]) {
38 case '0':
39 ret = cpu_down(cpu->sysdev.id);
40 if (!ret)
Kay Sievers312c0042005-11-16 09:00:00 +010041 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 break;
43 case '1':
Ashok Raj34f361a2006-03-25 03:08:18 -080044 ret = cpu_up(cpu->sysdev.id);
Nathan Lynchfb69c392005-06-25 14:55:05 -070045 if (!ret)
Kay Sievers312c0042005-11-16 09:00:00 +010046 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 break;
48 default:
49 ret = -EINVAL;
50 }
51
52 if (ret >= 0)
53 ret = count;
54 return ret;
55}
56static SYSDEV_ATTR(online, 0600, show_online, store_online);
57
58static void __devinit register_cpu_control(struct cpu *cpu)
59{
60 sysdev_create_file(&cpu->sysdev, &attr_online);
61}
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -070062void unregister_cpu(struct cpu *cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Ashok Rajad745572005-10-30 14:59:49 -080064 int logical_cpu = cpu->sysdev.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -070066 unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu));
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 sysdev_remove_file(&cpu->sysdev, &attr_online);
69
70 sysdev_unregister(&cpu->sysdev);
Ashok Rajad745572005-10-30 14:59:49 -080071 cpu_sys_devices[logical_cpu] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return;
73}
74#else /* ... !CONFIG_HOTPLUG_CPU */
75static inline void register_cpu_control(struct cpu *cpu)
76{
77}
78#endif /* CONFIG_HOTPLUG_CPU */
79
Vivek Goyal51be5602006-01-09 20:51:42 -080080#ifdef CONFIG_KEXEC
81#include <linux/kexec.h>
82
83static ssize_t show_crash_notes(struct sys_device *dev, char *buf)
84{
85 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
86 ssize_t rc;
87 unsigned long long addr;
88 int cpunum;
89
90 cpunum = cpu->sysdev.id;
91
92 /*
93 * Might be reading other cpu's data based on which cpu read thread
94 * has been scheduled. But cpu data (memory) is allocated once during
95 * boot up and this data does not change there after. Hence this
96 * operation should be safe. No locking required.
97 */
Vivek Goyal51be5602006-01-09 20:51:42 -080098 addr = __pa(per_cpu_ptr(crash_notes, cpunum));
99 rc = sprintf(buf, "%Lx\n", addr);
Vivek Goyal51be5602006-01-09 20:51:42 -0800100 return rc;
101}
102static SYSDEV_ATTR(crash_notes, 0400, show_crash_notes, NULL);
103#endif
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/*
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100106 * register_cpu - Setup a sysfs device for a CPU.
Siddha, Suresh B72486f12006-12-07 02:14:10 +0100107 * @cpu - cpu->hotpluggable field set to 1 will generate a control file in
108 * sysfs for this CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * @num - CPU number to use when creating the device.
110 *
111 * Initialize and register the CPU device.
112 */
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700113int __devinit register_cpu(struct cpu *cpu, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 cpu->node_id = cpu_to_node(num);
117 cpu->sysdev.id = num;
118 cpu->sysdev.cls = &cpu_sysdev_class;
119
120 error = sysdev_register(&cpu->sysdev);
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700121
Siddha, Suresh B72486f12006-12-07 02:14:10 +0100122 if (!error && cpu->hotpluggable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 register_cpu_control(cpu);
Ashok Rajad745572005-10-30 14:59:49 -0800124 if (!error)
125 cpu_sys_devices[num] = &cpu->sysdev;
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700126 if (!error)
127 register_cpu_under_node(num, cpu_to_node(num));
Vivek Goyal51be5602006-01-09 20:51:42 -0800128
129#ifdef CONFIG_KEXEC
130 if (!error)
131 error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes);
132#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return error;
134}
135
Andrew Mortona29d6422006-03-07 23:53:25 -0800136struct sys_device *get_cpu_sysdev(unsigned cpu)
Ashok Rajad745572005-10-30 14:59:49 -0800137{
138 if (cpu < NR_CPUS)
139 return cpu_sys_devices[cpu];
140 else
141 return NULL;
142}
143EXPORT_SYMBOL_GPL(get_cpu_sysdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145int __init cpu_dev_init(void)
146{
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700147 int err;
148
149 err = sysdev_class_register(&cpu_sysdev_class);
150#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
151 if (!err)
152 err = sched_create_sysfs_power_savings_entries(&cpu_sysdev_class);
153#endif
154
155 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}