2 /* ----------------------------------------------------------------------- *
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 * ----------------------------------------------------------------------- */
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>
41 #include <asm/uaccess.h>
42 #include <asm/system.h>
46 struct cpuid_command
{
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]);
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
)
90 file
->f_pos
+= offset
;
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
;
104 int cpu
= MINOR(file
->f_dentry
->d_inode
->i_rdev
);
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) )
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 */
134 * File operations we support
136 static struct file_operations cpuid_fops
= {
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",
154 void __exit
cpuid_exit(void)
156 unregister_chrdev(CPUID_MAJOR
, "cpu/cpuid");
159 module_init(cpuid_init
);
160 module_exit(cpuid_exit
)
164 MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
165 MODULE_DESCRIPTION("x86 generic CPUID driver");