opal-memory-errors.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * OPAL asynchronus Memory error handling support in PowerNV.
  4. *
  5. * Copyright 2013 IBM Corporation
  6. * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com>
  7. */
  8. #undef DEBUG
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/of.h>
  12. #include <linux/mm.h>
  13. #include <linux/slab.h>
  14. #include <asm/machdep.h>
  15. #include <asm/opal.h>
  16. #include <asm/cputable.h>
  17. static int opal_mem_err_nb_init;
  18. static LIST_HEAD(opal_memory_err_list);
  19. static DEFINE_SPINLOCK(opal_mem_err_lock);
  20. struct OpalMsgNode {
  21. struct list_head list;
  22. struct opal_msg msg;
  23. };
  24. static void handle_memory_error_event(struct OpalMemoryErrorData *merr_evt)
  25. {
  26. uint64_t paddr_start, paddr_end;
  27. pr_debug("%s: Retrieved memory error event, type: 0x%x\n",
  28. __func__, merr_evt->type);
  29. switch (merr_evt->type) {
  30. case OPAL_MEM_ERR_TYPE_RESILIENCE:
  31. paddr_start = be64_to_cpu(merr_evt->u.resilience.physical_address_start);
  32. paddr_end = be64_to_cpu(merr_evt->u.resilience.physical_address_end);
  33. break;
  34. case OPAL_MEM_ERR_TYPE_DYN_DALLOC:
  35. paddr_start = be64_to_cpu(merr_evt->u.dyn_dealloc.physical_address_start);
  36. paddr_end = be64_to_cpu(merr_evt->u.dyn_dealloc.physical_address_end);
  37. break;
  38. default:
  39. return;
  40. }
  41. for (; paddr_start < paddr_end; paddr_start += PAGE_SIZE) {
  42. memory_failure(paddr_start >> PAGE_SHIFT, 0);
  43. }
  44. }
  45. static void handle_memory_error(void)
  46. {
  47. unsigned long flags;
  48. struct OpalMemoryErrorData *merr_evt;
  49. struct OpalMsgNode *msg_node;
  50. spin_lock_irqsave(&opal_mem_err_lock, flags);
  51. while (!list_empty(&opal_memory_err_list)) {
  52. msg_node = list_entry(opal_memory_err_list.next,
  53. struct OpalMsgNode, list);
  54. list_del(&msg_node->list);
  55. spin_unlock_irqrestore(&opal_mem_err_lock, flags);
  56. merr_evt = (struct OpalMemoryErrorData *)
  57. &msg_node->msg.params[0];
  58. handle_memory_error_event(merr_evt);
  59. kfree(msg_node);
  60. spin_lock_irqsave(&opal_mem_err_lock, flags);
  61. }
  62. spin_unlock_irqrestore(&opal_mem_err_lock, flags);
  63. }
  64. static void mem_error_handler(struct work_struct *work)
  65. {
  66. handle_memory_error();
  67. }
  68. static DECLARE_WORK(mem_error_work, mem_error_handler);
  69. /*
  70. * opal_memory_err_event - notifier handler that queues up the opal message
  71. * to be processed later.
  72. */
  73. static int opal_memory_err_event(struct notifier_block *nb,
  74. unsigned long msg_type, void *msg)
  75. {
  76. unsigned long flags;
  77. struct OpalMsgNode *msg_node;
  78. if (msg_type != OPAL_MSG_MEM_ERR)
  79. return 0;
  80. msg_node = kzalloc(sizeof(*msg_node), GFP_ATOMIC);
  81. if (!msg_node) {
  82. pr_err("MEMORY_ERROR: out of memory, Opal message event not"
  83. "handled\n");
  84. return -ENOMEM;
  85. }
  86. memcpy(&msg_node->msg, msg, sizeof(msg_node->msg));
  87. spin_lock_irqsave(&opal_mem_err_lock, flags);
  88. list_add(&msg_node->list, &opal_memory_err_list);
  89. spin_unlock_irqrestore(&opal_mem_err_lock, flags);
  90. schedule_work(&mem_error_work);
  91. return 0;
  92. }
  93. static struct notifier_block opal_mem_err_nb = {
  94. .notifier_call = opal_memory_err_event,
  95. .next = NULL,
  96. .priority = 0,
  97. };
  98. static int __init opal_mem_err_init(void)
  99. {
  100. int ret;
  101. if (!opal_mem_err_nb_init) {
  102. ret = opal_message_notifier_register(
  103. OPAL_MSG_MEM_ERR, &opal_mem_err_nb);
  104. if (ret) {
  105. pr_err("%s: Can't register OPAL event notifier (%d)\n",
  106. __func__, ret);
  107. return ret;
  108. }
  109. opal_mem_err_nb_init = 1;
  110. }
  111. return 0;
  112. }
  113. machine_device_initcall(powernv, opal_mem_err_init);