opal-memory-errors.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/machdep.h>
  28. #include <asm/opal.h>
  29. #include <asm/cputable.h>
  30. static int opal_mem_err_nb_init;
  31. static LIST_HEAD(opal_memory_err_list);
  32. static DEFINE_SPINLOCK(opal_mem_err_lock);
  33. struct OpalMsgNode {
  34. struct list_head list;
  35. struct opal_msg msg;
  36. };
  37. static void handle_memory_error_event(struct OpalMemoryErrorData *merr_evt)
  38. {
  39. uint64_t paddr_start, paddr_end;
  40. pr_debug("%s: Retrived memory error event, type: 0x%x\n",
  41. __func__, merr_evt->type);
  42. switch (merr_evt->type) {
  43. case OPAL_MEM_ERR_TYPE_RESILIENCE:
  44. paddr_start = be64_to_cpu(merr_evt->u.resilience.physical_address_start);
  45. paddr_end = be64_to_cpu(merr_evt->u.resilience.physical_address_end);
  46. break;
  47. case OPAL_MEM_ERR_TYPE_DYN_DALLOC:
  48. paddr_start = be64_to_cpu(merr_evt->u.dyn_dealloc.physical_address_start);
  49. paddr_end = be64_to_cpu(merr_evt->u.dyn_dealloc.physical_address_end);
  50. break;
  51. default:
  52. return;
  53. }
  54. for (; paddr_start < paddr_end; paddr_start += PAGE_SIZE) {
  55. memory_failure(paddr_start >> PAGE_SHIFT, 0, 0);
  56. }
  57. }
  58. static void handle_memory_error(void)
  59. {
  60. unsigned long flags;
  61. struct OpalMemoryErrorData *merr_evt;
  62. struct OpalMsgNode *msg_node;
  63. spin_lock_irqsave(&opal_mem_err_lock, flags);
  64. while (!list_empty(&opal_memory_err_list)) {
  65. msg_node = list_entry(opal_memory_err_list.next,
  66. struct OpalMsgNode, list);
  67. list_del(&msg_node->list);
  68. spin_unlock_irqrestore(&opal_mem_err_lock, flags);
  69. merr_evt = (struct OpalMemoryErrorData *)
  70. &msg_node->msg.params[0];
  71. handle_memory_error_event(merr_evt);
  72. kfree(msg_node);
  73. spin_lock_irqsave(&opal_mem_err_lock, flags);
  74. }
  75. spin_unlock_irqrestore(&opal_mem_err_lock, flags);
  76. }
  77. static void mem_error_handler(struct work_struct *work)
  78. {
  79. handle_memory_error();
  80. }
  81. static DECLARE_WORK(mem_error_work, mem_error_handler);
  82. /*
  83. * opal_memory_err_event - notifier handler that queues up the opal message
  84. * to be preocessed later.
  85. */
  86. static int opal_memory_err_event(struct notifier_block *nb,
  87. unsigned long msg_type, void *msg)
  88. {
  89. unsigned long flags;
  90. struct OpalMsgNode *msg_node;
  91. if (msg_type != OPAL_MSG_MEM_ERR)
  92. return 0;
  93. msg_node = kzalloc(sizeof(*msg_node), GFP_ATOMIC);
  94. if (!msg_node) {
  95. pr_err("MEMORY_ERROR: out of memory, Opal message event not"
  96. "handled\n");
  97. return -ENOMEM;
  98. }
  99. memcpy(&msg_node->msg, msg, sizeof(struct opal_msg));
  100. spin_lock_irqsave(&opal_mem_err_lock, flags);
  101. list_add(&msg_node->list, &opal_memory_err_list);
  102. spin_unlock_irqrestore(&opal_mem_err_lock, flags);
  103. schedule_work(&mem_error_work);
  104. return 0;
  105. }
  106. static struct notifier_block opal_mem_err_nb = {
  107. .notifier_call = opal_memory_err_event,
  108. .next = NULL,
  109. .priority = 0,
  110. };
  111. static int __init opal_mem_err_init(void)
  112. {
  113. int ret;
  114. if (!opal_mem_err_nb_init) {
  115. ret = opal_message_notifier_register(
  116. OPAL_MSG_MEM_ERR, &opal_mem_err_nb);
  117. if (ret) {
  118. pr_err("%s: Can't register OPAL event notifier (%d)\n",
  119. __func__, ret);
  120. return ret;
  121. }
  122. opal_mem_err_nb_init = 1;
  123. }
  124. return 0;
  125. }
  126. machine_device_initcall(powernv, opal_mem_err_init);