Home | History | Annotate | Line # | Download | only in aarch64
      1 /*	$NetBSD: aarch64_reboot.c,v 1.5 2024/03/05 14:15:28 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2002, 2003, 2005  Genetec Corporation.  All rights reserved.
      5  * Written by Hiroyuki Bessho for Genetec Corporation.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of Genetec Corporation may not be used to endorse or
     16  *    promote products derived from this software without specific prior
     17  *    written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  *
     31  * Copyright (c) 2001 Wasabi Systems, Inc.
     32  * All rights reserved.
     33  *
     34  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgement:
     46  *	This product includes software developed for the NetBSD Project by
     47  *	Wasabi Systems, Inc.
     48  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     49  *    or promote products derived from this software without specific prior
     50  *    written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     54  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     55  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     56  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     57  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     58  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     59  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     60  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     61  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     62  * POSSIBILITY OF SUCH DAMAGE.
     63  *
     64  * Copyright (c) 1997,1998 Mark Brinicombe.
     65  * Copyright (c) 1997,1998 Causality Limited.
     66  * All rights reserved.
     67  *
     68  * Redistribution and use in source and binary forms, with or without
     69  * modification, are permitted provided that the following conditions
     70  * are met:
     71  * 1. Redistributions of source code must retain the above copyright
     72  *    notice, this list of conditions and the following disclaimer.
     73  * 2. Redistributions in binary form must reproduce the above copyright
     74  *    notice, this list of conditions and the following disclaimer in the
     75  *    documentation and/or other materials provided with the distribution.
     76  * 3. All advertising materials mentioning features or use of this software
     77  *    must display the following acknowledgement:
     78  *	This product includes software developed by Mark Brinicombe
     79  *	for the NetBSD Project.
     80  * 4. The name of the company nor the name of the author may be used to
     81  *    endorse or promote products derived from this software without specific
     82  *    prior written permission.
     83  *
     84  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     85  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     86  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     87  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     88  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     89  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     90  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     91  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     92  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     93  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     94  * SUCH DAMAGE.
     95  *
     96  * Copyright (c) 2007 Microsoft
     97  * All rights reserved.
     98  *
     99  * Redistribution and use in source and binary forms, with or without
    100  * modification, are permitted provided that the following conditions
    101  * are met:
    102  * 1. Redistributions of source code must retain the above copyright
    103  *    notice, this list of conditions and the following disclaimer.
    104  * 2. Redistributions in binary form must reproduce the above copyright
    105  *    notice, this list of conditions and the following disclaimer in the
    106  *    documentation and/or other materials provided with the distribution.
    107  * 3. All advertising materials mentioning features or use of this software
    108  *    must display the following acknowledgement:
    109  *	This product includes software developed by Microsoft
    110  *
    111  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
    112  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    113  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    114  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTERS BE LIABLE FOR ANY DIRECT,
    115  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    116  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    117  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    118  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    119  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    120  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    121  * SUCH DAMAGE.
    122  */
    123 
    124 #include <sys/cdefs.h>
    125 __KERNEL_RCSID(0, "$NetBSD: aarch64_reboot.c,v 1.5 2024/03/05 14:15:28 thorpej Exp $");
    126 
    127 #include <sys/param.h>
    128 #include <sys/device.h>
    129 #include <sys/kernel.h>
    130 #include <sys/reboot.h>
    131 #include <sys/mount.h>
    132 
    133 #include <dev/cons.h>
    134 #include <uvm/uvm_extern.h>
    135 
    136 #include <aarch64/machdep.h>
    137 #include <aarch64/armreg.h>
    138 #include <aarch64/locore.h>
    139 
    140 #include <arm/cpufunc.h>
    141 
    142 void (*cpu_reset_address0)(void);
    143 void (*cpu_reset_address)(void);
    144 void (*cpu_powerdown_address)(void);
    145 
    146 static void
    147 docpureset(int howto)
    148 {
    149 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
    150 		if (cpu_powerdown_address) {
    151 			(*cpu_powerdown_address)();
    152 			printf("WARNING: powerdown failed\r\n");
    153 		} else {
    154 			printf("WARNING: powerdown not supported\r\n");
    155 		}
    156 	}
    157 
    158 	if (howto & RB_HALT) {
    159 		printf("The operating system has halted.\r\n");
    160 		printf("Please press any key to reboot.\r\n");
    161 		cnpollc(true);	/* for proper keyboard command handling */
    162 		/* If there is no keyboard, cngetc() returns 0, so loop */
    163 		while (cngetc() == 0)
    164 			delay(100000);
    165 		cnpollc(false);
    166 	}
    167 
    168 	printf("rebooting...\r\n");
    169 
    170 	/* make sure to write to memory (msgbuf) */
    171 	aarch64_dcache_wb_all();
    172 
    173 	/* console output delay */
    174 	delay(1000);
    175 
    176 	if (cpu_reset_address != NULL)
    177 		(*cpu_reset_address)();
    178 	if (cpu_reset_address0 != NULL)
    179 		(*cpu_reset_address0)();
    180 
    181 	for (;;)
    182 		asm("wfi");
    183 
    184 	/*NOTREACHED*/
    185 }
    186 
    187 /* Sync the discs, unmount the filesystems, and adjust the todr */
    188 static void
    189 bootsync(void)
    190 {
    191 	static bool bootsyncdone = false;
    192 
    193 	if (bootsyncdone)
    194 		return;
    195 	bootsyncdone = true;
    196 
    197 	/* Make sure we can still manage to do things */
    198 	if ((reg_daif_read() & DAIF_I) != 0) {
    199 		/*
    200 		 * If we get here then boot has been called without RB_NOSYNC
    201 		 * and interrupts were disabled. This means the boot() call
    202 		 * did not come from a user process e.g. shutdown, but must
    203 		 * have come from somewhere in the kernel.
    204 		 */
    205 		ENABLE_INTERRUPT();
    206 		printf("Warning IRQ's disabled during boot()\n");
    207 	}
    208 
    209 	vfs_shutdown();
    210 }
    211 
    212 void
    213 cpu_reboot(int howto, char *bootstr)
    214 {
    215 
    216 	/*
    217 	 * If we are still cold then hit the air brakes
    218 	 * and crash to earth fast
    219 	 */
    220 	if (cold) {
    221 		doshutdownhooks();
    222 		docpureset(RB_HALT | howto);
    223 	}
    224 
    225 	/*
    226 	 * If RB_NOSYNC was not specified sync the discs.
    227 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    228 	 * unmount.  It looks like syslogd is getting woken up only to find
    229 	 * that it cannot page part of the binary in as the filesystem has
    230 	 * been unmounted.
    231 	 */
    232 	if ((howto & RB_NOSYNC) == 0)
    233 		bootsync();
    234 
    235 	/* Say NO to interrupts for the duration of the dump */
    236 	int s = splhigh();
    237 
    238 	/* Do a dump if requested. */
    239 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    240 		dumpsys();
    241 
    242 	splx(s);
    243 
    244 	pmf_system_shutdown(boothowto);
    245 
    246 	/* Say NO to interrupts for good */
    247 	splhigh();
    248 
    249 	/* Run any shutdown hooks */
    250 	doshutdownhooks();
    251 
    252 	/* Make sure IRQ's are disabled */
    253 	DISABLE_INTERRUPT();
    254 
    255 	docpureset(howto);
    256 	__unreachable();
    257 }
    258