cpuid.c 4.3 KB

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