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>
47 #include <net/ethernet.h>
49 #include <dev/ofw/openfirm.h>
50 #include <dev/ofw/ofw_pci.h>
51 #include <dev/ofw/ofw_bus.h>
54 #include <vm/vm_param.h>
55 #include <vm/vm_page.h>
57 #include <machine/bus.h>
58 #include <machine/cpu.h>
59 #include <machine/md_var.h>
60 #include <machine/platform.h>
61 #include <machine/ofw_machdep.h>
63 #define OFMEM_REGIONS 32
64 static struct mem_region OFmem[OFMEM_REGIONS + 1], OFavail[OFMEM_REGIONS + 3];
65 static struct mem_region OFfree[OFMEM_REGIONS + 3];
68 vm_offset_t mr_start_hi;
69 vm_offset_t mr_start_lo;
73 extern register_t ofmsr[5];
74 extern struct pmap ofw_pmap;
75 static int (*ofwcall)(void *);
79 static void ofw_quiesce(void);
80 static int openfirmware(void *args);
83 * Saved SPRG0-3 from OpenFirmware. Will be restored prior to the callback.
85 register_t ofw_sprg0_save;
88 ofw_sprg_prepare(void)
91 * Assume that interrupt are disabled at this point, or
92 * SPRG1-3 could be trashed
94 __asm __volatile("mfsprg0 %0\n\t"
99 : "=&r"(ofw_sprg0_save)
107 ofw_sprg_restore(void)
110 * Note that SPRG1-3 contents are irrelevant. They are scratch
111 * registers used in the early portion of trap handling when
112 * interrupts are disabled.
114 * PCPU data cannot be used until this routine is called !
116 __asm __volatile("mtsprg0 %0" :: "r"(ofw_sprg0_save));
120 * Memory region utilities: determine if two regions overlap,
121 * and merge two overlapping regions into one
124 memr_overlap(struct mem_region *r1, struct mem_region *r2)
126 if ((r1->mr_start + r1->mr_size) < r2->mr_start ||
127 (r2->mr_start + r2->mr_size) < r1->mr_start)
134 memr_merge(struct mem_region *from, struct mem_region *to)
137 end = imax(to->mr_start + to->mr_size, from->mr_start + from->mr_size);
138 to->mr_start = imin(from->mr_start, to->mr_start);
139 to->mr_size = end - to->mr_start;
143 * This is called during powerpc_init, before the system is really initialized.
144 * It shall provide the total and the available regions of RAM.
145 * Both lists must have a zero-size entry as terminator.
146 * The available regions need not take the kernel into account, but needs
147 * to provide space for two additional entry beyond the terminating one.
150 ofw_mem_regions(struct mem_region **memp, int *memsz,
151 struct mem_region **availp, int *availsz)
157 cell_t address_cells;
162 * Get #address-cells from root node, defaulting to 1 if it cannot
165 phandle = OF_finddevice("/");
166 if (OF_getprop(phandle, "#address-cells", &address_cells,
167 sizeof(address_cells)) < sizeof(address_cells))
173 if ((phandle = OF_finddevice("/memory")) == -1
174 || (asz = OF_getprop(phandle, "available",
175 OFavail, sizeof OFavail[0] * OFMEM_REGIONS)) <= 0)
179 printf("Physical memory unknown -- guessing 128 MB\n");
181 /* Leave the first 0xA000000 bytes for the kernel */
182 OFavail[0].mr_start = 0xA00000;
183 OFavail[0].mr_size = 0x75FFFFF;
184 asz = sizeof(OFavail[0]);
190 if (address_cells == 2) {
191 struct mem_region64 OFmem64[OFMEM_REGIONS + 1];
192 if ((phandle == -1) || (msz = OF_getprop(phandle, "reg",
193 OFmem64, sizeof OFmem64[0] * OFMEM_REGIONS)) <= 0) {
196 OFmem64[0].mr_start_hi = 0;
197 OFmem64[0].mr_start_lo = 0x0;
198 OFmem64[0].mr_size = 0x7FFFFFF;
199 msz = sizeof(OFmem64[0]);
201 panic("Physical memory map not found");
205 for (i = 0, j = 0; i < msz/sizeof(OFmem64[0]); i++) {
206 if (OFmem64[i].mr_start_hi == 0) {
207 OFmem[i].mr_start = OFmem64[i].mr_start_lo;
208 OFmem[i].mr_size = OFmem64[i].mr_size;
211 * Check for memory regions extending above 32-bit
212 * memory space, and restrict them to stay there.
214 if (((uint64_t)OFmem[i].mr_start +
215 (uint64_t)OFmem[i].mr_size) >
216 BUS_SPACE_MAXADDR_32BIT) {
217 OFmem[i].mr_size = BUS_SPACE_MAXADDR_32BIT -
223 msz = j*sizeof(OFmem[0]);
225 if ((msz = OF_getprop(phandle, "reg",
226 OFmem, sizeof OFmem[0] * OFMEM_REGIONS)) <= 0)
227 panic("Physical memory map not found");
231 *memsz = msz / sizeof(struct mem_region);
235 * OFavail may have overlapping regions - collapse these
236 * and copy out remaining regions to OFfree
238 asz /= sizeof(struct mem_region);
240 still_merging = FALSE;
241 for (i = 0; i < asz; i++) {
242 if (OFavail[i].mr_size == 0)
244 for (j = i+1; j < asz; j++) {
245 if (OFavail[j].mr_size == 0)
247 if (memr_overlap(&OFavail[j], &OFavail[i])) {
248 memr_merge(&OFavail[j], &OFavail[i]);
250 OFavail[j].mr_size = 0;
251 still_merging = TRUE;
255 } while (still_merging == TRUE);
257 /* evict inactive ranges */
258 for (i = 0, fsz = 0; i < asz; i++) {
259 if (OFavail[i].mr_size != 0) {
260 OFfree[fsz] = OFavail[i];
270 OF_initial_setup(void *fdt_ptr, void *junk, int (*openfirm)(void *))
272 if (ofmsr[0] & PSL_DR)
284 boolean_t status = FALSE;
286 if (ofwcall != NULL) {
288 status = OF_install(OFW_STD_REAL, 0);
290 status = OF_install(OFW_STD_DIRECT, 0);
295 OF_init(openfirmware);
298 * On some machines, we need to quiesce OF to turn off
299 * background processes.
303 status = OF_install(OFW_FDT, 0);
326 * Only quiesce Open Firmware on PowerMac11,2 and 12,1. It is
327 * necessary there to shut down a background thread doing fan
328 * management, and is harmful on other machines.
330 * Note: we don't need to worry about which OF module we are
331 * using since this is called only from very early boot, within
335 rootnode = OF_finddevice("/");
336 if (OF_getprop(rootnode, "model", model, sizeof(model)) > 0) {
337 if (strcmp(model, "PowerMac11,2") == 0 ||
338 strcmp(model, "PowerMac12,1") == 0) {
339 args.name = (cell_t)(uintptr_t)"quiesce";
348 openfirmware_core(void *args)
355 __asm __volatile( "\t"
366 if (pmap_bootstrapped && !ofw_real_mode) {
368 * Swap the kernel's address space with Open Firmware's
370 for (i = 0; i < 16; i++) {
371 srsave[i] = mfsrin(i << ADDR_SR_SHFT);
372 mtsrin(i << ADDR_SR_SHFT, ofw_pmap.pm_sr[i]);
376 * Clear battable[] translations
378 if (!(cpu_features & PPC_FEATURE_64)) {
379 __asm __volatile("mtdbatu 2, %0\n"
380 "mtdbatu 3, %0" : : "r" (0));
385 result = ofwcall(args);
387 if (pmap_bootstrapped && !ofw_real_mode) {
389 * Restore the kernel's addr space. The isync() doesn;t
390 * work outside the loop unless mtsrin() is open-coded
391 * in an asm statement :(
393 for (i = 0; i < 16; i++) {
394 mtsrin(i << ADDR_SR_SHFT, srsave[i]);
414 volatile int in_progress;
418 ofw_rendezvous_dispatch(void *xargs)
420 struct ofw_rv_args *rv_args = xargs;
422 /* NOTE: Interrupts are disabled here */
424 if (PCPU_GET(cpuid) == 0) {
426 * Execute all OF calls on CPU 0
428 rv_args->retval = openfirmware_core(rv_args->args);
429 rv_args->in_progress = 0;
432 * Spin with interrupts off on other CPUs while OF has
433 * control of the machine.
435 while (rv_args->in_progress)
442 openfirmware(void *args)
446 struct ofw_rv_args rv_args;
449 if (pmap_bootstrapped && ofw_real_mode)
450 args = (void *)pmap_kextract((vm_offset_t)args);
454 rv_args.in_progress = 1;
455 smp_rendezvous(smp_no_rendevous_barrier, ofw_rendezvous_dispatch,
456 smp_no_rendevous_barrier, &rv_args);
457 result = rv_args.retval;
459 result = openfirmware_core(args);
468 int retval; /* dummy, this may not be needed */
470 OF_interpret("shut-down", 1, &retval);
471 for (;;); /* just in case */
477 int retval; /* dummy, this may not be needed */
479 OF_interpret("reset-all", 1, &retval);
480 for (;;); /* just in case */
484 OF_getetheraddr(device_t dev, u_char *addr)
488 node = ofw_bus_get_node(dev);
489 OF_getprop(node, "local-mac-address", addr, ETHER_ADDR_LEN);
493 * Return a bus handle and bus tag that corresponds to the register
494 * numbered regno for the device referenced by the package handle
495 * dev. This function is intended to be used by console drivers in
496 * early boot only. It works by mapping the address of the device's
497 * register in the address space of its parent and recursively walk
498 * the device tree upward this way.
501 OF_get_addr_props(phandle_t node, uint32_t *addrp, uint32_t *sizep, int *pcip)
507 res = OF_getprop(node, "#address-cells", &addr, sizeof(addr));
510 res = OF_getprop(node, "#size-cells", &size, sizeof(size));
514 if (addr == 3 && size == 2) {
515 res = OF_getprop(node, "name", name, sizeof(name));
517 name[sizeof(name) - 1] = '\0';
518 pci = (strcmp(name, "pci") == 0) ? 1 : 0;
530 OF_decode_addr(phandle_t dev, int regno, bus_space_tag_t *tag,
531 bus_space_handle_t *handle)
534 bus_addr_t addr, raddr, baddr;
535 bus_size_t size, rsize;
536 uint32_t c, nbridge, naddr, nsize;
537 phandle_t bridge, parent;
541 /* Sanity checking. */
544 bridge = OF_parent(dev);
549 if (tag == NULL || handle == NULL)
552 /* Get the requested register. */
553 OF_get_addr_props(bridge, &naddr, &nsize, &pci);
554 res = OF_getprop(dev, (pci) ? "assigned-addresses" : "reg",
558 if (res % sizeof(cell[0]))
560 res /= sizeof(cell[0]);
561 regno *= naddr + nsize;
562 if (regno + naddr + nsize > res)
564 spc = (pci) ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK : ~0;
566 for (c = 0; c < naddr; c++)
567 addr = ((uint64_t)addr << 32) | cell[regno++];
569 for (c = 0; c < nsize; c++)
570 size = ((uint64_t)size << 32) | cell[regno++];
573 * Map the address range in the bridge's decoding window as given
574 * by the "ranges" property. If a node doesn't have such property
575 * then no mapping is done.
577 parent = OF_parent(bridge);
578 while (parent != 0) {
579 OF_get_addr_props(parent, &nbridge, NULL, &pcib);
580 res = OF_getprop(bridge, "ranges", cell, sizeof(cell));
583 if (res % sizeof(cell[0]))
585 res /= sizeof(cell[0]);
587 while (regno < res) {
589 ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK
592 regno += naddr + nbridge + nsize;
596 for (c = 0; c < naddr; c++)
597 raddr = ((uint64_t)raddr << 32) | cell[regno++];
599 ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK
602 for (c = 0; c < nbridge; c++)
603 baddr = ((uint64_t)baddr << 32) | cell[regno++];
605 for (c = 0; c < nsize; c++)
606 rsize = ((uint64_t)rsize << 32) | cell[regno++];
607 if (addr < raddr || addr >= raddr + rsize)
609 addr = addr - raddr + baddr;
616 parent = OF_parent(bridge);
617 OF_get_addr_props(bridge, &naddr, &nsize, &pci);
621 return (bus_space_map(*tag, addr, size, 0, handle));
625 mem_valid(vm_offset_t addr, int len)
629 for (i = 0; i < OFMEM_REGIONS; i++)
630 if ((addr >= OFmem[i].mr_start)
631 && (addr + len < OFmem[i].mr_start + OFmem[i].mr_size))