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 
48777Sschwartz #define	PCIEX_BDF_OFFSET_DELTA	4
49777Sschwartz #define	PCIEX_REG_FUNC_SHIFT	(PCI_REG_FUNC_SHIFT + PCIEX_BDF_OFFSET_DELTA)
50777Sschwartz #define	PCIEX_REG_DEV_SHIFT	(PCI_REG_DEV_SHIFT + PCIEX_BDF_OFFSET_DELTA)
51777Sschwartz #define	PCIEX_REG_BUS_SHIFT	(PCI_REG_BUS_SHIFT + PCIEX_BDF_OFFSET_DELTA)
52777Sschwartz 
530Sstevel@tonic-gate #define	SUCCESS	0
540Sstevel@tonic-gate 
550Sstevel@tonic-gate int pcitool_debug = 0;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate  * Offsets of BARS in config space.  First entry of 0 means config space.
590Sstevel@tonic-gate  * Entries here correlate to pcitool_bars_t enumerated type.
600Sstevel@tonic-gate  */
610Sstevel@tonic-gate static uint8_t pci_bars[] = {
620Sstevel@tonic-gate 	0x0,
630Sstevel@tonic-gate 	PCI_CONF_BASE0,
640Sstevel@tonic-gate 	PCI_CONF_BASE1,
650Sstevel@tonic-gate 	PCI_CONF_BASE2,
660Sstevel@tonic-gate 	PCI_CONF_BASE3,
670Sstevel@tonic-gate 	PCI_CONF_BASE4,
680Sstevel@tonic-gate 	PCI_CONF_BASE5,
690Sstevel@tonic-gate 	PCI_CONF_ROM
700Sstevel@tonic-gate };
710Sstevel@tonic-gate 
72777Sschwartz /* Max offset allowed into config space for a particular device. */
73777Sschwartz static uint64_t max_cfg_size = PCI_CONF_HDR_SIZE;
74777Sschwartz 
750Sstevel@tonic-gate static uint64_t pcitool_swap_endian(uint64_t data, int size);
76777Sschwartz static int pcitool_pciex_cfg_access(dev_info_t *dip, pcitool_reg_t *prg,
77777Sschwartz     boolean_t write_flag);
780Sstevel@tonic-gate static int pcitool_cfg_access(dev_info_t *dip, pcitool_reg_t *prg,
790Sstevel@tonic-gate     boolean_t write_flag);
800Sstevel@tonic-gate static int pcitool_io_access(dev_info_t *dip, pcitool_reg_t *prg,
810Sstevel@tonic-gate     boolean_t write_flag);
820Sstevel@tonic-gate static int pcitool_mem_access(dev_info_t *dip, pcitool_reg_t *prg,
830Sstevel@tonic-gate     uint64_t virt_addr, boolean_t write_flag);
840Sstevel@tonic-gate static uint64_t pcitool_map(uint64_t phys_addr, size_t size, size_t *num_pages);
850Sstevel@tonic-gate static void pcitool_unmap(uint64_t virt_addr, size_t num_pages);
860Sstevel@tonic-gate 
87*4397Sschwartz /* Extern declarations */
88916Sschwartz extern int	(*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *,
89916Sschwartz 		    psm_intr_op_t, int *);
90916Sschwartz 
91117Sschwartz int
92777Sschwartz pcitool_init(dev_info_t *dip, boolean_t is_pciex)
93117Sschwartz {
94117Sschwartz 	int instance = ddi_get_instance(dip);
95117Sschwartz 
96117Sschwartz 	/* Create pcitool nodes for register access and interrupt routing. */
97117Sschwartz 
98117Sschwartz 	if (ddi_create_minor_node(dip, PCI_MINOR_REG, S_IFCHR,
99117Sschwartz 	    PCIHP_AP_MINOR_NUM(instance, PCI_TOOL_REG_MINOR_NUM),
100117Sschwartz 	    DDI_NT_REGACC, 0) != DDI_SUCCESS) {
101117Sschwartz 		return (DDI_FAILURE);
102117Sschwartz 	}
103117Sschwartz 
104117Sschwartz 	if (ddi_create_minor_node(dip, PCI_MINOR_INTR, S_IFCHR,
105117Sschwartz 	    PCIHP_AP_MINOR_NUM(instance, PCI_TOOL_INTR_MINOR_NUM),
106117Sschwartz 	    DDI_NT_INTRCTL, 0) != DDI_SUCCESS) {
107117Sschwartz 		ddi_remove_minor_node(dip, PCI_MINOR_REG);
108117Sschwartz 		return (DDI_FAILURE);
109117Sschwartz 	}
110117Sschwartz 
111777Sschwartz 	if (is_pciex)
112777Sschwartz 		max_cfg_size = PCIE_CONF_HDR_SIZE;
113777Sschwartz 
114117Sschwartz 	return (DDI_SUCCESS);
115117Sschwartz }
116117Sschwartz 
117117Sschwartz void
118117Sschwartz pcitool_uninit(dev_info_t *dip)
119117Sschwartz {
120117Sschwartz 	ddi_remove_minor_node(dip, PCI_MINOR_INTR);
121117Sschwartz 	ddi_remove_minor_node(dip, PCI_MINOR_REG);
122117Sschwartz }
123117Sschwartz 
124916Sschwartz /*ARGSUSED*/
125916Sschwartz static int
126916Sschwartz pcitool_set_intr(dev_info_t *dip, void *arg, int mode)
127916Sschwartz {
128916Sschwartz 	ddi_intr_handle_impl_t info_hdl;
129916Sschwartz 	pcitool_intr_set_t iset;
130916Sschwartz 	uint32_t old_cpu;
131916Sschwartz 	int ret, result;
132*4397Sschwartz 	size_t copyinout_size;
133916Sschwartz 	int rval = SUCCESS;
134916Sschwartz 
135*4397Sschwartz 	/* Version 1 of pcitool_intr_set_t doesn't have flags. */
136*4397Sschwartz 	copyinout_size = (size_t)&iset.flags - (size_t)&iset;
137*4397Sschwartz 
138*4397Sschwartz 	if (ddi_copyin(arg, &iset, copyinout_size, mode) != DDI_SUCCESS)
139916Sschwartz 		return (EFAULT);
140916Sschwartz 
141*4397Sschwartz 	switch (iset.user_version) {
142*4397Sschwartz 	case PCITOOL_V1:
143*4397Sschwartz 		break;
144*4397Sschwartz 
145*4397Sschwartz 	case PCITOOL_V2:
146*4397Sschwartz 		copyinout_size = sizeof (pcitool_intr_set_t);
147*4397Sschwartz 		if (ddi_copyin(arg, &iset, copyinout_size, mode) != DDI_SUCCESS)
148*4397Sschwartz 			return (EFAULT);
149*4397Sschwartz 		break;
150*4397Sschwartz 
151*4397Sschwartz 	default:
152*4397Sschwartz 		iset.status = PCITOOL_OUT_OF_RANGE;
153*4397Sschwartz 		rval = ENOTSUP;
154*4397Sschwartz 		goto done_set_intr;
155*4397Sschwartz 	}
156*4397Sschwartz 
157916Sschwartz 	if (iset.ino > APIC_MAX_VECTOR) {
158916Sschwartz 		rval = EINVAL;
159916Sschwartz 		iset.status = PCITOOL_INVALID_INO;
160916Sschwartz 		goto done_set_intr;
161916Sschwartz 	}
162916Sschwartz 
163916Sschwartz 	iset.status = PCITOOL_SUCCESS;
164916Sschwartz 
165916Sschwartz 	if ((old_cpu = pci_get_cpu_from_vecirq(iset.ino, IS_VEC)) == -1) {
166916Sschwartz 		iset.status = PCITOOL_IO_ERROR;
167916Sschwartz 		rval = EINVAL;
168916Sschwartz 		goto done_set_intr;
169916Sschwartz 	}
170916Sschwartz 
171*4397Sschwartz 
172916Sschwartz 	old_cpu &= ~PSMGI_CPU_USER_BOUND;
173916Sschwartz 
174916Sschwartz 	/*
175916Sschwartz 	 * For this locally-declared and used handle, ih_private will contain a
176916Sschwartz 	 * CPU value, not an ihdl_plat_t as used for global interrupt handling.
177916Sschwartz 	 */
178916Sschwartz 	info_hdl.ih_vector = iset.ino;
179916Sschwartz 	info_hdl.ih_private = (void *)(uintptr_t)iset.cpu_id;
180*4397Sschwartz 	if (pcitool_debug)
181*4397Sschwartz 		prom_printf("user version:%d, flags:0x%x\n",
182*4397Sschwartz 		    iset.user_version, iset.flags);
183916Sschwartz 
184*4397Sschwartz 	result = ENOTSUP;
185*4397Sschwartz 	if ((iset.user_version >= PCITOOL_V2) &&
186*4397Sschwartz 	    (iset.flags & PCITOOL_INTR_SET_FLAG_GROUP)) {
187*4397Sschwartz 		ret = (*psm_intr_ops)(NULL, &info_hdl, PSM_INTR_OP_GRP_SET_CPU,
188*4397Sschwartz 		    &result);
189*4397Sschwartz 	} else {
190*4397Sschwartz 		ret = (*psm_intr_ops)(NULL, &info_hdl, PSM_INTR_OP_SET_CPU,
191*4397Sschwartz 		    &result);
192*4397Sschwartz 	}
193*4397Sschwartz 
194916Sschwartz 	if (ret != PSM_SUCCESS) {
195916Sschwartz 		switch (result) {
196916Sschwartz 		case EIO:		/* Error making the change */
197916Sschwartz 			rval = EIO;
198916Sschwartz 			iset.status = PCITOOL_IO_ERROR;
199916Sschwartz 			break;
200916Sschwartz 		case ENXIO:		/* Couldn't convert vector to irq */
201916Sschwartz 			rval = EINVAL;
202916Sschwartz 			iset.status = PCITOOL_INVALID_INO;
203916Sschwartz 			break;
204916Sschwartz 		case EINVAL:		/* CPU out of range */
205916Sschwartz 			rval = EINVAL;
206916Sschwartz 			iset.status = PCITOOL_INVALID_CPUID;
207916Sschwartz 			break;
208*4397Sschwartz 		case ENOTSUP:		/* Requested PSM intr ops missing */
209*4397Sschwartz 			rval = ENOTSUP;
210*4397Sschwartz 			iset.status = PCITOOL_IO_ERROR;
211*4397Sschwartz 			break;
212916Sschwartz 		}
213916Sschwartz 	}
214916Sschwartz 
215916Sschwartz 	/* Return original CPU. */
216916Sschwartz 	iset.cpu_id = old_cpu;
217916Sschwartz 
218916Sschwartz done_set_intr:
219*4397Sschwartz 	iset.drvr_version = PCITOOL_VERSION;
220*4397Sschwartz 	if (ddi_copyout(&iset, arg, copyinout_size, mode) != DDI_SUCCESS)
221916Sschwartz 		rval = EFAULT;
222916Sschwartz 	return (rval);
223916Sschwartz }
224916Sschwartz 
225916Sschwartz 
226916Sschwartz /* It is assumed that dip != NULL */
227916Sschwartz static void
228916Sschwartz pcitool_get_intr_dev_info(dev_info_t *dip, pcitool_intr_dev_t *devs)
229916Sschwartz {
230916Sschwartz 	(void) strncpy(devs->driver_name,
231916Sschwartz 	    ddi_driver_name(dip), MAXMODCONFNAME-1);
232916Sschwartz 	devs->driver_name[MAXMODCONFNAME] = '\0';
233916Sschwartz 	(void) ddi_pathname(dip, devs->path);
234916Sschwartz 	devs->dev_inst = ddi_get_instance(dip);
235916Sschwartz }
236916Sschwartz 
237916Sschwartz 
238916Sschwartz /*ARGSUSED*/
239916Sschwartz static int
240916Sschwartz pcitool_get_intr(dev_info_t *dip, void *arg, int mode)
241916Sschwartz {
242916Sschwartz 	/* Array part isn't used here, but oh well... */
243916Sschwartz 	pcitool_intr_get_t partial_iget;
244916Sschwartz 	pcitool_intr_get_t *iget = &partial_iget;
245916Sschwartz 	size_t	iget_kmem_alloc_size = 0;
246916Sschwartz 	uint8_t num_devs_ret;
247916Sschwartz 	int copyout_rval;
248916Sschwartz 	int rval = SUCCESS;
249916Sschwartz 	int circ;
250916Sschwartz 	int i;
251916Sschwartz 
252916Sschwartz 	ddi_intr_handle_impl_t info_hdl;
253916Sschwartz 	apic_get_intr_t intr_info;
254916Sschwartz 
255916Sschwartz 	/* Read in just the header part, no array section. */
256916Sschwartz 	if (ddi_copyin(arg, &partial_iget, PCITOOL_IGET_SIZE(0), mode) !=
257916Sschwartz 	    DDI_SUCCESS)
258916Sschwartz 		return (EFAULT);
259916Sschwartz 
260916Sschwartz 	/* Validate argument. */
261916Sschwartz 	if (partial_iget.ino > APIC_MAX_VECTOR) {
262916Sschwartz 		partial_iget.status = PCITOOL_INVALID_INO;
263916Sschwartz 		partial_iget.num_devs_ret = 0;
264916Sschwartz 		rval = EINVAL;
265916Sschwartz 		goto done_get_intr;
266916Sschwartz 	}
267916Sschwartz 
268916Sschwartz 	num_devs_ret = partial_iget.num_devs_ret;
269916Sschwartz 	intr_info.avgi_dip_list = NULL;
270916Sschwartz 	intr_info.avgi_req_flags =
271916Sschwartz 	    PSMGI_REQ_CPUID | PSMGI_REQ_NUM_DEVS | PSMGI_INTRBY_VEC;
272916Sschwartz 	/*
273916Sschwartz 	 * For this locally-declared and used handle, ih_private will contain a
274916Sschwartz 	 * pointer to apic_get_intr_t, not an ihdl_plat_t as used for
275916Sschwartz 	 * global interrupt handling.
276916Sschwartz 	 */
277916Sschwartz 	info_hdl.ih_private = &intr_info;
278916Sschwartz 	info_hdl.ih_vector = partial_iget.ino;
279916Sschwartz 
280916Sschwartz 	/* Caller wants device information returned. */
281916Sschwartz 	if (num_devs_ret > 0) {
282916Sschwartz 
283916Sschwartz 		intr_info.avgi_req_flags |= PSMGI_REQ_GET_DEVS;
284916Sschwartz 
285916Sschwartz 		/*
286916Sschwartz 		 * Allocate room.
287916Sschwartz 		 * If num_devs_ret == 0 iget remains pointing to partial_iget.
288916Sschwartz 		 */
289916Sschwartz 		iget_kmem_alloc_size = PCITOOL_IGET_SIZE(num_devs_ret);
290916Sschwartz 		iget = kmem_alloc(iget_kmem_alloc_size, KM_SLEEP);
291916Sschwartz 
292916Sschwartz 		/* Read in whole structure to verify there's room. */
293916Sschwartz 		if (ddi_copyin(arg, iget, iget_kmem_alloc_size, mode) !=
294916Sschwartz 		    SUCCESS) {
295916Sschwartz 
296916Sschwartz 			/* Be consistent and just return EFAULT here. */
297916Sschwartz 			kmem_free(iget, iget_kmem_alloc_size);
298916Sschwartz 
299916Sschwartz 			return (EFAULT);
300916Sschwartz 		}
301916Sschwartz 	}
302916Sschwartz 
303916Sschwartz 	bzero(iget, PCITOOL_IGET_SIZE(num_devs_ret));
304916Sschwartz 	iget->ino = info_hdl.ih_vector;
305916Sschwartz 
306916Sschwartz 	/*
307916Sschwartz 	 * Lock device tree branch from the pci root nexus on down if info will
308916Sschwartz 	 * be extracted from dips returned from the tree.
309916Sschwartz 	 */
310916Sschwartz 	if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) {
311916Sschwartz 		ndi_devi_enter(dip, &circ);
312916Sschwartz 	}
313916Sschwartz 
314916Sschwartz 	/* Call psm_intr_ops(PSM_INTR_OP_GET_INTR) to get information. */
315916Sschwartz 	if ((rval = (*psm_intr_ops)(NULL, &info_hdl,
316916Sschwartz 	    PSM_INTR_OP_GET_INTR, NULL)) != PSM_SUCCESS) {
317916Sschwartz 		iget->status = PCITOOL_IO_ERROR;
318916Sschwartz 		iget->num_devs_ret = 0;
319916Sschwartz 		rval = EINVAL;
320916Sschwartz 		goto done_get_intr;
321916Sschwartz 	}
322916Sschwartz 
323916Sschwartz 	/*
324916Sschwartz 	 * Fill in the pcitool_intr_get_t to be returned,
325916Sschwartz 	 * with the CPU, num_devs_ret and num_devs.
326916Sschwartz 	 */
327916Sschwartz 	iget->cpu_id = intr_info.avgi_cpu_id & ~PSMGI_CPU_USER_BOUND;
328916Sschwartz 
329916Sschwartz 	/* Number of devices returned by apic. */
330916Sschwartz 	iget->num_devs = intr_info.avgi_num_devs;
331916Sschwartz 
332916Sschwartz 	/* Device info was returned. */
333916Sschwartz 	if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) {
334916Sschwartz 
335916Sschwartz 		/*
336916Sschwartz 		 * num devs returned is num devs ret by apic,
337916Sschwartz 		 * space permitting.
338916Sschwartz 		 */
339916Sschwartz 		iget->num_devs_ret = min(num_devs_ret, intr_info.avgi_num_devs);
340916Sschwartz 
341916Sschwartz 		/*
342916Sschwartz 		 * Loop thru list of dips and extract driver, name and instance.
343916Sschwartz 		 * Fill in the pcitool_intr_dev_t's with this info.
344916Sschwartz 		 */
345916Sschwartz 		for (i = 0; i < iget->num_devs_ret; i++)
346916Sschwartz 			pcitool_get_intr_dev_info(intr_info.avgi_dip_list[i],
347916Sschwartz 			    &iget->dev[i]);
348916Sschwartz 
349916Sschwartz 		/* Free kmem_alloc'ed memory of the apic_get_intr_t */
350916Sschwartz 		kmem_free(intr_info.avgi_dip_list,
351916Sschwartz 		    intr_info.avgi_num_devs * sizeof (dev_info_t *));
352916Sschwartz 	}
353916Sschwartz 
354916Sschwartz done_get_intr:
355916Sschwartz 
356916Sschwartz 	if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) {
357916Sschwartz 		ndi_devi_exit(dip, circ);
358916Sschwartz 	}
359916Sschwartz 
360*4397Sschwartz 	iget->drvr_version = PCITOOL_VERSION;
361916Sschwartz 	copyout_rval = ddi_copyout(iget, arg,
362916Sschwartz 	    PCITOOL_IGET_SIZE(num_devs_ret), mode);
363916Sschwartz 
364916Sschwartz 	if (iget_kmem_alloc_size > 0)
365916Sschwartz 		kmem_free(iget, iget_kmem_alloc_size);
366916Sschwartz 
367916Sschwartz 	if (copyout_rval != DDI_SUCCESS)
368916Sschwartz 		rval = EFAULT;
369916Sschwartz 
370916Sschwartz 	return (rval);
371916Sschwartz }
372916Sschwartz 
373*4397Sschwartz /*ARGSUSED*/
374*4397Sschwartz static int
375*4397Sschwartz pcitool_intr_info(dev_info_t *dip, void *arg, int mode)
376*4397Sschwartz {
377*4397Sschwartz 	pcitool_intr_info_t intr_info;
378*4397Sschwartz 	ddi_intr_handle_impl_t info_hdl;
379*4397Sschwartz 	int rval = SUCCESS;
380*4397Sschwartz 
381*4397Sschwartz 	/* If we need user_version, and to ret same user version as passed in */
382*4397Sschwartz 	if (ddi_copyin(arg, &intr_info, sizeof (pcitool_intr_info_t), mode) !=
383*4397Sschwartz 	    DDI_SUCCESS) {
384*4397Sschwartz 		if (pcitool_debug)
385*4397Sschwartz 			prom_printf("Error reading arguments\n");
386*4397Sschwartz 		return (EFAULT);
387*4397Sschwartz 	}
388*4397Sschwartz 
389*4397Sschwartz 	/* For UPPC systems, psm_intr_ops has no entry for APIC_TYPE. */
390*4397Sschwartz 	if ((rval = (*psm_intr_ops)(NULL, &info_hdl,
391*4397Sschwartz 	    PSM_INTR_OP_APIC_TYPE, NULL)) != PSM_SUCCESS) {
392*4397Sschwartz 		intr_info.ctlr_type = PCITOOL_CTLR_TYPE_UPPC;
393*4397Sschwartz 		intr_info.ctlr_version = 0;
394*4397Sschwartz 
395*4397Sschwartz 	} else {
396*4397Sschwartz 		intr_info.ctlr_version = (uint32_t)info_hdl.ih_ver;
397*4397Sschwartz 		if (strcmp((char *)info_hdl.ih_private,
398*4397Sschwartz 		    APIC_PCPLUSMP_NAME) == 0)
399*4397Sschwartz 			intr_info.ctlr_type = PCITOOL_CTLR_TYPE_PCPLUSMP;
400*4397Sschwartz 		else
401*4397Sschwartz 			intr_info.ctlr_type = PCITOOL_CTLR_TYPE_UNKNOWN;
402*4397Sschwartz 	}
403*4397Sschwartz 
404*4397Sschwartz 	intr_info.num_intr = APIC_MAX_VECTOR;
405*4397Sschwartz 	intr_info.drvr_version = PCITOOL_VERSION;
406*4397Sschwartz 	if (ddi_copyout(&intr_info, arg, sizeof (pcitool_intr_info_t), mode) !=
407*4397Sschwartz 	    DDI_SUCCESS) {
408*4397Sschwartz 		if (pcitool_debug)
409*4397Sschwartz 			prom_printf("Error returning arguments.\n");
410*4397Sschwartz 		rval = EFAULT;
411*4397Sschwartz 	}
412*4397Sschwartz 
413*4397Sschwartz 	return (rval);
414*4397Sschwartz }
415*4397Sschwartz 
416*4397Sschwartz 
417916Sschwartz 
418916Sschwartz /*
419916Sschwartz  * Main function for handling interrupt CPU binding requests and queries.
420916Sschwartz  * Need to implement later
421916Sschwartz  */
422916Sschwartz /*ARGSUSED*/
423916Sschwartz int
424916Sschwartz pcitool_intr_admn(dev_info_t *dip, void *arg, int cmd, int mode)
425916Sschwartz {
426916Sschwartz 	int rval;
427916Sschwartz 
428916Sschwartz 	switch (cmd) {
429916Sschwartz 
430916Sschwartz 	/* Associate a new CPU with a given vector */
431916Sschwartz 	case PCITOOL_DEVICE_SET_INTR:
432916Sschwartz 		rval = pcitool_set_intr(dip, arg, mode);
433916Sschwartz 		break;
434916Sschwartz 
435916Sschwartz 	case PCITOOL_DEVICE_GET_INTR:
436916Sschwartz 		rval = pcitool_get_intr(dip, arg, mode);
437916Sschwartz 		break;
438916Sschwartz 
439*4397Sschwartz 	case PCITOOL_SYSTEM_INTR_INFO:
440*4397Sschwartz 		rval = pcitool_intr_info(dip, arg, mode);
441916Sschwartz 		break;
442916Sschwartz 
443916Sschwartz 	default:
444916Sschwartz 		rval = ENOTSUP;
445916Sschwartz 	}
446916Sschwartz 
447916Sschwartz 	return (rval);
448916Sschwartz }
449916Sschwartz 
450916Sschwartz 
4510Sstevel@tonic-gate /*
4520Sstevel@tonic-gate  * A note about ontrap handling:
4530Sstevel@tonic-gate  *
4540Sstevel@tonic-gate  * X86 systems on which this module was tested return FFs instead of bus errors
4550Sstevel@tonic-gate  * when accessing devices with invalid addresses.  Ontrap handling, which
4560Sstevel@tonic-gate  * gracefully handles kernel bus errors, is installed anyway, in case future
4570Sstevel@tonic-gate  * X86 platforms require it.
4580Sstevel@tonic-gate  */
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate /*
4610Sstevel@tonic-gate  * Perform register accesses on the nexus device itself.
4620Sstevel@tonic-gate  * No explicit PCI nexus device for X86, so not applicable.
4630Sstevel@tonic-gate  */
464916Sschwartz 
4650Sstevel@tonic-gate /*ARGSUSED*/
4660Sstevel@tonic-gate int
467777Sschwartz pcitool_bus_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode)
4680Sstevel@tonic-gate {
4690Sstevel@tonic-gate 	return (ENOTSUP);
4700Sstevel@tonic-gate }
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate /* Swap endianness. */
4730Sstevel@tonic-gate static uint64_t
4740Sstevel@tonic-gate pcitool_swap_endian(uint64_t data, int size)
4750Sstevel@tonic-gate {
4760Sstevel@tonic-gate 	typedef union {
4770Sstevel@tonic-gate 		uint64_t data64;
4780Sstevel@tonic-gate 		uint8_t data8[8];
4790Sstevel@tonic-gate 	} data_split_t;
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	data_split_t orig_data;
4820Sstevel@tonic-gate 	data_split_t returned_data;
4830Sstevel@tonic-gate 	int i;
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	orig_data.data64 = data;
4860Sstevel@tonic-gate 	returned_data.data64 = 0;
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	for (i = 0; i < size; i++) {
4890Sstevel@tonic-gate 		returned_data.data8[i] = orig_data.data8[size - 1 - i];
4900Sstevel@tonic-gate 	}
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	return (returned_data.data64);
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 
496777Sschwartz /*
497777Sschwartz  * Access device.  prg is modified.
498777Sschwartz  *
499777Sschwartz  * Extended config space is available only through memory-mapped access.
500777Sschwartz  * Standard config space on pci express devices is available either way,
501777Sschwartz  * so do it memory-mapped here too, for simplicity.
502777Sschwartz  */
503777Sschwartz /*ARGSUSED*/
504777Sschwartz static int
505777Sschwartz pcitool_pciex_cfg_access(dev_info_t *dip, pcitool_reg_t *prg,
506777Sschwartz     boolean_t write_flag)
507777Sschwartz {
508777Sschwartz 	int rval = SUCCESS;
509777Sschwartz 	uint64_t virt_addr;
510777Sschwartz 	size_t	num_virt_pages;
511777Sschwartz 
512777Sschwartz 	prg->status = PCITOOL_SUCCESS;
513777Sschwartz 
514777Sschwartz 	prg->phys_addr = ddi_prop_get_int64(DDI_DEV_T_ANY, dip, 0,
515881Sjohnny 	    "ecfga-base-address", 0);
516777Sschwartz 	if (prg->phys_addr == 0) {
517777Sschwartz 		prg->status = PCITOOL_IO_ERROR;
518777Sschwartz 		return (EIO);
519777Sschwartz 	}
520777Sschwartz 
521777Sschwartz 	prg->phys_addr += prg->offset +
522777Sschwartz 	    ((prg->bus_no << PCIEX_REG_BUS_SHIFT) |
523777Sschwartz 	    (prg->dev_no << PCIEX_REG_DEV_SHIFT) |
524777Sschwartz 	    (prg->func_no << PCIEX_REG_FUNC_SHIFT));
525777Sschwartz 
526777Sschwartz 	virt_addr = pcitool_map(prg->phys_addr,
527777Sschwartz 	    PCITOOL_ACC_ATTR_SIZE(prg->acc_attr), &num_virt_pages);
528777Sschwartz 	if (virt_addr == NULL) {
529777Sschwartz 		prg->status = PCITOOL_IO_ERROR;
530777Sschwartz 		return (EIO);
531777Sschwartz 	}
532777Sschwartz 
533777Sschwartz 	rval = pcitool_mem_access(dip, prg, virt_addr, write_flag);
534777Sschwartz 	pcitool_unmap(virt_addr, num_virt_pages);
535777Sschwartz 	return (rval);
536777Sschwartz }
537777Sschwartz 
5380Sstevel@tonic-gate /* Access device.  prg is modified. */
5390Sstevel@tonic-gate /*ARGSUSED*/
5400Sstevel@tonic-gate static int
5410Sstevel@tonic-gate pcitool_cfg_access(dev_info_t *dip, pcitool_reg_t *prg, boolean_t write_flag)
5420Sstevel@tonic-gate {
5430Sstevel@tonic-gate 	int size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
5440Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
5450Sstevel@tonic-gate 	int rval = SUCCESS;
5460Sstevel@tonic-gate 	uint64_t local_data;
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	/*
5490Sstevel@tonic-gate 	 * NOTE: there is no way to verify whether or not the address is valid.
5500Sstevel@tonic-gate 	 * The put functions return void and the get functions return ff on
5510Sstevel@tonic-gate 	 * error.
5520Sstevel@tonic-gate 	 */
5530Sstevel@tonic-gate 	prg->status = PCITOOL_SUCCESS;
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	if (write_flag) {
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 		if (big_endian) {
5580Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
5590Sstevel@tonic-gate 		} else {
5600Sstevel@tonic-gate 			local_data = prg->data;
5610Sstevel@tonic-gate 		}
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 		switch (size) {
5640Sstevel@tonic-gate 		case 1:
5650Sstevel@tonic-gate 			(*pci_putb_func)(prg->bus_no, prg->dev_no,
5660Sstevel@tonic-gate 			    prg->func_no, prg->offset, local_data);
5670Sstevel@tonic-gate 			break;
5680Sstevel@tonic-gate 		case 2:
5690Sstevel@tonic-gate 			(*pci_putw_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 4:
5730Sstevel@tonic-gate 			(*pci_putl_func)(prg->bus_no, prg->dev_no,
5740Sstevel@tonic-gate 			    prg->func_no, prg->offset, local_data);
5750Sstevel@tonic-gate 			break;
5760Sstevel@tonic-gate 		default:
5770Sstevel@tonic-gate 			rval = ENOTSUP;
5780Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
5790Sstevel@tonic-gate 			break;
5800Sstevel@tonic-gate 		}
5810Sstevel@tonic-gate 	} else {
5820Sstevel@tonic-gate 		switch (size) {
5830Sstevel@tonic-gate 		case 1:
5840Sstevel@tonic-gate 			local_data = (*pci_getb_func)(prg->bus_no, prg->dev_no,
5850Sstevel@tonic-gate 			    prg->func_no, prg->offset);
5860Sstevel@tonic-gate 			break;
5870Sstevel@tonic-gate 		case 2:
5880Sstevel@tonic-gate 			local_data = (*pci_getw_func)(prg->bus_no, prg->dev_no,
5890Sstevel@tonic-gate 			    prg->func_no, prg->offset);
5900Sstevel@tonic-gate 			break;
5910Sstevel@tonic-gate 		case 4:
5920Sstevel@tonic-gate 			local_data = (*pci_getl_func)(prg->bus_no, prg->dev_no,
5930Sstevel@tonic-gate 			    prg->func_no, prg->offset);
5940Sstevel@tonic-gate 			break;
5950Sstevel@tonic-gate 		default:
5960Sstevel@tonic-gate 			rval = ENOTSUP;
5970Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
5980Sstevel@tonic-gate 			break;
5990Sstevel@tonic-gate 		}
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 		if (rval == SUCCESS) {
6020Sstevel@tonic-gate 			if (big_endian) {
6030Sstevel@tonic-gate 				prg->data =
6040Sstevel@tonic-gate 				    pcitool_swap_endian(local_data, size);
6050Sstevel@tonic-gate 			} else {
6060Sstevel@tonic-gate 				prg->data = local_data;
6070Sstevel@tonic-gate 			}
6080Sstevel@tonic-gate 		}
6090Sstevel@tonic-gate 	}
6100Sstevel@tonic-gate 	prg->phys_addr = 0;	/* Config space is not memory mapped on X86. */
6110Sstevel@tonic-gate 	return (rval);
6120Sstevel@tonic-gate }
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate /*ARGSUSED*/
6160Sstevel@tonic-gate static int
6170Sstevel@tonic-gate pcitool_io_access(dev_info_t *dip, pcitool_reg_t *prg, boolean_t write_flag)
6180Sstevel@tonic-gate {
6190Sstevel@tonic-gate 	int port = (int)prg->phys_addr;
6200Sstevel@tonic-gate 	size_t size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
6210Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
6220Sstevel@tonic-gate 	int rval = SUCCESS;
6230Sstevel@tonic-gate 	on_trap_data_t otd;
6240Sstevel@tonic-gate 	uint64_t local_data;
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	/*
6280Sstevel@tonic-gate 	 * on_trap works like setjmp.
6290Sstevel@tonic-gate 	 *
6300Sstevel@tonic-gate 	 * A non-zero return here means on_trap has returned from an error.
6310Sstevel@tonic-gate 	 *
6320Sstevel@tonic-gate 	 * A zero return here means that on_trap has just returned from setup.
6330Sstevel@tonic-gate 	 */
6340Sstevel@tonic-gate 	if (on_trap(&otd, OT_DATA_ACCESS)) {
6350Sstevel@tonic-gate 		no_trap();
6360Sstevel@tonic-gate 		if (pcitool_debug)
6370Sstevel@tonic-gate 			prom_printf(
638*4397Sschwartz 			    "pcitool_io_access: on_trap caught an error...\n");
6390Sstevel@tonic-gate 		prg->status = PCITOOL_INVALID_ADDRESS;
6400Sstevel@tonic-gate 		return (EFAULT);
6410Sstevel@tonic-gate 	}
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	if (write_flag) {
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 		if (big_endian) {
6460Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
6470Sstevel@tonic-gate 		} else {
6480Sstevel@tonic-gate 			local_data = prg->data;
6490Sstevel@tonic-gate 		}
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 		if (pcitool_debug)
6520Sstevel@tonic-gate 			prom_printf("Writing %ld byte(s) to port 0x%x\n",
6530Sstevel@tonic-gate 			    size, port);
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 		switch (size) {
6560Sstevel@tonic-gate 		case 1:
6570Sstevel@tonic-gate 			outb(port, (uint8_t)local_data);
6580Sstevel@tonic-gate 			break;
6590Sstevel@tonic-gate 		case 2:
6600Sstevel@tonic-gate 			outw(port, (uint16_t)local_data);
6610Sstevel@tonic-gate 			break;
6620Sstevel@tonic-gate 		case 4:
6630Sstevel@tonic-gate 			outl(port, (uint32_t)local_data);
6640Sstevel@tonic-gate 			break;
6650Sstevel@tonic-gate 		default:
6660Sstevel@tonic-gate 			rval = ENOTSUP;
6670Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
6680Sstevel@tonic-gate 			break;
6690Sstevel@tonic-gate 		}
6700Sstevel@tonic-gate 	} else {
6710Sstevel@tonic-gate 		if (pcitool_debug)
6720Sstevel@tonic-gate 			prom_printf("Reading %ld byte(s) from port 0x%x\n",
6730Sstevel@tonic-gate 			    size, port);
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 		switch (size) {
6760Sstevel@tonic-gate 		case 1:
6770Sstevel@tonic-gate 			local_data = inb(port);
6780Sstevel@tonic-gate 			break;
6790Sstevel@tonic-gate 		case 2:
6800Sstevel@tonic-gate 			local_data = inw(port);
6810Sstevel@tonic-gate 			break;
6820Sstevel@tonic-gate 		case 4:
6830Sstevel@tonic-gate 			local_data = inl(port);
6840Sstevel@tonic-gate 			break;
6850Sstevel@tonic-gate 		default:
6860Sstevel@tonic-gate 			rval = ENOTSUP;
6870Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
6880Sstevel@tonic-gate 			break;
6890Sstevel@tonic-gate 		}
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 		if (rval == SUCCESS) {
6920Sstevel@tonic-gate 			if (big_endian) {
6930Sstevel@tonic-gate 				prg->data =
6940Sstevel@tonic-gate 				    pcitool_swap_endian(local_data, size);
6950Sstevel@tonic-gate 			} else {
6960Sstevel@tonic-gate 				prg->data = local_data;
6970Sstevel@tonic-gate 			}
6980Sstevel@tonic-gate 		}
6990Sstevel@tonic-gate 	}
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	no_trap();
7020Sstevel@tonic-gate 	return (rval);
7030Sstevel@tonic-gate }
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate /*ARGSUSED*/
7060Sstevel@tonic-gate static int
7070Sstevel@tonic-gate pcitool_mem_access(dev_info_t *dip, pcitool_reg_t *prg, uint64_t virt_addr,
708117Sschwartz 	boolean_t write_flag)
7090Sstevel@tonic-gate {
7100Sstevel@tonic-gate 	size_t size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
7110Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
7120Sstevel@tonic-gate 	int rval = DDI_SUCCESS;
7130Sstevel@tonic-gate 	on_trap_data_t otd;
7140Sstevel@tonic-gate 	uint64_t local_data;
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	/*
7170Sstevel@tonic-gate 	 * on_trap works like setjmp.
7180Sstevel@tonic-gate 	 *
7190Sstevel@tonic-gate 	 * A non-zero return here means on_trap has returned from an error.
7200Sstevel@tonic-gate 	 *
7210Sstevel@tonic-gate 	 * A zero return here means that on_trap has just returned from setup.
7220Sstevel@tonic-gate 	 */
7230Sstevel@tonic-gate 	if (on_trap(&otd, OT_DATA_ACCESS)) {
7240Sstevel@tonic-gate 		no_trap();
7250Sstevel@tonic-gate 		if (pcitool_debug)
7260Sstevel@tonic-gate 			prom_printf(
7270Sstevel@tonic-gate 			    "pcitool_mem_access: on_trap caught an error...\n");
7280Sstevel@tonic-gate 		prg->status = PCITOOL_INVALID_ADDRESS;
7290Sstevel@tonic-gate 		return (EFAULT);
7300Sstevel@tonic-gate 	}
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	if (write_flag) {
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 		if (big_endian) {
7350Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
7360Sstevel@tonic-gate 		} else {
7370Sstevel@tonic-gate 			local_data = prg->data;
7380Sstevel@tonic-gate 		}
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 		switch (size) {
7410Sstevel@tonic-gate 		case 1:
7420Sstevel@tonic-gate 			*((uint8_t *)(uintptr_t)virt_addr) = local_data;
7430Sstevel@tonic-gate 			break;
7440Sstevel@tonic-gate 		case 2:
7450Sstevel@tonic-gate 			*((uint16_t *)(uintptr_t)virt_addr) = local_data;
7460Sstevel@tonic-gate 			break;
7470Sstevel@tonic-gate 		case 4:
7480Sstevel@tonic-gate 			*((uint32_t *)(uintptr_t)virt_addr) = local_data;
7490Sstevel@tonic-gate 			break;
7500Sstevel@tonic-gate 		case 8:
7510Sstevel@tonic-gate 			*((uint64_t *)(uintptr_t)virt_addr) = local_data;
7520Sstevel@tonic-gate 			break;
7530Sstevel@tonic-gate 		default:
7540Sstevel@tonic-gate 			rval = ENOTSUP;
7550Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
7560Sstevel@tonic-gate 			break;
7570Sstevel@tonic-gate 		}
7580Sstevel@tonic-gate 	} else {
7590Sstevel@tonic-gate 		switch (size) {
7600Sstevel@tonic-gate 		case 1:
7610Sstevel@tonic-gate 			local_data = *((uint8_t *)(uintptr_t)virt_addr);
7620Sstevel@tonic-gate 			break;
7630Sstevel@tonic-gate 		case 2:
7640Sstevel@tonic-gate 			local_data = *((uint16_t *)(uintptr_t)virt_addr);
7650Sstevel@tonic-gate 			break;
7660Sstevel@tonic-gate 		case 4:
7670Sstevel@tonic-gate 			local_data = *((uint32_t *)(uintptr_t)virt_addr);
7680Sstevel@tonic-gate 			break;
7690Sstevel@tonic-gate 		case 8:
7700Sstevel@tonic-gate 			local_data = *((uint64_t *)(uintptr_t)virt_addr);
7710Sstevel@tonic-gate 			break;
7720Sstevel@tonic-gate 		default:
7730Sstevel@tonic-gate 			rval = ENOTSUP;
7740Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
7750Sstevel@tonic-gate 			break;
7760Sstevel@tonic-gate 		}
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 		if (rval == SUCCESS) {
7790Sstevel@tonic-gate 			if (big_endian) {
7800Sstevel@tonic-gate 				prg->data =
7810Sstevel@tonic-gate 				    pcitool_swap_endian(local_data, size);
7820Sstevel@tonic-gate 			} else {
7830Sstevel@tonic-gate 				prg->data = local_data;
7840Sstevel@tonic-gate 			}
7850Sstevel@tonic-gate 		}
7860Sstevel@tonic-gate 	}
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	no_trap();
7890Sstevel@tonic-gate 	return (rval);
7900Sstevel@tonic-gate }
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate /*
7930Sstevel@tonic-gate  * Map up to 2 pages which contain the address we want to access.
7940Sstevel@tonic-gate  *
7950Sstevel@tonic-gate  * Mapping should span no more than 8 bytes.  With X86 it is possible for an
7960Sstevel@tonic-gate  * 8 byte value to start on a 4 byte boundary, so it can cross a page boundary.
7970Sstevel@tonic-gate  * We'll never have to map more than two pages.
7980Sstevel@tonic-gate  */
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate static uint64_t
8010Sstevel@tonic-gate pcitool_map(uint64_t phys_addr, size_t size, size_t *num_pages)
8020Sstevel@tonic-gate {
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	uint64_t page_base = phys_addr & ~MMU_PAGEOFFSET;
8050Sstevel@tonic-gate 	uint64_t offset = phys_addr & MMU_PAGEOFFSET;
8060Sstevel@tonic-gate 	void *virt_base;
8070Sstevel@tonic-gate 	uint64_t returned_addr;
8083446Smrj 	pfn_t pfn;
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	if (pcitool_debug)
8110Sstevel@tonic-gate 		prom_printf("pcitool_map: Called with PA:0x%p\n",
8120Sstevel@tonic-gate 		    (uint8_t *)(uintptr_t)phys_addr);
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	*num_pages = 1;
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	/* Desired mapping would span more than two pages. */
8170Sstevel@tonic-gate 	if ((offset + size) > (MMU_PAGESIZE * 2)) {
8180Sstevel@tonic-gate 		if (pcitool_debug)
8190Sstevel@tonic-gate 			prom_printf("boundary violation: "
820777Sschwartz 			    "offset:0x%" PRIx64 ", size:%ld, pagesize:0x%lx\n",
821777Sschwartz 			    offset, (uintptr_t)size, (uintptr_t)MMU_PAGESIZE);
8220Sstevel@tonic-gate 		return (NULL);
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	} else if ((offset + size) > MMU_PAGESIZE) {
8250Sstevel@tonic-gate 		(*num_pages)++;
8260Sstevel@tonic-gate 	}
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	/* Get page(s) of virtual space. */
8290Sstevel@tonic-gate 	virt_base = vmem_alloc(heap_arena, ptob(*num_pages), VM_NOSLEEP);
8300Sstevel@tonic-gate 	if (virt_base == NULL) {
8310Sstevel@tonic-gate 		if (pcitool_debug)
8320Sstevel@tonic-gate 			prom_printf("Couldn't get virtual base address.\n");
8330Sstevel@tonic-gate 		return (NULL);
8340Sstevel@tonic-gate 	}
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	if (pcitool_debug)
8370Sstevel@tonic-gate 		prom_printf("Got base virtual address:0x%p\n", virt_base);
8380Sstevel@tonic-gate 
8393446Smrj 	pfn = btop(page_base);
8403446Smrj 
8410Sstevel@tonic-gate 	/* Now map the allocated virtual space to the physical address. */
8423446Smrj 	hat_devload(kas.a_hat, virt_base, mmu_ptob(*num_pages), pfn,
8433446Smrj 	    PROT_READ | PROT_WRITE | HAT_STRICTORDER,
8440Sstevel@tonic-gate 	    HAT_LOAD_LOCK);
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	returned_addr = ((uintptr_t)(virt_base)) + offset;
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	if (pcitool_debug)
8490Sstevel@tonic-gate 		prom_printf("pcitool_map: returning VA:0x%p\n",
8500Sstevel@tonic-gate 		    (void *)(uintptr_t)returned_addr);
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	return (returned_addr);
8530Sstevel@tonic-gate }
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate /* Unmap the mapped page(s). */
8560Sstevel@tonic-gate static void
8570Sstevel@tonic-gate pcitool_unmap(uint64_t virt_addr, size_t num_pages)
8580Sstevel@tonic-gate {
8590Sstevel@tonic-gate 	void *base_virt_addr = (void *)(uintptr_t)(virt_addr & ~MMU_PAGEOFFSET);
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	hat_unload(kas.a_hat, base_virt_addr, ptob(num_pages),
8620Sstevel@tonic-gate 	    HAT_UNLOAD_UNLOCK);
8630Sstevel@tonic-gate 	vmem_free(heap_arena, base_virt_addr, ptob(num_pages));
8640Sstevel@tonic-gate }
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate /* Perform register accesses on PCI leaf devices. */
8680Sstevel@tonic-gate int
869777Sschwartz pcitool_dev_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode)
8700Sstevel@tonic-gate {
8710Sstevel@tonic-gate 	boolean_t	write_flag = B_FALSE;
8720Sstevel@tonic-gate 	int		rval = 0;
8730Sstevel@tonic-gate 	pcitool_reg_t	prg;
8740Sstevel@tonic-gate 	uint8_t		size;
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	uint64_t	base_addr;
8770Sstevel@tonic-gate 	uint64_t	virt_addr;
8780Sstevel@tonic-gate 	size_t		num_virt_pages;
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	switch (cmd) {
8810Sstevel@tonic-gate 	case (PCITOOL_DEVICE_SET_REG):
8820Sstevel@tonic-gate 		write_flag = B_TRUE;
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	/*FALLTHRU*/
8850Sstevel@tonic-gate 	case (PCITOOL_DEVICE_GET_REG):
8860Sstevel@tonic-gate 		if (pcitool_debug)
8870Sstevel@tonic-gate 			prom_printf("pci_dev_reg_ops set/get reg\n");
8880Sstevel@tonic-gate 		if (ddi_copyin(arg, &prg, sizeof (pcitool_reg_t), mode) !=
8890Sstevel@tonic-gate 		    DDI_SUCCESS) {
8900Sstevel@tonic-gate 			if (pcitool_debug)
8910Sstevel@tonic-gate 				prom_printf("Error reading arguments\n");
8920Sstevel@tonic-gate 			return (EFAULT);
8930Sstevel@tonic-gate 		}
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 		if (prg.barnum >= (sizeof (pci_bars) / sizeof (pci_bars[0]))) {
8960Sstevel@tonic-gate 			prg.status = PCITOOL_OUT_OF_RANGE;
8970Sstevel@tonic-gate 			rval = EINVAL;
8980Sstevel@tonic-gate 			goto done_reg;
8990Sstevel@tonic-gate 		}
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 		if (pcitool_debug)
9020Sstevel@tonic-gate 			prom_printf("raw bus:0x%x, dev:0x%x, func:0x%x\n",
9030Sstevel@tonic-gate 			    prg.bus_no, prg.dev_no, prg.func_no);
9040Sstevel@tonic-gate 		/* Validate address arguments of bus / dev / func */
9050Sstevel@tonic-gate 		if (((prg.bus_no &
9060Sstevel@tonic-gate 		    (PCI_REG_BUS_M >> PCI_REG_BUS_SHIFT)) !=
9070Sstevel@tonic-gate 		    prg.bus_no) ||
9080Sstevel@tonic-gate 		    ((prg.dev_no &
9090Sstevel@tonic-gate 		    (PCI_REG_DEV_M >> PCI_REG_DEV_SHIFT)) !=
9100Sstevel@tonic-gate 		    prg.dev_no) ||
9110Sstevel@tonic-gate 		    ((prg.func_no &
9120Sstevel@tonic-gate 		    (PCI_REG_FUNC_M >> PCI_REG_FUNC_SHIFT)) !=
9130Sstevel@tonic-gate 		    prg.func_no)) {
9140Sstevel@tonic-gate 			prg.status = PCITOOL_INVALID_ADDRESS;
9150Sstevel@tonic-gate 			rval = EINVAL;
9160Sstevel@tonic-gate 			goto done_reg;
9170Sstevel@tonic-gate 		}
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 		size = PCITOOL_ACC_ATTR_SIZE(prg.acc_attr);
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 		/* Proper config space desired. */
9220Sstevel@tonic-gate 		if (prg.barnum == 0) {
9230Sstevel@tonic-gate 
924777Sschwartz 			if (pcitool_debug)
925777Sschwartz 				prom_printf(
926777Sschwartz 				    "config access: offset:0x%" PRIx64 ", "
927777Sschwartz 				    "phys_addr:0x%" PRIx64 "\n",
928777Sschwartz 				    prg.offset, prg.phys_addr);
929777Sschwartz 
930777Sschwartz 			if (prg.offset >= max_cfg_size) {
9310Sstevel@tonic-gate 				prg.status = PCITOOL_OUT_OF_RANGE;
9320Sstevel@tonic-gate 				rval = EINVAL;
9330Sstevel@tonic-gate 				goto done_reg;
9340Sstevel@tonic-gate 			}
9350Sstevel@tonic-gate 
9361083Sanish 			/*
9371083Sanish 			 * Access device.  prg is modified.
9381083Sanish 			 * First, check for AMD northbridges for I/O access
9391083Sanish 			 * (This fix will move in future to pcitool user-land)
9401083Sanish 			 * Next, check for PCIe devices and do
9411083Sanish 			 * memory-mapped access
9421083Sanish 			 * Lastly, check for PCI devices and do I/O access
9431083Sanish 			 */
9442434Sanish 			if ((prg.bus_no == 0) &&
9452434Sanish 			    (prg.dev_no >= 0x18) &&
9462434Sanish 			    (prg.dev_no < (0x18 + ncpus))) {
9471083Sanish 				if (cpuid_getvendor(CPU) == X86_VENDOR_AMD)
9481083Sanish 					rval = pcitool_cfg_access(dip, &prg,
9491083Sanish 					    write_flag);
9501083Sanish 			} else if (max_cfg_size == PCIE_CONF_HDR_SIZE)
951777Sschwartz 				rval = pcitool_pciex_cfg_access(dip, &prg,
952777Sschwartz 				    write_flag);
953777Sschwartz 			else
954777Sschwartz 				rval = pcitool_cfg_access(dip, &prg,
955777Sschwartz 				    write_flag);
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 			if (pcitool_debug)
9580Sstevel@tonic-gate 				prom_printf(
9590Sstevel@tonic-gate 				    "config access: data:0x%" PRIx64 "\n",
9600Sstevel@tonic-gate 				    prg.data);
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 		/* IO/ MEM/ MEM64 space. */
9630Sstevel@tonic-gate 		} else {
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 			pcitool_reg_t	prg2;
9660Sstevel@tonic-gate 			bcopy(&prg, &prg2, sizeof (pcitool_reg_t));
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 			/*
9690Sstevel@tonic-gate 			 * Translate BAR number into offset of the BAR in
9700Sstevel@tonic-gate 			 * the device's config space.
9710Sstevel@tonic-gate 			 */
9720Sstevel@tonic-gate 			prg2.offset = pci_bars[prg2.barnum];
9730Sstevel@tonic-gate 			prg2.acc_attr =
9740Sstevel@tonic-gate 			    PCITOOL_ACC_ATTR_SIZE_4 | PCITOOL_ACC_ATTR_ENDN_LTL;
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 			if (pcitool_debug)
9770Sstevel@tonic-gate 				prom_printf(
9780Sstevel@tonic-gate 				    "barnum:%d, bar_offset:0x%" PRIx64 "\n",
9790Sstevel@tonic-gate 				    prg2.barnum, prg2.offset);
9800Sstevel@tonic-gate 			/*
9810Sstevel@tonic-gate 			 * Get Bus Address Register (BAR) from config space.
9820Sstevel@tonic-gate 			 * prg2.offset is the offset into config space of the
9830Sstevel@tonic-gate 			 * BAR desired.  prg.status is modified on error.
9840Sstevel@tonic-gate 			 */
9850Sstevel@tonic-gate 			rval = pcitool_cfg_access(dip, &prg2, B_FALSE);
9860Sstevel@tonic-gate 			if (rval != SUCCESS) {
9870Sstevel@tonic-gate 				if (pcitool_debug)
9880Sstevel@tonic-gate 					prom_printf("BAR access failed\n");
9890Sstevel@tonic-gate 				prg.status = prg2.status;
9900Sstevel@tonic-gate 				goto done_reg;
9910Sstevel@tonic-gate 			}
9920Sstevel@tonic-gate 			/*
9930Sstevel@tonic-gate 			 * Reference proper PCI space based on the BAR.
9940Sstevel@tonic-gate 			 * If 64 bit MEM space, need to load other half of the
9950Sstevel@tonic-gate 			 * BAR first.
9960Sstevel@tonic-gate 			 */
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 			if (pcitool_debug)
9990Sstevel@tonic-gate 				prom_printf("bar returned is 0x%" PRIx64 "\n",
10000Sstevel@tonic-gate 				    prg2.data);
10010Sstevel@tonic-gate 			if (!prg2.data) {
10020Sstevel@tonic-gate 				if (pcitool_debug)
10030Sstevel@tonic-gate 					prom_printf("BAR data == 0\n");
10040Sstevel@tonic-gate 				rval = EINVAL;
10050Sstevel@tonic-gate 				prg.status = PCITOOL_INVALID_ADDRESS;
10060Sstevel@tonic-gate 				goto done_reg;
10070Sstevel@tonic-gate 			}
10080Sstevel@tonic-gate 			if (prg2.data == 0xffffffff) {
10090Sstevel@tonic-gate 				if (pcitool_debug)
10100Sstevel@tonic-gate 					prom_printf("BAR data == -1\n");
10110Sstevel@tonic-gate 				rval = EINVAL;
10120Sstevel@tonic-gate 				prg.status = PCITOOL_INVALID_ADDRESS;
10130Sstevel@tonic-gate 				goto done_reg;
10140Sstevel@tonic-gate 			}
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 			/*
10170Sstevel@tonic-gate 			 * BAR has bits saying this space is IO space, unless
10180Sstevel@tonic-gate 			 * this is the ROM address register.
10190Sstevel@tonic-gate 			 */
10200Sstevel@tonic-gate 			if (((PCI_BASE_SPACE_M & prg2.data) ==
10210Sstevel@tonic-gate 			    PCI_BASE_SPACE_IO) &&
10220Sstevel@tonic-gate 			    (prg2.offset != PCI_CONF_ROM)) {
10230Sstevel@tonic-gate 				if (pcitool_debug)
10240Sstevel@tonic-gate 					prom_printf("IO space\n");
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 				prg2.data &= PCI_BASE_IO_ADDR_M;
10270Sstevel@tonic-gate 				prg.phys_addr = prg2.data + prg.offset;
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 				rval = pcitool_io_access(dip, &prg, write_flag);
10300Sstevel@tonic-gate 				if ((rval != SUCCESS) && (pcitool_debug))
10310Sstevel@tonic-gate 					prom_printf("IO access failed\n");
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 				goto done_reg;
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 			/*
10370Sstevel@tonic-gate 			 * BAR has bits saying this space is 64 bit memory
10380Sstevel@tonic-gate 			 * space, unless this is the ROM address register.
10390Sstevel@tonic-gate 			 *
10400Sstevel@tonic-gate 			 * The 64 bit address stored in two BAR cells is not
10410Sstevel@tonic-gate 			 * necessarily aligned on an 8-byte boundary.
10420Sstevel@tonic-gate 			 * Need to keep the first 4 bytes read,
10430Sstevel@tonic-gate 			 * and do a separate read of the high 4 bytes.
10440Sstevel@tonic-gate 			 */
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 			} else if ((PCI_BASE_TYPE_ALL & prg2.data) &&
10470Sstevel@tonic-gate 			    (prg2.offset != PCI_CONF_ROM)) {
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 				uint32_t low_bytes =
10500Sstevel@tonic-gate 				    (uint32_t)(prg2.data & ~PCI_BASE_TYPE_ALL);
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 				/*
10530Sstevel@tonic-gate 				 * Don't try to read the next 4 bytes
10540Sstevel@tonic-gate 				 * past the end of BARs.
10550Sstevel@tonic-gate 				 */
10560Sstevel@tonic-gate 				if (prg2.offset >= PCI_CONF_BASE5) {
10570Sstevel@tonic-gate 					prg.status = PCITOOL_OUT_OF_RANGE;
10580Sstevel@tonic-gate 					rval = EIO;
10590Sstevel@tonic-gate 					goto done_reg;
10600Sstevel@tonic-gate 				}
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 				/*
10630Sstevel@tonic-gate 				 * Access device.
10640Sstevel@tonic-gate 				 * prg2.status is modified on error.
10650Sstevel@tonic-gate 				 */
10660Sstevel@tonic-gate 				prg2.offset += 4;
10670Sstevel@tonic-gate 				rval = pcitool_cfg_access(dip, &prg2, B_FALSE);
10680Sstevel@tonic-gate 				if (rval != SUCCESS) {
10690Sstevel@tonic-gate 					prg.status = prg2.status;
10700Sstevel@tonic-gate 					goto done_reg;
10710Sstevel@tonic-gate 				}
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 				if (prg2.data == 0xffffffff) {
10740Sstevel@tonic-gate 					prg.status = PCITOOL_INVALID_ADDRESS;
10750Sstevel@tonic-gate 					prg.status = EFAULT;
10760Sstevel@tonic-gate 					goto done_reg;
10770Sstevel@tonic-gate 				}
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 				prg2.data = (prg2.data << 32) + low_bytes;
10800Sstevel@tonic-gate 				if (pcitool_debug)
10810Sstevel@tonic-gate 					prom_printf(
10820Sstevel@tonic-gate 					    "64 bit mem space.  "
10830Sstevel@tonic-gate 					    "64-bit bar is 0x%" PRIx64 "\n",
10840Sstevel@tonic-gate 					    prg2.data);
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 			/* Mem32 space, including ROM */
10870Sstevel@tonic-gate 			} else {
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 				if (prg2.offset == PCI_CONF_ROM) {
10900Sstevel@tonic-gate 					if (pcitool_debug)
10910Sstevel@tonic-gate 						prom_printf(
10920Sstevel@tonic-gate 						    "Additional ROM "
10930Sstevel@tonic-gate 						    "checking\n");
10940Sstevel@tonic-gate 					/* Can't write to ROM */
10950Sstevel@tonic-gate 					if (write_flag) {
10960Sstevel@tonic-gate 						prg.status = PCITOOL_ROM_WRITE;
10970Sstevel@tonic-gate 						rval = EIO;
10980Sstevel@tonic-gate 						goto done_reg;
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 					/* ROM disabled for reading */
11010Sstevel@tonic-gate 					} else if (!(prg2.data & 0x00000001)) {
11020Sstevel@tonic-gate 						prg.status =
11030Sstevel@tonic-gate 						    PCITOOL_ROM_DISABLED;
11040Sstevel@tonic-gate 						rval = EIO;
11050Sstevel@tonic-gate 						goto done_reg;
11060Sstevel@tonic-gate 					}
11070Sstevel@tonic-gate 				}
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 				if (pcitool_debug)
11100Sstevel@tonic-gate 					prom_printf("32 bit mem space\n");
11110Sstevel@tonic-gate 			}
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 			/* Common code for all IO/MEM range spaces. */
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 			base_addr = prg2.data;
11160Sstevel@tonic-gate 			if (pcitool_debug)
11170Sstevel@tonic-gate 				prom_printf(
11180Sstevel@tonic-gate 				    "addr portion of bar is 0x%" PRIx64 ", "
11190Sstevel@tonic-gate 				    "base=0x%" PRIx64 ", "
11200Sstevel@tonic-gate 				    "offset:0x%" PRIx64 "\n",
11210Sstevel@tonic-gate 				    prg2.data, base_addr, prg.offset);
11220Sstevel@tonic-gate 			/*
11230Sstevel@tonic-gate 			 * Use offset provided by caller to index into
11240Sstevel@tonic-gate 			 * desired space, then access.
11250Sstevel@tonic-gate 			 * Note that prg.status is modified on error.
11260Sstevel@tonic-gate 			 */
11270Sstevel@tonic-gate 			prg.phys_addr = base_addr + prg.offset;
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 			virt_addr = pcitool_map(prg.phys_addr, size,
11300Sstevel@tonic-gate 			    &num_virt_pages);
11310Sstevel@tonic-gate 			if (virt_addr == NULL) {
11320Sstevel@tonic-gate 				prg.status = PCITOOL_IO_ERROR;
11330Sstevel@tonic-gate 				rval = EIO;
11340Sstevel@tonic-gate 				goto done_reg;
11350Sstevel@tonic-gate 			}
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 			rval = pcitool_mem_access(dip, &prg, virt_addr,
11380Sstevel@tonic-gate 			    write_flag);
11390Sstevel@tonic-gate 			pcitool_unmap(virt_addr, num_virt_pages);
11400Sstevel@tonic-gate 		}
11410Sstevel@tonic-gate done_reg:
1142*4397Sschwartz 		prg.drvr_version = PCITOOL_VERSION;
11430Sstevel@tonic-gate 		if (ddi_copyout(&prg, arg, sizeof (pcitool_reg_t), mode) !=
11440Sstevel@tonic-gate 		    DDI_SUCCESS) {
11450Sstevel@tonic-gate 			if (pcitool_debug)
11460Sstevel@tonic-gate 				prom_printf("Error returning arguments.\n");
11470Sstevel@tonic-gate 			rval = EFAULT;
11480Sstevel@tonic-gate 		}
11490Sstevel@tonic-gate 		break;
11500Sstevel@tonic-gate 	default:
11510Sstevel@tonic-gate 		rval = ENOTTY;
11520Sstevel@tonic-gate 		break;
11530Sstevel@tonic-gate 	}
11540Sstevel@tonic-gate 	return (rval);
11550Sstevel@tonic-gate }
1156