blob: 2ce138542083bd82af773d5015025d0a2a3d0890 [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 Fontenot5f97b2a2015-02-10 13:48:25 -060072static void dlpar_free_drconf_property(struct property *prop)
73{
74 kfree(prop->name);
75 kfree(prop->value);
76 kfree(prop);
77}
78
79static struct property *dlpar_clone_drconf_property(struct device_node *dn)
80{
81 struct property *prop, *new_prop;
82 struct of_drconf_cell *lmbs;
83 u32 num_lmbs, *p;
84 int i;
85
86 prop = of_find_property(dn, "ibm,dynamic-memory", NULL);
87 if (!prop)
88 return NULL;
89
90 new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
91 if (!new_prop)
92 return NULL;
93
94 new_prop->name = kstrdup(prop->name, GFP_KERNEL);
Andrzej Hajda2e16acc2015-08-07 09:59:12 +020095 new_prop->value = kmemdup(prop->value, prop->length, GFP_KERNEL);
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -060096 if (!new_prop->name || !new_prop->value) {
97 dlpar_free_drconf_property(new_prop);
98 return NULL;
99 }
100
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600101 new_prop->length = prop->length;
102
103 /* Convert the property to cpu endian-ness */
104 p = new_prop->value;
105 *p = be32_to_cpu(*p);
106
107 num_lmbs = *p++;
108 lmbs = (struct of_drconf_cell *)p;
109
110 for (i = 0; i < num_lmbs; i++) {
111 lmbs[i].base_addr = be64_to_cpu(lmbs[i].base_addr);
112 lmbs[i].drc_index = be32_to_cpu(lmbs[i].drc_index);
113 lmbs[i].flags = be32_to_cpu(lmbs[i].flags);
114 }
115
116 return new_prop;
117}
118
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600119static void dlpar_update_drconf_property(struct device_node *dn,
120 struct property *prop)
121{
122 struct of_drconf_cell *lmbs;
123 u32 num_lmbs, *p;
124 int i;
125
126 /* Convert the property back to BE */
127 p = prop->value;
128 num_lmbs = *p;
129 *p = cpu_to_be32(*p);
130 p++;
131
132 lmbs = (struct of_drconf_cell *)p;
133 for (i = 0; i < num_lmbs; i++) {
134 lmbs[i].base_addr = cpu_to_be64(lmbs[i].base_addr);
135 lmbs[i].drc_index = cpu_to_be32(lmbs[i].drc_index);
136 lmbs[i].flags = cpu_to_be32(lmbs[i].flags);
137 }
138
139 rtas_hp_event = true;
140 of_update_property(dn, prop);
141 rtas_hp_event = false;
142}
143
144static int dlpar_update_device_tree_lmb(struct of_drconf_cell *lmb)
145{
146 struct device_node *dn;
147 struct property *prop;
148 struct of_drconf_cell *lmbs;
149 u32 *p, num_lmbs;
150 int i;
151
152 dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
153 if (!dn)
154 return -ENODEV;
155
156 prop = dlpar_clone_drconf_property(dn);
157 if (!prop) {
158 of_node_put(dn);
159 return -ENODEV;
160 }
161
162 p = prop->value;
163 num_lmbs = *p++;
164 lmbs = (struct of_drconf_cell *)p;
165
166 for (i = 0; i < num_lmbs; i++) {
167 if (lmbs[i].drc_index == lmb->drc_index) {
168 lmbs[i].flags = lmb->flags;
169 lmbs[i].aa_index = lmb->aa_index;
170
171 dlpar_update_drconf_property(dn, prop);
172 break;
173 }
174 }
175
176 of_node_put(dn);
177 return 0;
178}
179
180static u32 lookup_lmb_associativity_index(struct of_drconf_cell *lmb)
181{
182 struct device_node *parent, *lmb_node, *dr_node;
183 const u32 *lmb_assoc;
184 const u32 *assoc_arrays;
185 u32 aa_index;
186 int aa_arrays, aa_array_entries, aa_array_sz;
187 int i;
188
189 parent = of_find_node_by_path("/");
190 if (!parent)
191 return -ENODEV;
192
193 lmb_node = dlpar_configure_connector(cpu_to_be32(lmb->drc_index),
194 parent);
195 of_node_put(parent);
196 if (!lmb_node)
197 return -EINVAL;
198
199 lmb_assoc = of_get_property(lmb_node, "ibm,associativity", NULL);
200 if (!lmb_assoc) {
201 dlpar_free_cc_nodes(lmb_node);
202 return -ENODEV;
203 }
204
205 dr_node = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
206 if (!dr_node) {
207 dlpar_free_cc_nodes(lmb_node);
208 return -ENODEV;
209 }
210
211 assoc_arrays = of_get_property(dr_node,
212 "ibm,associativity-lookup-arrays",
213 NULL);
214 of_node_put(dr_node);
215 if (!assoc_arrays) {
216 dlpar_free_cc_nodes(lmb_node);
217 return -ENODEV;
218 }
219
220 /* The ibm,associativity-lookup-arrays property is defined to be
221 * a 32-bit value specifying the number of associativity arrays
222 * followed by a 32-bitvalue specifying the number of entries per
223 * array, followed by the associativity arrays.
224 */
225 aa_arrays = be32_to_cpu(assoc_arrays[0]);
226 aa_array_entries = be32_to_cpu(assoc_arrays[1]);
227 aa_array_sz = aa_array_entries * sizeof(u32);
228
229 aa_index = -1;
230 for (i = 0; i < aa_arrays; i++) {
231 int indx = (i * aa_array_entries) + 2;
232
233 if (memcmp(&assoc_arrays[indx], &lmb_assoc[1], aa_array_sz))
234 continue;
235
236 aa_index = i;
237 break;
238 }
239
240 dlpar_free_cc_nodes(lmb_node);
241 return aa_index;
242}
243
244static int dlpar_add_device_tree_lmb(struct of_drconf_cell *lmb)
245{
246 int aa_index;
247
248 lmb->flags |= DRCONF_MEM_ASSIGNED;
249
250 aa_index = lookup_lmb_associativity_index(lmb);
251 if (aa_index < 0) {
252 pr_err("Couldn't find associativity index for drc index %x\n",
253 lmb->drc_index);
254 return aa_index;
255 }
256
257 lmb->aa_index = aa_index;
258 return dlpar_update_device_tree_lmb(lmb);
259}
260
261static int dlpar_remove_device_tree_lmb(struct of_drconf_cell *lmb)
262{
263 lmb->flags &= ~DRCONF_MEM_ASSIGNED;
264 lmb->aa_index = 0xffffffff;
265 return dlpar_update_device_tree_lmb(lmb);
266}
267
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600268static struct memory_block *lmb_to_memblock(struct of_drconf_cell *lmb)
269{
270 unsigned long section_nr;
271 struct mem_section *mem_sect;
272 struct memory_block *mem_block;
273
274 section_nr = pfn_to_section_nr(PFN_DOWN(lmb->base_addr));
275 mem_sect = __nr_to_section(section_nr);
276
277 mem_block = find_memory_block(mem_sect);
278 return mem_block;
279}
280
David Rientjes4edd7ce2013-04-29 15:08:22 -0700281#ifdef CONFIG_MEMORY_HOTREMOVE
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600282static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
283{
284 unsigned long block_sz, start_pfn;
285 int sections_per_block;
286 int i, nid;
287
288 start_pfn = base >> PAGE_SHIFT;
289
Li Zhong42dbfc82014-04-10 16:25:31 +0800290 lock_device_hotplug();
291
292 if (!pfn_valid(start_pfn))
293 goto out;
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600294
Anton Blancharda5d86252014-06-04 17:50:47 +1000295 block_sz = pseries_memory_block_size();
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600296 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
297 nid = memory_add_physaddr_to_nid(base);
298
299 for (i = 0; i < sections_per_block; i++) {
300 remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
301 base += MIN_MEMORY_BLOCK_SIZE;
302 }
303
Li Zhong42dbfc82014-04-10 16:25:31 +0800304out:
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600305 /* Update memory regions for memory remove */
306 memblock_remove(base, memblock_size);
Li Zhong42dbfc82014-04-10 16:25:31 +0800307 unlock_device_hotplug();
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600308 return 0;
309}
310
311static int pseries_remove_mem_node(struct device_node *np)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000312{
313 const char *type;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500314 const __be32 *regs;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000315 unsigned long base;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000316 unsigned int lmb_size;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000317 int ret = -EINVAL;
318
319 /*
320 * Check to see if we are actually removing memory
321 */
322 type = of_get_property(np, "device_type", NULL);
323 if (type == NULL || strcmp(type, "memory") != 0)
324 return 0;
325
326 /*
Li Zhong4646d132014-08-07 13:11:58 +0800327 * Find the base address and size of the memblock
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000328 */
329 regs = of_get_property(np, "reg", NULL);
330 if (!regs)
331 return ret;
332
Thomas Falconc9ac4082014-08-19 15:44:57 -0500333 base = be64_to_cpu(*(unsigned long *)regs);
334 lmb_size = be32_to_cpu(regs[3]);
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000335
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600336 pseries_remove_memblock(base, lmb_size);
337 return 0;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000338}
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600339
340static bool lmb_is_removable(struct of_drconf_cell *lmb)
341{
342 int i, scns_per_block;
343 int rc = 1;
344 unsigned long pfn, block_sz;
345 u64 phys_addr;
346
347 if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
348 return false;
349
350 block_sz = memory_block_size_bytes();
351 scns_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
352 phys_addr = lmb->base_addr;
353
354 for (i = 0; i < scns_per_block; i++) {
355 pfn = PFN_DOWN(phys_addr);
356 if (!pfn_present(pfn))
357 continue;
358
359 rc &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
360 phys_addr += MIN_MEMORY_BLOCK_SIZE;
361 }
362
363 return rc ? true : false;
364}
365
366static int dlpar_add_lmb(struct of_drconf_cell *);
367
368static int dlpar_remove_lmb(struct of_drconf_cell *lmb)
369{
370 struct memory_block *mem_block;
371 unsigned long block_sz;
372 int nid, rc;
373
374 if (!lmb_is_removable(lmb))
375 return -EINVAL;
376
377 mem_block = lmb_to_memblock(lmb);
378 if (!mem_block)
379 return -EINVAL;
380
381 rc = device_offline(&mem_block->dev);
382 put_device(&mem_block->dev);
383 if (rc)
384 return rc;
385
386 block_sz = pseries_memory_block_size();
387 nid = memory_add_physaddr_to_nid(lmb->base_addr);
388
389 remove_memory(nid, lmb->base_addr, block_sz);
390
391 /* Update memory regions for memory remove */
392 memblock_remove(lmb->base_addr, block_sz);
393
394 dlpar_release_drc(lmb->drc_index);
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600395 dlpar_remove_device_tree_lmb(lmb);
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600396
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600397 return 0;
398}
399
400static int dlpar_memory_remove_by_count(u32 lmbs_to_remove,
401 struct property *prop)
402{
403 struct of_drconf_cell *lmbs;
404 int lmbs_removed = 0;
405 int lmbs_available = 0;
406 u32 num_lmbs, *p;
407 int i, rc;
408
409 pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove);
410
411 if (lmbs_to_remove == 0)
412 return -EINVAL;
413
414 p = prop->value;
415 num_lmbs = *p++;
416 lmbs = (struct of_drconf_cell *)p;
417
418 /* Validate that there are enough LMBs to satisfy the request */
419 for (i = 0; i < num_lmbs; i++) {
420 if (lmbs[i].flags & DRCONF_MEM_ASSIGNED)
421 lmbs_available++;
422 }
423
424 if (lmbs_available < lmbs_to_remove)
425 return -EINVAL;
426
427 for (i = 0; i < num_lmbs && lmbs_removed < lmbs_to_remove; i++) {
428 rc = dlpar_remove_lmb(&lmbs[i]);
429 if (rc)
430 continue;
431
432 lmbs_removed++;
433
434 /* Mark this lmb so we can add it later if all of the
435 * requested LMBs cannot be removed.
436 */
437 lmbs[i].reserved = 1;
438 }
439
440 if (lmbs_removed != lmbs_to_remove) {
441 pr_err("Memory hot-remove failed, adding LMB's back\n");
442
443 for (i = 0; i < num_lmbs; i++) {
444 if (!lmbs[i].reserved)
445 continue;
446
447 rc = dlpar_add_lmb(&lmbs[i]);
448 if (rc)
449 pr_err("Failed to add LMB back, drc index %x\n",
450 lmbs[i].drc_index);
451
452 lmbs[i].reserved = 0;
453 }
454
455 rc = -EINVAL;
456 } else {
457 for (i = 0; i < num_lmbs; i++) {
458 if (!lmbs[i].reserved)
459 continue;
460
461 pr_info("Memory at %llx was hot-removed\n",
462 lmbs[i].base_addr);
463
464 lmbs[i].reserved = 0;
465 }
466 rc = 0;
467 }
468
469 return rc;
470}
471
472static int dlpar_memory_remove_by_index(u32 drc_index, struct property *prop)
473{
474 struct of_drconf_cell *lmbs;
475 u32 num_lmbs, *p;
476 int lmb_found;
477 int i, rc;
478
479 pr_info("Attempting to hot-remove LMB, drc index %x\n", drc_index);
480
481 p = prop->value;
482 num_lmbs = *p++;
483 lmbs = (struct of_drconf_cell *)p;
484
485 lmb_found = 0;
486 for (i = 0; i < num_lmbs; i++) {
487 if (lmbs[i].drc_index == drc_index) {
488 lmb_found = 1;
489 rc = dlpar_remove_lmb(&lmbs[i]);
490 break;
491 }
492 }
493
494 if (!lmb_found)
495 rc = -EINVAL;
496
497 if (rc)
498 pr_info("Failed to hot-remove memory at %llx\n",
499 lmbs[i].base_addr);
500 else
501 pr_info("Memory at %llx was hot-removed\n", lmbs[i].base_addr);
502
503 return rc;
504}
505
David Rientjes4edd7ce2013-04-29 15:08:22 -0700506#else
507static inline int pseries_remove_memblock(unsigned long base,
508 unsigned int memblock_size)
509{
510 return -EOPNOTSUPP;
511}
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600512static inline int pseries_remove_mem_node(struct device_node *np)
David Rientjes4edd7ce2013-04-29 15:08:22 -0700513{
Gavin Shanf1b39292014-08-11 19:16:19 +1000514 return 0;
David Rientjes4edd7ce2013-04-29 15:08:22 -0700515}
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600516static inline int dlpar_memory_remove(struct pseries_hp_errorlog *hp_elog)
517{
518 return -EOPNOTSUPP;
519}
Alexey Kardashevskiy16e00f52015-04-14 17:01:56 +1000520static int dlpar_remove_lmb(struct of_drconf_cell *lmb)
521{
522 return -EOPNOTSUPP;
523}
524static int dlpar_memory_remove_by_count(u32 lmbs_to_remove,
525 struct property *prop)
526{
527 return -EOPNOTSUPP;
528}
529static int dlpar_memory_remove_by_index(u32 drc_index, struct property *prop)
530{
531 return -EOPNOTSUPP;
532}
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600533
David Rientjes4edd7ce2013-04-29 15:08:22 -0700534#endif /* CONFIG_MEMORY_HOTREMOVE */
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000535
Nathan Fontenot4a4bdfe2016-02-10 11:10:44 -0600536static int dlpar_add_lmb_memory(struct of_drconf_cell *lmb)
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600537{
538 struct memory_block *mem_block;
539 unsigned long block_sz;
540 int nid, rc;
541
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600542 block_sz = memory_block_size_bytes();
543
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600544 /* Find the node id for this address */
545 nid = memory_add_physaddr_to_nid(lmb->base_addr);
546
547 /* Add the memory */
548 rc = add_memory(nid, lmb->base_addr, block_sz);
Nathan Fontenot4a4bdfe2016-02-10 11:10:44 -0600549 if (rc)
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600550 return rc;
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600551
552 /* Register this block of memory */
553 rc = memblock_add(lmb->base_addr, block_sz);
554 if (rc) {
555 remove_memory(nid, lmb->base_addr, block_sz);
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600556 return rc;
557 }
558
559 mem_block = lmb_to_memblock(lmb);
560 if (!mem_block) {
561 remove_memory(nid, lmb->base_addr, block_sz);
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600562 return -EINVAL;
563 }
564
565 rc = device_online(&mem_block->dev);
566 put_device(&mem_block->dev);
567 if (rc) {
568 remove_memory(nid, lmb->base_addr, block_sz);
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600569 return rc;
570 }
571
572 lmb->flags |= DRCONF_MEM_ASSIGNED;
573 return 0;
574}
575
Nathan Fontenot4a4bdfe2016-02-10 11:10:44 -0600576static int dlpar_add_lmb(struct of_drconf_cell *lmb)
577{
578 int rc;
579
580 if (lmb->flags & DRCONF_MEM_ASSIGNED)
581 return -EINVAL;
582
583 rc = dlpar_acquire_drc(lmb->drc_index);
584 if (rc)
585 return rc;
586
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600587 rc = dlpar_add_device_tree_lmb(lmb);
588 if (rc) {
589 pr_err("Couldn't update device tree for drc index %x\n",
590 lmb->drc_index);
Nathan Fontenot4a4bdfe2016-02-10 11:10:44 -0600591 dlpar_release_drc(lmb->drc_index);
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600592 return rc;
593 }
594
595 rc = dlpar_add_lmb_memory(lmb);
596 if (rc) {
597 dlpar_remove_device_tree_lmb(lmb);
598 dlpar_release_drc(lmb->drc_index);
599 }
Nathan Fontenot4a4bdfe2016-02-10 11:10:44 -0600600
601 return rc;
602}
603
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600604static int dlpar_memory_add_by_count(u32 lmbs_to_add, struct property *prop)
605{
606 struct of_drconf_cell *lmbs;
607 u32 num_lmbs, *p;
608 int lmbs_available = 0;
609 int lmbs_added = 0;
610 int i, rc;
611
612 pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add);
613
614 if (lmbs_to_add == 0)
615 return -EINVAL;
616
617 p = prop->value;
618 num_lmbs = *p++;
619 lmbs = (struct of_drconf_cell *)p;
620
621 /* Validate that there are enough LMBs to satisfy the request */
622 for (i = 0; i < num_lmbs; i++) {
623 if (!(lmbs[i].flags & DRCONF_MEM_ASSIGNED))
624 lmbs_available++;
625 }
626
627 if (lmbs_available < lmbs_to_add)
628 return -EINVAL;
629
630 for (i = 0; i < num_lmbs && lmbs_to_add != lmbs_added; i++) {
631 rc = dlpar_add_lmb(&lmbs[i]);
632 if (rc)
633 continue;
634
635 lmbs_added++;
636
637 /* Mark this lmb so we can remove it later if all of the
638 * requested LMBs cannot be added.
639 */
640 lmbs[i].reserved = 1;
641 }
642
643 if (lmbs_added != lmbs_to_add) {
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600644 pr_err("Memory hot-add failed, removing any added LMBs\n");
645
646 for (i = 0; i < num_lmbs; i++) {
647 if (!lmbs[i].reserved)
648 continue;
649
650 rc = dlpar_remove_lmb(&lmbs[i]);
651 if (rc)
652 pr_err("Failed to remove LMB, drc index %x\n",
653 be32_to_cpu(lmbs[i].drc_index));
654 }
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600655 rc = -EINVAL;
656 } else {
657 for (i = 0; i < num_lmbs; i++) {
658 if (!lmbs[i].reserved)
659 continue;
660
661 pr_info("Memory at %llx (drc index %x) was hot-added\n",
662 lmbs[i].base_addr, lmbs[i].drc_index);
663 lmbs[i].reserved = 0;
664 }
665 }
666
667 return rc;
668}
669
670static int dlpar_memory_add_by_index(u32 drc_index, struct property *prop)
671{
672 struct of_drconf_cell *lmbs;
673 u32 num_lmbs, *p;
674 int i, lmb_found;
675 int rc;
676
677 pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index);
678
679 p = prop->value;
680 num_lmbs = *p++;
681 lmbs = (struct of_drconf_cell *)p;
682
683 lmb_found = 0;
684 for (i = 0; i < num_lmbs; i++) {
685 if (lmbs[i].drc_index == drc_index) {
686 lmb_found = 1;
687 rc = dlpar_add_lmb(&lmbs[i]);
688 break;
689 }
690 }
691
692 if (!lmb_found)
693 rc = -EINVAL;
694
695 if (rc)
696 pr_info("Failed to hot-add memory, drc index %x\n", drc_index);
697 else
698 pr_info("Memory at %llx (drc index %x) was hot-added\n",
699 lmbs[i].base_addr, drc_index);
700
701 return rc;
702}
703
Nathan Fontenot999e2da2015-02-10 13:47:02 -0600704int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
705{
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600706 struct device_node *dn;
707 struct property *prop;
708 u32 count, drc_index;
709 int rc;
710
711 count = hp_elog->_drc_u.drc_count;
712 drc_index = hp_elog->_drc_u.drc_index;
Nathan Fontenot999e2da2015-02-10 13:47:02 -0600713
714 lock_device_hotplug();
715
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600716 dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
Nathan Fontenotb0a478e2015-04-07 09:53:46 -0500717 if (!dn) {
718 rc = -EINVAL;
719 goto dlpar_memory_out;
720 }
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600721
722 prop = dlpar_clone_drconf_property(dn);
723 if (!prop) {
Nathan Fontenotb0a478e2015-04-07 09:53:46 -0500724 rc = -EINVAL;
725 goto dlpar_memory_out;
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600726 }
727
Nathan Fontenot999e2da2015-02-10 13:47:02 -0600728 switch (hp_elog->action) {
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600729 case PSERIES_HP_ELOG_ACTION_ADD:
730 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
731 rc = dlpar_memory_add_by_count(count, prop);
732 else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
733 rc = dlpar_memory_add_by_index(drc_index, prop);
734 else
735 rc = -EINVAL;
736 break;
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600737 case PSERIES_HP_ELOG_ACTION_REMOVE:
738 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
739 rc = dlpar_memory_remove_by_count(count, prop);
740 else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
741 rc = dlpar_memory_remove_by_index(drc_index, prop);
742 else
743 rc = -EINVAL;
744 break;
Nathan Fontenot999e2da2015-02-10 13:47:02 -0600745 default:
746 pr_err("Invalid action (%d) specified\n", hp_elog->action);
747 rc = -EINVAL;
748 break;
749 }
750
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600751 dlpar_free_drconf_property(prop);
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600752
Nathan Fontenotb0a478e2015-04-07 09:53:46 -0500753dlpar_memory_out:
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600754 of_node_put(dn);
Nathan Fontenot999e2da2015-02-10 13:47:02 -0600755 unlock_device_hotplug();
756 return rc;
757}
758
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600759static int pseries_add_mem_node(struct device_node *np)
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700760{
761 const char *type;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500762 const __be32 *regs;
Nathan Fontenot92ecd172008-07-03 13:20:58 +1000763 unsigned long base;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000764 unsigned int lmb_size;
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700765 int ret = -EINVAL;
766
767 /*
768 * Check to see if we are actually adding memory
769 */
770 type = of_get_property(np, "device_type", NULL);
771 if (type == NULL || strcmp(type, "memory") != 0)
772 return 0;
773
774 /*
Yinghai Lu95f72d12010-07-12 14:36:09 +1000775 * Find the base and size of the memblock
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700776 */
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700777 regs = of_get_property(np, "reg", NULL);
778 if (!regs)
779 return ret;
780
Thomas Falconc9ac4082014-08-19 15:44:57 -0500781 base = be64_to_cpu(*(unsigned long *)regs);
782 lmb_size = be32_to_cpu(regs[3]);
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700783
784 /*
785 * Update memory region to represent the memory add
786 */
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000787 ret = memblock_add(base, lmb_size);
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000788 return (ret < 0) ? -EINVAL : 0;
789}
790
Grant Likelyf5242e52014-11-24 17:58:01 +0000791static int pseries_update_drconf_memory(struct of_reconfig_data *pr)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000792{
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000793 struct of_drconf_cell *new_drmem, *old_drmem;
Nathan Fontenotc540ada2011-01-20 10:45:20 -0600794 unsigned long memblock_size;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000795 u32 entries;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500796 __be32 *p;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000797 int i, rc = -EINVAL;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000798
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600799 if (rtas_hp_event)
800 return 0;
801
Anton Blancharda5d86252014-06-04 17:50:47 +1000802 memblock_size = pseries_memory_block_size();
Nathan Fontenotc540ada2011-01-20 10:45:20 -0600803 if (!memblock_size)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000804 return -EINVAL;
805
Thomas Falconc9ac4082014-08-19 15:44:57 -0500806 p = (__be32 *) pr->old_prop->value;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000807 if (!p)
808 return -EINVAL;
809
810 /* The first int of the property is the number of lmb's described
811 * by the property. This is followed by an array of of_drconf_cell
Li Zhong4646d132014-08-07 13:11:58 +0800812 * entries. Get the number of entries and skip to the array of
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000813 * of_drconf_cell's.
814 */
Thomas Falconc9ac4082014-08-19 15:44:57 -0500815 entries = be32_to_cpu(*p++);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000816 old_drmem = (struct of_drconf_cell *)p;
817
Thomas Falconc9ac4082014-08-19 15:44:57 -0500818 p = (__be32 *)pr->prop->value;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000819 p++;
820 new_drmem = (struct of_drconf_cell *)p;
821
822 for (i = 0; i < entries; i++) {
Thomas Falconc9ac4082014-08-19 15:44:57 -0500823 if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) &&
824 (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) {
825 rc = pseries_remove_memblock(
826 be64_to_cpu(old_drmem[i].base_addr),
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000827 memblock_size);
828 break;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500829 } else if ((!(be32_to_cpu(old_drmem[i].flags) &
830 DRCONF_MEM_ASSIGNED)) &&
831 (be32_to_cpu(new_drmem[i].flags) &
832 DRCONF_MEM_ASSIGNED)) {
833 rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr),
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000834 memblock_size);
835 rc = (rc < 0) ? -EINVAL : 0;
836 break;
837 }
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000838 }
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000839 return rc;
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700840}
841
Badari Pulavarty57b53922008-04-18 13:33:50 -0700842static int pseries_memory_notifier(struct notifier_block *nb,
Grant Likelyf5242e52014-11-24 17:58:01 +0000843 unsigned long action, void *data)
Badari Pulavarty57b53922008-04-18 13:33:50 -0700844{
Grant Likelyf5242e52014-11-24 17:58:01 +0000845 struct of_reconfig_data *rd = data;
Akinobu Mitade2780a2011-06-21 03:35:56 +0000846 int err = 0;
Badari Pulavarty57b53922008-04-18 13:33:50 -0700847
848 switch (action) {
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000849 case OF_RECONFIG_ATTACH_NODE:
Grant Likelyf5242e52014-11-24 17:58:01 +0000850 err = pseries_add_mem_node(rd->dn);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700851 break;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000852 case OF_RECONFIG_DETACH_NODE:
Grant Likelyf5242e52014-11-24 17:58:01 +0000853 err = pseries_remove_mem_node(rd->dn);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700854 break;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000855 case OF_RECONFIG_UPDATE_PROPERTY:
Grant Likelyf5242e52014-11-24 17:58:01 +0000856 if (!strcmp(rd->prop->name, "ibm,dynamic-memory"))
857 err = pseries_update_drconf_memory(rd);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700858 break;
859 }
Akinobu Mitade2780a2011-06-21 03:35:56 +0000860 return notifier_from_errno(err);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700861}
862
863static struct notifier_block pseries_mem_nb = {
864 .notifier_call = pseries_memory_notifier,
865};
866
867static int __init pseries_memory_hotplug_init(void)
868{
869 if (firmware_has_feature(FW_FEATURE_LPAR))
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000870 of_reconfig_notifier_register(&pseries_mem_nb);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700871
Badari Pulavarty57b53922008-04-18 13:33:50 -0700872 return 0;
873}
874machine_device_initcall(pseries, pseries_memory_hotplug_init);