11ae08745Sheppo /*
21ae08745Sheppo  * CDDL HEADER START
31ae08745Sheppo  *
41ae08745Sheppo  * The contents of this file are subject to the terms of the
51ae08745Sheppo  * Common Development and Distribution License (the "License").
61ae08745Sheppo  * You may not use this file except in compliance with the License.
71ae08745Sheppo  *
81ae08745Sheppo  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91ae08745Sheppo  * or http://www.opensolaris.org/os/licensing.
101ae08745Sheppo  * See the License for the specific language governing permissions
111ae08745Sheppo  * and limitations under the License.
121ae08745Sheppo  *
131ae08745Sheppo  * When distributing Covered Code, include this CDDL HEADER in each
141ae08745Sheppo  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151ae08745Sheppo  * If applicable, add the following below this CDDL HEADER, with the
161ae08745Sheppo  * fields enclosed by brackets "[]" replaced with your own identifying
171ae08745Sheppo  * information: Portions Copyright [yyyy] [name of copyright owner]
181ae08745Sheppo  *
191ae08745Sheppo  * CDDL HEADER END
201ae08745Sheppo  */
211ae08745Sheppo 
221ae08745Sheppo /*
239853d9e8SJason Beloro  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
241ae08745Sheppo  * Use is subject to license terms.
251ae08745Sheppo  */
261ae08745Sheppo 
271ae08745Sheppo #include <sys/promif_impl.h>
281ae08745Sheppo #include <sys/machsystm.h>
291ae08745Sheppo #include <sys/lpad.h>
301ae08745Sheppo #include <sys/vmsystm.h>
311ae08745Sheppo #include <sys/prom_plat.h>
321ae08745Sheppo #include <sys/ldoms.h>
331ae08745Sheppo #include <sys/kobj.h>
341ae08745Sheppo #include <sys/reboot.h>
351ae08745Sheppo #include <sys/hypervisor_api.h>
364bac2208Snarayan #include <sys/mdesc.h>
374bac2208Snarayan #include <sys/mach_descrip.h>
389853d9e8SJason Beloro #include <sys/cpu_module.h>
391ae08745Sheppo 
401ae08745Sheppo #ifndef _KMDB
419853d9e8SJason Beloro #include <sys/pte.h>
429853d9e8SJason Beloro #include <vm/hat_sfmmu.h>
439853d9e8SJason Beloro #include <sys/memlist_impl.h>
449853d9e8SJason Beloro 
451ae08745Sheppo static processorid_t cif_cpu;
461ae08745Sheppo static struct translation *cif_prom_trans;
471ae08745Sheppo static size_t cif_prom_ntrans;
481ae08745Sheppo 
491ae08745Sheppo int cif_cpu_mp_ready;
501ae08745Sheppo int (*prom_cif_handler)(void *) = NULL;
519853d9e8SJason Beloro 
529853d9e8SJason Beloro extern struct memlist *phys_avail;
539853d9e8SJason Beloro extern struct vnode prom_ppages;
549853d9e8SJason Beloro extern void kdi_tlb_page_unlock(caddr_t, int);
559853d9e8SJason Beloro 
569853d9e8SJason Beloro #define	COMBINE(hi, lo) (((uint64_t)(uint32_t)(hi) << 32) | (uint32_t)(lo))
579853d9e8SJason Beloro #define	OFW_PT_START_ADDR	0xfffffffc00000000	/* OBP PT start */
589853d9e8SJason Beloro #define	OFW_PT_END_ADDR		0xffffffffffffffff	/* OBP PT end */
599853d9e8SJason Beloro 
609853d9e8SJason Beloro #define	PROM_ADDR(a)	(((a) >= OFW_START_ADDR && (a) <= OFW_END_ADDR) || \
619853d9e8SJason Beloro 			((a) >= OFW_PT_START_ADDR && (a) <= OFW_PT_END_ADDR))
621ae08745Sheppo #endif
631ae08745Sheppo 
641ae08745Sheppo #ifdef DEBUG
651ae08745Sheppo uint_t cif_debug;
669853d9e8SJason Beloro int prom_free_debug;
679853d9e8SJason Beloro #define	PMFREE_DEBUG(args...) if (prom_free_debug) printf(args)
689853d9e8SJason Beloro #else
699853d9e8SJason Beloro #define	PMFREE_DEBUG(args...)
709853d9e8SJason Beloro #endif
711ae08745Sheppo 
721ae08745Sheppo extern int (*cif_handler)(void *);
731ae08745Sheppo 
741ae08745Sheppo typedef struct {
751ae08745Sheppo 	char		*name;
761ae08745Sheppo 	cif_func_t	func;
771ae08745Sheppo } cif_callback_t;
781ae08745Sheppo 
791ae08745Sheppo static cif_callback_t cb_table[] = {
801ae08745Sheppo 	{ "getprop",			promif_getprop		    },
811ae08745Sheppo 	{ "getproplen",			promif_getproplen	    },
821ae08745Sheppo 	{ "nextprop",			promif_nextprop		    },
831ae08745Sheppo 	{ "peer",			promif_nextnode		    },
841ae08745Sheppo 	{ "child",			promif_childnode	    },
851ae08745Sheppo 	{ "parent",			promif_parentnode	    },
861ae08745Sheppo 	{ "enter",			promif_enter_mon	    },
871ae08745Sheppo 	{ "exit",			promif_exit_to_mon	    },
881ae08745Sheppo 	{ "boot",			promif_reboot		    },
891ae08745Sheppo 	{ "write",			promif_write		    },
901ae08745Sheppo 	{ "read",			promif_read		    },
911ae08745Sheppo 	{ "interpret",			promif_interpret	    },
921ae08745Sheppo 	{ "finddevice",			promif_finddevice	    },
931ae08745Sheppo 	{ "instance-to-package",	promif_instance_to_package  },
941ae08745Sheppo #ifndef _KMDB
951ae08745Sheppo 	{ "setprop",			promif_setprop		    },
961ae08745Sheppo 	{ "test",			promif_test		    },
971ae08745Sheppo 	{ "instance-to-path",		promif_instance_to_path	    },
981ae08745Sheppo 	{ "SUNW,power-off",		promif_power_off	    },
991ae08745Sheppo 	{ "SUNW,asr-list-keys-len",	promif_asr_list_keys_len    },
1001ae08745Sheppo 	{ "SUNW,asr-list-keys",		promif_asr_list_keys	    },
1011ae08745Sheppo 	{ "SUNW,asr-export-len",	promif_asr_export_len	    },
1021ae08745Sheppo 	{ "SUNW,asr-export",		promif_asr_export	    },
1031ae08745Sheppo 	{ "SUNW,set-security-key",	promif_set_security_key	    },
1041ae08745Sheppo 	{ "SUNW,get-security-key",	promif_get_security_key	    },
1051ae08745Sheppo 	{ "SUNW,start-cpu-by-cpuid",	promif_start_cpu	    },
1061ae08745Sheppo 	{ "SUNW,set-trap-table",	promif_set_mmfsa_traptable  },
1071ae08745Sheppo 	{ "SUNW,set-sun4v-api-version",	promif_set_sun4v_api_version },
1081ae08745Sheppo 	{ "SUNW,get-sun4v-api-version",	promif_get_sun4v_api_version },
1091ae08745Sheppo #endif
1101ae08745Sheppo 	{ NULL,				NULL			    }
1111ae08745Sheppo };
1121ae08745Sheppo 
1131ae08745Sheppo cif_func_t
1141ae08745Sheppo promif_find_cif_callback(char *opname)
1151ae08745Sheppo {
1161ae08745Sheppo 	cif_callback_t	*cb;
1171ae08745Sheppo 
1181ae08745Sheppo 	if (opname == NULL)
1191ae08745Sheppo 		return (NULL);
1201ae08745Sheppo 
1211ae08745Sheppo 	for (cb = cb_table; cb->name; cb++) {
1221ae08745Sheppo 		if (prom_strcmp(cb->name, opname) == 0)
1231ae08745Sheppo 			break;
1241ae08745Sheppo 	}
1251ae08745Sheppo 
1261ae08745Sheppo 	return (cb->func);
1271ae08745Sheppo }
1281ae08745Sheppo 
1291ae08745Sheppo static int
1301ae08745Sheppo kern_cif_handler(void *p)
1311ae08745Sheppo {
1321ae08745Sheppo 	cell_t		*ci = (cell_t *)p;
1331ae08745Sheppo 	char		*opname;
1341ae08745Sheppo 	cif_func_t	func;
1351ae08745Sheppo 	int		rv;
1361ae08745Sheppo 
1371ae08745Sheppo 	ASSERT(cif_handler == kern_cif_handler);
1381ae08745Sheppo 
1391ae08745Sheppo #ifndef _KMDB
1401ae08745Sheppo 	cif_cpu = getprocessorid();
1411ae08745Sheppo #endif
1421ae08745Sheppo 
1431ae08745Sheppo 	opname = p1275_cell2ptr(ci[0]);
1441ae08745Sheppo 
1451ae08745Sheppo 	/* lookup the callback for the desired operation */
1461ae08745Sheppo 	func = promif_find_cif_callback(opname);
1471ae08745Sheppo 
1481ae08745Sheppo 	if (func == NULL) {
1491ae08745Sheppo #ifdef _KMDB
1501ae08745Sheppo 		prom_fatal_error("sun4v unsupported CIFs\n");
1511ae08745Sheppo #else
1521ae08745Sheppo 		cmn_err(CE_CONT, "!sun4v unsupported CIF: %s\n", opname);
1531ae08745Sheppo 		return (-1);
1541ae08745Sheppo #endif
1551ae08745Sheppo 	}
1561ae08745Sheppo 
1571ae08745Sheppo 	/* callback found, execute it */
1581ae08745Sheppo 	rv = func(p);
1591ae08745Sheppo 
1601ae08745Sheppo #ifndef _KMDB
1611ae08745Sheppo 	cif_cpu = -1;
1621ae08745Sheppo #endif
1631ae08745Sheppo 
1641ae08745Sheppo 	return (rv);
1651ae08745Sheppo }
1661ae08745Sheppo 
1671ae08745Sheppo #ifdef _KMDB
1681ae08745Sheppo 
1691ae08745Sheppo void
1701ae08745Sheppo cif_init(char *pgmname, caddr_t root, ihandle_t in, ihandle_t out,
1711ae08745Sheppo     phandle_t pin, phandle_t pout, pnode_t chosen, pnode_t options)
1721ae08745Sheppo {
1731ae08745Sheppo 	/* initialize pointer to a copy of OBP device tree */
1741ae08745Sheppo 	promif_stree_setroot(root);
1751ae08745Sheppo 
1761ae08745Sheppo 	promif_set_nodes(chosen, options);
1771ae08745Sheppo 
1781ae08745Sheppo 	/* initialize io parameters */
1791ae08745Sheppo 	promif_io_init(in, out, pin, pout);
1801ae08745Sheppo 
1811ae08745Sheppo 	/*
1821ae08745Sheppo 	 * Switch CIF handler to the kernel.
1831ae08745Sheppo 	 */
1841ae08745Sheppo 	if (pgmname != NULL)
1851ae08745Sheppo 		prom_init(pgmname, (void *)kern_cif_handler);
1861ae08745Sheppo 	else
1871ae08745Sheppo 		cif_handler = kern_cif_handler;
1881ae08745Sheppo }
1891ae08745Sheppo 
1901ae08745Sheppo #else
1911ae08745Sheppo 
1929853d9e8SJason Beloro static struct translation *
1939853d9e8SJason Beloro read_prom_mappings(size_t *ntransp)
1949853d9e8SJason Beloro {
1959853d9e8SJason Beloro 	char *prop = "translations";
1969853d9e8SJason Beloro 	pnode_t node;
1979853d9e8SJason Beloro 	size_t translen;
1989853d9e8SJason Beloro 	ihandle_t immu;
1999853d9e8SJason Beloro 	struct translation *transroot;
2009853d9e8SJason Beloro 
2019853d9e8SJason Beloro 	*ntransp = 0;
2029853d9e8SJason Beloro 
2039853d9e8SJason Beloro 	/*
2049853d9e8SJason Beloro 	 * the "translations" property is associated with the mmu node
2059853d9e8SJason Beloro 	 */
2069853d9e8SJason Beloro 	if ((immu = prom_mmu_ihandle()) == (ihandle_t)-1) {
2079853d9e8SJason Beloro 		PMFREE_DEBUG("no mmu ihandle");
2089853d9e8SJason Beloro 		return (NULL);
2099853d9e8SJason Beloro 	}
2109853d9e8SJason Beloro 	node = (pnode_t)prom_getphandle(immu);
2119853d9e8SJason Beloro 	if (node == OBP_NONODE || node == OBP_BADNODE) {
2129853d9e8SJason Beloro 		PMFREE_DEBUG("no mmu node");
2139853d9e8SJason Beloro 		return (NULL);
2149853d9e8SJason Beloro 	}
2159853d9e8SJason Beloro 
2169853d9e8SJason Beloro 	if ((translen = prom_getproplen(node, prop)) == -1) {
2179853d9e8SJason Beloro 		PMFREE_DEBUG("no translations property");
2189853d9e8SJason Beloro 		return (NULL);
2199853d9e8SJason Beloro 	}
2209853d9e8SJason Beloro 	transroot = (struct translation *)kmem_zalloc(translen, KM_SLEEP);
2219853d9e8SJason Beloro 
2229853d9e8SJason Beloro 	if (prom_getprop(node, prop, (caddr_t)transroot) == -1) {
2239853d9e8SJason Beloro 		PMFREE_DEBUG("translations getprop failed");
2249853d9e8SJason Beloro 		kmem_free(transroot, translen);
2259853d9e8SJason Beloro 		return (NULL);
2269853d9e8SJason Beloro 	}
2279853d9e8SJason Beloro 	*ntransp = translen / sizeof (*transroot);
2289853d9e8SJason Beloro 
2299853d9e8SJason Beloro 	return (transroot);
2309853d9e8SJason Beloro }
2319853d9e8SJason Beloro 
2329853d9e8SJason Beloro static void
2339853d9e8SJason Beloro unmap_prom_mappings(struct translation *transroot, size_t ntransroot)
2349853d9e8SJason Beloro {
2359853d9e8SJason Beloro 	int i, j, rv;
236*0a9b4b43SBijan Mottahedeh 	int npgs, nunmapped, nfreed, nskipped, nskipped_io;
2379853d9e8SJason Beloro 	char *p;
2389853d9e8SJason Beloro 	tte_t tte;
2399853d9e8SJason Beloro 	pfn_t pfn;
2409853d9e8SJason Beloro 	page_t *pp;
2419853d9e8SJason Beloro 	uint64_t vaddr;
2429853d9e8SJason Beloro 	struct translation *promt;
2439853d9e8SJason Beloro 	cpuset_t other_cpus;
2449853d9e8SJason Beloro 
2459853d9e8SJason Beloro 	/*
2469853d9e8SJason Beloro 	 * During startup isa_list is allocated in OBP address space
2479853d9e8SJason Beloro 	 * so it needs to be re-allocated in kernel address space
2489853d9e8SJason Beloro 	 * before OBP memory is unmapped.
2499853d9e8SJason Beloro 	 *
2509853d9e8SJason Beloro 	 * see cpu_setup_common().
2519853d9e8SJason Beloro 	 */
2529853d9e8SJason Beloro 	p = kmem_zalloc(strlen(isa_list) + 1, KM_SLEEP);
2539853d9e8SJason Beloro 	(void) strcpy(p, isa_list);
2549853d9e8SJason Beloro 	isa_list = p;
2559853d9e8SJason Beloro 
2569853d9e8SJason Beloro 	nfreed = 0;
2579853d9e8SJason Beloro 	nunmapped = 0;
2589853d9e8SJason Beloro 	nskipped = 0;
259*0a9b4b43SBijan Mottahedeh 	nskipped_io = 0;
2609853d9e8SJason Beloro 
2619853d9e8SJason Beloro 	for (i = 0, promt = transroot; i < ntransroot; i++, promt++) {
2629853d9e8SJason Beloro 		ASSERT(promt->tte_hi != 0);
2639853d9e8SJason Beloro 		ASSERT32(promt->virt_hi == 0 && promt->size_hi == 0);
2649853d9e8SJason Beloro 
2659853d9e8SJason Beloro 		vaddr = COMBINE(promt->virt_hi, promt->virt_lo);
2669853d9e8SJason Beloro 
2679853d9e8SJason Beloro 		if (!PROM_ADDR(vaddr)) {
2689853d9e8SJason Beloro 			nskipped++;
2699853d9e8SJason Beloro 			continue;
2709853d9e8SJason Beloro 		}
2719853d9e8SJason Beloro 
2729853d9e8SJason Beloro 		npgs = mmu_btopr(COMBINE(promt->size_hi, promt->size_lo));
2739853d9e8SJason Beloro 
2749853d9e8SJason Beloro 		if (npgs > 1) {
2759853d9e8SJason Beloro 			PMFREE_DEBUG("large trans vaddr=0x%lx, npgs=%d\n",
2769853d9e8SJason Beloro 			    vaddr, npgs);
2779853d9e8SJason Beloro 		}
2789853d9e8SJason Beloro 		for (j = 0; j < npgs; j++) {
2799853d9e8SJason Beloro 
2809853d9e8SJason Beloro 			pfn = sfmmu_vatopfn((caddr_t)vaddr, KHATID, &tte);
2819853d9e8SJason Beloro 
2829853d9e8SJason Beloro 			if (pfn == PFN_INVALID) {
2839853d9e8SJason Beloro 				tte.tte_inthi = promt->tte_hi;
2849853d9e8SJason Beloro 				tte.tte_intlo = promt->tte_lo;
2859853d9e8SJason Beloro 				pfn = TTE_TO_PFN((caddr_t)COMBINE(
2869853d9e8SJason Beloro 				    promt->virt_hi, promt->virt_lo), &tte);
2879853d9e8SJason Beloro 				PMFREE_DEBUG(
2889853d9e8SJason Beloro 				    "no mapping for vaddr=0x%lx (opfn=0x%lx)\n",
2899853d9e8SJason Beloro 				    vaddr, pfn);
2909853d9e8SJason Beloro 				break;
2919853d9e8SJason Beloro 			}
2929853d9e8SJason Beloro 			ASSERT(!TTE_IS_LOCKED(&tte));
2939853d9e8SJason Beloro 			ASSERT(TTE_IS_8K(&tte));
2949853d9e8SJason Beloro 
2959853d9e8SJason Beloro 			/*
296*0a9b4b43SBijan Mottahedeh 			 * Unload the current mapping for the pfn and
297*0a9b4b43SBijan Mottahedeh 			 * if it is the last mapping for a memory page,
298*0a9b4b43SBijan Mottahedeh 			 * free the page.
2999853d9e8SJason Beloro 			 */
300*0a9b4b43SBijan Mottahedeh 			PMFREE_DEBUG("unmap vaddr=0x%lx pfn=0x%lx", vaddr, pfn);
3019853d9e8SJason Beloro 
3029853d9e8SJason Beloro 			hat_unload(kas.a_hat, (caddr_t)vaddr, PAGESIZE,
3039853d9e8SJason Beloro 			    HAT_UNLOAD_UNLOCK);
3049853d9e8SJason Beloro 
305*0a9b4b43SBijan Mottahedeh 			if (pf_is_memory(pfn)) {
306*0a9b4b43SBijan Mottahedeh 				pp = page_numtopp_nolock(pfn);
307*0a9b4b43SBijan Mottahedeh 				PMFREE_DEBUG(" pp=0x%p", (void *)pp);
308*0a9b4b43SBijan Mottahedeh 				ASSERT(pp);
309*0a9b4b43SBijan Mottahedeh 				ASSERT(PAGE_EXCL(pp));
310*0a9b4b43SBijan Mottahedeh 				ASSERT(PP_ISNORELOC(pp));
311*0a9b4b43SBijan Mottahedeh 				ASSERT(!PP_ISFREE(pp));
312*0a9b4b43SBijan Mottahedeh 				ASSERT(page_find(&prom_ppages, pfn));
313*0a9b4b43SBijan Mottahedeh 				ASSERT(page_get_pagecnt(pp->p_szc) == 1);
314*0a9b4b43SBijan Mottahedeh 
315*0a9b4b43SBijan Mottahedeh 				if (pp->p_mapping) {
316*0a9b4b43SBijan Mottahedeh 					PMFREE_DEBUG(" skip\n");
317*0a9b4b43SBijan Mottahedeh 				} else {
318*0a9b4b43SBijan Mottahedeh 					PP_CLRNORELOC(pp);
319*0a9b4b43SBijan Mottahedeh 					page_destroy(pp, 0);
320*0a9b4b43SBijan Mottahedeh 					memlist_write_lock();
321*0a9b4b43SBijan Mottahedeh 					rv = memlist_add_span(pfn << PAGESHIFT,
322*0a9b4b43SBijan Mottahedeh 					    PAGESIZE, &phys_avail);
323*0a9b4b43SBijan Mottahedeh 					ASSERT(rv == MEML_SPANOP_OK);
324*0a9b4b43SBijan Mottahedeh 					memlist_write_unlock();
325*0a9b4b43SBijan Mottahedeh 					PMFREE_DEBUG(" free\n");
326*0a9b4b43SBijan Mottahedeh 					nfreed++;
327*0a9b4b43SBijan Mottahedeh 				}
3289853d9e8SJason Beloro 			} else {
329*0a9b4b43SBijan Mottahedeh 				nskipped_io++;
330*0a9b4b43SBijan Mottahedeh 				PMFREE_DEBUG(" skip IO\n");
3319853d9e8SJason Beloro 			}
3329853d9e8SJason Beloro 			nunmapped++;
3339853d9e8SJason Beloro 			vaddr += PAGESIZE;
3349853d9e8SJason Beloro 		}
3359853d9e8SJason Beloro 	}
3369853d9e8SJason Beloro 
3379853d9e8SJason Beloro 	if (transroot) {
338*0a9b4b43SBijan Mottahedeh 		PMFREE_DEBUG(
339*0a9b4b43SBijan Mottahedeh 		    "nunmapped=%d nfreed=%d nskipped=%d nskipped_io=%d\n",
340*0a9b4b43SBijan Mottahedeh 		    nunmapped, nfreed, nskipped, nskipped_io);
3419853d9e8SJason Beloro 		kmem_free(transroot, ntransroot * sizeof (*transroot));
3429853d9e8SJason Beloro 	}
3439853d9e8SJason Beloro 
3449853d9e8SJason Beloro 	/*
3459853d9e8SJason Beloro 	 * Unload OBP permanent mappings.
3469853d9e8SJason Beloro 	 */
3479853d9e8SJason Beloro 	kdi_tlb_page_unlock((caddr_t)OFW_START_ADDR, 1);
3489853d9e8SJason Beloro 	kpreempt_disable();
3499853d9e8SJason Beloro 	other_cpus = cpu_ready_set;
3509853d9e8SJason Beloro 	CPUSET_DEL(other_cpus, CPU->cpu_id);
3519853d9e8SJason Beloro 	xt_some(other_cpus, vtag_unmap_perm_tl1, (uint64_t)OFW_START_ADDR,
3529853d9e8SJason Beloro 	    KCONTEXT);
3539853d9e8SJason Beloro 	kpreempt_enable();
3549853d9e8SJason Beloro }
3559853d9e8SJason Beloro 
3561ae08745Sheppo static void cache_prom_data(void);
3571ae08745Sheppo 
3581ae08745Sheppo /*
3591ae08745Sheppo  * This function returns 1 if the current thread is executing in
3601ae08745Sheppo  * the CIF and 0 otherwise. This is useful information to know
3611ae08745Sheppo  * since code that implements CIF handlers can assume that it has
3621ae08745Sheppo  * gone through the kern_preprom() entry point, implying it is
3631ae08745Sheppo  * running single threaded, has preemption disabled, etc.
3641ae08745Sheppo  */
3651ae08745Sheppo int
3661ae08745Sheppo promif_in_cif(void)
3671ae08745Sheppo {
3681ae08745Sheppo 	int	mycpuid = getprocessorid();
3691ae08745Sheppo 
3701ae08745Sheppo 	return ((cif_cpu == mycpuid) ? 1 : 0);
3711ae08745Sheppo }
3721ae08745Sheppo 
3734bac2208Snarayan /*
3744bac2208Snarayan  * Check that all cpus in the MD are within range (< NCPU).  Attempt
3754bac2208Snarayan  * to stop any that aren't.
3764bac2208Snarayan  */
3774bac2208Snarayan static void
3784bac2208Snarayan cif_check_cpus(void)
3794bac2208Snarayan {
3804bac2208Snarayan 	md_t		*mdp;
3814bac2208Snarayan 	mde_cookie_t	rootnode;
3824bac2208Snarayan 	size_t		listsz;
3834bac2208Snarayan 	int		i;
3844bac2208Snarayan 	mde_cookie_t	*listp = NULL;
3854bac2208Snarayan 	int		num_nodes;
3864bac2208Snarayan 	uint64_t	cpuid;
3874bac2208Snarayan 	int		status;
3884bac2208Snarayan 
3894bac2208Snarayan 	mdp = md_get_handle();
3904bac2208Snarayan 	ASSERT(mdp);
3914bac2208Snarayan 
3924bac2208Snarayan 	rootnode = md_root_node(mdp);
3934bac2208Snarayan 	ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE);
3944bac2208Snarayan 
3954bac2208Snarayan 	num_nodes = md_node_count(mdp);
3964bac2208Snarayan 	ASSERT(num_nodes > 0);
3974bac2208Snarayan 
3984bac2208Snarayan 	listsz = num_nodes * sizeof (mde_cookie_t);
3994bac2208Snarayan 	listp = kmem_zalloc(listsz, KM_SLEEP);
4004bac2208Snarayan 
4014bac2208Snarayan 	num_nodes = md_scan_dag(mdp, rootnode, md_find_name(mdp, "cpu"),
4024bac2208Snarayan 	    md_find_name(mdp, "fwd"), listp);
4034bac2208Snarayan 
4044bac2208Snarayan 	if (num_nodes <= 0)
4054bac2208Snarayan 		goto done;
4064bac2208Snarayan 
4074bac2208Snarayan 	for (i = 0; i < num_nodes; i++) {
4084bac2208Snarayan 		if (md_get_prop_val(mdp, listp[i], "id", &cpuid)) {
4094bac2208Snarayan 			cmn_err(CE_WARN, "cif_check_cpus: "
4104bac2208Snarayan 			    "CPU instance %d has no 'id' property", i);
4114bac2208Snarayan 			continue;
4124bac2208Snarayan 		}
4134bac2208Snarayan 
4144bac2208Snarayan 		mutex_enter(&cpu_lock);
4154bac2208Snarayan 
4164bac2208Snarayan 		if (cpuid >= NCPU) {
4174bac2208Snarayan 			status = stopcpu_bycpuid(cpuid);
4184bac2208Snarayan 			if (status != 0 && status != ENOTSUP)
4194bac2208Snarayan 				cmn_err(CE_PANIC, "failed to stop cpu %lu (%d)",
4204bac2208Snarayan 				    cpuid, status);
4214bac2208Snarayan 		}
4224bac2208Snarayan 
4234bac2208Snarayan 		mutex_exit(&cpu_lock);
4244bac2208Snarayan 	}
4254bac2208Snarayan 
4264bac2208Snarayan done:
4274bac2208Snarayan 	kmem_free(listp, listsz);
428a3f75865Sjm 	(void) md_fini_handle(mdp);
4294bac2208Snarayan }
4304bac2208Snarayan 
4311ae08745Sheppo void
4321ae08745Sheppo cif_init(void)
4331ae08745Sheppo {
4341ae08745Sheppo 	void (*kmdb_cb)(void);
4351ae08745Sheppo 	uint64_t rtba;
4361ae08745Sheppo 	uint64_t rv;
4379853d9e8SJason Beloro 	size_t ntransroot;
4389853d9e8SJason Beloro 	struct translation *transroot;
4391ae08745Sheppo 
4401ae08745Sheppo 	/*
4411ae08745Sheppo 	 * Check if domaining is enabled. If not, do not
4421ae08745Sheppo 	 * initialize the kernel CIF handler.
4431ae08745Sheppo 	 */
44422e19ac1Sjm 	if (!domaining_enabled())
4451ae08745Sheppo 		return;
4461ae08745Sheppo 
4479853d9e8SJason Beloro 	transroot = read_prom_mappings(&ntransroot);
4489853d9e8SJason Beloro 
4491ae08745Sheppo 	/*
4501ae08745Sheppo 	 * Cache PROM data that is needed later, e.g. a shadow
4511ae08745Sheppo 	 * copy of the device tree, IO mappings, etc.
4521ae08745Sheppo 	 */
4531ae08745Sheppo 	cache_prom_data();
4541ae08745Sheppo 
4551ae08745Sheppo 	/*
4561ae08745Sheppo 	 * Prepare to take over the get/set of environmental variables.
4571ae08745Sheppo 	 */
4581ae08745Sheppo 	promif_prop_init();
4591ae08745Sheppo 
4601ae08745Sheppo 	/*
4611ae08745Sheppo 	 * Switch CIF handler to the kernel.
4621ae08745Sheppo 	 */
4631ae08745Sheppo 	prom_cif_handler = cif_handler;
4641ae08745Sheppo 
4651ae08745Sheppo 	promif_preprom();
4661ae08745Sheppo 	cif_handler = kern_cif_handler;
4671ae08745Sheppo 
4681ae08745Sheppo 	/*
4691ae08745Sheppo 	 * Take over rtba for the boot CPU. The rtba for
4701ae08745Sheppo 	 * all other CPUs are set as they enter the system.
4711ae08745Sheppo 	 */
4721ae08745Sheppo 	rtba = va_to_pa(&trap_table);
4731ae08745Sheppo 	if ((rv = hv_cpu_set_rtba(&rtba)) != H_EOK)
4741ae08745Sheppo 		panic("hv_cpu_set_rtba failed: %ld\n", rv);
4751ae08745Sheppo 
4761ae08745Sheppo 	promif_postprom();
4771ae08745Sheppo 
4781ae08745Sheppo 	/*
4791ae08745Sheppo 	 * If the system has been booted with kmdb we need kmdb to
4801ae08745Sheppo 	 * use the kernel cif handler instead of the PROM cif handler.
4811ae08745Sheppo 	 */
4821ae08745Sheppo 	if (boothowto & RB_KMDB) {
4831ae08745Sheppo 		kmdb_cb = (void (*)(void))modlookup("misc/kmdbmod",
4841ae08745Sheppo 		    "kctl_switch_promif");
4851ae08745Sheppo 		ASSERT(kmdb_cb != NULL);
4861ae08745Sheppo 		(*kmdb_cb)();
4871ae08745Sheppo 	}
4884bac2208Snarayan 
4894bac2208Snarayan 	cif_check_cpus();
4909853d9e8SJason Beloro 
4919853d9e8SJason Beloro 	if (transroot != NULL)
4929853d9e8SJason Beloro 		unmap_prom_mappings(transroot, ntransroot);
4931ae08745Sheppo }
4941ae08745Sheppo 
4951ae08745Sheppo static void
4961ae08745Sheppo cache_prom_data(void)
4971ae08745Sheppo {
4981ae08745Sheppo 	/* initialize copy of OBP device tree */
4991ae08745Sheppo 	promif_stree_init();
5001ae08745Sheppo 
5011ae08745Sheppo 	/* initialize io parameters */
5021ae08745Sheppo 	promif_io_init();
5031ae08745Sheppo }
5041ae08745Sheppo 
5051ae08745Sheppo #endif	/* _KMDB */
506