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 output[j].mr_start = OFmem[i++];
188 if (address_cells == 2) {
189 output[j].mr_start <<= 32;
190 output[j].mr_start += OFmem[i++];
193 output[j].mr_size = OFmem[i++];
194 if (size_cells == 2) {
195 output[j].mr_size <<= 32;
196 output[j].mr_size += OFmem[i++];
199 if (output[j].mr_start > BUS_SPACE_MAXADDR)
203 * Constrain memory to that which we can access.
204 * 32-bit AIM can only reference 32 bits of address currently,
205 * but Book-E can access 36 bits.
207 if (((uint64_t)output[j].mr_start +
208 (uint64_t)output[j].mr_size - 1) >
210 output[j].mr_size = BUS_SPACE_MAXADDR -
211 output[j].mr_start + 1;
216 sz = j*sizeof(output[0]);
222 excise_fdt_reserved(struct mem_region *avail, int asz)
232 chosen = OF_finddevice("/chosen");
233 fdtmapsize = OF_getprop(chosen, "fdtmemreserv", fdtmap, sizeof(fdtmap));
235 for (j = 0; j < fdtmapsize/sizeof(fdtmap[0]); j++) {
236 fdtmap[j].address = be64toh(fdtmap[j].address);
237 fdtmap[j].size = be64toh(fdtmap[j].size);
240 for (i = 0; i < asz; i++) {
241 for (j = 0; j < fdtmapsize/sizeof(fdtmap[0]); j++) {
243 * Case 1: Exclusion region encloses complete
244 * available entry. Drop it and move on.
246 if (fdtmap[j].address <= avail[i].mr_start &&
247 fdtmap[j].address + fdtmap[j].size >=
248 avail[i].mr_start + avail[i].mr_size) {
249 for (k = i+1; k < asz; k++)
250 avail[k-1] = avail[k];
252 i--; /* Repeat some entries */
257 * Case 2: Exclusion region starts in available entry.
258 * Trim it to where the entry begins and append
259 * a new available entry with the region after
260 * the excluded region, if any.
262 if (fdtmap[j].address >= avail[i].mr_start &&
263 fdtmap[j].address < avail[i].mr_start +
265 if (fdtmap[j].address + fdtmap[j].size <
266 avail[i].mr_start + avail[i].mr_size) {
267 avail[asz].mr_start =
268 fdtmap[j].address + fdtmap[j].size;
269 avail[asz].mr_size = avail[i].mr_start +
275 avail[i].mr_size = fdtmap[j].address -
280 * Case 3: Exclusion region ends in available entry.
281 * Move start point to where the exclusion zone ends.
282 * The case of a contained exclusion zone has already
283 * been caught in case 2.
285 if (fdtmap[j].address + fdtmap[j].size >=
286 avail[i].mr_start && fdtmap[j].address +
287 fdtmap[j].size < avail[i].mr_start +
289 avail[i].mr_size += avail[i].mr_start;
291 fdtmap[j].address + fdtmap[j].size;
292 avail[i].mr_size -= avail[i].mr_start;
301 * This is called during powerpc_init, before the system is really initialized.
302 * It shall provide the total and the available regions of RAM.
303 * The available regions need not take the kernel into account.
306 ofw_mem_regions(struct mem_region *memp, int *memsz,
307 struct mem_region *availp, int *availsz)
317 * Get memory from all the /memory nodes.
319 for (phandle = OF_child(OF_peer(0)); phandle != 0;
320 phandle = OF_peer(phandle)) {
321 if (OF_getprop(phandle, "name", name, sizeof(name)) <= 0)
323 if (strncmp(name, "memory", sizeof(name)) != 0 &&
324 strncmp(name, "memory@", strlen("memory@")) != 0)
327 res = parse_ofw_memory(phandle, "reg", &memp[msz]);
328 msz += res/sizeof(struct mem_region);
329 if (OF_getproplen(phandle, "available") >= 0)
330 res = parse_ofw_memory(phandle, "available",
333 res = parse_ofw_memory(phandle, "reg", &availp[asz]);
334 asz += res/sizeof(struct mem_region);
337 phandle = OF_finddevice("/chosen");
338 if (OF_hasprop(phandle, "fdtmemreserv"))
339 asz = excise_fdt_reserved(availp, asz);
346 OF_initial_setup(void *fdt_ptr, void *junk, int (*openfirm)(void *))
353 __asm __volatile("mfsprg0 %0" : "=&r"(ofmsr[1]));
355 __asm __volatile("mfsprg1 %0" : "=&r"(ofmsr[2]));
356 __asm __volatile("mfsprg2 %0" : "=&r"(ofmsr[3]));
357 __asm __volatile("mfsprg3 %0" : "=&r"(ofmsr[4]));
358 openfirmware_entry = openfirm;
360 if (ofmsr[0] & PSL_DR)
365 ofw_save_trap_vec(save_trap_init);
372 #ifdef FDT_DTB_STATIC
373 /* Check for a statically included blob */
375 fdt = &fdt_static_dtb;
382 boolean_t status = FALSE;
385 if (openfirmware_entry != NULL) {
387 status = OF_install(OFW_STD_REAL, 0);
390 status = OF_install(OFW_STD_32BIT, 0);
392 status = OF_install(OFW_STD_DIRECT, 0);
399 OF_init(openfirmware);
403 status = OF_install(OFW_FDT, 0);
409 OF_interpret("perform-fixup", 0);
425 KASSERT(!pmap_bootstrapped, ("Cannot call ofw_quiesce after VM is up"));
427 args.name = (cell_t)(uintptr_t)"quiesce";
434 openfirmware_core(void *args)
439 if (openfirmware_entry == NULL)
443 * Turn off exceptions - we really don't want to end up
444 * anywhere unexpected with PCPU set to something strange
445 * or the stack pointer wrong.
447 oldmsr = intr_disable();
451 /* Save trap vectors */
452 ofw_save_trap_vec(save_trap_of);
454 /* Restore initially saved trap vectors */
455 ofw_restore_trap_vec(save_trap_init);
457 #ifndef __powerpc64__
459 * Clear battable[] translations
461 if (!(cpu_features & PPC_FEATURE_64))
462 __asm __volatile("mtdbatu 2, %0\n"
463 "mtdbatu 3, %0" : : "r" (0));
467 result = ofwcall(args);
469 /* Restore trap vecotrs */
470 ofw_restore_trap_vec(save_trap_of);
474 intr_restore(oldmsr);
483 volatile int in_progress;
487 ofw_rendezvous_dispatch(void *xargs)
489 struct ofw_rv_args *rv_args = xargs;
491 /* NOTE: Interrupts are disabled here */
493 if (PCPU_GET(cpuid) == 0) {
495 * Execute all OF calls on CPU 0
497 rv_args->retval = openfirmware_core(rv_args->args);
498 rv_args->in_progress = 0;
501 * Spin with interrupts off on other CPUs while OF has
502 * control of the machine.
504 while (rv_args->in_progress)
511 openfirmware(void *args)
515 struct ofw_rv_args rv_args;
518 if (openfirmware_entry == NULL)
523 rv_args.in_progress = 1;
524 smp_rendezvous(smp_no_rendezvous_barrier, ofw_rendezvous_dispatch,
525 smp_no_rendezvous_barrier, &rv_args);
526 result = rv_args.retval;
528 result = openfirmware_core(args);
544 args.name = (cell_t)(uintptr_t)"interpret";
547 args.arg = (cell_t)(uintptr_t)"reset-all";
548 openfirmware_core(&args); /* Don't do rendezvous! */
550 for (;;); /* just in case */
556 OF_getetheraddr(device_t dev, u_char *addr)
560 node = ofw_bus_get_node(dev);
561 OF_getprop(node, "local-mac-address", addr, ETHER_ADDR_LEN);
565 * Return a bus handle and bus tag that corresponds to the register
566 * numbered regno for the device referenced by the package handle
567 * dev. This function is intended to be used by console drivers in
568 * early boot only. It works by mapping the address of the device's
569 * register in the address space of its parent and recursively walk
570 * the device tree upward this way.
573 OF_decode_addr(phandle_t dev, int regno, bus_space_tag_t *tag,
574 bus_space_handle_t *handle, bus_size_t *sz)
581 res = ofw_reg_to_paddr(dev, regno, &addr, &size, &pci_hi);
585 if (pci_hi == OFW_PADDR_NOT_PCI) {
590 flags = (pci_hi & OFW_PCI_PHYS_HI_PREFETCHABLE) ?
591 BUS_SPACE_MAP_PREFETCHABLE: 0;
597 return (bus_space_map(*tag, addr, size, flags, handle));