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 <sys/param.h>
39 #include <sys/systm.h>
42 #include <sys/fcntl.h>
43 #include <sys/malloc.h>
46 #include <sys/endian.h>
48 #include <net/ethernet.h>
50 #include <dev/ofw/openfirm.h>
51 #include <dev/ofw/ofw_pci.h>
52 #include <dev/ofw/ofw_bus.h>
55 #include <vm/vm_param.h>
56 #include <vm/vm_page.h>
58 #include <machine/bus.h>
59 #include <machine/cpu.h>
60 #include <machine/md_var.h>
61 #include <machine/platform.h>
62 #include <machine/ofw_machdep.h>
63 #include <machine/trap.h>
66 extern register_t ofmsr[5];
67 extern void *openfirmware_entry;
70 char save_trap_init[0x2f00]; /* EXC_LAST */
71 char save_trap_of[0x2f00]; /* EXC_LAST */
74 static int openfirmware(void *args);
77 ofw_save_trap_vec(char *save_trap_vec)
82 bcopy((void *)EXC_RST, save_trap_vec, EXC_LAST - EXC_RST);
86 ofw_restore_trap_vec(char *restore_trap_vec)
91 bcopy(restore_trap_vec, (void *)EXC_RST, EXC_LAST - EXC_RST);
92 __syncicache(EXC_RSVD, EXC_LAST - EXC_RSVD);
96 * Saved SPRG0-3 from OpenFirmware. Will be restored prior to the callback.
98 register_t ofw_sprg0_save;
101 ofw_sprg_prepare(void)
107 * Assume that interrupt are disabled at this point, or
108 * SPRG1-3 could be trashed
110 __asm __volatile("mfsprg0 %0\n\t"
115 : "=&r"(ofw_sprg0_save)
123 ofw_sprg_restore(void)
129 * Note that SPRG1-3 contents are irrelevant. They are scratch
130 * registers used in the early portion of trap handling when
131 * interrupts are disabled.
133 * PCPU data cannot be used until this routine is called !
135 __asm __volatile("mtsprg0 %0" :: "r"(ofw_sprg0_save));
140 parse_ofw_memory(phandle_t node, const char *prop, struct mem_region *output)
142 cell_t address_cells, size_cells;
143 cell_t OFmem[4 * PHYS_AVAIL_SZ];
150 * Get #address-cells from root node, defaulting to 1 if it cannot
153 phandle = OF_finddevice("/");
154 if (OF_getprop(phandle, "#address-cells", &address_cells,
155 sizeof(address_cells)) < (ssize_t)sizeof(address_cells))
157 if (OF_getprop(phandle, "#size-cells", &size_cells,
158 sizeof(size_cells)) < (ssize_t)sizeof(size_cells))
164 if (node == -1 || (sz = OF_getprop(node, prop,
165 OFmem, sizeof(OFmem))) <= 0)
166 panic("Physical memory map not found");
170 while (i < sz/sizeof(cell_t)) {
171 #ifndef __powerpc64__
172 /* On 32-bit PPC, ignore regions starting above 4 GB */
173 if (address_cells > 1 && OFmem[i] > 0) {
174 i += address_cells + size_cells;
179 output[j].mr_start = OFmem[i++];
180 if (address_cells == 2) {
182 output[j].mr_start <<= 32;
184 output[j].mr_start += OFmem[i++];
187 output[j].mr_size = OFmem[i++];
188 if (size_cells == 2) {
190 output[j].mr_size <<= 32;
192 output[j].mr_size += OFmem[i++];
195 #ifndef __powerpc64__
197 * Check for memory regions extending above 32-bit
198 * memory space, and restrict them to stay there.
200 if (((uint64_t)output[j].mr_start +
201 (uint64_t)output[j].mr_size) >
202 BUS_SPACE_MAXADDR_32BIT) {
203 output[j].mr_size = BUS_SPACE_MAXADDR_32BIT -
210 sz = j*sizeof(output[0]);
216 excise_fdt_reserved(struct mem_region *avail, int asz)
226 chosen = OF_finddevice("/chosen");
227 fdtmapsize = OF_getprop(chosen, "fdtmemreserv", fdtmap, sizeof(fdtmap));
229 for (j = 0; j < fdtmapsize/sizeof(fdtmap[0]); j++) {
230 fdtmap[j].address = be64toh(fdtmap[j].address);
231 fdtmap[j].size = be64toh(fdtmap[j].size);
234 for (i = 0; i < asz; i++) {
235 for (j = 0; j < fdtmapsize/sizeof(fdtmap[0]); j++) {
237 * Case 1: Exclusion region encloses complete
238 * available entry. Drop it and move on.
240 if (fdtmap[j].address <= avail[i].mr_start &&
241 fdtmap[j].address + fdtmap[j].size >=
242 avail[i].mr_start + avail[i].mr_size) {
243 for (k = i+1; k < asz; k++)
244 avail[k-1] = avail[k];
246 i--; /* Repeat some entries */
251 * Case 2: Exclusion region starts in available entry.
252 * Trim it to where the entry begins and append
253 * a new available entry with the region after
254 * the excluded region, if any.
256 if (fdtmap[j].address >= avail[i].mr_start &&
257 fdtmap[j].address < avail[i].mr_start +
259 if (fdtmap[j].address + fdtmap[j].size <
260 avail[i].mr_start + avail[i].mr_size) {
261 avail[asz].mr_start =
262 fdtmap[j].address + fdtmap[j].size;
263 avail[asz].mr_size = avail[i].mr_start +
269 avail[i].mr_size = fdtmap[j].address -
274 * Case 3: Exclusion region ends in available entry.
275 * Move start point to where the exclusion zone ends.
276 * The case of a contained exclusion zone has already
277 * been caught in case 2.
279 if (fdtmap[j].address + fdtmap[j].size >=
280 avail[i].mr_start && fdtmap[j].address +
281 fdtmap[j].size < avail[i].mr_start +
283 avail[i].mr_size += avail[i].mr_start;
285 fdtmap[j].address + fdtmap[j].size;
286 avail[i].mr_size -= avail[i].mr_start;
295 * This is called during powerpc_init, before the system is really initialized.
296 * It shall provide the total and the available regions of RAM.
297 * The available regions need not take the kernel into account.
300 ofw_mem_regions(struct mem_region *memp, int *memsz,
301 struct mem_region *availp, int *availsz)
311 * Get memory from all the /memory nodes.
313 for (phandle = OF_child(OF_peer(0)); phandle != 0;
314 phandle = OF_peer(phandle)) {
315 if (OF_getprop(phandle, "name", name, sizeof(name)) <= 0)
317 if (strncmp(name, "memory", sizeof(name)) != 0 &&
318 strncmp(name, "memory@", strlen("memory@")) != 0)
321 res = parse_ofw_memory(phandle, "reg", &memp[msz]);
322 msz += res/sizeof(struct mem_region);
323 if (OF_getproplen(phandle, "available") >= 0)
324 res = parse_ofw_memory(phandle, "available",
327 res = parse_ofw_memory(phandle, "reg", &availp[asz]);
328 asz += res/sizeof(struct mem_region);
331 phandle = OF_finddevice("/chosen");
332 if (OF_hasprop(phandle, "fdtmemreserv"))
333 asz = excise_fdt_reserved(availp, asz);
341 OF_initial_setup(void *fdt_ptr, void *junk, int (*openfirm)(void *))
347 __asm __volatile("mfsprg0 %0" : "=&r"(ofmsr[1]));
348 __asm __volatile("mfsprg1 %0" : "=&r"(ofmsr[2]));
349 __asm __volatile("mfsprg2 %0" : "=&r"(ofmsr[3]));
350 __asm __volatile("mfsprg3 %0" : "=&r"(ofmsr[4]));
352 if (ofmsr[0] & PSL_DR)
358 openfirmware_entry = openfirm;
360 #ifdef FDT_DTB_STATIC
361 /* Check for a statically included blob */
363 fdt = &fdt_static_dtb;
366 ofw_save_trap_vec(save_trap_init);
372 boolean_t status = FALSE;
374 if (openfirmware_entry != NULL) {
376 status = OF_install(OFW_STD_REAL, 0);
379 status = OF_install(OFW_STD_32BIT, 0);
381 status = OF_install(OFW_STD_DIRECT, 0);
388 OF_init(openfirmware);
389 } else if (fdt != NULL) {
390 status = OF_install(OFW_FDT, 0);
410 KASSERT(!pmap_bootstrapped, ("Cannot call ofw_quiesce after VM is up"));
412 args.name = (cell_t)(uintptr_t)"quiesce";
419 openfirmware_core(void *args)
424 if (openfirmware_entry == NULL)
428 * Turn off exceptions - we really don't want to end up
429 * anywhere unexpected with PCPU set to something strange
430 * or the stack pointer wrong.
432 oldmsr = intr_disable();
436 /* Save trap vectors */
437 ofw_save_trap_vec(save_trap_of);
439 /* Restore initially saved trap vectors */
440 ofw_restore_trap_vec(save_trap_init);
442 #if defined(AIM) && !defined(__powerpc64__)
444 * Clear battable[] translations
446 if (!(cpu_features & PPC_FEATURE_64))
447 __asm __volatile("mtdbatu 2, %0\n"
448 "mtdbatu 3, %0" : : "r" (0));
452 result = ofwcall(args);
454 /* Restore trap vecotrs */
455 ofw_restore_trap_vec(save_trap_of);
459 intr_restore(oldmsr);
468 volatile int in_progress;
472 ofw_rendezvous_dispatch(void *xargs)
474 struct ofw_rv_args *rv_args = xargs;
476 /* NOTE: Interrupts are disabled here */
478 if (PCPU_GET(cpuid) == 0) {
480 * Execute all OF calls on CPU 0
482 rv_args->retval = openfirmware_core(rv_args->args);
483 rv_args->in_progress = 0;
486 * Spin with interrupts off on other CPUs while OF has
487 * control of the machine.
489 while (rv_args->in_progress)
496 openfirmware(void *args)
500 struct ofw_rv_args rv_args;
503 if (openfirmware_entry == NULL)
508 rv_args.in_progress = 1;
509 smp_rendezvous(smp_no_rendevous_barrier, ofw_rendezvous_dispatch,
510 smp_no_rendevous_barrier, &rv_args);
511 result = rv_args.retval;
513 result = openfirmware_core(args);
529 args.name = (cell_t)(uintptr_t)"interpret";
532 args.arg = (cell_t)(uintptr_t)"reset-all";
533 openfirmware_core(&args); /* Don't do rendezvous! */
535 for (;;); /* just in case */
541 OF_getetheraddr(device_t dev, u_char *addr)
545 node = ofw_bus_get_node(dev);
546 OF_getprop(node, "local-mac-address", addr, ETHER_ADDR_LEN);
550 * Return a bus handle and bus tag that corresponds to the register
551 * numbered regno for the device referenced by the package handle
552 * dev. This function is intended to be used by console drivers in
553 * early boot only. It works by mapping the address of the device's
554 * register in the address space of its parent and recursively walk
555 * the device tree upward this way.
558 OF_get_addr_props(phandle_t node, uint32_t *addrp, uint32_t *sizep, int *pcip)
564 res = OF_getprop(node, "#address-cells", &addr, sizeof(addr));
567 res = OF_getprop(node, "#size-cells", &size, sizeof(size));
571 if (addr == 3 && size == 2) {
572 res = OF_getprop(node, "device_type", type, sizeof(type));
574 type[sizeof(type) - 1] = '\0';
575 pci = (strcmp(type, "pci") == 0) ? 1 : 0;
587 OF_decode_addr(phandle_t dev, int regno, bus_space_tag_t *tag,
588 bus_space_handle_t *handle)
591 bus_addr_t addr, raddr, baddr;
592 bus_size_t size, rsize;
593 uint32_t c, nbridge, naddr, nsize;
594 phandle_t bridge, parent;
595 u_int spc, rspc, prefetch;
598 /* Sanity checking. */
601 bridge = OF_parent(dev);
606 if (tag == NULL || handle == NULL)
609 /* Assume big-endian unless we find a PCI device */
612 /* Get the requested register. */
613 OF_get_addr_props(bridge, &naddr, &nsize, &pci);
616 res = OF_getprop(dev, (pci) ? "assigned-addresses" : "reg",
620 if (res % sizeof(cell[0]))
622 res /= sizeof(cell[0]);
623 regno *= naddr + nsize;
624 if (regno + naddr + nsize > res)
626 spc = (pci) ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK : ~0;
627 prefetch = (pci) ? cell[regno] & OFW_PCI_PHYS_HI_PREFETCHABLE : 0;
629 for (c = 0; c < naddr; c++)
630 addr = ((uint64_t)addr << 32) | cell[regno++];
632 for (c = 0; c < nsize; c++)
633 size = ((uint64_t)size << 32) | cell[regno++];
636 * Map the address range in the bridge's decoding window as given
637 * by the "ranges" property. If a node doesn't have such property
638 * then no mapping is done.
640 parent = OF_parent(bridge);
641 while (parent != 0) {
642 OF_get_addr_props(parent, &nbridge, NULL, &pcib);
645 res = OF_getprop(bridge, "ranges", cell, sizeof(cell));
648 if (res % sizeof(cell[0]))
650 res /= sizeof(cell[0]);
652 while (regno < res) {
654 ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK
657 regno += naddr + nbridge + nsize;
661 for (c = 0; c < naddr; c++)
662 raddr = ((uint64_t)raddr << 32) | cell[regno++];
664 ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK
667 for (c = 0; c < nbridge; c++)
668 baddr = ((uint64_t)baddr << 32) | cell[regno++];
670 for (c = 0; c < nsize; c++)
671 rsize = ((uint64_t)rsize << 32) | cell[regno++];
672 if (addr < raddr || addr >= raddr + rsize)
674 addr = addr - raddr + baddr;
681 parent = OF_parent(bridge);
682 OF_get_addr_props(bridge, &naddr, &nsize, &pci);
685 return (bus_space_map(*tag, addr, size,
686 prefetch ? BUS_SPACE_MAP_PREFETCHABLE : 0, handle));