File: /usr/src/linux/arch/i386/kernel/cpuid.c
1 #ident "$Id$"
2 /* ----------------------------------------------------------------------- *
3 *
4 * Copyright 2000 H. Peter Anvin - All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
9 * USA; either version 2 of the License, or (at your option) any later
10 * version; incorporated herein by reference.
11 *
12 * ----------------------------------------------------------------------- */
13
14
15 /*
16 * cpuid.c
17 *
18 * x86 CPUID access device
19 *
20 * This device is accessed by lseek() to the appropriate CPUID level
21 * and then read in chunks of 16 bytes. A larger size means multiple
22 * reads of consecutive levels.
23 *
24 * This driver uses /dev/cpu/%d/cpuid where %d is the minor number, and on
25 * an SMP box will direct the access to CPU %d.
26 */
27
28 #include <linux/module.h>
29 #include <linux/config.h>
30
31 #include <linux/types.h>
32 #include <linux/errno.h>
33 #include <linux/fcntl.h>
34 #include <linux/init.h>
35 #include <linux/poll.h>
36 #include <linux/smp.h>
37 #include <linux/major.h>
38
39 #include <asm/processor.h>
40 #include <asm/msr.h>
41 #include <asm/uaccess.h>
42 #include <asm/system.h>
43
44 #ifdef CONFIG_SMP
45
46 struct cpuid_command {
47 int cpu;
48 u32 reg;
49 u32 *data;
50 };
51
52 static void cpuid_smp_cpuid(void *cmd_block)
53 {
54 struct cpuid_command *cmd = (struct cpuid_command *) cmd_block;
55
56 if ( cmd->cpu == smp_processor_id() )
57 cpuid(cmd->reg, &cmd->data[0], &cmd->data[1], &cmd->data[2], &cmd->data[3]);
58 }
59
60 static inline void do_cpuid(int cpu, u32 reg, u32 *data)
61 {
62 struct cpuid_command cmd;
63
64 if ( cpu == smp_processor_id() ) {
65 cpuid(reg, &data[0], &data[1], &data[2], &data[3]);
66 } else {
67 cmd.cpu = cpu;
68 cmd.reg = reg;
69 cmd.data = data;
70
71 smp_call_function(cpuid_smp_cpuid, &cmd, 1, 1);
72 }
73 }
74 #else /* ! CONFIG_SMP */
75
76 static inline void do_cpuid(int cpu, u32 reg, u32 *data)
77 {
78 cpuid(reg, &data[0], &data[1], &data[2], &data[3]);
79 }
80
81 #endif /* ! CONFIG_SMP */
82
83 static loff_t cpuid_seek(struct file *file, loff_t offset, int orig)
84 {
85 switch (orig) {
86 case 0:
87 file->f_pos = offset;
88 return file->f_pos;
89 case 1:
90 file->f_pos += offset;
91 return file->f_pos;
92 default:
93 return -EINVAL; /* SEEK_END not supported */
94 }
95 }
96
97 static ssize_t cpuid_read(struct file * file, char * buf,
98 size_t count, loff_t *ppos)
99 {
100 u32 *tmp = (u32 *)buf;
101 u32 data[4];
102 size_t rv;
103 u32 reg = *ppos;
104 int cpu = MINOR(file->f_dentry->d_inode->i_rdev);
105
106 if ( count % 16 )
107 return -EINVAL; /* Invalid chunk size */
108
109 for ( rv = 0 ; count ; count -= 16 ) {
110 do_cpuid(cpu, reg, data);
111 if ( copy_to_user(tmp,&data,16) )
112 return -EFAULT;
113 tmp += 4;
114 *ppos = reg++;
115 }
116
117 return ((char *)tmp) - buf;
118 }
119
120 static int cpuid_open(struct inode *inode, struct file *file)
121 {
122 int cpu = MINOR(file->f_dentry->d_inode->i_rdev);
123 struct cpuinfo_x86 *c = &(cpu_data)[cpu];
124
125 if ( !(cpu_online_map & (1UL << cpu)) )
126 return -ENXIO; /* No such CPU */
127 if ( c->cpuid_level < 0 )
128 return -EIO; /* CPUID not supported */
129
130 return 0;
131 }
132
133 /*
134 * File operations we support
135 */
136 static struct file_operations cpuid_fops = {
137 owner: THIS_MODULE,
138 llseek: cpuid_seek,
139 read: cpuid_read,
140 open: cpuid_open,
141 };
142
143 int __init cpuid_init(void)
144 {
145 if (register_chrdev(CPUID_MAJOR, "cpu/cpuid", &cpuid_fops)) {
146 printk(KERN_ERR "cpuid: unable to get major %d for cpuid\n",
147 CPUID_MAJOR);
148 return -EBUSY;
149 }
150
151 return 0;
152 }
153
154 void __exit cpuid_exit(void)
155 {
156 unregister_chrdev(CPUID_MAJOR, "cpu/cpuid");
157 }
158
159 module_init(cpuid_init);
160 module_exit(cpuid_exit)
161
162 EXPORT_NO_SYMBOLS;
163
164 MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
165 MODULE_DESCRIPTION("x86 generic CPUID driver");
166