xref: /onnv-gate/usr/src/uts/common/io/cpuid_drv.c (revision 7656:2621e50fdf4a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7656SSherry.Moore@Sun.COM  * Common Development and Distribution License (the "License").
6*7656SSherry.Moore@Sun.COM  * 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*7656SSherry.Moore@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/file.h>
290Sstevel@tonic-gate #include <sys/errno.h>
300Sstevel@tonic-gate #include <sys/open.h>
310Sstevel@tonic-gate #include <sys/cred.h>
320Sstevel@tonic-gate #include <sys/conf.h>
330Sstevel@tonic-gate #include <sys/stat.h>
340Sstevel@tonic-gate #include <sys/processor.h>
350Sstevel@tonic-gate #include <sys/cpuvar.h>
360Sstevel@tonic-gate #include <sys/kmem.h>
370Sstevel@tonic-gate #include <sys/modctl.h>
380Sstevel@tonic-gate #include <sys/ddi.h>
390Sstevel@tonic-gate #include <sys/sunddi.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <sys/auxv.h>
420Sstevel@tonic-gate #include <sys/cpuid_drv.h>
430Sstevel@tonic-gate #include <sys/systeminfo.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #if defined(__x86)
460Sstevel@tonic-gate #include <sys/x86_archext.h>
470Sstevel@tonic-gate #endif
480Sstevel@tonic-gate 
490Sstevel@tonic-gate static dev_info_t *cpuid_devi;
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*ARGSUSED*/
520Sstevel@tonic-gate static int
530Sstevel@tonic-gate cpuid_getinfo(dev_info_t *devi, ddi_info_cmd_t cmd, void *arg, void **result)
540Sstevel@tonic-gate {
550Sstevel@tonic-gate 	switch (cmd) {
560Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
570Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
580Sstevel@tonic-gate 		break;
590Sstevel@tonic-gate 	default:
600Sstevel@tonic-gate 		return (DDI_FAILURE);
610Sstevel@tonic-gate 	}
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	switch (getminor((dev_t)arg)) {
640Sstevel@tonic-gate 	case CPUID_SELF_CPUID_MINOR:
650Sstevel@tonic-gate 		break;
660Sstevel@tonic-gate 	default:
670Sstevel@tonic-gate 		return (DDI_FAILURE);
680Sstevel@tonic-gate 	}
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	if (cmd == DDI_INFO_DEVT2INSTANCE)
710Sstevel@tonic-gate 		*result = 0;
720Sstevel@tonic-gate 	else
730Sstevel@tonic-gate 		*result = cpuid_devi;
740Sstevel@tonic-gate 	return (DDI_SUCCESS);
750Sstevel@tonic-gate }
760Sstevel@tonic-gate 
770Sstevel@tonic-gate static int
780Sstevel@tonic-gate cpuid_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
790Sstevel@tonic-gate {
800Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
810Sstevel@tonic-gate 		return (DDI_FAILURE);
820Sstevel@tonic-gate 	cpuid_devi = devi;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	return (ddi_create_minor_node(devi, CPUID_DRIVER_SELF_NODE, S_IFCHR,
850Sstevel@tonic-gate 	    CPUID_SELF_CPUID_MINOR, DDI_PSEUDO, 0));
860Sstevel@tonic-gate }
870Sstevel@tonic-gate 
880Sstevel@tonic-gate static int
890Sstevel@tonic-gate cpuid_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
900Sstevel@tonic-gate {
910Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
920Sstevel@tonic-gate 		return (DDI_FAILURE);
930Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
940Sstevel@tonic-gate 	cpuid_devi = NULL;
950Sstevel@tonic-gate 	return (DDI_SUCCESS);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /*ARGSUSED1*/
990Sstevel@tonic-gate static int
1000Sstevel@tonic-gate cpuid_open(dev_t *dev, int flag, int otyp, cred_t *cr)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	return (getminor(*dev) == CPUID_SELF_CPUID_MINOR ? 0 : ENXIO);
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate #if defined(_HAVE_CPUID_INSN)
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate /*ARGSUSED*/
1080Sstevel@tonic-gate static int
1090Sstevel@tonic-gate cpuid_read(dev_t dev, uio_t *uio, cred_t *cr)
1100Sstevel@tonic-gate {
1111228Sandrei 	struct cpuid_regs crs;
1120Sstevel@tonic-gate 	int error = 0;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	if ((x86_feature & X86_CPUID) == 0)
1150Sstevel@tonic-gate 		return (ENXIO);
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	if (uio->uio_resid & (sizeof (crs) - 1))
1180Sstevel@tonic-gate 		return (EINVAL);
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	while (uio->uio_resid > 0) {
1210Sstevel@tonic-gate 		u_offset_t uoff;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 		if ((uoff = (u_offset_t)uio->uio_loffset) > UINT_MAX) {
1240Sstevel@tonic-gate 			error = EINVAL;
1250Sstevel@tonic-gate 			break;
1260Sstevel@tonic-gate 		}
1270Sstevel@tonic-gate 
1281228Sandrei 		crs.cp_eax = (uint32_t)uoff;
1291228Sandrei 		crs.cp_ebx = crs.cp_ecx = crs.cp_edx = 0;
1301228Sandrei 		(void) cpuid_insn(NULL, &crs);
1310Sstevel@tonic-gate 
1321228Sandrei 		if ((error = uiomove(&crs, sizeof (crs), UIO_READ, uio)) != 0)
1330Sstevel@tonic-gate 			break;
1340Sstevel@tonic-gate 		uio->uio_loffset = uoff + 1;
1350Sstevel@tonic-gate 	}
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	return (error);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate #else
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate #define	cpuid_read	nodev
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate #endif	/* _HAVE_CPUID_INSN */
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate /*ARGSUSED*/
1470Sstevel@tonic-gate static int
1480Sstevel@tonic-gate cpuid_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *cr, int *rval)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate 	char areq[16];
1510Sstevel@tonic-gate 	void *ustr;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	switch (cmd) {
1540Sstevel@tonic-gate 	case CPUID_GET_HWCAP: {
1550Sstevel@tonic-gate 		STRUCT_DECL(cpuid_get_hwcap, h);
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 		STRUCT_INIT(h, mode);
1580Sstevel@tonic-gate 		if (ddi_copyin((void *)arg,
1590Sstevel@tonic-gate 		    STRUCT_BUF(h), STRUCT_SIZE(h), mode))
1600Sstevel@tonic-gate 			return (EFAULT);
1610Sstevel@tonic-gate 		if ((ustr = STRUCT_FGETP(h, cgh_archname)) != NULL &&
1620Sstevel@tonic-gate 		    copyinstr(ustr, areq, sizeof (areq), NULL) != 0)
1630Sstevel@tonic-gate 			return (EFAULT);
1640Sstevel@tonic-gate 		areq[sizeof (areq) - 1] = '\0';
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 		if (strcmp(areq, architecture) == 0)
1670Sstevel@tonic-gate 			STRUCT_FSET(h, cgh_hwcap, auxv_hwcap);
1680Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
1690Sstevel@tonic-gate 		else if (strcmp(areq, architecture_32) == 0)
1700Sstevel@tonic-gate 			STRUCT_FSET(h, cgh_hwcap, auxv_hwcap32);
1710Sstevel@tonic-gate #endif
1720Sstevel@tonic-gate 		else
1730Sstevel@tonic-gate 			STRUCT_FSET(h, cgh_hwcap, 0);
1740Sstevel@tonic-gate 		if (ddi_copyout(STRUCT_BUF(h),
1750Sstevel@tonic-gate 		    (void *)arg, STRUCT_SIZE(h), mode))
1760Sstevel@tonic-gate 			return (EFAULT);
1770Sstevel@tonic-gate 		return (0);
1780Sstevel@tonic-gate 	}
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	default:
1810Sstevel@tonic-gate 		return (ENOTTY);
1820Sstevel@tonic-gate 	}
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate static struct cb_ops cpuid_cb_ops = {
1860Sstevel@tonic-gate 	cpuid_open,
1870Sstevel@tonic-gate 	nulldev,	/* close */
1880Sstevel@tonic-gate 	nodev,		/* strategy */
1890Sstevel@tonic-gate 	nodev,		/* print */
1900Sstevel@tonic-gate 	nodev,		/* dump */
1910Sstevel@tonic-gate 	cpuid_read,
1920Sstevel@tonic-gate 	nodev,		/* write */
1930Sstevel@tonic-gate 	cpuid_ioctl,
1940Sstevel@tonic-gate 	nodev,		/* devmap */
1950Sstevel@tonic-gate 	nodev,		/* mmap */
1960Sstevel@tonic-gate 	nodev,		/* segmap */
1970Sstevel@tonic-gate 	nochpoll,	/* poll */
1980Sstevel@tonic-gate 	ddi_prop_op,
1990Sstevel@tonic-gate 	NULL,
2000Sstevel@tonic-gate 	D_64BIT | D_NEW | D_MP
2010Sstevel@tonic-gate };
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate static struct dev_ops cpuid_dv_ops = {
2040Sstevel@tonic-gate 	DEVO_REV,
2050Sstevel@tonic-gate 	0,
2060Sstevel@tonic-gate 	cpuid_getinfo,
2070Sstevel@tonic-gate 	nulldev,	/* identify */
2080Sstevel@tonic-gate 	nulldev,	/* probe */
2090Sstevel@tonic-gate 	cpuid_attach,
2100Sstevel@tonic-gate 	cpuid_detach,
2110Sstevel@tonic-gate 	nodev,		/* reset */
2120Sstevel@tonic-gate 	&cpuid_cb_ops,
213*7656SSherry.Moore@Sun.COM 	(struct bus_ops *)0,
214*7656SSherry.Moore@Sun.COM 	NULL,
215*7656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
2160Sstevel@tonic-gate };
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate static struct modldrv modldrv = {
2190Sstevel@tonic-gate 	&mod_driverops,
220*7656SSherry.Moore@Sun.COM 	"cpuid driver",
2210Sstevel@tonic-gate 	&cpuid_dv_ops
2220Sstevel@tonic-gate };
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate static struct modlinkage modl = {
2250Sstevel@tonic-gate 	MODREV_1,
2260Sstevel@tonic-gate 	&modldrv
2270Sstevel@tonic-gate };
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate int
2300Sstevel@tonic-gate _init(void)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate 	return (mod_install(&modl));
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate int
2360Sstevel@tonic-gate _fini(void)
2370Sstevel@tonic-gate {
2380Sstevel@tonic-gate 	return (mod_remove(&modl));
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate int
2420Sstevel@tonic-gate _info(struct modinfo *modinfo)
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate 	return (mod_info(&modl, modinfo));
2450Sstevel@tonic-gate }
246