xref: /illumos-gate/usr/src/uts/i86pc/io/pci/pci_tools.c (revision 3fe80ca4)
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
50e6b5dadSanish  * Common Development and Distribution License (the "License").
60e6b5dadSanish  * 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 /*
228d7fafffSZhi-Jun Robin Fu  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
25*3fe80ca4SDan Cross /*
26*3fe80ca4SDan Cross  * Copyright 2023 Oxide Computer Company
27*3fe80ca4SDan Cross  */
28*3fe80ca4SDan Cross 
29de710d24SJosef 'Jeff' Sipek #include <sys/sysmacros.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
327851eb82Sschwartz #include <sys/stat.h>
337c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
347c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
357c478bd9Sstevel@tonic-gate #include <sys/machparam.h>
367a364d25Sschwartz #include <sys/sunndi.h>
377c478bd9Sstevel@tonic-gate #include <sys/ontrap.h>
387a364d25Sschwartz #include <sys/psm.h>
3970025d76Sjohnny #include <sys/pcie.h>
407c478bd9Sstevel@tonic-gate #include <sys/pci_cfgspace.h>
417c478bd9Sstevel@tonic-gate #include <sys/pci_tools.h>
42649d4cceSanish #include <io/pci/pci_tools_ext.h>
43ae115bc7Smrj #include <sys/apic.h>
447ff178cdSJimmy Vetayases #include <sys/apix.h>
457a364d25Sschwartz #include <io/pci/pci_var.h>
4626947304SEvan Yan #include <sys/pci_impl.h>
477c478bd9Sstevel@tonic-gate #include <sys/promif.h>
48649d4cceSanish #include <sys/x86_archext.h>
490e6b5dadSanish #include <sys/cpuvar.h>
50c0da6274SZhi-Jun Robin Fu #include <sys/pci_cfgacc.h>
517c478bd9Sstevel@tonic-gate 
52843e1988Sjohnlev #ifdef __xpv
53843e1988Sjohnlev #include <sys/hypervisor.h>
54843e1988Sjohnlev #endif
55843e1988Sjohnlev 
56d4476ccbSschwartz #define	PCIEX_BDF_OFFSET_DELTA	4
57d4476ccbSschwartz #define	PCIEX_REG_FUNC_SHIFT	(PCI_REG_FUNC_SHIFT + PCIEX_BDF_OFFSET_DELTA)
58d4476ccbSschwartz #define	PCIEX_REG_DEV_SHIFT	(PCI_REG_DEV_SHIFT + PCIEX_BDF_OFFSET_DELTA)
59d4476ccbSschwartz #define	PCIEX_REG_BUS_SHIFT	(PCI_REG_BUS_SHIFT + PCIEX_BDF_OFFSET_DELTA)
60d4476ccbSschwartz 
617c478bd9Sstevel@tonic-gate #define	SUCCESS	0
627c478bd9Sstevel@tonic-gate 
63c0da6274SZhi-Jun Robin Fu extern uint64_t mcfg_mem_base;
64671e12f2SRobert Mustacchi extern uint_t pci_iocfg_max_offset;
657c478bd9Sstevel@tonic-gate int pcitool_debug = 0;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * Offsets of BARS in config space.  First entry of 0 means config space.
697c478bd9Sstevel@tonic-gate  * Entries here correlate to pcitool_bars_t enumerated type.
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate static uint8_t pci_bars[] = {
727c478bd9Sstevel@tonic-gate 	0x0,
737c478bd9Sstevel@tonic-gate 	PCI_CONF_BASE0,
747c478bd9Sstevel@tonic-gate 	PCI_CONF_BASE1,
757c478bd9Sstevel@tonic-gate 	PCI_CONF_BASE2,
767c478bd9Sstevel@tonic-gate 	PCI_CONF_BASE3,
777c478bd9Sstevel@tonic-gate 	PCI_CONF_BASE4,
787c478bd9Sstevel@tonic-gate 	PCI_CONF_BASE5,
797c478bd9Sstevel@tonic-gate 	PCI_CONF_ROM
807c478bd9Sstevel@tonic-gate };
817c478bd9Sstevel@tonic-gate 
82d4476ccbSschwartz /* Max offset allowed into config space for a particular device. */
83d4476ccbSschwartz static uint64_t max_cfg_size = PCI_CONF_HDR_SIZE;
84d4476ccbSschwartz 
857c478bd9Sstevel@tonic-gate static uint64_t pcitool_swap_endian(uint64_t data, int size);
86c0da6274SZhi-Jun Robin Fu static int pcitool_cfg_access(pcitool_reg_t *prg, boolean_t write_flag,
87c0da6274SZhi-Jun Robin Fu     boolean_t io_access);
88c0da6274SZhi-Jun Robin Fu static int pcitool_io_access(pcitool_reg_t *prg, boolean_t write_flag);
89c0da6274SZhi-Jun Robin Fu static int pcitool_mem_access(pcitool_reg_t *prg, uint64_t virt_addr,
90d4476ccbSschwartz     boolean_t write_flag);
917c478bd9Sstevel@tonic-gate static uint64_t pcitool_map(uint64_t phys_addr, size_t size, size_t *num_pages);
927c478bd9Sstevel@tonic-gate static void pcitool_unmap(uint64_t virt_addr, size_t num_pages);
937c478bd9Sstevel@tonic-gate 
942917a9c9Sschwartz /* Extern declarations */
957a364d25Sschwartz extern int	(*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *,
967a364d25Sschwartz 		    psm_intr_op_t, int *);
977a364d25Sschwartz 
987851eb82Sschwartz int
pcitool_init(dev_info_t * dip,boolean_t is_pciex)99d4476ccbSschwartz pcitool_init(dev_info_t *dip, boolean_t is_pciex)
1007851eb82Sschwartz {
1017851eb82Sschwartz 	int instance = ddi_get_instance(dip);
1027851eb82Sschwartz 
1037851eb82Sschwartz 	/* Create pcitool nodes for register access and interrupt routing. */
1047851eb82Sschwartz 
1057851eb82Sschwartz 	if (ddi_create_minor_node(dip, PCI_MINOR_REG, S_IFCHR,
10626947304SEvan Yan 	    PCI_MINOR_NUM(instance, PCI_TOOL_REG_MINOR_NUM),
1077851eb82Sschwartz 	    DDI_NT_REGACC, 0) != DDI_SUCCESS) {
1087851eb82Sschwartz 		return (DDI_FAILURE);
1097851eb82Sschwartz 	}
1107851eb82Sschwartz 
1117851eb82Sschwartz 	if (ddi_create_minor_node(dip, PCI_MINOR_INTR, S_IFCHR,
11226947304SEvan Yan 	    PCI_MINOR_NUM(instance, PCI_TOOL_INTR_MINOR_NUM),
1137851eb82Sschwartz 	    DDI_NT_INTRCTL, 0) != DDI_SUCCESS) {
1147851eb82Sschwartz 		ddi_remove_minor_node(dip, PCI_MINOR_REG);
1157851eb82Sschwartz 		return (DDI_FAILURE);
1167851eb82Sschwartz 	}
1177851eb82Sschwartz 
118d4476ccbSschwartz 	if (is_pciex)
119d4476ccbSschwartz 		max_cfg_size = PCIE_CONF_HDR_SIZE;
120d4476ccbSschwartz 
1217851eb82Sschwartz 	return (DDI_SUCCESS);
1227851eb82Sschwartz }
1237851eb82Sschwartz 
1247851eb82Sschwartz void
pcitool_uninit(dev_info_t * dip)1257851eb82Sschwartz pcitool_uninit(dev_info_t *dip)
1267851eb82Sschwartz {
1277851eb82Sschwartz 	ddi_remove_minor_node(dip, PCI_MINOR_INTR);
1287851eb82Sschwartz 	ddi_remove_minor_node(dip, PCI_MINOR_REG);
1297851eb82Sschwartz }
1307851eb82Sschwartz 
1317a364d25Sschwartz /*ARGSUSED*/
1327a364d25Sschwartz static int
pcitool_set_intr(dev_info_t * dip,void * arg,int mode)1337a364d25Sschwartz pcitool_set_intr(dev_info_t *dip, void *arg, int mode)
1347a364d25Sschwartz {
1357a364d25Sschwartz 	ddi_intr_handle_impl_t info_hdl;
1367a364d25Sschwartz 	pcitool_intr_set_t iset;
1377a364d25Sschwartz 	uint32_t old_cpu;
1387a364d25Sschwartz 	int ret, result;
1392917a9c9Sschwartz 	size_t copyinout_size;
1407a364d25Sschwartz 	int rval = SUCCESS;
1417ff178cdSJimmy Vetayases 	apic_get_type_t type_info;
1427a364d25Sschwartz 
1432917a9c9Sschwartz 	/* Version 1 of pcitool_intr_set_t doesn't have flags. */
1442917a9c9Sschwartz 	copyinout_size = (size_t)&iset.flags - (size_t)&iset;
1452917a9c9Sschwartz 
1462917a9c9Sschwartz 	if (ddi_copyin(arg, &iset, copyinout_size, mode) != DDI_SUCCESS)
1477a364d25Sschwartz 		return (EFAULT);
1487a364d25Sschwartz 
1492917a9c9Sschwartz 	switch (iset.user_version) {
1502917a9c9Sschwartz 	case PCITOOL_V1:
1512917a9c9Sschwartz 		break;
1522917a9c9Sschwartz 
1532917a9c9Sschwartz 	case PCITOOL_V2:
1542917a9c9Sschwartz 		copyinout_size = sizeof (pcitool_intr_set_t);
1552917a9c9Sschwartz 		if (ddi_copyin(arg, &iset, copyinout_size, mode) != DDI_SUCCESS)
1562917a9c9Sschwartz 			return (EFAULT);
1572917a9c9Sschwartz 		break;
1582917a9c9Sschwartz 
1592917a9c9Sschwartz 	default:
1602917a9c9Sschwartz 		iset.status = PCITOOL_OUT_OF_RANGE;
1612917a9c9Sschwartz 		rval = ENOTSUP;
1622917a9c9Sschwartz 		goto done_set_intr;
1632917a9c9Sschwartz 	}
1642917a9c9Sschwartz 
16509b1eac2SEvan Yan 	if (iset.flags & PCITOOL_INTR_FLAG_SET_MSI) {
16609b1eac2SEvan Yan 		rval = ENOTSUP;
16709b1eac2SEvan Yan 		iset.status = PCITOOL_IO_ERROR;
16809b1eac2SEvan Yan 		goto done_set_intr;
16909b1eac2SEvan Yan 	}
17009b1eac2SEvan Yan 
1717ff178cdSJimmy Vetayases 	info_hdl.ih_private = &type_info;
1727ff178cdSJimmy Vetayases 
1737ff178cdSJimmy Vetayases 	if ((*psm_intr_ops)(NULL, &info_hdl,
1747ff178cdSJimmy Vetayases 	    PSM_INTR_OP_APIC_TYPE, NULL) != PSM_SUCCESS) {
1757ff178cdSJimmy Vetayases 		rval = ENOTSUP;
1767ff178cdSJimmy Vetayases 		iset.status = PCITOOL_IO_ERROR;
1777a364d25Sschwartz 		goto done_set_intr;
1787a364d25Sschwartz 	}
1797a364d25Sschwartz 
1807ff178cdSJimmy Vetayases 	if (strcmp(type_info.avgi_type, APIC_APIX_NAME) == 0) {
1817ff178cdSJimmy Vetayases 		if (iset.old_cpu > type_info.avgi_num_cpu) {
1827ff178cdSJimmy Vetayases 			rval = EINVAL;
1837ff178cdSJimmy Vetayases 			iset.status = PCITOOL_INVALID_CPUID;
1847ff178cdSJimmy Vetayases 			goto done_set_intr;
1857ff178cdSJimmy Vetayases 		}
1867ff178cdSJimmy Vetayases 		old_cpu = iset.old_cpu;
1877ff178cdSJimmy Vetayases 	} else {
1887ff178cdSJimmy Vetayases 		if ((old_cpu =
1897ff178cdSJimmy Vetayases 		    pci_get_cpu_from_vecirq(iset.ino, IS_VEC)) == -1) {
1907ff178cdSJimmy Vetayases 			iset.status = PCITOOL_IO_ERROR;
1917ff178cdSJimmy Vetayases 			rval = EINVAL;
1927ff178cdSJimmy Vetayases 			goto done_set_intr;
1937ff178cdSJimmy Vetayases 		}
1947ff178cdSJimmy Vetayases 	}
1957a364d25Sschwartz 
1967ff178cdSJimmy Vetayases 	if (iset.ino > type_info.avgi_num_intr) {
1977a364d25Sschwartz 		rval = EINVAL;
1987ff178cdSJimmy Vetayases 		iset.status = PCITOOL_INVALID_INO;
1997a364d25Sschwartz 		goto done_set_intr;
2007a364d25Sschwartz 	}
2017a364d25Sschwartz 
2027ff178cdSJimmy Vetayases 	iset.status = PCITOOL_SUCCESS;
2032917a9c9Sschwartz 
2047a364d25Sschwartz 	old_cpu &= ~PSMGI_CPU_USER_BOUND;
2057a364d25Sschwartz 
2067a364d25Sschwartz 	/*
2077a364d25Sschwartz 	 * For this locally-declared and used handle, ih_private will contain a
2087a364d25Sschwartz 	 * CPU value, not an ihdl_plat_t as used for global interrupt handling.
2097a364d25Sschwartz 	 */
2107ff178cdSJimmy Vetayases 	if (strcmp(type_info.avgi_type, APIC_APIX_NAME) == 0) {
2117ff178cdSJimmy Vetayases 		info_hdl.ih_vector = APIX_VIRTVECTOR(old_cpu, iset.ino);
2127ff178cdSJimmy Vetayases 	} else {
2137ff178cdSJimmy Vetayases 		info_hdl.ih_vector = iset.ino;
2147ff178cdSJimmy Vetayases 	}
2157a364d25Sschwartz 	info_hdl.ih_private = (void *)(uintptr_t)iset.cpu_id;
21609b1eac2SEvan Yan 	info_hdl.ih_flags = PSMGI_INTRBY_VEC;
2172917a9c9Sschwartz 	if (pcitool_debug)
2182917a9c9Sschwartz 		prom_printf("user version:%d, flags:0x%x\n",
2192917a9c9Sschwartz 		    iset.user_version, iset.flags);
2202917a9c9Sschwartz 
2212917a9c9Sschwartz 	result = ENOTSUP;
2222917a9c9Sschwartz 	if ((iset.user_version >= PCITOOL_V2) &&
22309b1eac2SEvan Yan 	    (iset.flags & PCITOOL_INTR_FLAG_SET_GROUP)) {
2242917a9c9Sschwartz 		ret = (*psm_intr_ops)(NULL, &info_hdl, PSM_INTR_OP_GRP_SET_CPU,
2252917a9c9Sschwartz 		    &result);
2262917a9c9Sschwartz 	} else {
2272917a9c9Sschwartz 		ret = (*psm_intr_ops)(NULL, &info_hdl, PSM_INTR_OP_SET_CPU,
2282917a9c9Sschwartz 		    &result);
2292917a9c9Sschwartz 	}
2307a364d25Sschwartz 
2317a364d25Sschwartz 	if (ret != PSM_SUCCESS) {
2327a364d25Sschwartz 		switch (result) {
2337a364d25Sschwartz 		case EIO:		/* Error making the change */
2347a364d25Sschwartz 			rval = EIO;
2357a364d25Sschwartz 			iset.status = PCITOOL_IO_ERROR;
2367a364d25Sschwartz 			break;
2377a364d25Sschwartz 		case ENXIO:		/* Couldn't convert vector to irq */
2387a364d25Sschwartz 			rval = EINVAL;
2397a364d25Sschwartz 			iset.status = PCITOOL_INVALID_INO;
2407a364d25Sschwartz 			break;
2417a364d25Sschwartz 		case EINVAL:		/* CPU out of range */
2427a364d25Sschwartz 			rval = EINVAL;
2437a364d25Sschwartz 			iset.status = PCITOOL_INVALID_CPUID;
2447a364d25Sschwartz 			break;
2452917a9c9Sschwartz 		case ENOTSUP:		/* Requested PSM intr ops missing */
2462917a9c9Sschwartz 			rval = ENOTSUP;
2472917a9c9Sschwartz 			iset.status = PCITOOL_IO_ERROR;
2482917a9c9Sschwartz 			break;
2497a364d25Sschwartz 		}
2507a364d25Sschwartz 	}
2517a364d25Sschwartz 
2527a364d25Sschwartz 	/* Return original CPU. */
2537a364d25Sschwartz 	iset.cpu_id = old_cpu;
2547a364d25Sschwartz 
2557ff178cdSJimmy Vetayases 	/* Return new vector */
2567ff178cdSJimmy Vetayases 	if (strcmp(type_info.avgi_type, APIC_APIX_NAME) == 0) {
2577ff178cdSJimmy Vetayases 		iset.ino = APIX_VIRTVEC_VECTOR(info_hdl.ih_vector);
2587ff178cdSJimmy Vetayases 	}
2597ff178cdSJimmy Vetayases 
2607a364d25Sschwartz done_set_intr:
2612917a9c9Sschwartz 	iset.drvr_version = PCITOOL_VERSION;
2622917a9c9Sschwartz 	if (ddi_copyout(&iset, arg, copyinout_size, mode) != DDI_SUCCESS)
2637a364d25Sschwartz 		rval = EFAULT;
2647a364d25Sschwartz 	return (rval);
2657a364d25Sschwartz }
2667a364d25Sschwartz 
2677a364d25Sschwartz 
2687a364d25Sschwartz /* It is assumed that dip != NULL */
2697a364d25Sschwartz static void
pcitool_get_intr_dev_info(dev_info_t * dip,pcitool_intr_dev_t * devs)2707a364d25Sschwartz pcitool_get_intr_dev_info(dev_info_t *dip, pcitool_intr_dev_t *devs)
2717a364d25Sschwartz {
2727a364d25Sschwartz 	(void) strncpy(devs->driver_name,
273d5ace945SErwin T Tsaur 	    ddi_driver_name(dip), MAXMODCONFNAME-2);
274d5ace945SErwin T Tsaur 	devs->driver_name[MAXMODCONFNAME-1] = '\0';
2757a364d25Sschwartz 	(void) ddi_pathname(dip, devs->path);
2767a364d25Sschwartz 	devs->dev_inst = ddi_get_instance(dip);
2777a364d25Sschwartz }
2787a364d25Sschwartz 
2797a364d25Sschwartz static int
pcitool_get_intr(dev_info_t * dip,void * arg,int mode)2807a364d25Sschwartz pcitool_get_intr(dev_info_t *dip, void *arg, int mode)
2817a364d25Sschwartz {
2827a364d25Sschwartz 	/* Array part isn't used here, but oh well... */
2837a364d25Sschwartz 	pcitool_intr_get_t partial_iget;
2847a364d25Sschwartz 	pcitool_intr_get_t *iget = &partial_iget;
2857a364d25Sschwartz 	size_t	iget_kmem_alloc_size = 0;
286584b574aSToomas Soome 	uint8_t num_devs_ret = 0;
2877a364d25Sschwartz 	int copyout_rval;
2887a364d25Sschwartz 	int rval = SUCCESS;
2897a364d25Sschwartz 	int i;
2907a364d25Sschwartz 	ddi_intr_handle_impl_t info_hdl;
2917a364d25Sschwartz 	apic_get_intr_t intr_info;
2927ff178cdSJimmy Vetayases 	apic_get_type_t type_info;
2937a364d25Sschwartz 
2947a364d25Sschwartz 	/* Read in just the header part, no array section. */
2957a364d25Sschwartz 	if (ddi_copyin(arg, &partial_iget, PCITOOL_IGET_SIZE(0), mode) !=
2967a364d25Sschwartz 	    DDI_SUCCESS)
2977a364d25Sschwartz 		return (EFAULT);
2987a364d25Sschwartz 
29909b1eac2SEvan Yan 	if (partial_iget.flags & PCITOOL_INTR_FLAG_GET_MSI) {
30009b1eac2SEvan Yan 		partial_iget.status = PCITOOL_IO_ERROR;
30109b1eac2SEvan Yan 		partial_iget.num_devs_ret = 0;
30209b1eac2SEvan Yan 		rval = ENOTSUP;
30309b1eac2SEvan Yan 		goto done_get_intr;
30409b1eac2SEvan Yan 	}
30509b1eac2SEvan Yan 
3067ff178cdSJimmy Vetayases 	info_hdl.ih_private = &type_info;
3077ff178cdSJimmy Vetayases 
3087ff178cdSJimmy Vetayases 	if ((*psm_intr_ops)(NULL, &info_hdl,
3097ff178cdSJimmy Vetayases 	    PSM_INTR_OP_APIC_TYPE, NULL) != PSM_SUCCESS) {
3107ff178cdSJimmy Vetayases 		iget->status = PCITOOL_IO_ERROR;
3117ff178cdSJimmy Vetayases 		iget->num_devs_ret = 0;
3127ff178cdSJimmy Vetayases 		rval = EINVAL;
3137ff178cdSJimmy Vetayases 		goto done_get_intr;
3147ff178cdSJimmy Vetayases 	}
3157ff178cdSJimmy Vetayases 
3167ff178cdSJimmy Vetayases 	if (strcmp(type_info.avgi_type, APIC_APIX_NAME) == 0) {
3177ff178cdSJimmy Vetayases 		if (partial_iget.cpu_id > type_info.avgi_num_cpu) {
3187ff178cdSJimmy Vetayases 			partial_iget.status = PCITOOL_INVALID_CPUID;
3197ff178cdSJimmy Vetayases 			partial_iget.num_devs_ret = 0;
3207ff178cdSJimmy Vetayases 			rval = EINVAL;
3217ff178cdSJimmy Vetayases 			goto done_get_intr;
3227ff178cdSJimmy Vetayases 		}
3237ff178cdSJimmy Vetayases 	}
3247ff178cdSJimmy Vetayases 
3257a364d25Sschwartz 	/* Validate argument. */
3267ff178cdSJimmy Vetayases 	if ((partial_iget.ino & APIX_VIRTVEC_VECMASK) >
3277ff178cdSJimmy Vetayases 	    type_info.avgi_num_intr) {
3287a364d25Sschwartz 		partial_iget.status = PCITOOL_INVALID_INO;
3297a364d25Sschwartz 		partial_iget.num_devs_ret = 0;
3307a364d25Sschwartz 		rval = EINVAL;
3317a364d25Sschwartz 		goto done_get_intr;
3327a364d25Sschwartz 	}
3337a364d25Sschwartz 
3347a364d25Sschwartz 	num_devs_ret = partial_iget.num_devs_ret;
3357a364d25Sschwartz 	intr_info.avgi_dip_list = NULL;
3367a364d25Sschwartz 	intr_info.avgi_req_flags =
3377a364d25Sschwartz 	    PSMGI_REQ_CPUID | PSMGI_REQ_NUM_DEVS | PSMGI_INTRBY_VEC;
3387a364d25Sschwartz 	/*
3397a364d25Sschwartz 	 * For this locally-declared and used handle, ih_private will contain a
3407a364d25Sschwartz 	 * pointer to apic_get_intr_t, not an ihdl_plat_t as used for
3417a364d25Sschwartz 	 * global interrupt handling.
3427a364d25Sschwartz 	 */
3437a364d25Sschwartz 	info_hdl.ih_private = &intr_info;
3447ff178cdSJimmy Vetayases 
3457ff178cdSJimmy Vetayases 	if (strcmp(type_info.avgi_type, APIC_APIX_NAME) == 0) {
3467ff178cdSJimmy Vetayases 		info_hdl.ih_vector =
3477ff178cdSJimmy Vetayases 		    APIX_VIRTVECTOR(partial_iget.cpu_id, partial_iget.ino);
3487ff178cdSJimmy Vetayases 	} else {
3497ff178cdSJimmy Vetayases 		info_hdl.ih_vector = partial_iget.ino;
3507ff178cdSJimmy Vetayases 	}
3517a364d25Sschwartz 
3527a364d25Sschwartz 	/* Caller wants device information returned. */
3537a364d25Sschwartz 	if (num_devs_ret > 0) {
3547a364d25Sschwartz 
3557a364d25Sschwartz 		intr_info.avgi_req_flags |= PSMGI_REQ_GET_DEVS;
3567a364d25Sschwartz 
3577a364d25Sschwartz 		/*
3587a364d25Sschwartz 		 * Allocate room.
3597a364d25Sschwartz 		 * If num_devs_ret == 0 iget remains pointing to partial_iget.
3607a364d25Sschwartz 		 */
3617a364d25Sschwartz 		iget_kmem_alloc_size = PCITOOL_IGET_SIZE(num_devs_ret);
3627a364d25Sschwartz 		iget = kmem_alloc(iget_kmem_alloc_size, KM_SLEEP);
3637a364d25Sschwartz 
3647a364d25Sschwartz 		/* Read in whole structure to verify there's room. */
3657a364d25Sschwartz 		if (ddi_copyin(arg, iget, iget_kmem_alloc_size, mode) !=
3667a364d25Sschwartz 		    SUCCESS) {
3677a364d25Sschwartz 
3687a364d25Sschwartz 			/* Be consistent and just return EFAULT here. */
3697a364d25Sschwartz 			kmem_free(iget, iget_kmem_alloc_size);
3707a364d25Sschwartz 
3717a364d25Sschwartz 			return (EFAULT);
3727a364d25Sschwartz 		}
3737a364d25Sschwartz 	}
3747a364d25Sschwartz 
3757a364d25Sschwartz 	bzero(iget, PCITOOL_IGET_SIZE(num_devs_ret));
3767a364d25Sschwartz 	iget->ino = info_hdl.ih_vector;
3777a364d25Sschwartz 
3787a364d25Sschwartz 	/*
3797a364d25Sschwartz 	 * Lock device tree branch from the pci root nexus on down if info will
3807a364d25Sschwartz 	 * be extracted from dips returned from the tree.
3817a364d25Sschwartz 	 */
3827a364d25Sschwartz 	if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) {
383*3fe80ca4SDan Cross 		ndi_devi_enter(dip);
3847a364d25Sschwartz 	}
3857a364d25Sschwartz 
3867a364d25Sschwartz 	/* Call psm_intr_ops(PSM_INTR_OP_GET_INTR) to get information. */
3877a364d25Sschwartz 	if ((rval = (*psm_intr_ops)(NULL, &info_hdl,
3887a364d25Sschwartz 	    PSM_INTR_OP_GET_INTR, NULL)) != PSM_SUCCESS) {
3897a364d25Sschwartz 		iget->status = PCITOOL_IO_ERROR;
3907a364d25Sschwartz 		iget->num_devs_ret = 0;
3917a364d25Sschwartz 		rval = EINVAL;
3927a364d25Sschwartz 		goto done_get_intr;
3937a364d25Sschwartz 	}
3947a364d25Sschwartz 
3957a364d25Sschwartz 	/*
3967a364d25Sschwartz 	 * Fill in the pcitool_intr_get_t to be returned,
3977a364d25Sschwartz 	 * with the CPU, num_devs_ret and num_devs.
3987a364d25Sschwartz 	 */
3994e30c628SEvan Yan 	if (intr_info.avgi_cpu_id == IRQ_UNBOUND ||
4004e30c628SEvan Yan 	    intr_info.avgi_cpu_id == IRQ_UNINIT)
4014e30c628SEvan Yan 		iget->cpu_id = 0;
4024e30c628SEvan Yan 	else
4034e30c628SEvan Yan 		iget->cpu_id = intr_info.avgi_cpu_id & ~PSMGI_CPU_USER_BOUND;
4047a364d25Sschwartz 
4057a364d25Sschwartz 	/* Number of devices returned by apic. */
4067a364d25Sschwartz 	iget->num_devs = intr_info.avgi_num_devs;
4077a364d25Sschwartz 
4087a364d25Sschwartz 	/* Device info was returned. */
4097a364d25Sschwartz 	if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) {
4107a364d25Sschwartz 
4117a364d25Sschwartz 		/*
4127a364d25Sschwartz 		 * num devs returned is num devs ret by apic,
4137a364d25Sschwartz 		 * space permitting.
4147a364d25Sschwartz 		 */
4157a364d25Sschwartz 		iget->num_devs_ret = min(num_devs_ret, intr_info.avgi_num_devs);
4167a364d25Sschwartz 
4177a364d25Sschwartz 		/*
4187a364d25Sschwartz 		 * Loop thru list of dips and extract driver, name and instance.
4197a364d25Sschwartz 		 * Fill in the pcitool_intr_dev_t's with this info.
4207a364d25Sschwartz 		 */
4217a364d25Sschwartz 		for (i = 0; i < iget->num_devs_ret; i++)
4227a364d25Sschwartz 			pcitool_get_intr_dev_info(intr_info.avgi_dip_list[i],
4237a364d25Sschwartz 			    &iget->dev[i]);
4247a364d25Sschwartz 
4257a364d25Sschwartz 		/* Free kmem_alloc'ed memory of the apic_get_intr_t */
4267a364d25Sschwartz 		kmem_free(intr_info.avgi_dip_list,
4277a364d25Sschwartz 		    intr_info.avgi_num_devs * sizeof (dev_info_t *));
4287a364d25Sschwartz 	}
4297a364d25Sschwartz 
4307a364d25Sschwartz done_get_intr:
4317a364d25Sschwartz 
4327a364d25Sschwartz 	if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) {
433*3fe80ca4SDan Cross 		ndi_devi_exit(dip);
4347a364d25Sschwartz 	}
4357a364d25Sschwartz 
4362917a9c9Sschwartz 	iget->drvr_version = PCITOOL_VERSION;
4377a364d25Sschwartz 	copyout_rval = ddi_copyout(iget, arg,
4387a364d25Sschwartz 	    PCITOOL_IGET_SIZE(num_devs_ret), mode);
4397a364d25Sschwartz 
4407a364d25Sschwartz 	if (iget_kmem_alloc_size > 0)
4417a364d25Sschwartz 		kmem_free(iget, iget_kmem_alloc_size);
4427a364d25Sschwartz 
4437a364d25Sschwartz 	if (copyout_rval != DDI_SUCCESS)
4447a364d25Sschwartz 		rval = EFAULT;
4457a364d25Sschwartz 
4467a364d25Sschwartz 	return (rval);
4477a364d25Sschwartz }
4487a364d25Sschwartz 
4492917a9c9Sschwartz /*ARGSUSED*/
4502917a9c9Sschwartz static int
pcitool_intr_info(dev_info_t * dip,void * arg,int mode)4512917a9c9Sschwartz pcitool_intr_info(dev_info_t *dip, void *arg, int mode)
4522917a9c9Sschwartz {
4532917a9c9Sschwartz 	pcitool_intr_info_t intr_info;
4542917a9c9Sschwartz 	ddi_intr_handle_impl_t info_hdl;
4552917a9c9Sschwartz 	int rval = SUCCESS;
4567ff178cdSJimmy Vetayases 	apic_get_type_t type_info;
4572917a9c9Sschwartz 
4582917a9c9Sschwartz 	/* If we need user_version, and to ret same user version as passed in */
4592917a9c9Sschwartz 	if (ddi_copyin(arg, &intr_info, sizeof (pcitool_intr_info_t), mode) !=
4602917a9c9Sschwartz 	    DDI_SUCCESS) {
4612917a9c9Sschwartz 		if (pcitool_debug)
4622917a9c9Sschwartz 			prom_printf("Error reading arguments\n");
4632917a9c9Sschwartz 		return (EFAULT);
4642917a9c9Sschwartz 	}
4652917a9c9Sschwartz 
46609b1eac2SEvan Yan 	if (intr_info.flags & PCITOOL_INTR_FLAG_GET_MSI)
46709b1eac2SEvan Yan 		return (ENOTSUP);
46809b1eac2SEvan Yan 
4697ff178cdSJimmy Vetayases 	info_hdl.ih_private = &type_info;
4707ff178cdSJimmy Vetayases 
4712917a9c9Sschwartz 	/* For UPPC systems, psm_intr_ops has no entry for APIC_TYPE. */
4722917a9c9Sschwartz 	if ((rval = (*psm_intr_ops)(NULL, &info_hdl,
4732917a9c9Sschwartz 	    PSM_INTR_OP_APIC_TYPE, NULL)) != PSM_SUCCESS) {
4742917a9c9Sschwartz 		intr_info.ctlr_type = PCITOOL_CTLR_TYPE_UPPC;
4752917a9c9Sschwartz 		intr_info.ctlr_version = 0;
4767ff178cdSJimmy Vetayases 		intr_info.num_intr = APIC_MAX_VECTOR;
4772917a9c9Sschwartz 	} else {
4782917a9c9Sschwartz 		intr_info.ctlr_version = (uint32_t)info_hdl.ih_ver;
4797ff178cdSJimmy Vetayases 		intr_info.num_cpu = type_info.avgi_num_cpu;
4807ff178cdSJimmy Vetayases 		if (strcmp(type_info.avgi_type,
4817ff178cdSJimmy Vetayases 		    APIC_PCPLUSMP_NAME) == 0) {
4822917a9c9Sschwartz 			intr_info.ctlr_type = PCITOOL_CTLR_TYPE_PCPLUSMP;
4837ff178cdSJimmy Vetayases 			intr_info.num_intr = type_info.avgi_num_intr;
4847ff178cdSJimmy Vetayases 		} else if (strcmp(type_info.avgi_type,
4857ff178cdSJimmy Vetayases 		    APIC_APIX_NAME) == 0) {
4867ff178cdSJimmy Vetayases 			intr_info.ctlr_type = PCITOOL_CTLR_TYPE_APIX;
4877ff178cdSJimmy Vetayases 			intr_info.num_intr = type_info.avgi_num_intr;
4887ff178cdSJimmy Vetayases 		} else {
4892917a9c9Sschwartz 			intr_info.ctlr_type = PCITOOL_CTLR_TYPE_UNKNOWN;
4907ff178cdSJimmy Vetayases 			intr_info.num_intr = APIC_MAX_VECTOR;
4917ff178cdSJimmy Vetayases 		}
4922917a9c9Sschwartz 	}
4932917a9c9Sschwartz 
4942917a9c9Sschwartz 	intr_info.drvr_version = PCITOOL_VERSION;
4952917a9c9Sschwartz 	if (ddi_copyout(&intr_info, arg, sizeof (pcitool_intr_info_t), mode) !=
4962917a9c9Sschwartz 	    DDI_SUCCESS) {
4972917a9c9Sschwartz 		if (pcitool_debug)
4982917a9c9Sschwartz 			prom_printf("Error returning arguments.\n");
4992917a9c9Sschwartz 		rval = EFAULT;
5002917a9c9Sschwartz 	}
5012917a9c9Sschwartz 
5022917a9c9Sschwartz 	return (rval);
5032917a9c9Sschwartz }
5042917a9c9Sschwartz 
5052917a9c9Sschwartz 
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate /*
5087c478bd9Sstevel@tonic-gate  * Main function for handling interrupt CPU binding requests and queries.
5097c478bd9Sstevel@tonic-gate  * Need to implement later
5107c478bd9Sstevel@tonic-gate  */
5117c478bd9Sstevel@tonic-gate int
pcitool_intr_admn(dev_info_t * dip,void * arg,int cmd,int mode)512d4476ccbSschwartz pcitool_intr_admn(dev_info_t *dip, void *arg, int cmd, int mode)
5137c478bd9Sstevel@tonic-gate {
5147a364d25Sschwartz 	int rval;
5157a364d25Sschwartz 
5167a364d25Sschwartz 	switch (cmd) {
5177a364d25Sschwartz 
5187a364d25Sschwartz 	/* Associate a new CPU with a given vector */
5197a364d25Sschwartz 	case PCITOOL_DEVICE_SET_INTR:
5207a364d25Sschwartz 		rval = pcitool_set_intr(dip, arg, mode);
5217a364d25Sschwartz 		break;
5227a364d25Sschwartz 
5237a364d25Sschwartz 	case PCITOOL_DEVICE_GET_INTR:
5247a364d25Sschwartz 		rval = pcitool_get_intr(dip, arg, mode);
5257a364d25Sschwartz 		break;
5267a364d25Sschwartz 
5272917a9c9Sschwartz 	case PCITOOL_SYSTEM_INTR_INFO:
5282917a9c9Sschwartz 		rval = pcitool_intr_info(dip, arg, mode);
5297a364d25Sschwartz 		break;
5307a364d25Sschwartz 
5317a364d25Sschwartz 	default:
5327a364d25Sschwartz 		rval = ENOTSUP;
5337a364d25Sschwartz 	}
5347a364d25Sschwartz 
5357a364d25Sschwartz 	return (rval);
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate /*
5397c478bd9Sstevel@tonic-gate  * Perform register accesses on the nexus device itself.
5407c478bd9Sstevel@tonic-gate  * No explicit PCI nexus device for X86, so not applicable.
5417c478bd9Sstevel@tonic-gate  */
5427a364d25Sschwartz 
5437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5447c478bd9Sstevel@tonic-gate int
pcitool_bus_reg_ops(dev_info_t * dip,void * arg,int cmd,int mode)545d4476ccbSschwartz pcitool_bus_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode)
5467c478bd9Sstevel@tonic-gate {
5477c478bd9Sstevel@tonic-gate 	return (ENOTSUP);
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate /* Swap endianness. */
5517c478bd9Sstevel@tonic-gate static uint64_t
pcitool_swap_endian(uint64_t data,int size)5527c478bd9Sstevel@tonic-gate pcitool_swap_endian(uint64_t data, int size)
5537c478bd9Sstevel@tonic-gate {
5547c478bd9Sstevel@tonic-gate 	typedef union {
5557c478bd9Sstevel@tonic-gate 		uint64_t data64;
5567c478bd9Sstevel@tonic-gate 		uint8_t data8[8];
5577c478bd9Sstevel@tonic-gate 	} data_split_t;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	data_split_t orig_data;
5607c478bd9Sstevel@tonic-gate 	data_split_t returned_data;
5617c478bd9Sstevel@tonic-gate 	int i;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	orig_data.data64 = data;
5647c478bd9Sstevel@tonic-gate 	returned_data.data64 = 0;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	for (i = 0; i < size; i++) {
5677c478bd9Sstevel@tonic-gate 		returned_data.data8[i] = orig_data.data8[size - 1 - i];
5687c478bd9Sstevel@tonic-gate 	}
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	return (returned_data.data64);
5717c478bd9Sstevel@tonic-gate }
5727c478bd9Sstevel@tonic-gate 
573d4476ccbSschwartz /*
574c0da6274SZhi-Jun Robin Fu  * A note about ontrap handling:
575d4476ccbSschwartz  *
576c0da6274SZhi-Jun Robin Fu  * X86 systems on which this module was tested return FFs instead of bus errors
577c0da6274SZhi-Jun Robin Fu  * when accessing devices with invalid addresses.  Ontrap handling, which
578c0da6274SZhi-Jun Robin Fu  * gracefully handles kernel bus errors, is installed anyway for I/O and mem
579c0da6274SZhi-Jun Robin Fu  * space accessing (not for pci config space), in case future X86 platforms
580c0da6274SZhi-Jun Robin Fu  * require it.
581d4476ccbSschwartz  */
582d4476ccbSschwartz 
5837c478bd9Sstevel@tonic-gate /* Access device.  prg is modified. */
5847c478bd9Sstevel@tonic-gate static int
pcitool_cfg_access(pcitool_reg_t * prg,boolean_t write_flag,boolean_t io_access)585c0da6274SZhi-Jun Robin Fu pcitool_cfg_access(pcitool_reg_t *prg, boolean_t write_flag,
586c0da6274SZhi-Jun Robin Fu     boolean_t io_access)
5877c478bd9Sstevel@tonic-gate {
5887c478bd9Sstevel@tonic-gate 	int size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
5897c478bd9Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
5907c478bd9Sstevel@tonic-gate 	int rval = SUCCESS;
5917c478bd9Sstevel@tonic-gate 	uint64_t local_data;
592c0da6274SZhi-Jun Robin Fu 	pci_cfgacc_req_t req;
593c0da6274SZhi-Jun Robin Fu 	uint32_t max_offset;
594c0da6274SZhi-Jun Robin Fu 
595de710d24SJosef 'Jeff' Sipek 	if ((size <= 0) || (size > 8) || !ISP2(size)) {
596c0da6274SZhi-Jun Robin Fu 		prg->status = PCITOOL_INVALID_SIZE;
597c0da6274SZhi-Jun Robin Fu 		return (ENOTSUP);
598c0da6274SZhi-Jun Robin Fu 	}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	/*
6015c59319bSDan Mick 	 * NOTE: there is no way to verify whether or not the address is
6025c59319bSDan Mick 	 * valid other than that it is within the maximum offset.  The
603c0da6274SZhi-Jun Robin Fu 	 * put functions return void and the get functions return -1 on error.
6047c478bd9Sstevel@tonic-gate 	 */
6055c59319bSDan Mick 
606c0da6274SZhi-Jun Robin Fu 	if (io_access)
607671e12f2SRobert Mustacchi 		max_offset = pci_iocfg_max_offset;
608c0da6274SZhi-Jun Robin Fu 	else
609c0da6274SZhi-Jun Robin Fu 		max_offset = 0xFFF;
610c0da6274SZhi-Jun Robin Fu 	if (prg->offset + size - 1 > max_offset) {
6115c59319bSDan Mick 		prg->status = PCITOOL_INVALID_ADDRESS;
6125c59319bSDan Mick 		return (ENOTSUP);
6135c59319bSDan Mick 	}
6145c59319bSDan Mick 
6157c478bd9Sstevel@tonic-gate 	prg->status = PCITOOL_SUCCESS;
6167c478bd9Sstevel@tonic-gate 
617c0da6274SZhi-Jun Robin Fu 	req.rcdip = NULL;
618c0da6274SZhi-Jun Robin Fu 	req.bdf = PCI_GETBDF(prg->bus_no, prg->dev_no, prg->func_no);
619c0da6274SZhi-Jun Robin Fu 	req.offset = prg->offset;
620c0da6274SZhi-Jun Robin Fu 	req.size = size;
621c0da6274SZhi-Jun Robin Fu 	req.write = write_flag;
622c0da6274SZhi-Jun Robin Fu 	req.ioacc = io_access;
6237c478bd9Sstevel@tonic-gate 	if (write_flag) {
6247c478bd9Sstevel@tonic-gate 		if (big_endian) {
6257c478bd9Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
6267c478bd9Sstevel@tonic-gate 		} else {
6277c478bd9Sstevel@tonic-gate 			local_data = prg->data;
6287c478bd9Sstevel@tonic-gate 		}
629c0da6274SZhi-Jun Robin Fu 		VAL64(&req) = local_data;
630c0da6274SZhi-Jun Robin Fu 		pci_cfgacc_acc(&req);
6317c478bd9Sstevel@tonic-gate 	} else {
632c0da6274SZhi-Jun Robin Fu 		pci_cfgacc_acc(&req);
6338d7fafffSZhi-Jun Robin Fu 		switch (size) {
6348d7fafffSZhi-Jun Robin Fu 		case 1:
6358d7fafffSZhi-Jun Robin Fu 			local_data = VAL8(&req);
6368d7fafffSZhi-Jun Robin Fu 			break;
6378d7fafffSZhi-Jun Robin Fu 		case 2:
6388d7fafffSZhi-Jun Robin Fu 			local_data = VAL16(&req);
6398d7fafffSZhi-Jun Robin Fu 			break;
6408d7fafffSZhi-Jun Robin Fu 		case 4:
6418d7fafffSZhi-Jun Robin Fu 			local_data = VAL32(&req);
6428d7fafffSZhi-Jun Robin Fu 			break;
6438d7fafffSZhi-Jun Robin Fu 		case 8:
6448d7fafffSZhi-Jun Robin Fu 			local_data = VAL64(&req);
6458d7fafffSZhi-Jun Robin Fu 			break;
646584b574aSToomas Soome 		default:
647584b574aSToomas Soome 			prg->status = PCITOOL_INVALID_ADDRESS;
648584b574aSToomas Soome 			return (ENOTSUP);
6498d7fafffSZhi-Jun Robin Fu 		}
650c0da6274SZhi-Jun Robin Fu 		if (big_endian) {
651c0da6274SZhi-Jun Robin Fu 			prg->data =
652c0da6274SZhi-Jun Robin Fu 			    pcitool_swap_endian(local_data, size);
653c0da6274SZhi-Jun Robin Fu 		} else {
654c0da6274SZhi-Jun Robin Fu 			prg->data = local_data;
6557c478bd9Sstevel@tonic-gate 		}
656c0da6274SZhi-Jun Robin Fu 	}
657c0da6274SZhi-Jun Robin Fu 	/*
658671e12f2SRobert Mustacchi 	 * Check if legacy I/O config access is used, in which case the valid
659671e12f2SRobert Mustacchi 	 * range varies with the I/O space mechanism used.
660c0da6274SZhi-Jun Robin Fu 	 */
661671e12f2SRobert Mustacchi 	if (req.ioacc && (prg->offset + size - 1 > pci_iocfg_max_offset)) {
662c0da6274SZhi-Jun Robin Fu 		prg->status = PCITOOL_INVALID_ADDRESS;
663c0da6274SZhi-Jun Robin Fu 		return (ENOTSUP);
664c0da6274SZhi-Jun Robin Fu 	}
6657c478bd9Sstevel@tonic-gate 
666c0da6274SZhi-Jun Robin Fu 	/* Set phys_addr only if MMIO is used */
667c0da6274SZhi-Jun Robin Fu 	prg->phys_addr = 0;
668c0da6274SZhi-Jun Robin Fu 	if (!req.ioacc && mcfg_mem_base != 0) {
669c0da6274SZhi-Jun Robin Fu 		prg->phys_addr = mcfg_mem_base + prg->offset +
670c0da6274SZhi-Jun Robin Fu 		    ((prg->bus_no << PCIEX_REG_BUS_SHIFT) |
671c0da6274SZhi-Jun Robin Fu 		    (prg->dev_no << PCIEX_REG_DEV_SHIFT) |
672c0da6274SZhi-Jun Robin Fu 		    (prg->func_no << PCIEX_REG_FUNC_SHIFT));
6737c478bd9Sstevel@tonic-gate 	}
674c0da6274SZhi-Jun Robin Fu 
6757c478bd9Sstevel@tonic-gate 	return (rval);
6767c478bd9Sstevel@tonic-gate }
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate static int
pcitool_io_access(pcitool_reg_t * prg,boolean_t write_flag)679c0da6274SZhi-Jun Robin Fu pcitool_io_access(pcitool_reg_t *prg, boolean_t write_flag)
6807c478bd9Sstevel@tonic-gate {
6817c478bd9Sstevel@tonic-gate 	int port = (int)prg->phys_addr;
6827c478bd9Sstevel@tonic-gate 	size_t size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
6837c478bd9Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
6841ee1bcbaSToomas Soome 	volatile int rval = SUCCESS;
6857c478bd9Sstevel@tonic-gate 	on_trap_data_t otd;
6861ee1bcbaSToomas Soome 	volatile uint64_t local_data;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	/*
6907c478bd9Sstevel@tonic-gate 	 * on_trap works like setjmp.
6917c478bd9Sstevel@tonic-gate 	 *
6927c478bd9Sstevel@tonic-gate 	 * A non-zero return here means on_trap has returned from an error.
6937c478bd9Sstevel@tonic-gate 	 *
6947c478bd9Sstevel@tonic-gate 	 * A zero return here means that on_trap has just returned from setup.
6957c478bd9Sstevel@tonic-gate 	 */
6967c478bd9Sstevel@tonic-gate 	if (on_trap(&otd, OT_DATA_ACCESS)) {
6977c478bd9Sstevel@tonic-gate 		no_trap();
6987c478bd9Sstevel@tonic-gate 		if (pcitool_debug)
6997c478bd9Sstevel@tonic-gate 			prom_printf(
7002917a9c9Sschwartz 			    "pcitool_io_access: on_trap caught an error...\n");
7017c478bd9Sstevel@tonic-gate 		prg->status = PCITOOL_INVALID_ADDRESS;
7027c478bd9Sstevel@tonic-gate 		return (EFAULT);
7037c478bd9Sstevel@tonic-gate 	}
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	if (write_flag) {
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 		if (big_endian) {
7087c478bd9Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
7097c478bd9Sstevel@tonic-gate 		} else {
7107c478bd9Sstevel@tonic-gate 			local_data = prg->data;
7117c478bd9Sstevel@tonic-gate 		}
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 		if (pcitool_debug)
7147c478bd9Sstevel@tonic-gate 			prom_printf("Writing %ld byte(s) to port 0x%x\n",
7157c478bd9Sstevel@tonic-gate 			    size, port);
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 		switch (size) {
7187c478bd9Sstevel@tonic-gate 		case 1:
7197c478bd9Sstevel@tonic-gate 			outb(port, (uint8_t)local_data);
7207c478bd9Sstevel@tonic-gate 			break;
7217c478bd9Sstevel@tonic-gate 		case 2:
7227c478bd9Sstevel@tonic-gate 			outw(port, (uint16_t)local_data);
7237c478bd9Sstevel@tonic-gate 			break;
7247c478bd9Sstevel@tonic-gate 		case 4:
7257c478bd9Sstevel@tonic-gate 			outl(port, (uint32_t)local_data);
7267c478bd9Sstevel@tonic-gate 			break;
7277c478bd9Sstevel@tonic-gate 		default:
7287c478bd9Sstevel@tonic-gate 			rval = ENOTSUP;
7297c478bd9Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
7307c478bd9Sstevel@tonic-gate 			break;
7317c478bd9Sstevel@tonic-gate 		}
7327c478bd9Sstevel@tonic-gate 	} else {
7337c478bd9Sstevel@tonic-gate 		if (pcitool_debug)
7347c478bd9Sstevel@tonic-gate 			prom_printf("Reading %ld byte(s) from port 0x%x\n",
7357c478bd9Sstevel@tonic-gate 			    size, port);
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 		switch (size) {
7387c478bd9Sstevel@tonic-gate 		case 1:
7397c478bd9Sstevel@tonic-gate 			local_data = inb(port);
7407c478bd9Sstevel@tonic-gate 			break;
7417c478bd9Sstevel@tonic-gate 		case 2:
7427c478bd9Sstevel@tonic-gate 			local_data = inw(port);
7437c478bd9Sstevel@tonic-gate 			break;
7447c478bd9Sstevel@tonic-gate 		case 4:
7457c478bd9Sstevel@tonic-gate 			local_data = inl(port);
7467c478bd9Sstevel@tonic-gate 			break;
7477c478bd9Sstevel@tonic-gate 		default:
7487c478bd9Sstevel@tonic-gate 			rval = ENOTSUP;
7497c478bd9Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
7507c478bd9Sstevel@tonic-gate 			break;
7517c478bd9Sstevel@tonic-gate 		}
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 		if (rval == SUCCESS) {
7547c478bd9Sstevel@tonic-gate 			if (big_endian) {
7557c478bd9Sstevel@tonic-gate 				prg->data =
7567c478bd9Sstevel@tonic-gate 				    pcitool_swap_endian(local_data, size);
7577c478bd9Sstevel@tonic-gate 			} else {
7587c478bd9Sstevel@tonic-gate 				prg->data = local_data;
7597c478bd9Sstevel@tonic-gate 			}
7607c478bd9Sstevel@tonic-gate 		}
7617c478bd9Sstevel@tonic-gate 	}
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	no_trap();
7647c478bd9Sstevel@tonic-gate 	return (rval);
7657c478bd9Sstevel@tonic-gate }
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate static int
pcitool_mem_access(pcitool_reg_t * prg,uint64_t virt_addr,boolean_t write_flag)768c0da6274SZhi-Jun Robin Fu pcitool_mem_access(pcitool_reg_t *prg, uint64_t virt_addr, boolean_t write_flag)
7697c478bd9Sstevel@tonic-gate {
7707c478bd9Sstevel@tonic-gate 	size_t size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
7717c478bd9Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
7721ee1bcbaSToomas Soome 	volatile int rval = DDI_SUCCESS;
7737c478bd9Sstevel@tonic-gate 	on_trap_data_t otd;
7741ee1bcbaSToomas Soome 	volatile uint64_t local_data;
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	/*
7777c478bd9Sstevel@tonic-gate 	 * on_trap works like setjmp.
7787c478bd9Sstevel@tonic-gate 	 *
7797c478bd9Sstevel@tonic-gate 	 * A non-zero return here means on_trap has returned from an error.
7807c478bd9Sstevel@tonic-gate 	 *
7817c478bd9Sstevel@tonic-gate 	 * A zero return here means that on_trap has just returned from setup.
7827c478bd9Sstevel@tonic-gate 	 */
7837c478bd9Sstevel@tonic-gate 	if (on_trap(&otd, OT_DATA_ACCESS)) {
7847c478bd9Sstevel@tonic-gate 		no_trap();
7857c478bd9Sstevel@tonic-gate 		if (pcitool_debug)
7867c478bd9Sstevel@tonic-gate 			prom_printf(
7877c478bd9Sstevel@tonic-gate 			    "pcitool_mem_access: on_trap caught an error...\n");
7887c478bd9Sstevel@tonic-gate 		prg->status = PCITOOL_INVALID_ADDRESS;
7897c478bd9Sstevel@tonic-gate 		return (EFAULT);
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	if (write_flag) {
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 		if (big_endian) {
7957c478bd9Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
7967c478bd9Sstevel@tonic-gate 		} else {
7977c478bd9Sstevel@tonic-gate 			local_data = prg->data;
7987c478bd9Sstevel@tonic-gate 		}
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 		switch (size) {
8017c478bd9Sstevel@tonic-gate 		case 1:
8027c478bd9Sstevel@tonic-gate 			*((uint8_t *)(uintptr_t)virt_addr) = local_data;
8037c478bd9Sstevel@tonic-gate 			break;
8047c478bd9Sstevel@tonic-gate 		case 2:
8057c478bd9Sstevel@tonic-gate 			*((uint16_t *)(uintptr_t)virt_addr) = local_data;
8067c478bd9Sstevel@tonic-gate 			break;
8077c478bd9Sstevel@tonic-gate 		case 4:
8087c478bd9Sstevel@tonic-gate 			*((uint32_t *)(uintptr_t)virt_addr) = local_data;
8097c478bd9Sstevel@tonic-gate 			break;
8107c478bd9Sstevel@tonic-gate 		case 8:
8117c478bd9Sstevel@tonic-gate 			*((uint64_t *)(uintptr_t)virt_addr) = local_data;
8127c478bd9Sstevel@tonic-gate 			break;
8137c478bd9Sstevel@tonic-gate 		default:
8147c478bd9Sstevel@tonic-gate 			rval = ENOTSUP;
8157c478bd9Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
8167c478bd9Sstevel@tonic-gate 			break;
8177c478bd9Sstevel@tonic-gate 		}
8187c478bd9Sstevel@tonic-gate 	} else {
8197c478bd9Sstevel@tonic-gate 		switch (size) {
8207c478bd9Sstevel@tonic-gate 		case 1:
8217c478bd9Sstevel@tonic-gate 			local_data = *((uint8_t *)(uintptr_t)virt_addr);
8227c478bd9Sstevel@tonic-gate 			break;
8237c478bd9Sstevel@tonic-gate 		case 2:
8247c478bd9Sstevel@tonic-gate 			local_data = *((uint16_t *)(uintptr_t)virt_addr);
8257c478bd9Sstevel@tonic-gate 			break;
8267c478bd9Sstevel@tonic-gate 		case 4:
8277c478bd9Sstevel@tonic-gate 			local_data = *((uint32_t *)(uintptr_t)virt_addr);
8287c478bd9Sstevel@tonic-gate 			break;
8297c478bd9Sstevel@tonic-gate 		case 8:
8307c478bd9Sstevel@tonic-gate 			local_data = *((uint64_t *)(uintptr_t)virt_addr);
8317c478bd9Sstevel@tonic-gate 			break;
8327c478bd9Sstevel@tonic-gate 		default:
8337c478bd9Sstevel@tonic-gate 			rval = ENOTSUP;
8347c478bd9Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
8357c478bd9Sstevel@tonic-gate 			break;
8367c478bd9Sstevel@tonic-gate 		}
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 		if (rval == SUCCESS) {
8397c478bd9Sstevel@tonic-gate 			if (big_endian) {
8407c478bd9Sstevel@tonic-gate 				prg->data =
8417c478bd9Sstevel@tonic-gate 				    pcitool_swap_endian(local_data, size);
8427c478bd9Sstevel@tonic-gate 			} else {
8437c478bd9Sstevel@tonic-gate 				prg->data = local_data;
8447c478bd9Sstevel@tonic-gate 			}
8457c478bd9Sstevel@tonic-gate 		}
8467c478bd9Sstevel@tonic-gate 	}
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	no_trap();
8497c478bd9Sstevel@tonic-gate 	return (rval);
8507c478bd9Sstevel@tonic-gate }
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate /*
8537c478bd9Sstevel@tonic-gate  * Map up to 2 pages which contain the address we want to access.
8547c478bd9Sstevel@tonic-gate  *
8557c478bd9Sstevel@tonic-gate  * Mapping should span no more than 8 bytes.  With X86 it is possible for an
8567c478bd9Sstevel@tonic-gate  * 8 byte value to start on a 4 byte boundary, so it can cross a page boundary.
8577c478bd9Sstevel@tonic-gate  * We'll never have to map more than two pages.
8587c478bd9Sstevel@tonic-gate  */
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate static uint64_t
pcitool_map(uint64_t phys_addr,size_t size,size_t * num_pages)8617c478bd9Sstevel@tonic-gate pcitool_map(uint64_t phys_addr, size_t size, size_t *num_pages)
8627c478bd9Sstevel@tonic-gate {
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	uint64_t page_base = phys_addr & ~MMU_PAGEOFFSET;
8657c478bd9Sstevel@tonic-gate 	uint64_t offset = phys_addr & MMU_PAGEOFFSET;
8667c478bd9Sstevel@tonic-gate 	void *virt_base;
8677c478bd9Sstevel@tonic-gate 	uint64_t returned_addr;
868ae115bc7Smrj 	pfn_t pfn;
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	if (pcitool_debug)
8717c478bd9Sstevel@tonic-gate 		prom_printf("pcitool_map: Called with PA:0x%p\n",
8728793b36bSNick Todd 		    (void *)(uintptr_t)phys_addr);
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	*num_pages = 1;
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	/* Desired mapping would span more than two pages. */
8777c478bd9Sstevel@tonic-gate 	if ((offset + size) > (MMU_PAGESIZE * 2)) {
8787c478bd9Sstevel@tonic-gate 		if (pcitool_debug)
8797c478bd9Sstevel@tonic-gate 			prom_printf("boundary violation: "
880d4476ccbSschwartz 			    "offset:0x%" PRIx64 ", size:%ld, pagesize:0x%lx\n",
881d4476ccbSschwartz 			    offset, (uintptr_t)size, (uintptr_t)MMU_PAGESIZE);
88249ee6bfbSToomas Soome 		return (0);
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	} else if ((offset + size) > MMU_PAGESIZE) {
8857c478bd9Sstevel@tonic-gate 		(*num_pages)++;
8867c478bd9Sstevel@tonic-gate 	}
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	/* Get page(s) of virtual space. */
8897c478bd9Sstevel@tonic-gate 	virt_base = vmem_alloc(heap_arena, ptob(*num_pages), VM_NOSLEEP);
8907c478bd9Sstevel@tonic-gate 	if (virt_base == NULL) {
8917c478bd9Sstevel@tonic-gate 		if (pcitool_debug)
8927c478bd9Sstevel@tonic-gate 			prom_printf("Couldn't get virtual base address.\n");
89349ee6bfbSToomas Soome 		return (0);
8947c478bd9Sstevel@tonic-gate 	}
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	if (pcitool_debug)
8977c478bd9Sstevel@tonic-gate 		prom_printf("Got base virtual address:0x%p\n", virt_base);
8987c478bd9Sstevel@tonic-gate 
899843e1988Sjohnlev #ifdef __xpv
900843e1988Sjohnlev 	/*
901843e1988Sjohnlev 	 * We should only get here if we are dom0.
902843e1988Sjohnlev 	 * We're using a real device so we need to translate the MA to a PFN.
903843e1988Sjohnlev 	 */
904843e1988Sjohnlev 	ASSERT(DOMAIN_IS_INITDOMAIN(xen_info));
905843e1988Sjohnlev 	pfn = xen_assign_pfn(mmu_btop(page_base));
906843e1988Sjohnlev #else
907ae115bc7Smrj 	pfn = btop(page_base);
908843e1988Sjohnlev #endif
909ae115bc7Smrj 
9107c478bd9Sstevel@tonic-gate 	/* Now map the allocated virtual space to the physical address. */
911ae115bc7Smrj 	hat_devload(kas.a_hat, virt_base, mmu_ptob(*num_pages), pfn,
912ae115bc7Smrj 	    PROT_READ | PROT_WRITE | HAT_STRICTORDER,
9137c478bd9Sstevel@tonic-gate 	    HAT_LOAD_LOCK);
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	returned_addr = ((uintptr_t)(virt_base)) + offset;
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	if (pcitool_debug)
9187c478bd9Sstevel@tonic-gate 		prom_printf("pcitool_map: returning VA:0x%p\n",
9197c478bd9Sstevel@tonic-gate 		    (void *)(uintptr_t)returned_addr);
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	return (returned_addr);
9227c478bd9Sstevel@tonic-gate }
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate /* Unmap the mapped page(s). */
9257c478bd9Sstevel@tonic-gate static void
pcitool_unmap(uint64_t virt_addr,size_t num_pages)9267c478bd9Sstevel@tonic-gate pcitool_unmap(uint64_t virt_addr, size_t num_pages)
9277c478bd9Sstevel@tonic-gate {
9287c478bd9Sstevel@tonic-gate 	void *base_virt_addr = (void *)(uintptr_t)(virt_addr & ~MMU_PAGEOFFSET);
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	hat_unload(kas.a_hat, base_virt_addr, ptob(num_pages),
9317c478bd9Sstevel@tonic-gate 	    HAT_UNLOAD_UNLOCK);
9327c478bd9Sstevel@tonic-gate 	vmem_free(heap_arena, base_virt_addr, ptob(num_pages));
9337c478bd9Sstevel@tonic-gate }
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate /* Perform register accesses on PCI leaf devices. */
937c0da6274SZhi-Jun Robin Fu /*ARGSUSED*/
9387c478bd9Sstevel@tonic-gate int
pcitool_dev_reg_ops(dev_info_t * dip,void * arg,int cmd,int mode)939d4476ccbSschwartz pcitool_dev_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode)
9407c478bd9Sstevel@tonic-gate {
9417c478bd9Sstevel@tonic-gate 	boolean_t	write_flag = B_FALSE;
942c0da6274SZhi-Jun Robin Fu 	boolean_t	io_access = B_TRUE;
9437c478bd9Sstevel@tonic-gate 	int		rval = 0;
9447c478bd9Sstevel@tonic-gate 	pcitool_reg_t	prg;
9457c478bd9Sstevel@tonic-gate 	uint8_t		size;
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	uint64_t	base_addr;
9487c478bd9Sstevel@tonic-gate 	uint64_t	virt_addr;
9497c478bd9Sstevel@tonic-gate 	size_t		num_virt_pages;
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	switch (cmd) {
9527c478bd9Sstevel@tonic-gate 	case (PCITOOL_DEVICE_SET_REG):
9537c478bd9Sstevel@tonic-gate 		write_flag = B_TRUE;
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	/*FALLTHRU*/
9567c478bd9Sstevel@tonic-gate 	case (PCITOOL_DEVICE_GET_REG):
9577c478bd9Sstevel@tonic-gate 		if (pcitool_debug)
9587c478bd9Sstevel@tonic-gate 			prom_printf("pci_dev_reg_ops set/get reg\n");
9597c478bd9Sstevel@tonic-gate 		if (ddi_copyin(arg, &prg, sizeof (pcitool_reg_t), mode) !=
9607c478bd9Sstevel@tonic-gate 		    DDI_SUCCESS) {
9617c478bd9Sstevel@tonic-gate 			if (pcitool_debug)
9627c478bd9Sstevel@tonic-gate 				prom_printf("Error reading arguments\n");
9637c478bd9Sstevel@tonic-gate 			return (EFAULT);
9647c478bd9Sstevel@tonic-gate 		}
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 		if (prg.barnum >= (sizeof (pci_bars) / sizeof (pci_bars[0]))) {
9677c478bd9Sstevel@tonic-gate 			prg.status = PCITOOL_OUT_OF_RANGE;
9687c478bd9Sstevel@tonic-gate 			rval = EINVAL;
9697c478bd9Sstevel@tonic-gate 			goto done_reg;
9707c478bd9Sstevel@tonic-gate 		}
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 		if (pcitool_debug)
9737c478bd9Sstevel@tonic-gate 			prom_printf("raw bus:0x%x, dev:0x%x, func:0x%x\n",
9747c478bd9Sstevel@tonic-gate 			    prg.bus_no, prg.dev_no, prg.func_no);
9757c478bd9Sstevel@tonic-gate 		/* Validate address arguments of bus / dev / func */
9767c478bd9Sstevel@tonic-gate 		if (((prg.bus_no &
9777c478bd9Sstevel@tonic-gate 		    (PCI_REG_BUS_M >> PCI_REG_BUS_SHIFT)) !=
9787c478bd9Sstevel@tonic-gate 		    prg.bus_no) ||
9797c478bd9Sstevel@tonic-gate 		    ((prg.dev_no &
9807c478bd9Sstevel@tonic-gate 		    (PCI_REG_DEV_M >> PCI_REG_DEV_SHIFT)) !=
9817c478bd9Sstevel@tonic-gate 		    prg.dev_no) ||
9827c478bd9Sstevel@tonic-gate 		    ((prg.func_no &
9837c478bd9Sstevel@tonic-gate 		    (PCI_REG_FUNC_M >> PCI_REG_FUNC_SHIFT)) !=
9847c478bd9Sstevel@tonic-gate 		    prg.func_no)) {
9857c478bd9Sstevel@tonic-gate 			prg.status = PCITOOL_INVALID_ADDRESS;
9867c478bd9Sstevel@tonic-gate 			rval = EINVAL;
9877c478bd9Sstevel@tonic-gate 			goto done_reg;
9887c478bd9Sstevel@tonic-gate 		}
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 		size = PCITOOL_ACC_ATTR_SIZE(prg.acc_attr);
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 		/* Proper config space desired. */
9937c478bd9Sstevel@tonic-gate 		if (prg.barnum == 0) {
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 			if (pcitool_debug)
9967c478bd9Sstevel@tonic-gate 				prom_printf(
9977c478bd9Sstevel@tonic-gate 				    "config access: offset:0x%" PRIx64 ", "
9987c478bd9Sstevel@tonic-gate 				    "phys_addr:0x%" PRIx64 "\n",
9997c478bd9Sstevel@tonic-gate 				    prg.offset, prg.phys_addr);
1000d4476ccbSschwartz 
1001d4476ccbSschwartz 			if (prg.offset >= max_cfg_size) {
1002d4476ccbSschwartz 				prg.status = PCITOOL_OUT_OF_RANGE;
1003d4476ccbSschwartz 				rval = EINVAL;
1004d4476ccbSschwartz 				goto done_reg;
1005d4476ccbSschwartz 			}
1006c0da6274SZhi-Jun Robin Fu 			if (max_cfg_size == PCIE_CONF_HDR_SIZE)
1007c0da6274SZhi-Jun Robin Fu 				io_access = B_FALSE;
1008d4476ccbSschwartz 
1009c0da6274SZhi-Jun Robin Fu 			rval = pcitool_cfg_access(&prg, write_flag, io_access);
10107c478bd9Sstevel@tonic-gate 			if (pcitool_debug)
10117c478bd9Sstevel@tonic-gate 				prom_printf(
10127c478bd9Sstevel@tonic-gate 				    "config access: data:0x%" PRIx64 "\n",
10137c478bd9Sstevel@tonic-gate 				    prg.data);
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 		/* IO/ MEM/ MEM64 space. */
10167c478bd9Sstevel@tonic-gate 		} else {
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 			pcitool_reg_t	prg2;
10197c478bd9Sstevel@tonic-gate 			bcopy(&prg, &prg2, sizeof (pcitool_reg_t));
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 			/*
10227c478bd9Sstevel@tonic-gate 			 * Translate BAR number into offset of the BAR in
10237c478bd9Sstevel@tonic-gate 			 * the device's config space.
10247c478bd9Sstevel@tonic-gate 			 */
10257c478bd9Sstevel@tonic-gate 			prg2.offset = pci_bars[prg2.barnum];
10267c478bd9Sstevel@tonic-gate 			prg2.acc_attr =
10277c478bd9Sstevel@tonic-gate 			    PCITOOL_ACC_ATTR_SIZE_4 | PCITOOL_ACC_ATTR_ENDN_LTL;
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 			if (pcitool_debug)
10307c478bd9Sstevel@tonic-gate 				prom_printf(
10317c478bd9Sstevel@tonic-gate 				    "barnum:%d, bar_offset:0x%" PRIx64 "\n",
10327c478bd9Sstevel@tonic-gate 				    prg2.barnum, prg2.offset);
10337c478bd9Sstevel@tonic-gate 			/*
10347c478bd9Sstevel@tonic-gate 			 * Get Bus Address Register (BAR) from config space.
10357c478bd9Sstevel@tonic-gate 			 * prg2.offset is the offset into config space of the
10367c478bd9Sstevel@tonic-gate 			 * BAR desired.  prg.status is modified on error.
10377c478bd9Sstevel@tonic-gate 			 */
1038c0da6274SZhi-Jun Robin Fu 			rval = pcitool_cfg_access(&prg2, B_FALSE, B_TRUE);
10397c478bd9Sstevel@tonic-gate 			if (rval != SUCCESS) {
10407c478bd9Sstevel@tonic-gate 				if (pcitool_debug)
10417c478bd9Sstevel@tonic-gate 					prom_printf("BAR access failed\n");
10427c478bd9Sstevel@tonic-gate 				prg.status = prg2.status;
10437c478bd9Sstevel@tonic-gate 				goto done_reg;
10447c478bd9Sstevel@tonic-gate 			}
10457c478bd9Sstevel@tonic-gate 			/*
10467c478bd9Sstevel@tonic-gate 			 * Reference proper PCI space based on the BAR.
10477c478bd9Sstevel@tonic-gate 			 * If 64 bit MEM space, need to load other half of the
10487c478bd9Sstevel@tonic-gate 			 * BAR first.
10497c478bd9Sstevel@tonic-gate 			 */
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 			if (pcitool_debug)
10527c478bd9Sstevel@tonic-gate 				prom_printf("bar returned is 0x%" PRIx64 "\n",
10537c478bd9Sstevel@tonic-gate 				    prg2.data);
10547c478bd9Sstevel@tonic-gate 			if (!prg2.data) {
10557c478bd9Sstevel@tonic-gate 				if (pcitool_debug)
10567c478bd9Sstevel@tonic-gate 					prom_printf("BAR data == 0\n");
10577c478bd9Sstevel@tonic-gate 				rval = EINVAL;
10587c478bd9Sstevel@tonic-gate 				prg.status = PCITOOL_INVALID_ADDRESS;
10597c478bd9Sstevel@tonic-gate 				goto done_reg;
10607c478bd9Sstevel@tonic-gate 			}
10617c478bd9Sstevel@tonic-gate 			if (prg2.data == 0xffffffff) {
10627c478bd9Sstevel@tonic-gate 				if (pcitool_debug)
10637c478bd9Sstevel@tonic-gate 					prom_printf("BAR data == -1\n");
10647c478bd9Sstevel@tonic-gate 				rval = EINVAL;
10657c478bd9Sstevel@tonic-gate 				prg.status = PCITOOL_INVALID_ADDRESS;
10667c478bd9Sstevel@tonic-gate 				goto done_reg;
10677c478bd9Sstevel@tonic-gate 			}
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 			/*
10707c478bd9Sstevel@tonic-gate 			 * BAR has bits saying this space is IO space, unless
10717c478bd9Sstevel@tonic-gate 			 * this is the ROM address register.
10727c478bd9Sstevel@tonic-gate 			 */
10737c478bd9Sstevel@tonic-gate 			if (((PCI_BASE_SPACE_M & prg2.data) ==
10747c478bd9Sstevel@tonic-gate 			    PCI_BASE_SPACE_IO) &&
10757c478bd9Sstevel@tonic-gate 			    (prg2.offset != PCI_CONF_ROM)) {
10767c478bd9Sstevel@tonic-gate 				if (pcitool_debug)
10777c478bd9Sstevel@tonic-gate 					prom_printf("IO space\n");
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 				prg2.data &= PCI_BASE_IO_ADDR_M;
10807c478bd9Sstevel@tonic-gate 				prg.phys_addr = prg2.data + prg.offset;
10817c478bd9Sstevel@tonic-gate 
1082c0da6274SZhi-Jun Robin Fu 				rval = pcitool_io_access(&prg, write_flag);
10837c478bd9Sstevel@tonic-gate 				if ((rval != SUCCESS) && (pcitool_debug))
10847c478bd9Sstevel@tonic-gate 					prom_printf("IO access failed\n");
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 				goto done_reg;
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 			/*
10907c478bd9Sstevel@tonic-gate 			 * BAR has bits saying this space is 64 bit memory
10917c478bd9Sstevel@tonic-gate 			 * space, unless this is the ROM address register.
10927c478bd9Sstevel@tonic-gate 			 *
10937c478bd9Sstevel@tonic-gate 			 * The 64 bit address stored in two BAR cells is not
10947c478bd9Sstevel@tonic-gate 			 * necessarily aligned on an 8-byte boundary.
10957c478bd9Sstevel@tonic-gate 			 * Need to keep the first 4 bytes read,
10967c478bd9Sstevel@tonic-gate 			 * and do a separate read of the high 4 bytes.
10977c478bd9Sstevel@tonic-gate 			 */
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 			} else if ((PCI_BASE_TYPE_ALL & prg2.data) &&
11007c478bd9Sstevel@tonic-gate 			    (prg2.offset != PCI_CONF_ROM)) {
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 				uint32_t low_bytes =
11037c478bd9Sstevel@tonic-gate 				    (uint32_t)(prg2.data & ~PCI_BASE_TYPE_ALL);
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 				/*
11067c478bd9Sstevel@tonic-gate 				 * Don't try to read the next 4 bytes
11077c478bd9Sstevel@tonic-gate 				 * past the end of BARs.
11087c478bd9Sstevel@tonic-gate 				 */
11097c478bd9Sstevel@tonic-gate 				if (prg2.offset >= PCI_CONF_BASE5) {
11107c478bd9Sstevel@tonic-gate 					prg.status = PCITOOL_OUT_OF_RANGE;
11117c478bd9Sstevel@tonic-gate 					rval = EIO;
11127c478bd9Sstevel@tonic-gate 					goto done_reg;
11137c478bd9Sstevel@tonic-gate 				}
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 				/*
11167c478bd9Sstevel@tonic-gate 				 * Access device.
11177c478bd9Sstevel@tonic-gate 				 * prg2.status is modified on error.
11187c478bd9Sstevel@tonic-gate 				 */
11197c478bd9Sstevel@tonic-gate 				prg2.offset += 4;
1120c0da6274SZhi-Jun Robin Fu 				rval = pcitool_cfg_access(&prg2,
1121c0da6274SZhi-Jun Robin Fu 				    B_FALSE, B_TRUE);
11227c478bd9Sstevel@tonic-gate 				if (rval != SUCCESS) {
11237c478bd9Sstevel@tonic-gate 					prg.status = prg2.status;
11247c478bd9Sstevel@tonic-gate 					goto done_reg;
11257c478bd9Sstevel@tonic-gate 				}
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 				if (prg2.data == 0xffffffff) {
11287c478bd9Sstevel@tonic-gate 					prg.status = PCITOOL_INVALID_ADDRESS;
11297c478bd9Sstevel@tonic-gate 					prg.status = EFAULT;
11307c478bd9Sstevel@tonic-gate 					goto done_reg;
11317c478bd9Sstevel@tonic-gate 				}
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 				prg2.data = (prg2.data << 32) + low_bytes;
11347c478bd9Sstevel@tonic-gate 				if (pcitool_debug)
11357c478bd9Sstevel@tonic-gate 					prom_printf(
11367c478bd9Sstevel@tonic-gate 					    "64 bit mem space.  "
11377c478bd9Sstevel@tonic-gate 					    "64-bit bar is 0x%" PRIx64 "\n",
11387c478bd9Sstevel@tonic-gate 					    prg2.data);
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 			/* Mem32 space, including ROM */
11417c478bd9Sstevel@tonic-gate 			} else {
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 				if (prg2.offset == PCI_CONF_ROM) {
11447c478bd9Sstevel@tonic-gate 					if (pcitool_debug)
11457c478bd9Sstevel@tonic-gate 						prom_printf(
11467c478bd9Sstevel@tonic-gate 						    "Additional ROM "
11477c478bd9Sstevel@tonic-gate 						    "checking\n");
11487c478bd9Sstevel@tonic-gate 					/* Can't write to ROM */
11497c478bd9Sstevel@tonic-gate 					if (write_flag) {
11507c478bd9Sstevel@tonic-gate 						prg.status = PCITOOL_ROM_WRITE;
11517c478bd9Sstevel@tonic-gate 						rval = EIO;
11527c478bd9Sstevel@tonic-gate 						goto done_reg;
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 					/* ROM disabled for reading */
11557c478bd9Sstevel@tonic-gate 					} else if (!(prg2.data & 0x00000001)) {
11567c478bd9Sstevel@tonic-gate 						prg.status =
11577c478bd9Sstevel@tonic-gate 						    PCITOOL_ROM_DISABLED;
11587c478bd9Sstevel@tonic-gate 						rval = EIO;
11597c478bd9Sstevel@tonic-gate 						goto done_reg;
11607c478bd9Sstevel@tonic-gate 					}
11617c478bd9Sstevel@tonic-gate 				}
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 				if (pcitool_debug)
11647c478bd9Sstevel@tonic-gate 					prom_printf("32 bit mem space\n");
11657c478bd9Sstevel@tonic-gate 			}
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 			/* Common code for all IO/MEM range spaces. */
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 			base_addr = prg2.data;
11707c478bd9Sstevel@tonic-gate 			if (pcitool_debug)
11717c478bd9Sstevel@tonic-gate 				prom_printf(
11727c478bd9Sstevel@tonic-gate 				    "addr portion of bar is 0x%" PRIx64 ", "
11737c478bd9Sstevel@tonic-gate 				    "base=0x%" PRIx64 ", "
11747c478bd9Sstevel@tonic-gate 				    "offset:0x%" PRIx64 "\n",
11757c478bd9Sstevel@tonic-gate 				    prg2.data, base_addr, prg.offset);
11767c478bd9Sstevel@tonic-gate 			/*
11777c478bd9Sstevel@tonic-gate 			 * Use offset provided by caller to index into
11787c478bd9Sstevel@tonic-gate 			 * desired space, then access.
11797c478bd9Sstevel@tonic-gate 			 * Note that prg.status is modified on error.
11807c478bd9Sstevel@tonic-gate 			 */
11817c478bd9Sstevel@tonic-gate 			prg.phys_addr = base_addr + prg.offset;
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 			virt_addr = pcitool_map(prg.phys_addr, size,
11847c478bd9Sstevel@tonic-gate 			    &num_virt_pages);
118549ee6bfbSToomas Soome 			if (virt_addr == 0) {
11867c478bd9Sstevel@tonic-gate 				prg.status = PCITOOL_IO_ERROR;
11877c478bd9Sstevel@tonic-gate 				rval = EIO;
11887c478bd9Sstevel@tonic-gate 				goto done_reg;
11897c478bd9Sstevel@tonic-gate 			}
11907c478bd9Sstevel@tonic-gate 
1191c0da6274SZhi-Jun Robin Fu 			rval = pcitool_mem_access(&prg, virt_addr, write_flag);
11927c478bd9Sstevel@tonic-gate 			pcitool_unmap(virt_addr, num_virt_pages);
11937c478bd9Sstevel@tonic-gate 		}
11947c478bd9Sstevel@tonic-gate done_reg:
11952917a9c9Sschwartz 		prg.drvr_version = PCITOOL_VERSION;
11967c478bd9Sstevel@tonic-gate 		if (ddi_copyout(&prg, arg, sizeof (pcitool_reg_t), mode) !=
11977c478bd9Sstevel@tonic-gate 		    DDI_SUCCESS) {
11987c478bd9Sstevel@tonic-gate 			if (pcitool_debug)
11997c478bd9Sstevel@tonic-gate 				prom_printf("Error returning arguments.\n");
12007c478bd9Sstevel@tonic-gate 			rval = EFAULT;
12017c478bd9Sstevel@tonic-gate 		}
12027c478bd9Sstevel@tonic-gate 		break;
12037c478bd9Sstevel@tonic-gate 	default:
12047c478bd9Sstevel@tonic-gate 		rval = ENOTTY;
12057c478bd9Sstevel@tonic-gate 		break;
12067c478bd9Sstevel@tonic-gate 	}
12077c478bd9Sstevel@tonic-gate 	return (rval);
12087c478bd9Sstevel@tonic-gate }
1209