1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2007 Lemote, Inc. & Institute of Computing Technology
5 * Author: Fuxin Zhang, zhangfx@lemote.com
6 * Copyright (C) 2009 Lemote, Inc.
7 * Author: Zhangjin Wu, wuzhangjin@gmail.com
10 #include <linux/delay.h>
11 #include <linux/init.h>
12 #include <linux/kexec.h>
14 #include <linux/slab.h>
16 #include <asm/bootinfo.h>
18 #include <asm/reboot.h>
21 #include <boot_param.h>
23 static void loongson_restart(char *command)
26 void (*fw_restart)(void) = (void *)loongson_sysconf.restart_addr;
35 static void loongson_poweroff(void)
37 void (*fw_poweroff)(void) = (void *)loongson_sysconf.poweroff_addr;
46 static void loongson_halt(void)
48 pr_notice("\n\n** You can safely turn off the power now **\n\n");
57 /* 0X80000000~0X80200000 is safe */
59 #define KEXEC_CTRL_CODE 0xFFFFFFFF80100000UL
60 #define KEXEC_ARGV_ADDR 0xFFFFFFFF80108000UL
61 #define KEXEC_ARGV_SIZE COMMAND_LINE_SIZE
62 #define KEXEC_ENVP_SIZE 4800
64 static int kexec_argc;
65 static int kdump_argc;
66 static void *kexec_argv;
67 static void *kdump_argv;
68 static void *kexec_envp;
70 static int loongson_kexec_prepare(struct kimage *image)
74 char *str, *ptr, *bootloader = "kexec";
76 /* argv at offset 0, argv[] at offset KEXEC_ARGV_SIZE/2 */
77 if (image->type == KEXEC_TYPE_DEFAULT)
78 argv = (unsigned int *)kexec_argv;
80 argv = (unsigned int *)kdump_argv;
82 argv[argc++] = (unsigned int)(KEXEC_ARGV_ADDR + KEXEC_ARGV_SIZE/2);
84 for (i = 0; i < image->nr_segments; i++) {
85 if (!strncmp(bootloader, (char *)image->segment[i].buf,
86 strlen(bootloader))) {
88 * convert command line string to array
89 * of parameters (as bootloader does).
92 str = (char *)argv + KEXEC_ARGV_SIZE/2;
93 memcpy(str, image->segment[i].buf, KEXEC_ARGV_SIZE/2);
94 ptr = strchr(str, ' ');
96 while (ptr && (argc < MAX_ARGS)) {
99 offt = (int)(ptr - str + 1);
100 argv[argc] = KEXEC_ARGV_ADDR + KEXEC_ARGV_SIZE/2 + offt;
103 ptr = strchr(ptr + 1, ' ');
109 if (image->type == KEXEC_TYPE_DEFAULT)
114 /* kexec/kdump need a safe page to save reboot_code_buffer */
115 image->control_code_page = virt_to_page((void *)KEXEC_CTRL_CODE);
120 static void loongson_kexec_shutdown(void)
125 /* All CPUs go to reboot_code_buffer */
126 for_each_possible_cpu(cpu)
127 if (!cpu_online(cpu))
128 cpu_device_up(get_cpu_device(cpu));
130 secondary_kexec_args[0] = TO_UNCAC(0x3ff01000);
132 kexec_args[0] = kexec_argc;
133 kexec_args[1] = fw_arg1;
134 kexec_args[2] = fw_arg2;
135 memcpy((void *)fw_arg1, kexec_argv, KEXEC_ARGV_SIZE);
136 memcpy((void *)fw_arg2, kexec_envp, KEXEC_ENVP_SIZE);
139 static void loongson_crash_shutdown(struct pt_regs *regs)
141 default_machine_crash_shutdown(regs);
142 kexec_args[0] = kdump_argc;
143 kexec_args[1] = fw_arg1;
144 kexec_args[2] = fw_arg2;
146 secondary_kexec_args[0] = TO_UNCAC(0x3ff01000);
148 memcpy((void *)fw_arg1, kdump_argv, KEXEC_ARGV_SIZE);
149 memcpy((void *)fw_arg2, kexec_envp, KEXEC_ENVP_SIZE);
154 static int __init mips_reboot_setup(void)
156 _machine_restart = loongson_restart;
157 _machine_halt = loongson_halt;
158 pm_power_off = loongson_poweroff;
161 kexec_argv = kmalloc(KEXEC_ARGV_SIZE, GFP_KERNEL);
162 kdump_argv = kmalloc(KEXEC_ARGV_SIZE, GFP_KERNEL);
163 kexec_envp = kmalloc(KEXEC_ENVP_SIZE, GFP_KERNEL);
164 fw_arg1 = KEXEC_ARGV_ADDR;
165 memcpy(kexec_envp, (void *)fw_arg2, KEXEC_ENVP_SIZE);
167 _machine_kexec_prepare = loongson_kexec_prepare;
168 _machine_kexec_shutdown = loongson_kexec_shutdown;
169 _machine_crash_shutdown = loongson_crash_shutdown;
175 arch_initcall(mips_reboot_setup);