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>
22 #include <boot_param.h>
24 static void loongson_restart(char *command)
27 void (*fw_restart)(void) = (void *)loongson_sysconf.restart_addr;
36 static void loongson_poweroff(void)
38 void (*fw_poweroff)(void) = (void *)loongson_sysconf.poweroff_addr;
47 static void loongson_halt(void)
49 pr_notice("\n\n** You can safely turn off the power now **\n\n");
58 /* 0X80000000~0X80200000 is safe */
60 #define KEXEC_CTRL_CODE 0xFFFFFFFF80100000UL
61 #define KEXEC_ARGV_ADDR 0xFFFFFFFF80108000UL
62 #define KEXEC_ARGV_SIZE COMMAND_LINE_SIZE
63 #define KEXEC_ENVP_SIZE 4800
65 static int kexec_argc;
66 static int kdump_argc;
67 static void *kexec_argv;
68 static void *kdump_argv;
69 static void *kexec_envp;
71 static int loongson_kexec_prepare(struct kimage *image)
75 char *str, *ptr, *bootloader = "kexec";
77 /* argv at offset 0, argv[] at offset KEXEC_ARGV_SIZE/2 */
78 if (image->type == KEXEC_TYPE_DEFAULT)
79 argv = (unsigned int *)kexec_argv;
81 argv = (unsigned int *)kdump_argv;
83 argv[argc++] = (unsigned int)(KEXEC_ARGV_ADDR + KEXEC_ARGV_SIZE/2);
85 for (i = 0; i < image->nr_segments; i++) {
86 if (!strncmp(bootloader, (char *)image->segment[i].buf,
87 strlen(bootloader))) {
89 * convert command line string to array
90 * of parameters (as bootloader does).
93 str = (char *)argv + KEXEC_ARGV_SIZE/2;
94 memcpy(str, image->segment[i].buf, KEXEC_ARGV_SIZE/2);
95 ptr = strchr(str, ' ');
97 while (ptr && (argc < MAX_ARGS)) {
100 offt = (int)(ptr - str + 1);
101 argv[argc] = KEXEC_ARGV_ADDR + KEXEC_ARGV_SIZE/2 + offt;
104 ptr = strchr(ptr + 1, ' ');
110 if (image->type == KEXEC_TYPE_DEFAULT)
115 /* kexec/kdump need a safe page to save reboot_code_buffer */
116 image->control_code_page = virt_to_page((void *)KEXEC_CTRL_CODE);
121 static void loongson_kexec_shutdown(void)
126 /* All CPUs go to reboot_code_buffer */
127 for_each_possible_cpu(cpu)
128 if (!cpu_online(cpu))
129 cpu_device_up(get_cpu_device(cpu));
131 secondary_kexec_args[0] = TO_UNCAC(0x3ff01000);
133 kexec_args[0] = kexec_argc;
134 kexec_args[1] = fw_arg1;
135 kexec_args[2] = fw_arg2;
136 memcpy((void *)fw_arg1, kexec_argv, KEXEC_ARGV_SIZE);
137 memcpy((void *)fw_arg2, kexec_envp, KEXEC_ENVP_SIZE);
140 static void loongson_crash_shutdown(struct pt_regs *regs)
142 default_machine_crash_shutdown(regs);
143 kexec_args[0] = kdump_argc;
144 kexec_args[1] = fw_arg1;
145 kexec_args[2] = fw_arg2;
147 secondary_kexec_args[0] = TO_UNCAC(0x3ff01000);
149 memcpy((void *)fw_arg1, kdump_argv, KEXEC_ARGV_SIZE);
150 memcpy((void *)fw_arg2, kexec_envp, KEXEC_ENVP_SIZE);
155 static int __init mips_reboot_setup(void)
157 _machine_restart = loongson_restart;
158 _machine_halt = loongson_halt;
159 pm_power_off = loongson_poweroff;
162 kexec_argv = kmalloc(KEXEC_ARGV_SIZE, GFP_KERNEL);
163 if (WARN_ON(!kexec_argv))
166 kdump_argv = kmalloc(KEXEC_ARGV_SIZE, GFP_KERNEL);
167 if (WARN_ON(!kdump_argv))
170 kexec_envp = kmalloc(KEXEC_ENVP_SIZE, GFP_KERNEL);
171 if (WARN_ON(!kexec_envp))
174 fw_arg1 = KEXEC_ARGV_ADDR;
175 memcpy(kexec_envp, (void *)fw_arg2, KEXEC_ENVP_SIZE);
177 _machine_kexec_prepare = loongson_kexec_prepare;
178 _machine_kexec_shutdown = loongson_kexec_shutdown;
179 _machine_crash_shutdown = loongson_crash_shutdown;
185 arch_initcall(mips_reboot_setup);