2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (C) 1996 Wolfgang Solfrank.
5 * Copyright (C) 1996 TooLs GmbH.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by TooLs GmbH.
19 * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * $NetBSD: ofw_machdep.c,v 1.5 2000/05/23 13:25:43 tsubai Exp $
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include "opt_platform.h"
40 #include <sys/param.h>
42 #include <sys/systm.h>
45 #include <sys/fcntl.h>
47 #include <sys/malloc.h>
50 #include <sys/endian.h>
52 #include <net/ethernet.h>
54 #include <dev/fdt/fdt_common.h>
55 #include <dev/ofw/openfirm.h>
56 #include <dev/ofw/ofw_pci.h>
57 #include <dev/ofw/ofw_bus.h>
58 #include <dev/ofw/ofw_subr.h>
61 #include <vm/vm_param.h>
62 #include <vm/vm_page.h>
63 #include <vm/vm_phys.h>
65 #include <machine/bus.h>
66 #include <machine/cpu.h>
67 #include <machine/md_var.h>
68 #include <machine/platform.h>
69 #include <machine/ofw_machdep.h>
70 #include <machine/trap.h>
72 #include <contrib/libfdt/libfdt.h>
75 #include <powerpc/powernv/opal.h>
82 extern register_t ofmsr[5];
83 extern void *openfirmware_entry;
84 char save_trap_init[0x2f00]; /* EXC_LAST */
85 char save_trap_of[0x2f00]; /* EXC_LAST */
88 static int openfirmware(void *args);
91 ofw_save_trap_vec(char *save_trap_vec)
93 if (!ofw_real_mode || !hw_direct_map)
96 bcopy((void *)PHYS_TO_DMAP(EXC_RST), save_trap_vec, EXC_LAST - EXC_RST);
100 ofw_restore_trap_vec(char *restore_trap_vec)
102 if (!ofw_real_mode || !hw_direct_map)
105 bcopy(restore_trap_vec, (void *)PHYS_TO_DMAP(EXC_RST),
107 __syncicache((void *)PHYS_TO_DMAP(EXC_RSVD), EXC_LAST - EXC_RSVD);
111 * Saved SPRG0-3 from OpenFirmware. Will be restored prior to the callback.
113 register_t ofw_sprg0_save;
116 ofw_sprg_prepare(void)
122 * Assume that interrupt are disabled at this point, or
123 * SPRG1-3 could be trashed
126 __asm __volatile("mtsprg1 %0\n\t"
134 __asm __volatile("mfsprg0 %0\n\t"
139 : "=&r"(ofw_sprg0_save)
148 ofw_sprg_restore(void)
154 * Note that SPRG1-3 contents are irrelevant. They are scratch
155 * registers used in the early portion of trap handling when
156 * interrupts are disabled.
158 * PCPU data cannot be used until this routine is called !
160 #ifndef __powerpc64__
161 __asm __volatile("mtsprg0 %0" :: "r"(ofw_sprg0_save));
167 parse_ofw_memory(phandle_t node, const char *prop, struct mem_region *output)
169 cell_t address_cells, size_cells;
170 cell_t OFmem[4 * PHYS_AVAIL_SZ];
177 * Get #address-cells from root node, defaulting to 1 if it cannot
180 phandle = OF_finddevice("/");
181 if (OF_getencprop(phandle, "#address-cells", &address_cells,
182 sizeof(address_cells)) < (ssize_t)sizeof(address_cells))
184 if (OF_getencprop(phandle, "#size-cells", &size_cells,
185 sizeof(size_cells)) < (ssize_t)sizeof(size_cells))
191 if (node == -1 || (sz = OF_getencprop(node, prop,
192 OFmem, sizeof(OFmem))) <= 0)
193 panic("Physical memory map not found");
197 while (i < sz/sizeof(cell_t)) {
198 output[j].mr_start = OFmem[i++];
199 if (address_cells == 2) {
200 output[j].mr_start <<= 32;
201 output[j].mr_start += OFmem[i++];
204 output[j].mr_size = OFmem[i++];
205 if (size_cells == 2) {
206 output[j].mr_size <<= 32;
207 output[j].mr_size += OFmem[i++];
210 if (output[j].mr_start > BUS_SPACE_MAXADDR)
214 * Constrain memory to that which we can access.
215 * 32-bit AIM can only reference 32 bits of address currently,
216 * but Book-E can access 36 bits.
218 if (((uint64_t)output[j].mr_start +
219 (uint64_t)output[j].mr_size - 1) >
221 output[j].mr_size = BUS_SPACE_MAXADDR -
222 output[j].mr_start + 1;
232 parse_numa_ofw_memory(phandle_t node, const char *prop,
233 struct numa_mem_region *output)
235 cell_t address_cells, size_cells;
236 cell_t OFmem[4 * PHYS_AVAIL_SZ];
243 * Get #address-cells from root node, defaulting to 1 if it cannot
246 phandle = OF_finddevice("/");
247 if (OF_getencprop(phandle, "#address-cells", &address_cells,
248 sizeof(address_cells)) < (ssize_t)sizeof(address_cells))
250 if (OF_getencprop(phandle, "#size-cells", &size_cells,
251 sizeof(size_cells)) < (ssize_t)sizeof(size_cells))
257 if (node == -1 || (sz = OF_getencprop(node, prop,
258 OFmem, sizeof(OFmem))) <= 0)
259 panic("Physical memory map not found");
263 while (i < sz/sizeof(cell_t)) {
264 output[j].mr_start = OFmem[i++];
265 if (address_cells == 2) {
266 output[j].mr_start <<= 32;
267 output[j].mr_start += OFmem[i++];
269 output[j].mr_size = OFmem[i++];
270 if (size_cells == 2) {
271 output[j].mr_size <<= 32;
272 output[j].mr_size += OFmem[i++];
282 excise_reserved_regions(struct mem_region *avail, int asz,
283 struct mem_region *exclude, int esz)
287 for (i = 0; i < asz; i++) {
288 for (j = 0; j < esz; j++) {
290 * Case 1: Exclusion region encloses complete
291 * available entry. Drop it and move on.
293 if (exclude[j].mr_start <= avail[i].mr_start &&
294 exclude[j].mr_start + exclude[j].mr_size >=
295 avail[i].mr_start + avail[i].mr_size) {
296 for (k = i+1; k < asz; k++)
297 avail[k-1] = avail[k];
299 i--; /* Repeat some entries */
304 * Case 2: Exclusion region starts in available entry.
305 * Trim it to where the entry begins and append
306 * a new available entry with the region after
307 * the excluded region, if any.
309 if (exclude[j].mr_start >= avail[i].mr_start &&
310 exclude[j].mr_start < avail[i].mr_start +
312 if (exclude[j].mr_start + exclude[j].mr_size <
313 avail[i].mr_start + avail[i].mr_size) {
314 avail[asz].mr_start =
315 exclude[j].mr_start + exclude[j].mr_size;
316 avail[asz].mr_size = avail[i].mr_start +
322 avail[i].mr_size = exclude[j].mr_start -
327 * Case 3: Exclusion region ends in available entry.
328 * Move start point to where the exclusion zone ends.
329 * The case of a contained exclusion zone has already
330 * been caught in case 2.
332 if (exclude[j].mr_start + exclude[j].mr_size >=
333 avail[i].mr_start && exclude[j].mr_start +
334 exclude[j].mr_size < avail[i].mr_start +
336 avail[i].mr_size += avail[i].mr_start;
338 exclude[j].mr_start + exclude[j].mr_size;
339 avail[i].mr_size -= avail[i].mr_start;
348 excise_initrd_region(struct mem_region *avail, int asz)
353 struct mem_region initrdmap[1];
356 chosen = OF_finddevice("/chosen");
358 size = OF_getencprop(chosen, "linux,initrd-start", cell, sizeof(cell));
364 start = (uint64_t)cell[0] << 32 | cell[1];
366 /* Invalid value length */
367 printf("WARNING: linux,initrd-start must be either 4 or 8 bytes long\n");
371 size = OF_getencprop(chosen, "linux,initrd-end", cell, sizeof(cell));
377 end = (uint64_t)cell[0] << 32 | cell[1];
379 /* Invalid value length */
380 printf("WARNING: linux,initrd-end must be either 4 or 8 bytes long\n");
387 initrdmap[0].mr_start = start;
388 initrdmap[0].mr_size = end - start;
390 asz = excise_reserved_regions(avail, asz, initrdmap, 1);
397 excise_msi_region(struct mem_region *avail, int asz)
400 struct mem_region initrdmap[1];
403 * This range of physical addresses is used to implement optimized
404 * 32 bit MSI interrupts on POWER9. Exclude it to avoid accidentally
405 * using it for DMA, as this will cause an immediate PHB fence.
406 * While we could theoretically turn off this behavior in the ETU,
407 * doing so would break 32-bit MSI, so just reserve the range in
408 * the physical map instead.
409 * See section 4.4.2.8 of the PHB4 specification.
411 start = 0x00000000ffff0000ul;
412 end = 0x00000000fffffffful;
414 initrdmap[0].mr_start = start;
415 initrdmap[0].mr_size = end - start;
417 asz = excise_reserved_regions(avail, asz, initrdmap, 1);
424 excise_fdt_reserved(struct mem_region *avail, int asz)
426 struct mem_region fdtmap[32];
431 chosen = OF_finddevice("/chosen");
432 fdtmapsize = OF_getprop(chosen, "fdtmemreserv", fdtmap, sizeof(fdtmap));
434 for (j = 0; j < fdtmapsize/sizeof(fdtmap[0]); j++) {
435 fdtmap[j].mr_start = be64toh(fdtmap[j].mr_start) & ~PAGE_MASK;
436 fdtmap[j].mr_size = round_page(be64toh(fdtmap[j].mr_size));
439 KASSERT(j*sizeof(fdtmap[0]) < sizeof(fdtmap),
440 ("Exceeded number of FDT reservations"));
441 /* Add a virtual entry for the FDT itself */
443 fdtmap[j].mr_start = (vm_offset_t)fdt & ~PAGE_MASK;
444 fdtmap[j].mr_size = round_page(fdt_totalsize(fdt));
445 fdtmapsize += sizeof(fdtmap[0]);
448 fdtentries = fdtmapsize/sizeof(fdtmap[0]);
449 asz = excise_reserved_regions(avail, asz, fdtmap, fdtentries);
456 * This is called during powerpc_init, before the system is really initialized.
457 * It shall provide the total and the available regions of RAM.
458 * The available regions need not take the kernel into account.
461 ofw_numa_mem_regions(struct numa_mem_region *memp, int *memsz)
466 cell_t associativity[5];
467 struct numa_mem_region *curmemp;
471 * Get memory from all the /memory nodes.
473 for (phandle = OF_child(OF_peer(0)); phandle != 0;
474 phandle = OF_peer(phandle)) {
475 if (OF_getprop(phandle, "name", name, sizeof(name)) <= 0)
477 if (strncmp(name, "memory@", strlen("memory@")) != 0)
480 count = parse_numa_ofw_memory(phandle, "reg", &memp[msz]);
483 curmemp = &memp[msz];
484 res = OF_getproplen(phandle, "ibm,associativity");
488 OF_getencprop(phandle, "ibm,associativity",
490 curmemp->mr_domain = associativity[3];
492 printf("%s %#jx-%#jx domain(%ju)\n",
493 name, (uintmax_t)curmemp->mr_start,
494 (uintmax_t)curmemp->mr_start + curmemp->mr_size,
495 (uintmax_t)curmemp->mr_domain);
501 * This is called during powerpc_init, before the system is really initialized.
502 * It shall provide the total and the available regions of RAM.
503 * The available regions need not take the kernel into account.
506 ofw_mem_regions(struct mem_region *memp, int *memsz,
507 struct mem_region *availp, int *availsz)
517 * Get memory from all the /memory nodes.
519 for (phandle = OF_child(OF_peer(0)); phandle != 0;
520 phandle = OF_peer(phandle)) {
521 if (OF_getprop(phandle, "name", name, sizeof(name)) <= 0)
523 if (strncmp(name, "memory", sizeof(name)) != 0 &&
524 strncmp(name, "memory@", strlen("memory@")) != 0)
527 res = parse_ofw_memory(phandle, "reg", &memp[msz]);
531 * On POWER9 Systems we might have both linux,usable-memory and
532 * reg properties. 'reg' denotes all available memory, but we
533 * must use 'linux,usable-memory', a subset, as some memory
534 * regions are reserved for NVLink.
536 if (OF_getproplen(phandle, "linux,usable-memory") >= 0)
537 res = parse_ofw_memory(phandle, "linux,usable-memory",
539 else if (OF_getproplen(phandle, "available") >= 0)
540 res = parse_ofw_memory(phandle, "available",
543 res = parse_ofw_memory(phandle, "reg", &availp[asz]);
548 phandle = OF_finddevice("/chosen");
549 if (OF_hasprop(phandle, "fdtmemreserv"))
550 asz = excise_fdt_reserved(availp, asz);
552 /* If the kernel is being loaded through kexec, initrd region is listed
553 * in /chosen but the region is not marked as reserved, so, we might exclude
556 if (OF_hasprop(phandle, "linux,initrd-start"))
557 asz = excise_initrd_region(availp, asz);
561 if (opal_check() == 0)
562 asz = excise_msi_region(availp, asz);
570 OF_initial_setup(void *fdt_ptr, void *junk, int (*openfirm)(void *))
577 __asm __volatile("mfsprg0 %0" : "=&r"(ofmsr[1]));
579 __asm __volatile("mfsprg1 %0" : "=&r"(ofmsr[2]));
580 __asm __volatile("mfsprg2 %0" : "=&r"(ofmsr[3]));
581 __asm __volatile("mfsprg3 %0" : "=&r"(ofmsr[4]));
582 openfirmware_entry = openfirm;
584 if (ofmsr[0] & PSL_DR)
589 ofw_save_trap_vec(save_trap_init);
600 boolean_t status = FALSE;
604 if (openfirmware_entry != NULL) {
606 status = OF_install(OFW_STD_REAL, 0);
609 status = OF_install(OFW_STD_32BIT, 0);
611 status = OF_install(OFW_STD_DIRECT, 0);
618 err = OF_init(openfirmware);
624 bus_space_tag_t fdt_bt;
625 vm_offset_t tmp_fdt_ptr;
630 status = OF_install(OFW_FDT, 0);
634 #ifdef AIM /* AIM-only for now -- Book-E does this remapping in early init */
635 /* Get the FDT size for mapping if we can */
636 tmp_fdt_ptr = pmap_early_io_map((vm_paddr_t)fdt, PAGE_SIZE);
637 if (fdt_check_header((void *)tmp_fdt_ptr) != 0) {
638 pmap_early_io_unmap(tmp_fdt_ptr, PAGE_SIZE);
641 fdt_size = fdt_totalsize((void *)tmp_fdt_ptr);
642 pmap_early_io_unmap(tmp_fdt_ptr, PAGE_SIZE);
645 * Map this for real. Use bus_space_map() to take advantage
646 * of its auto-remapping function once the kernel is loaded.
647 * This is a dirty hack, but what we have.
649 #ifdef _LITTLE_ENDIAN
654 bus_space_map(fdt_bt, (vm_paddr_t)fdt, fdt_size, 0, &fdt_va);
656 err = OF_init((void *)fdt_va);
663 #ifdef FDT_DTB_STATIC
665 * Check for a statically included blob already in the kernel and
666 * needing no mapping.
669 status = OF_install(OFW_FDT, 0);
672 err = OF_init(&fdt_static_dtb);
694 KASSERT(!pmap_bootstrapped, ("Cannot call ofw_quiesce after VM is up"));
696 args.name = (cell_t)(uintptr_t)"quiesce";
703 openfirmware_core(void *args)
708 if (openfirmware_entry == NULL)
712 * Turn off exceptions - we really don't want to end up
713 * anywhere unexpected with PCPU set to something strange
714 * or the stack pointer wrong.
716 oldmsr = intr_disable();
720 /* Save trap vectors */
721 ofw_save_trap_vec(save_trap_of);
723 /* Restore initially saved trap vectors */
724 ofw_restore_trap_vec(save_trap_init);
726 #ifndef __powerpc64__
728 * Clear battable[] translations
730 if (!(cpu_features & PPC_FEATURE_64))
731 __asm __volatile("mtdbatu 2, %0\n"
732 "mtdbatu 3, %0" : : "r" (0));
736 result = ofwcall(args);
738 /* Restore trap vecotrs */
739 ofw_restore_trap_vec(save_trap_of);
743 intr_restore(oldmsr);
752 volatile int in_progress;
756 ofw_rendezvous_dispatch(void *xargs)
758 struct ofw_rv_args *rv_args = xargs;
760 /* NOTE: Interrupts are disabled here */
762 if (PCPU_GET(cpuid) == 0) {
764 * Execute all OF calls on CPU 0
766 rv_args->retval = openfirmware_core(rv_args->args);
767 rv_args->in_progress = 0;
770 * Spin with interrupts off on other CPUs while OF has
771 * control of the machine.
773 while (rv_args->in_progress)
780 openfirmware(void *args)
784 struct ofw_rv_args rv_args;
787 if (openfirmware_entry == NULL)
792 result = openfirmware_core(args);
795 rv_args.in_progress = 1;
796 smp_rendezvous(smp_no_rendezvous_barrier,
797 ofw_rendezvous_dispatch, smp_no_rendezvous_barrier,
799 result = rv_args.retval;
802 result = openfirmware_core(args);
818 args.name = (cell_t)(uintptr_t)"interpret";
821 args.arg = (cell_t)(uintptr_t)"reset-all";
822 openfirmware_core(&args); /* Don't do rendezvous! */
824 for (;;); /* just in case */
830 OF_getetheraddr(device_t dev, u_char *addr)
834 node = ofw_bus_get_node(dev);
835 OF_getprop(node, "local-mac-address", addr, ETHER_ADDR_LEN);
839 * Return a bus handle and bus tag that corresponds to the register
840 * numbered regno for the device referenced by the package handle
841 * dev. This function is intended to be used by console drivers in
842 * early boot only. It works by mapping the address of the device's
843 * register in the address space of its parent and recursively walk
844 * the device tree upward this way.
847 OF_decode_addr(phandle_t dev, int regno, bus_space_tag_t *tag,
848 bus_space_handle_t *handle, bus_size_t *sz)
855 res = ofw_reg_to_paddr(dev, regno, &addr, &size, &pci_hi);
859 if (pci_hi == OFW_PADDR_NOT_PCI) {
864 flags = (pci_hi & OFW_PCI_PHYS_HI_PREFETCHABLE) ?
865 BUS_SPACE_MAP_PREFETCHABLE: 0;
871 return (bus_space_map(*tag, addr, size, flags, handle));