xref: /freebsd/sys/powerpc/ofw/ofw_machdep.c (revision 35feca37)
1fe3b4685SNathan Whitehorn /*-
271e3c308SPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
371e3c308SPedro F. Giffuni  *
4fe3b4685SNathan Whitehorn  * Copyright (C) 1996 Wolfgang Solfrank.
5fe3b4685SNathan Whitehorn  * Copyright (C) 1996 TooLs GmbH.
6fe3b4685SNathan Whitehorn  * All rights reserved.
7fe3b4685SNathan Whitehorn  *
8fe3b4685SNathan Whitehorn  * Redistribution and use in source and binary forms, with or without
9fe3b4685SNathan Whitehorn  * modification, are permitted provided that the following conditions
10fe3b4685SNathan Whitehorn  * are met:
11fe3b4685SNathan Whitehorn  * 1. Redistributions of source code must retain the above copyright
12fe3b4685SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer.
13fe3b4685SNathan Whitehorn  * 2. Redistributions in binary form must reproduce the above copyright
14fe3b4685SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer in the
15fe3b4685SNathan Whitehorn  *    documentation and/or other materials provided with the distribution.
16fe3b4685SNathan Whitehorn  * 3. All advertising materials mentioning features or use of this software
17fe3b4685SNathan Whitehorn  *    must display the following acknowledgement:
18fe3b4685SNathan Whitehorn  *	This product includes software developed by TooLs GmbH.
19fe3b4685SNathan Whitehorn  * 4. The name of TooLs GmbH may not be used to endorse or promote products
20fe3b4685SNathan Whitehorn  *    derived from this software without specific prior written permission.
21fe3b4685SNathan Whitehorn  *
22fe3b4685SNathan Whitehorn  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23fe3b4685SNathan Whitehorn  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24fe3b4685SNathan Whitehorn  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25fe3b4685SNathan Whitehorn  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26fe3b4685SNathan Whitehorn  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27fe3b4685SNathan Whitehorn  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28fe3b4685SNathan Whitehorn  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29fe3b4685SNathan Whitehorn  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30fe3b4685SNathan Whitehorn  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31fe3b4685SNathan Whitehorn  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32fe3b4685SNathan Whitehorn  *
33fe3b4685SNathan Whitehorn  * $NetBSD: ofw_machdep.c,v 1.5 2000/05/23 13:25:43 tsubai Exp $
34fe3b4685SNathan Whitehorn  */
35fe3b4685SNathan Whitehorn 
36fe3b4685SNathan Whitehorn #include <sys/cdefs.h>
37fe3b4685SNathan Whitehorn __FBSDID("$FreeBSD$");
38fe3b4685SNathan Whitehorn 
398c092157SJustin Hibbits #include "opt_platform.h"
40fe3b4685SNathan Whitehorn #include <sys/param.h>
41fe3b4685SNathan Whitehorn #include <sys/bus.h>
42fe3b4685SNathan Whitehorn #include <sys/systm.h>
43fe3b4685SNathan Whitehorn #include <sys/conf.h>
44fe3b4685SNathan Whitehorn #include <sys/disk.h>
45fe3b4685SNathan Whitehorn #include <sys/fcntl.h>
46fe3b4685SNathan Whitehorn #include <sys/malloc.h>
47fe3b4685SNathan Whitehorn #include <sys/smp.h>
48fe3b4685SNathan Whitehorn #include <sys/stat.h>
49b9d056f3SNathan Whitehorn #include <sys/endian.h>
50fe3b4685SNathan Whitehorn 
51fe3b4685SNathan Whitehorn #include <net/ethernet.h>
52fe3b4685SNathan Whitehorn 
538c092157SJustin Hibbits #include <dev/fdt/fdt_common.h>
54fe3b4685SNathan Whitehorn #include <dev/ofw/openfirm.h>
55fe3b4685SNathan Whitehorn #include <dev/ofw/ofw_pci.h>
56fe3b4685SNathan Whitehorn #include <dev/ofw/ofw_bus.h>
57bc7b9300SIan Lepore #include <dev/ofw/ofw_subr.h>
58fe3b4685SNathan Whitehorn 
59fe3b4685SNathan Whitehorn #include <vm/vm.h>
60fe3b4685SNathan Whitehorn #include <vm/vm_param.h>
61fe3b4685SNathan Whitehorn #include <vm/vm_page.h>
62fe3b4685SNathan Whitehorn 
63fe3b4685SNathan Whitehorn #include <machine/bus.h>
64fe3b4685SNathan Whitehorn #include <machine/cpu.h>
65fe3b4685SNathan Whitehorn #include <machine/md_var.h>
66fe3b4685SNathan Whitehorn #include <machine/platform.h>
67fe3b4685SNathan Whitehorn #include <machine/ofw_machdep.h>
68f367ffdeSAndreas Tobler #include <machine/trap.h>
69fe3b4685SNathan Whitehorn 
70c0650b2fSNathan Whitehorn #include <contrib/libfdt/libfdt.h>
71c0650b2fSNathan Whitehorn 
726f489d43SJustin Hibbits static void	*fdt;
736f489d43SJustin Hibbits int		ofw_real_mode;
746f489d43SJustin Hibbits 
75629aa519SNathan Whitehorn #ifdef AIM
76fe3b4685SNathan Whitehorn extern register_t ofmsr[5];
7717879090SNathan Whitehorn extern void	*openfirmware_entry;
78bb808254SNathan Whitehorn char		save_trap_init[0x2f00];          /* EXC_LAST */
79f367ffdeSAndreas Tobler char		save_trap_of[0x2f00];            /* EXC_LAST */
80fe3b4685SNathan Whitehorn 
81d8c6808aSNathan Whitehorn int		ofwcall(void *);
82fe3b4685SNathan Whitehorn static int	openfirmware(void *args);
83fe3b4685SNathan Whitehorn 
84f367ffdeSAndreas Tobler __inline void
85f367ffdeSAndreas Tobler ofw_save_trap_vec(char *save_trap_vec)
86f367ffdeSAndreas Tobler {
87f9edb09dSNathan Whitehorn 	if (!ofw_real_mode || !hw_direct_map)
88f367ffdeSAndreas Tobler                 return;
89f367ffdeSAndreas Tobler 
90f9edb09dSNathan Whitehorn 	bcopy((void *)PHYS_TO_DMAP(EXC_RST), save_trap_vec, EXC_LAST - EXC_RST);
91f367ffdeSAndreas Tobler }
92f367ffdeSAndreas Tobler 
93f367ffdeSAndreas Tobler static __inline void
94f367ffdeSAndreas Tobler ofw_restore_trap_vec(char *restore_trap_vec)
95f367ffdeSAndreas Tobler {
96f9edb09dSNathan Whitehorn 	if (!ofw_real_mode || !hw_direct_map)
97f367ffdeSAndreas Tobler                 return;
98f367ffdeSAndreas Tobler 
99f9edb09dSNathan Whitehorn 	bcopy(restore_trap_vec, (void *)PHYS_TO_DMAP(EXC_RST),
100f9edb09dSNathan Whitehorn 	    EXC_LAST - EXC_RST);
101f9edb09dSNathan Whitehorn 	__syncicache((void *)PHYS_TO_DMAP(EXC_RSVD), EXC_LAST - EXC_RSVD);
102f367ffdeSAndreas Tobler }
1034aa3cee6SNathan Whitehorn 
1044aa3cee6SNathan Whitehorn /*
1054aa3cee6SNathan Whitehorn  * Saved SPRG0-3 from OpenFirmware. Will be restored prior to the callback.
1064aa3cee6SNathan Whitehorn  */
1074aa3cee6SNathan Whitehorn register_t	ofw_sprg0_save;
1084aa3cee6SNathan Whitehorn 
1094aa3cee6SNathan Whitehorn static __inline void
1104aa3cee6SNathan Whitehorn ofw_sprg_prepare(void)
1114aa3cee6SNathan Whitehorn {
1124aa3cee6SNathan Whitehorn 	if (ofw_real_mode)
1134aa3cee6SNathan Whitehorn 		return;
1144aa3cee6SNathan Whitehorn 
1154aa3cee6SNathan Whitehorn 	/*
1164aa3cee6SNathan Whitehorn 	 * Assume that interrupt are disabled at this point, or
1174aa3cee6SNathan Whitehorn 	 * SPRG1-3 could be trashed
1184aa3cee6SNathan Whitehorn 	 */
11962c6b30eSJustin Hibbits #ifdef __powerpc64__
12062c6b30eSJustin Hibbits 	__asm __volatile("mtsprg1 %0\n\t"
12162c6b30eSJustin Hibbits 	    		 "mtsprg2 %1\n\t"
12262c6b30eSJustin Hibbits 			 "mtsprg3 %2\n\t"
12362c6b30eSJustin Hibbits 			 :
12462c6b30eSJustin Hibbits 			 : "r"(ofmsr[2]),
12562c6b30eSJustin Hibbits 			 "r"(ofmsr[3]),
12662c6b30eSJustin Hibbits 			 "r"(ofmsr[4]));
12762c6b30eSJustin Hibbits #else
1284aa3cee6SNathan Whitehorn 	__asm __volatile("mfsprg0 %0\n\t"
1294aa3cee6SNathan Whitehorn 			 "mtsprg0 %1\n\t"
1304aa3cee6SNathan Whitehorn 	    		 "mtsprg1 %2\n\t"
1314aa3cee6SNathan Whitehorn 	    		 "mtsprg2 %3\n\t"
1324aa3cee6SNathan Whitehorn 			 "mtsprg3 %4\n\t"
1334aa3cee6SNathan Whitehorn 			 : "=&r"(ofw_sprg0_save)
1344aa3cee6SNathan Whitehorn 			 : "r"(ofmsr[1]),
1354aa3cee6SNathan Whitehorn 			 "r"(ofmsr[2]),
1364aa3cee6SNathan Whitehorn 			 "r"(ofmsr[3]),
1374aa3cee6SNathan Whitehorn 			 "r"(ofmsr[4]));
13862c6b30eSJustin Hibbits #endif
1394aa3cee6SNathan Whitehorn }
1404aa3cee6SNathan Whitehorn 
1414aa3cee6SNathan Whitehorn static __inline void
1424aa3cee6SNathan Whitehorn ofw_sprg_restore(void)
1434aa3cee6SNathan Whitehorn {
1444aa3cee6SNathan Whitehorn 	if (ofw_real_mode)
1454aa3cee6SNathan Whitehorn 		return;
1464aa3cee6SNathan Whitehorn 
1474aa3cee6SNathan Whitehorn 	/*
1484aa3cee6SNathan Whitehorn 	 * Note that SPRG1-3 contents are irrelevant. They are scratch
1494aa3cee6SNathan Whitehorn 	 * registers used in the early portion of trap handling when
1504aa3cee6SNathan Whitehorn 	 * interrupts are disabled.
1514aa3cee6SNathan Whitehorn 	 *
1524aa3cee6SNathan Whitehorn 	 * PCPU data cannot be used until this routine is called !
1534aa3cee6SNathan Whitehorn 	 */
15462c6b30eSJustin Hibbits #ifndef __powerpc64__
1554aa3cee6SNathan Whitehorn 	__asm __volatile("mtsprg0 %0" :: "r"(ofw_sprg0_save));
15662c6b30eSJustin Hibbits #endif
1574aa3cee6SNathan Whitehorn }
158c7291bdcSNathan Whitehorn #endif
159f367ffdeSAndreas Tobler 
160fe3b4685SNathan Whitehorn static int
161fe3b4685SNathan Whitehorn parse_ofw_memory(phandle_t node, const char *prop, struct mem_region *output)
162fe3b4685SNathan Whitehorn {
163fe3b4685SNathan Whitehorn 	cell_t address_cells, size_cells;
164d8c6808aSNathan Whitehorn 	cell_t OFmem[4 * PHYS_AVAIL_SZ];
165fe3b4685SNathan Whitehorn 	int sz, i, j;
166fe3b4685SNathan Whitehorn 	phandle_t phandle;
167fe3b4685SNathan Whitehorn 
168fe3b4685SNathan Whitehorn 	sz = 0;
169fe3b4685SNathan Whitehorn 
170fe3b4685SNathan Whitehorn 	/*
171fe3b4685SNathan Whitehorn 	 * Get #address-cells from root node, defaulting to 1 if it cannot
172fe3b4685SNathan Whitehorn 	 * be found.
173fe3b4685SNathan Whitehorn 	 */
174fe3b4685SNathan Whitehorn 	phandle = OF_finddevice("/");
175509142e1SNathan Whitehorn 	if (OF_getencprop(phandle, "#address-cells", &address_cells,
1768bab0d80SNathan Whitehorn 	    sizeof(address_cells)) < (ssize_t)sizeof(address_cells))
177fe3b4685SNathan Whitehorn 		address_cells = 1;
178509142e1SNathan Whitehorn 	if (OF_getencprop(phandle, "#size-cells", &size_cells,
1798bab0d80SNathan Whitehorn 	    sizeof(size_cells)) < (ssize_t)sizeof(size_cells))
180fe3b4685SNathan Whitehorn 		size_cells = 1;
181fe3b4685SNathan Whitehorn 
182fe3b4685SNathan Whitehorn 	/*
183fe3b4685SNathan Whitehorn 	 * Get memory.
184fe3b4685SNathan Whitehorn 	 */
185509142e1SNathan Whitehorn 	if (node == -1 || (sz = OF_getencprop(node, prop,
186d8c6808aSNathan Whitehorn 	    OFmem, sizeof(OFmem))) <= 0)
187fe3b4685SNathan Whitehorn 		panic("Physical memory map not found");
188fe3b4685SNathan Whitehorn 
189fe3b4685SNathan Whitehorn 	i = 0;
190fe3b4685SNathan Whitehorn 	j = 0;
191fe3b4685SNathan Whitehorn 	while (i < sz/sizeof(cell_t)) {
192fe3b4685SNathan Whitehorn 		output[j].mr_start = OFmem[i++];
193fe3b4685SNathan Whitehorn 		if (address_cells == 2) {
194fe3b4685SNathan Whitehorn 			output[j].mr_start <<= 32;
195fe3b4685SNathan Whitehorn 			output[j].mr_start += OFmem[i++];
196fe3b4685SNathan Whitehorn 		}
197fe3b4685SNathan Whitehorn 
198fe3b4685SNathan Whitehorn 		output[j].mr_size = OFmem[i++];
199fe3b4685SNathan Whitehorn 		if (size_cells == 2) {
200fe3b4685SNathan Whitehorn 			output[j].mr_size <<= 32;
201fe3b4685SNathan Whitehorn 			output[j].mr_size += OFmem[i++];
202fe3b4685SNathan Whitehorn 		}
203fe3b4685SNathan Whitehorn 
204bb7137e1SJustin Hibbits 		if (output[j].mr_start > BUS_SPACE_MAXADDR)
205bb7137e1SJustin Hibbits 			continue;
206bb7137e1SJustin Hibbits 
207fe3b4685SNathan Whitehorn 		/*
208bb7137e1SJustin Hibbits 		 * Constrain memory to that which we can access.
209bb7137e1SJustin Hibbits 		 * 32-bit AIM can only reference 32 bits of address currently,
210bb7137e1SJustin Hibbits 		 * but Book-E can access 36 bits.
211fe3b4685SNathan Whitehorn 		 */
212fe3b4685SNathan Whitehorn 		if (((uint64_t)output[j].mr_start +
213bb7137e1SJustin Hibbits 		    (uint64_t)output[j].mr_size - 1) >
214bb7137e1SJustin Hibbits 		    BUS_SPACE_MAXADDR) {
215bb7137e1SJustin Hibbits 			output[j].mr_size = BUS_SPACE_MAXADDR -
216bb7137e1SJustin Hibbits 			    output[j].mr_start + 1;
217fe3b4685SNathan Whitehorn 		}
218fe3b4685SNathan Whitehorn 
219fe3b4685SNathan Whitehorn 		j++;
220fe3b4685SNathan Whitehorn 	}
221fe3b4685SNathan Whitehorn 	sz = j*sizeof(output[0]);
222fe3b4685SNathan Whitehorn 
223fe3b4685SNathan Whitehorn 	return (sz);
224fe3b4685SNathan Whitehorn }
225fe3b4685SNathan Whitehorn 
22635feca37SNathan Whitehorn #ifdef FDT
227b9d056f3SNathan Whitehorn static int
228b9d056f3SNathan Whitehorn excise_fdt_reserved(struct mem_region *avail, int asz)
229b9d056f3SNathan Whitehorn {
230b9d056f3SNathan Whitehorn 	struct {
231b9d056f3SNathan Whitehorn 		uint64_t address;
232b9d056f3SNathan Whitehorn 		uint64_t size;
233b9d056f3SNathan Whitehorn 	} fdtmap[16];
234b9d056f3SNathan Whitehorn 	ssize_t fdtmapsize;
235b9d056f3SNathan Whitehorn 	phandle_t chosen;
236b9d056f3SNathan Whitehorn 	int i, j, k;
237b9d056f3SNathan Whitehorn 
238b9d056f3SNathan Whitehorn 	chosen = OF_finddevice("/chosen");
239b9d056f3SNathan Whitehorn 	fdtmapsize = OF_getprop(chosen, "fdtmemreserv", fdtmap, sizeof(fdtmap));
240b9d056f3SNathan Whitehorn 
241b9d056f3SNathan Whitehorn 	for (j = 0; j < fdtmapsize/sizeof(fdtmap[0]); j++) {
242c0650b2fSNathan Whitehorn 		fdtmap[j].address = be64toh(fdtmap[j].address) & ~PAGE_MASK;
243c0650b2fSNathan Whitehorn 		fdtmap[j].size = round_page(be64toh(fdtmap[j].size));
244c0650b2fSNathan Whitehorn 	}
245c0650b2fSNathan Whitehorn 
246c0650b2fSNathan Whitehorn 	KASSERT(j*sizeof(fdtmap[0]) < sizeof(fdtmap),
247c0650b2fSNathan Whitehorn 	    ("Exceeded number of FDT reservations"));
248c0650b2fSNathan Whitehorn 	/* Add a virtual entry for the FDT itself */
249c0650b2fSNathan Whitehorn 	if (fdt != NULL) {
250c0650b2fSNathan Whitehorn 		fdtmap[j].address = (vm_offset_t)fdt & ~PAGE_MASK;
251c0650b2fSNathan Whitehorn 		fdtmap[j].size = round_page(fdt_totalsize(fdt));
252c0650b2fSNathan Whitehorn 		fdtmapsize += sizeof(fdtmap[0]);
253b9d056f3SNathan Whitehorn 	}
254b9d056f3SNathan Whitehorn 
255b9d056f3SNathan Whitehorn 	for (i = 0; i < asz; i++) {
256b9d056f3SNathan Whitehorn 		for (j = 0; j < fdtmapsize/sizeof(fdtmap[0]); j++) {
257b9d056f3SNathan Whitehorn 			/*
258b9d056f3SNathan Whitehorn 			 * Case 1: Exclusion region encloses complete
259b9d056f3SNathan Whitehorn 			 * available entry. Drop it and move on.
260b9d056f3SNathan Whitehorn 			 */
261b9d056f3SNathan Whitehorn 			if (fdtmap[j].address <= avail[i].mr_start &&
262b9d056f3SNathan Whitehorn 			    fdtmap[j].address + fdtmap[j].size >=
263b9d056f3SNathan Whitehorn 			    avail[i].mr_start + avail[i].mr_size) {
264b9d056f3SNathan Whitehorn 				for (k = i+1; k < asz; k++)
265b9d056f3SNathan Whitehorn 					avail[k-1] = avail[k];
266b9d056f3SNathan Whitehorn 				asz--;
267b9d056f3SNathan Whitehorn 				i--; /* Repeat some entries */
268b9d056f3SNathan Whitehorn 				continue;
269b9d056f3SNathan Whitehorn 			}
270b9d056f3SNathan Whitehorn 
271b9d056f3SNathan Whitehorn 			/*
272b9d056f3SNathan Whitehorn 			 * Case 2: Exclusion region starts in available entry.
273b9d056f3SNathan Whitehorn 			 * Trim it to where the entry begins and append
274b9d056f3SNathan Whitehorn 			 * a new available entry with the region after
275b9d056f3SNathan Whitehorn 			 * the excluded region, if any.
276b9d056f3SNathan Whitehorn 			 */
277b9d056f3SNathan Whitehorn 			if (fdtmap[j].address >= avail[i].mr_start &&
278b9d056f3SNathan Whitehorn 			    fdtmap[j].address < avail[i].mr_start +
279b9d056f3SNathan Whitehorn 			    avail[i].mr_size) {
280b9d056f3SNathan Whitehorn 				if (fdtmap[j].address + fdtmap[j].size <
281b9d056f3SNathan Whitehorn 				    avail[i].mr_start + avail[i].mr_size) {
282b9d056f3SNathan Whitehorn 					avail[asz].mr_start =
283922a3152SNathan Whitehorn 					    fdtmap[j].address + fdtmap[j].size;
284b9d056f3SNathan Whitehorn 					avail[asz].mr_size = avail[i].mr_start +
285b9d056f3SNathan Whitehorn 					     avail[i].mr_size -
286b9d056f3SNathan Whitehorn 					     avail[asz].mr_start;
287b9d056f3SNathan Whitehorn 					asz++;
288b9d056f3SNathan Whitehorn 				}
289b9d056f3SNathan Whitehorn 
290922a3152SNathan Whitehorn 				avail[i].mr_size = fdtmap[j].address -
291b9d056f3SNathan Whitehorn 				    avail[i].mr_start;
292b9d056f3SNathan Whitehorn 			}
293b9d056f3SNathan Whitehorn 
294b9d056f3SNathan Whitehorn 			/*
295b9d056f3SNathan Whitehorn 			 * Case 3: Exclusion region ends in available entry.
296b9d056f3SNathan Whitehorn 			 * Move start point to where the exclusion zone ends.
297b9d056f3SNathan Whitehorn 			 * The case of a contained exclusion zone has already
298b9d056f3SNathan Whitehorn 			 * been caught in case 2.
299b9d056f3SNathan Whitehorn 			 */
300b9d056f3SNathan Whitehorn 			if (fdtmap[j].address + fdtmap[j].size >=
301b9d056f3SNathan Whitehorn 			    avail[i].mr_start && fdtmap[j].address +
302b9d056f3SNathan Whitehorn 			    fdtmap[j].size < avail[i].mr_start +
303b9d056f3SNathan Whitehorn 			    avail[i].mr_size) {
304922a3152SNathan Whitehorn 				avail[i].mr_size += avail[i].mr_start;
305b9d056f3SNathan Whitehorn 				avail[i].mr_start =
306922a3152SNathan Whitehorn 				    fdtmap[j].address + fdtmap[j].size;
307922a3152SNathan Whitehorn 				avail[i].mr_size -= avail[i].mr_start;
308b9d056f3SNathan Whitehorn 			}
309b9d056f3SNathan Whitehorn 		}
310b9d056f3SNathan Whitehorn 	}
311b9d056f3SNathan Whitehorn 
312b9d056f3SNathan Whitehorn 	return (asz);
313b9d056f3SNathan Whitehorn }
31435feca37SNathan Whitehorn #endif
315b9d056f3SNathan Whitehorn 
316fe3b4685SNathan Whitehorn /*
317fe3b4685SNathan Whitehorn  * This is called during powerpc_init, before the system is really initialized.
318fe3b4685SNathan Whitehorn  * It shall provide the total and the available regions of RAM.
319b9d056f3SNathan Whitehorn  * The available regions need not take the kernel into account.
320fe3b4685SNathan Whitehorn  */
321fe3b4685SNathan Whitehorn void
322c1cb22d7SNathan Whitehorn ofw_mem_regions(struct mem_region *memp, int *memsz,
323c1cb22d7SNathan Whitehorn 		struct mem_region *availp, int *availsz)
324fe3b4685SNathan Whitehorn {
325fe3b4685SNathan Whitehorn 	phandle_t phandle;
326c1cb22d7SNathan Whitehorn 	int asz, msz;
327c1cb22d7SNathan Whitehorn 	int res;
328d8c6808aSNathan Whitehorn 	char name[31];
329fe3b4685SNathan Whitehorn 
330fe3b4685SNathan Whitehorn 	asz = msz = 0;
331fe3b4685SNathan Whitehorn 
332fe3b4685SNathan Whitehorn 	/*
333d8c6808aSNathan Whitehorn 	 * Get memory from all the /memory nodes.
334fe3b4685SNathan Whitehorn 	 */
335d8c6808aSNathan Whitehorn 	for (phandle = OF_child(OF_peer(0)); phandle != 0;
336d8c6808aSNathan Whitehorn 	    phandle = OF_peer(phandle)) {
337d8c6808aSNathan Whitehorn 		if (OF_getprop(phandle, "name", name, sizeof(name)) <= 0)
338d8c6808aSNathan Whitehorn 			continue;
339b9d056f3SNathan Whitehorn 		if (strncmp(name, "memory", sizeof(name)) != 0 &&
340b9d056f3SNathan Whitehorn 		    strncmp(name, "memory@", strlen("memory@")) != 0)
341d8c6808aSNathan Whitehorn 			continue;
342fe3b4685SNathan Whitehorn 
343c1cb22d7SNathan Whitehorn 		res = parse_ofw_memory(phandle, "reg", &memp[msz]);
344d8c6808aSNathan Whitehorn 		msz += res/sizeof(struct mem_region);
345d8c6808aSNathan Whitehorn 		if (OF_getproplen(phandle, "available") >= 0)
346d8c6808aSNathan Whitehorn 			res = parse_ofw_memory(phandle, "available",
347c1cb22d7SNathan Whitehorn 			    &availp[asz]);
348d8c6808aSNathan Whitehorn 		else
349c1cb22d7SNathan Whitehorn 			res = parse_ofw_memory(phandle, "reg", &availp[asz]);
350d8c6808aSNathan Whitehorn 		asz += res/sizeof(struct mem_region);
351d8c6808aSNathan Whitehorn 	}
352d8c6808aSNathan Whitehorn 
35335feca37SNathan Whitehorn #ifdef FDT
354b9d056f3SNathan Whitehorn 	phandle = OF_finddevice("/chosen");
355b9d056f3SNathan Whitehorn 	if (OF_hasprop(phandle, "fdtmemreserv"))
356b9d056f3SNathan Whitehorn 		asz = excise_fdt_reserved(availp, asz);
35735feca37SNathan Whitehorn #endif
358b9d056f3SNathan Whitehorn 
359d8c6808aSNathan Whitehorn 	*memsz = msz;
360c1cb22d7SNathan Whitehorn 	*availsz = asz;
361fe3b4685SNathan Whitehorn }
362fe3b4685SNathan Whitehorn 
363fe3b4685SNathan Whitehorn void
364fe3b4685SNathan Whitehorn OF_initial_setup(void *fdt_ptr, void *junk, int (*openfirm)(void *))
365fe3b4685SNathan Whitehorn {
3666f489d43SJustin Hibbits #ifdef AIM
367bb808254SNathan Whitehorn 	ofmsr[0] = mfmsr();
368bb808254SNathan Whitehorn 	#ifdef __powerpc64__
369bb808254SNathan Whitehorn 	ofmsr[0] &= ~PSL_SF;
37062c6b30eSJustin Hibbits 	#else
371bb808254SNathan Whitehorn 	__asm __volatile("mfsprg0 %0" : "=&r"(ofmsr[1]));
37262c6b30eSJustin Hibbits 	#endif
373bb808254SNathan Whitehorn 	__asm __volatile("mfsprg1 %0" : "=&r"(ofmsr[2]));
374bb808254SNathan Whitehorn 	__asm __volatile("mfsprg2 %0" : "=&r"(ofmsr[3]));
375bb808254SNathan Whitehorn 	__asm __volatile("mfsprg3 %0" : "=&r"(ofmsr[4]));
3766f489d43SJustin Hibbits 	openfirmware_entry = openfirm;
377bb808254SNathan Whitehorn 
378fe3b4685SNathan Whitehorn 	if (ofmsr[0] & PSL_DR)
379fe3b4685SNathan Whitehorn 		ofw_real_mode = 0;
380fe3b4685SNathan Whitehorn 	else
381fe3b4685SNathan Whitehorn 		ofw_real_mode = 1;
382fe3b4685SNathan Whitehorn 
3836f489d43SJustin Hibbits 	ofw_save_trap_vec(save_trap_init);
3846f489d43SJustin Hibbits #else
3856f489d43SJustin Hibbits 	ofw_real_mode = 1;
3866f489d43SJustin Hibbits #endif
3876f489d43SJustin Hibbits 
388fe3b4685SNathan Whitehorn 	fdt = fdt_ptr;
389fe3b4685SNathan Whitehorn }
390fe3b4685SNathan Whitehorn 
391fe3b4685SNathan Whitehorn boolean_t
392fe3b4685SNathan Whitehorn OF_bootstrap()
393fe3b4685SNathan Whitehorn {
394fe3b4685SNathan Whitehorn 	boolean_t status = FALSE;
3952bfca577SNathan Whitehorn 	int err = 0;
396fe3b4685SNathan Whitehorn 
3976f489d43SJustin Hibbits #ifdef AIM
39817879090SNathan Whitehorn 	if (openfirmware_entry != NULL) {
399fe3b4685SNathan Whitehorn 		if (ofw_real_mode) {
400fe3b4685SNathan Whitehorn 			status = OF_install(OFW_STD_REAL, 0);
401fe3b4685SNathan Whitehorn 		} else {
402fe3b4685SNathan Whitehorn 			#ifdef __powerpc64__
403fe3b4685SNathan Whitehorn 			status = OF_install(OFW_STD_32BIT, 0);
404fe3b4685SNathan Whitehorn 			#else
405fe3b4685SNathan Whitehorn 			status = OF_install(OFW_STD_DIRECT, 0);
406fe3b4685SNathan Whitehorn 			#endif
407fe3b4685SNathan Whitehorn 		}
408fe3b4685SNathan Whitehorn 
409fe3b4685SNathan Whitehorn 		if (status != TRUE)
410fe3b4685SNathan Whitehorn 			return status;
411fe3b4685SNathan Whitehorn 
4122bfca577SNathan Whitehorn 		err = OF_init(openfirmware);
4136f489d43SJustin Hibbits 	} else
4146f489d43SJustin Hibbits #endif
4156f489d43SJustin Hibbits 	if (fdt != NULL) {
41635feca37SNathan Whitehorn #ifdef FDT
417f9edb09dSNathan Whitehorn #ifdef AIM
418f9edb09dSNathan Whitehorn 		bus_space_tag_t fdt_bt;
419f9edb09dSNathan Whitehorn 		vm_offset_t tmp_fdt_ptr;
420f9edb09dSNathan Whitehorn 		vm_size_t fdt_size;
421f9edb09dSNathan Whitehorn 		uintptr_t fdt_va;
422f9edb09dSNathan Whitehorn #endif
423fe3b4685SNathan Whitehorn 
424f9edb09dSNathan Whitehorn 		status = OF_install(OFW_FDT, 0);
425fe3b4685SNathan Whitehorn 		if (status != TRUE)
426fe3b4685SNathan Whitehorn 			return status;
427fe3b4685SNathan Whitehorn 
428f9edb09dSNathan Whitehorn #ifdef AIM /* AIM-only for now -- Book-E does this remapping in early init */
429f9edb09dSNathan Whitehorn 		/* Get the FDT size for mapping if we can */
430f9edb09dSNathan Whitehorn 		tmp_fdt_ptr = pmap_early_io_map((vm_paddr_t)fdt, PAGE_SIZE);
431f9edb09dSNathan Whitehorn 		if (fdt_check_header((void *)tmp_fdt_ptr) != 0) {
432f9edb09dSNathan Whitehorn 			pmap_early_io_unmap(tmp_fdt_ptr, PAGE_SIZE);
433f9edb09dSNathan Whitehorn 			return FALSE;
434fe3b4685SNathan Whitehorn 		}
435f9edb09dSNathan Whitehorn 		fdt_size = fdt_totalsize((void *)tmp_fdt_ptr);
436f9edb09dSNathan Whitehorn 		pmap_early_io_unmap(tmp_fdt_ptr, PAGE_SIZE);
437f9edb09dSNathan Whitehorn 
438f9edb09dSNathan Whitehorn 		/*
439f9edb09dSNathan Whitehorn 		 * Map this for real. Use bus_space_map() to take advantage
440f9edb09dSNathan Whitehorn 		 * of its auto-remapping function once the kernel is loaded.
441f9edb09dSNathan Whitehorn 		 * This is a dirty hack, but what we have.
442f9edb09dSNathan Whitehorn 		 */
443f9edb09dSNathan Whitehorn #ifdef _LITTLE_ENDIAN
444f9edb09dSNathan Whitehorn 		fdt_bt = &bs_le_tag;
445f9edb09dSNathan Whitehorn #else
446f9edb09dSNathan Whitehorn 		fdt_bt = &bs_be_tag;
447f9edb09dSNathan Whitehorn #endif
448f9edb09dSNathan Whitehorn 		bus_space_map(fdt_bt, (vm_paddr_t)fdt, fdt_size, 0, &fdt_va);
449f9edb09dSNathan Whitehorn 
450f9edb09dSNathan Whitehorn 		err = OF_init((void *)fdt_va);
451f9edb09dSNathan Whitehorn #else
452f9edb09dSNathan Whitehorn 		err = OF_init(fdt);
453f9edb09dSNathan Whitehorn #endif
45435feca37SNathan Whitehorn #endif
455f9edb09dSNathan Whitehorn 	}
456f9edb09dSNathan Whitehorn 
457f9edb09dSNathan Whitehorn 	#ifdef FDT_DTB_STATIC
458f9edb09dSNathan Whitehorn 	/*
459f9edb09dSNathan Whitehorn 	 * Check for a statically included blob already in the kernel and
460f9edb09dSNathan Whitehorn 	 * needing no mapping.
461f9edb09dSNathan Whitehorn 	 */
462f9edb09dSNathan Whitehorn 	else {
463f9edb09dSNathan Whitehorn 		status = OF_install(OFW_FDT, 0);
464f9edb09dSNathan Whitehorn 		if (status != TRUE)
465f9edb09dSNathan Whitehorn 			return status;
466f9edb09dSNathan Whitehorn 		err = OF_init(&fdt_static_dtb);
467f9edb09dSNathan Whitehorn 	}
468f9edb09dSNathan Whitehorn 	#endif
469fe3b4685SNathan Whitehorn 
4702bfca577SNathan Whitehorn 	if (err != 0) {
4712bfca577SNathan Whitehorn 		OF_install(NULL, 0);
4722bfca577SNathan Whitehorn 		status = FALSE;
4732bfca577SNathan Whitehorn 	}
4742bfca577SNathan Whitehorn 
475fe3b4685SNathan Whitehorn 	return (status);
476fe3b4685SNathan Whitehorn }
477fe3b4685SNathan Whitehorn 
4786f489d43SJustin Hibbits #ifdef AIM
4799f706727SNathan Whitehorn void
480fe3b4685SNathan Whitehorn ofw_quiesce(void)
481fe3b4685SNathan Whitehorn {
482fe3b4685SNathan Whitehorn 	struct {
483fe3b4685SNathan Whitehorn 		cell_t name;
484fe3b4685SNathan Whitehorn 		cell_t nargs;
485fe3b4685SNathan Whitehorn 		cell_t nreturns;
486fe3b4685SNathan Whitehorn 	} args;
487fe3b4685SNathan Whitehorn 
4889f706727SNathan Whitehorn 	KASSERT(!pmap_bootstrapped, ("Cannot call ofw_quiesce after VM is up"));
489fe3b4685SNathan Whitehorn 
490fe3b4685SNathan Whitehorn 	args.name = (cell_t)(uintptr_t)"quiesce";
491fe3b4685SNathan Whitehorn 	args.nargs = 0;
492fe3b4685SNathan Whitehorn 	args.nreturns = 0;
493fe3b4685SNathan Whitehorn 	openfirmware(&args);
494fe3b4685SNathan Whitehorn }
495fe3b4685SNathan Whitehorn 
496fe3b4685SNathan Whitehorn static int
497fe3b4685SNathan Whitehorn openfirmware_core(void *args)
498fe3b4685SNathan Whitehorn {
499fe3b4685SNathan Whitehorn 	int		result;
500fe3b4685SNathan Whitehorn 	register_t	oldmsr;
501fe3b4685SNathan Whitehorn 
50244d29d47SNathan Whitehorn 	if (openfirmware_entry == NULL)
50344d29d47SNathan Whitehorn 		return (-1);
50444d29d47SNathan Whitehorn 
505fe3b4685SNathan Whitehorn 	/*
506fe3b4685SNathan Whitehorn 	 * Turn off exceptions - we really don't want to end up
5074aa3cee6SNathan Whitehorn 	 * anywhere unexpected with PCPU set to something strange
5084aa3cee6SNathan Whitehorn 	 * or the stack pointer wrong.
509fe3b4685SNathan Whitehorn 	 */
510fe3b4685SNathan Whitehorn 	oldmsr = intr_disable();
511fe3b4685SNathan Whitehorn 
5124aa3cee6SNathan Whitehorn 	ofw_sprg_prepare();
5134aa3cee6SNathan Whitehorn 
514f367ffdeSAndreas Tobler 	/* Save trap vectors */
515f367ffdeSAndreas Tobler 	ofw_save_trap_vec(save_trap_of);
516f367ffdeSAndreas Tobler 
517f367ffdeSAndreas Tobler 	/* Restore initially saved trap vectors */
518f367ffdeSAndreas Tobler 	ofw_restore_trap_vec(save_trap_init);
519f367ffdeSAndreas Tobler 
52095ce4c00SJustin Hibbits #ifndef __powerpc64__
521fe3b4685SNathan Whitehorn 	/*
522fe3b4685SNathan Whitehorn 	 * Clear battable[] translations
523fe3b4685SNathan Whitehorn 	 */
524fe3b4685SNathan Whitehorn 	if (!(cpu_features & PPC_FEATURE_64))
525fe3b4685SNathan Whitehorn 		__asm __volatile("mtdbatu 2, %0\n"
526fe3b4685SNathan Whitehorn 				 "mtdbatu 3, %0" : : "r" (0));
527fe3b4685SNathan Whitehorn 	isync();
528fe3b4685SNathan Whitehorn #endif
529fe3b4685SNathan Whitehorn 
530fe3b4685SNathan Whitehorn 	result = ofwcall(args);
531f367ffdeSAndreas Tobler 
532f367ffdeSAndreas Tobler 	/* Restore trap vecotrs */
533f367ffdeSAndreas Tobler 	ofw_restore_trap_vec(save_trap_of);
534f367ffdeSAndreas Tobler 
5354aa3cee6SNathan Whitehorn 	ofw_sprg_restore();
5364aa3cee6SNathan Whitehorn 
537fe3b4685SNathan Whitehorn 	intr_restore(oldmsr);
538fe3b4685SNathan Whitehorn 
539fe3b4685SNathan Whitehorn 	return (result);
540fe3b4685SNathan Whitehorn }
541fe3b4685SNathan Whitehorn 
542fe3b4685SNathan Whitehorn #ifdef SMP
543fe3b4685SNathan Whitehorn struct ofw_rv_args {
544fe3b4685SNathan Whitehorn 	void *args;
545fe3b4685SNathan Whitehorn 	int retval;
546fe3b4685SNathan Whitehorn 	volatile int in_progress;
547fe3b4685SNathan Whitehorn };
548fe3b4685SNathan Whitehorn 
549fe3b4685SNathan Whitehorn static void
550fe3b4685SNathan Whitehorn ofw_rendezvous_dispatch(void *xargs)
551fe3b4685SNathan Whitehorn {
552fe3b4685SNathan Whitehorn 	struct ofw_rv_args *rv_args = xargs;
553fe3b4685SNathan Whitehorn 
554fe3b4685SNathan Whitehorn 	/* NOTE: Interrupts are disabled here */
555fe3b4685SNathan Whitehorn 
556fe3b4685SNathan Whitehorn 	if (PCPU_GET(cpuid) == 0) {
557fe3b4685SNathan Whitehorn 		/*
558fe3b4685SNathan Whitehorn 		 * Execute all OF calls on CPU 0
559fe3b4685SNathan Whitehorn 		 */
560fe3b4685SNathan Whitehorn 		rv_args->retval = openfirmware_core(rv_args->args);
561fe3b4685SNathan Whitehorn 		rv_args->in_progress = 0;
562fe3b4685SNathan Whitehorn 	} else {
563fe3b4685SNathan Whitehorn 		/*
564fe3b4685SNathan Whitehorn 		 * Spin with interrupts off on other CPUs while OF has
565fe3b4685SNathan Whitehorn 		 * control of the machine.
566fe3b4685SNathan Whitehorn 		 */
567fe3b4685SNathan Whitehorn 		while (rv_args->in_progress)
568fe3b4685SNathan Whitehorn 			cpu_spinwait();
569fe3b4685SNathan Whitehorn 	}
570fe3b4685SNathan Whitehorn }
571fe3b4685SNathan Whitehorn #endif
572fe3b4685SNathan Whitehorn 
573fe3b4685SNathan Whitehorn static int
574fe3b4685SNathan Whitehorn openfirmware(void *args)
575fe3b4685SNathan Whitehorn {
576fe3b4685SNathan Whitehorn 	int result;
577fe3b4685SNathan Whitehorn 	#ifdef SMP
578fe3b4685SNathan Whitehorn 	struct ofw_rv_args rv_args;
57944d29d47SNathan Whitehorn 	#endif
580fe3b4685SNathan Whitehorn 
58144d29d47SNathan Whitehorn 	if (openfirmware_entry == NULL)
58244d29d47SNathan Whitehorn 		return (-1);
58344d29d47SNathan Whitehorn 
58444d29d47SNathan Whitehorn 	#ifdef SMP
58591419bdaSNathan Whitehorn 	if (cold) {
58691419bdaSNathan Whitehorn 		result = openfirmware_core(args);
58791419bdaSNathan Whitehorn 	} else {
588fe3b4685SNathan Whitehorn 		rv_args.args = args;
589fe3b4685SNathan Whitehorn 		rv_args.in_progress = 1;
59091419bdaSNathan Whitehorn 		smp_rendezvous(smp_no_rendezvous_barrier,
59191419bdaSNathan Whitehorn 		    ofw_rendezvous_dispatch, smp_no_rendezvous_barrier,
59291419bdaSNathan Whitehorn 		    &rv_args);
593fe3b4685SNathan Whitehorn 		result = rv_args.retval;
59491419bdaSNathan Whitehorn 	}
595fe3b4685SNathan Whitehorn 	#else
596fe3b4685SNathan Whitehorn 	result = openfirmware_core(args);
597fe3b4685SNathan Whitehorn 	#endif
598fe3b4685SNathan Whitehorn 
599fe3b4685SNathan Whitehorn 	return (result);
600fe3b4685SNathan Whitehorn }
601fe3b4685SNathan Whitehorn 
602fe3b4685SNathan Whitehorn void
603fe3b4685SNathan Whitehorn OF_reboot()
604fe3b4685SNathan Whitehorn {
605fe3b4685SNathan Whitehorn 	struct {
606fe3b4685SNathan Whitehorn 		cell_t name;
607fe3b4685SNathan Whitehorn 		cell_t nargs;
608fe3b4685SNathan Whitehorn 		cell_t nreturns;
609fe3b4685SNathan Whitehorn 		cell_t arg;
610fe3b4685SNathan Whitehorn 	} args;
611fe3b4685SNathan Whitehorn 
612fe3b4685SNathan Whitehorn 	args.name = (cell_t)(uintptr_t)"interpret";
613fe3b4685SNathan Whitehorn 	args.nargs = 1;
614fe3b4685SNathan Whitehorn 	args.nreturns = 0;
615fe3b4685SNathan Whitehorn 	args.arg = (cell_t)(uintptr_t)"reset-all";
616fe3b4685SNathan Whitehorn 	openfirmware_core(&args); /* Don't do rendezvous! */
617fe3b4685SNathan Whitehorn 
618fe3b4685SNathan Whitehorn 	for (;;);	/* just in case */
619fe3b4685SNathan Whitehorn }
620fe3b4685SNathan Whitehorn 
621629aa519SNathan Whitehorn #endif /* AIM */
622629aa519SNathan Whitehorn 
623fe3b4685SNathan Whitehorn void
624fe3b4685SNathan Whitehorn OF_getetheraddr(device_t dev, u_char *addr)
625fe3b4685SNathan Whitehorn {
626fe3b4685SNathan Whitehorn 	phandle_t	node;
627fe3b4685SNathan Whitehorn 
628fe3b4685SNathan Whitehorn 	node = ofw_bus_get_node(dev);
629fe3b4685SNathan Whitehorn 	OF_getprop(node, "local-mac-address", addr, ETHER_ADDR_LEN);
630fe3b4685SNathan Whitehorn }
631fe3b4685SNathan Whitehorn 
632fe3b4685SNathan Whitehorn /*
633fe3b4685SNathan Whitehorn  * Return a bus handle and bus tag that corresponds to the register
634fe3b4685SNathan Whitehorn  * numbered regno for the device referenced by the package handle
635fe3b4685SNathan Whitehorn  * dev. This function is intended to be used by console drivers in
636fe3b4685SNathan Whitehorn  * early boot only. It works by mapping the address of the device's
637fe3b4685SNathan Whitehorn  * register in the address space of its parent and recursively walk
638fe3b4685SNathan Whitehorn  * the device tree upward this way.
639fe3b4685SNathan Whitehorn  */
640fe3b4685SNathan Whitehorn int
641fe3b4685SNathan Whitehorn OF_decode_addr(phandle_t dev, int regno, bus_space_tag_t *tag,
64245fd1862SAndrew Turner     bus_space_handle_t *handle, bus_size_t *sz)
643fe3b4685SNathan Whitehorn {
644bc7b9300SIan Lepore 	bus_addr_t addr;
645bc7b9300SIan Lepore 	bus_size_t size;
646bc7b9300SIan Lepore 	pcell_t pci_hi;
647bc7b9300SIan Lepore 	int flags, res;
648fe3b4685SNathan Whitehorn 
649bc7b9300SIan Lepore 	res = ofw_reg_to_paddr(dev, regno, &addr, &size, &pci_hi);
650bc7b9300SIan Lepore 	if (res < 0)
651bc7b9300SIan Lepore 		return (res);
652fe3b4685SNathan Whitehorn 
653bc7b9300SIan Lepore 	if (pci_hi == OFW_PADDR_NOT_PCI) {
65443a581e1SNathan Whitehorn 		*tag = &bs_be_tag;
655bc7b9300SIan Lepore 		flags = 0;
656bc7b9300SIan Lepore 	} else {
65743a581e1SNathan Whitehorn 		*tag = &bs_le_tag;
658bc7b9300SIan Lepore 		flags = (pci_hi & OFW_PCI_PHYS_HI_PREFETCHABLE) ?
659bc7b9300SIan Lepore 		    BUS_SPACE_MAP_PREFETCHABLE: 0;
660fe3b4685SNathan Whitehorn 	}
661fe3b4685SNathan Whitehorn 
66245fd1862SAndrew Turner 	if (sz != NULL)
66345fd1862SAndrew Turner 		*sz = size;
66445fd1862SAndrew Turner 
665bc7b9300SIan Lepore 	return (bus_space_map(*tag, addr, size, flags, handle));
666fe3b4685SNathan Whitehorn }
667fe3b4685SNathan Whitehorn 
668