xref: /netbsd-src/sys/arch/evbppc/virtex/machdep.c (revision 7433666e375b3ac4cc764df5a6726be98bc1cdd5)
1 /*	$NetBSD: machdep.c,v 1.31 2023/12/20 14:18:37 thorpej Exp $ */
2 
3 /*
4  * Copyright (c) 2006 Jachym Holecek
5  * All rights reserved.
6  *
7  * Written for DFC Design, s.r.o.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Based on Walnut and Explora.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.31 2023/12/20 14:18:37 thorpej Exp $");
38 
39 #include "opt_compat_netbsd.h"
40 #include "opt_ddb.h"
41 #include "opt_virtex.h"
42 #include "opt_kgdb.h"
43 
44 #include <sys/param.h>
45 #include <sys/boot_flag.h>
46 #include <sys/buf.h>
47 #include <sys/bus.h>
48 #include <sys/device.h>
49 #include <sys/exec.h>
50 #include <sys/mbuf.h>
51 #include <sys/module.h>
52 #include <sys/mount.h>
53 #include <sys/msgbuf.h>
54 #include <sys/kernel.h>
55 #include <sys/ksyms.h>
56 #include <sys/proc.h>
57 #include <sys/reboot.h>
58 #include <sys/syscallargs.h>
59 #include <sys/syslog.h>
60 #include <sys/systm.h>
61 
62 #include <uvm/uvm_extern.h>
63 
64 #include <dev/cons.h>
65 
66 #include <machine/powerpc.h>
67 
68 #include <powerpc/trap.h>
69 #include <powerpc/pcb.h>
70 
71 #include <powerpc/spr.h>
72 #include <powerpc/ibm4xx/spr.h>
73 
74 #include <powerpc/ibm4xx/cpu.h>
75 
76 #include <evbppc/virtex/dcr.h>
77 #include <evbppc/virtex/virtex.h>
78 
79 #include "ksyms.h"
80 
81 #if defined(DDB)
82 #include <powerpc/db_machdep.h>
83 #include <ddb/db_extern.h>
84 #endif
85 
86 #if defined(KGDB)
87 #include <sys/kgdb.h>
88 #endif
89 
90 void initppc(vaddr_t, vaddr_t);
91 
92 /* BSS segment start & end. */
93 extern char 		edata[], end[];
94 
95 /* One region holds all memory, the other is terminator expected by 405 pmap. */
96 #define MEMREGIONS 	2
97 struct mem_region 	physmemr[MEMREGIONS];
98 struct mem_region 	availmemr[MEMREGIONS];
99 
100 /* Maximum TLB page size. */
101 #define TLB_PG_SIZE 	(16*1024*1024)
102 
103 void
initppc(vaddr_t startkernel,vaddr_t endkernel)104 initppc(vaddr_t startkernel, vaddr_t endkernel)
105 {
106 	paddr_t			addr, memsize;
107 
108         /* Initialize cache info for memcpy, memset, etc. */
109         cpu_probe_cache();
110 
111 	memset(physmemr, 0, sizeof(physmemr)); 		/* [1].size = 0 */
112 	memset(availmemr, 0, sizeof(availmemr)); 	/* [1].size = 0 */
113 
114 	memsize = (PHYSMEM * 1024 * 1024) & ~PGOFSET;
115 
116 	physmemr[0].start 	= 0;
117 	physmemr[0].size 	= memsize;
118 
119 	availmemr[0].start 	= startkernel;
120 	availmemr[0].size 	= memsize - availmemr[0].start;
121 
122 	/* Map kernel memory linearly. */
123 	for (addr = 0; addr < endkernel; addr += TLB_PG_SIZE)
124 		ppc4xx_tlb_reserve(addr, addr, TLB_PG_SIZE, TLB_EX);
125 
126 	/* Give design-specific code a hint for reserved mappings. */
127 	virtex_machdep_init(roundup(memsize, TLB_PG_SIZE), TLB_PG_SIZE,
128 	    physmemr, availmemr);
129 
130 	ibm4xx_init(startkernel, endkernel, pic_ext_intr);
131 
132 #ifdef DDB
133 	if (boothowto & RB_KDB)
134 		Debugger();
135 #endif
136 
137 #ifdef KGDB
138 	/*
139 	 * Now trap to KGDB
140 	 */
141 	kgdb_connect(1);
142 #endif /* KGDB */
143 }
144 
145 /*
146  * Machine dependent startup code.
147  */
148 
149 void
cpu_startup(void)150 cpu_startup(void)
151 {
152 	/* For use by propdb. */
153 	static u_int 	memsize = PHYSMEM * 1024 * 1024;
154 	static u_int 	cpuspeed = CPUFREQ * 1000 * 1000;
155 	prop_number_t 	pn;
156 
157 	vaddr_t 	minaddr, maxaddr;
158 	char 		pbuf[9];
159 
160 	curcpu()->ci_khz = cpuspeed / 1000;
161 
162 	/* Initialize error message buffer. */
163 	initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE));
164 
165 	printf("%s%s", copyright, version);
166 
167 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
168 	printf("total memory = %s\n", pbuf);
169 
170 	minaddr = 0;
171 	/*
172 	 * Allocate a submap for physio
173 	 */
174 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
175 				 VM_PHYS_SIZE, 0, false, NULL);
176 
177 	/*
178 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
179 	 * are allocated via the pool allocator, and we use direct-mapped
180 	 * pool pages.
181 	 */
182 
183 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvm_availmem(false)));
184 	printf("avail memory = %s\n", pbuf);
185 
186 	/*
187 	 * Set up the board properties database.
188 	 */
189 	board_info_init();
190 
191 	pn = prop_number_create_integer(memsize);
192 	KASSERT(pn != NULL);
193 	if (prop_dictionary_set(board_properties, "mem-size", pn) == false)
194 		panic("setting mem-size");
195 	prop_object_release(pn);
196 
197 	pn = prop_number_create_integer(cpuspeed);
198 	KASSERT(pn != NULL);
199 	if (prop_dictionary_set(board_properties, "processor-frequency",
200 	    pn) == false)
201 		panic("setting processor-frequency");
202 	prop_object_release(pn);
203 
204 	/*
205 	 * Now that we have VM, malloc()s are OK in bus_space.
206 	 */
207 	bus_space_mallocok();
208 	fake_mapiodev = 0;
209 }
210 
211 /* Hook used by 405 pmap module. */
212 void
mem_regions(struct mem_region ** mem,struct mem_region ** avail)213 mem_regions(struct mem_region **mem, struct mem_region **avail)
214 {
215 	*mem 	= physmemr;
216 	*avail 	= availmemr;
217 }
218