1 // SPDX-License-Identifier: GPL-2.0
2 
3 /*
4  * Handles hot and cold plug of persistent memory regions on pseries.
5  */
6 
7 #define pr_fmt(fmt)     "pseries-pmem: " fmt
8 
9 #include <linux/kernel.h>
10 #include <linux/interrupt.h>
11 #include <linux/delay.h>
12 #include <linux/sched.h>	/* for idle_task_exit */
13 #include <linux/sched/hotplug.h>
14 #include <linux/cpu.h>
15 #include <linux/of.h>
16 #include <linux/of_platform.h>
17 #include <linux/slab.h>
18 #include <asm/prom.h>
19 #include <asm/rtas.h>
20 #include <asm/firmware.h>
21 #include <asm/machdep.h>
22 #include <asm/vdso_datapage.h>
23 #include <asm/plpar_wrappers.h>
24 #include <asm/topology.h>
25 
26 #include "pseries.h"
27 
28 static struct device_node *pmem_node;
29 
pmem_drc_add_node(u32 drc_index)30 static ssize_t pmem_drc_add_node(u32 drc_index)
31 {
32 	struct device_node *dn;
33 	int rc;
34 
35 	pr_debug("Attempting to add pmem node, drc index: %x\n", drc_index);
36 
37 	rc = dlpar_acquire_drc(drc_index);
38 	if (rc) {
39 		pr_err("Failed to acquire DRC, rc: %d, drc index: %x\n",
40 			rc, drc_index);
41 		return -EINVAL;
42 	}
43 
44 	dn = dlpar_configure_connector(cpu_to_be32(drc_index), pmem_node);
45 	if (!dn) {
46 		pr_err("configure-connector failed for drc %x\n", drc_index);
47 		dlpar_release_drc(drc_index);
48 		return -EINVAL;
49 	}
50 
51 	/* NB: The of reconfig notifier creates platform device from the node */
52 	rc = dlpar_attach_node(dn, pmem_node);
53 	if (rc) {
54 		pr_err("Failed to attach node %pOF, rc: %d, drc index: %x\n",
55 			dn, rc, drc_index);
56 
57 		if (dlpar_release_drc(drc_index))
58 			dlpar_free_cc_nodes(dn);
59 
60 		return rc;
61 	}
62 
63 	pr_info("Successfully added %pOF, drc index: %x\n", dn, drc_index);
64 
65 	return 0;
66 }
67 
pmem_drc_remove_node(u32 drc_index)68 static ssize_t pmem_drc_remove_node(u32 drc_index)
69 {
70 	struct device_node *dn;
71 	uint32_t index;
72 	int rc;
73 
74 	for_each_child_of_node(pmem_node, dn) {
75 		if (of_property_read_u32(dn, "ibm,my-drc-index", &index))
76 			continue;
77 		if (index == drc_index)
78 			break;
79 	}
80 
81 	if (!dn) {
82 		pr_err("Attempting to remove unused DRC index %x\n", drc_index);
83 		return -ENODEV;
84 	}
85 
86 	pr_debug("Attempting to remove %pOF, drc index: %x\n", dn, drc_index);
87 
88 	/* * NB: tears down the ibm,pmemory device as a side-effect */
89 	rc = dlpar_detach_node(dn);
90 	if (rc)
91 		return rc;
92 
93 	rc = dlpar_release_drc(drc_index);
94 	if (rc) {
95 		pr_err("Failed to release drc (%x) for CPU %pOFn, rc: %d\n",
96 			drc_index, dn, rc);
97 		dlpar_attach_node(dn, pmem_node);
98 		return rc;
99 	}
100 
101 	pr_info("Successfully removed PMEM with drc index: %x\n", drc_index);
102 
103 	return 0;
104 }
105 
dlpar_hp_pmem(struct pseries_hp_errorlog * hp_elog)106 int dlpar_hp_pmem(struct pseries_hp_errorlog *hp_elog)
107 {
108 	u32 drc_index;
109 	int rc;
110 
111 	/* slim chance, but we might get a hotplug event while booting */
112 	if (!pmem_node)
113 		pmem_node = of_find_node_by_type(NULL, "ibm,persistent-memory");
114 	if (!pmem_node) {
115 		pr_err("Hotplug event for a pmem device, but none exists\n");
116 		return -ENODEV;
117 	}
118 
119 	if (hp_elog->id_type != PSERIES_HP_ELOG_ID_DRC_INDEX) {
120 		pr_err("Unsupported hotplug event type %d\n",
121 				hp_elog->id_type);
122 		return -EINVAL;
123 	}
124 
125 	drc_index = hp_elog->_drc_u.drc_index;
126 
127 	lock_device_hotplug();
128 
129 	if (hp_elog->action == PSERIES_HP_ELOG_ACTION_ADD) {
130 		rc = pmem_drc_add_node(drc_index);
131 	} else if (hp_elog->action == PSERIES_HP_ELOG_ACTION_REMOVE) {
132 		rc = pmem_drc_remove_node(drc_index);
133 	} else {
134 		pr_err("Unsupported hotplug action (%d)\n", hp_elog->action);
135 		rc = -EINVAL;
136 	}
137 
138 	unlock_device_hotplug();
139 	return rc;
140 }
141 
142 static const struct of_device_id drc_pmem_match[] = {
143 	{ .type = "ibm,persistent-memory", },
144 	{}
145 };
146 
pseries_pmem_init(void)147 static int pseries_pmem_init(void)
148 {
149 	/*
150 	 * Only supported on POWER8 and above.
151 	 */
152 	if (!cpu_has_feature(CPU_FTR_ARCH_207S))
153 		return 0;
154 
155 	pmem_node = of_find_node_by_type(NULL, "ibm,persistent-memory");
156 	if (!pmem_node)
157 		return 0;
158 
159 	/*
160 	 * The generic OF bus probe/populate handles creating platform devices
161 	 * from the child (ibm,pmemory) nodes. The generic code registers an of
162 	 * reconfig notifier to handle the hot-add/remove cases too.
163 	 */
164 	of_platform_bus_probe(pmem_node, drc_pmem_match, NULL);
165 
166 	return 0;
167 }
168 machine_arch_initcall(pseries, pseries_pmem_init);
169