cpuid.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 2000-2008 H. Peter Anvin - All Rights Reserved
  4. *
  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.
  10. *
  11. * ----------------------------------------------------------------------- */
  12. /*
  13. * x86 CPUID access device
  14. *
  15. * This device is accessed by lseek() to the appropriate CPUID level
  16. * and then read in chunks of 16 bytes. A larger size means multiple
  17. * reads of consecutive levels.
  18. *
  19. * The lower 32 bits of the file position is used as the incoming %eax,
  20. * and the upper 32 bits of the file position as the incoming %ecx,
  21. * the latter intended for "counting" eax levels like eax=4.
  22. *
  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.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/types.h>
  28. #include <linux/errno.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/init.h>
  31. #include <linux/poll.h>
  32. #include <linux/smp.h>
  33. #include <linux/major.h>
  34. #include <linux/fs.h>
  35. #include <linux/device.h>
  36. #include <linux/cpu.h>
  37. #include <linux/notifier.h>
  38. #include <linux/uaccess.h>
  39. #include <linux/gfp.h>
  40. #include <linux/slab.h>
  41. #include <asm/processor.h>
  42. #include <asm/msr.h>
  43. #include <asm/system.h>
  44. static struct class *cpuid_class;
  45. struct cpuid_regs {
  46. u32 eax, ebx, ecx, edx;
  47. };
  48. static void cpuid_smp_cpuid(void *cmd_block)
  49. {
  50. struct cpuid_regs *cmd = (struct cpuid_regs *)cmd_block;
  51. cpuid_count(cmd->eax, cmd->ecx,
  52. &cmd->eax, &cmd->ebx, &cmd->ecx, &cmd->edx);
  53. }
  54. static loff_t cpuid_seek(struct file *file, loff_t offset, int orig)
  55. {
  56. loff_t ret;
  57. struct inode *inode = file->f_mapping->host;
  58. mutex_lock(&inode->i_mutex);
  59. switch (orig) {
  60. case 0:
  61. file->f_pos = offset;
  62. ret = file->f_pos;
  63. break;
  64. case 1:
  65. file->f_pos += offset;
  66. ret = file->f_pos;
  67. break;
  68. default:
  69. ret = -EINVAL;
  70. }
  71. mutex_unlock(&inode->i_mutex);
  72. return ret;
  73. }
  74. static ssize_t cpuid_read(struct file *file, char __user *buf,
  75. size_t count, loff_t *ppos)
  76. {
  77. char __user *tmp = buf;
  78. struct cpuid_regs cmd;
  79. int cpu = iminor(file->f_path.dentry->d_inode);
  80. u64 pos = *ppos;
  81. ssize_t bytes = 0;
  82. int err = 0;
  83. if (count % 16)
  84. return -EINVAL; /* Invalid chunk size */
  85. for (; count; count -= 16) {
  86. cmd.eax = pos;
  87. cmd.ecx = pos >> 32;
  88. err = smp_call_function_single(cpu, cpuid_smp_cpuid, &cmd, 1);
  89. if (err)
  90. break;
  91. if (copy_to_user(tmp, &cmd, 16)) {
  92. err = -EFAULT;
  93. break;
  94. }
  95. tmp += 16;
  96. bytes += 16;
  97. *ppos = ++pos;
  98. }
  99. return bytes ? bytes : err;
  100. }
  101. static int cpuid_open(struct inode *inode, struct file *file)
  102. {
  103. unsigned int cpu;
  104. struct cpuinfo_x86 *c;
  105. cpu = iminor(file->f_path.dentry->d_inode);
  106. if (cpu >= nr_cpu_ids || !cpu_online(cpu))
  107. return -ENXIO; /* No such CPU */
  108. c = &cpu_data(cpu);
  109. if (c->cpuid_level < 0)
  110. return -EIO; /* CPUID not supported */
  111. return 0;
  112. }
  113. /*
  114. * File operations we support
  115. */
  116. static const struct file_operations cpuid_fops = {
  117. .owner = THIS_MODULE,
  118. .llseek = cpuid_seek,
  119. .read = cpuid_read,
  120. .open = cpuid_open,
  121. };
  122. static ssize_t print_cpu_modalias(struct device *dev,
  123. struct device_attribute *attr,
  124. char *bufptr)
  125. {
  126. int size = PAGE_SIZE;
  127. int i, n;
  128. char *buf = bufptr;
  129. n = snprintf(buf, size, "x86cpu:vendor:%04X:family:"
  130. "%04X:model:%04X:feature:",
  131. boot_cpu_data.x86_vendor,
  132. boot_cpu_data.x86,
  133. boot_cpu_data.x86_model);
  134. size -= n;
  135. buf += n;
  136. size -= 2;
  137. for (i = 0; i < NCAPINTS*32; i++) {
  138. if (boot_cpu_has(i)) {
  139. n = snprintf(buf, size, ",%04X", i);
  140. if (n < 0) {
  141. WARN(1, "x86 features overflow page\n");
  142. break;
  143. }
  144. size -= n;
  145. buf += n;
  146. }
  147. }
  148. *buf++ = ',';
  149. *buf++ = '\n';
  150. return buf - bufptr;
  151. }
  152. static DEVICE_ATTR(modalias, 0444, print_cpu_modalias, NULL);
  153. static __cpuinit int cpuid_device_create(int cpu)
  154. {
  155. struct device *dev;
  156. int err;
  157. dev = device_create(cpuid_class, NULL, MKDEV(CPUID_MAJOR, cpu), NULL,
  158. "cpu%d", cpu);
  159. if (IS_ERR(dev))
  160. return PTR_ERR(dev);
  161. err = device_create_file(dev, &dev_attr_modalias);
  162. if (err) {
  163. /* keep device around on error. attribute is optional. */
  164. err = 0;
  165. }
  166. return 0;
  167. }
  168. static void cpuid_device_destroy(int cpu)
  169. {
  170. device_destroy(cpuid_class, MKDEV(CPUID_MAJOR, cpu));
  171. }
  172. static int __cpuinit cpuid_class_cpu_callback(struct notifier_block *nfb,
  173. unsigned long action,
  174. void *hcpu)
  175. {
  176. unsigned int cpu = (unsigned long)hcpu;
  177. int err = 0;
  178. switch (action) {
  179. case CPU_UP_PREPARE:
  180. err = cpuid_device_create(cpu);
  181. break;
  182. case CPU_UP_CANCELED:
  183. case CPU_UP_CANCELED_FROZEN:
  184. case CPU_DEAD:
  185. cpuid_device_destroy(cpu);
  186. break;
  187. }
  188. return notifier_from_errno(err);
  189. }
  190. static struct notifier_block __refdata cpuid_class_cpu_notifier =
  191. {
  192. .notifier_call = cpuid_class_cpu_callback,
  193. };
  194. static char *cpuid_devnode(struct device *dev, umode_t *mode)
  195. {
  196. return kasprintf(GFP_KERNEL, "cpu/%u/cpuid", MINOR(dev->devt));
  197. }
  198. static int cpuid_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
  199. {
  200. char *buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
  201. if (buf) {
  202. print_cpu_modalias(NULL, NULL, buf);
  203. add_uevent_var(env, "MODALIAS=%s", buf);
  204. kfree(buf);
  205. }
  206. return 0;
  207. }
  208. static int __init cpuid_init(void)
  209. {
  210. int i, err = 0;
  211. i = 0;
  212. if (__register_chrdev(CPUID_MAJOR, 0, NR_CPUS,
  213. "cpu/cpuid", &cpuid_fops)) {
  214. printk(KERN_ERR "cpuid: unable to get major %d for cpuid\n",
  215. CPUID_MAJOR);
  216. err = -EBUSY;
  217. goto out;
  218. }
  219. cpuid_class = class_create(THIS_MODULE, "cpuid");
  220. if (IS_ERR(cpuid_class)) {
  221. err = PTR_ERR(cpuid_class);
  222. goto out_chrdev;
  223. }
  224. cpuid_class->devnode = cpuid_devnode;
  225. cpuid_class->dev_uevent = cpuid_dev_uevent;
  226. for_each_online_cpu(i) {
  227. err = cpuid_device_create(i);
  228. if (err != 0)
  229. goto out_class;
  230. }
  231. register_hotcpu_notifier(&cpuid_class_cpu_notifier);
  232. err = 0;
  233. goto out;
  234. out_class:
  235. i = 0;
  236. for_each_online_cpu(i) {
  237. cpuid_device_destroy(i);
  238. }
  239. class_destroy(cpuid_class);
  240. out_chrdev:
  241. __unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid");
  242. out:
  243. return err;
  244. }
  245. static void __exit cpuid_exit(void)
  246. {
  247. int cpu = 0;
  248. for_each_online_cpu(cpu)
  249. cpuid_device_destroy(cpu);
  250. class_destroy(cpuid_class);
  251. __unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid");
  252. unregister_hotcpu_notifier(&cpuid_class_cpu_notifier);
  253. }
  254. module_init(cpuid_init);
  255. module_exit(cpuid_exit);
  256. MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
  257. MODULE_DESCRIPTION("x86 generic CPUID driver");
  258. MODULE_LICENSE("GPL");