Files

196 lines
4.6 KiB
C
Raw Permalink Normal View History

2005-04-16 15:20:36 -07:00
/* ----------------------------------------------------------------------- *
*
* Copyright 2000-2008 H. Peter Anvin - All Rights Reserved
2005-04-16 15:20:36 -07:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
* USA; either version 2 of the License, or (at your option) any later
* version; incorporated herein by reference.
*
* ----------------------------------------------------------------------- */
/*
* x86 CPUID access device
*
* This device is accessed by lseek() to the appropriate CPUID level
* and then read in chunks of 16 bytes. A larger size means multiple
* reads of consecutive levels.
*
* The lower 32 bits of the file position is used as the incoming %eax,
* and the upper 32 bits of the file position as the incoming %ecx,
* the latter intended for "counting" eax levels like eax=4.
*
2005-04-16 15:20:36 -07:00
* This driver uses /dev/cpu/%d/cpuid where %d is the minor number, and on
* an SMP box will direct the access to CPU %d.
*/
#include <linux/module.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/fcntl.h>
#include <linux/init.h>
#include <linux/poll.h>
#include <linux/smp.h>
#include <linux/major.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/cpu.h>
#include <linux/notifier.h>
2008-12-31 16:29:48 +05:30
#include <linux/uaccess.h>
#include <linux/gfp.h>
2018-03-23 14:58:18 -07:00
#include <linux/completion.h>
2005-04-16 15:20:36 -07:00
#include <asm/processor.h>
#include <asm/msr.h>
static struct class *cpuid_class;
static enum cpuhp_state cpuhp_cpuid_state;
2005-04-16 15:20:36 -07:00
2018-03-23 14:58:18 -07:00
struct cpuid_regs_done {
struct cpuid_regs regs;
struct completion done;
};
2005-04-16 15:20:36 -07:00
static void cpuid_smp_cpuid(void *cmd_block)
{
2018-03-23 14:58:18 -07:00
struct cpuid_regs_done *cmd = cmd_block;
2005-04-16 15:20:36 -07:00
2018-03-23 14:58:18 -07:00
cpuid_count(cmd->regs.eax, cmd->regs.ecx,
&cmd->regs.eax, &cmd->regs.ebx,
&cmd->regs.ecx, &cmd->regs.edx);
complete(&cmd->done);
2005-04-16 15:20:36 -07:00
}
static ssize_t cpuid_read(struct file *file, char __user *buf,
2008-12-31 16:29:48 +05:30
size_t count, loff_t *ppos)
2005-04-16 15:20:36 -07:00
{
char __user *tmp = buf;
2018-03-23 14:58:18 -07:00
struct cpuid_regs_done cmd;
2013-01-23 17:07:38 -05:00
int cpu = iminor(file_inode(file));
u64 pos = *ppos;
ssize_t bytes = 0;
int err = 0;
2005-04-16 15:20:36 -07:00
if (count % 16)
return -EINVAL; /* Invalid chunk size */
2018-03-23 14:58:18 -07:00
init_completion(&cmd.done);
for (; count; count -= 16) {
2018-03-23 14:58:18 -07:00
call_single_data_t csd = {
.func = cpuid_smp_cpuid,
.info = &cmd,
};
cmd.regs.eax = pos;
cmd.regs.ecx = pos >> 32;
err = smp_call_function_single_async(cpu, &csd);
if (err)
break;
2018-03-23 14:58:18 -07:00
wait_for_completion(&cmd.done);
if (copy_to_user(tmp, &cmd.regs, 16)) {
err = -EFAULT;
break;
}
2005-04-16 15:20:36 -07:00
tmp += 16;
bytes += 16;
*ppos = ++pos;
2018-03-23 14:58:18 -07:00
reinit_completion(&cmd.done);
2005-04-16 15:20:36 -07:00
}
return bytes ? bytes : err;
2005-04-16 15:20:36 -07:00
}
static int cpuid_open(struct inode *inode, struct file *file)
{
2008-05-15 09:12:01 -06:00
unsigned int cpu;
struct cpuinfo_x86 *c;
2005-04-16 15:20:36 -07:00
2013-01-23 17:07:38 -05:00
cpu = iminor(file_inode(file));
2009-10-08 17:20:15 +02:00
if (cpu >= nr_cpu_ids || !cpu_online(cpu))
return -ENXIO; /* No such CPU */
2008-05-15 09:12:01 -06:00
c = &cpu_data(cpu);
2005-04-16 15:20:36 -07:00
if (c->cpuid_level < 0)
2009-10-08 17:20:15 +02:00
return -EIO; /* CPUID not supported */
return 0;
2005-04-16 15:20:36 -07:00
}
/*
* File operations we support
*/
2007-02-12 00:55:31 -08:00
static const struct file_operations cpuid_fops = {
2005-04-16 15:20:36 -07:00
.owner = THIS_MODULE,
2015-12-05 22:04:48 -05:00
.llseek = no_seek_end_llseek,
2005-04-16 15:20:36 -07:00
.read = cpuid_read,
.open = cpuid_open,
};
2016-11-17 19:35:23 +01:00
static int cpuid_device_create(unsigned int cpu)
2005-04-16 15:20:36 -07:00
{
struct device *dev;
2005-04-16 15:20:36 -07:00
dev = device_create(cpuid_class, NULL, MKDEV(CPUID_MAJOR, cpu), NULL,
"cpu%d", cpu);
2014-10-17 22:01:17 +02:00
return PTR_ERR_OR_ZERO(dev);
}
2016-11-17 19:35:23 +01:00
static int cpuid_device_destroy(unsigned int cpu)
{
device_destroy(cpuid_class, MKDEV(CPUID_MAJOR, cpu));
2016-11-17 19:35:23 +01:00
return 0;
2005-04-16 15:20:36 -07:00
}
static char *cpuid_devnode(struct device *dev, umode_t *mode)
{
return kasprintf(GFP_KERNEL, "cpu/%u/cpuid", MINOR(dev->devt));
}
2005-04-16 15:20:36 -07:00
static int __init cpuid_init(void)
{
2016-11-17 19:35:23 +01:00
int err;
2005-04-16 15:20:36 -07:00
if (__register_chrdev(CPUID_MAJOR, 0, NR_CPUS,
"cpu/cpuid", &cpuid_fops)) {
2005-04-16 15:20:36 -07:00
printk(KERN_ERR "cpuid: unable to get major %d for cpuid\n",
CPUID_MAJOR);
2016-11-17 19:35:23 +01:00
return -EBUSY;
2005-04-16 15:20:36 -07:00
}
cpuid_class = class_create(THIS_MODULE, "cpuid");
2005-04-16 15:20:36 -07:00
if (IS_ERR(cpuid_class)) {
err = PTR_ERR(cpuid_class);
goto out_chrdev;
}
cpuid_class->devnode = cpuid_devnode;
err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/cpuid:online",
2016-11-17 19:35:23 +01:00
cpuid_device_create, cpuid_device_destroy);
if (err < 0)
2016-11-17 19:35:23 +01:00
goto out_class;
2005-04-16 15:20:36 -07:00
cpuhp_cpuid_state = err;
2016-11-17 19:35:23 +01:00
return 0;
2005-04-16 15:20:36 -07:00
out_class:
class_destroy(cpuid_class);
2005-04-16 15:20:36 -07:00
out_chrdev:
__unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid");
2005-04-16 15:20:36 -07:00
return err;
}
module_init(cpuid_init);
2005-04-16 15:20:36 -07:00
static void __exit cpuid_exit(void)
{
cpuhp_remove_state(cpuhp_cpuid_state);
class_destroy(cpuid_class);
__unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid");
2005-04-16 15:20:36 -07:00
}
module_exit(cpuid_exit);
MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
MODULE_DESCRIPTION("x86 generic CPUID driver");
MODULE_LICENSE("GPL");