xref: /onnv-gate/usr/src/uts/common/io/cpuid_drv.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/file.h>
31*0Sstevel@tonic-gate #include <sys/errno.h>
32*0Sstevel@tonic-gate #include <sys/open.h>
33*0Sstevel@tonic-gate #include <sys/cred.h>
34*0Sstevel@tonic-gate #include <sys/conf.h>
35*0Sstevel@tonic-gate #include <sys/stat.h>
36*0Sstevel@tonic-gate #include <sys/processor.h>
37*0Sstevel@tonic-gate #include <sys/cpuvar.h>
38*0Sstevel@tonic-gate #include <sys/kmem.h>
39*0Sstevel@tonic-gate #include <sys/modctl.h>
40*0Sstevel@tonic-gate #include <sys/ddi.h>
41*0Sstevel@tonic-gate #include <sys/sunddi.h>
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #include <sys/auxv.h>
44*0Sstevel@tonic-gate #include <sys/cpuid_drv.h>
45*0Sstevel@tonic-gate #include <sys/systeminfo.h>
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate #if defined(__x86)
48*0Sstevel@tonic-gate #include <sys/x86_archext.h>
49*0Sstevel@tonic-gate #endif
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate static dev_info_t *cpuid_devi;
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate /*ARGSUSED*/
54*0Sstevel@tonic-gate static int
55*0Sstevel@tonic-gate cpuid_getinfo(dev_info_t *devi, ddi_info_cmd_t cmd, void *arg, void **result)
56*0Sstevel@tonic-gate {
57*0Sstevel@tonic-gate 	switch (cmd) {
58*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
59*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
60*0Sstevel@tonic-gate 		break;
61*0Sstevel@tonic-gate 	default:
62*0Sstevel@tonic-gate 		return (DDI_FAILURE);
63*0Sstevel@tonic-gate 	}
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	switch (getminor((dev_t)arg)) {
66*0Sstevel@tonic-gate 	case CPUID_SELF_CPUID_MINOR:
67*0Sstevel@tonic-gate 		break;
68*0Sstevel@tonic-gate 	default:
69*0Sstevel@tonic-gate 		return (DDI_FAILURE);
70*0Sstevel@tonic-gate 	}
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	if (cmd == DDI_INFO_DEVT2INSTANCE)
73*0Sstevel@tonic-gate 		*result = 0;
74*0Sstevel@tonic-gate 	else
75*0Sstevel@tonic-gate 		*result = cpuid_devi;
76*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate static int
80*0Sstevel@tonic-gate cpuid_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
81*0Sstevel@tonic-gate {
82*0Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
83*0Sstevel@tonic-gate 		return (DDI_FAILURE);
84*0Sstevel@tonic-gate 	cpuid_devi = devi;
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	return (ddi_create_minor_node(devi, CPUID_DRIVER_SELF_NODE, S_IFCHR,
87*0Sstevel@tonic-gate 	    CPUID_SELF_CPUID_MINOR, DDI_PSEUDO, 0));
88*0Sstevel@tonic-gate }
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate static int
91*0Sstevel@tonic-gate cpuid_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
92*0Sstevel@tonic-gate {
93*0Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
94*0Sstevel@tonic-gate 		return (DDI_FAILURE);
95*0Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
96*0Sstevel@tonic-gate 	cpuid_devi = NULL;
97*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
98*0Sstevel@tonic-gate }
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate /*ARGSUSED1*/
101*0Sstevel@tonic-gate static int
102*0Sstevel@tonic-gate cpuid_open(dev_t *dev, int flag, int otyp, cred_t *cr)
103*0Sstevel@tonic-gate {
104*0Sstevel@tonic-gate 	return (getminor(*dev) == CPUID_SELF_CPUID_MINOR ? 0 : ENXIO);
105*0Sstevel@tonic-gate }
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate #if defined(_HAVE_CPUID_INSN)
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate /*ARGSUSED*/
110*0Sstevel@tonic-gate static int
111*0Sstevel@tonic-gate cpuid_read(dev_t dev, uio_t *uio, cred_t *cr)
112*0Sstevel@tonic-gate {
113*0Sstevel@tonic-gate 	uint32_t crs[4];
114*0Sstevel@tonic-gate 	int error = 0;
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	if ((x86_feature & X86_CPUID) == 0)
117*0Sstevel@tonic-gate 		return (ENXIO);
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	if (uio->uio_resid & (sizeof (crs) - 1))
120*0Sstevel@tonic-gate 		return (EINVAL);
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	while (uio->uio_resid > 0) {
123*0Sstevel@tonic-gate 		u_offset_t uoff;
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 		if ((uoff = (u_offset_t)uio->uio_loffset) > UINT_MAX) {
126*0Sstevel@tonic-gate 			error = EINVAL;
127*0Sstevel@tonic-gate 			break;
128*0Sstevel@tonic-gate 		}
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 		crs[0] = cpuid_insn(NULL,
131*0Sstevel@tonic-gate 		    (uint32_t)uoff, &crs[1], &crs[2], &crs[3]);
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 		if ((error = uiomove(crs, sizeof (crs), UIO_READ, uio)) != 0)
134*0Sstevel@tonic-gate 			break;
135*0Sstevel@tonic-gate 		uio->uio_loffset = uoff + 1;
136*0Sstevel@tonic-gate 	}
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	return (error);
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate #else
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate #define	cpuid_read	nodev
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate #endif	/* _HAVE_CPUID_INSN */
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate /*ARGSUSED*/
148*0Sstevel@tonic-gate static int
149*0Sstevel@tonic-gate cpuid_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *cr, int *rval)
150*0Sstevel@tonic-gate {
151*0Sstevel@tonic-gate 	char areq[16];
152*0Sstevel@tonic-gate 	void *ustr;
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	switch (cmd) {
155*0Sstevel@tonic-gate 	case CPUID_GET_HWCAP: {
156*0Sstevel@tonic-gate 		STRUCT_DECL(cpuid_get_hwcap, h);
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 		STRUCT_INIT(h, mode);
159*0Sstevel@tonic-gate 		if (ddi_copyin((void *)arg,
160*0Sstevel@tonic-gate 		    STRUCT_BUF(h), STRUCT_SIZE(h), mode))
161*0Sstevel@tonic-gate 			return (EFAULT);
162*0Sstevel@tonic-gate 		if ((ustr = STRUCT_FGETP(h, cgh_archname)) != NULL &&
163*0Sstevel@tonic-gate 		    copyinstr(ustr, areq, sizeof (areq), NULL) != 0)
164*0Sstevel@tonic-gate 			return (EFAULT);
165*0Sstevel@tonic-gate 		areq[sizeof (areq) - 1] = '\0';
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 		if (strcmp(areq, architecture) == 0)
168*0Sstevel@tonic-gate 			STRUCT_FSET(h, cgh_hwcap, auxv_hwcap);
169*0Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
170*0Sstevel@tonic-gate 		else if (strcmp(areq, architecture_32) == 0)
171*0Sstevel@tonic-gate 			STRUCT_FSET(h, cgh_hwcap, auxv_hwcap32);
172*0Sstevel@tonic-gate #endif
173*0Sstevel@tonic-gate 		else
174*0Sstevel@tonic-gate 			STRUCT_FSET(h, cgh_hwcap, 0);
175*0Sstevel@tonic-gate 		if (ddi_copyout(STRUCT_BUF(h),
176*0Sstevel@tonic-gate 		    (void *)arg, STRUCT_SIZE(h), mode))
177*0Sstevel@tonic-gate 			return (EFAULT);
178*0Sstevel@tonic-gate 		return (0);
179*0Sstevel@tonic-gate 	}
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	default:
182*0Sstevel@tonic-gate 		return (ENOTTY);
183*0Sstevel@tonic-gate 	}
184*0Sstevel@tonic-gate }
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate static struct cb_ops cpuid_cb_ops = {
187*0Sstevel@tonic-gate 	cpuid_open,
188*0Sstevel@tonic-gate 	nulldev,	/* close */
189*0Sstevel@tonic-gate 	nodev,		/* strategy */
190*0Sstevel@tonic-gate 	nodev,		/* print */
191*0Sstevel@tonic-gate 	nodev,		/* dump */
192*0Sstevel@tonic-gate 	cpuid_read,
193*0Sstevel@tonic-gate 	nodev,		/* write */
194*0Sstevel@tonic-gate 	cpuid_ioctl,
195*0Sstevel@tonic-gate 	nodev,		/* devmap */
196*0Sstevel@tonic-gate 	nodev,		/* mmap */
197*0Sstevel@tonic-gate 	nodev,		/* segmap */
198*0Sstevel@tonic-gate 	nochpoll,	/* poll */
199*0Sstevel@tonic-gate 	ddi_prop_op,
200*0Sstevel@tonic-gate 	NULL,
201*0Sstevel@tonic-gate 	D_64BIT | D_NEW | D_MP
202*0Sstevel@tonic-gate };
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate static struct dev_ops cpuid_dv_ops = {
205*0Sstevel@tonic-gate 	DEVO_REV,
206*0Sstevel@tonic-gate 	0,
207*0Sstevel@tonic-gate 	cpuid_getinfo,
208*0Sstevel@tonic-gate 	nulldev,	/* identify */
209*0Sstevel@tonic-gate 	nulldev,	/* probe */
210*0Sstevel@tonic-gate 	cpuid_attach,
211*0Sstevel@tonic-gate 	cpuid_detach,
212*0Sstevel@tonic-gate 	nodev,		/* reset */
213*0Sstevel@tonic-gate 	&cpuid_cb_ops,
214*0Sstevel@tonic-gate 	(struct bus_ops *)0
215*0Sstevel@tonic-gate };
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate static struct modldrv modldrv = {
218*0Sstevel@tonic-gate 	&mod_driverops,
219*0Sstevel@tonic-gate 	"cpuid driver v%I%",
220*0Sstevel@tonic-gate 	&cpuid_dv_ops
221*0Sstevel@tonic-gate };
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate static struct modlinkage modl = {
224*0Sstevel@tonic-gate 	MODREV_1,
225*0Sstevel@tonic-gate 	&modldrv
226*0Sstevel@tonic-gate };
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate int
229*0Sstevel@tonic-gate _init(void)
230*0Sstevel@tonic-gate {
231*0Sstevel@tonic-gate 	return (mod_install(&modl));
232*0Sstevel@tonic-gate }
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate int
235*0Sstevel@tonic-gate _fini(void)
236*0Sstevel@tonic-gate {
237*0Sstevel@tonic-gate 	return (mod_remove(&modl));
238*0Sstevel@tonic-gate }
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate int
241*0Sstevel@tonic-gate _info(struct modinfo *modinfo)
242*0Sstevel@tonic-gate {
243*0Sstevel@tonic-gate 	return (mod_info(&modl, modinfo));
244*0Sstevel@tonic-gate }
245