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 kexec_args[0] = kexec_argc;
131 kexec_args[1] = fw_arg1;
132 kexec_args[2] = fw_arg2;
133 secondary_kexec_args[0] = TO_UNCAC(0x3ff01000);
134 memcpy((void *)fw_arg1, kexec_argv, KEXEC_ARGV_SIZE);
135 memcpy((void *)fw_arg2, kexec_envp, KEXEC_ENVP_SIZE);
138 static void loongson_crash_shutdown(struct pt_regs *regs)
140 default_machine_crash_shutdown(regs);
141 kexec_args[0] = kdump_argc;
142 kexec_args[1] = fw_arg1;
143 kexec_args[2] = fw_arg2;
144 secondary_kexec_args[0] = TO_UNCAC(0x3ff01000);
145 memcpy((void *)fw_arg1, kdump_argv, KEXEC_ARGV_SIZE);
146 memcpy((void *)fw_arg2, kexec_envp, KEXEC_ENVP_SIZE);
151 static int __init mips_reboot_setup(void)
153 _machine_restart = loongson_restart;
154 _machine_halt = loongson_halt;
155 pm_power_off = loongson_poweroff;
158 kexec_argv = kmalloc(KEXEC_ARGV_SIZE, GFP_KERNEL);
159 kdump_argv = kmalloc(KEXEC_ARGV_SIZE, GFP_KERNEL);
160 kexec_envp = kmalloc(KEXEC_ENVP_SIZE, GFP_KERNEL);
161 fw_arg1 = KEXEC_ARGV_ADDR;
162 memcpy(kexec_envp, (void *)fw_arg2, KEXEC_ENVP_SIZE);
164 _machine_kexec_prepare = loongson_kexec_prepare;
165 _machine_kexec_shutdown = loongson_kexec_shutdown;
166 _machine_crash_shutdown = loongson_crash_shutdown;
172 arch_initcall(mips_reboot_setup);