xref: /onnv-gate/usr/src/uts/i86pc/io/pci/pci_tools.c (revision 12212)
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 /*
22*12212SZhijun.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 	 */
339916Sschwartz 	iget->cpu_id = intr_info.avgi_cpu_id & ~PSMGI_CPU_USER_BOUND;
340916Sschwartz 
341916Sschwartz 	/* Number of devices returned by apic. */
342916Sschwartz 	iget->num_devs = intr_info.avgi_num_devs;
343916Sschwartz 
344916Sschwartz 	/* Device info was returned. */
345916Sschwartz 	if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) {
346916Sschwartz 
347916Sschwartz 		/*
348916Sschwartz 		 * num devs returned is num devs ret by apic,
349916Sschwartz 		 * space permitting.
350916Sschwartz 		 */
351916Sschwartz 		iget->num_devs_ret = min(num_devs_ret, intr_info.avgi_num_devs);
352916Sschwartz 
353916Sschwartz 		/*
354916Sschwartz 		 * Loop thru list of dips and extract driver, name and instance.
355916Sschwartz 		 * Fill in the pcitool_intr_dev_t's with this info.
356916Sschwartz 		 */
357916Sschwartz 		for (i = 0; i < iget->num_devs_ret; i++)
358916Sschwartz 			pcitool_get_intr_dev_info(intr_info.avgi_dip_list[i],
359916Sschwartz 			    &iget->dev[i]);
360916Sschwartz 
361916Sschwartz 		/* Free kmem_alloc'ed memory of the apic_get_intr_t */
362916Sschwartz 		kmem_free(intr_info.avgi_dip_list,
363916Sschwartz 		    intr_info.avgi_num_devs * sizeof (dev_info_t *));
364916Sschwartz 	}
365916Sschwartz 
366916Sschwartz done_get_intr:
367916Sschwartz 
368916Sschwartz 	if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) {
369916Sschwartz 		ndi_devi_exit(dip, circ);
370916Sschwartz 	}
371916Sschwartz 
3724397Sschwartz 	iget->drvr_version = PCITOOL_VERSION;
373916Sschwartz 	copyout_rval = ddi_copyout(iget, arg,
374916Sschwartz 	    PCITOOL_IGET_SIZE(num_devs_ret), mode);
375916Sschwartz 
376916Sschwartz 	if (iget_kmem_alloc_size > 0)
377916Sschwartz 		kmem_free(iget, iget_kmem_alloc_size);
378916Sschwartz 
379916Sschwartz 	if (copyout_rval != DDI_SUCCESS)
380916Sschwartz 		rval = EFAULT;
381916Sschwartz 
382916Sschwartz 	return (rval);
383916Sschwartz }
384916Sschwartz 
3854397Sschwartz /*ARGSUSED*/
3864397Sschwartz static int
3874397Sschwartz pcitool_intr_info(dev_info_t *dip, void *arg, int mode)
3884397Sschwartz {
3894397Sschwartz 	pcitool_intr_info_t intr_info;
3904397Sschwartz 	ddi_intr_handle_impl_t info_hdl;
3914397Sschwartz 	int rval = SUCCESS;
3924397Sschwartz 
3934397Sschwartz 	/* If we need user_version, and to ret same user version as passed in */
3944397Sschwartz 	if (ddi_copyin(arg, &intr_info, sizeof (pcitool_intr_info_t), mode) !=
3954397Sschwartz 	    DDI_SUCCESS) {
3964397Sschwartz 		if (pcitool_debug)
3974397Sschwartz 			prom_printf("Error reading arguments\n");
3984397Sschwartz 		return (EFAULT);
3994397Sschwartz 	}
4004397Sschwartz 
40110053SEvan.Yan@Sun.COM 	if (intr_info.flags & PCITOOL_INTR_FLAG_GET_MSI)
40210053SEvan.Yan@Sun.COM 		return (ENOTSUP);
40310053SEvan.Yan@Sun.COM 
4044397Sschwartz 	/* For UPPC systems, psm_intr_ops has no entry for APIC_TYPE. */
4054397Sschwartz 	if ((rval = (*psm_intr_ops)(NULL, &info_hdl,
4064397Sschwartz 	    PSM_INTR_OP_APIC_TYPE, NULL)) != PSM_SUCCESS) {
4074397Sschwartz 		intr_info.ctlr_type = PCITOOL_CTLR_TYPE_UPPC;
4084397Sschwartz 		intr_info.ctlr_version = 0;
4094397Sschwartz 
4104397Sschwartz 	} else {
4114397Sschwartz 		intr_info.ctlr_version = (uint32_t)info_hdl.ih_ver;
4124397Sschwartz 		if (strcmp((char *)info_hdl.ih_private,
4134397Sschwartz 		    APIC_PCPLUSMP_NAME) == 0)
4144397Sschwartz 			intr_info.ctlr_type = PCITOOL_CTLR_TYPE_PCPLUSMP;
4154397Sschwartz 		else
4164397Sschwartz 			intr_info.ctlr_type = PCITOOL_CTLR_TYPE_UNKNOWN;
4174397Sschwartz 	}
4184397Sschwartz 
4194397Sschwartz 	intr_info.num_intr = APIC_MAX_VECTOR;
4204397Sschwartz 	intr_info.drvr_version = PCITOOL_VERSION;
4214397Sschwartz 	if (ddi_copyout(&intr_info, arg, sizeof (pcitool_intr_info_t), mode) !=
4224397Sschwartz 	    DDI_SUCCESS) {
4234397Sschwartz 		if (pcitool_debug)
4244397Sschwartz 			prom_printf("Error returning arguments.\n");
4254397Sschwartz 		rval = EFAULT;
4264397Sschwartz 	}
4274397Sschwartz 
4284397Sschwartz 	return (rval);
4294397Sschwartz }
4304397Sschwartz 
4314397Sschwartz 
432916Sschwartz 
433916Sschwartz /*
434916Sschwartz  * Main function for handling interrupt CPU binding requests and queries.
435916Sschwartz  * Need to implement later
436916Sschwartz  */
437916Sschwartz int
438916Sschwartz pcitool_intr_admn(dev_info_t *dip, void *arg, int cmd, int mode)
439916Sschwartz {
440916Sschwartz 	int rval;
441916Sschwartz 
442916Sschwartz 	switch (cmd) {
443916Sschwartz 
444916Sschwartz 	/* Associate a new CPU with a given vector */
445916Sschwartz 	case PCITOOL_DEVICE_SET_INTR:
446916Sschwartz 		rval = pcitool_set_intr(dip, arg, mode);
447916Sschwartz 		break;
448916Sschwartz 
449916Sschwartz 	case PCITOOL_DEVICE_GET_INTR:
450916Sschwartz 		rval = pcitool_get_intr(dip, arg, mode);
451916Sschwartz 		break;
452916Sschwartz 
4534397Sschwartz 	case PCITOOL_SYSTEM_INTR_INFO:
4544397Sschwartz 		rval = pcitool_intr_info(dip, arg, mode);
455916Sschwartz 		break;
456916Sschwartz 
457916Sschwartz 	default:
458916Sschwartz 		rval = ENOTSUP;
459916Sschwartz 	}
460916Sschwartz 
461916Sschwartz 	return (rval);
462916Sschwartz }
463916Sschwartz 
4640Sstevel@tonic-gate /*
4650Sstevel@tonic-gate  * Perform register accesses on the nexus device itself.
4660Sstevel@tonic-gate  * No explicit PCI nexus device for X86, so not applicable.
4670Sstevel@tonic-gate  */
468916Sschwartz 
4690Sstevel@tonic-gate /*ARGSUSED*/
4700Sstevel@tonic-gate int
471777Sschwartz pcitool_bus_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode)
4720Sstevel@tonic-gate {
4730Sstevel@tonic-gate 	return (ENOTSUP);
4740Sstevel@tonic-gate }
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate /* Swap endianness. */
4770Sstevel@tonic-gate static uint64_t
4780Sstevel@tonic-gate pcitool_swap_endian(uint64_t data, int size)
4790Sstevel@tonic-gate {
4800Sstevel@tonic-gate 	typedef union {
4810Sstevel@tonic-gate 		uint64_t data64;
4820Sstevel@tonic-gate 		uint8_t data8[8];
4830Sstevel@tonic-gate 	} data_split_t;
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	data_split_t orig_data;
4860Sstevel@tonic-gate 	data_split_t returned_data;
4870Sstevel@tonic-gate 	int i;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	orig_data.data64 = data;
4900Sstevel@tonic-gate 	returned_data.data64 = 0;
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	for (i = 0; i < size; i++) {
4930Sstevel@tonic-gate 		returned_data.data8[i] = orig_data.data8[size - 1 - i];
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	return (returned_data.data64);
4970Sstevel@tonic-gate }
4980Sstevel@tonic-gate 
499777Sschwartz /*
50011245SZhijun.Fu@Sun.COM  * A note about ontrap handling:
501777Sschwartz  *
50211245SZhijun.Fu@Sun.COM  * X86 systems on which this module was tested return FFs instead of bus errors
50311245SZhijun.Fu@Sun.COM  * when accessing devices with invalid addresses.  Ontrap handling, which
50411245SZhijun.Fu@Sun.COM  * gracefully handles kernel bus errors, is installed anyway for I/O and mem
50511245SZhijun.Fu@Sun.COM  * space accessing (not for pci config space), in case future X86 platforms
50611245SZhijun.Fu@Sun.COM  * require it.
507777Sschwartz  */
508777Sschwartz 
5090Sstevel@tonic-gate /* Access device.  prg is modified. */
5100Sstevel@tonic-gate static int
51111245SZhijun.Fu@Sun.COM pcitool_cfg_access(pcitool_reg_t *prg, boolean_t write_flag,
51211245SZhijun.Fu@Sun.COM     boolean_t io_access)
5130Sstevel@tonic-gate {
5140Sstevel@tonic-gate 	int size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
5150Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
5160Sstevel@tonic-gate 	int rval = SUCCESS;
5170Sstevel@tonic-gate 	uint64_t local_data;
51811245SZhijun.Fu@Sun.COM 	pci_cfgacc_req_t req;
51911245SZhijun.Fu@Sun.COM 	uint32_t max_offset;
52011245SZhijun.Fu@Sun.COM 
52111245SZhijun.Fu@Sun.COM 	if ((size <= 0) || (size > 8) || ((size & (size - 1)) != 0)) {
52211245SZhijun.Fu@Sun.COM 		prg->status = PCITOOL_INVALID_SIZE;
52311245SZhijun.Fu@Sun.COM 		return (ENOTSUP);
52411245SZhijun.Fu@Sun.COM 	}
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	/*
5278772SDan.Mick@Sun.COM 	 * NOTE: there is no way to verify whether or not the address is
5288772SDan.Mick@Sun.COM 	 * valid other than that it is within the maximum offset.  The
52911245SZhijun.Fu@Sun.COM 	 * put functions return void and the get functions return -1 on error.
5300Sstevel@tonic-gate 	 */
5318772SDan.Mick@Sun.COM 
53211245SZhijun.Fu@Sun.COM 	if (io_access)
53311245SZhijun.Fu@Sun.COM 		max_offset = 0xFF;
53411245SZhijun.Fu@Sun.COM 	else
53511245SZhijun.Fu@Sun.COM 		max_offset = 0xFFF;
53611245SZhijun.Fu@Sun.COM 	if (prg->offset + size - 1 > max_offset) {
5378772SDan.Mick@Sun.COM 		prg->status = PCITOOL_INVALID_ADDRESS;
5388772SDan.Mick@Sun.COM 		return (ENOTSUP);
5398772SDan.Mick@Sun.COM 	}
5408772SDan.Mick@Sun.COM 
5410Sstevel@tonic-gate 	prg->status = PCITOOL_SUCCESS;
5420Sstevel@tonic-gate 
54311245SZhijun.Fu@Sun.COM 	req.rcdip = NULL;
54411245SZhijun.Fu@Sun.COM 	req.bdf = PCI_GETBDF(prg->bus_no, prg->dev_no, prg->func_no);
54511245SZhijun.Fu@Sun.COM 	req.offset = prg->offset;
54611245SZhijun.Fu@Sun.COM 	req.size = size;
54711245SZhijun.Fu@Sun.COM 	req.write = write_flag;
54811245SZhijun.Fu@Sun.COM 	req.ioacc = io_access;
5490Sstevel@tonic-gate 	if (write_flag) {
5500Sstevel@tonic-gate 		if (big_endian) {
5510Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
5520Sstevel@tonic-gate 		} else {
5530Sstevel@tonic-gate 			local_data = prg->data;
5540Sstevel@tonic-gate 		}
55511245SZhijun.Fu@Sun.COM 		VAL64(&req) = local_data;
55611245SZhijun.Fu@Sun.COM 		pci_cfgacc_acc(&req);
5570Sstevel@tonic-gate 	} else {
55811245SZhijun.Fu@Sun.COM 		pci_cfgacc_acc(&req);
559*12212SZhijun.Fu@Sun.COM 		switch (size) {
560*12212SZhijun.Fu@Sun.COM 		case 1:
561*12212SZhijun.Fu@Sun.COM 			local_data = VAL8(&req);
562*12212SZhijun.Fu@Sun.COM 			break;
563*12212SZhijun.Fu@Sun.COM 		case 2:
564*12212SZhijun.Fu@Sun.COM 			local_data = VAL16(&req);
565*12212SZhijun.Fu@Sun.COM 			break;
566*12212SZhijun.Fu@Sun.COM 		case 4:
567*12212SZhijun.Fu@Sun.COM 			local_data = VAL32(&req);
568*12212SZhijun.Fu@Sun.COM 			break;
569*12212SZhijun.Fu@Sun.COM 		case 8:
570*12212SZhijun.Fu@Sun.COM 			local_data = VAL64(&req);
571*12212SZhijun.Fu@Sun.COM 			break;
572*12212SZhijun.Fu@Sun.COM 		}
57311245SZhijun.Fu@Sun.COM 		if (big_endian) {
57411245SZhijun.Fu@Sun.COM 			prg->data =
57511245SZhijun.Fu@Sun.COM 			    pcitool_swap_endian(local_data, size);
57611245SZhijun.Fu@Sun.COM 		} else {
57711245SZhijun.Fu@Sun.COM 			prg->data = local_data;
5780Sstevel@tonic-gate 		}
5790Sstevel@tonic-gate 	}
58011245SZhijun.Fu@Sun.COM 	/*
58111245SZhijun.Fu@Sun.COM 	 * Check if legacy IO config access is used, in which case
58211245SZhijun.Fu@Sun.COM 	 * only first 256 bytes are valid.
58311245SZhijun.Fu@Sun.COM 	 */
58411245SZhijun.Fu@Sun.COM 	if (req.ioacc && (prg->offset + size - 1 > 0xFF)) {
58511245SZhijun.Fu@Sun.COM 		prg->status = PCITOOL_INVALID_ADDRESS;
58611245SZhijun.Fu@Sun.COM 		return (ENOTSUP);
58711245SZhijun.Fu@Sun.COM 	}
58811245SZhijun.Fu@Sun.COM 
58911245SZhijun.Fu@Sun.COM 	/* Set phys_addr only if MMIO is used */
59011245SZhijun.Fu@Sun.COM 	prg->phys_addr = 0;
59111245SZhijun.Fu@Sun.COM 	if (!req.ioacc && mcfg_mem_base != 0) {
59211245SZhijun.Fu@Sun.COM 		prg->phys_addr = mcfg_mem_base + prg->offset +
59311245SZhijun.Fu@Sun.COM 		    ((prg->bus_no << PCIEX_REG_BUS_SHIFT) |
59411245SZhijun.Fu@Sun.COM 		    (prg->dev_no << PCIEX_REG_DEV_SHIFT) |
59511245SZhijun.Fu@Sun.COM 		    (prg->func_no << PCIEX_REG_FUNC_SHIFT));
59611245SZhijun.Fu@Sun.COM 	}
59711245SZhijun.Fu@Sun.COM 
5980Sstevel@tonic-gate 	return (rval);
5990Sstevel@tonic-gate }
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate static int
60211245SZhijun.Fu@Sun.COM pcitool_io_access(pcitool_reg_t *prg, boolean_t write_flag)
6030Sstevel@tonic-gate {
6040Sstevel@tonic-gate 	int port = (int)prg->phys_addr;
6050Sstevel@tonic-gate 	size_t size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
6060Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
6070Sstevel@tonic-gate 	int rval = SUCCESS;
6080Sstevel@tonic-gate 	on_trap_data_t otd;
6090Sstevel@tonic-gate 	uint64_t local_data;
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	/*
6130Sstevel@tonic-gate 	 * on_trap works like setjmp.
6140Sstevel@tonic-gate 	 *
6150Sstevel@tonic-gate 	 * A non-zero return here means on_trap has returned from an error.
6160Sstevel@tonic-gate 	 *
6170Sstevel@tonic-gate 	 * A zero return here means that on_trap has just returned from setup.
6180Sstevel@tonic-gate 	 */
6190Sstevel@tonic-gate 	if (on_trap(&otd, OT_DATA_ACCESS)) {
6200Sstevel@tonic-gate 		no_trap();
6210Sstevel@tonic-gate 		if (pcitool_debug)
6220Sstevel@tonic-gate 			prom_printf(
6234397Sschwartz 			    "pcitool_io_access: on_trap caught an error...\n");
6240Sstevel@tonic-gate 		prg->status = PCITOOL_INVALID_ADDRESS;
6250Sstevel@tonic-gate 		return (EFAULT);
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	if (write_flag) {
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 		if (big_endian) {
6310Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
6320Sstevel@tonic-gate 		} else {
6330Sstevel@tonic-gate 			local_data = prg->data;
6340Sstevel@tonic-gate 		}
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 		if (pcitool_debug)
6370Sstevel@tonic-gate 			prom_printf("Writing %ld byte(s) to port 0x%x\n",
6380Sstevel@tonic-gate 			    size, port);
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 		switch (size) {
6410Sstevel@tonic-gate 		case 1:
6420Sstevel@tonic-gate 			outb(port, (uint8_t)local_data);
6430Sstevel@tonic-gate 			break;
6440Sstevel@tonic-gate 		case 2:
6450Sstevel@tonic-gate 			outw(port, (uint16_t)local_data);
6460Sstevel@tonic-gate 			break;
6470Sstevel@tonic-gate 		case 4:
6480Sstevel@tonic-gate 			outl(port, (uint32_t)local_data);
6490Sstevel@tonic-gate 			break;
6500Sstevel@tonic-gate 		default:
6510Sstevel@tonic-gate 			rval = ENOTSUP;
6520Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
6530Sstevel@tonic-gate 			break;
6540Sstevel@tonic-gate 		}
6550Sstevel@tonic-gate 	} else {
6560Sstevel@tonic-gate 		if (pcitool_debug)
6570Sstevel@tonic-gate 			prom_printf("Reading %ld byte(s) from port 0x%x\n",
6580Sstevel@tonic-gate 			    size, port);
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 		switch (size) {
6610Sstevel@tonic-gate 		case 1:
6620Sstevel@tonic-gate 			local_data = inb(port);
6630Sstevel@tonic-gate 			break;
6640Sstevel@tonic-gate 		case 2:
6650Sstevel@tonic-gate 			local_data = inw(port);
6660Sstevel@tonic-gate 			break;
6670Sstevel@tonic-gate 		case 4:
6680Sstevel@tonic-gate 			local_data = inl(port);
6690Sstevel@tonic-gate 			break;
6700Sstevel@tonic-gate 		default:
6710Sstevel@tonic-gate 			rval = ENOTSUP;
6720Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
6730Sstevel@tonic-gate 			break;
6740Sstevel@tonic-gate 		}
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 		if (rval == SUCCESS) {
6770Sstevel@tonic-gate 			if (big_endian) {
6780Sstevel@tonic-gate 				prg->data =
6790Sstevel@tonic-gate 				    pcitool_swap_endian(local_data, size);
6800Sstevel@tonic-gate 			} else {
6810Sstevel@tonic-gate 				prg->data = local_data;
6820Sstevel@tonic-gate 			}
6830Sstevel@tonic-gate 		}
6840Sstevel@tonic-gate 	}
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	no_trap();
6870Sstevel@tonic-gate 	return (rval);
6880Sstevel@tonic-gate }
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate static int
69111245SZhijun.Fu@Sun.COM pcitool_mem_access(pcitool_reg_t *prg, uint64_t virt_addr, boolean_t write_flag)
6920Sstevel@tonic-gate {
6930Sstevel@tonic-gate 	size_t size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr);
6940Sstevel@tonic-gate 	boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr);
6950Sstevel@tonic-gate 	int rval = DDI_SUCCESS;
6960Sstevel@tonic-gate 	on_trap_data_t otd;
6970Sstevel@tonic-gate 	uint64_t local_data;
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	/*
7000Sstevel@tonic-gate 	 * on_trap works like setjmp.
7010Sstevel@tonic-gate 	 *
7020Sstevel@tonic-gate 	 * A non-zero return here means on_trap has returned from an error.
7030Sstevel@tonic-gate 	 *
7040Sstevel@tonic-gate 	 * A zero return here means that on_trap has just returned from setup.
7050Sstevel@tonic-gate 	 */
7060Sstevel@tonic-gate 	if (on_trap(&otd, OT_DATA_ACCESS)) {
7070Sstevel@tonic-gate 		no_trap();
7080Sstevel@tonic-gate 		if (pcitool_debug)
7090Sstevel@tonic-gate 			prom_printf(
7100Sstevel@tonic-gate 			    "pcitool_mem_access: on_trap caught an error...\n");
7110Sstevel@tonic-gate 		prg->status = PCITOOL_INVALID_ADDRESS;
7120Sstevel@tonic-gate 		return (EFAULT);
7130Sstevel@tonic-gate 	}
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	if (write_flag) {
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 		if (big_endian) {
7180Sstevel@tonic-gate 			local_data = pcitool_swap_endian(prg->data, size);
7190Sstevel@tonic-gate 		} else {
7200Sstevel@tonic-gate 			local_data = prg->data;
7210Sstevel@tonic-gate 		}
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 		switch (size) {
7240Sstevel@tonic-gate 		case 1:
7250Sstevel@tonic-gate 			*((uint8_t *)(uintptr_t)virt_addr) = local_data;
7260Sstevel@tonic-gate 			break;
7270Sstevel@tonic-gate 		case 2:
7280Sstevel@tonic-gate 			*((uint16_t *)(uintptr_t)virt_addr) = local_data;
7290Sstevel@tonic-gate 			break;
7300Sstevel@tonic-gate 		case 4:
7310Sstevel@tonic-gate 			*((uint32_t *)(uintptr_t)virt_addr) = local_data;
7320Sstevel@tonic-gate 			break;
7330Sstevel@tonic-gate 		case 8:
7340Sstevel@tonic-gate 			*((uint64_t *)(uintptr_t)virt_addr) = local_data;
7350Sstevel@tonic-gate 			break;
7360Sstevel@tonic-gate 		default:
7370Sstevel@tonic-gate 			rval = ENOTSUP;
7380Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
7390Sstevel@tonic-gate 			break;
7400Sstevel@tonic-gate 		}
7410Sstevel@tonic-gate 	} else {
7420Sstevel@tonic-gate 		switch (size) {
7430Sstevel@tonic-gate 		case 1:
7440Sstevel@tonic-gate 			local_data = *((uint8_t *)(uintptr_t)virt_addr);
7450Sstevel@tonic-gate 			break;
7460Sstevel@tonic-gate 		case 2:
7470Sstevel@tonic-gate 			local_data = *((uint16_t *)(uintptr_t)virt_addr);
7480Sstevel@tonic-gate 			break;
7490Sstevel@tonic-gate 		case 4:
7500Sstevel@tonic-gate 			local_data = *((uint32_t *)(uintptr_t)virt_addr);
7510Sstevel@tonic-gate 			break;
7520Sstevel@tonic-gate 		case 8:
7530Sstevel@tonic-gate 			local_data = *((uint64_t *)(uintptr_t)virt_addr);
7540Sstevel@tonic-gate 			break;
7550Sstevel@tonic-gate 		default:
7560Sstevel@tonic-gate 			rval = ENOTSUP;
7570Sstevel@tonic-gate 			prg->status = PCITOOL_INVALID_SIZE;
7580Sstevel@tonic-gate 			break;
7590Sstevel@tonic-gate 		}
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 		if (rval == SUCCESS) {
7620Sstevel@tonic-gate 			if (big_endian) {
7630Sstevel@tonic-gate 				prg->data =
7640Sstevel@tonic-gate 				    pcitool_swap_endian(local_data, size);
7650Sstevel@tonic-gate 			} else {
7660Sstevel@tonic-gate 				prg->data = local_data;
7670Sstevel@tonic-gate 			}
7680Sstevel@tonic-gate 		}
7690Sstevel@tonic-gate 	}
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	no_trap();
7720Sstevel@tonic-gate 	return (rval);
7730Sstevel@tonic-gate }
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate /*
7760Sstevel@tonic-gate  * Map up to 2 pages which contain the address we want to access.
7770Sstevel@tonic-gate  *
7780Sstevel@tonic-gate  * Mapping should span no more than 8 bytes.  With X86 it is possible for an
7790Sstevel@tonic-gate  * 8 byte value to start on a 4 byte boundary, so it can cross a page boundary.
7800Sstevel@tonic-gate  * We'll never have to map more than two pages.
7810Sstevel@tonic-gate  */
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate static uint64_t
7840Sstevel@tonic-gate pcitool_map(uint64_t phys_addr, size_t size, size_t *num_pages)
7850Sstevel@tonic-gate {
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	uint64_t page_base = phys_addr & ~MMU_PAGEOFFSET;
7880Sstevel@tonic-gate 	uint64_t offset = phys_addr & MMU_PAGEOFFSET;
7890Sstevel@tonic-gate 	void *virt_base;
7900Sstevel@tonic-gate 	uint64_t returned_addr;
7913446Smrj 	pfn_t pfn;
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	if (pcitool_debug)
7940Sstevel@tonic-gate 		prom_printf("pcitool_map: Called with PA:0x%p\n",
7957632SNick.Todd@Sun.COM 		    (void *)(uintptr_t)phys_addr);
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	*num_pages = 1;
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	/* Desired mapping would span more than two pages. */
8000Sstevel@tonic-gate 	if ((offset + size) > (MMU_PAGESIZE * 2)) {
8010Sstevel@tonic-gate 		if (pcitool_debug)
8020Sstevel@tonic-gate 			prom_printf("boundary violation: "
803777Sschwartz 			    "offset:0x%" PRIx64 ", size:%ld, pagesize:0x%lx\n",
804777Sschwartz 			    offset, (uintptr_t)size, (uintptr_t)MMU_PAGESIZE);
8050Sstevel@tonic-gate 		return (NULL);
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	} else if ((offset + size) > MMU_PAGESIZE) {
8080Sstevel@tonic-gate 		(*num_pages)++;
8090Sstevel@tonic-gate 	}
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	/* Get page(s) of virtual space. */
8120Sstevel@tonic-gate 	virt_base = vmem_alloc(heap_arena, ptob(*num_pages), VM_NOSLEEP);
8130Sstevel@tonic-gate 	if (virt_base == NULL) {
8140Sstevel@tonic-gate 		if (pcitool_debug)
8150Sstevel@tonic-gate 			prom_printf("Couldn't get virtual base address.\n");
8160Sstevel@tonic-gate 		return (NULL);
8170Sstevel@tonic-gate 	}
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	if (pcitool_debug)
8200Sstevel@tonic-gate 		prom_printf("Got base virtual address:0x%p\n", virt_base);
8210Sstevel@tonic-gate 
8225084Sjohnlev #ifdef __xpv
8235084Sjohnlev 	/*
8245084Sjohnlev 	 * We should only get here if we are dom0.
8255084Sjohnlev 	 * We're using a real device so we need to translate the MA to a PFN.
8265084Sjohnlev 	 */
8275084Sjohnlev 	ASSERT(DOMAIN_IS_INITDOMAIN(xen_info));
8285084Sjohnlev 	pfn = xen_assign_pfn(mmu_btop(page_base));
8295084Sjohnlev #else
8303446Smrj 	pfn = btop(page_base);
8315084Sjohnlev #endif
8323446Smrj 
8330Sstevel@tonic-gate 	/* Now map the allocated virtual space to the physical address. */
8343446Smrj 	hat_devload(kas.a_hat, virt_base, mmu_ptob(*num_pages), pfn,
8353446Smrj 	    PROT_READ | PROT_WRITE | HAT_STRICTORDER,
8360Sstevel@tonic-gate 	    HAT_LOAD_LOCK);
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 	returned_addr = ((uintptr_t)(virt_base)) + offset;
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 	if (pcitool_debug)
8410Sstevel@tonic-gate 		prom_printf("pcitool_map: returning VA:0x%p\n",
8420Sstevel@tonic-gate 		    (void *)(uintptr_t)returned_addr);
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	return (returned_addr);
8450Sstevel@tonic-gate }
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate /* Unmap the mapped page(s). */
8480Sstevel@tonic-gate static void
8490Sstevel@tonic-gate pcitool_unmap(uint64_t virt_addr, size_t num_pages)
8500Sstevel@tonic-gate {
8510Sstevel@tonic-gate 	void *base_virt_addr = (void *)(uintptr_t)(virt_addr & ~MMU_PAGEOFFSET);
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	hat_unload(kas.a_hat, base_virt_addr, ptob(num_pages),
8540Sstevel@tonic-gate 	    HAT_UNLOAD_UNLOCK);
8550Sstevel@tonic-gate 	vmem_free(heap_arena, base_virt_addr, ptob(num_pages));
8560Sstevel@tonic-gate }
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate /* Perform register accesses on PCI leaf devices. */
86011245SZhijun.Fu@Sun.COM /*ARGSUSED*/
8610Sstevel@tonic-gate int
862777Sschwartz pcitool_dev_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode)
8630Sstevel@tonic-gate {
8640Sstevel@tonic-gate 	boolean_t	write_flag = B_FALSE;
86511245SZhijun.Fu@Sun.COM 	boolean_t	io_access = B_TRUE;
8660Sstevel@tonic-gate 	int		rval = 0;
8670Sstevel@tonic-gate 	pcitool_reg_t	prg;
8680Sstevel@tonic-gate 	uint8_t		size;
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	uint64_t	base_addr;
8710Sstevel@tonic-gate 	uint64_t	virt_addr;
8720Sstevel@tonic-gate 	size_t		num_virt_pages;
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	switch (cmd) {
8750Sstevel@tonic-gate 	case (PCITOOL_DEVICE_SET_REG):
8760Sstevel@tonic-gate 		write_flag = B_TRUE;
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	/*FALLTHRU*/
8790Sstevel@tonic-gate 	case (PCITOOL_DEVICE_GET_REG):
8800Sstevel@tonic-gate 		if (pcitool_debug)
8810Sstevel@tonic-gate 			prom_printf("pci_dev_reg_ops set/get reg\n");
8820Sstevel@tonic-gate 		if (ddi_copyin(arg, &prg, sizeof (pcitool_reg_t), mode) !=
8830Sstevel@tonic-gate 		    DDI_SUCCESS) {
8840Sstevel@tonic-gate 			if (pcitool_debug)
8850Sstevel@tonic-gate 				prom_printf("Error reading arguments\n");
8860Sstevel@tonic-gate 			return (EFAULT);
8870Sstevel@tonic-gate 		}
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 		if (prg.barnum >= (sizeof (pci_bars) / sizeof (pci_bars[0]))) {
8900Sstevel@tonic-gate 			prg.status = PCITOOL_OUT_OF_RANGE;
8910Sstevel@tonic-gate 			rval = EINVAL;
8920Sstevel@tonic-gate 			goto done_reg;
8930Sstevel@tonic-gate 		}
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 		if (pcitool_debug)
8960Sstevel@tonic-gate 			prom_printf("raw bus:0x%x, dev:0x%x, func:0x%x\n",
8970Sstevel@tonic-gate 			    prg.bus_no, prg.dev_no, prg.func_no);
8980Sstevel@tonic-gate 		/* Validate address arguments of bus / dev / func */
8990Sstevel@tonic-gate 		if (((prg.bus_no &
9000Sstevel@tonic-gate 		    (PCI_REG_BUS_M >> PCI_REG_BUS_SHIFT)) !=
9010Sstevel@tonic-gate 		    prg.bus_no) ||
9020Sstevel@tonic-gate 		    ((prg.dev_no &
9030Sstevel@tonic-gate 		    (PCI_REG_DEV_M >> PCI_REG_DEV_SHIFT)) !=
9040Sstevel@tonic-gate 		    prg.dev_no) ||
9050Sstevel@tonic-gate 		    ((prg.func_no &
9060Sstevel@tonic-gate 		    (PCI_REG_FUNC_M >> PCI_REG_FUNC_SHIFT)) !=
9070Sstevel@tonic-gate 		    prg.func_no)) {
9080Sstevel@tonic-gate 			prg.status = PCITOOL_INVALID_ADDRESS;
9090Sstevel@tonic-gate 			rval = EINVAL;
9100Sstevel@tonic-gate 			goto done_reg;
9110Sstevel@tonic-gate 		}
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 		size = PCITOOL_ACC_ATTR_SIZE(prg.acc_attr);
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 		/* Proper config space desired. */
9160Sstevel@tonic-gate 		if (prg.barnum == 0) {
9170Sstevel@tonic-gate 
918777Sschwartz 			if (pcitool_debug)
919777Sschwartz 				prom_printf(
920777Sschwartz 				    "config access: offset:0x%" PRIx64 ", "
921777Sschwartz 				    "phys_addr:0x%" PRIx64 "\n",
922777Sschwartz 				    prg.offset, prg.phys_addr);
923777Sschwartz 
924777Sschwartz 			if (prg.offset >= max_cfg_size) {
9250Sstevel@tonic-gate 				prg.status = PCITOOL_OUT_OF_RANGE;
9260Sstevel@tonic-gate 				rval = EINVAL;
9270Sstevel@tonic-gate 				goto done_reg;
9280Sstevel@tonic-gate 			}
92911245SZhijun.Fu@Sun.COM 			if (max_cfg_size == PCIE_CONF_HDR_SIZE)
93011245SZhijun.Fu@Sun.COM 				io_access = B_FALSE;
9310Sstevel@tonic-gate 
93211245SZhijun.Fu@Sun.COM 			rval = pcitool_cfg_access(&prg, write_flag, io_access);
9330Sstevel@tonic-gate 			if (pcitool_debug)
9340Sstevel@tonic-gate 				prom_printf(
9350Sstevel@tonic-gate 				    "config access: data:0x%" PRIx64 "\n",
9360Sstevel@tonic-gate 				    prg.data);
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 		/* IO/ MEM/ MEM64 space. */
9390Sstevel@tonic-gate 		} else {
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 			pcitool_reg_t	prg2;
9420Sstevel@tonic-gate 			bcopy(&prg, &prg2, sizeof (pcitool_reg_t));
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 			/*
9450Sstevel@tonic-gate 			 * Translate BAR number into offset of the BAR in
9460Sstevel@tonic-gate 			 * the device's config space.
9470Sstevel@tonic-gate 			 */
9480Sstevel@tonic-gate 			prg2.offset = pci_bars[prg2.barnum];
9490Sstevel@tonic-gate 			prg2.acc_attr =
9500Sstevel@tonic-gate 			    PCITOOL_ACC_ATTR_SIZE_4 | PCITOOL_ACC_ATTR_ENDN_LTL;
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 			if (pcitool_debug)
9530Sstevel@tonic-gate 				prom_printf(
9540Sstevel@tonic-gate 				    "barnum:%d, bar_offset:0x%" PRIx64 "\n",
9550Sstevel@tonic-gate 				    prg2.barnum, prg2.offset);
9560Sstevel@tonic-gate 			/*
9570Sstevel@tonic-gate 			 * Get Bus Address Register (BAR) from config space.
9580Sstevel@tonic-gate 			 * prg2.offset is the offset into config space of the
9590Sstevel@tonic-gate 			 * BAR desired.  prg.status is modified on error.
9600Sstevel@tonic-gate 			 */
96111245SZhijun.Fu@Sun.COM 			rval = pcitool_cfg_access(&prg2, B_FALSE, B_TRUE);
9620Sstevel@tonic-gate 			if (rval != SUCCESS) {
9630Sstevel@tonic-gate 				if (pcitool_debug)
9640Sstevel@tonic-gate 					prom_printf("BAR access failed\n");
9650Sstevel@tonic-gate 				prg.status = prg2.status;
9660Sstevel@tonic-gate 				goto done_reg;
9670Sstevel@tonic-gate 			}
9680Sstevel@tonic-gate 			/*
9690Sstevel@tonic-gate 			 * Reference proper PCI space based on the BAR.
9700Sstevel@tonic-gate 			 * If 64 bit MEM space, need to load other half of the
9710Sstevel@tonic-gate 			 * BAR first.
9720Sstevel@tonic-gate 			 */
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 			if (pcitool_debug)
9750Sstevel@tonic-gate 				prom_printf("bar returned is 0x%" PRIx64 "\n",
9760Sstevel@tonic-gate 				    prg2.data);
9770Sstevel@tonic-gate 			if (!prg2.data) {
9780Sstevel@tonic-gate 				if (pcitool_debug)
9790Sstevel@tonic-gate 					prom_printf("BAR data == 0\n");
9800Sstevel@tonic-gate 				rval = EINVAL;
9810Sstevel@tonic-gate 				prg.status = PCITOOL_INVALID_ADDRESS;
9820Sstevel@tonic-gate 				goto done_reg;
9830Sstevel@tonic-gate 			}
9840Sstevel@tonic-gate 			if (prg2.data == 0xffffffff) {
9850Sstevel@tonic-gate 				if (pcitool_debug)
9860Sstevel@tonic-gate 					prom_printf("BAR data == -1\n");
9870Sstevel@tonic-gate 				rval = EINVAL;
9880Sstevel@tonic-gate 				prg.status = PCITOOL_INVALID_ADDRESS;
9890Sstevel@tonic-gate 				goto done_reg;
9900Sstevel@tonic-gate 			}
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate 			/*
9930Sstevel@tonic-gate 			 * BAR has bits saying this space is IO space, unless
9940Sstevel@tonic-gate 			 * this is the ROM address register.
9950Sstevel@tonic-gate 			 */
9960Sstevel@tonic-gate 			if (((PCI_BASE_SPACE_M & prg2.data) ==
9970Sstevel@tonic-gate 			    PCI_BASE_SPACE_IO) &&
9980Sstevel@tonic-gate 			    (prg2.offset != PCI_CONF_ROM)) {
9990Sstevel@tonic-gate 				if (pcitool_debug)
10000Sstevel@tonic-gate 					prom_printf("IO space\n");
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 				prg2.data &= PCI_BASE_IO_ADDR_M;
10030Sstevel@tonic-gate 				prg.phys_addr = prg2.data + prg.offset;
10040Sstevel@tonic-gate 
100511245SZhijun.Fu@Sun.COM 				rval = pcitool_io_access(&prg, write_flag);
10060Sstevel@tonic-gate 				if ((rval != SUCCESS) && (pcitool_debug))
10070Sstevel@tonic-gate 					prom_printf("IO access failed\n");
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 				goto done_reg;
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 			/*
10130Sstevel@tonic-gate 			 * BAR has bits saying this space is 64 bit memory
10140Sstevel@tonic-gate 			 * space, unless this is the ROM address register.
10150Sstevel@tonic-gate 			 *
10160Sstevel@tonic-gate 			 * The 64 bit address stored in two BAR cells is not
10170Sstevel@tonic-gate 			 * necessarily aligned on an 8-byte boundary.
10180Sstevel@tonic-gate 			 * Need to keep the first 4 bytes read,
10190Sstevel@tonic-gate 			 * and do a separate read of the high 4 bytes.
10200Sstevel@tonic-gate 			 */
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 			} else if ((PCI_BASE_TYPE_ALL & prg2.data) &&
10230Sstevel@tonic-gate 			    (prg2.offset != PCI_CONF_ROM)) {
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 				uint32_t low_bytes =
10260Sstevel@tonic-gate 				    (uint32_t)(prg2.data & ~PCI_BASE_TYPE_ALL);
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 				/*
10290Sstevel@tonic-gate 				 * Don't try to read the next 4 bytes
10300Sstevel@tonic-gate 				 * past the end of BARs.
10310Sstevel@tonic-gate 				 */
10320Sstevel@tonic-gate 				if (prg2.offset >= PCI_CONF_BASE5) {
10330Sstevel@tonic-gate 					prg.status = PCITOOL_OUT_OF_RANGE;
10340Sstevel@tonic-gate 					rval = EIO;
10350Sstevel@tonic-gate 					goto done_reg;
10360Sstevel@tonic-gate 				}
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 				/*
10390Sstevel@tonic-gate 				 * Access device.
10400Sstevel@tonic-gate 				 * prg2.status is modified on error.
10410Sstevel@tonic-gate 				 */
10420Sstevel@tonic-gate 				prg2.offset += 4;
104311245SZhijun.Fu@Sun.COM 				rval = pcitool_cfg_access(&prg2,
104411245SZhijun.Fu@Sun.COM 				    B_FALSE, B_TRUE);
10450Sstevel@tonic-gate 				if (rval != SUCCESS) {
10460Sstevel@tonic-gate 					prg.status = prg2.status;
10470Sstevel@tonic-gate 					goto done_reg;
10480Sstevel@tonic-gate 				}
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 				if (prg2.data == 0xffffffff) {
10510Sstevel@tonic-gate 					prg.status = PCITOOL_INVALID_ADDRESS;
10520Sstevel@tonic-gate 					prg.status = EFAULT;
10530Sstevel@tonic-gate 					goto done_reg;
10540Sstevel@tonic-gate 				}
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 				prg2.data = (prg2.data << 32) + low_bytes;
10570Sstevel@tonic-gate 				if (pcitool_debug)
10580Sstevel@tonic-gate 					prom_printf(
10590Sstevel@tonic-gate 					    "64 bit mem space.  "
10600Sstevel@tonic-gate 					    "64-bit bar is 0x%" PRIx64 "\n",
10610Sstevel@tonic-gate 					    prg2.data);
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 			/* Mem32 space, including ROM */
10640Sstevel@tonic-gate 			} else {
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 				if (prg2.offset == PCI_CONF_ROM) {
10670Sstevel@tonic-gate 					if (pcitool_debug)
10680Sstevel@tonic-gate 						prom_printf(
10690Sstevel@tonic-gate 						    "Additional ROM "
10700Sstevel@tonic-gate 						    "checking\n");
10710Sstevel@tonic-gate 					/* Can't write to ROM */
10720Sstevel@tonic-gate 					if (write_flag) {
10730Sstevel@tonic-gate 						prg.status = PCITOOL_ROM_WRITE;
10740Sstevel@tonic-gate 						rval = EIO;
10750Sstevel@tonic-gate 						goto done_reg;
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 					/* ROM disabled for reading */
10780Sstevel@tonic-gate 					} else if (!(prg2.data & 0x00000001)) {
10790Sstevel@tonic-gate 						prg.status =
10800Sstevel@tonic-gate 						    PCITOOL_ROM_DISABLED;
10810Sstevel@tonic-gate 						rval = EIO;
10820Sstevel@tonic-gate 						goto done_reg;
10830Sstevel@tonic-gate 					}
10840Sstevel@tonic-gate 				}
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 				if (pcitool_debug)
10870Sstevel@tonic-gate 					prom_printf("32 bit mem space\n");
10880Sstevel@tonic-gate 			}
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 			/* Common code for all IO/MEM range spaces. */
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 			base_addr = prg2.data;
10930Sstevel@tonic-gate 			if (pcitool_debug)
10940Sstevel@tonic-gate 				prom_printf(
10950Sstevel@tonic-gate 				    "addr portion of bar is 0x%" PRIx64 ", "
10960Sstevel@tonic-gate 				    "base=0x%" PRIx64 ", "
10970Sstevel@tonic-gate 				    "offset:0x%" PRIx64 "\n",
10980Sstevel@tonic-gate 				    prg2.data, base_addr, prg.offset);
10990Sstevel@tonic-gate 			/*
11000Sstevel@tonic-gate 			 * Use offset provided by caller to index into
11010Sstevel@tonic-gate 			 * desired space, then access.
11020Sstevel@tonic-gate 			 * Note that prg.status is modified on error.
11030Sstevel@tonic-gate 			 */
11040Sstevel@tonic-gate 			prg.phys_addr = base_addr + prg.offset;
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 			virt_addr = pcitool_map(prg.phys_addr, size,
11070Sstevel@tonic-gate 			    &num_virt_pages);
11080Sstevel@tonic-gate 			if (virt_addr == NULL) {
11090Sstevel@tonic-gate 				prg.status = PCITOOL_IO_ERROR;
11100Sstevel@tonic-gate 				rval = EIO;
11110Sstevel@tonic-gate 				goto done_reg;
11120Sstevel@tonic-gate 			}
11130Sstevel@tonic-gate 
111411245SZhijun.Fu@Sun.COM 			rval = pcitool_mem_access(&prg, virt_addr, write_flag);
11150Sstevel@tonic-gate 			pcitool_unmap(virt_addr, num_virt_pages);
11160Sstevel@tonic-gate 		}
11170Sstevel@tonic-gate done_reg:
11184397Sschwartz 		prg.drvr_version = PCITOOL_VERSION;
11190Sstevel@tonic-gate 		if (ddi_copyout(&prg, arg, sizeof (pcitool_reg_t), mode) !=
11200Sstevel@tonic-gate 		    DDI_SUCCESS) {
11210Sstevel@tonic-gate 			if (pcitool_debug)
11220Sstevel@tonic-gate 				prom_printf("Error returning arguments.\n");
11230Sstevel@tonic-gate 			rval = EFAULT;
11240Sstevel@tonic-gate 		}
11250Sstevel@tonic-gate 		break;
11260Sstevel@tonic-gate 	default:
11270Sstevel@tonic-gate 		rval = ENOTTY;
11280Sstevel@tonic-gate 		break;
11290Sstevel@tonic-gate 	}
11300Sstevel@tonic-gate 	return (rval);
11310Sstevel@tonic-gate }
1132