blob: 758d5d26aaaa24e46e7bac7d7e040f2fb4c34240 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Wu Zhangjin85749d22009-07-02 23:26:45 +08002/*
Wu Zhangjin85749d22009-07-02 23:26:45 +08003 *
4 * Copyright (C) 2007 Lemote, Inc. & Institute of Computing Technology
5 * Author: Fuxin Zhang, zhangfx@lemote.com
Wu Zhangjin6f320962010-01-04 17:16:45 +08006 * Copyright (C) 2009 Lemote, Inc.
Wu Zhangjinf7a904d2010-01-04 17:16:51 +08007 * Author: Zhangjin Wu, wuzhangjin@gmail.com
Wu Zhangjin85749d22009-07-02 23:26:45 +08008 */
Huacai Chen6ce48892021-04-13 16:57:23 +08009#include <linux/cpu.h>
10#include <linux/delay.h>
Wu Zhangjin85749d22009-07-02 23:26:45 +080011#include <linux/init.h>
Huacai Chen6ce48892021-04-13 16:57:23 +080012#include <linux/kexec.h>
Wu Zhangjin85749d22009-07-02 23:26:45 +080013#include <linux/pm.h>
Huacai Chen6ce48892021-04-13 16:57:23 +080014#include <linux/slab.h>
Wu Zhangjin85749d22009-07-02 23:26:45 +080015
Huacai Chen6ce48892021-04-13 16:57:23 +080016#include <asm/bootinfo.h>
Ralf Baechlebdc92d742013-05-21 16:59:19 +020017#include <asm/idle.h>
Wu Zhangjin85749d22009-07-02 23:26:45 +080018#include <asm/reboot.h>
19
20#include <loongson.h>
Huacai Chen1a08f152014-03-21 18:44:02 +080021#include <boot_param.h>
Wu Zhangjin85749d22009-07-02 23:26:45 +080022
23static void loongson_restart(char *command)
24{
Wu Zhangjin85749d22009-07-02 23:26:45 +080025
Huacai Chen1a08f152014-03-21 18:44:02 +080026 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 Zhangjin85749d22009-07-02 23:26:45 +080033}
34
Wu Zhangjinfc48c412010-01-27 22:39:46 +080035static void loongson_poweroff(void)
Wu Zhangjin85749d22009-07-02 23:26:45 +080036{
Huacai Chen1a08f152014-03-21 18:44:02 +080037 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 Zhangjin85749d22009-07-02 23:26:45 +080044}
45
Wu Zhangjinfc48c412010-01-27 22:39:46 +080046static 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 Chen6ce48892021-04-13 16:57:23 +080055#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
64static int kexec_argc;
65static int kdump_argc;
66static void *kexec_argv;
67static void *kdump_argv;
68static void *kexec_envp;
69
70static 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
120static 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 Tang6a730222021-05-06 10:02:50 +0800129
130 secondary_kexec_args[0] = TO_UNCAC(0x3ff01000);
Huacai Chen6ce48892021-04-13 16:57:23 +0800131#endif
132 kexec_args[0] = kexec_argc;
133 kexec_args[1] = fw_arg1;
134 kexec_args[2] = fw_arg2;
Huacai Chen6ce48892021-04-13 16:57:23 +0800135 memcpy((void *)fw_arg1, kexec_argv, KEXEC_ARGV_SIZE);
136 memcpy((void *)fw_arg2, kexec_envp, KEXEC_ENVP_SIZE);
137}
138
139static 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 Tang6a730222021-05-06 10:02:50 +0800145#ifdef CONFIG_SMP
Huacai Chen6ce48892021-04-13 16:57:23 +0800146 secondary_kexec_args[0] = TO_UNCAC(0x3ff01000);
Youling Tang6a730222021-05-06 10:02:50 +0800147#endif
Huacai Chen6ce48892021-04-13 16:57:23 +0800148 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 Zhangjin85749d22009-07-02 23:26:45 +0800154static int __init mips_reboot_setup(void)
155{
156 _machine_restart = loongson_restart;
157 _machine_halt = loongson_halt;
Wu Zhangjinfc48c412010-01-27 22:39:46 +0800158 pm_power_off = loongson_poweroff;
Wu Zhangjin85749d22009-07-02 23:26:45 +0800159
Huacai Chen6ce48892021-04-13 16:57:23 +0800160#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 Zhangjin85749d22009-07-02 23:26:45 +0800172 return 0;
173}
174
175arch_initcall(mips_reboot_setup);