199767f8919635c4928607450d9e0abb932109ceToomas Soome * Initial implementation:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2001 Robert Drehmel
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * As long as the above copyright statement and this notice remain
199767f8919635c4928607450d9e0abb932109ceToomas Soome * unchanged, you can do what ever you want with this file.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2008 - 2012 Marius Strobl <marius@FreeBSD.org>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FreeBSD/sparc64 kernel loader - machine dependent part
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - implements copyin and readin functions that map kernel
199767f8919635c4928607450d9e0abb932109ceToomas Soome * pages on demand. The machine independent code does not
199767f8919635c4928607450d9e0abb932109ceToomas Soome * know the size of the kernel early enough to pre-enter
199767f8919635c4928607450d9e0abb932109ceToomas Soome * TTEs and install just one 4MB mapping seemed to limiting
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern char bootprog_name[], bootprog_rev[], bootprog_date[], bootprog_maker[];
199767f8919635c4928607450d9e0abb932109ceToomas Soome LOADSZ = 0x1000000 /* for kernel and modules */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* At least Sun Fire V1280 require page sized allocations to be claimed. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int (*mmu_mapin)(vm_offset_t va, vm_size_t len);
199767f8919635c4928607450d9e0abb932109ceToomas Soometypedef void kernel_entry_t(vm_offset_t mdp, u_long o1, u_long o2, u_long o3,
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic inline u_long dtlb_get_data_sun4u(u_int, u_int);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int dtlb_enter_sun4u(u_int, u_long data, vm_offset_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic vm_offset_t dtlb_va_to_pa_sun4u(vm_offset_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic inline u_long itlb_get_data_sun4u(u_int, u_int);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int itlb_enter_sun4u(u_int, u_long data, vm_offset_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic vm_offset_t itlb_va_to_pa_sun4u(vm_offset_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern vm_offset_t md_load(char *, vm_offset_t *, vm_offset_t *);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int sparc64_autoload(void);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic ssize_t sparc64_readin(const int, vm_offset_t, const size_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic ssize_t sparc64_copyin(const void *, vm_offset_t, size_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic vm_offset_t claim_virt(vm_offset_t, size_t, int);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int map_phys(int, size_t, vm_offset_t, vm_offset_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int __elfN(exec)(struct preloaded_file *);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int mmu_mapin_sun4u(vm_offset_t, vm_size_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic phandle_t find_bsp_sun4u(phandle_t, uint32_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soomeconst char *cpu_cpuid_prop_sun4u(void);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void tlb_init_sun4u(void);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void pmap_print_tlb_sun4u(void);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic struct mmu_ops mmu_ops_sun4u = { tlb_init_sun4u, mmu_mapin_sun4u };
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Machine dependent structures that the machine independent
199767f8919635c4928607450d9e0abb932109ceToomas Soome * loader part uses.
199767f8919635c4928607450d9e0abb932109ceToomas Soomewatch_phys_set_mask(vm_offset_t pa, u_long mask)
199767f8919635c4928607450d9e0abb932109ceToomas Soome stxa(AA_DMMU_PWPR, ASI_DMMU, pa & (((2UL << 38) - 1) << 3));
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Test for misaligned watch points. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (watch_phys_set_mask(pa, ((1 << sz) - 1) << off));
199767f8919635c4928607450d9e0abb932109ceToomas Soomewatch_virt_set_mask(vm_offset_t va, u_long mask)
199767f8919635c4928607450d9e0abb932109ceToomas Soome stxa(AA_DMMU_VWPR, ASI_DMMU, va & (((2UL << 41) - 1) << 3));
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Test for misaligned watch points. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (watch_virt_set_mask(va, ((1 << sz) - 1) << off));
199767f8919635c4928607450d9e0abb932109ceToomas Soome * archsw functions
199767f8919635c4928607450d9e0abb932109ceToomas Soomesparc64_readin(const int fd, vm_offset_t va, const size_t len)
199767f8919635c4928607450d9e0abb932109ceToomas Soomesparc64_copyin(const void *src, vm_offset_t dest, size_t len)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * other MD functions
199767f8919635c4928607450d9e0abb932109ceToomas Soomeclaim_virt(vm_offset_t virt, size_t size, int align)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (OF_call_method("claim", mmu, 3, 1, virt, size, align, &mva) == -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (OF_call_method("claim", memory, 2, 2, size, align, &phys_low,
199767f8919635c4928607450d9e0abb932109ceToomas Soome return ((vm_offset_t)phys_hi << 32 | phys_low);
199767f8919635c4928607450d9e0abb932109ceToomas Soomemap_phys(int mode, size_t size, vm_offset_t virt, vm_offset_t phys)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (OF_call_method("map", mmu, 5, 0, (uint32_t)phys,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void)OF_call_method("release", memory, 3, 0, (uint32_t)phys,
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((fmp = file_findmetadata(fp, MODINFOMD_ELFHDR)) == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((error = md_load(fp->f_args, &mdp, &dtbp)) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("jumping to kernel entry at %#lx.\n", e->e_entry);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ((kernel_entry_t *)entry)(mdp, 0, 0, 0, openfirmware);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * We read ASI_DTLB_DATA_ACCESS_REG twice back-to-back in order to
199767f8919635c4928607450d9e0abb932109ceToomas Soome * work around errata of USIII and beyond.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * We read ASI_DTLB_DATA_ACCESS_REG twice back-to-back in order to
199767f8919635c4928607450d9e0abb932109ceToomas Soome * work around errata of USIII and beyond.
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < dtlb_slot_max; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < itlb_slot_max; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soomedtlb_enter_sun4u(u_int index, u_long data, vm_offset_t virt)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (OF_call_method("SUNW,dtlb-load", mmu, 3, 0, index, data,
199767f8919635c4928607450d9e0abb932109ceToomas Soomeitlb_enter_sun4u(u_int index, u_long data, vm_offset_t virt)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cpu_impl == CPU_IMPL_ULTRASPARCIIIp && index == 0 &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome panic("%s: won't enter locked TLB entry at index 0 on USIII+",
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (OF_call_method("SUNW,itlb-load", mmu, 3, 0, index, data,
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Flush the mapping of slot 0. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome tag = ldxa(TLB_DAR_SLOT(tlb_locked, 0), ASI_ITLB_TAG_READ_REG);
199767f8919635c4928607450d9e0abb932109ceToomas Soome stxa(TLB_DEMAP_VA(TLB_TAR_VA(tag)) | TLB_DEMAP_PRIMARY |
199767f8919635c4928607450d9e0abb932109ceToomas Soome flush(0); /* The USIII-family ignores the address. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Search a replacement slot != 0 and enter the data and tag
199767f8919635c4928607450d9e0abb932109ceToomas Soome * that formerly were in slot 0.
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((itlb_get_data_sun4u(tlb_locked, i) & TD_V) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome stxa(TLB_DAR_SLOT(tlb_locked, i), ASI_ITLB_DATA_ACCESS_REG,
199767f8919635c4928607450d9e0abb932109ceToomas Soome flush(0); /* The USIII-family ignores the address. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome panic("%s: could not find a replacement slot", __func__);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dtlb_va_to_pa_sun4u(va) == (vm_offset_t)-1 ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Allocate a physical page, claim the virtual area. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome "(wanted %#lx, got %#lx)",
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The mappings may have changed, be paranoid.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Actually, we can only allocate two pages less at
199767f8919635c4928607450d9e0abb932109ceToomas Soome * most (depending on the kernel TSB size).
199767f8919635c4928607450d9e0abb932109ceToomas Soome data = TD_V | TD_4M | TD_PA(pa) | TD_L | TD_CP |
199767f8919635c4928607450d9e0abb932109ceToomas Soome len -= len > PAGE_SIZE_4M ? PAGE_SIZE_4M : len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* There is no need for continuous physical heap memory. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome heapva = (vm_offset_t)OF_claim((void *)HEAPVA, HEAPSZ, 32);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sizeof(type)) <= 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (OF_getprop(node, cpu_cpuid_prop_sun4u(), &cpuid,
199767f8919635c4928607450d9e0abb932109ceToomas Soome sizeof(cpuid)) <= 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return ("upa-portid");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return ("portid");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return ("cpuid");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return ("");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (UPA_CR_GET_MID(ldxa(0, ASI_UPA_CONFIG_REG)));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (FIREPLANE_CR_GET_AID(ldxa(AA_FIREPLANE_CONFIG,
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (JBUS_CR_GET_JID(ldxa(0, ASI_JBUS_CONFIG_REG)));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (INTR_ID_GET_ID(ldxa(AA_INTR_ID, ASI_INTR_ID)));
199767f8919635c4928607450d9e0abb932109ceToomas Soome bsp = find_bsp_sun4u(OF_child(root), cpu_get_mid_sun4u());
199767f8919635c4928607450d9e0abb932109ceToomas Soome panic("%s: no node for bootcpu?!?!", __func__);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (OF_getprop(bsp, "#dtlb-entries", &dtlb_slot_max,
199767f8919635c4928607450d9e0abb932109ceToomas Soome OF_getprop(bsp, "#itlb-entries", &itlb_slot_max,
199767f8919635c4928607450d9e0abb932109ceToomas Soome panic("%s: can't get TLB slot max.", __func__);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Relocate the locked entry in it16 slot 0 (if existent)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * as part of working around Cheetah+ erratum 34.
199767f8919635c4928607450d9e0abb932109ceToomas Soome dtlb_store = malloc(dtlb_slot_max * sizeof(*dtlb_store));
199767f8919635c4928607450d9e0abb932109ceToomas Soome itlb_store = malloc(itlb_slot_max * sizeof(*itlb_store));
199767f8919635c4928607450d9e0abb932109ceToomas Soome panic("%s: can't allocate TLB store", __func__);
199767f8919635c4928607450d9e0abb932109ceToomas Soome char alias[64], devname[sizeof(alias) + sizeof(":x") - 1];
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Get the GUID of the ZFS pool on the boot device. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Get the GUIDs of the ZFS pools on any additional disks listed in
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the boot-device environment variable.
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((aliases = OF_finddevice("/aliases")) == -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (OF_getprop(options, "boot-device", bdev, len) <= 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void)OF_getprop(aliases, dev, alias, sizeof(alias));
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Don't probe the boot disk twice. Note that bootpath
199767f8919635c4928607450d9e0abb932109ceToomas Soome * includes the partition specifier.
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strncmp(alias, bootpath, strlen(alias)) == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (OF_getprop(OF_finddevice(alias), "device_type", type,
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Find freebsd-zfs slices in the VTOC. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (read(fd, &vtoc, sizeof(vtoc)) != sizeof(vtoc)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void)sprintf(devname, "%s:%c", alias, part + 'a');
199767f8919635c4928607450d9e0abb932109ceToomas Soome zfs_currdev.d_type = zfs_currdev.d_dev->dv_type;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* LOADER_ZFS_SUPPORT */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Tell the Open Firmware functions where they find the OFW gate.
199767f8919635c4928607450d9e0abb932109ceToomas Soome setheap((void *)heapva, (void *)(heapva + HEAPSZ));
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Probe for a console.
199767f8919635c4928607450d9e0abb932109ceToomas Soome OF_getprop(root, "compatible", compatible, sizeof(compatible));
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Set up the current device.
199767f8919635c4928607450d9e0abb932109ceToomas Soome OF_getprop(chosen, "bootpath", bootpath, sizeof(bootpath));
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Initialize devices.
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void)strncpy(bootpath, zfs_fmtdev(&zfs_currdev),
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Sun compatible bootable CD-ROMs have a disk label placed before
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the ISO 9660 data, with the actual file system being in the first
199767f8919635c4928607450d9e0abb932109ceToomas Soome * partition, while the other partitions contain pseudo disk labels
199767f8919635c4928607450d9e0abb932109ceToomas Soome * with embedded boot blocks for different architectures, which may
199767f8919635c4928607450d9e0abb932109ceToomas Soome * be followed by UFS file systems.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The firmware will set the boot path to the partition it boots from
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ('f' in the sun4u/sun4v case), but we want the kernel to be loaded
199767f8919635c4928607450d9e0abb932109ceToomas Soome * from the ISO 9660 file system ('a'), so the boot path needs to be
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("(%s, %s)\n", bootprog_maker, bootprog_date);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Give control to the machine independent loader code. */
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(heap, "heap", "show heap usage", command_heap);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("heap base at %p, top at %p, upper limit at %p\n", heapva,
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* provide this for panic, as it's not in the startup code */
199767f8919635c4928607450d9e0abb932109ceToomas Soome page_sizes[(tte >> TD_SIZE_SHIFT) & TD_SIZE_MASK],
199767f8919635c4928607450d9e0abb932109ceToomas Soome TD_PA(tte), TLB_TAR_VA(tag), TLB_TAR_CTX(tag));
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < itlb_slot_max; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < dtlb_slot_max; i++) {