1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * pseries Memory Hotplug infrastructure. 4 * 5 * Copyright (C) 2008 Badari Pulavarty, IBM Corporation 6 */ 7 8 #define pr_fmt(fmt) "pseries-hotplug-mem: " fmt 9 10 #include <linux/of.h> 11 #include <linux/of_address.h> 12 #include <linux/memblock.h> 13 #include <linux/memory.h> 14 #include <linux/memory_hotplug.h> 15 #include <linux/slab.h> 16 17 #include <asm/firmware.h> 18 #include <asm/machdep.h> 19 #include <asm/prom.h> 20 #include <asm/sparsemem.h> 21 #include <asm/fadump.h> 22 #include <asm/drmem.h> 23 #include "pseries.h" 24 25 static bool rtas_hp_event; 26 27 unsigned long pseries_memory_block_size(void) 28 { 29 struct device_node *np; 30 unsigned int memblock_size = MIN_MEMORY_BLOCK_SIZE; 31 struct resource r; 32 33 np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory"); 34 if (np) { 35 const __be64 *size; 36 37 size = of_get_property(np, "ibm,lmb-size", NULL); 38 if (size) 39 memblock_size = be64_to_cpup(size); 40 of_node_put(np); 41 } else if (machine_is(pseries)) { 42 /* This fallback really only applies to pseries */ 43 unsigned int memzero_size = 0; 44 45 np = of_find_node_by_path("/memory@0"); 46 if (np) { 47 if (!of_address_to_resource(np, 0, &r)) 48 memzero_size = resource_size(&r); 49 of_node_put(np); 50 } 51 52 if (memzero_size) { 53 /* We now know the size of memory@0, use this to find 54 * the first memoryblock and get its size. 55 */ 56 char buf[64]; 57 58 sprintf(buf, "/memory@%x", memzero_size); 59 np = of_find_node_by_path(buf); 60 if (np) { 61 if (!of_address_to_resource(np, 0, &r)) 62 memblock_size = resource_size(&r); 63 of_node_put(np); 64 } 65 } 66 } 67 return memblock_size; 68 } 69 70 static void dlpar_free_property(struct property *prop) 71 { 72 kfree(prop->name); 73 kfree(prop->value); 74 kfree(prop); 75 } 76 77 static struct property *dlpar_clone_property(struct property *prop, 78 u32 prop_size) 79 { 80 struct property *new_prop; 81 82 new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL); 83 if (!new_prop) 84 return NULL; 85 86 new_prop->name = kstrdup(prop->name, GFP_KERNEL); 87 new_prop->value = kzalloc(prop_size, GFP_KERNEL); 88 if (!new_prop->name || !new_prop->value) { 89 dlpar_free_property(new_prop); 90 return NULL; 91 } 92 93 memcpy(new_prop->value, prop->value, prop->length); 94 new_prop->length = prop_size; 95 96 of_property_set_flag(new_prop, OF_DYNAMIC); 97 return new_prop; 98 } 99 100 static bool find_aa_index(struct device_node *dr_node, 101 struct property *ala_prop, 102 const u32 *lmb_assoc, u32 *aa_index) 103 { 104 u32 *assoc_arrays, new_prop_size; 105 struct property *new_prop; 106 int aa_arrays, aa_array_entries, aa_array_sz; 107 int i, index; 108 109 /* 110 * The ibm,associativity-lookup-arrays property is defined to be 111 * a 32-bit value specifying the number of associativity arrays 112 * followed by a 32-bitvalue specifying the number of entries per 113 * array, followed by the associativity arrays. 114 */ 115 assoc_arrays = ala_prop->value; 116 117 aa_arrays = be32_to_cpu(assoc_arrays[0]); 118 aa_array_entries = be32_to_cpu(assoc_arrays[1]); 119 aa_array_sz = aa_array_entries * sizeof(u32); 120 121 for (i = 0; i < aa_arrays; i++) { 122 index = (i * aa_array_entries) + 2; 123 124 if (memcmp(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz)) 125 continue; 126 127 *aa_index = i; 128 return true; 129 } 130 131 new_prop_size = ala_prop->length + aa_array_sz; 132 new_prop = dlpar_clone_property(ala_prop, new_prop_size); 133 if (!new_prop) 134 return false; 135 136 assoc_arrays = new_prop->value; 137 138 /* increment the number of entries in the lookup array */ 139 assoc_arrays[0] = cpu_to_be32(aa_arrays + 1); 140 141 /* copy the new associativity into the lookup array */ 142 index = aa_arrays * aa_array_entries + 2; 143 memcpy(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz); 144 145 of_update_property(dr_node, new_prop); 146 147 /* 148 * The associativity lookup array index for this lmb is 149 * number of entries - 1 since we added its associativity 150 * to the end of the lookup array. 151 */ 152 *aa_index = be32_to_cpu(assoc_arrays[0]) - 1; 153 return true; 154 } 155 156 static int update_lmb_associativity_index(struct drmem_lmb *lmb) 157 { 158 struct device_node *parent, *lmb_node, *dr_node; 159 struct property *ala_prop; 160 const u32 *lmb_assoc; 161 u32 aa_index; 162 bool found; 163 164 parent = of_find_node_by_path("/"); 165 if (!parent) 166 return -ENODEV; 167 168 lmb_node = dlpar_configure_connector(cpu_to_be32(lmb->drc_index), 169 parent); 170 of_node_put(parent); 171 if (!lmb_node) 172 return -EINVAL; 173 174 lmb_assoc = of_get_property(lmb_node, "ibm,associativity", NULL); 175 if (!lmb_assoc) { 176 dlpar_free_cc_nodes(lmb_node); 177 return -ENODEV; 178 } 179 180 dr_node = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory"); 181 if (!dr_node) { 182 dlpar_free_cc_nodes(lmb_node); 183 return -ENODEV; 184 } 185 186 ala_prop = of_find_property(dr_node, "ibm,associativity-lookup-arrays", 187 NULL); 188 if (!ala_prop) { 189 of_node_put(dr_node); 190 dlpar_free_cc_nodes(lmb_node); 191 return -ENODEV; 192 } 193 194 found = find_aa_index(dr_node, ala_prop, lmb_assoc, &aa_index); 195 196 of_node_put(dr_node); 197 dlpar_free_cc_nodes(lmb_node); 198 199 if (!found) { 200 pr_err("Could not find LMB associativity\n"); 201 return -1; 202 } 203 204 lmb->aa_index = aa_index; 205 return 0; 206 } 207 208 static struct memory_block *lmb_to_memblock(struct drmem_lmb *lmb) 209 { 210 unsigned long section_nr; 211 struct mem_section *mem_sect; 212 struct memory_block *mem_block; 213 214 section_nr = pfn_to_section_nr(PFN_DOWN(lmb->base_addr)); 215 mem_sect = __nr_to_section(section_nr); 216 217 mem_block = find_memory_block(mem_sect); 218 return mem_block; 219 } 220 221 static int get_lmb_range(u32 drc_index, int n_lmbs, 222 struct drmem_lmb **start_lmb, 223 struct drmem_lmb **end_lmb) 224 { 225 struct drmem_lmb *lmb, *start, *end; 226 struct drmem_lmb *last_lmb; 227 228 start = NULL; 229 for_each_drmem_lmb(lmb) { 230 if (lmb->drc_index == drc_index) { 231 start = lmb; 232 break; 233 } 234 } 235 236 if (!start) 237 return -EINVAL; 238 239 end = &start[n_lmbs - 1]; 240 241 last_lmb = &drmem_info->lmbs[drmem_info->n_lmbs - 1]; 242 if (end > last_lmb) 243 return -EINVAL; 244 245 *start_lmb = start; 246 *end_lmb = end; 247 return 0; 248 } 249 250 static int dlpar_change_lmb_state(struct drmem_lmb *lmb, bool online) 251 { 252 struct memory_block *mem_block; 253 int rc; 254 255 mem_block = lmb_to_memblock(lmb); 256 if (!mem_block) 257 return -EINVAL; 258 259 if (online && mem_block->dev.offline) 260 rc = device_online(&mem_block->dev); 261 else if (!online && !mem_block->dev.offline) 262 rc = device_offline(&mem_block->dev); 263 else 264 rc = 0; 265 266 put_device(&mem_block->dev); 267 268 return rc; 269 } 270 271 static int dlpar_online_lmb(struct drmem_lmb *lmb) 272 { 273 return dlpar_change_lmb_state(lmb, true); 274 } 275 276 #ifdef CONFIG_MEMORY_HOTREMOVE 277 static int dlpar_offline_lmb(struct drmem_lmb *lmb) 278 { 279 return dlpar_change_lmb_state(lmb, false); 280 } 281 282 static 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 290 lock_device_hotplug(); 291 292 if (!pfn_valid(start_pfn)) 293 goto out; 294 295 block_sz = pseries_memory_block_size(); 296 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 304 out: 305 /* Update memory regions for memory remove */ 306 memblock_remove(base, memblock_size); 307 unlock_device_hotplug(); 308 return 0; 309 } 310 311 static int pseries_remove_mem_node(struct device_node *np) 312 { 313 const __be32 *regs; 314 unsigned long base; 315 unsigned int lmb_size; 316 int ret = -EINVAL; 317 318 /* 319 * Check to see if we are actually removing memory 320 */ 321 if (!of_node_is_type(np, "memory")) 322 return 0; 323 324 /* 325 * Find the base address and size of the memblock 326 */ 327 regs = of_get_property(np, "reg", NULL); 328 if (!regs) 329 return ret; 330 331 base = be64_to_cpu(*(unsigned long *)regs); 332 lmb_size = be32_to_cpu(regs[3]); 333 334 pseries_remove_memblock(base, lmb_size); 335 return 0; 336 } 337 338 static bool lmb_is_removable(struct drmem_lmb *lmb) 339 { 340 int i, scns_per_block; 341 int rc = 1; 342 unsigned long pfn, block_sz; 343 u64 phys_addr; 344 345 if (!(lmb->flags & DRCONF_MEM_ASSIGNED)) 346 return false; 347 348 block_sz = memory_block_size_bytes(); 349 scns_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE; 350 phys_addr = lmb->base_addr; 351 352 #ifdef CONFIG_FA_DUMP 353 /* 354 * Don't hot-remove memory that falls in fadump boot memory area 355 * and memory that is reserved for capturing old kernel memory. 356 */ 357 if (is_fadump_memory_area(phys_addr, block_sz)) 358 return false; 359 #endif 360 361 for (i = 0; i < scns_per_block; i++) { 362 pfn = PFN_DOWN(phys_addr); 363 if (!pfn_present(pfn)) 364 continue; 365 366 rc &= is_mem_section_removable(pfn, PAGES_PER_SECTION); 367 phys_addr += MIN_MEMORY_BLOCK_SIZE; 368 } 369 370 return rc ? true : false; 371 } 372 373 static int dlpar_add_lmb(struct drmem_lmb *); 374 375 static int dlpar_remove_lmb(struct drmem_lmb *lmb) 376 { 377 unsigned long block_sz; 378 int rc; 379 380 if (!lmb_is_removable(lmb)) 381 return -EINVAL; 382 383 rc = dlpar_offline_lmb(lmb); 384 if (rc) 385 return rc; 386 387 block_sz = pseries_memory_block_size(); 388 389 __remove_memory(lmb->nid, lmb->base_addr, block_sz); 390 391 /* Update memory regions for memory remove */ 392 memblock_remove(lmb->base_addr, block_sz); 393 394 invalidate_lmb_associativity_index(lmb); 395 lmb_clear_nid(lmb); 396 lmb->flags &= ~DRCONF_MEM_ASSIGNED; 397 398 return 0; 399 } 400 401 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove) 402 { 403 struct drmem_lmb *lmb; 404 int lmbs_removed = 0; 405 int lmbs_available = 0; 406 int rc; 407 408 pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove); 409 410 if (lmbs_to_remove == 0) 411 return -EINVAL; 412 413 /* Validate that there are enough LMBs to satisfy the request */ 414 for_each_drmem_lmb(lmb) { 415 if (lmb_is_removable(lmb)) 416 lmbs_available++; 417 418 if (lmbs_available == lmbs_to_remove) 419 break; 420 } 421 422 if (lmbs_available < lmbs_to_remove) { 423 pr_info("Not enough LMBs available (%d of %d) to satisfy request\n", 424 lmbs_available, lmbs_to_remove); 425 return -EINVAL; 426 } 427 428 for_each_drmem_lmb(lmb) { 429 rc = dlpar_remove_lmb(lmb); 430 if (rc) 431 continue; 432 433 /* Mark this lmb so we can add it later if all of the 434 * requested LMBs cannot be removed. 435 */ 436 drmem_mark_lmb_reserved(lmb); 437 438 lmbs_removed++; 439 if (lmbs_removed == lmbs_to_remove) 440 break; 441 } 442 443 if (lmbs_removed != lmbs_to_remove) { 444 pr_err("Memory hot-remove failed, adding LMB's back\n"); 445 446 for_each_drmem_lmb(lmb) { 447 if (!drmem_lmb_reserved(lmb)) 448 continue; 449 450 rc = dlpar_add_lmb(lmb); 451 if (rc) 452 pr_err("Failed to add LMB back, drc index %x\n", 453 lmb->drc_index); 454 455 drmem_remove_lmb_reservation(lmb); 456 } 457 458 rc = -EINVAL; 459 } else { 460 for_each_drmem_lmb(lmb) { 461 if (!drmem_lmb_reserved(lmb)) 462 continue; 463 464 dlpar_release_drc(lmb->drc_index); 465 pr_info("Memory at %llx was hot-removed\n", 466 lmb->base_addr); 467 468 drmem_remove_lmb_reservation(lmb); 469 } 470 rc = 0; 471 } 472 473 return rc; 474 } 475 476 static int dlpar_memory_remove_by_index(u32 drc_index) 477 { 478 struct drmem_lmb *lmb; 479 int lmb_found; 480 int rc; 481 482 pr_info("Attempting to hot-remove LMB, drc index %x\n", drc_index); 483 484 lmb_found = 0; 485 for_each_drmem_lmb(lmb) { 486 if (lmb->drc_index == drc_index) { 487 lmb_found = 1; 488 rc = dlpar_remove_lmb(lmb); 489 if (!rc) 490 dlpar_release_drc(lmb->drc_index); 491 492 break; 493 } 494 } 495 496 if (!lmb_found) 497 rc = -EINVAL; 498 499 if (rc) 500 pr_info("Failed to hot-remove memory at %llx\n", 501 lmb->base_addr); 502 else 503 pr_info("Memory at %llx was hot-removed\n", lmb->base_addr); 504 505 return rc; 506 } 507 508 static int dlpar_memory_readd_by_index(u32 drc_index) 509 { 510 struct drmem_lmb *lmb; 511 int lmb_found; 512 int rc; 513 514 pr_info("Attempting to update LMB, drc index %x\n", drc_index); 515 516 lmb_found = 0; 517 for_each_drmem_lmb(lmb) { 518 if (lmb->drc_index == drc_index) { 519 lmb_found = 1; 520 rc = dlpar_remove_lmb(lmb); 521 if (!rc) { 522 rc = dlpar_add_lmb(lmb); 523 if (rc) 524 dlpar_release_drc(lmb->drc_index); 525 } 526 break; 527 } 528 } 529 530 if (!lmb_found) 531 rc = -EINVAL; 532 533 if (rc) 534 pr_info("Failed to update memory at %llx\n", 535 lmb->base_addr); 536 else 537 pr_info("Memory at %llx was updated\n", lmb->base_addr); 538 539 return rc; 540 } 541 542 static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index) 543 { 544 struct drmem_lmb *lmb, *start_lmb, *end_lmb; 545 int lmbs_available = 0; 546 int rc; 547 548 pr_info("Attempting to hot-remove %u LMB(s) at %x\n", 549 lmbs_to_remove, drc_index); 550 551 if (lmbs_to_remove == 0) 552 return -EINVAL; 553 554 rc = get_lmb_range(drc_index, lmbs_to_remove, &start_lmb, &end_lmb); 555 if (rc) 556 return -EINVAL; 557 558 /* Validate that there are enough LMBs to satisfy the request */ 559 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) { 560 if (lmb->flags & DRCONF_MEM_RESERVED) 561 break; 562 563 lmbs_available++; 564 } 565 566 if (lmbs_available < lmbs_to_remove) 567 return -EINVAL; 568 569 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) { 570 if (!(lmb->flags & DRCONF_MEM_ASSIGNED)) 571 continue; 572 573 rc = dlpar_remove_lmb(lmb); 574 if (rc) 575 break; 576 577 drmem_mark_lmb_reserved(lmb); 578 } 579 580 if (rc) { 581 pr_err("Memory indexed-count-remove failed, adding any removed LMBs\n"); 582 583 584 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) { 585 if (!drmem_lmb_reserved(lmb)) 586 continue; 587 588 rc = dlpar_add_lmb(lmb); 589 if (rc) 590 pr_err("Failed to add LMB, drc index %x\n", 591 lmb->drc_index); 592 593 drmem_remove_lmb_reservation(lmb); 594 } 595 rc = -EINVAL; 596 } else { 597 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) { 598 if (!drmem_lmb_reserved(lmb)) 599 continue; 600 601 dlpar_release_drc(lmb->drc_index); 602 pr_info("Memory at %llx (drc index %x) was hot-removed\n", 603 lmb->base_addr, lmb->drc_index); 604 605 drmem_remove_lmb_reservation(lmb); 606 } 607 } 608 609 return rc; 610 } 611 612 #else 613 static inline int pseries_remove_memblock(unsigned long base, 614 unsigned int memblock_size) 615 { 616 return -EOPNOTSUPP; 617 } 618 static inline int pseries_remove_mem_node(struct device_node *np) 619 { 620 return 0; 621 } 622 static inline int dlpar_memory_remove(struct pseries_hp_errorlog *hp_elog) 623 { 624 return -EOPNOTSUPP; 625 } 626 static int dlpar_remove_lmb(struct drmem_lmb *lmb) 627 { 628 return -EOPNOTSUPP; 629 } 630 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove) 631 { 632 return -EOPNOTSUPP; 633 } 634 static int dlpar_memory_remove_by_index(u32 drc_index) 635 { 636 return -EOPNOTSUPP; 637 } 638 static int dlpar_memory_readd_by_index(u32 drc_index) 639 { 640 return -EOPNOTSUPP; 641 } 642 643 static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index) 644 { 645 return -EOPNOTSUPP; 646 } 647 #endif /* CONFIG_MEMORY_HOTREMOVE */ 648 649 static int dlpar_add_lmb(struct drmem_lmb *lmb) 650 { 651 unsigned long block_sz; 652 int rc; 653 654 if (lmb->flags & DRCONF_MEM_ASSIGNED) 655 return -EINVAL; 656 657 rc = update_lmb_associativity_index(lmb); 658 if (rc) { 659 dlpar_release_drc(lmb->drc_index); 660 return rc; 661 } 662 663 lmb_set_nid(lmb); 664 block_sz = memory_block_size_bytes(); 665 666 /* Add the memory */ 667 rc = __add_memory(lmb->nid, lmb->base_addr, block_sz); 668 if (rc) { 669 invalidate_lmb_associativity_index(lmb); 670 return rc; 671 } 672 673 rc = dlpar_online_lmb(lmb); 674 if (rc) { 675 __remove_memory(lmb->nid, lmb->base_addr, block_sz); 676 invalidate_lmb_associativity_index(lmb); 677 lmb_clear_nid(lmb); 678 } else { 679 lmb->flags |= DRCONF_MEM_ASSIGNED; 680 } 681 682 return rc; 683 } 684 685 static int dlpar_memory_add_by_count(u32 lmbs_to_add) 686 { 687 struct drmem_lmb *lmb; 688 int lmbs_available = 0; 689 int lmbs_added = 0; 690 int rc; 691 692 pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add); 693 694 if (lmbs_to_add == 0) 695 return -EINVAL; 696 697 /* Validate that there are enough LMBs to satisfy the request */ 698 for_each_drmem_lmb(lmb) { 699 if (!(lmb->flags & DRCONF_MEM_ASSIGNED)) 700 lmbs_available++; 701 702 if (lmbs_available == lmbs_to_add) 703 break; 704 } 705 706 if (lmbs_available < lmbs_to_add) 707 return -EINVAL; 708 709 for_each_drmem_lmb(lmb) { 710 if (lmb->flags & DRCONF_MEM_ASSIGNED) 711 continue; 712 713 rc = dlpar_acquire_drc(lmb->drc_index); 714 if (rc) 715 continue; 716 717 rc = dlpar_add_lmb(lmb); 718 if (rc) { 719 dlpar_release_drc(lmb->drc_index); 720 continue; 721 } 722 723 /* Mark this lmb so we can remove it later if all of the 724 * requested LMBs cannot be added. 725 */ 726 drmem_mark_lmb_reserved(lmb); 727 728 lmbs_added++; 729 if (lmbs_added == lmbs_to_add) 730 break; 731 } 732 733 if (lmbs_added != lmbs_to_add) { 734 pr_err("Memory hot-add failed, removing any added LMBs\n"); 735 736 for_each_drmem_lmb(lmb) { 737 if (!drmem_lmb_reserved(lmb)) 738 continue; 739 740 rc = dlpar_remove_lmb(lmb); 741 if (rc) 742 pr_err("Failed to remove LMB, drc index %x\n", 743 lmb->drc_index); 744 else 745 dlpar_release_drc(lmb->drc_index); 746 747 drmem_remove_lmb_reservation(lmb); 748 } 749 rc = -EINVAL; 750 } else { 751 for_each_drmem_lmb(lmb) { 752 if (!drmem_lmb_reserved(lmb)) 753 continue; 754 755 pr_info("Memory at %llx (drc index %x) was hot-added\n", 756 lmb->base_addr, lmb->drc_index); 757 drmem_remove_lmb_reservation(lmb); 758 } 759 rc = 0; 760 } 761 762 return rc; 763 } 764 765 static int dlpar_memory_add_by_index(u32 drc_index) 766 { 767 struct drmem_lmb *lmb; 768 int rc, lmb_found; 769 770 pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index); 771 772 lmb_found = 0; 773 for_each_drmem_lmb(lmb) { 774 if (lmb->drc_index == drc_index) { 775 lmb_found = 1; 776 rc = dlpar_acquire_drc(lmb->drc_index); 777 if (!rc) { 778 rc = dlpar_add_lmb(lmb); 779 if (rc) 780 dlpar_release_drc(lmb->drc_index); 781 } 782 783 break; 784 } 785 } 786 787 if (!lmb_found) 788 rc = -EINVAL; 789 790 if (rc) 791 pr_info("Failed to hot-add memory, drc index %x\n", drc_index); 792 else 793 pr_info("Memory at %llx (drc index %x) was hot-added\n", 794 lmb->base_addr, drc_index); 795 796 return rc; 797 } 798 799 static int dlpar_memory_add_by_ic(u32 lmbs_to_add, u32 drc_index) 800 { 801 struct drmem_lmb *lmb, *start_lmb, *end_lmb; 802 int lmbs_available = 0; 803 int rc; 804 805 pr_info("Attempting to hot-add %u LMB(s) at index %x\n", 806 lmbs_to_add, drc_index); 807 808 if (lmbs_to_add == 0) 809 return -EINVAL; 810 811 rc = get_lmb_range(drc_index, lmbs_to_add, &start_lmb, &end_lmb); 812 if (rc) 813 return -EINVAL; 814 815 /* Validate that the LMBs in this range are not reserved */ 816 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) { 817 if (lmb->flags & DRCONF_MEM_RESERVED) 818 break; 819 820 lmbs_available++; 821 } 822 823 if (lmbs_available < lmbs_to_add) 824 return -EINVAL; 825 826 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) { 827 if (lmb->flags & DRCONF_MEM_ASSIGNED) 828 continue; 829 830 rc = dlpar_acquire_drc(lmb->drc_index); 831 if (rc) 832 break; 833 834 rc = dlpar_add_lmb(lmb); 835 if (rc) { 836 dlpar_release_drc(lmb->drc_index); 837 break; 838 } 839 840 drmem_mark_lmb_reserved(lmb); 841 } 842 843 if (rc) { 844 pr_err("Memory indexed-count-add failed, removing any added LMBs\n"); 845 846 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) { 847 if (!drmem_lmb_reserved(lmb)) 848 continue; 849 850 rc = dlpar_remove_lmb(lmb); 851 if (rc) 852 pr_err("Failed to remove LMB, drc index %x\n", 853 lmb->drc_index); 854 else 855 dlpar_release_drc(lmb->drc_index); 856 857 drmem_remove_lmb_reservation(lmb); 858 } 859 rc = -EINVAL; 860 } else { 861 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) { 862 if (!drmem_lmb_reserved(lmb)) 863 continue; 864 865 pr_info("Memory at %llx (drc index %x) was hot-added\n", 866 lmb->base_addr, lmb->drc_index); 867 drmem_remove_lmb_reservation(lmb); 868 } 869 } 870 871 return rc; 872 } 873 874 int dlpar_memory(struct pseries_hp_errorlog *hp_elog) 875 { 876 u32 count, drc_index; 877 int rc; 878 879 lock_device_hotplug(); 880 881 switch (hp_elog->action) { 882 case PSERIES_HP_ELOG_ACTION_ADD: 883 switch (hp_elog->id_type) { 884 case PSERIES_HP_ELOG_ID_DRC_COUNT: 885 count = hp_elog->_drc_u.drc_count; 886 rc = dlpar_memory_add_by_count(count); 887 break; 888 case PSERIES_HP_ELOG_ID_DRC_INDEX: 889 drc_index = hp_elog->_drc_u.drc_index; 890 rc = dlpar_memory_add_by_index(drc_index); 891 break; 892 case PSERIES_HP_ELOG_ID_DRC_IC: 893 count = hp_elog->_drc_u.ic.count; 894 drc_index = hp_elog->_drc_u.ic.index; 895 rc = dlpar_memory_add_by_ic(count, drc_index); 896 break; 897 default: 898 rc = -EINVAL; 899 break; 900 } 901 902 break; 903 case PSERIES_HP_ELOG_ACTION_REMOVE: 904 switch (hp_elog->id_type) { 905 case PSERIES_HP_ELOG_ID_DRC_COUNT: 906 count = hp_elog->_drc_u.drc_count; 907 rc = dlpar_memory_remove_by_count(count); 908 break; 909 case PSERIES_HP_ELOG_ID_DRC_INDEX: 910 drc_index = hp_elog->_drc_u.drc_index; 911 rc = dlpar_memory_remove_by_index(drc_index); 912 break; 913 case PSERIES_HP_ELOG_ID_DRC_IC: 914 count = hp_elog->_drc_u.ic.count; 915 drc_index = hp_elog->_drc_u.ic.index; 916 rc = dlpar_memory_remove_by_ic(count, drc_index); 917 break; 918 default: 919 rc = -EINVAL; 920 break; 921 } 922 923 break; 924 case PSERIES_HP_ELOG_ACTION_READD: 925 drc_index = hp_elog->_drc_u.drc_index; 926 rc = dlpar_memory_readd_by_index(drc_index); 927 break; 928 default: 929 pr_err("Invalid action (%d) specified\n", hp_elog->action); 930 rc = -EINVAL; 931 break; 932 } 933 934 if (!rc) { 935 rtas_hp_event = true; 936 rc = drmem_update_dt(); 937 rtas_hp_event = false; 938 } 939 940 unlock_device_hotplug(); 941 return rc; 942 } 943 944 static int pseries_add_mem_node(struct device_node *np) 945 { 946 const __be32 *regs; 947 unsigned long base; 948 unsigned int lmb_size; 949 int ret = -EINVAL; 950 951 /* 952 * Check to see if we are actually adding memory 953 */ 954 if (!of_node_is_type(np, "memory")) 955 return 0; 956 957 /* 958 * Find the base and size of the memblock 959 */ 960 regs = of_get_property(np, "reg", NULL); 961 if (!regs) 962 return ret; 963 964 base = be64_to_cpu(*(unsigned long *)regs); 965 lmb_size = be32_to_cpu(regs[3]); 966 967 /* 968 * Update memory region to represent the memory add 969 */ 970 ret = memblock_add(base, lmb_size); 971 return (ret < 0) ? -EINVAL : 0; 972 } 973 974 static int pseries_update_drconf_memory(struct of_reconfig_data *pr) 975 { 976 struct of_drconf_cell_v1 *new_drmem, *old_drmem; 977 unsigned long memblock_size; 978 u32 entries; 979 __be32 *p; 980 int i, rc = -EINVAL; 981 982 if (rtas_hp_event) 983 return 0; 984 985 memblock_size = pseries_memory_block_size(); 986 if (!memblock_size) 987 return -EINVAL; 988 989 if (!pr->old_prop) 990 return 0; 991 992 p = (__be32 *) pr->old_prop->value; 993 if (!p) 994 return -EINVAL; 995 996 /* The first int of the property is the number of lmb's described 997 * by the property. This is followed by an array of of_drconf_cell 998 * entries. Get the number of entries and skip to the array of 999 * of_drconf_cell's. 1000 */ 1001 entries = be32_to_cpu(*p++); 1002 old_drmem = (struct of_drconf_cell_v1 *)p; 1003 1004 p = (__be32 *)pr->prop->value; 1005 p++; 1006 new_drmem = (struct of_drconf_cell_v1 *)p; 1007 1008 for (i = 0; i < entries; i++) { 1009 if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) && 1010 (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) { 1011 rc = pseries_remove_memblock( 1012 be64_to_cpu(old_drmem[i].base_addr), 1013 memblock_size); 1014 break; 1015 } else if ((!(be32_to_cpu(old_drmem[i].flags) & 1016 DRCONF_MEM_ASSIGNED)) && 1017 (be32_to_cpu(new_drmem[i].flags) & 1018 DRCONF_MEM_ASSIGNED)) { 1019 rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr), 1020 memblock_size); 1021 rc = (rc < 0) ? -EINVAL : 0; 1022 break; 1023 } 1024 } 1025 return rc; 1026 } 1027 1028 static int pseries_memory_notifier(struct notifier_block *nb, 1029 unsigned long action, void *data) 1030 { 1031 struct of_reconfig_data *rd = data; 1032 int err = 0; 1033 1034 switch (action) { 1035 case OF_RECONFIG_ATTACH_NODE: 1036 err = pseries_add_mem_node(rd->dn); 1037 break; 1038 case OF_RECONFIG_DETACH_NODE: 1039 err = pseries_remove_mem_node(rd->dn); 1040 break; 1041 case OF_RECONFIG_UPDATE_PROPERTY: 1042 if (!strcmp(rd->prop->name, "ibm,dynamic-memory")) 1043 err = pseries_update_drconf_memory(rd); 1044 break; 1045 } 1046 return notifier_from_errno(err); 1047 } 1048 1049 static struct notifier_block pseries_mem_nb = { 1050 .notifier_call = pseries_memory_notifier, 1051 }; 1052 1053 static int __init pseries_memory_hotplug_init(void) 1054 { 1055 if (firmware_has_feature(FW_FEATURE_LPAR)) 1056 of_reconfig_notifier_register(&pseries_mem_nb); 1057 1058 return 0; 1059 } 1060 machine_device_initcall(pseries, pseries_memory_hotplug_init); 1061