xref: /titanic_52/usr/src/uts/common/io/busra.c (revision 39b361b2ebefcef5612a54ae5cbd2179e19be296)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ae115bc7Smrj  * Common Development and Distribution License (the "License").
6ae115bc7Smrj  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*39b361b2SRichard Bean  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #if defined(DEBUG)
277c478bd9Sstevel@tonic-gate #define	BUSRA_DEBUG
287c478bd9Sstevel@tonic-gate #endif
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * This module provides a set of resource management interfaces
327c478bd9Sstevel@tonic-gate  * to manage bus resources globally in the system.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * The bus nexus drivers are typically responsible to setup resource
357c478bd9Sstevel@tonic-gate  * maps for the bus resources available for a bus instance. However
367c478bd9Sstevel@tonic-gate  * this module also provides resource setup functions for PCI bus
377c478bd9Sstevel@tonic-gate  * (used by both SPARC and X86 platforms) and ISA bus instances (used
387c478bd9Sstevel@tonic-gate  * only for X86 platforms).
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <sys/systm.h>
437c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
447c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
457c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
467c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
477c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
487c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
497c478bd9Sstevel@tonic-gate #include <sys/pctypes.h>
507c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
517c478bd9Sstevel@tonic-gate #include <sys/debug.h>
527c478bd9Sstevel@tonic-gate #include <sys/spl.h>
537c478bd9Sstevel@tonic-gate #include <sys/pci.h>
547c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #if defined(BUSRA_DEBUG)
577c478bd9Sstevel@tonic-gate int busra_debug = 0;
587c478bd9Sstevel@tonic-gate #define	DEBUGPRT \
597c478bd9Sstevel@tonic-gate 	if (busra_debug) cmn_err
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #else
627c478bd9Sstevel@tonic-gate #define	DEBUGPRT \
637c478bd9Sstevel@tonic-gate 	if (0) cmn_err
647c478bd9Sstevel@tonic-gate #endif
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * global mutex that protects the global list of resource maps.
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate kmutex_t ra_lock;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * basic resource element
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate struct ra_resource {
767c478bd9Sstevel@tonic-gate 	struct ra_resource *ra_next;
777c478bd9Sstevel@tonic-gate 	uint64_t	ra_base;
787c478bd9Sstevel@tonic-gate 	uint64_t 	ra_len;
797c478bd9Sstevel@tonic-gate };
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * link list element for the list of dips (and their resource ranges)
837c478bd9Sstevel@tonic-gate  * for a particular resource type.
847c478bd9Sstevel@tonic-gate  * ra_rangeset points to the list of resources available
857c478bd9Sstevel@tonic-gate  * for this type and this dip.
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate struct ra_dip_type  {
887c478bd9Sstevel@tonic-gate 	struct ra_dip_type *ra_next;
897c478bd9Sstevel@tonic-gate 	struct ra_resource  *ra_rangeset;
907c478bd9Sstevel@tonic-gate 	dev_info_t *ra_dip;
917c478bd9Sstevel@tonic-gate };
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * link list element for list of types resources. Each element
967c478bd9Sstevel@tonic-gate  * has all resources for a particular type.
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate struct ra_type_map {
997c478bd9Sstevel@tonic-gate 	struct ra_type_map *ra_next;
1007c478bd9Sstevel@tonic-gate 	struct ra_dip_type *ra_dip_list;
1017c478bd9Sstevel@tonic-gate 	char *type;
1027c478bd9Sstevel@tonic-gate };
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * place holder to keep the head of the whole global list.
1077c478bd9Sstevel@tonic-gate  * the address of the first typemap would be stored in it.
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate static struct ra_type_map	*ra_map_list_head = NULL;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate  * This is the loadable module wrapper.
1147c478bd9Sstevel@tonic-gate  * It is essentially boilerplate so isn't documented
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate extern struct mod_ops mod_miscops;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate #ifdef BUSRA_DEBUG
1197c478bd9Sstevel@tonic-gate void ra_dump_all();
1207c478bd9Sstevel@tonic-gate #endif
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /* internal function prototypes */
1237c478bd9Sstevel@tonic-gate static struct ra_dip_type *find_dip_map_resources(dev_info_t *dip, char *type,
1247c478bd9Sstevel@tonic-gate     struct ra_dip_type ***backdip, struct ra_type_map ***backtype,
1257c478bd9Sstevel@tonic-gate     uint32_t flag);
1267c478bd9Sstevel@tonic-gate static int isnot_pow2(uint64_t value);
1277c478bd9Sstevel@tonic-gate static int claim_pci_busnum(dev_info_t *dip, void *arg);
1287c478bd9Sstevel@tonic-gate static int ra_map_exist(dev_info_t *dip, char *type);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate #define	RA_INSERT(prev, el) \
1327c478bd9Sstevel@tonic-gate 	el->ra_next = *prev; \
1337c478bd9Sstevel@tonic-gate 	*prev = el;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate #define	RA_REMOVE(prev, el) \
1367c478bd9Sstevel@tonic-gate 	*prev = el->ra_next;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate static struct modlmisc modlmisc = {
1407c478bd9Sstevel@tonic-gate 	&mod_miscops,		/* Type of module. This one is a module */
141*39b361b2SRichard Bean 	"Bus Resource Allocator (BUSRA)",	/* Name of the module. */
1427c478bd9Sstevel@tonic-gate };
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1457c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modlmisc, NULL
1467c478bd9Sstevel@tonic-gate };
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate int
1497c478bd9Sstevel@tonic-gate _init()
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	int	ret;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	mutex_init(&ra_lock, NULL, MUTEX_DRIVER,
1547c478bd9Sstevel@tonic-gate 		(void *)(intptr_t)__ipltospl(SPL7 - 1));
1557c478bd9Sstevel@tonic-gate 	if ((ret = mod_install(&modlinkage)) != 0) {
1567c478bd9Sstevel@tonic-gate 		mutex_destroy(&ra_lock);
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 	return (ret);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate int
1627c478bd9Sstevel@tonic-gate _fini()
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	int	ret;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	mutex_enter(&ra_lock);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	if (ra_map_list_head != NULL) {
1697c478bd9Sstevel@tonic-gate 		mutex_exit(&ra_lock);
1707c478bd9Sstevel@tonic-gate 		return (EBUSY);
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	ret = mod_remove(&modlinkage);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	mutex_exit(&ra_lock);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if (ret == 0)
1787c478bd9Sstevel@tonic-gate 		mutex_destroy(&ra_lock);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	return (ret);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate int
1847c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate  * set up an empty resource map for a given type and dip
1927c478bd9Sstevel@tonic-gate  */
1937c478bd9Sstevel@tonic-gate int
1947c478bd9Sstevel@tonic-gate ndi_ra_map_setup(dev_info_t *dip, char *type)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	struct ra_type_map  *typemapp;
1977c478bd9Sstevel@tonic-gate 	struct ra_dip_type  *dipmap;
1987c478bd9Sstevel@tonic-gate 	struct ra_dip_type  **backdip;
1997c478bd9Sstevel@tonic-gate 	struct ra_type_map  **backtype;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	mutex_enter(&ra_lock);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	dipmap = find_dip_map_resources(dip, type, &backdip, &backtype, 0);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	if (dipmap == NULL) {
2077c478bd9Sstevel@tonic-gate 		if (backtype == NULL) {
2087c478bd9Sstevel@tonic-gate 			typemapp = (struct ra_type_map *)
2097c478bd9Sstevel@tonic-gate 			kmem_zalloc(sizeof (*typemapp), KM_SLEEP);
2107c478bd9Sstevel@tonic-gate 			typemapp->type = (char *)kmem_zalloc(strlen(type) + 1,
2117c478bd9Sstevel@tonic-gate 				KM_SLEEP);
2127c478bd9Sstevel@tonic-gate 			(void) strcpy(typemapp->type, type);
2137c478bd9Sstevel@tonic-gate 			RA_INSERT(&ra_map_list_head, typemapp);
2147c478bd9Sstevel@tonic-gate 		} else {
2157c478bd9Sstevel@tonic-gate 			typemapp = *backtype;
2167c478bd9Sstevel@tonic-gate 		}
2177c478bd9Sstevel@tonic-gate 		if (backdip == NULL) {
2187c478bd9Sstevel@tonic-gate 			/* allocate and insert in list of dips for this type */
2197c478bd9Sstevel@tonic-gate 			dipmap = (struct ra_dip_type *)
2207c478bd9Sstevel@tonic-gate 			kmem_zalloc(sizeof (*dipmap), KM_SLEEP);
2217c478bd9Sstevel@tonic-gate 			dipmap->ra_dip = dip;
2227c478bd9Sstevel@tonic-gate 			RA_INSERT(&typemapp->ra_dip_list, dipmap);
2237c478bd9Sstevel@tonic-gate 		}
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	mutex_exit(&ra_lock);
2277c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate /*
2317c478bd9Sstevel@tonic-gate  * destroys a resource map for a given dip and type
2327c478bd9Sstevel@tonic-gate  */
2337c478bd9Sstevel@tonic-gate int
2347c478bd9Sstevel@tonic-gate ndi_ra_map_destroy(dev_info_t *dip, char *type)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	struct ra_dip_type	*dipmap;
2377c478bd9Sstevel@tonic-gate 	struct ra_dip_type	**backdip;
2387c478bd9Sstevel@tonic-gate 	struct ra_type_map  	**backtype, *typemap;
2397c478bd9Sstevel@tonic-gate 	struct ra_resource	*range;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	mutex_enter(&ra_lock);
2427c478bd9Sstevel@tonic-gate 	dipmap = find_dip_map_resources(dip, type, &backdip, &backtype, 0);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	if (dipmap == NULL) {
2457c478bd9Sstevel@tonic-gate 		mutex_exit(&ra_lock);
2467c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	/*
2507c478bd9Sstevel@tonic-gate 	 * destroy all resources for this dip
2517c478bd9Sstevel@tonic-gate 	 * remove dip from type list
2527c478bd9Sstevel@tonic-gate 	 */
2537c478bd9Sstevel@tonic-gate 	ASSERT((backdip != NULL) && (backtype != NULL));
2547c478bd9Sstevel@tonic-gate 	while (dipmap->ra_rangeset != NULL) {
2557c478bd9Sstevel@tonic-gate 		range = dipmap->ra_rangeset;
2567c478bd9Sstevel@tonic-gate 		RA_REMOVE(&dipmap->ra_rangeset, range);
2577c478bd9Sstevel@tonic-gate 		kmem_free((caddr_t)range, sizeof (*range));
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 	/* remove from dip list */
2607c478bd9Sstevel@tonic-gate 	RA_REMOVE(backdip, dipmap);
2617c478bd9Sstevel@tonic-gate 	kmem_free((caddr_t)dipmap, sizeof (*dipmap));
2627c478bd9Sstevel@tonic-gate 	if ((*backtype)->ra_dip_list == NULL) {
2637c478bd9Sstevel@tonic-gate 		/*
2647c478bd9Sstevel@tonic-gate 		 * This was the last dip with this resource type.
2657c478bd9Sstevel@tonic-gate 		 * Remove the type from the global list.
2667c478bd9Sstevel@tonic-gate 		 */
2677c478bd9Sstevel@tonic-gate 		typemap = *backtype;
2687c478bd9Sstevel@tonic-gate 		RA_REMOVE(backtype, (*backtype));
2697c478bd9Sstevel@tonic-gate 		kmem_free((caddr_t)typemap->type, strlen(typemap->type) + 1);
2707c478bd9Sstevel@tonic-gate 		kmem_free((caddr_t)typemap, sizeof (*typemap));
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	mutex_exit(&ra_lock);
2747c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate static int
2787c478bd9Sstevel@tonic-gate ra_map_exist(dev_info_t *dip, char *type)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate 	struct ra_dip_type  **backdip;
2817c478bd9Sstevel@tonic-gate 	struct ra_type_map  **backtype;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	mutex_enter(&ra_lock);
2847c478bd9Sstevel@tonic-gate 	if (find_dip_map_resources(dip, type, &backdip, &backtype, 0) == NULL) {
2857c478bd9Sstevel@tonic-gate 		mutex_exit(&ra_lock);
2867c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	mutex_exit(&ra_lock);
2907c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate /*
2937c478bd9Sstevel@tonic-gate  * Find a dip map for the specified type, if NDI_RA_PASS will go up on dev tree
2947c478bd9Sstevel@tonic-gate  * if found, backdip and backtype will be updated to point to the previous
2957c478bd9Sstevel@tonic-gate  * dip in the list and previous type for this dip in the list.
2967c478bd9Sstevel@tonic-gate  * If no such type at all in the resource list both backdip and backtype
2977c478bd9Sstevel@tonic-gate  * will be null. If the type found but no dip, back dip will be null.
2987c478bd9Sstevel@tonic-gate  */
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate static struct ra_dip_type *
3017c478bd9Sstevel@tonic-gate find_dip_map_resources(dev_info_t *dip, char *type,
3027c478bd9Sstevel@tonic-gate     struct ra_dip_type ***backdip, struct ra_type_map ***backtype,
3037c478bd9Sstevel@tonic-gate     uint32_t flag)
3047c478bd9Sstevel@tonic-gate {
3057c478bd9Sstevel@tonic-gate 	struct ra_type_map **prevmap;
3067c478bd9Sstevel@tonic-gate 	struct ra_dip_type *dipmap, **prevdip;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ra_lock));
3097c478bd9Sstevel@tonic-gate 	prevdip = NULL;
3107c478bd9Sstevel@tonic-gate 	dipmap = NULL;
3117c478bd9Sstevel@tonic-gate 	prevmap = &ra_map_list_head;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	while (*prevmap) {
3147c478bd9Sstevel@tonic-gate 		if (strcmp((*prevmap)->type, type) == 0)
3157c478bd9Sstevel@tonic-gate 			break;
3167c478bd9Sstevel@tonic-gate 		prevmap = &(*prevmap)->ra_next;
3177c478bd9Sstevel@tonic-gate 	}
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	if (*prevmap) {
3207c478bd9Sstevel@tonic-gate 		for (; dip != NULL; dip = ddi_get_parent(dip)) {
3217c478bd9Sstevel@tonic-gate 			prevdip = &(*prevmap)->ra_dip_list;
3227c478bd9Sstevel@tonic-gate 			dipmap = *prevdip;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 			while (dipmap) {
3257c478bd9Sstevel@tonic-gate 				if (dipmap->ra_dip == dip)
3267c478bd9Sstevel@tonic-gate 					break;
3277c478bd9Sstevel@tonic-gate 				prevdip =  &dipmap->ra_next;
3287c478bd9Sstevel@tonic-gate 				dipmap = dipmap->ra_next;
3297c478bd9Sstevel@tonic-gate 			}
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 			if (dipmap != NULL) {
3327c478bd9Sstevel@tonic-gate 				/* found it */
3337c478bd9Sstevel@tonic-gate 				break;
3347c478bd9Sstevel@tonic-gate 			}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 			if (!(flag & NDI_RA_PASS)) {
3377c478bd9Sstevel@tonic-gate 				break;
3387c478bd9Sstevel@tonic-gate 			}
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	*backtype = (*prevmap == NULL) ?  NULL: prevmap;
3437c478bd9Sstevel@tonic-gate 	*backdip = (dipmap == NULL) ?  NULL: prevdip;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	return (dipmap);
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate int
3497c478bd9Sstevel@tonic-gate ndi_ra_free(dev_info_t *dip, uint64_t base, uint64_t len, char *type,
3507c478bd9Sstevel@tonic-gate     uint32_t flag)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	struct ra_dip_type *dipmap;
3537c478bd9Sstevel@tonic-gate 	struct ra_resource *newmap, *overlapmap, *oldmap = NULL;
3547c478bd9Sstevel@tonic-gate 	struct ra_resource  *mapp, **backp;
3557c478bd9Sstevel@tonic-gate 	uint64_t newend, mapend;
3567c478bd9Sstevel@tonic-gate 	struct ra_dip_type **backdip;
3577c478bd9Sstevel@tonic-gate 	struct ra_type_map **backtype;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	if (len == 0) {
3607c478bd9Sstevel@tonic-gate 		return (NDI_SUCCESS);
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	mutex_enter(&ra_lock);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	if ((dipmap = find_dip_map_resources(dip, type, &backdip, &backtype,
3667c478bd9Sstevel@tonic-gate 	    flag)) == NULL) {
3677c478bd9Sstevel@tonic-gate 		mutex_exit(&ra_lock);
3687c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	mapp = dipmap->ra_rangeset;
3727c478bd9Sstevel@tonic-gate 	backp = &dipmap->ra_rangeset;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	/* now find where range lies and fix things up */
3757c478bd9Sstevel@tonic-gate 	newend = base + len;
3767c478bd9Sstevel@tonic-gate 	for (; mapp != NULL; backp = &(mapp->ra_next), mapp = mapp->ra_next) {
3777c478bd9Sstevel@tonic-gate 		mapend = mapp->ra_base + mapp->ra_len;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 		/* check for overlap first */
3807c478bd9Sstevel@tonic-gate 		if ((base <= mapp->ra_base && newend > mapp->ra_base) ||
3817c478bd9Sstevel@tonic-gate 		    (base > mapp->ra_base && base < mapend)) {
3827c478bd9Sstevel@tonic-gate 			/* overlap with mapp */
3837c478bd9Sstevel@tonic-gate 			overlapmap = mapp;
3847c478bd9Sstevel@tonic-gate 			goto overlap;
3857c478bd9Sstevel@tonic-gate 		} else if ((base == mapend && mapp->ra_next) &&
3867c478bd9Sstevel@tonic-gate 		    (newend > mapp->ra_next->ra_base)) {
3877c478bd9Sstevel@tonic-gate 			/* overlap with mapp->ra_next */
3887c478bd9Sstevel@tonic-gate 			overlapmap = mapp->ra_next;
3897c478bd9Sstevel@tonic-gate 			goto overlap;
3907c478bd9Sstevel@tonic-gate 		}
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 		if (newend == mapp->ra_base) {
3937c478bd9Sstevel@tonic-gate 			/* simple - on front */
3947c478bd9Sstevel@tonic-gate 			mapp->ra_base = base;
3957c478bd9Sstevel@tonic-gate 			mapp->ra_len += len;
3967c478bd9Sstevel@tonic-gate 			/*
3977c478bd9Sstevel@tonic-gate 			 * don't need to check if it merges with
3987c478bd9Sstevel@tonic-gate 			 * previous since that would match on on end
3997c478bd9Sstevel@tonic-gate 			 */
4007c478bd9Sstevel@tonic-gate 			break;
4017c478bd9Sstevel@tonic-gate 		} else if (base == mapend) {
4027c478bd9Sstevel@tonic-gate 			/* simple - on end */
4037c478bd9Sstevel@tonic-gate 			mapp->ra_len += len;
4047c478bd9Sstevel@tonic-gate 			if (mapp->ra_next &&
4057c478bd9Sstevel@tonic-gate 			    (newend == mapp->ra_next->ra_base)) {
4067c478bd9Sstevel@tonic-gate 				/* merge with next node */
4077c478bd9Sstevel@tonic-gate 				oldmap = mapp->ra_next;
4087c478bd9Sstevel@tonic-gate 				mapp->ra_len += oldmap->ra_len;
4097c478bd9Sstevel@tonic-gate 				RA_REMOVE(&mapp->ra_next, oldmap);
4107c478bd9Sstevel@tonic-gate 				kmem_free((caddr_t)oldmap, sizeof (*oldmap));
4117c478bd9Sstevel@tonic-gate 			}
4127c478bd9Sstevel@tonic-gate 			break;
4137c478bd9Sstevel@tonic-gate 		} else if (base < mapp->ra_base) {
4147c478bd9Sstevel@tonic-gate 			/* somewhere in between so just an insert */
4157c478bd9Sstevel@tonic-gate 			newmap = (struct ra_resource *)
4167c478bd9Sstevel@tonic-gate 				kmem_zalloc(sizeof (*newmap), KM_SLEEP);
4177c478bd9Sstevel@tonic-gate 			newmap->ra_base = base;
4187c478bd9Sstevel@tonic-gate 			newmap->ra_len = len;
4197c478bd9Sstevel@tonic-gate 			RA_INSERT(backp, newmap);
4207c478bd9Sstevel@tonic-gate 			break;
4217c478bd9Sstevel@tonic-gate 		}
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 	if (mapp == NULL) {
4247c478bd9Sstevel@tonic-gate 		/* stick on end */
4257c478bd9Sstevel@tonic-gate 		newmap = (struct ra_resource *)
4267c478bd9Sstevel@tonic-gate 				kmem_zalloc(sizeof (*newmap), KM_SLEEP);
4277c478bd9Sstevel@tonic-gate 		newmap->ra_base = base;
4287c478bd9Sstevel@tonic-gate 		newmap->ra_len = len;
4297c478bd9Sstevel@tonic-gate 		RA_INSERT(backp, newmap);
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	mutex_exit(&ra_lock);
4337c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate overlap:
4367c478bd9Sstevel@tonic-gate 	/*
4377c478bd9Sstevel@tonic-gate 	 * Bad free may happen on some x86 platforms with BIOS exporting
4387c478bd9Sstevel@tonic-gate 	 * incorrect resource maps. The system is otherwise functioning
4397c478bd9Sstevel@tonic-gate 	 * normally. We send such messages to syslog only.
4407c478bd9Sstevel@tonic-gate 	 */
4417c478bd9Sstevel@tonic-gate 	cmn_err(CE_NOTE, "!ndi_ra_free: bad free, dip %p, resource type %s \n",
4427c478bd9Sstevel@tonic-gate 	    (void *)dip, type);
4437c478bd9Sstevel@tonic-gate 	cmn_err(CE_NOTE, "!ndi_ra_free: freeing base 0x%" PRIx64 ", len 0x%"
4447c478bd9Sstevel@tonic-gate 	    PRIX64 " overlaps with existing resource base 0x%" PRIx64
4457c478bd9Sstevel@tonic-gate 	    ", len 0x%" PRIx64 "\n", base, len, overlapmap->ra_base,
4467c478bd9Sstevel@tonic-gate 	    overlapmap->ra_len);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	mutex_exit(&ra_lock);
4497c478bd9Sstevel@tonic-gate 	return (NDI_FAILURE);
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate /* check to see if value is power of 2 or not. */
4537c478bd9Sstevel@tonic-gate static int
4547c478bd9Sstevel@tonic-gate isnot_pow2(uint64_t value)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	uint32_t low;
4577c478bd9Sstevel@tonic-gate 	uint32_t hi;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	low = value & 0xffffffff;
4607c478bd9Sstevel@tonic-gate 	hi = value >> 32;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	/*
4637c478bd9Sstevel@tonic-gate 	 * ddi_ffs and ddi_fls gets long values, so in 32bit environment
4647c478bd9Sstevel@tonic-gate 	 * won't work correctly for 64bit values
4657c478bd9Sstevel@tonic-gate 	 */
4667c478bd9Sstevel@tonic-gate 	if ((ddi_ffs(low) == ddi_fls(low)) &&
4677c478bd9Sstevel@tonic-gate 	    (ddi_ffs(hi) == ddi_fls(hi)))
4687c478bd9Sstevel@tonic-gate 		return (0);
4697c478bd9Sstevel@tonic-gate 	return (1);
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate static  void
4737c478bd9Sstevel@tonic-gate adjust_link(struct ra_resource **backp, struct ra_resource *mapp,
4747c478bd9Sstevel@tonic-gate 	    uint64_t base, uint64_t len)
4757c478bd9Sstevel@tonic-gate {
4767c478bd9Sstevel@tonic-gate 	struct ra_resource *newmap;
4777c478bd9Sstevel@tonic-gate 	uint64_t newlen;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	if (base != mapp->ra_base) {
4807c478bd9Sstevel@tonic-gate 		/* in the middle or end */
4817c478bd9Sstevel@tonic-gate 		newlen = base - mapp->ra_base;
4827c478bd9Sstevel@tonic-gate 		if ((mapp->ra_len - newlen) == len) {
4837c478bd9Sstevel@tonic-gate 			/* on the end */
4847c478bd9Sstevel@tonic-gate 			mapp->ra_len = newlen;
4857c478bd9Sstevel@tonic-gate 		} else {
4867c478bd9Sstevel@tonic-gate 			/* in the middle */
4877c478bd9Sstevel@tonic-gate 			newmap = (struct ra_resource *)
4887c478bd9Sstevel@tonic-gate 					kmem_zalloc(sizeof (*newmap), KM_SLEEP);
4897c478bd9Sstevel@tonic-gate 			newmap->ra_base = base + len;
4907c478bd9Sstevel@tonic-gate 			newmap->ra_len = mapp->ra_len -
4917c478bd9Sstevel@tonic-gate 				(len + newlen);
4927c478bd9Sstevel@tonic-gate 			mapp->ra_len = newlen;
4937c478bd9Sstevel@tonic-gate 			RA_INSERT(&(mapp->ra_next), newmap);
4947c478bd9Sstevel@tonic-gate 		}
4957c478bd9Sstevel@tonic-gate 	} else {
4967c478bd9Sstevel@tonic-gate 		/* at the beginning */
4977c478bd9Sstevel@tonic-gate 		mapp->ra_base += len;
4987c478bd9Sstevel@tonic-gate 		mapp->ra_len -= len;
4997c478bd9Sstevel@tonic-gate 		if (mapp->ra_len == 0) {
5007c478bd9Sstevel@tonic-gate 			/* remove the whole node */
5017c478bd9Sstevel@tonic-gate 			RA_REMOVE(backp, mapp);
5027c478bd9Sstevel@tonic-gate 			kmem_free((caddr_t)mapp, sizeof (*mapp));
5037c478bd9Sstevel@tonic-gate 		}
5047c478bd9Sstevel@tonic-gate 	}
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate int
5087c478bd9Sstevel@tonic-gate ndi_ra_alloc(dev_info_t *dip, ndi_ra_request_t *req, uint64_t *retbasep,
5097c478bd9Sstevel@tonic-gate     uint64_t *retlenp, char *type, uint32_t flag)
5107c478bd9Sstevel@tonic-gate {
5117c478bd9Sstevel@tonic-gate 	struct ra_dip_type *dipmap;
5127c478bd9Sstevel@tonic-gate 	struct ra_resource *mapp, **backp, **backlargestp;
5137c478bd9Sstevel@tonic-gate 	uint64_t mask = 0;
5147c478bd9Sstevel@tonic-gate 	uint64_t len, remlen, largestbase, largestlen;
5157c478bd9Sstevel@tonic-gate 	uint64_t base, oldbase, lower, upper;
5167c478bd9Sstevel@tonic-gate 	struct ra_dip_type  **backdip;
5177c478bd9Sstevel@tonic-gate 	struct ra_type_map  **backtype;
5187c478bd9Sstevel@tonic-gate 	int  rval = NDI_FAILURE;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	len = req->ra_len;
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	if (req->ra_flags & NDI_RA_ALIGN_SIZE) {
5247c478bd9Sstevel@tonic-gate 		if (isnot_pow2(req->ra_len)) {
5257c478bd9Sstevel@tonic-gate 			DEBUGPRT(CE_WARN, "ndi_ra_alloc: bad length(pow2) 0x%"
5267c478bd9Sstevel@tonic-gate 				PRIx64, req->ra_len);
5277c478bd9Sstevel@tonic-gate 			*retbasep = 0;
5287c478bd9Sstevel@tonic-gate 			*retlenp = 0;
5297c478bd9Sstevel@tonic-gate 			return (NDI_FAILURE);
5307c478bd9Sstevel@tonic-gate 		}
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	mask = (req->ra_flags & NDI_RA_ALIGN_SIZE) ? (len - 1) :
5347c478bd9Sstevel@tonic-gate 	    req->ra_align_mask;
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	mutex_enter(&ra_lock);
5387c478bd9Sstevel@tonic-gate 	dipmap = find_dip_map_resources(dip, type, &backdip, &backtype, flag);
5397c478bd9Sstevel@tonic-gate 	if ((dipmap == NULL) || ((mapp = dipmap->ra_rangeset) == NULL)) {
5407c478bd9Sstevel@tonic-gate 		mutex_exit(&ra_lock);
5417c478bd9Sstevel@tonic-gate 		DEBUGPRT(CE_CONT, "ndi_ra_alloc no map found for this type\n");
5427c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	DEBUGPRT(CE_CONT, "ndi_ra_alloc: mapp = %p len=%" PRIx64 ", mask=%"
5467c478bd9Sstevel@tonic-gate 			PRIx64 "\n", (void *)mapp, len, mask);
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	backp = &(dipmap->ra_rangeset);
5497c478bd9Sstevel@tonic-gate 	backlargestp = NULL;
5507c478bd9Sstevel@tonic-gate 	largestbase = 0;
5517c478bd9Sstevel@tonic-gate 	largestlen = 0;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	lower = 0;
5547c478bd9Sstevel@tonic-gate 	upper = ~(uint64_t)0;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	if (req->ra_flags & NDI_RA_ALLOC_BOUNDED) {
5577c478bd9Sstevel@tonic-gate 		/* bounded so skip to first possible */
5587c478bd9Sstevel@tonic-gate 		lower = req->ra_boundbase;
5597c478bd9Sstevel@tonic-gate 		upper = req->ra_boundlen + lower;
5607c478bd9Sstevel@tonic-gate 		if ((upper == 0) || (upper < req->ra_boundlen))
5617c478bd9Sstevel@tonic-gate 			upper = ~(uint64_t)0;
5627c478bd9Sstevel@tonic-gate 		DEBUGPRT(CE_CONT, "ndi_ra_alloc: ra_len = %" PRIx64 ", len = %"
5637c478bd9Sstevel@tonic-gate 				PRIx64 " ra_base=%" PRIx64 ", mask=%" PRIx64
5647c478bd9Sstevel@tonic-gate 				"\n", mapp->ra_len, len, mapp->ra_base, mask);
5657c478bd9Sstevel@tonic-gate 		for (; mapp != NULL &&
5667c478bd9Sstevel@tonic-gate 			(mapp->ra_base + mapp->ra_len) < lower;
5677c478bd9Sstevel@tonic-gate 			backp = &(mapp->ra_next), mapp = mapp->ra_next) {
5687c478bd9Sstevel@tonic-gate 			if (((mapp->ra_len + mapp->ra_base) == 0) ||
5697c478bd9Sstevel@tonic-gate 			    ((mapp->ra_len + mapp->ra_base) < mapp->ra_len))
5707c478bd9Sstevel@tonic-gate 				/*
5717c478bd9Sstevel@tonic-gate 				 * This elements end goes beyond max uint64_t.
5727c478bd9Sstevel@tonic-gate 				 * potential candidate, check end against lower
5737c478bd9Sstevel@tonic-gate 				 * would not be precise.
5747c478bd9Sstevel@tonic-gate 				 */
5757c478bd9Sstevel@tonic-gate 				break;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 			DEBUGPRT(CE_CONT, " ra_len = %" PRIx64 ", ra_base=%"
5787c478bd9Sstevel@tonic-gate 			    PRIx64 "\n", mapp->ra_len, mapp->ra_base);
5797c478bd9Sstevel@tonic-gate 			}
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	if (!(req->ra_flags & NDI_RA_ALLOC_SPECIFIED)) {
5847c478bd9Sstevel@tonic-gate 		/* first fit - not user specified */
5857c478bd9Sstevel@tonic-gate 		DEBUGPRT(CE_CONT, "ndi_ra_alloc(unspecified request)"
5867c478bd9Sstevel@tonic-gate 			"lower=%" PRIx64 ", upper=%" PRIx64 "\n", lower, upper);
5877c478bd9Sstevel@tonic-gate 		for (; mapp != NULL && mapp->ra_base <= upper;
5887c478bd9Sstevel@tonic-gate 			backp = &(mapp->ra_next), mapp = mapp->ra_next) {
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 			DEBUGPRT(CE_CONT, "ndi_ra_alloc: ra_len = %" PRIx64
5917c478bd9Sstevel@tonic-gate 			    ", len = %" PRIx64 "", mapp->ra_len, len);
5927c478bd9Sstevel@tonic-gate 			base = mapp->ra_base;
5937c478bd9Sstevel@tonic-gate 			if (base < lower) {
5947c478bd9Sstevel@tonic-gate 				base = lower;
5957c478bd9Sstevel@tonic-gate 				DEBUGPRT(CE_CONT, "\tbase=%" PRIx64
5967c478bd9Sstevel@tonic-gate 				    ", ra_base=%" PRIx64 ", mask=%" PRIx64,
5977c478bd9Sstevel@tonic-gate 				    base, mapp->ra_base, mask);
5987c478bd9Sstevel@tonic-gate 			}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 			if ((base & mask) != 0) {
6017c478bd9Sstevel@tonic-gate 				oldbase = base;
6027c478bd9Sstevel@tonic-gate 				/*
6037c478bd9Sstevel@tonic-gate 				 * failed a critical constraint
6047c478bd9Sstevel@tonic-gate 				 * adjust and see if it still fits
6057c478bd9Sstevel@tonic-gate 				 */
6067c478bd9Sstevel@tonic-gate 				base = base & ~mask;
6077c478bd9Sstevel@tonic-gate 				base += (mask + 1);
6087c478bd9Sstevel@tonic-gate 				DEBUGPRT(CE_CONT, "\tnew base=%" PRIx64 "\n",
6097c478bd9Sstevel@tonic-gate 					base);
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 				/*
6127c478bd9Sstevel@tonic-gate 				 * Check to see if the new base is past
6137c478bd9Sstevel@tonic-gate 				 * the end of the resource.
6147c478bd9Sstevel@tonic-gate 				 */
6157c478bd9Sstevel@tonic-gate 				if (base >= (oldbase + mapp->ra_len + 1)) {
6167c478bd9Sstevel@tonic-gate 					continue;
6177c478bd9Sstevel@tonic-gate 				}
6187c478bd9Sstevel@tonic-gate 			}
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 			if (req->ra_flags & NDI_RA_ALLOC_PARTIAL_OK) {
6217c478bd9Sstevel@tonic-gate 				if ((upper - mapp->ra_base)  <  mapp->ra_len)
6227c478bd9Sstevel@tonic-gate 					remlen = upper - base;
6237c478bd9Sstevel@tonic-gate 				else
6247c478bd9Sstevel@tonic-gate 					remlen = mapp->ra_len -
6257c478bd9Sstevel@tonic-gate 						(base - mapp->ra_base);
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 				if ((backlargestp == NULL) ||
6287c478bd9Sstevel@tonic-gate 				    (largestlen < remlen)) {
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 					backlargestp = backp;
6317c478bd9Sstevel@tonic-gate 					largestbase = base;
6327c478bd9Sstevel@tonic-gate 					largestlen = remlen;
6337c478bd9Sstevel@tonic-gate 				}
6347c478bd9Sstevel@tonic-gate 			}
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 			if (mapp->ra_len >= len) {
6377c478bd9Sstevel@tonic-gate 				/* a candidate -- apply constraints */
6387c478bd9Sstevel@tonic-gate 				if ((len > (mapp->ra_len -
6397c478bd9Sstevel@tonic-gate 				    (base - mapp->ra_base))) ||
6407c478bd9Sstevel@tonic-gate 				    ((len - 1 + base) > upper)) {
6417c478bd9Sstevel@tonic-gate 					continue;
6427c478bd9Sstevel@tonic-gate 				}
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 				/* we have a fit */
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 				DEBUGPRT(CE_CONT, "\thave a fit\n");
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 				adjust_link(backp, mapp, base, len);
6497c478bd9Sstevel@tonic-gate 				rval = NDI_SUCCESS;
6507c478bd9Sstevel@tonic-gate 				break;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 			}
6537c478bd9Sstevel@tonic-gate 		}
6547c478bd9Sstevel@tonic-gate 	} else {
6557c478bd9Sstevel@tonic-gate 		/* want an exact value/fit */
6567c478bd9Sstevel@tonic-gate 		base = req->ra_addr;
6577c478bd9Sstevel@tonic-gate 		len = req->ra_len;
6587c478bd9Sstevel@tonic-gate 		for (; mapp != NULL && mapp->ra_base <= upper;
6597c478bd9Sstevel@tonic-gate 			backp = &(mapp->ra_next), mapp = mapp->ra_next) {
6607c478bd9Sstevel@tonic-gate 			if (base >= mapp->ra_base &&
6617c478bd9Sstevel@tonic-gate 			    ((base - mapp->ra_base) < mapp->ra_len)) {
6627c478bd9Sstevel@tonic-gate 				/*
6637c478bd9Sstevel@tonic-gate 				 * This is the node with he requested base in
6647c478bd9Sstevel@tonic-gate 				 * its range
6657c478bd9Sstevel@tonic-gate 				 */
6667c478bd9Sstevel@tonic-gate 				if ((len > mapp->ra_len) ||
6677c478bd9Sstevel@tonic-gate 				    (base - mapp->ra_base >
6687c478bd9Sstevel@tonic-gate 				    mapp->ra_len - len)) {
6697c478bd9Sstevel@tonic-gate 					/* length requirement not satisfied */
6707c478bd9Sstevel@tonic-gate 					if (req->ra_flags &
6717c478bd9Sstevel@tonic-gate 					    NDI_RA_ALLOC_PARTIAL_OK) {
6727c478bd9Sstevel@tonic-gate 						if ((upper - mapp->ra_base)
6737c478bd9Sstevel@tonic-gate 						    < mapp->ra_len)
6747c478bd9Sstevel@tonic-gate 							remlen = upper - base;
6757c478bd9Sstevel@tonic-gate 						else
6767c478bd9Sstevel@tonic-gate 							remlen =
6777c478bd9Sstevel@tonic-gate 							    mapp->ra_len -
6787c478bd9Sstevel@tonic-gate 							    (base -
6797c478bd9Sstevel@tonic-gate 							    mapp->ra_base);
6807c478bd9Sstevel@tonic-gate 					}
6817c478bd9Sstevel@tonic-gate 					backlargestp = backp;
6827c478bd9Sstevel@tonic-gate 					largestbase = base;
6837c478bd9Sstevel@tonic-gate 					largestlen = remlen;
6847c478bd9Sstevel@tonic-gate 					base = 0;
6857c478bd9Sstevel@tonic-gate 				} else {
6867c478bd9Sstevel@tonic-gate 					/* We have a match */
6877c478bd9Sstevel@tonic-gate 					adjust_link(backp, mapp, base, len);
6887c478bd9Sstevel@tonic-gate 					rval = NDI_SUCCESS;
6897c478bd9Sstevel@tonic-gate 				}
6907c478bd9Sstevel@tonic-gate 				break;
6917c478bd9Sstevel@tonic-gate 			}
6927c478bd9Sstevel@tonic-gate 		}
6937c478bd9Sstevel@tonic-gate 	}
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	if ((rval != NDI_SUCCESS) &&
6967c478bd9Sstevel@tonic-gate 	    (req->ra_flags & NDI_RA_ALLOC_PARTIAL_OK) &&
6977c478bd9Sstevel@tonic-gate 	    (backlargestp != NULL)) {
6987c478bd9Sstevel@tonic-gate 		adjust_link(backlargestp, *backlargestp, largestbase,
6997c478bd9Sstevel@tonic-gate 			largestlen);
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 		base = largestbase;
7027c478bd9Sstevel@tonic-gate 		len = largestlen;
7037c478bd9Sstevel@tonic-gate 		rval = NDI_RA_PARTIAL_REQ;
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	mutex_exit(&ra_lock);
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	if (rval == NDI_FAILURE) {
7097c478bd9Sstevel@tonic-gate 		*retbasep = 0;
7107c478bd9Sstevel@tonic-gate 		*retlenp = 0;
7117c478bd9Sstevel@tonic-gate 	} else {
7127c478bd9Sstevel@tonic-gate 		*retbasep = base;
7137c478bd9Sstevel@tonic-gate 		*retlenp = len;
7147c478bd9Sstevel@tonic-gate 	}
7157c478bd9Sstevel@tonic-gate 	return (rval);
7167c478bd9Sstevel@tonic-gate }
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate /*
7197c478bd9Sstevel@tonic-gate  * isa_resource_setup
7207c478bd9Sstevel@tonic-gate  *	check for /used-resources and initialize
7217c478bd9Sstevel@tonic-gate  *	based on info there.  If no /used-resources,
7227c478bd9Sstevel@tonic-gate  *	fail.
7237c478bd9Sstevel@tonic-gate  */
7247c478bd9Sstevel@tonic-gate int
7257c478bd9Sstevel@tonic-gate isa_resource_setup()
7267c478bd9Sstevel@tonic-gate {
7277c478bd9Sstevel@tonic-gate 	dev_info_t *used, *usedpdip;
7287c478bd9Sstevel@tonic-gate 	/*
7297c478bd9Sstevel@tonic-gate 	 * note that at this time bootconf creates 32 bit properties for
7307c478bd9Sstevel@tonic-gate 	 * io-space and device-memory
7317c478bd9Sstevel@tonic-gate 	 */
7327c478bd9Sstevel@tonic-gate 	struct iorange {
7337c478bd9Sstevel@tonic-gate 		uint32_t	base;
7347c478bd9Sstevel@tonic-gate 		uint32_t	len;
7357c478bd9Sstevel@tonic-gate 	} *iorange;
7367c478bd9Sstevel@tonic-gate 	struct memrange {
7377c478bd9Sstevel@tonic-gate 		uint32_t	base;
7387c478bd9Sstevel@tonic-gate 		uint32_t	len;
7397c478bd9Sstevel@tonic-gate 	} *memrange;
7407c478bd9Sstevel@tonic-gate 	uint32_t *irq;
7417c478bd9Sstevel@tonic-gate 	int proplen;
7427c478bd9Sstevel@tonic-gate 	int i, len;
7437c478bd9Sstevel@tonic-gate 	int maxrange;
7447c478bd9Sstevel@tonic-gate 	ndi_ra_request_t req;
7457c478bd9Sstevel@tonic-gate 	uint64_t retbase;
7467c478bd9Sstevel@tonic-gate 	uint64_t retlen;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	used = ddi_find_devinfo("used-resources", -1, 0);
7497c478bd9Sstevel@tonic-gate 	if (used == NULL) {
7507c478bd9Sstevel@tonic-gate 		DEBUGPRT(CE_CONT,
7517c478bd9Sstevel@tonic-gate 			"isa_resource_setup: used-resources not found");
7527c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
7537c478bd9Sstevel@tonic-gate 	}
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	/*
7567c478bd9Sstevel@tonic-gate 	 * initialize to all resources being present
7577c478bd9Sstevel@tonic-gate 	 * and then remove the ones in use.
7587c478bd9Sstevel@tonic-gate 	 */
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	usedpdip = ddi_root_node();
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	DEBUGPRT(CE_CONT, "isa_resource_setup: used = %p usedpdip = %p\n",
7637c478bd9Sstevel@tonic-gate 	    (void *)used, (void *)usedpdip);
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	if (ndi_ra_map_setup(usedpdip, NDI_RA_TYPE_IO) == NDI_FAILURE) {
7667c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
7677c478bd9Sstevel@tonic-gate 	}
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	/* initialize io space, highest end base is 0xffff */
7707c478bd9Sstevel@tonic-gate 	/* note that length is highest addr + 1 since starts from 0 */
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	(void) ndi_ra_free(usedpdip, 0, 0xffff + 1,  NDI_RA_TYPE_IO, 0);
7737c478bd9Sstevel@tonic-gate 
774a3282898Scth 	if (ddi_getlongprop(DDI_DEV_T_ANY, used, DDI_PROP_DONTPASS,
7757c478bd9Sstevel@tonic-gate 	    "io-space", (caddr_t)&iorange, &proplen) == DDI_SUCCESS) {
7767c478bd9Sstevel@tonic-gate 		maxrange = proplen / sizeof (struct iorange);
7777c478bd9Sstevel@tonic-gate 		/* remove the "used" I/O resources */
7787c478bd9Sstevel@tonic-gate 		for (i = 0; i < maxrange; i++) {
7797c478bd9Sstevel@tonic-gate 			bzero((caddr_t)&req, sizeof (req));
7807c478bd9Sstevel@tonic-gate 			req.ra_addr =  (uint64_t)iorange[i].base;
7817c478bd9Sstevel@tonic-gate 			req.ra_len = (uint64_t)iorange[i].len;
7827c478bd9Sstevel@tonic-gate 			req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
7837c478bd9Sstevel@tonic-gate 			(void) ndi_ra_alloc(usedpdip, &req, &retbase, &retlen,
7847c478bd9Sstevel@tonic-gate 			    NDI_RA_TYPE_IO, 0);
7857c478bd9Sstevel@tonic-gate 		}
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 		kmem_free((caddr_t)iorange, proplen);
7887c478bd9Sstevel@tonic-gate 	}
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	if (ndi_ra_map_setup(usedpdip, NDI_RA_TYPE_MEM) == NDI_FAILURE) {
7917c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
7927c478bd9Sstevel@tonic-gate 	}
7937c478bd9Sstevel@tonic-gate 	/* initialize memory space where highest end base is 0xffffffff */
7947c478bd9Sstevel@tonic-gate 	/* note that length is highest addr + 1 since starts from 0 */
7957c478bd9Sstevel@tonic-gate 	(void) ndi_ra_free(usedpdip, 0, ((uint64_t)((uint32_t)~0)) + 1,
7967c478bd9Sstevel@tonic-gate 	    NDI_RA_TYPE_MEM, 0);
7977c478bd9Sstevel@tonic-gate 
798a3282898Scth 	if (ddi_getlongprop(DDI_DEV_T_ANY, used, DDI_PROP_DONTPASS,
7997c478bd9Sstevel@tonic-gate 	    "device-memory", (caddr_t)&memrange, &proplen) == DDI_SUCCESS) {
8007c478bd9Sstevel@tonic-gate 		maxrange = proplen / sizeof (struct memrange);
8017c478bd9Sstevel@tonic-gate 		/* remove the "used" memory resources */
8027c478bd9Sstevel@tonic-gate 		for (i = 0; i < maxrange; i++) {
8037c478bd9Sstevel@tonic-gate 			bzero((caddr_t)&req, sizeof (req));
8047c478bd9Sstevel@tonic-gate 			req.ra_addr = (uint64_t)memrange[i].base;
8057c478bd9Sstevel@tonic-gate 			req.ra_len = (uint64_t)memrange[i].len;
8067c478bd9Sstevel@tonic-gate 			req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
8077c478bd9Sstevel@tonic-gate 			(void) ndi_ra_alloc(usedpdip, &req, &retbase, &retlen,
8087c478bd9Sstevel@tonic-gate 			    NDI_RA_TYPE_MEM, 0);
8097c478bd9Sstevel@tonic-gate 		}
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 		kmem_free((caddr_t)memrange, proplen);
8127c478bd9Sstevel@tonic-gate 	}
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	if (ndi_ra_map_setup(usedpdip, NDI_RA_TYPE_INTR) == NDI_FAILURE) {
8157c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
8167c478bd9Sstevel@tonic-gate 	}
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	/* initialize the interrupt space */
8197c478bd9Sstevel@tonic-gate 	(void) ndi_ra_free(usedpdip, 0, 16, NDI_RA_TYPE_INTR, 0);
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
8227c478bd9Sstevel@tonic-gate 	bzero(&req, sizeof (req));
8237c478bd9Sstevel@tonic-gate 	req.ra_addr = 2;	/* 2 == 9 so never allow */
8247c478bd9Sstevel@tonic-gate 	req.ra_len = 1;
8257c478bd9Sstevel@tonic-gate 	req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
8267c478bd9Sstevel@tonic-gate 	(void) ndi_ra_alloc(usedpdip, &req, &retbase, &retlen,
8277c478bd9Sstevel@tonic-gate 	    NDI_RA_TYPE_INTR, 0);
8287c478bd9Sstevel@tonic-gate #endif
8297c478bd9Sstevel@tonic-gate 
830a3282898Scth 	if (ddi_getlongprop(DDI_DEV_T_ANY, used, DDI_PROP_DONTPASS,
8317c478bd9Sstevel@tonic-gate 	    "interrupts", (caddr_t)&irq, &proplen) == DDI_SUCCESS) {
8327c478bd9Sstevel@tonic-gate 		/* Initialize available interrupts by negating the used */
8337c478bd9Sstevel@tonic-gate 		len = (proplen / sizeof (uint32_t));
8347c478bd9Sstevel@tonic-gate 		for (i = 0; i < len; i++) {
8357c478bd9Sstevel@tonic-gate 			bzero((caddr_t)&req, sizeof (req));
8367c478bd9Sstevel@tonic-gate 			req.ra_addr = (uint64_t)irq[i];
8377c478bd9Sstevel@tonic-gate 			req.ra_len = 1;
8387c478bd9Sstevel@tonic-gate 			req.ra_flags = NDI_RA_ALLOC_SPECIFIED;
8397c478bd9Sstevel@tonic-gate 			(void) ndi_ra_alloc(usedpdip, &req, &retbase, &retlen,
8407c478bd9Sstevel@tonic-gate 			    NDI_RA_TYPE_INTR, 0);
8417c478bd9Sstevel@tonic-gate 		}
8427c478bd9Sstevel@tonic-gate 		kmem_free((caddr_t)irq, proplen);
8437c478bd9Sstevel@tonic-gate 	}
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate #ifdef BUSRA_DEBUG
8467c478bd9Sstevel@tonic-gate 	if (busra_debug) {
8477c478bd9Sstevel@tonic-gate 		(void) ra_dump_all(NULL, usedpdip);
8487c478bd9Sstevel@tonic-gate 	}
8497c478bd9Sstevel@tonic-gate #endif
8507c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate }
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate #ifdef BUSRA_DEBUG
8557c478bd9Sstevel@tonic-gate void
8567c478bd9Sstevel@tonic-gate ra_dump_all(char *type, dev_info_t *dip)
8577c478bd9Sstevel@tonic-gate {
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	struct ra_type_map *typemap;
8607c478bd9Sstevel@tonic-gate 	struct ra_dip_type *dipmap;
8617c478bd9Sstevel@tonic-gate 	struct ra_resource *res;
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	typemap =  (struct ra_type_map *)ra_map_list_head;
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	for (; typemap != NULL; typemap = typemap->ra_next) {
8667c478bd9Sstevel@tonic-gate 		if (type != NULL) {
8677c478bd9Sstevel@tonic-gate 			if (strcmp(typemap->type, type) != 0)
8687c478bd9Sstevel@tonic-gate 				continue;
8697c478bd9Sstevel@tonic-gate 		}
8707c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "type is %s\n", typemap->type);
8717c478bd9Sstevel@tonic-gate 		for (dipmap = typemap->ra_dip_list; dipmap != NULL;
8727c478bd9Sstevel@tonic-gate 			dipmap = dipmap->ra_next) {
8737c478bd9Sstevel@tonic-gate 			if (dip != NULL) {
8747c478bd9Sstevel@tonic-gate 				if ((dipmap->ra_dip) != dip)
8757c478bd9Sstevel@tonic-gate 					continue;
8767c478bd9Sstevel@tonic-gate 			}
8777c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "  dip is %p\n",
8787c478bd9Sstevel@tonic-gate 			    (void *)dipmap->ra_dip);
8797c478bd9Sstevel@tonic-gate 			for (res = dipmap->ra_rangeset; res != NULL;
8807c478bd9Sstevel@tonic-gate 				res = res->ra_next) {
8817c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT, "\t  range is %" PRIx64
8827c478bd9Sstevel@tonic-gate 				    " %" PRIx64 "\n", res->ra_base,
8837c478bd9Sstevel@tonic-gate 				    res->ra_len);
8847c478bd9Sstevel@tonic-gate 			}
8857c478bd9Sstevel@tonic-gate 			if (dip != NULL)
8867c478bd9Sstevel@tonic-gate 				break;
8877c478bd9Sstevel@tonic-gate 		}
8887c478bd9Sstevel@tonic-gate 		if (type != NULL)
8897c478bd9Sstevel@tonic-gate 			break;
8907c478bd9Sstevel@tonic-gate 	}
8917c478bd9Sstevel@tonic-gate }
8927c478bd9Sstevel@tonic-gate #endif
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate struct bus_range {	/* 1275 "bus-range" property definition */
8957c478bd9Sstevel@tonic-gate 	uint32_t lo;
8967c478bd9Sstevel@tonic-gate 	uint32_t hi;
8977c478bd9Sstevel@tonic-gate } pci_bus_range;
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate struct busnum_ctrl {
9007c478bd9Sstevel@tonic-gate 	int	rv;
9017c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
9027c478bd9Sstevel@tonic-gate 	struct	bus_range *range;
9037c478bd9Sstevel@tonic-gate };
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate /*
9077c478bd9Sstevel@tonic-gate  * Setup resource map for the pci bus node based on the "available"
9087c478bd9Sstevel@tonic-gate  * property and "bus-range" property.
9097c478bd9Sstevel@tonic-gate  */
9107c478bd9Sstevel@tonic-gate int
9117c478bd9Sstevel@tonic-gate pci_resource_setup(dev_info_t *dip)
9127c478bd9Sstevel@tonic-gate {
9137c478bd9Sstevel@tonic-gate 	pci_regspec_t *regs;
9147c478bd9Sstevel@tonic-gate 	int rlen, rcount, i;
9157c478bd9Sstevel@tonic-gate 	char bus_type[16] = "(unknown)";
9167c478bd9Sstevel@tonic-gate 	int len;
9177c478bd9Sstevel@tonic-gate 	struct busnum_ctrl ctrl;
9187c478bd9Sstevel@tonic-gate 	int circular_count;
9197c478bd9Sstevel@tonic-gate 	int rval = NDI_SUCCESS;
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	/*
9227c478bd9Sstevel@tonic-gate 	 * If this is a pci bus node then look for "available" property
9237c478bd9Sstevel@tonic-gate 	 * to find the available resources on this bus.
9247c478bd9Sstevel@tonic-gate 	 */
9257c478bd9Sstevel@tonic-gate 	len = sizeof (bus_type);
9267c478bd9Sstevel@tonic-gate 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN_AND_VAL_BUF,
9277c478bd9Sstevel@tonic-gate 	    DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS, "device_type",
9287c478bd9Sstevel@tonic-gate 	    (caddr_t)&bus_type, &len) != DDI_SUCCESS)
9297c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
9307c478bd9Sstevel@tonic-gate 
93170025d76Sjohnny 	/* it is not a pci/pci-ex bus type */
932c9f965e3Set142600 	if ((strcmp(bus_type, "pci") != 0) && (strcmp(bus_type, "pciex") != 0))
9337c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	/*
9367c478bd9Sstevel@tonic-gate 	 * The pci-hotplug project addresses adding the call
9377c478bd9Sstevel@tonic-gate 	 * to pci_resource_setup from pci nexus driver.
9387c478bd9Sstevel@tonic-gate 	 * However that project would initially be only for x86,
9397c478bd9Sstevel@tonic-gate 	 * so for sparc pcmcia-pci support we still need to call
9407c478bd9Sstevel@tonic-gate 	 * pci_resource_setup in pcic driver. Once all pci nexus drivers
9417c478bd9Sstevel@tonic-gate 	 * are updated to call pci_resource_setup this portion of the
9427c478bd9Sstevel@tonic-gate 	 * code would really become an assert to make sure this
9437c478bd9Sstevel@tonic-gate 	 * function is not called for the same dip twice.
9447c478bd9Sstevel@tonic-gate 	 */
9457c478bd9Sstevel@tonic-gate 	{
9467c478bd9Sstevel@tonic-gate 		if (ra_map_exist(dip, NDI_RA_TYPE_MEM) == NDI_SUCCESS) {
9477c478bd9Sstevel@tonic-gate 			return (NDI_FAILURE);
9487c478bd9Sstevel@tonic-gate 		}
9497c478bd9Sstevel@tonic-gate 	}
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 
95270025d76Sjohnny 	/*
95370025d76Sjohnny 	 * Create empty resource maps first.
95470025d76Sjohnny 	 *
95570025d76Sjohnny 	 * NOTE: If all the allocated resources are already assigned to
95670025d76Sjohnny 	 * device(s) in the hot plug slot then "available" property may not
95770025d76Sjohnny 	 * be present. But, subsequent hot plug operation may unconfigure
95870025d76Sjohnny 	 * the device in the slot and try to free up it's resources. So,
95970025d76Sjohnny 	 * at the minimum we should create empty maps here.
96070025d76Sjohnny 	 */
9617c478bd9Sstevel@tonic-gate 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_MEM) == NDI_FAILURE) {
9627c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
9637c478bd9Sstevel@tonic-gate 	}
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_IO) == NDI_FAILURE) {
9667c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
9677c478bd9Sstevel@tonic-gate 	}
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_PCI_BUSNUM) == NDI_FAILURE) {
9707c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
9717c478bd9Sstevel@tonic-gate 	}
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_PCI_PREFETCH_MEM) ==
9747c478bd9Sstevel@tonic-gate 	    NDI_FAILURE) {
9757c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
9767c478bd9Sstevel@tonic-gate 	}
9777c478bd9Sstevel@tonic-gate 
97870025d76Sjohnny 	/* read the "available" property if it is available */
97970025d76Sjohnny 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
98070025d76Sjohnny 	    "available", (caddr_t)&regs, &rlen) == DDI_SUCCESS) {
98170025d76Sjohnny 		/*
98270025d76Sjohnny 		 * create the available resource list for both memory and
98370025d76Sjohnny 		 * io space
98470025d76Sjohnny 		 */
9857c478bd9Sstevel@tonic-gate 		rcount = rlen / sizeof (pci_regspec_t);
9867c478bd9Sstevel@tonic-gate 		for (i = 0; i < rcount; i++) {
9877c478bd9Sstevel@tonic-gate 		    switch (PCI_REG_ADDR_G(regs[i].pci_phys_hi)) {
9887c478bd9Sstevel@tonic-gate 		    case PCI_REG_ADDR_G(PCI_ADDR_MEM32):
9897c478bd9Sstevel@tonic-gate 			(void) ndi_ra_free(dip,
9907c478bd9Sstevel@tonic-gate 			    (uint64_t)regs[i].pci_phys_low,
9917c478bd9Sstevel@tonic-gate 			    (uint64_t)regs[i].pci_size_low,
9927c478bd9Sstevel@tonic-gate 			    (regs[i].pci_phys_hi & PCI_REG_PF_M) ?
9937c478bd9Sstevel@tonic-gate 			    NDI_RA_TYPE_PCI_PREFETCH_MEM : NDI_RA_TYPE_MEM,
9947c478bd9Sstevel@tonic-gate 			    0);
9957c478bd9Sstevel@tonic-gate 			break;
9967c478bd9Sstevel@tonic-gate 		    case PCI_REG_ADDR_G(PCI_ADDR_MEM64):
9977c478bd9Sstevel@tonic-gate 			(void) ndi_ra_free(dip,
9987c478bd9Sstevel@tonic-gate 			    ((uint64_t)(regs[i].pci_phys_mid) << 32) |
9997c478bd9Sstevel@tonic-gate 			    ((uint64_t)(regs[i].pci_phys_low)),
10007c478bd9Sstevel@tonic-gate 			    ((uint64_t)(regs[i].pci_size_hi) << 32) |
10017c478bd9Sstevel@tonic-gate 			    ((uint64_t)(regs[i].pci_size_low)),
10027c478bd9Sstevel@tonic-gate 			    (regs[i].pci_phys_hi & PCI_REG_PF_M) ?
10037c478bd9Sstevel@tonic-gate 			    NDI_RA_TYPE_PCI_PREFETCH_MEM : NDI_RA_TYPE_MEM,
10047c478bd9Sstevel@tonic-gate 			    0);
10057c478bd9Sstevel@tonic-gate 			break;
10067c478bd9Sstevel@tonic-gate 		    case PCI_REG_ADDR_G(PCI_ADDR_IO):
10077c478bd9Sstevel@tonic-gate 			(void) ndi_ra_free(dip,
10087c478bd9Sstevel@tonic-gate 			    (uint64_t)regs[i].pci_phys_low,
10097c478bd9Sstevel@tonic-gate 			    (uint64_t)regs[i].pci_size_low,
10107c478bd9Sstevel@tonic-gate 			    NDI_RA_TYPE_IO,
10117c478bd9Sstevel@tonic-gate 			    0);
10127c478bd9Sstevel@tonic-gate 			break;
10137c478bd9Sstevel@tonic-gate 		    case PCI_REG_ADDR_G(PCI_ADDR_CONFIG):
10147c478bd9Sstevel@tonic-gate 			break;
10157c478bd9Sstevel@tonic-gate 		    default:
10167c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
10177c478bd9Sstevel@tonic-gate 			    "pci_resource_setup: bad addr type: %x\n",
10187c478bd9Sstevel@tonic-gate 			    PCI_REG_ADDR_G(regs[i].pci_phys_hi));
10197c478bd9Sstevel@tonic-gate 			break;
10207c478bd9Sstevel@tonic-gate 		    }
10217c478bd9Sstevel@tonic-gate 		}
1022ae115bc7Smrj 		kmem_free(regs, rlen);
102370025d76Sjohnny 	}
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	/*
102670025d76Sjohnny 	 * update resource map for available bus numbers if the node
10277c478bd9Sstevel@tonic-gate 	 * has available-bus-range or bus-range property.
10287c478bd9Sstevel@tonic-gate 	 */
10297c478bd9Sstevel@tonic-gate 	len = sizeof (struct bus_range);
1030a3282898Scth 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
10317c478bd9Sstevel@tonic-gate 	    "available-bus-range", (caddr_t)&pci_bus_range, &len) ==
10327c478bd9Sstevel@tonic-gate 	    DDI_SUCCESS) {
10337c478bd9Sstevel@tonic-gate 		/*
10347c478bd9Sstevel@tonic-gate 		 * Add bus numbers in the range to the free list.
10357c478bd9Sstevel@tonic-gate 		 */
10367c478bd9Sstevel@tonic-gate 		(void) ndi_ra_free(dip, (uint64_t)pci_bus_range.lo,
10377c478bd9Sstevel@tonic-gate 		    (uint64_t)pci_bus_range.hi - (uint64_t)pci_bus_range.lo +
10387c478bd9Sstevel@tonic-gate 		    1, NDI_RA_TYPE_PCI_BUSNUM, 0);
10397c478bd9Sstevel@tonic-gate 	} else {
10407c478bd9Sstevel@tonic-gate 		/*
10417c478bd9Sstevel@tonic-gate 		 * We don't have an available-bus-range property. If, instead,
10427c478bd9Sstevel@tonic-gate 		 * we have a bus-range property we add all the bus numbers
10437c478bd9Sstevel@tonic-gate 		 * in that range to the free list but we must then scan
10447c478bd9Sstevel@tonic-gate 		 * for pci-pci bridges on this bus to find out the if there
10457c478bd9Sstevel@tonic-gate 		 * are any of those bus numbers already in use. If so, we can
10467c478bd9Sstevel@tonic-gate 		 * reclaim them.
10477c478bd9Sstevel@tonic-gate 		 */
10487c478bd9Sstevel@tonic-gate 		len = sizeof (struct bus_range);
1049a3282898Scth 		if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip,
10507c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "bus-range", (caddr_t)&pci_bus_range,
10517c478bd9Sstevel@tonic-gate 		    &len) == DDI_SUCCESS) {
10527c478bd9Sstevel@tonic-gate 			if (pci_bus_range.lo != pci_bus_range.hi) {
10537c478bd9Sstevel@tonic-gate 				/*
10547c478bd9Sstevel@tonic-gate 				 * Add bus numbers other than the secondary
10557c478bd9Sstevel@tonic-gate 				 * bus number to the free list.
10567c478bd9Sstevel@tonic-gate 				 */
10577c478bd9Sstevel@tonic-gate 				(void) ndi_ra_free(dip,
10587c478bd9Sstevel@tonic-gate 				    (uint64_t)pci_bus_range.lo + 1,
10597c478bd9Sstevel@tonic-gate 				    (uint64_t)pci_bus_range.hi -
10607c478bd9Sstevel@tonic-gate 				    (uint64_t)pci_bus_range.lo,
10617c478bd9Sstevel@tonic-gate 				    NDI_RA_TYPE_PCI_BUSNUM, 0);
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 				/* scan for pci-pci bridges */
10647c478bd9Sstevel@tonic-gate 				ctrl.rv = DDI_SUCCESS;
10657c478bd9Sstevel@tonic-gate 				ctrl.dip = dip;
10667c478bd9Sstevel@tonic-gate 				ctrl.range = &pci_bus_range;
10677c478bd9Sstevel@tonic-gate 				ndi_devi_enter(dip, &circular_count);
10687c478bd9Sstevel@tonic-gate 				ddi_walk_devs(ddi_get_child(dip),
10697c478bd9Sstevel@tonic-gate 				    claim_pci_busnum, (void *)&ctrl);
10707c478bd9Sstevel@tonic-gate 				ndi_devi_exit(dip, circular_count);
10717c478bd9Sstevel@tonic-gate 				if (ctrl.rv != DDI_SUCCESS) {
10727c478bd9Sstevel@tonic-gate 					/* failed to create the map */
10737c478bd9Sstevel@tonic-gate 					(void) ndi_ra_map_destroy(dip,
10747c478bd9Sstevel@tonic-gate 					    NDI_RA_TYPE_PCI_BUSNUM);
10757c478bd9Sstevel@tonic-gate 					rval = NDI_FAILURE;
10767c478bd9Sstevel@tonic-gate 				}
10777c478bd9Sstevel@tonic-gate 			}
10787c478bd9Sstevel@tonic-gate 		}
10797c478bd9Sstevel@tonic-gate 	}
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate #ifdef BUSRA_DEBUG
10827c478bd9Sstevel@tonic-gate 	if (busra_debug) {
10837c478bd9Sstevel@tonic-gate 		(void) ra_dump_all(NULL, dip);
10847c478bd9Sstevel@tonic-gate 	}
10857c478bd9Sstevel@tonic-gate #endif
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	return (rval);
10887c478bd9Sstevel@tonic-gate }
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate /*
10917c478bd9Sstevel@tonic-gate  * If the device is a PCI bus device (i.e bus-range property exists) then
10927c478bd9Sstevel@tonic-gate  * claim the bus numbers used by the device from the specified bus
10937c478bd9Sstevel@tonic-gate  * resource map.
10947c478bd9Sstevel@tonic-gate  */
10957c478bd9Sstevel@tonic-gate static int
10967c478bd9Sstevel@tonic-gate claim_pci_busnum(dev_info_t *dip, void *arg)
10977c478bd9Sstevel@tonic-gate {
10987c478bd9Sstevel@tonic-gate 	struct bus_range pci_bus_range;
10997c478bd9Sstevel@tonic-gate 	struct busnum_ctrl *ctrl;
11007c478bd9Sstevel@tonic-gate 	ndi_ra_request_t req;
11017c478bd9Sstevel@tonic-gate 	char bus_type[16] = "(unknown)";
11027c478bd9Sstevel@tonic-gate 	int len;
11037c478bd9Sstevel@tonic-gate 	uint64_t base;
11047c478bd9Sstevel@tonic-gate 	uint64_t retlen;
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	ctrl = (struct busnum_ctrl *)arg;
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	/* check if this is a PCI bus node */
11097c478bd9Sstevel@tonic-gate 	len = sizeof (bus_type);
11107c478bd9Sstevel@tonic-gate 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN_AND_VAL_BUF,
11117c478bd9Sstevel@tonic-gate 	    DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS, "device_type",
11127c478bd9Sstevel@tonic-gate 	    (caddr_t)&bus_type, &len) != DDI_SUCCESS)
11137c478bd9Sstevel@tonic-gate 		return (DDI_WALK_PRUNECHILD);
11147c478bd9Sstevel@tonic-gate 
111570025d76Sjohnny 	/* it is not a pci/pci-ex bus type */
1116c9f965e3Set142600 	if ((strcmp(bus_type, "pci") != 0) && (strcmp(bus_type, "pciex") != 0))
11177c478bd9Sstevel@tonic-gate 		return (DDI_WALK_PRUNECHILD);
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	/* look for the bus-range property */
11207c478bd9Sstevel@tonic-gate 	len = sizeof (struct bus_range);
1121a3282898Scth 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
11227c478bd9Sstevel@tonic-gate 	    "bus-range", (caddr_t)&pci_bus_range, &len) == DDI_SUCCESS) {
11237c478bd9Sstevel@tonic-gate 		if ((pci_bus_range.lo >= ctrl->range->lo) &&
11247c478bd9Sstevel@tonic-gate 		    (pci_bus_range.hi <= ctrl->range->hi)) {
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 			/* claim the bus range from the bus resource map */
11277c478bd9Sstevel@tonic-gate 			bzero((caddr_t)&req, sizeof (req));
11287c478bd9Sstevel@tonic-gate 			req.ra_addr = (uint64_t)pci_bus_range.lo;
11297c478bd9Sstevel@tonic-gate 			req.ra_flags |= NDI_RA_ALLOC_SPECIFIED;
11307c478bd9Sstevel@tonic-gate 			req.ra_len = (uint64_t)pci_bus_range.hi -
11317c478bd9Sstevel@tonic-gate 			    (uint64_t)pci_bus_range.lo + 1;
11327c478bd9Sstevel@tonic-gate 			if (ndi_ra_alloc(ctrl->dip, &req, &base, &retlen,
11337c478bd9Sstevel@tonic-gate 			    NDI_RA_TYPE_PCI_BUSNUM, 0) == NDI_SUCCESS)
11347c478bd9Sstevel@tonic-gate 				return (DDI_WALK_PRUNECHILD);
11357c478bd9Sstevel@tonic-gate 		}
11367c478bd9Sstevel@tonic-gate 	}
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	/*
11397c478bd9Sstevel@tonic-gate 	 * Error return.
11407c478bd9Sstevel@tonic-gate 	 */
11417c478bd9Sstevel@tonic-gate 	ctrl->rv = DDI_FAILURE;
11427c478bd9Sstevel@tonic-gate 	return (DDI_WALK_TERMINATE);
11437c478bd9Sstevel@tonic-gate }
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate void
11467c478bd9Sstevel@tonic-gate pci_resource_destroy(dev_info_t *dip)
11477c478bd9Sstevel@tonic-gate {
11487c478bd9Sstevel@tonic-gate 	(void) ndi_ra_map_destroy(dip, NDI_RA_TYPE_IO);
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	(void) ndi_ra_map_destroy(dip, NDI_RA_TYPE_MEM);
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 	(void) ndi_ra_map_destroy(dip, NDI_RA_TYPE_PCI_BUSNUM);
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	(void) ndi_ra_map_destroy(dip, NDI_RA_TYPE_PCI_PREFETCH_MEM);
11557c478bd9Sstevel@tonic-gate }
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate int
11597c478bd9Sstevel@tonic-gate pci_resource_setup_avail(dev_info_t *dip, pci_regspec_t *avail_p, int entries)
11607c478bd9Sstevel@tonic-gate {
11617c478bd9Sstevel@tonic-gate 	int i;
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_MEM) == NDI_FAILURE)
11647c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
11657c478bd9Sstevel@tonic-gate 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_IO) == NDI_FAILURE)
11667c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
11677c478bd9Sstevel@tonic-gate 	if (ndi_ra_map_setup(dip, NDI_RA_TYPE_PCI_PREFETCH_MEM) == NDI_FAILURE)
11687c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	/* for each entry in the PCI "available" property */
11717c478bd9Sstevel@tonic-gate 	for (i = 0; i < entries; i++, avail_p++) {
11727c478bd9Sstevel@tonic-gate 		if (avail_p->pci_phys_hi == -1u)
11737c478bd9Sstevel@tonic-gate 			goto err;
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 		switch (PCI_REG_ADDR_G(avail_p->pci_phys_hi)) {
11767c478bd9Sstevel@tonic-gate 		case PCI_REG_ADDR_G(PCI_ADDR_MEM32): {
11777c478bd9Sstevel@tonic-gate 			(void) ndi_ra_free(dip,
11787c478bd9Sstevel@tonic-gate 				(uint64_t)avail_p->pci_phys_low,
11797c478bd9Sstevel@tonic-gate 				(uint64_t)avail_p->pci_size_low,
11807c478bd9Sstevel@tonic-gate 				(avail_p->pci_phys_hi &
11817c478bd9Sstevel@tonic-gate 					PCI_REG_PF_M) ?
11827c478bd9Sstevel@tonic-gate 					NDI_RA_TYPE_PCI_PREFETCH_MEM :
11837c478bd9Sstevel@tonic-gate 					NDI_RA_TYPE_MEM,
11847c478bd9Sstevel@tonic-gate 				0);
11857c478bd9Sstevel@tonic-gate 			}
11867c478bd9Sstevel@tonic-gate 			break;
11877c478bd9Sstevel@tonic-gate 		case PCI_REG_ADDR_G(PCI_ADDR_IO):
11887c478bd9Sstevel@tonic-gate 			(void) ndi_ra_free(dip,
11897c478bd9Sstevel@tonic-gate 				(uint64_t)avail_p->pci_phys_low,
11907c478bd9Sstevel@tonic-gate 				(uint64_t)avail_p->pci_size_low,
11917c478bd9Sstevel@tonic-gate 				NDI_RA_TYPE_IO,
11927c478bd9Sstevel@tonic-gate 				0);
11937c478bd9Sstevel@tonic-gate 			break;
11947c478bd9Sstevel@tonic-gate 		default:
11957c478bd9Sstevel@tonic-gate 			goto err;
11967c478bd9Sstevel@tonic-gate 		}
11977c478bd9Sstevel@tonic-gate 	}
11987c478bd9Sstevel@tonic-gate #ifdef BUSRA_DEBUG
11997c478bd9Sstevel@tonic-gate 	if (busra_debug) {
12007c478bd9Sstevel@tonic-gate 		(void) ra_dump_all(NULL, dip);
12017c478bd9Sstevel@tonic-gate 	}
12027c478bd9Sstevel@tonic-gate #endif
12037c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate err:
12067c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "pci_resource_setup_avail: bad entry[%d]=%x\n",
12077c478bd9Sstevel@tonic-gate 		i, avail_p->pci_phys_hi);
12087c478bd9Sstevel@tonic-gate 	return (NDI_FAILURE);
12097c478bd9Sstevel@tonic-gate }
1210