blob: dffb102fb8c76a0a980903c2ca14435fa100456c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* ----------------------------------------------------------------------- *
2 *
3 * Copyright 2000 H. Peter Anvin - All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
8 * USA; either version 2 of the License, or (at your option) any later
9 * version; incorporated herein by reference.
10 *
11 * ----------------------------------------------------------------------- */
12
13/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * x86 CPUID access device
15 *
16 * This device is accessed by lseek() to the appropriate CPUID level
17 * and then read in chunks of 16 bytes. A larger size means multiple
18 * reads of consecutive levels.
19 *
20 * This driver uses /dev/cpu/%d/cpuid where %d is the minor number, and on
21 * an SMP box will direct the access to CPU %d.
22 */
23
24#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <linux/types.h>
27#include <linux/errno.h>
28#include <linux/fcntl.h>
29#include <linux/init.h>
30#include <linux/poll.h>
31#include <linux/smp.h>
32#include <linux/major.h>
33#include <linux/fs.h>
34#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/device.h>
36#include <linux/cpu.h>
37#include <linux/notifier.h>
38
39#include <asm/processor.h>
40#include <asm/msr.h>
41#include <asm/uaccess.h>
42#include <asm/system.h>
43
gregkh@suse.de8874b412005-03-23 09:56:34 -080044static struct class *cpuid_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#ifdef CONFIG_SMP
47
48struct cpuid_command {
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 u32 reg;
50 u32 *data;
51};
52
53static void cpuid_smp_cpuid(void *cmd_block)
54{
55 struct cpuid_command *cmd = (struct cpuid_command *)cmd_block;
56
Alexey Dobriyanad4e6802007-02-13 13:26:23 +010057 cpuid(cmd->reg, &cmd->data[0], &cmd->data[1], &cmd->data[2],
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 &cmd->data[3]);
59}
60
61static inline void do_cpuid(int cpu, u32 reg, u32 * data)
62{
63 struct cpuid_command cmd;
64
65 preempt_disable();
66 if (cpu == smp_processor_id()) {
67 cpuid(reg, &data[0], &data[1], &data[2], &data[3]);
68 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 cmd.reg = reg;
70 cmd.data = data;
71
Alexey Dobriyanad4e6802007-02-13 13:26:23 +010072 smp_call_function_single(cpu, cpuid_smp_cpuid, &cmd, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
74 preempt_enable();
75}
76#else /* ! CONFIG_SMP */
77
78static inline void do_cpuid(int cpu, u32 reg, u32 * data)
79{
80 cpuid(reg, &data[0], &data[1], &data[2], &data[3]);
81}
82
83#endif /* ! CONFIG_SMP */
84
85static loff_t cpuid_seek(struct file *file, loff_t offset, int orig)
86{
87 loff_t ret;
88
89 lock_kernel();
90
91 switch (orig) {
92 case 0:
93 file->f_pos = offset;
94 ret = file->f_pos;
95 break;
96 case 1:
97 file->f_pos += offset;
98 ret = file->f_pos;
99 break;
100 default:
101 ret = -EINVAL;
102 }
103
104 unlock_kernel();
105 return ret;
106}
107
108static ssize_t cpuid_read(struct file *file, char __user *buf,
109 size_t count, loff_t * ppos)
110{
111 char __user *tmp = buf;
112 u32 data[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 u32 reg = *ppos;
Josef "Jeff" Sipekaab4c5a2006-12-08 02:36:42 -0800114 int cpu = iminor(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 if (count % 16)
117 return -EINVAL; /* Invalid chunk size */
118
Daniel Marjamaki6b7f4302006-01-06 00:12:13 -0800119 for (; count; count -= 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 do_cpuid(cpu, reg, data);
121 if (copy_to_user(tmp, &data, 16))
122 return -EFAULT;
123 tmp += 16;
124 *ppos = reg++;
125 }
126
127 return tmp - buf;
128}
129
130static int cpuid_open(struct inode *inode, struct file *file)
131{
Josef "Jeff" Sipekaab4c5a2006-12-08 02:36:42 -0800132 unsigned int cpu = iminor(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 struct cpuinfo_x86 *c = &(cpu_data)[cpu];
134
135 if (cpu >= NR_CPUS || !cpu_online(cpu))
136 return -ENXIO; /* No such CPU */
137 if (c->cpuid_level < 0)
138 return -EIO; /* CPUID not supported */
139
140 return 0;
141}
142
143/*
144 * File operations we support
145 */
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800146static const struct file_operations cpuid_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 .owner = THIS_MODULE,
148 .llseek = cpuid_seek,
149 .read = cpuid_read,
150 .open = cpuid_open,
151};
152
Satyam Sharma1e32b072007-10-17 18:04:36 +0200153static int __cpuinit cpuid_device_create(int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 int err = 0;
Greg Kroah-Hartman07accdc2006-08-07 22:19:37 -0700156 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Greg Kroah-Hartman07accdc2006-08-07 22:19:37 -0700158 dev = device_create(cpuid_class, NULL, MKDEV(CPUID_MAJOR, i), "cpu%d",i);
159 if (IS_ERR(dev))
160 err = PTR_ERR(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return err;
162}
163
Satyam Sharma1e32b072007-10-17 18:04:36 +0200164static int __cpuinit cpuid_class_cpu_callback(struct notifier_block *nfb,
165 unsigned long action,
166 void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 unsigned int cpu = (unsigned long)hcpu;
169
170 switch (action) {
171 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700172 case CPU_ONLINE_FROZEN:
Greg Kroah-Hartman07accdc2006-08-07 22:19:37 -0700173 cpuid_device_create(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 break;
175 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700176 case CPU_DEAD_FROZEN:
Greg Kroah-Hartman07accdc2006-08-07 22:19:37 -0700177 device_destroy(cpuid_class, MKDEV(CPUID_MAJOR, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 break;
179 }
180 return NOTIFY_OK;
181}
182
Chandra Seetharaman74b85f32006-06-27 02:54:09 -0700183static struct notifier_block __cpuinitdata cpuid_class_cpu_notifier =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 .notifier_call = cpuid_class_cpu_callback,
186};
187
188static int __init cpuid_init(void)
189{
190 int i, err = 0;
191 i = 0;
192
193 if (register_chrdev(CPUID_MAJOR, "cpu/cpuid", &cpuid_fops)) {
194 printk(KERN_ERR "cpuid: unable to get major %d for cpuid\n",
195 CPUID_MAJOR);
196 err = -EBUSY;
197 goto out;
198 }
gregkh@suse.de8874b412005-03-23 09:56:34 -0800199 cpuid_class = class_create(THIS_MODULE, "cpuid");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (IS_ERR(cpuid_class)) {
201 err = PTR_ERR(cpuid_class);
202 goto out_chrdev;
203 }
204 for_each_online_cpu(i) {
Greg Kroah-Hartman07accdc2006-08-07 22:19:37 -0700205 err = cpuid_device_create(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (err != 0)
207 goto out_class;
208 }
OGAWA Hirofumib6a7c792006-07-03 17:32:20 -0700209 register_hotcpu_notifier(&cpuid_class_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 err = 0;
212 goto out;
213
214out_class:
215 i = 0;
216 for_each_online_cpu(i) {
Greg Kroah-Hartman07accdc2006-08-07 22:19:37 -0700217 device_destroy(cpuid_class, MKDEV(CPUID_MAJOR, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
gregkh@suse.de8874b412005-03-23 09:56:34 -0800219 class_destroy(cpuid_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220out_chrdev:
221 unregister_chrdev(CPUID_MAJOR, "cpu/cpuid");
222out:
223 return err;
224}
225
226static void __exit cpuid_exit(void)
227{
228 int cpu = 0;
229
230 for_each_online_cpu(cpu)
Greg Kroah-Hartman07accdc2006-08-07 22:19:37 -0700231 device_destroy(cpuid_class, MKDEV(CPUID_MAJOR, cpu));
gregkh@suse.de8874b412005-03-23 09:56:34 -0800232 class_destroy(cpuid_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 unregister_chrdev(CPUID_MAJOR, "cpu/cpuid");
OGAWA Hirofumib6a7c792006-07-03 17:32:20 -0700234 unregister_hotcpu_notifier(&cpuid_class_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
237module_init(cpuid_init);
238module_exit(cpuid_exit);
239
240MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
241MODULE_DESCRIPTION("x86 generic CPUID driver");
242MODULE_LICENSE("GPL");