Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *
0004  * Copyright (C) 2007 Lemote, Inc. & Institute of Computing Technology
0005  * Author: Fuxin Zhang, zhangfx@lemote.com
0006  * Copyright (C) 2009 Lemote, Inc.
0007  * Author: Zhangjin Wu, wuzhangjin@gmail.com
0008  */
0009 #include <linux/cpu.h>
0010 #include <linux/delay.h>
0011 #include <linux/init.h>
0012 #include <linux/kexec.h>
0013 #include <linux/pm.h>
0014 #include <linux/slab.h>
0015 
0016 #include <asm/bootinfo.h>
0017 #include <asm/idle.h>
0018 #include <asm/reboot.h>
0019 
0020 #include <loongson.h>
0021 #include <boot_param.h>
0022 
0023 static void loongson_restart(char *command)
0024 {
0025 
0026     void (*fw_restart)(void) = (void *)loongson_sysconf.restart_addr;
0027 
0028     fw_restart();
0029     while (1) {
0030         if (cpu_wait)
0031             cpu_wait();
0032     }
0033 }
0034 
0035 static void loongson_poweroff(void)
0036 {
0037     void (*fw_poweroff)(void) = (void *)loongson_sysconf.poweroff_addr;
0038 
0039     fw_poweroff();
0040     while (1) {
0041         if (cpu_wait)
0042             cpu_wait();
0043     }
0044 }
0045 
0046 static void loongson_halt(void)
0047 {
0048     pr_notice("\n\n** You can safely turn off the power now **\n\n");
0049     while (1) {
0050         if (cpu_wait)
0051             cpu_wait();
0052     }
0053 }
0054 
0055 #ifdef CONFIG_KEXEC
0056 
0057 /* 0X80000000~0X80200000 is safe */
0058 #define MAX_ARGS    64
0059 #define KEXEC_CTRL_CODE 0xFFFFFFFF80100000UL
0060 #define KEXEC_ARGV_ADDR 0xFFFFFFFF80108000UL
0061 #define KEXEC_ARGV_SIZE COMMAND_LINE_SIZE
0062 #define KEXEC_ENVP_SIZE 4800
0063 
0064 static int kexec_argc;
0065 static int kdump_argc;
0066 static void *kexec_argv;
0067 static void *kdump_argv;
0068 static void *kexec_envp;
0069 
0070 static int loongson_kexec_prepare(struct kimage *image)
0071 {
0072     int i, argc = 0;
0073     unsigned int *argv;
0074     char *str, *ptr, *bootloader = "kexec";
0075 
0076     /* argv at offset 0, argv[] at offset KEXEC_ARGV_SIZE/2 */
0077     if (image->type == KEXEC_TYPE_DEFAULT)
0078         argv = (unsigned int *)kexec_argv;
0079     else
0080         argv = (unsigned int *)kdump_argv;
0081 
0082     argv[argc++] = (unsigned int)(KEXEC_ARGV_ADDR + KEXEC_ARGV_SIZE/2);
0083 
0084     for (i = 0; i < image->nr_segments; i++) {
0085         if (!strncmp(bootloader, (char *)image->segment[i].buf,
0086                 strlen(bootloader))) {
0087             /*
0088              * convert command line string to array
0089              * of parameters (as bootloader does).
0090              */
0091             int offt;
0092             str = (char *)argv + KEXEC_ARGV_SIZE/2;
0093             memcpy(str, image->segment[i].buf, KEXEC_ARGV_SIZE/2);
0094             ptr = strchr(str, ' ');
0095 
0096             while (ptr && (argc < MAX_ARGS)) {
0097                 *ptr = '\0';
0098                 if (ptr[1] != ' ') {
0099                     offt = (int)(ptr - str + 1);
0100                     argv[argc] = KEXEC_ARGV_ADDR + KEXEC_ARGV_SIZE/2 + offt;
0101                     argc++;
0102                 }
0103                 ptr = strchr(ptr + 1, ' ');
0104             }
0105             break;
0106         }
0107     }
0108 
0109     if (image->type == KEXEC_TYPE_DEFAULT)
0110         kexec_argc = argc;
0111     else
0112         kdump_argc = argc;
0113 
0114     /* kexec/kdump need a safe page to save reboot_code_buffer */
0115     image->control_code_page = virt_to_page((void *)KEXEC_CTRL_CODE);
0116 
0117     return 0;
0118 }
0119 
0120 static void loongson_kexec_shutdown(void)
0121 {
0122 #ifdef CONFIG_SMP
0123     int cpu;
0124 
0125     /* All CPUs go to reboot_code_buffer */
0126     for_each_possible_cpu(cpu)
0127         if (!cpu_online(cpu))
0128             cpu_device_up(get_cpu_device(cpu));
0129 
0130     secondary_kexec_args[0] = TO_UNCAC(0x3ff01000);
0131 #endif
0132     kexec_args[0] = kexec_argc;
0133     kexec_args[1] = fw_arg1;
0134     kexec_args[2] = fw_arg2;
0135     memcpy((void *)fw_arg1, kexec_argv, KEXEC_ARGV_SIZE);
0136     memcpy((void *)fw_arg2, kexec_envp, KEXEC_ENVP_SIZE);
0137 }
0138 
0139 static void loongson_crash_shutdown(struct pt_regs *regs)
0140 {
0141     default_machine_crash_shutdown(regs);
0142     kexec_args[0] = kdump_argc;
0143     kexec_args[1] = fw_arg1;
0144     kexec_args[2] = fw_arg2;
0145 #ifdef CONFIG_SMP
0146     secondary_kexec_args[0] = TO_UNCAC(0x3ff01000);
0147 #endif
0148     memcpy((void *)fw_arg1, kdump_argv, KEXEC_ARGV_SIZE);
0149     memcpy((void *)fw_arg2, kexec_envp, KEXEC_ENVP_SIZE);
0150 }
0151 
0152 #endif
0153 
0154 static int __init mips_reboot_setup(void)
0155 {
0156     _machine_restart = loongson_restart;
0157     _machine_halt = loongson_halt;
0158     pm_power_off = loongson_poweroff;
0159 
0160 #ifdef CONFIG_KEXEC
0161     kexec_argv = kmalloc(KEXEC_ARGV_SIZE, GFP_KERNEL);
0162     kdump_argv = kmalloc(KEXEC_ARGV_SIZE, GFP_KERNEL);
0163     kexec_envp = kmalloc(KEXEC_ENVP_SIZE, GFP_KERNEL);
0164     fw_arg1 = KEXEC_ARGV_ADDR;
0165     memcpy(kexec_envp, (void *)fw_arg2, KEXEC_ENVP_SIZE);
0166 
0167     _machine_kexec_prepare = loongson_kexec_prepare;
0168     _machine_kexec_shutdown = loongson_kexec_shutdown;
0169     _machine_crash_shutdown = loongson_crash_shutdown;
0170 #endif
0171 
0172     return 0;
0173 }
0174 
0175 arch_initcall(mips_reboot_setup);