1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * 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 8 */ 9 #include <linux/cpu.h> 10 #include <linux/delay.h> 11 #include <linux/init.h> 12 #include <linux/kexec.h> 13 #include <linux/pm.h> 14 #include <linux/reboot.h> 15 #include <linux/slab.h> 16 17 #include <asm/bootinfo.h> 18 #include <asm/idle.h> 19 #include <asm/reboot.h> 20 #include <asm/bug.h> 21 22 #include <loongson.h> 23 #include <boot_param.h> 24 25 static int firmware_restart(struct sys_off_data *unusedd) 26 { 27 28 void (*fw_restart)(void) = (void *)loongson_sysconf.restart_addr; 29 30 fw_restart(); 31 return NOTIFY_DONE; 32 } 33 34 static int firmware_poweroff(struct sys_off_data *unused) 35 { 36 void (*fw_poweroff)(void) = (void *)loongson_sysconf.poweroff_addr; 37 38 fw_poweroff(); 39 return NOTIFY_DONE; 40 } 41 42 #ifdef CONFIG_KEXEC_CORE 43 44 /* 0X80000000~0X80200000 is safe */ 45 #define MAX_ARGS 64 46 #define KEXEC_CTRL_CODE 0xFFFFFFFF80100000UL 47 #define KEXEC_ARGV_ADDR 0xFFFFFFFF80108000UL 48 #define KEXEC_ARGV_SIZE COMMAND_LINE_SIZE 49 #define KEXEC_ENVP_SIZE 4800 50 51 static int kexec_argc; 52 static int kdump_argc; 53 static void *kexec_argv; 54 static void *kdump_argv; 55 static void *kexec_envp; 56 57 static int loongson_kexec_prepare(struct kimage *image) 58 { 59 int i, argc = 0; 60 unsigned int *argv; 61 char *str, *ptr, *bootloader = "kexec"; 62 63 /* argv at offset 0, argv[] at offset KEXEC_ARGV_SIZE/2 */ 64 if (image->type == KEXEC_TYPE_DEFAULT) 65 argv = (unsigned int *)kexec_argv; 66 else 67 argv = (unsigned int *)kdump_argv; 68 69 argv[argc++] = (unsigned int)(KEXEC_ARGV_ADDR + KEXEC_ARGV_SIZE/2); 70 71 for (i = 0; i < image->nr_segments; i++) { 72 if (!strncmp(bootloader, (char *)image->segment[i].buf, 73 strlen(bootloader))) { 74 /* 75 * convert command line string to array 76 * of parameters (as bootloader does). 77 */ 78 int offt; 79 str = (char *)argv + KEXEC_ARGV_SIZE/2; 80 memcpy(str, image->segment[i].buf, KEXEC_ARGV_SIZE/2); 81 ptr = strchr(str, ' '); 82 83 while (ptr && (argc < MAX_ARGS)) { 84 *ptr = '\0'; 85 if (ptr[1] != ' ') { 86 offt = (int)(ptr - str + 1); 87 argv[argc] = KEXEC_ARGV_ADDR + KEXEC_ARGV_SIZE/2 + offt; 88 argc++; 89 } 90 ptr = strchr(ptr + 1, ' '); 91 } 92 break; 93 } 94 } 95 96 if (image->type == KEXEC_TYPE_DEFAULT) 97 kexec_argc = argc; 98 else 99 kdump_argc = argc; 100 101 /* kexec/kdump need a safe page to save reboot_code_buffer */ 102 image->control_code_page = virt_to_page((void *)KEXEC_CTRL_CODE); 103 104 return 0; 105 } 106 107 static void loongson_kexec_shutdown(void) 108 { 109 #ifdef CONFIG_SMP 110 int cpu; 111 112 /* All CPUs go to reboot_code_buffer */ 113 for_each_possible_cpu(cpu) 114 if (!cpu_online(cpu)) 115 cpu_device_up(get_cpu_device(cpu)); 116 117 secondary_kexec_args[0] = TO_UNCAC(0x3ff01000); 118 #endif 119 kexec_args[0] = kexec_argc; 120 kexec_args[1] = fw_arg1; 121 kexec_args[2] = fw_arg2; 122 memcpy((void *)fw_arg1, kexec_argv, KEXEC_ARGV_SIZE); 123 memcpy((void *)fw_arg2, kexec_envp, KEXEC_ENVP_SIZE); 124 } 125 126 static void loongson_crash_shutdown(struct pt_regs *regs) 127 { 128 default_machine_crash_shutdown(regs); 129 kexec_args[0] = kdump_argc; 130 kexec_args[1] = fw_arg1; 131 kexec_args[2] = fw_arg2; 132 #ifdef CONFIG_SMP 133 secondary_kexec_args[0] = TO_UNCAC(0x3ff01000); 134 #endif 135 memcpy((void *)fw_arg1, kdump_argv, KEXEC_ARGV_SIZE); 136 memcpy((void *)fw_arg2, kexec_envp, KEXEC_ENVP_SIZE); 137 } 138 139 #endif 140 141 static int __init mips_reboot_setup(void) 142 { 143 if (loongson_sysconf.restart_addr) { 144 register_sys_off_handler(SYS_OFF_MODE_RESTART, 145 SYS_OFF_PRIO_FIRMWARE, 146 firmware_restart, NULL); 147 } 148 149 if (loongson_sysconf.poweroff_addr) { 150 register_sys_off_handler(SYS_OFF_MODE_POWER_OFF, 151 SYS_OFF_PRIO_FIRMWARE, 152 firmware_poweroff, NULL); 153 } 154 155 #ifdef CONFIG_KEXEC_CORE 156 kexec_argv = kmalloc(KEXEC_ARGV_SIZE, GFP_KERNEL); 157 if (WARN_ON(!kexec_argv)) 158 return -ENOMEM; 159 160 kdump_argv = kmalloc(KEXEC_ARGV_SIZE, GFP_KERNEL); 161 if (WARN_ON(!kdump_argv)) 162 return -ENOMEM; 163 164 kexec_envp = kmalloc(KEXEC_ENVP_SIZE, GFP_KERNEL); 165 if (WARN_ON(!kexec_envp)) 166 return -ENOMEM; 167 168 fw_arg1 = KEXEC_ARGV_ADDR; 169 memcpy(kexec_envp, (void *)fw_arg2, KEXEC_ENVP_SIZE); 170 171 _machine_kexec_prepare = loongson_kexec_prepare; 172 _machine_kexec_shutdown = loongson_kexec_shutdown; 173 _machine_crash_shutdown = loongson_crash_shutdown; 174 #endif 175 176 return 0; 177 } 178 179 arch_initcall(mips_reboot_setup); 180