Thomas Gleixner | a94da20 | 2019-05-24 12:04:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* ----------------------------------------------------------------------- * |
H. Peter Anvin | 2347d93 | 2008-02-04 16:47:59 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2000-2008 H. Peter Anvin - All Rights Reserved |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * ----------------------------------------------------------------------- */ |
| 7 | |
| 8 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * x86 CPUID access device |
| 10 | * |
| 11 | * This device is accessed by lseek() to the appropriate CPUID level |
| 12 | * and then read in chunks of 16 bytes. A larger size means multiple |
| 13 | * reads of consecutive levels. |
| 14 | * |
H. Peter Anvin | 2347d93 | 2008-02-04 16:47:59 +0100 | [diff] [blame] | 15 | * The lower 32 bits of the file position is used as the incoming %eax, |
| 16 | * and the upper 32 bits of the file position as the incoming %ecx, |
| 17 | * the latter intended for "counting" eax levels like eax=4. |
| 18 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * This driver uses /dev/cpu/%d/cpuid where %d is the minor number, and on |
| 20 | * an SMP box will direct the access to CPU %d. |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | #include <linux/types.h> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/fcntl.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/poll.h> |
| 30 | #include <linux/smp.h> |
| 31 | #include <linux/major.h> |
| 32 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/device.h> |
| 34 | #include <linux/cpu.h> |
| 35 | #include <linux/notifier.h> |
Jaswinder Singh Rajput | f634fa9 | 2008-12-31 16:29:48 +0530 | [diff] [blame] | 36 | #include <linux/uaccess.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 37 | #include <linux/gfp.h> |
Eric Dumazet | 67bbd7a | 2018-03-23 14:58:18 -0700 | [diff] [blame] | 38 | #include <linux/completion.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | #include <asm/processor.h> |
| 41 | #include <asm/msr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
gregkh@suse.de | 8874b41 | 2005-03-23 09:56:34 -0800 | [diff] [blame] | 43 | static struct class *cpuid_class; |
Thomas Gleixner | ee92be9 | 2016-11-21 13:07:34 +0100 | [diff] [blame] | 44 | static enum cpuhp_state cpuhp_cpuid_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Eric Dumazet | 67bbd7a | 2018-03-23 14:58:18 -0700 | [diff] [blame] | 46 | struct cpuid_regs_done { |
| 47 | struct cpuid_regs regs; |
| 48 | struct completion done; |
| 49 | }; |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | static void cpuid_smp_cpuid(void *cmd_block) |
| 52 | { |
Eric Dumazet | 67bbd7a | 2018-03-23 14:58:18 -0700 | [diff] [blame] | 53 | struct cpuid_regs_done *cmd = cmd_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Eric Dumazet | 67bbd7a | 2018-03-23 14:58:18 -0700 | [diff] [blame] | 55 | cpuid_count(cmd->regs.eax, cmd->regs.ecx, |
| 56 | &cmd->regs.eax, &cmd->regs.ebx, |
| 57 | &cmd->regs.ecx, &cmd->regs.edx); |
| 58 | |
| 59 | complete(&cmd->done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | static ssize_t cpuid_read(struct file *file, char __user *buf, |
Jaswinder Singh Rajput | f634fa9 | 2008-12-31 16:29:48 +0530 | [diff] [blame] | 63 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
| 65 | char __user *tmp = buf; |
Eric Dumazet | 67bbd7a | 2018-03-23 14:58:18 -0700 | [diff] [blame] | 66 | struct cpuid_regs_done cmd; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 67 | int cpu = iminor(file_inode(file)); |
H. Peter Anvin | 2347d93 | 2008-02-04 16:47:59 +0100 | [diff] [blame] | 68 | u64 pos = *ppos; |
H. Peter Anvin | 9ea2b82 | 2008-08-25 17:35:25 -0700 | [diff] [blame] | 69 | ssize_t bytes = 0; |
| 70 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | if (count % 16) |
| 73 | return -EINVAL; /* Invalid chunk size */ |
| 74 | |
Eric Dumazet | 67bbd7a | 2018-03-23 14:58:18 -0700 | [diff] [blame] | 75 | init_completion(&cmd.done); |
Daniel Marjamaki | 6b7f430 | 2006-01-06 00:12:13 -0800 | [diff] [blame] | 76 | for (; count; count -= 16) { |
Eric Dumazet | 67bbd7a | 2018-03-23 14:58:18 -0700 | [diff] [blame] | 77 | call_single_data_t csd = { |
| 78 | .func = cpuid_smp_cpuid, |
| 79 | .info = &cmd, |
| 80 | }; |
| 81 | |
| 82 | cmd.regs.eax = pos; |
| 83 | cmd.regs.ecx = pos >> 32; |
| 84 | |
| 85 | err = smp_call_function_single_async(cpu, &csd); |
H. Peter Anvin | 4b46ca7 | 2008-08-25 17:28:20 -0700 | [diff] [blame] | 86 | if (err) |
H. Peter Anvin | 9ea2b82 | 2008-08-25 17:35:25 -0700 | [diff] [blame] | 87 | break; |
Eric Dumazet | 67bbd7a | 2018-03-23 14:58:18 -0700 | [diff] [blame] | 88 | wait_for_completion(&cmd.done); |
| 89 | if (copy_to_user(tmp, &cmd.regs, 16)) { |
H. Peter Anvin | 9ea2b82 | 2008-08-25 17:35:25 -0700 | [diff] [blame] | 90 | err = -EFAULT; |
| 91 | break; |
| 92 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | tmp += 16; |
H. Peter Anvin | 9ea2b82 | 2008-08-25 17:35:25 -0700 | [diff] [blame] | 94 | bytes += 16; |
H. Peter Anvin | 2347d93 | 2008-02-04 16:47:59 +0100 | [diff] [blame] | 95 | *ppos = ++pos; |
Eric Dumazet | 67bbd7a | 2018-03-23 14:58:18 -0700 | [diff] [blame] | 96 | reinit_completion(&cmd.done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
H. Peter Anvin | 9ea2b82 | 2008-08-25 17:35:25 -0700 | [diff] [blame] | 99 | return bytes ? bytes : err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static int cpuid_open(struct inode *inode, struct file *file) |
| 103 | { |
Jonathan Corbet | 5119e92 | 2008-05-15 09:12:01 -0600 | [diff] [blame] | 104 | unsigned int cpu; |
| 105 | struct cpuinfo_x86 *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 107 | cpu = iminor(file_inode(file)); |
John Kacur | 5a94361 | 2009-10-08 17:20:15 +0200 | [diff] [blame] | 108 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
| 109 | return -ENXIO; /* No such CPU */ |
| 110 | |
Jonathan Corbet | 5119e92 | 2008-05-15 09:12:01 -0600 | [diff] [blame] | 111 | c = &cpu_data(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | if (c->cpuid_level < 0) |
John Kacur | 5a94361 | 2009-10-08 17:20:15 +0200 | [diff] [blame] | 113 | return -EIO; /* CPUID not supported */ |
| 114 | |
| 115 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* |
| 119 | * File operations we support |
| 120 | */ |
Arjan van de Ven | 5dfe4c9 | 2007-02-12 00:55:31 -0800 | [diff] [blame] | 121 | static const struct file_operations cpuid_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | .owner = THIS_MODULE, |
Al Viro | b25472f | 2015-12-05 22:04:48 -0500 | [diff] [blame] | 123 | .llseek = no_seek_end_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | .read = cpuid_read, |
| 125 | .open = cpuid_open, |
| 126 | }; |
| 127 | |
Sebastian Andrzej Siewior | 8c07b49 | 2016-11-17 19:35:23 +0100 | [diff] [blame] | 128 | static int cpuid_device_create(unsigned int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
Greg Kroah-Hartman | 07accdc | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 130 | struct device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Greg Kroah-Hartman | a9b1261 | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 132 | dev = device_create(cpuid_class, NULL, MKDEV(CPUID_MAJOR, cpu), NULL, |
| 133 | "cpu%d", cpu); |
Fabian Frederick | cbda45a | 2014-10-17 22:01:17 +0200 | [diff] [blame] | 134 | return PTR_ERR_OR_ZERO(dev); |
Akinobu Mita | 1f503e7 | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 135 | } |
| 136 | |
Sebastian Andrzej Siewior | 8c07b49 | 2016-11-17 19:35:23 +0100 | [diff] [blame] | 137 | static int cpuid_device_destroy(unsigned int cpu) |
Akinobu Mita | 1f503e7 | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 138 | { |
| 139 | device_destroy(cpuid_class, MKDEV(CPUID_MAJOR, cpu)); |
Sebastian Andrzej Siewior | 8c07b49 | 2016-11-17 19:35:23 +0100 | [diff] [blame] | 140 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Al Viro | 2c9ede5 | 2011-07-23 20:24:48 -0400 | [diff] [blame] | 143 | static char *cpuid_devnode(struct device *dev, umode_t *mode) |
Kay Sievers | 07e9bb8 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 144 | { |
| 145 | return kasprintf(GFP_KERNEL, "cpu/%u/cpuid", MINOR(dev->devt)); |
| 146 | } |
| 147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | static int __init cpuid_init(void) |
| 149 | { |
Sebastian Andrzej Siewior | 8c07b49 | 2016-11-17 19:35:23 +0100 | [diff] [blame] | 150 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
H. Peter Anvin | 0b962d4 | 2009-12-15 15:13:07 -0800 | [diff] [blame] | 152 | if (__register_chrdev(CPUID_MAJOR, 0, NR_CPUS, |
| 153 | "cpu/cpuid", &cpuid_fops)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | printk(KERN_ERR "cpuid: unable to get major %d for cpuid\n", |
| 155 | CPUID_MAJOR); |
Sebastian Andrzej Siewior | 8c07b49 | 2016-11-17 19:35:23 +0100 | [diff] [blame] | 156 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
gregkh@suse.de | 8874b41 | 2005-03-23 09:56:34 -0800 | [diff] [blame] | 158 | cpuid_class = class_create(THIS_MODULE, "cpuid"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | if (IS_ERR(cpuid_class)) { |
| 160 | err = PTR_ERR(cpuid_class); |
| 161 | goto out_chrdev; |
| 162 | } |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 163 | cpuid_class->devnode = cpuid_devnode; |
Srivatsa S. Bhat | 4b660b3 | 2014-03-11 02:06:44 +0530 | [diff] [blame] | 164 | |
Thomas Gleixner | ee92be9 | 2016-11-21 13:07:34 +0100 | [diff] [blame] | 165 | err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/cpuid:online", |
Sebastian Andrzej Siewior | 8c07b49 | 2016-11-17 19:35:23 +0100 | [diff] [blame] | 166 | cpuid_device_create, cpuid_device_destroy); |
Thomas Gleixner | ee92be9 | 2016-11-21 13:07:34 +0100 | [diff] [blame] | 167 | if (err < 0) |
Sebastian Andrzej Siewior | 8c07b49 | 2016-11-17 19:35:23 +0100 | [diff] [blame] | 168 | goto out_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Thomas Gleixner | ee92be9 | 2016-11-21 13:07:34 +0100 | [diff] [blame] | 170 | cpuhp_cpuid_state = err; |
Sebastian Andrzej Siewior | 8c07b49 | 2016-11-17 19:35:23 +0100 | [diff] [blame] | 171 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | out_class: |
gregkh@suse.de | 8874b41 | 2005-03-23 09:56:34 -0800 | [diff] [blame] | 174 | class_destroy(cpuid_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | out_chrdev: |
H. Peter Anvin | 0b962d4 | 2009-12-15 15:13:07 -0800 | [diff] [blame] | 176 | __unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | return err; |
| 178 | } |
Thomas Gleixner | ee92be9 | 2016-11-21 13:07:34 +0100 | [diff] [blame] | 179 | module_init(cpuid_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
| 181 | static void __exit cpuid_exit(void) |
| 182 | { |
Thomas Gleixner | ee92be9 | 2016-11-21 13:07:34 +0100 | [diff] [blame] | 183 | cpuhp_remove_state(cpuhp_cpuid_state); |
gregkh@suse.de | 8874b41 | 2005-03-23 09:56:34 -0800 | [diff] [blame] | 184 | class_destroy(cpuid_class); |
Russ Anderson | da48247 | 2010-01-26 20:37:22 -0600 | [diff] [blame] | 185 | __unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | module_exit(cpuid_exit); |
| 188 | |
| 189 | MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>"); |
| 190 | MODULE_DESCRIPTION("x86 generic CPUID driver"); |
| 191 | MODULE_LICENSE("GPL"); |