xref: /netbsd-src/sys/arch/evbppc/virtex/machdep.c (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
1 /*	$NetBSD: machdep.c,v 1.30 2021/08/03 09:25:44 rin 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.30 2021/08/03 09:25:44 rin 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/malloc.h>
51 #include <sys/mbuf.h>
52 #include <sys/module.h>
53 #include <sys/mount.h>
54 #include <sys/msgbuf.h>
55 #include <sys/kernel.h>
56 #include <sys/ksyms.h>
57 #include <sys/proc.h>
58 #include <sys/reboot.h>
59 #include <sys/syscallargs.h>
60 #include <sys/syslog.h>
61 #include <sys/systm.h>
62 
63 #include <uvm/uvm_extern.h>
64 
65 #include <dev/cons.h>
66 
67 #include <machine/powerpc.h>
68 
69 #include <powerpc/trap.h>
70 #include <powerpc/pcb.h>
71 
72 #include <powerpc/spr.h>
73 #include <powerpc/ibm4xx/spr.h>
74 
75 #include <powerpc/ibm4xx/cpu.h>
76 
77 #include <evbppc/virtex/dcr.h>
78 #include <evbppc/virtex/virtex.h>
79 
80 #include "ksyms.h"
81 
82 #if defined(DDB)
83 #include <powerpc/db_machdep.h>
84 #include <ddb/db_extern.h>
85 #endif
86 
87 #if defined(KGDB)
88 #include <sys/kgdb.h>
89 #endif
90 
91 void initppc(vaddr_t, vaddr_t);
92 
93 /* BSS segment start & end. */
94 extern char 		edata[], end[];
95 
96 /* One region holds all memory, the other is terminator expected by 405 pmap. */
97 #define MEMREGIONS 	2
98 struct mem_region 	physmemr[MEMREGIONS];
99 struct mem_region 	availmemr[MEMREGIONS];
100 
101 /* Maximum TLB page size. */
102 #define TLB_PG_SIZE 	(16*1024*1024)
103 
104 void
105 initppc(vaddr_t startkernel, vaddr_t endkernel)
106 {
107 	paddr_t			addr, memsize;
108 
109         /* Initialize cache info for memcpy, memset, etc. */
110         cpu_probe_cache();
111 
112 	memset(physmemr, 0, sizeof(physmemr)); 		/* [1].size = 0 */
113 	memset(availmemr, 0, sizeof(availmemr)); 	/* [1].size = 0 */
114 
115 	memsize = (PHYSMEM * 1024 * 1024) & ~PGOFSET;
116 
117 	physmemr[0].start 	= 0;
118 	physmemr[0].size 	= memsize;
119 
120 	availmemr[0].start 	= startkernel;
121 	availmemr[0].size 	= memsize - availmemr[0].start;
122 
123 	/* Map kernel memory linearly. */
124 	for (addr = 0; addr < endkernel; addr += TLB_PG_SIZE)
125 		ppc4xx_tlb_reserve(addr, addr, TLB_PG_SIZE, TLB_EX);
126 
127 	/* Give design-specific code a hint for reserved mappings. */
128 	virtex_machdep_init(roundup(memsize, TLB_PG_SIZE), TLB_PG_SIZE,
129 	    physmemr, availmemr);
130 
131 	ibm4xx_init(startkernel, endkernel, pic_ext_intr);
132 
133 #ifdef DDB
134 	if (boothowto & RB_KDB)
135 		Debugger();
136 #endif
137 
138 #ifdef KGDB
139 	/*
140 	 * Now trap to KGDB
141 	 */
142 	kgdb_connect(1);
143 #endif /* KGDB */
144 }
145 
146 /*
147  * Machine dependent startup code.
148  */
149 
150 void
151 cpu_startup(void)
152 {
153 	/* For use by propdb. */
154 	static u_int 	memsize = PHYSMEM * 1024 * 1024;
155 	static u_int 	cpuspeed = CPUFREQ * 1000 * 1000;
156 	prop_number_t 	pn;
157 
158 	vaddr_t 	minaddr, maxaddr;
159 	char 		pbuf[9];
160 
161 	curcpu()->ci_khz = cpuspeed / 1000;
162 
163 	/* Initialize error message buffer. */
164 	initmsgbuf((void *)msgbuf, round_page(MSGBUFSIZE));
165 
166 	printf("%s%s", copyright, version);
167 
168 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
169 	printf("total memory = %s\n", pbuf);
170 
171 	minaddr = 0;
172 	/*
173 	 * Allocate a submap for physio
174 	 */
175 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
176 				 VM_PHYS_SIZE, 0, false, NULL);
177 
178 	/*
179 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
180 	 * are allocated via the pool allocator, and we use direct-mapped
181 	 * pool pages.
182 	 */
183 
184 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvm_availmem(false)));
185 	printf("avail memory = %s\n", pbuf);
186 
187 	/*
188 	 * Set up the board properties database.
189 	 */
190 	board_info_init();
191 
192 	pn = prop_number_create_integer(memsize);
193 	KASSERT(pn != NULL);
194 	if (prop_dictionary_set(board_properties, "mem-size", pn) == false)
195 		panic("setting mem-size");
196 	prop_object_release(pn);
197 
198 	pn = prop_number_create_integer(cpuspeed);
199 	KASSERT(pn != NULL);
200 	if (prop_dictionary_set(board_properties, "processor-frequency",
201 	    pn) == false)
202 		panic("setting processor-frequency");
203 	prop_object_release(pn);
204 
205 	/*
206 	 * Now that we have VM, malloc()s are OK in bus_space.
207 	 */
208 	bus_space_mallocok();
209 	fake_mapiodev = 0;
210 }
211 
212 /* Hook used by 405 pmap module. */
213 void
214 mem_regions(struct mem_region **mem, struct mem_region **avail)
215 {
216 	*mem 	= physmemr;
217 	*avail 	= availmemr;
218 }
219