10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52434Sanish  * Common Development and Distribution License (the "License").
62434Sanish  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
223446Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/mkdev.h>
30117Sschwartz #include <sys/stat.h>
310Sstevel@tonic-gate #include <sys/sunddi.h>
320Sstevel@tonic-gate #include <vm/seg_kmem.h>
330Sstevel@tonic-gate #include <sys/machparam.h>
34916Sschwartz #include <sys/sunndi.h>
350Sstevel@tonic-gate #include <sys/ontrap.h>
36916Sschwartz #include <sys/psm.h>
37881Sjohnny #include <sys/pcie.h>
380Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h>
390Sstevel@tonic-gate #include <sys/pci_cfgspace.h>
400Sstevel@tonic-gate #include <sys/pci_tools.h>
411083Sanish #include <io/pci/pci_tools_ext.h>
423446Smrj #include <sys/apic.h>
43916Sschwartz #include <io/pci/pci_var.h>
440Sstevel@tonic-gate #include <sys/promif.h>
451083Sanish #include <sys/x86_archext.h>
462434Sanish #include <sys/cpuvar.h>
470Sstevel@tonic-gate 
48*5084Sjohnlev #ifdef __xpv
49*5084Sjohnlev #include <sys/hypervisor.h>
50*5084Sjohnlev #endif
51*5084Sjohnlev 
52777Sschwartz #define	PCIEX_BDF_OFFSET_DELTA	4
53777Sschwartz #define	PCIEX_REG_FUNC_SHIFT	(PCI_REG_FUNC_SHIFT + PCIEX_BDF_OFFSET_DELTA)
54777Sschwartz #define	PCIEX_REG_DEV_SHIFT	(PCI_REG_DEV_SHIFT + PCIEX_BDF_OFFSET_DELTA)
55777Sschwartz #define	PCIEX_REG_BUS_SHIFT	(PCI_REG_BUS_SHIFT + PCIEX_BDF_OFFSET_DELTA)
56777Sschwartz 
570Sstevel@tonic-gate #define	SUCCESS	0
580Sstevel@tonic-gate 
590Sstevel@tonic-gate int pcitool_debug = 0;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate  * Offsets of BARS in config space.  First entry of 0 means config space.
630Sstevel@tonic-gate  * Entries here correlate to pcitool_bars_t enumerated type.
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate static uint8_t pci_bars[] = {
660Sstevel@tonic-gate 	0x0,
670Sstevel@tonic-gate 	PCI_CONF_BASE0,
680Sstevel@tonic-gate 	PCI_CONF_BASE1,
690Sstevel@tonic-gate 	PCI_CONF_BASE2,
700Sstevel@tonic-gate 	PCI_CONF_BASE3,
710Sstevel@tonic-gate 	PCI_CONF_BASE4,
720Sstevel@tonic-gate 	PCI_CONF_BASE5,
730Sstevel@tonic-gate 	PCI_CONF_ROM
740Sstevel@tonic-gate };
750Sstevel@tonic-gate 
76777Sschwartz /* Max offset allowed into config space for a particular device. */
77777Sschwartz static uint64_t max_cfg_size = PCI_CONF_HDR_SIZE;
78777Sschwartz 
790Sstevel@tonic-gate static uint64_t pcitool_swap_endian(uint64_t data, int size);
80777Sschwartz static int pcitool_pciex_cfg_access(dev_info_t *dip, pcitool_reg_t *prg,
81777Sschwartz     boolean_t write_flag);
820Sstevel@tonic-gate static int pcitool_cfg_access(dev_info_t *dip, pcitool_reg_t *prg,
830Sstevel@tonic-gate     boolean_t write_flag);
840Sstevel@tonic-gate static int pcitool_io_access(dev_info_t *dip, pcitool_reg_t *prg,
850Sstevel@tonic-gate     boolean_t write_flag);
860Sstevel@tonic-gate static int pcitool_mem_access(dev_info_t *dip, pcitool_reg_t *prg,
870Sstevel@tonic-gate     uint64_t virt_addr, boolean_t write_flag);
880Sstevel@tonic-gate static uint64_t pcitool_map(uint64_t phys_addr, size_t size, size_t *num_pages);
890Sstevel@tonic-gate static void pcitool_unmap(uint64_t virt_addr, size_t num_pages);
900Sstevel@tonic-gate 
914397Sschwartz /* Extern declarations */
92916Sschwartz extern int	(*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *,
93916Sschwartz 		    psm_intr_op_t, int *);
94916Sschwartz 
95117Sschwartz int
96777Sschwartz pcitool_init(dev_info_t *dip, boolean_t is_pciex)
97117Sschwartz {
98117Sschwartz 	int instance = ddi_get_instance(dip);
99117Sschwartz 
100117Sschwartz 	/* Create pcitool nodes for register access and interrupt routing. */
101117Sschwartz 
102117Sschwartz 	if (ddi_create_minor_node(dip, PCI_MINOR_REG, S_IFCHR,
103117Sschwartz 	    PCIHP_AP_MINOR_NUM(instance, PCI_TOOL_REG_MINOR_NUM),
104117Sschwartz 	    DDI_NT_REGACC, 0) != DDI_SUCCESS) {
105117Sschwartz 		return (DDI_FAILURE);
106117Sschwartz 	}
107117Sschwartz 
108117Sschwartz 	if (ddi_create_minor_node(dip, PCI_MINOR_INTR, S_IFCHR,
109117Sschwartz 	    PCIHP_AP_MINOR_NUM(instance, PCI_TOOL_INTR_MINOR_NUM),
110117Sschwartz 	    DDI_NT_INTRCTL, 0) != DDI_SUCCESS) {
111117Sschwartz 		ddi_remove_minor_node(dip, PCI_MINOR_REG);
112117Sschwartz 		return (DDI_FAILURE);
113117Sschwartz 	}
114117Sschwartz 
115777Sschwartz 	if (is_pciex)
116777Sschwartz 		max_cfg_size = PCIE_CONF_HDR_SIZE;
117777Sschwartz 
118117Sschwartz 	return (DDI_SUCCESS);
119117Sschwartz }
120117Sschwartz 
121117Sschwartz void
122117Sschwartz pcitool_uninit(dev_info_t *dip)
123117Sschwartz {
124117Sschwartz 	ddi_remove_minor_node(dip, PCI_MINOR_INTR);
125117Sschwartz 	ddi_remove_minor_node(dip, PCI_MINOR_REG);
126117Sschwartz }
127117Sschwartz 
128916Sschwartz /*ARGSUSED*/
129916Sschwartz static int
130916Sschwartz pcitool_set_intr(dev_info_t *dip, void *arg, int mode)
131916Sschwartz {
132916Sschwartz 	ddi_intr_handle_impl_t info_hdl;
133916Sschwartz 	pcitool_intr_set_t iset;
134916Sschwartz 	uint32_t old_cpu;
135916Sschwartz 	int ret, result;
1364397Sschwartz 	size_t copyinout_size;
137916Sschwartz 	int rval = SUCCESS;
138916Sschwartz 
1394397Sschwartz 	/* Version 1 of pcitool_intr_set_t doesn't have flags. */
1404397Sschwartz 	copyinout_size = (size_t)&iset.flags - (size_t)&iset;
1414397Sschwartz 
1424397Sschwartz 	if (ddi_copyin(arg, &iset, copyinout_size, mode) != DDI_SUCCESS)
143916Sschwartz 		return (EFAULT);
144916Sschwartz 
1454397Sschwartz 	switch (iset.user_version) {
1464397Sschwartz 	case PCITOOL_V1:
1474397Sschwartz 		break;
1484397Sschwartz 
1494397Sschwartz 	case PCITOOL_V2:
1504397Sschwartz 		copyinout_size = sizeof (pcitool_intr_set_t);
1514397Sschwartz 		if (ddi_copyin(arg, &iset, copyinout_size, mode) != DDI_SUCCESS)
1524397Sschwartz 			return (EFAULT);
1534397Sschwartz 		break;
1544397Sschwartz 
1554397Sschwartz 	default:
1564397Sschwartz 		iset.status = PCITOOL_OUT_OF_RANGE;
1574397Sschwartz 		rval = ENOTSUP;
1584397Sschwartz 		goto done_set_intr;
1594397Sschwartz 	}
1604397Sschwartz 
161916Sschwartz 	if (iset.ino > APIC_MAX_VECTOR) {
162916Sschwartz 		rval = EINVAL;
163916Sschwartz 		iset.status = PCITOOL_INVALID_INO;
164916Sschwartz 		goto done_set_intr;
165916Sschwartz 	}
166916Sschwartz 
167916Sschwartz 	iset.status = PCITOOL_SUCCESS;
168916Sschwartz 
169916Sschwartz 	if ((old_cpu = pci_get_cpu_from_vecirq(iset.ino, IS_VEC)) == -1) {
170916Sschwartz 		iset.status = PCITOOL_IO_ERROR;
171916Sschwartz 		rval = EINVAL;
172916Sschwartz 		goto done_set_intr;
173916Sschwartz 	}
174916Sschwartz 
1754397Sschwartz 
176916Sschwartz 	old_cpu &= ~PSMGI_CPU_USER_BOUND;
177916Sschwartz 
178916Sschwartz 	/*
179916Sschwartz 	 * For this locally-declared and used handle, ih_private will contain a
180916Sschwartz 	 * CPU value, not an ihdl_plat_t as used for global interrupt handling.
181916Sschwartz 	 */
182916Sschwartz 	info_hdl.ih_vector = iset.ino;
183916Sschwartz 	info_hdl.ih_private = (void *)(uintptr_t)iset.cpu_id;
1844397Sschwartz 	if (pcitool_debug)
1854397Sschwartz 		prom_printf("user version:%d, flags:0x%x\n",
1864397Sschwartz 		    iset.user_version, iset.flags);
187916Sschwartz 
1884397Sschwartz 	result = ENOTSUP;
1894397Sschwartz 	if ((iset.user_version >= PCITOOL_V2) &&
1904397Sschwartz 	    (iset.flags & PCITOOL_INTR_SET_FLAG_GROUP)) {
1914397Sschwartz 		ret = (*psm_intr_ops)(NULL, &info_hdl, PSM_INTR_OP_GRP_SET_CPU,
1924397Sschwartz 		    &result);
1934397Sschwartz 	} else {
1944397Sschwartz 		ret = (*psm_intr_ops)(NULL, &info_hdl, PSM_INTR_OP_SET_CPU,
1954397Sschwartz 		    &result);
1964397Sschwartz 	}
1974397Sschwartz 
198916Sschwartz 	if (ret != PSM_SUCCESS) {
199916Sschwartz 		switch (result) {
200916Sschwartz 		case EIO:		/* Error making the change */
201916Sschwartz 			rval = EIO;
202916Sschwartz 			iset.status = PCITOOL_IO_ERROR;
203916Sschwartz 			break;
204916Sschwartz 		case ENXIO:		/* Couldn't convert vector to irq */
205916Sschwartz 			rval = EINVAL;
206916Sschwartz 			iset.status = PCITOOL_INVALID_INO;
207916Sschwartz 			break;
208916Sschwartz 		case EINVAL:		/* CPU out of range */
209916Sschwartz 			rval = EINVAL;
210916Sschwartz 			iset.status = PCITOOL_INVALID_CPUID;
211916Sschwartz 			break;
2124397Sschwartz 		case ENOTSUP:		/* Requested PSM intr ops missing */
2134397Sschwartz 			rval = ENOTSUP;
2144397Sschwartz 			iset.status = PCITOOL_IO_ERROR;
2154397Sschwartz 			break;
216916Sschwartz 		}
217916Sschwartz 	}
218916Sschwartz 
219916Sschwartz 	/* Return original CPU. */
220916Sschwartz 	iset.cpu_id = old_cpu;
221916Sschwartz 
222916Sschwartz done_set_intr:
2234397Sschwartz 	iset.drvr_version = PCITOOL_VERSION;
2244397Sschwartz 	if (ddi_copyout(&iset, arg, copyinout_size, mode) != DDI_SUCCESS)
225916Sschwartz 		rval = EFAULT;
226916Sschwartz 	return (rval);
227916Sschwartz }
228916Sschwartz 
229916Sschwartz 
230916Sschwartz /* It is assumed that dip != NULL */
231916Sschwartz static void
232916Sschwartz pcitool_get_intr_dev_info(dev_info_t *dip, pcitool_intr_dev_t *devs)
233916Sschwartz {
234916Sschwartz 	(void) strncpy(devs->driver_name,
235916Sschwartz 	    ddi_driver_name(dip), MAXMODCONFNAME-1);
236916Sschwartz 	devs->driver_name[MAXMODCONFNAME] = '\0';
237916Sschwartz 	(void) ddi_pathname(dip, devs->path);
238916Sschwartz 	devs->dev_inst = ddi_get_instance(dip);
239916Sschwartz }
240916Sschwartz 
241916Sschwartz 
242916Sschwartz /*ARGSUSED*/
243916Sschwartz static int
244916Sschwartz pcitool_get_intr(dev_info_t *dip, void *arg, int mode)
245916Sschwartz {
246916Sschwartz 	/* Array part isn't used here, but oh well... */
247916Sschwartz 	pcitool_intr_get_t partial_iget;
248916Sschwartz 	pcitool_intr_get_t *iget = &partial_iget;
249916Sschwartz 	size_t	iget_kmem_alloc_size = 0;
250916Sschwartz 	uint8_t num_devs_ret;
251916Sschwartz 	int copyout_rval;
252916Sschwartz 	int rval = SUCCESS;
253916Sschwartz 	int circ;
254916Sschwartz 	int i;
255916Sschwartz 
256916Sschwartz 	ddi_intr_handle_impl_t info_hdl;
257916Sschwartz 	apic_get_intr_t intr_info;
258916Sschwartz 
259916Sschwartz 	/* Read in just the header part, no array section. */
260916Sschwartz 	if (ddi_copyin(arg, &partial_iget, PCITOOL_IGET_SIZE(0), mode) !=
261916Sschwartz 	    DDI_SUCCESS)
262916Sschwartz 		return (EFAULT);
263916Sschwartz 
264916Sschwartz 	/* Validate argument. */
265916Sschwartz 	if (partial_iget.ino > APIC_MAX_VECTOR) {
266916Sschwartz 		partial_iget.status = PCITOOL_INVALID_INO;
267916Sschwartz 		partial_iget.num_devs_ret = 0;
268916Sschwartz 		rval = EINVAL;
269916Sschwartz 		goto done_get_intr;
270916Sschwartz 	}
271916Sschwartz 
272916Sschwartz 	num_devs_ret = partial_iget.num_devs_ret;
273916Sschwartz 	intr_info.avgi_dip_list = NULL;
274916Sschwartz 	intr_info.avgi_req_flags =
275916Sschwartz 	    PSMGI_REQ_CPUID | PSMGI_REQ_NUM_DEVS | PSMGI_INTRBY_VEC;
276916Sschwartz 	/*
277916Sschwartz 	 * For this locally-declared and used handle, ih_private will contain a
278916Sschwartz 	 * pointer to apic_get_intr_t, not an ihdl_plat_t as used for
279916Sschwartz 	 * global interrupt handling.
280916Sschwartz 	 */
281916Sschwartz 	info_hdl.ih_private = &intr_info;
282916Sschwartz 	info_hdl.ih_vector = partial_iget.ino;
283916Sschwartz 
284916Sschwartz 	/* Caller wants device information returned. */
285916Sschwartz 	if (num_devs_ret > 0) {
286916Sschwartz 
287916Sschwartz 		intr_info.avgi_req_flags |= PSMGI_REQ_GET_DEVS;
288916Sschwartz 
289916Sschwartz 		/*
290916Sschwartz 		 * Allocate room.
291916Sschwartz 		 * If num_devs_ret == 0 iget remains pointing to partial_iget.
292916Sschwartz 		 */
293916Sschwartz 		iget_kmem_alloc_size = PCITOOL_IGET_SIZE(num_devs_ret);
294916Sschwartz 		iget = kmem_alloc(iget_kmem_alloc_size, KM_SLEEP);
295916Sschwartz 
296916Sschwartz 		/* Read in whole structure to verify there's room. */
297916Sschwartz 		if (ddi_copyin(arg, iget, iget_kmem_alloc_size, mode) !=
298916Sschwartz 		    SUCCESS) {
299916Sschwartz 
300916Sschwartz 			/* Be consistent and just return EFAULT here. */
301916Sschwartz 			kmem_free(iget, iget_kmem_alloc_size);
302916Sschwartz 
303916Sschwartz 			return (EFAULT);
304916Sschwartz 		}
305916Sschwartz 	}
306916Sschwartz 
307916Sschwartz 	bzero(iget, PCITOOL_IGET_SIZE(num_devs_ret));
308916Sschwartz 	iget->ino = info_hdl.ih_vector;
309916Sschwartz 
310916Sschwartz 	/*
311916Sschwartz 	 * Lock device tree branch from the pci root nexus on down if info will
312916Sschwartz 	 * be extracted from dips returned from the tree.
313916Sschwartz 	 */
314916Sschwartz 	if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) {
315916Sschwartz 		ndi_devi_enter(dip, &circ);
316916Sschwartz 	}
317916Sschwartz 
318916Sschwartz 	/* Call psm_intr_ops(PSM_INTR_OP_GET_INTR) to get information. */
319916Sschwartz 	if ((rval = (*psm_intr_ops)(NULL, &info_hdl,
320916Sschwartz 	    PSM_INTR_OP_GET_INTR, NULL)) != PSM_SUCCESS) {
321916Sschwartz 		iget->status = PCITOOL_IO_ERROR;
322916Sschwartz 		iget->num_devs_ret = 0;
323916Sschwartz 		rval = EINVAL;
324916Sschwartz 		goto done_get_intr;
325916Sschwartz 	}
326916Sschwartz 
327916Sschwartz 	/*
328916Sschwartz 	 * Fill in the pcitool_intr_get_t to be returned,
329916Sschwartz 	 * with the CPU, num_devs_ret and num_devs.
330916Sschwartz 	 */
331916Sschwartz 	iget->cpu_id = intr_info.avgi_cpu_id & ~PSMGI_CPU_USER_BOUND;
332916Sschwartz 
333916Sschwartz 	/* Number of devices returned by apic. */
334916Sschwartz 	iget->num_devs = intr_info.avgi_num_devs;
335916Sschwartz 
336916Sschwartz 	/* Device info was returned. */
337916Sschwartz 	if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) {
338916Sschwartz 
339916Sschwartz 		/*
340916Sschwartz 		 * num devs returned is num devs ret by apic,
341916Sschwartz 		 * space permitting.
342916Sschwartz 		 */
343916Sschwartz 		iget->num_devs_ret = min(num_devs_ret, intr_info.avgi_num_devs);
344916Sschwartz 
345916Sschwartz 		/*
346916Sschwartz 		 * Loop thru list of dips and extract driver, name and instance.
347916Sschwartz 		 * Fill in the pcitool_intr_dev_t's with this info.
348916Sschwartz 		 */
349916Sschwartz 		for (i = 0; i < iget->num_devs_ret; i++)
350916Sschwartz 			pcitool_get_intr_dev_info(intr_info.avgi_dip_list[i],
351916Sschwartz 			    &iget->dev[i]);
352916Sschwartz 
353916Sschwartz 		/* Free kmem_alloc'ed memory of the apic_get_intr_t */
354916Sschwartz 		kmem_free(intr_info.avgi_dip_list,
355916Sschwartz 		    intr_info.avgi_num_devs * sizeof (dev_info_t *));
356916Sschwartz 	}
357916Sschwartz 
358916Sschwartz done_get_intr:
359916Sschwartz 
360916Sschwartz 	if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) {
361916Sschwartz 		ndi_devi_exit(dip, circ);
362916Sschwartz 	}
363916Sschwartz 
3644397Sschwartz 	iget->drvr_version = PCITOOL_VERSION;
365916Sschwartz 	copyout_rval = ddi_copyout(iget, arg,
366916Sschwartz 	    PCITOOL_IGET_SIZE(num_devs_ret), mode);
367916Sschwartz 
368916Sschwartz 	if (iget_kmem_alloc_size > 0)
369916Sschwartz 		kmem_free(iget, iget_kmem_alloc_size);
370916Sschwartz 
371916Sschwartz 	if (copyout_rval != DDI_SUCCESS)
372916Sschwartz 		rval = EFAULT;
373916Sschwartz 
374916Sschwartz 	return (rval);
375916Sschwartz }
376916Sschwartz 
3774397Sschwartz /*ARGSUSED*/
3784397Sschwartz static int
3794397Sschwartz pcitool_intr_info(dev_info_t *dip, void *arg, int mode)
3804397Sschwartz {
3814397Sschwartz 	pcitool_intr_info_t intr_info;
3824397Sschwartz 	ddi_intr_handle_impl_t info_hdl;
3834397Sschwartz 	int rval = SUCCESS;
3844397Sschwartz 
3854397Sschwartz 	/* If we need user_version, and to ret same user version as passed in */
3864397Sschwartz 	if (ddi_copyin(arg, &intr_info, sizeof (pcitool_intr_info_t), mode) !=
3874397Sschwartz 	    DDI_SUCCESS) {
3884397Sschwartz 		if (pcitool_debug)
3894397Sschwartz 			prom_printf("Error reading arguments\n");
3904397Sschwartz 		return (EFAULT);
3914397Sschwartz 	}
3924397Sschwartz 
3934397Sschwartz 	/* For UPPC systems, psm_intr_ops has no entry for APIC_TYPE. */
3944397Sschwartz 	if ((rval = (*psm_intr_ops)(NULL, &info_hdl,
3954397Sschwartz 	    PSM_INTR_OP_APIC_TYPE, NULL)) != PSM_SUCCESS) {
3964397Sschwartz 		intr_info.ctlr_type = PCITOOL_CTLR_TYPE_UPPC;
3974397Sschwartz 		intr_info.ctlr_version = 0;
3984397Sschwartz 
3994397Sschwartz 	} else {
4004397Sschwartz 		intr_info.ctlr_version = (uint32_t)info_hdl.ih_ver;
4014397Sschwartz 		if (strcmp((char *)info_hdl.ih_private,
4024397Sschwartz 		    APIC_PCPLUSMP_NAME) == 0)
4034397Sschwartz 			intr_info.ctlr_type = PCITOOL_CTLR_TYPE_PCPLUSMP;
4044397Sschwartz 		else
4054397Sschwartz 			intr_info.ctlr_type = PCITOOL_CTLR_TYPE_UNKNOWN;
4064397Sschwartz 	}
4074397Sschwartz 
4084397Sschwartz 	intr_info.num_intr = APIC_MAX_VECTOR;
4094397Sschwartz 	intr_info.drvr_version = PCITOOL_VERSION;
4104397Sschwartz 	if (ddi_copyout(&intr_info, arg, sizeof (pcitool_intr_info_t), mode) !=
4114397Sschwartz 	    DDI_SUCCESS) {
4124397Sschwartz 		if (pcitool_debug)
4134397Sschwartz 			prom_printf("Error returning arguments.\n");
4144397Sschwartz 		rval = EFAULT;
4154397Sschwartz 	}
4164397Sschwartz 
4174397Sschwartz 	return (rval);
4184397Sschwartz }
4194397Sschwartz 
4204397Sschwartz 
421916Sschwartz 
422916Sschwartz /*
423916Sschwartz  * Main function for handling interrupt CPU binding requests and queries.
424916Sschwartz  * Need to implement later
425916Sschwartz  */
426916Sschwartz /*ARGSUSED*/
427916Sschwartz int
428916Sschwartz pcitool_intr_admn(dev_info_t *dip, void *arg, int cmd, int mode)
429916Sschwartz {
430916Sschwartz 	int rval;
431916Sschwartz 
432916Sschwartz 	switch (cmd) {
433916Sschwartz 
434916Sschwartz 	/* Associate a new CPU with a given vector */
435916Sschwartz 	case PCITOOL_DEVICE_SET_INTR:
436916Sschwartz 		rval = pcitool_set_intr(dip, arg, mode);
437916Sschwartz 		break;
438916Sschwartz 
439916Sschwartz 	case PCITOOL_DEVICE_GET_INTR:
440916Sschwartz 		rval = pcitool_get_intr(dip, arg, mode);
441916Sschwartz 		break;
442916Sschwartz 
4434397Sschwartz 	case PCITOOL_SYSTEM_INTR_INFO:
4444397Sschwartz 		rval = pcitool_intr_info(dip, arg, mode);
445916Sschwartz 		break;
446916Sschwartz 
447916Sschwartz 	default:
448916Sschwartz 		rval = ENOTSUP;
449916Sschwartz 	}
450916Sschwartz 
451916Sschwartz 	return (rval);
452916Sschwartz }
453916Sschwartz 
454916Sschwartz 
4550Sstevel@tonic-gate /*
4560Sstevel@tonic-gate  * A note about ontrap handling:
4570Sstevel@tonic-gate  *
4580Sstevel@tonic-gate  * X86 systems on which this module was tested return FFs instead of bus errors
4590Sstevel@tonic-gate  * when accessing devices with invalid addresses.  Ontrap handling, which
4600Sstevel@tonic-gate  * gracefully handles kernel bus errors, is installed anyway, in case future
4610Sstevel@tonic-gate  * X86 platforms require it.
4620Sstevel@tonic-gate  */
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate /*
4650Sstevel@tonic-gate  * Perform register accesses on the nexus device itself.
4660Sstevel@tonic-gate  * No explicit PCI nexus device for X86, so not applicable.
4670Sstevel@tonic-gate  */
468916Sschwartz 
4690Sstevel@tonic-gate /*ARGSUSED*/
4700Sstevel@tonic-gate int
471777Sschwartz pcitool_bus_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode)
4720Sstevel@tonic-gate {
4730Sstevel@tonic-gate 	return (ENOTSUP);
4740Sstevel@tonic-gate }
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate /* Swap endianness. */
4770Sstevel@tonic-gate static uint64_t
4780Sstevel@tonic-gate pcitool_swap_endian(uint64_t data, int size)
4790Sstevel@tonic-gate {
4800Sstevel@tonic-gate 	typedef union {
4810Sstevel@tonic-gate 		uint64_t data64;
4820Sstevel@tonic-gate 		uint8_t data8[8];
4830Sstevel@tonic-gate 	} data_split_t;
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	data_split_t orig_data;
4860Sstevel@tonic-gate 	data_split_t returned_data;
4870Sstevel@tonic-gate 	int i;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	orig_data.data64 = data;
4900Sstevel@tonic-gate 	returned_data.data64 = 0;
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	for (i = 0; i < size; i++) {
4930Sstevel@tonic-gate 		returned_data.data8[i] = orig_data.data8[size - 1 - i];
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	return (returned_data.data64);
4970Sstevel@tonic-gate }
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 
500777Sschwartz /*
501777Sschwartz  * Access device.  prg is modified.
502777Sschwartz  *
503777Sschwartz  * Extended config space is available only through memory-mapped access.
504777Sschwartz  * Standard config space on pci express devices is available either way,
505777Sschwartz  * so do it memory-mapped here too, for simplicity.
506777Sschwartz  */
507777Sschwartz /*ARGSUSED*/
508777Sschwartz static int
509777Sschwartz pcitool_pciex_cfg_access(dev_info_t *dip, pcitool_reg_t *prg,
510777Sschwartz     boolean_t write_flag)
511777Sschwartz {
512777Sschwartz 	int rval = SUCCESS;
513777Sschwartz 	uint64_t virt_addr;
514777Sschwartz 	size_t	num_virt_pages;
515777Sschwartz 
516777Sschwartz 	prg->status = PCITOOL_SUCCESS;
517777Sschwartz 
518777Sschwartz 	prg->phys_addr = ddi_prop_get_int64(DDI_DEV_T_ANY, dip, 0,
519881Sjohnny 	    "ecfga-base-address", 0);
520777Sschwartz 	if (prg->phys_addr == 0) {
521777Sschwartz 		prg->status = PCITOOL_IO_ERROR;
522777Sschwartz 		return (EIO);
523777Sschwartz 	}
524777Sschwartz 
525777Sschwartz 	prg->phys_addr += prg->offset +
526777Sschwartz 	    ((prg->bus_no << PCIEX_REG_BUS_SHIFT) |
527777Sschwartz 	    (prg->dev_no << PCIEX_REG_DEV_SHIFT) |
528777Sschwartz 	    (prg->func_no << PCIEX_REG_FUNC_SHIFT));
529777Sschwartz 
530777Sschwartz 	virt_addr = pcitool_map(prg->phys_addr,
531777Sschwartz 	    PCITOOL_ACC_ATTR_SIZE(prg->acc_attr), &num_virt_pages);
532777Sschwartz 	if (virt_addr == NULL) {
533777Sschwartz 		prg->status = PCITOOL_IO_ERROR;
534777Sschwartz 		return (EIO);
535777Sschwartz 	}
536777Sschwartz 
537777Sschwartz 	rval = pcitool_mem_access(dip, prg, virt_addr, write_flag);
538777Sschwartz 	pcitool_unmap(virt_addr, num_virt_pages);
539777Sschwartz 	return (rval);
540777Sschwartz }
541777Sschwartz 
5420Sstevel@tonic-gate /* Access device.  prg is modified. */
5430Sstevel@tonic-gate /*ARGSUSED*/
5440Sstevel@tonic-gate static int
5450Sstevel@tonic-gate pcitool_cfg_access(dev_info_t *dip, pcitool_reg_t *prg, boolean_t write_flag)
5460Sstevel@tonic-gate {
5470Sstevel@tonic-gate 	int size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
5480Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
5490Sstevel@tonic-gate 	int rval = SUCCESS;
5500Sstevel@tonic-gate 	uint64_t local_data;
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	/*
5530Sstevel@tonic-gate 	 * NOTE: there is no way to verify whether or not the address is valid.
5540Sstevel@tonic-gate 	 * The put functions return void and the get functions return ff on
5550Sstevel@tonic-gate 	 * error.
5560Sstevel@tonic-gate 	 */
5570Sstevel@tonic-gate 	prg->status = PCITOOL_SUCCESS;
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	if (write_flag) {
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 		if (big_endian) {
5620Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
5630Sstevel@tonic-gate 		} else {
5640Sstevel@tonic-gate 			local_data = prg->data;
5650Sstevel@tonic-gate 		}
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 		switch (size) {
5680Sstevel@tonic-gate 		case 1:
5690Sstevel@tonic-gate 			(*pci_putb_func)(prg->bus_no, prg->dev_no,
5700Sstevel@tonic-gate 			    prg->func_no, prg->offset, local_data);
5710Sstevel@tonic-gate 			break;
5720Sstevel@tonic-gate 		case 2:
5730Sstevel@tonic-gate 			(*pci_putw_func)(prg->bus_no, prg->dev_no,
5740Sstevel@tonic-gate 			    prg->func_no, prg->offset, local_data);
5750Sstevel@tonic-gate 			break;
5760Sstevel@tonic-gate 		case 4:
5770Sstevel@tonic-gate 			(*pci_putl_func)(prg->bus_no, prg->dev_no,
5780Sstevel@tonic-gate 			    prg->func_no, prg->offset, local_data);
5790Sstevel@tonic-gate 			break;
5800Sstevel@tonic-gate 		default:
5810Sstevel@tonic-gate 			rval = ENOTSUP;
5820Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
5830Sstevel@tonic-gate 			break;
5840Sstevel@tonic-gate 		}
5850Sstevel@tonic-gate 	} else {
5860Sstevel@tonic-gate 		switch (size) {
5870Sstevel@tonic-gate 		case 1:
5880Sstevel@tonic-gate 			local_data = (*pci_getb_func)(prg->bus_no, prg->dev_no,
5890Sstevel@tonic-gate 			    prg->func_no, prg->offset);
5900Sstevel@tonic-gate 			break;
5910Sstevel@tonic-gate 		case 2:
5920Sstevel@tonic-gate 			local_data = (*pci_getw_func)(prg->bus_no, prg->dev_no,
5930Sstevel@tonic-gate 			    prg->func_no, prg->offset);
5940Sstevel@tonic-gate 			break;
5950Sstevel@tonic-gate 		case 4:
5960Sstevel@tonic-gate 			local_data = (*pci_getl_func)(prg->bus_no, prg->dev_no,
5970Sstevel@tonic-gate 			    prg->func_no, prg->offset);
5980Sstevel@tonic-gate 			break;
5990Sstevel@tonic-gate 		default:
6000Sstevel@tonic-gate 			rval = ENOTSUP;
6010Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
6020Sstevel@tonic-gate 			break;
6030Sstevel@tonic-gate 		}
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 		if (rval == SUCCESS) {
6060Sstevel@tonic-gate 			if (big_endian) {
6070Sstevel@tonic-gate 				prg->data =
6080Sstevel@tonic-gate 				    pcitool_swap_endian(local_data, size);
6090Sstevel@tonic-gate 			} else {
6100Sstevel@tonic-gate 				prg->data = local_data;
6110Sstevel@tonic-gate 			}
6120Sstevel@tonic-gate 		}
6130Sstevel@tonic-gate 	}
6140Sstevel@tonic-gate 	prg->phys_addr = 0;	/* Config space is not memory mapped on X86. */
6150Sstevel@tonic-gate 	return (rval);
6160Sstevel@tonic-gate }
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate /*ARGSUSED*/
6200Sstevel@tonic-gate static int
6210Sstevel@tonic-gate pcitool_io_access(dev_info_t *dip, pcitool_reg_t *prg, boolean_t write_flag)
6220Sstevel@tonic-gate {
6230Sstevel@tonic-gate 	int port = (int)prg->phys_addr;
6240Sstevel@tonic-gate 	size_t size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
6250Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
6260Sstevel@tonic-gate 	int rval = SUCCESS;
6270Sstevel@tonic-gate 	on_trap_data_t otd;
6280Sstevel@tonic-gate 	uint64_t local_data;
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	/*
6320Sstevel@tonic-gate 	 * on_trap works like setjmp.
6330Sstevel@tonic-gate 	 *
6340Sstevel@tonic-gate 	 * A non-zero return here means on_trap has returned from an error.
6350Sstevel@tonic-gate 	 *
6360Sstevel@tonic-gate 	 * A zero return here means that on_trap has just returned from setup.
6370Sstevel@tonic-gate 	 */
6380Sstevel@tonic-gate 	if (on_trap(&otd, OT_DATA_ACCESS)) {
6390Sstevel@tonic-gate 		no_trap();
6400Sstevel@tonic-gate 		if (pcitool_debug)
6410Sstevel@tonic-gate 			prom_printf(
6424397Sschwartz 			    "pcitool_io_access: on_trap caught an error...\n");
6430Sstevel@tonic-gate 		prg->status = PCITOOL_INVALID_ADDRESS;
6440Sstevel@tonic-gate 		return (EFAULT);
6450Sstevel@tonic-gate 	}
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	if (write_flag) {
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 		if (big_endian) {
6500Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
6510Sstevel@tonic-gate 		} else {
6520Sstevel@tonic-gate 			local_data = prg->data;
6530Sstevel@tonic-gate 		}
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 		if (pcitool_debug)
6560Sstevel@tonic-gate 			prom_printf("Writing %ld byte(s) to port 0x%x\n",
6570Sstevel@tonic-gate 			    size, port);
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 		switch (size) {
6600Sstevel@tonic-gate 		case 1:
6610Sstevel@tonic-gate 			outb(port, (uint8_t)local_data);
6620Sstevel@tonic-gate 			break;
6630Sstevel@tonic-gate 		case 2:
6640Sstevel@tonic-gate 			outw(port, (uint16_t)local_data);
6650Sstevel@tonic-gate 			break;
6660Sstevel@tonic-gate 		case 4:
6670Sstevel@tonic-gate 			outl(port, (uint32_t)local_data);
6680Sstevel@tonic-gate 			break;
6690Sstevel@tonic-gate 		default:
6700Sstevel@tonic-gate 			rval = ENOTSUP;
6710Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
6720Sstevel@tonic-gate 			break;
6730Sstevel@tonic-gate 		}
6740Sstevel@tonic-gate 	} else {
6750Sstevel@tonic-gate 		if (pcitool_debug)
6760Sstevel@tonic-gate 			prom_printf("Reading %ld byte(s) from port 0x%x\n",
6770Sstevel@tonic-gate 			    size, port);
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 		switch (size) {
6800Sstevel@tonic-gate 		case 1:
6810Sstevel@tonic-gate 			local_data = inb(port);
6820Sstevel@tonic-gate 			break;
6830Sstevel@tonic-gate 		case 2:
6840Sstevel@tonic-gate 			local_data = inw(port);
6850Sstevel@tonic-gate 			break;
6860Sstevel@tonic-gate 		case 4:
6870Sstevel@tonic-gate 			local_data = inl(port);
6880Sstevel@tonic-gate 			break;
6890Sstevel@tonic-gate 		default:
6900Sstevel@tonic-gate 			rval = ENOTSUP;
6910Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
6920Sstevel@tonic-gate 			break;
6930Sstevel@tonic-gate 		}
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 		if (rval == SUCCESS) {
6960Sstevel@tonic-gate 			if (big_endian) {
6970Sstevel@tonic-gate 				prg->data =
6980Sstevel@tonic-gate 				    pcitool_swap_endian(local_data, size);
6990Sstevel@tonic-gate 			} else {
7000Sstevel@tonic-gate 				prg->data = local_data;
7010Sstevel@tonic-gate 			}
7020Sstevel@tonic-gate 		}
7030Sstevel@tonic-gate 	}
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	no_trap();
7060Sstevel@tonic-gate 	return (rval);
7070Sstevel@tonic-gate }
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate /*ARGSUSED*/
7100Sstevel@tonic-gate static int
7110Sstevel@tonic-gate pcitool_mem_access(dev_info_t *dip, pcitool_reg_t *prg, uint64_t virt_addr,
712117Sschwartz 	boolean_t write_flag)
7130Sstevel@tonic-gate {
7140Sstevel@tonic-gate 	size_t size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
7150Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
7160Sstevel@tonic-gate 	int rval = DDI_SUCCESS;
7170Sstevel@tonic-gate 	on_trap_data_t otd;
7180Sstevel@tonic-gate 	uint64_t local_data;
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	/*
7210Sstevel@tonic-gate 	 * on_trap works like setjmp.
7220Sstevel@tonic-gate 	 *
7230Sstevel@tonic-gate 	 * A non-zero return here means on_trap has returned from an error.
7240Sstevel@tonic-gate 	 *
7250Sstevel@tonic-gate 	 * A zero return here means that on_trap has just returned from setup.
7260Sstevel@tonic-gate 	 */
7270Sstevel@tonic-gate 	if (on_trap(&otd, OT_DATA_ACCESS)) {
7280Sstevel@tonic-gate 		no_trap();
7290Sstevel@tonic-gate 		if (pcitool_debug)
7300Sstevel@tonic-gate 			prom_printf(
7310Sstevel@tonic-gate 			    "pcitool_mem_access: on_trap caught an error...\n");
7320Sstevel@tonic-gate 		prg->status = PCITOOL_INVALID_ADDRESS;
7330Sstevel@tonic-gate 		return (EFAULT);
7340Sstevel@tonic-gate 	}
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	if (write_flag) {
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 		if (big_endian) {
7390Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
7400Sstevel@tonic-gate 		} else {
7410Sstevel@tonic-gate 			local_data = prg->data;
7420Sstevel@tonic-gate 		}
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 		switch (size) {
7450Sstevel@tonic-gate 		case 1:
7460Sstevel@tonic-gate 			*((uint8_t *)(uintptr_t)virt_addr) = local_data;
7470Sstevel@tonic-gate 			break;
7480Sstevel@tonic-gate 		case 2:
7490Sstevel@tonic-gate 			*((uint16_t *)(uintptr_t)virt_addr) = local_data;
7500Sstevel@tonic-gate 			break;
7510Sstevel@tonic-gate 		case 4:
7520Sstevel@tonic-gate 			*((uint32_t *)(uintptr_t)virt_addr) = local_data;
7530Sstevel@tonic-gate 			break;
7540Sstevel@tonic-gate 		case 8:
7550Sstevel@tonic-gate 			*((uint64_t *)(uintptr_t)virt_addr) = local_data;
7560Sstevel@tonic-gate 			break;
7570Sstevel@tonic-gate 		default:
7580Sstevel@tonic-gate 			rval = ENOTSUP;
7590Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
7600Sstevel@tonic-gate 			break;
7610Sstevel@tonic-gate 		}
7620Sstevel@tonic-gate 	} else {
7630Sstevel@tonic-gate 		switch (size) {
7640Sstevel@tonic-gate 		case 1:
7650Sstevel@tonic-gate 			local_data = *((uint8_t *)(uintptr_t)virt_addr);
7660Sstevel@tonic-gate 			break;
7670Sstevel@tonic-gate 		case 2:
7680Sstevel@tonic-gate 			local_data = *((uint16_t *)(uintptr_t)virt_addr);
7690Sstevel@tonic-gate 			break;
7700Sstevel@tonic-gate 		case 4:
7710Sstevel@tonic-gate 			local_data = *((uint32_t *)(uintptr_t)virt_addr);
7720Sstevel@tonic-gate 			break;
7730Sstevel@tonic-gate 		case 8:
7740Sstevel@tonic-gate 			local_data = *((uint64_t *)(uintptr_t)virt_addr);
7750Sstevel@tonic-gate 			break;
7760Sstevel@tonic-gate 		default:
7770Sstevel@tonic-gate 			rval = ENOTSUP;
7780Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
7790Sstevel@tonic-gate 			break;
7800Sstevel@tonic-gate 		}
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 		if (rval == SUCCESS) {
7830Sstevel@tonic-gate 			if (big_endian) {
7840Sstevel@tonic-gate 				prg->data =
7850Sstevel@tonic-gate 				    pcitool_swap_endian(local_data, size);
7860Sstevel@tonic-gate 			} else {
7870Sstevel@tonic-gate 				prg->data = local_data;
7880Sstevel@tonic-gate 			}
7890Sstevel@tonic-gate 		}
7900Sstevel@tonic-gate 	}
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	no_trap();
7930Sstevel@tonic-gate 	return (rval);
7940Sstevel@tonic-gate }
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate /*
7970Sstevel@tonic-gate  * Map up to 2 pages which contain the address we want to access.
7980Sstevel@tonic-gate  *
7990Sstevel@tonic-gate  * Mapping should span no more than 8 bytes.  With X86 it is possible for an
8000Sstevel@tonic-gate  * 8 byte value to start on a 4 byte boundary, so it can cross a page boundary.
8010Sstevel@tonic-gate  * We'll never have to map more than two pages.
8020Sstevel@tonic-gate  */
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate static uint64_t
8050Sstevel@tonic-gate pcitool_map(uint64_t phys_addr, size_t size, size_t *num_pages)
8060Sstevel@tonic-gate {
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	uint64_t page_base = phys_addr & ~MMU_PAGEOFFSET;
8090Sstevel@tonic-gate 	uint64_t offset = phys_addr & MMU_PAGEOFFSET;
8100Sstevel@tonic-gate 	void *virt_base;
8110Sstevel@tonic-gate 	uint64_t returned_addr;
8123446Smrj 	pfn_t pfn;
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	if (pcitool_debug)
8150Sstevel@tonic-gate 		prom_printf("pcitool_map: Called with PA:0x%p\n",
8160Sstevel@tonic-gate 		    (uint8_t *)(uintptr_t)phys_addr);
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	*num_pages = 1;
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	/* Desired mapping would span more than two pages. */
8210Sstevel@tonic-gate 	if ((offset + size) > (MMU_PAGESIZE * 2)) {
8220Sstevel@tonic-gate 		if (pcitool_debug)
8230Sstevel@tonic-gate 			prom_printf("boundary violation: "
824777Sschwartz 			    "offset:0x%" PRIx64 ", size:%ld, pagesize:0x%lx\n",
825777Sschwartz 			    offset, (uintptr_t)size, (uintptr_t)MMU_PAGESIZE);
8260Sstevel@tonic-gate 		return (NULL);
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	} else if ((offset + size) > MMU_PAGESIZE) {
8290Sstevel@tonic-gate 		(*num_pages)++;
8300Sstevel@tonic-gate 	}
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	/* Get page(s) of virtual space. */
8330Sstevel@tonic-gate 	virt_base = vmem_alloc(heap_arena, ptob(*num_pages), VM_NOSLEEP);
8340Sstevel@tonic-gate 	if (virt_base == NULL) {
8350Sstevel@tonic-gate 		if (pcitool_debug)
8360Sstevel@tonic-gate 			prom_printf("Couldn't get virtual base address.\n");
8370Sstevel@tonic-gate 		return (NULL);
8380Sstevel@tonic-gate 	}
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 	if (pcitool_debug)
8410Sstevel@tonic-gate 		prom_printf("Got base virtual address:0x%p\n", virt_base);
8420Sstevel@tonic-gate 
843*5084Sjohnlev #ifdef __xpv
844*5084Sjohnlev 	/*
845*5084Sjohnlev 	 * We should only get here if we are dom0.
846*5084Sjohnlev 	 * We're using a real device so we need to translate the MA to a PFN.
847*5084Sjohnlev 	 */
848*5084Sjohnlev 	ASSERT(DOMAIN_IS_INITDOMAIN(xen_info));
849*5084Sjohnlev 	pfn = xen_assign_pfn(mmu_btop(page_base));
850*5084Sjohnlev #else
8513446Smrj 	pfn = btop(page_base);
852*5084Sjohnlev #endif
8533446Smrj 
8540Sstevel@tonic-gate 	/* Now map the allocated virtual space to the physical address. */
8553446Smrj 	hat_devload(kas.a_hat, virt_base, mmu_ptob(*num_pages), pfn,
8563446Smrj 	    PROT_READ | PROT_WRITE | HAT_STRICTORDER,
8570Sstevel@tonic-gate 	    HAT_LOAD_LOCK);
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	returned_addr = ((uintptr_t)(virt_base)) + offset;
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	if (pcitool_debug)
8620Sstevel@tonic-gate 		prom_printf("pcitool_map: returning VA:0x%p\n",
8630Sstevel@tonic-gate 		    (void *)(uintptr_t)returned_addr);
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 	return (returned_addr);
8660Sstevel@tonic-gate }
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate /* Unmap the mapped page(s). */
8690Sstevel@tonic-gate static void
8700Sstevel@tonic-gate pcitool_unmap(uint64_t virt_addr, size_t num_pages)
8710Sstevel@tonic-gate {
8720Sstevel@tonic-gate 	void *base_virt_addr = (void *)(uintptr_t)(virt_addr & ~MMU_PAGEOFFSET);
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	hat_unload(kas.a_hat, base_virt_addr, ptob(num_pages),
8750Sstevel@tonic-gate 	    HAT_UNLOAD_UNLOCK);
8760Sstevel@tonic-gate 	vmem_free(heap_arena, base_virt_addr, ptob(num_pages));
8770Sstevel@tonic-gate }
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate /* Perform register accesses on PCI leaf devices. */
8810Sstevel@tonic-gate int
882777Sschwartz pcitool_dev_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode)
8830Sstevel@tonic-gate {
8840Sstevel@tonic-gate 	boolean_t	write_flag = B_FALSE;
8850Sstevel@tonic-gate 	int		rval = 0;
8860Sstevel@tonic-gate 	pcitool_reg_t	prg;
8870Sstevel@tonic-gate 	uint8_t		size;
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	uint64_t	base_addr;
8900Sstevel@tonic-gate 	uint64_t	virt_addr;
8910Sstevel@tonic-gate 	size_t		num_virt_pages;
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 	switch (cmd) {
8940Sstevel@tonic-gate 	case (PCITOOL_DEVICE_SET_REG):
8950Sstevel@tonic-gate 		write_flag = B_TRUE;
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 	/*FALLTHRU*/
8980Sstevel@tonic-gate 	case (PCITOOL_DEVICE_GET_REG):
8990Sstevel@tonic-gate 		if (pcitool_debug)
9000Sstevel@tonic-gate 			prom_printf("pci_dev_reg_ops set/get reg\n");
9010Sstevel@tonic-gate 		if (ddi_copyin(arg, &prg, sizeof (pcitool_reg_t), mode) !=
9020Sstevel@tonic-gate 		    DDI_SUCCESS) {
9030Sstevel@tonic-gate 			if (pcitool_debug)
9040Sstevel@tonic-gate 				prom_printf("Error reading arguments\n");
9050Sstevel@tonic-gate 			return (EFAULT);
9060Sstevel@tonic-gate 		}
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 		if (prg.barnum >= (sizeof (pci_bars) / sizeof (pci_bars[0]))) {
9090Sstevel@tonic-gate 			prg.status = PCITOOL_OUT_OF_RANGE;
9100Sstevel@tonic-gate 			rval = EINVAL;
9110Sstevel@tonic-gate 			goto done_reg;
9120Sstevel@tonic-gate 		}
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 		if (pcitool_debug)
9150Sstevel@tonic-gate 			prom_printf("raw bus:0x%x, dev:0x%x, func:0x%x\n",
9160Sstevel@tonic-gate 			    prg.bus_no, prg.dev_no, prg.func_no);
9170Sstevel@tonic-gate 		/* Validate address arguments of bus / dev / func */
9180Sstevel@tonic-gate 		if (((prg.bus_no &
9190Sstevel@tonic-gate 		    (PCI_REG_BUS_M >> PCI_REG_BUS_SHIFT)) !=
9200Sstevel@tonic-gate 		    prg.bus_no) ||
9210Sstevel@tonic-gate 		    ((prg.dev_no &
9220Sstevel@tonic-gate 		    (PCI_REG_DEV_M >> PCI_REG_DEV_SHIFT)) !=
9230Sstevel@tonic-gate 		    prg.dev_no) ||
9240Sstevel@tonic-gate 		    ((prg.func_no &
9250Sstevel@tonic-gate 		    (PCI_REG_FUNC_M >> PCI_REG_FUNC_SHIFT)) !=
9260Sstevel@tonic-gate 		    prg.func_no)) {
9270Sstevel@tonic-gate 			prg.status = PCITOOL_INVALID_ADDRESS;
9280Sstevel@tonic-gate 			rval = EINVAL;
9290Sstevel@tonic-gate 			goto done_reg;
9300Sstevel@tonic-gate 		}
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 		size = PCITOOL_ACC_ATTR_SIZE(prg.acc_attr);
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 		/* Proper config space desired. */
9350Sstevel@tonic-gate 		if (prg.barnum == 0) {
9360Sstevel@tonic-gate 
937777Sschwartz 			if (pcitool_debug)
938777Sschwartz 				prom_printf(
939777Sschwartz 				    "config access: offset:0x%" PRIx64 ", "
940777Sschwartz 				    "phys_addr:0x%" PRIx64 "\n",
941777Sschwartz 				    prg.offset, prg.phys_addr);
942777Sschwartz 
943777Sschwartz 			if (prg.offset >= max_cfg_size) {
9440Sstevel@tonic-gate 				prg.status = PCITOOL_OUT_OF_RANGE;
9450Sstevel@tonic-gate 				rval = EINVAL;
9460Sstevel@tonic-gate 				goto done_reg;
9470Sstevel@tonic-gate 			}
9480Sstevel@tonic-gate 
9491083Sanish 			/*
9501083Sanish 			 * Access device.  prg is modified.
9511083Sanish 			 * First, check for AMD northbridges for I/O access
9521083Sanish 			 * (This fix will move in future to pcitool user-land)
9531083Sanish 			 * Next, check for PCIe devices and do
9541083Sanish 			 * memory-mapped access
9551083Sanish 			 * Lastly, check for PCI devices and do I/O access
9561083Sanish 			 */
9572434Sanish 			if ((prg.bus_no == 0) &&
9582434Sanish 			    (prg.dev_no >= 0x18) &&
9592434Sanish 			    (prg.dev_no < (0x18 + ncpus))) {
9601083Sanish 				if (cpuid_getvendor(CPU) == X86_VENDOR_AMD)
9611083Sanish 					rval = pcitool_cfg_access(dip, &prg,
9621083Sanish 					    write_flag);
9631083Sanish 			} else if (max_cfg_size == PCIE_CONF_HDR_SIZE)
964777Sschwartz 				rval = pcitool_pciex_cfg_access(dip, &prg,
965777Sschwartz 				    write_flag);
966777Sschwartz 			else
967777Sschwartz 				rval = pcitool_cfg_access(dip, &prg,
968777Sschwartz 				    write_flag);
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 			if (pcitool_debug)
9710Sstevel@tonic-gate 				prom_printf(
9720Sstevel@tonic-gate 				    "config access: data:0x%" PRIx64 "\n",
9730Sstevel@tonic-gate 				    prg.data);
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 		/* IO/ MEM/ MEM64 space. */
9760Sstevel@tonic-gate 		} else {
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate 			pcitool_reg_t	prg2;
9790Sstevel@tonic-gate 			bcopy(&prg, &prg2, sizeof (pcitool_reg_t));
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 			/*
9820Sstevel@tonic-gate 			 * Translate BAR number into offset of the BAR in
9830Sstevel@tonic-gate 			 * the device's config space.
9840Sstevel@tonic-gate 			 */
9850Sstevel@tonic-gate 			prg2.offset = pci_bars[prg2.barnum];
9860Sstevel@tonic-gate 			prg2.acc_attr =
9870Sstevel@tonic-gate 			    PCITOOL_ACC_ATTR_SIZE_4 | PCITOOL_ACC_ATTR_ENDN_LTL;
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 			if (pcitool_debug)
9900Sstevel@tonic-gate 				prom_printf(
9910Sstevel@tonic-gate 				    "barnum:%d, bar_offset:0x%" PRIx64 "\n",
9920Sstevel@tonic-gate 				    prg2.barnum, prg2.offset);
9930Sstevel@tonic-gate 			/*
9940Sstevel@tonic-gate 			 * Get Bus Address Register (BAR) from config space.
9950Sstevel@tonic-gate 			 * prg2.offset is the offset into config space of the
9960Sstevel@tonic-gate 			 * BAR desired.  prg.status is modified on error.
9970Sstevel@tonic-gate 			 */
9980Sstevel@tonic-gate 			rval = pcitool_cfg_access(dip, &prg2, B_FALSE);
9990Sstevel@tonic-gate 			if (rval != SUCCESS) {
10000Sstevel@tonic-gate 				if (pcitool_debug)
10010Sstevel@tonic-gate 					prom_printf("BAR access failed\n");
10020Sstevel@tonic-gate 				prg.status = prg2.status;
10030Sstevel@tonic-gate 				goto done_reg;
10040Sstevel@tonic-gate 			}
10050Sstevel@tonic-gate 			/*
10060Sstevel@tonic-gate 			 * Reference proper PCI space based on the BAR.
10070Sstevel@tonic-gate 			 * If 64 bit MEM space, need to load other half of the
10080Sstevel@tonic-gate 			 * BAR first.
10090Sstevel@tonic-gate 			 */
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 			if (pcitool_debug)
10120Sstevel@tonic-gate 				prom_printf("bar returned is 0x%" PRIx64 "\n",
10130Sstevel@tonic-gate 				    prg2.data);
10140Sstevel@tonic-gate 			if (!prg2.data) {
10150Sstevel@tonic-gate 				if (pcitool_debug)
10160Sstevel@tonic-gate 					prom_printf("BAR data == 0\n");
10170Sstevel@tonic-gate 				rval = EINVAL;
10180Sstevel@tonic-gate 				prg.status = PCITOOL_INVALID_ADDRESS;
10190Sstevel@tonic-gate 				goto done_reg;
10200Sstevel@tonic-gate 			}
10210Sstevel@tonic-gate 			if (prg2.data == 0xffffffff) {
10220Sstevel@tonic-gate 				if (pcitool_debug)
10230Sstevel@tonic-gate 					prom_printf("BAR data == -1\n");
10240Sstevel@tonic-gate 				rval = EINVAL;
10250Sstevel@tonic-gate 				prg.status = PCITOOL_INVALID_ADDRESS;
10260Sstevel@tonic-gate 				goto done_reg;
10270Sstevel@tonic-gate 			}
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 			/*
10300Sstevel@tonic-gate 			 * BAR has bits saying this space is IO space, unless
10310Sstevel@tonic-gate 			 * this is the ROM address register.
10320Sstevel@tonic-gate 			 */
10330Sstevel@tonic-gate 			if (((PCI_BASE_SPACE_M & prg2.data) ==
10340Sstevel@tonic-gate 			    PCI_BASE_SPACE_IO) &&
10350Sstevel@tonic-gate 			    (prg2.offset != PCI_CONF_ROM)) {
10360Sstevel@tonic-gate 				if (pcitool_debug)
10370Sstevel@tonic-gate 					prom_printf("IO space\n");
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 				prg2.data &= PCI_BASE_IO_ADDR_M;
10400Sstevel@tonic-gate 				prg.phys_addr = prg2.data + prg.offset;
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 				rval = pcitool_io_access(dip, &prg, write_flag);
10430Sstevel@tonic-gate 				if ((rval != SUCCESS) && (pcitool_debug))
10440Sstevel@tonic-gate 					prom_printf("IO access failed\n");
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 				goto done_reg;
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 			/*
10500Sstevel@tonic-gate 			 * BAR has bits saying this space is 64 bit memory
10510Sstevel@tonic-gate 			 * space, unless this is the ROM address register.
10520Sstevel@tonic-gate 			 *
10530Sstevel@tonic-gate 			 * The 64 bit address stored in two BAR cells is not
10540Sstevel@tonic-gate 			 * necessarily aligned on an 8-byte boundary.
10550Sstevel@tonic-gate 			 * Need to keep the first 4 bytes read,
10560Sstevel@tonic-gate 			 * and do a separate read of the high 4 bytes.
10570Sstevel@tonic-gate 			 */
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 			} else if ((PCI_BASE_TYPE_ALL & prg2.data) &&
10600Sstevel@tonic-gate 			    (prg2.offset != PCI_CONF_ROM)) {
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 				uint32_t low_bytes =
10630Sstevel@tonic-gate 				    (uint32_t)(prg2.data & ~PCI_BASE_TYPE_ALL);
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 				/*
10660Sstevel@tonic-gate 				 * Don't try to read the next 4 bytes
10670Sstevel@tonic-gate 				 * past the end of BARs.
10680Sstevel@tonic-gate 				 */
10690Sstevel@tonic-gate 				if (prg2.offset >= PCI_CONF_BASE5) {
10700Sstevel@tonic-gate 					prg.status = PCITOOL_OUT_OF_RANGE;
10710Sstevel@tonic-gate 					rval = EIO;
10720Sstevel@tonic-gate 					goto done_reg;
10730Sstevel@tonic-gate 				}
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 				/*
10760Sstevel@tonic-gate 				 * Access device.
10770Sstevel@tonic-gate 				 * prg2.status is modified on error.
10780Sstevel@tonic-gate 				 */
10790Sstevel@tonic-gate 				prg2.offset += 4;
10800Sstevel@tonic-gate 				rval = pcitool_cfg_access(dip, &prg2, B_FALSE);
10810Sstevel@tonic-gate 				if (rval != SUCCESS) {
10820Sstevel@tonic-gate 					prg.status = prg2.status;
10830Sstevel@tonic-gate 					goto done_reg;
10840Sstevel@tonic-gate 				}
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 				if (prg2.data == 0xffffffff) {
10870Sstevel@tonic-gate 					prg.status = PCITOOL_INVALID_ADDRESS;
10880Sstevel@tonic-gate 					prg.status = EFAULT;
10890Sstevel@tonic-gate 					goto done_reg;
10900Sstevel@tonic-gate 				}
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 				prg2.data = (prg2.data << 32) + low_bytes;
10930Sstevel@tonic-gate 				if (pcitool_debug)
10940Sstevel@tonic-gate 					prom_printf(
10950Sstevel@tonic-gate 					    "64 bit mem space.  "
10960Sstevel@tonic-gate 					    "64-bit bar is 0x%" PRIx64 "\n",
10970Sstevel@tonic-gate 					    prg2.data);
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 			/* Mem32 space, including ROM */
11000Sstevel@tonic-gate 			} else {
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 				if (prg2.offset == PCI_CONF_ROM) {
11030Sstevel@tonic-gate 					if (pcitool_debug)
11040Sstevel@tonic-gate 						prom_printf(
11050Sstevel@tonic-gate 						    "Additional ROM "
11060Sstevel@tonic-gate 						    "checking\n");
11070Sstevel@tonic-gate 					/* Can't write to ROM */
11080Sstevel@tonic-gate 					if (write_flag) {
11090Sstevel@tonic-gate 						prg.status = PCITOOL_ROM_WRITE;
11100Sstevel@tonic-gate 						rval = EIO;
11110Sstevel@tonic-gate 						goto done_reg;
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 					/* ROM disabled for reading */
11140Sstevel@tonic-gate 					} else if (!(prg2.data & 0x00000001)) {
11150Sstevel@tonic-gate 						prg.status =
11160Sstevel@tonic-gate 						    PCITOOL_ROM_DISABLED;
11170Sstevel@tonic-gate 						rval = EIO;
11180Sstevel@tonic-gate 						goto done_reg;
11190Sstevel@tonic-gate 					}
11200Sstevel@tonic-gate 				}
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 				if (pcitool_debug)
11230Sstevel@tonic-gate 					prom_printf("32 bit mem space\n");
11240Sstevel@tonic-gate 			}
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 			/* Common code for all IO/MEM range spaces. */
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 			base_addr = prg2.data;
11290Sstevel@tonic-gate 			if (pcitool_debug)
11300Sstevel@tonic-gate 				prom_printf(
11310Sstevel@tonic-gate 				    "addr portion of bar is 0x%" PRIx64 ", "
11320Sstevel@tonic-gate 				    "base=0x%" PRIx64 ", "
11330Sstevel@tonic-gate 				    "offset:0x%" PRIx64 "\n",
11340Sstevel@tonic-gate 				    prg2.data, base_addr, prg.offset);
11350Sstevel@tonic-gate 			/*
11360Sstevel@tonic-gate 			 * Use offset provided by caller to index into
11370Sstevel@tonic-gate 			 * desired space, then access.
11380Sstevel@tonic-gate 			 * Note that prg.status is modified on error.
11390Sstevel@tonic-gate 			 */
11400Sstevel@tonic-gate 			prg.phys_addr = base_addr + prg.offset;
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 			virt_addr = pcitool_map(prg.phys_addr, size,
11430Sstevel@tonic-gate 			    &num_virt_pages);
11440Sstevel@tonic-gate 			if (virt_addr == NULL) {
11450Sstevel@tonic-gate 				prg.status = PCITOOL_IO_ERROR;
11460Sstevel@tonic-gate 				rval = EIO;
11470Sstevel@tonic-gate 				goto done_reg;
11480Sstevel@tonic-gate 			}
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 			rval = pcitool_mem_access(dip, &prg, virt_addr,
11510Sstevel@tonic-gate 			    write_flag);
11520Sstevel@tonic-gate 			pcitool_unmap(virt_addr, num_virt_pages);
11530Sstevel@tonic-gate 		}
11540Sstevel@tonic-gate done_reg:
11554397Sschwartz 		prg.drvr_version = PCITOOL_VERSION;
11560Sstevel@tonic-gate 		if (ddi_copyout(&prg, arg, sizeof (pcitool_reg_t), mode) !=
11570Sstevel@tonic-gate 		    DDI_SUCCESS) {
11580Sstevel@tonic-gate 			if (pcitool_debug)
11590Sstevel@tonic-gate 				prom_printf("Error returning arguments.\n");
11600Sstevel@tonic-gate 			rval = EFAULT;
11610Sstevel@tonic-gate 		}
11620Sstevel@tonic-gate 		break;
11630Sstevel@tonic-gate 	default:
11640Sstevel@tonic-gate 		rval = ENOTTY;
11650Sstevel@tonic-gate 		break;
11660Sstevel@tonic-gate 	}
11670Sstevel@tonic-gate 	return (rval);
11680Sstevel@tonic-gate }
1169