cpuid.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <asm/processor.h>
  41. #include <asm/msr.h>
  42. static struct class *cpuid_class;
  43. struct cpuid_regs {
  44. u32 eax, ebx, ecx, edx;
  45. };
  46. static void cpuid_smp_cpuid(void *cmd_block)
  47. {
  48. struct cpuid_regs *cmd = (struct cpuid_regs *)cmd_block;
  49. cpuid_count(cmd->eax, cmd->ecx,
  50. &cmd->eax, &cmd->ebx, &cmd->ecx, &cmd->edx);
  51. }
  52. static ssize_t cpuid_read(struct file *file, char __user *buf,
  53. size_t count, loff_t *ppos)
  54. {
  55. char __user *tmp = buf;
  56. struct cpuid_regs cmd;
  57. int cpu = iminor(file_inode(file));
  58. u64 pos = *ppos;
  59. ssize_t bytes = 0;
  60. int err = 0;
  61. if (count % 16)
  62. return -EINVAL; /* Invalid chunk size */
  63. for (; count; count -= 16) {
  64. cmd.eax = pos;
  65. cmd.ecx = pos >> 32;
  66. err = smp_call_function_single(cpu, cpuid_smp_cpuid, &cmd, 1);
  67. if (err)
  68. break;
  69. if (copy_to_user(tmp, &cmd, 16)) {
  70. err = -EFAULT;
  71. break;
  72. }
  73. tmp += 16;
  74. bytes += 16;
  75. *ppos = ++pos;
  76. }
  77. return bytes ? bytes : err;
  78. }
  79. static int cpuid_open(struct inode *inode, struct file *file)
  80. {
  81. unsigned int cpu;
  82. struct cpuinfo_x86 *c;
  83. cpu = iminor(file_inode(file));
  84. if (cpu >= nr_cpu_ids || !cpu_online(cpu))
  85. return -ENXIO; /* No such CPU */
  86. c = &cpu_data(cpu);
  87. if (c->cpuid_level < 0)
  88. return -EIO; /* CPUID not supported */
  89. return 0;
  90. }
  91. /*
  92. * File operations we support
  93. */
  94. static const struct file_operations cpuid_fops = {
  95. .owner = THIS_MODULE,
  96. .llseek = no_seek_end_llseek,
  97. .read = cpuid_read,
  98. .open = cpuid_open,
  99. };
  100. static int cpuid_device_create(int cpu)
  101. {
  102. struct device *dev;
  103. dev = device_create(cpuid_class, NULL, MKDEV(CPUID_MAJOR, cpu), NULL,
  104. "cpu%d", cpu);
  105. return PTR_ERR_OR_ZERO(dev);
  106. }
  107. static void cpuid_device_destroy(int cpu)
  108. {
  109. device_destroy(cpuid_class, MKDEV(CPUID_MAJOR, cpu));
  110. }
  111. static int cpuid_class_cpu_callback(struct notifier_block *nfb,
  112. unsigned long action, void *hcpu)
  113. {
  114. unsigned int cpu = (unsigned long)hcpu;
  115. int err = 0;
  116. switch (action) {
  117. case CPU_UP_PREPARE:
  118. err = cpuid_device_create(cpu);
  119. break;
  120. case CPU_UP_CANCELED:
  121. case CPU_UP_CANCELED_FROZEN:
  122. case CPU_DEAD:
  123. cpuid_device_destroy(cpu);
  124. break;
  125. }
  126. return notifier_from_errno(err);
  127. }
  128. static struct notifier_block cpuid_class_cpu_notifier =
  129. {
  130. .notifier_call = cpuid_class_cpu_callback,
  131. };
  132. static char *cpuid_devnode(struct device *dev, umode_t *mode)
  133. {
  134. return kasprintf(GFP_KERNEL, "cpu/%u/cpuid", MINOR(dev->devt));
  135. }
  136. static int __init cpuid_init(void)
  137. {
  138. int i, err = 0;
  139. i = 0;
  140. if (__register_chrdev(CPUID_MAJOR, 0, NR_CPUS,
  141. "cpu/cpuid", &cpuid_fops)) {
  142. printk(KERN_ERR "cpuid: unable to get major %d for cpuid\n",
  143. CPUID_MAJOR);
  144. err = -EBUSY;
  145. goto out;
  146. }
  147. cpuid_class = class_create(THIS_MODULE, "cpuid");
  148. if (IS_ERR(cpuid_class)) {
  149. err = PTR_ERR(cpuid_class);
  150. goto out_chrdev;
  151. }
  152. cpuid_class->devnode = cpuid_devnode;
  153. cpu_notifier_register_begin();
  154. for_each_online_cpu(i) {
  155. err = cpuid_device_create(i);
  156. if (err != 0)
  157. goto out_class;
  158. }
  159. __register_hotcpu_notifier(&cpuid_class_cpu_notifier);
  160. cpu_notifier_register_done();
  161. err = 0;
  162. goto out;
  163. out_class:
  164. i = 0;
  165. for_each_online_cpu(i) {
  166. cpuid_device_destroy(i);
  167. }
  168. cpu_notifier_register_done();
  169. class_destroy(cpuid_class);
  170. out_chrdev:
  171. __unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid");
  172. out:
  173. return err;
  174. }
  175. static void __exit cpuid_exit(void)
  176. {
  177. int cpu = 0;
  178. cpu_notifier_register_begin();
  179. for_each_online_cpu(cpu)
  180. cpuid_device_destroy(cpu);
  181. class_destroy(cpuid_class);
  182. __unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid");
  183. __unregister_hotcpu_notifier(&cpuid_class_cpu_notifier);
  184. cpu_notifier_register_done();
  185. }
  186. module_init(cpuid_init);
  187. module_exit(cpuid_exit);
  188. MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
  189. MODULE_DESCRIPTION("x86 generic CPUID driver");
  190. MODULE_LICENSE("GPL");