1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * ppc64 code to implement the kexec_file_load syscall 4 * 5 * Copyright (C) 2004 Adam Litke (agl@us.ibm.com) 6 * Copyright (C) 2004 IBM Corp. 7 * Copyright (C) 2004,2005 Milton D Miller II, IBM Corporation 8 * Copyright (C) 2005 R Sharada (sharada@in.ibm.com) 9 * Copyright (C) 2006 Mohan Kumar M (mohan@in.ibm.com) 10 * Copyright (C) 2020 IBM Corporation 11 * 12 * Based on kexec-tools' kexec-ppc64.c, kexec-elf-rel-ppc64.c, fs2dt.c. 13 * Heavily modified for the kernel by 14 * Hari Bathini, IBM Corporation. 15 */ 16 17 #include <linux/kexec.h> 18 #include <linux/of_fdt.h> 19 #include <linux/libfdt.h> 20 #include <linux/of.h> 21 #include <linux/of_address.h> 22 #include <linux/memblock.h> 23 #include <linux/slab.h> 24 #include <linux/vmalloc.h> 25 #include <asm/setup.h> 26 #include <asm/drmem.h> 27 #include <asm/firmware.h> 28 #include <asm/kexec_ranges.h> 29 #include <asm/crashdump-ppc64.h> 30 #include <asm/mmzone.h> 31 #include <asm/iommu.h> 32 #include <asm/prom.h> 33 #include <asm/plpks.h> 34 #include <asm/cputhreads.h> 35 36 struct umem_info { 37 __be64 *buf; /* data buffer for usable-memory property */ 38 u32 size; /* size allocated for the data buffer */ 39 u32 max_entries; /* maximum no. of entries */ 40 u32 idx; /* index of current entry */ 41 42 /* usable memory ranges to look up */ 43 unsigned int nr_ranges; 44 const struct range *ranges; 45 }; 46 47 const struct kexec_file_ops * const kexec_file_loaders[] = { 48 &kexec_elf64_ops, 49 NULL 50 }; 51 52 int arch_check_excluded_range(struct kimage *image, unsigned long start, 53 unsigned long end) 54 { 55 struct crash_mem *emem; 56 int i; 57 58 emem = image->arch.exclude_ranges; 59 for (i = 0; i < emem->nr_ranges; i++) 60 if (start < emem->ranges[i].end && end > emem->ranges[i].start) 61 return 1; 62 63 return 0; 64 } 65 66 #ifdef CONFIG_CRASH_DUMP 67 /** 68 * check_realloc_usable_mem - Reallocate buffer if it can't accommodate entries 69 * @um_info: Usable memory buffer and ranges info. 70 * @cnt: No. of entries to accommodate. 71 * 72 * Frees up the old buffer if memory reallocation fails. 73 * 74 * Returns buffer on success, NULL on error. 75 */ 76 static __be64 *check_realloc_usable_mem(struct umem_info *um_info, int cnt) 77 { 78 u32 new_size; 79 __be64 *tbuf; 80 81 if ((um_info->idx + cnt) <= um_info->max_entries) 82 return um_info->buf; 83 84 new_size = um_info->size + MEM_RANGE_CHUNK_SZ; 85 tbuf = krealloc(um_info->buf, new_size, GFP_KERNEL); 86 if (tbuf) { 87 um_info->buf = tbuf; 88 um_info->size = new_size; 89 um_info->max_entries = (um_info->size / sizeof(u64)); 90 } 91 92 return tbuf; 93 } 94 95 /** 96 * add_usable_mem - Add the usable memory ranges within the given memory range 97 * to the buffer 98 * @um_info: Usable memory buffer and ranges info. 99 * @base: Base address of memory range to look for. 100 * @end: End address of memory range to look for. 101 * 102 * Returns 0 on success, negative errno on error. 103 */ 104 static int add_usable_mem(struct umem_info *um_info, u64 base, u64 end) 105 { 106 u64 loc_base, loc_end; 107 bool add; 108 int i; 109 110 for (i = 0; i < um_info->nr_ranges; i++) { 111 add = false; 112 loc_base = um_info->ranges[i].start; 113 loc_end = um_info->ranges[i].end; 114 if (loc_base >= base && loc_end <= end) 115 add = true; 116 else if (base < loc_end && end > loc_base) { 117 if (loc_base < base) 118 loc_base = base; 119 if (loc_end > end) 120 loc_end = end; 121 add = true; 122 } 123 124 if (add) { 125 if (!check_realloc_usable_mem(um_info, 2)) 126 return -ENOMEM; 127 128 um_info->buf[um_info->idx++] = cpu_to_be64(loc_base); 129 um_info->buf[um_info->idx++] = 130 cpu_to_be64(loc_end - loc_base + 1); 131 } 132 } 133 134 return 0; 135 } 136 137 /** 138 * kdump_setup_usable_lmb - This is a callback function that gets called by 139 * walk_drmem_lmbs for every LMB to set its 140 * usable memory ranges. 141 * @lmb: LMB info. 142 * @usm: linux,drconf-usable-memory property value. 143 * @data: Pointer to usable memory buffer and ranges info. 144 * 145 * Returns 0 on success, negative errno on error. 146 */ 147 static int kdump_setup_usable_lmb(struct drmem_lmb *lmb, const __be32 **usm, 148 void *data) 149 { 150 struct umem_info *um_info; 151 int tmp_idx, ret; 152 u64 base, end; 153 154 /* 155 * kdump load isn't supported on kernels already booted with 156 * linux,drconf-usable-memory property. 157 */ 158 if (*usm) { 159 pr_err("linux,drconf-usable-memory property already exists!"); 160 return -EINVAL; 161 } 162 163 um_info = data; 164 tmp_idx = um_info->idx; 165 if (!check_realloc_usable_mem(um_info, 1)) 166 return -ENOMEM; 167 168 um_info->idx++; 169 base = lmb->base_addr; 170 end = base + drmem_lmb_size() - 1; 171 ret = add_usable_mem(um_info, base, end); 172 if (!ret) { 173 /* 174 * Update the no. of ranges added. Two entries (base & size) 175 * for every range added. 176 */ 177 um_info->buf[tmp_idx] = 178 cpu_to_be64((um_info->idx - tmp_idx - 1) / 2); 179 } 180 181 return ret; 182 } 183 184 #define NODE_PATH_LEN 256 185 /** 186 * add_usable_mem_property - Add usable memory property for the given 187 * memory node. 188 * @fdt: Flattened device tree for the kdump kernel. 189 * @dn: Memory node. 190 * @um_info: Usable memory buffer and ranges info. 191 * 192 * Returns 0 on success, negative errno on error. 193 */ 194 static int add_usable_mem_property(void *fdt, struct device_node *dn, 195 struct umem_info *um_info) 196 { 197 int node; 198 char path[NODE_PATH_LEN]; 199 int i, ret; 200 u64 base, size; 201 202 of_node_get(dn); 203 204 if (snprintf(path, NODE_PATH_LEN, "%pOF", dn) > (NODE_PATH_LEN - 1)) { 205 pr_err("Buffer (%d) too small for memory node: %pOF\n", 206 NODE_PATH_LEN, dn); 207 return -EOVERFLOW; 208 } 209 kexec_dprintk("Memory node path: %s\n", path); 210 211 /* Now that we know the path, find its offset in kdump kernel's fdt */ 212 node = fdt_path_offset(fdt, path); 213 if (node < 0) { 214 pr_err("Malformed device tree: error reading %s\n", path); 215 ret = -EINVAL; 216 goto out; 217 } 218 219 um_info->idx = 0; 220 if (!check_realloc_usable_mem(um_info, 2)) { 221 ret = -ENOMEM; 222 goto out; 223 } 224 225 /* 226 * "reg" property represents sequence of (addr,size) tuples 227 * each representing a memory range. 228 */ 229 for (i = 0; ; i++) { 230 ret = of_property_read_reg(dn, i, &base, &size); 231 if (ret) 232 break; 233 234 ret = add_usable_mem(um_info, base, base + size - 1); 235 if (ret) 236 goto out; 237 } 238 239 // No reg or empty reg? Skip this node. 240 if (i == 0) 241 goto out; 242 243 /* 244 * No kdump kernel usable memory found in this memory node. 245 * Write (0,0) tuple in linux,usable-memory property for 246 * this region to be ignored. 247 */ 248 if (um_info->idx == 0) { 249 um_info->buf[0] = 0; 250 um_info->buf[1] = 0; 251 um_info->idx = 2; 252 } 253 254 ret = fdt_setprop(fdt, node, "linux,usable-memory", um_info->buf, 255 (um_info->idx * sizeof(u64))); 256 257 out: 258 of_node_put(dn); 259 return ret; 260 } 261 262 263 /** 264 * update_usable_mem_fdt - Updates kdump kernel's fdt with linux,usable-memory 265 * and linux,drconf-usable-memory DT properties as 266 * appropriate to restrict its memory usage. 267 * @fdt: Flattened device tree for the kdump kernel. 268 * @usable_mem: Usable memory ranges for kdump kernel. 269 * 270 * Returns 0 on success, negative errno on error. 271 */ 272 static int update_usable_mem_fdt(void *fdt, struct crash_mem *usable_mem) 273 { 274 struct umem_info um_info; 275 struct device_node *dn; 276 int node, ret = 0; 277 278 if (!usable_mem) { 279 pr_err("Usable memory ranges for kdump kernel not found\n"); 280 return -ENOENT; 281 } 282 283 node = fdt_path_offset(fdt, "/ibm,dynamic-reconfiguration-memory"); 284 if (node == -FDT_ERR_NOTFOUND) 285 kexec_dprintk("No dynamic reconfiguration memory found\n"); 286 else if (node < 0) { 287 pr_err("Malformed device tree: error reading /ibm,dynamic-reconfiguration-memory.\n"); 288 return -EINVAL; 289 } 290 291 um_info.buf = NULL; 292 um_info.size = 0; 293 um_info.max_entries = 0; 294 um_info.idx = 0; 295 /* Memory ranges to look up */ 296 um_info.ranges = &(usable_mem->ranges[0]); 297 um_info.nr_ranges = usable_mem->nr_ranges; 298 299 dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory"); 300 if (dn) { 301 ret = walk_drmem_lmbs(dn, &um_info, kdump_setup_usable_lmb); 302 of_node_put(dn); 303 304 if (ret) { 305 pr_err("Could not setup linux,drconf-usable-memory property for kdump\n"); 306 goto out; 307 } 308 309 ret = fdt_setprop(fdt, node, "linux,drconf-usable-memory", 310 um_info.buf, (um_info.idx * sizeof(u64))); 311 if (ret) { 312 pr_err("Failed to update fdt with linux,drconf-usable-memory property: %s", 313 fdt_strerror(ret)); 314 goto out; 315 } 316 } 317 318 /* 319 * Walk through each memory node and set linux,usable-memory property 320 * for the corresponding node in kdump kernel's fdt. 321 */ 322 for_each_node_by_type(dn, "memory") { 323 ret = add_usable_mem_property(fdt, dn, &um_info); 324 if (ret) { 325 pr_err("Failed to set linux,usable-memory property for %s node", 326 dn->full_name); 327 of_node_put(dn); 328 goto out; 329 } 330 } 331 332 out: 333 kfree(um_info.buf); 334 return ret; 335 } 336 337 /** 338 * load_backup_segment - Locate a memory hole to place the backup region. 339 * @image: Kexec image. 340 * @kbuf: Buffer contents and memory parameters. 341 * 342 * Returns 0 on success, negative errno on error. 343 */ 344 static int load_backup_segment(struct kimage *image, struct kexec_buf *kbuf) 345 { 346 void *buf; 347 int ret; 348 349 /* 350 * Setup a source buffer for backup segment. 351 * 352 * A source buffer has no meaning for backup region as data will 353 * be copied from backup source, after crash, in the purgatory. 354 * But as load segment code doesn't recognize such segments, 355 * setup a dummy source buffer to keep it happy for now. 356 */ 357 buf = vzalloc(BACKUP_SRC_SIZE); 358 if (!buf) 359 return -ENOMEM; 360 361 kbuf->buffer = buf; 362 kbuf->mem = KEXEC_BUF_MEM_UNKNOWN; 363 kbuf->bufsz = kbuf->memsz = BACKUP_SRC_SIZE; 364 kbuf->top_down = false; 365 366 ret = kexec_add_buffer(kbuf); 367 if (ret) { 368 vfree(buf); 369 return ret; 370 } 371 372 image->arch.backup_buf = buf; 373 image->arch.backup_start = kbuf->mem; 374 return 0; 375 } 376 377 /** 378 * update_backup_region_phdr - Update backup region's offset for the core to 379 * export the region appropriately. 380 * @image: Kexec image. 381 * @ehdr: ELF core header. 382 * 383 * Assumes an exclusive program header is setup for the backup region 384 * in the ELF headers 385 * 386 * Returns nothing. 387 */ 388 static void update_backup_region_phdr(struct kimage *image, Elf64_Ehdr *ehdr) 389 { 390 Elf64_Phdr *phdr; 391 unsigned int i; 392 393 phdr = (Elf64_Phdr *)(ehdr + 1); 394 for (i = 0; i < ehdr->e_phnum; i++) { 395 if (phdr->p_paddr == BACKUP_SRC_START) { 396 phdr->p_offset = image->arch.backup_start; 397 kexec_dprintk("Backup region offset updated to 0x%lx\n", 398 image->arch.backup_start); 399 return; 400 } 401 } 402 } 403 404 static unsigned int kdump_extra_elfcorehdr_size(struct crash_mem *cmem) 405 { 406 #if defined(CONFIG_CRASH_HOTPLUG) && defined(CONFIG_MEMORY_HOTPLUG) 407 unsigned int extra_sz = 0; 408 409 if (CONFIG_CRASH_MAX_MEMORY_RANGES > (unsigned int)PN_XNUM) 410 pr_warn("Number of Phdrs %u exceeds max\n", CONFIG_CRASH_MAX_MEMORY_RANGES); 411 else if (cmem->nr_ranges >= CONFIG_CRASH_MAX_MEMORY_RANGES) 412 pr_warn("Configured crash mem ranges may not be enough\n"); 413 else 414 extra_sz = (CONFIG_CRASH_MAX_MEMORY_RANGES - cmem->nr_ranges) * sizeof(Elf64_Phdr); 415 416 return extra_sz; 417 #endif 418 return 0; 419 } 420 421 /** 422 * load_elfcorehdr_segment - Setup crash memory ranges and initialize elfcorehdr 423 * segment needed to load kdump kernel. 424 * @image: Kexec image. 425 * @kbuf: Buffer contents and memory parameters. 426 * 427 * Returns 0 on success, negative errno on error. 428 */ 429 static int load_elfcorehdr_segment(struct kimage *image, struct kexec_buf *kbuf) 430 { 431 struct crash_mem *cmem = NULL; 432 unsigned long headers_sz; 433 void *headers = NULL; 434 int ret; 435 436 ret = get_crash_memory_ranges(&cmem); 437 if (ret) 438 goto out; 439 440 /* Setup elfcorehdr segment */ 441 ret = crash_prepare_elf64_headers(cmem, false, &headers, &headers_sz); 442 if (ret) { 443 pr_err("Failed to prepare elf headers for the core\n"); 444 goto out; 445 } 446 447 /* Fix the offset for backup region in the ELF header */ 448 update_backup_region_phdr(image, headers); 449 450 kbuf->buffer = headers; 451 kbuf->mem = KEXEC_BUF_MEM_UNKNOWN; 452 kbuf->bufsz = headers_sz; 453 454 /* 455 * Account for extra space required to accommodate additional memory 456 * ranges in elfcorehdr due to memory hotplug events. 457 */ 458 kbuf->memsz = headers_sz + kdump_extra_elfcorehdr_size(cmem); 459 kbuf->top_down = false; 460 461 ret = kexec_add_buffer(kbuf); 462 if (ret) { 463 vfree(headers); 464 goto out; 465 } 466 467 image->elf_load_addr = kbuf->mem; 468 469 /* 470 * If CONFIG_CRASH_HOTPLUG is enabled, the elfcorehdr kexec segment 471 * memsz can be larger than bufsz. Always initialize elf_headers_sz 472 * with memsz. This ensures the correct size is reserved for elfcorehdr 473 * memory in the FDT prepared for kdump. 474 */ 475 image->elf_headers_sz = kbuf->memsz; 476 image->elf_headers = headers; 477 out: 478 kfree(cmem); 479 return ret; 480 } 481 482 /** 483 * load_crashdump_segments_ppc64 - Initialize the additional segements needed 484 * to load kdump kernel. 485 * @image: Kexec image. 486 * @kbuf: Buffer contents and memory parameters. 487 * 488 * Returns 0 on success, negative errno on error. 489 */ 490 int load_crashdump_segments_ppc64(struct kimage *image, 491 struct kexec_buf *kbuf) 492 { 493 int ret; 494 495 /* Load backup segment - first 64K bytes of the crashing kernel */ 496 ret = load_backup_segment(image, kbuf); 497 if (ret) { 498 pr_err("Failed to load backup segment\n"); 499 return ret; 500 } 501 kexec_dprintk("Loaded the backup region at 0x%lx\n", kbuf->mem); 502 503 /* Load elfcorehdr segment - to export crashing kernel's vmcore */ 504 ret = load_elfcorehdr_segment(image, kbuf); 505 if (ret) { 506 pr_err("Failed to load elfcorehdr segment\n"); 507 return ret; 508 } 509 kexec_dprintk("Loaded elf core header at 0x%lx, bufsz=0x%lx memsz=0x%lx\n", 510 image->elf_load_addr, kbuf->bufsz, kbuf->memsz); 511 512 return 0; 513 } 514 #endif 515 516 /** 517 * setup_purgatory_ppc64 - initialize PPC64 specific purgatory's global 518 * variables and call setup_purgatory() to initialize 519 * common global variable. 520 * @image: kexec image. 521 * @slave_code: Slave code for the purgatory. 522 * @fdt: Flattened device tree for the next kernel. 523 * @kernel_load_addr: Address where the kernel is loaded. 524 * @fdt_load_addr: Address where the flattened device tree is loaded. 525 * 526 * Returns 0 on success, negative errno on error. 527 */ 528 int setup_purgatory_ppc64(struct kimage *image, const void *slave_code, 529 const void *fdt, unsigned long kernel_load_addr, 530 unsigned long fdt_load_addr) 531 { 532 struct device_node *dn = NULL; 533 int ret; 534 535 ret = setup_purgatory(image, slave_code, fdt, kernel_load_addr, 536 fdt_load_addr); 537 if (ret) 538 goto out; 539 540 if (image->type == KEXEC_TYPE_CRASH) { 541 u32 my_run_at_load = 1; 542 543 /* 544 * Tell relocatable kernel to run at load address 545 * via the word meant for that at 0x5c. 546 */ 547 ret = kexec_purgatory_get_set_symbol(image, "run_at_load", 548 &my_run_at_load, 549 sizeof(my_run_at_load), 550 false); 551 if (ret) 552 goto out; 553 } 554 555 /* Tell purgatory where to look for backup region */ 556 ret = kexec_purgatory_get_set_symbol(image, "backup_start", 557 &image->arch.backup_start, 558 sizeof(image->arch.backup_start), 559 false); 560 if (ret) 561 goto out; 562 563 /* Setup OPAL base & entry values */ 564 dn = of_find_node_by_path("/ibm,opal"); 565 if (dn) { 566 u64 val; 567 568 ret = of_property_read_u64(dn, "opal-base-address", &val); 569 if (ret) 570 goto out; 571 572 ret = kexec_purgatory_get_set_symbol(image, "opal_base", &val, 573 sizeof(val), false); 574 if (ret) 575 goto out; 576 577 ret = of_property_read_u64(dn, "opal-entry-address", &val); 578 if (ret) 579 goto out; 580 ret = kexec_purgatory_get_set_symbol(image, "opal_entry", &val, 581 sizeof(val), false); 582 } 583 out: 584 if (ret) 585 pr_err("Failed to setup purgatory symbols"); 586 of_node_put(dn); 587 return ret; 588 } 589 590 /** 591 * cpu_node_size - Compute the size of a CPU node in the FDT. 592 * This should be done only once and the value is stored in 593 * a static variable. 594 * Returns the max size of a CPU node in the FDT. 595 */ 596 static unsigned int cpu_node_size(void) 597 { 598 static unsigned int size; 599 struct device_node *dn; 600 struct property *pp; 601 602 /* 603 * Don't compute it twice, we are assuming that the per CPU node size 604 * doesn't change during the system's life. 605 */ 606 if (size) 607 return size; 608 609 dn = of_find_node_by_type(NULL, "cpu"); 610 if (WARN_ON_ONCE(!dn)) { 611 // Unlikely to happen 612 return 0; 613 } 614 615 /* 616 * We compute the sub node size for a CPU node, assuming it 617 * will be the same for all. 618 */ 619 size += strlen(dn->name) + 5; 620 for_each_property_of_node(dn, pp) { 621 size += strlen(pp->name); 622 size += pp->length; 623 } 624 625 of_node_put(dn); 626 return size; 627 } 628 629 static unsigned int kdump_extra_fdt_size_ppc64(struct kimage *image, unsigned int cpu_nodes) 630 { 631 unsigned int extra_size = 0; 632 u64 usm_entries; 633 #ifdef CONFIG_CRASH_HOTPLUG 634 unsigned int possible_cpu_nodes; 635 #endif 636 637 if (!IS_ENABLED(CONFIG_CRASH_DUMP) || image->type != KEXEC_TYPE_CRASH) 638 return 0; 639 640 /* 641 * For kdump kernel, account for linux,usable-memory and 642 * linux,drconf-usable-memory properties. Get an approximate on the 643 * number of usable memory entries and use for FDT size estimation. 644 */ 645 if (drmem_lmb_size()) { 646 usm_entries = ((memory_hotplug_max() / drmem_lmb_size()) + 647 (2 * (resource_size(&crashk_res) / drmem_lmb_size()))); 648 extra_size += (unsigned int)(usm_entries * sizeof(u64)); 649 } 650 651 #ifdef CONFIG_CRASH_HOTPLUG 652 /* 653 * Make sure enough space is reserved to accommodate possible CPU nodes 654 * in the crash FDT. This allows packing possible CPU nodes which are 655 * not yet present in the system without regenerating the entire FDT. 656 */ 657 if (image->type == KEXEC_TYPE_CRASH) { 658 possible_cpu_nodes = num_possible_cpus() / threads_per_core; 659 if (possible_cpu_nodes > cpu_nodes) 660 extra_size += (possible_cpu_nodes - cpu_nodes) * cpu_node_size(); 661 } 662 #endif 663 664 return extra_size; 665 } 666 667 /** 668 * kexec_extra_fdt_size_ppc64 - Return the estimated additional size needed to 669 * setup FDT for kexec/kdump kernel. 670 * @image: kexec image being loaded. 671 * 672 * Returns the estimated extra size needed for kexec/kdump kernel FDT. 673 */ 674 unsigned int kexec_extra_fdt_size_ppc64(struct kimage *image, struct crash_mem *rmem) 675 { 676 struct device_node *dn; 677 unsigned int cpu_nodes = 0, extra_size = 0; 678 679 // Budget some space for the password blob. There's already extra space 680 // for the key name 681 if (plpks_is_available()) 682 extra_size += (unsigned int)plpks_get_passwordlen(); 683 684 /* Get the number of CPU nodes in the current device tree */ 685 for_each_node_by_type(dn, "cpu") { 686 cpu_nodes++; 687 } 688 689 /* Consider extra space for CPU nodes added since the boot time */ 690 if (cpu_nodes > boot_cpu_node_count) 691 extra_size += (cpu_nodes - boot_cpu_node_count) * cpu_node_size(); 692 693 /* Consider extra space for reserved memory ranges if any */ 694 if (rmem->nr_ranges > 0) 695 extra_size += sizeof(struct fdt_reserve_entry) * rmem->nr_ranges; 696 697 return extra_size + kdump_extra_fdt_size_ppc64(image, cpu_nodes); 698 } 699 700 static int copy_property(void *fdt, int node_offset, const struct device_node *dn, 701 const char *propname) 702 { 703 const void *prop, *fdtprop; 704 int len = 0, fdtlen = 0; 705 706 prop = of_get_property(dn, propname, &len); 707 fdtprop = fdt_getprop(fdt, node_offset, propname, &fdtlen); 708 709 if (fdtprop && !prop) 710 return fdt_delprop(fdt, node_offset, propname); 711 else if (prop) 712 return fdt_setprop(fdt, node_offset, propname, prop, len); 713 else 714 return -FDT_ERR_NOTFOUND; 715 } 716 717 static int update_pci_dma_nodes(void *fdt, const char *dmapropname) 718 { 719 struct device_node *dn; 720 int pci_offset, root_offset, ret = 0; 721 722 if (!firmware_has_feature(FW_FEATURE_LPAR)) 723 return 0; 724 725 root_offset = fdt_path_offset(fdt, "/"); 726 for_each_node_with_property(dn, dmapropname) { 727 pci_offset = fdt_subnode_offset(fdt, root_offset, of_node_full_name(dn)); 728 if (pci_offset < 0) 729 continue; 730 731 ret = copy_property(fdt, pci_offset, dn, "ibm,dma-window"); 732 if (ret < 0) { 733 of_node_put(dn); 734 break; 735 } 736 ret = copy_property(fdt, pci_offset, dn, dmapropname); 737 if (ret < 0) { 738 of_node_put(dn); 739 break; 740 } 741 } 742 743 return ret; 744 } 745 746 /** 747 * setup_new_fdt_ppc64 - Update the flattend device-tree of the kernel 748 * being loaded. 749 * @image: kexec image being loaded. 750 * @fdt: Flattened device tree for the next kernel. 751 * @rmem: Reserved memory ranges. 752 * 753 * Returns 0 on success, negative errno on error. 754 */ 755 int setup_new_fdt_ppc64(const struct kimage *image, void *fdt, struct crash_mem *rmem) 756 { 757 struct crash_mem *umem = NULL; 758 int i, nr_ranges, ret; 759 760 #ifdef CONFIG_CRASH_DUMP 761 /* 762 * Restrict memory usage for kdump kernel by setting up 763 * usable memory ranges and memory reserve map. 764 */ 765 if (image->type == KEXEC_TYPE_CRASH) { 766 ret = get_usable_memory_ranges(&umem); 767 if (ret) 768 goto out; 769 770 ret = update_usable_mem_fdt(fdt, umem); 771 if (ret) { 772 pr_err("Error setting up usable-memory property for kdump kernel\n"); 773 goto out; 774 } 775 776 /* 777 * Ensure we don't touch crashed kernel's memory except the 778 * first 64K of RAM, which will be backed up. 779 */ 780 ret = fdt_add_mem_rsv(fdt, BACKUP_SRC_END + 1, 781 crashk_res.start - BACKUP_SRC_SIZE); 782 if (ret) { 783 pr_err("Error reserving crash memory: %s\n", 784 fdt_strerror(ret)); 785 goto out; 786 } 787 788 /* Ensure backup region is not used by kdump/capture kernel */ 789 ret = fdt_add_mem_rsv(fdt, image->arch.backup_start, 790 BACKUP_SRC_SIZE); 791 if (ret) { 792 pr_err("Error reserving memory for backup: %s\n", 793 fdt_strerror(ret)); 794 goto out; 795 } 796 } 797 #endif 798 799 /* Update cpus nodes information to account hotplug CPUs. */ 800 ret = update_cpus_node(fdt); 801 if (ret < 0) 802 goto out; 803 804 ret = update_pci_dma_nodes(fdt, DIRECT64_PROPNAME); 805 if (ret < 0) 806 goto out; 807 808 ret = update_pci_dma_nodes(fdt, DMA64_PROPNAME); 809 if (ret < 0) 810 goto out; 811 812 /* Update memory reserve map */ 813 nr_ranges = rmem ? rmem->nr_ranges : 0; 814 for (i = 0; i < nr_ranges; i++) { 815 u64 base, size; 816 817 base = rmem->ranges[i].start; 818 size = rmem->ranges[i].end - base + 1; 819 ret = fdt_add_mem_rsv(fdt, base, size); 820 if (ret) { 821 pr_err("Error updating memory reserve map: %s\n", 822 fdt_strerror(ret)); 823 goto out; 824 } 825 } 826 827 // If we have PLPKS active, we need to provide the password to the new kernel 828 if (plpks_is_available()) 829 ret = plpks_populate_fdt(fdt); 830 831 out: 832 kfree(umem); 833 return ret; 834 } 835 836 /** 837 * arch_kexec_kernel_image_probe - Does additional handling needed to setup 838 * kexec segments. 839 * @image: kexec image being loaded. 840 * @buf: Buffer pointing to elf data. 841 * @buf_len: Length of the buffer. 842 * 843 * Returns 0 on success, negative errno on error. 844 */ 845 int arch_kexec_kernel_image_probe(struct kimage *image, void *buf, 846 unsigned long buf_len) 847 { 848 int ret; 849 850 /* Get exclude memory ranges needed for setting up kexec segments */ 851 ret = get_exclude_memory_ranges(&(image->arch.exclude_ranges)); 852 if (ret) { 853 pr_err("Failed to setup exclude memory ranges for buffer lookup\n"); 854 return ret; 855 } 856 857 return kexec_image_probe_default(image, buf, buf_len); 858 } 859 860 /** 861 * arch_kimage_file_post_load_cleanup - Frees up all the allocations done 862 * while loading the image. 863 * @image: kexec image being loaded. 864 * 865 * Returns 0 on success, negative errno on error. 866 */ 867 int arch_kimage_file_post_load_cleanup(struct kimage *image) 868 { 869 kfree(image->arch.exclude_ranges); 870 image->arch.exclude_ranges = NULL; 871 872 vfree(image->arch.backup_buf); 873 image->arch.backup_buf = NULL; 874 875 vfree(image->elf_headers); 876 image->elf_headers = NULL; 877 image->elf_headers_sz = 0; 878 879 kvfree(image->arch.fdt); 880 image->arch.fdt = NULL; 881 882 return kexec_image_post_load_cleanup_default(image); 883 } 884