17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5986fd29aSsetje  * Common Development and Distribution License (the "License").
6986fd29aSsetje  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22e7cbe64fSgw  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/cpr.h>
277c478bd9Sstevel@tonic-gate #include <sys/promimpl.h>
287c478bd9Sstevel@tonic-gate #include "cprboot.h"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
31986fd29aSsetje static ihandle_t	cb_rih;
32986fd29aSsetje static pnode_t		chosen;
33986fd29aSsetje 
34986fd29aSsetje static int statefile_special;
35986fd29aSsetje 
367c478bd9Sstevel@tonic-gate static int reset_input = 0;
377c478bd9Sstevel@tonic-gate static char kbd_input[] = "keyboard input";
387c478bd9Sstevel@tonic-gate static char null_input[] = "\" /nulldev\" input";
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * Ask prom to open a disk file given either the OBP device path, or the
427c478bd9Sstevel@tonic-gate  * device path representing the target drive/partition and the fs-relative
437c478bd9Sstevel@tonic-gate  * path of the file.  Handle file pathnames with or without leading '/'.
447c478bd9Sstevel@tonic-gate  * if fs points to a null char, it indicates that we are opening a device.
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate int
cpr_statefile_open(char * path,char * fs_dev)47986fd29aSsetje cpr_statefile_open(char *path, char *fs_dev)
487c478bd9Sstevel@tonic-gate {
49986fd29aSsetje 	int plen, dlen;
507c478bd9Sstevel@tonic-gate 	int handle;
51986fd29aSsetje 	char fs_pkg[OBP_MAXPATHLEN];
52986fd29aSsetje 	char fs_name[OBP_MAXDRVNAME];
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	/*
557c478bd9Sstevel@tonic-gate 	 * instead of using specialstate, we use fs as the flag
567c478bd9Sstevel@tonic-gate 	 */
57986fd29aSsetje 	if (*fs_dev == '\0') {	/* device open */
58986fd29aSsetje 		statefile_special = 1;
597c478bd9Sstevel@tonic-gate 		handle = prom_open(path);
607c478bd9Sstevel@tonic-gate 		/* IEEE1275 prom_open returns 0 on failure; we return -1 */
617c478bd9Sstevel@tonic-gate 		return (handle ? handle : -1);
627c478bd9Sstevel@tonic-gate 	}
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	/*
65986fd29aSsetje 	 * No cif for $open-package, so we have to use interpret
667c478bd9Sstevel@tonic-gate 	 */
67986fd29aSsetje 	if (prom_getprop(chosen, "fs-package", fs_pkg) == -1) {
68986fd29aSsetje 		prom_printf("Missing fs-package name\n");
69986fd29aSsetje 		return (-1);
70986fd29aSsetje 	}
71986fd29aSsetje 	plen = prom_strlen(fs_pkg);
72986fd29aSsetje 	dlen = prom_strlen(fs_dev);
73986fd29aSsetje 	prom_interpret("$open-package swap l!", plen, (uintptr_t)fs_pkg,
74986fd29aSsetje 	    dlen, (uintptr_t)fs_dev, (uintptr_t)&cb_rih);
75986fd29aSsetje 	if (cb_rih == OBP_BADNODE || cb_rih == 0) {
76986fd29aSsetje 		prom_printf("Can't open %s\n", fs_pkg);
77986fd29aSsetje 		return (-1);
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 
80e7cbe64fSgw 	if (volname) {
81e7cbe64fSgw 		return (cpr_fs_volopen(volname));
82e7cbe64fSgw 	}
83e7cbe64fSgw 
847c478bd9Sstevel@tonic-gate 	/*
85986fd29aSsetje 	 * Prepend '/' if it's not there already
867c478bd9Sstevel@tonic-gate 	 */
87986fd29aSsetje 	if (*path != '/') {
88986fd29aSsetje 		(void) prom_sprintf(fs_name, "/%s", path);
89986fd29aSsetje 		return (cpr_fs_open(fs_name));
90986fd29aSsetje 	} else
91986fd29aSsetje 		return (cpr_fs_open(path));
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
95986fd29aSsetje  * Mount root fs so we can read statefile, etc
96986fd29aSsetje  *
97986fd29aSsetje  * sets global
98986fd29aSsetje  *	cb_rih
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate int
cb_mountroot()101986fd29aSsetje cb_mountroot()
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 
104986fd29aSsetje 	chosen = prom_chosennode();
105986fd29aSsetje 	if (chosen == OBP_BADNODE) {
106986fd29aSsetje 		prom_printf("Missing chosen node\n");
107986fd29aSsetje 		return (ERR);
108986fd29aSsetje 	}
109986fd29aSsetje 	if (prom_getprop(chosen, "bootfs", (caddr_t)&cb_rih) == -1) {
110986fd29aSsetje 		prom_printf("Missing bootfs ihandle\n");
1117c478bd9Sstevel@tonic-gate 		return (ERR);
1127c478bd9Sstevel@tonic-gate 	}
113986fd29aSsetje 	return (0);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
116986fd29aSsetje /*
117986fd29aSsetje  * Unmount root
118986fd29aSsetje  */
119986fd29aSsetje int
cb_unmountroot()120986fd29aSsetje cb_unmountroot()
121986fd29aSsetje {
122986fd29aSsetje 	(void) prom_close(cb_rih);
123986fd29aSsetje 	cb_rih = OBP_BADNODE;
124986fd29aSsetje 	return (0);
125986fd29aSsetje }
1267c478bd9Sstevel@tonic-gate 
127e7cbe64fSgw int
cpr_fs_volopen(char * path)128e7cbe64fSgw cpr_fs_volopen(char *path)
129e7cbe64fSgw {
130e7cbe64fSgw 
131e7cbe64fSgw 	CB_VENTRY(cpr_fs_volopen);
132e7cbe64fSgw 
133e7cbe64fSgw 	if (cb_rih == OBP_BADNODE)
134e7cbe64fSgw 		return (-1);
135e7cbe64fSgw 	return (prom_volopen(cb_rih, path));
136e7cbe64fSgw }
137e7cbe64fSgw 
1387c478bd9Sstevel@tonic-gate /*
139986fd29aSsetje  * Ask prom to open a disk file.
140986fd29aSsetje  */
141986fd29aSsetje int
cpr_fs_open(char * path)142986fd29aSsetje cpr_fs_open(char *path)
143986fd29aSsetje {
144986fd29aSsetje 
145986fd29aSsetje 	CB_VENTRY(cpr_fs_open);
146986fd29aSsetje 
147986fd29aSsetje 	if (cb_rih == OBP_BADNODE)
148986fd29aSsetje 		return (-1);
149986fd29aSsetje 	return (prom_fopen(cb_rih, path));
150986fd29aSsetje }
151986fd29aSsetje 
152986fd29aSsetje 
153986fd29aSsetje /*
154986fd29aSsetje  * Direct read if using block special,
155986fd29aSsetje  * otherwise use fs read
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate int
cpr_read(int fd,caddr_t buf,size_t len)1587c478bd9Sstevel@tonic-gate cpr_read(int fd, caddr_t buf, size_t len)
1597c478bd9Sstevel@tonic-gate {
160e7cbe64fSgw 	if (!statefile_special || volname)
161986fd29aSsetje 		return (cpr_fs_read(fd, buf, len));
162986fd29aSsetje 	else
163986fd29aSsetje 		return (prom_read(fd, buf, len, 0, 0));
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate int
cpr_fs_read(int fd,caddr_t buf,int len)168986fd29aSsetje cpr_fs_read(int fd, caddr_t buf, int len)
1697c478bd9Sstevel@tonic-gate {
170986fd29aSsetje 	if (cb_rih == OBP_BADNODE)
171986fd29aSsetje 		return (-1);
172986fd29aSsetje 	return (prom_fread(cb_rih, fd, buf, len));
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate int
cpr_fs_close(int fd)177986fd29aSsetje cpr_fs_close(int fd)
178986fd29aSsetje {
179986fd29aSsetje 	CB_VPRINTF(("cpr_fs_close 0x%x\n", fd));
180986fd29aSsetje 
181986fd29aSsetje 	if (cb_rih == OBP_BADNODE)
182986fd29aSsetje 		return (-1);
183986fd29aSsetje 	prom_fclose(cb_rih, fd);
184986fd29aSsetje 	return (0);
185986fd29aSsetje }
186986fd29aSsetje 
187986fd29aSsetje int
cpr_fs_seek(int fd,offset_t off)188986fd29aSsetje cpr_fs_seek(int fd, offset_t off)
1897c478bd9Sstevel@tonic-gate {
190986fd29aSsetje 	if (cb_rih == OBP_BADNODE)
191986fd29aSsetje 		return (-1);
192986fd29aSsetje 	return (prom_fseek(cb_rih, fd, off));
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate int
cpr_statefile_close(int fd)1977c478bd9Sstevel@tonic-gate cpr_statefile_close(int fd)
1987c478bd9Sstevel@tonic-gate {
199986fd29aSsetje 	if (statefile_special) {
200986fd29aSsetje 		statefile_special = 0;
201986fd29aSsetje 		return (prom_close(fd));
202986fd29aSsetje 	} else
203986fd29aSsetje 		return (cpr_fs_close(fd));
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate void
cb_spin(void)2087c478bd9Sstevel@tonic-gate cb_spin(void)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate 	static int spindex = 0;
2117c478bd9Sstevel@tonic-gate 	static char *spin_pairs[] = { "|\b", "/\b", "-\b", "\\\b" };
2127c478bd9Sstevel@tonic-gate 	const size_t nspin_pairs = sizeof (spin_pairs) / sizeof (spin_pairs[0]);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	prom_printf(spin_pairs[spindex]);
2157c478bd9Sstevel@tonic-gate 	spindex = (spindex + 1) % nspin_pairs;
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate /*
2207c478bd9Sstevel@tonic-gate  * translate vaddr to phys page number
2217c478bd9Sstevel@tonic-gate  */
2227c478bd9Sstevel@tonic-gate pfn_t
cpr_vatopfn(caddr_t vaddr)2237c478bd9Sstevel@tonic-gate cpr_vatopfn(caddr_t vaddr)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate 	physaddr_t paddr;
2267c478bd9Sstevel@tonic-gate 	int valid, mode;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	(void) prom_translate_virt(vaddr, &valid, &paddr, &mode);
2297c478bd9Sstevel@tonic-gate 	if (valid != -1)
2307c478bd9Sstevel@tonic-gate 		return (PFN_INVALID);
2317c478bd9Sstevel@tonic-gate 	return (paddr >> MMU_PAGESHIFT);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate /*
2367c478bd9Sstevel@tonic-gate  * unmap virt, then map virt to new phys;
2377c478bd9Sstevel@tonic-gate  * see remap definition below
2387c478bd9Sstevel@tonic-gate  */
2397c478bd9Sstevel@tonic-gate int
prom_remap(size_t size,caddr_t virt,physaddr_t phys)2407c478bd9Sstevel@tonic-gate prom_remap(size_t size, caddr_t virt, physaddr_t phys)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	ihandle_t immu;
2437c478bd9Sstevel@tonic-gate 	cell_t ci[8];
2447c478bd9Sstevel@tonic-gate 	int rv;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	immu = prom_mmu_ihandle();
2477c478bd9Sstevel@tonic-gate 	if (immu == (ihandle_t)-1)
2487c478bd9Sstevel@tonic-gate 		return (ERR);
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("call-method");	/* Service name */
2517c478bd9Sstevel@tonic-gate 	ci[1] = (cell_t)5;			/* #argument cells */
2527c478bd9Sstevel@tonic-gate 	ci[2] = (cell_t)0;			/* #result cells */
2537c478bd9Sstevel@tonic-gate 	ci[3] = p1275_ptr2cell("remap");	/* Arg1: Method name */
2547c478bd9Sstevel@tonic-gate 	ci[4] = p1275_ihandle2cell(immu);	/* Arg2: memory ihandle */
2557c478bd9Sstevel@tonic-gate 	ci[5] = p1275_size2cell(size);		/* remap arg0 */
2567c478bd9Sstevel@tonic-gate 	ci[6] = p1275_ptr2cell(virt);		/* remap arg1 */
2577c478bd9Sstevel@tonic-gate 	ci[7] = p1275_ull2cell_low(phys);	/* remap arg2 */
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	promif_preprom();
2607c478bd9Sstevel@tonic-gate 	rv = p1275_cif_handler(ci);
2617c478bd9Sstevel@tonic-gate 	promif_postprom();
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	if (rv)
2647c478bd9Sstevel@tonic-gate 		return (rv);		/* Service "call-method" failed */
2657c478bd9Sstevel@tonic-gate 	return (0);
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate /*
2707c478bd9Sstevel@tonic-gate  * install remap definition in /virtual-memory node;
2717c478bd9Sstevel@tonic-gate  * used for replacing a virt->phys mapping in one promif call;
2727c478bd9Sstevel@tonic-gate  * this needs to be atomic from the client's perspective to
2737c478bd9Sstevel@tonic-gate  * avoid faults while relocating client text.
2747c478bd9Sstevel@tonic-gate  */
2757c478bd9Sstevel@tonic-gate void
install_remap(void)2767c478bd9Sstevel@tonic-gate install_remap(void)
2777c478bd9Sstevel@tonic-gate {
2787c478bd9Sstevel@tonic-gate 	static char remap_def[] =
2797c478bd9Sstevel@tonic-gate 	    "\" /virtual-memory\" find-device "
2807c478bd9Sstevel@tonic-gate 	    ": remap ( phys.lo virt size -- )"
2817c478bd9Sstevel@tonic-gate 	    "	2dup unmap ( phys.lo virt size )"
2827c478bd9Sstevel@tonic-gate 	    "	0 -rot -1 map ( ) ; "
2837c478bd9Sstevel@tonic-gate 	    "device-end";
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	prom_interpret(remap_def, 0, 0, 0, 0, 0);
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate /*
2907c478bd9Sstevel@tonic-gate  * allocate virt and phys space without any mapping;
2917c478bd9Sstevel@tonic-gate  * stores virt and phys addrs at *vap and *pap
2927c478bd9Sstevel@tonic-gate  */
2937c478bd9Sstevel@tonic-gate int
cb_alloc(size_t size,uint_t align,caddr_t * vap,physaddr_t * pap)2947c478bd9Sstevel@tonic-gate cb_alloc(size_t size, uint_t align, caddr_t *vap, physaddr_t *pap)
2957c478bd9Sstevel@tonic-gate {
2967c478bd9Sstevel@tonic-gate 	physaddr_t phys;
2977c478bd9Sstevel@tonic-gate 	caddr_t virt;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	virt = prom_allocate_virt(align, (size_t)align);
3007c478bd9Sstevel@tonic-gate 	if (virt == (caddr_t)-1)
3017c478bd9Sstevel@tonic-gate 		return (ERR);
3027c478bd9Sstevel@tonic-gate 	if (prom_allocate_phys(size, align, &phys) == -1) {
3037c478bd9Sstevel@tonic-gate 		prom_free_virt(size, virt);
3047c478bd9Sstevel@tonic-gate 		return (ERR);
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	*vap = virt;
3087c478bd9Sstevel@tonic-gate 	*pap = phys;
3097c478bd9Sstevel@tonic-gate 	return (0);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate static int
get_intprop(pnode_t node,caddr_t prop,void * dst)314fa9e4066Sahrens get_intprop(pnode_t node, caddr_t prop, void *dst)
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate 	int len, glen;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	len = sizeof (uint_t);
3197c478bd9Sstevel@tonic-gate 	glen = prom_getprop(node, prop, dst);
3207c478bd9Sstevel@tonic-gate 	if (glen != len)
3217c478bd9Sstevel@tonic-gate 		return (ERR);
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	return (0);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate /*
3287c478bd9Sstevel@tonic-gate  * find cpu node for the boot processor
3297c478bd9Sstevel@tonic-gate  *
3307c478bd9Sstevel@tonic-gate  * sets globals:
3317c478bd9Sstevel@tonic-gate  * 	cb_mid
3327c478bd9Sstevel@tonic-gate  */
333fa9e4066Sahrens static pnode_t
get_cpu_node(void)3347c478bd9Sstevel@tonic-gate get_cpu_node(void)
3357c478bd9Sstevel@tonic-gate {
3367c478bd9Sstevel@tonic-gate 	static char *props[] = { "upa-portid", "portid", NULL };
337fa9e4066Sahrens 	pnode_t node;
3387c478bd9Sstevel@tonic-gate 	char *str, *name, **propp;
3397c478bd9Sstevel@tonic-gate 	uint_t cpu_id;
3407c478bd9Sstevel@tonic-gate 	int err;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	str = "get_cpu_node";
3437c478bd9Sstevel@tonic-gate 	name = "cpu";
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	cb_mid = getmid();
3467c478bd9Sstevel@tonic-gate 	for (node = prom_rootnode(); ; node = prom_nextnode(node)) {
3477c478bd9Sstevel@tonic-gate 		node = prom_findnode_bydevtype(node, name);
3487c478bd9Sstevel@tonic-gate 		if (node == OBP_NONODE) {
3497c478bd9Sstevel@tonic-gate 			prom_printf("\n%s: cant find node for devtype \"%s\"\n",
3507c478bd9Sstevel@tonic-gate 			    str, name);
3517c478bd9Sstevel@tonic-gate 			break;
3527c478bd9Sstevel@tonic-gate 		}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 		cpu_id = (uint_t)-1;
3557c478bd9Sstevel@tonic-gate 		for (propp = props; *propp; propp++) {
3567c478bd9Sstevel@tonic-gate 			err = get_intprop(node, *propp, &cpu_id);
3577c478bd9Sstevel@tonic-gate 			CB_VPRINTF(("    cpu node 0x%x, "
3587c478bd9Sstevel@tonic-gate 			    "prop \"%s\", cpu_id %d\n",
3597c478bd9Sstevel@tonic-gate 			    node, *propp, (int)cpu_id));
3607c478bd9Sstevel@tonic-gate 			if (err == 0)
3617c478bd9Sstevel@tonic-gate 				break;
3627c478bd9Sstevel@tonic-gate 		}
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 		if (cpu_id == cb_mid)
3657c478bd9Sstevel@tonic-gate 			return (node);
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	return (OBP_NONODE);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate /*
3737c478bd9Sstevel@tonic-gate  * lookup prom properties
3747c478bd9Sstevel@tonic-gate  *
3757c478bd9Sstevel@tonic-gate  * sets globals:
3767c478bd9Sstevel@tonic-gate  *	cb_dents
3777c478bd9Sstevel@tonic-gate  *	cb_clock_freq
3787c478bd9Sstevel@tonic-gate  *	cpu_delay
3797c478bd9Sstevel@tonic-gate  */
3807c478bd9Sstevel@tonic-gate int
cb_get_props(void)3817c478bd9Sstevel@tonic-gate cb_get_props(void)
3827c478bd9Sstevel@tonic-gate {
3837c478bd9Sstevel@tonic-gate 	uint_t clock_mhz;
384fa9e4066Sahrens 	pnode_t node;
3857c478bd9Sstevel@tonic-gate 	struct cb_props *cbp;
3867c478bd9Sstevel@tonic-gate 	static struct cb_props cpu_data[] = {
3877c478bd9Sstevel@tonic-gate 		"#dtlb-entries", &cb_dents,
3887c478bd9Sstevel@tonic-gate 		"clock-frequency", &cb_clock_freq,
3897c478bd9Sstevel@tonic-gate 		NULL, NULL,
3907c478bd9Sstevel@tonic-gate 	};
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	CB_VENTRY(cb_get_props);
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	node = get_cpu_node();
3957c478bd9Sstevel@tonic-gate 	if (node == OBP_NONODE)
3967c478bd9Sstevel@tonic-gate 		return (ERR);
3977c478bd9Sstevel@tonic-gate 	for (cbp = cpu_data; cbp->prop; cbp++) {
3987c478bd9Sstevel@tonic-gate 		if (get_intprop(node, cbp->prop, cbp->datap)) {
3997c478bd9Sstevel@tonic-gate 			prom_printf("\n%s: getprop error, "
4007c478bd9Sstevel@tonic-gate 			    "node 0x%x, prop \"%s\"\n",
4017c478bd9Sstevel@tonic-gate 			    prog, node, cbp->prop);
4027c478bd9Sstevel@tonic-gate 			return (ERR);
4037c478bd9Sstevel@tonic-gate 		}
4047c478bd9Sstevel@tonic-gate 		CB_VPRINTF(("    \"%s\" = 0x%x\n",
4057c478bd9Sstevel@tonic-gate 		    cbp->prop, *cbp->datap));
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	/*
4097c478bd9Sstevel@tonic-gate 	 * setup cpu_delay for cb_usec_wait
4107c478bd9Sstevel@tonic-gate 	 */
4117c478bd9Sstevel@tonic-gate 	clock_mhz = (cb_clock_freq + 500000) / 1000000;
4127c478bd9Sstevel@tonic-gate 	cpu_delay = clock_mhz - 7;
4137c478bd9Sstevel@tonic-gate 	CB_VPRINTF(("    clock_mhz %d, cpu_delay %d\n",
4147c478bd9Sstevel@tonic-gate 	    clock_mhz, cpu_delay));
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	return (0);
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate /*
4217c478bd9Sstevel@tonic-gate  * map-in data pages
4227c478bd9Sstevel@tonic-gate  * size should fit tte_bit.sz
4237c478bd9Sstevel@tonic-gate  * rw should be 0 or TTE_HWWR_INT
4247c478bd9Sstevel@tonic-gate  */
4257c478bd9Sstevel@tonic-gate void
cb_mapin(caddr_t vaddr,pfn_t ppn,uint_t size,uint_t rw,uint_t dtlb_index)4267c478bd9Sstevel@tonic-gate cb_mapin(caddr_t vaddr, pfn_t ppn, uint_t size, uint_t rw, uint_t dtlb_index)
4277c478bd9Sstevel@tonic-gate {
4287c478bd9Sstevel@tonic-gate 	tte_t tte;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	tte.tte_inthi = TTE_VALID_INT | TTE_SZ_INT(size) |
4317c478bd9Sstevel@tonic-gate 	    TTE_PFN_INTHI(ppn);
4327c478bd9Sstevel@tonic-gate 	tte.tte_intlo = TTE_PFN_INTLO(ppn) | TTE_LCK_INT |
4337c478bd9Sstevel@tonic-gate 	    TTE_CP_INT | TTE_CV_INT | TTE_PRIV_INT | rw;
4347c478bd9Sstevel@tonic-gate 	set_dtlb_entry(dtlb_index, vaddr, &tte);
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate static char *
prom_strstr(char * string,char * substr)4397c478bd9Sstevel@tonic-gate prom_strstr(char *string, char *substr)
4407c478bd9Sstevel@tonic-gate {
4417c478bd9Sstevel@tonic-gate 	char *strp, *subp, *tmp, c;
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	if (substr == NULL || *substr == '\0')
4447c478bd9Sstevel@tonic-gate 		return (string);
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	strp = string;
4477c478bd9Sstevel@tonic-gate 	subp = substr;
4487c478bd9Sstevel@tonic-gate 	c = *subp;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	while (*strp) {
4517c478bd9Sstevel@tonic-gate 		if (*strp++ == c) {
4527c478bd9Sstevel@tonic-gate 			tmp = strp;
4537c478bd9Sstevel@tonic-gate 			while ((c = *++subp) == *strp++ && c)
4547c478bd9Sstevel@tonic-gate 				;
4557c478bd9Sstevel@tonic-gate 			if (c == '\0')
4567c478bd9Sstevel@tonic-gate 				return (tmp - 1);
4577c478bd9Sstevel@tonic-gate 			strp = tmp;
4587c478bd9Sstevel@tonic-gate 			subp = substr;
4597c478bd9Sstevel@tonic-gate 			c = *subp;
4607c478bd9Sstevel@tonic-gate 		}
4617c478bd9Sstevel@tonic-gate 	}
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	return (NULL);
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate static void
cb_set_idev(char * istr)4687c478bd9Sstevel@tonic-gate cb_set_idev(char *istr)
4697c478bd9Sstevel@tonic-gate {
4707c478bd9Sstevel@tonic-gate 	if (reset_input) {
4717c478bd9Sstevel@tonic-gate 		prom_interpret(istr, 0, 0, 0, 0, 0);
4727c478bd9Sstevel@tonic-gate 		CB_VPRINTF(("\ncb_set_idev: reset with [%s]\n", istr));
4737c478bd9Sstevel@tonic-gate 	}
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate /*
4787c478bd9Sstevel@tonic-gate  * workaround for USB keyboard:
4797c478bd9Sstevel@tonic-gate  * USB DMA activity has been known to corrupt kernel pages while cprboot
4807c478bd9Sstevel@tonic-gate  * is restoring them.  to quiesce the USB chip, we craft a "null" device
4817c478bd9Sstevel@tonic-gate  * and temporarily use that as the prom's input device.  this effectively
4827c478bd9Sstevel@tonic-gate  * disables the USB keyboard until the cpr module restores the original
4837c478bd9Sstevel@tonic-gate  * prom and a kernel driver re-inits and takes-over control of USB.
4847c478bd9Sstevel@tonic-gate  *
4857c478bd9Sstevel@tonic-gate  * may set globals:
4867c478bd9Sstevel@tonic-gate  *	reset_input
4877c478bd9Sstevel@tonic-gate  */
4887c478bd9Sstevel@tonic-gate int
cb_usb_setup(void)4897c478bd9Sstevel@tonic-gate cb_usb_setup(void)
4907c478bd9Sstevel@tonic-gate {
4917c478bd9Sstevel@tonic-gate 	char sp[OBP_MAXPATHLEN];
492*b531f6d1SToomas Soome 	static char cb_nulldev[] =
4937c478bd9Sstevel@tonic-gate 		"\" /\" select-dev "
4947c478bd9Sstevel@tonic-gate 		"new-device "
4957c478bd9Sstevel@tonic-gate 		"\" nulldev\" device-name "
4967c478bd9Sstevel@tonic-gate 		": read 2drop -2 ; "
4977c478bd9Sstevel@tonic-gate 		": open true ; "
4987c478bd9Sstevel@tonic-gate 		": close ; "
4997c478bd9Sstevel@tonic-gate 		": install-abort ; "
5007c478bd9Sstevel@tonic-gate 		": remove-abort ; "
5017c478bd9Sstevel@tonic-gate 		": write 2drop 0 ; "
5027c478bd9Sstevel@tonic-gate 		": restore ; "
5037c478bd9Sstevel@tonic-gate 		"finish-device "
504*b531f6d1SToomas Soome 		"unselect-dev";
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	CB_VENTRY(cb_usb_setup);
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	bzero(sp, sizeof (sp));
5097c478bd9Sstevel@tonic-gate 	prom_interpret("stdin @ ihandle>devname swap -rot move",
5107c478bd9Sstevel@tonic-gate 	    (uintptr_t)sp, 0, 0, 0, 0);
5117c478bd9Sstevel@tonic-gate 	if (prom_strstr(sp, "usb") && prom_strstr(sp, "keyboard")) {
5127c478bd9Sstevel@tonic-gate 		prom_interpret(cb_nulldev, 0, 0, 0, 0, 0);
5137c478bd9Sstevel@tonic-gate 		reset_input = 1;
5147c478bd9Sstevel@tonic-gate 		cb_set_idev(null_input);
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	return (0);
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate /*
5227c478bd9Sstevel@tonic-gate  * switch input to keyboard before entering the prom, and switch to the
5237c478bd9Sstevel@tonic-gate  * crafted nulldev after returning from the prom.  this occurs only when
5247c478bd9Sstevel@tonic-gate  * stdinpath is a USB keyboard; entering the prom is usually done only
5257c478bd9Sstevel@tonic-gate  * for debugging purposes - see check_halt() and above DMA comment.
5267c478bd9Sstevel@tonic-gate  */
5277c478bd9Sstevel@tonic-gate void
cb_enter_mon(void)5287c478bd9Sstevel@tonic-gate cb_enter_mon(void)
5297c478bd9Sstevel@tonic-gate {
5307c478bd9Sstevel@tonic-gate 	cb_set_idev(kbd_input);
5317c478bd9Sstevel@tonic-gate 	prom_enter_mon();
5327c478bd9Sstevel@tonic-gate 	cb_set_idev(null_input);
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate /*
5377c478bd9Sstevel@tonic-gate  * similar to above before exiting to the prom
5387c478bd9Sstevel@tonic-gate  */
5397c478bd9Sstevel@tonic-gate void
cb_exit_to_mon(void)5407c478bd9Sstevel@tonic-gate cb_exit_to_mon(void)
5417c478bd9Sstevel@tonic-gate {
5427c478bd9Sstevel@tonic-gate 	cb_set_idev(kbd_input);
5437c478bd9Sstevel@tonic-gate 	prom_exit_to_mon();
5447c478bd9Sstevel@tonic-gate }
545