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