file_load_64.c 23 KB

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