blob: be11fc3cdeb063ff044f613ec3b4eec6e37f19e0 [file] [log] [blame]
Badari Pulavarty57b53922008-04-18 13:33:50 -07001/*
2 * pseries Memory Hotplug infrastructure.
3 *
4 * Copyright (C) 2008 Badari Pulavarty, IBM Corporation
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
Nathan Fontenot999e2da2015-02-10 13:47:02 -060012#define pr_fmt(fmt) "pseries-hotplug-mem: " fmt
13
Badari Pulavarty57b53922008-04-18 13:33:50 -070014#include <linux/of.h>
Rob Herring26a20562013-09-26 07:40:04 -050015#include <linux/of_address.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100016#include <linux/memblock.h>
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100017#include <linux/memory.h>
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -060018#include <linux/memory_hotplug.h>
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -060019#include <linux/slab.h>
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100020
Badari Pulavarty57b53922008-04-18 13:33:50 -070021#include <asm/firmware.h>
22#include <asm/machdep.h>
Rob Herring26a20562013-09-26 07:40:04 -050023#include <asm/prom.h>
Michael Neuling0b2f8282009-02-08 14:49:39 +000024#include <asm/sparsemem.h>
Anton Blanchard1217d342014-08-20 08:55:19 +100025#include "pseries.h"
Badari Pulavarty57b53922008-04-18 13:33:50 -070026
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -060027static bool rtas_hp_event;
28
Anton Blancharda5d86252014-06-04 17:50:47 +100029unsigned long pseries_memory_block_size(void)
Nathan Fontenotc540ada2011-01-20 10:45:20 -060030{
31 struct device_node *np;
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100032 unsigned int memblock_size = MIN_MEMORY_BLOCK_SIZE;
33 struct resource r;
Nathan Fontenotc540ada2011-01-20 10:45:20 -060034
35 np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
36 if (np) {
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100037 const __be64 *size;
Nathan Fontenotc540ada2011-01-20 10:45:20 -060038
39 size = of_get_property(np, "ibm,lmb-size", NULL);
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100040 if (size)
41 memblock_size = be64_to_cpup(size);
Nathan Fontenotc540ada2011-01-20 10:45:20 -060042 of_node_put(np);
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100043 } else if (machine_is(pseries)) {
44 /* This fallback really only applies to pseries */
Nathan Fontenotc540ada2011-01-20 10:45:20 -060045 unsigned int memzero_size = 0;
Nathan Fontenotc540ada2011-01-20 10:45:20 -060046
47 np = of_find_node_by_path("/memory@0");
48 if (np) {
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100049 if (!of_address_to_resource(np, 0, &r))
50 memzero_size = resource_size(&r);
Nathan Fontenotc540ada2011-01-20 10:45:20 -060051 of_node_put(np);
52 }
53
54 if (memzero_size) {
55 /* We now know the size of memory@0, use this to find
56 * the first memoryblock and get its size.
57 */
58 char buf[64];
59
60 sprintf(buf, "/memory@%x", memzero_size);
61 np = of_find_node_by_path(buf);
62 if (np) {
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100063 if (!of_address_to_resource(np, 0, &r))
64 memblock_size = resource_size(&r);
Nathan Fontenotc540ada2011-01-20 10:45:20 -060065 of_node_put(np);
66 }
67 }
68 }
Nathan Fontenotc540ada2011-01-20 10:45:20 -060069 return memblock_size;
70}
71
Nathan Fontenotc2101c92016-06-20 09:00:39 -050072static void dlpar_free_property(struct property *prop)
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -060073{
74 kfree(prop->name);
75 kfree(prop->value);
76 kfree(prop);
77}
78
Nathan Fontenotc2101c92016-06-20 09:00:39 -050079static struct property *dlpar_clone_property(struct property *prop,
80 u32 prop_size)
81{
82 struct property *new_prop;
83
84 new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
85 if (!new_prop)
86 return NULL;
87
88 new_prop->name = kstrdup(prop->name, GFP_KERNEL);
89 new_prop->value = kzalloc(prop_size, GFP_KERNEL);
90 if (!new_prop->name || !new_prop->value) {
91 dlpar_free_property(new_prop);
92 return NULL;
93 }
94
95 memcpy(new_prop->value, prop->value, prop->length);
96 new_prop->length = prop_size;
97
98 of_property_set_flag(new_prop, OF_DYNAMIC);
99 return new_prop;
100}
101
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600102static struct property *dlpar_clone_drconf_property(struct device_node *dn)
103{
104 struct property *prop, *new_prop;
105 struct of_drconf_cell *lmbs;
106 u32 num_lmbs, *p;
107 int i;
108
109 prop = of_find_property(dn, "ibm,dynamic-memory", NULL);
110 if (!prop)
111 return NULL;
112
Nathan Fontenotc2101c92016-06-20 09:00:39 -0500113 new_prop = dlpar_clone_property(prop, prop->length);
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600114 if (!new_prop)
115 return NULL;
116
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600117 /* Convert the property to cpu endian-ness */
118 p = new_prop->value;
119 *p = be32_to_cpu(*p);
120
121 num_lmbs = *p++;
122 lmbs = (struct of_drconf_cell *)p;
123
124 for (i = 0; i < num_lmbs; i++) {
125 lmbs[i].base_addr = be64_to_cpu(lmbs[i].base_addr);
126 lmbs[i].drc_index = be32_to_cpu(lmbs[i].drc_index);
127 lmbs[i].flags = be32_to_cpu(lmbs[i].flags);
128 }
129
130 return new_prop;
131}
132
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600133static void dlpar_update_drconf_property(struct device_node *dn,
134 struct property *prop)
135{
136 struct of_drconf_cell *lmbs;
137 u32 num_lmbs, *p;
138 int i;
139
140 /* Convert the property back to BE */
141 p = prop->value;
142 num_lmbs = *p;
143 *p = cpu_to_be32(*p);
144 p++;
145
146 lmbs = (struct of_drconf_cell *)p;
147 for (i = 0; i < num_lmbs; i++) {
148 lmbs[i].base_addr = cpu_to_be64(lmbs[i].base_addr);
149 lmbs[i].drc_index = cpu_to_be32(lmbs[i].drc_index);
150 lmbs[i].flags = cpu_to_be32(lmbs[i].flags);
151 }
152
153 rtas_hp_event = true;
154 of_update_property(dn, prop);
155 rtas_hp_event = false;
156}
157
158static int dlpar_update_device_tree_lmb(struct of_drconf_cell *lmb)
159{
160 struct device_node *dn;
161 struct property *prop;
162 struct of_drconf_cell *lmbs;
163 u32 *p, num_lmbs;
164 int i;
165
166 dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
167 if (!dn)
168 return -ENODEV;
169
170 prop = dlpar_clone_drconf_property(dn);
171 if (!prop) {
172 of_node_put(dn);
173 return -ENODEV;
174 }
175
176 p = prop->value;
177 num_lmbs = *p++;
178 lmbs = (struct of_drconf_cell *)p;
179
180 for (i = 0; i < num_lmbs; i++) {
181 if (lmbs[i].drc_index == lmb->drc_index) {
182 lmbs[i].flags = lmb->flags;
183 lmbs[i].aa_index = lmb->aa_index;
184
185 dlpar_update_drconf_property(dn, prop);
186 break;
187 }
188 }
189
190 of_node_put(dn);
191 return 0;
192}
193
Nathan Fontenotc05a5a42016-06-20 09:01:38 -0500194static u32 find_aa_index(struct device_node *dr_node,
195 struct property *ala_prop, const u32 *lmb_assoc)
196{
197 u32 *assoc_arrays;
198 u32 aa_index;
199 int aa_arrays, aa_array_entries, aa_array_sz;
200 int i, index;
201
202 /*
203 * The ibm,associativity-lookup-arrays property is defined to be
204 * a 32-bit value specifying the number of associativity arrays
205 * followed by a 32-bitvalue specifying the number of entries per
206 * array, followed by the associativity arrays.
207 */
208 assoc_arrays = ala_prop->value;
209
210 aa_arrays = be32_to_cpu(assoc_arrays[0]);
211 aa_array_entries = be32_to_cpu(assoc_arrays[1]);
212 aa_array_sz = aa_array_entries * sizeof(u32);
213
214 aa_index = -1;
215 for (i = 0; i < aa_arrays; i++) {
216 index = (i * aa_array_entries) + 2;
217
218 if (memcmp(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz))
219 continue;
220
221 aa_index = i;
222 break;
223 }
224
225 if (aa_index == -1) {
226 struct property *new_prop;
227 u32 new_prop_size;
228
229 new_prop_size = ala_prop->length + aa_array_sz;
230 new_prop = dlpar_clone_property(ala_prop, new_prop_size);
231 if (!new_prop)
232 return -1;
233
234 assoc_arrays = new_prop->value;
235
236 /* increment the number of entries in the lookup array */
237 assoc_arrays[0] = cpu_to_be32(aa_arrays + 1);
238
239 /* copy the new associativity into the lookup array */
240 index = aa_arrays * aa_array_entries + 2;
241 memcpy(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz);
242
243 of_update_property(dr_node, new_prop);
244
245 /*
246 * The associativity lookup array index for this lmb is
247 * number of entries - 1 since we added its associativity
248 * to the end of the lookup array.
249 */
250 aa_index = be32_to_cpu(assoc_arrays[0]) - 1;
251 }
252
253 return aa_index;
254}
255
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600256static u32 lookup_lmb_associativity_index(struct of_drconf_cell *lmb)
257{
258 struct device_node *parent, *lmb_node, *dr_node;
Nathan Fontenotc05a5a42016-06-20 09:01:38 -0500259 struct property *ala_prop;
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600260 const u32 *lmb_assoc;
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600261 u32 aa_index;
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600262
263 parent = of_find_node_by_path("/");
264 if (!parent)
265 return -ENODEV;
266
267 lmb_node = dlpar_configure_connector(cpu_to_be32(lmb->drc_index),
268 parent);
269 of_node_put(parent);
270 if (!lmb_node)
271 return -EINVAL;
272
273 lmb_assoc = of_get_property(lmb_node, "ibm,associativity", NULL);
274 if (!lmb_assoc) {
275 dlpar_free_cc_nodes(lmb_node);
276 return -ENODEV;
277 }
278
279 dr_node = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
280 if (!dr_node) {
281 dlpar_free_cc_nodes(lmb_node);
282 return -ENODEV;
283 }
284
Nathan Fontenotc05a5a42016-06-20 09:01:38 -0500285 ala_prop = of_find_property(dr_node, "ibm,associativity-lookup-arrays",
286 NULL);
287 if (!ala_prop) {
288 of_node_put(dr_node);
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600289 dlpar_free_cc_nodes(lmb_node);
290 return -ENODEV;
291 }
292
Nathan Fontenotc05a5a42016-06-20 09:01:38 -0500293 aa_index = find_aa_index(dr_node, ala_prop, lmb_assoc);
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600294
295 dlpar_free_cc_nodes(lmb_node);
296 return aa_index;
297}
298
299static int dlpar_add_device_tree_lmb(struct of_drconf_cell *lmb)
300{
301 int aa_index;
302
303 lmb->flags |= DRCONF_MEM_ASSIGNED;
304
305 aa_index = lookup_lmb_associativity_index(lmb);
306 if (aa_index < 0) {
307 pr_err("Couldn't find associativity index for drc index %x\n",
308 lmb->drc_index);
309 return aa_index;
310 }
311
312 lmb->aa_index = aa_index;
313 return dlpar_update_device_tree_lmb(lmb);
314}
315
316static int dlpar_remove_device_tree_lmb(struct of_drconf_cell *lmb)
317{
318 lmb->flags &= ~DRCONF_MEM_ASSIGNED;
319 lmb->aa_index = 0xffffffff;
320 return dlpar_update_device_tree_lmb(lmb);
321}
322
David Rientjes4edd7ce2013-04-29 15:08:22 -0700323#ifdef CONFIG_MEMORY_HOTREMOVE
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600324static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
325{
326 unsigned long block_sz, start_pfn;
327 int sections_per_block;
328 int i, nid;
329
330 start_pfn = base >> PAGE_SHIFT;
331
Li Zhong42dbfc82014-04-10 16:25:31 +0800332 lock_device_hotplug();
333
334 if (!pfn_valid(start_pfn))
335 goto out;
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600336
Anton Blancharda5d86252014-06-04 17:50:47 +1000337 block_sz = pseries_memory_block_size();
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600338 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
339 nid = memory_add_physaddr_to_nid(base);
340
341 for (i = 0; i < sections_per_block; i++) {
342 remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
343 base += MIN_MEMORY_BLOCK_SIZE;
344 }
345
Li Zhong42dbfc82014-04-10 16:25:31 +0800346out:
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600347 /* Update memory regions for memory remove */
348 memblock_remove(base, memblock_size);
Li Zhong42dbfc82014-04-10 16:25:31 +0800349 unlock_device_hotplug();
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600350 return 0;
351}
352
353static int pseries_remove_mem_node(struct device_node *np)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000354{
355 const char *type;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500356 const __be32 *regs;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000357 unsigned long base;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000358 unsigned int lmb_size;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000359 int ret = -EINVAL;
360
361 /*
362 * Check to see if we are actually removing memory
363 */
364 type = of_get_property(np, "device_type", NULL);
365 if (type == NULL || strcmp(type, "memory") != 0)
366 return 0;
367
368 /*
Li Zhong4646d132014-08-07 13:11:58 +0800369 * Find the base address and size of the memblock
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000370 */
371 regs = of_get_property(np, "reg", NULL);
372 if (!regs)
373 return ret;
374
Thomas Falconc9ac4082014-08-19 15:44:57 -0500375 base = be64_to_cpu(*(unsigned long *)regs);
376 lmb_size = be32_to_cpu(regs[3]);
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000377
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600378 pseries_remove_memblock(base, lmb_size);
379 return 0;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000380}
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600381
382static bool lmb_is_removable(struct of_drconf_cell *lmb)
383{
384 int i, scns_per_block;
385 int rc = 1;
386 unsigned long pfn, block_sz;
387 u64 phys_addr;
388
389 if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
390 return false;
391
392 block_sz = memory_block_size_bytes();
393 scns_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
394 phys_addr = lmb->base_addr;
395
396 for (i = 0; i < scns_per_block; i++) {
397 pfn = PFN_DOWN(phys_addr);
398 if (!pfn_present(pfn))
399 continue;
400
401 rc &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
402 phys_addr += MIN_MEMORY_BLOCK_SIZE;
403 }
404
405 return rc ? true : false;
406}
407
408static int dlpar_add_lmb(struct of_drconf_cell *);
409
Alastair D'Silva9dc51282016-08-01 16:32:51 +1000410static struct memory_block *lmb_to_memblock(struct of_drconf_cell *lmb)
411{
412 unsigned long section_nr;
413 struct mem_section *mem_sect;
414 struct memory_block *mem_block;
415
416 section_nr = pfn_to_section_nr(PFN_DOWN(lmb->base_addr));
417 mem_sect = __nr_to_section(section_nr);
418
419 mem_block = find_memory_block(mem_sect);
420 return mem_block;
421}
422
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600423static int dlpar_remove_lmb(struct of_drconf_cell *lmb)
424{
425 struct memory_block *mem_block;
426 unsigned long block_sz;
427 int nid, rc;
428
429 if (!lmb_is_removable(lmb))
430 return -EINVAL;
431
432 mem_block = lmb_to_memblock(lmb);
433 if (!mem_block)
434 return -EINVAL;
435
436 rc = device_offline(&mem_block->dev);
437 put_device(&mem_block->dev);
438 if (rc)
439 return rc;
440
441 block_sz = pseries_memory_block_size();
442 nid = memory_add_physaddr_to_nid(lmb->base_addr);
443
444 remove_memory(nid, lmb->base_addr, block_sz);
445
446 /* Update memory regions for memory remove */
447 memblock_remove(lmb->base_addr, block_sz);
448
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600449 dlpar_remove_device_tree_lmb(lmb);
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600450 return 0;
451}
452
453static int dlpar_memory_remove_by_count(u32 lmbs_to_remove,
454 struct property *prop)
455{
456 struct of_drconf_cell *lmbs;
457 int lmbs_removed = 0;
458 int lmbs_available = 0;
459 u32 num_lmbs, *p;
460 int i, rc;
461
462 pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove);
463
464 if (lmbs_to_remove == 0)
465 return -EINVAL;
466
467 p = prop->value;
468 num_lmbs = *p++;
469 lmbs = (struct of_drconf_cell *)p;
470
471 /* Validate that there are enough LMBs to satisfy the request */
472 for (i = 0; i < num_lmbs; i++) {
Nathan Fontenot2db029e2016-11-28 11:50:45 -0500473 if (lmb_is_removable(&lmbs[i]))
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600474 lmbs_available++;
475 }
476
Nathan Fontenot2db029e2016-11-28 11:50:45 -0500477 if (lmbs_available < lmbs_to_remove) {
478 pr_info("Not enough LMBs available (%d of %d) to satisfy request\n",
479 lmbs_available, lmbs_to_remove);
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600480 return -EINVAL;
Nathan Fontenot2db029e2016-11-28 11:50:45 -0500481 }
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600482
483 for (i = 0; i < num_lmbs && lmbs_removed < lmbs_to_remove; i++) {
484 rc = dlpar_remove_lmb(&lmbs[i]);
485 if (rc)
486 continue;
487
488 lmbs_removed++;
489
490 /* Mark this lmb so we can add it later if all of the
491 * requested LMBs cannot be removed.
492 */
493 lmbs[i].reserved = 1;
494 }
495
496 if (lmbs_removed != lmbs_to_remove) {
497 pr_err("Memory hot-remove failed, adding LMB's back\n");
498
499 for (i = 0; i < num_lmbs; i++) {
500 if (!lmbs[i].reserved)
501 continue;
502
503 rc = dlpar_add_lmb(&lmbs[i]);
504 if (rc)
505 pr_err("Failed to add LMB back, drc index %x\n",
506 lmbs[i].drc_index);
507
508 lmbs[i].reserved = 0;
509 }
510
511 rc = -EINVAL;
512 } else {
513 for (i = 0; i < num_lmbs; i++) {
514 if (!lmbs[i].reserved)
515 continue;
516
John Allenc21f5152017-01-06 13:25:53 -0600517 dlpar_release_drc(lmbs[i].drc_index);
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600518 pr_info("Memory at %llx was hot-removed\n",
519 lmbs[i].base_addr);
520
521 lmbs[i].reserved = 0;
522 }
523 rc = 0;
524 }
525
526 return rc;
527}
528
529static int dlpar_memory_remove_by_index(u32 drc_index, struct property *prop)
530{
531 struct of_drconf_cell *lmbs;
532 u32 num_lmbs, *p;
533 int lmb_found;
534 int i, rc;
535
536 pr_info("Attempting to hot-remove LMB, drc index %x\n", drc_index);
537
538 p = prop->value;
539 num_lmbs = *p++;
540 lmbs = (struct of_drconf_cell *)p;
541
542 lmb_found = 0;
543 for (i = 0; i < num_lmbs; i++) {
544 if (lmbs[i].drc_index == drc_index) {
545 lmb_found = 1;
546 rc = dlpar_remove_lmb(&lmbs[i]);
John Allenc21f5152017-01-06 13:25:53 -0600547 if (!rc)
548 dlpar_release_drc(lmbs[i].drc_index);
549
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600550 break;
551 }
552 }
553
554 if (!lmb_found)
555 rc = -EINVAL;
556
557 if (rc)
558 pr_info("Failed to hot-remove memory at %llx\n",
559 lmbs[i].base_addr);
560 else
561 pr_info("Memory at %llx was hot-removed\n", lmbs[i].base_addr);
562
563 return rc;
564}
565
David Rientjes4edd7ce2013-04-29 15:08:22 -0700566#else
567static inline int pseries_remove_memblock(unsigned long base,
568 unsigned int memblock_size)
569{
570 return -EOPNOTSUPP;
571}
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600572static inline int pseries_remove_mem_node(struct device_node *np)
David Rientjes4edd7ce2013-04-29 15:08:22 -0700573{
Gavin Shanf1b39292014-08-11 19:16:19 +1000574 return 0;
David Rientjes4edd7ce2013-04-29 15:08:22 -0700575}
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600576static inline int dlpar_memory_remove(struct pseries_hp_errorlog *hp_elog)
577{
578 return -EOPNOTSUPP;
579}
Alexey Kardashevskiy16e00f52015-04-14 17:01:56 +1000580static int dlpar_remove_lmb(struct of_drconf_cell *lmb)
581{
582 return -EOPNOTSUPP;
583}
584static int dlpar_memory_remove_by_count(u32 lmbs_to_remove,
585 struct property *prop)
586{
587 return -EOPNOTSUPP;
588}
589static int dlpar_memory_remove_by_index(u32 drc_index, struct property *prop)
590{
591 return -EOPNOTSUPP;
592}
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600593
David Rientjes4edd7ce2013-04-29 15:08:22 -0700594#endif /* CONFIG_MEMORY_HOTREMOVE */
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000595
Nathan Fontenotfdb4f6e2016-06-29 12:20:30 -0500596static int dlpar_add_lmb(struct of_drconf_cell *lmb)
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600597{
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600598 unsigned long block_sz;
599 int nid, rc;
600
Nathan Fontenot4a4bdfe2016-02-10 11:10:44 -0600601 if (lmb->flags & DRCONF_MEM_ASSIGNED)
602 return -EINVAL;
603
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600604 rc = dlpar_add_device_tree_lmb(lmb);
605 if (rc) {
606 pr_err("Couldn't update device tree for drc index %x\n",
607 lmb->drc_index);
Nathan Fontenot4a4bdfe2016-02-10 11:10:44 -0600608 dlpar_release_drc(lmb->drc_index);
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600609 return rc;
610 }
611
Nathan Fontenotfdb4f6e2016-06-29 12:20:30 -0500612 block_sz = memory_block_size_bytes();
613
614 /* Find the node id for this address */
615 nid = memory_add_physaddr_to_nid(lmb->base_addr);
616
617 /* Add the memory */
618 rc = add_memory(nid, lmb->base_addr, block_sz);
John Allenc21f5152017-01-06 13:25:53 -0600619 if (rc)
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600620 dlpar_remove_device_tree_lmb(lmb);
John Allenc21f5152017-01-06 13:25:53 -0600621 else
Nathan Fontenotfdb4f6e2016-06-29 12:20:30 -0500622 lmb->flags |= DRCONF_MEM_ASSIGNED;
Nathan Fontenot4a4bdfe2016-02-10 11:10:44 -0600623
624 return rc;
625}
626
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600627static int dlpar_memory_add_by_count(u32 lmbs_to_add, struct property *prop)
628{
629 struct of_drconf_cell *lmbs;
630 u32 num_lmbs, *p;
631 int lmbs_available = 0;
632 int lmbs_added = 0;
633 int i, rc;
634
635 pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add);
636
637 if (lmbs_to_add == 0)
638 return -EINVAL;
639
640 p = prop->value;
641 num_lmbs = *p++;
642 lmbs = (struct of_drconf_cell *)p;
643
644 /* Validate that there are enough LMBs to satisfy the request */
645 for (i = 0; i < num_lmbs; i++) {
646 if (!(lmbs[i].flags & DRCONF_MEM_ASSIGNED))
647 lmbs_available++;
648 }
649
650 if (lmbs_available < lmbs_to_add)
651 return -EINVAL;
652
653 for (i = 0; i < num_lmbs && lmbs_to_add != lmbs_added; i++) {
John Allenc21f5152017-01-06 13:25:53 -0600654 rc = dlpar_acquire_drc(lmbs[i].drc_index);
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600655 if (rc)
656 continue;
657
John Allenc21f5152017-01-06 13:25:53 -0600658 rc = dlpar_add_lmb(&lmbs[i]);
659 if (rc) {
660 dlpar_release_drc(lmbs[i].drc_index);
661 continue;
662 }
663
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600664 lmbs_added++;
665
666 /* Mark this lmb so we can remove it later if all of the
667 * requested LMBs cannot be added.
668 */
669 lmbs[i].reserved = 1;
670 }
671
672 if (lmbs_added != lmbs_to_add) {
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600673 pr_err("Memory hot-add failed, removing any added LMBs\n");
674
675 for (i = 0; i < num_lmbs; i++) {
676 if (!lmbs[i].reserved)
677 continue;
678
679 rc = dlpar_remove_lmb(&lmbs[i]);
680 if (rc)
681 pr_err("Failed to remove LMB, drc index %x\n",
682 be32_to_cpu(lmbs[i].drc_index));
John Allenc21f5152017-01-06 13:25:53 -0600683 else
684 dlpar_release_drc(lmbs[i].drc_index);
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600685 }
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600686 rc = -EINVAL;
687 } else {
688 for (i = 0; i < num_lmbs; i++) {
689 if (!lmbs[i].reserved)
690 continue;
691
692 pr_info("Memory at %llx (drc index %x) was hot-added\n",
693 lmbs[i].base_addr, lmbs[i].drc_index);
694 lmbs[i].reserved = 0;
695 }
696 }
697
698 return rc;
699}
700
701static int dlpar_memory_add_by_index(u32 drc_index, struct property *prop)
702{
703 struct of_drconf_cell *lmbs;
704 u32 num_lmbs, *p;
705 int i, lmb_found;
706 int rc;
707
708 pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index);
709
710 p = prop->value;
711 num_lmbs = *p++;
712 lmbs = (struct of_drconf_cell *)p;
713
714 lmb_found = 0;
715 for (i = 0; i < num_lmbs; i++) {
716 if (lmbs[i].drc_index == drc_index) {
717 lmb_found = 1;
John Allenc21f5152017-01-06 13:25:53 -0600718 rc = dlpar_acquire_drc(lmbs[i].drc_index);
719 if (!rc) {
720 rc = dlpar_add_lmb(&lmbs[i]);
721 if (rc)
722 dlpar_release_drc(lmbs[i].drc_index);
723 }
724
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600725 break;
726 }
727 }
728
729 if (!lmb_found)
730 rc = -EINVAL;
731
732 if (rc)
733 pr_info("Failed to hot-add memory, drc index %x\n", drc_index);
734 else
735 pr_info("Memory at %llx (drc index %x) was hot-added\n",
736 lmbs[i].base_addr, drc_index);
737
738 return rc;
739}
740
Nathan Fontenot999e2da2015-02-10 13:47:02 -0600741int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
742{
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600743 struct device_node *dn;
744 struct property *prop;
745 u32 count, drc_index;
746 int rc;
747
748 count = hp_elog->_drc_u.drc_count;
749 drc_index = hp_elog->_drc_u.drc_index;
Nathan Fontenot999e2da2015-02-10 13:47:02 -0600750
751 lock_device_hotplug();
752
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600753 dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
Nathan Fontenotb0a478e2015-04-07 09:53:46 -0500754 if (!dn) {
755 rc = -EINVAL;
756 goto dlpar_memory_out;
757 }
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600758
759 prop = dlpar_clone_drconf_property(dn);
760 if (!prop) {
Nathan Fontenotb0a478e2015-04-07 09:53:46 -0500761 rc = -EINVAL;
762 goto dlpar_memory_out;
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600763 }
764
Nathan Fontenot999e2da2015-02-10 13:47:02 -0600765 switch (hp_elog->action) {
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600766 case PSERIES_HP_ELOG_ACTION_ADD:
767 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
768 rc = dlpar_memory_add_by_count(count, prop);
769 else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
770 rc = dlpar_memory_add_by_index(drc_index, prop);
771 else
772 rc = -EINVAL;
773 break;
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600774 case PSERIES_HP_ELOG_ACTION_REMOVE:
775 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
776 rc = dlpar_memory_remove_by_count(count, prop);
777 else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
778 rc = dlpar_memory_remove_by_index(drc_index, prop);
779 else
780 rc = -EINVAL;
781 break;
Nathan Fontenot999e2da2015-02-10 13:47:02 -0600782 default:
783 pr_err("Invalid action (%d) specified\n", hp_elog->action);
784 rc = -EINVAL;
785 break;
786 }
787
Nathan Fontenotc2101c92016-06-20 09:00:39 -0500788 dlpar_free_property(prop);
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600789
Nathan Fontenotb0a478e2015-04-07 09:53:46 -0500790dlpar_memory_out:
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600791 of_node_put(dn);
Nathan Fontenot999e2da2015-02-10 13:47:02 -0600792 unlock_device_hotplug();
793 return rc;
794}
795
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600796static int pseries_add_mem_node(struct device_node *np)
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700797{
798 const char *type;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500799 const __be32 *regs;
Nathan Fontenot92ecd172008-07-03 13:20:58 +1000800 unsigned long base;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000801 unsigned int lmb_size;
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700802 int ret = -EINVAL;
803
804 /*
805 * Check to see if we are actually adding memory
806 */
807 type = of_get_property(np, "device_type", NULL);
808 if (type == NULL || strcmp(type, "memory") != 0)
809 return 0;
810
811 /*
Yinghai Lu95f72d12010-07-12 14:36:09 +1000812 * Find the base and size of the memblock
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700813 */
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700814 regs = of_get_property(np, "reg", NULL);
815 if (!regs)
816 return ret;
817
Thomas Falconc9ac4082014-08-19 15:44:57 -0500818 base = be64_to_cpu(*(unsigned long *)regs);
819 lmb_size = be32_to_cpu(regs[3]);
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700820
821 /*
822 * Update memory region to represent the memory add
823 */
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000824 ret = memblock_add(base, lmb_size);
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000825 return (ret < 0) ? -EINVAL : 0;
826}
827
Grant Likelyf5242e52014-11-24 17:58:01 +0000828static int pseries_update_drconf_memory(struct of_reconfig_data *pr)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000829{
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000830 struct of_drconf_cell *new_drmem, *old_drmem;
Nathan Fontenotc540ada2011-01-20 10:45:20 -0600831 unsigned long memblock_size;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000832 u32 entries;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500833 __be32 *p;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000834 int i, rc = -EINVAL;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000835
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600836 if (rtas_hp_event)
837 return 0;
838
Anton Blancharda5d86252014-06-04 17:50:47 +1000839 memblock_size = pseries_memory_block_size();
Nathan Fontenotc540ada2011-01-20 10:45:20 -0600840 if (!memblock_size)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000841 return -EINVAL;
842
Thomas Falconc9ac4082014-08-19 15:44:57 -0500843 p = (__be32 *) pr->old_prop->value;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000844 if (!p)
845 return -EINVAL;
846
847 /* The first int of the property is the number of lmb's described
848 * by the property. This is followed by an array of of_drconf_cell
Li Zhong4646d132014-08-07 13:11:58 +0800849 * entries. Get the number of entries and skip to the array of
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000850 * of_drconf_cell's.
851 */
Thomas Falconc9ac4082014-08-19 15:44:57 -0500852 entries = be32_to_cpu(*p++);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000853 old_drmem = (struct of_drconf_cell *)p;
854
Thomas Falconc9ac4082014-08-19 15:44:57 -0500855 p = (__be32 *)pr->prop->value;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000856 p++;
857 new_drmem = (struct of_drconf_cell *)p;
858
859 for (i = 0; i < entries; i++) {
Thomas Falconc9ac4082014-08-19 15:44:57 -0500860 if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) &&
861 (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) {
862 rc = pseries_remove_memblock(
863 be64_to_cpu(old_drmem[i].base_addr),
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000864 memblock_size);
865 break;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500866 } else if ((!(be32_to_cpu(old_drmem[i].flags) &
867 DRCONF_MEM_ASSIGNED)) &&
868 (be32_to_cpu(new_drmem[i].flags) &
869 DRCONF_MEM_ASSIGNED)) {
870 rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr),
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000871 memblock_size);
872 rc = (rc < 0) ? -EINVAL : 0;
873 break;
874 }
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000875 }
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000876 return rc;
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700877}
878
Badari Pulavarty57b53922008-04-18 13:33:50 -0700879static int pseries_memory_notifier(struct notifier_block *nb,
Grant Likelyf5242e52014-11-24 17:58:01 +0000880 unsigned long action, void *data)
Badari Pulavarty57b53922008-04-18 13:33:50 -0700881{
Grant Likelyf5242e52014-11-24 17:58:01 +0000882 struct of_reconfig_data *rd = data;
Akinobu Mitade2780a2011-06-21 03:35:56 +0000883 int err = 0;
Badari Pulavarty57b53922008-04-18 13:33:50 -0700884
885 switch (action) {
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000886 case OF_RECONFIG_ATTACH_NODE:
Grant Likelyf5242e52014-11-24 17:58:01 +0000887 err = pseries_add_mem_node(rd->dn);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700888 break;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000889 case OF_RECONFIG_DETACH_NODE:
Grant Likelyf5242e52014-11-24 17:58:01 +0000890 err = pseries_remove_mem_node(rd->dn);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700891 break;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000892 case OF_RECONFIG_UPDATE_PROPERTY:
Grant Likelyf5242e52014-11-24 17:58:01 +0000893 if (!strcmp(rd->prop->name, "ibm,dynamic-memory"))
894 err = pseries_update_drconf_memory(rd);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700895 break;
896 }
Akinobu Mitade2780a2011-06-21 03:35:56 +0000897 return notifier_from_errno(err);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700898}
899
900static struct notifier_block pseries_mem_nb = {
901 .notifier_call = pseries_memory_notifier,
902};
903
904static int __init pseries_memory_hotplug_init(void)
905{
906 if (firmware_has_feature(FW_FEATURE_LPAR))
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000907 of_reconfig_notifier_register(&pseries_mem_nb);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700908
Badari Pulavarty57b53922008-04-18 13:33:50 -0700909 return 0;
910}
911machine_device_initcall(pseries, pseries_memory_hotplug_init);