/* SPDX-License-Identifier: GPL-2.01* $Id: head.S,v 1.7 2003/09/01 17:58:19 lethal Exp $2*3* arch/sh/kernel/head.S4*5* Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima6* Copyright (C) 2010 Matt Fleming7*8* Head.S contains the SH exception handlers and startup code.9*/10#include <linux/init.h>11#include <linux/linkage.h>12#include <asm/thread_info.h>13#include <asm/mmu.h>14#include <cpu/mmu_context.h>1516#ifdef CONFIG_CPU_SH4A17#define SYNCO() synco1819#define PREFI(label, reg) \20mov.l label, reg; \21prefi @reg22#else23#define SYNCO()24#define PREFI(label, reg)25#endif2627.section .empty_zero_page, "aw"28ENTRY(empty_zero_page)29.long 1 /* MOUNT_ROOT_RDONLY */30.long 0 /* RAMDISK_FLAGS */31.long 0x0200 /* ORIG_ROOT_DEV */32.long 1 /* LOADER_TYPE */33.long 0x00000000 /* INITRD_START */34.long 0x00000000 /* INITRD_SIZE */35#ifdef CONFIG_32BIT36.long 0x53453f00 + 32 /* "SE?" = 32 bit */37#else38.long 0x53453f00 + 29 /* "SE?" = 29 bit */39#endif401:41.skip PAGE_SIZE - empty_zero_page - 1b4243__HEAD4445/*46* Condition at the entry of _stext:47*48* BSC has already been initialized.49* INTC may or may not be initialized.50* VBR may or may not be initialized.51* MMU may or may not be initialized.52* Cache may or may not be initialized.53* Hardware (including on-chip modules) may or may not be initialized.54*55*/56ENTRY(_stext)57! Initialize Status Register58mov.l 1f, r0 ! MD=1, RB=0, BL=0, IMASK=0xF59ldc r0, sr60! Initialize global interrupt mask61#ifdef CONFIG_CPU_HAS_SR_RB62mov #0, r063ldc r0, r6_bank64#endif6566#ifdef CONFIG_OF_EARLY_FLATTREE67mov r4, r12 ! Store device tree blob pointer in r1268#endif6970/*71* Prefetch if possible to reduce cache miss penalty.72*73* We do this early on for SH-4A as a micro-optimization,74* as later on we will have speculative execution enabled75* and this will become less of an issue.76*/77PREFI(5f, r0)78PREFI(6f, r0)7980!81mov.l 2f, r082mov r0, r15 ! Set initial r15 (stack pointer)83#ifdef CONFIG_CPU_HAS_SR_RB84mov.l 7f, r085ldc r0, r7_bank ! ... and initial thread_info86#endif8788#ifdef CONFIG_PMB89/*90* Reconfigure the initial PMB mappings setup by the hardware.91*92* When we boot in 32-bit MMU mode there are 2 PMB entries already93* setup for us.94*95* Entry VPN PPN V SZ C UB WT96* ---------------------------------------------------------------97* 0 0x80000000 0x00000000 1 512MB 1 0 198* 1 0xA0000000 0x00000000 1 512MB 0 0 099*100* But we reprogram them here because we want complete control over101* our address space and the initial mappings may not map PAGE_OFFSET102* to __MEMORY_START (or even map all of our RAM).103*104* Once we've setup cached and uncached mappings we clear the rest of the105* PMB entries. This clearing also deals with the fact that PMB entries106* can persist across reboots. The PMB could have been left in any state107* when the reboot occurred, so to be safe we clear all entries and start108* with with a clean slate.109*110* The uncached mapping is constructed using the smallest possible111* mapping with a single unbufferable page. Only the kernel text needs to112* be covered via the uncached mapping so that certain functions can be113* run uncached.114*115* Drivers and the like that have previously abused the 1:1 identity116* mapping are unsupported in 32-bit mode and must specify their caching117* preference when page tables are constructed.118*119* This frees up the P2 space for more nefarious purposes.120*121* Register utilization is as follows:122*123* r0 = PMB_DATA data field124* r1 = PMB_DATA address field125* r2 = PMB_ADDR data field126* r3 = PMB_ADDR address field127* r4 = PMB_E_SHIFT128* r5 = remaining amount of RAM to map129* r6 = PMB mapping size we're trying to use130* r7 = cached_to_uncached131* r8 = scratch register132* r9 = scratch register133* r10 = number of PMB entries we've setup134* r11 = scratch register135*/136137mov.l .LMMUCR, r1 /* Flush the TLB */138mov.l @r1, r0139or #MMUCR_TI, r0140mov.l r0, @r1141142mov.l .LMEMORY_SIZE, r5143144mov #PMB_E_SHIFT, r0145mov #0x1, r4146shld r0, r4147148mov.l .LFIRST_DATA_ENTRY, r0149mov.l .LPMB_DATA, r1150mov.l .LFIRST_ADDR_ENTRY, r2151mov.l .LPMB_ADDR, r3152153/*154* First we need to walk the PMB and figure out if there are any155* existing mappings that match the initial mappings VPN/PPN.156* If these have already been established by the bootloader, we157* don't bother setting up new entries here, and let the late PMB158* initialization take care of things instead.159*160* Note that we may need to coalesce and merge entries in order161* to reclaim more available PMB slots, which is much more than162* we want to do at this early stage.163*/164mov #0, r10165mov #NR_PMB_ENTRIES, r9166167mov r1, r7 /* temporary PMB_DATA iter */168169.Lvalidate_existing_mappings:170171mov.l .LPMB_DATA_MASK, r11172mov.l @r7, r8173and r11, r8174cmp/eq r0, r8 /* Check for valid __MEMORY_START mappings */175bt .Lpmb_done176177add #1, r10 /* Increment the loop counter */178cmp/eq r9, r10179bf/s .Lvalidate_existing_mappings180add r4, r7 /* Increment to the next PMB_DATA entry */181182/*183* If we've fallen through, continue with setting up the initial184* mappings.185*/186187mov r5, r7 /* cached_to_uncached */188mov #0, r10189190#ifdef CONFIG_UNCACHED_MAPPING191/*192* Uncached mapping193*/194mov #(PMB_SZ_16M >> 2), r9195shll2 r9196197mov #(PMB_UB >> 8), r8198shll8 r8199200or r0, r8201or r9, r8202mov.l r8, @r1203mov r2, r8204add r7, r8205mov.l r8, @r3206207add r4, r1208add r4, r3209add #1, r10210#endif211212/*213* Iterate over all of the available sizes from largest to214* smallest for constructing the cached mapping.215*/216#define __PMB_ITER_BY_SIZE(size) \217.L##size: \218mov #(size >> 4), r6; \219shll16 r6; \220shll8 r6; \221\222cmp/hi r5, r6; \223bt 9999f; \224\225mov #(PMB_SZ_##size##M >> 2), r9; \226shll2 r9; \227\228/* \229* Cached mapping \230*/ \231mov #PMB_C, r8; \232or r0, r8; \233or r9, r8; \234mov.l r8, @r1; \235mov.l r2, @r3; \236\237/* Increment to the next PMB_DATA entry */ \238add r4, r1; \239/* Increment to the next PMB_ADDR entry */ \240add r4, r3; \241/* Increment number of PMB entries */ \242add #1, r10; \243\244sub r6, r5; \245add r6, r0; \246add r6, r2; \247\248bra .L##size; \2499999:250251__PMB_ITER_BY_SIZE(512)252__PMB_ITER_BY_SIZE(128)253__PMB_ITER_BY_SIZE(64)254__PMB_ITER_BY_SIZE(16)255256#ifdef CONFIG_UNCACHED_MAPPING257/*258* Now that we can access it, update cached_to_uncached and259* uncached_size.260*/261mov.l .Lcached_to_uncached, r0262mov.l r7, @r0263264mov.l .Luncached_size, r0265mov #1, r7266shll16 r7267shll8 r7268mov.l r7, @r0269#endif270271/*272* Clear the remaining PMB entries.273*274* r3 = entry to begin clearing from275* r10 = number of entries we've setup so far276*/277mov #0, r1278mov #NR_PMB_ENTRIES, r0279280.Lagain:281mov.l r1, @r3 /* Clear PMB_ADDR entry */282add #1, r10 /* Increment the loop counter */283cmp/eq r0, r10284bf/s .Lagain285add r4, r3 /* Increment to the next PMB_ADDR entry */286287mov.l 6f, r0288icbi @r0289290.Lpmb_done:291#endif /* CONFIG_PMB */292293#ifndef CONFIG_SH_NO_BSS_INIT294/*295* Don't clear BSS if running on slow platforms such as an RTL simulation,296* remote memory via SHdebug link, etc. For these the memory can be guaranteed297* to be all zero on boot anyway.298*/299! Clear BSS area300#ifdef CONFIG_SMP301mov.l 3f, r0302cmp/eq #0, r0 ! skip clear if set to zero303bt 10f304#endif305306mov.l 3f, r1307add #4, r1308mov.l 4f, r2309mov #0, r03109: cmp/hs r2, r1311bf/s 9b ! while (r1 < r2)312mov.l r0,@-r231331410:315#endif316317#ifdef CONFIG_OF_EARLY_FLATTREE318mov.l 8f, r0 ! Make flat device tree available early.319jsr @r0320mov r12, r4321#endif322323! Additional CPU initialization324mov.l 6f, r0325jsr @r0326nop327328SYNCO() ! Wait for pending instructions..329330! Start kernel331mov.l 5f, r0332jmp @r0333nop334335.balign 4336#if defined(CONFIG_CPU_SH2)3371: .long 0x000000F0 ! IMASK=0xF338#else3391: .long 0x500080F0 ! MD=1, RB=0, BL=1, FD=1, IMASK=0xF340#endif341ENTRY(stack_start)3422: .long init_thread_union+THREAD_SIZE3433: .long __bss_start3444: .long _end3455: .long start_kernel3466: .long cpu_init3477: .long init_thread_union348#if defined(CONFIG_OF_EARLY_FLATTREE)3498: .long sh_fdt_init350#endif351352#ifdef CONFIG_PMB353.LPMB_ADDR: .long PMB_ADDR354.LPMB_DATA: .long PMB_DATA355.LPMB_DATA_MASK: .long PMB_PFN_MASK | PMB_V356.LFIRST_ADDR_ENTRY: .long PAGE_OFFSET | PMB_V357.LFIRST_DATA_ENTRY: .long __MEMORY_START | PMB_V358.LMMUCR: .long MMUCR359.LMEMORY_SIZE: .long __MEMORY_SIZE360#ifdef CONFIG_UNCACHED_MAPPING361.Lcached_to_uncached: .long cached_to_uncached362.Luncached_size: .long uncached_size363#endif364#endif365366367