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