Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * OPAL asynchronus Memory error handling support in PowerNV.
0004  *
0005  * Copyright 2013 IBM Corporation
0006  * Author: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com>
0007  */
0008 
0009 #undef DEBUG
0010 
0011 #include <linux/kernel.h>
0012 #include <linux/init.h>
0013 #include <linux/of.h>
0014 #include <linux/mm.h>
0015 #include <linux/slab.h>
0016 
0017 #include <asm/machdep.h>
0018 #include <asm/opal.h>
0019 #include <asm/cputable.h>
0020 
0021 static int opal_mem_err_nb_init;
0022 static LIST_HEAD(opal_memory_err_list);
0023 static DEFINE_SPINLOCK(opal_mem_err_lock);
0024 
0025 struct OpalMsgNode {
0026     struct list_head list;
0027     struct opal_msg msg;
0028 };
0029 
0030 static void handle_memory_error_event(struct OpalMemoryErrorData *merr_evt)
0031 {
0032     uint64_t paddr_start, paddr_end;
0033 
0034     pr_debug("%s: Retrieved memory error event, type: 0x%x\n",
0035           __func__, merr_evt->type);
0036     switch (merr_evt->type) {
0037     case OPAL_MEM_ERR_TYPE_RESILIENCE:
0038         paddr_start = be64_to_cpu(merr_evt->u.resilience.physical_address_start);
0039         paddr_end = be64_to_cpu(merr_evt->u.resilience.physical_address_end);
0040         break;
0041     case OPAL_MEM_ERR_TYPE_DYN_DALLOC:
0042         paddr_start = be64_to_cpu(merr_evt->u.dyn_dealloc.physical_address_start);
0043         paddr_end = be64_to_cpu(merr_evt->u.dyn_dealloc.physical_address_end);
0044         break;
0045     default:
0046         return;
0047     }
0048 
0049     for (; paddr_start < paddr_end; paddr_start += PAGE_SIZE) {
0050         memory_failure(paddr_start >> PAGE_SHIFT, 0);
0051     }
0052 }
0053 
0054 static void handle_memory_error(void)
0055 {
0056     unsigned long flags;
0057     struct OpalMemoryErrorData *merr_evt;
0058     struct OpalMsgNode *msg_node;
0059 
0060     spin_lock_irqsave(&opal_mem_err_lock, flags);
0061     while (!list_empty(&opal_memory_err_list)) {
0062          msg_node = list_entry(opal_memory_err_list.next,
0063                        struct OpalMsgNode, list);
0064         list_del(&msg_node->list);
0065         spin_unlock_irqrestore(&opal_mem_err_lock, flags);
0066 
0067         merr_evt = (struct OpalMemoryErrorData *)
0068                     &msg_node->msg.params[0];
0069         handle_memory_error_event(merr_evt);
0070         kfree(msg_node);
0071         spin_lock_irqsave(&opal_mem_err_lock, flags);
0072     }
0073     spin_unlock_irqrestore(&opal_mem_err_lock, flags);
0074 }
0075 
0076 static void mem_error_handler(struct work_struct *work)
0077 {
0078     handle_memory_error();
0079 }
0080 
0081 static DECLARE_WORK(mem_error_work, mem_error_handler);
0082 
0083 /*
0084  * opal_memory_err_event - notifier handler that queues up the opal message
0085  * to be processed later.
0086  */
0087 static int opal_memory_err_event(struct notifier_block *nb,
0088               unsigned long msg_type, void *msg)
0089 {
0090     unsigned long flags;
0091     struct OpalMsgNode *msg_node;
0092 
0093     if (msg_type != OPAL_MSG_MEM_ERR)
0094         return 0;
0095 
0096     msg_node = kzalloc(sizeof(*msg_node), GFP_ATOMIC);
0097     if (!msg_node) {
0098         pr_err("MEMORY_ERROR: out of memory, Opal message event not"
0099                "handled\n");
0100         return -ENOMEM;
0101     }
0102     memcpy(&msg_node->msg, msg, sizeof(msg_node->msg));
0103 
0104     spin_lock_irqsave(&opal_mem_err_lock, flags);
0105     list_add(&msg_node->list, &opal_memory_err_list);
0106     spin_unlock_irqrestore(&opal_mem_err_lock, flags);
0107 
0108     schedule_work(&mem_error_work);
0109     return 0;
0110 }
0111 
0112 static struct notifier_block opal_mem_err_nb = {
0113     .notifier_call  = opal_memory_err_event,
0114     .next       = NULL,
0115     .priority   = 0,
0116 };
0117 
0118 static int __init opal_mem_err_init(void)
0119 {
0120     int ret;
0121 
0122     if (!opal_mem_err_nb_init) {
0123         ret = opal_message_notifier_register(
0124                     OPAL_MSG_MEM_ERR, &opal_mem_err_nb);
0125         if (ret) {
0126             pr_err("%s: Can't register OPAL event notifier (%d)\n",
0127                    __func__, ret);
0128             return ret;
0129         }
0130         opal_mem_err_nb_init = 1;
0131     }
0132     return 0;
0133 }
0134 machine_device_initcall(powernv, opal_mem_err_init);