Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / arch / i386 / kernel / cpuid.c
blobfe6528460f8411cabdf4ecd41cc58de282a944a9
1 #ident "$Id$"
2 /* ----------------------------------------------------------------------- *
3 *
4 * Copyright 2000 H. Peter Anvin - All Rights Reserved
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.
12 * ----------------------------------------------------------------------- */
16 * cpuid.c
18 * x86 CPUID access device
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.
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.
28 #include <linux/module.h>
29 #include <linux/config.h>
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>
39 #include <asm/processor.h>
40 #include <asm/msr.h>
41 #include <asm/uaccess.h>
42 #include <asm/system.h>
44 #ifdef CONFIG_SMP
46 struct cpuid_command {
47 int cpu;
48 u32 reg;
49 u32 *data;
52 static void cpuid_smp_cpuid(void *cmd_block)
54 struct cpuid_command *cmd = (struct cpuid_command *) cmd_block;
56 if ( cmd->cpu == smp_processor_id() )
57 cpuid(cmd->reg, &cmd->data[0], &cmd->data[1], &cmd->data[2], &cmd->data[3]);
60 extern inline void do_cpuid(int cpu, u32 reg, u32 *data)
62 struct cpuid_command cmd;
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;
71 smp_call_function(cpuid_smp_cpuid, &cmd, 1, 1);
74 #else /* ! CONFIG_SMP */
76 extern inline void do_cpuid(int cpu, u32 reg, u32 *data)
78 cpuid(reg, &data[0], &data[1], &data[2], &data[3]);
81 #endif /* ! CONFIG_SMP */
83 static loff_t cpuid_seek(struct file *file, loff_t offset, int orig)
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 */
97 static ssize_t cpuid_read(struct file * file, char * buf,
98 size_t count, loff_t *ppos)
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);
106 if ( count % 16 )
107 return -EINVAL; /* Invalid chunk size */
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++;
117 return ((char *)tmp) - buf;
120 static int cpuid_open(struct inode *inode, struct file *file)
122 int cpu = MINOR(file->f_dentry->d_inode->i_rdev);
123 struct cpuinfo_x86 *c = &(cpu_data)[cpu];
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 */
130 return 0;
134 * File operations we support
136 static struct file_operations cpuid_fops = {
137 owner: THIS_MODULE,
138 llseek: cpuid_seek,
139 read: cpuid_read,
140 open: cpuid_open,
143 int __init cpuid_init(void)
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;
151 return 0;
154 void __exit cpuid_exit(void)
156 unregister_chrdev(CPUID_MAJOR, "cpu/cpuid");
159 module_init(cpuid_init);
160 module_exit(cpuid_exit)
162 EXPORT_NO_SYMBOLS;
164 MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
165 MODULE_DESCRIPTION("x86 generic CPUID driver");