xref: /freebsd/sys/powerpc/ofw/ofw_machdep.c (revision b52dceb8)
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>
378c092157SJustin Hibbits #include "opt_platform.h"
38fe3b4685SNathan Whitehorn #include <sys/param.h>
39fe3b4685SNathan Whitehorn #include <sys/bus.h>
40fe3b4685SNathan Whitehorn #include <sys/systm.h>
41fe3b4685SNathan Whitehorn #include <sys/conf.h>
42fe3b4685SNathan Whitehorn #include <sys/disk.h>
43fe3b4685SNathan Whitehorn #include <sys/fcntl.h>
44e2e050c8SConrad Meyer #include <sys/lock.h>
45fe3b4685SNathan Whitehorn #include <sys/malloc.h>
46fe3b4685SNathan Whitehorn #include <sys/smp.h>
47fe3b4685SNathan Whitehorn #include <sys/stat.h>
48b9d056f3SNathan Whitehorn #include <sys/endian.h>
49fe3b4685SNathan Whitehorn 
50fe3b4685SNathan Whitehorn #include <net/ethernet.h>
51fe3b4685SNathan Whitehorn 
528c092157SJustin Hibbits #include <dev/fdt/fdt_common.h>
53fe3b4685SNathan Whitehorn #include <dev/ofw/openfirm.h>
54fe3b4685SNathan Whitehorn #include <dev/ofw/ofw_pci.h>
55fe3b4685SNathan Whitehorn #include <dev/ofw/ofw_bus.h>
56bc7b9300SIan Lepore #include <dev/ofw/ofw_subr.h>
57fe3b4685SNathan Whitehorn 
58fe3b4685SNathan Whitehorn #include <vm/vm.h>
59fe3b4685SNathan Whitehorn #include <vm/vm_param.h>
60fe3b4685SNathan Whitehorn #include <vm/vm_page.h>
6149d9a597SJustin Hibbits #include <vm/vm_phys.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 
72fd8cf3beSJustin Hibbits #ifdef POWERNV
73fd8cf3beSJustin Hibbits #include <powerpc/powernv/opal.h>
74fd8cf3beSJustin Hibbits #endif
75fd8cf3beSJustin Hibbits 
766f489d43SJustin Hibbits static void	*fdt;
776f489d43SJustin Hibbits int		ofw_real_mode;
786f489d43SJustin Hibbits 
79629aa519SNathan Whitehorn #ifdef AIM
80fe3b4685SNathan Whitehorn extern register_t ofmsr[5];
8117879090SNathan Whitehorn extern void	*openfirmware_entry;
82bb808254SNathan Whitehorn char		save_trap_init[0x2f00];          /* EXC_LAST */
83f367ffdeSAndreas Tobler char		save_trap_of[0x2f00];            /* EXC_LAST */
84fe3b4685SNathan Whitehorn 
85d8c6808aSNathan Whitehorn int		ofwcall(void *);
86fe3b4685SNathan Whitehorn static int	openfirmware(void *args);
87fe3b4685SNathan Whitehorn 
8812fe21a3SBrandon Bergren #pragma clang diagnostic push
8912fe21a3SBrandon Bergren #pragma clang diagnostic ignored "-Wfortify-source"
9012fe21a3SBrandon Bergren 
91f367ffdeSAndreas Tobler __inline void
ofw_save_trap_vec(char * save_trap_vec)92f367ffdeSAndreas Tobler ofw_save_trap_vec(char *save_trap_vec)
93f367ffdeSAndreas Tobler {
94f9edb09dSNathan Whitehorn 	if (!ofw_real_mode || !hw_direct_map)
95f367ffdeSAndreas Tobler                 return;
96f367ffdeSAndreas Tobler 
97f9edb09dSNathan Whitehorn 	bcopy((void *)PHYS_TO_DMAP(EXC_RST), save_trap_vec, EXC_LAST - EXC_RST);
98f367ffdeSAndreas Tobler }
99f367ffdeSAndreas Tobler 
100f367ffdeSAndreas Tobler static __inline void
ofw_restore_trap_vec(char * restore_trap_vec)101f367ffdeSAndreas Tobler ofw_restore_trap_vec(char *restore_trap_vec)
102f367ffdeSAndreas Tobler {
103f9edb09dSNathan Whitehorn 	if (!ofw_real_mode || !hw_direct_map)
104f367ffdeSAndreas Tobler                 return;
105f367ffdeSAndreas Tobler 
106f9edb09dSNathan Whitehorn 	bcopy(restore_trap_vec, (void *)PHYS_TO_DMAP(EXC_RST),
107f9edb09dSNathan Whitehorn 	    EXC_LAST - EXC_RST);
108f9edb09dSNathan Whitehorn 	__syncicache((void *)PHYS_TO_DMAP(EXC_RSVD), EXC_LAST - EXC_RSVD);
109f367ffdeSAndreas Tobler }
1104aa3cee6SNathan Whitehorn 
11112fe21a3SBrandon Bergren #pragma clang diagnostic pop
11212fe21a3SBrandon Bergren 
1134aa3cee6SNathan Whitehorn /*
1144aa3cee6SNathan Whitehorn  * Saved SPRG0-3 from OpenFirmware. Will be restored prior to the callback.
1154aa3cee6SNathan Whitehorn  */
1164aa3cee6SNathan Whitehorn register_t	ofw_sprg0_save;
1174aa3cee6SNathan Whitehorn 
1184aa3cee6SNathan Whitehorn static __inline void
ofw_sprg_prepare(void)1194aa3cee6SNathan Whitehorn ofw_sprg_prepare(void)
1204aa3cee6SNathan Whitehorn {
1214aa3cee6SNathan Whitehorn 	if (ofw_real_mode)
1224aa3cee6SNathan Whitehorn 		return;
1234aa3cee6SNathan Whitehorn 
1244aa3cee6SNathan Whitehorn 	/*
1254aa3cee6SNathan Whitehorn 	 * Assume that interrupt are disabled at this point, or
1264aa3cee6SNathan Whitehorn 	 * SPRG1-3 could be trashed
1274aa3cee6SNathan Whitehorn 	 */
12862c6b30eSJustin Hibbits #ifdef __powerpc64__
12962c6b30eSJustin Hibbits 	__asm __volatile("mtsprg1 %0\n\t"
13062c6b30eSJustin Hibbits 	    		 "mtsprg2 %1\n\t"
13162c6b30eSJustin Hibbits 			 "mtsprg3 %2\n\t"
13262c6b30eSJustin Hibbits 			 :
13362c6b30eSJustin Hibbits 			 : "r"(ofmsr[2]),
13462c6b30eSJustin Hibbits 			 "r"(ofmsr[3]),
13562c6b30eSJustin Hibbits 			 "r"(ofmsr[4]));
13662c6b30eSJustin Hibbits #else
1374aa3cee6SNathan Whitehorn 	__asm __volatile("mfsprg0 %0\n\t"
1384aa3cee6SNathan Whitehorn 			 "mtsprg0 %1\n\t"
1394aa3cee6SNathan Whitehorn 	    		 "mtsprg1 %2\n\t"
1404aa3cee6SNathan Whitehorn 	    		 "mtsprg2 %3\n\t"
1414aa3cee6SNathan Whitehorn 			 "mtsprg3 %4\n\t"
1424aa3cee6SNathan Whitehorn 			 : "=&r"(ofw_sprg0_save)
1434aa3cee6SNathan Whitehorn 			 : "r"(ofmsr[1]),
1444aa3cee6SNathan Whitehorn 			 "r"(ofmsr[2]),
1454aa3cee6SNathan Whitehorn 			 "r"(ofmsr[3]),
1464aa3cee6SNathan Whitehorn 			 "r"(ofmsr[4]));
14762c6b30eSJustin Hibbits #endif
1484aa3cee6SNathan Whitehorn }
1494aa3cee6SNathan Whitehorn 
1504aa3cee6SNathan Whitehorn static __inline void
ofw_sprg_restore(void)1514aa3cee6SNathan Whitehorn ofw_sprg_restore(void)
1524aa3cee6SNathan Whitehorn {
1534aa3cee6SNathan Whitehorn 	if (ofw_real_mode)
1544aa3cee6SNathan Whitehorn 		return;
1554aa3cee6SNathan Whitehorn 
1564aa3cee6SNathan Whitehorn 	/*
1574aa3cee6SNathan Whitehorn 	 * Note that SPRG1-3 contents are irrelevant. They are scratch
1584aa3cee6SNathan Whitehorn 	 * registers used in the early portion of trap handling when
1594aa3cee6SNathan Whitehorn 	 * interrupts are disabled.
1604aa3cee6SNathan Whitehorn 	 *
1614aa3cee6SNathan Whitehorn 	 * PCPU data cannot be used until this routine is called !
1624aa3cee6SNathan Whitehorn 	 */
16362c6b30eSJustin Hibbits #ifndef __powerpc64__
1644aa3cee6SNathan Whitehorn 	__asm __volatile("mtsprg0 %0" :: "r"(ofw_sprg0_save));
16562c6b30eSJustin Hibbits #endif
1664aa3cee6SNathan Whitehorn }
167c7291bdcSNathan Whitehorn #endif
168f367ffdeSAndreas Tobler 
169fe3b4685SNathan Whitehorn static int
parse_ofw_memory(phandle_t node,const char * prop,struct mem_region * output)170fe3b4685SNathan Whitehorn parse_ofw_memory(phandle_t node, const char *prop, struct mem_region *output)
171fe3b4685SNathan Whitehorn {
172fe3b4685SNathan Whitehorn 	cell_t address_cells, size_cells;
173d8c6808aSNathan Whitehorn 	cell_t OFmem[4 * PHYS_AVAIL_SZ];
174fe3b4685SNathan Whitehorn 	int sz, i, j;
175fe3b4685SNathan Whitehorn 	phandle_t phandle;
176fe3b4685SNathan Whitehorn 
177fe3b4685SNathan Whitehorn 	sz = 0;
178fe3b4685SNathan Whitehorn 
179fe3b4685SNathan Whitehorn 	/*
180fe3b4685SNathan Whitehorn 	 * Get #address-cells from root node, defaulting to 1 if it cannot
181fe3b4685SNathan Whitehorn 	 * be found.
182fe3b4685SNathan Whitehorn 	 */
183fe3b4685SNathan Whitehorn 	phandle = OF_finddevice("/");
184509142e1SNathan Whitehorn 	if (OF_getencprop(phandle, "#address-cells", &address_cells,
1858bab0d80SNathan Whitehorn 	    sizeof(address_cells)) < (ssize_t)sizeof(address_cells))
186fe3b4685SNathan Whitehorn 		address_cells = 1;
187509142e1SNathan Whitehorn 	if (OF_getencprop(phandle, "#size-cells", &size_cells,
1888bab0d80SNathan Whitehorn 	    sizeof(size_cells)) < (ssize_t)sizeof(size_cells))
189fe3b4685SNathan Whitehorn 		size_cells = 1;
190fe3b4685SNathan Whitehorn 
191fe3b4685SNathan Whitehorn 	/*
192fe3b4685SNathan Whitehorn 	 * Get memory.
193fe3b4685SNathan Whitehorn 	 */
194509142e1SNathan Whitehorn 	if (node == -1 || (sz = OF_getencprop(node, prop,
195d8c6808aSNathan Whitehorn 	    OFmem, sizeof(OFmem))) <= 0)
196fe3b4685SNathan Whitehorn 		panic("Physical memory map not found");
197fe3b4685SNathan Whitehorn 
198fe3b4685SNathan Whitehorn 	i = 0;
199fe3b4685SNathan Whitehorn 	j = 0;
200fe3b4685SNathan Whitehorn 	while (i < sz/sizeof(cell_t)) {
201fe3b4685SNathan Whitehorn 		output[j].mr_start = OFmem[i++];
202fe3b4685SNathan Whitehorn 		if (address_cells == 2) {
203fe3b4685SNathan Whitehorn 			output[j].mr_start <<= 32;
204fe3b4685SNathan Whitehorn 			output[j].mr_start += OFmem[i++];
205fe3b4685SNathan Whitehorn 		}
206fe3b4685SNathan Whitehorn 
207fe3b4685SNathan Whitehorn 		output[j].mr_size = OFmem[i++];
208fe3b4685SNathan Whitehorn 		if (size_cells == 2) {
209fe3b4685SNathan Whitehorn 			output[j].mr_size <<= 32;
210fe3b4685SNathan Whitehorn 			output[j].mr_size += OFmem[i++];
211fe3b4685SNathan Whitehorn 		}
212fe3b4685SNathan Whitehorn 
213bb7137e1SJustin Hibbits 		if (output[j].mr_start > BUS_SPACE_MAXADDR)
214bb7137e1SJustin Hibbits 			continue;
215bb7137e1SJustin Hibbits 
216fe3b4685SNathan Whitehorn 		/*
217bb7137e1SJustin Hibbits 		 * Constrain memory to that which we can access.
218bb7137e1SJustin Hibbits 		 * 32-bit AIM can only reference 32 bits of address currently,
219bb7137e1SJustin Hibbits 		 * but Book-E can access 36 bits.
220fe3b4685SNathan Whitehorn 		 */
221fe3b4685SNathan Whitehorn 		if (((uint64_t)output[j].mr_start +
222bb7137e1SJustin Hibbits 		    (uint64_t)output[j].mr_size - 1) >
223bb7137e1SJustin Hibbits 		    BUS_SPACE_MAXADDR) {
224bb7137e1SJustin Hibbits 			output[j].mr_size = BUS_SPACE_MAXADDR -
225bb7137e1SJustin Hibbits 			    output[j].mr_start + 1;
226fe3b4685SNathan Whitehorn 		}
227fe3b4685SNathan Whitehorn 
228fe3b4685SNathan Whitehorn 		j++;
229fe3b4685SNathan Whitehorn 	}
230fe3b4685SNathan Whitehorn 
23149d9a597SJustin Hibbits 	return (j);
23249d9a597SJustin Hibbits }
23349d9a597SJustin Hibbits 
23449d9a597SJustin Hibbits static int
parse_numa_ofw_memory(phandle_t node,const char * prop,struct numa_mem_region * output)23549d9a597SJustin Hibbits parse_numa_ofw_memory(phandle_t node, const char *prop,
23649d9a597SJustin Hibbits     struct numa_mem_region *output)
23749d9a597SJustin Hibbits {
23849d9a597SJustin Hibbits 	cell_t address_cells, size_cells;
23949d9a597SJustin Hibbits 	cell_t OFmem[4 * PHYS_AVAIL_SZ];
24049d9a597SJustin Hibbits 	int sz, i, j;
24149d9a597SJustin Hibbits 	phandle_t phandle;
24249d9a597SJustin Hibbits 
24349d9a597SJustin Hibbits 	sz = 0;
24449d9a597SJustin Hibbits 
24549d9a597SJustin Hibbits 	/*
24649d9a597SJustin Hibbits 	 * Get #address-cells from root node, defaulting to 1 if it cannot
24749d9a597SJustin Hibbits 	 * be found.
24849d9a597SJustin Hibbits 	 */
24949d9a597SJustin Hibbits 	phandle = OF_finddevice("/");
25049d9a597SJustin Hibbits 	if (OF_getencprop(phandle, "#address-cells", &address_cells,
25149d9a597SJustin Hibbits 	    sizeof(address_cells)) < (ssize_t)sizeof(address_cells))
25249d9a597SJustin Hibbits 		address_cells = 1;
25349d9a597SJustin Hibbits 	if (OF_getencprop(phandle, "#size-cells", &size_cells,
25449d9a597SJustin Hibbits 	    sizeof(size_cells)) < (ssize_t)sizeof(size_cells))
25549d9a597SJustin Hibbits 		size_cells = 1;
25649d9a597SJustin Hibbits 
25749d9a597SJustin Hibbits 	/*
25849d9a597SJustin Hibbits 	 * Get memory.
25949d9a597SJustin Hibbits 	 */
26049d9a597SJustin Hibbits 	if (node == -1 || (sz = OF_getencprop(node, prop,
26149d9a597SJustin Hibbits 	    OFmem, sizeof(OFmem))) <= 0)
26249d9a597SJustin Hibbits 		panic("Physical memory map not found");
26349d9a597SJustin Hibbits 
26449d9a597SJustin Hibbits 	i = 0;
26549d9a597SJustin Hibbits 	j = 0;
26649d9a597SJustin Hibbits 	while (i < sz/sizeof(cell_t)) {
26749d9a597SJustin Hibbits 		output[j].mr_start = OFmem[i++];
26849d9a597SJustin Hibbits 		if (address_cells == 2) {
26949d9a597SJustin Hibbits 			output[j].mr_start <<= 32;
27049d9a597SJustin Hibbits 			output[j].mr_start += OFmem[i++];
27149d9a597SJustin Hibbits 		}
27249d9a597SJustin Hibbits 		output[j].mr_size = OFmem[i++];
27349d9a597SJustin Hibbits 		if (size_cells == 2) {
27449d9a597SJustin Hibbits 			output[j].mr_size <<= 32;
27549d9a597SJustin Hibbits 			output[j].mr_size += OFmem[i++];
27649d9a597SJustin Hibbits 		}
27749d9a597SJustin Hibbits 		j++;
27849d9a597SJustin Hibbits 	}
27949d9a597SJustin Hibbits 
28049d9a597SJustin Hibbits 	return (j);
281fe3b4685SNathan Whitehorn }
282fe3b4685SNathan Whitehorn 
28335feca37SNathan Whitehorn #ifdef FDT
284b9d056f3SNathan Whitehorn static int
excise_reserved_regions(struct mem_region * avail,int asz,struct mem_region * exclude,int esz)28548f64992SBreno Leitao excise_reserved_regions(struct mem_region *avail, int asz,
28648f64992SBreno Leitao 			struct mem_region *exclude, int esz)
287b9d056f3SNathan Whitehorn {
288b9d056f3SNathan Whitehorn 	int i, j, k;
289b9d056f3SNathan Whitehorn 
290b9d056f3SNathan Whitehorn 	for (i = 0; i < asz; i++) {
29148f64992SBreno Leitao 		for (j = 0; j < esz; j++) {
292b9d056f3SNathan Whitehorn 			/*
293b9d056f3SNathan Whitehorn 			 * Case 1: Exclusion region encloses complete
294b9d056f3SNathan Whitehorn 			 * available entry. Drop it and move on.
295b9d056f3SNathan Whitehorn 			 */
29648f64992SBreno Leitao 			if (exclude[j].mr_start <= avail[i].mr_start &&
29748f64992SBreno Leitao 			    exclude[j].mr_start + exclude[j].mr_size >=
298b9d056f3SNathan Whitehorn 			    avail[i].mr_start + avail[i].mr_size) {
299b9d056f3SNathan Whitehorn 				for (k = i+1; k < asz; k++)
300b9d056f3SNathan Whitehorn 					avail[k-1] = avail[k];
301b9d056f3SNathan Whitehorn 				asz--;
302b9d056f3SNathan Whitehorn 				i--; /* Repeat some entries */
303b9d056f3SNathan Whitehorn 				continue;
304b9d056f3SNathan Whitehorn 			}
305b9d056f3SNathan Whitehorn 
306b9d056f3SNathan Whitehorn 			/*
307b9d056f3SNathan Whitehorn 			 * Case 2: Exclusion region starts in available entry.
308b9d056f3SNathan Whitehorn 			 * Trim it to where the entry begins and append
309b9d056f3SNathan Whitehorn 			 * a new available entry with the region after
310b9d056f3SNathan Whitehorn 			 * the excluded region, if any.
311b9d056f3SNathan Whitehorn 			 */
31248f64992SBreno Leitao 			if (exclude[j].mr_start >= avail[i].mr_start &&
31348f64992SBreno Leitao 			    exclude[j].mr_start < avail[i].mr_start +
314b9d056f3SNathan Whitehorn 			    avail[i].mr_size) {
31548f64992SBreno Leitao 				if (exclude[j].mr_start + exclude[j].mr_size <
316b9d056f3SNathan Whitehorn 				    avail[i].mr_start + avail[i].mr_size) {
317b9d056f3SNathan Whitehorn 					avail[asz].mr_start =
31848f64992SBreno Leitao 					    exclude[j].mr_start + exclude[j].mr_size;
319b9d056f3SNathan Whitehorn 					avail[asz].mr_size = avail[i].mr_start +
320b9d056f3SNathan Whitehorn 					     avail[i].mr_size -
321b9d056f3SNathan Whitehorn 					     avail[asz].mr_start;
322b9d056f3SNathan Whitehorn 					asz++;
323b9d056f3SNathan Whitehorn 				}
324b9d056f3SNathan Whitehorn 
32548f64992SBreno Leitao 				avail[i].mr_size = exclude[j].mr_start -
326b9d056f3SNathan Whitehorn 				    avail[i].mr_start;
327b9d056f3SNathan Whitehorn 			}
328b9d056f3SNathan Whitehorn 
329b9d056f3SNathan Whitehorn 			/*
330b9d056f3SNathan Whitehorn 			 * Case 3: Exclusion region ends in available entry.
331b9d056f3SNathan Whitehorn 			 * Move start point to where the exclusion zone ends.
332b9d056f3SNathan Whitehorn 			 * The case of a contained exclusion zone has already
333b9d056f3SNathan Whitehorn 			 * been caught in case 2.
334b9d056f3SNathan Whitehorn 			 */
33548f64992SBreno Leitao 			if (exclude[j].mr_start + exclude[j].mr_size >=
33648f64992SBreno Leitao 			    avail[i].mr_start && exclude[j].mr_start +
33748f64992SBreno Leitao 			    exclude[j].mr_size < avail[i].mr_start +
338b9d056f3SNathan Whitehorn 			    avail[i].mr_size) {
339922a3152SNathan Whitehorn 				avail[i].mr_size += avail[i].mr_start;
340b9d056f3SNathan Whitehorn 				avail[i].mr_start =
34148f64992SBreno Leitao 				    exclude[j].mr_start + exclude[j].mr_size;
342922a3152SNathan Whitehorn 				avail[i].mr_size -= avail[i].mr_start;
343b9d056f3SNathan Whitehorn 			}
344b9d056f3SNathan Whitehorn 		}
345b9d056f3SNathan Whitehorn 	}
346b9d056f3SNathan Whitehorn 
347b9d056f3SNathan Whitehorn 	return (asz);
348b9d056f3SNathan Whitehorn }
34948f64992SBreno Leitao 
35048f64992SBreno Leitao static int
excise_initrd_region(struct mem_region * avail,int asz)35148f64992SBreno Leitao excise_initrd_region(struct mem_region *avail, int asz)
35248f64992SBreno Leitao {
35348f64992SBreno Leitao 	phandle_t chosen;
35448f64992SBreno Leitao 	uint64_t start, end;
35548f64992SBreno Leitao 	ssize_t size;
35648f64992SBreno Leitao 	struct mem_region initrdmap[1];
3576d645c57SBreno Leitao 	pcell_t cell[2];
35848f64992SBreno Leitao 
35948f64992SBreno Leitao 	chosen = OF_finddevice("/chosen");
36048f64992SBreno Leitao 
3616d645c57SBreno Leitao 	size = OF_getencprop(chosen, "linux,initrd-start", cell, sizeof(cell));
3626d645c57SBreno Leitao 	if (size < 0)
3636d645c57SBreno Leitao 		return (asz);
3646d645c57SBreno Leitao 	else if (size == 4)
3656d645c57SBreno Leitao 		start = cell[0];
3666d645c57SBreno Leitao 	else if (size == 8)
3676d645c57SBreno Leitao 		start = (uint64_t)cell[0] << 32 | cell[1];
3686d645c57SBreno Leitao 	else {
3696d645c57SBreno Leitao 		/* Invalid value length */
3706d645c57SBreno Leitao 		printf("WARNING: linux,initrd-start must be either 4 or 8 bytes long\n");
3716d645c57SBreno Leitao 		return (asz);
3726d645c57SBreno Leitao 	}
3736d645c57SBreno Leitao 
3746d645c57SBreno Leitao 	size = OF_getencprop(chosen, "linux,initrd-end", cell, sizeof(cell));
3756d645c57SBreno Leitao 	if (size < 0)
3766d645c57SBreno Leitao 		return (asz);
3776d645c57SBreno Leitao 	else if (size == 4)
3786d645c57SBreno Leitao 		end = cell[0];
3796d645c57SBreno Leitao 	else if (size == 8)
3806d645c57SBreno Leitao 		end = (uint64_t)cell[0] << 32 | cell[1];
3816d645c57SBreno Leitao 	else {
3826d645c57SBreno Leitao 		/* Invalid value length */
3836d645c57SBreno Leitao 		printf("WARNING: linux,initrd-end must be either 4 or 8 bytes long\n");
3846d645c57SBreno Leitao 		return (asz);
3856d645c57SBreno Leitao 	}
3866d645c57SBreno Leitao 
3876d645c57SBreno Leitao 	if (end <= start)
38848f64992SBreno Leitao 		return (asz);
38948f64992SBreno Leitao 
39048f64992SBreno Leitao 	initrdmap[0].mr_start = start;
39148f64992SBreno Leitao 	initrdmap[0].mr_size = end - start;
39248f64992SBreno Leitao 
39348f64992SBreno Leitao 	asz = excise_reserved_regions(avail, asz, initrdmap, 1);
39448f64992SBreno Leitao 
39548f64992SBreno Leitao 	return (asz);
39648f64992SBreno Leitao }
39748f64992SBreno Leitao 
398fd8cf3beSJustin Hibbits #ifdef POWERNV
399fd8cf3beSJustin Hibbits static int
excise_msi_region(struct mem_region * avail,int asz)400fd8cf3beSJustin Hibbits excise_msi_region(struct mem_region *avail, int asz)
401fd8cf3beSJustin Hibbits {
402fd8cf3beSJustin Hibbits         uint64_t start, end;
403fd8cf3beSJustin Hibbits         struct mem_region initrdmap[1];
404fd8cf3beSJustin Hibbits 
405fd8cf3beSJustin Hibbits 	/*
406fd8cf3beSJustin Hibbits 	 * This range of physical addresses is used to implement optimized
407fd8cf3beSJustin Hibbits 	 * 32 bit MSI interrupts on POWER9. Exclude it to avoid accidentally
408fd8cf3beSJustin Hibbits 	 * using it for DMA, as this will cause an immediate PHB fence.
409fd8cf3beSJustin Hibbits 	 * While we could theoretically turn off this behavior in the ETU,
410fd8cf3beSJustin Hibbits 	 * doing so would break 32-bit MSI, so just reserve the range in
411fd8cf3beSJustin Hibbits 	 * the physical map instead.
412fd8cf3beSJustin Hibbits 	 * See section 4.4.2.8 of the PHB4 specification.
413fd8cf3beSJustin Hibbits 	 */
414fd8cf3beSJustin Hibbits 	start	= 0x00000000ffff0000ul;
415fd8cf3beSJustin Hibbits 	end	= 0x00000000fffffffful;
416fd8cf3beSJustin Hibbits 
417fd8cf3beSJustin Hibbits 	initrdmap[0].mr_start = start;
418fd8cf3beSJustin Hibbits 	initrdmap[0].mr_size = end - start;
419fd8cf3beSJustin Hibbits 
420fd8cf3beSJustin Hibbits 	asz = excise_reserved_regions(avail, asz, initrdmap, 1);
421fd8cf3beSJustin Hibbits 
422fd8cf3beSJustin Hibbits 	return (asz);
423fd8cf3beSJustin Hibbits }
424fd8cf3beSJustin Hibbits #endif
425fd8cf3beSJustin Hibbits 
42648f64992SBreno Leitao static int
excise_fdt_reserved(struct mem_region * avail,int asz)42748f64992SBreno Leitao excise_fdt_reserved(struct mem_region *avail, int asz)
42848f64992SBreno Leitao {
429b52dceb8SShawn Anastasio 	struct mem_region fdtmap[64];
43048f64992SBreno Leitao 	ssize_t fdtmapsize;
43148f64992SBreno Leitao 	phandle_t chosen;
43248f64992SBreno Leitao 	int j, fdtentries;
43348f64992SBreno Leitao 
43448f64992SBreno Leitao 	chosen = OF_finddevice("/chosen");
43548f64992SBreno Leitao 	fdtmapsize = OF_getprop(chosen, "fdtmemreserv", fdtmap, sizeof(fdtmap));
43648f64992SBreno Leitao 
43748f64992SBreno Leitao 	for (j = 0; j < fdtmapsize/sizeof(fdtmap[0]); j++) {
43848f64992SBreno Leitao 		fdtmap[j].mr_start = be64toh(fdtmap[j].mr_start) & ~PAGE_MASK;
43948f64992SBreno Leitao 		fdtmap[j].mr_size = round_page(be64toh(fdtmap[j].mr_size));
44048f64992SBreno Leitao 	}
44148f64992SBreno Leitao 
44248f64992SBreno Leitao 	KASSERT(j*sizeof(fdtmap[0]) < sizeof(fdtmap),
44348f64992SBreno Leitao 	    ("Exceeded number of FDT reservations"));
44448f64992SBreno Leitao 	/* Add a virtual entry for the FDT itself */
44548f64992SBreno Leitao 	if (fdt != NULL) {
44648f64992SBreno Leitao 		fdtmap[j].mr_start = (vm_offset_t)fdt & ~PAGE_MASK;
44748f64992SBreno Leitao 		fdtmap[j].mr_size = round_page(fdt_totalsize(fdt));
44848f64992SBreno Leitao 		fdtmapsize += sizeof(fdtmap[0]);
44948f64992SBreno Leitao 	}
45048f64992SBreno Leitao 
45148f64992SBreno Leitao 	fdtentries = fdtmapsize/sizeof(fdtmap[0]);
45248f64992SBreno Leitao 	asz = excise_reserved_regions(avail, asz, fdtmap, fdtentries);
45348f64992SBreno Leitao 
45448f64992SBreno Leitao 	return (asz);
45548f64992SBreno Leitao }
45635feca37SNathan Whitehorn #endif
457b9d056f3SNathan Whitehorn 
458fe3b4685SNathan Whitehorn /*
459fe3b4685SNathan Whitehorn  * This is called during powerpc_init, before the system is really initialized.
460fe3b4685SNathan Whitehorn  * It shall provide the total and the available regions of RAM.
461b9d056f3SNathan Whitehorn  * The available regions need not take the kernel into account.
462fe3b4685SNathan Whitehorn  */
463fe3b4685SNathan Whitehorn void
ofw_numa_mem_regions(struct numa_mem_region * memp,int * memsz)46449d9a597SJustin Hibbits ofw_numa_mem_regions(struct numa_mem_region *memp, int *memsz)
46549d9a597SJustin Hibbits {
46649d9a597SJustin Hibbits 	phandle_t phandle;
467490ebb8fSJustin Hibbits 	int count, msz;
46849d9a597SJustin Hibbits 	char name[31];
46949d9a597SJustin Hibbits 	struct numa_mem_region *curmemp;
47049d9a597SJustin Hibbits 
47149d9a597SJustin Hibbits 	msz = 0;
47249d9a597SJustin Hibbits 	/*
47349d9a597SJustin Hibbits 	 * Get memory from all the /memory nodes.
47449d9a597SJustin Hibbits 	 */
47549d9a597SJustin Hibbits 	for (phandle = OF_child(OF_peer(0)); phandle != 0;
47649d9a597SJustin Hibbits 	    phandle = OF_peer(phandle)) {
47749d9a597SJustin Hibbits 		if (OF_getprop(phandle, "name", name, sizeof(name)) <= 0)
47849d9a597SJustin Hibbits 			continue;
47949d9a597SJustin Hibbits 		if (strncmp(name, "memory@", strlen("memory@")) != 0)
48049d9a597SJustin Hibbits 			continue;
48149d9a597SJustin Hibbits 
48249d9a597SJustin Hibbits 		count = parse_numa_ofw_memory(phandle, "reg", &memp[msz]);
48349d9a597SJustin Hibbits 		if (count == 0)
48449d9a597SJustin Hibbits 			continue;
48549d9a597SJustin Hibbits 		curmemp = &memp[msz];
48649d9a597SJustin Hibbits 		MPASS(count == 1);
487490ebb8fSJustin Hibbits 		curmemp->mr_domain = platform_node_numa_domain(phandle);
48849d9a597SJustin Hibbits 		if (bootverbose)
48949d9a597SJustin Hibbits 			printf("%s %#jx-%#jx domain(%ju)\n",
49049d9a597SJustin Hibbits 			    name, (uintmax_t)curmemp->mr_start,
49149d9a597SJustin Hibbits 			    (uintmax_t)curmemp->mr_start + curmemp->mr_size,
49249d9a597SJustin Hibbits 			    (uintmax_t)curmemp->mr_domain);
49349d9a597SJustin Hibbits 		msz += count;
49449d9a597SJustin Hibbits 	}
49549d9a597SJustin Hibbits 	*memsz = msz;
49649d9a597SJustin Hibbits }
49749d9a597SJustin Hibbits /*
49849d9a597SJustin Hibbits  * This is called during powerpc_init, before the system is really initialized.
49949d9a597SJustin Hibbits  * It shall provide the total and the available regions of RAM.
50049d9a597SJustin Hibbits  * The available regions need not take the kernel into account.
50149d9a597SJustin Hibbits  */
50249d9a597SJustin Hibbits void
ofw_mem_regions(struct mem_region * memp,int * memsz,struct mem_region * availp,int * availsz)503c1cb22d7SNathan Whitehorn ofw_mem_regions(struct mem_region *memp, int *memsz,
504c1cb22d7SNathan Whitehorn 		struct mem_region *availp, int *availsz)
505fe3b4685SNathan Whitehorn {
506fe3b4685SNathan Whitehorn 	phandle_t phandle;
507c1cb22d7SNathan Whitehorn 	int asz, msz;
508c1cb22d7SNathan Whitehorn 	int res;
509d8c6808aSNathan Whitehorn 	char name[31];
510fe3b4685SNathan Whitehorn 
511fe3b4685SNathan Whitehorn 	asz = msz = 0;
512fe3b4685SNathan Whitehorn 
513fe3b4685SNathan Whitehorn 	/*
514d8c6808aSNathan Whitehorn 	 * Get memory from all the /memory nodes.
515fe3b4685SNathan Whitehorn 	 */
516d8c6808aSNathan Whitehorn 	for (phandle = OF_child(OF_peer(0)); phandle != 0;
517d8c6808aSNathan Whitehorn 	    phandle = OF_peer(phandle)) {
518d8c6808aSNathan Whitehorn 		if (OF_getprop(phandle, "name", name, sizeof(name)) <= 0)
519d8c6808aSNathan Whitehorn 			continue;
520b9d056f3SNathan Whitehorn 		if (strncmp(name, "memory", sizeof(name)) != 0 &&
521b9d056f3SNathan Whitehorn 		    strncmp(name, "memory@", strlen("memory@")) != 0)
522d8c6808aSNathan Whitehorn 			continue;
523fe3b4685SNathan Whitehorn 
524c1cb22d7SNathan Whitehorn 		res = parse_ofw_memory(phandle, "reg", &memp[msz]);
52549d9a597SJustin Hibbits 		msz += res;
526876f3b92SJustin Hibbits 
527876f3b92SJustin Hibbits 		/*
528876f3b92SJustin Hibbits 		 * On POWER9 Systems we might have both linux,usable-memory and
529876f3b92SJustin Hibbits 		 * reg properties.  'reg' denotes all available memory, but we
530876f3b92SJustin Hibbits 		 * must use 'linux,usable-memory', a subset, as some memory
531876f3b92SJustin Hibbits 		 * regions are reserved for NVLink.
532876f3b92SJustin Hibbits 		 */
533876f3b92SJustin Hibbits 		if (OF_getproplen(phandle, "linux,usable-memory") >= 0)
534876f3b92SJustin Hibbits 			res = parse_ofw_memory(phandle, "linux,usable-memory",
535876f3b92SJustin Hibbits 			    &availp[asz]);
536c07c77a3SJustin Hibbits 		else if (OF_getproplen(phandle, "available") >= 0)
537d8c6808aSNathan Whitehorn 			res = parse_ofw_memory(phandle, "available",
538c1cb22d7SNathan Whitehorn 			    &availp[asz]);
539d8c6808aSNathan Whitehorn 		else
540c1cb22d7SNathan Whitehorn 			res = parse_ofw_memory(phandle, "reg", &availp[asz]);
54149d9a597SJustin Hibbits 		asz += res;
542d8c6808aSNathan Whitehorn 	}
543d8c6808aSNathan Whitehorn 
54435feca37SNathan Whitehorn #ifdef FDT
545b9d056f3SNathan Whitehorn 	phandle = OF_finddevice("/chosen");
546b9d056f3SNathan Whitehorn 	if (OF_hasprop(phandle, "fdtmemreserv"))
547b9d056f3SNathan Whitehorn 		asz = excise_fdt_reserved(availp, asz);
54848f64992SBreno Leitao 
54948f64992SBreno Leitao 	/* If the kernel is being loaded through kexec, initrd region is listed
55048f64992SBreno Leitao 	 * in /chosen but the region is not marked as reserved, so, we might exclude
55148f64992SBreno Leitao 	 * it here.
55248f64992SBreno Leitao 	 */
55348f64992SBreno Leitao 	if (OF_hasprop(phandle, "linux,initrd-start"))
55448f64992SBreno Leitao 		asz = excise_initrd_region(availp, asz);
55535feca37SNathan Whitehorn #endif
556b9d056f3SNathan Whitehorn 
557fd8cf3beSJustin Hibbits #ifdef POWERNV
558fd8cf3beSJustin Hibbits 	if (opal_check() == 0)
559fd8cf3beSJustin Hibbits 		asz = excise_msi_region(availp, asz);
560fd8cf3beSJustin Hibbits #endif
561fd8cf3beSJustin Hibbits 
562d8c6808aSNathan Whitehorn 	*memsz = msz;
563c1cb22d7SNathan Whitehorn 	*availsz = asz;
564fe3b4685SNathan Whitehorn }
565fe3b4685SNathan Whitehorn 
566fe3b4685SNathan Whitehorn void
OF_initial_setup(void * fdt_ptr,void * junk,int (* openfirm)(void *))567fe3b4685SNathan Whitehorn OF_initial_setup(void *fdt_ptr, void *junk, int (*openfirm)(void *))
568fe3b4685SNathan Whitehorn {
5696f489d43SJustin Hibbits #ifdef AIM
570bb808254SNathan Whitehorn 	ofmsr[0] = mfmsr();
571bb808254SNathan Whitehorn 	#ifdef __powerpc64__
572bb808254SNathan Whitehorn 	ofmsr[0] &= ~PSL_SF;
573a6625592SBrandon Bergren 	#ifdef __LITTLE_ENDIAN__
574a6625592SBrandon Bergren 	/* Assume OFW is BE. */
575a6625592SBrandon Bergren 	ofmsr[0] &= ~PSL_LE;
576a6625592SBrandon Bergren 	#endif
57762c6b30eSJustin Hibbits 	#else
578bb808254SNathan Whitehorn 	__asm __volatile("mfsprg0 %0" : "=&r"(ofmsr[1]));
57962c6b30eSJustin Hibbits 	#endif
580bb808254SNathan Whitehorn 	__asm __volatile("mfsprg1 %0" : "=&r"(ofmsr[2]));
581bb808254SNathan Whitehorn 	__asm __volatile("mfsprg2 %0" : "=&r"(ofmsr[3]));
582bb808254SNathan Whitehorn 	__asm __volatile("mfsprg3 %0" : "=&r"(ofmsr[4]));
5836f489d43SJustin Hibbits 	openfirmware_entry = openfirm;
584bb808254SNathan Whitehorn 
585fe3b4685SNathan Whitehorn 	if (ofmsr[0] & PSL_DR)
586fe3b4685SNathan Whitehorn 		ofw_real_mode = 0;
587fe3b4685SNathan Whitehorn 	else
588fe3b4685SNathan Whitehorn 		ofw_real_mode = 1;
589fe3b4685SNathan Whitehorn 
5906f489d43SJustin Hibbits 	ofw_save_trap_vec(save_trap_init);
5916f489d43SJustin Hibbits #else
5926f489d43SJustin Hibbits 	ofw_real_mode = 1;
5936f489d43SJustin Hibbits #endif
5946f489d43SJustin Hibbits 
595fe3b4685SNathan Whitehorn 	fdt = fdt_ptr;
596fe3b4685SNathan Whitehorn }
597fe3b4685SNathan Whitehorn 
598fe3b4685SNathan Whitehorn boolean_t
OF_bootstrap(void)599429ba161SPiotr Kubaj OF_bootstrap(void)
600fe3b4685SNathan Whitehorn {
601fe3b4685SNathan Whitehorn 	boolean_t status = FALSE;
6022bfca577SNathan Whitehorn 	int err = 0;
603fe3b4685SNathan Whitehorn 
6046f489d43SJustin Hibbits #ifdef AIM
60517879090SNathan Whitehorn 	if (openfirmware_entry != NULL) {
606fe3b4685SNathan Whitehorn 		if (ofw_real_mode) {
607fe3b4685SNathan Whitehorn 			status = OF_install(OFW_STD_REAL, 0);
608fe3b4685SNathan Whitehorn 		} else {
609fe3b4685SNathan Whitehorn 			#ifdef __powerpc64__
610fe3b4685SNathan Whitehorn 			status = OF_install(OFW_STD_32BIT, 0);
611fe3b4685SNathan Whitehorn 			#else
612fe3b4685SNathan Whitehorn 			status = OF_install(OFW_STD_DIRECT, 0);
613fe3b4685SNathan Whitehorn 			#endif
614fe3b4685SNathan Whitehorn 		}
615fe3b4685SNathan Whitehorn 
616fe3b4685SNathan Whitehorn 		if (status != TRUE)
617fe3b4685SNathan Whitehorn 			return status;
618fe3b4685SNathan Whitehorn 
6192bfca577SNathan Whitehorn 		err = OF_init(openfirmware);
6206f489d43SJustin Hibbits 	} else
6216f489d43SJustin Hibbits #endif
6226f489d43SJustin Hibbits 	if (fdt != NULL) {
62335feca37SNathan Whitehorn #ifdef FDT
624f9edb09dSNathan Whitehorn #ifdef AIM
625f9edb09dSNathan Whitehorn 		bus_space_tag_t fdt_bt;
626f9edb09dSNathan Whitehorn 		vm_offset_t tmp_fdt_ptr;
627f9edb09dSNathan Whitehorn 		vm_size_t fdt_size;
628f9edb09dSNathan Whitehorn 		uintptr_t fdt_va;
629f9edb09dSNathan Whitehorn #endif
630fe3b4685SNathan Whitehorn 
631f9edb09dSNathan Whitehorn 		status = OF_install(OFW_FDT, 0);
632fe3b4685SNathan Whitehorn 		if (status != TRUE)
633fe3b4685SNathan Whitehorn 			return status;
634fe3b4685SNathan Whitehorn 
635f9edb09dSNathan Whitehorn #ifdef AIM /* AIM-only for now -- Book-E does this remapping in early init */
636f9edb09dSNathan Whitehorn 		/* Get the FDT size for mapping if we can */
637f9edb09dSNathan Whitehorn 		tmp_fdt_ptr = pmap_early_io_map((vm_paddr_t)fdt, PAGE_SIZE);
638f9edb09dSNathan Whitehorn 		if (fdt_check_header((void *)tmp_fdt_ptr) != 0) {
639f9edb09dSNathan Whitehorn 			pmap_early_io_unmap(tmp_fdt_ptr, PAGE_SIZE);
640f9edb09dSNathan Whitehorn 			return FALSE;
641fe3b4685SNathan Whitehorn 		}
642f9edb09dSNathan Whitehorn 		fdt_size = fdt_totalsize((void *)tmp_fdt_ptr);
643f9edb09dSNathan Whitehorn 		pmap_early_io_unmap(tmp_fdt_ptr, PAGE_SIZE);
644f9edb09dSNathan Whitehorn 
645f9edb09dSNathan Whitehorn 		/*
646f9edb09dSNathan Whitehorn 		 * Map this for real. Use bus_space_map() to take advantage
647f9edb09dSNathan Whitehorn 		 * of its auto-remapping function once the kernel is loaded.
648f9edb09dSNathan Whitehorn 		 * This is a dirty hack, but what we have.
649f9edb09dSNathan Whitehorn 		 */
650a6625592SBrandon Bergren #ifdef __LITTLE_ENDIAN__
651f9edb09dSNathan Whitehorn 		fdt_bt = &bs_le_tag;
652f9edb09dSNathan Whitehorn #else
653f9edb09dSNathan Whitehorn 		fdt_bt = &bs_be_tag;
654f9edb09dSNathan Whitehorn #endif
655f9edb09dSNathan Whitehorn 		bus_space_map(fdt_bt, (vm_paddr_t)fdt, fdt_size, 0, &fdt_va);
656f9edb09dSNathan Whitehorn 
657f9edb09dSNathan Whitehorn 		err = OF_init((void *)fdt_va);
658f9edb09dSNathan Whitehorn #else
659f9edb09dSNathan Whitehorn 		err = OF_init(fdt);
660f9edb09dSNathan Whitehorn #endif
66135feca37SNathan Whitehorn #endif
662f9edb09dSNathan Whitehorn 	}
663f9edb09dSNathan Whitehorn 
664f9edb09dSNathan Whitehorn 	#ifdef FDT_DTB_STATIC
665f9edb09dSNathan Whitehorn 	/*
666f9edb09dSNathan Whitehorn 	 * Check for a statically included blob already in the kernel and
667f9edb09dSNathan Whitehorn 	 * needing no mapping.
668f9edb09dSNathan Whitehorn 	 */
669f9edb09dSNathan Whitehorn 	else {
670f9edb09dSNathan Whitehorn 		status = OF_install(OFW_FDT, 0);
671f9edb09dSNathan Whitehorn 		if (status != TRUE)
672f9edb09dSNathan Whitehorn 			return status;
673f9edb09dSNathan Whitehorn 		err = OF_init(&fdt_static_dtb);
674f9edb09dSNathan Whitehorn 	}
675f9edb09dSNathan Whitehorn 	#endif
676fe3b4685SNathan Whitehorn 
6772bfca577SNathan Whitehorn 	if (err != 0) {
6782bfca577SNathan Whitehorn 		OF_install(NULL, 0);
6792bfca577SNathan Whitehorn 		status = FALSE;
6802bfca577SNathan Whitehorn 	}
6812bfca577SNathan Whitehorn 
682fe3b4685SNathan Whitehorn 	return (status);
683fe3b4685SNathan Whitehorn }
684fe3b4685SNathan Whitehorn 
6856f489d43SJustin Hibbits #ifdef AIM
6869f706727SNathan Whitehorn void
ofw_quiesce(void)687fe3b4685SNathan Whitehorn ofw_quiesce(void)
688fe3b4685SNathan Whitehorn {
689fe3b4685SNathan Whitehorn 	struct {
690fe3b4685SNathan Whitehorn 		cell_t name;
691fe3b4685SNathan Whitehorn 		cell_t nargs;
692fe3b4685SNathan Whitehorn 		cell_t nreturns;
693fe3b4685SNathan Whitehorn 	} args;
694fe3b4685SNathan Whitehorn 
6959f706727SNathan Whitehorn 	KASSERT(!pmap_bootstrapped, ("Cannot call ofw_quiesce after VM is up"));
696fe3b4685SNathan Whitehorn 
697fe3b4685SNathan Whitehorn 	args.name = (cell_t)(uintptr_t)"quiesce";
698fe3b4685SNathan Whitehorn 	args.nargs = 0;
699fe3b4685SNathan Whitehorn 	args.nreturns = 0;
700fe3b4685SNathan Whitehorn 	openfirmware(&args);
701fe3b4685SNathan Whitehorn }
702fe3b4685SNathan Whitehorn 
703fe3b4685SNathan Whitehorn static int
openfirmware_core(void * args)704fe3b4685SNathan Whitehorn openfirmware_core(void *args)
705fe3b4685SNathan Whitehorn {
706fe3b4685SNathan Whitehorn 	int		result;
707fe3b4685SNathan Whitehorn 	register_t	oldmsr;
708fe3b4685SNathan Whitehorn 
70944d29d47SNathan Whitehorn 	if (openfirmware_entry == NULL)
71044d29d47SNathan Whitehorn 		return (-1);
71144d29d47SNathan Whitehorn 
712fe3b4685SNathan Whitehorn 	/*
713fe3b4685SNathan Whitehorn 	 * Turn off exceptions - we really don't want to end up
7144aa3cee6SNathan Whitehorn 	 * anywhere unexpected with PCPU set to something strange
7154aa3cee6SNathan Whitehorn 	 * or the stack pointer wrong.
716fe3b4685SNathan Whitehorn 	 */
717fe3b4685SNathan Whitehorn 	oldmsr = intr_disable();
718fe3b4685SNathan Whitehorn 
7194aa3cee6SNathan Whitehorn 	ofw_sprg_prepare();
7204aa3cee6SNathan Whitehorn 
721f367ffdeSAndreas Tobler 	/* Save trap vectors */
722f367ffdeSAndreas Tobler 	ofw_save_trap_vec(save_trap_of);
723f367ffdeSAndreas Tobler 
724f367ffdeSAndreas Tobler 	/* Restore initially saved trap vectors */
725f367ffdeSAndreas Tobler 	ofw_restore_trap_vec(save_trap_init);
726f367ffdeSAndreas Tobler 
72795ce4c00SJustin Hibbits #ifndef __powerpc64__
728fe3b4685SNathan Whitehorn 	/*
729fe3b4685SNathan Whitehorn 	 * Clear battable[] translations
730fe3b4685SNathan Whitehorn 	 */
731fe3b4685SNathan Whitehorn 	if (!(cpu_features & PPC_FEATURE_64))
732fe3b4685SNathan Whitehorn 		__asm __volatile("mtdbatu 2, %0\n"
733fe3b4685SNathan Whitehorn 				 "mtdbatu 3, %0" : : "r" (0));
734fe3b4685SNathan Whitehorn 	isync();
735fe3b4685SNathan Whitehorn #endif
736fe3b4685SNathan Whitehorn 
737fe3b4685SNathan Whitehorn 	result = ofwcall(args);
738f367ffdeSAndreas Tobler 
739f367ffdeSAndreas Tobler 	/* Restore trap vecotrs */
740f367ffdeSAndreas Tobler 	ofw_restore_trap_vec(save_trap_of);
741f367ffdeSAndreas Tobler 
7424aa3cee6SNathan Whitehorn 	ofw_sprg_restore();
7434aa3cee6SNathan Whitehorn 
744fe3b4685SNathan Whitehorn 	intr_restore(oldmsr);
745fe3b4685SNathan Whitehorn 
746fe3b4685SNathan Whitehorn 	return (result);
747fe3b4685SNathan Whitehorn }
748fe3b4685SNathan Whitehorn 
749fe3b4685SNathan Whitehorn #ifdef SMP
750fe3b4685SNathan Whitehorn struct ofw_rv_args {
751fe3b4685SNathan Whitehorn 	void *args;
752fe3b4685SNathan Whitehorn 	int retval;
753fe3b4685SNathan Whitehorn 	volatile int in_progress;
754fe3b4685SNathan Whitehorn };
755fe3b4685SNathan Whitehorn 
756fe3b4685SNathan Whitehorn static void
ofw_rendezvous_dispatch(void * xargs)757fe3b4685SNathan Whitehorn ofw_rendezvous_dispatch(void *xargs)
758fe3b4685SNathan Whitehorn {
759fe3b4685SNathan Whitehorn 	struct ofw_rv_args *rv_args = xargs;
760fe3b4685SNathan Whitehorn 
761fe3b4685SNathan Whitehorn 	/* NOTE: Interrupts are disabled here */
762fe3b4685SNathan Whitehorn 
763fe3b4685SNathan Whitehorn 	if (PCPU_GET(cpuid) == 0) {
764fe3b4685SNathan Whitehorn 		/*
765fe3b4685SNathan Whitehorn 		 * Execute all OF calls on CPU 0
766fe3b4685SNathan Whitehorn 		 */
767fe3b4685SNathan Whitehorn 		rv_args->retval = openfirmware_core(rv_args->args);
768fe3b4685SNathan Whitehorn 		rv_args->in_progress = 0;
769fe3b4685SNathan Whitehorn 	} else {
770fe3b4685SNathan Whitehorn 		/*
771fe3b4685SNathan Whitehorn 		 * Spin with interrupts off on other CPUs while OF has
772fe3b4685SNathan Whitehorn 		 * control of the machine.
773fe3b4685SNathan Whitehorn 		 */
774fe3b4685SNathan Whitehorn 		while (rv_args->in_progress)
775fe3b4685SNathan Whitehorn 			cpu_spinwait();
776fe3b4685SNathan Whitehorn 	}
777fe3b4685SNathan Whitehorn }
778fe3b4685SNathan Whitehorn #endif
779fe3b4685SNathan Whitehorn 
780fe3b4685SNathan Whitehorn static int
openfirmware(void * args)781fe3b4685SNathan Whitehorn openfirmware(void *args)
782fe3b4685SNathan Whitehorn {
783fe3b4685SNathan Whitehorn 	int result;
784fe3b4685SNathan Whitehorn 	#ifdef SMP
785fe3b4685SNathan Whitehorn 	struct ofw_rv_args rv_args;
78644d29d47SNathan Whitehorn 	#endif
787fe3b4685SNathan Whitehorn 
78844d29d47SNathan Whitehorn 	if (openfirmware_entry == NULL)
78944d29d47SNathan Whitehorn 		return (-1);
79044d29d47SNathan Whitehorn 
79144d29d47SNathan Whitehorn 	#ifdef SMP
79291419bdaSNathan Whitehorn 	if (cold) {
79391419bdaSNathan Whitehorn 		result = openfirmware_core(args);
79491419bdaSNathan Whitehorn 	} else {
795fe3b4685SNathan Whitehorn 		rv_args.args = args;
796fe3b4685SNathan Whitehorn 		rv_args.in_progress = 1;
79791419bdaSNathan Whitehorn 		smp_rendezvous(smp_no_rendezvous_barrier,
79891419bdaSNathan Whitehorn 		    ofw_rendezvous_dispatch, smp_no_rendezvous_barrier,
79991419bdaSNathan Whitehorn 		    &rv_args);
800fe3b4685SNathan Whitehorn 		result = rv_args.retval;
80191419bdaSNathan Whitehorn 	}
802fe3b4685SNathan Whitehorn 	#else
803fe3b4685SNathan Whitehorn 	result = openfirmware_core(args);
804fe3b4685SNathan Whitehorn 	#endif
805fe3b4685SNathan Whitehorn 
806fe3b4685SNathan Whitehorn 	return (result);
807fe3b4685SNathan Whitehorn }
808fe3b4685SNathan Whitehorn 
809fe3b4685SNathan Whitehorn void
OF_reboot(void)810429ba161SPiotr Kubaj OF_reboot(void)
811fe3b4685SNathan Whitehorn {
812fe3b4685SNathan Whitehorn 	struct {
813fe3b4685SNathan Whitehorn 		cell_t name;
814fe3b4685SNathan Whitehorn 		cell_t nargs;
815fe3b4685SNathan Whitehorn 		cell_t nreturns;
816fe3b4685SNathan Whitehorn 		cell_t arg;
817fe3b4685SNathan Whitehorn 	} args;
818fe3b4685SNathan Whitehorn 
819fe3b4685SNathan Whitehorn 	args.name = (cell_t)(uintptr_t)"interpret";
820fe3b4685SNathan Whitehorn 	args.nargs = 1;
821fe3b4685SNathan Whitehorn 	args.nreturns = 0;
822fe3b4685SNathan Whitehorn 	args.arg = (cell_t)(uintptr_t)"reset-all";
823fe3b4685SNathan Whitehorn 	openfirmware_core(&args); /* Don't do rendezvous! */
824fe3b4685SNathan Whitehorn 
825fe3b4685SNathan Whitehorn 	for (;;);	/* just in case */
826fe3b4685SNathan Whitehorn }
827fe3b4685SNathan Whitehorn 
828629aa519SNathan Whitehorn #endif /* AIM */
829629aa519SNathan Whitehorn 
830fe3b4685SNathan Whitehorn void
OF_getetheraddr(device_t dev,u_char * addr)831fe3b4685SNathan Whitehorn OF_getetheraddr(device_t dev, u_char *addr)
832fe3b4685SNathan Whitehorn {
833fe3b4685SNathan Whitehorn 	phandle_t	node;
834fe3b4685SNathan Whitehorn 
835fe3b4685SNathan Whitehorn 	node = ofw_bus_get_node(dev);
836fe3b4685SNathan Whitehorn 	OF_getprop(node, "local-mac-address", addr, ETHER_ADDR_LEN);
837fe3b4685SNathan Whitehorn }
838fe3b4685SNathan Whitehorn 
839fe3b4685SNathan Whitehorn /*
840fe3b4685SNathan Whitehorn  * Return a bus handle and bus tag that corresponds to the register
841fe3b4685SNathan Whitehorn  * numbered regno for the device referenced by the package handle
842fe3b4685SNathan Whitehorn  * dev. This function is intended to be used by console drivers in
843fe3b4685SNathan Whitehorn  * early boot only. It works by mapping the address of the device's
844fe3b4685SNathan Whitehorn  * register in the address space of its parent and recursively walk
845fe3b4685SNathan Whitehorn  * the device tree upward this way.
846fe3b4685SNathan Whitehorn  */
847fe3b4685SNathan Whitehorn int
OF_decode_addr(phandle_t dev,int regno,bus_space_tag_t * tag,bus_space_handle_t * handle,bus_size_t * sz)848fe3b4685SNathan Whitehorn OF_decode_addr(phandle_t dev, int regno, bus_space_tag_t *tag,
84945fd1862SAndrew Turner     bus_space_handle_t *handle, bus_size_t *sz)
850fe3b4685SNathan Whitehorn {
851bc7b9300SIan Lepore 	bus_addr_t addr;
852bc7b9300SIan Lepore 	bus_size_t size;
853bc7b9300SIan Lepore 	pcell_t pci_hi;
854bc7b9300SIan Lepore 	int flags, res;
855fe3b4685SNathan Whitehorn 
856bc7b9300SIan Lepore 	res = ofw_reg_to_paddr(dev, regno, &addr, &size, &pci_hi);
857bc7b9300SIan Lepore 	if (res < 0)
858bc7b9300SIan Lepore 		return (res);
859fe3b4685SNathan Whitehorn 
860bc7b9300SIan Lepore 	if (pci_hi == OFW_PADDR_NOT_PCI) {
86143a581e1SNathan Whitehorn 		*tag = &bs_be_tag;
862bc7b9300SIan Lepore 		flags = 0;
863bc7b9300SIan Lepore 	} else {
86443a581e1SNathan Whitehorn 		*tag = &bs_le_tag;
865bc7b9300SIan Lepore 		flags = (pci_hi & OFW_PCI_PHYS_HI_PREFETCHABLE) ?
866bc7b9300SIan Lepore 		    BUS_SPACE_MAP_PREFETCHABLE: 0;
867fe3b4685SNathan Whitehorn 	}
868fe3b4685SNathan Whitehorn 
86945fd1862SAndrew Turner 	if (sz != NULL)
87045fd1862SAndrew Turner 		*sz = size;
87145fd1862SAndrew Turner 
872bc7b9300SIan Lepore 	return (bus_space_map(*tag, addr, size, flags, handle));
873fe3b4685SNathan Whitehorn }
874