cpuid.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/completion.h>
  41. #include <asm/processor.h>
  42. #include <asm/msr.h>
  43. static struct class *cpuid_class;
  44. static enum cpuhp_state cpuhp_cpuid_state;
  45. struct cpuid_regs_done {
  46. struct cpuid_regs regs;
  47. struct completion done;
  48. };
  49. static void cpuid_smp_cpuid(void *cmd_block)
  50. {
  51. struct cpuid_regs_done *cmd = cmd_block;
  52. cpuid_count(cmd->regs.eax, cmd->regs.ecx,
  53. &cmd->regs.eax, &cmd->regs.ebx,
  54. &cmd->regs.ecx, &cmd->regs.edx);
  55. complete(&cmd->done);
  56. }
  57. static ssize_t cpuid_read(struct file *file, char __user *buf,
  58. size_t count, loff_t *ppos)
  59. {
  60. char __user *tmp = buf;
  61. struct cpuid_regs_done cmd;
  62. int cpu = iminor(file_inode(file));
  63. u64 pos = *ppos;
  64. ssize_t bytes = 0;
  65. int err = 0;
  66. if (count % 16)
  67. return -EINVAL; /* Invalid chunk size */
  68. init_completion(&cmd.done);
  69. for (; count; count -= 16) {
  70. call_single_data_t csd = {
  71. .func = cpuid_smp_cpuid,
  72. .info = &cmd,
  73. };
  74. cmd.regs.eax = pos;
  75. cmd.regs.ecx = pos >> 32;
  76. err = smp_call_function_single_async(cpu, &csd);
  77. if (err)
  78. break;
  79. wait_for_completion(&cmd.done);
  80. if (copy_to_user(tmp, &cmd.regs, 16)) {
  81. err = -EFAULT;
  82. break;
  83. }
  84. tmp += 16;
  85. bytes += 16;
  86. *ppos = ++pos;
  87. reinit_completion(&cmd.done);
  88. }
  89. return bytes ? bytes : err;
  90. }
  91. static int cpuid_open(struct inode *inode, struct file *file)
  92. {
  93. unsigned int cpu;
  94. struct cpuinfo_x86 *c;
  95. cpu = iminor(file_inode(file));
  96. if (cpu >= nr_cpu_ids || !cpu_online(cpu))
  97. return -ENXIO; /* No such CPU */
  98. c = &cpu_data(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 = no_seek_end_llseek,
  109. .read = cpuid_read,
  110. .open = cpuid_open,
  111. };
  112. static int cpuid_device_create(unsigned int cpu)
  113. {
  114. struct device *dev;
  115. dev = device_create(cpuid_class, NULL, MKDEV(CPUID_MAJOR, cpu), NULL,
  116. "cpu%d", cpu);
  117. return PTR_ERR_OR_ZERO(dev);
  118. }
  119. static int cpuid_device_destroy(unsigned int cpu)
  120. {
  121. device_destroy(cpuid_class, MKDEV(CPUID_MAJOR, cpu));
  122. return 0;
  123. }
  124. static char *cpuid_devnode(struct device *dev, umode_t *mode)
  125. {
  126. return kasprintf(GFP_KERNEL, "cpu/%u/cpuid", MINOR(dev->devt));
  127. }
  128. static int __init cpuid_init(void)
  129. {
  130. int err;
  131. if (__register_chrdev(CPUID_MAJOR, 0, NR_CPUS,
  132. "cpu/cpuid", &cpuid_fops)) {
  133. printk(KERN_ERR "cpuid: unable to get major %d for cpuid\n",
  134. CPUID_MAJOR);
  135. return -EBUSY;
  136. }
  137. cpuid_class = class_create(THIS_MODULE, "cpuid");
  138. if (IS_ERR(cpuid_class)) {
  139. err = PTR_ERR(cpuid_class);
  140. goto out_chrdev;
  141. }
  142. cpuid_class->devnode = cpuid_devnode;
  143. err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/cpuid:online",
  144. cpuid_device_create, cpuid_device_destroy);
  145. if (err < 0)
  146. goto out_class;
  147. cpuhp_cpuid_state = err;
  148. return 0;
  149. out_class:
  150. class_destroy(cpuid_class);
  151. out_chrdev:
  152. __unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid");
  153. return err;
  154. }
  155. module_init(cpuid_init);
  156. static void __exit cpuid_exit(void)
  157. {
  158. cpuhp_remove_state(cpuhp_cpuid_state);
  159. class_destroy(cpuid_class);
  160. __unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid");
  161. }
  162. module_exit(cpuid_exit);
  163. MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
  164. MODULE_DESCRIPTION("x86 generic CPUID driver");
  165. MODULE_LICENSE("GPL");