blob: 6c12b02f4a61df21c411680e4807508d53a4f4d7 [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);
Michael Bringmanncbf687a2017-05-22 15:44:37 -0500127 lmbs[i].aa_index = be32_to_cpu(lmbs[i].aa_index);
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600128 lmbs[i].flags = be32_to_cpu(lmbs[i].flags);
129 }
130
131 return new_prop;
132}
133
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600134static void dlpar_update_drconf_property(struct device_node *dn,
135 struct property *prop)
136{
137 struct of_drconf_cell *lmbs;
138 u32 num_lmbs, *p;
139 int i;
140
141 /* Convert the property back to BE */
142 p = prop->value;
143 num_lmbs = *p;
144 *p = cpu_to_be32(*p);
145 p++;
146
147 lmbs = (struct of_drconf_cell *)p;
148 for (i = 0; i < num_lmbs; i++) {
149 lmbs[i].base_addr = cpu_to_be64(lmbs[i].base_addr);
150 lmbs[i].drc_index = cpu_to_be32(lmbs[i].drc_index);
Michael Bringmanncbf687a2017-05-22 15:44:37 -0500151 lmbs[i].aa_index = cpu_to_be32(lmbs[i].aa_index);
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600152 lmbs[i].flags = cpu_to_be32(lmbs[i].flags);
153 }
154
155 rtas_hp_event = true;
156 of_update_property(dn, prop);
157 rtas_hp_event = false;
158}
159
160static int dlpar_update_device_tree_lmb(struct of_drconf_cell *lmb)
161{
162 struct device_node *dn;
163 struct property *prop;
164 struct of_drconf_cell *lmbs;
165 u32 *p, num_lmbs;
166 int i;
167
168 dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
169 if (!dn)
170 return -ENODEV;
171
172 prop = dlpar_clone_drconf_property(dn);
173 if (!prop) {
174 of_node_put(dn);
175 return -ENODEV;
176 }
177
178 p = prop->value;
179 num_lmbs = *p++;
180 lmbs = (struct of_drconf_cell *)p;
181
182 for (i = 0; i < num_lmbs; i++) {
183 if (lmbs[i].drc_index == lmb->drc_index) {
184 lmbs[i].flags = lmb->flags;
185 lmbs[i].aa_index = lmb->aa_index;
186
187 dlpar_update_drconf_property(dn, prop);
188 break;
189 }
190 }
191
192 of_node_put(dn);
193 return 0;
194}
195
Nathan Fontenotc05a5a42016-06-20 09:01:38 -0500196static u32 find_aa_index(struct device_node *dr_node,
197 struct property *ala_prop, const u32 *lmb_assoc)
198{
199 u32 *assoc_arrays;
200 u32 aa_index;
201 int aa_arrays, aa_array_entries, aa_array_sz;
202 int i, index;
203
204 /*
205 * The ibm,associativity-lookup-arrays property is defined to be
206 * a 32-bit value specifying the number of associativity arrays
207 * followed by a 32-bitvalue specifying the number of entries per
208 * array, followed by the associativity arrays.
209 */
210 assoc_arrays = ala_prop->value;
211
212 aa_arrays = be32_to_cpu(assoc_arrays[0]);
213 aa_array_entries = be32_to_cpu(assoc_arrays[1]);
214 aa_array_sz = aa_array_entries * sizeof(u32);
215
216 aa_index = -1;
217 for (i = 0; i < aa_arrays; i++) {
218 index = (i * aa_array_entries) + 2;
219
220 if (memcmp(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz))
221 continue;
222
223 aa_index = i;
224 break;
225 }
226
227 if (aa_index == -1) {
228 struct property *new_prop;
229 u32 new_prop_size;
230
231 new_prop_size = ala_prop->length + aa_array_sz;
232 new_prop = dlpar_clone_property(ala_prop, new_prop_size);
233 if (!new_prop)
234 return -1;
235
236 assoc_arrays = new_prop->value;
237
238 /* increment the number of entries in the lookup array */
239 assoc_arrays[0] = cpu_to_be32(aa_arrays + 1);
240
241 /* copy the new associativity into the lookup array */
242 index = aa_arrays * aa_array_entries + 2;
243 memcpy(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz);
244
245 of_update_property(dr_node, new_prop);
246
247 /*
248 * The associativity lookup array index for this lmb is
249 * number of entries - 1 since we added its associativity
250 * to the end of the lookup array.
251 */
252 aa_index = be32_to_cpu(assoc_arrays[0]) - 1;
253 }
254
255 return aa_index;
256}
257
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600258static u32 lookup_lmb_associativity_index(struct of_drconf_cell *lmb)
259{
260 struct device_node *parent, *lmb_node, *dr_node;
Nathan Fontenotc05a5a42016-06-20 09:01:38 -0500261 struct property *ala_prop;
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600262 const u32 *lmb_assoc;
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600263 u32 aa_index;
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600264
265 parent = of_find_node_by_path("/");
266 if (!parent)
267 return -ENODEV;
268
269 lmb_node = dlpar_configure_connector(cpu_to_be32(lmb->drc_index),
270 parent);
271 of_node_put(parent);
272 if (!lmb_node)
273 return -EINVAL;
274
275 lmb_assoc = of_get_property(lmb_node, "ibm,associativity", NULL);
276 if (!lmb_assoc) {
277 dlpar_free_cc_nodes(lmb_node);
278 return -ENODEV;
279 }
280
281 dr_node = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
282 if (!dr_node) {
283 dlpar_free_cc_nodes(lmb_node);
284 return -ENODEV;
285 }
286
Nathan Fontenotc05a5a42016-06-20 09:01:38 -0500287 ala_prop = of_find_property(dr_node, "ibm,associativity-lookup-arrays",
288 NULL);
289 if (!ala_prop) {
290 of_node_put(dr_node);
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600291 dlpar_free_cc_nodes(lmb_node);
292 return -ENODEV;
293 }
294
Nathan Fontenotc05a5a42016-06-20 09:01:38 -0500295 aa_index = find_aa_index(dr_node, ala_prop, lmb_assoc);
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600296
Michael Ellermane7c9dda2018-11-27 19:16:44 +1100297 of_node_put(dr_node);
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600298 dlpar_free_cc_nodes(lmb_node);
299 return aa_index;
300}
301
302static int dlpar_add_device_tree_lmb(struct of_drconf_cell *lmb)
303{
304 int aa_index;
305
306 lmb->flags |= DRCONF_MEM_ASSIGNED;
307
308 aa_index = lookup_lmb_associativity_index(lmb);
309 if (aa_index < 0) {
310 pr_err("Couldn't find associativity index for drc index %x\n",
311 lmb->drc_index);
312 return aa_index;
313 }
314
315 lmb->aa_index = aa_index;
316 return dlpar_update_device_tree_lmb(lmb);
317}
318
319static int dlpar_remove_device_tree_lmb(struct of_drconf_cell *lmb)
320{
321 lmb->flags &= ~DRCONF_MEM_ASSIGNED;
322 lmb->aa_index = 0xffffffff;
323 return dlpar_update_device_tree_lmb(lmb);
324}
325
David Rientjes4edd7ce2013-04-29 15:08:22 -0700326#ifdef CONFIG_MEMORY_HOTREMOVE
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600327static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
328{
329 unsigned long block_sz, start_pfn;
330 int sections_per_block;
331 int i, nid;
332
333 start_pfn = base >> PAGE_SHIFT;
334
Li Zhong42dbfc82014-04-10 16:25:31 +0800335 lock_device_hotplug();
336
337 if (!pfn_valid(start_pfn))
338 goto out;
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600339
Anton Blancharda5d86252014-06-04 17:50:47 +1000340 block_sz = pseries_memory_block_size();
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600341 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
342 nid = memory_add_physaddr_to_nid(base);
343
344 for (i = 0; i < sections_per_block; i++) {
345 remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
346 base += MIN_MEMORY_BLOCK_SIZE;
347 }
348
Li Zhong42dbfc82014-04-10 16:25:31 +0800349out:
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600350 /* Update memory regions for memory remove */
351 memblock_remove(base, memblock_size);
Li Zhong42dbfc82014-04-10 16:25:31 +0800352 unlock_device_hotplug();
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600353 return 0;
354}
355
356static int pseries_remove_mem_node(struct device_node *np)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000357{
358 const char *type;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500359 const __be32 *regs;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000360 unsigned long base;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000361 unsigned int lmb_size;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000362 int ret = -EINVAL;
363
364 /*
365 * Check to see if we are actually removing memory
366 */
367 type = of_get_property(np, "device_type", NULL);
368 if (type == NULL || strcmp(type, "memory") != 0)
369 return 0;
370
371 /*
Li Zhong4646d132014-08-07 13:11:58 +0800372 * Find the base address and size of the memblock
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000373 */
374 regs = of_get_property(np, "reg", NULL);
375 if (!regs)
376 return ret;
377
Thomas Falconc9ac4082014-08-19 15:44:57 -0500378 base = be64_to_cpu(*(unsigned long *)regs);
379 lmb_size = be32_to_cpu(regs[3]);
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000380
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600381 pseries_remove_memblock(base, lmb_size);
382 return 0;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000383}
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600384
385static bool lmb_is_removable(struct of_drconf_cell *lmb)
386{
387 int i, scns_per_block;
388 int rc = 1;
389 unsigned long pfn, block_sz;
390 u64 phys_addr;
391
392 if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
393 return false;
394
395 block_sz = memory_block_size_bytes();
396 scns_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
397 phys_addr = lmb->base_addr;
398
399 for (i = 0; i < scns_per_block; i++) {
400 pfn = PFN_DOWN(phys_addr);
401 if (!pfn_present(pfn))
402 continue;
403
404 rc &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
405 phys_addr += MIN_MEMORY_BLOCK_SIZE;
406 }
407
408 return rc ? true : false;
409}
410
411static int dlpar_add_lmb(struct of_drconf_cell *);
412
Alastair D'Silva9dc51282016-08-01 16:32:51 +1000413static struct memory_block *lmb_to_memblock(struct of_drconf_cell *lmb)
414{
415 unsigned long section_nr;
416 struct mem_section *mem_sect;
417 struct memory_block *mem_block;
418
419 section_nr = pfn_to_section_nr(PFN_DOWN(lmb->base_addr));
420 mem_sect = __nr_to_section(section_nr);
421
422 mem_block = find_memory_block(mem_sect);
423 return mem_block;
424}
425
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600426static int dlpar_remove_lmb(struct of_drconf_cell *lmb)
427{
428 struct memory_block *mem_block;
429 unsigned long block_sz;
430 int nid, rc;
431
432 if (!lmb_is_removable(lmb))
433 return -EINVAL;
434
435 mem_block = lmb_to_memblock(lmb);
436 if (!mem_block)
437 return -EINVAL;
438
439 rc = device_offline(&mem_block->dev);
440 put_device(&mem_block->dev);
441 if (rc)
442 return rc;
443
444 block_sz = pseries_memory_block_size();
445 nid = memory_add_physaddr_to_nid(lmb->base_addr);
446
447 remove_memory(nid, lmb->base_addr, block_sz);
448
449 /* Update memory regions for memory remove */
450 memblock_remove(lmb->base_addr, block_sz);
451
452 dlpar_release_drc(lmb->drc_index);
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600453 dlpar_remove_device_tree_lmb(lmb);
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600454
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600455 return 0;
456}
457
458static int dlpar_memory_remove_by_count(u32 lmbs_to_remove,
459 struct property *prop)
460{
461 struct of_drconf_cell *lmbs;
462 int lmbs_removed = 0;
463 int lmbs_available = 0;
464 u32 num_lmbs, *p;
465 int i, rc;
466
467 pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove);
468
469 if (lmbs_to_remove == 0)
470 return -EINVAL;
471
472 p = prop->value;
473 num_lmbs = *p++;
474 lmbs = (struct of_drconf_cell *)p;
475
476 /* Validate that there are enough LMBs to satisfy the request */
477 for (i = 0; i < num_lmbs; i++) {
478 if (lmbs[i].flags & DRCONF_MEM_ASSIGNED)
479 lmbs_available++;
480 }
481
482 if (lmbs_available < lmbs_to_remove)
483 return -EINVAL;
484
485 for (i = 0; i < num_lmbs && lmbs_removed < lmbs_to_remove; i++) {
486 rc = dlpar_remove_lmb(&lmbs[i]);
487 if (rc)
488 continue;
489
490 lmbs_removed++;
491
492 /* Mark this lmb so we can add it later if all of the
493 * requested LMBs cannot be removed.
494 */
495 lmbs[i].reserved = 1;
496 }
497
498 if (lmbs_removed != lmbs_to_remove) {
499 pr_err("Memory hot-remove failed, adding LMB's back\n");
500
501 for (i = 0; i < num_lmbs; i++) {
502 if (!lmbs[i].reserved)
503 continue;
504
505 rc = dlpar_add_lmb(&lmbs[i]);
506 if (rc)
507 pr_err("Failed to add LMB back, drc index %x\n",
508 lmbs[i].drc_index);
509
510 lmbs[i].reserved = 0;
511 }
512
513 rc = -EINVAL;
514 } else {
515 for (i = 0; i < num_lmbs; i++) {
516 if (!lmbs[i].reserved)
517 continue;
518
519 pr_info("Memory at %llx was hot-removed\n",
520 lmbs[i].base_addr);
521
522 lmbs[i].reserved = 0;
523 }
524 rc = 0;
525 }
526
527 return rc;
528}
529
530static int dlpar_memory_remove_by_index(u32 drc_index, struct property *prop)
531{
532 struct of_drconf_cell *lmbs;
533 u32 num_lmbs, *p;
534 int lmb_found;
535 int i, rc;
536
537 pr_info("Attempting to hot-remove LMB, drc index %x\n", drc_index);
538
539 p = prop->value;
540 num_lmbs = *p++;
541 lmbs = (struct of_drconf_cell *)p;
542
543 lmb_found = 0;
544 for (i = 0; i < num_lmbs; i++) {
545 if (lmbs[i].drc_index == drc_index) {
546 lmb_found = 1;
547 rc = dlpar_remove_lmb(&lmbs[i]);
548 break;
549 }
550 }
551
552 if (!lmb_found)
553 rc = -EINVAL;
554
555 if (rc)
556 pr_info("Failed to hot-remove memory at %llx\n",
557 lmbs[i].base_addr);
558 else
559 pr_info("Memory at %llx was hot-removed\n", lmbs[i].base_addr);
560
561 return rc;
562}
563
David Rientjes4edd7ce2013-04-29 15:08:22 -0700564#else
565static inline int pseries_remove_memblock(unsigned long base,
566 unsigned int memblock_size)
567{
568 return -EOPNOTSUPP;
569}
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600570static inline int pseries_remove_mem_node(struct device_node *np)
David Rientjes4edd7ce2013-04-29 15:08:22 -0700571{
Gavin Shanf1b39292014-08-11 19:16:19 +1000572 return 0;
David Rientjes4edd7ce2013-04-29 15:08:22 -0700573}
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600574static inline int dlpar_memory_remove(struct pseries_hp_errorlog *hp_elog)
575{
576 return -EOPNOTSUPP;
577}
Alexey Kardashevskiy16e00f52015-04-14 17:01:56 +1000578static int dlpar_remove_lmb(struct of_drconf_cell *lmb)
579{
580 return -EOPNOTSUPP;
581}
582static int dlpar_memory_remove_by_count(u32 lmbs_to_remove,
583 struct property *prop)
584{
585 return -EOPNOTSUPP;
586}
587static int dlpar_memory_remove_by_index(u32 drc_index, struct property *prop)
588{
589 return -EOPNOTSUPP;
590}
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600591
David Rientjes4edd7ce2013-04-29 15:08:22 -0700592#endif /* CONFIG_MEMORY_HOTREMOVE */
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000593
Nathan Fontenotfdb4f6e2016-06-29 12:20:30 -0500594static int dlpar_add_lmb(struct of_drconf_cell *lmb)
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600595{
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600596 unsigned long block_sz;
597 int nid, rc;
598
Nathan Fontenot4a4bdfe2016-02-10 11:10:44 -0600599 if (lmb->flags & DRCONF_MEM_ASSIGNED)
600 return -EINVAL;
601
602 rc = dlpar_acquire_drc(lmb->drc_index);
603 if (rc)
604 return rc;
605
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600606 rc = dlpar_add_device_tree_lmb(lmb);
607 if (rc) {
608 pr_err("Couldn't update device tree for drc index %x\n",
609 lmb->drc_index);
Nathan Fontenot4a4bdfe2016-02-10 11:10:44 -0600610 dlpar_release_drc(lmb->drc_index);
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600611 return rc;
612 }
613
Nathan Fontenotfdb4f6e2016-06-29 12:20:30 -0500614 block_sz = memory_block_size_bytes();
615
616 /* Find the node id for this address */
617 nid = memory_add_physaddr_to_nid(lmb->base_addr);
618
619 /* Add the memory */
David Hildenbrande2e7b552018-10-30 15:10:24 -0700620 rc = __add_memory(nid, lmb->base_addr, block_sz);
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600621 if (rc) {
622 dlpar_remove_device_tree_lmb(lmb);
623 dlpar_release_drc(lmb->drc_index);
Nathan Fontenotfdb4f6e2016-06-29 12:20:30 -0500624 } else {
625 lmb->flags |= DRCONF_MEM_ASSIGNED;
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600626 }
Nathan Fontenot4a4bdfe2016-02-10 11:10:44 -0600627
628 return rc;
629}
630
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600631static int dlpar_memory_add_by_count(u32 lmbs_to_add, struct property *prop)
632{
633 struct of_drconf_cell *lmbs;
634 u32 num_lmbs, *p;
635 int lmbs_available = 0;
636 int lmbs_added = 0;
637 int i, rc;
638
639 pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add);
640
641 if (lmbs_to_add == 0)
642 return -EINVAL;
643
644 p = prop->value;
645 num_lmbs = *p++;
646 lmbs = (struct of_drconf_cell *)p;
647
648 /* Validate that there are enough LMBs to satisfy the request */
649 for (i = 0; i < num_lmbs; i++) {
650 if (!(lmbs[i].flags & DRCONF_MEM_ASSIGNED))
651 lmbs_available++;
652 }
653
654 if (lmbs_available < lmbs_to_add)
655 return -EINVAL;
656
657 for (i = 0; i < num_lmbs && lmbs_to_add != lmbs_added; i++) {
658 rc = dlpar_add_lmb(&lmbs[i]);
659 if (rc)
660 continue;
661
662 lmbs_added++;
663
664 /* Mark this lmb so we can remove it later if all of the
665 * requested LMBs cannot be added.
666 */
667 lmbs[i].reserved = 1;
668 }
669
670 if (lmbs_added != lmbs_to_add) {
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600671 pr_err("Memory hot-add failed, removing any added LMBs\n");
672
673 for (i = 0; i < num_lmbs; i++) {
674 if (!lmbs[i].reserved)
675 continue;
676
677 rc = dlpar_remove_lmb(&lmbs[i]);
678 if (rc)
679 pr_err("Failed to remove LMB, drc index %x\n",
680 be32_to_cpu(lmbs[i].drc_index));
681 }
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600682 rc = -EINVAL;
683 } else {
684 for (i = 0; i < num_lmbs; i++) {
685 if (!lmbs[i].reserved)
686 continue;
687
688 pr_info("Memory at %llx (drc index %x) was hot-added\n",
689 lmbs[i].base_addr, lmbs[i].drc_index);
690 lmbs[i].reserved = 0;
691 }
692 }
693
694 return rc;
695}
696
697static int dlpar_memory_add_by_index(u32 drc_index, struct property *prop)
698{
699 struct of_drconf_cell *lmbs;
700 u32 num_lmbs, *p;
701 int i, lmb_found;
702 int rc;
703
704 pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index);
705
706 p = prop->value;
707 num_lmbs = *p++;
708 lmbs = (struct of_drconf_cell *)p;
709
710 lmb_found = 0;
711 for (i = 0; i < num_lmbs; i++) {
712 if (lmbs[i].drc_index == drc_index) {
713 lmb_found = 1;
714 rc = dlpar_add_lmb(&lmbs[i]);
715 break;
716 }
717 }
718
719 if (!lmb_found)
720 rc = -EINVAL;
721
722 if (rc)
723 pr_info("Failed to hot-add memory, drc index %x\n", drc_index);
724 else
725 pr_info("Memory at %llx (drc index %x) was hot-added\n",
726 lmbs[i].base_addr, drc_index);
727
728 return rc;
729}
730
Nathan Fontenot999e2da2015-02-10 13:47:02 -0600731int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
732{
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600733 struct device_node *dn;
734 struct property *prop;
735 u32 count, drc_index;
736 int rc;
737
738 count = hp_elog->_drc_u.drc_count;
739 drc_index = hp_elog->_drc_u.drc_index;
Nathan Fontenot999e2da2015-02-10 13:47:02 -0600740
741 lock_device_hotplug();
742
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600743 dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
Nathan Fontenotb0a478e2015-04-07 09:53:46 -0500744 if (!dn) {
745 rc = -EINVAL;
746 goto dlpar_memory_out;
747 }
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600748
749 prop = dlpar_clone_drconf_property(dn);
750 if (!prop) {
Nathan Fontenotb0a478e2015-04-07 09:53:46 -0500751 rc = -EINVAL;
752 goto dlpar_memory_out;
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600753 }
754
Nathan Fontenot999e2da2015-02-10 13:47:02 -0600755 switch (hp_elog->action) {
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600756 case PSERIES_HP_ELOG_ACTION_ADD:
757 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
758 rc = dlpar_memory_add_by_count(count, prop);
759 else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
760 rc = dlpar_memory_add_by_index(drc_index, prop);
761 else
762 rc = -EINVAL;
763 break;
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600764 case PSERIES_HP_ELOG_ACTION_REMOVE:
765 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
766 rc = dlpar_memory_remove_by_count(count, prop);
767 else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
768 rc = dlpar_memory_remove_by_index(drc_index, prop);
769 else
770 rc = -EINVAL;
771 break;
Nathan Fontenot999e2da2015-02-10 13:47:02 -0600772 default:
773 pr_err("Invalid action (%d) specified\n", hp_elog->action);
774 rc = -EINVAL;
775 break;
776 }
777
Nathan Fontenotc2101c92016-06-20 09:00:39 -0500778 dlpar_free_property(prop);
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600779
Nathan Fontenotb0a478e2015-04-07 09:53:46 -0500780dlpar_memory_out:
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600781 of_node_put(dn);
Nathan Fontenot999e2da2015-02-10 13:47:02 -0600782 unlock_device_hotplug();
783 return rc;
784}
785
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600786static int pseries_add_mem_node(struct device_node *np)
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700787{
788 const char *type;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500789 const __be32 *regs;
Nathan Fontenot92ecd172008-07-03 13:20:58 +1000790 unsigned long base;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000791 unsigned int lmb_size;
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700792 int ret = -EINVAL;
793
794 /*
795 * Check to see if we are actually adding memory
796 */
797 type = of_get_property(np, "device_type", NULL);
798 if (type == NULL || strcmp(type, "memory") != 0)
799 return 0;
800
801 /*
Yinghai Lu95f72d12010-07-12 14:36:09 +1000802 * Find the base and size of the memblock
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700803 */
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700804 regs = of_get_property(np, "reg", NULL);
805 if (!regs)
806 return ret;
807
Thomas Falconc9ac4082014-08-19 15:44:57 -0500808 base = be64_to_cpu(*(unsigned long *)regs);
809 lmb_size = be32_to_cpu(regs[3]);
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700810
811 /*
812 * Update memory region to represent the memory add
813 */
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000814 ret = memblock_add(base, lmb_size);
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000815 return (ret < 0) ? -EINVAL : 0;
816}
817
Grant Likelyf5242e52014-11-24 17:58:01 +0000818static int pseries_update_drconf_memory(struct of_reconfig_data *pr)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000819{
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000820 struct of_drconf_cell *new_drmem, *old_drmem;
Nathan Fontenotc540ada2011-01-20 10:45:20 -0600821 unsigned long memblock_size;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000822 u32 entries;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500823 __be32 *p;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000824 int i, rc = -EINVAL;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000825
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600826 if (rtas_hp_event)
827 return 0;
828
Anton Blancharda5d86252014-06-04 17:50:47 +1000829 memblock_size = pseries_memory_block_size();
Nathan Fontenotc540ada2011-01-20 10:45:20 -0600830 if (!memblock_size)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000831 return -EINVAL;
832
Thomas Falconc9ac4082014-08-19 15:44:57 -0500833 p = (__be32 *) pr->old_prop->value;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000834 if (!p)
835 return -EINVAL;
836
837 /* The first int of the property is the number of lmb's described
838 * by the property. This is followed by an array of of_drconf_cell
Li Zhong4646d132014-08-07 13:11:58 +0800839 * entries. Get the number of entries and skip to the array of
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000840 * of_drconf_cell's.
841 */
Thomas Falconc9ac4082014-08-19 15:44:57 -0500842 entries = be32_to_cpu(*p++);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000843 old_drmem = (struct of_drconf_cell *)p;
844
Thomas Falconc9ac4082014-08-19 15:44:57 -0500845 p = (__be32 *)pr->prop->value;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000846 p++;
847 new_drmem = (struct of_drconf_cell *)p;
848
849 for (i = 0; i < entries; i++) {
Thomas Falconc9ac4082014-08-19 15:44:57 -0500850 if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) &&
851 (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) {
852 rc = pseries_remove_memblock(
853 be64_to_cpu(old_drmem[i].base_addr),
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000854 memblock_size);
855 break;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500856 } else if ((!(be32_to_cpu(old_drmem[i].flags) &
857 DRCONF_MEM_ASSIGNED)) &&
858 (be32_to_cpu(new_drmem[i].flags) &
859 DRCONF_MEM_ASSIGNED)) {
860 rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr),
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000861 memblock_size);
862 rc = (rc < 0) ? -EINVAL : 0;
863 break;
864 }
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000865 }
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000866 return rc;
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700867}
868
Badari Pulavarty57b53922008-04-18 13:33:50 -0700869static int pseries_memory_notifier(struct notifier_block *nb,
Grant Likelyf5242e52014-11-24 17:58:01 +0000870 unsigned long action, void *data)
Badari Pulavarty57b53922008-04-18 13:33:50 -0700871{
Grant Likelyf5242e52014-11-24 17:58:01 +0000872 struct of_reconfig_data *rd = data;
Akinobu Mitade2780a2011-06-21 03:35:56 +0000873 int err = 0;
Badari Pulavarty57b53922008-04-18 13:33:50 -0700874
875 switch (action) {
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000876 case OF_RECONFIG_ATTACH_NODE:
Grant Likelyf5242e52014-11-24 17:58:01 +0000877 err = pseries_add_mem_node(rd->dn);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700878 break;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000879 case OF_RECONFIG_DETACH_NODE:
Grant Likelyf5242e52014-11-24 17:58:01 +0000880 err = pseries_remove_mem_node(rd->dn);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700881 break;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000882 case OF_RECONFIG_UPDATE_PROPERTY:
Grant Likelyf5242e52014-11-24 17:58:01 +0000883 if (!strcmp(rd->prop->name, "ibm,dynamic-memory"))
884 err = pseries_update_drconf_memory(rd);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700885 break;
886 }
Akinobu Mitade2780a2011-06-21 03:35:56 +0000887 return notifier_from_errno(err);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700888}
889
890static struct notifier_block pseries_mem_nb = {
891 .notifier_call = pseries_memory_notifier,
892};
893
894static int __init pseries_memory_hotplug_init(void)
895{
896 if (firmware_has_feature(FW_FEATURE_LPAR))
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000897 of_reconfig_notifier_register(&pseries_mem_nb);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700898
Badari Pulavarty57b53922008-04-18 13:33:50 -0700899 return 0;
900}
901machine_device_initcall(pseries, pseries_memory_hotplug_init);