xref: /onnv-gate/usr/src/uts/i86pc/io/pci/pci_tools.c (revision 12243:78579915ae6d)
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 /*
2212212SZhijun.Fu@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #include <sys/types.h>
260Sstevel@tonic-gate #include <sys/mkdev.h>
27117Sschwartz #include <sys/stat.h>
280Sstevel@tonic-gate #include <sys/sunddi.h>
290Sstevel@tonic-gate #include <vm/seg_kmem.h>
300Sstevel@tonic-gate #include <sys/machparam.h>
31916Sschwartz #include <sys/sunndi.h>
320Sstevel@tonic-gate #include <sys/ontrap.h>
33916Sschwartz #include <sys/psm.h>
34881Sjohnny #include <sys/pcie.h>
350Sstevel@tonic-gate #include <sys/pci_cfgspace.h>
360Sstevel@tonic-gate #include <sys/pci_tools.h>
371083Sanish #include <io/pci/pci_tools_ext.h>
383446Smrj #include <sys/apic.h>
39916Sschwartz #include <io/pci/pci_var.h>
4010923SEvan.Yan@Sun.COM #include <sys/pci_impl.h>
410Sstevel@tonic-gate #include <sys/promif.h>
421083Sanish #include <sys/x86_archext.h>
432434Sanish #include <sys/cpuvar.h>
4411245SZhijun.Fu@Sun.COM #include <sys/pci_cfgacc.h>
450Sstevel@tonic-gate 
465084Sjohnlev #ifdef __xpv
475084Sjohnlev #include <sys/hypervisor.h>
485084Sjohnlev #endif
495084Sjohnlev 
50777Sschwartz #define	PCIEX_BDF_OFFSET_DELTA	4
51777Sschwartz #define	PCIEX_REG_FUNC_SHIFT	(PCI_REG_FUNC_SHIFT + PCIEX_BDF_OFFSET_DELTA)
52777Sschwartz #define	PCIEX_REG_DEV_SHIFT	(PCI_REG_DEV_SHIFT + PCIEX_BDF_OFFSET_DELTA)
53777Sschwartz #define	PCIEX_REG_BUS_SHIFT	(PCI_REG_BUS_SHIFT + PCIEX_BDF_OFFSET_DELTA)
54777Sschwartz 
550Sstevel@tonic-gate #define	SUCCESS	0
560Sstevel@tonic-gate 
5711245SZhijun.Fu@Sun.COM extern uint64_t mcfg_mem_base;
580Sstevel@tonic-gate int pcitool_debug = 0;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate  * Offsets of BARS in config space.  First entry of 0 means config space.
620Sstevel@tonic-gate  * Entries here correlate to pcitool_bars_t enumerated type.
630Sstevel@tonic-gate  */
640Sstevel@tonic-gate static uint8_t pci_bars[] = {
650Sstevel@tonic-gate 	0x0,
660Sstevel@tonic-gate 	PCI_CONF_BASE0,
670Sstevel@tonic-gate 	PCI_CONF_BASE1,
680Sstevel@tonic-gate 	PCI_CONF_BASE2,
690Sstevel@tonic-gate 	PCI_CONF_BASE3,
700Sstevel@tonic-gate 	PCI_CONF_BASE4,
710Sstevel@tonic-gate 	PCI_CONF_BASE5,
720Sstevel@tonic-gate 	PCI_CONF_ROM
730Sstevel@tonic-gate };
740Sstevel@tonic-gate 
75777Sschwartz /* Max offset allowed into config space for a particular device. */
76777Sschwartz static uint64_t max_cfg_size = PCI_CONF_HDR_SIZE;
77777Sschwartz 
780Sstevel@tonic-gate static uint64_t pcitool_swap_endian(uint64_t data, int size);
7911245SZhijun.Fu@Sun.COM static int pcitool_cfg_access(pcitool_reg_t *prg, boolean_t write_flag,
8011245SZhijun.Fu@Sun.COM     boolean_t io_access);
8111245SZhijun.Fu@Sun.COM static int pcitool_io_access(pcitool_reg_t *prg, boolean_t write_flag);
8211245SZhijun.Fu@Sun.COM static int pcitool_mem_access(pcitool_reg_t *prg, uint64_t virt_addr,
830Sstevel@tonic-gate     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 
874397Sschwartz /* 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,
9910923SEvan.Yan@Sun.COM 	    PCI_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,
10510923SEvan.Yan@Sun.COM 	    PCI_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;
1324397Sschwartz 	size_t copyinout_size;
133916Sschwartz 	int rval = SUCCESS;
134916Sschwartz 
1354397Sschwartz 	/* Version 1 of pcitool_intr_set_t doesn't have flags. */
1364397Sschwartz 	copyinout_size = (size_t)&iset.flags - (size_t)&iset;
1374397Sschwartz 
1384397Sschwartz 	if (ddi_copyin(arg, &iset, copyinout_size, mode) != DDI_SUCCESS)
139916Sschwartz 		return (EFAULT);
140916Sschwartz 
1414397Sschwartz 	switch (iset.user_version) {
1424397Sschwartz 	case PCITOOL_V1:
1434397Sschwartz 		break;
1444397Sschwartz 
1454397Sschwartz 	case PCITOOL_V2:
1464397Sschwartz 		copyinout_size = sizeof (pcitool_intr_set_t);
1474397Sschwartz 		if (ddi_copyin(arg, &iset, copyinout_size, mode) != DDI_SUCCESS)
1484397Sschwartz 			return (EFAULT);
1494397Sschwartz 		break;
1504397Sschwartz 
1514397Sschwartz 	default:
1524397Sschwartz 		iset.status = PCITOOL_OUT_OF_RANGE;
1534397Sschwartz 		rval = ENOTSUP;
1544397Sschwartz 		goto done_set_intr;
1554397Sschwartz 	}
1564397Sschwartz 
15710053SEvan.Yan@Sun.COM 	if (iset.flags & PCITOOL_INTR_FLAG_SET_MSI) {
15810053SEvan.Yan@Sun.COM 		rval = ENOTSUP;
15910053SEvan.Yan@Sun.COM 		iset.status = PCITOOL_IO_ERROR;
16010053SEvan.Yan@Sun.COM 		goto done_set_intr;
16110053SEvan.Yan@Sun.COM 	}
16210053SEvan.Yan@Sun.COM 
163916Sschwartz 	if (iset.ino > APIC_MAX_VECTOR) {
164916Sschwartz 		rval = EINVAL;
165916Sschwartz 		iset.status = PCITOOL_INVALID_INO;
166916Sschwartz 		goto done_set_intr;
167916Sschwartz 	}
168916Sschwartz 
169916Sschwartz 	iset.status = PCITOOL_SUCCESS;
170916Sschwartz 
171916Sschwartz 	if ((old_cpu = pci_get_cpu_from_vecirq(iset.ino, IS_VEC)) == -1) {
172916Sschwartz 		iset.status = PCITOOL_IO_ERROR;
173916Sschwartz 		rval = EINVAL;
174916Sschwartz 		goto done_set_intr;
175916Sschwartz 	}
176916Sschwartz 
1774397Sschwartz 
178916Sschwartz 	old_cpu &= ~PSMGI_CPU_USER_BOUND;
179916Sschwartz 
180916Sschwartz 	/*
181916Sschwartz 	 * For this locally-declared and used handle, ih_private will contain a
182916Sschwartz 	 * CPU value, not an ihdl_plat_t as used for global interrupt handling.
183916Sschwartz 	 */
184916Sschwartz 	info_hdl.ih_vector = iset.ino;
185916Sschwartz 	info_hdl.ih_private = (void *)(uintptr_t)iset.cpu_id;
18610053SEvan.Yan@Sun.COM 	info_hdl.ih_flags = PSMGI_INTRBY_VEC;
1874397Sschwartz 	if (pcitool_debug)
1884397Sschwartz 		prom_printf("user version:%d, flags:0x%x\n",
1894397Sschwartz 		    iset.user_version, iset.flags);
190916Sschwartz 
1914397Sschwartz 	result = ENOTSUP;
1924397Sschwartz 	if ((iset.user_version >= PCITOOL_V2) &&
19310053SEvan.Yan@Sun.COM 	    (iset.flags & PCITOOL_INTR_FLAG_SET_GROUP)) {
1944397Sschwartz 		ret = (*psm_intr_ops)(NULL, &info_hdl, PSM_INTR_OP_GRP_SET_CPU,
1954397Sschwartz 		    &result);
1964397Sschwartz 	} else {
1974397Sschwartz 		ret = (*psm_intr_ops)(NULL, &info_hdl, PSM_INTR_OP_SET_CPU,
1984397Sschwartz 		    &result);
1994397Sschwartz 	}
2004397Sschwartz 
201916Sschwartz 	if (ret != PSM_SUCCESS) {
202916Sschwartz 		switch (result) {
203916Sschwartz 		case EIO:		/* Error making the change */
204916Sschwartz 			rval = EIO;
205916Sschwartz 			iset.status = PCITOOL_IO_ERROR;
206916Sschwartz 			break;
207916Sschwartz 		case ENXIO:		/* Couldn't convert vector to irq */
208916Sschwartz 			rval = EINVAL;
209916Sschwartz 			iset.status = PCITOOL_INVALID_INO;
210916Sschwartz 			break;
211916Sschwartz 		case EINVAL:		/* CPU out of range */
212916Sschwartz 			rval = EINVAL;
213916Sschwartz 			iset.status = PCITOOL_INVALID_CPUID;
214916Sschwartz 			break;
2154397Sschwartz 		case ENOTSUP:		/* Requested PSM intr ops missing */
2164397Sschwartz 			rval = ENOTSUP;
2174397Sschwartz 			iset.status = PCITOOL_IO_ERROR;
2184397Sschwartz 			break;
219916Sschwartz 		}
220916Sschwartz 	}
221916Sschwartz 
222916Sschwartz 	/* Return original CPU. */
223916Sschwartz 	iset.cpu_id = old_cpu;
224916Sschwartz 
225916Sschwartz done_set_intr:
2264397Sschwartz 	iset.drvr_version = PCITOOL_VERSION;
2274397Sschwartz 	if (ddi_copyout(&iset, arg, copyinout_size, mode) != DDI_SUCCESS)
228916Sschwartz 		rval = EFAULT;
229916Sschwartz 	return (rval);
230916Sschwartz }
231916Sschwartz 
232916Sschwartz 
233916Sschwartz /* It is assumed that dip != NULL */
234916Sschwartz static void
235916Sschwartz pcitool_get_intr_dev_info(dev_info_t *dip, pcitool_intr_dev_t *devs)
236916Sschwartz {
237916Sschwartz 	(void) strncpy(devs->driver_name,
2389537SErwin.Tsaur@Sun.COM 	    ddi_driver_name(dip), MAXMODCONFNAME-2);
2399537SErwin.Tsaur@Sun.COM 	devs->driver_name[MAXMODCONFNAME-1] = '\0';
240916Sschwartz 	(void) ddi_pathname(dip, devs->path);
241916Sschwartz 	devs->dev_inst = ddi_get_instance(dip);
242916Sschwartz }
243916Sschwartz 
244916Sschwartz static int
245916Sschwartz pcitool_get_intr(dev_info_t *dip, void *arg, int mode)
246916Sschwartz {
247916Sschwartz 	/* Array part isn't used here, but oh well... */
248916Sschwartz 	pcitool_intr_get_t partial_iget;
249916Sschwartz 	pcitool_intr_get_t *iget = &partial_iget;
250916Sschwartz 	size_t	iget_kmem_alloc_size = 0;
251916Sschwartz 	uint8_t num_devs_ret;
252916Sschwartz 	int copyout_rval;
253916Sschwartz 	int rval = SUCCESS;
254916Sschwartz 	int circ;
255916Sschwartz 	int i;
256916Sschwartz 
257916Sschwartz 	ddi_intr_handle_impl_t info_hdl;
258916Sschwartz 	apic_get_intr_t intr_info;
259916Sschwartz 
260916Sschwartz 	/* Read in just the header part, no array section. */
261916Sschwartz 	if (ddi_copyin(arg, &partial_iget, PCITOOL_IGET_SIZE(0), mode) !=
262916Sschwartz 	    DDI_SUCCESS)
263916Sschwartz 		return (EFAULT);
264916Sschwartz 
26510053SEvan.Yan@Sun.COM 	if (partial_iget.flags & PCITOOL_INTR_FLAG_GET_MSI) {
26610053SEvan.Yan@Sun.COM 		partial_iget.status = PCITOOL_IO_ERROR;
26710053SEvan.Yan@Sun.COM 		partial_iget.num_devs_ret = 0;
26810053SEvan.Yan@Sun.COM 		rval = ENOTSUP;
26910053SEvan.Yan@Sun.COM 		goto done_get_intr;
27010053SEvan.Yan@Sun.COM 	}
27110053SEvan.Yan@Sun.COM 
272916Sschwartz 	/* Validate argument. */
273916Sschwartz 	if (partial_iget.ino > APIC_MAX_VECTOR) {
274916Sschwartz 		partial_iget.status = PCITOOL_INVALID_INO;
275916Sschwartz 		partial_iget.num_devs_ret = 0;
276916Sschwartz 		rval = EINVAL;
277916Sschwartz 		goto done_get_intr;
278916Sschwartz 	}
279916Sschwartz 
280916Sschwartz 	num_devs_ret = partial_iget.num_devs_ret;
281916Sschwartz 	intr_info.avgi_dip_list = NULL;
282916Sschwartz 	intr_info.avgi_req_flags =
283916Sschwartz 	    PSMGI_REQ_CPUID | PSMGI_REQ_NUM_DEVS | PSMGI_INTRBY_VEC;
284916Sschwartz 	/*
285916Sschwartz 	 * For this locally-declared and used handle, ih_private will contain a
286916Sschwartz 	 * pointer to apic_get_intr_t, not an ihdl_plat_t as used for
287916Sschwartz 	 * global interrupt handling.
288916Sschwartz 	 */
289916Sschwartz 	info_hdl.ih_private = &intr_info;
290916Sschwartz 	info_hdl.ih_vector = partial_iget.ino;
291916Sschwartz 
292916Sschwartz 	/* Caller wants device information returned. */
293916Sschwartz 	if (num_devs_ret > 0) {
294916Sschwartz 
295916Sschwartz 		intr_info.avgi_req_flags |= PSMGI_REQ_GET_DEVS;
296916Sschwartz 
297916Sschwartz 		/*
298916Sschwartz 		 * Allocate room.
299916Sschwartz 		 * If num_devs_ret == 0 iget remains pointing to partial_iget.
300916Sschwartz 		 */
301916Sschwartz 		iget_kmem_alloc_size = PCITOOL_IGET_SIZE(num_devs_ret);
302916Sschwartz 		iget = kmem_alloc(iget_kmem_alloc_size, KM_SLEEP);
303916Sschwartz 
304916Sschwartz 		/* Read in whole structure to verify there's room. */
305916Sschwartz 		if (ddi_copyin(arg, iget, iget_kmem_alloc_size, mode) !=
306916Sschwartz 		    SUCCESS) {
307916Sschwartz 
308916Sschwartz 			/* Be consistent and just return EFAULT here. */
309916Sschwartz 			kmem_free(iget, iget_kmem_alloc_size);
310916Sschwartz 
311916Sschwartz 			return (EFAULT);
312916Sschwartz 		}
313916Sschwartz 	}
314916Sschwartz 
315916Sschwartz 	bzero(iget, PCITOOL_IGET_SIZE(num_devs_ret));
316916Sschwartz 	iget->ino = info_hdl.ih_vector;
317916Sschwartz 
318916Sschwartz 	/*
319916Sschwartz 	 * Lock device tree branch from the pci root nexus on down if info will
320916Sschwartz 	 * be extracted from dips returned from the tree.
321916Sschwartz 	 */
322916Sschwartz 	if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) {
323916Sschwartz 		ndi_devi_enter(dip, &circ);
324916Sschwartz 	}
325916Sschwartz 
326916Sschwartz 	/* Call psm_intr_ops(PSM_INTR_OP_GET_INTR) to get information. */
327916Sschwartz 	if ((rval = (*psm_intr_ops)(NULL, &info_hdl,
328916Sschwartz 	    PSM_INTR_OP_GET_INTR, NULL)) != PSM_SUCCESS) {
329916Sschwartz 		iget->status = PCITOOL_IO_ERROR;
330916Sschwartz 		iget->num_devs_ret = 0;
331916Sschwartz 		rval = EINVAL;
332916Sschwartz 		goto done_get_intr;
333916Sschwartz 	}
334916Sschwartz 
335916Sschwartz 	/*
336916Sschwartz 	 * Fill in the pcitool_intr_get_t to be returned,
337916Sschwartz 	 * with the CPU, num_devs_ret and num_devs.
338916Sschwartz 	 */
339*12243SEvan.Yan@Sun.COM 	if (intr_info.avgi_cpu_id == IRQ_UNBOUND ||
340*12243SEvan.Yan@Sun.COM 	    intr_info.avgi_cpu_id == IRQ_UNINIT)
341*12243SEvan.Yan@Sun.COM 		iget->cpu_id = 0;
342*12243SEvan.Yan@Sun.COM 	else
343*12243SEvan.Yan@Sun.COM 		iget->cpu_id = intr_info.avgi_cpu_id & ~PSMGI_CPU_USER_BOUND;
344916Sschwartz 
345916Sschwartz 	/* Number of devices returned by apic. */
346916Sschwartz 	iget->num_devs = intr_info.avgi_num_devs;
347916Sschwartz 
348916Sschwartz 	/* Device info was returned. */
349916Sschwartz 	if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) {
350916Sschwartz 
351916Sschwartz 		/*
352916Sschwartz 		 * num devs returned is num devs ret by apic,
353916Sschwartz 		 * space permitting.
354916Sschwartz 		 */
355916Sschwartz 		iget->num_devs_ret = min(num_devs_ret, intr_info.avgi_num_devs);
356916Sschwartz 
357916Sschwartz 		/*
358916Sschwartz 		 * Loop thru list of dips and extract driver, name and instance.
359916Sschwartz 		 * Fill in the pcitool_intr_dev_t's with this info.
360916Sschwartz 		 */
361916Sschwartz 		for (i = 0; i < iget->num_devs_ret; i++)
362916Sschwartz 			pcitool_get_intr_dev_info(intr_info.avgi_dip_list[i],
363916Sschwartz 			    &iget->dev[i]);
364916Sschwartz 
365916Sschwartz 		/* Free kmem_alloc'ed memory of the apic_get_intr_t */
366916Sschwartz 		kmem_free(intr_info.avgi_dip_list,
367916Sschwartz 		    intr_info.avgi_num_devs * sizeof (dev_info_t *));
368916Sschwartz 	}
369916Sschwartz 
370916Sschwartz done_get_intr:
371916Sschwartz 
372916Sschwartz 	if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) {
373916Sschwartz 		ndi_devi_exit(dip, circ);
374916Sschwartz 	}
375916Sschwartz 
3764397Sschwartz 	iget->drvr_version = PCITOOL_VERSION;
377916Sschwartz 	copyout_rval = ddi_copyout(iget, arg,
378916Sschwartz 	    PCITOOL_IGET_SIZE(num_devs_ret), mode);
379916Sschwartz 
380916Sschwartz 	if (iget_kmem_alloc_size > 0)
381916Sschwartz 		kmem_free(iget, iget_kmem_alloc_size);
382916Sschwartz 
383916Sschwartz 	if (copyout_rval != DDI_SUCCESS)
384916Sschwartz 		rval = EFAULT;
385916Sschwartz 
386916Sschwartz 	return (rval);
387916Sschwartz }
388916Sschwartz 
3894397Sschwartz /*ARGSUSED*/
3904397Sschwartz static int
3914397Sschwartz pcitool_intr_info(dev_info_t *dip, void *arg, int mode)
3924397Sschwartz {
3934397Sschwartz 	pcitool_intr_info_t intr_info;
3944397Sschwartz 	ddi_intr_handle_impl_t info_hdl;
3954397Sschwartz 	int rval = SUCCESS;
3964397Sschwartz 
3974397Sschwartz 	/* If we need user_version, and to ret same user version as passed in */
3984397Sschwartz 	if (ddi_copyin(arg, &intr_info, sizeof (pcitool_intr_info_t), mode) !=
3994397Sschwartz 	    DDI_SUCCESS) {
4004397Sschwartz 		if (pcitool_debug)
4014397Sschwartz 			prom_printf("Error reading arguments\n");
4024397Sschwartz 		return (EFAULT);
4034397Sschwartz 	}
4044397Sschwartz 
40510053SEvan.Yan@Sun.COM 	if (intr_info.flags & PCITOOL_INTR_FLAG_GET_MSI)
40610053SEvan.Yan@Sun.COM 		return (ENOTSUP);
40710053SEvan.Yan@Sun.COM 
4084397Sschwartz 	/* For UPPC systems, psm_intr_ops has no entry for APIC_TYPE. */
4094397Sschwartz 	if ((rval = (*psm_intr_ops)(NULL, &info_hdl,
4104397Sschwartz 	    PSM_INTR_OP_APIC_TYPE, NULL)) != PSM_SUCCESS) {
4114397Sschwartz 		intr_info.ctlr_type = PCITOOL_CTLR_TYPE_UPPC;
4124397Sschwartz 		intr_info.ctlr_version = 0;
4134397Sschwartz 
4144397Sschwartz 	} else {
4154397Sschwartz 		intr_info.ctlr_version = (uint32_t)info_hdl.ih_ver;
4164397Sschwartz 		if (strcmp((char *)info_hdl.ih_private,
4174397Sschwartz 		    APIC_PCPLUSMP_NAME) == 0)
4184397Sschwartz 			intr_info.ctlr_type = PCITOOL_CTLR_TYPE_PCPLUSMP;
4194397Sschwartz 		else
4204397Sschwartz 			intr_info.ctlr_type = PCITOOL_CTLR_TYPE_UNKNOWN;
4214397Sschwartz 	}
4224397Sschwartz 
4234397Sschwartz 	intr_info.num_intr = APIC_MAX_VECTOR;
4244397Sschwartz 	intr_info.drvr_version = PCITOOL_VERSION;
4254397Sschwartz 	if (ddi_copyout(&intr_info, arg, sizeof (pcitool_intr_info_t), mode) !=
4264397Sschwartz 	    DDI_SUCCESS) {
4274397Sschwartz 		if (pcitool_debug)
4284397Sschwartz 			prom_printf("Error returning arguments.\n");
4294397Sschwartz 		rval = EFAULT;
4304397Sschwartz 	}
4314397Sschwartz 
4324397Sschwartz 	return (rval);
4334397Sschwartz }
4344397Sschwartz 
4354397Sschwartz 
436916Sschwartz 
437916Sschwartz /*
438916Sschwartz  * Main function for handling interrupt CPU binding requests and queries.
439916Sschwartz  * Need to implement later
440916Sschwartz  */
441916Sschwartz int
442916Sschwartz pcitool_intr_admn(dev_info_t *dip, void *arg, int cmd, int mode)
443916Sschwartz {
444916Sschwartz 	int rval;
445916Sschwartz 
446916Sschwartz 	switch (cmd) {
447916Sschwartz 
448916Sschwartz 	/* Associate a new CPU with a given vector */
449916Sschwartz 	case PCITOOL_DEVICE_SET_INTR:
450916Sschwartz 		rval = pcitool_set_intr(dip, arg, mode);
451916Sschwartz 		break;
452916Sschwartz 
453916Sschwartz 	case PCITOOL_DEVICE_GET_INTR:
454916Sschwartz 		rval = pcitool_get_intr(dip, arg, mode);
455916Sschwartz 		break;
456916Sschwartz 
4574397Sschwartz 	case PCITOOL_SYSTEM_INTR_INFO:
4584397Sschwartz 		rval = pcitool_intr_info(dip, arg, mode);
459916Sschwartz 		break;
460916Sschwartz 
461916Sschwartz 	default:
462916Sschwartz 		rval = ENOTSUP;
463916Sschwartz 	}
464916Sschwartz 
465916Sschwartz 	return (rval);
466916Sschwartz }
467916Sschwartz 
4680Sstevel@tonic-gate /*
4690Sstevel@tonic-gate  * Perform register accesses on the nexus device itself.
4700Sstevel@tonic-gate  * No explicit PCI nexus device for X86, so not applicable.
4710Sstevel@tonic-gate  */
472916Sschwartz 
4730Sstevel@tonic-gate /*ARGSUSED*/
4740Sstevel@tonic-gate int
475777Sschwartz pcitool_bus_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode)
4760Sstevel@tonic-gate {
4770Sstevel@tonic-gate 	return (ENOTSUP);
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate /* Swap endianness. */
4810Sstevel@tonic-gate static uint64_t
4820Sstevel@tonic-gate pcitool_swap_endian(uint64_t data, int size)
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate 	typedef union {
4850Sstevel@tonic-gate 		uint64_t data64;
4860Sstevel@tonic-gate 		uint8_t data8[8];
4870Sstevel@tonic-gate 	} data_split_t;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	data_split_t orig_data;
4900Sstevel@tonic-gate 	data_split_t returned_data;
4910Sstevel@tonic-gate 	int i;
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	orig_data.data64 = data;
4940Sstevel@tonic-gate 	returned_data.data64 = 0;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	for (i = 0; i < size; i++) {
4970Sstevel@tonic-gate 		returned_data.data8[i] = orig_data.data8[size - 1 - i];
4980Sstevel@tonic-gate 	}
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	return (returned_data.data64);
5010Sstevel@tonic-gate }
5020Sstevel@tonic-gate 
503777Sschwartz /*
50411245SZhijun.Fu@Sun.COM  * A note about ontrap handling:
505777Sschwartz  *
50611245SZhijun.Fu@Sun.COM  * X86 systems on which this module was tested return FFs instead of bus errors
50711245SZhijun.Fu@Sun.COM  * when accessing devices with invalid addresses.  Ontrap handling, which
50811245SZhijun.Fu@Sun.COM  * gracefully handles kernel bus errors, is installed anyway for I/O and mem
50911245SZhijun.Fu@Sun.COM  * space accessing (not for pci config space), in case future X86 platforms
51011245SZhijun.Fu@Sun.COM  * require it.
511777Sschwartz  */
512777Sschwartz 
5130Sstevel@tonic-gate /* Access device.  prg is modified. */
5140Sstevel@tonic-gate static int
51511245SZhijun.Fu@Sun.COM pcitool_cfg_access(pcitool_reg_t *prg, boolean_t write_flag,
51611245SZhijun.Fu@Sun.COM     boolean_t io_access)
5170Sstevel@tonic-gate {
5180Sstevel@tonic-gate 	int size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
5190Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
5200Sstevel@tonic-gate 	int rval = SUCCESS;
5210Sstevel@tonic-gate 	uint64_t local_data;
52211245SZhijun.Fu@Sun.COM 	pci_cfgacc_req_t req;
52311245SZhijun.Fu@Sun.COM 	uint32_t max_offset;
52411245SZhijun.Fu@Sun.COM 
52511245SZhijun.Fu@Sun.COM 	if ((size <= 0) || (size > 8) || ((size & (size - 1)) != 0)) {
52611245SZhijun.Fu@Sun.COM 		prg->status = PCITOOL_INVALID_SIZE;
52711245SZhijun.Fu@Sun.COM 		return (ENOTSUP);
52811245SZhijun.Fu@Sun.COM 	}
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	/*
5318772SDan.Mick@Sun.COM 	 * NOTE: there is no way to verify whether or not the address is
5328772SDan.Mick@Sun.COM 	 * valid other than that it is within the maximum offset.  The
53311245SZhijun.Fu@Sun.COM 	 * put functions return void and the get functions return -1 on error.
5340Sstevel@tonic-gate 	 */
5358772SDan.Mick@Sun.COM 
53611245SZhijun.Fu@Sun.COM 	if (io_access)
53711245SZhijun.Fu@Sun.COM 		max_offset = 0xFF;
53811245SZhijun.Fu@Sun.COM 	else
53911245SZhijun.Fu@Sun.COM 		max_offset = 0xFFF;
54011245SZhijun.Fu@Sun.COM 	if (prg->offset + size - 1 > max_offset) {
5418772SDan.Mick@Sun.COM 		prg->status = PCITOOL_INVALID_ADDRESS;
5428772SDan.Mick@Sun.COM 		return (ENOTSUP);
5438772SDan.Mick@Sun.COM 	}
5448772SDan.Mick@Sun.COM 
5450Sstevel@tonic-gate 	prg->status = PCITOOL_SUCCESS;
5460Sstevel@tonic-gate 
54711245SZhijun.Fu@Sun.COM 	req.rcdip = NULL;
54811245SZhijun.Fu@Sun.COM 	req.bdf = PCI_GETBDF(prg->bus_no, prg->dev_no, prg->func_no);
54911245SZhijun.Fu@Sun.COM 	req.offset = prg->offset;
55011245SZhijun.Fu@Sun.COM 	req.size = size;
55111245SZhijun.Fu@Sun.COM 	req.write = write_flag;
55211245SZhijun.Fu@Sun.COM 	req.ioacc = io_access;
5530Sstevel@tonic-gate 	if (write_flag) {
5540Sstevel@tonic-gate 		if (big_endian) {
5550Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
5560Sstevel@tonic-gate 		} else {
5570Sstevel@tonic-gate 			local_data = prg->data;
5580Sstevel@tonic-gate 		}
55911245SZhijun.Fu@Sun.COM 		VAL64(&req) = local_data;
56011245SZhijun.Fu@Sun.COM 		pci_cfgacc_acc(&req);
5610Sstevel@tonic-gate 	} else {
56211245SZhijun.Fu@Sun.COM 		pci_cfgacc_acc(&req);
56312212SZhijun.Fu@Sun.COM 		switch (size) {
56412212SZhijun.Fu@Sun.COM 		case 1:
56512212SZhijun.Fu@Sun.COM 			local_data = VAL8(&req);
56612212SZhijun.Fu@Sun.COM 			break;
56712212SZhijun.Fu@Sun.COM 		case 2:
56812212SZhijun.Fu@Sun.COM 			local_data = VAL16(&req);
56912212SZhijun.Fu@Sun.COM 			break;
57012212SZhijun.Fu@Sun.COM 		case 4:
57112212SZhijun.Fu@Sun.COM 			local_data = VAL32(&req);
57212212SZhijun.Fu@Sun.COM 			break;
57312212SZhijun.Fu@Sun.COM 		case 8:
57412212SZhijun.Fu@Sun.COM 			local_data = VAL64(&req);
57512212SZhijun.Fu@Sun.COM 			break;
57612212SZhijun.Fu@Sun.COM 		}
57711245SZhijun.Fu@Sun.COM 		if (big_endian) {
57811245SZhijun.Fu@Sun.COM 			prg->data =
57911245SZhijun.Fu@Sun.COM 			    pcitool_swap_endian(local_data, size);
58011245SZhijun.Fu@Sun.COM 		} else {
58111245SZhijun.Fu@Sun.COM 			prg->data = local_data;
5820Sstevel@tonic-gate 		}
5830Sstevel@tonic-gate 	}
58411245SZhijun.Fu@Sun.COM 	/*
58511245SZhijun.Fu@Sun.COM 	 * Check if legacy IO config access is used, in which case
58611245SZhijun.Fu@Sun.COM 	 * only first 256 bytes are valid.
58711245SZhijun.Fu@Sun.COM 	 */
58811245SZhijun.Fu@Sun.COM 	if (req.ioacc && (prg->offset + size - 1 > 0xFF)) {
58911245SZhijun.Fu@Sun.COM 		prg->status = PCITOOL_INVALID_ADDRESS;
59011245SZhijun.Fu@Sun.COM 		return (ENOTSUP);
59111245SZhijun.Fu@Sun.COM 	}
59211245SZhijun.Fu@Sun.COM 
59311245SZhijun.Fu@Sun.COM 	/* Set phys_addr only if MMIO is used */
59411245SZhijun.Fu@Sun.COM 	prg->phys_addr = 0;
59511245SZhijun.Fu@Sun.COM 	if (!req.ioacc && mcfg_mem_base != 0) {
59611245SZhijun.Fu@Sun.COM 		prg->phys_addr = mcfg_mem_base + prg->offset +
59711245SZhijun.Fu@Sun.COM 		    ((prg->bus_no << PCIEX_REG_BUS_SHIFT) |
59811245SZhijun.Fu@Sun.COM 		    (prg->dev_no << PCIEX_REG_DEV_SHIFT) |
59911245SZhijun.Fu@Sun.COM 		    (prg->func_no << PCIEX_REG_FUNC_SHIFT));
60011245SZhijun.Fu@Sun.COM 	}
60111245SZhijun.Fu@Sun.COM 
6020Sstevel@tonic-gate 	return (rval);
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate static int
60611245SZhijun.Fu@Sun.COM pcitool_io_access(pcitool_reg_t *prg, boolean_t write_flag)
6070Sstevel@tonic-gate {
6080Sstevel@tonic-gate 	int port = (int)prg->phys_addr;
6090Sstevel@tonic-gate 	size_t size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
6100Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
6110Sstevel@tonic-gate 	int rval = SUCCESS;
6120Sstevel@tonic-gate 	on_trap_data_t otd;
6130Sstevel@tonic-gate 	uint64_t local_data;
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	/*
6170Sstevel@tonic-gate 	 * on_trap works like setjmp.
6180Sstevel@tonic-gate 	 *
6190Sstevel@tonic-gate 	 * A non-zero return here means on_trap has returned from an error.
6200Sstevel@tonic-gate 	 *
6210Sstevel@tonic-gate 	 * A zero return here means that on_trap has just returned from setup.
6220Sstevel@tonic-gate 	 */
6230Sstevel@tonic-gate 	if (on_trap(&otd, OT_DATA_ACCESS)) {
6240Sstevel@tonic-gate 		no_trap();
6250Sstevel@tonic-gate 		if (pcitool_debug)
6260Sstevel@tonic-gate 			prom_printf(
6274397Sschwartz 			    "pcitool_io_access: on_trap caught an error...\n");
6280Sstevel@tonic-gate 		prg->status = PCITOOL_INVALID_ADDRESS;
6290Sstevel@tonic-gate 		return (EFAULT);
6300Sstevel@tonic-gate 	}
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	if (write_flag) {
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 		if (big_endian) {
6350Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
6360Sstevel@tonic-gate 		} else {
6370Sstevel@tonic-gate 			local_data = prg->data;
6380Sstevel@tonic-gate 		}
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 		if (pcitool_debug)
6410Sstevel@tonic-gate 			prom_printf("Writing %ld byte(s) to port 0x%x\n",
6420Sstevel@tonic-gate 			    size, port);
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 		switch (size) {
6450Sstevel@tonic-gate 		case 1:
6460Sstevel@tonic-gate 			outb(port, (uint8_t)local_data);
6470Sstevel@tonic-gate 			break;
6480Sstevel@tonic-gate 		case 2:
6490Sstevel@tonic-gate 			outw(port, (uint16_t)local_data);
6500Sstevel@tonic-gate 			break;
6510Sstevel@tonic-gate 		case 4:
6520Sstevel@tonic-gate 			outl(port, (uint32_t)local_data);
6530Sstevel@tonic-gate 			break;
6540Sstevel@tonic-gate 		default:
6550Sstevel@tonic-gate 			rval = ENOTSUP;
6560Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
6570Sstevel@tonic-gate 			break;
6580Sstevel@tonic-gate 		}
6590Sstevel@tonic-gate 	} else {
6600Sstevel@tonic-gate 		if (pcitool_debug)
6610Sstevel@tonic-gate 			prom_printf("Reading %ld byte(s) from port 0x%x\n",
6620Sstevel@tonic-gate 			    size, port);
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 		switch (size) {
6650Sstevel@tonic-gate 		case 1:
6660Sstevel@tonic-gate 			local_data = inb(port);
6670Sstevel@tonic-gate 			break;
6680Sstevel@tonic-gate 		case 2:
6690Sstevel@tonic-gate 			local_data = inw(port);
6700Sstevel@tonic-gate 			break;
6710Sstevel@tonic-gate 		case 4:
6720Sstevel@tonic-gate 			local_data = inl(port);
6730Sstevel@tonic-gate 			break;
6740Sstevel@tonic-gate 		default:
6750Sstevel@tonic-gate 			rval = ENOTSUP;
6760Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
6770Sstevel@tonic-gate 			break;
6780Sstevel@tonic-gate 		}
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 		if (rval == SUCCESS) {
6810Sstevel@tonic-gate 			if (big_endian) {
6820Sstevel@tonic-gate 				prg->data =
6830Sstevel@tonic-gate 				    pcitool_swap_endian(local_data, size);
6840Sstevel@tonic-gate 			} else {
6850Sstevel@tonic-gate 				prg->data = local_data;
6860Sstevel@tonic-gate 			}
6870Sstevel@tonic-gate 		}
6880Sstevel@tonic-gate 	}
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	no_trap();
6910Sstevel@tonic-gate 	return (rval);
6920Sstevel@tonic-gate }
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate static int
69511245SZhijun.Fu@Sun.COM pcitool_mem_access(pcitool_reg_t *prg, uint64_t virt_addr, boolean_t write_flag)
6960Sstevel@tonic-gate {
6970Sstevel@tonic-gate 	size_t size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
6980Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
6990Sstevel@tonic-gate 	int rval = DDI_SUCCESS;
7000Sstevel@tonic-gate 	on_trap_data_t otd;
7010Sstevel@tonic-gate 	uint64_t local_data;
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	/*
7040Sstevel@tonic-gate 	 * on_trap works like setjmp.
7050Sstevel@tonic-gate 	 *
7060Sstevel@tonic-gate 	 * A non-zero return here means on_trap has returned from an error.
7070Sstevel@tonic-gate 	 *
7080Sstevel@tonic-gate 	 * A zero return here means that on_trap has just returned from setup.
7090Sstevel@tonic-gate 	 */
7100Sstevel@tonic-gate 	if (on_trap(&otd, OT_DATA_ACCESS)) {
7110Sstevel@tonic-gate 		no_trap();
7120Sstevel@tonic-gate 		if (pcitool_debug)
7130Sstevel@tonic-gate 			prom_printf(
7140Sstevel@tonic-gate 			    "pcitool_mem_access: on_trap caught an error...\n");
7150Sstevel@tonic-gate 		prg->status = PCITOOL_INVALID_ADDRESS;
7160Sstevel@tonic-gate 		return (EFAULT);
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	if (write_flag) {
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 		if (big_endian) {
7220Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
7230Sstevel@tonic-gate 		} else {
7240Sstevel@tonic-gate 			local_data = prg->data;
7250Sstevel@tonic-gate 		}
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 		switch (size) {
7280Sstevel@tonic-gate 		case 1:
7290Sstevel@tonic-gate 			*((uint8_t *)(uintptr_t)virt_addr) = local_data;
7300Sstevel@tonic-gate 			break;
7310Sstevel@tonic-gate 		case 2:
7320Sstevel@tonic-gate 			*((uint16_t *)(uintptr_t)virt_addr) = local_data;
7330Sstevel@tonic-gate 			break;
7340Sstevel@tonic-gate 		case 4:
7350Sstevel@tonic-gate 			*((uint32_t *)(uintptr_t)virt_addr) = local_data;
7360Sstevel@tonic-gate 			break;
7370Sstevel@tonic-gate 		case 8:
7380Sstevel@tonic-gate 			*((uint64_t *)(uintptr_t)virt_addr) = local_data;
7390Sstevel@tonic-gate 			break;
7400Sstevel@tonic-gate 		default:
7410Sstevel@tonic-gate 			rval = ENOTSUP;
7420Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
7430Sstevel@tonic-gate 			break;
7440Sstevel@tonic-gate 		}
7450Sstevel@tonic-gate 	} else {
7460Sstevel@tonic-gate 		switch (size) {
7470Sstevel@tonic-gate 		case 1:
7480Sstevel@tonic-gate 			local_data = *((uint8_t *)(uintptr_t)virt_addr);
7490Sstevel@tonic-gate 			break;
7500Sstevel@tonic-gate 		case 2:
7510Sstevel@tonic-gate 			local_data = *((uint16_t *)(uintptr_t)virt_addr);
7520Sstevel@tonic-gate 			break;
7530Sstevel@tonic-gate 		case 4:
7540Sstevel@tonic-gate 			local_data = *((uint32_t *)(uintptr_t)virt_addr);
7550Sstevel@tonic-gate 			break;
7560Sstevel@tonic-gate 		case 8:
7570Sstevel@tonic-gate 			local_data = *((uint64_t *)(uintptr_t)virt_addr);
7580Sstevel@tonic-gate 			break;
7590Sstevel@tonic-gate 		default:
7600Sstevel@tonic-gate 			rval = ENOTSUP;
7610Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
7620Sstevel@tonic-gate 			break;
7630Sstevel@tonic-gate 		}
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 		if (rval == SUCCESS) {
7660Sstevel@tonic-gate 			if (big_endian) {
7670Sstevel@tonic-gate 				prg->data =
7680Sstevel@tonic-gate 				    pcitool_swap_endian(local_data, size);
7690Sstevel@tonic-gate 			} else {
7700Sstevel@tonic-gate 				prg->data = local_data;
7710Sstevel@tonic-gate 			}
7720Sstevel@tonic-gate 		}
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	no_trap();
7760Sstevel@tonic-gate 	return (rval);
7770Sstevel@tonic-gate }
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate /*
7800Sstevel@tonic-gate  * Map up to 2 pages which contain the address we want to access.
7810Sstevel@tonic-gate  *
7820Sstevel@tonic-gate  * Mapping should span no more than 8 bytes.  With X86 it is possible for an
7830Sstevel@tonic-gate  * 8 byte value to start on a 4 byte boundary, so it can cross a page boundary.
7840Sstevel@tonic-gate  * We'll never have to map more than two pages.
7850Sstevel@tonic-gate  */
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate static uint64_t
7880Sstevel@tonic-gate pcitool_map(uint64_t phys_addr, size_t size, size_t *num_pages)
7890Sstevel@tonic-gate {
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	uint64_t page_base = phys_addr & ~MMU_PAGEOFFSET;
7920Sstevel@tonic-gate 	uint64_t offset = phys_addr & MMU_PAGEOFFSET;
7930Sstevel@tonic-gate 	void *virt_base;
7940Sstevel@tonic-gate 	uint64_t returned_addr;
7953446Smrj 	pfn_t pfn;
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	if (pcitool_debug)
7980Sstevel@tonic-gate 		prom_printf("pcitool_map: Called with PA:0x%p\n",
7997632SNick.Todd@Sun.COM 		    (void *)(uintptr_t)phys_addr);
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	*num_pages = 1;
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	/* Desired mapping would span more than two pages. */
8040Sstevel@tonic-gate 	if ((offset + size) > (MMU_PAGESIZE * 2)) {
8050Sstevel@tonic-gate 		if (pcitool_debug)
8060Sstevel@tonic-gate 			prom_printf("boundary violation: "
807777Sschwartz 			    "offset:0x%" PRIx64 ", size:%ld, pagesize:0x%lx\n",
808777Sschwartz 			    offset, (uintptr_t)size, (uintptr_t)MMU_PAGESIZE);
8090Sstevel@tonic-gate 		return (NULL);
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	} else if ((offset + size) > MMU_PAGESIZE) {
8120Sstevel@tonic-gate 		(*num_pages)++;
8130Sstevel@tonic-gate 	}
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	/* Get page(s) of virtual space. */
8160Sstevel@tonic-gate 	virt_base = vmem_alloc(heap_arena, ptob(*num_pages), VM_NOSLEEP);
8170Sstevel@tonic-gate 	if (virt_base == NULL) {
8180Sstevel@tonic-gate 		if (pcitool_debug)
8190Sstevel@tonic-gate 			prom_printf("Couldn't get virtual base address.\n");
8200Sstevel@tonic-gate 		return (NULL);
8210Sstevel@tonic-gate 	}
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	if (pcitool_debug)
8240Sstevel@tonic-gate 		prom_printf("Got base virtual address:0x%p\n", virt_base);
8250Sstevel@tonic-gate 
8265084Sjohnlev #ifdef __xpv
8275084Sjohnlev 	/*
8285084Sjohnlev 	 * We should only get here if we are dom0.
8295084Sjohnlev 	 * We're using a real device so we need to translate the MA to a PFN.
8305084Sjohnlev 	 */
8315084Sjohnlev 	ASSERT(DOMAIN_IS_INITDOMAIN(xen_info));
8325084Sjohnlev 	pfn = xen_assign_pfn(mmu_btop(page_base));
8335084Sjohnlev #else
8343446Smrj 	pfn = btop(page_base);
8355084Sjohnlev #endif
8363446Smrj 
8370Sstevel@tonic-gate 	/* Now map the allocated virtual space to the physical address. */
8383446Smrj 	hat_devload(kas.a_hat, virt_base, mmu_ptob(*num_pages), pfn,
8393446Smrj 	    PROT_READ | PROT_WRITE | HAT_STRICTORDER,
8400Sstevel@tonic-gate 	    HAT_LOAD_LOCK);
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 	returned_addr = ((uintptr_t)(virt_base)) + offset;
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	if (pcitool_debug)
8450Sstevel@tonic-gate 		prom_printf("pcitool_map: returning VA:0x%p\n",
8460Sstevel@tonic-gate 		    (void *)(uintptr_t)returned_addr);
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	return (returned_addr);
8490Sstevel@tonic-gate }
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate /* Unmap the mapped page(s). */
8520Sstevel@tonic-gate static void
8530Sstevel@tonic-gate pcitool_unmap(uint64_t virt_addr, size_t num_pages)
8540Sstevel@tonic-gate {
8550Sstevel@tonic-gate 	void *base_virt_addr = (void *)(uintptr_t)(virt_addr & ~MMU_PAGEOFFSET);
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	hat_unload(kas.a_hat, base_virt_addr, ptob(num_pages),
8580Sstevel@tonic-gate 	    HAT_UNLOAD_UNLOCK);
8590Sstevel@tonic-gate 	vmem_free(heap_arena, base_virt_addr, ptob(num_pages));
8600Sstevel@tonic-gate }
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate /* Perform register accesses on PCI leaf devices. */
86411245SZhijun.Fu@Sun.COM /*ARGSUSED*/
8650Sstevel@tonic-gate int
866777Sschwartz pcitool_dev_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode)
8670Sstevel@tonic-gate {
8680Sstevel@tonic-gate 	boolean_t	write_flag = B_FALSE;
86911245SZhijun.Fu@Sun.COM 	boolean_t	io_access = B_TRUE;
8700Sstevel@tonic-gate 	int		rval = 0;
8710Sstevel@tonic-gate 	pcitool_reg_t	prg;
8720Sstevel@tonic-gate 	uint8_t		size;
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	uint64_t	base_addr;
8750Sstevel@tonic-gate 	uint64_t	virt_addr;
8760Sstevel@tonic-gate 	size_t		num_virt_pages;
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	switch (cmd) {
8790Sstevel@tonic-gate 	case (PCITOOL_DEVICE_SET_REG):
8800Sstevel@tonic-gate 		write_flag = B_TRUE;
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	/*FALLTHRU*/
8830Sstevel@tonic-gate 	case (PCITOOL_DEVICE_GET_REG):
8840Sstevel@tonic-gate 		if (pcitool_debug)
8850Sstevel@tonic-gate 			prom_printf("pci_dev_reg_ops set/get reg\n");
8860Sstevel@tonic-gate 		if (ddi_copyin(arg, &prg, sizeof (pcitool_reg_t), mode) !=
8870Sstevel@tonic-gate 		    DDI_SUCCESS) {
8880Sstevel@tonic-gate 			if (pcitool_debug)
8890Sstevel@tonic-gate 				prom_printf("Error reading arguments\n");
8900Sstevel@tonic-gate 			return (EFAULT);
8910Sstevel@tonic-gate 		}
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 		if (prg.barnum >= (sizeof (pci_bars) / sizeof (pci_bars[0]))) {
8940Sstevel@tonic-gate 			prg.status = PCITOOL_OUT_OF_RANGE;
8950Sstevel@tonic-gate 			rval = EINVAL;
8960Sstevel@tonic-gate 			goto done_reg;
8970Sstevel@tonic-gate 		}
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 		if (pcitool_debug)
9000Sstevel@tonic-gate 			prom_printf("raw bus:0x%x, dev:0x%x, func:0x%x\n",
9010Sstevel@tonic-gate 			    prg.bus_no, prg.dev_no, prg.func_no);
9020Sstevel@tonic-gate 		/* Validate address arguments of bus / dev / func */
9030Sstevel@tonic-gate 		if (((prg.bus_no &
9040Sstevel@tonic-gate 		    (PCI_REG_BUS_M >> PCI_REG_BUS_SHIFT)) !=
9050Sstevel@tonic-gate 		    prg.bus_no) ||
9060Sstevel@tonic-gate 		    ((prg.dev_no &
9070Sstevel@tonic-gate 		    (PCI_REG_DEV_M >> PCI_REG_DEV_SHIFT)) !=
9080Sstevel@tonic-gate 		    prg.dev_no) ||
9090Sstevel@tonic-gate 		    ((prg.func_no &
9100Sstevel@tonic-gate 		    (PCI_REG_FUNC_M >> PCI_REG_FUNC_SHIFT)) !=
9110Sstevel@tonic-gate 		    prg.func_no)) {
9120Sstevel@tonic-gate 			prg.status = PCITOOL_INVALID_ADDRESS;
9130Sstevel@tonic-gate 			rval = EINVAL;
9140Sstevel@tonic-gate 			goto done_reg;
9150Sstevel@tonic-gate 		}
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 		size = PCITOOL_ACC_ATTR_SIZE(prg.acc_attr);
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 		/* Proper config space desired. */
9200Sstevel@tonic-gate 		if (prg.barnum == 0) {
9210Sstevel@tonic-gate 
922777Sschwartz 			if (pcitool_debug)
923777Sschwartz 				prom_printf(
924777Sschwartz 				    "config access: offset:0x%" PRIx64 ", "
925777Sschwartz 				    "phys_addr:0x%" PRIx64 "\n",
926777Sschwartz 				    prg.offset, prg.phys_addr);
927777Sschwartz 
928777Sschwartz 			if (prg.offset >= max_cfg_size) {
9290Sstevel@tonic-gate 				prg.status = PCITOOL_OUT_OF_RANGE;
9300Sstevel@tonic-gate 				rval = EINVAL;
9310Sstevel@tonic-gate 				goto done_reg;
9320Sstevel@tonic-gate 			}
93311245SZhijun.Fu@Sun.COM 			if (max_cfg_size == PCIE_CONF_HDR_SIZE)
93411245SZhijun.Fu@Sun.COM 				io_access = B_FALSE;
9350Sstevel@tonic-gate 
93611245SZhijun.Fu@Sun.COM 			rval = pcitool_cfg_access(&prg, write_flag, io_access);
9370Sstevel@tonic-gate 			if (pcitool_debug)
9380Sstevel@tonic-gate 				prom_printf(
9390Sstevel@tonic-gate 				    "config access: data:0x%" PRIx64 "\n",
9400Sstevel@tonic-gate 				    prg.data);
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 		/* IO/ MEM/ MEM64 space. */
9430Sstevel@tonic-gate 		} else {
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 			pcitool_reg_t	prg2;
9460Sstevel@tonic-gate 			bcopy(&prg, &prg2, sizeof (pcitool_reg_t));
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 			/*
9490Sstevel@tonic-gate 			 * Translate BAR number into offset of the BAR in
9500Sstevel@tonic-gate 			 * the device's config space.
9510Sstevel@tonic-gate 			 */
9520Sstevel@tonic-gate 			prg2.offset = pci_bars[prg2.barnum];
9530Sstevel@tonic-gate 			prg2.acc_attr =
9540Sstevel@tonic-gate 			    PCITOOL_ACC_ATTR_SIZE_4 | PCITOOL_ACC_ATTR_ENDN_LTL;
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 			if (pcitool_debug)
9570Sstevel@tonic-gate 				prom_printf(
9580Sstevel@tonic-gate 				    "barnum:%d, bar_offset:0x%" PRIx64 "\n",
9590Sstevel@tonic-gate 				    prg2.barnum, prg2.offset);
9600Sstevel@tonic-gate 			/*
9610Sstevel@tonic-gate 			 * Get Bus Address Register (BAR) from config space.
9620Sstevel@tonic-gate 			 * prg2.offset is the offset into config space of the
9630Sstevel@tonic-gate 			 * BAR desired.  prg.status is modified on error.
9640Sstevel@tonic-gate 			 */
96511245SZhijun.Fu@Sun.COM 			rval = pcitool_cfg_access(&prg2, B_FALSE, B_TRUE);
9660Sstevel@tonic-gate 			if (rval != SUCCESS) {
9670Sstevel@tonic-gate 				if (pcitool_debug)
9680Sstevel@tonic-gate 					prom_printf("BAR access failed\n");
9690Sstevel@tonic-gate 				prg.status = prg2.status;
9700Sstevel@tonic-gate 				goto done_reg;
9710Sstevel@tonic-gate 			}
9720Sstevel@tonic-gate 			/*
9730Sstevel@tonic-gate 			 * Reference proper PCI space based on the BAR.
9740Sstevel@tonic-gate 			 * If 64 bit MEM space, need to load other half of the
9750Sstevel@tonic-gate 			 * BAR first.
9760Sstevel@tonic-gate 			 */
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate 			if (pcitool_debug)
9790Sstevel@tonic-gate 				prom_printf("bar returned is 0x%" PRIx64 "\n",
9800Sstevel@tonic-gate 				    prg2.data);
9810Sstevel@tonic-gate 			if (!prg2.data) {
9820Sstevel@tonic-gate 				if (pcitool_debug)
9830Sstevel@tonic-gate 					prom_printf("BAR data == 0\n");
9840Sstevel@tonic-gate 				rval = EINVAL;
9850Sstevel@tonic-gate 				prg.status = PCITOOL_INVALID_ADDRESS;
9860Sstevel@tonic-gate 				goto done_reg;
9870Sstevel@tonic-gate 			}
9880Sstevel@tonic-gate 			if (prg2.data == 0xffffffff) {
9890Sstevel@tonic-gate 				if (pcitool_debug)
9900Sstevel@tonic-gate 					prom_printf("BAR data == -1\n");
9910Sstevel@tonic-gate 				rval = EINVAL;
9920Sstevel@tonic-gate 				prg.status = PCITOOL_INVALID_ADDRESS;
9930Sstevel@tonic-gate 				goto done_reg;
9940Sstevel@tonic-gate 			}
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 			/*
9970Sstevel@tonic-gate 			 * BAR has bits saying this space is IO space, unless
9980Sstevel@tonic-gate 			 * this is the ROM address register.
9990Sstevel@tonic-gate 			 */
10000Sstevel@tonic-gate 			if (((PCI_BASE_SPACE_M & prg2.data) ==
10010Sstevel@tonic-gate 			    PCI_BASE_SPACE_IO) &&
10020Sstevel@tonic-gate 			    (prg2.offset != PCI_CONF_ROM)) {
10030Sstevel@tonic-gate 				if (pcitool_debug)
10040Sstevel@tonic-gate 					prom_printf("IO space\n");
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 				prg2.data &= PCI_BASE_IO_ADDR_M;
10070Sstevel@tonic-gate 				prg.phys_addr = prg2.data + prg.offset;
10080Sstevel@tonic-gate 
100911245SZhijun.Fu@Sun.COM 				rval = pcitool_io_access(&prg, write_flag);
10100Sstevel@tonic-gate 				if ((rval != SUCCESS) && (pcitool_debug))
10110Sstevel@tonic-gate 					prom_printf("IO access failed\n");
10120Sstevel@tonic-gate 
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 64 bit memory
10180Sstevel@tonic-gate 			 * space, unless this is the ROM address register.
10190Sstevel@tonic-gate 			 *
10200Sstevel@tonic-gate 			 * The 64 bit address stored in two BAR cells is not
10210Sstevel@tonic-gate 			 * necessarily aligned on an 8-byte boundary.
10220Sstevel@tonic-gate 			 * Need to keep the first 4 bytes read,
10230Sstevel@tonic-gate 			 * and do a separate read of the high 4 bytes.
10240Sstevel@tonic-gate 			 */
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 			} else if ((PCI_BASE_TYPE_ALL & prg2.data) &&
10270Sstevel@tonic-gate 			    (prg2.offset != PCI_CONF_ROM)) {
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 				uint32_t low_bytes =
10300Sstevel@tonic-gate 				    (uint32_t)(prg2.data & ~PCI_BASE_TYPE_ALL);
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 				/*
10330Sstevel@tonic-gate 				 * Don't try to read the next 4 bytes
10340Sstevel@tonic-gate 				 * past the end of BARs.
10350Sstevel@tonic-gate 				 */
10360Sstevel@tonic-gate 				if (prg2.offset >= PCI_CONF_BASE5) {
10370Sstevel@tonic-gate 					prg.status = PCITOOL_OUT_OF_RANGE;
10380Sstevel@tonic-gate 					rval = EIO;
10390Sstevel@tonic-gate 					goto done_reg;
10400Sstevel@tonic-gate 				}
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 				/*
10430Sstevel@tonic-gate 				 * Access device.
10440Sstevel@tonic-gate 				 * prg2.status is modified on error.
10450Sstevel@tonic-gate 				 */
10460Sstevel@tonic-gate 				prg2.offset += 4;
104711245SZhijun.Fu@Sun.COM 				rval = pcitool_cfg_access(&prg2,
104811245SZhijun.Fu@Sun.COM 				    B_FALSE, B_TRUE);
10490Sstevel@tonic-gate 				if (rval != SUCCESS) {
10500Sstevel@tonic-gate 					prg.status = prg2.status;
10510Sstevel@tonic-gate 					goto done_reg;
10520Sstevel@tonic-gate 				}
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 				if (prg2.data == 0xffffffff) {
10550Sstevel@tonic-gate 					prg.status = PCITOOL_INVALID_ADDRESS;
10560Sstevel@tonic-gate 					prg.status = EFAULT;
10570Sstevel@tonic-gate 					goto done_reg;
10580Sstevel@tonic-gate 				}
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 				prg2.data = (prg2.data << 32) + low_bytes;
10610Sstevel@tonic-gate 				if (pcitool_debug)
10620Sstevel@tonic-gate 					prom_printf(
10630Sstevel@tonic-gate 					    "64 bit mem space.  "
10640Sstevel@tonic-gate 					    "64-bit bar is 0x%" PRIx64 "\n",
10650Sstevel@tonic-gate 					    prg2.data);
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 			/* Mem32 space, including ROM */
10680Sstevel@tonic-gate 			} else {
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 				if (prg2.offset == PCI_CONF_ROM) {
10710Sstevel@tonic-gate 					if (pcitool_debug)
10720Sstevel@tonic-gate 						prom_printf(
10730Sstevel@tonic-gate 						    "Additional ROM "
10740Sstevel@tonic-gate 						    "checking\n");
10750Sstevel@tonic-gate 					/* Can't write to ROM */
10760Sstevel@tonic-gate 					if (write_flag) {
10770Sstevel@tonic-gate 						prg.status = PCITOOL_ROM_WRITE;
10780Sstevel@tonic-gate 						rval = EIO;
10790Sstevel@tonic-gate 						goto done_reg;
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 					/* ROM disabled for reading */
10820Sstevel@tonic-gate 					} else if (!(prg2.data & 0x00000001)) {
10830Sstevel@tonic-gate 						prg.status =
10840Sstevel@tonic-gate 						    PCITOOL_ROM_DISABLED;
10850Sstevel@tonic-gate 						rval = EIO;
10860Sstevel@tonic-gate 						goto done_reg;
10870Sstevel@tonic-gate 					}
10880Sstevel@tonic-gate 				}
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 				if (pcitool_debug)
10910Sstevel@tonic-gate 					prom_printf("32 bit mem space\n");
10920Sstevel@tonic-gate 			}
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 			/* Common code for all IO/MEM range spaces. */
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 			base_addr = prg2.data;
10970Sstevel@tonic-gate 			if (pcitool_debug)
10980Sstevel@tonic-gate 				prom_printf(
10990Sstevel@tonic-gate 				    "addr portion of bar is 0x%" PRIx64 ", "
11000Sstevel@tonic-gate 				    "base=0x%" PRIx64 ", "
11010Sstevel@tonic-gate 				    "offset:0x%" PRIx64 "\n",
11020Sstevel@tonic-gate 				    prg2.data, base_addr, prg.offset);
11030Sstevel@tonic-gate 			/*
11040Sstevel@tonic-gate 			 * Use offset provided by caller to index into
11050Sstevel@tonic-gate 			 * desired space, then access.
11060Sstevel@tonic-gate 			 * Note that prg.status is modified on error.
11070Sstevel@tonic-gate 			 */
11080Sstevel@tonic-gate 			prg.phys_addr = base_addr + prg.offset;
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 			virt_addr = pcitool_map(prg.phys_addr, size,
11110Sstevel@tonic-gate 			    &num_virt_pages);
11120Sstevel@tonic-gate 			if (virt_addr == NULL) {
11130Sstevel@tonic-gate 				prg.status = PCITOOL_IO_ERROR;
11140Sstevel@tonic-gate 				rval = EIO;
11150Sstevel@tonic-gate 				goto done_reg;
11160Sstevel@tonic-gate 			}
11170Sstevel@tonic-gate 
111811245SZhijun.Fu@Sun.COM 			rval = pcitool_mem_access(&prg, virt_addr, write_flag);
11190Sstevel@tonic-gate 			pcitool_unmap(virt_addr, num_virt_pages);
11200Sstevel@tonic-gate 		}
11210Sstevel@tonic-gate done_reg:
11224397Sschwartz 		prg.drvr_version = PCITOOL_VERSION;
11230Sstevel@tonic-gate 		if (ddi_copyout(&prg, arg, sizeof (pcitool_reg_t), mode) !=
11240Sstevel@tonic-gate 		    DDI_SUCCESS) {
11250Sstevel@tonic-gate 			if (pcitool_debug)
11260Sstevel@tonic-gate 				prom_printf("Error returning arguments.\n");
11270Sstevel@tonic-gate 			rval = EFAULT;
11280Sstevel@tonic-gate 		}
11290Sstevel@tonic-gate 		break;
11300Sstevel@tonic-gate 	default:
11310Sstevel@tonic-gate 		rval = ENOTTY;
11320Sstevel@tonic-gate 		break;
11330Sstevel@tonic-gate 	}
11340Sstevel@tonic-gate 	return (rval);
11350Sstevel@tonic-gate }
1136