/*	$NetBSD: locore.S,v 1.82 2022/12/18 01:16:05 macallan Exp $	*/

/*
 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
 * Copyright (C) 1995, 1996 TooLs GmbH.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by TooLs GmbH.
 * 4. The name of TooLs GmbH may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "opt_ddb.h"
#include "opt_kgdb.h"
#include "opt_lockdebug.h"
#include "opt_modular.h"
#include "opt_multiprocessor.h"
#include "opt_altivec.h"
#include "opt_ppcparam.h"
#include "ksyms.h"
#include "assym.h"

#include <sys/syscall.h>

#include <machine/param.h>
#include <machine/psl.h>
#include <machine/trap.h>
#include <machine/asm.h>

#include <powerpc/spr.h>
#include <powerpc/oea/spr.h>

/*
 * Some instructions gas doesn't understand (yet?)
 */
#define	bdneq	bdnzf 2,

/*
 * Globals
 */
	.data
GLOBAL(esym)
	.long	0			/* end of symbol table */

/*
 * This symbol is here for the benefit of kvm_mkdb, and is supposed to
 * mark the start of kernel text.
 */

	.text
	.globl	_C_LABEL(kernel_text)
_C_LABEL(kernel_text):

/*
 * Startup entry.  Note, this must be the first thing in the text
 * segment!
 *
 * Register state as transfer is passed from OpenFirmware / boot loader:
 *
 *	%r1	Stack provided by OpenFirmware / boot loader
 *	%r3	Reserved for platform binding (unused here)
 *	%r4	Reserved for platform binding (unused here)
 *	%r5	OpenFirmware client entry point
 *	%r6	Arguments
 *	%r7	Arguments length
 */
	.text
	.globl	__start
__start:
	/* Save off arguments that we need preserved. */
	mr	%r13,%r6
	mr	%r14,%r7

/* 
 * Zero SPRG0 so we can catch usage of curcpu(), which just returns SPRG0,
 * before it is set up properly. Console output uses locks now, which need
 * working curcpu(), and macppc sets up its console *very* early.
 */
#ifdef DEBUG
	li	%r0,0
	mtsprg0	%r0
#endif

	bl	_C_LABEL(ofwinit)		/* init OF */

	li	%r0,0
#ifndef FIRMWORKSBUGS
	mtmsr	%r0			/* Disable FPU/MMU/exceptions */
#endif
	isync

	bl	_C_LABEL(cpu_model_init)	/* init oeacpufeat */

/* compute end of kernel memory */
	lis	%r4,_C_LABEL(end)@ha
	addi	%r4,%r4,_C_LABEL(end)@l
#if NKSYMS || defined(DDB) || defined(MODULAR)
	/* skip symbol table */
	mr	%r6,%r13
	mr	%r7,%r14
	cmpwi	%r6,0
	beq	1f
	add	%r9,%r6,%r7		/* r9 = args + l */
	lwz	%r9,-8(%r9)		/* esym */
	cmpwi	%r9,0
	beq	1f
	mr	%r4,%r9
1:
#endif

#if defined (PMAC_G5) || defined (MAMBO)
	 /* and clear HID5 DCBZ bits (56/57), need to do this early */
	mfspr	%r11,SPR_HID5
	rldimi	%r11,%r0,6,56
	sync
	mtspr	SPR_HID5,%r11
	isync
	sync

	/* Setup HID1 features, prefetch + i-cacheability controlled by PTE */
	mfspr	%r0,SPR_HID1
	li	%r11,0x1200
	sldi	%r11,%r11,44
	or	%r0,%r0,%r11
	mtspr	SPR_HID1,%r0
	isync
	sync

	/* Restore r0 */
	li	%r0,0
#endif /* PMAC_G5 */


	/*
	 * Initialize cpu_info[0]
	 */
	INIT_CPUINFO(%r4,%r1,%r9,%r0)

	lis	%r3,__start@ha
	addi	%r3,%r3,__start@l
	mr	%r5,%r6			/* args string */
	bl	_C_LABEL(initppc)
	bl	_C_LABEL(main)
	b	_C_LABEL(OF_exit)

#if defined (PPC_OEA64_BRIDGE)
/* Return all 64 bits of HID4 */
	.text
	.globl _C_LABEL(mfhid4)
_C_LABEL(mfhid4):
	mfspr	%r4, SPR_HID4 
	std	%r4, 0(%r3)
	blr

/* Set all 64 bits of HID4 */
	.text
	.globl _C_LABEL(mthid4)
_C_LABEL(mthid4):
	ld %r4, 0(%r3)
	sync
	mtspr SPR_HID4, %r4 
	isync 
	isync
	blr

	.text
	.globl _C_LABEL(change_hid4)
_C_LABEL(change_hid4):
	mfspr %r6, SPR_HID4
	rldicl %r5, %r6, 32, 0 
	ori %r5, %r5, 0x100
	rldicl %r5, %r5, 32, 0 
	std %r5, 0(%r3)
	blr
#endif

/*
 * Pull in common Open Firmware code.
 */
#include <powerpc/oea/ofw_subr.S>

/*
 * Pull in common switch and setfault code.
 */
#include <powerpc/powerpc/locore_subr.S>

/*
 * Pull in common trap vector code.
 */
#include <powerpc/powerpc/trap_subr.S>

/*
 * Pull in common pio / bus_space code.
 */
#include <powerpc/powerpc/pio_subr.S>
