ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / i386 / kernel / cpuid.c
1 /* ----------------------------------------------------------------------- *
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
14 /*
15  * cpuid.c
16  *
17  * x86 CPUID access device
18  *
19  * This device is accessed by lseek() to the appropriate CPUID level
20  * and then read in chunks of 16 bytes.  A larger size means multiple
21  * reads of consecutive levels.
22  *
23  * This driver uses /dev/cpu/%d/cpuid where %d is the minor number, and on
24  * an SMP box will direct the access to CPU %d.
25  */
26
27 #include <linux/module.h>
28 #include <linux/config.h>
29
30 #include <linux/types.h>
31 #include <linux/errno.h>
32 #include <linux/fcntl.h>
33 #include <linux/init.h>
34 #include <linux/poll.h>
35 #include <linux/smp.h>
36 #include <linux/major.h>
37 #include <linux/fs.h>
38 #include <linux/smp_lock.h>
39 #include <linux/fs.h>
40
41 #include <asm/processor.h>
42 #include <asm/msr.h>
43 #include <asm/uaccess.h>
44 #include <asm/system.h>
45
46 #ifdef CONFIG_SMP
47
48 struct cpuid_command {
49   int cpu;
50   u32 reg;
51   u32 *data;
52 };
53
54 static void cpuid_smp_cpuid(void *cmd_block)
55 {
56   struct cpuid_command *cmd = (struct cpuid_command *) cmd_block;
57   
58   if ( cmd->cpu == smp_processor_id() )
59     cpuid(cmd->reg, &cmd->data[0], &cmd->data[1], &cmd->data[2], &cmd->data[3]);
60 }
61
62 static inline void do_cpuid(int cpu, u32 reg, u32 *data)
63 {
64   struct cpuid_command cmd;
65   
66   preempt_disable();
67   if ( cpu == smp_processor_id() ) {
68     cpuid(reg, &data[0], &data[1], &data[2], &data[3]);
69   } else {
70     cmd.cpu  = cpu;
71     cmd.reg  = reg;
72     cmd.data = data;
73     
74     smp_call_function(cpuid_smp_cpuid, &cmd, 1, 1);
75   }
76   preempt_enable();
77 }
78 #else /* ! CONFIG_SMP */
79
80 static inline void do_cpuid(int cpu, u32 reg, u32 *data)
81 {
82   cpuid(reg, &data[0], &data[1], &data[2], &data[3]);
83 }
84
85 #endif /* ! CONFIG_SMP */
86
87 static loff_t cpuid_seek(struct file *file, loff_t offset, int orig)
88 {
89   loff_t ret;
90
91   lock_kernel();
92
93   switch (orig) {
94   case 0:
95     file->f_pos = offset;
96     ret = file->f_pos;
97     break;
98   case 1:
99     file->f_pos += offset;
100     ret = file->f_pos;
101     break;
102   default:
103     ret = -EINVAL;
104   }
105
106   unlock_kernel();
107   return ret;
108 }
109
110 static ssize_t cpuid_read(struct file * file, char * buf,
111                         size_t count, loff_t *ppos)
112 {
113   u32 *tmp = (u32 *)buf;
114   u32 data[4];
115   size_t rv;
116   u32 reg = *ppos;
117   int cpu = iminor(file->f_dentry->d_inode);
118   
119   if ( count % 16 )
120     return -EINVAL; /* Invalid chunk size */
121   
122   for ( rv = 0 ; count ; count -= 16 ) {
123     do_cpuid(cpu, reg, data);
124     if ( copy_to_user(tmp,&data,16) )
125       return -EFAULT;
126     tmp += 4;
127     *ppos = reg++;
128   }
129   
130   return ((char *)tmp) - buf;
131 }
132
133 static int cpuid_open(struct inode *inode, struct file *file)
134 {
135   int cpu = iminor(file->f_dentry->d_inode);
136   struct cpuinfo_x86 *c = &(cpu_data)[cpu];
137
138   if (!cpu_online(cpu))
139     return -ENXIO;              /* No such CPU */
140   if ( c->cpuid_level < 0 )
141     return -EIO;                /* CPUID not supported */
142   
143   return 0;
144 }
145
146 /*
147  * File operations we support
148  */
149 static struct file_operations cpuid_fops = {
150   .owner        = THIS_MODULE,
151   .llseek       = cpuid_seek,
152   .read         = cpuid_read,
153   .open         = cpuid_open,
154 };
155
156 int __init cpuid_init(void)
157 {
158   if (register_chrdev(CPUID_MAJOR, "cpu/cpuid", &cpuid_fops)) {
159     printk(KERN_ERR "cpuid: unable to get major %d for cpuid\n",
160            CPUID_MAJOR);
161     return -EBUSY;
162   }
163
164   return 0;
165 }
166
167 void __exit cpuid_exit(void)
168 {
169   unregister_chrdev(CPUID_MAJOR, "cpu/cpuid");
170 }
171
172 module_init(cpuid_init);
173 module_exit(cpuid_exit)
174
175 MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
176 MODULE_DESCRIPTION("x86 generic CPUID driver");
177 MODULE_LICENSE("GPL");