2 * Copyright (C) 1996 Wolfgang Solfrank.
3 * Copyright (C) 1996 TooLs GmbH.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by TooLs GmbH.
17 * 4. The name of TooLs GmbH may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``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 TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * $NetBSD: ofw_machdep.c,v 1.5 2000/05/23 13:25:43 tsubai Exp $
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "opt_platform.h"
38 #include <sys/param.h>
40 #include <sys/systm.h>
43 #include <sys/fcntl.h>
44 #include <sys/malloc.h>
47 #include <sys/endian.h>
49 #include <net/ethernet.h>
51 #include <dev/fdt/fdt_common.h>
52 #include <dev/ofw/openfirm.h>
53 #include <dev/ofw/ofw_pci.h>
54 #include <dev/ofw/ofw_bus.h>
55 #include <dev/ofw/ofw_subr.h>
58 #include <vm/vm_param.h>
59 #include <vm/vm_page.h>
61 #include <machine/bus.h>
62 #include <machine/cpu.h>
63 #include <machine/md_var.h>
64 #include <machine/platform.h>
65 #include <machine/ofw_machdep.h>
66 #include <machine/trap.h>
72 extern register_t ofmsr[5];
73 extern void *openfirmware_entry;
74 char save_trap_init[0x2f00]; /* EXC_LAST */
75 char save_trap_of[0x2f00]; /* EXC_LAST */
78 static int openfirmware(void *args);
81 ofw_save_trap_vec(char *save_trap_vec)
86 bcopy((void *)EXC_RST, save_trap_vec, EXC_LAST - EXC_RST);
90 ofw_restore_trap_vec(char *restore_trap_vec)
95 bcopy(restore_trap_vec, (void *)EXC_RST, EXC_LAST - EXC_RST);
96 __syncicache(EXC_RSVD, EXC_LAST - EXC_RSVD);
100 * Saved SPRG0-3 from OpenFirmware. Will be restored prior to the callback.
102 register_t ofw_sprg0_save;
105 ofw_sprg_prepare(void)
111 * Assume that interrupt are disabled at this point, or
112 * SPRG1-3 could be trashed
115 __asm __volatile("mtsprg1 %0\n\t"
123 __asm __volatile("mfsprg0 %0\n\t"
128 : "=&r"(ofw_sprg0_save)
137 ofw_sprg_restore(void)
143 * Note that SPRG1-3 contents are irrelevant. They are scratch
144 * registers used in the early portion of trap handling when
145 * interrupts are disabled.
147 * PCPU data cannot be used until this routine is called !
149 #ifndef __powerpc64__
150 __asm __volatile("mtsprg0 %0" :: "r"(ofw_sprg0_save));
156 parse_ofw_memory(phandle_t node, const char *prop, struct mem_region *output)
158 cell_t address_cells, size_cells;
159 cell_t OFmem[4 * PHYS_AVAIL_SZ];
166 * Get #address-cells from root node, defaulting to 1 if it cannot
169 phandle = OF_finddevice("/");
170 if (OF_getencprop(phandle, "#address-cells", &address_cells,
171 sizeof(address_cells)) < (ssize_t)sizeof(address_cells))
173 if (OF_getencprop(phandle, "#size-cells", &size_cells,
174 sizeof(size_cells)) < (ssize_t)sizeof(size_cells))
180 if (node == -1 || (sz = OF_getencprop(node, prop,
181 OFmem, sizeof(OFmem))) <= 0)
182 panic("Physical memory map not found");
186 while (i < sz/sizeof(cell_t)) {
187 #if !defined(__powerpc64__) && !defined(BOOKE)
188 /* On 32-bit PPC (OEA), ignore regions starting above 4 GB */
189 if (address_cells > 1 && OFmem[i] > 0) {
190 i += address_cells + size_cells;
195 output[j].mr_start = OFmem[i++];
196 if (address_cells == 2) {
197 output[j].mr_start <<= 32;
198 output[j].mr_start += OFmem[i++];
201 output[j].mr_size = OFmem[i++];
202 if (size_cells == 2) {
203 output[j].mr_size <<= 32;
204 output[j].mr_size += OFmem[i++];
207 #if !defined(__powerpc64__) && !defined(BOOKE)
208 /* Book-E can support 36-bit addresses. */
210 * Check for memory regions extending above 32-bit
211 * memory space, and restrict them to stay there.
213 if (((uint64_t)output[j].mr_start +
214 (uint64_t)output[j].mr_size) >
215 BUS_SPACE_MAXADDR_32BIT) {
216 output[j].mr_size = BUS_SPACE_MAXADDR_32BIT -
223 sz = j*sizeof(output[0]);
229 excise_fdt_reserved(struct mem_region *avail, int asz)
239 chosen = OF_finddevice("/chosen");
240 fdtmapsize = OF_getprop(chosen, "fdtmemreserv", fdtmap, sizeof(fdtmap));
242 for (j = 0; j < fdtmapsize/sizeof(fdtmap[0]); j++) {
243 fdtmap[j].address = be64toh(fdtmap[j].address);
244 fdtmap[j].size = be64toh(fdtmap[j].size);
247 for (i = 0; i < asz; i++) {
248 for (j = 0; j < fdtmapsize/sizeof(fdtmap[0]); j++) {
250 * Case 1: Exclusion region encloses complete
251 * available entry. Drop it and move on.
253 if (fdtmap[j].address <= avail[i].mr_start &&
254 fdtmap[j].address + fdtmap[j].size >=
255 avail[i].mr_start + avail[i].mr_size) {
256 for (k = i+1; k < asz; k++)
257 avail[k-1] = avail[k];
259 i--; /* Repeat some entries */
264 * Case 2: Exclusion region starts in available entry.
265 * Trim it to where the entry begins and append
266 * a new available entry with the region after
267 * the excluded region, if any.
269 if (fdtmap[j].address >= avail[i].mr_start &&
270 fdtmap[j].address < avail[i].mr_start +
272 if (fdtmap[j].address + fdtmap[j].size <
273 avail[i].mr_start + avail[i].mr_size) {
274 avail[asz].mr_start =
275 fdtmap[j].address + fdtmap[j].size;
276 avail[asz].mr_size = avail[i].mr_start +
282 avail[i].mr_size = fdtmap[j].address -
287 * Case 3: Exclusion region ends in available entry.
288 * Move start point to where the exclusion zone ends.
289 * The case of a contained exclusion zone has already
290 * been caught in case 2.
292 if (fdtmap[j].address + fdtmap[j].size >=
293 avail[i].mr_start && fdtmap[j].address +
294 fdtmap[j].size < avail[i].mr_start +
296 avail[i].mr_size += avail[i].mr_start;
298 fdtmap[j].address + fdtmap[j].size;
299 avail[i].mr_size -= avail[i].mr_start;
308 * This is called during powerpc_init, before the system is really initialized.
309 * It shall provide the total and the available regions of RAM.
310 * The available regions need not take the kernel into account.
313 ofw_mem_regions(struct mem_region *memp, int *memsz,
314 struct mem_region *availp, int *availsz)
324 * Get memory from all the /memory nodes.
326 for (phandle = OF_child(OF_peer(0)); phandle != 0;
327 phandle = OF_peer(phandle)) {
328 if (OF_getprop(phandle, "name", name, sizeof(name)) <= 0)
330 if (strncmp(name, "memory", sizeof(name)) != 0 &&
331 strncmp(name, "memory@", strlen("memory@")) != 0)
334 res = parse_ofw_memory(phandle, "reg", &memp[msz]);
335 msz += res/sizeof(struct mem_region);
336 if (OF_getproplen(phandle, "available") >= 0)
337 res = parse_ofw_memory(phandle, "available",
340 res = parse_ofw_memory(phandle, "reg", &availp[asz]);
341 asz += res/sizeof(struct mem_region);
344 phandle = OF_finddevice("/chosen");
345 if (OF_hasprop(phandle, "fdtmemreserv"))
346 asz = excise_fdt_reserved(availp, asz);
353 OF_initial_setup(void *fdt_ptr, void *junk, int (*openfirm)(void *))
360 __asm __volatile("mfsprg0 %0" : "=&r"(ofmsr[1]));
362 __asm __volatile("mfsprg1 %0" : "=&r"(ofmsr[2]));
363 __asm __volatile("mfsprg2 %0" : "=&r"(ofmsr[3]));
364 __asm __volatile("mfsprg3 %0" : "=&r"(ofmsr[4]));
365 openfirmware_entry = openfirm;
367 if (ofmsr[0] & PSL_DR)
372 ofw_save_trap_vec(save_trap_init);
379 #ifdef FDT_DTB_STATIC
380 /* Check for a statically included blob */
382 fdt = &fdt_static_dtb;
389 boolean_t status = FALSE;
392 if (openfirmware_entry != NULL) {
394 status = OF_install(OFW_STD_REAL, 0);
397 status = OF_install(OFW_STD_32BIT, 0);
399 status = OF_install(OFW_STD_DIRECT, 0);
406 OF_init(openfirmware);
410 status = OF_install(OFW_FDT, 0);
416 OF_interpret("perform-fixup", 0);
432 KASSERT(!pmap_bootstrapped, ("Cannot call ofw_quiesce after VM is up"));
434 args.name = (cell_t)(uintptr_t)"quiesce";
441 openfirmware_core(void *args)
446 if (openfirmware_entry == NULL)
450 * Turn off exceptions - we really don't want to end up
451 * anywhere unexpected with PCPU set to something strange
452 * or the stack pointer wrong.
454 oldmsr = intr_disable();
458 /* Save trap vectors */
459 ofw_save_trap_vec(save_trap_of);
461 /* Restore initially saved trap vectors */
462 ofw_restore_trap_vec(save_trap_init);
464 #if defined(AIM) && !defined(__powerpc64__)
466 * Clear battable[] translations
468 if (!(cpu_features & PPC_FEATURE_64))
469 __asm __volatile("mtdbatu 2, %0\n"
470 "mtdbatu 3, %0" : : "r" (0));
474 result = ofwcall(args);
476 /* Restore trap vecotrs */
477 ofw_restore_trap_vec(save_trap_of);
481 intr_restore(oldmsr);
490 volatile int in_progress;
494 ofw_rendezvous_dispatch(void *xargs)
496 struct ofw_rv_args *rv_args = xargs;
498 /* NOTE: Interrupts are disabled here */
500 if (PCPU_GET(cpuid) == 0) {
502 * Execute all OF calls on CPU 0
504 rv_args->retval = openfirmware_core(rv_args->args);
505 rv_args->in_progress = 0;
508 * Spin with interrupts off on other CPUs while OF has
509 * control of the machine.
511 while (rv_args->in_progress)
518 openfirmware(void *args)
522 struct ofw_rv_args rv_args;
525 if (openfirmware_entry == NULL)
530 rv_args.in_progress = 1;
531 smp_rendezvous(smp_no_rendezvous_barrier, ofw_rendezvous_dispatch,
532 smp_no_rendezvous_barrier, &rv_args);
533 result = rv_args.retval;
535 result = openfirmware_core(args);
551 args.name = (cell_t)(uintptr_t)"interpret";
554 args.arg = (cell_t)(uintptr_t)"reset-all";
555 openfirmware_core(&args); /* Don't do rendezvous! */
557 for (;;); /* just in case */
563 OF_getetheraddr(device_t dev, u_char *addr)
567 node = ofw_bus_get_node(dev);
568 OF_getprop(node, "local-mac-address", addr, ETHER_ADDR_LEN);
572 * Return a bus handle and bus tag that corresponds to the register
573 * numbered regno for the device referenced by the package handle
574 * dev. This function is intended to be used by console drivers in
575 * early boot only. It works by mapping the address of the device's
576 * register in the address space of its parent and recursively walk
577 * the device tree upward this way.
580 OF_decode_addr(phandle_t dev, int regno, bus_space_tag_t *tag,
581 bus_space_handle_t *handle, bus_size_t *sz)
588 res = ofw_reg_to_paddr(dev, regno, &addr, &size, &pci_hi);
592 if (pci_hi == OFW_PADDR_NOT_PCI) {
597 flags = (pci_hi & OFW_PCI_PHYS_HI_PREFETCHABLE) ?
598 BUS_SPACE_MAP_PREFETCHABLE: 0;
604 return (bus_space_map(*tag, addr, size, flags, handle));