xref: /onnv-gate/usr/src/uts/i86pc/os/ddi_impl.c (revision 10394:5f92fec873b0)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51865Sdilpreet  * Common Development and Distribution License (the "License").
61865Sdilpreet  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211900Seota 
220Sstevel@tonic-gate /*
239149SJudy.Chen@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * PC specific DDI implementation
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/autoconf.h>
320Sstevel@tonic-gate #include <sys/avintr.h>
330Sstevel@tonic-gate #include <sys/bootconf.h>
340Sstevel@tonic-gate #include <sys/conf.h>
350Sstevel@tonic-gate #include <sys/cpuvar.h>
360Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
370Sstevel@tonic-gate #include <sys/ddi_subrdefs.h>
380Sstevel@tonic-gate #include <sys/ethernet.h>
390Sstevel@tonic-gate #include <sys/fp.h>
400Sstevel@tonic-gate #include <sys/instance.h>
410Sstevel@tonic-gate #include <sys/kmem.h>
420Sstevel@tonic-gate #include <sys/machsystm.h>
430Sstevel@tonic-gate #include <sys/modctl.h>
440Sstevel@tonic-gate #include <sys/promif.h>
450Sstevel@tonic-gate #include <sys/prom_plat.h>
460Sstevel@tonic-gate #include <sys/sunndi.h>
470Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
480Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
490Sstevel@tonic-gate #include <sys/sysmacros.h>
500Sstevel@tonic-gate #include <sys/systeminfo.h>
510Sstevel@tonic-gate #include <sys/utsname.h>
520Sstevel@tonic-gate #include <sys/atomic.h>
530Sstevel@tonic-gate #include <sys/spl.h>
540Sstevel@tonic-gate #include <sys/archsystm.h>
550Sstevel@tonic-gate #include <vm/seg_kmem.h>
560Sstevel@tonic-gate #include <sys/ontrap.h>
571865Sdilpreet #include <sys/fm/protocol.h>
580Sstevel@tonic-gate #include <sys/ramdisk.h>
590Sstevel@tonic-gate #include <sys/sunndi.h>
600Sstevel@tonic-gate #include <sys/vmem.h>
610Sstevel@tonic-gate #include <sys/pci_impl.h>
625084Sjohnlev #if defined(__xpv)
635084Sjohnlev #include <sys/hypervisor.h>
645084Sjohnlev #endif
65916Sschwartz #include <sys/mach_intr.h>
661900Seota #include <vm/hat_i86.h>
671900Seota #include <sys/x86_archext.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  * DDI Boot Configuration
710Sstevel@tonic-gate  */
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
745295Srandyf  * Platform drivers on this platform
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate char *platform_module_list[] = {
775295Srandyf 	"acpippm",
784667Smh27603 	"ppm",
790Sstevel@tonic-gate 	(char *)0
800Sstevel@tonic-gate };
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /* pci bus resource maps */
830Sstevel@tonic-gate struct pci_bus_resource *pci_bus_res;
840Sstevel@tonic-gate 
855251Smrj size_t dma_max_copybuf_size = 0x101000;		/* 1M + 4K */
865251Smrj 
870Sstevel@tonic-gate uint64_t ramdisk_start, ramdisk_end;
880Sstevel@tonic-gate 
899149SJudy.Chen@Sun.COM int pseudo_isa = 0;
909149SJudy.Chen@Sun.COM 
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate  * Forward declarations
930Sstevel@tonic-gate  */
940Sstevel@tonic-gate static int getlongprop_buf();
950Sstevel@tonic-gate static void get_boot_properties(void);
960Sstevel@tonic-gate static void impl_bus_initialprobe(void);
970Sstevel@tonic-gate static void impl_bus_reprobe(void);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate static int poke_mem(peekpoke_ctlops_t *in_args);
1000Sstevel@tonic-gate static int peek_mem(peekpoke_ctlops_t *in_args);
1010Sstevel@tonic-gate 
1021900Seota static int kmem_override_cache_attrs(caddr_t, size_t, uint_t);
1031900Seota 
1040Sstevel@tonic-gate #define	CTGENTRIES	15
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate static struct ctgas {
1070Sstevel@tonic-gate 	struct ctgas	*ctg_next;
1080Sstevel@tonic-gate 	int		ctg_index;
1090Sstevel@tonic-gate 	void		*ctg_addr[CTGENTRIES];
1100Sstevel@tonic-gate 	size_t		ctg_size[CTGENTRIES];
1110Sstevel@tonic-gate } ctglist;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate static kmutex_t		ctgmutex;
1140Sstevel@tonic-gate #define	CTGLOCK()	mutex_enter(&ctgmutex)
1150Sstevel@tonic-gate #define	CTGUNLOCK()	mutex_exit(&ctgmutex)
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate /*
1180Sstevel@tonic-gate  * Minimum pfn value of page_t's put on the free list.  This is to simplify
1190Sstevel@tonic-gate  * support of ddi dma memory requests which specify small, non-zero addr_lo
1200Sstevel@tonic-gate  * values.
1210Sstevel@tonic-gate  *
1220Sstevel@tonic-gate  * The default value of 2, which corresponds to the only known non-zero addr_lo
1230Sstevel@tonic-gate  * value used, means a single page will be sacrificed (pfn typically starts
1240Sstevel@tonic-gate  * at 1).  ddiphysmin can be set to 0 to disable. It cannot be set above 0x100
1250Sstevel@tonic-gate  * otherwise mp startup panics.
1260Sstevel@tonic-gate  */
1270Sstevel@tonic-gate pfn_t	ddiphysmin = 2;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate static void
1300Sstevel@tonic-gate check_driver_disable(void)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	int proplen = 128;
1330Sstevel@tonic-gate 	char *prop_name;
1340Sstevel@tonic-gate 	char *drv_name, *propval;
1350Sstevel@tonic-gate 	major_t major;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	prop_name = kmem_alloc(proplen, KM_SLEEP);
1380Sstevel@tonic-gate 	for (major = 0; major < devcnt; major++) {
1390Sstevel@tonic-gate 		drv_name = ddi_major_to_name(major);
1400Sstevel@tonic-gate 		if (drv_name == NULL)
1410Sstevel@tonic-gate 			continue;
1420Sstevel@tonic-gate 		(void) snprintf(prop_name, proplen, "disable-%s", drv_name);
1430Sstevel@tonic-gate 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
1440Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, prop_name, &propval) == DDI_SUCCESS) {
1450Sstevel@tonic-gate 			if (strcmp(propval, "true") == 0) {
1460Sstevel@tonic-gate 				devnamesp[major].dn_flags |= DN_DRIVER_REMOVED;
1470Sstevel@tonic-gate 				cmn_err(CE_NOTE, "driver %s disabled",
1480Sstevel@tonic-gate 				    drv_name);
1490Sstevel@tonic-gate 			}
1500Sstevel@tonic-gate 			ddi_prop_free(propval);
1510Sstevel@tonic-gate 		}
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate 	kmem_free(prop_name, proplen);
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate  * Configure the hardware on the system.
1590Sstevel@tonic-gate  * Called before the rootfs is mounted
1600Sstevel@tonic-gate  */
1610Sstevel@tonic-gate void
1620Sstevel@tonic-gate configure(void)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	extern void i_ddi_init_root();
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate #if defined(__i386)
1670Sstevel@tonic-gate 	extern int fpu_pentium_fdivbug;
1680Sstevel@tonic-gate #endif	/* __i386 */
1690Sstevel@tonic-gate 	extern int fpu_ignored;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	/*
1720Sstevel@tonic-gate 	 * Determine if an FPU is attached
1730Sstevel@tonic-gate 	 */
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	fpu_probe();
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate #if defined(__i386)
1780Sstevel@tonic-gate 	if (fpu_pentium_fdivbug) {
1790Sstevel@tonic-gate 		printf("\
1800Sstevel@tonic-gate FP hardware exhibits Pentium floating point divide problem\n");
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate #endif	/* __i386 */
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	if (fpu_ignored) {
1850Sstevel@tonic-gate 		printf("FP hardware will not be used\n");
1860Sstevel@tonic-gate 	} else if (!fpu_exists) {
1870Sstevel@tonic-gate 		printf("No FPU in configuration\n");
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	/*
1910Sstevel@tonic-gate 	 * Initialize devices on the machine.
1920Sstevel@tonic-gate 	 * Uses configuration tree built by the PROMs to determine what
1930Sstevel@tonic-gate 	 * is present, and builds a tree of prototype dev_info nodes
1940Sstevel@tonic-gate 	 * corresponding to the hardware which identified itself.
1950Sstevel@tonic-gate 	 */
1960Sstevel@tonic-gate #if !defined(SAS) && !defined(MPSAS)
1970Sstevel@tonic-gate 	/*
1980Sstevel@tonic-gate 	 * Check for disabled drivers and initialize root node.
1990Sstevel@tonic-gate 	 */
2000Sstevel@tonic-gate 	check_driver_disable();
2010Sstevel@tonic-gate 	i_ddi_init_root();
2020Sstevel@tonic-gate 
20310127SLipeng.Sang@Sun.COM 	/* reprogram devices not set up by firmware (BIOS) */
20410127SLipeng.Sang@Sun.COM 	impl_bus_reprobe();
20510127SLipeng.Sang@Sun.COM 
2060Sstevel@tonic-gate 	/*
2070Sstevel@tonic-gate 	 * attach the isa nexus to get ACPI resource usage
2080Sstevel@tonic-gate 	 * isa is "kind of" a pseudo node
2090Sstevel@tonic-gate 	 */
2105084Sjohnlev #if defined(__xpv)
2119149SJudy.Chen@Sun.COM 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
2129149SJudy.Chen@Sun.COM 		if (pseudo_isa)
2139149SJudy.Chen@Sun.COM 			(void) i_ddi_attach_pseudo_node("isa");
2149149SJudy.Chen@Sun.COM 		else
2159149SJudy.Chen@Sun.COM 			(void) i_ddi_attach_hw_nodes("isa");
2169149SJudy.Chen@Sun.COM 	}
2179149SJudy.Chen@Sun.COM #else
2189149SJudy.Chen@Sun.COM 	if (pseudo_isa)
2195084Sjohnlev 		(void) i_ddi_attach_pseudo_node("isa");
2209149SJudy.Chen@Sun.COM 	else
2219149SJudy.Chen@Sun.COM 		(void) i_ddi_attach_hw_nodes("isa");
2225084Sjohnlev #endif
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate #endif	/* !SAS && !MPSAS */
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate /*
2280Sstevel@tonic-gate  * The "status" property indicates the operational status of a device.
2290Sstevel@tonic-gate  * If this property is present, the value is a string indicating the
2300Sstevel@tonic-gate  * status of the device as follows:
2310Sstevel@tonic-gate  *
2320Sstevel@tonic-gate  *	"okay"		operational.
2330Sstevel@tonic-gate  *	"disabled"	not operational, but might become operational.
2340Sstevel@tonic-gate  *	"fail"		not operational because a fault has been detected,
2350Sstevel@tonic-gate  *			and it is unlikely that the device will become
2360Sstevel@tonic-gate  *			operational without repair. no additional details
2370Sstevel@tonic-gate  *			are available.
2380Sstevel@tonic-gate  *	"fail-xxx"	not operational because a fault has been detected,
2390Sstevel@tonic-gate  *			and it is unlikely that the device will become
2400Sstevel@tonic-gate  *			operational without repair. "xxx" is additional
2410Sstevel@tonic-gate  *			human-readable information about the particular
2420Sstevel@tonic-gate  *			fault condition that was detected.
2430Sstevel@tonic-gate  *
2440Sstevel@tonic-gate  * The absence of this property means that the operational status is
2450Sstevel@tonic-gate  * unknown or okay.
2460Sstevel@tonic-gate  *
2470Sstevel@tonic-gate  * This routine checks the status property of the specified device node
2480Sstevel@tonic-gate  * and returns 0 if the operational status indicates failure, and 1 otherwise.
2490Sstevel@tonic-gate  *
2500Sstevel@tonic-gate  * The property may exist on plug-in cards the existed before IEEE 1275-1994.
2510Sstevel@tonic-gate  * And, in that case, the property may not even be a string. So we carefully
2520Sstevel@tonic-gate  * check for the value "fail", in the beginning of the string, noting
2530Sstevel@tonic-gate  * the property length.
2540Sstevel@tonic-gate  */
2550Sstevel@tonic-gate int
2560Sstevel@tonic-gate status_okay(int id, char *buf, int buflen)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate 	char status_buf[OBP_MAXPROPNAME];
2590Sstevel@tonic-gate 	char *bufp = buf;
2600Sstevel@tonic-gate 	int len = buflen;
2610Sstevel@tonic-gate 	int proplen;
2620Sstevel@tonic-gate 	static const char *status = "status";
2630Sstevel@tonic-gate 	static const char *fail = "fail";
2640Sstevel@tonic-gate 	int fail_len = (int)strlen(fail);
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	/*
2670Sstevel@tonic-gate 	 * Get the proplen ... if it's smaller than "fail",
2680Sstevel@tonic-gate 	 * or doesn't exist ... then we don't care, since
2690Sstevel@tonic-gate 	 * the value can't begin with the char string "fail".
2700Sstevel@tonic-gate 	 *
2710Sstevel@tonic-gate 	 * NB: proplen, if it's a string, includes the NULL in the
2720Sstevel@tonic-gate 	 * the size of the property, and fail_len does not.
2730Sstevel@tonic-gate 	 */
274789Sahrens 	proplen = prom_getproplen((pnode_t)id, (caddr_t)status);
2750Sstevel@tonic-gate 	if (proplen <= fail_len)	/* nonexistant or uninteresting len */
2760Sstevel@tonic-gate 		return (1);
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	/*
2790Sstevel@tonic-gate 	 * if a buffer was provided, use it
2800Sstevel@tonic-gate 	 */
2810Sstevel@tonic-gate 	if ((buf == (char *)NULL) || (buflen <= 0)) {
2820Sstevel@tonic-gate 		bufp = status_buf;
2830Sstevel@tonic-gate 		len = sizeof (status_buf);
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 	*bufp = (char)0;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	/*
2880Sstevel@tonic-gate 	 * Get the property into the buffer, to the extent of the buffer,
2890Sstevel@tonic-gate 	 * and in case the buffer is smaller than the property size,
2900Sstevel@tonic-gate 	 * NULL terminate the buffer. (This handles the case where
2910Sstevel@tonic-gate 	 * a buffer was passed in and the caller wants to print the
2920Sstevel@tonic-gate 	 * value, but the buffer was too small).
2930Sstevel@tonic-gate 	 */
294789Sahrens 	(void) prom_bounded_getprop((pnode_t)id, (caddr_t)status,
2950Sstevel@tonic-gate 	    (caddr_t)bufp, len);
2960Sstevel@tonic-gate 	*(bufp + len - 1) = (char)0;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	/*
2990Sstevel@tonic-gate 	 * If the value begins with the char string "fail",
3000Sstevel@tonic-gate 	 * then it means the node is failed. We don't care
3010Sstevel@tonic-gate 	 * about any other values. We assume the node is ok
3020Sstevel@tonic-gate 	 * although it might be 'disabled'.
3030Sstevel@tonic-gate 	 */
3040Sstevel@tonic-gate 	if (strncmp(bufp, fail, fail_len) == 0)
3050Sstevel@tonic-gate 		return (0);
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	return (1);
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate /*
3110Sstevel@tonic-gate  * Check the status of the device node passed as an argument.
3120Sstevel@tonic-gate  *
3130Sstevel@tonic-gate  *	if ((status is OKAY) || (status is DISABLED))
3140Sstevel@tonic-gate  *		return DDI_SUCCESS
3150Sstevel@tonic-gate  *	else
3160Sstevel@tonic-gate  *		print a warning and return DDI_FAILURE
3170Sstevel@tonic-gate  */
3180Sstevel@tonic-gate /*ARGSUSED1*/
3190Sstevel@tonic-gate int
3200Sstevel@tonic-gate check_status(int id, char *name, dev_info_t *parent)
3210Sstevel@tonic-gate {
3220Sstevel@tonic-gate 	char status_buf[64];
3230Sstevel@tonic-gate 	char devtype_buf[OBP_MAXPROPNAME];
3240Sstevel@tonic-gate 	int retval = DDI_FAILURE;
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	/*
3270Sstevel@tonic-gate 	 * is the status okay?
3280Sstevel@tonic-gate 	 */
3290Sstevel@tonic-gate 	if (status_okay(id, status_buf, sizeof (status_buf)))
3300Sstevel@tonic-gate 		return (DDI_SUCCESS);
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	/*
3330Sstevel@tonic-gate 	 * a status property indicating bad memory will be associated
3340Sstevel@tonic-gate 	 * with a node which has a "device_type" property with a value of
3350Sstevel@tonic-gate 	 * "memory-controller". in this situation, return DDI_SUCCESS
3360Sstevel@tonic-gate 	 */
3370Sstevel@tonic-gate 	if (getlongprop_buf(id, OBP_DEVICETYPE, devtype_buf,
3380Sstevel@tonic-gate 	    sizeof (devtype_buf)) > 0) {
3390Sstevel@tonic-gate 		if (strcmp(devtype_buf, "memory-controller") == 0)
3400Sstevel@tonic-gate 			retval = DDI_SUCCESS;
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	/*
3440Sstevel@tonic-gate 	 * print the status property information
3450Sstevel@tonic-gate 	 */
3460Sstevel@tonic-gate 	cmn_err(CE_WARN, "status '%s' for '%s'", status_buf, name);
3470Sstevel@tonic-gate 	return (retval);
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate /*ARGSUSED*/
3510Sstevel@tonic-gate uint_t
3520Sstevel@tonic-gate softlevel1(caddr_t arg1, caddr_t arg2)
3530Sstevel@tonic-gate {
3540Sstevel@tonic-gate 	softint();
3550Sstevel@tonic-gate 	return (1);
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate /*
3590Sstevel@tonic-gate  * Allow for implementation specific correction of PROM property values.
3600Sstevel@tonic-gate  */
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate /*ARGSUSED*/
3630Sstevel@tonic-gate void
3640Sstevel@tonic-gate impl_fix_props(dev_info_t *dip, dev_info_t *ch_dip, char *name, int len,
3650Sstevel@tonic-gate     caddr_t buffer)
3660Sstevel@tonic-gate {
3670Sstevel@tonic-gate 	/*
3680Sstevel@tonic-gate 	 * There are no adjustments needed in this implementation.
3690Sstevel@tonic-gate 	 */
3700Sstevel@tonic-gate }
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate static int
3730Sstevel@tonic-gate getlongprop_buf(int id, char *name, char *buf, int maxlen)
3740Sstevel@tonic-gate {
3750Sstevel@tonic-gate 	int size;
3760Sstevel@tonic-gate 
377789Sahrens 	size = prom_getproplen((pnode_t)id, name);
3780Sstevel@tonic-gate 	if (size <= 0 || (size > maxlen - 1))
3790Sstevel@tonic-gate 		return (-1);
3800Sstevel@tonic-gate 
381789Sahrens 	if (-1 == prom_getprop((pnode_t)id, name, buf))
3820Sstevel@tonic-gate 		return (-1);
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	if (strcmp("name", name) == 0) {
3850Sstevel@tonic-gate 		if (buf[size - 1] != '\0') {
3860Sstevel@tonic-gate 			buf[size] = '\0';
3870Sstevel@tonic-gate 			size += 1;
3880Sstevel@tonic-gate 		}
3890Sstevel@tonic-gate 	}
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	return (size);
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate static int
3950Sstevel@tonic-gate get_prop_int_array(dev_info_t *di, char *pname, int **pval, uint_t *plen)
3960Sstevel@tonic-gate {
3970Sstevel@tonic-gate 	int ret;
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	if ((ret = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, di,
4000Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, pname, pval, plen))
4010Sstevel@tonic-gate 	    == DDI_PROP_SUCCESS) {
4020Sstevel@tonic-gate 		*plen = (*plen) * (sizeof (int));
4030Sstevel@tonic-gate 	}
4040Sstevel@tonic-gate 	return (ret);
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate /*
4090Sstevel@tonic-gate  * Node Configuration
4100Sstevel@tonic-gate  */
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate struct prop_ispec {
4130Sstevel@tonic-gate 	uint_t	pri, vec;
4140Sstevel@tonic-gate };
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate /*
417693Sgovinda  * For the x86, we're prepared to claim that the interrupt string
418693Sgovinda  * is in the form of a list of <ipl,vec> specifications.
419693Sgovinda  */
420693Sgovinda 
421693Sgovinda #define	VEC_MIN	1
422693Sgovinda #define	VEC_MAX	255
423693Sgovinda 
424693Sgovinda static int
425693Sgovinda impl_xlate_intrs(dev_info_t *child, int *in,
426693Sgovinda     struct ddi_parent_private_data *pdptr)
427693Sgovinda {
428693Sgovinda 	size_t size;
429693Sgovinda 	int n;
430693Sgovinda 	struct intrspec *new;
431693Sgovinda 	caddr_t got_prop;
432693Sgovinda 	int *inpri;
433693Sgovinda 	int got_len;
434693Sgovinda 	extern int ignore_hardware_nodes;	/* force flag from ddi_impl.c */
435693Sgovinda 
436693Sgovinda 	static char bad_intr_fmt[] =
437693Sgovinda 	    "bad interrupt spec from %s%d - ipl %d, irq %d\n";
438693Sgovinda 
439693Sgovinda 	/*
440693Sgovinda 	 * determine if the driver is expecting the new style "interrupts"
441693Sgovinda 	 * property which just contains the IRQ, or the old style which
442693Sgovinda 	 * contains pairs of <IPL,IRQ>.  if it is the new style, we always
443693Sgovinda 	 * assign IPL 5 unless an "interrupt-priorities" property exists.
444693Sgovinda 	 * in that case, the "interrupt-priorities" property contains the
445693Sgovinda 	 * IPL values that match, one for one, the IRQ values in the
446693Sgovinda 	 * "interrupts" property.
447693Sgovinda 	 */
448693Sgovinda 	inpri = NULL;
449693Sgovinda 	if ((ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
450693Sgovinda 	    "ignore-hardware-nodes", -1) != -1) || ignore_hardware_nodes) {
451693Sgovinda 		/* the old style "interrupts" property... */
452693Sgovinda 
453693Sgovinda 		/*
454693Sgovinda 		 * The list consists of <ipl,vec> elements
455693Sgovinda 		 */
456693Sgovinda 		if ((n = (*in++ >> 1)) < 1)
457693Sgovinda 			return (DDI_FAILURE);
458693Sgovinda 
459693Sgovinda 		pdptr->par_nintr = n;
460693Sgovinda 		size = n * sizeof (struct intrspec);
461693Sgovinda 		new = pdptr->par_intr = kmem_zalloc(size, KM_SLEEP);
462693Sgovinda 
463693Sgovinda 		while (n--) {
464693Sgovinda 			int level = *in++;
465693Sgovinda 			int vec = *in++;
466693Sgovinda 
467693Sgovinda 			if (level < 1 || level > MAXIPL ||
468693Sgovinda 			    vec < VEC_MIN || vec > VEC_MAX) {
469693Sgovinda 				cmn_err(CE_CONT, bad_intr_fmt,
470693Sgovinda 				    DEVI(child)->devi_name,
471693Sgovinda 				    DEVI(child)->devi_instance, level, vec);
472693Sgovinda 				goto broken;
473693Sgovinda 			}
474693Sgovinda 			new->intrspec_pri = level;
475693Sgovinda 			if (vec != 2)
476693Sgovinda 				new->intrspec_vec = vec;
477693Sgovinda 			else
478693Sgovinda 				/*
479693Sgovinda 				 * irq 2 on the PC bus is tied to irq 9
480693Sgovinda 				 * on ISA, EISA and MicroChannel
481693Sgovinda 				 */
482693Sgovinda 				new->intrspec_vec = 9;
483693Sgovinda 			new++;
484693Sgovinda 		}
485693Sgovinda 
486693Sgovinda 		return (DDI_SUCCESS);
487693Sgovinda 	} else {
488693Sgovinda 		/* the new style "interrupts" property... */
489693Sgovinda 
490693Sgovinda 		/*
491693Sgovinda 		 * The list consists of <vec> elements
492693Sgovinda 		 */
493693Sgovinda 		if ((n = (*in++)) < 1)
494693Sgovinda 			return (DDI_FAILURE);
495693Sgovinda 
496693Sgovinda 		pdptr->par_nintr = n;
497693Sgovinda 		size = n * sizeof (struct intrspec);
498693Sgovinda 		new = pdptr->par_intr = kmem_zalloc(size, KM_SLEEP);
499693Sgovinda 
500693Sgovinda 		/* XXX check for "interrupt-priorities" property... */
501693Sgovinda 		if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
502693Sgovinda 		    "interrupt-priorities", (caddr_t)&got_prop, &got_len)
503693Sgovinda 		    == DDI_PROP_SUCCESS) {
504693Sgovinda 			if (n != (got_len / sizeof (int))) {
505693Sgovinda 				cmn_err(CE_CONT,
506693Sgovinda 				    "bad interrupt-priorities length"
507693Sgovinda 				    " from %s%d: expected %d, got %d\n",
508693Sgovinda 				    DEVI(child)->devi_name,
509693Sgovinda 				    DEVI(child)->devi_instance, n,
510693Sgovinda 				    (int)(got_len / sizeof (int)));
511693Sgovinda 				goto broken;
512693Sgovinda 			}
513693Sgovinda 			inpri = (int *)got_prop;
514693Sgovinda 		}
515693Sgovinda 
516693Sgovinda 		while (n--) {
517693Sgovinda 			int level;
518693Sgovinda 			int vec = *in++;
519693Sgovinda 
520693Sgovinda 			if (inpri == NULL)
521693Sgovinda 				level = 5;
522693Sgovinda 			else
523693Sgovinda 				level = *inpri++;
524693Sgovinda 
525693Sgovinda 			if (level < 1 || level > MAXIPL ||
526693Sgovinda 			    vec < VEC_MIN || vec > VEC_MAX) {
527693Sgovinda 				cmn_err(CE_CONT, bad_intr_fmt,
528693Sgovinda 				    DEVI(child)->devi_name,
529693Sgovinda 				    DEVI(child)->devi_instance, level, vec);
530693Sgovinda 				goto broken;
531693Sgovinda 			}
532693Sgovinda 			new->intrspec_pri = level;
533693Sgovinda 			if (vec != 2)
534693Sgovinda 				new->intrspec_vec = vec;
535693Sgovinda 			else
536693Sgovinda 				/*
537693Sgovinda 				 * irq 2 on the PC bus is tied to irq 9
538693Sgovinda 				 * on ISA, EISA and MicroChannel
539693Sgovinda 				 */
540693Sgovinda 				new->intrspec_vec = 9;
541693Sgovinda 			new++;
542693Sgovinda 		}
543693Sgovinda 
544693Sgovinda 		if (inpri != NULL)
545693Sgovinda 			kmem_free(got_prop, got_len);
546693Sgovinda 		return (DDI_SUCCESS);
547693Sgovinda 	}
548693Sgovinda 
549693Sgovinda broken:
550693Sgovinda 	kmem_free(pdptr->par_intr, size);
551693Sgovinda 	pdptr->par_intr = NULL;
552693Sgovinda 	pdptr->par_nintr = 0;
553693Sgovinda 	if (inpri != NULL)
554693Sgovinda 		kmem_free(got_prop, got_len);
555693Sgovinda 
556693Sgovinda 	return (DDI_FAILURE);
557693Sgovinda }
558693Sgovinda 
559693Sgovinda /*
5600Sstevel@tonic-gate  * Create a ddi_parent_private_data structure from the ddi properties of
5610Sstevel@tonic-gate  * the dev_info node.
5620Sstevel@tonic-gate  *
5630Sstevel@tonic-gate  * The "reg" and either an "intr" or "interrupts" properties are required
5640Sstevel@tonic-gate  * if the driver wishes to create mappings or field interrupts on behalf
5650Sstevel@tonic-gate  * of the device.
5660Sstevel@tonic-gate  *
5670Sstevel@tonic-gate  * The "reg" property is assumed to be a list of at least one triple
5680Sstevel@tonic-gate  *
5690Sstevel@tonic-gate  *	<bustype, address, size>*1
5700Sstevel@tonic-gate  *
5710Sstevel@tonic-gate  * The "intr" property is assumed to be a list of at least one duple
5720Sstevel@tonic-gate  *
5730Sstevel@tonic-gate  *	<SPARC ipl, vector#>*1
5740Sstevel@tonic-gate  *
5750Sstevel@tonic-gate  * The "interrupts" property is assumed to be a list of at least one
5760Sstevel@tonic-gate  * n-tuples that describes the interrupt capabilities of the bus the device
5770Sstevel@tonic-gate  * is connected to.  For SBus, this looks like
5780Sstevel@tonic-gate  *
5790Sstevel@tonic-gate  *	<SBus-level>*1
5800Sstevel@tonic-gate  *
5810Sstevel@tonic-gate  * (This property obsoletes the 'intr' property).
5820Sstevel@tonic-gate  *
5830Sstevel@tonic-gate  * The "ranges" property is optional.
5840Sstevel@tonic-gate  */
5850Sstevel@tonic-gate void
5860Sstevel@tonic-gate make_ddi_ppd(dev_info_t *child, struct ddi_parent_private_data **ppd)
5870Sstevel@tonic-gate {
5880Sstevel@tonic-gate 	struct ddi_parent_private_data *pdptr;
5890Sstevel@tonic-gate 	int n;
5900Sstevel@tonic-gate 	int *reg_prop, *rng_prop, *intr_prop, *irupts_prop;
5910Sstevel@tonic-gate 	uint_t reg_len, rng_len, intr_len, irupts_len;
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	*ppd = pdptr = kmem_zalloc(sizeof (*pdptr), KM_SLEEP);
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	/*
5960Sstevel@tonic-gate 	 * Handle the 'reg' property.
5970Sstevel@tonic-gate 	 */
5980Sstevel@tonic-gate 	if ((get_prop_int_array(child, "reg", &reg_prop, &reg_len) ==
5990Sstevel@tonic-gate 	    DDI_PROP_SUCCESS) && (reg_len != 0)) {
6000Sstevel@tonic-gate 		pdptr->par_nreg = reg_len / (int)sizeof (struct regspec);
6010Sstevel@tonic-gate 		pdptr->par_reg = (struct regspec *)reg_prop;
6020Sstevel@tonic-gate 	}
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	/*
6050Sstevel@tonic-gate 	 * See if I have a range (adding one where needed - this
6060Sstevel@tonic-gate 	 * means to add one for sbus node in sun4c, when romvec > 0,
6070Sstevel@tonic-gate 	 * if no range is already defined in the PROM node.
6080Sstevel@tonic-gate 	 * (Currently no sun4c PROMS define range properties,
6090Sstevel@tonic-gate 	 * but they should and may in the future.)  For the SBus
6100Sstevel@tonic-gate 	 * node, the range is defined by the SBus reg property.
6110Sstevel@tonic-gate 	 */
6120Sstevel@tonic-gate 	if (get_prop_int_array(child, "ranges", &rng_prop, &rng_len)
6130Sstevel@tonic-gate 	    == DDI_PROP_SUCCESS) {
6140Sstevel@tonic-gate 		pdptr->par_nrng = rng_len / (int)(sizeof (struct rangespec));
6150Sstevel@tonic-gate 		pdptr->par_rng = (struct rangespec *)rng_prop;
6160Sstevel@tonic-gate 	}
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	/*
6190Sstevel@tonic-gate 	 * Handle the 'intr' and 'interrupts' properties
6200Sstevel@tonic-gate 	 */
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	/*
6230Sstevel@tonic-gate 	 * For backwards compatibility
6240Sstevel@tonic-gate 	 * we first look for the 'intr' property for the device.
6250Sstevel@tonic-gate 	 */
6260Sstevel@tonic-gate 	if (get_prop_int_array(child, "intr", &intr_prop, &intr_len)
6270Sstevel@tonic-gate 	    != DDI_PROP_SUCCESS) {
6280Sstevel@tonic-gate 		intr_len = 0;
6290Sstevel@tonic-gate 	}
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	/*
6320Sstevel@tonic-gate 	 * If we're to support bus adapters and future platforms cleanly,
6330Sstevel@tonic-gate 	 * we need to support the generalized 'interrupts' property.
6340Sstevel@tonic-gate 	 */
6350Sstevel@tonic-gate 	if (get_prop_int_array(child, "interrupts", &irupts_prop,
6360Sstevel@tonic-gate 	    &irupts_len) != DDI_PROP_SUCCESS) {
6370Sstevel@tonic-gate 		irupts_len = 0;
6380Sstevel@tonic-gate 	} else if (intr_len != 0) {
6390Sstevel@tonic-gate 		/*
6400Sstevel@tonic-gate 		 * If both 'intr' and 'interrupts' are defined,
6410Sstevel@tonic-gate 		 * then 'interrupts' wins and we toss the 'intr' away.
6420Sstevel@tonic-gate 		 */
6430Sstevel@tonic-gate 		ddi_prop_free((void *)intr_prop);
6440Sstevel@tonic-gate 		intr_len = 0;
6450Sstevel@tonic-gate 	}
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	if (intr_len != 0) {
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 		/*
6500Sstevel@tonic-gate 		 * Translate the 'intr' property into an array
6510Sstevel@tonic-gate 		 * an array of struct intrspec's.  There's not really
6520Sstevel@tonic-gate 		 * very much to do here except copy what's out there.
6530Sstevel@tonic-gate 		 */
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 		struct intrspec *new;
6560Sstevel@tonic-gate 		struct prop_ispec *l;
6570Sstevel@tonic-gate 
6585084Sjohnlev 		n = pdptr->par_nintr = intr_len / sizeof (struct prop_ispec);
6590Sstevel@tonic-gate 		l = (struct prop_ispec *)intr_prop;
6600Sstevel@tonic-gate 		pdptr->par_intr =
6610Sstevel@tonic-gate 		    new = kmem_zalloc(n * sizeof (struct intrspec), KM_SLEEP);
6620Sstevel@tonic-gate 		while (n--) {
6630Sstevel@tonic-gate 			new->intrspec_pri = l->pri;
6640Sstevel@tonic-gate 			new->intrspec_vec = l->vec;
6650Sstevel@tonic-gate 			new++;
6660Sstevel@tonic-gate 			l++;
6670Sstevel@tonic-gate 		}
6680Sstevel@tonic-gate 		ddi_prop_free((void *)intr_prop);
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	} else if ((n = irupts_len) != 0) {
6710Sstevel@tonic-gate 		size_t size;
6720Sstevel@tonic-gate 		int *out;
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 		/*
6750Sstevel@tonic-gate 		 * Translate the 'interrupts' property into an array
6760Sstevel@tonic-gate 		 * of intrspecs for the rest of the DDI framework to
6770Sstevel@tonic-gate 		 * toy with.  Only our ancestors really know how to
6780Sstevel@tonic-gate 		 * do this, so ask 'em.  We massage the 'interrupts'
6790Sstevel@tonic-gate 		 * property so that it is pre-pended by a count of
6800Sstevel@tonic-gate 		 * the number of integers in the argument.
6810Sstevel@tonic-gate 		 */
6820Sstevel@tonic-gate 		size = sizeof (int) + n;
6830Sstevel@tonic-gate 		out = kmem_alloc(size, KM_SLEEP);
6840Sstevel@tonic-gate 		*out = n / sizeof (int);
6850Sstevel@tonic-gate 		bcopy(irupts_prop, out + 1, (size_t)n);
6860Sstevel@tonic-gate 		ddi_prop_free((void *)irupts_prop);
687693Sgovinda 		if (impl_xlate_intrs(child, out, pdptr) != DDI_SUCCESS) {
6880Sstevel@tonic-gate 			cmn_err(CE_CONT,
6890Sstevel@tonic-gate 			    "Unable to translate 'interrupts' for %s%d\n",
6900Sstevel@tonic-gate 			    DEVI(child)->devi_binding_name,
6910Sstevel@tonic-gate 			    DEVI(child)->devi_instance);
6920Sstevel@tonic-gate 		}
6930Sstevel@tonic-gate 		kmem_free(out, size);
6940Sstevel@tonic-gate 	}
6950Sstevel@tonic-gate }
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate /*
6980Sstevel@tonic-gate  * Name a child
6990Sstevel@tonic-gate  */
7000Sstevel@tonic-gate static int
7010Sstevel@tonic-gate impl_sunbus_name_child(dev_info_t *child, char *name, int namelen)
7020Sstevel@tonic-gate {
7030Sstevel@tonic-gate 	/*
7040Sstevel@tonic-gate 	 * Fill in parent-private data and this function returns to us
7050Sstevel@tonic-gate 	 * an indication if it used "registers" to fill in the data.
7060Sstevel@tonic-gate 	 */
7070Sstevel@tonic-gate 	if (ddi_get_parent_data(child) == NULL) {
7080Sstevel@tonic-gate 		struct ddi_parent_private_data *pdptr;
7090Sstevel@tonic-gate 		make_ddi_ppd(child, &pdptr);
7100Sstevel@tonic-gate 		ddi_set_parent_data(child, pdptr);
7110Sstevel@tonic-gate 	}
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	name[0] = '\0';
7140Sstevel@tonic-gate 	if (sparc_pd_getnreg(child) > 0) {
7150Sstevel@tonic-gate 		(void) snprintf(name, namelen, "%x,%x",
7160Sstevel@tonic-gate 		    (uint_t)sparc_pd_getreg(child, 0)->regspec_bustype,
7170Sstevel@tonic-gate 		    (uint_t)sparc_pd_getreg(child, 0)->regspec_addr);
7180Sstevel@tonic-gate 	}
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	return (DDI_SUCCESS);
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate /*
7240Sstevel@tonic-gate  * Called from the bus_ctl op of sunbus (sbus, obio, etc) nexus drivers
7250Sstevel@tonic-gate  * to implement the DDI_CTLOPS_INITCHILD operation.  That is, it names
7260Sstevel@tonic-gate  * the children of sun busses based on the reg spec.
7270Sstevel@tonic-gate  *
7280Sstevel@tonic-gate  * Handles the following properties (in make_ddi_ppd):
7290Sstevel@tonic-gate  *	Property		value
7300Sstevel@tonic-gate  *	  Name			type
7310Sstevel@tonic-gate  *	reg		register spec
7320Sstevel@tonic-gate  *	intr		old-form interrupt spec
7330Sstevel@tonic-gate  *	interrupts	new (bus-oriented) interrupt spec
7340Sstevel@tonic-gate  *	ranges		range spec
7350Sstevel@tonic-gate  */
7360Sstevel@tonic-gate int
7370Sstevel@tonic-gate impl_ddi_sunbus_initchild(dev_info_t *child)
7380Sstevel@tonic-gate {
7390Sstevel@tonic-gate 	char name[MAXNAMELEN];
7400Sstevel@tonic-gate 	void impl_ddi_sunbus_removechild(dev_info_t *);
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	/*
7430Sstevel@tonic-gate 	 * Name the child, also makes parent private data
7440Sstevel@tonic-gate 	 */
7450Sstevel@tonic-gate 	(void) impl_sunbus_name_child(child, name, MAXNAMELEN);
7460Sstevel@tonic-gate 	ddi_set_name_addr(child, name);
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	/*
7490Sstevel@tonic-gate 	 * Attempt to merge a .conf node; if successful, remove the
7500Sstevel@tonic-gate 	 * .conf node.
7510Sstevel@tonic-gate 	 */
7520Sstevel@tonic-gate 	if ((ndi_dev_is_persistent_node(child) == 0) &&
7530Sstevel@tonic-gate 	    (ndi_merge_node(child, impl_sunbus_name_child) == DDI_SUCCESS)) {
7540Sstevel@tonic-gate 		/*
7550Sstevel@tonic-gate 		 * Return failure to remove node
7560Sstevel@tonic-gate 		 */
7570Sstevel@tonic-gate 		impl_ddi_sunbus_removechild(child);
7580Sstevel@tonic-gate 		return (DDI_FAILURE);
7590Sstevel@tonic-gate 	}
7600Sstevel@tonic-gate 	return (DDI_SUCCESS);
7610Sstevel@tonic-gate }
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate void
7640Sstevel@tonic-gate impl_free_ddi_ppd(dev_info_t *dip)
7650Sstevel@tonic-gate {
7660Sstevel@tonic-gate 	struct ddi_parent_private_data *pdptr;
7670Sstevel@tonic-gate 	size_t n;
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	if ((pdptr = ddi_get_parent_data(dip)) == NULL)
7700Sstevel@tonic-gate 		return;
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	if ((n = (size_t)pdptr->par_nintr) != 0)
7730Sstevel@tonic-gate 		/*
7740Sstevel@tonic-gate 		 * Note that kmem_free is used here (instead of
7750Sstevel@tonic-gate 		 * ddi_prop_free) because the contents of the
7760Sstevel@tonic-gate 		 * property were placed into a separate buffer and
7770Sstevel@tonic-gate 		 * mucked with a bit before being stored in par_intr.
7780Sstevel@tonic-gate 		 * The actual return value from the prop lookup
7790Sstevel@tonic-gate 		 * was freed with ddi_prop_free previously.
7800Sstevel@tonic-gate 		 */
7810Sstevel@tonic-gate 		kmem_free(pdptr->par_intr, n * sizeof (struct intrspec));
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	if ((n = (size_t)pdptr->par_nrng) != 0)
7840Sstevel@tonic-gate 		ddi_prop_free((void *)pdptr->par_rng);
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	if ((n = pdptr->par_nreg) != 0)
7870Sstevel@tonic-gate 		ddi_prop_free((void *)pdptr->par_reg);
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	kmem_free(pdptr, sizeof (*pdptr));
7900Sstevel@tonic-gate 	ddi_set_parent_data(dip, NULL);
7910Sstevel@tonic-gate }
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate void
7940Sstevel@tonic-gate impl_ddi_sunbus_removechild(dev_info_t *dip)
7950Sstevel@tonic-gate {
7960Sstevel@tonic-gate 	impl_free_ddi_ppd(dip);
7970Sstevel@tonic-gate 	ddi_set_name_addr(dip, NULL);
7980Sstevel@tonic-gate 	/*
7990Sstevel@tonic-gate 	 * Strip the node to properly convert it back to prototype form
8000Sstevel@tonic-gate 	 */
8010Sstevel@tonic-gate 	impl_rem_dev_props(dip);
8020Sstevel@tonic-gate }
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate /*
8050Sstevel@tonic-gate  * DDI Interrupt
8060Sstevel@tonic-gate  */
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate /*
8090Sstevel@tonic-gate  * turn this on to force isa, eisa, and mca device to ignore the new
8100Sstevel@tonic-gate  * hardware nodes in the device tree (normally turned on only for
8110Sstevel@tonic-gate  * drivers that need it by setting the property "ignore-hardware-nodes"
8120Sstevel@tonic-gate  * in their driver.conf file).
8130Sstevel@tonic-gate  *
8140Sstevel@tonic-gate  * 7/31/96 -- Turned off globally.  Leaving variable in for the moment
8150Sstevel@tonic-gate  *		as safety valve.
8160Sstevel@tonic-gate  */
8170Sstevel@tonic-gate int ignore_hardware_nodes = 0;
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate /*
8200Sstevel@tonic-gate  * Local data
8210Sstevel@tonic-gate  */
8220Sstevel@tonic-gate static struct impl_bus_promops *impl_busp;
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate /*
8260Sstevel@tonic-gate  * New DDI interrupt framework
8270Sstevel@tonic-gate  */
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate /*
8300Sstevel@tonic-gate  * i_ddi_intr_ops:
8310Sstevel@tonic-gate  *
8320Sstevel@tonic-gate  * This is the interrupt operator function wrapper for the bus function
8330Sstevel@tonic-gate  * bus_intr_op.
8340Sstevel@tonic-gate  */
8350Sstevel@tonic-gate int
8360Sstevel@tonic-gate i_ddi_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t op,
8370Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void * result)
8380Sstevel@tonic-gate {
8390Sstevel@tonic-gate 	dev_info_t	*pdip = (dev_info_t *)DEVI(dip)->devi_parent;
8400Sstevel@tonic-gate 	int		ret = DDI_FAILURE;
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 	/* request parent to process this interrupt op */
8430Sstevel@tonic-gate 	if (NEXUS_HAS_INTR_OP(pdip))
8440Sstevel@tonic-gate 		ret = (*(DEVI(pdip)->devi_ops->devo_bus_ops->bus_intr_op))(
8450Sstevel@tonic-gate 		    pdip, rdip, op, hdlp, result);
8460Sstevel@tonic-gate 	else
8470Sstevel@tonic-gate 		cmn_err(CE_WARN, "Failed to process interrupt "
8480Sstevel@tonic-gate 		    "for %s%d due to down-rev nexus driver %s%d",
8490Sstevel@tonic-gate 		    ddi_get_name(rdip), ddi_get_instance(rdip),
8500Sstevel@tonic-gate 		    ddi_get_name(pdip), ddi_get_instance(pdip));
8510Sstevel@tonic-gate 	return (ret);
8520Sstevel@tonic-gate }
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate /*
8550Sstevel@tonic-gate  * i_ddi_add_softint - allocate and add a soft interrupt to the system
8560Sstevel@tonic-gate  */
8570Sstevel@tonic-gate int
8580Sstevel@tonic-gate i_ddi_add_softint(ddi_softint_hdl_impl_t *hdlp)
8590Sstevel@tonic-gate {
8600Sstevel@tonic-gate 	int ret;
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	/* add soft interrupt handler */
8630Sstevel@tonic-gate 	ret = add_avsoftintr((void *)hdlp, hdlp->ih_pri, hdlp->ih_cb_func,
8640Sstevel@tonic-gate 	    DEVI(hdlp->ih_dip)->devi_name, hdlp->ih_cb_arg1, hdlp->ih_cb_arg2);
8650Sstevel@tonic-gate 	return (ret ? DDI_SUCCESS : DDI_FAILURE);
8660Sstevel@tonic-gate }
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate void
8700Sstevel@tonic-gate i_ddi_remove_softint(ddi_softint_hdl_impl_t *hdlp)
8710Sstevel@tonic-gate {
8720Sstevel@tonic-gate 	(void) rem_avsoftintr((void *)hdlp, hdlp->ih_pri, hdlp->ih_cb_func);
8730Sstevel@tonic-gate }
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 
876999Slq150181 extern void (*setsoftint)(int, struct av_softinfo *);
877999Slq150181 extern boolean_t av_check_softint_pending(struct av_softinfo *, boolean_t);
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate int
880278Sgovinda i_ddi_trigger_softint(ddi_softint_hdl_impl_t *hdlp, void *arg2)
8810Sstevel@tonic-gate {
882999Slq150181 	if (av_check_softint_pending(hdlp->ih_pending, B_FALSE))
883999Slq150181 		return (DDI_EPENDING);
884278Sgovinda 
885999Slq150181 	update_avsoftintr_args((void *)hdlp, hdlp->ih_pri, arg2);
886278Sgovinda 
887999Slq150181 	(*setsoftint)(hdlp->ih_pri, hdlp->ih_pending);
888999Slq150181 	return (DDI_SUCCESS);
8890Sstevel@tonic-gate }
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate /*
8920Sstevel@tonic-gate  * i_ddi_set_softint_pri:
8930Sstevel@tonic-gate  *
8940Sstevel@tonic-gate  * The way this works is that it first tries to add a softint vector
8950Sstevel@tonic-gate  * at the new priority in hdlp. If that succeeds; then it removes the
8960Sstevel@tonic-gate  * existing softint vector at the old priority.
8970Sstevel@tonic-gate  */
8980Sstevel@tonic-gate int
8990Sstevel@tonic-gate i_ddi_set_softint_pri(ddi_softint_hdl_impl_t *hdlp, uint_t old_pri)
9000Sstevel@tonic-gate {
901999Slq150181 	int ret;
902999Slq150181 
9030Sstevel@tonic-gate 	/*
9040Sstevel@tonic-gate 	 * If a softint is pending at the old priority then fail the request.
9050Sstevel@tonic-gate 	 */
906999Slq150181 	if (av_check_softint_pending(hdlp->ih_pending, B_TRUE))
9070Sstevel@tonic-gate 		return (DDI_FAILURE);
9080Sstevel@tonic-gate 
909999Slq150181 	ret = av_softint_movepri((void *)hdlp, old_pri);
910999Slq150181 	return (ret ? DDI_SUCCESS : DDI_FAILURE);
9110Sstevel@tonic-gate }
9120Sstevel@tonic-gate 
913916Sschwartz void
914916Sschwartz i_ddi_alloc_intr_phdl(ddi_intr_handle_impl_t *hdlp)
915916Sschwartz {
916916Sschwartz 	hdlp->ih_private = (void *)kmem_zalloc(sizeof (ihdl_plat_t), KM_SLEEP);
917916Sschwartz }
918916Sschwartz 
919916Sschwartz void
920916Sschwartz i_ddi_free_intr_phdl(ddi_intr_handle_impl_t *hdlp)
921916Sschwartz {
922916Sschwartz 	kmem_free(hdlp->ih_private, sizeof (ihdl_plat_t));
923916Sschwartz 	hdlp->ih_private = NULL;
924916Sschwartz }
925916Sschwartz 
9262580Sanish int
9272580Sanish i_ddi_get_intx_nintrs(dev_info_t *dip)
9282580Sanish {
9292580Sanish 	struct ddi_parent_private_data *pdp;
9302580Sanish 
9312580Sanish 	if ((pdp = ddi_get_parent_data(dip)) == NULL)
9322580Sanish 		return (0);
9332580Sanish 
9342580Sanish 	return (pdp->par_nintr);
9352580Sanish }
9362580Sanish 
9370Sstevel@tonic-gate /*
9380Sstevel@tonic-gate  * DDI Memory/DMA
9390Sstevel@tonic-gate  */
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate /*
9420Sstevel@tonic-gate  * Support for allocating DMAable memory to implement
9430Sstevel@tonic-gate  * ddi_dma_mem_alloc(9F) interface.
9440Sstevel@tonic-gate  */
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate #define	KA_ALIGN_SHIFT	7
9470Sstevel@tonic-gate #define	KA_ALIGN	(1 << KA_ALIGN_SHIFT)
9480Sstevel@tonic-gate #define	KA_NCACHE	(PAGESHIFT + 1 - KA_ALIGN_SHIFT)
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate /*
9510Sstevel@tonic-gate  * Dummy DMA attribute template for kmem_io[].kmem_io_attr.  We only
9520Sstevel@tonic-gate  * care about addr_lo, addr_hi, and align.  addr_hi will be dynamically set.
9530Sstevel@tonic-gate  */
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate static ddi_dma_attr_t kmem_io_attr = {
9560Sstevel@tonic-gate 	DMA_ATTR_V0,
9570Sstevel@tonic-gate 	0x0000000000000000ULL,		/* dma_attr_addr_lo */
9580Sstevel@tonic-gate 	0x0000000000000000ULL,		/* dma_attr_addr_hi */
9590Sstevel@tonic-gate 	0x00ffffff,
9600Sstevel@tonic-gate 	0x1000,				/* dma_attr_align */
9610Sstevel@tonic-gate 	1, 1, 0xffffffffULL, 0xffffffffULL, 0x1, 1, 0
9620Sstevel@tonic-gate };
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate /* kmem io memory ranges and indices */
9650Sstevel@tonic-gate enum {
9660Sstevel@tonic-gate 	IO_4P, IO_64G, IO_4G, IO_2G, IO_1G, IO_512M,
9670Sstevel@tonic-gate 	IO_256M, IO_128M, IO_64M, IO_32M, IO_16M, MAX_MEM_RANGES
9680Sstevel@tonic-gate };
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate static struct {
9710Sstevel@tonic-gate 	vmem_t		*kmem_io_arena;
9720Sstevel@tonic-gate 	kmem_cache_t	*kmem_io_cache[KA_NCACHE];
9730Sstevel@tonic-gate 	ddi_dma_attr_t	kmem_io_attr;
9740Sstevel@tonic-gate } kmem_io[MAX_MEM_RANGES];
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate static int kmem_io_idx;		/* index of first populated kmem_io[] */
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate static page_t *
9790Sstevel@tonic-gate page_create_io_wrapper(void *addr, size_t len, int vmflag, void *arg)
9800Sstevel@tonic-gate {
9810Sstevel@tonic-gate 	extern page_t *page_create_io(vnode_t *, u_offset_t, uint_t,
9820Sstevel@tonic-gate 	    uint_t, struct as *, caddr_t, ddi_dma_attr_t *);
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 	return (page_create_io(&kvp, (u_offset_t)(uintptr_t)addr, len,
9850Sstevel@tonic-gate 	    PG_EXCL | ((vmflag & VM_NOSLEEP) ? 0 : PG_WAIT), &kas, addr, arg));
9860Sstevel@tonic-gate }
9870Sstevel@tonic-gate 
9885084Sjohnlev #ifdef __xpv
9895084Sjohnlev static void
9905084Sjohnlev segkmem_free_io(vmem_t *vmp, void * ptr, size_t size)
9915084Sjohnlev {
9925084Sjohnlev 	extern void page_destroy_io(page_t *);
9935084Sjohnlev 	segkmem_xfree(vmp, ptr, size, page_destroy_io);
9945084Sjohnlev }
9955084Sjohnlev #endif
9965084Sjohnlev 
9970Sstevel@tonic-gate static void *
9980Sstevel@tonic-gate segkmem_alloc_io_4P(vmem_t *vmp, size_t size, int vmflag)
9990Sstevel@tonic-gate {
10000Sstevel@tonic-gate 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
10010Sstevel@tonic-gate 	    page_create_io_wrapper, &kmem_io[IO_4P].kmem_io_attr));
10020Sstevel@tonic-gate }
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate static void *
10050Sstevel@tonic-gate segkmem_alloc_io_64G(vmem_t *vmp, size_t size, int vmflag)
10060Sstevel@tonic-gate {
10070Sstevel@tonic-gate 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
10080Sstevel@tonic-gate 	    page_create_io_wrapper, &kmem_io[IO_64G].kmem_io_attr));
10090Sstevel@tonic-gate }
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate static void *
10120Sstevel@tonic-gate segkmem_alloc_io_4G(vmem_t *vmp, size_t size, int vmflag)
10130Sstevel@tonic-gate {
10140Sstevel@tonic-gate 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
10150Sstevel@tonic-gate 	    page_create_io_wrapper, &kmem_io[IO_4G].kmem_io_attr));
10160Sstevel@tonic-gate }
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate static void *
10190Sstevel@tonic-gate segkmem_alloc_io_2G(vmem_t *vmp, size_t size, int vmflag)
10200Sstevel@tonic-gate {
10210Sstevel@tonic-gate 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
10220Sstevel@tonic-gate 	    page_create_io_wrapper, &kmem_io[IO_2G].kmem_io_attr));
10230Sstevel@tonic-gate }
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate static void *
10260Sstevel@tonic-gate segkmem_alloc_io_1G(vmem_t *vmp, size_t size, int vmflag)
10270Sstevel@tonic-gate {
10280Sstevel@tonic-gate 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
10290Sstevel@tonic-gate 	    page_create_io_wrapper, &kmem_io[IO_1G].kmem_io_attr));
10300Sstevel@tonic-gate }
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate static void *
10330Sstevel@tonic-gate segkmem_alloc_io_512M(vmem_t *vmp, size_t size, int vmflag)
10340Sstevel@tonic-gate {
10350Sstevel@tonic-gate 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
10360Sstevel@tonic-gate 	    page_create_io_wrapper, &kmem_io[IO_512M].kmem_io_attr));
10370Sstevel@tonic-gate }
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate static void *
10400Sstevel@tonic-gate segkmem_alloc_io_256M(vmem_t *vmp, size_t size, int vmflag)
10410Sstevel@tonic-gate {
10420Sstevel@tonic-gate 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
10430Sstevel@tonic-gate 	    page_create_io_wrapper, &kmem_io[IO_256M].kmem_io_attr));
10440Sstevel@tonic-gate }
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate static void *
10470Sstevel@tonic-gate segkmem_alloc_io_128M(vmem_t *vmp, size_t size, int vmflag)
10480Sstevel@tonic-gate {
10490Sstevel@tonic-gate 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
10500Sstevel@tonic-gate 	    page_create_io_wrapper, &kmem_io[IO_128M].kmem_io_attr));
10510Sstevel@tonic-gate }
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate static void *
10540Sstevel@tonic-gate segkmem_alloc_io_64M(vmem_t *vmp, size_t size, int vmflag)
10550Sstevel@tonic-gate {
10560Sstevel@tonic-gate 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
10570Sstevel@tonic-gate 	    page_create_io_wrapper, &kmem_io[IO_64M].kmem_io_attr));
10580Sstevel@tonic-gate }
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate static void *
10610Sstevel@tonic-gate segkmem_alloc_io_32M(vmem_t *vmp, size_t size, int vmflag)
10620Sstevel@tonic-gate {
10630Sstevel@tonic-gate 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
10640Sstevel@tonic-gate 	    page_create_io_wrapper, &kmem_io[IO_32M].kmem_io_attr));
10650Sstevel@tonic-gate }
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate static void *
10680Sstevel@tonic-gate segkmem_alloc_io_16M(vmem_t *vmp, size_t size, int vmflag)
10690Sstevel@tonic-gate {
10700Sstevel@tonic-gate 	return (segkmem_xalloc(vmp, NULL, size, vmflag, 0,
10710Sstevel@tonic-gate 	    page_create_io_wrapper, &kmem_io[IO_16M].kmem_io_attr));
10720Sstevel@tonic-gate }
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate struct {
10750Sstevel@tonic-gate 	uint64_t	io_limit;
10760Sstevel@tonic-gate 	char		*io_name;
10770Sstevel@tonic-gate 	void		*(*io_alloc)(vmem_t *, size_t, int);
10780Sstevel@tonic-gate 	int		io_initial;	/* kmem_io_init during startup */
10790Sstevel@tonic-gate } io_arena_params[MAX_MEM_RANGES] = {
10800Sstevel@tonic-gate 	{0x000fffffffffffffULL,	"kmem_io_4P",	segkmem_alloc_io_4P,	1},
10810Sstevel@tonic-gate 	{0x0000000fffffffffULL,	"kmem_io_64G",	segkmem_alloc_io_64G,	0},
10820Sstevel@tonic-gate 	{0x00000000ffffffffULL,	"kmem_io_4G",	segkmem_alloc_io_4G,	1},
10830Sstevel@tonic-gate 	{0x000000007fffffffULL,	"kmem_io_2G",	segkmem_alloc_io_2G,	1},
10840Sstevel@tonic-gate 	{0x000000003fffffffULL,	"kmem_io_1G",	segkmem_alloc_io_1G,	0},
10850Sstevel@tonic-gate 	{0x000000001fffffffULL,	"kmem_io_512M",	segkmem_alloc_io_512M,	0},
10860Sstevel@tonic-gate 	{0x000000000fffffffULL,	"kmem_io_256M",	segkmem_alloc_io_256M,	0},
10870Sstevel@tonic-gate 	{0x0000000007ffffffULL,	"kmem_io_128M",	segkmem_alloc_io_128M,	0},
10880Sstevel@tonic-gate 	{0x0000000003ffffffULL,	"kmem_io_64M",	segkmem_alloc_io_64M,	0},
10890Sstevel@tonic-gate 	{0x0000000001ffffffULL,	"kmem_io_32M",	segkmem_alloc_io_32M,	0},
10900Sstevel@tonic-gate 	{0x0000000000ffffffULL,	"kmem_io_16M",	segkmem_alloc_io_16M,	1}
10910Sstevel@tonic-gate };
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate void
10940Sstevel@tonic-gate kmem_io_init(int a)
10950Sstevel@tonic-gate {
10960Sstevel@tonic-gate 	int	c;
10970Sstevel@tonic-gate 	char name[40];
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 	kmem_io[a].kmem_io_arena = vmem_create(io_arena_params[a].io_name,
11000Sstevel@tonic-gate 	    NULL, 0, PAGESIZE, io_arena_params[a].io_alloc,
11015084Sjohnlev #ifdef __xpv
11025084Sjohnlev 	    segkmem_free_io,
11035084Sjohnlev #else
11045084Sjohnlev 	    segkmem_free,
11055084Sjohnlev #endif
11065084Sjohnlev 	    heap_arena, 0, VM_SLEEP);
11075084Sjohnlev 
11080Sstevel@tonic-gate 	for (c = 0; c < KA_NCACHE; c++) {
11090Sstevel@tonic-gate 		size_t size = KA_ALIGN << c;
11100Sstevel@tonic-gate 		(void) sprintf(name, "%s_%lu",
11110Sstevel@tonic-gate 		    io_arena_params[a].io_name, size);
11120Sstevel@tonic-gate 		kmem_io[a].kmem_io_cache[c] = kmem_cache_create(name,
11130Sstevel@tonic-gate 		    size, size, NULL, NULL, NULL, NULL,
11140Sstevel@tonic-gate 		    kmem_io[a].kmem_io_arena, 0);
11150Sstevel@tonic-gate 	}
11160Sstevel@tonic-gate }
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate /*
11190Sstevel@tonic-gate  * Return the index of the highest memory range for addr.
11200Sstevel@tonic-gate  */
11210Sstevel@tonic-gate static int
11220Sstevel@tonic-gate kmem_io_index(uint64_t addr)
11230Sstevel@tonic-gate {
11240Sstevel@tonic-gate 	int n;
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 	for (n = kmem_io_idx; n < MAX_MEM_RANGES; n++) {
11270Sstevel@tonic-gate 		if (kmem_io[n].kmem_io_attr.dma_attr_addr_hi <= addr) {
11280Sstevel@tonic-gate 			if (kmem_io[n].kmem_io_arena == NULL)
11290Sstevel@tonic-gate 				kmem_io_init(n);
11300Sstevel@tonic-gate 			return (n);
11310Sstevel@tonic-gate 		}
11320Sstevel@tonic-gate 	}
11330Sstevel@tonic-gate 	panic("kmem_io_index: invalid addr - must be at least 16m");
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 	/*NOTREACHED*/
11360Sstevel@tonic-gate }
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate /*
11390Sstevel@tonic-gate  * Return the index of the next kmem_io populated memory range
11400Sstevel@tonic-gate  * after curindex.
11410Sstevel@tonic-gate  */
11420Sstevel@tonic-gate static int
11430Sstevel@tonic-gate kmem_io_index_next(int curindex)
11440Sstevel@tonic-gate {
11450Sstevel@tonic-gate 	int n;
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 	for (n = curindex + 1; n < MAX_MEM_RANGES; n++) {
11480Sstevel@tonic-gate 		if (kmem_io[n].kmem_io_arena)
11490Sstevel@tonic-gate 			return (n);
11500Sstevel@tonic-gate 	}
11510Sstevel@tonic-gate 	return (-1);
11520Sstevel@tonic-gate }
11530Sstevel@tonic-gate 
11541900Seota /*
11551900Seota  * allow kmem to be mapped in with different PTE cache attribute settings.
11561900Seota  * Used by i_ddi_mem_alloc()
11571900Seota  */
11581900Seota int
11591900Seota kmem_override_cache_attrs(caddr_t kva, size_t size, uint_t order)
11601900Seota {
11611900Seota 	uint_t hat_flags;
11621900Seota 	caddr_t kva_end;
11631900Seota 	uint_t hat_attr;
11641900Seota 	pfn_t pfn;
11651900Seota 
11661900Seota 	if (hat_getattr(kas.a_hat, kva, &hat_attr) == -1) {
11671900Seota 		return (-1);
11681900Seota 	}
11691900Seota 
11701900Seota 	hat_attr &= ~HAT_ORDER_MASK;
11711900Seota 	hat_attr |= order | HAT_NOSYNC;
11721900Seota 	hat_flags = HAT_LOAD_LOCK;
11731900Seota 
11741900Seota 	kva_end = (caddr_t)(((uintptr_t)kva + size + PAGEOFFSET) &
11751900Seota 	    (uintptr_t)PAGEMASK);
11761900Seota 	kva = (caddr_t)((uintptr_t)kva & (uintptr_t)PAGEMASK);
11771900Seota 
11781900Seota 	while (kva < kva_end) {
11791900Seota 		pfn = hat_getpfnum(kas.a_hat, kva);
11801900Seota 		hat_unload(kas.a_hat, kva, PAGESIZE, HAT_UNLOAD_UNLOCK);
11811900Seota 		hat_devload(kas.a_hat, kva, PAGESIZE, pfn, hat_attr, hat_flags);
11821900Seota 		kva += MMU_PAGESIZE;
11831900Seota 	}
11841900Seota 
11851900Seota 	return (0);
11861900Seota }
11871900Seota 
11880Sstevel@tonic-gate void
11890Sstevel@tonic-gate ka_init(void)
11900Sstevel@tonic-gate {
11910Sstevel@tonic-gate 	int a;
11925084Sjohnlev 	paddr_t maxphysaddr;
11935084Sjohnlev #if !defined(__xpv)
11940Sstevel@tonic-gate 	extern pfn_t physmax;
11955084Sjohnlev 
11965084Sjohnlev 	maxphysaddr = mmu_ptob((paddr_t)physmax) + MMU_PAGEOFFSET;
11975084Sjohnlev #else
11985084Sjohnlev 	maxphysaddr = mmu_ptob((paddr_t)HYPERVISOR_memory_op(
11995084Sjohnlev 	    XENMEM_maximum_ram_page, NULL)) + MMU_PAGEOFFSET;
12005084Sjohnlev #endif
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 	ASSERT(maxphysaddr <= io_arena_params[0].io_limit);
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	for (a = 0; a < MAX_MEM_RANGES; a++) {
12050Sstevel@tonic-gate 		if (maxphysaddr >= io_arena_params[a + 1].io_limit) {
12060Sstevel@tonic-gate 			if (maxphysaddr > io_arena_params[a + 1].io_limit)
12070Sstevel@tonic-gate 				io_arena_params[a].io_limit = maxphysaddr;
12080Sstevel@tonic-gate 			else
12090Sstevel@tonic-gate 				a++;
12100Sstevel@tonic-gate 			break;
12110Sstevel@tonic-gate 		}
12120Sstevel@tonic-gate 	}
12130Sstevel@tonic-gate 	kmem_io_idx = a;
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 	for (; a < MAX_MEM_RANGES; a++) {
12160Sstevel@tonic-gate 		kmem_io[a].kmem_io_attr = kmem_io_attr;
12170Sstevel@tonic-gate 		kmem_io[a].kmem_io_attr.dma_attr_addr_hi =
12180Sstevel@tonic-gate 		    io_arena_params[a].io_limit;
12190Sstevel@tonic-gate 		/*
12200Sstevel@tonic-gate 		 * initialize kmem_io[] arena/cache corresponding to
12210Sstevel@tonic-gate 		 * maxphysaddr and to the "common" io memory ranges that
12220Sstevel@tonic-gate 		 * have io_initial set to a non-zero value.
12230Sstevel@tonic-gate 		 */
12240Sstevel@tonic-gate 		if (io_arena_params[a].io_initial || a == kmem_io_idx)
12250Sstevel@tonic-gate 			kmem_io_init(a);
12260Sstevel@tonic-gate 	}
12270Sstevel@tonic-gate }
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate /*
12300Sstevel@tonic-gate  * put contig address/size
12310Sstevel@tonic-gate  */
12320Sstevel@tonic-gate static void *
12330Sstevel@tonic-gate putctgas(void *addr, size_t size)
12340Sstevel@tonic-gate {
12350Sstevel@tonic-gate 	struct ctgas	*ctgp = &ctglist;
12360Sstevel@tonic-gate 	int		i;
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	CTGLOCK();
12390Sstevel@tonic-gate 	do {
12400Sstevel@tonic-gate 		if ((i = ctgp->ctg_index) < CTGENTRIES) {
12410Sstevel@tonic-gate 			ctgp->ctg_addr[i] = addr;
12420Sstevel@tonic-gate 			ctgp->ctg_size[i] = size;
12430Sstevel@tonic-gate 			ctgp->ctg_index++;
12440Sstevel@tonic-gate 			break;
12450Sstevel@tonic-gate 		}
12460Sstevel@tonic-gate 		if (!ctgp->ctg_next)
12470Sstevel@tonic-gate 			ctgp->ctg_next = kmem_zalloc(sizeof (struct ctgas),
12480Sstevel@tonic-gate 			    KM_NOSLEEP);
12490Sstevel@tonic-gate 		ctgp = ctgp->ctg_next;
12500Sstevel@tonic-gate 	} while (ctgp);
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 	CTGUNLOCK();
12530Sstevel@tonic-gate 	return (ctgp);
12540Sstevel@tonic-gate }
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate /*
12570Sstevel@tonic-gate  * get contig size by addr
12580Sstevel@tonic-gate  */
12590Sstevel@tonic-gate static size_t
12600Sstevel@tonic-gate getctgsz(void *addr)
12610Sstevel@tonic-gate {
12620Sstevel@tonic-gate 	struct ctgas	*ctgp = &ctglist;
12630Sstevel@tonic-gate 	int		i, j;
12640Sstevel@tonic-gate 	size_t		sz;
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 	ASSERT(addr);
12670Sstevel@tonic-gate 	CTGLOCK();
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 	while (ctgp) {
12700Sstevel@tonic-gate 		for (i = 0; i < ctgp->ctg_index; i++) {
12710Sstevel@tonic-gate 			if (addr != ctgp->ctg_addr[i])
12720Sstevel@tonic-gate 				continue;
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 			sz = ctgp->ctg_size[i];
12750Sstevel@tonic-gate 			j = --ctgp->ctg_index;
12760Sstevel@tonic-gate 			if (i != j) {
12770Sstevel@tonic-gate 				ctgp->ctg_size[i] = ctgp->ctg_size[j];
12780Sstevel@tonic-gate 				ctgp->ctg_addr[i] = ctgp->ctg_addr[j];
12790Sstevel@tonic-gate 			}
12800Sstevel@tonic-gate 			CTGUNLOCK();
12810Sstevel@tonic-gate 			return (sz);
12820Sstevel@tonic-gate 		}
12830Sstevel@tonic-gate 		ctgp = ctgp->ctg_next;
12840Sstevel@tonic-gate 	}
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	CTGUNLOCK();
12870Sstevel@tonic-gate 	return (0);
12880Sstevel@tonic-gate }
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate /*
12910Sstevel@tonic-gate  * contig_alloc:
12920Sstevel@tonic-gate  *
12930Sstevel@tonic-gate  *	allocates contiguous memory to satisfy the 'size' and dma attributes
12940Sstevel@tonic-gate  *	specified in 'attr'.
12950Sstevel@tonic-gate  *
12960Sstevel@tonic-gate  *	Not all of memory need to be physically contiguous if the
12970Sstevel@tonic-gate  *	scatter-gather list length is greater than 1.
12980Sstevel@tonic-gate  */
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate /*ARGSUSED*/
13010Sstevel@tonic-gate void *
13020Sstevel@tonic-gate contig_alloc(size_t size, ddi_dma_attr_t *attr, uintptr_t align, int cansleep)
13030Sstevel@tonic-gate {
13040Sstevel@tonic-gate 	pgcnt_t		pgcnt = btopr(size);
13050Sstevel@tonic-gate 	size_t		asize = pgcnt * PAGESIZE;
13060Sstevel@tonic-gate 	page_t		*ppl;
13070Sstevel@tonic-gate 	int		pflag;
13080Sstevel@tonic-gate 	void		*addr;
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	extern page_t *page_create_io(vnode_t *, u_offset_t, uint_t,
13110Sstevel@tonic-gate 	    uint_t, struct as *, caddr_t, ddi_dma_attr_t *);
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 	/* segkmem_xalloc */
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 	if (align <= PAGESIZE)
13160Sstevel@tonic-gate 		addr = vmem_alloc(heap_arena, asize,
13170Sstevel@tonic-gate 		    (cansleep) ? VM_SLEEP : VM_NOSLEEP);
13180Sstevel@tonic-gate 	else
13190Sstevel@tonic-gate 		addr = vmem_xalloc(heap_arena, asize, align, 0, 0, NULL, NULL,
13200Sstevel@tonic-gate 		    (cansleep) ? VM_SLEEP : VM_NOSLEEP);
13210Sstevel@tonic-gate 	if (addr) {
13220Sstevel@tonic-gate 		ASSERT(!((uintptr_t)addr & (align - 1)));
13230Sstevel@tonic-gate 
13245084Sjohnlev 		if (page_resv(pgcnt, (cansleep) ? KM_SLEEP : KM_NOSLEEP) == 0) {
13250Sstevel@tonic-gate 			vmem_free(heap_arena, addr, asize);
13260Sstevel@tonic-gate 			return (NULL);
13270Sstevel@tonic-gate 		}
13280Sstevel@tonic-gate 		pflag = PG_EXCL;
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate 		if (cansleep)
13310Sstevel@tonic-gate 			pflag |= PG_WAIT;
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 		/* 4k req gets from freelists rather than pfn search */
13340Sstevel@tonic-gate 		if (pgcnt > 1 || align > PAGESIZE)
13350Sstevel@tonic-gate 			pflag |= PG_PHYSCONTIG;
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 		ppl = page_create_io(&kvp, (u_offset_t)(uintptr_t)addr,
13385084Sjohnlev 		    asize, pflag, &kas, (caddr_t)addr, attr);
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 		if (!ppl) {
13410Sstevel@tonic-gate 			vmem_free(heap_arena, addr, asize);
13420Sstevel@tonic-gate 			page_unresv(pgcnt);
13430Sstevel@tonic-gate 			return (NULL);
13440Sstevel@tonic-gate 		}
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 		while (ppl != NULL) {
13470Sstevel@tonic-gate 			page_t	*pp = ppl;
13480Sstevel@tonic-gate 			page_sub(&ppl, pp);
13490Sstevel@tonic-gate 			ASSERT(page_iolock_assert(pp));
13500Sstevel@tonic-gate 			page_io_unlock(pp);
13510Sstevel@tonic-gate 			page_downgrade(pp);
13520Sstevel@tonic-gate 			hat_memload(kas.a_hat, (caddr_t)(uintptr_t)pp->p_offset,
13535084Sjohnlev 			    pp, (PROT_ALL & ~PROT_USER) |
13545084Sjohnlev 			    HAT_NOSYNC, HAT_LOAD_LOCK);
13550Sstevel@tonic-gate 		}
13560Sstevel@tonic-gate 	}
13570Sstevel@tonic-gate 	return (addr);
13580Sstevel@tonic-gate }
13590Sstevel@tonic-gate 
13607750SSherry.Moore@Sun.COM void
13610Sstevel@tonic-gate contig_free(void *addr, size_t size)
13620Sstevel@tonic-gate {
13630Sstevel@tonic-gate 	pgcnt_t	pgcnt = btopr(size);
13640Sstevel@tonic-gate 	size_t	asize = pgcnt * PAGESIZE;
13650Sstevel@tonic-gate 	caddr_t	a, ea;
13660Sstevel@tonic-gate 	page_t	*pp;
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 	hat_unload(kas.a_hat, addr, asize, HAT_UNLOAD_UNLOCK);
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate 	for (a = addr, ea = a + asize; a < ea; a += PAGESIZE) {
13715084Sjohnlev 		pp = page_find(&kvp, (u_offset_t)(uintptr_t)a);
13720Sstevel@tonic-gate 		if (!pp)
13730Sstevel@tonic-gate 			panic("contig_free: contig pp not found");
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 		if (!page_tryupgrade(pp)) {
13760Sstevel@tonic-gate 			page_unlock(pp);
13770Sstevel@tonic-gate 			pp = page_lookup(&kvp,
13785084Sjohnlev 			    (u_offset_t)(uintptr_t)a, SE_EXCL);
13790Sstevel@tonic-gate 			if (pp == NULL)
13800Sstevel@tonic-gate 				panic("contig_free: page freed");
13810Sstevel@tonic-gate 		}
13820Sstevel@tonic-gate 		page_destroy(pp, 0);
13830Sstevel@tonic-gate 	}
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 	page_unresv(pgcnt);
13860Sstevel@tonic-gate 	vmem_free(heap_arena, addr, asize);
13870Sstevel@tonic-gate }
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate /*
13900Sstevel@tonic-gate  * Allocate from the system, aligned on a specific boundary.
13910Sstevel@tonic-gate  * The alignment, if non-zero, must be a power of 2.
13920Sstevel@tonic-gate  */
13930Sstevel@tonic-gate static void *
13940Sstevel@tonic-gate kalloca(size_t size, size_t align, int cansleep, int physcontig,
13950Sstevel@tonic-gate 	ddi_dma_attr_t *attr)
13960Sstevel@tonic-gate {
13970Sstevel@tonic-gate 	size_t *addr, *raddr, rsize;
13980Sstevel@tonic-gate 	size_t hdrsize = 4 * sizeof (size_t);	/* must be power of 2 */
13990Sstevel@tonic-gate 	int a, i, c;
14000Sstevel@tonic-gate 	vmem_t *vmp;
14010Sstevel@tonic-gate 	kmem_cache_t *cp = NULL;
14020Sstevel@tonic-gate 
14031242Scth 	if (attr->dma_attr_addr_lo > mmu_ptob((uint64_t)ddiphysmin))
14041242Scth 		return (NULL);
14051242Scth 
14060Sstevel@tonic-gate 	align = MAX(align, hdrsize);
14070Sstevel@tonic-gate 	ASSERT((align & (align - 1)) == 0);
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 	/*
14100Sstevel@tonic-gate 	 * All of our allocators guarantee 16-byte alignment, so we don't
14110Sstevel@tonic-gate 	 * need to reserve additional space for the header.
14120Sstevel@tonic-gate 	 * To simplify picking the correct kmem_io_cache, we round up to
14130Sstevel@tonic-gate 	 * a multiple of KA_ALIGN.
14140Sstevel@tonic-gate 	 */
14150Sstevel@tonic-gate 	rsize = P2ROUNDUP_TYPED(size + align, KA_ALIGN, size_t);
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 	if (physcontig && rsize > PAGESIZE) {
14180Sstevel@tonic-gate 		if (addr = contig_alloc(size, attr, align, cansleep)) {
14190Sstevel@tonic-gate 			if (!putctgas(addr, size))
14200Sstevel@tonic-gate 				contig_free(addr, size);
14210Sstevel@tonic-gate 			else
14220Sstevel@tonic-gate 				return (addr);
14230Sstevel@tonic-gate 		}
14240Sstevel@tonic-gate 		return (NULL);
14250Sstevel@tonic-gate 	}
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate 	a = kmem_io_index(attr->dma_attr_addr_hi);
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate 	if (rsize > PAGESIZE) {
14300Sstevel@tonic-gate 		vmp = kmem_io[a].kmem_io_arena;
14310Sstevel@tonic-gate 		raddr = vmem_alloc(vmp, rsize,
14320Sstevel@tonic-gate 		    (cansleep) ? VM_SLEEP : VM_NOSLEEP);
14330Sstevel@tonic-gate 	} else {
14340Sstevel@tonic-gate 		c = highbit((rsize >> KA_ALIGN_SHIFT) - 1);
14350Sstevel@tonic-gate 		cp = kmem_io[a].kmem_io_cache[c];
14360Sstevel@tonic-gate 		raddr = kmem_cache_alloc(cp, (cansleep) ? KM_SLEEP :
14370Sstevel@tonic-gate 		    KM_NOSLEEP);
14380Sstevel@tonic-gate 	}
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 	if (raddr == NULL) {
14410Sstevel@tonic-gate 		int	na;
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 		ASSERT(cansleep == 0);
14440Sstevel@tonic-gate 		if (rsize > PAGESIZE)
14450Sstevel@tonic-gate 			return (NULL);
14460Sstevel@tonic-gate 		/*
14470Sstevel@tonic-gate 		 * System does not have memory in the requested range.
14480Sstevel@tonic-gate 		 * Try smaller kmem io ranges and larger cache sizes
14490Sstevel@tonic-gate 		 * to see if there might be memory available in
14500Sstevel@tonic-gate 		 * these other caches.
14510Sstevel@tonic-gate 		 */
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate 		for (na = kmem_io_index_next(a); na >= 0;
14540Sstevel@tonic-gate 		    na = kmem_io_index_next(na)) {
14550Sstevel@tonic-gate 			ASSERT(kmem_io[na].kmem_io_arena);
14560Sstevel@tonic-gate 			cp = kmem_io[na].kmem_io_cache[c];
14570Sstevel@tonic-gate 			raddr = kmem_cache_alloc(cp, KM_NOSLEEP);
14580Sstevel@tonic-gate 			if (raddr)
14590Sstevel@tonic-gate 				goto kallocdone;
14600Sstevel@tonic-gate 		}
14610Sstevel@tonic-gate 		/* now try the larger kmem io cache sizes */
14620Sstevel@tonic-gate 		for (na = a; na >= 0; na = kmem_io_index_next(na)) {
14630Sstevel@tonic-gate 			for (i = c + 1; i < KA_NCACHE; i++) {
14640Sstevel@tonic-gate 				cp = kmem_io[na].kmem_io_cache[i];
14650Sstevel@tonic-gate 				raddr = kmem_cache_alloc(cp, KM_NOSLEEP);
14660Sstevel@tonic-gate 				if (raddr)
14670Sstevel@tonic-gate 					goto kallocdone;
14680Sstevel@tonic-gate 			}
14690Sstevel@tonic-gate 		}
14700Sstevel@tonic-gate 		return (NULL);
14710Sstevel@tonic-gate 	}
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate kallocdone:
14747837SMatthew.Ahrens@Sun.COM 	ASSERT(!P2BOUNDARY((uintptr_t)raddr, rsize, PAGESIZE) ||
14757837SMatthew.Ahrens@Sun.COM 	    rsize > PAGESIZE);
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate 	addr = (size_t *)P2ROUNDUP((uintptr_t)raddr + hdrsize, align);
14780Sstevel@tonic-gate 	ASSERT((uintptr_t)addr + size - (uintptr_t)raddr <= rsize);
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 	addr[-4] = (size_t)cp;
14810Sstevel@tonic-gate 	addr[-3] = (size_t)vmp;
14820Sstevel@tonic-gate 	addr[-2] = (size_t)raddr;
14830Sstevel@tonic-gate 	addr[-1] = rsize;
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 	return (addr);
14860Sstevel@tonic-gate }
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate static void
14890Sstevel@tonic-gate kfreea(void *addr)
14900Sstevel@tonic-gate {
14910Sstevel@tonic-gate 	size_t		size;
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate 	if (!((uintptr_t)addr & PAGEOFFSET) && (size = getctgsz(addr))) {
14940Sstevel@tonic-gate 		contig_free(addr, size);
14950Sstevel@tonic-gate 	} else {
14960Sstevel@tonic-gate 		size_t	*saddr = addr;
14970Sstevel@tonic-gate 		if (saddr[-4] == 0)
14980Sstevel@tonic-gate 			vmem_free((vmem_t *)saddr[-3], (void *)saddr[-2],
14995084Sjohnlev 			    saddr[-1]);
15000Sstevel@tonic-gate 		else
15010Sstevel@tonic-gate 			kmem_cache_free((kmem_cache_t *)saddr[-4],
15025084Sjohnlev 			    (void *)saddr[-2]);
15030Sstevel@tonic-gate 	}
15040Sstevel@tonic-gate }
15050Sstevel@tonic-gate 
15061910Seota /*ARGSUSED*/
15071900Seota void
15081900Seota i_ddi_devacc_to_hatacc(ddi_device_acc_attr_t *devaccp, uint_t *hataccp)
15091900Seota {
15101900Seota }
15111900Seota 
15121900Seota /*
15131900Seota  * Check if the specified cache attribute is supported on the platform.
15141900Seota  * This function must be called before i_ddi_cacheattr_to_hatacc().
15151900Seota  */
15161900Seota boolean_t
15171900Seota i_ddi_check_cache_attr(uint_t flags)
15181900Seota {
15191900Seota 	/*
15201900Seota 	 * The cache attributes are mutually exclusive. Any combination of
15211900Seota 	 * the attributes leads to a failure.
15221900Seota 	 */
15231900Seota 	uint_t cache_attr = IOMEM_CACHE_ATTR(flags);
15241900Seota 	if ((cache_attr != 0) && ((cache_attr & (cache_attr - 1)) != 0))
15251900Seota 		return (B_FALSE);
15261900Seota 
15271900Seota 	/* All cache attributes are supported on X86/X64 */
15281900Seota 	if (cache_attr & (IOMEM_DATA_UNCACHED | IOMEM_DATA_CACHED |
15291900Seota 	    IOMEM_DATA_UC_WR_COMBINE))
15301900Seota 		return (B_TRUE);
15311900Seota 
15321900Seota 	/* undefined attributes */
15331900Seota 	return (B_FALSE);
15341900Seota }
15351900Seota 
15361900Seota /* set HAT cache attributes from the cache attributes */
15371900Seota void
15381900Seota i_ddi_cacheattr_to_hatacc(uint_t flags, uint_t *hataccp)
15391900Seota {
15401900Seota 	uint_t cache_attr = IOMEM_CACHE_ATTR(flags);
15411900Seota 	static char *fname = "i_ddi_cacheattr_to_hatacc";
15421900Seota 
15431900Seota 	/*
15441900Seota 	 * If write-combining is not supported, then it falls back
15451900Seota 	 * to uncacheable.
15461900Seota 	 */
15471900Seota 	if (cache_attr == IOMEM_DATA_UC_WR_COMBINE && !(x86_feature & X86_PAT))
15481900Seota 		cache_attr = IOMEM_DATA_UNCACHED;
15491900Seota 
15501900Seota 	/*
15511900Seota 	 * set HAT attrs according to the cache attrs.
15521900Seota 	 */
15531900Seota 	switch (cache_attr) {
15541900Seota 	case IOMEM_DATA_UNCACHED:
15551900Seota 		*hataccp &= ~HAT_ORDER_MASK;
15561900Seota 		*hataccp |= (HAT_STRICTORDER | HAT_PLAT_NOCACHE);
15571900Seota 		break;
15581900Seota 	case IOMEM_DATA_UC_WR_COMBINE:
15591900Seota 		*hataccp &= ~HAT_ORDER_MASK;
15601900Seota 		*hataccp |= (HAT_MERGING_OK | HAT_PLAT_NOCACHE);
15611900Seota 		break;
15621900Seota 	case IOMEM_DATA_CACHED:
15631900Seota 		*hataccp &= ~HAT_ORDER_MASK;
15641900Seota 		*hataccp |= HAT_UNORDERED_OK;
15651900Seota 		break;
15661900Seota 	/*
15671900Seota 	 * This case must not occur because the cache attribute is scrutinized
15681900Seota 	 * before this function is called.
15691900Seota 	 */
15701900Seota 	default:
15711900Seota 		/*
15721900Seota 		 * set cacheable to hat attrs.
15731900Seota 		 */
15741900Seota 		*hataccp &= ~HAT_ORDER_MASK;
15751900Seota 		*hataccp |= HAT_UNORDERED_OK;
15761900Seota 		cmn_err(CE_WARN, "%s: cache_attr=0x%x is ignored.",
15771900Seota 		    fname, cache_attr);
15781900Seota 	}
15791900Seota }
15801900Seota 
15811900Seota /*
15820Sstevel@tonic-gate  * This should actually be called i_ddi_dma_mem_alloc. There should
15830Sstevel@tonic-gate  * also be an i_ddi_pio_mem_alloc. i_ddi_dma_mem_alloc should call
15840Sstevel@tonic-gate  * through the device tree with the DDI_CTLOPS_DMA_ALIGN ctl ops to
15850Sstevel@tonic-gate  * get alignment requirements for DMA memory. i_ddi_pio_mem_alloc
15860Sstevel@tonic-gate  * should use DDI_CTLOPS_PIO_ALIGN. Since we only have i_ddi_mem_alloc
15870Sstevel@tonic-gate  * so far which is used for both, DMA and PIO, we have to use the DMA
15880Sstevel@tonic-gate  * ctl ops to make everybody happy.
15890Sstevel@tonic-gate  */
15900Sstevel@tonic-gate /*ARGSUSED*/
15910Sstevel@tonic-gate int
15920Sstevel@tonic-gate i_ddi_mem_alloc(dev_info_t *dip, ddi_dma_attr_t *attr,
15931900Seota 	size_t length, int cansleep, int flags,
15940Sstevel@tonic-gate 	ddi_device_acc_attr_t *accattrp, caddr_t *kaddrp,
15950Sstevel@tonic-gate 	size_t *real_length, ddi_acc_hdl_t *ap)
15960Sstevel@tonic-gate {
15970Sstevel@tonic-gate 	caddr_t a;
15980Sstevel@tonic-gate 	int iomin;
15990Sstevel@tonic-gate 	ddi_acc_impl_t *iap;
16000Sstevel@tonic-gate 	int physcontig = 0;
16010Sstevel@tonic-gate 	pgcnt_t npages;
16020Sstevel@tonic-gate 	pgcnt_t minctg;
16031900Seota 	uint_t order;
16041900Seota 	int e;
16050Sstevel@tonic-gate 
16060Sstevel@tonic-gate 	/*
16070Sstevel@tonic-gate 	 * Check legality of arguments
16080Sstevel@tonic-gate 	 */
16090Sstevel@tonic-gate 	if (length == 0 || kaddrp == NULL || attr == NULL) {
16100Sstevel@tonic-gate 		return (DDI_FAILURE);
16110Sstevel@tonic-gate 	}
16121900Seota 
16130Sstevel@tonic-gate 	if (attr->dma_attr_minxfer == 0 || attr->dma_attr_align == 0 ||
16145084Sjohnlev 	    (attr->dma_attr_align & (attr->dma_attr_align - 1)) ||
16155084Sjohnlev 	    (attr->dma_attr_minxfer & (attr->dma_attr_minxfer - 1))) {
16160Sstevel@tonic-gate 			return (DDI_FAILURE);
16170Sstevel@tonic-gate 	}
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	/*
16200Sstevel@tonic-gate 	 * figure out most restrictive alignment requirement
16210Sstevel@tonic-gate 	 */
16220Sstevel@tonic-gate 	iomin = attr->dma_attr_minxfer;
16230Sstevel@tonic-gate 	iomin = maxbit(iomin, attr->dma_attr_align);
16240Sstevel@tonic-gate 	if (iomin == 0)
16250Sstevel@tonic-gate 		return (DDI_FAILURE);
16260Sstevel@tonic-gate 
16270Sstevel@tonic-gate 	ASSERT((iomin & (iomin - 1)) == 0);
16280Sstevel@tonic-gate 
16291900Seota 	/*
16301900Seota 	 * if we allocate memory with IOMEM_DATA_UNCACHED or
16311900Seota 	 * IOMEM_DATA_UC_WR_COMBINE, make sure we allocate a page aligned
16321900Seota 	 * memory that ends on a page boundry.
16331900Seota 	 * Don't want to have to different cache mappings to the same
16341900Seota 	 * physical page.
16351900Seota 	 */
16361900Seota 	if (OVERRIDE_CACHE_ATTR(flags)) {
16371900Seota 		iomin = (iomin + MMU_PAGEOFFSET) & MMU_PAGEMASK;
16381900Seota 		length = (length + MMU_PAGEOFFSET) & (size_t)MMU_PAGEMASK;
16391900Seota 	}
16400Sstevel@tonic-gate 
16410Sstevel@tonic-gate 	/*
16420Sstevel@tonic-gate 	 * Determine if we need to satisfy the request for physically
16430Sstevel@tonic-gate 	 * contiguous memory or alignments larger than pagesize.
16440Sstevel@tonic-gate 	 */
16450Sstevel@tonic-gate 	npages = btopr(length + attr->dma_attr_align);
16460Sstevel@tonic-gate 	minctg = howmany(npages, attr->dma_attr_sgllen);
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate 	if (minctg > 1) {
16490Sstevel@tonic-gate 		uint64_t pfnseg = attr->dma_attr_seg >> PAGESHIFT;
16500Sstevel@tonic-gate 		/*
16510Sstevel@tonic-gate 		 * verify that the minimum contig requirement for the
16520Sstevel@tonic-gate 		 * actual length does not cross segment boundary.
16530Sstevel@tonic-gate 		 */
16540Sstevel@tonic-gate 		length = P2ROUNDUP_TYPED(length, attr->dma_attr_minxfer,
16550Sstevel@tonic-gate 		    size_t);
16560Sstevel@tonic-gate 		npages = btopr(length);
16570Sstevel@tonic-gate 		minctg = howmany(npages, attr->dma_attr_sgllen);
16580Sstevel@tonic-gate 		if (minctg > pfnseg + 1)
16590Sstevel@tonic-gate 			return (DDI_FAILURE);
16600Sstevel@tonic-gate 		physcontig = 1;
16610Sstevel@tonic-gate 	} else {
16620Sstevel@tonic-gate 		length = P2ROUNDUP_TYPED(length, iomin, size_t);
16630Sstevel@tonic-gate 	}
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate 	/*
16660Sstevel@tonic-gate 	 * Allocate the requested amount from the system.
16670Sstevel@tonic-gate 	 */
16680Sstevel@tonic-gate 	a = kalloca(length, iomin, cansleep, physcontig, attr);
16690Sstevel@tonic-gate 
16700Sstevel@tonic-gate 	if ((*kaddrp = a) == NULL)
16710Sstevel@tonic-gate 		return (DDI_FAILURE);
16720Sstevel@tonic-gate 
16731900Seota 	/*
16741900Seota 	 * if we to modify the cache attributes, go back and muck with the
16751900Seota 	 * mappings.
16761900Seota 	 */
16771900Seota 	if (OVERRIDE_CACHE_ATTR(flags)) {
16781900Seota 		order = 0;
16791900Seota 		i_ddi_cacheattr_to_hatacc(flags, &order);
16801900Seota 		e = kmem_override_cache_attrs(a, length, order);
16811900Seota 		if (e != 0) {
16821900Seota 			kfreea(a);
16831900Seota 			return (DDI_FAILURE);
16841900Seota 		}
16851900Seota 	}
16861900Seota 
16870Sstevel@tonic-gate 	if (real_length) {
16880Sstevel@tonic-gate 		*real_length = length;
16890Sstevel@tonic-gate 	}
16900Sstevel@tonic-gate 	if (ap) {
16910Sstevel@tonic-gate 		/*
16920Sstevel@tonic-gate 		 * initialize access handle
16930Sstevel@tonic-gate 		 */
16940Sstevel@tonic-gate 		iap = (ddi_acc_impl_t *)ap->ah_platform_private;
16950Sstevel@tonic-gate 		iap->ahi_acc_attr |= DDI_ACCATTR_CPU_VADDR;
16960Sstevel@tonic-gate 		impl_acc_hdl_init(ap);
16970Sstevel@tonic-gate 	}
16981900Seota 
16990Sstevel@tonic-gate 	return (DDI_SUCCESS);
17000Sstevel@tonic-gate }
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate /*
17030Sstevel@tonic-gate  * covert old DMA limits structure to DMA attribute structure
17040Sstevel@tonic-gate  * and continue
17050Sstevel@tonic-gate  */
17060Sstevel@tonic-gate int
17070Sstevel@tonic-gate i_ddi_mem_alloc_lim(dev_info_t *dip, ddi_dma_lim_t *limits,
17080Sstevel@tonic-gate 	size_t length, int cansleep, int streaming,
17090Sstevel@tonic-gate 	ddi_device_acc_attr_t *accattrp, caddr_t *kaddrp,
17100Sstevel@tonic-gate 	uint_t *real_length, ddi_acc_hdl_t *ap)
17110Sstevel@tonic-gate {
17120Sstevel@tonic-gate 	ddi_dma_attr_t dma_attr, *attrp;
17130Sstevel@tonic-gate 	size_t rlen;
17140Sstevel@tonic-gate 	int ret;
17150Sstevel@tonic-gate 
17160Sstevel@tonic-gate 	if (limits == NULL) {
17170Sstevel@tonic-gate 		return (DDI_FAILURE);
17180Sstevel@tonic-gate 	}
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate 	/*
17210Sstevel@tonic-gate 	 * set up DMA attribute structure to pass to i_ddi_mem_alloc()
17220Sstevel@tonic-gate 	 */
17230Sstevel@tonic-gate 	attrp = &dma_attr;
17240Sstevel@tonic-gate 	attrp->dma_attr_version = DMA_ATTR_V0;
17250Sstevel@tonic-gate 	attrp->dma_attr_addr_lo = (uint64_t)limits->dlim_addr_lo;
17260Sstevel@tonic-gate 	attrp->dma_attr_addr_hi = (uint64_t)limits->dlim_addr_hi;
17270Sstevel@tonic-gate 	attrp->dma_attr_count_max = (uint64_t)limits->dlim_ctreg_max;
17280Sstevel@tonic-gate 	attrp->dma_attr_align = 1;
17290Sstevel@tonic-gate 	attrp->dma_attr_burstsizes = (uint_t)limits->dlim_burstsizes;
17300Sstevel@tonic-gate 	attrp->dma_attr_minxfer = (uint32_t)limits->dlim_minxfer;
17310Sstevel@tonic-gate 	attrp->dma_attr_maxxfer = (uint64_t)limits->dlim_reqsize;
17320Sstevel@tonic-gate 	attrp->dma_attr_seg = (uint64_t)limits->dlim_adreg_max;
17330Sstevel@tonic-gate 	attrp->dma_attr_sgllen = limits->dlim_sgllen;
17340Sstevel@tonic-gate 	attrp->dma_attr_granular = (uint32_t)limits->dlim_granular;
17350Sstevel@tonic-gate 	attrp->dma_attr_flags = 0;
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate 	ret = i_ddi_mem_alloc(dip, attrp, length, cansleep, streaming,
17385084Sjohnlev 	    accattrp, kaddrp, &rlen, ap);
17390Sstevel@tonic-gate 	if (ret == DDI_SUCCESS) {
17400Sstevel@tonic-gate 		if (real_length)
17410Sstevel@tonic-gate 			*real_length = (uint_t)rlen;
17420Sstevel@tonic-gate 	}
17430Sstevel@tonic-gate 	return (ret);
17440Sstevel@tonic-gate }
17450Sstevel@tonic-gate 
17460Sstevel@tonic-gate /* ARGSUSED */
17470Sstevel@tonic-gate void
17481900Seota i_ddi_mem_free(caddr_t kaddr, ddi_acc_hdl_t *ap)
17490Sstevel@tonic-gate {
17501900Seota 	if (ap != NULL) {
17511900Seota 		/*
17521900Seota 		 * if we modified the cache attributes on alloc, go back and
17531900Seota 		 * fix them since this memory could be returned to the
17541900Seota 		 * general pool.
17551900Seota 		 */
17561900Seota 		if (OVERRIDE_CACHE_ATTR(ap->ah_xfermodes)) {
17571900Seota 			uint_t order = 0;
17581900Seota 			int e;
17591900Seota 			i_ddi_cacheattr_to_hatacc(IOMEM_DATA_CACHED, &order);
17601900Seota 			e = kmem_override_cache_attrs(kaddr, ap->ah_len, order);
17611900Seota 			if (e != 0) {
17621900Seota 				cmn_err(CE_WARN, "i_ddi_mem_free() failed to "
17631900Seota 				    "override cache attrs, memory leaked\n");
17641900Seota 				return;
17651900Seota 			}
17661900Seota 		}
17671900Seota 	}
17680Sstevel@tonic-gate 	kfreea(kaddr);
17690Sstevel@tonic-gate }
17700Sstevel@tonic-gate 
17710Sstevel@tonic-gate /*
17720Sstevel@tonic-gate  * Access Barriers
17730Sstevel@tonic-gate  *
17740Sstevel@tonic-gate  */
17750Sstevel@tonic-gate /*ARGSUSED*/
17760Sstevel@tonic-gate int
17770Sstevel@tonic-gate i_ddi_ontrap(ddi_acc_handle_t hp)
17780Sstevel@tonic-gate {
17790Sstevel@tonic-gate 	return (DDI_FAILURE);
17800Sstevel@tonic-gate }
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate /*ARGSUSED*/
17830Sstevel@tonic-gate void
17840Sstevel@tonic-gate i_ddi_notrap(ddi_acc_handle_t hp)
17850Sstevel@tonic-gate {
17860Sstevel@tonic-gate }
17870Sstevel@tonic-gate 
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate /*
17900Sstevel@tonic-gate  * Misc Functions
17910Sstevel@tonic-gate  */
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate /*
17940Sstevel@tonic-gate  * Implementation instance override functions
17950Sstevel@tonic-gate  *
17960Sstevel@tonic-gate  * No override on i86pc
17970Sstevel@tonic-gate  */
17980Sstevel@tonic-gate /*ARGSUSED*/
17990Sstevel@tonic-gate uint_t
18000Sstevel@tonic-gate impl_assign_instance(dev_info_t *dip)
18010Sstevel@tonic-gate {
18020Sstevel@tonic-gate 	return ((uint_t)-1);
18030Sstevel@tonic-gate }
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate /*ARGSUSED*/
18060Sstevel@tonic-gate int
18070Sstevel@tonic-gate impl_keep_instance(dev_info_t *dip)
18080Sstevel@tonic-gate {
18095084Sjohnlev 
18105084Sjohnlev #if defined(__xpv)
18115084Sjohnlev 	/*
18125084Sjohnlev 	 * Do not persist instance numbers assigned to devices in dom0
18135084Sjohnlev 	 */
18145084Sjohnlev 	dev_info_t *pdip;
18155084Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
18165084Sjohnlev 		if (((pdip = ddi_get_parent(dip)) != NULL) &&
18175084Sjohnlev 		    (strcmp(ddi_get_name(pdip), "xpvd") == 0))
18185084Sjohnlev 			return (DDI_SUCCESS);
18195084Sjohnlev 	}
18205084Sjohnlev #endif
18210Sstevel@tonic-gate 	return (DDI_FAILURE);
18220Sstevel@tonic-gate }
18230Sstevel@tonic-gate 
18240Sstevel@tonic-gate /*ARGSUSED*/
18250Sstevel@tonic-gate int
18260Sstevel@tonic-gate impl_free_instance(dev_info_t *dip)
18270Sstevel@tonic-gate {
18280Sstevel@tonic-gate 	return (DDI_FAILURE);
18290Sstevel@tonic-gate }
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate /*ARGSUSED*/
18320Sstevel@tonic-gate int
18330Sstevel@tonic-gate impl_check_cpu(dev_info_t *devi)
18340Sstevel@tonic-gate {
18350Sstevel@tonic-gate 	return (DDI_SUCCESS);
18360Sstevel@tonic-gate }
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate /*
18390Sstevel@tonic-gate  * Referenced in common/cpr_driver.c: Power off machine.
18400Sstevel@tonic-gate  * Don't know how to power off i86pc.
18410Sstevel@tonic-gate  */
18420Sstevel@tonic-gate void
18430Sstevel@tonic-gate arch_power_down()
18440Sstevel@tonic-gate {}
18450Sstevel@tonic-gate 
18460Sstevel@tonic-gate /*
18470Sstevel@tonic-gate  * Copy name to property_name, since name
18480Sstevel@tonic-gate  * is in the low address range below kernelbase.
18490Sstevel@tonic-gate  */
18500Sstevel@tonic-gate static void
18510Sstevel@tonic-gate copy_boot_str(const char *boot_str, char *kern_str, int len)
18520Sstevel@tonic-gate {
18530Sstevel@tonic-gate 	int i = 0;
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate 	while (i < len - 1 && boot_str[i] != '\0') {
18560Sstevel@tonic-gate 		kern_str[i] = boot_str[i];
18570Sstevel@tonic-gate 		i++;
18580Sstevel@tonic-gate 	}
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate 	kern_str[i] = 0;	/* null terminate */
18610Sstevel@tonic-gate 	if (boot_str[i] != '\0')
18620Sstevel@tonic-gate 		cmn_err(CE_WARN,
18630Sstevel@tonic-gate 		    "boot property string is truncated to %s", kern_str);
18640Sstevel@tonic-gate }
18650Sstevel@tonic-gate 
18660Sstevel@tonic-gate static void
18670Sstevel@tonic-gate get_boot_properties(void)
18680Sstevel@tonic-gate {
18690Sstevel@tonic-gate 	extern char hw_provider[];
18700Sstevel@tonic-gate 	dev_info_t *devi;
18710Sstevel@tonic-gate 	char *name;
18720Sstevel@tonic-gate 	int length;
18730Sstevel@tonic-gate 	char property_name[50], property_val[50];
18740Sstevel@tonic-gate 	void *bop_staging_area;
18750Sstevel@tonic-gate 
18760Sstevel@tonic-gate 	bop_staging_area = kmem_zalloc(MMU_PAGESIZE, KM_NOSLEEP);
18770Sstevel@tonic-gate 
18780Sstevel@tonic-gate 	/*
18790Sstevel@tonic-gate 	 * Import "root" properties from the boot.
18800Sstevel@tonic-gate 	 *
18810Sstevel@tonic-gate 	 * We do this by invoking BOP_NEXTPROP until the list
18820Sstevel@tonic-gate 	 * is completely copied in.
18830Sstevel@tonic-gate 	 */
18840Sstevel@tonic-gate 
18850Sstevel@tonic-gate 	devi = ddi_root_node();
18860Sstevel@tonic-gate 	for (name = BOP_NEXTPROP(bootops, "");		/* get first */
18870Sstevel@tonic-gate 	    name;					/* NULL => DONE */
18880Sstevel@tonic-gate 	    name = BOP_NEXTPROP(bootops, name)) {	/* get next */
18890Sstevel@tonic-gate 
18900Sstevel@tonic-gate 		/* copy string to memory above kernelbase */
18910Sstevel@tonic-gate 		copy_boot_str(name, property_name, 50);
18920Sstevel@tonic-gate 
18930Sstevel@tonic-gate 		/*
18940Sstevel@tonic-gate 		 * Skip vga properties. They will be picked up later
18950Sstevel@tonic-gate 		 * by get_vga_properties.
18960Sstevel@tonic-gate 		 */
18970Sstevel@tonic-gate 		if (strcmp(property_name, "display-edif-block") == 0 ||
18980Sstevel@tonic-gate 		    strcmp(property_name, "display-edif-id") == 0) {
18990Sstevel@tonic-gate 			continue;
19000Sstevel@tonic-gate 		}
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate 		length = BOP_GETPROPLEN(bootops, property_name);
19030Sstevel@tonic-gate 		if (length == 0)
19040Sstevel@tonic-gate 			continue;
19050Sstevel@tonic-gate 		if (length > MMU_PAGESIZE) {
19060Sstevel@tonic-gate 			cmn_err(CE_NOTE,
19070Sstevel@tonic-gate 			    "boot property %s longer than 0x%x, ignored\n",
19080Sstevel@tonic-gate 			    property_name, MMU_PAGESIZE);
19090Sstevel@tonic-gate 			continue;
19100Sstevel@tonic-gate 		}
19110Sstevel@tonic-gate 		BOP_GETPROP(bootops, property_name, bop_staging_area);
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate 		/*
19140Sstevel@tonic-gate 		 * special properties:
19150Sstevel@tonic-gate 		 * si-machine, si-hw-provider
19160Sstevel@tonic-gate 		 *	goes to kernel data structures.
19170Sstevel@tonic-gate 		 * bios-boot-device and stdout
19180Sstevel@tonic-gate 		 *	goes to hardware property list so it may show up
19190Sstevel@tonic-gate 		 *	in the prtconf -vp output. This is needed by
19200Sstevel@tonic-gate 		 *	Install/Upgrade. Once we fix install upgrade,
19210Sstevel@tonic-gate 		 *	this can be taken out.
19220Sstevel@tonic-gate 		 */
19230Sstevel@tonic-gate 		if (strcmp(name, "si-machine") == 0) {
19240Sstevel@tonic-gate 			(void) strncpy(utsname.machine, bop_staging_area,
19250Sstevel@tonic-gate 			    SYS_NMLN);
19260Sstevel@tonic-gate 			utsname.machine[SYS_NMLN - 1] = (char)NULL;
19270Sstevel@tonic-gate 		} else if (strcmp(name, "si-hw-provider") == 0) {
19280Sstevel@tonic-gate 			(void) strncpy(hw_provider, bop_staging_area, SYS_NMLN);
19290Sstevel@tonic-gate 			hw_provider[SYS_NMLN - 1] = (char)NULL;
19300Sstevel@tonic-gate 		} else if (strcmp(name, "bios-boot-device") == 0) {
19310Sstevel@tonic-gate 			copy_boot_str(bop_staging_area, property_val, 50);
19320Sstevel@tonic-gate 			(void) ndi_prop_update_string(DDI_DEV_T_NONE, devi,
19330Sstevel@tonic-gate 			    property_name, property_val);
19340Sstevel@tonic-gate 		} else if (strcmp(name, "stdout") == 0) {
19350Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, devi,
19360Sstevel@tonic-gate 			    property_name, *((int *)bop_staging_area));
19370Sstevel@tonic-gate 		} else {
19380Sstevel@tonic-gate 			/* Property type unknown, use old prop interface */
19390Sstevel@tonic-gate 			(void) e_ddi_prop_create(DDI_DEV_T_NONE, devi,
19400Sstevel@tonic-gate 			    DDI_PROP_CANSLEEP, property_name, bop_staging_area,
19410Sstevel@tonic-gate 			    length);
19420Sstevel@tonic-gate 		}
19430Sstevel@tonic-gate 	}
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate 	kmem_free(bop_staging_area, MMU_PAGESIZE);
19460Sstevel@tonic-gate }
19470Sstevel@tonic-gate 
19480Sstevel@tonic-gate static void
19490Sstevel@tonic-gate get_vga_properties(void)
19500Sstevel@tonic-gate {
19510Sstevel@tonic-gate 	dev_info_t *devi;
19520Sstevel@tonic-gate 	major_t major;
19530Sstevel@tonic-gate 	char *name;
19540Sstevel@tonic-gate 	int length;
19550Sstevel@tonic-gate 	char property_val[50];
19560Sstevel@tonic-gate 	void *bop_staging_area;
19570Sstevel@tonic-gate 
19585295Srandyf 	/*
19595295Srandyf 	 * XXXX Hack Allert!
19605295Srandyf 	 * There really needs to be a better way for identifying various
19615295Srandyf 	 * console framebuffers and their related issues.  Till then,
19625295Srandyf 	 * check for this one as a replacement to vgatext.
19635295Srandyf 	 */
19645295Srandyf 	major = ddi_name_to_major("ragexl");
19655295Srandyf 	if (major == (major_t)-1) {
19665295Srandyf 		major = ddi_name_to_major("vgatext");
19675295Srandyf 		if (major == (major_t)-1)
19685295Srandyf 			return;
19695295Srandyf 	}
19700Sstevel@tonic-gate 	devi = devnamesp[major].dn_head;
19710Sstevel@tonic-gate 	if (devi == NULL)
19720Sstevel@tonic-gate 		return;
19730Sstevel@tonic-gate 
19740Sstevel@tonic-gate 	bop_staging_area = kmem_zalloc(MMU_PAGESIZE, KM_SLEEP);
19750Sstevel@tonic-gate 
19760Sstevel@tonic-gate 	/*
19770Sstevel@tonic-gate 	 * Import "vga" properties from the boot.
19780Sstevel@tonic-gate 	 */
19790Sstevel@tonic-gate 	name = "display-edif-block";
19800Sstevel@tonic-gate 	length = BOP_GETPROPLEN(bootops, name);
19810Sstevel@tonic-gate 	if (length > 0 && length < MMU_PAGESIZE) {
19820Sstevel@tonic-gate 		BOP_GETPROP(bootops, name, bop_staging_area);
19830Sstevel@tonic-gate 		(void) ndi_prop_update_byte_array(DDI_DEV_T_NONE,
19840Sstevel@tonic-gate 		    devi, name, bop_staging_area, length);
19850Sstevel@tonic-gate 	}
19860Sstevel@tonic-gate 
19870Sstevel@tonic-gate 	/*
19880Sstevel@tonic-gate 	 * kdmconfig is also looking for display-type and
19890Sstevel@tonic-gate 	 * video-adapter-type. We default to color and svga.
19900Sstevel@tonic-gate 	 *
19910Sstevel@tonic-gate 	 * Could it be "monochrome", "vga"?
19920Sstevel@tonic-gate 	 * Nah, you've got to come to the 21st century...
19930Sstevel@tonic-gate 	 * And you can set monitor type manually in kdmconfig
19940Sstevel@tonic-gate 	 * if you are really an old junky.
19950Sstevel@tonic-gate 	 */
19960Sstevel@tonic-gate 	(void) ndi_prop_update_string(DDI_DEV_T_NONE,
19970Sstevel@tonic-gate 	    devi, "display-type", "color");
19980Sstevel@tonic-gate 	(void) ndi_prop_update_string(DDI_DEV_T_NONE,
19990Sstevel@tonic-gate 	    devi, "video-adapter-type", "svga");
20000Sstevel@tonic-gate 
20010Sstevel@tonic-gate 	name = "display-edif-id";
20020Sstevel@tonic-gate 	length = BOP_GETPROPLEN(bootops, name);
20030Sstevel@tonic-gate 	if (length > 0 && length < MMU_PAGESIZE) {
20040Sstevel@tonic-gate 		BOP_GETPROP(bootops, name, bop_staging_area);
20050Sstevel@tonic-gate 		copy_boot_str(bop_staging_area, property_val, length);
20060Sstevel@tonic-gate 		(void) ndi_prop_update_string(DDI_DEV_T_NONE,
20070Sstevel@tonic-gate 		    devi, name, property_val);
20080Sstevel@tonic-gate 	}
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate 	kmem_free(bop_staging_area, MMU_PAGESIZE);
20110Sstevel@tonic-gate }
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate 
20140Sstevel@tonic-gate /*
20150Sstevel@tonic-gate  * This is temporary, but absolutely necessary.  If we are being
20160Sstevel@tonic-gate  * booted with a device tree created by the DevConf project's bootconf
20170Sstevel@tonic-gate  * program, then we have device information nodes that reflect
20180Sstevel@tonic-gate  * reality.  At this point in time in the Solaris release schedule, the
20190Sstevel@tonic-gate  * kernel drivers aren't prepared for reality.  They still depend on their
20200Sstevel@tonic-gate  * own ad-hoc interpretations of the properties created when their .conf
20210Sstevel@tonic-gate  * files were interpreted. These drivers use an "ignore-hardware-nodes"
20220Sstevel@tonic-gate  * property to prevent them from using the nodes passed up from the bootconf
20230Sstevel@tonic-gate  * device tree.
20240Sstevel@tonic-gate  *
20250Sstevel@tonic-gate  * Trying to assemble root file system drivers as we are booting from
20260Sstevel@tonic-gate  * devconf will fail if the kernel driver is basing its name_addr's on the
20270Sstevel@tonic-gate  * psuedo-node device info while the bootpath passed up from bootconf is using
20280Sstevel@tonic-gate  * reality-based name_addrs.  We help the boot along in this case by
20290Sstevel@tonic-gate  * looking at the pre-bootconf bootpath and determining if we would have
20300Sstevel@tonic-gate  * successfully matched if that had been the bootpath we had chosen.
20310Sstevel@tonic-gate  *
20320Sstevel@tonic-gate  * Note that we only even perform this extra check if we've booted
20330Sstevel@tonic-gate  * using bootconf's 1275 compliant bootpath, this is the boot device, and
20340Sstevel@tonic-gate  * we're trying to match the name_addr specified in the 1275 bootpath.
20350Sstevel@tonic-gate  */
20360Sstevel@tonic-gate 
20370Sstevel@tonic-gate #define	MAXCOMPONENTLEN	32
20380Sstevel@tonic-gate 
20390Sstevel@tonic-gate int
20400Sstevel@tonic-gate x86_old_bootpath_name_addr_match(dev_info_t *cdip, char *caddr, char *naddr)
20410Sstevel@tonic-gate {
20420Sstevel@tonic-gate 	/*
20430Sstevel@tonic-gate 	 *  There are multiple criteria to be met before we can even
20440Sstevel@tonic-gate 	 *  consider allowing a name_addr match here.
20450Sstevel@tonic-gate 	 *
20460Sstevel@tonic-gate 	 *  1) We must have been booted such that the bootconf program
20470Sstevel@tonic-gate 	 *	created device tree nodes and properties.  This can be
20480Sstevel@tonic-gate 	 *	determined by examining the 'bootpath' property.  This
20490Sstevel@tonic-gate 	 *	property will be a non-null string iff bootconf was
20500Sstevel@tonic-gate 	 *	involved in the boot.
20510Sstevel@tonic-gate 	 *
20520Sstevel@tonic-gate 	 *  2) The module that we want to match must be the boot device.
20530Sstevel@tonic-gate 	 *
20540Sstevel@tonic-gate 	 *  3) The instance of the module we are thinking of letting be
20550Sstevel@tonic-gate 	 *	our match must be ignoring hardware nodes.
20560Sstevel@tonic-gate 	 *
20570Sstevel@tonic-gate 	 *  4) The name_addr we want to match must be the name_addr
20580Sstevel@tonic-gate 	 *	specified in the 1275 bootpath.
20590Sstevel@tonic-gate 	 */
20600Sstevel@tonic-gate 	static char bootdev_module[MAXCOMPONENTLEN];
20610Sstevel@tonic-gate 	static char bootdev_oldmod[MAXCOMPONENTLEN];
20620Sstevel@tonic-gate 	static char bootdev_newaddr[MAXCOMPONENTLEN];
20630Sstevel@tonic-gate 	static char bootdev_oldaddr[MAXCOMPONENTLEN];
20640Sstevel@tonic-gate 	static int  quickexit;
20650Sstevel@tonic-gate 
20660Sstevel@tonic-gate 	char *daddr;
20670Sstevel@tonic-gate 	int dlen;
20680Sstevel@tonic-gate 
20690Sstevel@tonic-gate 	char	*lkupname;
20700Sstevel@tonic-gate 	int	rv = DDI_FAILURE;
20710Sstevel@tonic-gate 
20720Sstevel@tonic-gate 	if ((ddi_getlongprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
20735084Sjohnlev 	    "devconf-addr", (caddr_t)&daddr, &dlen) == DDI_PROP_SUCCESS) &&
20740Sstevel@tonic-gate 	    (ddi_getprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
20755084Sjohnlev 	    "ignore-hardware-nodes", -1) != -1)) {
20760Sstevel@tonic-gate 		if (strcmp(daddr, caddr) == 0) {
20770Sstevel@tonic-gate 			return (DDI_SUCCESS);
20780Sstevel@tonic-gate 		}
20790Sstevel@tonic-gate 	}
20800Sstevel@tonic-gate 
20810Sstevel@tonic-gate 	if (quickexit)
20820Sstevel@tonic-gate 		return (rv);
20830Sstevel@tonic-gate 
20840Sstevel@tonic-gate 	if (bootdev_module[0] == '\0') {
20850Sstevel@tonic-gate 		char *addrp, *eoaddrp;
20860Sstevel@tonic-gate 		char *busp, *modp, *atp;
20870Sstevel@tonic-gate 		char *bp1275, *bp;
20880Sstevel@tonic-gate 		int  bp1275len, bplen;
20890Sstevel@tonic-gate 
20900Sstevel@tonic-gate 		bp1275 = bp = addrp = eoaddrp = busp = modp = atp = NULL;
20910Sstevel@tonic-gate 
20920Sstevel@tonic-gate 		if (ddi_getlongprop(DDI_DEV_T_ANY,
20930Sstevel@tonic-gate 		    ddi_root_node(), 0, "bootpath",
20940Sstevel@tonic-gate 		    (caddr_t)&bp1275, &bp1275len) != DDI_PROP_SUCCESS ||
20950Sstevel@tonic-gate 		    bp1275len <= 1) {
20960Sstevel@tonic-gate 			/*
20970Sstevel@tonic-gate 			 * We didn't boot from bootconf so we never need to
20980Sstevel@tonic-gate 			 * do any special matches.
20990Sstevel@tonic-gate 			 */
21000Sstevel@tonic-gate 			quickexit = 1;
21010Sstevel@tonic-gate 			if (bp1275)
21020Sstevel@tonic-gate 				kmem_free(bp1275, bp1275len);
21030Sstevel@tonic-gate 			return (rv);
21040Sstevel@tonic-gate 		}
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate 		if (ddi_getlongprop(DDI_DEV_T_ANY,
21070Sstevel@tonic-gate 		    ddi_root_node(), 0, "boot-path",
21080Sstevel@tonic-gate 		    (caddr_t)&bp, &bplen) != DDI_PROP_SUCCESS || bplen <= 1) {
21090Sstevel@tonic-gate 			/*
21100Sstevel@tonic-gate 			 * No fallback position for matching. This is
21110Sstevel@tonic-gate 			 * certainly unexpected, but we'll handle it
21120Sstevel@tonic-gate 			 * just in case.
21130Sstevel@tonic-gate 			 */
21140Sstevel@tonic-gate 			quickexit = 1;
21150Sstevel@tonic-gate 			kmem_free(bp1275, bp1275len);
21160Sstevel@tonic-gate 			if (bp)
21170Sstevel@tonic-gate 				kmem_free(bp, bplen);
21180Sstevel@tonic-gate 			return (rv);
21190Sstevel@tonic-gate 		}
21200Sstevel@tonic-gate 
21210Sstevel@tonic-gate 		/*
21220Sstevel@tonic-gate 		 *  Determine boot device module and 1275 name_addr
21230Sstevel@tonic-gate 		 *
21240Sstevel@tonic-gate 		 *  bootpath assumed to be of the form /bus/module@name_addr
21250Sstevel@tonic-gate 		 */
21260Sstevel@tonic-gate 		if (busp = strchr(bp1275, '/')) {
21270Sstevel@tonic-gate 			if (modp = strchr(busp + 1, '/')) {
21280Sstevel@tonic-gate 				if (atp = strchr(modp + 1, '@')) {
21290Sstevel@tonic-gate 					*atp = '\0';
21300Sstevel@tonic-gate 					addrp = atp + 1;
21310Sstevel@tonic-gate 					if (eoaddrp = strchr(addrp, '/'))
21320Sstevel@tonic-gate 						*eoaddrp = '\0';
21330Sstevel@tonic-gate 				}
21340Sstevel@tonic-gate 			}
21350Sstevel@tonic-gate 		}
21360Sstevel@tonic-gate 
21370Sstevel@tonic-gate 		if (modp && addrp) {
21380Sstevel@tonic-gate 			(void) strncpy(bootdev_module, modp + 1,
21390Sstevel@tonic-gate 			    MAXCOMPONENTLEN);
21400Sstevel@tonic-gate 			bootdev_module[MAXCOMPONENTLEN - 1] = '\0';
21410Sstevel@tonic-gate 
21420Sstevel@tonic-gate 			(void) strncpy(bootdev_newaddr, addrp, MAXCOMPONENTLEN);
21430Sstevel@tonic-gate 			bootdev_newaddr[MAXCOMPONENTLEN - 1] = '\0';
21440Sstevel@tonic-gate 		} else {
21450Sstevel@tonic-gate 			quickexit = 1;
21460Sstevel@tonic-gate 			kmem_free(bp1275, bp1275len);
21470Sstevel@tonic-gate 			kmem_free(bp, bplen);
21480Sstevel@tonic-gate 			return (rv);
21490Sstevel@tonic-gate 		}
21500Sstevel@tonic-gate 
21510Sstevel@tonic-gate 		/*
21520Sstevel@tonic-gate 		 *  Determine fallback name_addr
21530Sstevel@tonic-gate 		 *
21540Sstevel@tonic-gate 		 *  10/3/96 - Also save fallback module name because it
21550Sstevel@tonic-gate 		 *  might actually be different than the current module
21560Sstevel@tonic-gate 		 *  name.  E.G., ISA pnp drivers have new names.
21570Sstevel@tonic-gate 		 *
21580Sstevel@tonic-gate 		 *  bootpath assumed to be of the form /bus/module@name_addr
21590Sstevel@tonic-gate 		 */
21600Sstevel@tonic-gate 		addrp = NULL;
21610Sstevel@tonic-gate 		if (busp = strchr(bp, '/')) {
21620Sstevel@tonic-gate 			if (modp = strchr(busp + 1, '/')) {
21630Sstevel@tonic-gate 				if (atp = strchr(modp + 1, '@')) {
21640Sstevel@tonic-gate 					*atp = '\0';
21650Sstevel@tonic-gate 					addrp = atp + 1;
21660Sstevel@tonic-gate 					if (eoaddrp = strchr(addrp, '/'))
21670Sstevel@tonic-gate 						*eoaddrp = '\0';
21680Sstevel@tonic-gate 				}
21690Sstevel@tonic-gate 			}
21700Sstevel@tonic-gate 		}
21710Sstevel@tonic-gate 
21720Sstevel@tonic-gate 		if (modp && addrp) {
21730Sstevel@tonic-gate 			(void) strncpy(bootdev_oldmod, modp + 1,
21740Sstevel@tonic-gate 			    MAXCOMPONENTLEN);
21750Sstevel@tonic-gate 			bootdev_module[MAXCOMPONENTLEN - 1] = '\0';
21760Sstevel@tonic-gate 
21770Sstevel@tonic-gate 			(void) strncpy(bootdev_oldaddr, addrp, MAXCOMPONENTLEN);
21780Sstevel@tonic-gate 			bootdev_oldaddr[MAXCOMPONENTLEN - 1] = '\0';
21790Sstevel@tonic-gate 		}
21800Sstevel@tonic-gate 
21810Sstevel@tonic-gate 		/* Free up the bootpath storage now that we're done with it. */
21820Sstevel@tonic-gate 		kmem_free(bp1275, bp1275len);
21830Sstevel@tonic-gate 		kmem_free(bp, bplen);
21840Sstevel@tonic-gate 
21850Sstevel@tonic-gate 		if (bootdev_oldaddr[0] == '\0') {
21860Sstevel@tonic-gate 			quickexit = 1;
21870Sstevel@tonic-gate 			return (rv);
21880Sstevel@tonic-gate 		}
21890Sstevel@tonic-gate 	}
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 	if (((lkupname = ddi_get_name(cdip)) != NULL) &&
21920Sstevel@tonic-gate 	    (strcmp(bootdev_module, lkupname) == 0 ||
21930Sstevel@tonic-gate 	    strcmp(bootdev_oldmod, lkupname) == 0) &&
21940Sstevel@tonic-gate 	    ((ddi_getprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
21955084Sjohnlev 	    "ignore-hardware-nodes", -1) != -1) ||
21965084Sjohnlev 	    ignore_hardware_nodes) &&
21970Sstevel@tonic-gate 	    strcmp(bootdev_newaddr, caddr) == 0 &&
21980Sstevel@tonic-gate 	    strcmp(bootdev_oldaddr, naddr) == 0) {
21990Sstevel@tonic-gate 		rv = DDI_SUCCESS;
22000Sstevel@tonic-gate 	}
22010Sstevel@tonic-gate 
22020Sstevel@tonic-gate 	return (rv);
22030Sstevel@tonic-gate }
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate /*
22060Sstevel@tonic-gate  * Perform a copy from a memory mapped device (whose devinfo pointer is devi)
22070Sstevel@tonic-gate  * separately mapped at devaddr in the kernel to a kernel buffer at kaddr.
22080Sstevel@tonic-gate  */
22090Sstevel@tonic-gate /*ARGSUSED*/
22100Sstevel@tonic-gate int
22110Sstevel@tonic-gate e_ddi_copyfromdev(dev_info_t *devi,
22120Sstevel@tonic-gate     off_t off, const void *devaddr, void *kaddr, size_t len)
22130Sstevel@tonic-gate {
22140Sstevel@tonic-gate 	bcopy(devaddr, kaddr, len);
22150Sstevel@tonic-gate 	return (0);
22160Sstevel@tonic-gate }
22170Sstevel@tonic-gate 
22180Sstevel@tonic-gate /*
22190Sstevel@tonic-gate  * Perform a copy to a memory mapped device (whose devinfo pointer is devi)
22200Sstevel@tonic-gate  * separately mapped at devaddr in the kernel from a kernel buffer at kaddr.
22210Sstevel@tonic-gate  */
22220Sstevel@tonic-gate /*ARGSUSED*/
22230Sstevel@tonic-gate int
22240Sstevel@tonic-gate e_ddi_copytodev(dev_info_t *devi,
22250Sstevel@tonic-gate     off_t off, const void *kaddr, void *devaddr, size_t len)
22260Sstevel@tonic-gate {
22270Sstevel@tonic-gate 	bcopy(kaddr, devaddr, len);
22280Sstevel@tonic-gate 	return (0);
22290Sstevel@tonic-gate }
22300Sstevel@tonic-gate 
22310Sstevel@tonic-gate 
22320Sstevel@tonic-gate static int
22330Sstevel@tonic-gate poke_mem(peekpoke_ctlops_t *in_args)
22340Sstevel@tonic-gate {
22350Sstevel@tonic-gate 	int err = DDI_SUCCESS;
22360Sstevel@tonic-gate 	on_trap_data_t otd;
22370Sstevel@tonic-gate 
22380Sstevel@tonic-gate 	/* Set up protected environment. */
22390Sstevel@tonic-gate 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
22400Sstevel@tonic-gate 		switch (in_args->size) {
22410Sstevel@tonic-gate 		case sizeof (uint8_t):
22420Sstevel@tonic-gate 			*(uint8_t *)(in_args->dev_addr) =
22430Sstevel@tonic-gate 			    *(uint8_t *)in_args->host_addr;
22440Sstevel@tonic-gate 			break;
22450Sstevel@tonic-gate 
22460Sstevel@tonic-gate 		case sizeof (uint16_t):
22470Sstevel@tonic-gate 			*(uint16_t *)(in_args->dev_addr) =
22480Sstevel@tonic-gate 			    *(uint16_t *)in_args->host_addr;
22490Sstevel@tonic-gate 			break;
22500Sstevel@tonic-gate 
22510Sstevel@tonic-gate 		case sizeof (uint32_t):
22520Sstevel@tonic-gate 			*(uint32_t *)(in_args->dev_addr) =
22530Sstevel@tonic-gate 			    *(uint32_t *)in_args->host_addr;
22540Sstevel@tonic-gate 			break;
22550Sstevel@tonic-gate 
22560Sstevel@tonic-gate 		case sizeof (uint64_t):
22570Sstevel@tonic-gate 			*(uint64_t *)(in_args->dev_addr) =
22580Sstevel@tonic-gate 			    *(uint64_t *)in_args->host_addr;
22590Sstevel@tonic-gate 			break;
22600Sstevel@tonic-gate 
22610Sstevel@tonic-gate 		default:
22620Sstevel@tonic-gate 			err = DDI_FAILURE;
22630Sstevel@tonic-gate 			break;
22640Sstevel@tonic-gate 		}
22650Sstevel@tonic-gate 	} else
22660Sstevel@tonic-gate 		err = DDI_FAILURE;
22670Sstevel@tonic-gate 
22680Sstevel@tonic-gate 	/* Take down protected environment. */
22690Sstevel@tonic-gate 	no_trap();
22700Sstevel@tonic-gate 
22710Sstevel@tonic-gate 	return (err);
22720Sstevel@tonic-gate }
22730Sstevel@tonic-gate 
22740Sstevel@tonic-gate 
22750Sstevel@tonic-gate static int
22760Sstevel@tonic-gate peek_mem(peekpoke_ctlops_t *in_args)
22770Sstevel@tonic-gate {
22780Sstevel@tonic-gate 	int err = DDI_SUCCESS;
22790Sstevel@tonic-gate 	on_trap_data_t otd;
22800Sstevel@tonic-gate 
22810Sstevel@tonic-gate 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
22820Sstevel@tonic-gate 		switch (in_args->size) {
22830Sstevel@tonic-gate 		case sizeof (uint8_t):
22840Sstevel@tonic-gate 			*(uint8_t *)in_args->host_addr =
22850Sstevel@tonic-gate 			    *(uint8_t *)in_args->dev_addr;
22860Sstevel@tonic-gate 			break;
22870Sstevel@tonic-gate 
22880Sstevel@tonic-gate 		case sizeof (uint16_t):
22890Sstevel@tonic-gate 			*(uint16_t *)in_args->host_addr =
22900Sstevel@tonic-gate 			    *(uint16_t *)in_args->dev_addr;
22910Sstevel@tonic-gate 			break;
22920Sstevel@tonic-gate 
22930Sstevel@tonic-gate 		case sizeof (uint32_t):
22940Sstevel@tonic-gate 			*(uint32_t *)in_args->host_addr =
22950Sstevel@tonic-gate 			    *(uint32_t *)in_args->dev_addr;
22960Sstevel@tonic-gate 			break;
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate 		case sizeof (uint64_t):
22990Sstevel@tonic-gate 			*(uint64_t *)in_args->host_addr =
23000Sstevel@tonic-gate 			    *(uint64_t *)in_args->dev_addr;
23010Sstevel@tonic-gate 			break;
23020Sstevel@tonic-gate 
23030Sstevel@tonic-gate 		default:
23040Sstevel@tonic-gate 			err = DDI_FAILURE;
23050Sstevel@tonic-gate 			break;
23060Sstevel@tonic-gate 		}
23070Sstevel@tonic-gate 	} else
23080Sstevel@tonic-gate 		err = DDI_FAILURE;
23090Sstevel@tonic-gate 
23100Sstevel@tonic-gate 	no_trap();
23110Sstevel@tonic-gate 	return (err);
23120Sstevel@tonic-gate }
23130Sstevel@tonic-gate 
23140Sstevel@tonic-gate 
23150Sstevel@tonic-gate /*
23160Sstevel@tonic-gate  * This is called only to process peek/poke when the DIP is NULL.
23170Sstevel@tonic-gate  * Assume that this is for memory, as nexi take care of device safe accesses.
23180Sstevel@tonic-gate  */
23190Sstevel@tonic-gate int
23200Sstevel@tonic-gate peekpoke_mem(ddi_ctl_enum_t cmd, peekpoke_ctlops_t *in_args)
23210Sstevel@tonic-gate {
23220Sstevel@tonic-gate 	return (cmd == DDI_CTLOPS_PEEK ? peek_mem(in_args) : poke_mem(in_args));
23230Sstevel@tonic-gate }
23240Sstevel@tonic-gate 
23251865Sdilpreet /*
23261865Sdilpreet  * we've just done a cautious put/get. Check if it was successful by
23271865Sdilpreet  * calling pci_ereport_post() on all puts and for any gets that return -1
23281865Sdilpreet  */
23291865Sdilpreet static int
23306313Skrishnae pci_peekpoke_check_fma(dev_info_t *dip, void *arg, ddi_ctl_enum_t ctlop,
23316313Skrishnae     void (*scan)(dev_info_t *, ddi_fm_error_t *))
23321865Sdilpreet {
23331865Sdilpreet 	int	rval = DDI_SUCCESS;
23341865Sdilpreet 	peekpoke_ctlops_t *in_args = (peekpoke_ctlops_t *)arg;
23351865Sdilpreet 	ddi_fm_error_t de;
23361865Sdilpreet 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)in_args->handle;
23371865Sdilpreet 	ddi_acc_hdl_t *hdlp = (ddi_acc_hdl_t *)in_args->handle;
23381865Sdilpreet 	int check_err = 0;
23391865Sdilpreet 	int repcount = in_args->repcount;
23401865Sdilpreet 
23411901Sdilpreet 	if (ctlop == DDI_CTLOPS_POKE &&
23421901Sdilpreet 	    hdlp->ah_acc.devacc_attr_access != DDI_CAUTIOUS_ACC)
23431901Sdilpreet 		return (DDI_SUCCESS);
23441901Sdilpreet 
23451865Sdilpreet 	if (ctlop == DDI_CTLOPS_PEEK &&
23461865Sdilpreet 	    hdlp->ah_acc.devacc_attr_access != DDI_CAUTIOUS_ACC) {
23471865Sdilpreet 		for (; repcount; repcount--) {
23481865Sdilpreet 			switch (in_args->size) {
23491865Sdilpreet 			case sizeof (uint8_t):
23501865Sdilpreet 				if (*(uint8_t *)in_args->host_addr == 0xff)
23511865Sdilpreet 					check_err = 1;
23521865Sdilpreet 				break;
23531865Sdilpreet 			case sizeof (uint16_t):
23541865Sdilpreet 				if (*(uint16_t *)in_args->host_addr == 0xffff)
23551865Sdilpreet 					check_err = 1;
23561865Sdilpreet 				break;
23571865Sdilpreet 			case sizeof (uint32_t):
23581865Sdilpreet 				if (*(uint32_t *)in_args->host_addr ==
23591865Sdilpreet 				    0xffffffff)
23601865Sdilpreet 					check_err = 1;
23611865Sdilpreet 				break;
23621865Sdilpreet 			case sizeof (uint64_t):
23631865Sdilpreet 				if (*(uint64_t *)in_args->host_addr ==
23641865Sdilpreet 				    0xffffffffffffffff)
23651865Sdilpreet 					check_err = 1;
23661865Sdilpreet 				break;
23671865Sdilpreet 			}
23681865Sdilpreet 		}
23691865Sdilpreet 		if (check_err == 0)
23701865Sdilpreet 			return (DDI_SUCCESS);
23711865Sdilpreet 	}
23721865Sdilpreet 	/*
23731865Sdilpreet 	 * for a cautious put or get or a non-cautious get that returned -1 call
23741865Sdilpreet 	 * io framework to see if there really was an error
23751865Sdilpreet 	 */
23761865Sdilpreet 	bzero(&de, sizeof (ddi_fm_error_t));
23773159Sstephh 	de.fme_version = DDI_FME_VERSION;
23781865Sdilpreet 	de.fme_ena = fm_ena_generate(0, FM_ENA_FMT1);
23791865Sdilpreet 	if (hdlp->ah_acc.devacc_attr_access == DDI_CAUTIOUS_ACC) {
23801865Sdilpreet 		de.fme_flag = DDI_FM_ERR_EXPECTED;
23811865Sdilpreet 		de.fme_acc_handle = in_args->handle;
23821865Sdilpreet 	} else if (hdlp->ah_acc.devacc_attr_access == DDI_DEFAULT_ACC) {
23831865Sdilpreet 		/*
23841865Sdilpreet 		 * We only get here with DDI_DEFAULT_ACC for config space gets.
23851865Sdilpreet 		 * Non-hardened drivers may be probing the hardware and
23861865Sdilpreet 		 * expecting -1 returned. So need to treat errors on
23871865Sdilpreet 		 * DDI_DEFAULT_ACC as DDI_FM_ERR_EXPECTED.
23881865Sdilpreet 		 */
23891865Sdilpreet 		de.fme_flag = DDI_FM_ERR_EXPECTED;
23901865Sdilpreet 		de.fme_acc_handle = in_args->handle;
23911865Sdilpreet 	} else {
23921865Sdilpreet 		/*
23931865Sdilpreet 		 * Hardened driver doing protected accesses shouldn't
23941865Sdilpreet 		 * get errors unless there's a hardware problem. Treat
23951865Sdilpreet 		 * as nonfatal if there's an error, but set UNEXPECTED
23961865Sdilpreet 		 * so we raise ereports on any errors and potentially
23971865Sdilpreet 		 * fault the device
23981865Sdilpreet 		 */
23991865Sdilpreet 		de.fme_flag = DDI_FM_ERR_UNEXPECTED;
24001865Sdilpreet 	}
24016313Skrishnae 	(void) scan(dip, &de);
24021865Sdilpreet 	if (hdlp->ah_acc.devacc_attr_access != DDI_DEFAULT_ACC &&
24031865Sdilpreet 	    de.fme_status != DDI_FM_OK) {
24041865Sdilpreet 		ndi_err_t *errp = (ndi_err_t *)hp->ahi_err;
24051865Sdilpreet 		rval = DDI_FAILURE;
24061865Sdilpreet 		errp->err_ena = de.fme_ena;
24071865Sdilpreet 		errp->err_expected = de.fme_flag;
24081865Sdilpreet 		errp->err_status = DDI_FM_NONFATAL;
24091865Sdilpreet 	}
24101865Sdilpreet 	return (rval);
24111865Sdilpreet }
24121865Sdilpreet 
24131865Sdilpreet /*
24141865Sdilpreet  * pci_peekpoke_check_nofma() is for when an error occurs on a register access
24151865Sdilpreet  * during pci_ereport_post(). We can't call pci_ereport_post() again or we'd
24161865Sdilpreet  * recurse, so assume all puts are OK and gets have failed if they return -1
24171865Sdilpreet  */
24181865Sdilpreet static int
24191865Sdilpreet pci_peekpoke_check_nofma(void *arg, ddi_ctl_enum_t ctlop)
24201865Sdilpreet {
24211865Sdilpreet 	int rval = DDI_SUCCESS;
24221865Sdilpreet 	peekpoke_ctlops_t *in_args = (peekpoke_ctlops_t *)arg;
24231865Sdilpreet 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)in_args->handle;
24241865Sdilpreet 	ddi_acc_hdl_t *hdlp = (ddi_acc_hdl_t *)in_args->handle;
24251865Sdilpreet 	int repcount = in_args->repcount;
24261865Sdilpreet 
24271865Sdilpreet 	if (ctlop == DDI_CTLOPS_POKE)
24281865Sdilpreet 		return (rval);
24291865Sdilpreet 
24301865Sdilpreet 	for (; repcount; repcount--) {
24311865Sdilpreet 		switch (in_args->size) {
24321865Sdilpreet 		case sizeof (uint8_t):
24331865Sdilpreet 			if (*(uint8_t *)in_args->host_addr == 0xff)
24341865Sdilpreet 				rval = DDI_FAILURE;
24351865Sdilpreet 			break;
24361865Sdilpreet 		case sizeof (uint16_t):
24371865Sdilpreet 			if (*(uint16_t *)in_args->host_addr == 0xffff)
24381865Sdilpreet 				rval = DDI_FAILURE;
24391865Sdilpreet 			break;
24401865Sdilpreet 		case sizeof (uint32_t):
24411865Sdilpreet 			if (*(uint32_t *)in_args->host_addr == 0xffffffff)
24421865Sdilpreet 				rval = DDI_FAILURE;
24431865Sdilpreet 			break;
24441865Sdilpreet 		case sizeof (uint64_t):
24451865Sdilpreet 			if (*(uint64_t *)in_args->host_addr ==
24461865Sdilpreet 			    0xffffffffffffffff)
24471865Sdilpreet 				rval = DDI_FAILURE;
24481865Sdilpreet 			break;
24491865Sdilpreet 		}
24501865Sdilpreet 	}
24511865Sdilpreet 	if (hdlp->ah_acc.devacc_attr_access != DDI_DEFAULT_ACC &&
24521865Sdilpreet 	    rval == DDI_FAILURE) {
24531865Sdilpreet 		ndi_err_t *errp = (ndi_err_t *)hp->ahi_err;
24541865Sdilpreet 		errp->err_ena = fm_ena_generate(0, FM_ENA_FMT1);
24551865Sdilpreet 		errp->err_expected = DDI_FM_ERR_UNEXPECTED;
24561865Sdilpreet 		errp->err_status = DDI_FM_NONFATAL;
24571865Sdilpreet 	}
24581865Sdilpreet 	return (rval);
24591865Sdilpreet }
24601865Sdilpreet 
24611865Sdilpreet int
24621865Sdilpreet pci_peekpoke_check(dev_info_t *dip, dev_info_t *rdip,
24631865Sdilpreet 	ddi_ctl_enum_t ctlop, void *arg, void *result,
24641865Sdilpreet 	int (*handler)(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *,
24656313Skrishnae 	void *), kmutex_t *err_mutexp, kmutex_t *peek_poke_mutexp,
24666313Skrishnae 	void (*scan)(dev_info_t *, ddi_fm_error_t *))
24671865Sdilpreet {
24681865Sdilpreet 	int rval;
24691865Sdilpreet 	peekpoke_ctlops_t *in_args = (peekpoke_ctlops_t *)arg;
24701865Sdilpreet 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)in_args->handle;
24711865Sdilpreet 
24723159Sstephh 	/*
24733159Sstephh 	 * this function only supports cautious accesses, not peeks/pokes
24743159Sstephh 	 * which don't have a handle
24753159Sstephh 	 */
24763159Sstephh 	if (hp == NULL)
24773159Sstephh 		return (DDI_FAILURE);
24783159Sstephh 
24791865Sdilpreet 	if (hp->ahi_acc_attr & DDI_ACCATTR_CONFIG_SPACE) {
24801901Sdilpreet 		if (!mutex_tryenter(err_mutexp)) {
24811865Sdilpreet 			/*
24821865Sdilpreet 			 * As this may be a recursive call from within
24831865Sdilpreet 			 * pci_ereport_post() we can't wait for the mutexes.
24841865Sdilpreet 			 * Fortunately we know someone is already calling
24851865Sdilpreet 			 * pci_ereport_post() which will handle the error bits
24861865Sdilpreet 			 * for us, and as this is a config space access we can
24871865Sdilpreet 			 * just do the access and check return value for -1
24881865Sdilpreet 			 * using pci_peekpoke_check_nofma().
24891865Sdilpreet 			 */
24901865Sdilpreet 			rval = handler(dip, rdip, ctlop, arg, result);
24911865Sdilpreet 			if (rval == DDI_SUCCESS)
24921865Sdilpreet 				rval = pci_peekpoke_check_nofma(arg, ctlop);
24931865Sdilpreet 			return (rval);
24941865Sdilpreet 		}
24951865Sdilpreet 		/*
24961865Sdilpreet 		 * This can't be a recursive call. Drop the err_mutex and get
24971865Sdilpreet 		 * both mutexes in the right order. If an error hasn't already
24981865Sdilpreet 		 * been detected by the ontrap code, use pci_peekpoke_check_fma
24991865Sdilpreet 		 * which will call pci_ereport_post() to check error status.
25001865Sdilpreet 		 */
25011865Sdilpreet 		mutex_exit(err_mutexp);
25021865Sdilpreet 	}
25031865Sdilpreet 	mutex_enter(peek_poke_mutexp);
25041865Sdilpreet 	rval = handler(dip, rdip, ctlop, arg, result);
25051865Sdilpreet 	if (rval == DDI_SUCCESS) {
25061865Sdilpreet 		mutex_enter(err_mutexp);
25076313Skrishnae 		rval = pci_peekpoke_check_fma(dip, arg, ctlop, scan);
25081865Sdilpreet 		mutex_exit(err_mutexp);
25091865Sdilpreet 	}
25101865Sdilpreet 	mutex_exit(peek_poke_mutexp);
25111865Sdilpreet 	return (rval);
25121865Sdilpreet }
25131865Sdilpreet 
25140Sstevel@tonic-gate void
25150Sstevel@tonic-gate impl_setup_ddi(void)
25160Sstevel@tonic-gate {
251710321SLipeng.Sang@Sun.COM #if !defined(__xpv)
251810321SLipeng.Sang@Sun.COM 	extern void startup_bios_disk(void);
251910321SLipeng.Sang@Sun.COM 	extern int post_fastreboot;
252010321SLipeng.Sang@Sun.COM #endif
25210Sstevel@tonic-gate 	dev_info_t *xdip, *isa_dip;
25220Sstevel@tonic-gate 	rd_existing_t rd_mem_prop;
25230Sstevel@tonic-gate 	int err;
25240Sstevel@tonic-gate 
25250Sstevel@tonic-gate 	ndi_devi_alloc_sleep(ddi_root_node(), "ramdisk",
2526789Sahrens 	    (pnode_t)DEVI_SID_NODEID, &xdip);
25270Sstevel@tonic-gate 
25280Sstevel@tonic-gate 	(void) BOP_GETPROP(bootops,
25290Sstevel@tonic-gate 	    "ramdisk_start", (void *)&ramdisk_start);
25300Sstevel@tonic-gate 	(void) BOP_GETPROP(bootops,
25310Sstevel@tonic-gate 	    "ramdisk_end", (void *)&ramdisk_end);
25320Sstevel@tonic-gate 
25335084Sjohnlev #ifdef __xpv
25345084Sjohnlev 	ramdisk_start -= ONE_GIG;
25355084Sjohnlev 	ramdisk_end -= ONE_GIG;
25365084Sjohnlev #endif
25370Sstevel@tonic-gate 	rd_mem_prop.phys = ramdisk_start;
25380Sstevel@tonic-gate 	rd_mem_prop.size = ramdisk_end - ramdisk_start + 1;
25390Sstevel@tonic-gate 
25400Sstevel@tonic-gate 	(void) ndi_prop_update_byte_array(DDI_DEV_T_NONE, xdip,
25410Sstevel@tonic-gate 	    RD_EXISTING_PROP_NAME, (uchar_t *)&rd_mem_prop,
25420Sstevel@tonic-gate 	    sizeof (rd_mem_prop));
25430Sstevel@tonic-gate 	err = ndi_devi_bind_driver(xdip, 0);
25440Sstevel@tonic-gate 	ASSERT(err == 0);
25450Sstevel@tonic-gate 
25460Sstevel@tonic-gate 	/* isa node */
25479149SJudy.Chen@Sun.COM 	if (pseudo_isa) {
25489149SJudy.Chen@Sun.COM 		ndi_devi_alloc_sleep(ddi_root_node(), "isa",
25499149SJudy.Chen@Sun.COM 		    (pnode_t)DEVI_SID_NODEID, &isa_dip);
25509149SJudy.Chen@Sun.COM 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, isa_dip,
25519149SJudy.Chen@Sun.COM 		    "device_type", "isa");
25529149SJudy.Chen@Sun.COM 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, isa_dip,
25539149SJudy.Chen@Sun.COM 		    "bus-type", "isa");
25549149SJudy.Chen@Sun.COM 		(void) ndi_devi_bind_driver(isa_dip, 0);
25559149SJudy.Chen@Sun.COM 	}
25560Sstevel@tonic-gate 
25570Sstevel@tonic-gate 	/*
25580Sstevel@tonic-gate 	 * Read in the properties from the boot.
25590Sstevel@tonic-gate 	 */
25600Sstevel@tonic-gate 	get_boot_properties();
25610Sstevel@tonic-gate 
256210321SLipeng.Sang@Sun.COM 	/* not framebuffer should be enumerated, if present */
256310321SLipeng.Sang@Sun.COM 	get_vga_properties();
256410321SLipeng.Sang@Sun.COM 
256510321SLipeng.Sang@Sun.COM #if !defined(__xpv)
256610321SLipeng.Sang@Sun.COM 	if (!post_fastreboot)
256710321SLipeng.Sang@Sun.COM 		startup_bios_disk();
256810321SLipeng.Sang@Sun.COM #endif
25690Sstevel@tonic-gate 	/* do bus dependent probes. */
25700Sstevel@tonic-gate 	impl_bus_initialprobe();
25710Sstevel@tonic-gate }
25720Sstevel@tonic-gate 
25730Sstevel@tonic-gate dev_t
25740Sstevel@tonic-gate getrootdev(void)
25750Sstevel@tonic-gate {
25760Sstevel@tonic-gate 	/*
25770Sstevel@tonic-gate 	 * Precedence given to rootdev if set in /etc/system
25780Sstevel@tonic-gate 	 */
25795648Ssetje 	if (root_is_svm == B_TRUE) {
25800Sstevel@tonic-gate 		return (ddi_pathname_to_dev_t(svm_bootpath));
25810Sstevel@tonic-gate 	}
25820Sstevel@tonic-gate 
25830Sstevel@tonic-gate 	/*
25840Sstevel@tonic-gate 	 * Usually rootfs.bo_name is initialized by the
25850Sstevel@tonic-gate 	 * the bootpath property from bootenv.rc, but
25860Sstevel@tonic-gate 	 * defaults to "/ramdisk:a" otherwise.
25870Sstevel@tonic-gate 	 */
25880Sstevel@tonic-gate 	return (ddi_pathname_to_dev_t(rootfs.bo_name));
25890Sstevel@tonic-gate }
25900Sstevel@tonic-gate 
25910Sstevel@tonic-gate static struct bus_probe {
25920Sstevel@tonic-gate 	struct bus_probe *next;
25930Sstevel@tonic-gate 	void (*probe)(int);
25940Sstevel@tonic-gate } *bus_probes;
25950Sstevel@tonic-gate 
25960Sstevel@tonic-gate void
25970Sstevel@tonic-gate impl_bus_add_probe(void (*func)(int))
25980Sstevel@tonic-gate {
25990Sstevel@tonic-gate 	struct bus_probe *probe;
260010127SLipeng.Sang@Sun.COM 	struct bus_probe *lastprobe = NULL;
26010Sstevel@tonic-gate 
26020Sstevel@tonic-gate 	probe = kmem_alloc(sizeof (*probe), KM_SLEEP);
26030Sstevel@tonic-gate 	probe->probe = func;
260410127SLipeng.Sang@Sun.COM 	probe->next = NULL;
260510127SLipeng.Sang@Sun.COM 
260610127SLipeng.Sang@Sun.COM 	if (!bus_probes) {
260710127SLipeng.Sang@Sun.COM 		bus_probes = probe;
260810127SLipeng.Sang@Sun.COM 		return;
260910127SLipeng.Sang@Sun.COM 	}
261010127SLipeng.Sang@Sun.COM 
261110127SLipeng.Sang@Sun.COM 	lastprobe = bus_probes;
261210127SLipeng.Sang@Sun.COM 	while (lastprobe->next)
261310127SLipeng.Sang@Sun.COM 		lastprobe = lastprobe->next;
261410127SLipeng.Sang@Sun.COM 	lastprobe->next = probe;
26150Sstevel@tonic-gate }
26160Sstevel@tonic-gate 
26170Sstevel@tonic-gate /*ARGSUSED*/
26180Sstevel@tonic-gate void
26190Sstevel@tonic-gate impl_bus_delete_probe(void (*func)(int))
26200Sstevel@tonic-gate {
26210Sstevel@tonic-gate 	struct bus_probe *prev = NULL;
26220Sstevel@tonic-gate 	struct bus_probe *probe = bus_probes;
26230Sstevel@tonic-gate 
26240Sstevel@tonic-gate 	while (probe) {
26250Sstevel@tonic-gate 		if (probe->probe == func)
26260Sstevel@tonic-gate 			break;
26270Sstevel@tonic-gate 		prev = probe;
26280Sstevel@tonic-gate 		probe = probe->next;
26290Sstevel@tonic-gate 	}
26300Sstevel@tonic-gate 
26310Sstevel@tonic-gate 	if (probe == NULL)
26320Sstevel@tonic-gate 		return;
26330Sstevel@tonic-gate 
26340Sstevel@tonic-gate 	if (prev)
26350Sstevel@tonic-gate 		prev->next = probe->next;
26360Sstevel@tonic-gate 	else
26370Sstevel@tonic-gate 		bus_probes = probe->next;
26380Sstevel@tonic-gate 
26390Sstevel@tonic-gate 	kmem_free(probe, sizeof (struct bus_probe));
26400Sstevel@tonic-gate }
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate /*
26430Sstevel@tonic-gate  * impl_bus_initialprobe
26440Sstevel@tonic-gate  *	Modload the prom simulator, then let it probe to verify existence
26450Sstevel@tonic-gate  *	and type of PCI support.
26460Sstevel@tonic-gate  */
26470Sstevel@tonic-gate static void
26480Sstevel@tonic-gate impl_bus_initialprobe(void)
26490Sstevel@tonic-gate {
26500Sstevel@tonic-gate 	struct bus_probe *probe;
26510Sstevel@tonic-gate 
26520Sstevel@tonic-gate 	/* load modules to install bus probes */
26535084Sjohnlev #if defined(__xpv)
26545084Sjohnlev 	if (DOMAIN_IS_INITDOMAIN(xen_info)) {
26555084Sjohnlev 		if (modload("misc", "pci_autoconfig") < 0) {
26565084Sjohnlev 			panic("failed to load misc/pci_autoconfig");
26575084Sjohnlev 		}
265810127SLipeng.Sang@Sun.COM 
265910127SLipeng.Sang@Sun.COM 		if (modload("drv", "isa") < 0)
266010127SLipeng.Sang@Sun.COM 			panic("failed to load drv/isa");
26615084Sjohnlev 	}
266210127SLipeng.Sang@Sun.COM 
26635084Sjohnlev 	(void) modload("misc", "xpv_autoconfig");
26645084Sjohnlev #else
2665*10394SMichael.Corcoran@Sun.COM 	(void) modload("misc", "acpidev");
2666*10394SMichael.Corcoran@Sun.COM 
26670Sstevel@tonic-gate 	if (modload("misc", "pci_autoconfig") < 0) {
26685084Sjohnlev 		panic("failed to load misc/pci_autoconfig");
26690Sstevel@tonic-gate 	}
267010127SLipeng.Sang@Sun.COM 
267110127SLipeng.Sang@Sun.COM 	if (modload("drv", "isa") < 0)
267210127SLipeng.Sang@Sun.COM 		panic("failed to load drv/isa");
26735084Sjohnlev #endif
26740Sstevel@tonic-gate 
26750Sstevel@tonic-gate 	probe = bus_probes;
26760Sstevel@tonic-gate 	while (probe) {
26775084Sjohnlev 		/* run the probe functions */
26780Sstevel@tonic-gate 		(*probe->probe)(0);
26790Sstevel@tonic-gate 		probe = probe->next;
26800Sstevel@tonic-gate 	}
26810Sstevel@tonic-gate }
26820Sstevel@tonic-gate 
26830Sstevel@tonic-gate /*
26840Sstevel@tonic-gate  * impl_bus_reprobe
26850Sstevel@tonic-gate  *	Reprogram devices not set up by firmware.
26860Sstevel@tonic-gate  */
26870Sstevel@tonic-gate static void
26880Sstevel@tonic-gate impl_bus_reprobe(void)
26890Sstevel@tonic-gate {
26900Sstevel@tonic-gate 	struct bus_probe *probe;
26910Sstevel@tonic-gate 
26920Sstevel@tonic-gate 	probe = bus_probes;
26930Sstevel@tonic-gate 	while (probe) {
26940Sstevel@tonic-gate 		/* run the probe function */
26950Sstevel@tonic-gate 		(*probe->probe)(1);
26960Sstevel@tonic-gate 		probe = probe->next;
26970Sstevel@tonic-gate 	}
26980Sstevel@tonic-gate }
26991865Sdilpreet 
27001865Sdilpreet 
27011865Sdilpreet /*
27021865Sdilpreet  * The following functions ready a cautious request to go up to the nexus
27031865Sdilpreet  * driver.  It is up to the nexus driver to decide how to process the request.
27041865Sdilpreet  * It may choose to call i_ddi_do_caut_get/put in this file, or do it
27051865Sdilpreet  * differently.
27061865Sdilpreet  */
27071865Sdilpreet 
27081865Sdilpreet static void
27091865Sdilpreet i_ddi_caut_getput_ctlops(ddi_acc_impl_t *hp, uint64_t host_addr,
27101865Sdilpreet     uint64_t dev_addr, size_t size, size_t repcount, uint_t flags,
27111865Sdilpreet     ddi_ctl_enum_t cmd)
27121865Sdilpreet {
27131865Sdilpreet 	peekpoke_ctlops_t	cautacc_ctlops_arg;
27141865Sdilpreet 
27151865Sdilpreet 	cautacc_ctlops_arg.size = size;
27161865Sdilpreet 	cautacc_ctlops_arg.dev_addr = dev_addr;
27171865Sdilpreet 	cautacc_ctlops_arg.host_addr = host_addr;
27181865Sdilpreet 	cautacc_ctlops_arg.handle = (ddi_acc_handle_t)hp;
27191865Sdilpreet 	cautacc_ctlops_arg.repcount = repcount;
27201865Sdilpreet 	cautacc_ctlops_arg.flags = flags;
27211865Sdilpreet 
27221865Sdilpreet 	(void) ddi_ctlops(hp->ahi_common.ah_dip, hp->ahi_common.ah_dip, cmd,
27231865Sdilpreet 	    &cautacc_ctlops_arg, NULL);
27241865Sdilpreet }
27251865Sdilpreet 
27261865Sdilpreet uint8_t
27271865Sdilpreet i_ddi_caut_get8(ddi_acc_impl_t *hp, uint8_t *addr)
27281865Sdilpreet {
27291865Sdilpreet 	uint8_t value;
27301865Sdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
27311865Sdilpreet 	    sizeof (uint8_t), 1, 0, DDI_CTLOPS_PEEK);
27321865Sdilpreet 
27331865Sdilpreet 	return (value);
27341865Sdilpreet }
27351865Sdilpreet 
27361865Sdilpreet uint16_t
27371865Sdilpreet i_ddi_caut_get16(ddi_acc_impl_t *hp, uint16_t *addr)
27381865Sdilpreet {
27391865Sdilpreet 	uint16_t value;
27401865Sdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
27411865Sdilpreet 	    sizeof (uint16_t), 1, 0, DDI_CTLOPS_PEEK);
27421865Sdilpreet 
27431865Sdilpreet 	return (value);
27441865Sdilpreet }
27451865Sdilpreet 
27461865Sdilpreet uint32_t
27471865Sdilpreet i_ddi_caut_get32(ddi_acc_impl_t *hp, uint32_t *addr)
27481865Sdilpreet {
27491865Sdilpreet 	uint32_t value;
27501865Sdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
27511865Sdilpreet 	    sizeof (uint32_t), 1, 0, DDI_CTLOPS_PEEK);
27521865Sdilpreet 
27531865Sdilpreet 	return (value);
27541865Sdilpreet }
27551865Sdilpreet 
27561865Sdilpreet uint64_t
27571865Sdilpreet i_ddi_caut_get64(ddi_acc_impl_t *hp, uint64_t *addr)
27581865Sdilpreet {
27591865Sdilpreet 	uint64_t value;
27601865Sdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
27611865Sdilpreet 	    sizeof (uint64_t), 1, 0, DDI_CTLOPS_PEEK);
27621865Sdilpreet 
27631865Sdilpreet 	return (value);
27641865Sdilpreet }
27651865Sdilpreet 
27661865Sdilpreet void
27671865Sdilpreet i_ddi_caut_put8(ddi_acc_impl_t *hp, uint8_t *addr, uint8_t value)
27681865Sdilpreet {
27691865Sdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
27701865Sdilpreet 	    sizeof (uint8_t), 1, 0, DDI_CTLOPS_POKE);
27711865Sdilpreet }
27721865Sdilpreet 
27731865Sdilpreet void
27741865Sdilpreet i_ddi_caut_put16(ddi_acc_impl_t *hp, uint16_t *addr, uint16_t value)
27751865Sdilpreet {
27761865Sdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
27771865Sdilpreet 	    sizeof (uint16_t), 1, 0, DDI_CTLOPS_POKE);
27781865Sdilpreet }
27791865Sdilpreet 
27801865Sdilpreet void
27811865Sdilpreet i_ddi_caut_put32(ddi_acc_impl_t *hp, uint32_t *addr, uint32_t value)
27821865Sdilpreet {
27831865Sdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
27841865Sdilpreet 	    sizeof (uint32_t), 1, 0, DDI_CTLOPS_POKE);
27851865Sdilpreet }
27861865Sdilpreet 
27871865Sdilpreet void
27881865Sdilpreet i_ddi_caut_put64(ddi_acc_impl_t *hp, uint64_t *addr, uint64_t value)
27891865Sdilpreet {
27901865Sdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)&value, (uintptr_t)addr,
27911865Sdilpreet 	    sizeof (uint64_t), 1, 0, DDI_CTLOPS_POKE);
27921865Sdilpreet }
27931865Sdilpreet 
27941865Sdilpreet void
27951865Sdilpreet i_ddi_caut_rep_get8(ddi_acc_impl_t *hp, uint8_t *host_addr, uint8_t *dev_addr,
27961865Sdilpreet 	size_t repcount, uint_t flags)
27971865Sdilpreet {
27981865Sdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
27991865Sdilpreet 	    sizeof (uint8_t), repcount, flags, DDI_CTLOPS_PEEK);
28001865Sdilpreet }
28011865Sdilpreet 
28021865Sdilpreet void
28031865Sdilpreet i_ddi_caut_rep_get16(ddi_acc_impl_t *hp, uint16_t *host_addr,
28041865Sdilpreet     uint16_t *dev_addr, size_t repcount, uint_t flags)
28051865Sdilpreet {
28061865Sdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
28071865Sdilpreet 	    sizeof (uint16_t), repcount, flags, DDI_CTLOPS_PEEK);
28081865Sdilpreet }
28091865Sdilpreet 
28101865Sdilpreet void
28111865Sdilpreet i_ddi_caut_rep_get32(ddi_acc_impl_t *hp, uint32_t *host_addr,
28121865Sdilpreet     uint32_t *dev_addr, size_t repcount, uint_t flags)
28131865Sdilpreet {
28141865Sdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
28151865Sdilpreet 	    sizeof (uint32_t), repcount, flags, DDI_CTLOPS_PEEK);
28161865Sdilpreet }
28171865Sdilpreet 
28181865Sdilpreet void
28191865Sdilpreet i_ddi_caut_rep_get64(ddi_acc_impl_t *hp, uint64_t *host_addr,
28201865Sdilpreet     uint64_t *dev_addr, size_t repcount, uint_t flags)
28211865Sdilpreet {
28221865Sdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
28231865Sdilpreet 	    sizeof (uint64_t), repcount, flags, DDI_CTLOPS_PEEK);
28241865Sdilpreet }
28251865Sdilpreet 
28261865Sdilpreet void
28271865Sdilpreet i_ddi_caut_rep_put8(ddi_acc_impl_t *hp, uint8_t *host_addr, uint8_t *dev_addr,
28281865Sdilpreet 	size_t repcount, uint_t flags)
28291865Sdilpreet {
28301865Sdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
28311865Sdilpreet 	    sizeof (uint8_t), repcount, flags, DDI_CTLOPS_POKE);
28321865Sdilpreet }
28331865Sdilpreet 
28341865Sdilpreet void
28351865Sdilpreet i_ddi_caut_rep_put16(ddi_acc_impl_t *hp, uint16_t *host_addr,
28361865Sdilpreet     uint16_t *dev_addr, size_t repcount, uint_t flags)
28371865Sdilpreet {
28381865Sdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
28391865Sdilpreet 	    sizeof (uint16_t), repcount, flags, DDI_CTLOPS_POKE);
28401865Sdilpreet }
28411865Sdilpreet 
28421865Sdilpreet void
28431865Sdilpreet i_ddi_caut_rep_put32(ddi_acc_impl_t *hp, uint32_t *host_addr,
28441865Sdilpreet     uint32_t *dev_addr, size_t repcount, uint_t flags)
28451865Sdilpreet {
28461865Sdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
28471865Sdilpreet 	    sizeof (uint32_t), repcount, flags, DDI_CTLOPS_POKE);
28481865Sdilpreet }
28491865Sdilpreet 
28501865Sdilpreet void
28511865Sdilpreet i_ddi_caut_rep_put64(ddi_acc_impl_t *hp, uint64_t *host_addr,
28521865Sdilpreet     uint64_t *dev_addr, size_t repcount, uint_t flags)
28531865Sdilpreet {
28541865Sdilpreet 	i_ddi_caut_getput_ctlops(hp, (uintptr_t)host_addr, (uintptr_t)dev_addr,
28551865Sdilpreet 	    sizeof (uint64_t), repcount, flags, DDI_CTLOPS_POKE);
28561865Sdilpreet }
28575251Smrj 
28585251Smrj boolean_t
28595251Smrj i_ddi_copybuf_required(ddi_dma_attr_t *attrp)
28605251Smrj {
28615251Smrj 	uint64_t hi_pa;
28625251Smrj 
28635251Smrj 	hi_pa = ((uint64_t)physmax + 1ull) << PAGESHIFT;
28645251Smrj 	if (attrp->dma_attr_addr_hi < hi_pa) {
28655251Smrj 		return (B_TRUE);
28665251Smrj 	}
28675251Smrj 
28685251Smrj 	return (B_FALSE);
28695251Smrj }
28705251Smrj 
28715251Smrj size_t
28725251Smrj i_ddi_copybuf_size()
28735251Smrj {
28745251Smrj 	return (dma_max_copybuf_size);
28755251Smrj }
28765251Smrj 
28775251Smrj /*
28785251Smrj  * i_ddi_dma_max()
28795251Smrj  *    returns the maximum DMA size which can be performed in a single DMA
28805251Smrj  *    window taking into account the devices DMA contraints (attrp), the
28815251Smrj  *    maximum copy buffer size (if applicable), and the worse case buffer
28825251Smrj  *    fragmentation.
28835251Smrj  */
28845251Smrj /*ARGSUSED*/
28855251Smrj uint32_t
28865251Smrj i_ddi_dma_max(dev_info_t *dip, ddi_dma_attr_t *attrp)
28875251Smrj {
28885251Smrj 	uint64_t maxxfer;
28895251Smrj 
28905251Smrj 
28915251Smrj 	/*
28925251Smrj 	 * take the min of maxxfer and the the worse case fragementation
28935251Smrj 	 * (e.g. every cookie <= 1 page)
28945251Smrj 	 */
28955251Smrj 	maxxfer = MIN(attrp->dma_attr_maxxfer,
28965251Smrj 	    ((uint64_t)(attrp->dma_attr_sgllen - 1) << PAGESHIFT));
28975251Smrj 
28985251Smrj 	/*
28995251Smrj 	 * If the DMA engine can't reach all off memory, we also need to take
29005251Smrj 	 * the max size of the copybuf into consideration.
29015251Smrj 	 */
29025251Smrj 	if (i_ddi_copybuf_required(attrp)) {
29035251Smrj 		maxxfer = MIN(i_ddi_copybuf_size(), maxxfer);
29045251Smrj 	}
29055251Smrj 
29065251Smrj 	/*
29075251Smrj 	 * we only return a 32-bit value. Make sure it's not -1. Round to a
29085251Smrj 	 * page so it won't be mistaken for an error value during debug.
29095251Smrj 	 */
29105251Smrj 	if (maxxfer >= 0xFFFFFFFF) {
29115251Smrj 		maxxfer = 0xFFFFF000;
29125251Smrj 	}
29135251Smrj 
29145251Smrj 	/*
29155251Smrj 	 * make sure the value we return is a whole multiple of the
29165251Smrj 	 * granlarity.
29175251Smrj 	 */
29185251Smrj 	if (attrp->dma_attr_granular > 1) {
29195251Smrj 		maxxfer = maxxfer - (maxxfer % attrp->dma_attr_granular);
29205251Smrj 	}
29215251Smrj 
29225251Smrj 	return ((uint32_t)maxxfer);
29235251Smrj }
2924