1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * arch/alpha/boot/bootp.c
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 1997 Jay Estabrook
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * This file is used for creating a bootp file for the Linux/AXP kernel
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * based significantly on the arch/alpha/boot/main.c of Linus Torvalds
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun #include <linux/kernel.h>
12*4882a593Smuzhiyun #include <linux/slab.h>
13*4882a593Smuzhiyun #include <linux/string.h>
14*4882a593Smuzhiyun #include <generated/utsrelease.h>
15*4882a593Smuzhiyun #include <linux/mm.h>
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include <asm/console.h>
18*4882a593Smuzhiyun #include <asm/hwrpb.h>
19*4882a593Smuzhiyun #include <asm/io.h>
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #include <stdarg.h>
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #include "ksize.h"
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun extern unsigned long switch_to_osf_pal(unsigned long nr,
26*4882a593Smuzhiyun struct pcb_struct * pcb_va, struct pcb_struct * pcb_pa,
27*4882a593Smuzhiyun unsigned long *vptb);
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun extern void move_stack(unsigned long new_stack);
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun struct hwrpb_struct *hwrpb = INIT_HWRPB;
32*4882a593Smuzhiyun static struct pcb_struct pcb_va[1];
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun /*
35*4882a593Smuzhiyun * Find a physical address of a virtual object..
36*4882a593Smuzhiyun *
37*4882a593Smuzhiyun * This is easy using the virtual page table address.
38*4882a593Smuzhiyun */
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun static inline void *
find_pa(unsigned long * vptb,void * ptr)41*4882a593Smuzhiyun find_pa(unsigned long *vptb, void *ptr)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun unsigned long address = (unsigned long) ptr;
44*4882a593Smuzhiyun unsigned long result;
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun result = vptb[address >> 13];
47*4882a593Smuzhiyun result >>= 32;
48*4882a593Smuzhiyun result <<= 13;
49*4882a593Smuzhiyun result |= address & 0x1fff;
50*4882a593Smuzhiyun return (void *) result;
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /*
54*4882a593Smuzhiyun * This function moves into OSF/1 pal-code, and has a temporary
55*4882a593Smuzhiyun * PCB for that. The kernel proper should replace this PCB with
56*4882a593Smuzhiyun * the real one as soon as possible.
57*4882a593Smuzhiyun *
58*4882a593Smuzhiyun * The page table muckery in here depends on the fact that the boot
59*4882a593Smuzhiyun * code has the L1 page table identity-map itself in the second PTE
60*4882a593Smuzhiyun * in the L1 page table. Thus the L1-page is virtually addressable
61*4882a593Smuzhiyun * itself (through three levels) at virtual address 0x200802000.
62*4882a593Smuzhiyun */
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun #define VPTB ((unsigned long *) 0x200000000)
65*4882a593Smuzhiyun #define L1 ((unsigned long *) 0x200802000)
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun void
pal_init(void)68*4882a593Smuzhiyun pal_init(void)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun unsigned long i, rev;
71*4882a593Smuzhiyun struct percpu_struct * percpu;
72*4882a593Smuzhiyun struct pcb_struct * pcb_pa;
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /* Create the dummy PCB. */
75*4882a593Smuzhiyun pcb_va->ksp = 0;
76*4882a593Smuzhiyun pcb_va->usp = 0;
77*4882a593Smuzhiyun pcb_va->ptbr = L1[1] >> 32;
78*4882a593Smuzhiyun pcb_va->asn = 0;
79*4882a593Smuzhiyun pcb_va->pcc = 0;
80*4882a593Smuzhiyun pcb_va->unique = 0;
81*4882a593Smuzhiyun pcb_va->flags = 1;
82*4882a593Smuzhiyun pcb_va->res1 = 0;
83*4882a593Smuzhiyun pcb_va->res2 = 0;
84*4882a593Smuzhiyun pcb_pa = find_pa(VPTB, pcb_va);
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun /*
87*4882a593Smuzhiyun * a0 = 2 (OSF)
88*4882a593Smuzhiyun * a1 = return address, but we give the asm the vaddr of the PCB
89*4882a593Smuzhiyun * a2 = physical addr of PCB
90*4882a593Smuzhiyun * a3 = new virtual page table pointer
91*4882a593Smuzhiyun * a4 = KSP (but the asm sets it)
92*4882a593Smuzhiyun */
93*4882a593Smuzhiyun srm_printk("Switching to OSF PAL-code .. ");
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun i = switch_to_osf_pal(2, pcb_va, pcb_pa, VPTB);
96*4882a593Smuzhiyun if (i) {
97*4882a593Smuzhiyun srm_printk("failed, code %ld\n", i);
98*4882a593Smuzhiyun __halt();
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun percpu = (struct percpu_struct *)
102*4882a593Smuzhiyun (INIT_HWRPB->processor_offset + (unsigned long) INIT_HWRPB);
103*4882a593Smuzhiyun rev = percpu->pal_revision = percpu->palcode_avail[2];
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun srm_printk("Ok (rev %lx)\n", rev);
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun tbia(); /* do it directly in case we are SMP */
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun static inline void
load(unsigned long dst,unsigned long src,unsigned long count)111*4882a593Smuzhiyun load(unsigned long dst, unsigned long src, unsigned long count)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun memcpy((void *)dst, (void *)src, count);
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun /*
117*4882a593Smuzhiyun * Start the kernel.
118*4882a593Smuzhiyun */
119*4882a593Smuzhiyun static inline void
runkernel(void)120*4882a593Smuzhiyun runkernel(void)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun __asm__ __volatile__(
123*4882a593Smuzhiyun "bis %0,%0,$27\n\t"
124*4882a593Smuzhiyun "jmp ($27)"
125*4882a593Smuzhiyun : /* no outputs: it doesn't even return */
126*4882a593Smuzhiyun : "r" (START_ADDR));
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun extern char _end;
130*4882a593Smuzhiyun #define KERNEL_ORIGIN \
131*4882a593Smuzhiyun ((((unsigned long)&_end) + 511) & ~511)
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun void
start_kernel(void)134*4882a593Smuzhiyun start_kernel(void)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun /*
137*4882a593Smuzhiyun * Note that this crufty stuff with static and envval
138*4882a593Smuzhiyun * and envbuf is because:
139*4882a593Smuzhiyun *
140*4882a593Smuzhiyun * 1. Frequently, the stack is short, and we don't want to overrun;
141*4882a593Smuzhiyun * 2. Frequently the stack is where we are going to copy the kernel to;
142*4882a593Smuzhiyun * 3. A certain SRM console required the GET_ENV output to stack.
143*4882a593Smuzhiyun * ??? A comment in the aboot sources indicates that the GET_ENV
144*4882a593Smuzhiyun * destination must be quadword aligned. Might this explain the
145*4882a593Smuzhiyun * behaviour, rather than requiring output to the stack, which
146*4882a593Smuzhiyun * seems rather far-fetched.
147*4882a593Smuzhiyun */
148*4882a593Smuzhiyun static long nbytes;
149*4882a593Smuzhiyun static char envval[256] __attribute__((aligned(8)));
150*4882a593Smuzhiyun static unsigned long initrd_start;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun srm_printk("Linux/AXP bootp loader for Linux " UTS_RELEASE "\n");
153*4882a593Smuzhiyun if (INIT_HWRPB->pagesize != 8192) {
154*4882a593Smuzhiyun srm_printk("Expected 8kB pages, got %ldkB\n",
155*4882a593Smuzhiyun INIT_HWRPB->pagesize >> 10);
156*4882a593Smuzhiyun return;
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun if (INIT_HWRPB->vptb != (unsigned long) VPTB) {
159*4882a593Smuzhiyun srm_printk("Expected vptb at %p, got %p\n",
160*4882a593Smuzhiyun VPTB, (void *)INIT_HWRPB->vptb);
161*4882a593Smuzhiyun return;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun pal_init();
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun /* The initrd must be page-aligned. See below for the
166*4882a593Smuzhiyun cause of the magic number 5. */
167*4882a593Smuzhiyun initrd_start = ((START_ADDR + 5*KERNEL_SIZE + PAGE_SIZE) |
168*4882a593Smuzhiyun (PAGE_SIZE-1)) + 1;
169*4882a593Smuzhiyun #ifdef INITRD_IMAGE_SIZE
170*4882a593Smuzhiyun srm_printk("Initrd positioned at %#lx\n", initrd_start);
171*4882a593Smuzhiyun #endif
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun /*
174*4882a593Smuzhiyun * Move the stack to a safe place to ensure it won't be
175*4882a593Smuzhiyun * overwritten by kernel image.
176*4882a593Smuzhiyun */
177*4882a593Smuzhiyun move_stack(initrd_start - PAGE_SIZE);
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun nbytes = callback_getenv(ENV_BOOTED_OSFLAGS, envval, sizeof(envval));
180*4882a593Smuzhiyun if (nbytes < 0 || nbytes >= sizeof(envval)) {
181*4882a593Smuzhiyun nbytes = 0;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun envval[nbytes] = '\0';
184*4882a593Smuzhiyun srm_printk("Loading the kernel...'%s'\n", envval);
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun /* NOTE: *no* callbacks or printouts from here on out!!! */
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun /* This is a hack, as some consoles seem to get virtual 20000000 (ie
189*4882a593Smuzhiyun * where the SRM console puts the kernel bootp image) memory
190*4882a593Smuzhiyun * overlapping physical memory where the kernel wants to be put,
191*4882a593Smuzhiyun * which causes real problems when attempting to copy the former to
192*4882a593Smuzhiyun * the latter... :-(
193*4882a593Smuzhiyun *
194*4882a593Smuzhiyun * So, we first move the kernel virtual-to-physical way above where
195*4882a593Smuzhiyun * we physically want the kernel to end up, then copy it from there
196*4882a593Smuzhiyun * to its final resting place... ;-}
197*4882a593Smuzhiyun *
198*4882a593Smuzhiyun * Sigh... */
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun #ifdef INITRD_IMAGE_SIZE
201*4882a593Smuzhiyun load(initrd_start, KERNEL_ORIGIN+KERNEL_SIZE, INITRD_IMAGE_SIZE);
202*4882a593Smuzhiyun #endif
203*4882a593Smuzhiyun load(START_ADDR+(4*KERNEL_SIZE), KERNEL_ORIGIN, KERNEL_SIZE);
204*4882a593Smuzhiyun load(START_ADDR, START_ADDR+(4*KERNEL_SIZE), KERNEL_SIZE);
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun memset((char*)ZERO_PGE, 0, PAGE_SIZE);
207*4882a593Smuzhiyun strcpy((char*)ZERO_PGE, envval);
208*4882a593Smuzhiyun #ifdef INITRD_IMAGE_SIZE
209*4882a593Smuzhiyun ((long *)(ZERO_PGE+256))[0] = initrd_start;
210*4882a593Smuzhiyun ((long *)(ZERO_PGE+256))[1] = INITRD_IMAGE_SIZE;
211*4882a593Smuzhiyun #endif
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun runkernel();
214*4882a593Smuzhiyun }
215