1 /* ----------------------------------------------------------------------- *
3 * Copyright 2000 H. Peter Anvin - All Rights Reserved
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.
11 * ----------------------------------------------------------------------- */
17 * x86 CPUID access device
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.
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.
27 #include <linux/module.h>
28 #include <linux/config.h>
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>
38 #include <linux/smp_lock.h>
41 #include <asm/processor.h>
43 #include <asm/uaccess.h>
44 #include <asm/system.h>
48 struct cpuid_command {
54 static void cpuid_smp_cpuid(void *cmd_block)
56 struct cpuid_command *cmd = (struct cpuid_command *) cmd_block;
58 if ( cmd->cpu == smp_processor_id() )
59 cpuid(cmd->reg, &cmd->data[0], &cmd->data[1], &cmd->data[2], &cmd->data[3]);
62 static inline void do_cpuid(int cpu, u32 reg, u32 *data)
64 struct cpuid_command cmd;
67 if ( cpu == smp_processor_id() ) {
68 cpuid(reg, &data[0], &data[1], &data[2], &data[3]);
74 smp_call_function(cpuid_smp_cpuid, &cmd, 1, 1);
78 #else /* ! CONFIG_SMP */
80 static inline void do_cpuid(int cpu, u32 reg, u32 *data)
82 cpuid(reg, &data[0], &data[1], &data[2], &data[3]);
85 #endif /* ! CONFIG_SMP */
87 static loff_t cpuid_seek(struct file *file, loff_t offset, int orig)
99 file->f_pos += offset;
110 static ssize_t cpuid_read(struct file * file, char * buf,
111 size_t count, loff_t *ppos)
113 u32 *tmp = (u32 *)buf;
117 int cpu = iminor(file->f_dentry->d_inode);
120 return -EINVAL; /* Invalid chunk size */
122 for ( rv = 0 ; count ; count -= 16 ) {
123 do_cpuid(cpu, reg, data);
124 if ( copy_to_user(tmp,&data,16) )
130 return ((char *)tmp) - buf;
133 static int cpuid_open(struct inode *inode, struct file *file)
135 int cpu = iminor(file->f_dentry->d_inode);
136 struct cpuinfo_x86 *c = &(cpu_data)[cpu];
138 if (!cpu_online(cpu))
139 return -ENXIO; /* No such CPU */
140 if ( c->cpuid_level < 0 )
141 return -EIO; /* CPUID not supported */
147 * File operations we support
149 static struct file_operations cpuid_fops = {
150 .owner = THIS_MODULE,
151 .llseek = cpuid_seek,
156 int __init cpuid_init(void)
158 if (register_chrdev(CPUID_MAJOR, "cpu/cpuid", &cpuid_fops)) {
159 printk(KERN_ERR "cpuid: unable to get major %d for cpuid\n",
167 void __exit cpuid_exit(void)
169 unregister_chrdev(CPUID_MAJOR, "cpu/cpuid");
172 module_init(cpuid_init);
173 module_exit(cpuid_exit)
175 MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
176 MODULE_DESCRIPTION("x86 generic CPUID driver");
177 MODULE_LICENSE("GPL");