xref: /illumos-gate/usr/src/uts/i86pc/os/mp_implfuncs.c (revision fdcca78f421670bb8f04d68bc3f2762a4b5342be)
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
5ae115bc7Smrj  * Common Development and Distribution License (the "License").
6ae115bc7Smrj  * 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 /*
22cc7a88b5Smrj  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*fdcca78fSJoshua M. Clulow  * Copyright 2020 Oxide Computer Company
257c478bd9Sstevel@tonic-gate  */
26a3114836SGerry Liu #define	PSMI_1_7
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
297c478bd9Sstevel@tonic-gate #include <vm/hat.h>
307c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
317c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
327c478bd9Sstevel@tonic-gate #include <sys/psm.h>
337c478bd9Sstevel@tonic-gate #include <sys/psm_modctl.h>
347c478bd9Sstevel@tonic-gate #include <sys/smp_impldefs.h>
357c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
36*fdcca78fSJoshua M. Clulow #include <sys/prom_debug.h>
37843e1988Sjohnlev #if defined(__xpv)
38843e1988Sjohnlev #include <sys/hypervisor.h>
39843e1988Sjohnlev #include <vm/kboot_mmu.h>
40843e1988Sjohnlev #include <vm/hat_pte.h>
41843e1988Sjohnlev #endif
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  *	External reference functions
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate extern void *get_next_mach(void *, char *);
477c478bd9Sstevel@tonic-gate extern void close_mach_list(void);
487c478bd9Sstevel@tonic-gate extern void open_mach_list(void);
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * from startup.c - kernel VA range allocator for device mappings
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate extern void *device_arena_alloc(size_t size, int vm_flag);
547c478bd9Sstevel@tonic-gate extern void device_arena_free(void * vaddr, size_t size);
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate void psm_modloadonly(void);
577c478bd9Sstevel@tonic-gate void psm_install(void);
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * Local Function Prototypes
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate static struct modlinkage *psm_modlinkage_alloc(struct psm_info *infop);
637c478bd9Sstevel@tonic-gate static void psm_modlinkage_free(struct modlinkage *mlinkp);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static char *psm_get_impl_module(int first);
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static int mod_installpsm(struct modlpsm *modl, struct modlinkage *modlp);
687c478bd9Sstevel@tonic-gate static int mod_removepsm(struct modlpsm *modl, struct modlinkage *modlp);
697c478bd9Sstevel@tonic-gate static int mod_infopsm(struct modlpsm *modl, struct modlinkage *modlp, int *p0);
707c478bd9Sstevel@tonic-gate struct mod_ops mod_psmops = {
717c478bd9Sstevel@tonic-gate 	mod_installpsm, mod_removepsm, mod_infopsm
727c478bd9Sstevel@tonic-gate };
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static struct psm_sw psm_swtab = {
754da99751SToomas Soome 	&psm_swtab, &psm_swtab, NULL, 0
767c478bd9Sstevel@tonic-gate };
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate kmutex_t psmsw_lock;			/* lock accesses to psmsw	*/
797c478bd9Sstevel@tonic-gate struct psm_sw *psmsw = &psm_swtab;	/* start of all psm_sw		*/
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static struct modlinkage *
psm_modlinkage_alloc(struct psm_info * infop)827c478bd9Sstevel@tonic-gate psm_modlinkage_alloc(struct psm_info *infop)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	int	memsz;
857c478bd9Sstevel@tonic-gate 	struct modlinkage *mlinkp;
867c478bd9Sstevel@tonic-gate 	struct modlpsm *mlpsmp;
877c478bd9Sstevel@tonic-gate 	struct psm_sw *swp;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	memsz = sizeof (struct modlinkage) + sizeof (struct modlpsm) +
907c478bd9Sstevel@tonic-gate 	    sizeof (struct psm_sw);
917c478bd9Sstevel@tonic-gate 	mlinkp = (struct modlinkage *)kmem_zalloc(memsz, KM_NOSLEEP);
927c478bd9Sstevel@tonic-gate 	if (!mlinkp) {
937c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "!psm_mod_init: Cannot install %s",
947c478bd9Sstevel@tonic-gate 		    infop->p_mach_idstring);
957c478bd9Sstevel@tonic-gate 		return (NULL);
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 	mlpsmp = (struct modlpsm *)(mlinkp + 1);
987c478bd9Sstevel@tonic-gate 	swp = (struct psm_sw *)(mlpsmp + 1);
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	mlinkp->ml_rev = MODREV_1;
1017c478bd9Sstevel@tonic-gate 	mlinkp->ml_linkage[0] = (void *)mlpsmp;
1027c478bd9Sstevel@tonic-gate 	mlinkp->ml_linkage[1] = (void *)NULL;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	mlpsmp->psm_modops = &mod_psmops;
1057c478bd9Sstevel@tonic-gate 	mlpsmp->psm_linkinfo = infop->p_mach_desc;
1067c478bd9Sstevel@tonic-gate 	mlpsmp->psm_swp = swp;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	swp->psw_infop = infop;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	return (mlinkp);
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate static void
psm_modlinkage_free(struct modlinkage * mlinkp)1147c478bd9Sstevel@tonic-gate psm_modlinkage_free(struct modlinkage *mlinkp)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	if (!mlinkp)
1177c478bd9Sstevel@tonic-gate 		return;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	(void) kmem_free(mlinkp, (sizeof (struct modlinkage) +
1207c478bd9Sstevel@tonic-gate 	    sizeof (struct modlpsm) + sizeof (struct psm_sw)));
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate int
psm_mod_init(void ** handlepp,struct psm_info * infop)1247c478bd9Sstevel@tonic-gate psm_mod_init(void **handlepp, struct psm_info *infop)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	struct modlinkage **modlpp = (struct modlinkage **)handlepp;
1277c478bd9Sstevel@tonic-gate 	int	status;
1287c478bd9Sstevel@tonic-gate 	struct modlinkage *mlinkp;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	if (!*modlpp) {
1317c478bd9Sstevel@tonic-gate 		mlinkp = psm_modlinkage_alloc(infop);
1327c478bd9Sstevel@tonic-gate 		if (!mlinkp)
1337c478bd9Sstevel@tonic-gate 			return (ENOSPC);
1347c478bd9Sstevel@tonic-gate 	} else
1357c478bd9Sstevel@tonic-gate 		mlinkp = *modlpp;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	status = mod_install(mlinkp);
1387c478bd9Sstevel@tonic-gate 	if (status) {
1397c478bd9Sstevel@tonic-gate 		psm_modlinkage_free(mlinkp);
1407c478bd9Sstevel@tonic-gate 		*modlpp = NULL;
1417c478bd9Sstevel@tonic-gate 	} else
1427c478bd9Sstevel@tonic-gate 		*modlpp = mlinkp;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	return (status);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
1487c478bd9Sstevel@tonic-gate int
psm_mod_fini(void ** handlepp,struct psm_info * infop)1497c478bd9Sstevel@tonic-gate psm_mod_fini(void **handlepp, struct psm_info *infop)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	struct modlinkage **modlpp = (struct modlinkage **)handlepp;
1527c478bd9Sstevel@tonic-gate 	int	status;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	status = mod_remove(*modlpp);
1557c478bd9Sstevel@tonic-gate 	if (status == 0) {
1567c478bd9Sstevel@tonic-gate 		psm_modlinkage_free(*modlpp);
1577c478bd9Sstevel@tonic-gate 		*modlpp = NULL;
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 	return (status);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate int
psm_mod_info(void ** handlepp,struct psm_info * infop,struct modinfo * modinfop)1637c478bd9Sstevel@tonic-gate psm_mod_info(void **handlepp, struct psm_info *infop, struct modinfo *modinfop)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate 	struct modlinkage **modlpp = (struct modlinkage **)handlepp;
1667c478bd9Sstevel@tonic-gate 	int status;
1677c478bd9Sstevel@tonic-gate 	struct modlinkage *mlinkp;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	if (!*modlpp) {
1707c478bd9Sstevel@tonic-gate 		mlinkp = psm_modlinkage_alloc(infop);
1717c478bd9Sstevel@tonic-gate 		if (!mlinkp)
1724da99751SToomas Soome 			return (0);
1737c478bd9Sstevel@tonic-gate 	} else
1747c478bd9Sstevel@tonic-gate 		mlinkp = *modlpp;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	status =  mod_info(mlinkp, modinfop);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	if (!status) {
1797c478bd9Sstevel@tonic-gate 		psm_modlinkage_free(mlinkp);
1807c478bd9Sstevel@tonic-gate 		*modlpp = NULL;
1817c478bd9Sstevel@tonic-gate 	} else
1827c478bd9Sstevel@tonic-gate 		*modlpp = mlinkp;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	return (status);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate int
psm_add_intr(int lvl,avfunc xxintr,char * name,int vect,caddr_t arg)1887c478bd9Sstevel@tonic-gate psm_add_intr(int lvl, avfunc xxintr, char *name, int vect, caddr_t arg)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	return (add_avintr((void *)NULL, lvl, xxintr, name, vect,
1917a364d25Sschwartz 	    arg, NULL, NULL, NULL));
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate int
psm_add_nmintr(int lvl,avfunc xxintr,char * name,caddr_t arg)1957c478bd9Sstevel@tonic-gate psm_add_nmintr(int lvl, avfunc xxintr, char *name, caddr_t arg)
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate 	return (add_nmintr(lvl, xxintr, name, arg));
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate processorid_t
psm_get_cpu_id(void)2017c478bd9Sstevel@tonic-gate psm_get_cpu_id(void)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	return (CPU->cpu_id);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate caddr_t
psm_map_phys_new(paddr_t addr,size_t len,int prot)2077c478bd9Sstevel@tonic-gate psm_map_phys_new(paddr_t addr, size_t len, int prot)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	uint_t pgoffset;
2107c478bd9Sstevel@tonic-gate 	paddr_t base;
2117c478bd9Sstevel@tonic-gate 	pgcnt_t npages;
2127c478bd9Sstevel@tonic-gate 	caddr_t cvaddr;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if (len == 0)
2157c478bd9Sstevel@tonic-gate 		return (0);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	pgoffset = addr & MMU_PAGEOFFSET;
218843e1988Sjohnlev #ifdef __xpv
219843e1988Sjohnlev 	/*
220843e1988Sjohnlev 	 * If we're dom0, we're starting from a MA. translate that to a PA
221843e1988Sjohnlev 	 * XXPV - what about driver domains???
222843e1988Sjohnlev 	 */
223843e1988Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
224843e1988Sjohnlev 		base = pfn_to_pa(xen_assign_pfn(mmu_btop(addr))) |
225843e1988Sjohnlev 		    (addr & MMU_PAGEOFFSET);
226843e1988Sjohnlev 	} else {
227ae115bc7Smrj 		base = addr;
228843e1988Sjohnlev 	}
229843e1988Sjohnlev #else
230843e1988Sjohnlev 	base = addr;
231843e1988Sjohnlev #endif
2327c478bd9Sstevel@tonic-gate 	npages = mmu_btopr(len + pgoffset);
2337c478bd9Sstevel@tonic-gate 	cvaddr = device_arena_alloc(ptob(npages), VM_NOSLEEP);
2347c478bd9Sstevel@tonic-gate 	if (cvaddr == NULL)
2357c478bd9Sstevel@tonic-gate 		return (0);
2367c478bd9Sstevel@tonic-gate 	hat_devload(kas.a_hat, cvaddr, mmu_ptob(npages), mmu_btop(base),
2377c478bd9Sstevel@tonic-gate 	    prot, HAT_LOAD_LOCK);
2387c478bd9Sstevel@tonic-gate 	return (cvaddr + pgoffset);
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate void
psm_unmap_phys(caddr_t addr,size_t len)2427c478bd9Sstevel@tonic-gate psm_unmap_phys(caddr_t addr, size_t len)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate 	uint_t pgoffset;
2457c478bd9Sstevel@tonic-gate 	caddr_t base;
2467c478bd9Sstevel@tonic-gate 	pgcnt_t npages;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	if (len == 0)
2497c478bd9Sstevel@tonic-gate 		return;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	pgoffset = (uintptr_t)addr & MMU_PAGEOFFSET;
2527c478bd9Sstevel@tonic-gate 	base = addr - pgoffset;
2537c478bd9Sstevel@tonic-gate 	npages = mmu_btopr(len + pgoffset);
2547c478bd9Sstevel@tonic-gate 	hat_unload(kas.a_hat, base, ptob(npages), HAT_UNLOAD_UNLOCK);
2557c478bd9Sstevel@tonic-gate 	device_arena_free(base, ptob(npages));
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate caddr_t
psm_map_new(paddr_t addr,size_t len,int prot)2597c478bd9Sstevel@tonic-gate psm_map_new(paddr_t addr, size_t len, int prot)
2607c478bd9Sstevel@tonic-gate {
2617c478bd9Sstevel@tonic-gate 	int phys_prot = PROT_READ;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	ASSERT(prot == (prot & (PSM_PROT_WRITE | PSM_PROT_READ)));
2647c478bd9Sstevel@tonic-gate 	if (prot & PSM_PROT_WRITE)
2657c478bd9Sstevel@tonic-gate 		phys_prot |= PROT_WRITE;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	return (psm_map_phys(addr, len, phys_prot));
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate #undef psm_map_phys
2717c478bd9Sstevel@tonic-gate #undef psm_map
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate caddr_t
psm_map_phys(uint32_t addr,size_t len,int prot)2747c478bd9Sstevel@tonic-gate psm_map_phys(uint32_t addr, size_t len, int prot)
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate 	return (psm_map_phys_new((paddr_t)(addr & 0xffffffff), len, prot));
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate caddr_t
psm_map(uint32_t addr,size_t len,int prot)2807c478bd9Sstevel@tonic-gate psm_map(uint32_t addr, size_t len, int prot)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate 	return (psm_map_new((paddr_t)(addr & 0xffffffff), len, prot));
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate void
psm_unmap(caddr_t addr,size_t len)2867c478bd9Sstevel@tonic-gate psm_unmap(caddr_t addr, size_t len)
2877c478bd9Sstevel@tonic-gate {
2887c478bd9Sstevel@tonic-gate 	uint_t pgoffset;
2897c478bd9Sstevel@tonic-gate 	caddr_t base;
2907c478bd9Sstevel@tonic-gate 	pgcnt_t npages;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	if (len == 0)
2937c478bd9Sstevel@tonic-gate 		return;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	pgoffset = (uintptr_t)addr & MMU_PAGEOFFSET;
2967c478bd9Sstevel@tonic-gate 	base = addr - pgoffset;
2977c478bd9Sstevel@tonic-gate 	npages = mmu_btopr(len + pgoffset);
2987c478bd9Sstevel@tonic-gate 	hat_unload(kas.a_hat, base, ptob(npages), HAT_UNLOAD_UNLOCK);
2997c478bd9Sstevel@tonic-gate 	device_arena_free(base, ptob(npages));
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
3037c478bd9Sstevel@tonic-gate static int
mod_installpsm(struct modlpsm * modl,struct modlinkage * modlp)3047c478bd9Sstevel@tonic-gate mod_installpsm(struct modlpsm *modl, struct modlinkage *modlp)
3057c478bd9Sstevel@tonic-gate {
3067c478bd9Sstevel@tonic-gate 	struct psm_sw *swp;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	swp = modl->psm_swp;
3097c478bd9Sstevel@tonic-gate 	mutex_enter(&psmsw_lock);
3107c478bd9Sstevel@tonic-gate 	psmsw->psw_back->psw_forw = swp;
3117c478bd9Sstevel@tonic-gate 	swp->psw_back = psmsw->psw_back;
3127c478bd9Sstevel@tonic-gate 	swp->psw_forw = psmsw;
3137c478bd9Sstevel@tonic-gate 	psmsw->psw_back = swp;
3147c478bd9Sstevel@tonic-gate 	swp->psw_flag |= PSM_MOD_INSTALL;
3157c478bd9Sstevel@tonic-gate 	mutex_exit(&psmsw_lock);
3167c478bd9Sstevel@tonic-gate 	return (0);
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
3207c478bd9Sstevel@tonic-gate static int
mod_removepsm(struct modlpsm * modl,struct modlinkage * modlp)3217c478bd9Sstevel@tonic-gate mod_removepsm(struct modlpsm *modl, struct modlinkage *modlp)
3227c478bd9Sstevel@tonic-gate {
3237c478bd9Sstevel@tonic-gate 	struct psm_sw *swp;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	swp = modl->psm_swp;
3267c478bd9Sstevel@tonic-gate 	mutex_enter(&psmsw_lock);
3277c478bd9Sstevel@tonic-gate 	if (swp->psw_flag & PSM_MOD_IDENTIFY) {
3287c478bd9Sstevel@tonic-gate 		mutex_exit(&psmsw_lock);
3297c478bd9Sstevel@tonic-gate 		return (EBUSY);
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 	if (!(swp->psw_flag & PSM_MOD_INSTALL)) {
3327c478bd9Sstevel@tonic-gate 		mutex_exit(&psmsw_lock);
3337c478bd9Sstevel@tonic-gate 		return (0);
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	swp->psw_back->psw_forw = swp->psw_forw;
3377c478bd9Sstevel@tonic-gate 	swp->psw_forw->psw_back = swp->psw_back;
3387c478bd9Sstevel@tonic-gate 	mutex_exit(&psmsw_lock);
3397c478bd9Sstevel@tonic-gate 	return (0);
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
3437c478bd9Sstevel@tonic-gate static int
mod_infopsm(struct modlpsm * modl,struct modlinkage * modlp,int * p0)3447c478bd9Sstevel@tonic-gate mod_infopsm(struct modlpsm *modl, struct modlinkage *modlp, int *p0)
3457c478bd9Sstevel@tonic-gate {
3467c478bd9Sstevel@tonic-gate 	*p0 = (int)modl->psm_swp->psw_infop->p_owner;
3477c478bd9Sstevel@tonic-gate 	return (0);
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate 
350843e1988Sjohnlev #if defined(__xpv)
351cc7a88b5Smrj #define	DEFAULT_PSM_MODULE	"xpv_uppc"
352843e1988Sjohnlev #else
353ae115bc7Smrj #define	DEFAULT_PSM_MODULE	"uppc"
354843e1988Sjohnlev #endif
355ae115bc7Smrj 
3567c478bd9Sstevel@tonic-gate static char *
psm_get_impl_module(int first)3577c478bd9Sstevel@tonic-gate psm_get_impl_module(int first)
3587c478bd9Sstevel@tonic-gate {
3597c478bd9Sstevel@tonic-gate 	static char **pnamep;
3607c478bd9Sstevel@tonic-gate 	static char *psm_impl_module_list[] = {
361ae115bc7Smrj 		DEFAULT_PSM_MODULE,
3627c478bd9Sstevel@tonic-gate 		(char *)0
3637c478bd9Sstevel@tonic-gate 	};
3647c478bd9Sstevel@tonic-gate 	static void *mhdl = NULL;
3657c478bd9Sstevel@tonic-gate 	static char machname[MAXNAMELEN];
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	if (first)
3687c478bd9Sstevel@tonic-gate 		pnamep = psm_impl_module_list;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	if (*pnamep != (char *)0)
3717c478bd9Sstevel@tonic-gate 		return (*pnamep++);
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	mhdl = get_next_mach(mhdl, machname);
3747c478bd9Sstevel@tonic-gate 	if (mhdl)
3757c478bd9Sstevel@tonic-gate 		return (machname);
3767c478bd9Sstevel@tonic-gate 	return ((char *)0);
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate void
psm_modload(void)3807c478bd9Sstevel@tonic-gate psm_modload(void)
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate 	char *this;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	mutex_init(&psmsw_lock, NULL, MUTEX_DEFAULT, NULL);
3857c478bd9Sstevel@tonic-gate 	open_mach_list();
3867c478bd9Sstevel@tonic-gate 
387843e1988Sjohnlev 	for (this = psm_get_impl_module(1); this != (char *)NULL;
3887c478bd9Sstevel@tonic-gate 	    this = psm_get_impl_module(0)) {
3897c478bd9Sstevel@tonic-gate 		if (modload("mach", this) == -1)
3905f890c5eSMark Johnson 			cmn_err(CE_CONT, "!Skipping psm: %s\n", this);
3917c478bd9Sstevel@tonic-gate 	}
3927c478bd9Sstevel@tonic-gate 	close_mach_list();
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate void
psm_install(void)3967c478bd9Sstevel@tonic-gate psm_install(void)
3977c478bd9Sstevel@tonic-gate {
3987c478bd9Sstevel@tonic-gate 	struct psm_sw *swp, *cswp;
3997c478bd9Sstevel@tonic-gate 	struct psm_ops *opsp;
4007c478bd9Sstevel@tonic-gate 	char machstring[15];
401b9bc7f78Ssmaybe 	int err, psmcnt = 0;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	mutex_enter(&psmsw_lock);
4047c478bd9Sstevel@tonic-gate 	for (swp = psmsw->psw_forw; swp != psmsw; ) {
405*fdcca78fSJoshua M. Clulow 		PRM_DEBUGS(swp->psw_infop->p_mach_idstring);
4067c478bd9Sstevel@tonic-gate 		opsp = swp->psw_infop->p_ops;
4077c478bd9Sstevel@tonic-gate 		if (opsp->psm_probe) {
408*fdcca78fSJoshua M. Clulow 			PRM_POINT("psm_probe()");
4097c478bd9Sstevel@tonic-gate 			if ((*opsp->psm_probe)() == PSM_SUCCESS) {
410*fdcca78fSJoshua M. Clulow 				PRM_POINT("psm_probe() PSM_SUCCESS");
411b9bc7f78Ssmaybe 				psmcnt++;
4127c478bd9Sstevel@tonic-gate 				swp->psw_flag |= PSM_MOD_IDENTIFY;
4137c478bd9Sstevel@tonic-gate 				swp = swp->psw_forw;
4147c478bd9Sstevel@tonic-gate 				continue;
4157c478bd9Sstevel@tonic-gate 			}
416*fdcca78fSJoshua M. Clulow 			PRM_POINT("psm_probe() FAILURE");
4177c478bd9Sstevel@tonic-gate 		}
4187c478bd9Sstevel@tonic-gate 		/* remove the unsuccessful psm modules */
4197c478bd9Sstevel@tonic-gate 		cswp = swp;
4207c478bd9Sstevel@tonic-gate 		swp = swp->psw_forw;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 		mutex_exit(&psmsw_lock);
4237c478bd9Sstevel@tonic-gate 		(void) strcpy(&machstring[0], cswp->psw_infop->p_mach_idstring);
4247c478bd9Sstevel@tonic-gate 		err = mod_remove_by_name(cswp->psw_infop->p_mach_idstring);
4257c478bd9Sstevel@tonic-gate 		if (err)
426408a1f8eSVikram Hegde 			cmn_err(CE_WARN, "!%s: mod_remove_by_name failed %d",
4277c478bd9Sstevel@tonic-gate 			    &machstring[0], err);
4287c478bd9Sstevel@tonic-gate 		mutex_enter(&psmsw_lock);
4297c478bd9Sstevel@tonic-gate 	}
4307c478bd9Sstevel@tonic-gate 	mutex_exit(&psmsw_lock);
431b9bc7f78Ssmaybe 	if (psmcnt == 0)
432*fdcca78fSJoshua M. Clulow 		halt("the operating system does not yet support this hardware");
433*fdcca78fSJoshua M. Clulow 	PRM_POINT("psminitf()");
4347c478bd9Sstevel@tonic-gate 	(*psminitf)();
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate /*
4387c478bd9Sstevel@tonic-gate  * Return 1 if kernel debugger is present, and 0 if not.
4397c478bd9Sstevel@tonic-gate  */
4407c478bd9Sstevel@tonic-gate int
psm_debugger(void)4417c478bd9Sstevel@tonic-gate psm_debugger(void)
4427c478bd9Sstevel@tonic-gate {
4437c478bd9Sstevel@tonic-gate 	return ((boothowto & RB_DEBUG) != 0);
4447c478bd9Sstevel@tonic-gate }
445