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
5*2434Sanish  * Common Development and Distribution License (the "License").
6*2434Sanish  * 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 /*
22*2434Sanish  * Copyright 2006 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>
42916Sschwartz #include <io/pcplusmp/apic.h>
43916Sschwartz #include <io/pci/pci_var.h>
440Sstevel@tonic-gate #include <sys/promif.h>
451083Sanish #include <sys/x86_archext.h>
46*2434Sanish #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 
87916Sschwartz /* Extern decalrations */
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 
124117Sschwartz 
125916Sschwartz /* Return the number of interrupts on a pci bus. */
126916Sschwartz static int
127916Sschwartz pcitool_intr_get_max_ino(uint32_t *arg, int mode)
128916Sschwartz {
129916Sschwartz 	uint32_t num_intr = APIC_MAX_VECTOR;
130916Sschwartz 
131916Sschwartz 	if (ddi_copyout(&num_intr, arg, sizeof (uint32_t), mode) !=
132916Sschwartz 	    DDI_SUCCESS)
133916Sschwartz 		return (EFAULT);
134916Sschwartz 	else
135916Sschwartz 		return (SUCCESS);
136916Sschwartz }
137916Sschwartz 
138916Sschwartz 
139916Sschwartz /*ARGSUSED*/
140916Sschwartz static int
141916Sschwartz pcitool_set_intr(dev_info_t *dip, void *arg, int mode)
142916Sschwartz {
143916Sschwartz 	ddi_intr_handle_impl_t info_hdl;
144916Sschwartz 	pcitool_intr_set_t iset;
145916Sschwartz 	uint32_t old_cpu;
146916Sschwartz 	int ret, result;
147916Sschwartz 	int rval = SUCCESS;
148916Sschwartz 
149916Sschwartz 	if (ddi_copyin(arg, &iset, sizeof (pcitool_intr_set_t), mode) !=
150916Sschwartz 	    DDI_SUCCESS)
151916Sschwartz 		return (EFAULT);
152916Sschwartz 
153916Sschwartz 	if (iset.ino > APIC_MAX_VECTOR) {
154916Sschwartz 		rval = EINVAL;
155916Sschwartz 		iset.status = PCITOOL_INVALID_INO;
156916Sschwartz 		goto done_set_intr;
157916Sschwartz 	}
158916Sschwartz 
159916Sschwartz 	iset.status = PCITOOL_SUCCESS;
160916Sschwartz 
161916Sschwartz 	if ((old_cpu = pci_get_cpu_from_vecirq(iset.ino, IS_VEC)) == -1) {
162916Sschwartz 		iset.status = PCITOOL_IO_ERROR;
163916Sschwartz 		rval = EINVAL;
164916Sschwartz 		goto done_set_intr;
165916Sschwartz 	}
166916Sschwartz 
167916Sschwartz 	old_cpu &= ~PSMGI_CPU_USER_BOUND;
168916Sschwartz 
169916Sschwartz 	/*
170916Sschwartz 	 * For this locally-declared and used handle, ih_private will contain a
171916Sschwartz 	 * CPU value, not an ihdl_plat_t as used for global interrupt handling.
172916Sschwartz 	 */
173916Sschwartz 	info_hdl.ih_vector = iset.ino;
174916Sschwartz 	info_hdl.ih_private = (void *)(uintptr_t)iset.cpu_id;
175916Sschwartz 	ret = (*psm_intr_ops)(NULL, &info_hdl, PSM_INTR_OP_SET_CPU, &result);
176916Sschwartz 
177916Sschwartz 	iset.drvr_version = PCITOOL_DRVR_VERSION;
178916Sschwartz 	if (ret != PSM_SUCCESS) {
179916Sschwartz 		switch (result) {
180916Sschwartz 		case EIO:		/* Error making the change */
181916Sschwartz 			rval = EIO;
182916Sschwartz 			iset.status = PCITOOL_IO_ERROR;
183916Sschwartz 			break;
184916Sschwartz 		case ENXIO:		/* Couldn't convert vector to irq */
185916Sschwartz 			rval = EINVAL;
186916Sschwartz 			iset.status = PCITOOL_INVALID_INO;
187916Sschwartz 			break;
188916Sschwartz 		case EINVAL:		/* CPU out of range */
189916Sschwartz 			rval = EINVAL;
190916Sschwartz 			iset.status = PCITOOL_INVALID_CPUID;
191916Sschwartz 			break;
192916Sschwartz 		}
193916Sschwartz 	}
194916Sschwartz 
195916Sschwartz 	/* Return original CPU. */
196916Sschwartz 	iset.cpu_id = old_cpu;
197916Sschwartz 
198916Sschwartz done_set_intr:
199916Sschwartz 	if (ddi_copyout(&iset, arg, sizeof (pcitool_intr_set_t), mode) !=
200916Sschwartz 	    DDI_SUCCESS)
201916Sschwartz 		rval = EFAULT;
202916Sschwartz 	return (rval);
203916Sschwartz }
204916Sschwartz 
205916Sschwartz 
206916Sschwartz /* It is assumed that dip != NULL */
207916Sschwartz static void
208916Sschwartz pcitool_get_intr_dev_info(dev_info_t *dip, pcitool_intr_dev_t *devs)
209916Sschwartz {
210916Sschwartz 	(void) strncpy(devs->driver_name,
211916Sschwartz 	    ddi_driver_name(dip), MAXMODCONFNAME-1);
212916Sschwartz 	devs->driver_name[MAXMODCONFNAME] = '\0';
213916Sschwartz 	(void) ddi_pathname(dip, devs->path);
214916Sschwartz 	devs->dev_inst = ddi_get_instance(dip);
215916Sschwartz }
216916Sschwartz 
217916Sschwartz 
218916Sschwartz /*ARGSUSED*/
219916Sschwartz static int
220916Sschwartz pcitool_get_intr(dev_info_t *dip, void *arg, int mode)
221916Sschwartz {
222916Sschwartz 	/* Array part isn't used here, but oh well... */
223916Sschwartz 	pcitool_intr_get_t partial_iget;
224916Sschwartz 	pcitool_intr_get_t *iget = &partial_iget;
225916Sschwartz 	size_t	iget_kmem_alloc_size = 0;
226916Sschwartz 	uint8_t num_devs_ret;
227916Sschwartz 	int copyout_rval;
228916Sschwartz 	int rval = SUCCESS;
229916Sschwartz 	int circ;
230916Sschwartz 	int i;
231916Sschwartz 
232916Sschwartz 	ddi_intr_handle_impl_t info_hdl;
233916Sschwartz 	apic_get_intr_t intr_info;
234916Sschwartz 
235916Sschwartz 	/* Read in just the header part, no array section. */
236916Sschwartz 	if (ddi_copyin(arg, &partial_iget, PCITOOL_IGET_SIZE(0), mode) !=
237916Sschwartz 	    DDI_SUCCESS)
238916Sschwartz 		return (EFAULT);
239916Sschwartz 
240916Sschwartz 	/* Validate argument. */
241916Sschwartz 	if (partial_iget.ino > APIC_MAX_VECTOR) {
242916Sschwartz 		partial_iget.status = PCITOOL_INVALID_INO;
243916Sschwartz 		partial_iget.num_devs_ret = 0;
244916Sschwartz 		rval = EINVAL;
245916Sschwartz 		goto done_get_intr;
246916Sschwartz 	}
247916Sschwartz 
248916Sschwartz 	num_devs_ret = partial_iget.num_devs_ret;
249916Sschwartz 	intr_info.avgi_dip_list = NULL;
250916Sschwartz 	intr_info.avgi_req_flags =
251916Sschwartz 	    PSMGI_REQ_CPUID | PSMGI_REQ_NUM_DEVS | PSMGI_INTRBY_VEC;
252916Sschwartz 	/*
253916Sschwartz 	 * For this locally-declared and used handle, ih_private will contain a
254916Sschwartz 	 * pointer to apic_get_intr_t, not an ihdl_plat_t as used for
255916Sschwartz 	 * global interrupt handling.
256916Sschwartz 	 */
257916Sschwartz 	info_hdl.ih_private = &intr_info;
258916Sschwartz 	info_hdl.ih_vector = partial_iget.ino;
259916Sschwartz 
260916Sschwartz 	/* Caller wants device information returned. */
261916Sschwartz 	if (num_devs_ret > 0) {
262916Sschwartz 
263916Sschwartz 		intr_info.avgi_req_flags |= PSMGI_REQ_GET_DEVS;
264916Sschwartz 
265916Sschwartz 		/*
266916Sschwartz 		 * Allocate room.
267916Sschwartz 		 * If num_devs_ret == 0 iget remains pointing to partial_iget.
268916Sschwartz 		 */
269916Sschwartz 		iget_kmem_alloc_size = PCITOOL_IGET_SIZE(num_devs_ret);
270916Sschwartz 		iget = kmem_alloc(iget_kmem_alloc_size, KM_SLEEP);
271916Sschwartz 
272916Sschwartz 		/* Read in whole structure to verify there's room. */
273916Sschwartz 		if (ddi_copyin(arg, iget, iget_kmem_alloc_size, mode) !=
274916Sschwartz 		    SUCCESS) {
275916Sschwartz 
276916Sschwartz 			/* Be consistent and just return EFAULT here. */
277916Sschwartz 			kmem_free(iget, iget_kmem_alloc_size);
278916Sschwartz 
279916Sschwartz 			return (EFAULT);
280916Sschwartz 		}
281916Sschwartz 	}
282916Sschwartz 
283916Sschwartz 	bzero(iget, PCITOOL_IGET_SIZE(num_devs_ret));
284916Sschwartz 	iget->ino = info_hdl.ih_vector;
285916Sschwartz 
286916Sschwartz 	/*
287916Sschwartz 	 * Lock device tree branch from the pci root nexus on down if info will
288916Sschwartz 	 * be extracted from dips returned from the tree.
289916Sschwartz 	 */
290916Sschwartz 	if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) {
291916Sschwartz 		ndi_devi_enter(dip, &circ);
292916Sschwartz 	}
293916Sschwartz 
294916Sschwartz 	/* Call psm_intr_ops(PSM_INTR_OP_GET_INTR) to get information. */
295916Sschwartz 	if ((rval = (*psm_intr_ops)(NULL, &info_hdl,
296916Sschwartz 	    PSM_INTR_OP_GET_INTR, NULL)) != PSM_SUCCESS) {
297916Sschwartz 		iget->status = PCITOOL_IO_ERROR;
298916Sschwartz 		iget->num_devs_ret = 0;
299916Sschwartz 		rval = EINVAL;
300916Sschwartz 		goto done_get_intr;
301916Sschwartz 	}
302916Sschwartz 
303916Sschwartz 	/*
304916Sschwartz 	 * Fill in the pcitool_intr_get_t to be returned,
305916Sschwartz 	 * with the CPU, num_devs_ret and num_devs.
306916Sschwartz 	 */
307916Sschwartz 	iget->cpu_id = intr_info.avgi_cpu_id & ~PSMGI_CPU_USER_BOUND;
308916Sschwartz 
309916Sschwartz 	/* Number of devices returned by apic. */
310916Sschwartz 	iget->num_devs = intr_info.avgi_num_devs;
311916Sschwartz 
312916Sschwartz 	/* Device info was returned. */
313916Sschwartz 	if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) {
314916Sschwartz 
315916Sschwartz 		/*
316916Sschwartz 		 * num devs returned is num devs ret by apic,
317916Sschwartz 		 * space permitting.
318916Sschwartz 		 */
319916Sschwartz 		iget->num_devs_ret = min(num_devs_ret, intr_info.avgi_num_devs);
320916Sschwartz 
321916Sschwartz 		/*
322916Sschwartz 		 * Loop thru list of dips and extract driver, name and instance.
323916Sschwartz 		 * Fill in the pcitool_intr_dev_t's with this info.
324916Sschwartz 		 */
325916Sschwartz 		for (i = 0; i < iget->num_devs_ret; i++)
326916Sschwartz 			pcitool_get_intr_dev_info(intr_info.avgi_dip_list[i],
327916Sschwartz 			    &iget->dev[i]);
328916Sschwartz 
329916Sschwartz 		/* Free kmem_alloc'ed memory of the apic_get_intr_t */
330916Sschwartz 		kmem_free(intr_info.avgi_dip_list,
331916Sschwartz 		    intr_info.avgi_num_devs * sizeof (dev_info_t *));
332916Sschwartz 	}
333916Sschwartz 
334916Sschwartz done_get_intr:
335916Sschwartz 
336916Sschwartz 	if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) {
337916Sschwartz 		ndi_devi_exit(dip, circ);
338916Sschwartz 	}
339916Sschwartz 
340916Sschwartz 	iget->drvr_version = PCITOOL_DRVR_VERSION;
341916Sschwartz 	copyout_rval = ddi_copyout(iget, arg,
342916Sschwartz 	    PCITOOL_IGET_SIZE(num_devs_ret), mode);
343916Sschwartz 
344916Sschwartz 	if (iget_kmem_alloc_size > 0)
345916Sschwartz 		kmem_free(iget, iget_kmem_alloc_size);
346916Sschwartz 
347916Sschwartz 	if (copyout_rval != DDI_SUCCESS)
348916Sschwartz 		rval = EFAULT;
349916Sschwartz 
350916Sschwartz 	return (rval);
351916Sschwartz }
352916Sschwartz 
353916Sschwartz 
354916Sschwartz /*
355916Sschwartz  * Main function for handling interrupt CPU binding requests and queries.
356916Sschwartz  * Need to implement later
357916Sschwartz  */
358916Sschwartz /*ARGSUSED*/
359916Sschwartz int
360916Sschwartz pcitool_intr_admn(dev_info_t *dip, void *arg, int cmd, int mode)
361916Sschwartz {
362916Sschwartz 	int rval;
363916Sschwartz 
364916Sschwartz 	switch (cmd) {
365916Sschwartz 
366916Sschwartz 	/* Associate a new CPU with a given vector */
367916Sschwartz 	case PCITOOL_DEVICE_SET_INTR:
368916Sschwartz 		rval = pcitool_set_intr(dip, arg, mode);
369916Sschwartz 		break;
370916Sschwartz 
371916Sschwartz 	case PCITOOL_DEVICE_GET_INTR:
372916Sschwartz 		rval = pcitool_get_intr(dip, arg, mode);
373916Sschwartz 		break;
374916Sschwartz 
375916Sschwartz 	case PCITOOL_DEVICE_NUM_INTR:
376916Sschwartz 		rval = pcitool_intr_get_max_ino(arg, mode);
377916Sschwartz 		break;
378916Sschwartz 
379916Sschwartz 	default:
380916Sschwartz 		rval = ENOTSUP;
381916Sschwartz 	}
382916Sschwartz 
383916Sschwartz 	return (rval);
384916Sschwartz }
385916Sschwartz 
386916Sschwartz 
3870Sstevel@tonic-gate /*
3880Sstevel@tonic-gate  * A note about ontrap handling:
3890Sstevel@tonic-gate  *
3900Sstevel@tonic-gate  * X86 systems on which this module was tested return FFs instead of bus errors
3910Sstevel@tonic-gate  * when accessing devices with invalid addresses.  Ontrap handling, which
3920Sstevel@tonic-gate  * gracefully handles kernel bus errors, is installed anyway, in case future
3930Sstevel@tonic-gate  * X86 platforms require it.
3940Sstevel@tonic-gate  */
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate /*
3970Sstevel@tonic-gate  * Perform register accesses on the nexus device itself.
3980Sstevel@tonic-gate  * No explicit PCI nexus device for X86, so not applicable.
3990Sstevel@tonic-gate  */
400916Sschwartz 
4010Sstevel@tonic-gate /*ARGSUSED*/
4020Sstevel@tonic-gate int
403777Sschwartz pcitool_bus_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode)
4040Sstevel@tonic-gate {
4050Sstevel@tonic-gate 	return (ENOTSUP);
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate /* Swap endianness. */
4090Sstevel@tonic-gate static uint64_t
4100Sstevel@tonic-gate pcitool_swap_endian(uint64_t data, int size)
4110Sstevel@tonic-gate {
4120Sstevel@tonic-gate 	typedef union {
4130Sstevel@tonic-gate 		uint64_t data64;
4140Sstevel@tonic-gate 		uint8_t data8[8];
4150Sstevel@tonic-gate 	} data_split_t;
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	data_split_t orig_data;
4180Sstevel@tonic-gate 	data_split_t returned_data;
4190Sstevel@tonic-gate 	int i;
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	orig_data.data64 = data;
4220Sstevel@tonic-gate 	returned_data.data64 = 0;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	for (i = 0; i < size; i++) {
4250Sstevel@tonic-gate 		returned_data.data8[i] = orig_data.data8[size - 1 - i];
4260Sstevel@tonic-gate 	}
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	return (returned_data.data64);
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 
432777Sschwartz /*
433777Sschwartz  * Access device.  prg is modified.
434777Sschwartz  *
435777Sschwartz  * Extended config space is available only through memory-mapped access.
436777Sschwartz  * Standard config space on pci express devices is available either way,
437777Sschwartz  * so do it memory-mapped here too, for simplicity.
438777Sschwartz  */
439777Sschwartz /*ARGSUSED*/
440777Sschwartz static int
441777Sschwartz pcitool_pciex_cfg_access(dev_info_t *dip, pcitool_reg_t *prg,
442777Sschwartz     boolean_t write_flag)
443777Sschwartz {
444777Sschwartz 	int rval = SUCCESS;
445777Sschwartz 	uint64_t virt_addr;
446777Sschwartz 	size_t	num_virt_pages;
447777Sschwartz 
448777Sschwartz 	prg->status = PCITOOL_SUCCESS;
449777Sschwartz 
450777Sschwartz 	prg->phys_addr = ddi_prop_get_int64(DDI_DEV_T_ANY, dip, 0,
451881Sjohnny 	    "ecfga-base-address", 0);
452777Sschwartz 	if (prg->phys_addr == 0) {
453777Sschwartz 		prg->status = PCITOOL_IO_ERROR;
454777Sschwartz 		return (EIO);
455777Sschwartz 	}
456777Sschwartz 
457777Sschwartz 	prg->phys_addr += prg->offset +
458777Sschwartz 	    ((prg->bus_no << PCIEX_REG_BUS_SHIFT) |
459777Sschwartz 	    (prg->dev_no << PCIEX_REG_DEV_SHIFT) |
460777Sschwartz 	    (prg->func_no << PCIEX_REG_FUNC_SHIFT));
461777Sschwartz 
462777Sschwartz 	virt_addr = pcitool_map(prg->phys_addr,
463777Sschwartz 	    PCITOOL_ACC_ATTR_SIZE(prg->acc_attr), &num_virt_pages);
464777Sschwartz 	if (virt_addr == NULL) {
465777Sschwartz 		prg->status = PCITOOL_IO_ERROR;
466777Sschwartz 		return (EIO);
467777Sschwartz 	}
468777Sschwartz 
469777Sschwartz 	rval = pcitool_mem_access(dip, prg, virt_addr, write_flag);
470777Sschwartz 	pcitool_unmap(virt_addr, num_virt_pages);
471777Sschwartz 	return (rval);
472777Sschwartz }
473777Sschwartz 
4740Sstevel@tonic-gate /* Access device.  prg is modified. */
4750Sstevel@tonic-gate /*ARGSUSED*/
4760Sstevel@tonic-gate static int
4770Sstevel@tonic-gate pcitool_cfg_access(dev_info_t *dip, pcitool_reg_t *prg, boolean_t write_flag)
4780Sstevel@tonic-gate {
4790Sstevel@tonic-gate 	int size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
4800Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
4810Sstevel@tonic-gate 	int rval = SUCCESS;
4820Sstevel@tonic-gate 	uint64_t local_data;
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	/*
4850Sstevel@tonic-gate 	 * NOTE: there is no way to verify whether or not the address is valid.
4860Sstevel@tonic-gate 	 * The put functions return void and the get functions return ff on
4870Sstevel@tonic-gate 	 * error.
4880Sstevel@tonic-gate 	 */
4890Sstevel@tonic-gate 	prg->status = PCITOOL_SUCCESS;
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	if (write_flag) {
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 		if (big_endian) {
4940Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
4950Sstevel@tonic-gate 		} else {
4960Sstevel@tonic-gate 			local_data = prg->data;
4970Sstevel@tonic-gate 		}
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 		switch (size) {
5000Sstevel@tonic-gate 		case 1:
5010Sstevel@tonic-gate 			(*pci_putb_func)(prg->bus_no, prg->dev_no,
5020Sstevel@tonic-gate 			    prg->func_no, prg->offset, local_data);
5030Sstevel@tonic-gate 			break;
5040Sstevel@tonic-gate 		case 2:
5050Sstevel@tonic-gate 			(*pci_putw_func)(prg->bus_no, prg->dev_no,
5060Sstevel@tonic-gate 			    prg->func_no, prg->offset, local_data);
5070Sstevel@tonic-gate 			break;
5080Sstevel@tonic-gate 		case 4:
5090Sstevel@tonic-gate 			(*pci_putl_func)(prg->bus_no, prg->dev_no,
5100Sstevel@tonic-gate 			    prg->func_no, prg->offset, local_data);
5110Sstevel@tonic-gate 			break;
5120Sstevel@tonic-gate 		default:
5130Sstevel@tonic-gate 			rval = ENOTSUP;
5140Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
5150Sstevel@tonic-gate 			break;
5160Sstevel@tonic-gate 		}
5170Sstevel@tonic-gate 	} else {
5180Sstevel@tonic-gate 		switch (size) {
5190Sstevel@tonic-gate 		case 1:
5200Sstevel@tonic-gate 			local_data = (*pci_getb_func)(prg->bus_no, prg->dev_no,
5210Sstevel@tonic-gate 			    prg->func_no, prg->offset);
5220Sstevel@tonic-gate 			break;
5230Sstevel@tonic-gate 		case 2:
5240Sstevel@tonic-gate 			local_data = (*pci_getw_func)(prg->bus_no, prg->dev_no,
5250Sstevel@tonic-gate 			    prg->func_no, prg->offset);
5260Sstevel@tonic-gate 			break;
5270Sstevel@tonic-gate 		case 4:
5280Sstevel@tonic-gate 			local_data = (*pci_getl_func)(prg->bus_no, prg->dev_no,
5290Sstevel@tonic-gate 			    prg->func_no, prg->offset);
5300Sstevel@tonic-gate 			break;
5310Sstevel@tonic-gate 		default:
5320Sstevel@tonic-gate 			rval = ENOTSUP;
5330Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
5340Sstevel@tonic-gate 			break;
5350Sstevel@tonic-gate 		}
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 		if (rval == SUCCESS) {
5380Sstevel@tonic-gate 			if (big_endian) {
5390Sstevel@tonic-gate 				prg->data =
5400Sstevel@tonic-gate 				    pcitool_swap_endian(local_data, size);
5410Sstevel@tonic-gate 			} else {
5420Sstevel@tonic-gate 				prg->data = local_data;
5430Sstevel@tonic-gate 			}
5440Sstevel@tonic-gate 		}
5450Sstevel@tonic-gate 	}
5460Sstevel@tonic-gate 	prg->phys_addr = 0;	/* Config space is not memory mapped on X86. */
5470Sstevel@tonic-gate 	return (rval);
5480Sstevel@tonic-gate }
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate /*ARGSUSED*/
5520Sstevel@tonic-gate static int
5530Sstevel@tonic-gate pcitool_io_access(dev_info_t *dip, pcitool_reg_t *prg, boolean_t write_flag)
5540Sstevel@tonic-gate {
5550Sstevel@tonic-gate 	int port = (int)prg->phys_addr;
5560Sstevel@tonic-gate 	size_t size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
5570Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
5580Sstevel@tonic-gate 	int rval = SUCCESS;
5590Sstevel@tonic-gate 	on_trap_data_t otd;
5600Sstevel@tonic-gate 	uint64_t local_data;
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	/*
5640Sstevel@tonic-gate 	 * on_trap works like setjmp.
5650Sstevel@tonic-gate 	 *
5660Sstevel@tonic-gate 	 * A non-zero return here means on_trap has returned from an error.
5670Sstevel@tonic-gate 	 *
5680Sstevel@tonic-gate 	 * A zero return here means that on_trap has just returned from setup.
5690Sstevel@tonic-gate 	 */
5700Sstevel@tonic-gate 	if (on_trap(&otd, OT_DATA_ACCESS)) {
5710Sstevel@tonic-gate 		no_trap();
5720Sstevel@tonic-gate 		if (pcitool_debug)
5730Sstevel@tonic-gate 			prom_printf(
5740Sstevel@tonic-gate 			    "pcitool_mem_access: on_trap caught an error...\n");
5750Sstevel@tonic-gate 		prg->status = PCITOOL_INVALID_ADDRESS;
5760Sstevel@tonic-gate 		return (EFAULT);
5770Sstevel@tonic-gate 	}
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	if (write_flag) {
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 		if (big_endian) {
5820Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
5830Sstevel@tonic-gate 		} else {
5840Sstevel@tonic-gate 			local_data = prg->data;
5850Sstevel@tonic-gate 		}
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 		if (pcitool_debug)
5880Sstevel@tonic-gate 			prom_printf("Writing %ld byte(s) to port 0x%x\n",
5890Sstevel@tonic-gate 			    size, port);
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 		switch (size) {
5920Sstevel@tonic-gate 		case 1:
5930Sstevel@tonic-gate 			outb(port, (uint8_t)local_data);
5940Sstevel@tonic-gate 			break;
5950Sstevel@tonic-gate 		case 2:
5960Sstevel@tonic-gate 			outw(port, (uint16_t)local_data);
5970Sstevel@tonic-gate 			break;
5980Sstevel@tonic-gate 		case 4:
5990Sstevel@tonic-gate 			outl(port, (uint32_t)local_data);
6000Sstevel@tonic-gate 			break;
6010Sstevel@tonic-gate 		default:
6020Sstevel@tonic-gate 			rval = ENOTSUP;
6030Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
6040Sstevel@tonic-gate 			break;
6050Sstevel@tonic-gate 		}
6060Sstevel@tonic-gate 	} else {
6070Sstevel@tonic-gate 		if (pcitool_debug)
6080Sstevel@tonic-gate 			prom_printf("Reading %ld byte(s) from port 0x%x\n",
6090Sstevel@tonic-gate 			    size, port);
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 		switch (size) {
6120Sstevel@tonic-gate 		case 1:
6130Sstevel@tonic-gate 			local_data = inb(port);
6140Sstevel@tonic-gate 			break;
6150Sstevel@tonic-gate 		case 2:
6160Sstevel@tonic-gate 			local_data = inw(port);
6170Sstevel@tonic-gate 			break;
6180Sstevel@tonic-gate 		case 4:
6190Sstevel@tonic-gate 			local_data = inl(port);
6200Sstevel@tonic-gate 			break;
6210Sstevel@tonic-gate 		default:
6220Sstevel@tonic-gate 			rval = ENOTSUP;
6230Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
6240Sstevel@tonic-gate 			break;
6250Sstevel@tonic-gate 		}
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 		if (rval == SUCCESS) {
6280Sstevel@tonic-gate 			if (big_endian) {
6290Sstevel@tonic-gate 				prg->data =
6300Sstevel@tonic-gate 				    pcitool_swap_endian(local_data, size);
6310Sstevel@tonic-gate 			} else {
6320Sstevel@tonic-gate 				prg->data = local_data;
6330Sstevel@tonic-gate 			}
6340Sstevel@tonic-gate 		}
6350Sstevel@tonic-gate 	}
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	no_trap();
6380Sstevel@tonic-gate 	return (rval);
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate /*ARGSUSED*/
6420Sstevel@tonic-gate static int
6430Sstevel@tonic-gate pcitool_mem_access(dev_info_t *dip, pcitool_reg_t *prg, uint64_t virt_addr,
644117Sschwartz 	boolean_t write_flag)
6450Sstevel@tonic-gate {
6460Sstevel@tonic-gate 	size_t size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
6470Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
6480Sstevel@tonic-gate 	int rval = DDI_SUCCESS;
6490Sstevel@tonic-gate 	on_trap_data_t otd;
6500Sstevel@tonic-gate 	uint64_t local_data;
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	/*
6530Sstevel@tonic-gate 	 * on_trap works like setjmp.
6540Sstevel@tonic-gate 	 *
6550Sstevel@tonic-gate 	 * A non-zero return here means on_trap has returned from an error.
6560Sstevel@tonic-gate 	 *
6570Sstevel@tonic-gate 	 * A zero return here means that on_trap has just returned from setup.
6580Sstevel@tonic-gate 	 */
6590Sstevel@tonic-gate 	if (on_trap(&otd, OT_DATA_ACCESS)) {
6600Sstevel@tonic-gate 		no_trap();
6610Sstevel@tonic-gate 		if (pcitool_debug)
6620Sstevel@tonic-gate 			prom_printf(
6630Sstevel@tonic-gate 			    "pcitool_mem_access: on_trap caught an error...\n");
6640Sstevel@tonic-gate 		prg->status = PCITOOL_INVALID_ADDRESS;
6650Sstevel@tonic-gate 		return (EFAULT);
6660Sstevel@tonic-gate 	}
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	if (write_flag) {
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 		if (big_endian) {
6710Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
6720Sstevel@tonic-gate 		} else {
6730Sstevel@tonic-gate 			local_data = prg->data;
6740Sstevel@tonic-gate 		}
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 		switch (size) {
6770Sstevel@tonic-gate 		case 1:
6780Sstevel@tonic-gate 			*((uint8_t *)(uintptr_t)virt_addr) = local_data;
6790Sstevel@tonic-gate 			break;
6800Sstevel@tonic-gate 		case 2:
6810Sstevel@tonic-gate 			*((uint16_t *)(uintptr_t)virt_addr) = local_data;
6820Sstevel@tonic-gate 			break;
6830Sstevel@tonic-gate 		case 4:
6840Sstevel@tonic-gate 			*((uint32_t *)(uintptr_t)virt_addr) = local_data;
6850Sstevel@tonic-gate 			break;
6860Sstevel@tonic-gate 		case 8:
6870Sstevel@tonic-gate 			*((uint64_t *)(uintptr_t)virt_addr) = local_data;
6880Sstevel@tonic-gate 			break;
6890Sstevel@tonic-gate 		default:
6900Sstevel@tonic-gate 			rval = ENOTSUP;
6910Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
6920Sstevel@tonic-gate 			break;
6930Sstevel@tonic-gate 		}
6940Sstevel@tonic-gate 	} else {
6950Sstevel@tonic-gate 		switch (size) {
6960Sstevel@tonic-gate 		case 1:
6970Sstevel@tonic-gate 			local_data = *((uint8_t *)(uintptr_t)virt_addr);
6980Sstevel@tonic-gate 			break;
6990Sstevel@tonic-gate 		case 2:
7000Sstevel@tonic-gate 			local_data = *((uint16_t *)(uintptr_t)virt_addr);
7010Sstevel@tonic-gate 			break;
7020Sstevel@tonic-gate 		case 4:
7030Sstevel@tonic-gate 			local_data = *((uint32_t *)(uintptr_t)virt_addr);
7040Sstevel@tonic-gate 			break;
7050Sstevel@tonic-gate 		case 8:
7060Sstevel@tonic-gate 			local_data = *((uint64_t *)(uintptr_t)virt_addr);
7070Sstevel@tonic-gate 			break;
7080Sstevel@tonic-gate 		default:
7090Sstevel@tonic-gate 			rval = ENOTSUP;
7100Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
7110Sstevel@tonic-gate 			break;
7120Sstevel@tonic-gate 		}
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 		if (rval == SUCCESS) {
7150Sstevel@tonic-gate 			if (big_endian) {
7160Sstevel@tonic-gate 				prg->data =
7170Sstevel@tonic-gate 				    pcitool_swap_endian(local_data, size);
7180Sstevel@tonic-gate 			} else {
7190Sstevel@tonic-gate 				prg->data = local_data;
7200Sstevel@tonic-gate 			}
7210Sstevel@tonic-gate 		}
7220Sstevel@tonic-gate 	}
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	no_trap();
7250Sstevel@tonic-gate 	return (rval);
7260Sstevel@tonic-gate }
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate /*
7290Sstevel@tonic-gate  * Map up to 2 pages which contain the address we want to access.
7300Sstevel@tonic-gate  *
7310Sstevel@tonic-gate  * Mapping should span no more than 8 bytes.  With X86 it is possible for an
7320Sstevel@tonic-gate  * 8 byte value to start on a 4 byte boundary, so it can cross a page boundary.
7330Sstevel@tonic-gate  * We'll never have to map more than two pages.
7340Sstevel@tonic-gate  */
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate static uint64_t
7370Sstevel@tonic-gate pcitool_map(uint64_t phys_addr, size_t size, size_t *num_pages)
7380Sstevel@tonic-gate {
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	uint64_t page_base = phys_addr & ~MMU_PAGEOFFSET;
7410Sstevel@tonic-gate 	uint64_t offset = phys_addr & MMU_PAGEOFFSET;
7420Sstevel@tonic-gate 	void *virt_base;
7430Sstevel@tonic-gate 	uint64_t returned_addr;
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	if (pcitool_debug)
7460Sstevel@tonic-gate 		prom_printf("pcitool_map: Called with PA:0x%p\n",
7470Sstevel@tonic-gate 		    (uint8_t *)(uintptr_t)phys_addr);
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	*num_pages = 1;
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	/* Desired mapping would span more than two pages. */
7520Sstevel@tonic-gate 	if ((offset + size) > (MMU_PAGESIZE * 2)) {
7530Sstevel@tonic-gate 		if (pcitool_debug)
7540Sstevel@tonic-gate 			prom_printf("boundary violation: "
755777Sschwartz 			    "offset:0x%" PRIx64 ", size:%ld, pagesize:0x%lx\n",
756777Sschwartz 			    offset, (uintptr_t)size, (uintptr_t)MMU_PAGESIZE);
7570Sstevel@tonic-gate 		return (NULL);
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	} else if ((offset + size) > MMU_PAGESIZE) {
7600Sstevel@tonic-gate 		(*num_pages)++;
7610Sstevel@tonic-gate 	}
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	/* Get page(s) of virtual space. */
7640Sstevel@tonic-gate 	virt_base = vmem_alloc(heap_arena, ptob(*num_pages), VM_NOSLEEP);
7650Sstevel@tonic-gate 	if (virt_base == NULL) {
7660Sstevel@tonic-gate 		if (pcitool_debug)
7670Sstevel@tonic-gate 			prom_printf("Couldn't get virtual base address.\n");
7680Sstevel@tonic-gate 		return (NULL);
7690Sstevel@tonic-gate 	}
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	if (pcitool_debug)
7720Sstevel@tonic-gate 		prom_printf("Got base virtual address:0x%p\n", virt_base);
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	/* Now map the allocated virtual space to the physical address. */
7750Sstevel@tonic-gate 	hat_devload(kas.a_hat, virt_base, mmu_ptob(*num_pages),
7760Sstevel@tonic-gate 	    mmu_btop(page_base), PROT_READ | PROT_WRITE | HAT_STRICTORDER,
7770Sstevel@tonic-gate 	    HAT_LOAD_LOCK);
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	returned_addr = ((uintptr_t)(virt_base)) + offset;
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	if (pcitool_debug)
7820Sstevel@tonic-gate 		prom_printf("pcitool_map: returning VA:0x%p\n",
7830Sstevel@tonic-gate 		    (void *)(uintptr_t)returned_addr);
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	return (returned_addr);
7860Sstevel@tonic-gate }
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate /* Unmap the mapped page(s). */
7890Sstevel@tonic-gate static void
7900Sstevel@tonic-gate pcitool_unmap(uint64_t virt_addr, size_t num_pages)
7910Sstevel@tonic-gate {
7920Sstevel@tonic-gate 	void *base_virt_addr = (void *)(uintptr_t)(virt_addr & ~MMU_PAGEOFFSET);
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	hat_unload(kas.a_hat, base_virt_addr, ptob(num_pages),
7950Sstevel@tonic-gate 	    HAT_UNLOAD_UNLOCK);
7960Sstevel@tonic-gate 	vmem_free(heap_arena, base_virt_addr, ptob(num_pages));
7970Sstevel@tonic-gate }
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate /* Perform register accesses on PCI leaf devices. */
8010Sstevel@tonic-gate int
802777Sschwartz pcitool_dev_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode)
8030Sstevel@tonic-gate {
8040Sstevel@tonic-gate 	boolean_t	write_flag = B_FALSE;
8050Sstevel@tonic-gate 	int		rval = 0;
8060Sstevel@tonic-gate 	pcitool_reg_t	prg;
8070Sstevel@tonic-gate 	uint8_t		size;
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	uint64_t	base_addr;
8100Sstevel@tonic-gate 	uint64_t	virt_addr;
8110Sstevel@tonic-gate 	size_t		num_virt_pages;
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	switch (cmd) {
8140Sstevel@tonic-gate 	case (PCITOOL_DEVICE_SET_REG):
8150Sstevel@tonic-gate 		write_flag = B_TRUE;
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	/*FALLTHRU*/
8180Sstevel@tonic-gate 	case (PCITOOL_DEVICE_GET_REG):
8190Sstevel@tonic-gate 		if (pcitool_debug)
8200Sstevel@tonic-gate 			prom_printf("pci_dev_reg_ops set/get reg\n");
8210Sstevel@tonic-gate 		if (ddi_copyin(arg, &prg, sizeof (pcitool_reg_t), mode) !=
8220Sstevel@tonic-gate 		    DDI_SUCCESS) {
8230Sstevel@tonic-gate 			if (pcitool_debug)
8240Sstevel@tonic-gate 				prom_printf("Error reading arguments\n");
8250Sstevel@tonic-gate 			return (EFAULT);
8260Sstevel@tonic-gate 		}
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 		if (prg.barnum >= (sizeof (pci_bars) / sizeof (pci_bars[0]))) {
8290Sstevel@tonic-gate 			prg.status = PCITOOL_OUT_OF_RANGE;
8300Sstevel@tonic-gate 			rval = EINVAL;
8310Sstevel@tonic-gate 			goto done_reg;
8320Sstevel@tonic-gate 		}
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 		if (pcitool_debug)
8350Sstevel@tonic-gate 			prom_printf("raw bus:0x%x, dev:0x%x, func:0x%x\n",
8360Sstevel@tonic-gate 			    prg.bus_no, prg.dev_no, prg.func_no);
8370Sstevel@tonic-gate 		/* Validate address arguments of bus / dev / func */
8380Sstevel@tonic-gate 		if (((prg.bus_no &
8390Sstevel@tonic-gate 		    (PCI_REG_BUS_M >> PCI_REG_BUS_SHIFT)) !=
8400Sstevel@tonic-gate 		    prg.bus_no) ||
8410Sstevel@tonic-gate 		    ((prg.dev_no &
8420Sstevel@tonic-gate 		    (PCI_REG_DEV_M >> PCI_REG_DEV_SHIFT)) !=
8430Sstevel@tonic-gate 		    prg.dev_no) ||
8440Sstevel@tonic-gate 		    ((prg.func_no &
8450Sstevel@tonic-gate 		    (PCI_REG_FUNC_M >> PCI_REG_FUNC_SHIFT)) !=
8460Sstevel@tonic-gate 		    prg.func_no)) {
8470Sstevel@tonic-gate 			prg.status = PCITOOL_INVALID_ADDRESS;
8480Sstevel@tonic-gate 			rval = EINVAL;
8490Sstevel@tonic-gate 			goto done_reg;
8500Sstevel@tonic-gate 		}
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 		size = PCITOOL_ACC_ATTR_SIZE(prg.acc_attr);
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 		/* Proper config space desired. */
8550Sstevel@tonic-gate 		if (prg.barnum == 0) {
8560Sstevel@tonic-gate 
857777Sschwartz 			if (pcitool_debug)
858777Sschwartz 				prom_printf(
859777Sschwartz 				    "config access: offset:0x%" PRIx64 ", "
860777Sschwartz 				    "phys_addr:0x%" PRIx64 "\n",
861777Sschwartz 				    prg.offset, prg.phys_addr);
862777Sschwartz 
863777Sschwartz 			if (prg.offset >= max_cfg_size) {
8640Sstevel@tonic-gate 				prg.status = PCITOOL_OUT_OF_RANGE;
8650Sstevel@tonic-gate 				rval = EINVAL;
8660Sstevel@tonic-gate 				goto done_reg;
8670Sstevel@tonic-gate 			}
8680Sstevel@tonic-gate 
8691083Sanish 			/*
8701083Sanish 			 * Access device.  prg is modified.
8711083Sanish 			 * First, check for AMD northbridges for I/O access
8721083Sanish 			 * (This fix will move in future to pcitool user-land)
8731083Sanish 			 * Next, check for PCIe devices and do
8741083Sanish 			 * memory-mapped access
8751083Sanish 			 * Lastly, check for PCI devices and do I/O access
8761083Sanish 			 */
877*2434Sanish 			if ((prg.bus_no == 0) &&
878*2434Sanish 			    (prg.dev_no >= 0x18) &&
879*2434Sanish 			    (prg.dev_no < (0x18 + ncpus))) {
8801083Sanish 				if (cpuid_getvendor(CPU) == X86_VENDOR_AMD)
8811083Sanish 					rval = pcitool_cfg_access(dip, &prg,
8821083Sanish 					    write_flag);
8831083Sanish 			} else if (max_cfg_size == PCIE_CONF_HDR_SIZE)
884777Sschwartz 				rval = pcitool_pciex_cfg_access(dip, &prg,
885777Sschwartz 				    write_flag);
886777Sschwartz 			else
887777Sschwartz 				rval = pcitool_cfg_access(dip, &prg,
888777Sschwartz 				    write_flag);
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 			if (pcitool_debug)
8910Sstevel@tonic-gate 				prom_printf(
8920Sstevel@tonic-gate 				    "config access: data:0x%" PRIx64 "\n",
8930Sstevel@tonic-gate 				    prg.data);
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 		/* IO/ MEM/ MEM64 space. */
8960Sstevel@tonic-gate 		} else {
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 			pcitool_reg_t	prg2;
8990Sstevel@tonic-gate 			bcopy(&prg, &prg2, sizeof (pcitool_reg_t));
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 			/*
9020Sstevel@tonic-gate 			 * Translate BAR number into offset of the BAR in
9030Sstevel@tonic-gate 			 * the device's config space.
9040Sstevel@tonic-gate 			 */
9050Sstevel@tonic-gate 			prg2.offset = pci_bars[prg2.barnum];
9060Sstevel@tonic-gate 			prg2.acc_attr =
9070Sstevel@tonic-gate 			    PCITOOL_ACC_ATTR_SIZE_4 | PCITOOL_ACC_ATTR_ENDN_LTL;
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 			if (pcitool_debug)
9100Sstevel@tonic-gate 				prom_printf(
9110Sstevel@tonic-gate 				    "barnum:%d, bar_offset:0x%" PRIx64 "\n",
9120Sstevel@tonic-gate 				    prg2.barnum, prg2.offset);
9130Sstevel@tonic-gate 			/*
9140Sstevel@tonic-gate 			 * Get Bus Address Register (BAR) from config space.
9150Sstevel@tonic-gate 			 * prg2.offset is the offset into config space of the
9160Sstevel@tonic-gate 			 * BAR desired.  prg.status is modified on error.
9170Sstevel@tonic-gate 			 */
9180Sstevel@tonic-gate 			rval = pcitool_cfg_access(dip, &prg2, B_FALSE);
9190Sstevel@tonic-gate 			if (rval != SUCCESS) {
9200Sstevel@tonic-gate 				if (pcitool_debug)
9210Sstevel@tonic-gate 					prom_printf("BAR access failed\n");
9220Sstevel@tonic-gate 				prg.status = prg2.status;
9230Sstevel@tonic-gate 				goto done_reg;
9240Sstevel@tonic-gate 			}
9250Sstevel@tonic-gate 			/*
9260Sstevel@tonic-gate 			 * Reference proper PCI space based on the BAR.
9270Sstevel@tonic-gate 			 * If 64 bit MEM space, need to load other half of the
9280Sstevel@tonic-gate 			 * BAR first.
9290Sstevel@tonic-gate 			 */
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 			if (pcitool_debug)
9320Sstevel@tonic-gate 				prom_printf("bar returned is 0x%" PRIx64 "\n",
9330Sstevel@tonic-gate 				    prg2.data);
9340Sstevel@tonic-gate 			if (!prg2.data) {
9350Sstevel@tonic-gate 				if (pcitool_debug)
9360Sstevel@tonic-gate 					prom_printf("BAR data == 0\n");
9370Sstevel@tonic-gate 				rval = EINVAL;
9380Sstevel@tonic-gate 				prg.status = PCITOOL_INVALID_ADDRESS;
9390Sstevel@tonic-gate 				goto done_reg;
9400Sstevel@tonic-gate 			}
9410Sstevel@tonic-gate 			if (prg2.data == 0xffffffff) {
9420Sstevel@tonic-gate 				if (pcitool_debug)
9430Sstevel@tonic-gate 					prom_printf("BAR data == -1\n");
9440Sstevel@tonic-gate 				rval = EINVAL;
9450Sstevel@tonic-gate 				prg.status = PCITOOL_INVALID_ADDRESS;
9460Sstevel@tonic-gate 				goto done_reg;
9470Sstevel@tonic-gate 			}
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 			/*
9500Sstevel@tonic-gate 			 * BAR has bits saying this space is IO space, unless
9510Sstevel@tonic-gate 			 * this is the ROM address register.
9520Sstevel@tonic-gate 			 */
9530Sstevel@tonic-gate 			if (((PCI_BASE_SPACE_M & prg2.data) ==
9540Sstevel@tonic-gate 			    PCI_BASE_SPACE_IO) &&
9550Sstevel@tonic-gate 			    (prg2.offset != PCI_CONF_ROM)) {
9560Sstevel@tonic-gate 				if (pcitool_debug)
9570Sstevel@tonic-gate 					prom_printf("IO space\n");
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 				prg2.data &= PCI_BASE_IO_ADDR_M;
9600Sstevel@tonic-gate 				prg.phys_addr = prg2.data + prg.offset;
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 				rval = pcitool_io_access(dip, &prg, write_flag);
9630Sstevel@tonic-gate 				if ((rval != SUCCESS) && (pcitool_debug))
9640Sstevel@tonic-gate 					prom_printf("IO access failed\n");
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 				goto done_reg;
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 			/*
9700Sstevel@tonic-gate 			 * BAR has bits saying this space is 64 bit memory
9710Sstevel@tonic-gate 			 * space, unless this is the ROM address register.
9720Sstevel@tonic-gate 			 *
9730Sstevel@tonic-gate 			 * The 64 bit address stored in two BAR cells is not
9740Sstevel@tonic-gate 			 * necessarily aligned on an 8-byte boundary.
9750Sstevel@tonic-gate 			 * Need to keep the first 4 bytes read,
9760Sstevel@tonic-gate 			 * and do a separate read of the high 4 bytes.
9770Sstevel@tonic-gate 			 */
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 			} else if ((PCI_BASE_TYPE_ALL & prg2.data) &&
9800Sstevel@tonic-gate 			    (prg2.offset != PCI_CONF_ROM)) {
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 				uint32_t low_bytes =
9830Sstevel@tonic-gate 				    (uint32_t)(prg2.data & ~PCI_BASE_TYPE_ALL);
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 				/*
9860Sstevel@tonic-gate 				 * Don't try to read the next 4 bytes
9870Sstevel@tonic-gate 				 * past the end of BARs.
9880Sstevel@tonic-gate 				 */
9890Sstevel@tonic-gate 				if (prg2.offset >= PCI_CONF_BASE5) {
9900Sstevel@tonic-gate 					prg.status = PCITOOL_OUT_OF_RANGE;
9910Sstevel@tonic-gate 					rval = EIO;
9920Sstevel@tonic-gate 					goto done_reg;
9930Sstevel@tonic-gate 				}
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 				/*
9960Sstevel@tonic-gate 				 * Access device.
9970Sstevel@tonic-gate 				 * prg2.status is modified on error.
9980Sstevel@tonic-gate 				 */
9990Sstevel@tonic-gate 				prg2.offset += 4;
10000Sstevel@tonic-gate 				rval = pcitool_cfg_access(dip, &prg2, B_FALSE);
10010Sstevel@tonic-gate 				if (rval != SUCCESS) {
10020Sstevel@tonic-gate 					prg.status = prg2.status;
10030Sstevel@tonic-gate 					goto done_reg;
10040Sstevel@tonic-gate 				}
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 				if (prg2.data == 0xffffffff) {
10070Sstevel@tonic-gate 					prg.status = PCITOOL_INVALID_ADDRESS;
10080Sstevel@tonic-gate 					prg.status = EFAULT;
10090Sstevel@tonic-gate 					goto done_reg;
10100Sstevel@tonic-gate 				}
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 				prg2.data = (prg2.data << 32) + low_bytes;
10130Sstevel@tonic-gate 				if (pcitool_debug)
10140Sstevel@tonic-gate 					prom_printf(
10150Sstevel@tonic-gate 					    "64 bit mem space.  "
10160Sstevel@tonic-gate 					    "64-bit bar is 0x%" PRIx64 "\n",
10170Sstevel@tonic-gate 					    prg2.data);
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 			/* Mem32 space, including ROM */
10200Sstevel@tonic-gate 			} else {
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 				if (prg2.offset == PCI_CONF_ROM) {
10230Sstevel@tonic-gate 					if (pcitool_debug)
10240Sstevel@tonic-gate 						prom_printf(
10250Sstevel@tonic-gate 						    "Additional ROM "
10260Sstevel@tonic-gate 						    "checking\n");
10270Sstevel@tonic-gate 					/* Can't write to ROM */
10280Sstevel@tonic-gate 					if (write_flag) {
10290Sstevel@tonic-gate 						prg.status = PCITOOL_ROM_WRITE;
10300Sstevel@tonic-gate 						rval = EIO;
10310Sstevel@tonic-gate 						goto done_reg;
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 					/* ROM disabled for reading */
10340Sstevel@tonic-gate 					} else if (!(prg2.data & 0x00000001)) {
10350Sstevel@tonic-gate 						prg.status =
10360Sstevel@tonic-gate 						    PCITOOL_ROM_DISABLED;
10370Sstevel@tonic-gate 						rval = EIO;
10380Sstevel@tonic-gate 						goto done_reg;
10390Sstevel@tonic-gate 					}
10400Sstevel@tonic-gate 				}
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 				if (pcitool_debug)
10430Sstevel@tonic-gate 					prom_printf("32 bit mem space\n");
10440Sstevel@tonic-gate 			}
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 			/* Common code for all IO/MEM range spaces. */
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 			base_addr = prg2.data;
10490Sstevel@tonic-gate 			if (pcitool_debug)
10500Sstevel@tonic-gate 				prom_printf(
10510Sstevel@tonic-gate 				    "addr portion of bar is 0x%" PRIx64 ", "
10520Sstevel@tonic-gate 				    "base=0x%" PRIx64 ", "
10530Sstevel@tonic-gate 				    "offset:0x%" PRIx64 "\n",
10540Sstevel@tonic-gate 				    prg2.data, base_addr, prg.offset);
10550Sstevel@tonic-gate 			/*
10560Sstevel@tonic-gate 			 * Use offset provided by caller to index into
10570Sstevel@tonic-gate 			 * desired space, then access.
10580Sstevel@tonic-gate 			 * Note that prg.status is modified on error.
10590Sstevel@tonic-gate 			 */
10600Sstevel@tonic-gate 			prg.phys_addr = base_addr + prg.offset;
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 			virt_addr = pcitool_map(prg.phys_addr, size,
10630Sstevel@tonic-gate 			    &num_virt_pages);
10640Sstevel@tonic-gate 			if (virt_addr == NULL) {
10650Sstevel@tonic-gate 				prg.status = PCITOOL_IO_ERROR;
10660Sstevel@tonic-gate 				rval = EIO;
10670Sstevel@tonic-gate 				goto done_reg;
10680Sstevel@tonic-gate 			}
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 			rval = pcitool_mem_access(dip, &prg, virt_addr,
10710Sstevel@tonic-gate 			    write_flag);
10720Sstevel@tonic-gate 			pcitool_unmap(virt_addr, num_virt_pages);
10730Sstevel@tonic-gate 		}
10740Sstevel@tonic-gate done_reg:
10750Sstevel@tonic-gate 		if (ddi_copyout(&prg, arg, sizeof (pcitool_reg_t), mode) !=
10760Sstevel@tonic-gate 		    DDI_SUCCESS) {
10770Sstevel@tonic-gate 			if (pcitool_debug)
10780Sstevel@tonic-gate 				prom_printf("Error returning arguments.\n");
10790Sstevel@tonic-gate 			rval = EFAULT;
10800Sstevel@tonic-gate 		}
10810Sstevel@tonic-gate 		break;
10820Sstevel@tonic-gate 	default:
10830Sstevel@tonic-gate 		rval = ENOTTY;
10840Sstevel@tonic-gate 		break;
10850Sstevel@tonic-gate 	}
10860Sstevel@tonic-gate 	return (rval);
10870Sstevel@tonic-gate }
1088