Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Wu Zhangjin | 85749d2 | 2009-07-02 23:26:45 +0800 | [diff] [blame] | 2 | /* |
Wu Zhangjin | 85749d2 | 2009-07-02 23:26:45 +0800 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2007 Lemote, Inc. & Institute of Computing Technology |
| 5 | * Author: Fuxin Zhang, zhangfx@lemote.com |
Wu Zhangjin | 6f32096 | 2010-01-04 17:16:45 +0800 | [diff] [blame] | 6 | * Copyright (C) 2009 Lemote, Inc. |
Wu Zhangjin | f7a904d | 2010-01-04 17:16:51 +0800 | [diff] [blame] | 7 | * Author: Zhangjin Wu, wuzhangjin@gmail.com |
Wu Zhangjin | 85749d2 | 2009-07-02 23:26:45 +0800 | [diff] [blame] | 8 | */ |
Huacai Chen | 6ce4889 | 2021-04-13 16:57:23 +0800 | [diff] [blame] | 9 | #include <linux/cpu.h> |
| 10 | #include <linux/delay.h> |
Wu Zhangjin | 85749d2 | 2009-07-02 23:26:45 +0800 | [diff] [blame] | 11 | #include <linux/init.h> |
Huacai Chen | 6ce4889 | 2021-04-13 16:57:23 +0800 | [diff] [blame] | 12 | #include <linux/kexec.h> |
Wu Zhangjin | 85749d2 | 2009-07-02 23:26:45 +0800 | [diff] [blame] | 13 | #include <linux/pm.h> |
Huacai Chen | 6ce4889 | 2021-04-13 16:57:23 +0800 | [diff] [blame] | 14 | #include <linux/slab.h> |
Wu Zhangjin | 85749d2 | 2009-07-02 23:26:45 +0800 | [diff] [blame] | 15 | |
Huacai Chen | 6ce4889 | 2021-04-13 16:57:23 +0800 | [diff] [blame] | 16 | #include <asm/bootinfo.h> |
Ralf Baechle | bdc92d74 | 2013-05-21 16:59:19 +0200 | [diff] [blame] | 17 | #include <asm/idle.h> |
Wu Zhangjin | 85749d2 | 2009-07-02 23:26:45 +0800 | [diff] [blame] | 18 | #include <asm/reboot.h> |
| 19 | |
| 20 | #include <loongson.h> |
Huacai Chen | 1a08f15 | 2014-03-21 18:44:02 +0800 | [diff] [blame] | 21 | #include <boot_param.h> |
Wu Zhangjin | 85749d2 | 2009-07-02 23:26:45 +0800 | [diff] [blame] | 22 | |
| 23 | static void loongson_restart(char *command) |
| 24 | { |
Wu Zhangjin | 85749d2 | 2009-07-02 23:26:45 +0800 | [diff] [blame] | 25 | |
Huacai Chen | 1a08f15 | 2014-03-21 18:44:02 +0800 | [diff] [blame] | 26 | void (*fw_restart)(void) = (void *)loongson_sysconf.restart_addr; |
| 27 | |
| 28 | fw_restart(); |
| 29 | while (1) { |
| 30 | if (cpu_wait) |
| 31 | cpu_wait(); |
| 32 | } |
Wu Zhangjin | 85749d2 | 2009-07-02 23:26:45 +0800 | [diff] [blame] | 33 | } |
| 34 | |
Wu Zhangjin | fc48c41 | 2010-01-27 22:39:46 +0800 | [diff] [blame] | 35 | static void loongson_poweroff(void) |
Wu Zhangjin | 85749d2 | 2009-07-02 23:26:45 +0800 | [diff] [blame] | 36 | { |
Huacai Chen | 1a08f15 | 2014-03-21 18:44:02 +0800 | [diff] [blame] | 37 | void (*fw_poweroff)(void) = (void *)loongson_sysconf.poweroff_addr; |
| 38 | |
| 39 | fw_poweroff(); |
| 40 | while (1) { |
| 41 | if (cpu_wait) |
| 42 | cpu_wait(); |
| 43 | } |
Wu Zhangjin | 85749d2 | 2009-07-02 23:26:45 +0800 | [diff] [blame] | 44 | } |
| 45 | |
Wu Zhangjin | fc48c41 | 2010-01-27 22:39:46 +0800 | [diff] [blame] | 46 | static void loongson_halt(void) |
| 47 | { |
| 48 | pr_notice("\n\n** You can safely turn off the power now **\n\n"); |
| 49 | while (1) { |
| 50 | if (cpu_wait) |
| 51 | cpu_wait(); |
| 52 | } |
| 53 | } |
| 54 | |
Huacai Chen | 6ce4889 | 2021-04-13 16:57:23 +0800 | [diff] [blame] | 55 | #ifdef CONFIG_KEXEC |
| 56 | |
| 57 | /* 0X80000000~0X80200000 is safe */ |
| 58 | #define MAX_ARGS 64 |
| 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 |
| 63 | |
| 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; |
| 69 | |
| 70 | static int loongson_kexec_prepare(struct kimage *image) |
| 71 | { |
| 72 | int i, argc = 0; |
| 73 | unsigned int *argv; |
| 74 | char *str, *ptr, *bootloader = "kexec"; |
| 75 | |
| 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; |
| 79 | else |
| 80 | argv = (unsigned int *)kdump_argv; |
| 81 | |
| 82 | argv[argc++] = (unsigned int)(KEXEC_ARGV_ADDR + KEXEC_ARGV_SIZE/2); |
| 83 | |
| 84 | for (i = 0; i < image->nr_segments; i++) { |
| 85 | if (!strncmp(bootloader, (char *)image->segment[i].buf, |
| 86 | strlen(bootloader))) { |
| 87 | /* |
| 88 | * convert command line string to array |
| 89 | * of parameters (as bootloader does). |
| 90 | */ |
| 91 | int offt; |
| 92 | str = (char *)argv + KEXEC_ARGV_SIZE/2; |
| 93 | memcpy(str, image->segment[i].buf, KEXEC_ARGV_SIZE/2); |
| 94 | ptr = strchr(str, ' '); |
| 95 | |
| 96 | while (ptr && (argc < MAX_ARGS)) { |
| 97 | *ptr = '\0'; |
| 98 | if (ptr[1] != ' ') { |
| 99 | offt = (int)(ptr - str + 1); |
| 100 | argv[argc] = KEXEC_ARGV_ADDR + KEXEC_ARGV_SIZE/2 + offt; |
| 101 | argc++; |
| 102 | } |
| 103 | ptr = strchr(ptr + 1, ' '); |
| 104 | } |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if (image->type == KEXEC_TYPE_DEFAULT) |
| 110 | kexec_argc = argc; |
| 111 | else |
| 112 | kdump_argc = argc; |
| 113 | |
| 114 | /* kexec/kdump need a safe page to save reboot_code_buffer */ |
| 115 | image->control_code_page = virt_to_page((void *)KEXEC_CTRL_CODE); |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static void loongson_kexec_shutdown(void) |
| 121 | { |
| 122 | #ifdef CONFIG_SMP |
| 123 | int cpu; |
| 124 | |
| 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)); |
Youling Tang | 6a73022 | 2021-05-06 10:02:50 +0800 | [diff] [blame] | 129 | |
| 130 | secondary_kexec_args[0] = TO_UNCAC(0x3ff01000); |
Huacai Chen | 6ce4889 | 2021-04-13 16:57:23 +0800 | [diff] [blame] | 131 | #endif |
| 132 | kexec_args[0] = kexec_argc; |
| 133 | kexec_args[1] = fw_arg1; |
| 134 | kexec_args[2] = fw_arg2; |
Huacai Chen | 6ce4889 | 2021-04-13 16:57:23 +0800 | [diff] [blame] | 135 | memcpy((void *)fw_arg1, kexec_argv, KEXEC_ARGV_SIZE); |
| 136 | memcpy((void *)fw_arg2, kexec_envp, KEXEC_ENVP_SIZE); |
| 137 | } |
| 138 | |
| 139 | static void loongson_crash_shutdown(struct pt_regs *regs) |
| 140 | { |
| 141 | default_machine_crash_shutdown(regs); |
| 142 | kexec_args[0] = kdump_argc; |
| 143 | kexec_args[1] = fw_arg1; |
| 144 | kexec_args[2] = fw_arg2; |
Youling Tang | 6a73022 | 2021-05-06 10:02:50 +0800 | [diff] [blame] | 145 | #ifdef CONFIG_SMP |
Huacai Chen | 6ce4889 | 2021-04-13 16:57:23 +0800 | [diff] [blame] | 146 | secondary_kexec_args[0] = TO_UNCAC(0x3ff01000); |
Youling Tang | 6a73022 | 2021-05-06 10:02:50 +0800 | [diff] [blame] | 147 | #endif |
Huacai Chen | 6ce4889 | 2021-04-13 16:57:23 +0800 | [diff] [blame] | 148 | memcpy((void *)fw_arg1, kdump_argv, KEXEC_ARGV_SIZE); |
| 149 | memcpy((void *)fw_arg2, kexec_envp, KEXEC_ENVP_SIZE); |
| 150 | } |
| 151 | |
| 152 | #endif |
| 153 | |
Wu Zhangjin | 85749d2 | 2009-07-02 23:26:45 +0800 | [diff] [blame] | 154 | static int __init mips_reboot_setup(void) |
| 155 | { |
| 156 | _machine_restart = loongson_restart; |
| 157 | _machine_halt = loongson_halt; |
Wu Zhangjin | fc48c41 | 2010-01-27 22:39:46 +0800 | [diff] [blame] | 158 | pm_power_off = loongson_poweroff; |
Wu Zhangjin | 85749d2 | 2009-07-02 23:26:45 +0800 | [diff] [blame] | 159 | |
Huacai Chen | 6ce4889 | 2021-04-13 16:57:23 +0800 | [diff] [blame] | 160 | #ifdef CONFIG_KEXEC |
| 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); |
| 166 | |
| 167 | _machine_kexec_prepare = loongson_kexec_prepare; |
| 168 | _machine_kexec_shutdown = loongson_kexec_shutdown; |
| 169 | _machine_crash_shutdown = loongson_crash_shutdown; |
| 170 | #endif |
| 171 | |
Wu Zhangjin | 85749d2 | 2009-07-02 23:26:45 +0800 | [diff] [blame] | 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | arch_initcall(mips_reboot_setup); |