1*f36002f2Sthorpej /* $NetBSD: ibm4xx_machdep.c,v 1.39 2024/03/05 14:15:34 thorpej Exp $ */
295b24072Sshige /* Original: ibm40x_machdep.c,v 1.3 2005/01/17 17:19:36 shige Exp $ */
395b24072Sshige
495b24072Sshige /*
595b24072Sshige * Copyright 2001, 2002 Wasabi Systems, Inc.
695b24072Sshige * All rights reserved.
795b24072Sshige *
895b24072Sshige * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
995b24072Sshige *
1095b24072Sshige * Redistribution and use in source and binary forms, with or without
1195b24072Sshige * modification, are permitted provided that the following conditions
1295b24072Sshige * are met:
1395b24072Sshige * 1. Redistributions of source code must retain the above copyright
1495b24072Sshige * notice, this list of conditions and the following disclaimer.
1595b24072Sshige * 2. Redistributions in binary form must reproduce the above copyright
1695b24072Sshige * notice, this list of conditions and the following disclaimer in the
1795b24072Sshige * documentation and/or other materials provided with the distribution.
1895b24072Sshige * 3. All advertising materials mentioning features or use of this software
1995b24072Sshige * must display the following acknowledgement:
2095b24072Sshige * This product includes software developed for the NetBSD Project by
2195b24072Sshige * Wasabi Systems, Inc.
2295b24072Sshige * 4. The name of Wasabi Systems, Inc. may not be used to endorse
2395b24072Sshige * or promote products derived from this software without specific prior
2495b24072Sshige * written permission.
2595b24072Sshige *
2695b24072Sshige * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
2795b24072Sshige * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2895b24072Sshige * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2995b24072Sshige * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
3095b24072Sshige * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3195b24072Sshige * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3295b24072Sshige * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3395b24072Sshige * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3495b24072Sshige * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3595b24072Sshige * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3695b24072Sshige * POSSIBILITY OF SUCH DAMAGE.
3795b24072Sshige */
3895b24072Sshige
3995b24072Sshige /*
4095b24072Sshige * Copyright (C) 1995, 1996 Wolfgang Solfrank.
4195b24072Sshige * Copyright (C) 1995, 1996 TooLs GmbH.
4295b24072Sshige * All rights reserved.
4395b24072Sshige *
4495b24072Sshige * Redistribution and use in source and binary forms, with or without
4595b24072Sshige * modification, are permitted provided that the following conditions
4695b24072Sshige * are met:
4795b24072Sshige * 1. Redistributions of source code must retain the above copyright
4895b24072Sshige * notice, this list of conditions and the following disclaimer.
4995b24072Sshige * 2. Redistributions in binary form must reproduce the above copyright
5095b24072Sshige * notice, this list of conditions and the following disclaimer in the
5195b24072Sshige * documentation and/or other materials provided with the distribution.
5295b24072Sshige * 3. All advertising materials mentioning features or use of this software
5395b24072Sshige * must display the following acknowledgement:
5495b24072Sshige * This product includes software developed by TooLs GmbH.
5595b24072Sshige * 4. The name of TooLs GmbH may not be used to endorse or promote products
5695b24072Sshige * derived from this software without specific prior written permission.
5795b24072Sshige *
5895b24072Sshige * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
5995b24072Sshige * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
6095b24072Sshige * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
6195b24072Sshige * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
6295b24072Sshige * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
6395b24072Sshige * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
6495b24072Sshige * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
6595b24072Sshige * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
6695b24072Sshige * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
6795b24072Sshige * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6895b24072Sshige */
6995b24072Sshige
7095b24072Sshige #include <sys/cdefs.h>
71*f36002f2Sthorpej __KERNEL_RCSID(0, "$NetBSD: ibm4xx_machdep.c,v 1.39 2024/03/05 14:15:34 thorpej Exp $");
7295b24072Sshige
7316031f7dSrin #include "ksyms.h"
7416031f7dSrin
7516031f7dSrin #ifdef _KERNEL_OPT
7695b24072Sshige #include "opt_ddb.h"
7795b24072Sshige #include "opt_kgdb.h"
78dba36e03Smatt #include "opt_modular.h"
7916031f7dSrin #endif
8095b24072Sshige
8195b24072Sshige #include <sys/param.h>
821fd2c684Smatt #include <sys/cpu.h>
83dba36e03Smatt #include <sys/ksyms.h>
84c98afc16Srin #include <sys/mount.h>
8504d12c46Srin #include <sys/msgbuf.h>
86c98afc16Srin #include <sys/pmf.h>
8704d12c46Srin #include <sys/proc.h>
88c98afc16Srin #include <sys/reboot.h>
89c98afc16Srin #include <sys/systm.h>
90c98afc16Srin
91c98afc16Srin #include <dev/cons.h>
9295b24072Sshige
9395b24072Sshige #include <uvm/uvm_extern.h>
9495b24072Sshige
9595b24072Sshige #if defined(DDB)
9695b24072Sshige #include <machine/db_machdep.h>
9795b24072Sshige #include <ddb/db_extern.h>
9895b24072Sshige #endif
9995b24072Sshige
10095b24072Sshige #if defined(KGDB)
10195b24072Sshige #include <sys/kgdb.h>
10295b24072Sshige #endif
10395b24072Sshige
10495b24072Sshige #include <machine/powerpc.h>
1051fd2c684Smatt #include <machine/trap.h>
1061fd2c684Smatt
10704d12c46Srin #include <powerpc/pcb.h>
10895b24072Sshige #include <powerpc/spr.h>
1091fd2c684Smatt
1101fd2c684Smatt #include <powerpc/ibm4xx/cpu.h>
11104d12c46Srin #include <powerpc/ibm4xx/spr.h>
11295b24072Sshige
11395b24072Sshige /*
11495b24072Sshige * Global variables used here and there
11595b24072Sshige */
11695b24072Sshige paddr_t msgbuf_paddr;
11795b24072Sshige vaddr_t msgbuf_vaddr;
11895b24072Sshige char msgbuf[MSGBUFSIZE];
11995b24072Sshige
120dba36e03Smatt #if NKSYMS || defined(DDB) || defined(MODULAR)
121dba36e03Smatt void *startsym, *endsym;
122dba36e03Smatt #endif
123dba36e03Smatt
124626c0d3cSrin #ifdef MODULAR
125626c0d3cSrin register_t cpu_psluserset = PSL_USERSET;
126626c0d3cSrin register_t cpu_pslusermod = PSL_USERMOD;
127626c0d3cSrin register_t cpu_pslusermask = PSL_USERMASK;
128626c0d3cSrin #endif
129626c0d3cSrin
130dba36e03Smatt /*
131dba36e03Smatt * Trap vectors
132dba36e03Smatt */
133dba36e03Smatt extern const uint32_t defaulttrap[], defaultsize;
134dba36e03Smatt extern const uint32_t sctrap[], scsize;
1350000e45fSrin extern const uint32_t accesstrap[], accesssize;
1360000e45fSrin extern const uint32_t criticaltrap[], criticalsize;
137dba36e03Smatt extern const uint32_t tlbimiss4xx[], tlbim4size;
138dba36e03Smatt extern const uint32_t tlbdmiss4xx[], tlbdm4size;
139dba36e03Smatt extern const uint32_t pitfitwdog[], pitfitwdogsize;
140dba36e03Smatt extern const uint32_t errata51handler[], errata51size;
1416407d377Srin #if defined(DDB)
142dba36e03Smatt extern const uint32_t ddblow[], ddbsize;
143dba36e03Smatt #endif
144dba36e03Smatt static const struct exc_info trap_table[] = {
145dba36e03Smatt { EXC_SC, sctrap, (uintptr_t)&scsize },
1460000e45fSrin { EXC_ALI, accesstrap, (uintptr_t)&accesssize },
1470000e45fSrin { EXC_DSI, accesstrap, (uintptr_t)&accesssize },
1480000e45fSrin { EXC_MCHK, criticaltrap, (uintptr_t)&criticalsize },
149dba36e03Smatt { EXC_ITMISS, tlbimiss4xx, (uintptr_t)&tlbim4size },
150dba36e03Smatt { EXC_DTMISS, tlbdmiss4xx, (uintptr_t)&tlbdm4size },
151dba36e03Smatt { EXC_PIT, pitfitwdog, (uintptr_t)&pitfitwdogsize },
1520000e45fSrin { EXC_DEBUG, criticaltrap, (uintptr_t)&criticalsize },
153dba36e03Smatt { (EXC_DTMISS|EXC_ALI),
154dba36e03Smatt errata51handler, (uintptr_t)&errata51size },
155dba36e03Smatt #if defined(DDB)
156dba36e03Smatt { EXC_PGM, ddblow, (uintptr_t)&ddbsize },
157fe285e40Srin #else
158fe285e40Srin { EXC_PGM, accesstrap, (uintptr_t)&accesssize },
159d0e34aacSrin #endif
160dba36e03Smatt };
161dba36e03Smatt
162c98afc16Srin void
cpu_reboot(int howto,char * what)163c98afc16Srin cpu_reboot(int howto, char *what)
164c98afc16Srin {
165c98afc16Srin static int syncing;
166c98afc16Srin static char str[256];
167c98afc16Srin char *ap = str, *ap1 = ap;
168c98afc16Srin
169c98afc16Srin boothowto = howto;
170c98afc16Srin if (!cold && !(howto & RB_NOSYNC) && !syncing) {
171c98afc16Srin syncing = 1;
172c98afc16Srin vfs_shutdown(); /* sync */
173c98afc16Srin }
174c98afc16Srin
175c98afc16Srin splhigh();
176c98afc16Srin
177c98afc16Srin if (!cold && (howto & RB_DUMP))
178c98afc16Srin ibm4xx_dumpsys();
179c98afc16Srin
180c98afc16Srin doshutdownhooks();
181c98afc16Srin
182c98afc16Srin pmf_system_shutdown(boothowto);
183c98afc16Srin
184c98afc16Srin if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
185c98afc16Srin /* Power off here if we know how... */
186c98afc16Srin }
187c98afc16Srin
188c98afc16Srin if (howto & RB_HALT) {
189c98afc16Srin printf("The operating system has halted.\n"
190c98afc16Srin "Press any key to reboot.\n\n");
191c98afc16Srin
192c98afc16Srin cnpollc(1);
193c98afc16Srin cngetc();
194c98afc16Srin cnpollc(0);
195c98afc16Srin }
196c98afc16Srin
197c98afc16Srin printf("rebooting...\n\n");
198c98afc16Srin if (what && *what) {
199c98afc16Srin if (strlen(what) > sizeof(str) - 5)
200c98afc16Srin printf("boot string too large, ignored\n");
201c98afc16Srin else {
202c98afc16Srin strcpy(str, what);
203c98afc16Srin ap1 = ap = str + strlen(str);
204c98afc16Srin *ap++ = ' ';
205c98afc16Srin }
206c98afc16Srin }
207c98afc16Srin *ap++ = '-';
208c98afc16Srin if (howto & RB_SINGLE)
209c98afc16Srin *ap++ = 's';
210c98afc16Srin if (howto & RB_KDB)
211c98afc16Srin *ap++ = 'd';
212c98afc16Srin *ap++ = '\0';
213c98afc16Srin if (ap[-2] == '-')
214c98afc16Srin *ap1 = '\0';
215c98afc16Srin
216c98afc16Srin /* flush cache for msgbuf */
217c98afc16Srin __syncicache((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
218c98afc16Srin
219c98afc16Srin ppc4xx_reset();
220c98afc16Srin
221c98afc16Srin printf("ppc4xx_reset() failed!\n");
222c98afc16Srin
223bd1d1ec9Srin for (;;) {
224c98afc16Srin #if defined(DDB)
225c98afc16Srin Debugger();
226c98afc16Srin #elif defined(KGDB)
227c98afc16Srin kgdb_connect(1);
228c98afc16Srin #else
229c98afc16Srin continue;
230c98afc16Srin #endif
231c98afc16Srin }
232c98afc16Srin }
233c98afc16Srin
234dba36e03Smatt /*
235dba36e03Smatt * Install a trap vector. We cannot use memcpy because the
236dba36e03Smatt * destination may be zero.
237dba36e03Smatt */
238dba36e03Smatt static void
trap_copy(const uint32_t * src,vaddr_t dest,size_t len)239dba36e03Smatt trap_copy(const uint32_t *src, vaddr_t dest, size_t len)
240dba36e03Smatt {
241dba36e03Smatt uint32_t *dest_p = (void *)dest;
242dba36e03Smatt
243dba36e03Smatt while (len > 0) {
244dba36e03Smatt *dest_p++ = *src++;
245dba36e03Smatt len -= sizeof(uint32_t);
246dba36e03Smatt }
247dba36e03Smatt }
248dba36e03Smatt
24995b24072Sshige /*
25095b24072Sshige * ibm4xx_init:
25195b24072Sshige */
25295b24072Sshige void
ibm4xx_init(vaddr_t startkernel,vaddr_t endkernel,void (* handler)(void))253dba36e03Smatt ibm4xx_init(vaddr_t startkernel, vaddr_t endkernel, void (*handler)(void))
25495b24072Sshige {
25595b24072Sshige /* Initialize cache info for memcpy, etc. */
25695b24072Sshige cpu_probe_cache();
25795b24072Sshige
25895b24072Sshige /*
259dba36e03Smatt * Initialize current pcb and pmap pointers.
26095b24072Sshige */
261dba36e03Smatt KASSERT(curcpu() == &cpu_info[0]);
262dba36e03Smatt KASSERT(lwp0.l_cpu == curcpu());
263dba36e03Smatt KASSERT(curlwp == &lwp0);
2643f18fe81Srmind
265dba36e03Smatt curpcb = lwp_getpcb(curlwp);
2663f18fe81Srmind memset(curpcb, 0, sizeof(struct pcb));
26795b24072Sshige
26895b24072Sshige curpcb->pcb_pm = pmap_kernel();
26995b24072Sshige
270dba36e03Smatt for (uintptr_t exc = EXC_RSVD; exc <= EXC_LAST; exc += 0x100) {
271dba36e03Smatt trap_copy(defaulttrap, exc, (uintptr_t)&defaultsize);
272dba36e03Smatt }
273dba36e03Smatt
274dba36e03Smatt for (size_t i = 0; i < __arraycount(trap_table); i++) {
275b72e1d6dSrin KASSERT(trap_table[i].exc_size <= 0x100);
276dba36e03Smatt trap_copy(trap_table[i].exc_addr, trap_table[i].exc_vector,
277dba36e03Smatt trap_table[i].exc_size);
27895b24072Sshige }
27995b24072Sshige
28095b24072Sshige __syncicache((void *)EXC_RST, EXC_LAST - EXC_RST + 0x100);
281dba36e03Smatt
28295b24072Sshige mtspr(SPR_EVPR, 0); /* Set Exception vector base */
28395b24072Sshige
28495b24072Sshige /* Handle trap instruction as PGM exception */
285dba36e03Smatt mtspr(SPR_DBCR0, mfspr(SPR_DBCR0) & ~DBCR0_TDE);
28695b24072Sshige
28795b24072Sshige /*
28895b24072Sshige * external interrupt handler install
28995b24072Sshige */
29095b24072Sshige if (handler)
29195b24072Sshige ibm4xx_install_extint(handler);
29295b24072Sshige
29395b24072Sshige /*
29495b24072Sshige * Now enable translation (and machine checks/recoverable interrupts).
29595b24072Sshige */
2962d65de24Sperry __asm volatile ("mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
29795b24072Sshige : : "r" (0), "K" (PSL_IR|PSL_DR));
29895b24072Sshige /* XXXX PSL_ME - With ME set kernel gets stuck... */
29995b24072Sshige
300dba36e03Smatt /*
301dba36e03Smatt * turn on console after enable translation
302dba36e03Smatt */
303dba36e03Smatt consinit();
304dba36e03Smatt
305e08917efScherry uvm_md_init();
306dba36e03Smatt
307dba36e03Smatt /*
308dba36e03Smatt * Initialize pmap module.
309dba36e03Smatt */
310dba36e03Smatt pmap_bootstrap(startkernel, endkernel);
311dba36e03Smatt
312dba36e03Smatt /*
313dba36e03Smatt * Let's take all the indirect calls via our stubs and patch
314dba36e03Smatt * them to be direct calls.
315dba36e03Smatt */
316dba36e03Smatt cpu_fixup_stubs();
317dba36e03Smatt
318dba36e03Smatt #if NKSYMS || defined(DDB) || defined(MODULAR)
319dba36e03Smatt ksyms_addsyms_elf((uintptr_t)endsym - (uintptr_t)startsym,
320dba36e03Smatt startsym, endsym);
321dba36e03Smatt #endif
32295b24072Sshige }
32395b24072Sshige
32495b24072Sshige void
ibm4xx_install_extint(void (* handler)(void))32595b24072Sshige ibm4xx_install_extint(void (*handler)(void))
32695b24072Sshige {
32795b24072Sshige extern int extint, extsize;
32895b24072Sshige extern u_long extint_call;
32995b24072Sshige u_long offset = (u_long)handler - (u_long)&extint_call;
33095b24072Sshige int msr;
33195b24072Sshige
33295b24072Sshige #ifdef DIAGNOSTIC
33395b24072Sshige if (offset > 0x1ffffff)
33495b24072Sshige panic("install_extint: too far away");
33595b24072Sshige #endif
3362d65de24Sperry __asm volatile ("mfmsr %0; wrteei 0" : "=r" (msr));
33795b24072Sshige extint_call = (extint_call & 0xfc000003) | offset;
33895b24072Sshige memcpy((void *)EXC_EXI, &extint, (size_t)&extsize);
33995b24072Sshige __syncicache((void *)&extint_call, sizeof extint_call);
34095b24072Sshige __syncicache((void *)EXC_EXI, (int)&extsize);
3412d65de24Sperry __asm volatile ("mtmsr %0" :: "r" (msr));
34295b24072Sshige }
34395b24072Sshige
34495b24072Sshige /*
34595b24072Sshige * ibm4xx_cpu_startup:
34695b24072Sshige * Machine dependent startup code.
34795b24072Sshige */
34895b24072Sshige void
ibm4xx_cpu_startup(const char * model)34995b24072Sshige ibm4xx_cpu_startup(const char *model)
35095b24072Sshige {
35195b24072Sshige vaddr_t minaddr, maxaddr;
35295b24072Sshige char pbuf[9];
35395b24072Sshige
35495b24072Sshige KASSERT(curcpu() != NULL);
35595b24072Sshige KASSERT(lwp0.l_cpu != NULL);
35695b24072Sshige KASSERT(curcpu()->ci_intstk != 0);
357b8ea2c8cSmatt KASSERT(curcpu()->ci_idepth == -1);
35895b24072Sshige
35995b24072Sshige /*
36095b24072Sshige * Initialize error message buffer (at end of core).
36195b24072Sshige */
36295b24072Sshige #if 0 /* For some reason this fails... --Artem
36395b24072Sshige * Besides, do we really have to put it at the end of core?
36495b24072Sshige * Let's use static buffer for now
36595b24072Sshige */
3666b2d8b66Syamt if (!(msgbuf_vaddr = uvm_km_alloc(kernel_map, round_page(MSGBUFSIZE),
3676b2d8b66Syamt 0, UVM_KMF_VAONLY)))
36895b24072Sshige panic("startup: no room for message buffer");
36995b24072Sshige for (i = 0; i < btoc(MSGBUFSIZE); i++)
37095b24072Sshige pmap_kenter_pa(msgbuf_vaddr + i * PAGE_SIZE,
3719480c51bScegger msgbuf_paddr + i * PAGE_SIZE,
3729480c51bScegger VM_PROT_READ|VM_PROT_WRITE, 0);
37353524e44Schristos initmsgbuf((void *)msgbuf_vaddr, round_page(MSGBUFSIZE));
37495b24072Sshige #else
37553524e44Schristos initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE));
37695b24072Sshige #endif
37795b24072Sshige
3783fd1802eSlukem printf("%s%s", copyright, version);
37995b24072Sshige if (model != NULL)
38095b24072Sshige printf("Model: %s\n", model);
38195b24072Sshige
38295b24072Sshige format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
38395b24072Sshige printf("total memory = %s\n", pbuf);
38495b24072Sshige
38595b24072Sshige minaddr = 0;
38695b24072Sshige /*
38795b24072Sshige * Allocate a submap for physio
38895b24072Sshige */
38995b24072Sshige phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
390c8daa94cSthorpej VM_PHYS_SIZE, 0, false, NULL);
39195b24072Sshige
39295b24072Sshige /*
39395b24072Sshige * No need to allocate an mbuf cluster submap. Mbuf clusters
39495b24072Sshige * are allocated via the pool allocator, and we use direct-mapped
39595b24072Sshige * pool pages.
39695b24072Sshige */
39795b24072Sshige
3984b8a875aSad format_bytes(pbuf, sizeof(pbuf), ptoa(uvm_availmem(false)));
39995b24072Sshige printf("avail memory = %s\n", pbuf);
40095b24072Sshige }
40195b24072Sshige
40295b24072Sshige /*
40395b24072Sshige * ibm4xx_dumpsys:
40495b24072Sshige * Crash dump handling.
40595b24072Sshige */
40695b24072Sshige void
ibm4xx_dumpsys(void)40795b24072Sshige ibm4xx_dumpsys(void)
40895b24072Sshige {
40995b24072Sshige
41095b24072Sshige printf("dumpsys: TBD\n");
41195b24072Sshige }
412