mce_amd_inj.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * A simple MCE injection facility for testing the MCE decoding code. This
  3. * driver should be built as module so that it can be loaded on production
  4. * kernels for testing purposes.
  5. *
  6. * This file may be distributed under the terms of the GNU General Public
  7. * License version 2.
  8. *
  9. * Copyright (c) 2010: Borislav Petkov <borislav.petkov@amd.com>
  10. * Advanced Micro Devices Inc.
  11. */
  12. #include <linux/kobject.h>
  13. #include <linux/sysdev.h>
  14. #include <linux/edac.h>
  15. #include <asm/mce.h>
  16. #include "mce_amd.h"
  17. struct edac_mce_attr {
  18. struct attribute attr;
  19. ssize_t (*show) (struct kobject *kobj, struct edac_mce_attr *attr, char *buf);
  20. ssize_t (*store)(struct kobject *kobj, struct edac_mce_attr *attr,
  21. const char *buf, size_t count);
  22. };
  23. #define EDAC_MCE_ATTR(_name, _mode, _show, _store) \
  24. static struct edac_mce_attr mce_attr_##_name = __ATTR(_name, _mode, _show, _store)
  25. static struct kobject *mce_kobj;
  26. /*
  27. * Collect all the MCi_XXX settings
  28. */
  29. static struct mce i_mce;
  30. #define MCE_INJECT_STORE(reg) \
  31. static ssize_t edac_inject_##reg##_store(struct kobject *kobj, \
  32. struct edac_mce_attr *attr, \
  33. const char *data, size_t count)\
  34. { \
  35. int ret = 0; \
  36. unsigned long value; \
  37. \
  38. ret = strict_strtoul(data, 16, &value); \
  39. if (ret < 0) \
  40. printk(KERN_ERR "Error writing MCE " #reg " field.\n"); \
  41. \
  42. i_mce.reg = value; \
  43. \
  44. return count; \
  45. }
  46. MCE_INJECT_STORE(status);
  47. MCE_INJECT_STORE(misc);
  48. MCE_INJECT_STORE(addr);
  49. #define MCE_INJECT_SHOW(reg) \
  50. static ssize_t edac_inject_##reg##_show(struct kobject *kobj, \
  51. struct edac_mce_attr *attr, \
  52. char *buf) \
  53. { \
  54. return sprintf(buf, "0x%016llx\n", i_mce.reg); \
  55. }
  56. MCE_INJECT_SHOW(status);
  57. MCE_INJECT_SHOW(misc);
  58. MCE_INJECT_SHOW(addr);
  59. EDAC_MCE_ATTR(status, 0644, edac_inject_status_show, edac_inject_status_store);
  60. EDAC_MCE_ATTR(misc, 0644, edac_inject_misc_show, edac_inject_misc_store);
  61. EDAC_MCE_ATTR(addr, 0644, edac_inject_addr_show, edac_inject_addr_store);
  62. /*
  63. * This denotes into which bank we're injecting and triggers
  64. * the injection, at the same time.
  65. */
  66. static ssize_t edac_inject_bank_store(struct kobject *kobj,
  67. struct edac_mce_attr *attr,
  68. const char *data, size_t count)
  69. {
  70. int ret = 0;
  71. unsigned long value;
  72. ret = strict_strtoul(data, 10, &value);
  73. if (ret < 0) {
  74. printk(KERN_ERR "Invalid bank value!\n");
  75. return -EINVAL;
  76. }
  77. if (value > 5)
  78. if (boot_cpu_data.x86 != 0x15 || value > 6) {
  79. printk(KERN_ERR "Non-existent MCE bank: %lu\n", value);
  80. return -EINVAL;
  81. }
  82. i_mce.bank = value;
  83. amd_decode_mce(NULL, 0, &i_mce);
  84. return count;
  85. }
  86. static ssize_t edac_inject_bank_show(struct kobject *kobj,
  87. struct edac_mce_attr *attr, char *buf)
  88. {
  89. return sprintf(buf, "%d\n", i_mce.bank);
  90. }
  91. EDAC_MCE_ATTR(bank, 0644, edac_inject_bank_show, edac_inject_bank_store);
  92. static struct edac_mce_attr *sysfs_attrs[] = { &mce_attr_status, &mce_attr_misc,
  93. &mce_attr_addr, &mce_attr_bank
  94. };
  95. static int __init edac_init_mce_inject(void)
  96. {
  97. struct sysdev_class *edac_class = NULL;
  98. int i, err = 0;
  99. edac_class = edac_get_sysfs_class();
  100. if (!edac_class)
  101. return -EINVAL;
  102. mce_kobj = kobject_create_and_add("mce", &edac_class->kset.kobj);
  103. if (!mce_kobj) {
  104. printk(KERN_ERR "Error creating a mce kset.\n");
  105. err = -ENOMEM;
  106. goto err_mce_kobj;
  107. }
  108. for (i = 0; i < ARRAY_SIZE(sysfs_attrs); i++) {
  109. err = sysfs_create_file(mce_kobj, &sysfs_attrs[i]->attr);
  110. if (err) {
  111. printk(KERN_ERR "Error creating %s in sysfs.\n",
  112. sysfs_attrs[i]->attr.name);
  113. goto err_sysfs_create;
  114. }
  115. }
  116. return 0;
  117. err_sysfs_create:
  118. while (--i >= 0)
  119. sysfs_remove_file(mce_kobj, &sysfs_attrs[i]->attr);
  120. kobject_del(mce_kobj);
  121. err_mce_kobj:
  122. edac_put_sysfs_class();
  123. return err;
  124. }
  125. static void __exit edac_exit_mce_inject(void)
  126. {
  127. int i;
  128. for (i = 0; i < ARRAY_SIZE(sysfs_attrs); i++)
  129. sysfs_remove_file(mce_kobj, &sysfs_attrs[i]->attr);
  130. kobject_del(mce_kobj);
  131. edac_put_sysfs_class();
  132. }
  133. module_init(edac_init_mce_inject);
  134. module_exit(edac_exit_mce_inject);
  135. MODULE_LICENSE("GPL");
  136. MODULE_AUTHOR("Borislav Petkov <borislav.petkov@amd.com>");
  137. MODULE_AUTHOR("AMD Inc.");
  138. MODULE_DESCRIPTION("MCE injection facility for testing MCE decoding");