xref: /illumos-gate/usr/src/uts/i86pc/os/ddi_impl.c (revision 9151f8f1317aea334cb8686b8543b1666a2824bd)
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
500d0963fSdilpreet  * Common Development and Distribution License (the "License").
600d0963fSdilpreet  * 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  */
217b93957cSeota 
227c478bd9Sstevel@tonic-gate /*
237417cfdeSKuriakose Kuruvilla  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
24a7175e20SGarrett D'Amore  * Copyright 2012 Garrett D'Amore <garrett@damore.org>
25a7175e20SGarrett D'Amore  * Copyright 2014 Pluribus Networks, Inc.
265f10ef69SYuri Pankov  * Copyright 2016 Nexenta Systems, Inc.
2704909c8cSJohn Levon  * Copyright 2018 Joyent, Inc.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * PC specific DDI implementation
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
357c478bd9Sstevel@tonic-gate #include <sys/avintr.h>
367c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
377c478bd9Sstevel@tonic-gate #include <sys/conf.h>
387c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
397c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
407c478bd9Sstevel@tonic-gate #include <sys/ddi_subrdefs.h>
417c478bd9Sstevel@tonic-gate #include <sys/ethernet.h>
427c478bd9Sstevel@tonic-gate #include <sys/fp.h>
437c478bd9Sstevel@tonic-gate #include <sys/instance.h>
447c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
457c478bd9Sstevel@tonic-gate #include <sys/machsystm.h>
467c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
477c478bd9Sstevel@tonic-gate #include <sys/promif.h>
487c478bd9Sstevel@tonic-gate #include <sys/prom_plat.h>
497c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
507c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
517c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
527c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
537c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
547c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
557c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
567c478bd9Sstevel@tonic-gate #include <sys/spl.h>
577c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
587c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
597c478bd9Sstevel@tonic-gate #include <sys/ontrap.h>
6000d0963fSdilpreet #include <sys/fm/protocol.h>
617c478bd9Sstevel@tonic-gate #include <sys/ramdisk.h>
627c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
637c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
647c478bd9Sstevel@tonic-gate #include <sys/pci_impl.h>
65843e1988Sjohnlev #if defined(__xpv)
66843e1988Sjohnlev #include <sys/hypervisor.h>
67843e1988Sjohnlev #endif
687a364d25Sschwartz #include <sys/mach_intr.h>
697b93957cSeota #include <vm/hat_i86.h>
707b93957cSeota #include <sys/x86_archext.h>
71a7175e20SGarrett D'Amore #include <sys/avl.h>
72cbc8e155SToomas Soome #include <sys/font.h>
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * DDI Boot Configuration
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
792df1fe9cSrandyf  * Platform drivers on this platform
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate char *platform_module_list[] = {
822df1fe9cSrandyf 	"acpippm",
835cff7825Smh27603 	"ppm",
847c478bd9Sstevel@tonic-gate 	(char *)0
857c478bd9Sstevel@tonic-gate };
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /* pci bus resource maps */
887c478bd9Sstevel@tonic-gate struct pci_bus_resource *pci_bus_res;
897c478bd9Sstevel@tonic-gate 
9036945f79Smrj size_t dma_max_copybuf_size = 0x101000;		/* 1M + 4K */
9136945f79Smrj 
927c478bd9Sstevel@tonic-gate uint64_t ramdisk_start, ramdisk_end;
937c478bd9Sstevel@tonic-gate 
9478323854SJudy Chen int pseudo_isa = 0;
9578323854SJudy Chen 
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate  * Forward declarations
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate static int getlongprop_buf();
1007c478bd9Sstevel@tonic-gate static void get_boot_properties(void);
1017c478bd9Sstevel@tonic-gate static void impl_bus_initialprobe(void);
1027c478bd9Sstevel@tonic-gate static void impl_bus_reprobe(void);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static int poke_mem(peekpoke_ctlops_t *in_args);
1057c478bd9Sstevel@tonic-gate static int peek_mem(peekpoke_ctlops_t *in_args);
1067c478bd9Sstevel@tonic-gate 
1077b93957cSeota static int kmem_override_cache_attrs(caddr_t, size_t, uint_t);
1087b93957cSeota 
10986ef0a63SRichard Lowe #if !defined(__xpv)
1103a634bfcSVikram Hegde extern void immu_init(void);
1113a634bfcSVikram Hegde #endif
1123a634bfcSVikram Hegde 
113a7175e20SGarrett D'Amore /*
114a7175e20SGarrett D'Amore  * We use an AVL tree to store contiguous address allocations made with the
115a7175e20SGarrett D'Amore  * kalloca() routine, so that we can return the size to free with kfreea().
116a7175e20SGarrett D'Amore  * Note that in the future it would be vastly faster if we could eliminate
117a7175e20SGarrett D'Amore  * this lookup by insisting that all callers keep track of their own sizes,
118a7175e20SGarrett D'Amore  * just as for kmem_alloc().
119a7175e20SGarrett D'Amore  */
120a7175e20SGarrett D'Amore struct ctgas {
121a7175e20SGarrett D'Amore 	avl_node_t ctg_link;
122a7175e20SGarrett D'Amore 	void *ctg_addr;
123a7175e20SGarrett D'Amore 	size_t ctg_size;
124a7175e20SGarrett D'Amore };
1257c478bd9Sstevel@tonic-gate 
126a7175e20SGarrett D'Amore static avl_tree_t ctgtree;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate static kmutex_t		ctgmutex;
1297c478bd9Sstevel@tonic-gate #define	CTGLOCK()	mutex_enter(&ctgmutex)
1307c478bd9Sstevel@tonic-gate #define	CTGUNLOCK()	mutex_exit(&ctgmutex)
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /*
1337c478bd9Sstevel@tonic-gate  * Minimum pfn value of page_t's put on the free list.  This is to simplify
1347c478bd9Sstevel@tonic-gate  * support of ddi dma memory requests which specify small, non-zero addr_lo
1357c478bd9Sstevel@tonic-gate  * values.
1367c478bd9Sstevel@tonic-gate  *
1377c478bd9Sstevel@tonic-gate  * The default value of 2, which corresponds to the only known non-zero addr_lo
1387c478bd9Sstevel@tonic-gate  * value used, means a single page will be sacrificed (pfn typically starts
1397c478bd9Sstevel@tonic-gate  * at 1).  ddiphysmin can be set to 0 to disable. It cannot be set above 0x100
1407c478bd9Sstevel@tonic-gate  * otherwise mp startup panics.
1417c478bd9Sstevel@tonic-gate  */
1427c478bd9Sstevel@tonic-gate pfn_t	ddiphysmin = 2;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate static void
check_driver_disable(void)1457c478bd9Sstevel@tonic-gate check_driver_disable(void)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	int proplen = 128;
1487c478bd9Sstevel@tonic-gate 	char *prop_name;
1497c478bd9Sstevel@tonic-gate 	char *drv_name, *propval;
1507c478bd9Sstevel@tonic-gate 	major_t major;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	prop_name = kmem_alloc(proplen, KM_SLEEP);
1537c478bd9Sstevel@tonic-gate 	for (major = 0; major < devcnt; major++) {
1547c478bd9Sstevel@tonic-gate 		drv_name = ddi_major_to_name(major);
1557c478bd9Sstevel@tonic-gate 		if (drv_name == NULL)
1567c478bd9Sstevel@tonic-gate 			continue;
1577c478bd9Sstevel@tonic-gate 		(void) snprintf(prop_name, proplen, "disable-%s", drv_name);
1587c478bd9Sstevel@tonic-gate 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
1597c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, prop_name, &propval) == DDI_SUCCESS) {
1607c478bd9Sstevel@tonic-gate 			if (strcmp(propval, "true") == 0) {
1617c478bd9Sstevel@tonic-gate 				devnamesp[major].dn_flags |= DN_DRIVER_REMOVED;
1627c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE, "driver %s disabled",
1637c478bd9Sstevel@tonic-gate 				    drv_name);
1647c478bd9Sstevel@tonic-gate 			}
1657c478bd9Sstevel@tonic-gate 			ddi_prop_free(propval);
1667c478bd9Sstevel@tonic-gate 		}
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 	kmem_free(prop_name, proplen);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate  * Configure the hardware on the system.
1747c478bd9Sstevel@tonic-gate  * Called before the rootfs is mounted
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate void
configure(void)1777c478bd9Sstevel@tonic-gate configure(void)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate 	extern void i_ddi_init_root();
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	extern int fpu_ignored;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/*
1847c478bd9Sstevel@tonic-gate 	 * Determine if an FPU is attached
1857c478bd9Sstevel@tonic-gate 	 */
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	fpu_probe();
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	if (fpu_ignored) {
1917c478bd9Sstevel@tonic-gate 		printf("FP hardware will not be used\n");
1927c478bd9Sstevel@tonic-gate 	} else if (!fpu_exists) {
1937c478bd9Sstevel@tonic-gate 		printf("No FPU in configuration\n");
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	/*
1977c478bd9Sstevel@tonic-gate 	 * Initialize devices on the machine.
1987c478bd9Sstevel@tonic-gate 	 * Uses configuration tree built by the PROMs to determine what
1997c478bd9Sstevel@tonic-gate 	 * is present, and builds a tree of prototype dev_info nodes
2007c478bd9Sstevel@tonic-gate 	 * corresponding to the hardware which identified itself.
2017c478bd9Sstevel@tonic-gate 	 */
202db9ce1c9SJerry Gilliam 
2037c478bd9Sstevel@tonic-gate 	/*
204288e2932SJerry Gilliam 	 * Initialize root node.
2057c478bd9Sstevel@tonic-gate 	 */
2067c478bd9Sstevel@tonic-gate 	i_ddi_init_root();
2077c478bd9Sstevel@tonic-gate 
20837d6e245Slipeng sang - Sun Microsystems - Beijing China 	/* reprogram devices not set up by firmware (BIOS) */
20937d6e245Slipeng sang - Sun Microsystems - Beijing China 	impl_bus_reprobe();
21037d6e245Slipeng sang - Sun Microsystems - Beijing China 
21186ef0a63SRichard Lowe #if !defined(__xpv)
2123a634bfcSVikram Hegde 	/*
2133a634bfcSVikram Hegde 	 * Setup but don't startup the IOMMU
2143a634bfcSVikram Hegde 	 * Startup happens later via a direct call
2153a634bfcSVikram Hegde 	 * to IOMMU code by boot code.
2163a634bfcSVikram Hegde 	 * At this point, all PCI bus renumbering
2173a634bfcSVikram Hegde 	 * is done, so safe to init the IMMU
2183a634bfcSVikram Hegde 	 * AKA Intel IOMMU.
2193a634bfcSVikram Hegde 	 */
2203a634bfcSVikram Hegde 	immu_init();
2213a634bfcSVikram Hegde #endif
2223a634bfcSVikram Hegde 
2237c478bd9Sstevel@tonic-gate 	/*
2247c478bd9Sstevel@tonic-gate 	 * attach the isa nexus to get ACPI resource usage
2257c478bd9Sstevel@tonic-gate 	 * isa is "kind of" a pseudo node
2267c478bd9Sstevel@tonic-gate 	 */
227843e1988Sjohnlev #if defined(__xpv)
22878323854SJudy Chen 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
22978323854SJudy Chen 		if (pseudo_isa)
2307c478bd9Sstevel@tonic-gate 			(void) i_ddi_attach_pseudo_node("isa");
23178323854SJudy Chen 		else
23278323854SJudy Chen 			(void) i_ddi_attach_hw_nodes("isa");
23378323854SJudy Chen 	}
234843e1988Sjohnlev #else
23578323854SJudy Chen 	if (pseudo_isa)
236843e1988Sjohnlev 		(void) i_ddi_attach_pseudo_node("isa");
23778323854SJudy Chen 	else
23878323854SJudy Chen 		(void) i_ddi_attach_hw_nodes("isa");
239843e1988Sjohnlev #endif
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate /*
2437c478bd9Sstevel@tonic-gate  * The "status" property indicates the operational status of a device.
2447c478bd9Sstevel@tonic-gate  * If this property is present, the value is a string indicating the
2457c478bd9Sstevel@tonic-gate  * status of the device as follows:
2467c478bd9Sstevel@tonic-gate  *
2477c478bd9Sstevel@tonic-gate  *	"okay"		operational.
2487c478bd9Sstevel@tonic-gate  *	"disabled"	not operational, but might become operational.
2497c478bd9Sstevel@tonic-gate  *	"fail"		not operational because a fault has been detected,
2507c478bd9Sstevel@tonic-gate  *			and it is unlikely that the device will become
2517c478bd9Sstevel@tonic-gate  *			operational without repair. no additional details
2527c478bd9Sstevel@tonic-gate  *			are available.
2537c478bd9Sstevel@tonic-gate  *	"fail-xxx"	not operational because a fault has been detected,
2547c478bd9Sstevel@tonic-gate  *			and it is unlikely that the device will become
2557c478bd9Sstevel@tonic-gate  *			operational without repair. "xxx" is additional
2567c478bd9Sstevel@tonic-gate  *			human-readable information about the particular
2577c478bd9Sstevel@tonic-gate  *			fault condition that was detected.
2587c478bd9Sstevel@tonic-gate  *
2597c478bd9Sstevel@tonic-gate  * The absence of this property means that the operational status is
2607c478bd9Sstevel@tonic-gate  * unknown or okay.
2617c478bd9Sstevel@tonic-gate  *
2627c478bd9Sstevel@tonic-gate  * This routine checks the status property of the specified device node
2637c478bd9Sstevel@tonic-gate  * and returns 0 if the operational status indicates failure, and 1 otherwise.
2647c478bd9Sstevel@tonic-gate  *
2657c478bd9Sstevel@tonic-gate  * The property may exist on plug-in cards the existed before IEEE 1275-1994.
2667c478bd9Sstevel@tonic-gate  * And, in that case, the property may not even be a string. So we carefully
2677c478bd9Sstevel@tonic-gate  * check for the value "fail", in the beginning of the string, noting
2687c478bd9Sstevel@tonic-gate  * the property length.
2697c478bd9Sstevel@tonic-gate  */
2707c478bd9Sstevel@tonic-gate int
status_okay(int id,char * buf,int buflen)2717c478bd9Sstevel@tonic-gate status_okay(int id, char *buf, int buflen)
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate 	char status_buf[OBP_MAXPROPNAME];
2747c478bd9Sstevel@tonic-gate 	char *bufp = buf;
2757c478bd9Sstevel@tonic-gate 	int len = buflen;
2767c478bd9Sstevel@tonic-gate 	int proplen;
2777c478bd9Sstevel@tonic-gate 	static const char *status = "status";
2787c478bd9Sstevel@tonic-gate 	static const char *fail = "fail";
2797c478bd9Sstevel@tonic-gate 	int fail_len = (int)strlen(fail);
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	/*
2827c478bd9Sstevel@tonic-gate 	 * Get the proplen ... if it's smaller than "fail",
2837c478bd9Sstevel@tonic-gate 	 * or doesn't exist ... then we don't care, since
2847c478bd9Sstevel@tonic-gate 	 * the value can't begin with the char string "fail".
2857c478bd9Sstevel@tonic-gate 	 *
2867c478bd9Sstevel@tonic-gate 	 * NB: proplen, if it's a string, includes the NULL in the
2877c478bd9Sstevel@tonic-gate 	 * the size of the property, and fail_len does not.
2887c478bd9Sstevel@tonic-gate 	 */
289fa9e4066Sahrens 	proplen = prom_getproplen((pnode_t)id, (caddr_t)status);
2907c478bd9Sstevel@tonic-gate 	if (proplen <= fail_len)	/* nonexistant or uninteresting len */
2917c478bd9Sstevel@tonic-gate 		return (1);
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	/*
2947c478bd9Sstevel@tonic-gate 	 * if a buffer was provided, use it
2957c478bd9Sstevel@tonic-gate 	 */
2967c478bd9Sstevel@tonic-gate 	if ((buf == (char *)NULL) || (buflen <= 0)) {
2977c478bd9Sstevel@tonic-gate 		bufp = status_buf;
2987c478bd9Sstevel@tonic-gate 		len = sizeof (status_buf);
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 	*bufp = (char)0;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	/*
3037c478bd9Sstevel@tonic-gate 	 * Get the property into the buffer, to the extent of the buffer,
3047c478bd9Sstevel@tonic-gate 	 * and in case the buffer is smaller than the property size,
3057c478bd9Sstevel@tonic-gate 	 * NULL terminate the buffer. (This handles the case where
3067c478bd9Sstevel@tonic-gate 	 * a buffer was passed in and the caller wants to print the
3077c478bd9Sstevel@tonic-gate 	 * value, but the buffer was too small).
3087c478bd9Sstevel@tonic-gate 	 */
309fa9e4066Sahrens 	(void) prom_bounded_getprop((pnode_t)id, (caddr_t)status,
3107c478bd9Sstevel@tonic-gate 	    (caddr_t)bufp, len);
3117c478bd9Sstevel@tonic-gate 	*(bufp + len - 1) = (char)0;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	/*
3147c478bd9Sstevel@tonic-gate 	 * If the value begins with the char string "fail",
3157c478bd9Sstevel@tonic-gate 	 * then it means the node is failed. We don't care
3167c478bd9Sstevel@tonic-gate 	 * about any other values. We assume the node is ok
3177c478bd9Sstevel@tonic-gate 	 * although it might be 'disabled'.
3187c478bd9Sstevel@tonic-gate 	 */
3197c478bd9Sstevel@tonic-gate 	if (strncmp(bufp, fail, fail_len) == 0)
3207c478bd9Sstevel@tonic-gate 		return (0);
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	return (1);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate /*
3267c478bd9Sstevel@tonic-gate  * Check the status of the device node passed as an argument.
3277c478bd9Sstevel@tonic-gate  *
3287c478bd9Sstevel@tonic-gate  *	if ((status is OKAY) || (status is DISABLED))
3297c478bd9Sstevel@tonic-gate  *		return DDI_SUCCESS
3307c478bd9Sstevel@tonic-gate  *	else
3317c478bd9Sstevel@tonic-gate  *		print a warning and return DDI_FAILURE
3327c478bd9Sstevel@tonic-gate  */
3337c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
3347c478bd9Sstevel@tonic-gate int
check_status(int id,char * name,dev_info_t * parent)3357c478bd9Sstevel@tonic-gate check_status(int id, char *name, dev_info_t *parent)
3367c478bd9Sstevel@tonic-gate {
3377c478bd9Sstevel@tonic-gate 	char status_buf[64];
3387c478bd9Sstevel@tonic-gate 	char devtype_buf[OBP_MAXPROPNAME];
3397c478bd9Sstevel@tonic-gate 	int retval = DDI_FAILURE;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	/*
3427c478bd9Sstevel@tonic-gate 	 * is the status okay?
3437c478bd9Sstevel@tonic-gate 	 */
3447c478bd9Sstevel@tonic-gate 	if (status_okay(id, status_buf, sizeof (status_buf)))
3457c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	/*
3487c478bd9Sstevel@tonic-gate 	 * a status property indicating bad memory will be associated
3497c478bd9Sstevel@tonic-gate 	 * with a node which has a "device_type" property with a value of
3507c478bd9Sstevel@tonic-gate 	 * "memory-controller". in this situation, return DDI_SUCCESS
3517c478bd9Sstevel@tonic-gate 	 */
3527c478bd9Sstevel@tonic-gate 	if (getlongprop_buf(id, OBP_DEVICETYPE, devtype_buf,
3537c478bd9Sstevel@tonic-gate 	    sizeof (devtype_buf)) > 0) {
3547c478bd9Sstevel@tonic-gate 		if (strcmp(devtype_buf, "memory-controller") == 0)
3557c478bd9Sstevel@tonic-gate 			retval = DDI_SUCCESS;
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	/*
3597c478bd9Sstevel@tonic-gate 	 * print the status property information
3607c478bd9Sstevel@tonic-gate 	 */
3617c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "status '%s' for '%s'", status_buf, name);
3627c478bd9Sstevel@tonic-gate 	return (retval);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3667c478bd9Sstevel@tonic-gate uint_t
softlevel1(caddr_t arg1,caddr_t arg2)3677c478bd9Sstevel@tonic-gate softlevel1(caddr_t arg1, caddr_t arg2)
3687c478bd9Sstevel@tonic-gate {
3697c478bd9Sstevel@tonic-gate 	softint();
3707c478bd9Sstevel@tonic-gate 	return (1);
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate /*
3747c478bd9Sstevel@tonic-gate  * Allow for implementation specific correction of PROM property values.
3757c478bd9Sstevel@tonic-gate  */
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3787c478bd9Sstevel@tonic-gate void
impl_fix_props(dev_info_t * dip,dev_info_t * ch_dip,char * name,int len,caddr_t buffer)3797c478bd9Sstevel@tonic-gate impl_fix_props(dev_info_t *dip, dev_info_t *ch_dip, char *name, int len,
3807c478bd9Sstevel@tonic-gate     caddr_t buffer)
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate 	/*
3837c478bd9Sstevel@tonic-gate 	 * There are no adjustments needed in this implementation.
3847c478bd9Sstevel@tonic-gate 	 */
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate static int
getlongprop_buf(int id,char * name,char * buf,int maxlen)3887c478bd9Sstevel@tonic-gate getlongprop_buf(int id, char *name, char *buf, int maxlen)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	int size;
3917c478bd9Sstevel@tonic-gate 
392fa9e4066Sahrens 	size = prom_getproplen((pnode_t)id, name);
3937c478bd9Sstevel@tonic-gate 	if (size <= 0 || (size > maxlen - 1))
3947c478bd9Sstevel@tonic-gate 		return (-1);
3957c478bd9Sstevel@tonic-gate 
396fa9e4066Sahrens 	if (-1 == prom_getprop((pnode_t)id, name, buf))
3977c478bd9Sstevel@tonic-gate 		return (-1);
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	if (strcmp("name", name) == 0) {
4007c478bd9Sstevel@tonic-gate 		if (buf[size - 1] != '\0') {
4017c478bd9Sstevel@tonic-gate 			buf[size] = '\0';
4027c478bd9Sstevel@tonic-gate 			size += 1;
4037c478bd9Sstevel@tonic-gate 		}
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	return (size);
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate static int
get_prop_int_array(dev_info_t * di,char * pname,int ** pval,uint_t * plen)4107c478bd9Sstevel@tonic-gate get_prop_int_array(dev_info_t *di, char *pname, int **pval, uint_t *plen)
4117c478bd9Sstevel@tonic-gate {
4127c478bd9Sstevel@tonic-gate 	int ret;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	if ((ret = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, di,
4157c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, pname, pval, plen))
4167c478bd9Sstevel@tonic-gate 	    == DDI_PROP_SUCCESS) {
4177c478bd9Sstevel@tonic-gate 		*plen = (*plen) * (sizeof (int));
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 	return (ret);
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate /*
4247c478bd9Sstevel@tonic-gate  * Node Configuration
4257c478bd9Sstevel@tonic-gate  */
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate struct prop_ispec {
4287c478bd9Sstevel@tonic-gate 	uint_t	pri, vec;
4297c478bd9Sstevel@tonic-gate };
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate /*
432a195726fSgovinda  * For the x86, we're prepared to claim that the interrupt string
433a195726fSgovinda  * is in the form of a list of <ipl,vec> specifications.
434a195726fSgovinda  */
435a195726fSgovinda 
436a195726fSgovinda #define	VEC_MIN	1
437a195726fSgovinda #define	VEC_MAX	255
438a195726fSgovinda 
439a195726fSgovinda static int
impl_xlate_intrs(dev_info_t * child,int * in,struct ddi_parent_private_data * pdptr)440a195726fSgovinda impl_xlate_intrs(dev_info_t *child, int *in,
441a195726fSgovinda     struct ddi_parent_private_data *pdptr)
442a195726fSgovinda {
443a195726fSgovinda 	size_t size;
444a195726fSgovinda 	int n;
445a195726fSgovinda 	struct intrspec *new;
446a195726fSgovinda 	caddr_t got_prop;
447a195726fSgovinda 	int *inpri;
448a195726fSgovinda 	int got_len;
449a195726fSgovinda 	extern int ignore_hardware_nodes;	/* force flag from ddi_impl.c */
450a195726fSgovinda 
451a195726fSgovinda 	static char bad_intr_fmt[] =
452a195726fSgovinda 	    "bad interrupt spec from %s%d - ipl %d, irq %d\n";
453a195726fSgovinda 
454a195726fSgovinda 	/*
455a195726fSgovinda 	 * determine if the driver is expecting the new style "interrupts"
456a195726fSgovinda 	 * property which just contains the IRQ, or the old style which
457a195726fSgovinda 	 * contains pairs of <IPL,IRQ>.  if it is the new style, we always
458a195726fSgovinda 	 * assign IPL 5 unless an "interrupt-priorities" property exists.
459a195726fSgovinda 	 * in that case, the "interrupt-priorities" property contains the
460a195726fSgovinda 	 * IPL values that match, one for one, the IRQ values in the
461a195726fSgovinda 	 * "interrupts" property.
462a195726fSgovinda 	 */
463a195726fSgovinda 	inpri = NULL;
464a195726fSgovinda 	if ((ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
465a195726fSgovinda 	    "ignore-hardware-nodes", -1) != -1) || ignore_hardware_nodes) {
466a195726fSgovinda 		/* the old style "interrupts" property... */
467a195726fSgovinda 
468a195726fSgovinda 		/*
469a195726fSgovinda 		 * The list consists of <ipl,vec> elements
470a195726fSgovinda 		 */
471a195726fSgovinda 		if ((n = (*in++ >> 1)) < 1)
472a195726fSgovinda 			return (DDI_FAILURE);
473a195726fSgovinda 
474a195726fSgovinda 		pdptr->par_nintr = n;
475a195726fSgovinda 		size = n * sizeof (struct intrspec);
476a195726fSgovinda 		new = pdptr->par_intr = kmem_zalloc(size, KM_SLEEP);
477a195726fSgovinda 
478a195726fSgovinda 		while (n--) {
479a195726fSgovinda 			int level = *in++;
480a195726fSgovinda 			int vec = *in++;
481a195726fSgovinda 
482a195726fSgovinda 			if (level < 1 || level > MAXIPL ||
483a195726fSgovinda 			    vec < VEC_MIN || vec > VEC_MAX) {
484a195726fSgovinda 				cmn_err(CE_CONT, bad_intr_fmt,
485a195726fSgovinda 				    DEVI(child)->devi_name,
486a195726fSgovinda 				    DEVI(child)->devi_instance, level, vec);
487a195726fSgovinda 				goto broken;
488a195726fSgovinda 			}
489a195726fSgovinda 			new->intrspec_pri = level;
490a195726fSgovinda 			if (vec != 2)
491a195726fSgovinda 				new->intrspec_vec = vec;
492a195726fSgovinda 			else
493a195726fSgovinda 				/*
494a195726fSgovinda 				 * irq 2 on the PC bus is tied to irq 9
495a195726fSgovinda 				 * on ISA, EISA and MicroChannel
496a195726fSgovinda 				 */
497a195726fSgovinda 				new->intrspec_vec = 9;
498a195726fSgovinda 			new++;
499a195726fSgovinda 		}
500a195726fSgovinda 
501a195726fSgovinda 		return (DDI_SUCCESS);
502a195726fSgovinda 	} else {
503a195726fSgovinda 		/* the new style "interrupts" property... */
504a195726fSgovinda 
505a195726fSgovinda 		/*
506a195726fSgovinda 		 * The list consists of <vec> elements
507a195726fSgovinda 		 */
508a195726fSgovinda 		if ((n = (*in++)) < 1)
509a195726fSgovinda 			return (DDI_FAILURE);
510a195726fSgovinda 
511a195726fSgovinda 		pdptr->par_nintr = n;
512a195726fSgovinda 		size = n * sizeof (struct intrspec);
513a195726fSgovinda 		new = pdptr->par_intr = kmem_zalloc(size, KM_SLEEP);
514a195726fSgovinda 
515a195726fSgovinda 		/* XXX check for "interrupt-priorities" property... */
516a195726fSgovinda 		if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
517a195726fSgovinda 		    "interrupt-priorities", (caddr_t)&got_prop, &got_len)
518a195726fSgovinda 		    == DDI_PROP_SUCCESS) {
519a195726fSgovinda 			if (n != (got_len / sizeof (int))) {
520a195726fSgovinda 				cmn_err(CE_CONT,
521a195726fSgovinda 				    "bad interrupt-priorities length"
522a195726fSgovinda 				    " from %s%d: expected %d, got %d\n",
523a195726fSgovinda 				    DEVI(child)->devi_name,
524a195726fSgovinda 				    DEVI(child)->devi_instance, n,
525a195726fSgovinda 				    (int)(got_len / sizeof (int)));
526a195726fSgovinda 				goto broken;
527a195726fSgovinda 			}
528a195726fSgovinda 			inpri = (int *)got_prop;
529a195726fSgovinda 		}
530a195726fSgovinda 
531a195726fSgovinda 		while (n--) {
532a195726fSgovinda 			int level;
533a195726fSgovinda 			int vec = *in++;
534a195726fSgovinda 
535a195726fSgovinda 			if (inpri == NULL)
536a195726fSgovinda 				level = 5;
537a195726fSgovinda 			else
538a195726fSgovinda 				level = *inpri++;
539a195726fSgovinda 
540a195726fSgovinda 			if (level < 1 || level > MAXIPL ||
541a195726fSgovinda 			    vec < VEC_MIN || vec > VEC_MAX) {
542a195726fSgovinda 				cmn_err(CE_CONT, bad_intr_fmt,
543a195726fSgovinda 				    DEVI(child)->devi_name,
544a195726fSgovinda 				    DEVI(child)->devi_instance, level, vec);
545a195726fSgovinda 				goto broken;
546a195726fSgovinda 			}
547a195726fSgovinda 			new->intrspec_pri = level;
548a195726fSgovinda 			if (vec != 2)
549a195726fSgovinda 				new->intrspec_vec = vec;
550a195726fSgovinda 			else
551a195726fSgovinda 				/*
552a195726fSgovinda 				 * irq 2 on the PC bus is tied to irq 9
553a195726fSgovinda 				 * on ISA, EISA and MicroChannel
554a195726fSgovinda 				 */
555a195726fSgovinda 				new->intrspec_vec = 9;
556a195726fSgovinda 			new++;
557a195726fSgovinda 		}
558a195726fSgovinda 
559a195726fSgovinda 		if (inpri != NULL)
560a195726fSgovinda 			kmem_free(got_prop, got_len);
561a195726fSgovinda 		return (DDI_SUCCESS);
562a195726fSgovinda 	}
563a195726fSgovinda 
564a195726fSgovinda broken:
565a195726fSgovinda 	kmem_free(pdptr->par_intr, size);
566a195726fSgovinda 	pdptr->par_intr = NULL;
567a195726fSgovinda 	pdptr->par_nintr = 0;
568a195726fSgovinda 	if (inpri != NULL)
569a195726fSgovinda 		kmem_free(got_prop, got_len);
570a195726fSgovinda 
571a195726fSgovinda 	return (DDI_FAILURE);
572a195726fSgovinda }
573a195726fSgovinda 
574a195726fSgovinda /*
5757c478bd9Sstevel@tonic-gate  * Create a ddi_parent_private_data structure from the ddi properties of
5767c478bd9Sstevel@tonic-gate  * the dev_info node.
5777c478bd9Sstevel@tonic-gate  *
5787c478bd9Sstevel@tonic-gate  * The "reg" and either an "intr" or "interrupts" properties are required
5797c478bd9Sstevel@tonic-gate  * if the driver wishes to create mappings or field interrupts on behalf
5807c478bd9Sstevel@tonic-gate  * of the device.
5817c478bd9Sstevel@tonic-gate  *
5827c478bd9Sstevel@tonic-gate  * The "reg" property is assumed to be a list of at least one triple
5837c478bd9Sstevel@tonic-gate  *
5847c478bd9Sstevel@tonic-gate  *	<bustype, address, size>*1
5857c478bd9Sstevel@tonic-gate  *
5867c478bd9Sstevel@tonic-gate  * The "intr" property is assumed to be a list of at least one duple
5877c478bd9Sstevel@tonic-gate  *
5887c478bd9Sstevel@tonic-gate  *	<SPARC ipl, vector#>*1
5897c478bd9Sstevel@tonic-gate  *
5907c478bd9Sstevel@tonic-gate  * The "interrupts" property is assumed to be a list of at least one
5917c478bd9Sstevel@tonic-gate  * n-tuples that describes the interrupt capabilities of the bus the device
5927c478bd9Sstevel@tonic-gate  * is connected to.  For SBus, this looks like
5937c478bd9Sstevel@tonic-gate  *
5947c478bd9Sstevel@tonic-gate  *	<SBus-level>*1
5957c478bd9Sstevel@tonic-gate  *
5967c478bd9Sstevel@tonic-gate  * (This property obsoletes the 'intr' property).
5977c478bd9Sstevel@tonic-gate  *
5987c478bd9Sstevel@tonic-gate  * The "ranges" property is optional.
5997c478bd9Sstevel@tonic-gate  */
6007c478bd9Sstevel@tonic-gate void
make_ddi_ppd(dev_info_t * child,struct ddi_parent_private_data ** ppd)6017c478bd9Sstevel@tonic-gate make_ddi_ppd(dev_info_t *child, struct ddi_parent_private_data **ppd)
6027c478bd9Sstevel@tonic-gate {
6037c478bd9Sstevel@tonic-gate 	struct ddi_parent_private_data *pdptr;
6047c478bd9Sstevel@tonic-gate 	int n;
6057c478bd9Sstevel@tonic-gate 	int *reg_prop, *rng_prop, *intr_prop, *irupts_prop;
6067c478bd9Sstevel@tonic-gate 	uint_t reg_len, rng_len, intr_len, irupts_len;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	*ppd = pdptr = kmem_zalloc(sizeof (*pdptr), KM_SLEEP);
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	/*
6117c478bd9Sstevel@tonic-gate 	 * Handle the 'reg' property.
6127c478bd9Sstevel@tonic-gate 	 */
6137c478bd9Sstevel@tonic-gate 	if ((get_prop_int_array(child, "reg", &reg_prop, &reg_len) ==
6147c478bd9Sstevel@tonic-gate 	    DDI_PROP_SUCCESS) && (reg_len != 0)) {
6157c478bd9Sstevel@tonic-gate 		pdptr->par_nreg = reg_len / (int)sizeof (struct regspec);
6167c478bd9Sstevel@tonic-gate 		pdptr->par_reg = (struct regspec *)reg_prop;
6177c478bd9Sstevel@tonic-gate 	}
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	/*
6207c478bd9Sstevel@tonic-gate 	 * See if I have a range (adding one where needed - this
6217c478bd9Sstevel@tonic-gate 	 * means to add one for sbus node in sun4c, when romvec > 0,
6227c478bd9Sstevel@tonic-gate 	 * if no range is already defined in the PROM node.
6237c478bd9Sstevel@tonic-gate 	 * (Currently no sun4c PROMS define range properties,
6247c478bd9Sstevel@tonic-gate 	 * but they should and may in the future.)  For the SBus
6257c478bd9Sstevel@tonic-gate 	 * node, the range is defined by the SBus reg property.
6267c478bd9Sstevel@tonic-gate 	 */
6277c478bd9Sstevel@tonic-gate 	if (get_prop_int_array(child, "ranges", &rng_prop, &rng_len)
6287c478bd9Sstevel@tonic-gate 	    == DDI_PROP_SUCCESS) {
6297c478bd9Sstevel@tonic-gate 		pdptr->par_nrng = rng_len / (int)(sizeof (struct rangespec));
6307c478bd9Sstevel@tonic-gate 		pdptr->par_rng = (struct rangespec *)rng_prop;
6317c478bd9Sstevel@tonic-gate 	}
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	/*
6347c478bd9Sstevel@tonic-gate 	 * Handle the 'intr' and 'interrupts' properties
6357c478bd9Sstevel@tonic-gate 	 */
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	/*
6387c478bd9Sstevel@tonic-gate 	 * For backwards compatibility
6397c478bd9Sstevel@tonic-gate 	 * we first look for the 'intr' property for the device.
6407c478bd9Sstevel@tonic-gate 	 */
6417c478bd9Sstevel@tonic-gate 	if (get_prop_int_array(child, "intr", &intr_prop, &intr_len)
6427c478bd9Sstevel@tonic-gate 	    != DDI_PROP_SUCCESS) {
6437c478bd9Sstevel@tonic-gate 		intr_len = 0;
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	/*
6477c478bd9Sstevel@tonic-gate 	 * If we're to support bus adapters and future platforms cleanly,
6487c478bd9Sstevel@tonic-gate 	 * we need to support the generalized 'interrupts' property.
6497c478bd9Sstevel@tonic-gate 	 */
6507c478bd9Sstevel@tonic-gate 	if (get_prop_int_array(child, "interrupts", &irupts_prop,
6517c478bd9Sstevel@tonic-gate 	    &irupts_len) != DDI_PROP_SUCCESS) {
6527c478bd9Sstevel@tonic-gate 		irupts_len = 0;
6537c478bd9Sstevel@tonic-gate 	} else if (intr_len != 0) {
6547c478bd9Sstevel@tonic-gate 		/*
6557c478bd9Sstevel@tonic-gate 		 * If both 'intr' and 'interrupts' are defined,
6567c478bd9Sstevel@tonic-gate 		 * then 'interrupts' wins and we toss the 'intr' away.
6577c478bd9Sstevel@tonic-gate 		 */
6587c478bd9Sstevel@tonic-gate 		ddi_prop_free((void *)intr_prop);
6597c478bd9Sstevel@tonic-gate 		intr_len = 0;
6607c478bd9Sstevel@tonic-gate 	}
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	if (intr_len != 0) {
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 		/*
6657c478bd9Sstevel@tonic-gate 		 * Translate the 'intr' property into an array
6667c478bd9Sstevel@tonic-gate 		 * an array of struct intrspec's.  There's not really
6677c478bd9Sstevel@tonic-gate 		 * very much to do here except copy what's out there.
6687c478bd9Sstevel@tonic-gate 		 */
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 		struct intrspec *new;
6717c478bd9Sstevel@tonic-gate 		struct prop_ispec *l;
6727c478bd9Sstevel@tonic-gate 
673843e1988Sjohnlev 		n = pdptr->par_nintr = intr_len / sizeof (struct prop_ispec);
6747c478bd9Sstevel@tonic-gate 		l = (struct prop_ispec *)intr_prop;
6757c478bd9Sstevel@tonic-gate 		pdptr->par_intr =
6767c478bd9Sstevel@tonic-gate 		    new = kmem_zalloc(n * sizeof (struct intrspec), KM_SLEEP);
6777c478bd9Sstevel@tonic-gate 		while (n--) {
6787c478bd9Sstevel@tonic-gate 			new->intrspec_pri = l->pri;
6797c478bd9Sstevel@tonic-gate 			new->intrspec_vec = l->vec;
6807c478bd9Sstevel@tonic-gate 			new++;
6817c478bd9Sstevel@tonic-gate 			l++;
6827c478bd9Sstevel@tonic-gate 		}
6837c478bd9Sstevel@tonic-gate 		ddi_prop_free((void *)intr_prop);
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	} else if ((n = irupts_len) != 0) {
6867c478bd9Sstevel@tonic-gate 		size_t size;
6877c478bd9Sstevel@tonic-gate 		int *out;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 		/*
6907c478bd9Sstevel@tonic-gate 		 * Translate the 'interrupts' property into an array
6917c478bd9Sstevel@tonic-gate 		 * of intrspecs for the rest of the DDI framework to
6927c478bd9Sstevel@tonic-gate 		 * toy with.  Only our ancestors really know how to
6937c478bd9Sstevel@tonic-gate 		 * do this, so ask 'em.  We massage the 'interrupts'
6947c478bd9Sstevel@tonic-gate 		 * property so that it is pre-pended by a count of
6957c478bd9Sstevel@tonic-gate 		 * the number of integers in the argument.
6967c478bd9Sstevel@tonic-gate 		 */
6977c478bd9Sstevel@tonic-gate 		size = sizeof (int) + n;
6987c478bd9Sstevel@tonic-gate 		out = kmem_alloc(size, KM_SLEEP);
6997c478bd9Sstevel@tonic-gate 		*out = n / sizeof (int);
7007c478bd9Sstevel@tonic-gate 		bcopy(irupts_prop, out + 1, (size_t)n);
7017c478bd9Sstevel@tonic-gate 		ddi_prop_free((void *)irupts_prop);
702a195726fSgovinda 		if (impl_xlate_intrs(child, out, pdptr) != DDI_SUCCESS) {
7037c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT,
7047c478bd9Sstevel@tonic-gate 			    "Unable to translate 'interrupts' for %s%d\n",
7057c478bd9Sstevel@tonic-gate 			    DEVI(child)->devi_binding_name,
7067c478bd9Sstevel@tonic-gate 			    DEVI(child)->devi_instance);
7077c478bd9Sstevel@tonic-gate 		}
7087c478bd9Sstevel@tonic-gate 		kmem_free(out, size);
7097c478bd9Sstevel@tonic-gate 	}
7107c478bd9Sstevel@tonic-gate }
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate /*
7137c478bd9Sstevel@tonic-gate  * Name a child
7147c478bd9Sstevel@tonic-gate  */
7157c478bd9Sstevel@tonic-gate static int
impl_sunbus_name_child(dev_info_t * child,char * name,int namelen)7167c478bd9Sstevel@tonic-gate impl_sunbus_name_child(dev_info_t *child, char *name, int namelen)
7177c478bd9Sstevel@tonic-gate {
7187c478bd9Sstevel@tonic-gate 	/*
7197c478bd9Sstevel@tonic-gate 	 * Fill in parent-private data and this function returns to us
7207c478bd9Sstevel@tonic-gate 	 * an indication if it used "registers" to fill in the data.
7217c478bd9Sstevel@tonic-gate 	 */
7227c478bd9Sstevel@tonic-gate 	if (ddi_get_parent_data(child) == NULL) {
7237c478bd9Sstevel@tonic-gate 		struct ddi_parent_private_data *pdptr;
7247c478bd9Sstevel@tonic-gate 		make_ddi_ppd(child, &pdptr);
7257c478bd9Sstevel@tonic-gate 		ddi_set_parent_data(child, pdptr);
7267c478bd9Sstevel@tonic-gate 	}
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	name[0] = '\0';
7297c478bd9Sstevel@tonic-gate 	if (sparc_pd_getnreg(child) > 0) {
7307c478bd9Sstevel@tonic-gate 		(void) snprintf(name, namelen, "%x,%x",
7317c478bd9Sstevel@tonic-gate 		    (uint_t)sparc_pd_getreg(child, 0)->regspec_bustype,
7327c478bd9Sstevel@tonic-gate 		    (uint_t)sparc_pd_getreg(child, 0)->regspec_addr);
7337c478bd9Sstevel@tonic-gate 	}
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7367c478bd9Sstevel@tonic-gate }
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate /*
7397c478bd9Sstevel@tonic-gate  * Called from the bus_ctl op of sunbus (sbus, obio, etc) nexus drivers
7407c478bd9Sstevel@tonic-gate  * to implement the DDI_CTLOPS_INITCHILD operation.  That is, it names
7417c478bd9Sstevel@tonic-gate  * the children of sun busses based on the reg spec.
7427c478bd9Sstevel@tonic-gate  *
7437c478bd9Sstevel@tonic-gate  * Handles the following properties (in make_ddi_ppd):
7447c478bd9Sstevel@tonic-gate  *	Property		value
7457c478bd9Sstevel@tonic-gate  *	  Name			type
7467c478bd9Sstevel@tonic-gate  *	reg		register spec
7477c478bd9Sstevel@tonic-gate  *	intr		old-form interrupt spec
7487c478bd9Sstevel@tonic-gate  *	interrupts	new (bus-oriented) interrupt spec
7497c478bd9Sstevel@tonic-gate  *	ranges		range spec
7507c478bd9Sstevel@tonic-gate  */
7517c478bd9Sstevel@tonic-gate int
impl_ddi_sunbus_initchild(dev_info_t * child)7527c478bd9Sstevel@tonic-gate impl_ddi_sunbus_initchild(dev_info_t *child)
7537c478bd9Sstevel@tonic-gate {
7547c478bd9Sstevel@tonic-gate 	char name[MAXNAMELEN];
7557c478bd9Sstevel@tonic-gate 	void impl_ddi_sunbus_removechild(dev_info_t *);
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	/*
7587c478bd9Sstevel@tonic-gate 	 * Name the child, also makes parent private data
7597c478bd9Sstevel@tonic-gate 	 */
7607c478bd9Sstevel@tonic-gate 	(void) impl_sunbus_name_child(child, name, MAXNAMELEN);
7617c478bd9Sstevel@tonic-gate 	ddi_set_name_addr(child, name);
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	/*
7647c478bd9Sstevel@tonic-gate 	 * Attempt to merge a .conf node; if successful, remove the
7657c478bd9Sstevel@tonic-gate 	 * .conf node.
7667c478bd9Sstevel@tonic-gate 	 */
7677c478bd9Sstevel@tonic-gate 	if ((ndi_dev_is_persistent_node(child) == 0) &&
7687c478bd9Sstevel@tonic-gate 	    (ndi_merge_node(child, impl_sunbus_name_child) == DDI_SUCCESS)) {
7697c478bd9Sstevel@tonic-gate 		/*
7707c478bd9Sstevel@tonic-gate 		 * Return failure to remove node
7717c478bd9Sstevel@tonic-gate 		 */
7727c478bd9Sstevel@tonic-gate 		impl_ddi_sunbus_removechild(child);
7737c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7747c478bd9Sstevel@tonic-gate 	}
7757c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7767c478bd9Sstevel@tonic-gate }
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate void
impl_free_ddi_ppd(dev_info_t * dip)7797c478bd9Sstevel@tonic-gate impl_free_ddi_ppd(dev_info_t *dip)
7807c478bd9Sstevel@tonic-gate {
7817c478bd9Sstevel@tonic-gate 	struct ddi_parent_private_data *pdptr;
7827c478bd9Sstevel@tonic-gate 	size_t n;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	if ((pdptr = ddi_get_parent_data(dip)) == NULL)
7857c478bd9Sstevel@tonic-gate 		return;
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	if ((n = (size_t)pdptr->par_nintr) != 0)
7887c478bd9Sstevel@tonic-gate 		/*
7897c478bd9Sstevel@tonic-gate 		 * Note that kmem_free is used here (instead of
7907c478bd9Sstevel@tonic-gate 		 * ddi_prop_free) because the contents of the
7917c478bd9Sstevel@tonic-gate 		 * property were placed into a separate buffer and
7927c478bd9Sstevel@tonic-gate 		 * mucked with a bit before being stored in par_intr.
7937c478bd9Sstevel@tonic-gate 		 * The actual return value from the prop lookup
7947c478bd9Sstevel@tonic-gate 		 * was freed with ddi_prop_free previously.
7957c478bd9Sstevel@tonic-gate 		 */
7967c478bd9Sstevel@tonic-gate 		kmem_free(pdptr->par_intr, n * sizeof (struct intrspec));
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	if ((n = (size_t)pdptr->par_nrng) != 0)
7997c478bd9Sstevel@tonic-gate 		ddi_prop_free((void *)pdptr->par_rng);
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	if ((n = pdptr->par_nreg) != 0)
8027c478bd9Sstevel@tonic-gate 		ddi_prop_free((void *)pdptr->par_reg);
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	kmem_free(pdptr, sizeof (*pdptr));
8057c478bd9Sstevel@tonic-gate 	ddi_set_parent_data(dip, NULL);
8067c478bd9Sstevel@tonic-gate }
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate void
impl_ddi_sunbus_removechild(dev_info_t * dip)8097c478bd9Sstevel@tonic-gate impl_ddi_sunbus_removechild(dev_info_t *dip)
8107c478bd9Sstevel@tonic-gate {
8117c478bd9Sstevel@tonic-gate 	impl_free_ddi_ppd(dip);
8127c478bd9Sstevel@tonic-gate 	ddi_set_name_addr(dip, NULL);
8137c478bd9Sstevel@tonic-gate 	/*
8147c478bd9Sstevel@tonic-gate 	 * Strip the node to properly convert it back to prototype form
8157c478bd9Sstevel@tonic-gate 	 */
8167c478bd9Sstevel@tonic-gate 	impl_rem_dev_props(dip);
8177c478bd9Sstevel@tonic-gate }
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate /*
8207c478bd9Sstevel@tonic-gate  * DDI Interrupt
8217c478bd9Sstevel@tonic-gate  */
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate /*
8247c478bd9Sstevel@tonic-gate  * turn this on to force isa, eisa, and mca device to ignore the new
8257c478bd9Sstevel@tonic-gate  * hardware nodes in the device tree (normally turned on only for
8267c478bd9Sstevel@tonic-gate  * drivers that need it by setting the property "ignore-hardware-nodes"
8277c478bd9Sstevel@tonic-gate  * in their driver.conf file).
8287c478bd9Sstevel@tonic-gate  *
8297c478bd9Sstevel@tonic-gate  * 7/31/96 -- Turned off globally.  Leaving variable in for the moment
8307c478bd9Sstevel@tonic-gate  *		as safety valve.
8317c478bd9Sstevel@tonic-gate  */
8327c478bd9Sstevel@tonic-gate int ignore_hardware_nodes = 0;
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate /*
8357c478bd9Sstevel@tonic-gate  * New DDI interrupt framework
8367c478bd9Sstevel@tonic-gate  */
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate /*
8397c478bd9Sstevel@tonic-gate  * i_ddi_intr_ops:
8407c478bd9Sstevel@tonic-gate  *
8417c478bd9Sstevel@tonic-gate  * This is the interrupt operator function wrapper for the bus function
8427c478bd9Sstevel@tonic-gate  * bus_intr_op.
8437c478bd9Sstevel@tonic-gate  */
8447c478bd9Sstevel@tonic-gate int
i_ddi_intr_ops(dev_info_t * dip,dev_info_t * rdip,ddi_intr_op_t op,ddi_intr_handle_impl_t * hdlp,void * result)8457c478bd9Sstevel@tonic-gate i_ddi_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t op,
8467c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void * result)
8477c478bd9Sstevel@tonic-gate {
8487c478bd9Sstevel@tonic-gate 	dev_info_t	*pdip = (dev_info_t *)DEVI(dip)->devi_parent;
8497c478bd9Sstevel@tonic-gate 	int		ret = DDI_FAILURE;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	/* request parent to process this interrupt op */
8527c478bd9Sstevel@tonic-gate 	if (NEXUS_HAS_INTR_OP(pdip))
8537c478bd9Sstevel@tonic-gate 		ret = (*(DEVI(pdip)->devi_ops->devo_bus_ops->bus_intr_op))(
8547c478bd9Sstevel@tonic-gate 		    pdip, rdip, op, hdlp, result);
8557c478bd9Sstevel@tonic-gate 	else
8567c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "Failed to process interrupt "
8577c478bd9Sstevel@tonic-gate 		    "for %s%d due to down-rev nexus driver %s%d",
8587c478bd9Sstevel@tonic-gate 		    ddi_get_name(rdip), ddi_get_instance(rdip),
8597c478bd9Sstevel@tonic-gate 		    ddi_get_name(pdip), ddi_get_instance(pdip));
8607c478bd9Sstevel@tonic-gate 	return (ret);
8617c478bd9Sstevel@tonic-gate }
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate /*
8647c478bd9Sstevel@tonic-gate  * i_ddi_add_softint - allocate and add a soft interrupt to the system
8657c478bd9Sstevel@tonic-gate  */
8667c478bd9Sstevel@tonic-gate int
i_ddi_add_softint(ddi_softint_hdl_impl_t * hdlp)8677c478bd9Sstevel@tonic-gate i_ddi_add_softint(ddi_softint_hdl_impl_t *hdlp)
8687c478bd9Sstevel@tonic-gate {
8697c478bd9Sstevel@tonic-gate 	int ret;
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	/* add soft interrupt handler */
8727c478bd9Sstevel@tonic-gate 	ret = add_avsoftintr((void *)hdlp, hdlp->ih_pri, hdlp->ih_cb_func,
8737c478bd9Sstevel@tonic-gate 	    DEVI(hdlp->ih_dip)->devi_name, hdlp->ih_cb_arg1, hdlp->ih_cb_arg2);
8747c478bd9Sstevel@tonic-gate 	return (ret ? DDI_SUCCESS : DDI_FAILURE);
8757c478bd9Sstevel@tonic-gate }
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate void
i_ddi_remove_softint(ddi_softint_hdl_impl_t * hdlp)8797c478bd9Sstevel@tonic-gate i_ddi_remove_softint(ddi_softint_hdl_impl_t *hdlp)
8807c478bd9Sstevel@tonic-gate {
8817c478bd9Sstevel@tonic-gate 	(void) rem_avsoftintr((void *)hdlp, hdlp->ih_pri, hdlp->ih_cb_func);
8827c478bd9Sstevel@tonic-gate }
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 
885e23a7e34Slq150181 extern void (*setsoftint)(int, struct av_softinfo *);
886e23a7e34Slq150181 extern boolean_t av_check_softint_pending(struct av_softinfo *, boolean_t);
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate int
i_ddi_trigger_softint(ddi_softint_hdl_impl_t * hdlp,void * arg2)889b08160e2Sgovinda i_ddi_trigger_softint(ddi_softint_hdl_impl_t *hdlp, void *arg2)
8907c478bd9Sstevel@tonic-gate {
891e23a7e34Slq150181 	if (av_check_softint_pending(hdlp->ih_pending, B_FALSE))
892e23a7e34Slq150181 		return (DDI_EPENDING);
893b08160e2Sgovinda 
894e23a7e34Slq150181 	update_avsoftintr_args((void *)hdlp, hdlp->ih_pri, arg2);
895b08160e2Sgovinda 
896e23a7e34Slq150181 	(*setsoftint)(hdlp->ih_pri, hdlp->ih_pending);
897e23a7e34Slq150181 	return (DDI_SUCCESS);
8987c478bd9Sstevel@tonic-gate }
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate /*
9017c478bd9Sstevel@tonic-gate  * i_ddi_set_softint_pri:
9027c478bd9Sstevel@tonic-gate  *
9037c478bd9Sstevel@tonic-gate  * The way this works is that it first tries to add a softint vector
9047c478bd9Sstevel@tonic-gate  * at the new priority in hdlp. If that succeeds; then it removes the
9057c478bd9Sstevel@tonic-gate  * existing softint vector at the old priority.
9067c478bd9Sstevel@tonic-gate  */
9077c478bd9Sstevel@tonic-gate int
i_ddi_set_softint_pri(ddi_softint_hdl_impl_t * hdlp,uint_t old_pri)9087c478bd9Sstevel@tonic-gate i_ddi_set_softint_pri(ddi_softint_hdl_impl_t *hdlp, uint_t old_pri)
9097c478bd9Sstevel@tonic-gate {
910e23a7e34Slq150181 	int ret;
911e23a7e34Slq150181 
9127c478bd9Sstevel@tonic-gate 	/*
9137c478bd9Sstevel@tonic-gate 	 * If a softint is pending at the old priority then fail the request.
9147c478bd9Sstevel@tonic-gate 	 */
915e23a7e34Slq150181 	if (av_check_softint_pending(hdlp->ih_pending, B_TRUE))
9167c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
9177c478bd9Sstevel@tonic-gate 
918e23a7e34Slq150181 	ret = av_softint_movepri((void *)hdlp, old_pri);
919e23a7e34Slq150181 	return (ret ? DDI_SUCCESS : DDI_FAILURE);
9207c478bd9Sstevel@tonic-gate }
9217c478bd9Sstevel@tonic-gate 
9227a364d25Sschwartz void
i_ddi_alloc_intr_phdl(ddi_intr_handle_impl_t * hdlp)9237a364d25Sschwartz i_ddi_alloc_intr_phdl(ddi_intr_handle_impl_t *hdlp)
9247a364d25Sschwartz {
9257a364d25Sschwartz 	hdlp->ih_private = (void *)kmem_zalloc(sizeof (ihdl_plat_t), KM_SLEEP);
9267a364d25Sschwartz }
9277a364d25Sschwartz 
9287a364d25Sschwartz void
i_ddi_free_intr_phdl(ddi_intr_handle_impl_t * hdlp)9297a364d25Sschwartz i_ddi_free_intr_phdl(ddi_intr_handle_impl_t *hdlp)
9307a364d25Sschwartz {
9317a364d25Sschwartz 	kmem_free(hdlp->ih_private, sizeof (ihdl_plat_t));
9327a364d25Sschwartz 	hdlp->ih_private = NULL;
9337a364d25Sschwartz }
9347a364d25Sschwartz 
935a54f81fbSanish int
i_ddi_get_intx_nintrs(dev_info_t * dip)936a54f81fbSanish i_ddi_get_intx_nintrs(dev_info_t *dip)
937a54f81fbSanish {
938a54f81fbSanish 	struct ddi_parent_private_data *pdp;
939a54f81fbSanish 
940a54f81fbSanish 	if ((pdp = ddi_get_parent_data(dip)) == NULL)
941a54f81fbSanish 		return (0);
942a54f81fbSanish 
943a54f81fbSanish 	return (pdp->par_nintr);
944a54f81fbSanish }
945a54f81fbSanish 
9467c478bd9Sstevel@tonic-gate /*
9477c478bd9Sstevel@tonic-gate  * DDI Memory/DMA
9487c478bd9Sstevel@tonic-gate  */
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate /*
9517c478bd9Sstevel@tonic-gate  * Support for allocating DMAable memory to implement
9527c478bd9Sstevel@tonic-gate  * ddi_dma_mem_alloc(9F) interface.
9537c478bd9Sstevel@tonic-gate  */
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate #define	KA_ALIGN_SHIFT	7
9567c478bd9Sstevel@tonic-gate #define	KA_ALIGN	(1 << KA_ALIGN_SHIFT)
9577c478bd9Sstevel@tonic-gate #define	KA_NCACHE	(PAGESHIFT + 1 - KA_ALIGN_SHIFT)
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate /*
9607c478bd9Sstevel@tonic-gate  * Dummy DMA attribute template for kmem_io[].kmem_io_attr.  We only
9617c478bd9Sstevel@tonic-gate  * care about addr_lo, addr_hi, and align.  addr_hi will be dynamically set.
9627c478bd9Sstevel@tonic-gate  */
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate static ddi_dma_attr_t kmem_io_attr = {
9657c478bd9Sstevel@tonic-gate 	DMA_ATTR_V0,
9667c478bd9Sstevel@tonic-gate 	0x0000000000000000ULL,		/* dma_attr_addr_lo */
9677c478bd9Sstevel@tonic-gate 	0x0000000000000000ULL,		/* dma_attr_addr_hi */
9687c478bd9Sstevel@tonic-gate 	0x00ffffff,
9697c478bd9Sstevel@tonic-gate 	0x1000,				/* dma_attr_align */
9707c478bd9Sstevel@tonic-gate 	1, 1, 0xffffffffULL, 0xffffffffULL, 0x1, 1, 0
9717c478bd9Sstevel@tonic-gate };
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate /* kmem io memory ranges and indices */
9747c478bd9Sstevel@tonic-gate enum {
9757c478bd9Sstevel@tonic-gate 	IO_4P, IO_64G, IO_4G, IO_2G, IO_1G, IO_512M,
9767c478bd9Sstevel@tonic-gate 	IO_256M, IO_128M, IO_64M, IO_32M, IO_16M, MAX_MEM_RANGES
9777c478bd9Sstevel@tonic-gate };
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate static struct {
9807c478bd9Sstevel@tonic-gate 	vmem_t		*kmem_io_arena;
9817c478bd9Sstevel@tonic-gate 	kmem_cache_t	*kmem_io_cache[KA_NCACHE];
9827c478bd9Sstevel@tonic-gate 	ddi_dma_attr_t	kmem_io_attr;
9837c478bd9Sstevel@tonic-gate } kmem_io[MAX_MEM_RANGES];
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate static int kmem_io_idx;		/* index of first populated kmem_io[] */
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate static page_t *
page_create_io_wrapper(void * addr,size_t len,int vmflag,void * arg)9887c478bd9Sstevel@tonic-gate page_create_io_wrapper(void *addr, size_t len, int vmflag, void *arg)
9897c478bd9Sstevel@tonic-gate {
9907c478bd9Sstevel@tonic-gate 	extern page_t *page_create_io(vnode_t *, u_offset_t, uint_t,
9917c478bd9Sstevel@tonic-gate 	    uint_t, struct as *, caddr_t, ddi_dma_attr_t *);
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	return (page_create_io(&kvp, (u_offset_t)(uintptr_t)addr, len,
9947c478bd9Sstevel@tonic-gate 	    PG_EXCL | ((vmflag & VM_NOSLEEP) ? 0 : PG_WAIT), &kas, addr, arg));
9957c478bd9Sstevel@tonic-gate }
9967c478bd9Sstevel@tonic-gate 
997843e1988Sjohnlev #ifdef __xpv
998843e1988Sjohnlev static void
segkmem_free_io(vmem_t * vmp,void * ptr,size_t size)999843e1988Sjohnlev segkmem_free_io(vmem_t *vmp, void *ptr, size_t size)
1000843e1988Sjohnlev {
1001843e1988Sjohnlev 	extern void page_destroy_io(page_t *);
100204909c8cSJohn Levon 	segkmem_xfree(vmp, ptr, size, &kvp, page_destroy_io);
1003843e1988Sjohnlev }
1004843e1988Sjohnlev #endif
1005843e1988Sjohnlev 
10067c478bd9Sstevel@tonic-gate static void *
segkmem_alloc_io_4P(vmem_t * vmp,size_t size,int vmflag)10077c478bd9Sstevel@tonic-gate segkmem_alloc_io_4P(vmem_t *vmp, size_t size, int vmflag)
10087c478bd9Sstevel@tonic-gate {
10097c478bd9Sstevel@tonic-gate 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
10107c478bd9Sstevel@tonic-gate 	    page_create_io_wrapper, &kmem_io[IO_4P].kmem_io_attr));
10117c478bd9Sstevel@tonic-gate }
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate static void *
segkmem_alloc_io_64G(vmem_t * vmp,size_t size,int vmflag)10147c478bd9Sstevel@tonic-gate segkmem_alloc_io_64G(vmem_t *vmp, size_t size, int vmflag)
10157c478bd9Sstevel@tonic-gate {
10167c478bd9Sstevel@tonic-gate 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
10177c478bd9Sstevel@tonic-gate 	    page_create_io_wrapper, &kmem_io[IO_64G].kmem_io_attr));
10187c478bd9Sstevel@tonic-gate }
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate static void *
segkmem_alloc_io_4G(vmem_t * vmp,size_t size,int vmflag)10217c478bd9Sstevel@tonic-gate segkmem_alloc_io_4G(vmem_t *vmp, size_t size, int vmflag)
10227c478bd9Sstevel@tonic-gate {
10237c478bd9Sstevel@tonic-gate 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
10247c478bd9Sstevel@tonic-gate 	    page_create_io_wrapper, &kmem_io[IO_4G].kmem_io_attr));
10257c478bd9Sstevel@tonic-gate }
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate static void *
segkmem_alloc_io_2G(vmem_t * vmp,size_t size,int vmflag)10287c478bd9Sstevel@tonic-gate segkmem_alloc_io_2G(vmem_t *vmp, size_t size, int vmflag)
10297c478bd9Sstevel@tonic-gate {
10307c478bd9Sstevel@tonic-gate 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
10317c478bd9Sstevel@tonic-gate 	    page_create_io_wrapper, &kmem_io[IO_2G].kmem_io_attr));
10327c478bd9Sstevel@tonic-gate }
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate static void *
segkmem_alloc_io_1G(vmem_t * vmp,size_t size,int vmflag)10357c478bd9Sstevel@tonic-gate segkmem_alloc_io_1G(vmem_t *vmp, size_t size, int vmflag)
10367c478bd9Sstevel@tonic-gate {
10377c478bd9Sstevel@tonic-gate 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
10387c478bd9Sstevel@tonic-gate 	    page_create_io_wrapper, &kmem_io[IO_1G].kmem_io_attr));
10397c478bd9Sstevel@tonic-gate }
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate static void *
segkmem_alloc_io_512M(vmem_t * vmp,size_t size,int vmflag)10427c478bd9Sstevel@tonic-gate segkmem_alloc_io_512M(vmem_t *vmp, size_t size, int vmflag)
10437c478bd9Sstevel@tonic-gate {
10447c478bd9Sstevel@tonic-gate 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
10457c478bd9Sstevel@tonic-gate 	    page_create_io_wrapper, &kmem_io[IO_512M].kmem_io_attr));
10467c478bd9Sstevel@tonic-gate }
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate static void *
segkmem_alloc_io_256M(vmem_t * vmp,size_t size,int vmflag)10497c478bd9Sstevel@tonic-gate segkmem_alloc_io_256M(vmem_t *vmp, size_t size, int vmflag)
10507c478bd9Sstevel@tonic-gate {
10517c478bd9Sstevel@tonic-gate 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
10527c478bd9Sstevel@tonic-gate 	    page_create_io_wrapper, &kmem_io[IO_256M].kmem_io_attr));
10537c478bd9Sstevel@tonic-gate }
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate static void *
segkmem_alloc_io_128M(vmem_t * vmp,size_t size,int vmflag)10567c478bd9Sstevel@tonic-gate segkmem_alloc_io_128M(vmem_t *vmp, size_t size, int vmflag)
10577c478bd9Sstevel@tonic-gate {
10587c478bd9Sstevel@tonic-gate 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
10597c478bd9Sstevel@tonic-gate 	    page_create_io_wrapper, &kmem_io[IO_128M].kmem_io_attr));
10607c478bd9Sstevel@tonic-gate }
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate static void *
segkmem_alloc_io_64M(vmem_t * vmp,size_t size,int vmflag)10637c478bd9Sstevel@tonic-gate segkmem_alloc_io_64M(vmem_t *vmp, size_t size, int vmflag)
10647c478bd9Sstevel@tonic-gate {
10657c478bd9Sstevel@tonic-gate 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
10667c478bd9Sstevel@tonic-gate 	    page_create_io_wrapper, &kmem_io[IO_64M].kmem_io_attr));
10677c478bd9Sstevel@tonic-gate }
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate static void *
segkmem_alloc_io_32M(vmem_t * vmp,size_t size,int vmflag)10707c478bd9Sstevel@tonic-gate segkmem_alloc_io_32M(vmem_t *vmp, size_t size, int vmflag)
10717c478bd9Sstevel@tonic-gate {
10727c478bd9Sstevel@tonic-gate 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
10737c478bd9Sstevel@tonic-gate 	    page_create_io_wrapper, &kmem_io[IO_32M].kmem_io_attr));
10747c478bd9Sstevel@tonic-gate }
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate static void *
segkmem_alloc_io_16M(vmem_t * vmp,size_t size,int vmflag)10777c478bd9Sstevel@tonic-gate segkmem_alloc_io_16M(vmem_t *vmp, size_t size, int vmflag)
10787c478bd9Sstevel@tonic-gate {
10797c478bd9Sstevel@tonic-gate 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
10807c478bd9Sstevel@tonic-gate 	    page_create_io_wrapper, &kmem_io[IO_16M].kmem_io_attr));
10817c478bd9Sstevel@tonic-gate }
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate struct {
10847c478bd9Sstevel@tonic-gate 	uint64_t	io_limit;
10857c478bd9Sstevel@tonic-gate 	char		*io_name;
10867c478bd9Sstevel@tonic-gate 	void		*(*io_alloc)(vmem_t *, size_t, int);
10877c478bd9Sstevel@tonic-gate 	int		io_initial;	/* kmem_io_init during startup */
10887c478bd9Sstevel@tonic-gate } io_arena_params[MAX_MEM_RANGES] = {
10897c478bd9Sstevel@tonic-gate 	{0x000fffffffffffffULL,	"kmem_io_4P",	segkmem_alloc_io_4P,	1},
10907c478bd9Sstevel@tonic-gate 	{0x0000000fffffffffULL,	"kmem_io_64G",	segkmem_alloc_io_64G,	0},
10917c478bd9Sstevel@tonic-gate 	{0x00000000ffffffffULL,	"kmem_io_4G",	segkmem_alloc_io_4G,	1},
10927c478bd9Sstevel@tonic-gate 	{0x000000007fffffffULL,	"kmem_io_2G",	segkmem_alloc_io_2G,	1},
10937c478bd9Sstevel@tonic-gate 	{0x000000003fffffffULL,	"kmem_io_1G",	segkmem_alloc_io_1G,	0},
10947c478bd9Sstevel@tonic-gate 	{0x000000001fffffffULL,	"kmem_io_512M",	segkmem_alloc_io_512M,	0},
10957c478bd9Sstevel@tonic-gate 	{0x000000000fffffffULL,	"kmem_io_256M",	segkmem_alloc_io_256M,	0},
10967c478bd9Sstevel@tonic-gate 	{0x0000000007ffffffULL,	"kmem_io_128M",	segkmem_alloc_io_128M,	0},
10977c478bd9Sstevel@tonic-gate 	{0x0000000003ffffffULL,	"kmem_io_64M",	segkmem_alloc_io_64M,	0},
10987c478bd9Sstevel@tonic-gate 	{0x0000000001ffffffULL,	"kmem_io_32M",	segkmem_alloc_io_32M,	0},
10997c478bd9Sstevel@tonic-gate 	{0x0000000000ffffffULL,	"kmem_io_16M",	segkmem_alloc_io_16M,	1}
11007c478bd9Sstevel@tonic-gate };
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate void
kmem_io_init(int a)11037c478bd9Sstevel@tonic-gate kmem_io_init(int a)
11047c478bd9Sstevel@tonic-gate {
11057c478bd9Sstevel@tonic-gate 	int	c;
11067c478bd9Sstevel@tonic-gate 	char name[40];
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	kmem_io[a].kmem_io_arena = vmem_create(io_arena_params[a].io_name,
11097c478bd9Sstevel@tonic-gate 	    NULL, 0, PAGESIZE, io_arena_params[a].io_alloc,
1110843e1988Sjohnlev #ifdef __xpv
1111843e1988Sjohnlev 	    segkmem_free_io,
1112843e1988Sjohnlev #else
1113843e1988Sjohnlev 	    segkmem_free,
1114843e1988Sjohnlev #endif
1115843e1988Sjohnlev 	    heap_arena, 0, VM_SLEEP);
1116843e1988Sjohnlev 
11177c478bd9Sstevel@tonic-gate 	for (c = 0; c < KA_NCACHE; c++) {
11187c478bd9Sstevel@tonic-gate 		size_t size = KA_ALIGN << c;
11197c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "%s_%lu",
11207c478bd9Sstevel@tonic-gate 		    io_arena_params[a].io_name, size);
11217c478bd9Sstevel@tonic-gate 		kmem_io[a].kmem_io_cache[c] = kmem_cache_create(name,
11227c478bd9Sstevel@tonic-gate 		    size, size, NULL, NULL, NULL, NULL,
11237c478bd9Sstevel@tonic-gate 		    kmem_io[a].kmem_io_arena, 0);
11247c478bd9Sstevel@tonic-gate 	}
11257c478bd9Sstevel@tonic-gate }
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate /*
11287c478bd9Sstevel@tonic-gate  * Return the index of the highest memory range for addr.
11297c478bd9Sstevel@tonic-gate  */
11307c478bd9Sstevel@tonic-gate static int
kmem_io_index(uint64_t addr)11317c478bd9Sstevel@tonic-gate kmem_io_index(uint64_t addr)
11327c478bd9Sstevel@tonic-gate {
11337c478bd9Sstevel@tonic-gate 	int n;
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	for (n = kmem_io_idx; n < MAX_MEM_RANGES; n++) {
11367c478bd9Sstevel@tonic-gate 		if (kmem_io[n].kmem_io_attr.dma_attr_addr_hi <= addr) {
11377c478bd9Sstevel@tonic-gate 			if (kmem_io[n].kmem_io_arena == NULL)
11387c478bd9Sstevel@tonic-gate 				kmem_io_init(n);
11397c478bd9Sstevel@tonic-gate 			return (n);
11407c478bd9Sstevel@tonic-gate 		}
11417c478bd9Sstevel@tonic-gate 	}
11427c478bd9Sstevel@tonic-gate 	panic("kmem_io_index: invalid addr - must be at least 16m");
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
11457c478bd9Sstevel@tonic-gate }
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate /*
11487c478bd9Sstevel@tonic-gate  * Return the index of the next kmem_io populated memory range
11497c478bd9Sstevel@tonic-gate  * after curindex.
11507c478bd9Sstevel@tonic-gate  */
11517c478bd9Sstevel@tonic-gate static int
kmem_io_index_next(int curindex)11527c478bd9Sstevel@tonic-gate kmem_io_index_next(int curindex)
11537c478bd9Sstevel@tonic-gate {
11547c478bd9Sstevel@tonic-gate 	int n;
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	for (n = curindex + 1; n < MAX_MEM_RANGES; n++) {
11577c478bd9Sstevel@tonic-gate 		if (kmem_io[n].kmem_io_arena)
11587c478bd9Sstevel@tonic-gate 			return (n);
11597c478bd9Sstevel@tonic-gate 	}
11607c478bd9Sstevel@tonic-gate 	return (-1);
11617c478bd9Sstevel@tonic-gate }
11627c478bd9Sstevel@tonic-gate 
11637b93957cSeota /*
11647b93957cSeota  * allow kmem to be mapped in with different PTE cache attribute settings.
11657b93957cSeota  * Used by i_ddi_mem_alloc()
11667b93957cSeota  */
11677b93957cSeota int
kmem_override_cache_attrs(caddr_t kva,size_t size,uint_t order)11687b93957cSeota kmem_override_cache_attrs(caddr_t kva, size_t size, uint_t order)
11697b93957cSeota {
11707b93957cSeota 	uint_t hat_flags;
11717b93957cSeota 	caddr_t kva_end;
11727b93957cSeota 	uint_t hat_attr;
11737b93957cSeota 	pfn_t pfn;
11747b93957cSeota 
11757b93957cSeota 	if (hat_getattr(kas.a_hat, kva, &hat_attr) == -1) {
11767b93957cSeota 		return (-1);
11777b93957cSeota 	}
11787b93957cSeota 
11797b93957cSeota 	hat_attr &= ~HAT_ORDER_MASK;
11807b93957cSeota 	hat_attr |= order | HAT_NOSYNC;
11817b93957cSeota 	hat_flags = HAT_LOAD_LOCK;
11827b93957cSeota 
11837b93957cSeota 	kva_end = (caddr_t)(((uintptr_t)kva + size + PAGEOFFSET) &
11847b93957cSeota 	    (uintptr_t)PAGEMASK);
11857b93957cSeota 	kva = (caddr_t)((uintptr_t)kva & (uintptr_t)PAGEMASK);
11867b93957cSeota 
11877b93957cSeota 	while (kva < kva_end) {
11887b93957cSeota 		pfn = hat_getpfnum(kas.a_hat, kva);
11897b93957cSeota 		hat_unload(kas.a_hat, kva, PAGESIZE, HAT_UNLOAD_UNLOCK);
11907b93957cSeota 		hat_devload(kas.a_hat, kva, PAGESIZE, pfn, hat_attr, hat_flags);
11917b93957cSeota 		kva += MMU_PAGESIZE;
11927b93957cSeota 	}
11937b93957cSeota 
11947b93957cSeota 	return (0);
11957b93957cSeota }
11967b93957cSeota 
1197a7175e20SGarrett D'Amore static int
ctgcompare(const void * a1,const void * a2)1198a7175e20SGarrett D'Amore ctgcompare(const void *a1, const void *a2)
1199a7175e20SGarrett D'Amore {
1200a7175e20SGarrett D'Amore 	/* we just want to compare virtual addresses */
1201a7175e20SGarrett D'Amore 	a1 = ((struct ctgas *)a1)->ctg_addr;
1202a7175e20SGarrett D'Amore 	a2 = ((struct ctgas *)a2)->ctg_addr;
1203a7175e20SGarrett D'Amore 	return (a1 == a2 ? 0 : (a1 < a2 ? -1 : 1));
1204a7175e20SGarrett D'Amore }
1205a7175e20SGarrett D'Amore 
12067c478bd9Sstevel@tonic-gate void
ka_init(void)12077c478bd9Sstevel@tonic-gate ka_init(void)
12087c478bd9Sstevel@tonic-gate {
12097c478bd9Sstevel@tonic-gate 	int a;
1210843e1988Sjohnlev 	paddr_t maxphysaddr;
1211843e1988Sjohnlev #if !defined(__xpv)
12127c478bd9Sstevel@tonic-gate 	extern pfn_t physmax;
1213843e1988Sjohnlev 
1214843e1988Sjohnlev 	maxphysaddr = mmu_ptob((paddr_t)physmax) + MMU_PAGEOFFSET;
1215843e1988Sjohnlev #else
1216843e1988Sjohnlev 	maxphysaddr = mmu_ptob((paddr_t)HYPERVISOR_memory_op(
1217843e1988Sjohnlev 	    XENMEM_maximum_ram_page, NULL)) + MMU_PAGEOFFSET;
1218843e1988Sjohnlev #endif
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 	ASSERT(maxphysaddr <= io_arena_params[0].io_limit);
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 	for (a = 0; a < MAX_MEM_RANGES; a++) {
12237c478bd9Sstevel@tonic-gate 		if (maxphysaddr >= io_arena_params[a + 1].io_limit) {
12247c478bd9Sstevel@tonic-gate 			if (maxphysaddr > io_arena_params[a + 1].io_limit)
12257c478bd9Sstevel@tonic-gate 				io_arena_params[a].io_limit = maxphysaddr;
12267c478bd9Sstevel@tonic-gate 			else
12277c478bd9Sstevel@tonic-gate 				a++;
12287c478bd9Sstevel@tonic-gate 			break;
12297c478bd9Sstevel@tonic-gate 		}
12307c478bd9Sstevel@tonic-gate 	}
12317c478bd9Sstevel@tonic-gate 	kmem_io_idx = a;
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 	for (; a < MAX_MEM_RANGES; a++) {
12347c478bd9Sstevel@tonic-gate 		kmem_io[a].kmem_io_attr = kmem_io_attr;
12357c478bd9Sstevel@tonic-gate 		kmem_io[a].kmem_io_attr.dma_attr_addr_hi =
12367c478bd9Sstevel@tonic-gate 		    io_arena_params[a].io_limit;
12377c478bd9Sstevel@tonic-gate 		/*
12387c478bd9Sstevel@tonic-gate 		 * initialize kmem_io[] arena/cache corresponding to
12397c478bd9Sstevel@tonic-gate 		 * maxphysaddr and to the "common" io memory ranges that
12407c478bd9Sstevel@tonic-gate 		 * have io_initial set to a non-zero value.
12417c478bd9Sstevel@tonic-gate 		 */
12427c478bd9Sstevel@tonic-gate 		if (io_arena_params[a].io_initial || a == kmem_io_idx)
12437c478bd9Sstevel@tonic-gate 			kmem_io_init(a);
12447c478bd9Sstevel@tonic-gate 	}
1245a7175e20SGarrett D'Amore 
1246a7175e20SGarrett D'Amore 	/* initialize ctgtree */
1247a7175e20SGarrett D'Amore 	avl_create(&ctgtree, ctgcompare, sizeof (struct ctgas),
1248a7175e20SGarrett D'Amore 	    offsetof(struct ctgas, ctg_link));
12497c478bd9Sstevel@tonic-gate }
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate /*
12527c478bd9Sstevel@tonic-gate  * put contig address/size
12537c478bd9Sstevel@tonic-gate  */
12547c478bd9Sstevel@tonic-gate static void *
putctgas(void * addr,size_t size)12557c478bd9Sstevel@tonic-gate putctgas(void *addr, size_t size)
12567c478bd9Sstevel@tonic-gate {
1257a7175e20SGarrett D'Amore 	struct ctgas    *ctgp;
1258a7175e20SGarrett D'Amore 	if ((ctgp = kmem_zalloc(sizeof (*ctgp), KM_NOSLEEP)) != NULL) {
1259a7175e20SGarrett D'Amore 		ctgp->ctg_addr = addr;
1260a7175e20SGarrett D'Amore 		ctgp->ctg_size = size;
12617c478bd9Sstevel@tonic-gate 		CTGLOCK();
1262a7175e20SGarrett D'Amore 		avl_add(&ctgtree, ctgp);
12637c478bd9Sstevel@tonic-gate 		CTGUNLOCK();
1264a7175e20SGarrett D'Amore 	}
12657c478bd9Sstevel@tonic-gate 	return (ctgp);
12667c478bd9Sstevel@tonic-gate }
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate /*
12697c478bd9Sstevel@tonic-gate  * get contig size by addr
12707c478bd9Sstevel@tonic-gate  */
12717c478bd9Sstevel@tonic-gate static size_t
getctgsz(void * addr)12727c478bd9Sstevel@tonic-gate getctgsz(void *addr)
12737c478bd9Sstevel@tonic-gate {
1274a7175e20SGarrett D'Amore 	struct ctgas    *ctgp;
1275a7175e20SGarrett D'Amore 	struct ctgas    find;
1276a7175e20SGarrett D'Amore 	size_t		sz = 0;
12777c478bd9Sstevel@tonic-gate 
1278a7175e20SGarrett D'Amore 	find.ctg_addr = addr;
12797c478bd9Sstevel@tonic-gate 	CTGLOCK();
1280a7175e20SGarrett D'Amore 	if ((ctgp = avl_find(&ctgtree, &find, NULL)) != NULL) {
1281a7175e20SGarrett D'Amore 		avl_remove(&ctgtree, ctgp);
12827c478bd9Sstevel@tonic-gate 	}
12837c478bd9Sstevel@tonic-gate 	CTGUNLOCK();
1284a7175e20SGarrett D'Amore 
1285a7175e20SGarrett D'Amore 	if (ctgp != NULL) {
1286a7175e20SGarrett D'Amore 		sz = ctgp->ctg_size;
1287a7175e20SGarrett D'Amore 		kmem_free(ctgp, sizeof (*ctgp));
1288a7175e20SGarrett D'Amore 	}
1289a7175e20SGarrett D'Amore 
12907c478bd9Sstevel@tonic-gate 	return (sz);
12917c478bd9Sstevel@tonic-gate }
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate /*
12947c478bd9Sstevel@tonic-gate  * contig_alloc:
12957c478bd9Sstevel@tonic-gate  *
12967c478bd9Sstevel@tonic-gate  *	allocates contiguous memory to satisfy the 'size' and dma attributes
12977c478bd9Sstevel@tonic-gate  *	specified in 'attr'.
12987c478bd9Sstevel@tonic-gate  *
12997c478bd9Sstevel@tonic-gate  *	Not all of memory need to be physically contiguous if the
13007c478bd9Sstevel@tonic-gate  *	scatter-gather list length is greater than 1.
13017c478bd9Sstevel@tonic-gate  */
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13047c478bd9Sstevel@tonic-gate void *
contig_alloc(size_t size,ddi_dma_attr_t * attr,uintptr_t align,int cansleep)13057c478bd9Sstevel@tonic-gate contig_alloc(size_t size, ddi_dma_attr_t *attr, uintptr_t align, int cansleep)
13067c478bd9Sstevel@tonic-gate {
13077c478bd9Sstevel@tonic-gate 	pgcnt_t		pgcnt = btopr(size);
13087c478bd9Sstevel@tonic-gate 	size_t		asize = pgcnt * PAGESIZE;
13097c478bd9Sstevel@tonic-gate 	page_t		*ppl;
13107c478bd9Sstevel@tonic-gate 	int		pflag;
13117c478bd9Sstevel@tonic-gate 	void		*addr;
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	extern page_t *page_create_io(vnode_t *, u_offset_t, uint_t,
13147c478bd9Sstevel@tonic-gate 	    uint_t, struct as *, caddr_t, ddi_dma_attr_t *);
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 	/* segkmem_xalloc */
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	if (align <= PAGESIZE)
13197c478bd9Sstevel@tonic-gate 		addr = vmem_alloc(heap_arena, asize,
13207c478bd9Sstevel@tonic-gate 		    (cansleep) ? VM_SLEEP : VM_NOSLEEP);
13217c478bd9Sstevel@tonic-gate 	else
13227c478bd9Sstevel@tonic-gate 		addr = vmem_xalloc(heap_arena, asize, align, 0, 0, NULL, NULL,
13237c478bd9Sstevel@tonic-gate 		    (cansleep) ? VM_SLEEP : VM_NOSLEEP);
13247c478bd9Sstevel@tonic-gate 	if (addr) {
13257c478bd9Sstevel@tonic-gate 		ASSERT(!((uintptr_t)addr & (align - 1)));
13267c478bd9Sstevel@tonic-gate 
1327843e1988Sjohnlev 		if (page_resv(pgcnt, (cansleep) ? KM_SLEEP : KM_NOSLEEP) == 0) {
13287c478bd9Sstevel@tonic-gate 			vmem_free(heap_arena, addr, asize);
13297c478bd9Sstevel@tonic-gate 			return (NULL);
13307c478bd9Sstevel@tonic-gate 		}
13317c478bd9Sstevel@tonic-gate 		pflag = PG_EXCL;
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 		if (cansleep)
13347c478bd9Sstevel@tonic-gate 			pflag |= PG_WAIT;
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 		/* 4k req gets from freelists rather than pfn search */
13377c478bd9Sstevel@tonic-gate 		if (pgcnt > 1 || align > PAGESIZE)
13387c478bd9Sstevel@tonic-gate 			pflag |= PG_PHYSCONTIG;
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 		ppl = page_create_io(&kvp, (u_offset_t)(uintptr_t)addr,
13417c478bd9Sstevel@tonic-gate 		    asize, pflag, &kas, (caddr_t)addr, attr);
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 		if (!ppl) {
13447c478bd9Sstevel@tonic-gate 			vmem_free(heap_arena, addr, asize);
13457c478bd9Sstevel@tonic-gate 			page_unresv(pgcnt);
13467c478bd9Sstevel@tonic-gate 			return (NULL);
13477c478bd9Sstevel@tonic-gate 		}
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 		while (ppl != NULL) {
13507c478bd9Sstevel@tonic-gate 			page_t	*pp = ppl;
13517c478bd9Sstevel@tonic-gate 			page_sub(&ppl, pp);
13527c478bd9Sstevel@tonic-gate 			ASSERT(page_iolock_assert(pp));
13537c478bd9Sstevel@tonic-gate 			page_io_unlock(pp);
13547c478bd9Sstevel@tonic-gate 			page_downgrade(pp);
13557c478bd9Sstevel@tonic-gate 			hat_memload(kas.a_hat, (caddr_t)(uintptr_t)pp->p_offset,
13567c478bd9Sstevel@tonic-gate 			    pp, (PROT_ALL & ~PROT_USER) |
13577c478bd9Sstevel@tonic-gate 			    HAT_NOSYNC, HAT_LOAD_LOCK);
13587c478bd9Sstevel@tonic-gate 		}
13597c478bd9Sstevel@tonic-gate 	}
13607c478bd9Sstevel@tonic-gate 	return (addr);
13617c478bd9Sstevel@tonic-gate }
13627c478bd9Sstevel@tonic-gate 
13636bc8bc6aSSherry Moore void
contig_free(void * addr,size_t size)13647c478bd9Sstevel@tonic-gate contig_free(void *addr, size_t size)
13657c478bd9Sstevel@tonic-gate {
13667c478bd9Sstevel@tonic-gate 	pgcnt_t	pgcnt = btopr(size);
13677c478bd9Sstevel@tonic-gate 	size_t	asize = pgcnt * PAGESIZE;
13687c478bd9Sstevel@tonic-gate 	caddr_t	a, ea;
13697c478bd9Sstevel@tonic-gate 	page_t	*pp;
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 	hat_unload(kas.a_hat, addr, asize, HAT_UNLOAD_UNLOCK);
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 	for (a = addr, ea = a + asize; a < ea; a += PAGESIZE) {
1374843e1988Sjohnlev 		pp = page_find(&kvp, (u_offset_t)(uintptr_t)a);
13757c478bd9Sstevel@tonic-gate 		if (!pp)
13767c478bd9Sstevel@tonic-gate 			panic("contig_free: contig pp not found");
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 		if (!page_tryupgrade(pp)) {
13797c478bd9Sstevel@tonic-gate 			page_unlock(pp);
13807c478bd9Sstevel@tonic-gate 			pp = page_lookup(&kvp,
13817c478bd9Sstevel@tonic-gate 			    (u_offset_t)(uintptr_t)a, SE_EXCL);
13827c478bd9Sstevel@tonic-gate 			if (pp == NULL)
13837c478bd9Sstevel@tonic-gate 				panic("contig_free: page freed");
13847c478bd9Sstevel@tonic-gate 		}
13857c478bd9Sstevel@tonic-gate 		page_destroy(pp, 0);
13867c478bd9Sstevel@tonic-gate 	}
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	page_unresv(pgcnt);
13897c478bd9Sstevel@tonic-gate 	vmem_free(heap_arena, addr, asize);
13907c478bd9Sstevel@tonic-gate }
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate /*
13937c478bd9Sstevel@tonic-gate  * Allocate from the system, aligned on a specific boundary.
13947c478bd9Sstevel@tonic-gate  * The alignment, if non-zero, must be a power of 2.
13957c478bd9Sstevel@tonic-gate  */
13967c478bd9Sstevel@tonic-gate static void *
kalloca(size_t size,size_t align,int cansleep,int physcontig,ddi_dma_attr_t * attr)13977c478bd9Sstevel@tonic-gate kalloca(size_t size, size_t align, int cansleep, int physcontig,
13987c478bd9Sstevel@tonic-gate     ddi_dma_attr_t *attr)
13997c478bd9Sstevel@tonic-gate {
14007c478bd9Sstevel@tonic-gate 	size_t *addr, *raddr, rsize;
14017c478bd9Sstevel@tonic-gate 	size_t hdrsize = 4 * sizeof (size_t);	/* must be power of 2 */
14027c478bd9Sstevel@tonic-gate 	int a, i, c;
1403584b574aSToomas Soome 	vmem_t *vmp = NULL;
14047c478bd9Sstevel@tonic-gate 	kmem_cache_t *cp = NULL;
14057c478bd9Sstevel@tonic-gate 
14068a552b2dScth 	if (attr->dma_attr_addr_lo > mmu_ptob((uint64_t)ddiphysmin))
14078a552b2dScth 		return (NULL);
14088a552b2dScth 
14097c478bd9Sstevel@tonic-gate 	align = MAX(align, hdrsize);
14107c478bd9Sstevel@tonic-gate 	ASSERT((align & (align - 1)) == 0);
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 	/*
14137c478bd9Sstevel@tonic-gate 	 * All of our allocators guarantee 16-byte alignment, so we don't
14147c478bd9Sstevel@tonic-gate 	 * need to reserve additional space for the header.
14157c478bd9Sstevel@tonic-gate 	 * To simplify picking the correct kmem_io_cache, we round up to
14167c478bd9Sstevel@tonic-gate 	 * a multiple of KA_ALIGN.
14177c478bd9Sstevel@tonic-gate 	 */
14187c478bd9Sstevel@tonic-gate 	rsize = P2ROUNDUP_TYPED(size + align, KA_ALIGN, size_t);
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate 	if (physcontig && rsize > PAGESIZE) {
14213df2e8b2SRobert Mustacchi 		if ((addr = contig_alloc(size, attr, align, cansleep)) !=
14223df2e8b2SRobert Mustacchi 		    NULL) {
14237c478bd9Sstevel@tonic-gate 			if (!putctgas(addr, size))
14247c478bd9Sstevel@tonic-gate 				contig_free(addr, size);
14257c478bd9Sstevel@tonic-gate 			else
14267c478bd9Sstevel@tonic-gate 				return (addr);
14277c478bd9Sstevel@tonic-gate 		}
14287c478bd9Sstevel@tonic-gate 		return (NULL);
14297c478bd9Sstevel@tonic-gate 	}
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 	a = kmem_io_index(attr->dma_attr_addr_hi);
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 	if (rsize > PAGESIZE) {
14347c478bd9Sstevel@tonic-gate 		vmp = kmem_io[a].kmem_io_arena;
14357c478bd9Sstevel@tonic-gate 		raddr = vmem_alloc(vmp, rsize,
14367c478bd9Sstevel@tonic-gate 		    (cansleep) ? VM_SLEEP : VM_NOSLEEP);
14377c478bd9Sstevel@tonic-gate 	} else {
14387c478bd9Sstevel@tonic-gate 		c = highbit((rsize >> KA_ALIGN_SHIFT) - 1);
14397c478bd9Sstevel@tonic-gate 		cp = kmem_io[a].kmem_io_cache[c];
14407c478bd9Sstevel@tonic-gate 		raddr = kmem_cache_alloc(cp, (cansleep) ? KM_SLEEP :
14417c478bd9Sstevel@tonic-gate 		    KM_NOSLEEP);
14427c478bd9Sstevel@tonic-gate 	}
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 	if (raddr == NULL) {
14457c478bd9Sstevel@tonic-gate 		int	na;
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate 		ASSERT(cansleep == 0);
14487c478bd9Sstevel@tonic-gate 		if (rsize > PAGESIZE)
14497c478bd9Sstevel@tonic-gate 			return (NULL);
14507c478bd9Sstevel@tonic-gate 		/*
14517c478bd9Sstevel@tonic-gate 		 * System does not have memory in the requested range.
14527c478bd9Sstevel@tonic-gate 		 * Try smaller kmem io ranges and larger cache sizes
14537c478bd9Sstevel@tonic-gate 		 * to see if there might be memory available in
14547c478bd9Sstevel@tonic-gate 		 * these other caches.
14557c478bd9Sstevel@tonic-gate 		 */
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 		for (na = kmem_io_index_next(a); na >= 0;
14587c478bd9Sstevel@tonic-gate 		    na = kmem_io_index_next(na)) {
14597c478bd9Sstevel@tonic-gate 			ASSERT(kmem_io[na].kmem_io_arena);
14607c478bd9Sstevel@tonic-gate 			cp = kmem_io[na].kmem_io_cache[c];
14617c478bd9Sstevel@tonic-gate 			raddr = kmem_cache_alloc(cp, KM_NOSLEEP);
14627c478bd9Sstevel@tonic-gate 			if (raddr)
14637c478bd9Sstevel@tonic-gate 				goto kallocdone;
14647c478bd9Sstevel@tonic-gate 		}
14657c478bd9Sstevel@tonic-gate 		/* now try the larger kmem io cache sizes */
14667c478bd9Sstevel@tonic-gate 		for (na = a; na >= 0; na = kmem_io_index_next(na)) {
14677c478bd9Sstevel@tonic-gate 			for (i = c + 1; i < KA_NCACHE; i++) {
14687c478bd9Sstevel@tonic-gate 				cp = kmem_io[na].kmem_io_cache[i];
14697c478bd9Sstevel@tonic-gate 				raddr = kmem_cache_alloc(cp, KM_NOSLEEP);
14707c478bd9Sstevel@tonic-gate 				if (raddr)
14717c478bd9Sstevel@tonic-gate 					goto kallocdone;
14727c478bd9Sstevel@tonic-gate 			}
14737c478bd9Sstevel@tonic-gate 		}
14747c478bd9Sstevel@tonic-gate 		return (NULL);
14757c478bd9Sstevel@tonic-gate 	}
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate kallocdone:
147888b7b0f2SMatthew Ahrens 	ASSERT(!P2BOUNDARY((uintptr_t)raddr, rsize, PAGESIZE) ||
147988b7b0f2SMatthew Ahrens 	    rsize > PAGESIZE);
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate 	addr = (size_t *)P2ROUNDUP((uintptr_t)raddr + hdrsize, align);
14827c478bd9Sstevel@tonic-gate 	ASSERT((uintptr_t)addr + size - (uintptr_t)raddr <= rsize);
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 	addr[-4] = (size_t)cp;
14857c478bd9Sstevel@tonic-gate 	addr[-3] = (size_t)vmp;
14867c478bd9Sstevel@tonic-gate 	addr[-2] = (size_t)raddr;
14877c478bd9Sstevel@tonic-gate 	addr[-1] = rsize;
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 	return (addr);
14907c478bd9Sstevel@tonic-gate }
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate static void
kfreea(void * addr)14937c478bd9Sstevel@tonic-gate kfreea(void *addr)
14947c478bd9Sstevel@tonic-gate {
14957c478bd9Sstevel@tonic-gate 	size_t		size;
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 	if (!((uintptr_t)addr & PAGEOFFSET) && (size = getctgsz(addr))) {
14987c478bd9Sstevel@tonic-gate 		contig_free(addr, size);
14997c478bd9Sstevel@tonic-gate 	} else {
15007c478bd9Sstevel@tonic-gate 		size_t	*saddr = addr;
15017c478bd9Sstevel@tonic-gate 		if (saddr[-4] == 0)
15027c478bd9Sstevel@tonic-gate 			vmem_free((vmem_t *)saddr[-3], (void *)saddr[-2],
15037c478bd9Sstevel@tonic-gate 			    saddr[-1]);
15047c478bd9Sstevel@tonic-gate 		else
15057c478bd9Sstevel@tonic-gate 			kmem_cache_free((kmem_cache_t *)saddr[-4],
15067c478bd9Sstevel@tonic-gate 			    (void *)saddr[-2]);
15077c478bd9Sstevel@tonic-gate 	}
15087c478bd9Sstevel@tonic-gate }
15097c478bd9Sstevel@tonic-gate 
151083220025Seota /*ARGSUSED*/
15117b93957cSeota void
i_ddi_devacc_to_hatacc(const ddi_device_acc_attr_t * devaccp,uint_t * hataccp)1512*cdd3e9a8SRobert Mustacchi i_ddi_devacc_to_hatacc(const ddi_device_acc_attr_t *devaccp, uint_t *hataccp)
15137b93957cSeota {
15147b93957cSeota }
15157b93957cSeota 
15167b93957cSeota /*
15177b93957cSeota  * Check if the specified cache attribute is supported on the platform.
15187b93957cSeota  * This function must be called before i_ddi_cacheattr_to_hatacc().
15197b93957cSeota  */
15207b93957cSeota boolean_t
i_ddi_check_cache_attr(uint_t flags)15217b93957cSeota i_ddi_check_cache_attr(uint_t flags)
15227b93957cSeota {
15237b93957cSeota 	/*
15247b93957cSeota 	 * The cache attributes are mutually exclusive. Any combination of
15257b93957cSeota 	 * the attributes leads to a failure.
15267b93957cSeota 	 */
15277b93957cSeota 	uint_t cache_attr = IOMEM_CACHE_ATTR(flags);
1528de710d24SJosef 'Jeff' Sipek 	if ((cache_attr != 0) && !ISP2(cache_attr))
15297b93957cSeota 		return (B_FALSE);
15307b93957cSeota 
15317b93957cSeota 	/* All cache attributes are supported on X86/X64 */
15327b93957cSeota 	if (cache_attr & (IOMEM_DATA_UNCACHED | IOMEM_DATA_CACHED |
15337b93957cSeota 	    IOMEM_DATA_UC_WR_COMBINE))
15347b93957cSeota 		return (B_TRUE);
15357b93957cSeota 
15367b93957cSeota 	/* undefined attributes */
15377b93957cSeota 	return (B_FALSE);
15387b93957cSeota }
15397b93957cSeota 
15407b93957cSeota /* set HAT cache attributes from the cache attributes */
15417b93957cSeota void
i_ddi_cacheattr_to_hatacc(uint_t flags,uint_t * hataccp)15427b93957cSeota i_ddi_cacheattr_to_hatacc(uint_t flags, uint_t *hataccp)
15437b93957cSeota {
15447b93957cSeota 	uint_t cache_attr = IOMEM_CACHE_ATTR(flags);
15457b93957cSeota 	static char *fname = "i_ddi_cacheattr_to_hatacc";
15467b93957cSeota 
15477b93957cSeota 	/*
15487b93957cSeota 	 * If write-combining is not supported, then it falls back
15497b93957cSeota 	 * to uncacheable.
15507b93957cSeota 	 */
15517417cfdeSKuriakose Kuruvilla 	if (cache_attr == IOMEM_DATA_UC_WR_COMBINE &&
15527417cfdeSKuriakose Kuruvilla 	    !is_x86_feature(x86_featureset, X86FSET_PAT))
15537b93957cSeota 		cache_attr = IOMEM_DATA_UNCACHED;
15547b93957cSeota 
15557b93957cSeota 	/*
15567b93957cSeota 	 * set HAT attrs according to the cache attrs.
15577b93957cSeota 	 */
15587b93957cSeota 	switch (cache_attr) {
15597b93957cSeota 	case IOMEM_DATA_UNCACHED:
15607b93957cSeota 		*hataccp &= ~HAT_ORDER_MASK;
15617b93957cSeota 		*hataccp |= (HAT_STRICTORDER | HAT_PLAT_NOCACHE);
15627b93957cSeota 		break;
15637b93957cSeota 	case IOMEM_DATA_UC_WR_COMBINE:
15647b93957cSeota 		*hataccp &= ~HAT_ORDER_MASK;
15657b93957cSeota 		*hataccp |= (HAT_MERGING_OK | HAT_PLAT_NOCACHE);
15667b93957cSeota 		break;
15677b93957cSeota 	case IOMEM_DATA_CACHED:
15687b93957cSeota 		*hataccp &= ~HAT_ORDER_MASK;
15697b93957cSeota 		*hataccp |= HAT_UNORDERED_OK;
15707b93957cSeota 		break;
15717b93957cSeota 	/*
15727b93957cSeota 	 * This case must not occur because the cache attribute is scrutinized
15737b93957cSeota 	 * before this function is called.
15747b93957cSeota 	 */
15757b93957cSeota 	default:
15767b93957cSeota 		/*
15777b93957cSeota 		 * set cacheable to hat attrs.
15787b93957cSeota 		 */
15797b93957cSeota 		*hataccp &= ~HAT_ORDER_MASK;
15807b93957cSeota 		*hataccp |= HAT_UNORDERED_OK;
15817b93957cSeota 		cmn_err(CE_WARN, "%s: cache_attr=0x%x is ignored.",
15827b93957cSeota 		    fname, cache_attr);
15837b93957cSeota 	}
15847b93957cSeota }
15857b93957cSeota 
15867b93957cSeota /*
15877c478bd9Sstevel@tonic-gate  * This should actually be called i_ddi_dma_mem_alloc. There should
15887c478bd9Sstevel@tonic-gate  * also be an i_ddi_pio_mem_alloc. i_ddi_dma_mem_alloc should call
15897c478bd9Sstevel@tonic-gate  * through the device tree with the DDI_CTLOPS_DMA_ALIGN ctl ops to
15907c478bd9Sstevel@tonic-gate  * get alignment requirements for DMA memory. i_ddi_pio_mem_alloc
15917c478bd9Sstevel@tonic-gate  * should use DDI_CTLOPS_PIO_ALIGN. Since we only have i_ddi_mem_alloc
15927c478bd9Sstevel@tonic-gate  * so far which is used for both, DMA and PIO, we have to use the DMA
15937c478bd9Sstevel@tonic-gate  * ctl ops to make everybody happy.
15947c478bd9Sstevel@tonic-gate  */
15957c478bd9Sstevel@tonic-gate /*ARGSUSED*/
15967c478bd9Sstevel@tonic-gate int
i_ddi_mem_alloc(dev_info_t * dip,ddi_dma_attr_t * attr,size_t length,int cansleep,int flags,const ddi_device_acc_attr_t * accattrp,caddr_t * kaddrp,size_t * real_length,ddi_acc_hdl_t * ap)15977c478bd9Sstevel@tonic-gate i_ddi_mem_alloc(dev_info_t *dip, ddi_dma_attr_t *attr,
15987b93957cSeota     size_t length, int cansleep, int flags,
1599*cdd3e9a8SRobert Mustacchi     const ddi_device_acc_attr_t *accattrp, caddr_t *kaddrp,
16007c478bd9Sstevel@tonic-gate     size_t *real_length, ddi_acc_hdl_t *ap)
16017c478bd9Sstevel@tonic-gate {
16027c478bd9Sstevel@tonic-gate 	caddr_t a;
16037c478bd9Sstevel@tonic-gate 	int iomin;
16047c478bd9Sstevel@tonic-gate 	ddi_acc_impl_t *iap;
16057c478bd9Sstevel@tonic-gate 	int physcontig = 0;
16067c478bd9Sstevel@tonic-gate 	pgcnt_t npages;
16077c478bd9Sstevel@tonic-gate 	pgcnt_t minctg;
16087b93957cSeota 	uint_t order;
16097b93957cSeota 	int e;
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 	/*
16127c478bd9Sstevel@tonic-gate 	 * Check legality of arguments
16137c478bd9Sstevel@tonic-gate 	 */
16147c478bd9Sstevel@tonic-gate 	if (length == 0 || kaddrp == NULL || attr == NULL) {
16157c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
16167c478bd9Sstevel@tonic-gate 	}
16177b93957cSeota 
16187c478bd9Sstevel@tonic-gate 	if (attr->dma_attr_minxfer == 0 || attr->dma_attr_align == 0 ||
1619de710d24SJosef 'Jeff' Sipek 	    !ISP2(attr->dma_attr_align) || !ISP2(attr->dma_attr_minxfer)) {
16207c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
16217c478bd9Sstevel@tonic-gate 	}
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 	/*
16247c478bd9Sstevel@tonic-gate 	 * figure out most restrictive alignment requirement
16257c478bd9Sstevel@tonic-gate 	 */
16267c478bd9Sstevel@tonic-gate 	iomin = attr->dma_attr_minxfer;
16277c478bd9Sstevel@tonic-gate 	iomin = maxbit(iomin, attr->dma_attr_align);
16287c478bd9Sstevel@tonic-gate 	if (iomin == 0)
16297c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 	ASSERT((iomin & (iomin - 1)) == 0);
16327c478bd9Sstevel@tonic-gate 
16337b93957cSeota 	/*
16347b93957cSeota 	 * if we allocate memory with IOMEM_DATA_UNCACHED or
16357b93957cSeota 	 * IOMEM_DATA_UC_WR_COMBINE, make sure we allocate a page aligned
16367b93957cSeota 	 * memory that ends on a page boundry.
16377b93957cSeota 	 * Don't want to have to different cache mappings to the same
16387b93957cSeota 	 * physical page.
16397b93957cSeota 	 */
16407b93957cSeota 	if (OVERRIDE_CACHE_ATTR(flags)) {
16417b93957cSeota 		iomin = (iomin + MMU_PAGEOFFSET) & MMU_PAGEMASK;
16427b93957cSeota 		length = (length + MMU_PAGEOFFSET) & (size_t)MMU_PAGEMASK;
16437b93957cSeota 	}
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 	/*
16467c478bd9Sstevel@tonic-gate 	 * Determine if we need to satisfy the request for physically
16477c478bd9Sstevel@tonic-gate 	 * contiguous memory or alignments larger than pagesize.
16487c478bd9Sstevel@tonic-gate 	 */
16497c478bd9Sstevel@tonic-gate 	npages = btopr(length + attr->dma_attr_align);
16507c478bd9Sstevel@tonic-gate 	minctg = howmany(npages, attr->dma_attr_sgllen);
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 	if (minctg > 1) {
16537c478bd9Sstevel@tonic-gate 		uint64_t pfnseg = attr->dma_attr_seg >> PAGESHIFT;
16547c478bd9Sstevel@tonic-gate 		/*
16557c478bd9Sstevel@tonic-gate 		 * verify that the minimum contig requirement for the
16567c478bd9Sstevel@tonic-gate 		 * actual length does not cross segment boundary.
16577c478bd9Sstevel@tonic-gate 		 */
16587c478bd9Sstevel@tonic-gate 		length = P2ROUNDUP_TYPED(length, attr->dma_attr_minxfer,
16597c478bd9Sstevel@tonic-gate 		    size_t);
16607c478bd9Sstevel@tonic-gate 		npages = btopr(length);
16617c478bd9Sstevel@tonic-gate 		minctg = howmany(npages, attr->dma_attr_sgllen);
16627c478bd9Sstevel@tonic-gate 		if (minctg > pfnseg + 1)
16637c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
16647c478bd9Sstevel@tonic-gate 		physcontig = 1;
16657c478bd9Sstevel@tonic-gate 	} else {
16667c478bd9Sstevel@tonic-gate 		length = P2ROUNDUP_TYPED(length, iomin, size_t);
16677c478bd9Sstevel@tonic-gate 	}
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 	/*
16707c478bd9Sstevel@tonic-gate 	 * Allocate the requested amount from the system.
16717c478bd9Sstevel@tonic-gate 	 */
16727c478bd9Sstevel@tonic-gate 	a = kalloca(length, iomin, cansleep, physcontig, attr);
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	if ((*kaddrp = a) == NULL)
16757c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
16767c478bd9Sstevel@tonic-gate 
16777b93957cSeota 	/*
16787b93957cSeota 	 * if we to modify the cache attributes, go back and muck with the
16797b93957cSeota 	 * mappings.
16807b93957cSeota 	 */
16817b93957cSeota 	if (OVERRIDE_CACHE_ATTR(flags)) {
16827b93957cSeota 		order = 0;
16837b93957cSeota 		i_ddi_cacheattr_to_hatacc(flags, &order);
16847b93957cSeota 		e = kmem_override_cache_attrs(a, length, order);
16857b93957cSeota 		if (e != 0) {
16867b93957cSeota 			kfreea(a);
16877b93957cSeota 			return (DDI_FAILURE);
16887b93957cSeota 		}
16897b93957cSeota 	}
16907b93957cSeota 
16917c478bd9Sstevel@tonic-gate 	if (real_length) {
16927c478bd9Sstevel@tonic-gate 		*real_length = length;
16937c478bd9Sstevel@tonic-gate 	}
16947c478bd9Sstevel@tonic-gate 	if (ap) {
16957c478bd9Sstevel@tonic-gate 		/*
16967c478bd9Sstevel@tonic-gate 		 * initialize access handle
16977c478bd9Sstevel@tonic-gate 		 */
16987c478bd9Sstevel@tonic-gate 		iap = (ddi_acc_impl_t *)ap->ah_platform_private;
16997c478bd9Sstevel@tonic-gate 		iap->ahi_acc_attr |= DDI_ACCATTR_CPU_VADDR;
17007c478bd9Sstevel@tonic-gate 		impl_acc_hdl_init(ap);
17017c478bd9Sstevel@tonic-gate 	}
17027b93957cSeota 
17037c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
17047c478bd9Sstevel@tonic-gate }
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate /* ARGSUSED */
17077c478bd9Sstevel@tonic-gate void
i_ddi_mem_free(caddr_t kaddr,ddi_acc_hdl_t * ap)17087b93957cSeota i_ddi_mem_free(caddr_t kaddr, ddi_acc_hdl_t *ap)
17097c478bd9Sstevel@tonic-gate {
17107b93957cSeota 	if (ap != NULL) {
17117b93957cSeota 		/*
17127b93957cSeota 		 * if we modified the cache attributes on alloc, go back and
17137b93957cSeota 		 * fix them since this memory could be returned to the
17147b93957cSeota 		 * general pool.
17157b93957cSeota 		 */
17167b93957cSeota 		if (OVERRIDE_CACHE_ATTR(ap->ah_xfermodes)) {
17177b93957cSeota 			uint_t order = 0;
17187b93957cSeota 			int e;
17197b93957cSeota 			i_ddi_cacheattr_to_hatacc(IOMEM_DATA_CACHED, &order);
17207b93957cSeota 			e = kmem_override_cache_attrs(kaddr, ap->ah_len, order);
17217b93957cSeota 			if (e != 0) {
17227b93957cSeota 				cmn_err(CE_WARN, "i_ddi_mem_free() failed to "
17237b93957cSeota 				    "override cache attrs, memory leaked\n");
17247b93957cSeota 				return;
17257b93957cSeota 			}
17267b93957cSeota 		}
17277b93957cSeota 	}
17287c478bd9Sstevel@tonic-gate 	kfreea(kaddr);
17297c478bd9Sstevel@tonic-gate }
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate /*
17327c478bd9Sstevel@tonic-gate  * Access Barriers
17337c478bd9Sstevel@tonic-gate  *
17347c478bd9Sstevel@tonic-gate  */
17357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17367c478bd9Sstevel@tonic-gate int
i_ddi_ontrap(ddi_acc_handle_t hp)17377c478bd9Sstevel@tonic-gate i_ddi_ontrap(ddi_acc_handle_t hp)
17387c478bd9Sstevel@tonic-gate {
17397c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
17407c478bd9Sstevel@tonic-gate }
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17437c478bd9Sstevel@tonic-gate void
i_ddi_notrap(ddi_acc_handle_t hp)17447c478bd9Sstevel@tonic-gate i_ddi_notrap(ddi_acc_handle_t hp)
17457c478bd9Sstevel@tonic-gate {
17467c478bd9Sstevel@tonic-gate }
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate /*
17507c478bd9Sstevel@tonic-gate  * Misc Functions
17517c478bd9Sstevel@tonic-gate  */
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate /*
17547c478bd9Sstevel@tonic-gate  * Implementation instance override functions
17557c478bd9Sstevel@tonic-gate  *
17567c478bd9Sstevel@tonic-gate  * No override on i86pc
17577c478bd9Sstevel@tonic-gate  */
17587c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17597c478bd9Sstevel@tonic-gate uint_t
impl_assign_instance(dev_info_t * dip)17607c478bd9Sstevel@tonic-gate impl_assign_instance(dev_info_t *dip)
17617c478bd9Sstevel@tonic-gate {
17627c478bd9Sstevel@tonic-gate 	return ((uint_t)-1);
17637c478bd9Sstevel@tonic-gate }
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17667c478bd9Sstevel@tonic-gate int
impl_keep_instance(dev_info_t * dip)17677c478bd9Sstevel@tonic-gate impl_keep_instance(dev_info_t *dip)
17687c478bd9Sstevel@tonic-gate {
1769843e1988Sjohnlev 
1770843e1988Sjohnlev #if defined(__xpv)
1771843e1988Sjohnlev 	/*
1772843e1988Sjohnlev 	 * Do not persist instance numbers assigned to devices in dom0
1773843e1988Sjohnlev 	 */
1774843e1988Sjohnlev 	dev_info_t *pdip;
1775843e1988Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
1776843e1988Sjohnlev 		if (((pdip = ddi_get_parent(dip)) != NULL) &&
1777843e1988Sjohnlev 		    (strcmp(ddi_get_name(pdip), "xpvd") == 0))
1778843e1988Sjohnlev 			return (DDI_SUCCESS);
1779843e1988Sjohnlev 	}
1780843e1988Sjohnlev #endif
17817c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
17827c478bd9Sstevel@tonic-gate }
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17857c478bd9Sstevel@tonic-gate int
impl_free_instance(dev_info_t * dip)17867c478bd9Sstevel@tonic-gate impl_free_instance(dev_info_t *dip)
17877c478bd9Sstevel@tonic-gate {
17887c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
17897c478bd9Sstevel@tonic-gate }
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17927c478bd9Sstevel@tonic-gate int
impl_check_cpu(dev_info_t * devi)17937c478bd9Sstevel@tonic-gate impl_check_cpu(dev_info_t *devi)
17947c478bd9Sstevel@tonic-gate {
17957c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
17967c478bd9Sstevel@tonic-gate }
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate /*
17997c478bd9Sstevel@tonic-gate  * Referenced in common/cpr_driver.c: Power off machine.
18007c478bd9Sstevel@tonic-gate  * Don't know how to power off i86pc.
18017c478bd9Sstevel@tonic-gate  */
18027c478bd9Sstevel@tonic-gate void
arch_power_down()18037c478bd9Sstevel@tonic-gate arch_power_down()
18047c478bd9Sstevel@tonic-gate {}
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate /*
18077c478bd9Sstevel@tonic-gate  * Copy name to property_name, since name
18087c478bd9Sstevel@tonic-gate  * is in the low address range below kernelbase.
18097c478bd9Sstevel@tonic-gate  */
18107c478bd9Sstevel@tonic-gate static void
copy_boot_str(const char * boot_str,char * kern_str,int len)18117c478bd9Sstevel@tonic-gate copy_boot_str(const char *boot_str, char *kern_str, int len)
18127c478bd9Sstevel@tonic-gate {
18137c478bd9Sstevel@tonic-gate 	int i = 0;
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 	while (i < len - 1 && boot_str[i] != '\0') {
18167c478bd9Sstevel@tonic-gate 		kern_str[i] = boot_str[i];
18177c478bd9Sstevel@tonic-gate 		i++;
18187c478bd9Sstevel@tonic-gate 	}
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 	kern_str[i] = 0;	/* null terminate */
18217c478bd9Sstevel@tonic-gate 	if (boot_str[i] != '\0')
18227c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
18237c478bd9Sstevel@tonic-gate 		    "boot property string is truncated to %s", kern_str);
18247c478bd9Sstevel@tonic-gate }
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate static void
get_boot_properties(void)18277c478bd9Sstevel@tonic-gate get_boot_properties(void)
18287c478bd9Sstevel@tonic-gate {
18297c478bd9Sstevel@tonic-gate 	extern char hw_provider[];
18307c478bd9Sstevel@tonic-gate 	dev_info_t *devi;
18317c478bd9Sstevel@tonic-gate 	char *name;
1832dd891561SToomas Soome 	int length, flags;
18337c478bd9Sstevel@tonic-gate 	char property_name[50], property_val[50];
18347c478bd9Sstevel@tonic-gate 	void *bop_staging_area;
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate 	bop_staging_area = kmem_zalloc(MMU_PAGESIZE, KM_NOSLEEP);
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 	/*
18397c478bd9Sstevel@tonic-gate 	 * Import "root" properties from the boot.
18407c478bd9Sstevel@tonic-gate 	 *
18417c478bd9Sstevel@tonic-gate 	 * We do this by invoking BOP_NEXTPROP until the list
18427c478bd9Sstevel@tonic-gate 	 * is completely copied in.
18437c478bd9Sstevel@tonic-gate 	 */
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	devi = ddi_root_node();
18467c478bd9Sstevel@tonic-gate 	for (name = BOP_NEXTPROP(bootops, "");		/* get first */
18477c478bd9Sstevel@tonic-gate 	    name;					/* NULL => DONE */
18487c478bd9Sstevel@tonic-gate 	    name = BOP_NEXTPROP(bootops, name)) {	/* get next */
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 		/* copy string to memory above kernelbase */
18517c478bd9Sstevel@tonic-gate 		copy_boot_str(name, property_name, 50);
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 		/*
18547c478bd9Sstevel@tonic-gate 		 * Skip vga properties. They will be picked up later
18557c478bd9Sstevel@tonic-gate 		 * by get_vga_properties.
18567c478bd9Sstevel@tonic-gate 		 */
18577c478bd9Sstevel@tonic-gate 		if (strcmp(property_name, "display-edif-block") == 0 ||
18587c478bd9Sstevel@tonic-gate 		    strcmp(property_name, "display-edif-id") == 0) {
18597c478bd9Sstevel@tonic-gate 			continue;
18607c478bd9Sstevel@tonic-gate 		}
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 		length = BOP_GETPROPLEN(bootops, property_name);
1863dd891561SToomas Soome 		if (length < 0)
18647c478bd9Sstevel@tonic-gate 			continue;
18657c478bd9Sstevel@tonic-gate 		if (length > MMU_PAGESIZE) {
18667c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE,
18677c478bd9Sstevel@tonic-gate 			    "boot property %s longer than 0x%x, ignored\n",
18687c478bd9Sstevel@tonic-gate 			    property_name, MMU_PAGESIZE);
18697c478bd9Sstevel@tonic-gate 			continue;
18707c478bd9Sstevel@tonic-gate 		}
18717c478bd9Sstevel@tonic-gate 		BOP_GETPROP(bootops, property_name, bop_staging_area);
1872dd891561SToomas Soome 		flags = do_bsys_getproptype(bootops, property_name);
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 		/*
18757c478bd9Sstevel@tonic-gate 		 * special properties:
18767c478bd9Sstevel@tonic-gate 		 * si-machine, si-hw-provider
18777c478bd9Sstevel@tonic-gate 		 *	goes to kernel data structures.
18787c478bd9Sstevel@tonic-gate 		 * bios-boot-device and stdout
18797c478bd9Sstevel@tonic-gate 		 *	goes to hardware property list so it may show up
18807c478bd9Sstevel@tonic-gate 		 *	in the prtconf -vp output. This is needed by
18817c478bd9Sstevel@tonic-gate 		 *	Install/Upgrade. Once we fix install upgrade,
18827c478bd9Sstevel@tonic-gate 		 *	this can be taken out.
18837c478bd9Sstevel@tonic-gate 		 */
18847c478bd9Sstevel@tonic-gate 		if (strcmp(name, "si-machine") == 0) {
18857c478bd9Sstevel@tonic-gate 			(void) strncpy(utsname.machine, bop_staging_area,
18867c478bd9Sstevel@tonic-gate 			    SYS_NMLN);
1887dd891561SToomas Soome 			utsname.machine[SYS_NMLN - 1] = '\0';
1888dd891561SToomas Soome 			continue;
1889dd891561SToomas Soome 		}
1890dd891561SToomas Soome 		if (strcmp(name, "si-hw-provider") == 0) {
18917c478bd9Sstevel@tonic-gate 			(void) strncpy(hw_provider, bop_staging_area, SYS_NMLN);
1892dd891561SToomas Soome 			hw_provider[SYS_NMLN - 1] = '\0';
1893dd891561SToomas Soome 			continue;
1894dd891561SToomas Soome 		}
1895dd891561SToomas Soome 		if (strcmp(name, "bios-boot-device") == 0) {
18967c478bd9Sstevel@tonic-gate 			copy_boot_str(bop_staging_area, property_val, 50);
18977c478bd9Sstevel@tonic-gate 			(void) ndi_prop_update_string(DDI_DEV_T_NONE, devi,
18987c478bd9Sstevel@tonic-gate 			    property_name, property_val);
1899dd891561SToomas Soome 			continue;
1900dd891561SToomas Soome 		}
1901dd891561SToomas Soome 		if (strcmp(name, "stdout") == 0) {
19027c478bd9Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, devi,
19037c478bd9Sstevel@tonic-gate 			    property_name, *((int *)bop_staging_area));
1904dd891561SToomas Soome 			continue;
1905dd891561SToomas Soome 		}
1906dd891561SToomas Soome 
1907dd891561SToomas Soome 		/* Boolean property */
1908dd891561SToomas Soome 		if (length == 0) {
1909dd891561SToomas Soome 			(void) e_ddi_prop_create(DDI_DEV_T_NONE, devi,
1910dd891561SToomas Soome 			    DDI_PROP_CANSLEEP, property_name, NULL, 0);
1911dd891561SToomas Soome 			continue;
1912dd891561SToomas Soome 		}
1913dd891561SToomas Soome 
1914dd891561SToomas Soome 		/* Now anything else based on type. */
1915dd891561SToomas Soome 		switch (flags) {
1916dd891561SToomas Soome 		case DDI_PROP_TYPE_INT:
1917dd891561SToomas Soome 			if (length == sizeof (int)) {
1918dd891561SToomas Soome 				(void) e_ddi_prop_update_int(DDI_DEV_T_NONE,
1919dd891561SToomas Soome 				    devi, property_name,
1920dd891561SToomas Soome 				    *((int *)bop_staging_area));
1921dd891561SToomas Soome 			} else {
1922dd891561SToomas Soome 				(void) e_ddi_prop_update_int_array(
1923dd891561SToomas Soome 				    DDI_DEV_T_NONE, devi, property_name,
1924dd891561SToomas Soome 				    bop_staging_area, length / sizeof (int));
1925dd891561SToomas Soome 			}
1926dd891561SToomas Soome 			break;
1927dd891561SToomas Soome 		case DDI_PROP_TYPE_STRING:
1928d98490e4SToomas Soome 			(void) e_ddi_prop_update_string(DDI_DEV_T_NONE, devi,
1929dd891561SToomas Soome 			    property_name, bop_staging_area);
1930dd891561SToomas Soome 			break;
1931dd891561SToomas Soome 		case DDI_PROP_TYPE_BYTE:
1932d98490e4SToomas Soome 			(void) e_ddi_prop_update_byte_array(DDI_DEV_T_NONE,
1933d98490e4SToomas Soome 			    devi, property_name, bop_staging_area, length);
1934dd891561SToomas Soome 			break;
1935dd891561SToomas Soome 		case DDI_PROP_TYPE_INT64:
1936dd891561SToomas Soome 			if (length == sizeof (int64_t)) {
1937dd891561SToomas Soome 				(void) e_ddi_prop_update_int64(DDI_DEV_T_NONE,
1938dd891561SToomas Soome 				    devi, property_name,
1939dd891561SToomas Soome 				    *((int64_t *)bop_staging_area));
19407c478bd9Sstevel@tonic-gate 			} else {
1941dd891561SToomas Soome 				(void) e_ddi_prop_update_int64_array(
1942dd891561SToomas Soome 				    DDI_DEV_T_NONE, devi, property_name,
1943dd891561SToomas Soome 				    bop_staging_area,
1944dd891561SToomas Soome 				    length / sizeof (int64_t));
1945dd891561SToomas Soome 			}
1946dd891561SToomas Soome 			break;
1947dd891561SToomas Soome 		default:
19487c478bd9Sstevel@tonic-gate 			/* Property type unknown, use old prop interface */
19497c478bd9Sstevel@tonic-gate 			(void) e_ddi_prop_create(DDI_DEV_T_NONE, devi,
19507c478bd9Sstevel@tonic-gate 			    DDI_PROP_CANSLEEP, property_name, bop_staging_area,
19517c478bd9Sstevel@tonic-gate 			    length);
19527c478bd9Sstevel@tonic-gate 		}
19537c478bd9Sstevel@tonic-gate 	}
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 	kmem_free(bop_staging_area, MMU_PAGESIZE);
19567c478bd9Sstevel@tonic-gate }
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate static void
get_vga_properties(void)19597c478bd9Sstevel@tonic-gate get_vga_properties(void)
19607c478bd9Sstevel@tonic-gate {
19617c478bd9Sstevel@tonic-gate 	dev_info_t *devi;
19627c478bd9Sstevel@tonic-gate 	major_t major;
19637c478bd9Sstevel@tonic-gate 	char *name;
19647c478bd9Sstevel@tonic-gate 	int length;
19657c478bd9Sstevel@tonic-gate 	char property_val[50];
19667c478bd9Sstevel@tonic-gate 	void *bop_staging_area;
19677c478bd9Sstevel@tonic-gate 
19682df1fe9cSrandyf 	/*
19692df1fe9cSrandyf 	 * XXXX Hack Allert!
19702df1fe9cSrandyf 	 * There really needs to be a better way for identifying various
19712df1fe9cSrandyf 	 * console framebuffers and their related issues.  Till then,
19722df1fe9cSrandyf 	 * check for this one as a replacement to vgatext.
19732df1fe9cSrandyf 	 */
19742df1fe9cSrandyf 	major = ddi_name_to_major("ragexl");
19752df1fe9cSrandyf 	if (major == (major_t)-1) {
19767c478bd9Sstevel@tonic-gate 		major = ddi_name_to_major("vgatext");
19777c478bd9Sstevel@tonic-gate 		if (major == (major_t)-1)
19787c478bd9Sstevel@tonic-gate 			return;
19792df1fe9cSrandyf 	}
19807c478bd9Sstevel@tonic-gate 	devi = devnamesp[major].dn_head;
19817c478bd9Sstevel@tonic-gate 	if (devi == NULL)
19827c478bd9Sstevel@tonic-gate 		return;
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate 	bop_staging_area = kmem_zalloc(MMU_PAGESIZE, KM_SLEEP);
19857c478bd9Sstevel@tonic-gate 
19867c478bd9Sstevel@tonic-gate 	/*
19877c478bd9Sstevel@tonic-gate 	 * Import "vga" properties from the boot.
19887c478bd9Sstevel@tonic-gate 	 */
19897c478bd9Sstevel@tonic-gate 	name = "display-edif-block";
19907c478bd9Sstevel@tonic-gate 	length = BOP_GETPROPLEN(bootops, name);
19917c478bd9Sstevel@tonic-gate 	if (length > 0 && length < MMU_PAGESIZE) {
19927c478bd9Sstevel@tonic-gate 		BOP_GETPROP(bootops, name, bop_staging_area);
19937c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_byte_array(DDI_DEV_T_NONE,
19947c478bd9Sstevel@tonic-gate 		    devi, name, bop_staging_area, length);
19957c478bd9Sstevel@tonic-gate 	}
19967c478bd9Sstevel@tonic-gate 
19977c478bd9Sstevel@tonic-gate 	/*
19987c478bd9Sstevel@tonic-gate 	 * kdmconfig is also looking for display-type and
19997c478bd9Sstevel@tonic-gate 	 * video-adapter-type. We default to color and svga.
20007c478bd9Sstevel@tonic-gate 	 *
20017c478bd9Sstevel@tonic-gate 	 * Could it be "monochrome", "vga"?
20027c478bd9Sstevel@tonic-gate 	 * Nah, you've got to come to the 21st century...
20037c478bd9Sstevel@tonic-gate 	 * And you can set monitor type manually in kdmconfig
20047c478bd9Sstevel@tonic-gate 	 * if you are really an old junky.
20057c478bd9Sstevel@tonic-gate 	 */
20067c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_string(DDI_DEV_T_NONE,
20077c478bd9Sstevel@tonic-gate 	    devi, "display-type", "color");
20087c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_string(DDI_DEV_T_NONE,
20097c478bd9Sstevel@tonic-gate 	    devi, "video-adapter-type", "svga");
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate 	name = "display-edif-id";
20127c478bd9Sstevel@tonic-gate 	length = BOP_GETPROPLEN(bootops, name);
20137c478bd9Sstevel@tonic-gate 	if (length > 0 && length < MMU_PAGESIZE) {
20147c478bd9Sstevel@tonic-gate 		BOP_GETPROP(bootops, name, bop_staging_area);
20157c478bd9Sstevel@tonic-gate 		copy_boot_str(bop_staging_area, property_val, length);
20167c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_string(DDI_DEV_T_NONE,
20177c478bd9Sstevel@tonic-gate 		    devi, name, property_val);
20187c478bd9Sstevel@tonic-gate 	}
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate 	kmem_free(bop_staging_area, MMU_PAGESIZE);
20217c478bd9Sstevel@tonic-gate }
20227c478bd9Sstevel@tonic-gate 
2023cbc8e155SToomas Soome /*
2024cbc8e155SToomas Soome  * Copy console font to kernel memory. The temporary font setup
2025cbc8e155SToomas Soome  * to use font module was done in early console setup, using low
2026cbc8e155SToomas Soome  * memory and data from font module. Now we need to allocate
2027cbc8e155SToomas Soome  * kernel memory and copy data over, so the low memory can be freed.
2028cbc8e155SToomas Soome  * We can have at most one entry in font list from early boot.
2029cbc8e155SToomas Soome  */
2030cbc8e155SToomas Soome static void
get_console_font(void)2031cbc8e155SToomas Soome get_console_font(void)
2032cbc8e155SToomas Soome {
2033cbc8e155SToomas Soome 	struct fontlist *fp, *fl;
2034cbc8e155SToomas Soome 	bitmap_data_t *bd;
2035cbc8e155SToomas Soome 	struct font *fd, *tmp;
2036cbc8e155SToomas Soome 	int i;
2037cbc8e155SToomas Soome 
2038cbc8e155SToomas Soome 	if (STAILQ_EMPTY(&fonts))
2039cbc8e155SToomas Soome 		return;
2040cbc8e155SToomas Soome 
2041cbc8e155SToomas Soome 	fl = STAILQ_FIRST(&fonts);
2042cbc8e155SToomas Soome 	STAILQ_REMOVE_HEAD(&fonts, font_next);
2043cbc8e155SToomas Soome 	fp = kmem_zalloc(sizeof (*fp), KM_SLEEP);
2044cbc8e155SToomas Soome 	bd = kmem_zalloc(sizeof (*bd), KM_SLEEP);
2045cbc8e155SToomas Soome 	fd = kmem_zalloc(sizeof (*fd), KM_SLEEP);
2046cbc8e155SToomas Soome 
2047cbc8e155SToomas Soome 	fp->font_name = NULL;
2048cbc8e155SToomas Soome 	fp->font_flags = FONT_BOOT;
2049cbc8e155SToomas Soome 	fp->font_data = bd;
2050cbc8e155SToomas Soome 
2051cbc8e155SToomas Soome 	bd->width = fl->font_data->width;
2052cbc8e155SToomas Soome 	bd->height = fl->font_data->height;
2053cbc8e155SToomas Soome 	bd->uncompressed_size = fl->font_data->uncompressed_size;
2054cbc8e155SToomas Soome 	bd->font = fd;
2055cbc8e155SToomas Soome 
2056cbc8e155SToomas Soome 	tmp = fl->font_data->font;
2057cbc8e155SToomas Soome 	fd->vf_width = tmp->vf_width;
2058cbc8e155SToomas Soome 	fd->vf_height = tmp->vf_height;
2059cbc8e155SToomas Soome 	for (i = 0; i < VFNT_MAPS; i++) {
2060cbc8e155SToomas Soome 		if (tmp->vf_map_count[i] == 0)
2061cbc8e155SToomas Soome 			continue;
2062cbc8e155SToomas Soome 		fd->vf_map_count[i] = tmp->vf_map_count[i];
2063cbc8e155SToomas Soome 		fd->vf_map[i] = kmem_alloc(fd->vf_map_count[i] *
2064cbc8e155SToomas Soome 		    sizeof (*fd->vf_map[i]), KM_SLEEP);
2065cbc8e155SToomas Soome 		bcopy(tmp->vf_map[i], fd->vf_map[i], fd->vf_map_count[i] *
2066cbc8e155SToomas Soome 		    sizeof (*fd->vf_map[i]));
2067cbc8e155SToomas Soome 	}
2068cbc8e155SToomas Soome 	fd->vf_bytes = kmem_alloc(bd->uncompressed_size, KM_SLEEP);
2069cbc8e155SToomas Soome 	bcopy(tmp->vf_bytes, fd->vf_bytes, bd->uncompressed_size);
2070cbc8e155SToomas Soome 	STAILQ_INSERT_HEAD(&fonts, fp, font_next);
2071cbc8e155SToomas Soome }
20727c478bd9Sstevel@tonic-gate 
20737c478bd9Sstevel@tonic-gate /*
20747c478bd9Sstevel@tonic-gate  * This is temporary, but absolutely necessary.  If we are being
20757c478bd9Sstevel@tonic-gate  * booted with a device tree created by the DevConf project's bootconf
20767c478bd9Sstevel@tonic-gate  * program, then we have device information nodes that reflect
20777c478bd9Sstevel@tonic-gate  * reality.  At this point in time in the Solaris release schedule, the
20787c478bd9Sstevel@tonic-gate  * kernel drivers aren't prepared for reality.  They still depend on their
20797c478bd9Sstevel@tonic-gate  * own ad-hoc interpretations of the properties created when their .conf
20807c478bd9Sstevel@tonic-gate  * files were interpreted. These drivers use an "ignore-hardware-nodes"
20817c478bd9Sstevel@tonic-gate  * property to prevent them from using the nodes passed up from the bootconf
20827c478bd9Sstevel@tonic-gate  * device tree.
20837c478bd9Sstevel@tonic-gate  *
20847c478bd9Sstevel@tonic-gate  * Trying to assemble root file system drivers as we are booting from
20857c478bd9Sstevel@tonic-gate  * devconf will fail if the kernel driver is basing its name_addr's on the
20867c478bd9Sstevel@tonic-gate  * psuedo-node device info while the bootpath passed up from bootconf is using
20877c478bd9Sstevel@tonic-gate  * reality-based name_addrs.  We help the boot along in this case by
20887c478bd9Sstevel@tonic-gate  * looking at the pre-bootconf bootpath and determining if we would have
20897c478bd9Sstevel@tonic-gate  * successfully matched if that had been the bootpath we had chosen.
20907c478bd9Sstevel@tonic-gate  *
20917c478bd9Sstevel@tonic-gate  * Note that we only even perform this extra check if we've booted
20927c478bd9Sstevel@tonic-gate  * using bootconf's 1275 compliant bootpath, this is the boot device, and
20937c478bd9Sstevel@tonic-gate  * we're trying to match the name_addr specified in the 1275 bootpath.
20947c478bd9Sstevel@tonic-gate  */
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate #define	MAXCOMPONENTLEN	32
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate int
x86_old_bootpath_name_addr_match(dev_info_t * cdip,char * caddr,char * naddr)20997c478bd9Sstevel@tonic-gate x86_old_bootpath_name_addr_match(dev_info_t *cdip, char *caddr, char *naddr)
21007c478bd9Sstevel@tonic-gate {
21017c478bd9Sstevel@tonic-gate 	/*
21027c478bd9Sstevel@tonic-gate 	 *  There are multiple criteria to be met before we can even
21037c478bd9Sstevel@tonic-gate 	 *  consider allowing a name_addr match here.
21047c478bd9Sstevel@tonic-gate 	 *
21057c478bd9Sstevel@tonic-gate 	 *  1) We must have been booted such that the bootconf program
21067c478bd9Sstevel@tonic-gate 	 *	created device tree nodes and properties.  This can be
21077c478bd9Sstevel@tonic-gate 	 *	determined by examining the 'bootpath' property.  This
21087c478bd9Sstevel@tonic-gate 	 *	property will be a non-null string iff bootconf was
21097c478bd9Sstevel@tonic-gate 	 *	involved in the boot.
21107c478bd9Sstevel@tonic-gate 	 *
21117c478bd9Sstevel@tonic-gate 	 *  2) The module that we want to match must be the boot device.
21127c478bd9Sstevel@tonic-gate 	 *
21137c478bd9Sstevel@tonic-gate 	 *  3) The instance of the module we are thinking of letting be
21147c478bd9Sstevel@tonic-gate 	 *	our match must be ignoring hardware nodes.
21157c478bd9Sstevel@tonic-gate 	 *
21167c478bd9Sstevel@tonic-gate 	 *  4) The name_addr we want to match must be the name_addr
21177c478bd9Sstevel@tonic-gate 	 *	specified in the 1275 bootpath.
21187c478bd9Sstevel@tonic-gate 	 */
21197c478bd9Sstevel@tonic-gate 	static char bootdev_module[MAXCOMPONENTLEN];
21207c478bd9Sstevel@tonic-gate 	static char bootdev_oldmod[MAXCOMPONENTLEN];
21217c478bd9Sstevel@tonic-gate 	static char bootdev_newaddr[MAXCOMPONENTLEN];
21227c478bd9Sstevel@tonic-gate 	static char bootdev_oldaddr[MAXCOMPONENTLEN];
21237c478bd9Sstevel@tonic-gate 	static int  quickexit;
21247c478bd9Sstevel@tonic-gate 
21257c478bd9Sstevel@tonic-gate 	char *daddr;
21267c478bd9Sstevel@tonic-gate 	int dlen;
21277c478bd9Sstevel@tonic-gate 
21287c478bd9Sstevel@tonic-gate 	char	*lkupname;
21297c478bd9Sstevel@tonic-gate 	int	rv = DDI_FAILURE;
21307c478bd9Sstevel@tonic-gate 
21317c478bd9Sstevel@tonic-gate 	if ((ddi_getlongprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
21327c478bd9Sstevel@tonic-gate 	    "devconf-addr", (caddr_t)&daddr, &dlen) == DDI_PROP_SUCCESS) &&
21337c478bd9Sstevel@tonic-gate 	    (ddi_getprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
21347c478bd9Sstevel@tonic-gate 	    "ignore-hardware-nodes", -1) != -1)) {
21357c478bd9Sstevel@tonic-gate 		if (strcmp(daddr, caddr) == 0) {
21367c478bd9Sstevel@tonic-gate 			return (DDI_SUCCESS);
21377c478bd9Sstevel@tonic-gate 		}
21387c478bd9Sstevel@tonic-gate 	}
21397c478bd9Sstevel@tonic-gate 
21407c478bd9Sstevel@tonic-gate 	if (quickexit)
21417c478bd9Sstevel@tonic-gate 		return (rv);
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate 	if (bootdev_module[0] == '\0') {
21447c478bd9Sstevel@tonic-gate 		char *addrp, *eoaddrp;
21457c478bd9Sstevel@tonic-gate 		char *busp, *modp, *atp;
21467c478bd9Sstevel@tonic-gate 		char *bp1275, *bp;
21477c478bd9Sstevel@tonic-gate 		int  bp1275len, bplen;
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 		bp1275 = bp = addrp = eoaddrp = busp = modp = atp = NULL;
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate 		if (ddi_getlongprop(DDI_DEV_T_ANY,
21527c478bd9Sstevel@tonic-gate 		    ddi_root_node(), 0, "bootpath",
21537c478bd9Sstevel@tonic-gate 		    (caddr_t)&bp1275, &bp1275len) != DDI_PROP_SUCCESS ||
21547c478bd9Sstevel@tonic-gate 		    bp1275len <= 1) {
21557c478bd9Sstevel@tonic-gate 			/*
21567c478bd9Sstevel@tonic-gate 			 * We didn't boot from bootconf so we never need to
21577c478bd9Sstevel@tonic-gate 			 * do any special matches.
21587c478bd9Sstevel@tonic-gate 			 */
21597c478bd9Sstevel@tonic-gate 			quickexit = 1;
21607c478bd9Sstevel@tonic-gate 			if (bp1275)
21617c478bd9Sstevel@tonic-gate 				kmem_free(bp1275, bp1275len);
21627c478bd9Sstevel@tonic-gate 			return (rv);
21637c478bd9Sstevel@tonic-gate 		}
21647c478bd9Sstevel@tonic-gate 
21657c478bd9Sstevel@tonic-gate 		if (ddi_getlongprop(DDI_DEV_T_ANY,
21667c478bd9Sstevel@tonic-gate 		    ddi_root_node(), 0, "boot-path",
21677c478bd9Sstevel@tonic-gate 		    (caddr_t)&bp, &bplen) != DDI_PROP_SUCCESS || bplen <= 1) {
21687c478bd9Sstevel@tonic-gate 			/*
21697c478bd9Sstevel@tonic-gate 			 * No fallback position for matching. This is
21707c478bd9Sstevel@tonic-gate 			 * certainly unexpected, but we'll handle it
21717c478bd9Sstevel@tonic-gate 			 * just in case.
21727c478bd9Sstevel@tonic-gate 			 */
21737c478bd9Sstevel@tonic-gate 			quickexit = 1;
21747c478bd9Sstevel@tonic-gate 			kmem_free(bp1275, bp1275len);
21757c478bd9Sstevel@tonic-gate 			if (bp)
21767c478bd9Sstevel@tonic-gate 				kmem_free(bp, bplen);
21777c478bd9Sstevel@tonic-gate 			return (rv);
21787c478bd9Sstevel@tonic-gate 		}
21797c478bd9Sstevel@tonic-gate 
21807c478bd9Sstevel@tonic-gate 		/*
21817c478bd9Sstevel@tonic-gate 		 *  Determine boot device module and 1275 name_addr
21827c478bd9Sstevel@tonic-gate 		 *
21837c478bd9Sstevel@tonic-gate 		 *  bootpath assumed to be of the form /bus/module@name_addr
21847c478bd9Sstevel@tonic-gate 		 */
21853df2e8b2SRobert Mustacchi 		if ((busp = strchr(bp1275, '/')) != NULL) {
21863df2e8b2SRobert Mustacchi 			if ((modp = strchr(busp + 1, '/')) != NULL) {
21873df2e8b2SRobert Mustacchi 				if ((atp = strchr(modp + 1, '@')) != NULL) {
21887c478bd9Sstevel@tonic-gate 					*atp = '\0';
21897c478bd9Sstevel@tonic-gate 					addrp = atp + 1;
21903df2e8b2SRobert Mustacchi 					if ((eoaddrp = strchr(addrp, '/')) !=
21913df2e8b2SRobert Mustacchi 					    NULL)
21927c478bd9Sstevel@tonic-gate 						*eoaddrp = '\0';
21937c478bd9Sstevel@tonic-gate 				}
21947c478bd9Sstevel@tonic-gate 			}
21957c478bd9Sstevel@tonic-gate 		}
21967c478bd9Sstevel@tonic-gate 
21977c478bd9Sstevel@tonic-gate 		if (modp && addrp) {
21987c478bd9Sstevel@tonic-gate 			(void) strncpy(bootdev_module, modp + 1,
21997c478bd9Sstevel@tonic-gate 			    MAXCOMPONENTLEN);
22007c478bd9Sstevel@tonic-gate 			bootdev_module[MAXCOMPONENTLEN - 1] = '\0';
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate 			(void) strncpy(bootdev_newaddr, addrp, MAXCOMPONENTLEN);
22037c478bd9Sstevel@tonic-gate 			bootdev_newaddr[MAXCOMPONENTLEN - 1] = '\0';
22047c478bd9Sstevel@tonic-gate 		} else {
22057c478bd9Sstevel@tonic-gate 			quickexit = 1;
22067c478bd9Sstevel@tonic-gate 			kmem_free(bp1275, bp1275len);
22077c478bd9Sstevel@tonic-gate 			kmem_free(bp, bplen);
22087c478bd9Sstevel@tonic-gate 			return (rv);
22097c478bd9Sstevel@tonic-gate 		}
22107c478bd9Sstevel@tonic-gate 
22117c478bd9Sstevel@tonic-gate 		/*
22127c478bd9Sstevel@tonic-gate 		 *  Determine fallback name_addr
22137c478bd9Sstevel@tonic-gate 		 *
22147c478bd9Sstevel@tonic-gate 		 *  10/3/96 - Also save fallback module name because it
22157c478bd9Sstevel@tonic-gate 		 *  might actually be different than the current module
22167c478bd9Sstevel@tonic-gate 		 *  name.  E.G., ISA pnp drivers have new names.
22177c478bd9Sstevel@tonic-gate 		 *
22187c478bd9Sstevel@tonic-gate 		 *  bootpath assumed to be of the form /bus/module@name_addr
22197c478bd9Sstevel@tonic-gate 		 */
22207c478bd9Sstevel@tonic-gate 		addrp = NULL;
22213df2e8b2SRobert Mustacchi 		if ((busp = strchr(bp, '/')) != NULL) {
22223df2e8b2SRobert Mustacchi 			if ((modp = strchr(busp + 1, '/')) != NULL) {
22233df2e8b2SRobert Mustacchi 				if ((atp = strchr(modp + 1, '@')) != NULL) {
22247c478bd9Sstevel@tonic-gate 					*atp = '\0';
22257c478bd9Sstevel@tonic-gate 					addrp = atp + 1;
22263df2e8b2SRobert Mustacchi 					if ((eoaddrp = strchr(addrp, '/')) !=
22273df2e8b2SRobert Mustacchi 					    NULL)
22287c478bd9Sstevel@tonic-gate 						*eoaddrp = '\0';
22297c478bd9Sstevel@tonic-gate 				}
22307c478bd9Sstevel@tonic-gate 			}
22317c478bd9Sstevel@tonic-gate 		}
22327c478bd9Sstevel@tonic-gate 
22337c478bd9Sstevel@tonic-gate 		if (modp && addrp) {
22347c478bd9Sstevel@tonic-gate 			(void) strncpy(bootdev_oldmod, modp + 1,
22357c478bd9Sstevel@tonic-gate 			    MAXCOMPONENTLEN);
22367c478bd9Sstevel@tonic-gate 			bootdev_module[MAXCOMPONENTLEN - 1] = '\0';
22377c478bd9Sstevel@tonic-gate 
22387c478bd9Sstevel@tonic-gate 			(void) strncpy(bootdev_oldaddr, addrp, MAXCOMPONENTLEN);
22397c478bd9Sstevel@tonic-gate 			bootdev_oldaddr[MAXCOMPONENTLEN - 1] = '\0';
22407c478bd9Sstevel@tonic-gate 		}
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 		/* Free up the bootpath storage now that we're done with it. */
22437c478bd9Sstevel@tonic-gate 		kmem_free(bp1275, bp1275len);
22447c478bd9Sstevel@tonic-gate 		kmem_free(bp, bplen);
22457c478bd9Sstevel@tonic-gate 
22467c478bd9Sstevel@tonic-gate 		if (bootdev_oldaddr[0] == '\0') {
22477c478bd9Sstevel@tonic-gate 			quickexit = 1;
22487c478bd9Sstevel@tonic-gate 			return (rv);
22497c478bd9Sstevel@tonic-gate 		}
22507c478bd9Sstevel@tonic-gate 	}
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 	if (((lkupname = ddi_get_name(cdip)) != NULL) &&
22537c478bd9Sstevel@tonic-gate 	    (strcmp(bootdev_module, lkupname) == 0 ||
22547c478bd9Sstevel@tonic-gate 	    strcmp(bootdev_oldmod, lkupname) == 0) &&
22557c478bd9Sstevel@tonic-gate 	    ((ddi_getprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
22567c478bd9Sstevel@tonic-gate 	    "ignore-hardware-nodes", -1) != -1) ||
22577c478bd9Sstevel@tonic-gate 	    ignore_hardware_nodes) &&
22587c478bd9Sstevel@tonic-gate 	    strcmp(bootdev_newaddr, caddr) == 0 &&
22597c478bd9Sstevel@tonic-gate 	    strcmp(bootdev_oldaddr, naddr) == 0) {
22607c478bd9Sstevel@tonic-gate 		rv = DDI_SUCCESS;
22617c478bd9Sstevel@tonic-gate 	}
22627c478bd9Sstevel@tonic-gate 
22637c478bd9Sstevel@tonic-gate 	return (rv);
22647c478bd9Sstevel@tonic-gate }
22657c478bd9Sstevel@tonic-gate 
22667c478bd9Sstevel@tonic-gate /*
22677c478bd9Sstevel@tonic-gate  * Perform a copy from a memory mapped device (whose devinfo pointer is devi)
22687c478bd9Sstevel@tonic-gate  * separately mapped at devaddr in the kernel to a kernel buffer at kaddr.
22697c478bd9Sstevel@tonic-gate  */
22707c478bd9Sstevel@tonic-gate /*ARGSUSED*/
22717c478bd9Sstevel@tonic-gate int
e_ddi_copyfromdev(dev_info_t * devi,off_t off,const void * devaddr,void * kaddr,size_t len)22727c478bd9Sstevel@tonic-gate e_ddi_copyfromdev(dev_info_t *devi,
22737c478bd9Sstevel@tonic-gate     off_t off, const void *devaddr, void *kaddr, size_t len)
22747c478bd9Sstevel@tonic-gate {
22757c478bd9Sstevel@tonic-gate 	bcopy(devaddr, kaddr, len);
22767c478bd9Sstevel@tonic-gate 	return (0);
22777c478bd9Sstevel@tonic-gate }
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate /*
22807c478bd9Sstevel@tonic-gate  * Perform a copy to a memory mapped device (whose devinfo pointer is devi)
22817c478bd9Sstevel@tonic-gate  * separately mapped at devaddr in the kernel from a kernel buffer at kaddr.
22827c478bd9Sstevel@tonic-gate  */
22837c478bd9Sstevel@tonic-gate /*ARGSUSED*/
22847c478bd9Sstevel@tonic-gate int
e_ddi_copytodev(dev_info_t * devi,off_t off,const void * kaddr,void * devaddr,size_t len)22857c478bd9Sstevel@tonic-gate e_ddi_copytodev(dev_info_t *devi,
22867c478bd9Sstevel@tonic-gate     off_t off, const void *kaddr, void *devaddr, size_t len)
22877c478bd9Sstevel@tonic-gate {
22887c478bd9Sstevel@tonic-gate 	bcopy(kaddr, devaddr, len);
22897c478bd9Sstevel@tonic-gate 	return (0);
22907c478bd9Sstevel@tonic-gate }
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate static int
poke_mem(peekpoke_ctlops_t * in_args)22947c478bd9Sstevel@tonic-gate poke_mem(peekpoke_ctlops_t *in_args)
22957c478bd9Sstevel@tonic-gate {
22963df2e8b2SRobert Mustacchi 	int err;
22977c478bd9Sstevel@tonic-gate 	on_trap_data_t otd;
22987c478bd9Sstevel@tonic-gate 
22997c478bd9Sstevel@tonic-gate 	/* Set up protected environment. */
23007c478bd9Sstevel@tonic-gate 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
23013df2e8b2SRobert Mustacchi 		err = DDI_SUCCESS;
23027c478bd9Sstevel@tonic-gate 		switch (in_args->size) {
23037c478bd9Sstevel@tonic-gate 		case sizeof (uint8_t):
23047c478bd9Sstevel@tonic-gate 			*(uint8_t *)(in_args->dev_addr) =
23057c478bd9Sstevel@tonic-gate 			    *(uint8_t *)in_args->host_addr;
23067c478bd9Sstevel@tonic-gate 			break;
23077c478bd9Sstevel@tonic-gate 
23087c478bd9Sstevel@tonic-gate 		case sizeof (uint16_t):
23097c478bd9Sstevel@tonic-gate 			*(uint16_t *)(in_args->dev_addr) =
23107c478bd9Sstevel@tonic-gate 			    *(uint16_t *)in_args->host_addr;
23117c478bd9Sstevel@tonic-gate 			break;
23127c478bd9Sstevel@tonic-gate 
23137c478bd9Sstevel@tonic-gate 		case sizeof (uint32_t):
23147c478bd9Sstevel@tonic-gate 			*(uint32_t *)(in_args->dev_addr) =
23157c478bd9Sstevel@tonic-gate 			    *(uint32_t *)in_args->host_addr;
23167c478bd9Sstevel@tonic-gate 			break;
23177c478bd9Sstevel@tonic-gate 
23187c478bd9Sstevel@tonic-gate 		case sizeof (uint64_t):
23197c478bd9Sstevel@tonic-gate 			*(uint64_t *)(in_args->dev_addr) =
23207c478bd9Sstevel@tonic-gate 			    *(uint64_t *)in_args->host_addr;
23217c478bd9Sstevel@tonic-gate 			break;
23227c478bd9Sstevel@tonic-gate 
23237c478bd9Sstevel@tonic-gate 		default:
23247c478bd9Sstevel@tonic-gate 			err = DDI_FAILURE;
23257c478bd9Sstevel@tonic-gate 			break;
23267c478bd9Sstevel@tonic-gate 		}
23273df2e8b2SRobert Mustacchi 	} else {
23287c478bd9Sstevel@tonic-gate 		err = DDI_FAILURE;
23293df2e8b2SRobert Mustacchi 	}
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate 	/* Take down protected environment. */
23327c478bd9Sstevel@tonic-gate 	no_trap();
23337c478bd9Sstevel@tonic-gate 
23347c478bd9Sstevel@tonic-gate 	return (err);
23357c478bd9Sstevel@tonic-gate }
23367c478bd9Sstevel@tonic-gate 
23377c478bd9Sstevel@tonic-gate 
23387c478bd9Sstevel@tonic-gate static int
peek_mem(peekpoke_ctlops_t * in_args)23397c478bd9Sstevel@tonic-gate peek_mem(peekpoke_ctlops_t *in_args)
23407c478bd9Sstevel@tonic-gate {
23413df2e8b2SRobert Mustacchi 	int err;
23427c478bd9Sstevel@tonic-gate 	on_trap_data_t otd;
23437c478bd9Sstevel@tonic-gate 
23447c478bd9Sstevel@tonic-gate 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
23453df2e8b2SRobert Mustacchi 		err = DDI_SUCCESS;
23467c478bd9Sstevel@tonic-gate 		switch (in_args->size) {
23477c478bd9Sstevel@tonic-gate 		case sizeof (uint8_t):
23487c478bd9Sstevel@tonic-gate 			*(uint8_t *)in_args->host_addr =
23497c478bd9Sstevel@tonic-gate 			    *(uint8_t *)in_args->dev_addr;
23507c478bd9Sstevel@tonic-gate 			break;
23517c478bd9Sstevel@tonic-gate 
23527c478bd9Sstevel@tonic-gate 		case sizeof (uint16_t):
23537c478bd9Sstevel@tonic-gate 			*(uint16_t *)in_args->host_addr =
23547c478bd9Sstevel@tonic-gate 			    *(uint16_t *)in_args->dev_addr;
23557c478bd9Sstevel@tonic-gate 			break;
23567c478bd9Sstevel@tonic-gate 
23577c478bd9Sstevel@tonic-gate 		case sizeof (uint32_t):
23587c478bd9Sstevel@tonic-gate 			*(uint32_t *)in_args->host_addr =
23597c478bd9Sstevel@tonic-gate 			    *(uint32_t *)in_args->dev_addr;
23607c478bd9Sstevel@tonic-gate 			break;
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate 		case sizeof (uint64_t):
23637c478bd9Sstevel@tonic-gate 			*(uint64_t *)in_args->host_addr =
23647c478bd9Sstevel@tonic-gate 			    *(uint64_t *)in_args->dev_addr;
23657c478bd9Sstevel@tonic-gate 			break;
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate 		default:
23687c478bd9Sstevel@tonic-gate 			err = DDI_FAILURE;
23697c478bd9Sstevel@tonic-gate 			break;
23707c478bd9Sstevel@tonic-gate 		}
23713df2e8b2SRobert Mustacchi 	} else {
23727c478bd9Sstevel@tonic-gate 		err = DDI_FAILURE;
23733df2e8b2SRobert Mustacchi 	}
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate 	no_trap();
23767c478bd9Sstevel@tonic-gate 	return (err);
23777c478bd9Sstevel@tonic-gate }
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate 
23807c478bd9Sstevel@tonic-gate /*
23817c478bd9Sstevel@tonic-gate  * This is called only to process peek/poke when the DIP is NULL.
23827c478bd9Sstevel@tonic-gate  * Assume that this is for memory, as nexi take care of device safe accesses.
23837c478bd9Sstevel@tonic-gate  */
23847c478bd9Sstevel@tonic-gate int
peekpoke_mem(ddi_ctl_enum_t cmd,peekpoke_ctlops_t * in_args)23857c478bd9Sstevel@tonic-gate peekpoke_mem(ddi_ctl_enum_t cmd, peekpoke_ctlops_t *in_args)
23867c478bd9Sstevel@tonic-gate {
23877c478bd9Sstevel@tonic-gate 	return (cmd == DDI_CTLOPS_PEEK ? peek_mem(in_args) : poke_mem(in_args));
23887c478bd9Sstevel@tonic-gate }
23897c478bd9Sstevel@tonic-gate 
239000d0963fSdilpreet /*
239100d0963fSdilpreet  * we've just done a cautious put/get. Check if it was successful by
239200d0963fSdilpreet  * calling pci_ereport_post() on all puts and for any gets that return -1
239300d0963fSdilpreet  */
239400d0963fSdilpreet static int
pci_peekpoke_check_fma(dev_info_t * dip,void * arg,ddi_ctl_enum_t ctlop,void (* scan)(dev_info_t *,ddi_fm_error_t *))2395eae2e508Skrishnae pci_peekpoke_check_fma(dev_info_t *dip, void *arg, ddi_ctl_enum_t ctlop,
2396eae2e508Skrishnae     void (*scan)(dev_info_t *, ddi_fm_error_t *))
239700d0963fSdilpreet {
239800d0963fSdilpreet 	int	rval = DDI_SUCCESS;
239900d0963fSdilpreet 	peekpoke_ctlops_t *in_args = (peekpoke_ctlops_t *)arg;
240000d0963fSdilpreet 	ddi_fm_error_t de;
240100d0963fSdilpreet 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)in_args->handle;
240200d0963fSdilpreet 	ddi_acc_hdl_t *hdlp = (ddi_acc_hdl_t *)in_args->handle;
240300d0963fSdilpreet 	int check_err = 0;
240400d0963fSdilpreet 	int repcount = in_args->repcount;
240500d0963fSdilpreet 
24067f58d724Sdilpreet 	if (ctlop == DDI_CTLOPS_POKE &&
24077f58d724Sdilpreet 	    hdlp->ah_acc.devacc_attr_access != DDI_CAUTIOUS_ACC)
24087f58d724Sdilpreet 		return (DDI_SUCCESS);
24097f58d724Sdilpreet 
241000d0963fSdilpreet 	if (ctlop == DDI_CTLOPS_PEEK &&
241100d0963fSdilpreet 	    hdlp->ah_acc.devacc_attr_access != DDI_CAUTIOUS_ACC) {
241200d0963fSdilpreet 		for (; repcount; repcount--) {
241300d0963fSdilpreet 			switch (in_args->size) {
241400d0963fSdilpreet 			case sizeof (uint8_t):
241500d0963fSdilpreet 				if (*(uint8_t *)in_args->host_addr == 0xff)
241600d0963fSdilpreet 					check_err = 1;
241700d0963fSdilpreet 				break;
241800d0963fSdilpreet 			case sizeof (uint16_t):
241900d0963fSdilpreet 				if (*(uint16_t *)in_args->host_addr == 0xffff)
242000d0963fSdilpreet 					check_err = 1;
242100d0963fSdilpreet 				break;
242200d0963fSdilpreet 			case sizeof (uint32_t):
242300d0963fSdilpreet 				if (*(uint32_t *)in_args->host_addr ==
242400d0963fSdilpreet 				    0xffffffff)
242500d0963fSdilpreet 					check_err = 1;
242600d0963fSdilpreet 				break;
242700d0963fSdilpreet 			case sizeof (uint64_t):
242800d0963fSdilpreet 				if (*(uint64_t *)in_args->host_addr ==
242900d0963fSdilpreet 				    0xffffffffffffffff)
243000d0963fSdilpreet 					check_err = 1;
243100d0963fSdilpreet 				break;
243200d0963fSdilpreet 			}
243300d0963fSdilpreet 		}
243400d0963fSdilpreet 		if (check_err == 0)
243500d0963fSdilpreet 			return (DDI_SUCCESS);
243600d0963fSdilpreet 	}
243700d0963fSdilpreet 	/*
243800d0963fSdilpreet 	 * for a cautious put or get or a non-cautious get that returned -1 call
243900d0963fSdilpreet 	 * io framework to see if there really was an error
244000d0963fSdilpreet 	 */
244100d0963fSdilpreet 	bzero(&de, sizeof (ddi_fm_error_t));
24428aec9182Sstephh 	de.fme_version = DDI_FME_VERSION;
244300d0963fSdilpreet 	de.fme_ena = fm_ena_generate(0, FM_ENA_FMT1);
244400d0963fSdilpreet 	if (hdlp->ah_acc.devacc_attr_access == DDI_CAUTIOUS_ACC) {
244500d0963fSdilpreet 		de.fme_flag = DDI_FM_ERR_EXPECTED;
244600d0963fSdilpreet 		de.fme_acc_handle = in_args->handle;
244700d0963fSdilpreet 	} else if (hdlp->ah_acc.devacc_attr_access == DDI_DEFAULT_ACC) {
244800d0963fSdilpreet 		/*
244900d0963fSdilpreet 		 * We only get here with DDI_DEFAULT_ACC for config space gets.
245000d0963fSdilpreet 		 * Non-hardened drivers may be probing the hardware and
245100d0963fSdilpreet 		 * expecting -1 returned. So need to treat errors on
245200d0963fSdilpreet 		 * DDI_DEFAULT_ACC as DDI_FM_ERR_EXPECTED.
245300d0963fSdilpreet 		 */
245400d0963fSdilpreet 		de.fme_flag = DDI_FM_ERR_EXPECTED;
245500d0963fSdilpreet 		de.fme_acc_handle = in_args->handle;
245600d0963fSdilpreet 	} else {
245700d0963fSdilpreet 		/*
245800d0963fSdilpreet 		 * Hardened driver doing protected accesses shouldn't
245900d0963fSdilpreet 		 * get errors unless there's a hardware problem. Treat
246000d0963fSdilpreet 		 * as nonfatal if there's an error, but set UNEXPECTED
246100d0963fSdilpreet 		 * so we raise ereports on any errors and potentially
246200d0963fSdilpreet 		 * fault the device
246300d0963fSdilpreet 		 */
246400d0963fSdilpreet 		de.fme_flag = DDI_FM_ERR_UNEXPECTED;
246500d0963fSdilpreet 	}
2466eae2e508Skrishnae 	(void) scan(dip, &de);
246700d0963fSdilpreet 	if (hdlp->ah_acc.devacc_attr_access != DDI_DEFAULT_ACC &&
246800d0963fSdilpreet 	    de.fme_status != DDI_FM_OK) {
246900d0963fSdilpreet 		ndi_err_t *errp = (ndi_err_t *)hp->ahi_err;
247000d0963fSdilpreet 		rval = DDI_FAILURE;
247100d0963fSdilpreet 		errp->err_ena = de.fme_ena;
247200d0963fSdilpreet 		errp->err_expected = de.fme_flag;
247300d0963fSdilpreet 		errp->err_status = DDI_FM_NONFATAL;
247400d0963fSdilpreet 	}
247500d0963fSdilpreet 	return (rval);
247600d0963fSdilpreet }
247700d0963fSdilpreet 
247800d0963fSdilpreet /*
247900d0963fSdilpreet  * pci_peekpoke_check_nofma() is for when an error occurs on a register access
248000d0963fSdilpreet  * during pci_ereport_post(). We can't call pci_ereport_post() again or we'd
248100d0963fSdilpreet  * recurse, so assume all puts are OK and gets have failed if they return -1
248200d0963fSdilpreet  */
248300d0963fSdilpreet static int
pci_peekpoke_check_nofma(void * arg,ddi_ctl_enum_t ctlop)248400d0963fSdilpreet pci_peekpoke_check_nofma(void *arg, ddi_ctl_enum_t ctlop)
248500d0963fSdilpreet {
248600d0963fSdilpreet 	int rval = DDI_SUCCESS;
248700d0963fSdilpreet 	peekpoke_ctlops_t *in_args = (peekpoke_ctlops_t *)arg;
248800d0963fSdilpreet 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)in_args->handle;
248900d0963fSdilpreet 	ddi_acc_hdl_t *hdlp = (ddi_acc_hdl_t *)in_args->handle;
249000d0963fSdilpreet 	int repcount = in_args->repcount;
249100d0963fSdilpreet 
249200d0963fSdilpreet 	if (ctlop == DDI_CTLOPS_POKE)
249300d0963fSdilpreet 		return (rval);
249400d0963fSdilpreet 
249500d0963fSdilpreet 	for (; repcount; repcount--) {
249600d0963fSdilpreet 		switch (in_args->size) {
249700d0963fSdilpreet 		case sizeof (uint8_t):
249800d0963fSdilpreet 			if (*(uint8_t *)in_args->host_addr == 0xff)
249900d0963fSdilpreet 				rval = DDI_FAILURE;
250000d0963fSdilpreet 			break;
250100d0963fSdilpreet 		case sizeof (uint16_t):
250200d0963fSdilpreet 			if (*(uint16_t *)in_args->host_addr == 0xffff)
250300d0963fSdilpreet 				rval = DDI_FAILURE;
250400d0963fSdilpreet 			break;
250500d0963fSdilpreet 		case sizeof (uint32_t):
250600d0963fSdilpreet 			if (*(uint32_t *)in_args->host_addr == 0xffffffff)
250700d0963fSdilpreet 				rval = DDI_FAILURE;
250800d0963fSdilpreet 			break;
250900d0963fSdilpreet 		case sizeof (uint64_t):
251000d0963fSdilpreet 			if (*(uint64_t *)in_args->host_addr ==
251100d0963fSdilpreet 			    0xffffffffffffffff)
251200d0963fSdilpreet 				rval = DDI_FAILURE;
251300d0963fSdilpreet 			break;
251400d0963fSdilpreet 		}
251500d0963fSdilpreet 	}
251600d0963fSdilpreet 	if (hdlp->ah_acc.devacc_attr_access != DDI_DEFAULT_ACC &&
251700d0963fSdilpreet 	    rval == DDI_FAILURE) {
251800d0963fSdilpreet 		ndi_err_t *errp = (ndi_err_t *)hp->ahi_err;
251900d0963fSdilpreet 		errp->err_ena = fm_ena_generate(0, FM_ENA_FMT1);
252000d0963fSdilpreet 		errp->err_expected = DDI_FM_ERR_UNEXPECTED;
252100d0963fSdilpreet 		errp->err_status = DDI_FM_NONFATAL;
252200d0963fSdilpreet 	}
252300d0963fSdilpreet 	return (rval);
252400d0963fSdilpreet }
252500d0963fSdilpreet 
252600d0963fSdilpreet int
pci_peekpoke_check(dev_info_t * dip,dev_info_t * rdip,ddi_ctl_enum_t ctlop,void * arg,void * result,int (* handler)(dev_info_t *,dev_info_t *,ddi_ctl_enum_t,void *,void *),kmutex_t * err_mutexp,kmutex_t * peek_poke_mutexp,void (* scan)(dev_info_t *,ddi_fm_error_t *))252700d0963fSdilpreet pci_peekpoke_check(dev_info_t *dip, dev_info_t *rdip,
252800d0963fSdilpreet     ddi_ctl_enum_t ctlop, void *arg, void *result,
252900d0963fSdilpreet     int (*handler)(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *,
2530eae2e508Skrishnae     void *), kmutex_t *err_mutexp, kmutex_t *peek_poke_mutexp,
2531eae2e508Skrishnae     void (*scan)(dev_info_t *, ddi_fm_error_t *))
253200d0963fSdilpreet {
253300d0963fSdilpreet 	int rval;
253400d0963fSdilpreet 	peekpoke_ctlops_t *in_args = (peekpoke_ctlops_t *)arg;
253500d0963fSdilpreet 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)in_args->handle;
253600d0963fSdilpreet 
25378aec9182Sstephh 	/*
25388aec9182Sstephh 	 * this function only supports cautious accesses, not peeks/pokes
25398aec9182Sstephh 	 * which don't have a handle
25408aec9182Sstephh 	 */
25418aec9182Sstephh 	if (hp == NULL)
25428aec9182Sstephh 		return (DDI_FAILURE);
25438aec9182Sstephh 
254400d0963fSdilpreet 	if (hp->ahi_acc_attr & DDI_ACCATTR_CONFIG_SPACE) {
254500d0963fSdilpreet 		if (!mutex_tryenter(err_mutexp)) {
254600d0963fSdilpreet 			/*
254700d0963fSdilpreet 			 * As this may be a recursive call from within
254800d0963fSdilpreet 			 * pci_ereport_post() we can't wait for the mutexes.
254900d0963fSdilpreet 			 * Fortunately we know someone is already calling
255000d0963fSdilpreet 			 * pci_ereport_post() which will handle the error bits
255100d0963fSdilpreet 			 * for us, and as this is a config space access we can
255200d0963fSdilpreet 			 * just do the access and check return value for -1
255300d0963fSdilpreet 			 * using pci_peekpoke_check_nofma().
255400d0963fSdilpreet 			 */
255500d0963fSdilpreet 			rval = handler(dip, rdip, ctlop, arg, result);
255600d0963fSdilpreet 			if (rval == DDI_SUCCESS)
255700d0963fSdilpreet 				rval = pci_peekpoke_check_nofma(arg, ctlop);
255800d0963fSdilpreet 			return (rval);
255900d0963fSdilpreet 		}
256000d0963fSdilpreet 		/*
256100d0963fSdilpreet 		 * This can't be a recursive call. Drop the err_mutex and get
256200d0963fSdilpreet 		 * both mutexes in the right order. If an error hasn't already
256300d0963fSdilpreet 		 * been detected by the ontrap code, use pci_peekpoke_check_fma
256400d0963fSdilpreet 		 * which will call pci_ereport_post() to check error status.
256500d0963fSdilpreet 		 */
256600d0963fSdilpreet 		mutex_exit(err_mutexp);
256700d0963fSdilpreet 	}
256800d0963fSdilpreet 	mutex_enter(peek_poke_mutexp);
256900d0963fSdilpreet 	rval = handler(dip, rdip, ctlop, arg, result);
257000d0963fSdilpreet 	if (rval == DDI_SUCCESS) {
257100d0963fSdilpreet 		mutex_enter(err_mutexp);
2572eae2e508Skrishnae 		rval = pci_peekpoke_check_fma(dip, arg, ctlop, scan);
257300d0963fSdilpreet 		mutex_exit(err_mutexp);
257400d0963fSdilpreet 	}
257500d0963fSdilpreet 	mutex_exit(peek_poke_mutexp);
257600d0963fSdilpreet 	return (rval);
257700d0963fSdilpreet }
257800d0963fSdilpreet 
25797c478bd9Sstevel@tonic-gate void
impl_setup_ddi(void)25807c478bd9Sstevel@tonic-gate impl_setup_ddi(void)
25817c478bd9Sstevel@tonic-gate {
25828770118eSlipeng sang - Sun Microsystems - Beijing China #if !defined(__xpv)
25838770118eSlipeng sang - Sun Microsystems - Beijing China 	extern void startup_bios_disk(void);
25848770118eSlipeng sang - Sun Microsystems - Beijing China 	extern int post_fastreboot;
25858770118eSlipeng sang - Sun Microsystems - Beijing China #endif
25867c478bd9Sstevel@tonic-gate 	dev_info_t *xdip, *isa_dip;
25877c478bd9Sstevel@tonic-gate 	rd_existing_t rd_mem_prop;
25887c478bd9Sstevel@tonic-gate 	int err;
25897c478bd9Sstevel@tonic-gate 
25907c478bd9Sstevel@tonic-gate 	ndi_devi_alloc_sleep(ddi_root_node(), "ramdisk",
2591fa9e4066Sahrens 	    (pnode_t)DEVI_SID_NODEID, &xdip);
25927c478bd9Sstevel@tonic-gate 
25937c478bd9Sstevel@tonic-gate 	(void) BOP_GETPROP(bootops,
25947c478bd9Sstevel@tonic-gate 	    "ramdisk_start", (void *)&ramdisk_start);
25957c478bd9Sstevel@tonic-gate 	(void) BOP_GETPROP(bootops,
25967c478bd9Sstevel@tonic-gate 	    "ramdisk_end", (void *)&ramdisk_end);
25977c478bd9Sstevel@tonic-gate 
2598843e1988Sjohnlev #ifdef __xpv
2599843e1988Sjohnlev 	ramdisk_start -= ONE_GIG;
2600843e1988Sjohnlev 	ramdisk_end -= ONE_GIG;
2601843e1988Sjohnlev #endif
26027c478bd9Sstevel@tonic-gate 	rd_mem_prop.phys = ramdisk_start;
26037c478bd9Sstevel@tonic-gate 	rd_mem_prop.size = ramdisk_end - ramdisk_start + 1;
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate 	(void) ndi_prop_update_byte_array(DDI_DEV_T_NONE, xdip,
26067c478bd9Sstevel@tonic-gate 	    RD_EXISTING_PROP_NAME, (uchar_t *)&rd_mem_prop,
26077c478bd9Sstevel@tonic-gate 	    sizeof (rd_mem_prop));
26087c478bd9Sstevel@tonic-gate 	err = ndi_devi_bind_driver(xdip, 0);
26097c478bd9Sstevel@tonic-gate 	ASSERT(err == 0);
26107c478bd9Sstevel@tonic-gate 
26117c478bd9Sstevel@tonic-gate 	/* isa node */
261278323854SJudy Chen 	if (pseudo_isa) {
26137c478bd9Sstevel@tonic-gate 		ndi_devi_alloc_sleep(ddi_root_node(), "isa",
2614fa9e4066Sahrens 		    (pnode_t)DEVI_SID_NODEID, &isa_dip);
26157c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, isa_dip,
26167c478bd9Sstevel@tonic-gate 		    "device_type", "isa");
26177c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, isa_dip,
26187c478bd9Sstevel@tonic-gate 		    "bus-type", "isa");
26197c478bd9Sstevel@tonic-gate 		(void) ndi_devi_bind_driver(isa_dip, 0);
262078323854SJudy Chen 	}
26217c478bd9Sstevel@tonic-gate 
26227c478bd9Sstevel@tonic-gate 	/*
26237c478bd9Sstevel@tonic-gate 	 * Read in the properties from the boot.
26247c478bd9Sstevel@tonic-gate 	 */
26257c478bd9Sstevel@tonic-gate 	get_boot_properties();
26267c478bd9Sstevel@tonic-gate 
26277c478bd9Sstevel@tonic-gate 	/* not framebuffer should be enumerated, if present */
26287c478bd9Sstevel@tonic-gate 	get_vga_properties();
26298770118eSlipeng sang - Sun Microsystems - Beijing China 
2630cbc8e155SToomas Soome 	/* Copy console font if provided by boot. */
2631cbc8e155SToomas Soome 	get_console_font();
2632cbc8e155SToomas Soome 
2633288e2932SJerry Gilliam 	/*
2634288e2932SJerry Gilliam 	 * Check for administratively disabled drivers.
2635288e2932SJerry Gilliam 	 */
2636288e2932SJerry Gilliam 	check_driver_disable();
2637288e2932SJerry Gilliam 
26388770118eSlipeng sang - Sun Microsystems - Beijing China #if !defined(__xpv)
2639fdbb9738SToomas Soome 	if (!post_fastreboot && BOP_GETPROPLEN(bootops, "efi-systab") < 0)
26408770118eSlipeng sang - Sun Microsystems - Beijing China 		startup_bios_disk();
26418770118eSlipeng sang - Sun Microsystems - Beijing China #endif
26428770118eSlipeng sang - Sun Microsystems - Beijing China 	/* do bus dependent probes. */
26438770118eSlipeng sang - Sun Microsystems - Beijing China 	impl_bus_initialprobe();
26447c478bd9Sstevel@tonic-gate }
26457c478bd9Sstevel@tonic-gate 
26467c478bd9Sstevel@tonic-gate dev_t
getrootdev(void)26477c478bd9Sstevel@tonic-gate getrootdev(void)
26487c478bd9Sstevel@tonic-gate {
26497c478bd9Sstevel@tonic-gate 	/*
26507c478bd9Sstevel@tonic-gate 	 * Usually rootfs.bo_name is initialized by the
26517c478bd9Sstevel@tonic-gate 	 * the bootpath property from bootenv.rc, but
26527c478bd9Sstevel@tonic-gate 	 * defaults to "/ramdisk:a" otherwise.
26537c478bd9Sstevel@tonic-gate 	 */
26547c478bd9Sstevel@tonic-gate 	return (ddi_pathname_to_dev_t(rootfs.bo_name));
26557c478bd9Sstevel@tonic-gate }
26567c478bd9Sstevel@tonic-gate 
26577c478bd9Sstevel@tonic-gate static struct bus_probe {
26587c478bd9Sstevel@tonic-gate 	struct bus_probe *next;
26597c478bd9Sstevel@tonic-gate 	void (*probe)(int);
26607c478bd9Sstevel@tonic-gate } *bus_probes;
26617c478bd9Sstevel@tonic-gate 
26627c478bd9Sstevel@tonic-gate void
impl_bus_add_probe(void (* func)(int))26637c478bd9Sstevel@tonic-gate impl_bus_add_probe(void (*func)(int))
26647c478bd9Sstevel@tonic-gate {
26657c478bd9Sstevel@tonic-gate 	struct bus_probe *probe;
266637d6e245Slipeng sang - Sun Microsystems - Beijing China 	struct bus_probe *lastprobe = NULL;
26677c478bd9Sstevel@tonic-gate 
26687c478bd9Sstevel@tonic-gate 	probe = kmem_alloc(sizeof (*probe), KM_SLEEP);
26697c478bd9Sstevel@tonic-gate 	probe->probe = func;
267037d6e245Slipeng sang - Sun Microsystems - Beijing China 	probe->next = NULL;
267137d6e245Slipeng sang - Sun Microsystems - Beijing China 
267237d6e245Slipeng sang - Sun Microsystems - Beijing China 	if (!bus_probes) {
26737c478bd9Sstevel@tonic-gate 		bus_probes = probe;
267437d6e245Slipeng sang - Sun Microsystems - Beijing China 		return;
267537d6e245Slipeng sang - Sun Microsystems - Beijing China 	}
267637d6e245Slipeng sang - Sun Microsystems - Beijing China 
267737d6e245Slipeng sang - Sun Microsystems - Beijing China 	lastprobe = bus_probes;
267837d6e245Slipeng sang - Sun Microsystems - Beijing China 	while (lastprobe->next)
267937d6e245Slipeng sang - Sun Microsystems - Beijing China 		lastprobe = lastprobe->next;
268037d6e245Slipeng sang - Sun Microsystems - Beijing China 	lastprobe->next = probe;
26817c478bd9Sstevel@tonic-gate }
26827c478bd9Sstevel@tonic-gate 
26837c478bd9Sstevel@tonic-gate /*ARGSUSED*/
26847c478bd9Sstevel@tonic-gate void
impl_bus_delete_probe(void (* func)(int))26857c478bd9Sstevel@tonic-gate impl_bus_delete_probe(void (*func)(int))
26867c478bd9Sstevel@tonic-gate {
26877c478bd9Sstevel@tonic-gate 	struct bus_probe *prev = NULL;
26887c478bd9Sstevel@tonic-gate 	struct bus_probe *probe = bus_probes;
26897c478bd9Sstevel@tonic-gate 
26907c478bd9Sstevel@tonic-gate 	while (probe) {
26917c478bd9Sstevel@tonic-gate 		if (probe->probe == func)
26927c478bd9Sstevel@tonic-gate 			break;
26937c478bd9Sstevel@tonic-gate 		prev = probe;
26947c478bd9Sstevel@tonic-gate 		probe = probe->next;
26957c478bd9Sstevel@tonic-gate 	}
26967c478bd9Sstevel@tonic-gate 
26977c478bd9Sstevel@tonic-gate 	if (probe == NULL)
26987c478bd9Sstevel@tonic-gate 		return;
26997c478bd9Sstevel@tonic-gate 
27007c478bd9Sstevel@tonic-gate 	if (prev)
27017c478bd9Sstevel@tonic-gate 		prev->next = probe->next;
27027c478bd9Sstevel@tonic-gate 	else
27037c478bd9Sstevel@tonic-gate 		bus_probes = probe->next;
27047c478bd9Sstevel@tonic-gate 
27057c478bd9Sstevel@tonic-gate 	kmem_free(probe, sizeof (struct bus_probe));
27067c478bd9Sstevel@tonic-gate }
27077c478bd9Sstevel@tonic-gate 
27087c478bd9Sstevel@tonic-gate /*
27097c478bd9Sstevel@tonic-gate  * impl_bus_initialprobe
27107c478bd9Sstevel@tonic-gate  *	Modload the prom simulator, then let it probe to verify existence
27117c478bd9Sstevel@tonic-gate  *	and type of PCI support.
27127c478bd9Sstevel@tonic-gate  */
27137c478bd9Sstevel@tonic-gate static void
impl_bus_initialprobe(void)27147c478bd9Sstevel@tonic-gate impl_bus_initialprobe(void)
27157c478bd9Sstevel@tonic-gate {
27167c478bd9Sstevel@tonic-gate 	struct bus_probe *probe;
27177c478bd9Sstevel@tonic-gate 
27187c478bd9Sstevel@tonic-gate 	/* load modules to install bus probes */
2719843e1988Sjohnlev #if defined(__xpv)
2720843e1988Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
27217c478bd9Sstevel@tonic-gate 		if (modload("misc", "pci_autoconfig") < 0) {
2722843e1988Sjohnlev 			panic("failed to load misc/pci_autoconfig");
27237c478bd9Sstevel@tonic-gate 		}
272437d6e245Slipeng sang - Sun Microsystems - Beijing China 
272537d6e245Slipeng sang - Sun Microsystems - Beijing China 		if (modload("drv", "isa") < 0)
272637d6e245Slipeng sang - Sun Microsystems - Beijing China 			panic("failed to load drv/isa");
2727843e1988Sjohnlev 	}
272837d6e245Slipeng sang - Sun Microsystems - Beijing China 
2729843e1988Sjohnlev 	(void) modload("misc", "xpv_autoconfig");
2730843e1988Sjohnlev #else
2731843e1988Sjohnlev 	if (modload("misc", "pci_autoconfig") < 0) {
2732843e1988Sjohnlev 		panic("failed to load misc/pci_autoconfig");
2733843e1988Sjohnlev 	}
273437d6e245Slipeng sang - Sun Microsystems - Beijing China 
2735432cf5a9SDana Myers 	(void) modload("misc", "acpidev");
2736432cf5a9SDana Myers 
273737d6e245Slipeng sang - Sun Microsystems - Beijing China 	if (modload("drv", "isa") < 0)
273837d6e245Slipeng sang - Sun Microsystems - Beijing China 		panic("failed to load drv/isa");
2739843e1988Sjohnlev #endif
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 	probe = bus_probes;
27427c478bd9Sstevel@tonic-gate 	while (probe) {
2743843e1988Sjohnlev 		/* run the probe functions */
27447c478bd9Sstevel@tonic-gate 		(*probe->probe)(0);
27457c478bd9Sstevel@tonic-gate 		probe = probe->next;
27467c478bd9Sstevel@tonic-gate 	}
27477c478bd9Sstevel@tonic-gate }
27487c478bd9Sstevel@tonic-gate 
27497c478bd9Sstevel@tonic-gate /*
27507c478bd9Sstevel@tonic-gate  * impl_bus_reprobe
27517c478bd9Sstevel@tonic-gate  *	Reprogram devices not set up by firmware.
27527c478bd9Sstevel@tonic-gate  */
27537c478bd9Sstevel@tonic-gate static void
impl_bus_reprobe(void)27547c478bd9Sstevel@tonic-gate impl_bus_reprobe(void)
27557c478bd9Sstevel@tonic-gate {
27567c478bd9Sstevel@tonic-gate 	struct bus_probe *probe;
27577c478bd9Sstevel@tonic-gate 
27587c478bd9Sstevel@tonic-gate 	probe = bus_probes;
27597c478bd9Sstevel@tonic-gate 	while (probe) {
27607c478bd9Sstevel@tonic-gate 		/* run the probe function */
27617c478bd9Sstevel@tonic-gate 		(*probe->probe)(1);
27627c478bd9Sstevel@tonic-gate 		probe = probe->next;
27637c478bd9Sstevel@tonic-gate 	}
27647c478bd9Sstevel@tonic-gate }
276500d0963fSdilpreet 
276600d0963fSdilpreet 
276700d0963fSdilpreet /*
276800d0963fSdilpreet  * The following functions ready a cautious request to go up to the nexus
276900d0963fSdilpreet  * driver.  It is up to the nexus driver to decide how to process the request.
277000d0963fSdilpreet  * It may choose to call i_ddi_do_caut_get/put in this file, or do it
277100d0963fSdilpreet  * differently.
277200d0963fSdilpreet  */
277300d0963fSdilpreet 
277400d0963fSdilpreet static void
i_ddi_caut_getput_ctlops(ddi_acc_impl_t * hp,uint64_t host_addr,uint64_t dev_addr,size_t size,size_t repcount,uint_t flags,ddi_ctl_enum_t cmd)277500d0963fSdilpreet i_ddi_caut_getput_ctlops(ddi_acc_impl_t *hp, uint64_t host_addr,
277600d0963fSdilpreet     uint64_t dev_addr, size_t size, size_t repcount, uint_t flags,
277700d0963fSdilpreet     ddi_ctl_enum_t cmd)
277800d0963fSdilpreet {
277900d0963fSdilpreet 	peekpoke_ctlops_t	cautacc_ctlops_arg;
278000d0963fSdilpreet 
278100d0963fSdilpreet 	cautacc_ctlops_arg.size = size;
278200d0963fSdilpreet 	cautacc_ctlops_arg.dev_addr = dev_addr;
278300d0963fSdilpreet 	cautacc_ctlops_arg.host_addr = host_addr;
278400d0963fSdilpreet 	cautacc_ctlops_arg.handle = (ddi_acc_handle_t)hp;
278500d0963fSdilpreet 	cautacc_ctlops_arg.repcount = repcount;
278600d0963fSdilpreet 	cautacc_ctlops_arg.flags = flags;
278700d0963fSdilpreet 
278800d0963fSdilpreet 	(void) ddi_ctlops(hp->ahi_common.ah_dip, hp->ahi_common.ah_dip, cmd,
278900d0963fSdilpreet 	    &cautacc_ctlops_arg, NULL);
279000d0963fSdilpreet }
279100d0963fSdilpreet 
279200d0963fSdilpreet uint8_t
i_ddi_caut_get8(ddi_acc_impl_t * hp,uint8_t * addr)279300d0963fSdilpreet i_ddi_caut_get8(ddi_acc_impl_t *hp, uint8_t *addr)
279400d0963fSdilpreet {
279500d0963fSdilpreet 	uint8_t value;
279600d0963fSdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
279700d0963fSdilpreet 	    sizeof (uint8_t), 1, 0, DDI_CTLOPS_PEEK);
279800d0963fSdilpreet 
279900d0963fSdilpreet 	return (value);
280000d0963fSdilpreet }
280100d0963fSdilpreet 
280200d0963fSdilpreet uint16_t
i_ddi_caut_get16(ddi_acc_impl_t * hp,uint16_t * addr)280300d0963fSdilpreet i_ddi_caut_get16(ddi_acc_impl_t *hp, uint16_t *addr)
280400d0963fSdilpreet {
280500d0963fSdilpreet 	uint16_t value;
280600d0963fSdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
280700d0963fSdilpreet 	    sizeof (uint16_t), 1, 0, DDI_CTLOPS_PEEK);
280800d0963fSdilpreet 
280900d0963fSdilpreet 	return (value);
281000d0963fSdilpreet }
281100d0963fSdilpreet 
281200d0963fSdilpreet uint32_t
i_ddi_caut_get32(ddi_acc_impl_t * hp,uint32_t * addr)281300d0963fSdilpreet i_ddi_caut_get32(ddi_acc_impl_t *hp, uint32_t *addr)
281400d0963fSdilpreet {
281500d0963fSdilpreet 	uint32_t value;
281600d0963fSdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
281700d0963fSdilpreet 	    sizeof (uint32_t), 1, 0, DDI_CTLOPS_PEEK);
281800d0963fSdilpreet 
281900d0963fSdilpreet 	return (value);
282000d0963fSdilpreet }
282100d0963fSdilpreet 
282200d0963fSdilpreet uint64_t
i_ddi_caut_get64(ddi_acc_impl_t * hp,uint64_t * addr)282300d0963fSdilpreet i_ddi_caut_get64(ddi_acc_impl_t *hp, uint64_t *addr)
282400d0963fSdilpreet {
282500d0963fSdilpreet 	uint64_t value;
282600d0963fSdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
282700d0963fSdilpreet 	    sizeof (uint64_t), 1, 0, DDI_CTLOPS_PEEK);
282800d0963fSdilpreet 
282900d0963fSdilpreet 	return (value);
283000d0963fSdilpreet }
283100d0963fSdilpreet 
283200d0963fSdilpreet void
i_ddi_caut_put8(ddi_acc_impl_t * hp,uint8_t * addr,uint8_t value)283300d0963fSdilpreet i_ddi_caut_put8(ddi_acc_impl_t *hp, uint8_t *addr, uint8_t value)
283400d0963fSdilpreet {
283500d0963fSdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
283600d0963fSdilpreet 	    sizeof (uint8_t), 1, 0, DDI_CTLOPS_POKE);
283700d0963fSdilpreet }
283800d0963fSdilpreet 
283900d0963fSdilpreet void
i_ddi_caut_put16(ddi_acc_impl_t * hp,uint16_t * addr,uint16_t value)284000d0963fSdilpreet i_ddi_caut_put16(ddi_acc_impl_t *hp, uint16_t *addr, uint16_t value)
284100d0963fSdilpreet {
284200d0963fSdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
284300d0963fSdilpreet 	    sizeof (uint16_t), 1, 0, DDI_CTLOPS_POKE);
284400d0963fSdilpreet }
284500d0963fSdilpreet 
284600d0963fSdilpreet void
i_ddi_caut_put32(ddi_acc_impl_t * hp,uint32_t * addr,uint32_t value)284700d0963fSdilpreet i_ddi_caut_put32(ddi_acc_impl_t *hp, uint32_t *addr, uint32_t value)
284800d0963fSdilpreet {
284900d0963fSdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
285000d0963fSdilpreet 	    sizeof (uint32_t), 1, 0, DDI_CTLOPS_POKE);
285100d0963fSdilpreet }
285200d0963fSdilpreet 
285300d0963fSdilpreet void
i_ddi_caut_put64(ddi_acc_impl_t * hp,uint64_t * addr,uint64_t value)285400d0963fSdilpreet i_ddi_caut_put64(ddi_acc_impl_t *hp, uint64_t *addr, uint64_t value)
285500d0963fSdilpreet {
285600d0963fSdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
285700d0963fSdilpreet 	    sizeof (uint64_t), 1, 0, DDI_CTLOPS_POKE);
285800d0963fSdilpreet }
285900d0963fSdilpreet 
286000d0963fSdilpreet void
i_ddi_caut_rep_get8(ddi_acc_impl_t * hp,uint8_t * host_addr,uint8_t * dev_addr,size_t repcount,uint_t flags)286100d0963fSdilpreet i_ddi_caut_rep_get8(ddi_acc_impl_t *hp, uint8_t *host_addr, uint8_t *dev_addr,
286200d0963fSdilpreet     size_t repcount, uint_t flags)
286300d0963fSdilpreet {
286400d0963fSdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
286500d0963fSdilpreet 	    sizeof (uint8_t), repcount, flags, DDI_CTLOPS_PEEK);
286600d0963fSdilpreet }
286700d0963fSdilpreet 
286800d0963fSdilpreet void
i_ddi_caut_rep_get16(ddi_acc_impl_t * hp,uint16_t * host_addr,uint16_t * dev_addr,size_t repcount,uint_t flags)286900d0963fSdilpreet i_ddi_caut_rep_get16(ddi_acc_impl_t *hp, uint16_t *host_addr,
287000d0963fSdilpreet     uint16_t *dev_addr, size_t repcount, uint_t flags)
287100d0963fSdilpreet {
287200d0963fSdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
287300d0963fSdilpreet 	    sizeof (uint16_t), repcount, flags, DDI_CTLOPS_PEEK);
287400d0963fSdilpreet }
287500d0963fSdilpreet 
287600d0963fSdilpreet void
i_ddi_caut_rep_get32(ddi_acc_impl_t * hp,uint32_t * host_addr,uint32_t * dev_addr,size_t repcount,uint_t flags)287700d0963fSdilpreet i_ddi_caut_rep_get32(ddi_acc_impl_t *hp, uint32_t *host_addr,
287800d0963fSdilpreet     uint32_t *dev_addr, size_t repcount, uint_t flags)
287900d0963fSdilpreet {
288000d0963fSdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
288100d0963fSdilpreet 	    sizeof (uint32_t), repcount, flags, DDI_CTLOPS_PEEK);
288200d0963fSdilpreet }
288300d0963fSdilpreet 
288400d0963fSdilpreet void
i_ddi_caut_rep_get64(ddi_acc_impl_t * hp,uint64_t * host_addr,uint64_t * dev_addr,size_t repcount,uint_t flags)288500d0963fSdilpreet i_ddi_caut_rep_get64(ddi_acc_impl_t *hp, uint64_t *host_addr,
288600d0963fSdilpreet     uint64_t *dev_addr, size_t repcount, uint_t flags)
288700d0963fSdilpreet {
288800d0963fSdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
288900d0963fSdilpreet 	    sizeof (uint64_t), repcount, flags, DDI_CTLOPS_PEEK);
289000d0963fSdilpreet }
289100d0963fSdilpreet 
289200d0963fSdilpreet void
i_ddi_caut_rep_put8(ddi_acc_impl_t * hp,uint8_t * host_addr,uint8_t * dev_addr,size_t repcount,uint_t flags)289300d0963fSdilpreet i_ddi_caut_rep_put8(ddi_acc_impl_t *hp, uint8_t *host_addr, uint8_t *dev_addr,
289400d0963fSdilpreet     size_t repcount, uint_t flags)
289500d0963fSdilpreet {
289600d0963fSdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
289700d0963fSdilpreet 	    sizeof (uint8_t), repcount, flags, DDI_CTLOPS_POKE);
289800d0963fSdilpreet }
289900d0963fSdilpreet 
290000d0963fSdilpreet void
i_ddi_caut_rep_put16(ddi_acc_impl_t * hp,uint16_t * host_addr,uint16_t * dev_addr,size_t repcount,uint_t flags)290100d0963fSdilpreet i_ddi_caut_rep_put16(ddi_acc_impl_t *hp, uint16_t *host_addr,
290200d0963fSdilpreet     uint16_t *dev_addr, size_t repcount, uint_t flags)
290300d0963fSdilpreet {
290400d0963fSdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
290500d0963fSdilpreet 	    sizeof (uint16_t), repcount, flags, DDI_CTLOPS_POKE);
290600d0963fSdilpreet }
290700d0963fSdilpreet 
290800d0963fSdilpreet void
i_ddi_caut_rep_put32(ddi_acc_impl_t * hp,uint32_t * host_addr,uint32_t * dev_addr,size_t repcount,uint_t flags)290900d0963fSdilpreet i_ddi_caut_rep_put32(ddi_acc_impl_t *hp, uint32_t *host_addr,
291000d0963fSdilpreet     uint32_t *dev_addr, size_t repcount, uint_t flags)
291100d0963fSdilpreet {
291200d0963fSdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
291300d0963fSdilpreet 	    sizeof (uint32_t), repcount, flags, DDI_CTLOPS_POKE);
291400d0963fSdilpreet }
291500d0963fSdilpreet 
291600d0963fSdilpreet void
i_ddi_caut_rep_put64(ddi_acc_impl_t * hp,uint64_t * host_addr,uint64_t * dev_addr,size_t repcount,uint_t flags)291700d0963fSdilpreet i_ddi_caut_rep_put64(ddi_acc_impl_t *hp, uint64_t *host_addr,
291800d0963fSdilpreet     uint64_t *dev_addr, size_t repcount, uint_t flags)
291900d0963fSdilpreet {
292000d0963fSdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
292100d0963fSdilpreet 	    sizeof (uint64_t), repcount, flags, DDI_CTLOPS_POKE);
292200d0963fSdilpreet }
292336945f79Smrj 
292436945f79Smrj boolean_t
i_ddi_copybuf_required(ddi_dma_attr_t * attrp)292536945f79Smrj i_ddi_copybuf_required(ddi_dma_attr_t *attrp)
292636945f79Smrj {
292736945f79Smrj 	uint64_t hi_pa;
292836945f79Smrj 
292936945f79Smrj 	hi_pa = ((uint64_t)physmax + 1ull) << PAGESHIFT;
293036945f79Smrj 	if (attrp->dma_attr_addr_hi < hi_pa) {
293136945f79Smrj 		return (B_TRUE);
293236945f79Smrj 	}
293336945f79Smrj 
293436945f79Smrj 	return (B_FALSE);
293536945f79Smrj }
293636945f79Smrj 
293736945f79Smrj size_t
i_ddi_copybuf_size()293836945f79Smrj i_ddi_copybuf_size()
293936945f79Smrj {
294036945f79Smrj 	return (dma_max_copybuf_size);
294136945f79Smrj }
294236945f79Smrj 
294336945f79Smrj /*
294436945f79Smrj  * i_ddi_dma_max()
294536945f79Smrj  *    returns the maximum DMA size which can be performed in a single DMA
294636945f79Smrj  *    window taking into account the devices DMA contraints (attrp), the
294736945f79Smrj  *    maximum copy buffer size (if applicable), and the worse case buffer
294836945f79Smrj  *    fragmentation.
294936945f79Smrj  */
295036945f79Smrj /*ARGSUSED*/
295136945f79Smrj uint32_t
i_ddi_dma_max(dev_info_t * dip,ddi_dma_attr_t * attrp)295236945f79Smrj i_ddi_dma_max(dev_info_t *dip, ddi_dma_attr_t *attrp)
295336945f79Smrj {
295436945f79Smrj 	uint64_t maxxfer;
295536945f79Smrj 
295636945f79Smrj 
295736945f79Smrj 	/*
295836945f79Smrj 	 * take the min of maxxfer and the the worse case fragementation
295936945f79Smrj 	 * (e.g. every cookie <= 1 page)
296036945f79Smrj 	 */
296136945f79Smrj 	maxxfer = MIN(attrp->dma_attr_maxxfer,
296236945f79Smrj 	    ((uint64_t)(attrp->dma_attr_sgllen - 1) << PAGESHIFT));
296336945f79Smrj 
296436945f79Smrj 	/*
296536945f79Smrj 	 * If the DMA engine can't reach all off memory, we also need to take
296636945f79Smrj 	 * the max size of the copybuf into consideration.
296736945f79Smrj 	 */
296836945f79Smrj 	if (i_ddi_copybuf_required(attrp)) {
296936945f79Smrj 		maxxfer = MIN(i_ddi_copybuf_size(), maxxfer);
297036945f79Smrj 	}
297136945f79Smrj 
297236945f79Smrj 	/*
297336945f79Smrj 	 * we only return a 32-bit value. Make sure it's not -1. Round to a
297436945f79Smrj 	 * page so it won't be mistaken for an error value during debug.
297536945f79Smrj 	 */
297636945f79Smrj 	if (maxxfer >= 0xFFFFFFFF) {
297736945f79Smrj 		maxxfer = 0xFFFFF000;
297836945f79Smrj 	}
297936945f79Smrj 
298036945f79Smrj 	/*
298136945f79Smrj 	 * make sure the value we return is a whole multiple of the
298236945f79Smrj 	 * granlarity.
298336945f79Smrj 	 */
298436945f79Smrj 	if (attrp->dma_attr_granular > 1) {
298536945f79Smrj 		maxxfer = maxxfer - (maxxfer % attrp->dma_attr_granular);
298636945f79Smrj 	}
298736945f79Smrj 
298836945f79Smrj 	return ((uint32_t)maxxfer);
298936945f79Smrj }
2990a56d24eaSMark Johnson 
2991a56d24eaSMark Johnson pfn_t
i_ddi_paddr_to_pfn(paddr_t paddr)2992a56d24eaSMark Johnson i_ddi_paddr_to_pfn(paddr_t paddr)
2993a56d24eaSMark Johnson {
2994a56d24eaSMark Johnson 	pfn_t pfn;
2995a56d24eaSMark Johnson 
2996a56d24eaSMark Johnson #ifdef __xpv
2997a56d24eaSMark Johnson 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
2998a56d24eaSMark Johnson 		pfn = xen_assign_pfn(mmu_btop(paddr));
2999a56d24eaSMark Johnson 	} else {
3000a56d24eaSMark Johnson 		pfn = mmu_btop(paddr);
3001a56d24eaSMark Johnson 	}
3002a56d24eaSMark Johnson #else
3003a56d24eaSMark Johnson 	pfn = mmu_btop(paddr);
3004a56d24eaSMark Johnson #endif
3005a56d24eaSMark Johnson 
3006a56d24eaSMark Johnson 	return (pfn);
3007a56d24eaSMark Johnson }
3008