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