cpuid.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. static enum cpuhp_state cpuhp_cpuid_state;
  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 ssize_t cpuid_read(struct file *file, char __user *buf,
  54. size_t count, loff_t *ppos)
  55. {
  56. char __user *tmp = buf;
  57. struct cpuid_regs cmd;
  58. int cpu = iminor(file_inode(file));
  59. u64 pos = *ppos;
  60. ssize_t bytes = 0;
  61. int err = 0;
  62. if (count % 16)
  63. return -EINVAL; /* Invalid chunk size */
  64. for (; count; count -= 16) {
  65. cmd.eax = pos;
  66. cmd.ecx = pos >> 32;
  67. err = smp_call_function_single(cpu, cpuid_smp_cpuid, &cmd, 1);
  68. if (err)
  69. break;
  70. if (copy_to_user(tmp, &cmd, 16)) {
  71. err = -EFAULT;
  72. break;
  73. }
  74. tmp += 16;
  75. bytes += 16;
  76. *ppos = ++pos;
  77. }
  78. return bytes ? bytes : err;
  79. }
  80. static int cpuid_open(struct inode *inode, struct file *file)
  81. {
  82. unsigned int cpu;
  83. struct cpuinfo_x86 *c;
  84. cpu = iminor(file_inode(file));
  85. if (cpu >= nr_cpu_ids || !cpu_online(cpu))
  86. return -ENXIO; /* No such CPU */
  87. c = &cpu_data(cpu);
  88. if (c->cpuid_level < 0)
  89. return -EIO; /* CPUID not supported */
  90. return 0;
  91. }
  92. /*
  93. * File operations we support
  94. */
  95. static const struct file_operations cpuid_fops = {
  96. .owner = THIS_MODULE,
  97. .llseek = no_seek_end_llseek,
  98. .read = cpuid_read,
  99. .open = cpuid_open,
  100. };
  101. static int cpuid_device_create(unsigned int cpu)
  102. {
  103. struct device *dev;
  104. dev = device_create(cpuid_class, NULL, MKDEV(CPUID_MAJOR, cpu), NULL,
  105. "cpu%d", cpu);
  106. return PTR_ERR_OR_ZERO(dev);
  107. }
  108. static int cpuid_device_destroy(unsigned int cpu)
  109. {
  110. device_destroy(cpuid_class, MKDEV(CPUID_MAJOR, cpu));
  111. return 0;
  112. }
  113. static char *cpuid_devnode(struct device *dev, umode_t *mode)
  114. {
  115. return kasprintf(GFP_KERNEL, "cpu/%u/cpuid", MINOR(dev->devt));
  116. }
  117. static int __init cpuid_init(void)
  118. {
  119. int err;
  120. if (__register_chrdev(CPUID_MAJOR, 0, NR_CPUS,
  121. "cpu/cpuid", &cpuid_fops)) {
  122. printk(KERN_ERR "cpuid: unable to get major %d for cpuid\n",
  123. CPUID_MAJOR);
  124. return -EBUSY;
  125. }
  126. cpuid_class = class_create(THIS_MODULE, "cpuid");
  127. if (IS_ERR(cpuid_class)) {
  128. err = PTR_ERR(cpuid_class);
  129. goto out_chrdev;
  130. }
  131. cpuid_class->devnode = cpuid_devnode;
  132. err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/cpuid:online",
  133. cpuid_device_create, cpuid_device_destroy);
  134. if (err < 0)
  135. goto out_class;
  136. cpuhp_cpuid_state = err;
  137. return 0;
  138. out_class:
  139. class_destroy(cpuid_class);
  140. out_chrdev:
  141. __unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid");
  142. return err;
  143. }
  144. module_init(cpuid_init);
  145. static void __exit cpuid_exit(void)
  146. {
  147. cpuhp_remove_state(cpuhp_cpuid_state);
  148. class_destroy(cpuid_class);
  149. __unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid");
  150. }
  151. module_exit(cpuid_exit);
  152. MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
  153. MODULE_DESCRIPTION("x86 generic CPUID driver");
  154. MODULE_LICENSE("GPL");