opal-memory-errors.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * OPAL asynchronus Memory error handling support in PowreNV.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright 2013 IBM Corporation
  19. * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com>
  20. */
  21. #undef DEBUG
  22. #include <linux/kernel.h>
  23. #include <linux/init.h>
  24. #include <linux/of.h>
  25. #include <linux/mm.h>
  26. #include <linux/slab.h>
  27. #include <asm/opal.h>
  28. #include <asm/cputable.h>
  29. static int opal_mem_err_nb_init;
  30. static LIST_HEAD(opal_memory_err_list);
  31. static DEFINE_SPINLOCK(opal_mem_err_lock);
  32. struct OpalMsgNode {
  33. struct list_head list;
  34. struct opal_msg msg;
  35. };
  36. static void handle_memory_error_event(struct OpalMemoryErrorData *merr_evt)
  37. {
  38. uint64_t paddr_start, paddr_end;
  39. pr_debug("%s: Retrived memory error event, type: 0x%x\n",
  40. __func__, merr_evt->type);
  41. switch (merr_evt->type) {
  42. case OPAL_MEM_ERR_TYPE_RESILIENCE:
  43. paddr_start = be64_to_cpu(merr_evt->u.resilience.physical_address_start);
  44. paddr_end = be64_to_cpu(merr_evt->u.resilience.physical_address_end);
  45. break;
  46. case OPAL_MEM_ERR_TYPE_DYN_DALLOC:
  47. paddr_start = be64_to_cpu(merr_evt->u.dyn_dealloc.physical_address_start);
  48. paddr_end = be64_to_cpu(merr_evt->u.dyn_dealloc.physical_address_end);
  49. break;
  50. default:
  51. return;
  52. }
  53. for (; paddr_start < paddr_end; paddr_start += PAGE_SIZE) {
  54. memory_failure(paddr_start >> PAGE_SHIFT, 0, 0);
  55. }
  56. }
  57. static void handle_memory_error(void)
  58. {
  59. unsigned long flags;
  60. struct OpalMemoryErrorData *merr_evt;
  61. struct OpalMsgNode *msg_node;
  62. spin_lock_irqsave(&opal_mem_err_lock, flags);
  63. while (!list_empty(&opal_memory_err_list)) {
  64. msg_node = list_entry(opal_memory_err_list.next,
  65. struct OpalMsgNode, list);
  66. list_del(&msg_node->list);
  67. spin_unlock_irqrestore(&opal_mem_err_lock, flags);
  68. merr_evt = (struct OpalMemoryErrorData *)
  69. &msg_node->msg.params[0];
  70. handle_memory_error_event(merr_evt);
  71. kfree(msg_node);
  72. spin_lock_irqsave(&opal_mem_err_lock, flags);
  73. }
  74. spin_unlock_irqrestore(&opal_mem_err_lock, flags);
  75. }
  76. static void mem_error_handler(struct work_struct *work)
  77. {
  78. handle_memory_error();
  79. }
  80. static DECLARE_WORK(mem_error_work, mem_error_handler);
  81. /*
  82. * opal_memory_err_event - notifier handler that queues up the opal message
  83. * to be preocessed later.
  84. */
  85. static int opal_memory_err_event(struct notifier_block *nb,
  86. unsigned long msg_type, void *msg)
  87. {
  88. unsigned long flags;
  89. struct OpalMsgNode *msg_node;
  90. if (msg_type != OPAL_MSG_MEM_ERR)
  91. return 0;
  92. msg_node = kzalloc(sizeof(*msg_node), GFP_ATOMIC);
  93. if (!msg_node) {
  94. pr_err("MEMORY_ERROR: out of memory, Opal message event not"
  95. "handled\n");
  96. return -ENOMEM;
  97. }
  98. memcpy(&msg_node->msg, msg, sizeof(struct opal_msg));
  99. spin_lock_irqsave(&opal_mem_err_lock, flags);
  100. list_add(&msg_node->list, &opal_memory_err_list);
  101. spin_unlock_irqrestore(&opal_mem_err_lock, flags);
  102. schedule_work(&mem_error_work);
  103. return 0;
  104. }
  105. static struct notifier_block opal_mem_err_nb = {
  106. .notifier_call = opal_memory_err_event,
  107. .next = NULL,
  108. .priority = 0,
  109. };
  110. static int __init opal_mem_err_init(void)
  111. {
  112. int ret;
  113. if (!opal_mem_err_nb_init) {
  114. ret = opal_message_notifier_register(
  115. OPAL_MSG_MEM_ERR, &opal_mem_err_nb);
  116. if (ret) {
  117. pr_err("%s: Can't register OPAL event notifier (%d)\n",
  118. __func__, ret);
  119. return ret;
  120. }
  121. opal_mem_err_nb_init = 1;
  122. }
  123. return 0;
  124. }
  125. subsys_initcall(opal_mem_err_init);