// SPDX-License-Identifier: GPL-2.0-only1/*2* PPC64 code to handle Linux booting another kernel.3*4* Copyright (C) 2004-2005, IBM Corp.5*6* Created by: Milton D Miller II7*/8910#include <linux/kexec.h>11#include <linux/smp.h>12#include <linux/thread_info.h>13#include <linux/init_task.h>14#include <linux/errno.h>15#include <linux/kernel.h>16#include <linux/cpu.h>17#include <linux/hardirq.h>18#include <linux/of.h>19#include <linux/libfdt.h>2021#include <asm/page.h>22#include <asm/current.h>23#include <asm/machdep.h>24#include <asm/cacheflush.h>25#include <asm/firmware.h>26#include <asm/paca.h>27#include <asm/mmu.h>28#include <asm/sections.h> /* _end */29#include <asm/setup.h>30#include <asm/smp.h>31#include <asm/hw_breakpoint.h>32#include <asm/svm.h>33#include <asm/ultravisor.h>34#include <asm/crashdump-ppc64.h>3536int machine_kexec_prepare(struct kimage *image)37{38int i;39unsigned long begin, end; /* limits of segment */40unsigned long low, high; /* limits of blocked memory range */41struct device_node *node;42const unsigned long *basep;43const unsigned int *sizep;4445/*46* Since we use the kernel fault handlers and paging code to47* handle the virtual mode, we must make sure no destination48* overlaps kernel static data or bss.49*/50for (i = 0; i < image->nr_segments; i++)51if (image->segment[i].mem < __pa(_end))52return -ETXTBSY;5354/* We also should not overwrite the tce tables */55for_each_node_by_type(node, "pci") {56basep = of_get_property(node, "linux,tce-base", NULL);57sizep = of_get_property(node, "linux,tce-size", NULL);58if (basep == NULL || sizep == NULL)59continue;6061low = *basep;62high = low + (*sizep);6364for (i = 0; i < image->nr_segments; i++) {65begin = image->segment[i].mem;66end = begin + image->segment[i].memsz;6768if ((begin < high) && (end > low)) {69of_node_put(node);70return -ETXTBSY;71}72}73}7475return 0;76}7778/* Called during kexec sequence with MMU off */79static notrace void copy_segments(unsigned long ind)80{81unsigned long entry;82unsigned long *ptr;83void *dest;84void *addr;8586/*87* We rely on kexec_load to create a lists that properly88* initializes these pointers before they are used.89* We will still crash if the list is wrong, but at least90* the compiler will be quiet.91*/92ptr = NULL;93dest = NULL;9495for (entry = ind; !(entry & IND_DONE); entry = *ptr++) {96addr = __va(entry & PAGE_MASK);9798switch (entry & IND_FLAGS) {99case IND_DESTINATION:100dest = addr;101break;102case IND_INDIRECTION:103ptr = addr;104break;105case IND_SOURCE:106copy_page(dest, addr);107dest += PAGE_SIZE;108}109}110}111112/* Called during kexec sequence with MMU off */113notrace void kexec_copy_flush(struct kimage *image)114{115long i, nr_segments = image->nr_segments;116struct kexec_segment ranges[KEXEC_SEGMENT_MAX];117118/* save the ranges on the stack to efficiently flush the icache */119memcpy(ranges, image->segment, sizeof(ranges));120121/*122* After this call we may not use anything allocated in dynamic123* memory, including *image.124*125* Only globals and the stack are allowed.126*/127copy_segments(image->head);128129/*130* we need to clear the icache for all dest pages sometime,131* including ones that were in place on the original copy132*/133for (i = 0; i < nr_segments; i++)134flush_icache_range((unsigned long)__va(ranges[i].mem),135(unsigned long)__va(ranges[i].mem + ranges[i].memsz));136}137138#ifdef CONFIG_SMP139140static int kexec_all_irq_disabled = 0;141142static void kexec_smp_down(void *arg)143{144local_irq_disable();145hard_irq_disable();146147mb(); /* make sure our irqs are disabled before we say they are */148get_paca()->kexec_state = KEXEC_STATE_IRQS_OFF;149while(kexec_all_irq_disabled == 0)150cpu_relax();151mb(); /* make sure all irqs are disabled before this */152hw_breakpoint_disable();153/*154* Now every CPU has IRQs off, we can clear out any pending155* IPIs and be sure that no more will come in after this.156*/157if (ppc_md.kexec_cpu_down)158ppc_md.kexec_cpu_down(0, 1);159160reset_sprs();161162kexec_smp_wait();163/* NOTREACHED */164}165166static void kexec_prepare_cpus_wait(int wait_state)167{168int my_cpu, i, notified=-1;169170hw_breakpoint_disable();171my_cpu = get_cpu();172/* Make sure each CPU has at least made it to the state we need.173*174* FIXME: There is a (slim) chance of a problem if not all of the CPUs175* are correctly onlined. If somehow we start a CPU on boot with RTAS176* start-cpu, but somehow that CPU doesn't write callin_cpu_map[] in177* time, the boot CPU will timeout. If it does eventually execute178* stuff, the secondary will start up (paca_ptrs[]->cpu_start was179* written) and get into a peculiar state.180* If the platform supports smp_ops->take_timebase(), the secondary CPU181* will probably be spinning in there. If not (i.e. pseries), the182* secondary will continue on and try to online itself/idle/etc. If it183* survives that, we need to find these184* possible-but-not-online-but-should-be CPUs and chaperone them into185* kexec_smp_wait().186*/187for_each_online_cpu(i) {188if (i == my_cpu)189continue;190191while (paca_ptrs[i]->kexec_state < wait_state) {192barrier();193if (i != notified) {194printk(KERN_INFO "kexec: waiting for cpu %d "195"(physical %d) to enter %i state\n",196i, paca_ptrs[i]->hw_cpu_id, wait_state);197notified = i;198}199}200}201mb();202}203204205/*206* The add_cpu() call in wake_offline_cpus() can fail as cpu_bootable()207* returns false for CPUs that fail the cpu_smt_thread_allowed() check208* or non primary threads if SMT is disabled. Re-enable SMT and set the209* number of SMT threads to threads per core.210*/211static void kexec_smt_reenable(void)212{213#if defined(CONFIG_SMP) && defined(CONFIG_HOTPLUG_SMT)214lock_device_hotplug();215cpu_smt_num_threads = threads_per_core;216cpu_smt_control = CPU_SMT_ENABLED;217unlock_device_hotplug();218#endif219}220221/*222* We need to make sure each present CPU is online. The next kernel will scan223* the device tree and assume primary threads are online and query secondary224* threads via RTAS to online them if required. If we don't online primary225* threads, they will be stuck. However, we also online secondary threads as we226* may be using 'cede offline'. In this case RTAS doesn't see the secondary227* threads as offline -- and again, these CPUs will be stuck.228*229* So, we online all CPUs that should be running, including secondary threads.230*/231static void wake_offline_cpus(void)232{233int cpu = 0;234235kexec_smt_reenable();236237for_each_present_cpu(cpu) {238if (!cpu_online(cpu)) {239printk(KERN_INFO "kexec: Waking offline cpu %d.\n",240cpu);241WARN_ON(add_cpu(cpu));242}243}244}245246static void kexec_prepare_cpus(void)247{248wake_offline_cpus();249smp_call_function(kexec_smp_down, NULL, /* wait */0);250local_irq_disable();251hard_irq_disable();252253mb(); /* make sure IRQs are disabled before we say they are */254get_paca()->kexec_state = KEXEC_STATE_IRQS_OFF;255256kexec_prepare_cpus_wait(KEXEC_STATE_IRQS_OFF);257/* we are sure every CPU has IRQs off at this point */258kexec_all_irq_disabled = 1;259260/*261* Before removing MMU mappings make sure all CPUs have entered real262* mode:263*/264kexec_prepare_cpus_wait(KEXEC_STATE_REAL_MODE);265266/* after we tell the others to go down */267if (ppc_md.kexec_cpu_down)268ppc_md.kexec_cpu_down(0, 0);269270put_cpu();271}272273#else /* ! SMP */274275static void kexec_prepare_cpus(void)276{277/*278* move the secondarys to us so that we can copy279* the new kernel 0-0x100 safely280*281* do this if kexec in setup.c ?282*283* We need to release the cpus if we are ever going from an284* UP to an SMP kernel.285*/286smp_release_cpus();287if (ppc_md.kexec_cpu_down)288ppc_md.kexec_cpu_down(0, 0);289local_irq_disable();290hard_irq_disable();291}292293#endif /* SMP */294295/*296* kexec thread structure and stack.297*298* We need to make sure that this is 16384-byte aligned due to the299* way process stacks are handled. It also must be statically allocated300* or allocated as part of the kimage, because everything else may be301* overwritten when we copy the kexec image. We piggyback on the302* "init_task" linker section here to statically allocate a stack.303*304* We could use a smaller stack if we don't care about anything using305* current, but that audit has not been performed.306*/307static union thread_union kexec_stack = { };308309/*310* For similar reasons to the stack above, the kexecing CPU needs to be on a311* static PACA; we switch to kexec_paca.312*/313static struct paca_struct kexec_paca;314315/* Our assembly helper, in misc_64.S */316extern void kexec_sequence(void *newstack, unsigned long start,317void *image, void *control,318void (*clear_all)(void),319bool copy_with_mmu_off) __noreturn;320321/* too late to fail here */322void default_machine_kexec(struct kimage *image)323{324bool copy_with_mmu_off;325326/* prepare control code if any */327328/*329* If the kexec boot is the normal one, need to shutdown other cpus330* into our wait loop and quiesce interrupts.331* Otherwise, in the case of crashed mode (crashing_cpu >= 0),332* stopping other CPUs and collecting their pt_regs is done before333* using debugger IPI.334*/335336if (!kdump_in_progress())337kexec_prepare_cpus();338339#ifdef CONFIG_PPC_PSERIES340/*341* This must be done after other CPUs have shut down, otherwise they342* could execute the 'scv' instruction, which is not supported with343* reloc disabled (see configure_exceptions()).344*/345if (firmware_has_feature(FW_FEATURE_SET_MODE))346pseries_disable_reloc_on_exc();347#endif348349printk("kexec: Starting switchover sequence.\n");350351/* switch to a staticly allocated stack. Based on irq stack code.352* We setup preempt_count to avoid using VMX in memcpy.353* XXX: the task struct will likely be invalid once we do the copy!354*/355current_thread_info()->flags = 0;356current_thread_info()->preempt_count = HARDIRQ_OFFSET;357358/* We need a static PACA, too; copy this CPU's PACA over and switch to359* it. Also poison per_cpu_offset and NULL lppaca to catch anyone using360* non-static data.361*/362memcpy(&kexec_paca, get_paca(), sizeof(struct paca_struct));363kexec_paca.data_offset = 0xedeaddeadeeeeeeeUL;364#ifdef CONFIG_PPC_PSERIES365kexec_paca.lppaca_ptr = NULL;366#endif367368if (is_secure_guest() && !(image->preserve_context ||369image->type == KEXEC_TYPE_CRASH)) {370uv_unshare_all_pages();371printk("kexec: Unshared all shared pages.\n");372}373374paca_ptrs[kexec_paca.paca_index] = &kexec_paca;375376setup_paca(&kexec_paca);377378/*379* The lppaca should be unregistered at this point so the HV won't380* touch it. In the case of a crash, none of the lppacas are381* unregistered so there is not much we can do about it here.382*/383384/*385* On Book3S, the copy must happen with the MMU off if we are either386* using Radix page tables or we are not in an LPAR since we can387* overwrite the page tables while copying.388*389* In an LPAR, we keep the MMU on otherwise we can't access beyond390* the RMA. On BookE there is no real MMU off mode, so we have to391* keep it enabled as well (but then we have bolted TLB entries).392*/393#ifdef CONFIG_PPC_BOOK3E_64394copy_with_mmu_off = false;395#else396copy_with_mmu_off = radix_enabled() ||397!(firmware_has_feature(FW_FEATURE_LPAR) ||398firmware_has_feature(FW_FEATURE_PS3_LV1));399#endif400401/* Some things are best done in assembly. Finding globals with402* a toc is easier in C, so pass in what we can.403*/404kexec_sequence(&kexec_stack, image->start, image,405page_address(image->control_code_page),406mmu_cleanup_all, copy_with_mmu_off);407/* NOTREACHED */408}409410#ifdef CONFIG_PPC_64S_HASH_MMU411/* Values we need to export to the second kernel via the device tree. */412static __be64 htab_base;413static __be64 htab_size;414415static struct property htab_base_prop = {416.name = "linux,htab-base",417.length = sizeof(unsigned long),418.value = &htab_base,419};420421static struct property htab_size_prop = {422.name = "linux,htab-size",423.length = sizeof(unsigned long),424.value = &htab_size,425};426427static int __init export_htab_values(void)428{429struct device_node *node;430431/* On machines with no htab htab_address is NULL */432if (!htab_address)433return -ENODEV;434435node = of_find_node_by_path("/chosen");436if (!node)437return -ENODEV;438439/* remove any stale properties so ours can be found */440of_remove_property(node, of_find_property(node, htab_base_prop.name, NULL));441of_remove_property(node, of_find_property(node, htab_size_prop.name, NULL));442443htab_base = cpu_to_be64(__pa(htab_address));444of_add_property(node, &htab_base_prop);445htab_size = cpu_to_be64(htab_size_bytes);446of_add_property(node, &htab_size_prop);447448of_node_put(node);449return 0;450}451late_initcall(export_htab_values);452#endif /* CONFIG_PPC_64S_HASH_MMU */453454#if defined(CONFIG_KEXEC_FILE) || defined(CONFIG_CRASH_DUMP)455/**456* add_node_props - Reads node properties from device node structure and add457* them to fdt.458* @fdt: Flattened device tree of the kernel459* @node_offset: offset of the node to add a property at460* @dn: device node pointer461*462* Returns 0 on success, negative errno on error.463*/464static int add_node_props(void *fdt, int node_offset, const struct device_node *dn)465{466int ret = 0;467struct property *pp;468469if (!dn)470return -EINVAL;471472for_each_property_of_node(dn, pp) {473ret = fdt_setprop(fdt, node_offset, pp->name, pp->value, pp->length);474if (ret < 0) {475pr_err("Unable to add %s property: %s\n", pp->name, fdt_strerror(ret));476return ret;477}478}479return ret;480}481482/**483* update_cpus_node - Update cpus node of flattened device tree using of_root484* device node.485* @fdt: Flattened device tree of the kernel.486*487* Returns 0 on success, negative errno on error.488*489* Note: expecting no subnodes under /cpus/<node> with device_type == "cpu".490* If this changes, update this function to include them.491*/492int update_cpus_node(void *fdt)493{494int prev_node_offset;495const char *device_type;496const struct fdt_property *prop;497struct device_node *cpus_node, *dn;498int cpus_offset, cpus_subnode_offset, ret = 0;499500cpus_offset = fdt_path_offset(fdt, "/cpus");501if (cpus_offset < 0 && cpus_offset != -FDT_ERR_NOTFOUND) {502pr_err("Malformed device tree: error reading /cpus node: %s\n",503fdt_strerror(cpus_offset));504return cpus_offset;505}506507prev_node_offset = cpus_offset;508/* Delete sub-nodes of /cpus node with device_type == "cpu" */509for (cpus_subnode_offset = fdt_first_subnode(fdt, cpus_offset); cpus_subnode_offset >= 0;) {510/* Ignore nodes that do not have a device_type property or device_type != "cpu" */511prop = fdt_get_property(fdt, cpus_subnode_offset, "device_type", NULL);512if (!prop || strcmp(prop->data, "cpu")) {513prev_node_offset = cpus_subnode_offset;514goto next_node;515}516517ret = fdt_del_node(fdt, cpus_subnode_offset);518if (ret < 0) {519pr_err("Failed to delete a cpus sub-node: %s\n", fdt_strerror(ret));520return ret;521}522next_node:523if (prev_node_offset == cpus_offset)524cpus_subnode_offset = fdt_first_subnode(fdt, cpus_offset);525else526cpus_subnode_offset = fdt_next_subnode(fdt, prev_node_offset);527}528529cpus_node = of_find_node_by_path("/cpus");530/* Fail here to avoid kexec/kdump kernel boot hung */531if (!cpus_node) {532pr_err("No /cpus node found\n");533return -EINVAL;534}535536/* Add all /cpus sub-nodes of device_type == "cpu" to FDT */537for_each_child_of_node(cpus_node, dn) {538/* Ignore device nodes that do not have a device_type property539* or device_type != "cpu".540*/541device_type = of_get_property(dn, "device_type", NULL);542if (!device_type || strcmp(device_type, "cpu"))543continue;544545cpus_subnode_offset = fdt_add_subnode(fdt, cpus_offset, dn->full_name);546if (cpus_subnode_offset < 0) {547pr_err("Unable to add %s subnode: %s\n", dn->full_name,548fdt_strerror(cpus_subnode_offset));549ret = cpus_subnode_offset;550goto out;551}552553ret = add_node_props(fdt, cpus_subnode_offset, dn);554if (ret < 0)555goto out;556}557out:558of_node_put(cpus_node);559of_node_put(dn);560return ret;561}562#endif /* CONFIG_KEXEC_FILE || CONFIG_CRASH_DUMP */563564565