xref: /onnv-gate/usr/src/uts/common/io/cpuid_drv.c (revision 1228:9e051e1a3f68)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*1228Sandrei  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/file.h>
310Sstevel@tonic-gate #include <sys/errno.h>
320Sstevel@tonic-gate #include <sys/open.h>
330Sstevel@tonic-gate #include <sys/cred.h>
340Sstevel@tonic-gate #include <sys/conf.h>
350Sstevel@tonic-gate #include <sys/stat.h>
360Sstevel@tonic-gate #include <sys/processor.h>
370Sstevel@tonic-gate #include <sys/cpuvar.h>
380Sstevel@tonic-gate #include <sys/kmem.h>
390Sstevel@tonic-gate #include <sys/modctl.h>
400Sstevel@tonic-gate #include <sys/ddi.h>
410Sstevel@tonic-gate #include <sys/sunddi.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include <sys/auxv.h>
440Sstevel@tonic-gate #include <sys/cpuid_drv.h>
450Sstevel@tonic-gate #include <sys/systeminfo.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #if defined(__x86)
480Sstevel@tonic-gate #include <sys/x86_archext.h>
490Sstevel@tonic-gate #endif
500Sstevel@tonic-gate 
510Sstevel@tonic-gate static dev_info_t *cpuid_devi;
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /*ARGSUSED*/
540Sstevel@tonic-gate static int
550Sstevel@tonic-gate cpuid_getinfo(dev_info_t *devi, ddi_info_cmd_t cmd, void *arg, void **result)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	switch (cmd) {
580Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
590Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
600Sstevel@tonic-gate 		break;
610Sstevel@tonic-gate 	default:
620Sstevel@tonic-gate 		return (DDI_FAILURE);
630Sstevel@tonic-gate 	}
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	switch (getminor((dev_t)arg)) {
660Sstevel@tonic-gate 	case CPUID_SELF_CPUID_MINOR:
670Sstevel@tonic-gate 		break;
680Sstevel@tonic-gate 	default:
690Sstevel@tonic-gate 		return (DDI_FAILURE);
700Sstevel@tonic-gate 	}
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	if (cmd == DDI_INFO_DEVT2INSTANCE)
730Sstevel@tonic-gate 		*result = 0;
740Sstevel@tonic-gate 	else
750Sstevel@tonic-gate 		*result = cpuid_devi;
760Sstevel@tonic-gate 	return (DDI_SUCCESS);
770Sstevel@tonic-gate }
780Sstevel@tonic-gate 
790Sstevel@tonic-gate static int
800Sstevel@tonic-gate cpuid_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
810Sstevel@tonic-gate {
820Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
830Sstevel@tonic-gate 		return (DDI_FAILURE);
840Sstevel@tonic-gate 	cpuid_devi = devi;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	return (ddi_create_minor_node(devi, CPUID_DRIVER_SELF_NODE, S_IFCHR,
870Sstevel@tonic-gate 	    CPUID_SELF_CPUID_MINOR, DDI_PSEUDO, 0));
880Sstevel@tonic-gate }
890Sstevel@tonic-gate 
900Sstevel@tonic-gate static int
910Sstevel@tonic-gate cpuid_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
940Sstevel@tonic-gate 		return (DDI_FAILURE);
950Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
960Sstevel@tonic-gate 	cpuid_devi = NULL;
970Sstevel@tonic-gate 	return (DDI_SUCCESS);
980Sstevel@tonic-gate }
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /*ARGSUSED1*/
1010Sstevel@tonic-gate static int
1020Sstevel@tonic-gate cpuid_open(dev_t *dev, int flag, int otyp, cred_t *cr)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	return (getminor(*dev) == CPUID_SELF_CPUID_MINOR ? 0 : ENXIO);
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate #if defined(_HAVE_CPUID_INSN)
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate /*ARGSUSED*/
1100Sstevel@tonic-gate static int
1110Sstevel@tonic-gate cpuid_read(dev_t dev, uio_t *uio, cred_t *cr)
1120Sstevel@tonic-gate {
113*1228Sandrei 	struct cpuid_regs crs;
1140Sstevel@tonic-gate 	int error = 0;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	if ((x86_feature & X86_CPUID) == 0)
1170Sstevel@tonic-gate 		return (ENXIO);
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	if (uio->uio_resid & (sizeof (crs) - 1))
1200Sstevel@tonic-gate 		return (EINVAL);
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	while (uio->uio_resid > 0) {
1230Sstevel@tonic-gate 		u_offset_t uoff;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 		if ((uoff = (u_offset_t)uio->uio_loffset) > UINT_MAX) {
1260Sstevel@tonic-gate 			error = EINVAL;
1270Sstevel@tonic-gate 			break;
1280Sstevel@tonic-gate 		}
1290Sstevel@tonic-gate 
130*1228Sandrei 		crs.cp_eax = (uint32_t)uoff;
131*1228Sandrei 		crs.cp_ebx = crs.cp_ecx = crs.cp_edx = 0;
132*1228Sandrei 		(void) cpuid_insn(NULL, &crs);
1330Sstevel@tonic-gate 
134*1228Sandrei 		if ((error = uiomove(&crs, sizeof (crs), UIO_READ, uio)) != 0)
1350Sstevel@tonic-gate 			break;
1360Sstevel@tonic-gate 		uio->uio_loffset = uoff + 1;
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	return (error);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate #else
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate #define	cpuid_read	nodev
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate #endif	/* _HAVE_CPUID_INSN */
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate /*ARGSUSED*/
1490Sstevel@tonic-gate static int
1500Sstevel@tonic-gate cpuid_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *cr, int *rval)
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate 	char areq[16];
1530Sstevel@tonic-gate 	void *ustr;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	switch (cmd) {
1560Sstevel@tonic-gate 	case CPUID_GET_HWCAP: {
1570Sstevel@tonic-gate 		STRUCT_DECL(cpuid_get_hwcap, h);
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 		STRUCT_INIT(h, mode);
1600Sstevel@tonic-gate 		if (ddi_copyin((void *)arg,
1610Sstevel@tonic-gate 		    STRUCT_BUF(h), STRUCT_SIZE(h), mode))
1620Sstevel@tonic-gate 			return (EFAULT);
1630Sstevel@tonic-gate 		if ((ustr = STRUCT_FGETP(h, cgh_archname)) != NULL &&
1640Sstevel@tonic-gate 		    copyinstr(ustr, areq, sizeof (areq), NULL) != 0)
1650Sstevel@tonic-gate 			return (EFAULT);
1660Sstevel@tonic-gate 		areq[sizeof (areq) - 1] = '\0';
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 		if (strcmp(areq, architecture) == 0)
1690Sstevel@tonic-gate 			STRUCT_FSET(h, cgh_hwcap, auxv_hwcap);
1700Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
1710Sstevel@tonic-gate 		else if (strcmp(areq, architecture_32) == 0)
1720Sstevel@tonic-gate 			STRUCT_FSET(h, cgh_hwcap, auxv_hwcap32);
1730Sstevel@tonic-gate #endif
1740Sstevel@tonic-gate 		else
1750Sstevel@tonic-gate 			STRUCT_FSET(h, cgh_hwcap, 0);
1760Sstevel@tonic-gate 		if (ddi_copyout(STRUCT_BUF(h),
1770Sstevel@tonic-gate 		    (void *)arg, STRUCT_SIZE(h), mode))
1780Sstevel@tonic-gate 			return (EFAULT);
1790Sstevel@tonic-gate 		return (0);
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	default:
1830Sstevel@tonic-gate 		return (ENOTTY);
1840Sstevel@tonic-gate 	}
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate static struct cb_ops cpuid_cb_ops = {
1880Sstevel@tonic-gate 	cpuid_open,
1890Sstevel@tonic-gate 	nulldev,	/* close */
1900Sstevel@tonic-gate 	nodev,		/* strategy */
1910Sstevel@tonic-gate 	nodev,		/* print */
1920Sstevel@tonic-gate 	nodev,		/* dump */
1930Sstevel@tonic-gate 	cpuid_read,
1940Sstevel@tonic-gate 	nodev,		/* write */
1950Sstevel@tonic-gate 	cpuid_ioctl,
1960Sstevel@tonic-gate 	nodev,		/* devmap */
1970Sstevel@tonic-gate 	nodev,		/* mmap */
1980Sstevel@tonic-gate 	nodev,		/* segmap */
1990Sstevel@tonic-gate 	nochpoll,	/* poll */
2000Sstevel@tonic-gate 	ddi_prop_op,
2010Sstevel@tonic-gate 	NULL,
2020Sstevel@tonic-gate 	D_64BIT | D_NEW | D_MP
2030Sstevel@tonic-gate };
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate static struct dev_ops cpuid_dv_ops = {
2060Sstevel@tonic-gate 	DEVO_REV,
2070Sstevel@tonic-gate 	0,
2080Sstevel@tonic-gate 	cpuid_getinfo,
2090Sstevel@tonic-gate 	nulldev,	/* identify */
2100Sstevel@tonic-gate 	nulldev,	/* probe */
2110Sstevel@tonic-gate 	cpuid_attach,
2120Sstevel@tonic-gate 	cpuid_detach,
2130Sstevel@tonic-gate 	nodev,		/* reset */
2140Sstevel@tonic-gate 	&cpuid_cb_ops,
2150Sstevel@tonic-gate 	(struct bus_ops *)0
2160Sstevel@tonic-gate };
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate static struct modldrv modldrv = {
2190Sstevel@tonic-gate 	&mod_driverops,
2200Sstevel@tonic-gate 	"cpuid driver v%I%",
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