blob: 3dbc82b7e374ad1fc9a94556def99c65110f4794 [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
194static u32 lookup_lmb_associativity_index(struct of_drconf_cell *lmb)
195{
196 struct device_node *parent, *lmb_node, *dr_node;
197 const u32 *lmb_assoc;
198 const u32 *assoc_arrays;
199 u32 aa_index;
200 int aa_arrays, aa_array_entries, aa_array_sz;
201 int i;
202
203 parent = of_find_node_by_path("/");
204 if (!parent)
205 return -ENODEV;
206
207 lmb_node = dlpar_configure_connector(cpu_to_be32(lmb->drc_index),
208 parent);
209 of_node_put(parent);
210 if (!lmb_node)
211 return -EINVAL;
212
213 lmb_assoc = of_get_property(lmb_node, "ibm,associativity", NULL);
214 if (!lmb_assoc) {
215 dlpar_free_cc_nodes(lmb_node);
216 return -ENODEV;
217 }
218
219 dr_node = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
220 if (!dr_node) {
221 dlpar_free_cc_nodes(lmb_node);
222 return -ENODEV;
223 }
224
225 assoc_arrays = of_get_property(dr_node,
226 "ibm,associativity-lookup-arrays",
227 NULL);
228 of_node_put(dr_node);
229 if (!assoc_arrays) {
230 dlpar_free_cc_nodes(lmb_node);
231 return -ENODEV;
232 }
233
234 /* The ibm,associativity-lookup-arrays property is defined to be
235 * a 32-bit value specifying the number of associativity arrays
236 * followed by a 32-bitvalue specifying the number of entries per
237 * array, followed by the associativity arrays.
238 */
239 aa_arrays = be32_to_cpu(assoc_arrays[0]);
240 aa_array_entries = be32_to_cpu(assoc_arrays[1]);
241 aa_array_sz = aa_array_entries * sizeof(u32);
242
243 aa_index = -1;
244 for (i = 0; i < aa_arrays; i++) {
245 int indx = (i * aa_array_entries) + 2;
246
247 if (memcmp(&assoc_arrays[indx], &lmb_assoc[1], aa_array_sz))
248 continue;
249
250 aa_index = i;
251 break;
252 }
253
254 dlpar_free_cc_nodes(lmb_node);
255 return aa_index;
256}
257
258static int dlpar_add_device_tree_lmb(struct of_drconf_cell *lmb)
259{
260 int aa_index;
261
262 lmb->flags |= DRCONF_MEM_ASSIGNED;
263
264 aa_index = lookup_lmb_associativity_index(lmb);
265 if (aa_index < 0) {
266 pr_err("Couldn't find associativity index for drc index %x\n",
267 lmb->drc_index);
268 return aa_index;
269 }
270
271 lmb->aa_index = aa_index;
272 return dlpar_update_device_tree_lmb(lmb);
273}
274
275static int dlpar_remove_device_tree_lmb(struct of_drconf_cell *lmb)
276{
277 lmb->flags &= ~DRCONF_MEM_ASSIGNED;
278 lmb->aa_index = 0xffffffff;
279 return dlpar_update_device_tree_lmb(lmb);
280}
281
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600282static struct memory_block *lmb_to_memblock(struct of_drconf_cell *lmb)
283{
284 unsigned long section_nr;
285 struct mem_section *mem_sect;
286 struct memory_block *mem_block;
287
288 section_nr = pfn_to_section_nr(PFN_DOWN(lmb->base_addr));
289 mem_sect = __nr_to_section(section_nr);
290
291 mem_block = find_memory_block(mem_sect);
292 return mem_block;
293}
294
David Rientjes4edd7ce2013-04-29 15:08:22 -0700295#ifdef CONFIG_MEMORY_HOTREMOVE
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600296static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
297{
298 unsigned long block_sz, start_pfn;
299 int sections_per_block;
300 int i, nid;
301
302 start_pfn = base >> PAGE_SHIFT;
303
Li Zhong42dbfc82014-04-10 16:25:31 +0800304 lock_device_hotplug();
305
306 if (!pfn_valid(start_pfn))
307 goto out;
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600308
Anton Blancharda5d86252014-06-04 17:50:47 +1000309 block_sz = pseries_memory_block_size();
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600310 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
311 nid = memory_add_physaddr_to_nid(base);
312
313 for (i = 0; i < sections_per_block; i++) {
314 remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
315 base += MIN_MEMORY_BLOCK_SIZE;
316 }
317
Li Zhong42dbfc82014-04-10 16:25:31 +0800318out:
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600319 /* Update memory regions for memory remove */
320 memblock_remove(base, memblock_size);
Li Zhong42dbfc82014-04-10 16:25:31 +0800321 unlock_device_hotplug();
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600322 return 0;
323}
324
325static int pseries_remove_mem_node(struct device_node *np)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000326{
327 const char *type;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500328 const __be32 *regs;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000329 unsigned long base;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000330 unsigned int lmb_size;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000331 int ret = -EINVAL;
332
333 /*
334 * Check to see if we are actually removing memory
335 */
336 type = of_get_property(np, "device_type", NULL);
337 if (type == NULL || strcmp(type, "memory") != 0)
338 return 0;
339
340 /*
Li Zhong4646d132014-08-07 13:11:58 +0800341 * Find the base address and size of the memblock
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000342 */
343 regs = of_get_property(np, "reg", NULL);
344 if (!regs)
345 return ret;
346
Thomas Falconc9ac4082014-08-19 15:44:57 -0500347 base = be64_to_cpu(*(unsigned long *)regs);
348 lmb_size = be32_to_cpu(regs[3]);
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000349
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600350 pseries_remove_memblock(base, lmb_size);
351 return 0;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000352}
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600353
354static bool lmb_is_removable(struct of_drconf_cell *lmb)
355{
356 int i, scns_per_block;
357 int rc = 1;
358 unsigned long pfn, block_sz;
359 u64 phys_addr;
360
361 if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
362 return false;
363
364 block_sz = memory_block_size_bytes();
365 scns_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
366 phys_addr = lmb->base_addr;
367
368 for (i = 0; i < scns_per_block; i++) {
369 pfn = PFN_DOWN(phys_addr);
370 if (!pfn_present(pfn))
371 continue;
372
373 rc &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
374 phys_addr += MIN_MEMORY_BLOCK_SIZE;
375 }
376
377 return rc ? true : false;
378}
379
380static int dlpar_add_lmb(struct of_drconf_cell *);
381
382static int dlpar_remove_lmb(struct of_drconf_cell *lmb)
383{
384 struct memory_block *mem_block;
385 unsigned long block_sz;
386 int nid, rc;
387
388 if (!lmb_is_removable(lmb))
389 return -EINVAL;
390
391 mem_block = lmb_to_memblock(lmb);
392 if (!mem_block)
393 return -EINVAL;
394
395 rc = device_offline(&mem_block->dev);
396 put_device(&mem_block->dev);
397 if (rc)
398 return rc;
399
400 block_sz = pseries_memory_block_size();
401 nid = memory_add_physaddr_to_nid(lmb->base_addr);
402
403 remove_memory(nid, lmb->base_addr, block_sz);
404
405 /* Update memory regions for memory remove */
406 memblock_remove(lmb->base_addr, block_sz);
407
408 dlpar_release_drc(lmb->drc_index);
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600409 dlpar_remove_device_tree_lmb(lmb);
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600410
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600411 return 0;
412}
413
414static int dlpar_memory_remove_by_count(u32 lmbs_to_remove,
415 struct property *prop)
416{
417 struct of_drconf_cell *lmbs;
418 int lmbs_removed = 0;
419 int lmbs_available = 0;
420 u32 num_lmbs, *p;
421 int i, rc;
422
423 pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove);
424
425 if (lmbs_to_remove == 0)
426 return -EINVAL;
427
428 p = prop->value;
429 num_lmbs = *p++;
430 lmbs = (struct of_drconf_cell *)p;
431
432 /* Validate that there are enough LMBs to satisfy the request */
433 for (i = 0; i < num_lmbs; i++) {
434 if (lmbs[i].flags & DRCONF_MEM_ASSIGNED)
435 lmbs_available++;
436 }
437
438 if (lmbs_available < lmbs_to_remove)
439 return -EINVAL;
440
441 for (i = 0; i < num_lmbs && lmbs_removed < lmbs_to_remove; i++) {
442 rc = dlpar_remove_lmb(&lmbs[i]);
443 if (rc)
444 continue;
445
446 lmbs_removed++;
447
448 /* Mark this lmb so we can add it later if all of the
449 * requested LMBs cannot be removed.
450 */
451 lmbs[i].reserved = 1;
452 }
453
454 if (lmbs_removed != lmbs_to_remove) {
455 pr_err("Memory hot-remove failed, adding LMB's back\n");
456
457 for (i = 0; i < num_lmbs; i++) {
458 if (!lmbs[i].reserved)
459 continue;
460
461 rc = dlpar_add_lmb(&lmbs[i]);
462 if (rc)
463 pr_err("Failed to add LMB back, drc index %x\n",
464 lmbs[i].drc_index);
465
466 lmbs[i].reserved = 0;
467 }
468
469 rc = -EINVAL;
470 } else {
471 for (i = 0; i < num_lmbs; i++) {
472 if (!lmbs[i].reserved)
473 continue;
474
475 pr_info("Memory at %llx was hot-removed\n",
476 lmbs[i].base_addr);
477
478 lmbs[i].reserved = 0;
479 }
480 rc = 0;
481 }
482
483 return rc;
484}
485
486static int dlpar_memory_remove_by_index(u32 drc_index, struct property *prop)
487{
488 struct of_drconf_cell *lmbs;
489 u32 num_lmbs, *p;
490 int lmb_found;
491 int i, rc;
492
493 pr_info("Attempting to hot-remove LMB, drc index %x\n", drc_index);
494
495 p = prop->value;
496 num_lmbs = *p++;
497 lmbs = (struct of_drconf_cell *)p;
498
499 lmb_found = 0;
500 for (i = 0; i < num_lmbs; i++) {
501 if (lmbs[i].drc_index == drc_index) {
502 lmb_found = 1;
503 rc = dlpar_remove_lmb(&lmbs[i]);
504 break;
505 }
506 }
507
508 if (!lmb_found)
509 rc = -EINVAL;
510
511 if (rc)
512 pr_info("Failed to hot-remove memory at %llx\n",
513 lmbs[i].base_addr);
514 else
515 pr_info("Memory at %llx was hot-removed\n", lmbs[i].base_addr);
516
517 return rc;
518}
519
David Rientjes4edd7ce2013-04-29 15:08:22 -0700520#else
521static inline int pseries_remove_memblock(unsigned long base,
522 unsigned int memblock_size)
523{
524 return -EOPNOTSUPP;
525}
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600526static inline int pseries_remove_mem_node(struct device_node *np)
David Rientjes4edd7ce2013-04-29 15:08:22 -0700527{
Gavin Shanf1b39292014-08-11 19:16:19 +1000528 return 0;
David Rientjes4edd7ce2013-04-29 15:08:22 -0700529}
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600530static inline int dlpar_memory_remove(struct pseries_hp_errorlog *hp_elog)
531{
532 return -EOPNOTSUPP;
533}
Alexey Kardashevskiy16e00f52015-04-14 17:01:56 +1000534static int dlpar_remove_lmb(struct of_drconf_cell *lmb)
535{
536 return -EOPNOTSUPP;
537}
538static int dlpar_memory_remove_by_count(u32 lmbs_to_remove,
539 struct property *prop)
540{
541 return -EOPNOTSUPP;
542}
543static int dlpar_memory_remove_by_index(u32 drc_index, struct property *prop)
544{
545 return -EOPNOTSUPP;
546}
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600547
David Rientjes4edd7ce2013-04-29 15:08:22 -0700548#endif /* CONFIG_MEMORY_HOTREMOVE */
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000549
Nathan Fontenot4a4bdfe2016-02-10 11:10:44 -0600550static int dlpar_add_lmb_memory(struct of_drconf_cell *lmb)
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600551{
552 struct memory_block *mem_block;
553 unsigned long block_sz;
554 int nid, rc;
555
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600556 block_sz = memory_block_size_bytes();
557
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600558 /* Find the node id for this address */
559 nid = memory_add_physaddr_to_nid(lmb->base_addr);
560
561 /* Add the memory */
562 rc = add_memory(nid, lmb->base_addr, block_sz);
Nathan Fontenot4a4bdfe2016-02-10 11:10:44 -0600563 if (rc)
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600564 return rc;
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600565
566 /* Register this block of memory */
567 rc = memblock_add(lmb->base_addr, block_sz);
568 if (rc) {
569 remove_memory(nid, lmb->base_addr, block_sz);
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600570 return rc;
571 }
572
573 mem_block = lmb_to_memblock(lmb);
574 if (!mem_block) {
575 remove_memory(nid, lmb->base_addr, block_sz);
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600576 return -EINVAL;
577 }
578
579 rc = device_online(&mem_block->dev);
580 put_device(&mem_block->dev);
581 if (rc) {
582 remove_memory(nid, lmb->base_addr, block_sz);
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600583 return rc;
584 }
585
586 lmb->flags |= DRCONF_MEM_ASSIGNED;
587 return 0;
588}
589
Nathan Fontenot4a4bdfe2016-02-10 11:10:44 -0600590static int dlpar_add_lmb(struct of_drconf_cell *lmb)
591{
592 int rc;
593
594 if (lmb->flags & DRCONF_MEM_ASSIGNED)
595 return -EINVAL;
596
597 rc = dlpar_acquire_drc(lmb->drc_index);
598 if (rc)
599 return rc;
600
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600601 rc = dlpar_add_device_tree_lmb(lmb);
602 if (rc) {
603 pr_err("Couldn't update device tree for drc index %x\n",
604 lmb->drc_index);
Nathan Fontenot4a4bdfe2016-02-10 11:10:44 -0600605 dlpar_release_drc(lmb->drc_index);
Nathan Fontenotbdf5fc62016-02-10 11:12:13 -0600606 return rc;
607 }
608
609 rc = dlpar_add_lmb_memory(lmb);
610 if (rc) {
611 dlpar_remove_device_tree_lmb(lmb);
612 dlpar_release_drc(lmb->drc_index);
613 }
Nathan Fontenot4a4bdfe2016-02-10 11:10:44 -0600614
615 return rc;
616}
617
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600618static int dlpar_memory_add_by_count(u32 lmbs_to_add, struct property *prop)
619{
620 struct of_drconf_cell *lmbs;
621 u32 num_lmbs, *p;
622 int lmbs_available = 0;
623 int lmbs_added = 0;
624 int i, rc;
625
626 pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add);
627
628 if (lmbs_to_add == 0)
629 return -EINVAL;
630
631 p = prop->value;
632 num_lmbs = *p++;
633 lmbs = (struct of_drconf_cell *)p;
634
635 /* Validate that there are enough LMBs to satisfy the request */
636 for (i = 0; i < num_lmbs; i++) {
637 if (!(lmbs[i].flags & DRCONF_MEM_ASSIGNED))
638 lmbs_available++;
639 }
640
641 if (lmbs_available < lmbs_to_add)
642 return -EINVAL;
643
644 for (i = 0; i < num_lmbs && lmbs_to_add != lmbs_added; i++) {
645 rc = dlpar_add_lmb(&lmbs[i]);
646 if (rc)
647 continue;
648
649 lmbs_added++;
650
651 /* Mark this lmb so we can remove it later if all of the
652 * requested LMBs cannot be added.
653 */
654 lmbs[i].reserved = 1;
655 }
656
657 if (lmbs_added != lmbs_to_add) {
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600658 pr_err("Memory hot-add failed, removing any added LMBs\n");
659
660 for (i = 0; i < num_lmbs; i++) {
661 if (!lmbs[i].reserved)
662 continue;
663
664 rc = dlpar_remove_lmb(&lmbs[i]);
665 if (rc)
666 pr_err("Failed to remove LMB, drc index %x\n",
667 be32_to_cpu(lmbs[i].drc_index));
668 }
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600669 rc = -EINVAL;
670 } else {
671 for (i = 0; i < num_lmbs; i++) {
672 if (!lmbs[i].reserved)
673 continue;
674
675 pr_info("Memory at %llx (drc index %x) was hot-added\n",
676 lmbs[i].base_addr, lmbs[i].drc_index);
677 lmbs[i].reserved = 0;
678 }
679 }
680
681 return rc;
682}
683
684static int dlpar_memory_add_by_index(u32 drc_index, struct property *prop)
685{
686 struct of_drconf_cell *lmbs;
687 u32 num_lmbs, *p;
688 int i, lmb_found;
689 int rc;
690
691 pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index);
692
693 p = prop->value;
694 num_lmbs = *p++;
695 lmbs = (struct of_drconf_cell *)p;
696
697 lmb_found = 0;
698 for (i = 0; i < num_lmbs; i++) {
699 if (lmbs[i].drc_index == drc_index) {
700 lmb_found = 1;
701 rc = dlpar_add_lmb(&lmbs[i]);
702 break;
703 }
704 }
705
706 if (!lmb_found)
707 rc = -EINVAL;
708
709 if (rc)
710 pr_info("Failed to hot-add memory, drc index %x\n", drc_index);
711 else
712 pr_info("Memory at %llx (drc index %x) was hot-added\n",
713 lmbs[i].base_addr, drc_index);
714
715 return rc;
716}
717
Nathan Fontenot999e2da2015-02-10 13:47:02 -0600718int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
719{
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600720 struct device_node *dn;
721 struct property *prop;
722 u32 count, drc_index;
723 int rc;
724
725 count = hp_elog->_drc_u.drc_count;
726 drc_index = hp_elog->_drc_u.drc_index;
Nathan Fontenot999e2da2015-02-10 13:47:02 -0600727
728 lock_device_hotplug();
729
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600730 dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
Nathan Fontenotb0a478e2015-04-07 09:53:46 -0500731 if (!dn) {
732 rc = -EINVAL;
733 goto dlpar_memory_out;
734 }
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600735
736 prop = dlpar_clone_drconf_property(dn);
737 if (!prop) {
Nathan Fontenotb0a478e2015-04-07 09:53:46 -0500738 rc = -EINVAL;
739 goto dlpar_memory_out;
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600740 }
741
Nathan Fontenot999e2da2015-02-10 13:47:02 -0600742 switch (hp_elog->action) {
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600743 case PSERIES_HP_ELOG_ACTION_ADD:
744 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
745 rc = dlpar_memory_add_by_count(count, prop);
746 else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
747 rc = dlpar_memory_add_by_index(drc_index, prop);
748 else
749 rc = -EINVAL;
750 break;
Nathan Fontenot51925fb2015-02-10 13:49:22 -0600751 case PSERIES_HP_ELOG_ACTION_REMOVE:
752 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
753 rc = dlpar_memory_remove_by_count(count, prop);
754 else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
755 rc = dlpar_memory_remove_by_index(drc_index, prop);
756 else
757 rc = -EINVAL;
758 break;
Nathan Fontenot999e2da2015-02-10 13:47:02 -0600759 default:
760 pr_err("Invalid action (%d) specified\n", hp_elog->action);
761 rc = -EINVAL;
762 break;
763 }
764
Nathan Fontenotc2101c92016-06-20 09:00:39 -0500765 dlpar_free_property(prop);
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600766
Nathan Fontenotb0a478e2015-04-07 09:53:46 -0500767dlpar_memory_out:
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600768 of_node_put(dn);
Nathan Fontenot999e2da2015-02-10 13:47:02 -0600769 unlock_device_hotplug();
770 return rc;
771}
772
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600773static int pseries_add_mem_node(struct device_node *np)
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700774{
775 const char *type;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500776 const __be32 *regs;
Nathan Fontenot92ecd172008-07-03 13:20:58 +1000777 unsigned long base;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000778 unsigned int lmb_size;
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700779 int ret = -EINVAL;
780
781 /*
782 * Check to see if we are actually adding memory
783 */
784 type = of_get_property(np, "device_type", NULL);
785 if (type == NULL || strcmp(type, "memory") != 0)
786 return 0;
787
788 /*
Yinghai Lu95f72d12010-07-12 14:36:09 +1000789 * Find the base and size of the memblock
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700790 */
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700791 regs = of_get_property(np, "reg", NULL);
792 if (!regs)
793 return ret;
794
Thomas Falconc9ac4082014-08-19 15:44:57 -0500795 base = be64_to_cpu(*(unsigned long *)regs);
796 lmb_size = be32_to_cpu(regs[3]);
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700797
798 /*
799 * Update memory region to represent the memory add
800 */
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000801 ret = memblock_add(base, lmb_size);
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000802 return (ret < 0) ? -EINVAL : 0;
803}
804
Grant Likelyf5242e52014-11-24 17:58:01 +0000805static int pseries_update_drconf_memory(struct of_reconfig_data *pr)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000806{
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000807 struct of_drconf_cell *new_drmem, *old_drmem;
Nathan Fontenotc540ada2011-01-20 10:45:20 -0600808 unsigned long memblock_size;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000809 u32 entries;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500810 __be32 *p;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000811 int i, rc = -EINVAL;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000812
Nathan Fontenot5f97b2a2015-02-10 13:48:25 -0600813 if (rtas_hp_event)
814 return 0;
815
Anton Blancharda5d86252014-06-04 17:50:47 +1000816 memblock_size = pseries_memory_block_size();
Nathan Fontenotc540ada2011-01-20 10:45:20 -0600817 if (!memblock_size)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000818 return -EINVAL;
819
Thomas Falconc9ac4082014-08-19 15:44:57 -0500820 p = (__be32 *) pr->old_prop->value;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000821 if (!p)
822 return -EINVAL;
823
824 /* The first int of the property is the number of lmb's described
825 * by the property. This is followed by an array of of_drconf_cell
Li Zhong4646d132014-08-07 13:11:58 +0800826 * entries. Get the number of entries and skip to the array of
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000827 * of_drconf_cell's.
828 */
Thomas Falconc9ac4082014-08-19 15:44:57 -0500829 entries = be32_to_cpu(*p++);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000830 old_drmem = (struct of_drconf_cell *)p;
831
Thomas Falconc9ac4082014-08-19 15:44:57 -0500832 p = (__be32 *)pr->prop->value;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000833 p++;
834 new_drmem = (struct of_drconf_cell *)p;
835
836 for (i = 0; i < entries; i++) {
Thomas Falconc9ac4082014-08-19 15:44:57 -0500837 if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) &&
838 (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) {
839 rc = pseries_remove_memblock(
840 be64_to_cpu(old_drmem[i].base_addr),
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000841 memblock_size);
842 break;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500843 } else if ((!(be32_to_cpu(old_drmem[i].flags) &
844 DRCONF_MEM_ASSIGNED)) &&
845 (be32_to_cpu(new_drmem[i].flags) &
846 DRCONF_MEM_ASSIGNED)) {
847 rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr),
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000848 memblock_size);
849 rc = (rc < 0) ? -EINVAL : 0;
850 break;
851 }
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000852 }
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000853 return rc;
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700854}
855
Badari Pulavarty57b53922008-04-18 13:33:50 -0700856static int pseries_memory_notifier(struct notifier_block *nb,
Grant Likelyf5242e52014-11-24 17:58:01 +0000857 unsigned long action, void *data)
Badari Pulavarty57b53922008-04-18 13:33:50 -0700858{
Grant Likelyf5242e52014-11-24 17:58:01 +0000859 struct of_reconfig_data *rd = data;
Akinobu Mitade2780a2011-06-21 03:35:56 +0000860 int err = 0;
Badari Pulavarty57b53922008-04-18 13:33:50 -0700861
862 switch (action) {
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000863 case OF_RECONFIG_ATTACH_NODE:
Grant Likelyf5242e52014-11-24 17:58:01 +0000864 err = pseries_add_mem_node(rd->dn);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700865 break;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000866 case OF_RECONFIG_DETACH_NODE:
Grant Likelyf5242e52014-11-24 17:58:01 +0000867 err = pseries_remove_mem_node(rd->dn);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700868 break;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000869 case OF_RECONFIG_UPDATE_PROPERTY:
Grant Likelyf5242e52014-11-24 17:58:01 +0000870 if (!strcmp(rd->prop->name, "ibm,dynamic-memory"))
871 err = pseries_update_drconf_memory(rd);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700872 break;
873 }
Akinobu Mitade2780a2011-06-21 03:35:56 +0000874 return notifier_from_errno(err);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700875}
876
877static struct notifier_block pseries_mem_nb = {
878 .notifier_call = pseries_memory_notifier,
879};
880
881static int __init pseries_memory_hotplug_init(void)
882{
883 if (firmware_has_feature(FW_FEATURE_LPAR))
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000884 of_reconfig_notifier_register(&pseries_mem_nb);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700885
Badari Pulavarty57b53922008-04-18 13:33:50 -0700886 return 0;
887}
888machine_device_initcall(pseries, pseries_memory_hotplug_init);