^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* ----------------------------------------------------------------------- *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright 2000-2008 H. Peter Anvin - All Rights Reserved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * ----------------------------------------------------------------------- */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * x86 CPUID access device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * This device is accessed by lseek() to the appropriate CPUID level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * and then read in chunks of 16 bytes. A larger size means multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * reads of consecutive levels.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * The lower 32 bits of the file position is used as the incoming %eax,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * and the upper 32 bits of the file position as the incoming %ecx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * the latter intended for "counting" eax levels like eax=4.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * This driver uses /dev/cpu/%d/cpuid where %d is the minor number, and on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * an SMP box will direct the access to CPU %d.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/major.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <linux/completion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <asm/processor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <asm/msr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static struct class *cpuid_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static enum cpuhp_state cpuhp_cpuid_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct cpuid_regs_done {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct cpuid_regs regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct completion done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static void cpuid_smp_cpuid(void *cmd_block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct cpuid_regs_done *cmd = cmd_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) cpuid_count(cmd->regs.eax, cmd->regs.ecx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) &cmd->regs.eax, &cmd->regs.ebx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) &cmd->regs.ecx, &cmd->regs.edx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) complete(&cmd->done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static ssize_t cpuid_read(struct file *file, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) char __user *tmp = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct cpuid_regs_done cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) int cpu = iminor(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) u64 pos = *ppos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) ssize_t bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (count % 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return -EINVAL; /* Invalid chunk size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) init_completion(&cmd.done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) for (; count; count -= 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) call_single_data_t csd = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) .func = cpuid_smp_cpuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) .info = &cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) cmd.regs.eax = pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) cmd.regs.ecx = pos >> 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) err = smp_call_function_single_async(cpu, &csd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) wait_for_completion(&cmd.done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (copy_to_user(tmp, &cmd.regs, 16)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) tmp += 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) bytes += 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) *ppos = ++pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) reinit_completion(&cmd.done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return bytes ? bytes : err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static int cpuid_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) unsigned int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct cpuinfo_x86 *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) cpu = iminor(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (cpu >= nr_cpu_ids || !cpu_online(cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return -ENXIO; /* No such CPU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) c = &cpu_data(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (c->cpuid_level < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return -EIO; /* CPUID not supported */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * File operations we support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static const struct file_operations cpuid_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) .llseek = no_seek_end_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) .read = cpuid_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) .open = cpuid_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static int cpuid_device_create(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) dev = device_create(cpuid_class, NULL, MKDEV(CPUID_MAJOR, cpu), NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) "cpu%d", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return PTR_ERR_OR_ZERO(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static int cpuid_device_destroy(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) device_destroy(cpuid_class, MKDEV(CPUID_MAJOR, cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static char *cpuid_devnode(struct device *dev, umode_t *mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return kasprintf(GFP_KERNEL, "cpu/%u/cpuid", MINOR(dev->devt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static int __init cpuid_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (__register_chrdev(CPUID_MAJOR, 0, NR_CPUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) "cpu/cpuid", &cpuid_fops)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) printk(KERN_ERR "cpuid: unable to get major %d for cpuid\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) CPUID_MAJOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) cpuid_class = class_create(THIS_MODULE, "cpuid");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (IS_ERR(cpuid_class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) err = PTR_ERR(cpuid_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) goto out_chrdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) cpuid_class->devnode = cpuid_devnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/cpuid:online",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) cpuid_device_create, cpuid_device_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) goto out_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) cpuhp_cpuid_state = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) out_class:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) class_destroy(cpuid_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) out_chrdev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) __unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) module_init(cpuid_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static void __exit cpuid_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) cpuhp_remove_state(cpuhp_cpuid_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) class_destroy(cpuid_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) __unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) module_exit(cpuid_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) MODULE_DESCRIPTION("x86 generic CPUID driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) MODULE_LICENSE("GPL");