1e4b86885SCheng Sean Ye /*
2e4b86885SCheng Sean Ye  * CDDL HEADER START
3e4b86885SCheng Sean Ye  *
4e4b86885SCheng Sean Ye  * The contents of this file are subject to the terms of the
5e4b86885SCheng Sean Ye  * Common Development and Distribution License (the "License").
6e4b86885SCheng Sean Ye  * You may not use this file except in compliance with the License.
7e4b86885SCheng Sean Ye  *
8e4b86885SCheng Sean Ye  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9e4b86885SCheng Sean Ye  * or http://www.opensolaris.org/os/licensing.
10e4b86885SCheng Sean Ye  * See the License for the specific language governing permissions
11e4b86885SCheng Sean Ye  * and limitations under the License.
12e4b86885SCheng Sean Ye  *
13e4b86885SCheng Sean Ye  * When distributing Covered Code, include this CDDL HEADER in each
14e4b86885SCheng Sean Ye  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15e4b86885SCheng Sean Ye  * If applicable, add the following below this CDDL HEADER, with the
16e4b86885SCheng Sean Ye  * fields enclosed by brackets "[]" replaced with your own identifying
17e4b86885SCheng Sean Ye  * information: Portions Copyright [yyyy] [name of copyright owner]
18e4b86885SCheng Sean Ye  *
19e4b86885SCheng Sean Ye  * CDDL HEADER END
20e4b86885SCheng Sean Ye  */
21e4b86885SCheng Sean Ye /*
22074bb90dSTom Pothier  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23e4b86885SCheng Sean Ye  * Use is subject to license terms.
242a613b59SRobert Mustacchi  * Copyright (c) 2018, Joyent, Inc.
25*dd23d762SRobert Mustacchi  * Copyright 2023 Oxide Computer Company
26e4b86885SCheng Sean Ye  */
27e4b86885SCheng Sean Ye 
28e4b86885SCheng Sean Ye #include <sys/stat.h>
29e4b86885SCheng Sean Ye #include <sys/types.h>
30e4b86885SCheng Sean Ye #include <sys/time.h>
31e4b86885SCheng Sean Ye 
32e4b86885SCheng Sean Ye #include <sys/fm/protocol.h>
33074bb90dSTom Pothier #include <sys/fm/smb/fmsmb.h>
34e4b86885SCheng Sean Ye #include <sys/devfm.h>
35e4b86885SCheng Sean Ye 
36e4b86885SCheng Sean Ye #include <sys/cpu_module.h>
37e4b86885SCheng Sean Ye 
38e4b86885SCheng Sean Ye #define	ANY_ID		(uint_t)-1
39e4b86885SCheng Sean Ye 
40e4b86885SCheng Sean Ye /*
41e4b86885SCheng Sean Ye  * INIT_HDLS is the initial size of cmi_hdl_t array.  We fill the array
42e4b86885SCheng Sean Ye  * during cmi_hdl_walk, if the array overflows, we will reallocate
43e4b86885SCheng Sean Ye  * a new array twice the size of the old one.
44e4b86885SCheng Sean Ye  */
45e4b86885SCheng Sean Ye #define	INIT_HDLS	16
46e4b86885SCheng Sean Ye 
47e4b86885SCheng Sean Ye typedef struct fm_cmi_walk_t
48e4b86885SCheng Sean Ye {
49e4b86885SCheng Sean Ye 	uint_t	chipid;		/* chipid to match during walk */
50e4b86885SCheng Sean Ye 	uint_t	coreid;		/* coreid to match */
51e4b86885SCheng Sean Ye 	uint_t	strandid;	/* strandid to match */
52*dd23d762SRobert Mustacchi 	int	(*cbfunc)(cmi_hdl_t, void *, void *);	/* callback function */
53e4b86885SCheng Sean Ye 	cmi_hdl_t *hdls;	/* allocated array to save the handles */
54*dd23d762SRobert Mustacchi 	uint_t	nhdl_max;	/* allocated array size */
55*dd23d762SRobert Mustacchi 	uint_t	nhdl;		/* handles saved */
56e4b86885SCheng Sean Ye } fm_cmi_walk_t;
57e4b86885SCheng Sean Ye 
58074bb90dSTom Pothier extern int x86gentopo_legacy;
59074bb90dSTom Pothier 
60e4b86885SCheng Sean Ye int
fm_get_paddr(nvlist_t * nvl,uint64_t * paddr)61e4b86885SCheng Sean Ye fm_get_paddr(nvlist_t *nvl, uint64_t *paddr)
62e4b86885SCheng Sean Ye {
63e4b86885SCheng Sean Ye 	uint8_t version;
64e4b86885SCheng Sean Ye 	uint64_t pa;
65e4b86885SCheng Sean Ye 	char *scheme;
66e4b86885SCheng Sean Ye 	int err;
67e4b86885SCheng Sean Ye 
68e4b86885SCheng Sean Ye 	/* Verify FMRI scheme name and version number */
69e4b86885SCheng Sean Ye 	if ((nvlist_lookup_string(nvl, FM_FMRI_SCHEME, &scheme) != 0) ||
70e4b86885SCheng Sean Ye 	    (strcmp(scheme, FM_FMRI_SCHEME_HC) != 0) ||
71e4b86885SCheng Sean Ye 	    (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0) ||
72e4b86885SCheng Sean Ye 	    version > FM_HC_SCHEME_VERSION) {
73e4b86885SCheng Sean Ye 		return (EINVAL);
74e4b86885SCheng Sean Ye 	}
75e4b86885SCheng Sean Ye 
76e4b86885SCheng Sean Ye 	if ((err = cmi_mc_unumtopa(NULL, nvl, &pa)) != CMI_SUCCESS &&
77e4b86885SCheng Sean Ye 	    err != CMIERR_MC_PARTIALUNUMTOPA)
78e4b86885SCheng Sean Ye 		return (EINVAL);
79e4b86885SCheng Sean Ye 
80e4b86885SCheng Sean Ye 	*paddr = pa;
81e4b86885SCheng Sean Ye 	return (0);
82e4b86885SCheng Sean Ye }
83e4b86885SCheng Sean Ye 
84e4b86885SCheng Sean Ye /*
85e4b86885SCheng Sean Ye  * Routines for cmi handles walk.
86e4b86885SCheng Sean Ye  */
87e4b86885SCheng Sean Ye 
88e4b86885SCheng Sean Ye static void
walk_init(fm_cmi_walk_t * wp,uint_t chipid,uint_t coreid,uint_t strandid,int (* cbfunc)(cmi_hdl_t,void *,void *))89e4b86885SCheng Sean Ye walk_init(fm_cmi_walk_t *wp, uint_t chipid, uint_t coreid, uint_t strandid,
90e4b86885SCheng Sean Ye     int (*cbfunc)(cmi_hdl_t, void *, void *))
91e4b86885SCheng Sean Ye {
92e4b86885SCheng Sean Ye 	wp->chipid = chipid;
93e4b86885SCheng Sean Ye 	wp->coreid = coreid;
94e4b86885SCheng Sean Ye 	wp->strandid = strandid;
95e4b86885SCheng Sean Ye 	/*
96e4b86885SCheng Sean Ye 	 * If callback is not set, we allocate an array to save the
97e4b86885SCheng Sean Ye 	 * cmi handles.
98e4b86885SCheng Sean Ye 	 */
99e4b86885SCheng Sean Ye 	if ((wp->cbfunc = cbfunc) == NULL) {
100e4b86885SCheng Sean Ye 		wp->hdls = kmem_alloc(sizeof (cmi_hdl_t) * INIT_HDLS, KM_SLEEP);
101e4b86885SCheng Sean Ye 		wp->nhdl_max = INIT_HDLS;
102e4b86885SCheng Sean Ye 		wp->nhdl = 0;
103e4b86885SCheng Sean Ye 	}
104e4b86885SCheng Sean Ye }
105e4b86885SCheng Sean Ye 
106e4b86885SCheng Sean Ye static void
walk_fini(fm_cmi_walk_t * wp)107e4b86885SCheng Sean Ye walk_fini(fm_cmi_walk_t *wp)
108e4b86885SCheng Sean Ye {
109e4b86885SCheng Sean Ye 	if (wp->cbfunc == NULL)
110e4b86885SCheng Sean Ye 		kmem_free(wp->hdls, sizeof (cmi_hdl_t) * wp->nhdl_max);
111e4b86885SCheng Sean Ye }
112e4b86885SCheng Sean Ye 
113e4b86885SCheng Sean Ye static int
select_cmi_hdl(cmi_hdl_t hdl,void * arg1,void * arg2,void * arg3)114e4b86885SCheng Sean Ye select_cmi_hdl(cmi_hdl_t hdl, void *arg1, void *arg2, void *arg3)
115e4b86885SCheng Sean Ye {
116e4b86885SCheng Sean Ye 	fm_cmi_walk_t *wp = (fm_cmi_walk_t *)arg1;
117e4b86885SCheng Sean Ye 
118e4b86885SCheng Sean Ye 	if (wp->chipid != ANY_ID && wp->chipid != cmi_hdl_chipid(hdl))
119e4b86885SCheng Sean Ye 		return (CMI_HDL_WALK_NEXT);
120e4b86885SCheng Sean Ye 	if (wp->coreid != ANY_ID && wp->coreid != cmi_hdl_coreid(hdl))
121e4b86885SCheng Sean Ye 		return (CMI_HDL_WALK_NEXT);
122e4b86885SCheng Sean Ye 	if (wp->strandid != ANY_ID && wp->strandid != cmi_hdl_strandid(hdl))
123e4b86885SCheng Sean Ye 		return (CMI_HDL_WALK_NEXT);
124e4b86885SCheng Sean Ye 
125e4b86885SCheng Sean Ye 	/*
126e4b86885SCheng Sean Ye 	 * Call the callback function if any exists, otherwise we hold a
127e4b86885SCheng Sean Ye 	 * reference of the handle and push it to preallocated array.
128e4b86885SCheng Sean Ye 	 * If the allocated array is going to overflow, reallocate a
129e4b86885SCheng Sean Ye 	 * bigger one to replace it.
130e4b86885SCheng Sean Ye 	 */
131e4b86885SCheng Sean Ye 	if (wp->cbfunc != NULL)
132e4b86885SCheng Sean Ye 		return (wp->cbfunc(hdl, arg2, arg3));
133e4b86885SCheng Sean Ye 
134e4b86885SCheng Sean Ye 	if (wp->nhdl == wp->nhdl_max) {
135e4b86885SCheng Sean Ye 		size_t sz = sizeof (cmi_hdl_t) * wp->nhdl_max;
136e4b86885SCheng Sean Ye 		cmi_hdl_t *newarray = kmem_alloc(sz << 1, KM_SLEEP);
137e4b86885SCheng Sean Ye 
138e4b86885SCheng Sean Ye 		bcopy(wp->hdls, newarray, sz);
139e4b86885SCheng Sean Ye 		kmem_free(wp->hdls, sz);
140e4b86885SCheng Sean Ye 		wp->hdls = newarray;
141e4b86885SCheng Sean Ye 		wp->nhdl_max <<= 1;
142e4b86885SCheng Sean Ye 	}
143e4b86885SCheng Sean Ye 
144e4b86885SCheng Sean Ye 	cmi_hdl_hold(hdl);
145e4b86885SCheng Sean Ye 	wp->hdls[wp->nhdl++] = hdl;
146e4b86885SCheng Sean Ye 
147e4b86885SCheng Sean Ye 	return (CMI_HDL_WALK_NEXT);
148e4b86885SCheng Sean Ye }
149e4b86885SCheng Sean Ye 
150e4b86885SCheng Sean Ye static void
populate_cpu(nvlist_t ** nvlp,cmi_hdl_t hdl)151e4b86885SCheng Sean Ye populate_cpu(nvlist_t **nvlp, cmi_hdl_t hdl)
152e4b86885SCheng Sean Ye {
153074bb90dSTom Pothier 	uint_t	fm_chipid;
154074bb90dSTom Pothier 	uint16_t smbios_id;
1552a613b59SRobert Mustacchi 	const char *idstr;
156074bb90dSTom Pothier 
157e4b86885SCheng Sean Ye 	(void) nvlist_alloc(nvlp, NV_UNIQUE_NAME, KM_SLEEP);
158074bb90dSTom Pothier 
159074bb90dSTom Pothier 	/*
160074bb90dSTom Pothier 	 * If SMBIOS satisfies FMA Topology needs, gather
161074bb90dSTom Pothier 	 * more information on the chip's physical roots
162074bb90dSTom Pothier 	 * like /chassis=x/motherboard=y/cpuboard=z and
163074bb90dSTom Pothier 	 * set the chip_id to match the SMBIOS' Type 4
164074bb90dSTom Pothier 	 * ordering & this has to match the ereport's chip
165074bb90dSTom Pothier 	 * resource instance derived off of SMBIOS.
166074bb90dSTom Pothier 	 * Multi-Chip-Module support should set the chipid
167074bb90dSTom Pothier 	 * in terms of the processor package rather than
168074bb90dSTom Pothier 	 * the die/node in the processor package, for FM.
169074bb90dSTom Pothier 	 */
170074bb90dSTom Pothier 
171074bb90dSTom Pothier 	if (!x86gentopo_legacy) {
172074bb90dSTom Pothier 		smbios_id = cmi_hdl_smbiosid(hdl);
173074bb90dSTom Pothier 		fm_chipid = cmi_hdl_smb_chipid(hdl);
174074bb90dSTom Pothier 		(void) nvlist_add_nvlist(*nvlp, FM_PHYSCPU_INFO_CHIP_ROOTS,
175074bb90dSTom Pothier 		    cmi_hdl_smb_bboard(hdl));
176074bb90dSTom Pothier 		(void) nvlist_add_uint16(*nvlp, FM_PHYSCPU_INFO_SMBIOS_ID,
177074bb90dSTom Pothier 		    (uint16_t)smbios_id);
178074bb90dSTom Pothier 	} else
179074bb90dSTom Pothier 		fm_chipid = cmi_hdl_chipid(hdl);
180074bb90dSTom Pothier 
181e4b86885SCheng Sean Ye 	fm_payload_set(*nvlp,
182e4b86885SCheng Sean Ye 	    FM_PHYSCPU_INFO_VENDOR_ID, DATA_TYPE_STRING,
183e4b86885SCheng Sean Ye 	    cmi_hdl_vendorstr(hdl),
184e4b86885SCheng Sean Ye 	    FM_PHYSCPU_INFO_FAMILY, DATA_TYPE_INT32,
185e4b86885SCheng Sean Ye 	    (int32_t)cmi_hdl_family(hdl),
186e4b86885SCheng Sean Ye 	    FM_PHYSCPU_INFO_MODEL, DATA_TYPE_INT32,
187e4b86885SCheng Sean Ye 	    (int32_t)cmi_hdl_model(hdl),
188e4b86885SCheng Sean Ye 	    FM_PHYSCPU_INFO_STEPPING, DATA_TYPE_INT32,
189e4b86885SCheng Sean Ye 	    (int32_t)cmi_hdl_stepping(hdl),
190e4b86885SCheng Sean Ye 	    FM_PHYSCPU_INFO_CHIP_ID, DATA_TYPE_INT32,
191074bb90dSTom Pothier 	    (int32_t)fm_chipid,
1928031591dSSrihari Venkatesan 	    FM_PHYSCPU_INFO_NPROCNODES, DATA_TYPE_INT32,
1938031591dSSrihari Venkatesan 	    (int32_t)cmi_hdl_procnodes_per_pkg(hdl),
1948031591dSSrihari Venkatesan 	    FM_PHYSCPU_INFO_PROCNODE_ID, DATA_TYPE_INT32,
1958031591dSSrihari Venkatesan 	    (int32_t)cmi_hdl_procnodeid(hdl),
196e4b86885SCheng Sean Ye 	    FM_PHYSCPU_INFO_CORE_ID, DATA_TYPE_INT32,
197e4b86885SCheng Sean Ye 	    (int32_t)cmi_hdl_coreid(hdl),
198e4b86885SCheng Sean Ye 	    FM_PHYSCPU_INFO_STRAND_ID, DATA_TYPE_INT32,
199e4b86885SCheng Sean Ye 	    (int32_t)cmi_hdl_strandid(hdl),
200074bb90dSTom Pothier 	    FM_PHYSCPU_INFO_STRAND_APICID, DATA_TYPE_INT32,
201074bb90dSTom Pothier 	    (int32_t)cmi_hdl_strand_apicid(hdl),
202e4b86885SCheng Sean Ye 	    FM_PHYSCPU_INFO_CHIP_REV, DATA_TYPE_STRING,
203e4b86885SCheng Sean Ye 	    cmi_hdl_chiprevstr(hdl),
204e4b86885SCheng Sean Ye 	    FM_PHYSCPU_INFO_SOCKET_TYPE, DATA_TYPE_UINT32,
205e4b86885SCheng Sean Ye 	    (uint32_t)cmi_hdl_getsockettype(hdl),
206e4b86885SCheng Sean Ye 	    FM_PHYSCPU_INFO_CPU_ID, DATA_TYPE_INT32,
207e4b86885SCheng Sean Ye 	    (int32_t)cmi_hdl_logical_id(hdl),
208e4b86885SCheng Sean Ye 	    NULL);
2092a613b59SRobert Mustacchi 
2102a613b59SRobert Mustacchi 	/*
2112a613b59SRobert Mustacchi 	 * Do this separately so that way if there is no ident string we do not
2122a613b59SRobert Mustacchi 	 * trigger an error.
2132a613b59SRobert Mustacchi 	 */
2142a613b59SRobert Mustacchi 	if ((idstr = cmi_hdl_chipident(hdl)) != NULL) {
2152a613b59SRobert Mustacchi 		fm_payload_set(*nvlp,
2162a613b59SRobert Mustacchi 		    FM_PHYSCPU_INFO_CHIP_IDENTSTR, DATA_TYPE_STRING, idstr,
2172a613b59SRobert Mustacchi 		    NULL);
2182a613b59SRobert Mustacchi 	}
219e4b86885SCheng Sean Ye }
220e4b86885SCheng Sean Ye 
221e4b86885SCheng Sean Ye /*ARGSUSED*/
222e4b86885SCheng Sean Ye int
fm_ioctl_physcpu_info(int cmd,nvlist_t * invl,nvlist_t ** onvlp)223e4b86885SCheng Sean Ye fm_ioctl_physcpu_info(int cmd, nvlist_t *invl, nvlist_t **onvlp)
224e4b86885SCheng Sean Ye {
225e4b86885SCheng Sean Ye 	nvlist_t **cpus, *nvl;
226e4b86885SCheng Sean Ye 	int i, err;
227e4b86885SCheng Sean Ye 	fm_cmi_walk_t wk;
228e4b86885SCheng Sean Ye 
229e4b86885SCheng Sean Ye 	/*
230e4b86885SCheng Sean Ye 	 * Do a walk to save all the cmi handles in the array.
231e4b86885SCheng Sean Ye 	 */
232e4b86885SCheng Sean Ye 	walk_init(&wk, ANY_ID, ANY_ID, ANY_ID, NULL);
233e4b86885SCheng Sean Ye 	cmi_hdl_walk(select_cmi_hdl, &wk, NULL, NULL);
234e4b86885SCheng Sean Ye 
235e4b86885SCheng Sean Ye 	if (wk.nhdl == 0) {
236e4b86885SCheng Sean Ye 		walk_fini(&wk);
237e4b86885SCheng Sean Ye 		return (ENOENT);
238e4b86885SCheng Sean Ye 	}
239e4b86885SCheng Sean Ye 
240e4b86885SCheng Sean Ye 	cpus = kmem_alloc(sizeof (nvlist_t *) * wk.nhdl, KM_SLEEP);
241e4b86885SCheng Sean Ye 	for (i = 0; i < wk.nhdl; i++) {
242e4b86885SCheng Sean Ye 		populate_cpu(cpus + i, wk.hdls[i]);
243e4b86885SCheng Sean Ye 		cmi_hdl_rele(wk.hdls[i]);
244e4b86885SCheng Sean Ye 	}
245e4b86885SCheng Sean Ye 
246e4b86885SCheng Sean Ye 	walk_fini(&wk);
247e4b86885SCheng Sean Ye 
248e4b86885SCheng Sean Ye 	(void) nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP);
249e4b86885SCheng Sean Ye 	err = nvlist_add_nvlist_array(nvl, FM_PHYSCPU_INFO_CPUS,
250e4b86885SCheng Sean Ye 	    cpus, wk.nhdl);
251e4b86885SCheng Sean Ye 
252e4b86885SCheng Sean Ye 	for (i = 0; i < wk.nhdl; i++)
253e4b86885SCheng Sean Ye 		nvlist_free(cpus[i]);
254e4b86885SCheng Sean Ye 	kmem_free(cpus, sizeof (nvlist_t *) * wk.nhdl);
255e4b86885SCheng Sean Ye 
256e4b86885SCheng Sean Ye 	if (err != 0) {
257e4b86885SCheng Sean Ye 		nvlist_free(nvl);
258e4b86885SCheng Sean Ye 		return (err);
259e4b86885SCheng Sean Ye 	}
260e4b86885SCheng Sean Ye 
261e4b86885SCheng Sean Ye 	*onvlp = nvl;
262e4b86885SCheng Sean Ye 	return (0);
263e4b86885SCheng Sean Ye }
264e4b86885SCheng Sean Ye 
265e4b86885SCheng Sean Ye int
fm_ioctl_cpu_retire(int cmd,nvlist_t * invl,nvlist_t ** onvlp)266e4b86885SCheng Sean Ye fm_ioctl_cpu_retire(int cmd, nvlist_t *invl, nvlist_t **onvlp)
267e4b86885SCheng Sean Ye {
268e4b86885SCheng Sean Ye 	int32_t chipid, coreid, strandid;
269e4b86885SCheng Sean Ye 	int rc, new_status, old_status;
270e4b86885SCheng Sean Ye 	cmi_hdl_t hdl;
271e4b86885SCheng Sean Ye 	nvlist_t *nvl;
272e4b86885SCheng Sean Ye 
273e4b86885SCheng Sean Ye 	switch (cmd) {
274e4b86885SCheng Sean Ye 	case FM_IOC_CPU_RETIRE:
275e4b86885SCheng Sean Ye 		new_status = P_FAULTED;
276e4b86885SCheng Sean Ye 		break;
277e4b86885SCheng Sean Ye 	case FM_IOC_CPU_STATUS:
278e4b86885SCheng Sean Ye 		new_status = P_STATUS;
279e4b86885SCheng Sean Ye 		break;
280e4b86885SCheng Sean Ye 	case FM_IOC_CPU_UNRETIRE:
281e4b86885SCheng Sean Ye 		new_status = P_ONLINE;
282e4b86885SCheng Sean Ye 		break;
283e4b86885SCheng Sean Ye 	default:
284e4b86885SCheng Sean Ye 		return (ENOTTY);
285e4b86885SCheng Sean Ye 	}
286e4b86885SCheng Sean Ye 
287e4b86885SCheng Sean Ye 	if (nvlist_lookup_int32(invl, FM_CPU_RETIRE_CHIP_ID, &chipid) != 0 ||
288e4b86885SCheng Sean Ye 	    nvlist_lookup_int32(invl, FM_CPU_RETIRE_CORE_ID, &coreid) != 0 ||
289e4b86885SCheng Sean Ye 	    nvlist_lookup_int32(invl, FM_CPU_RETIRE_STRAND_ID, &strandid) != 0)
290e4b86885SCheng Sean Ye 		return (EINVAL);
291e4b86885SCheng Sean Ye 
292e4b86885SCheng Sean Ye 	hdl = cmi_hdl_lookup(CMI_HDL_NEUTRAL, chipid, coreid, strandid);
293e4b86885SCheng Sean Ye 	if (hdl == NULL)
294e4b86885SCheng Sean Ye 		return (EINVAL);
295e4b86885SCheng Sean Ye 
296e4b86885SCheng Sean Ye 	rc = cmi_hdl_online(hdl, new_status, &old_status);
297e4b86885SCheng Sean Ye 	cmi_hdl_rele(hdl);
298e4b86885SCheng Sean Ye 
299e4b86885SCheng Sean Ye 	if (rc == 0) {
300e4b86885SCheng Sean Ye 		(void) nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP);
301e4b86885SCheng Sean Ye 		(void) nvlist_add_int32(nvl, FM_CPU_RETIRE_OLDSTATUS,
302e4b86885SCheng Sean Ye 		    old_status);
303e4b86885SCheng Sean Ye 		*onvlp = nvl;
304e4b86885SCheng Sean Ye 	}
305e4b86885SCheng Sean Ye 
306e4b86885SCheng Sean Ye 	return (rc);
307e4b86885SCheng Sean Ye }
308074bb90dSTom Pothier 
309074bb90dSTom Pothier /*
310074bb90dSTom Pothier  * Retrun the value of x86gentopo_legacy variable as an nvpair.
311074bb90dSTom Pothier  *
312074bb90dSTom Pothier  * The caller is responsible for freeing the nvlist.
313074bb90dSTom Pothier  */
314074bb90dSTom Pothier /* ARGSUSED */
315074bb90dSTom Pothier int
fm_ioctl_gentopo_legacy(int cmd,nvlist_t * invl,nvlist_t ** onvlp)316074bb90dSTom Pothier fm_ioctl_gentopo_legacy(int cmd, nvlist_t *invl, nvlist_t **onvlp)
317074bb90dSTom Pothier {
318074bb90dSTom Pothier 	nvlist_t *nvl;
319074bb90dSTom Pothier 
320074bb90dSTom Pothier 	if (cmd != FM_IOC_GENTOPO_LEGACY) {
321074bb90dSTom Pothier 		return (ENOTTY);
322074bb90dSTom Pothier 	}
323074bb90dSTom Pothier 
324074bb90dSTom Pothier 	/*
325074bb90dSTom Pothier 	 * Inform the caller of the intentions of the ereport generators to
326074bb90dSTom Pothier 	 * generate either a "generic" or "legacy" x86 topology.
327074bb90dSTom Pothier 	 */
328074bb90dSTom Pothier 
329074bb90dSTom Pothier 	(void) nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP);
330074bb90dSTom Pothier 	(void) nvlist_add_int32(nvl, FM_GENTOPO_LEGACY, x86gentopo_legacy);
331074bb90dSTom Pothier 	*onvlp = nvl;
332074bb90dSTom Pothier 
333074bb90dSTom Pothier 	return (0);
334074bb90dSTom Pothier }
335*dd23d762SRobert Mustacchi 
336*dd23d762SRobert Mustacchi /*
337*dd23d762SRobert Mustacchi  * This is an internal bound on the maximum number of caches that we expect to
338*dd23d762SRobert Mustacchi  * encounter to reduce dynamic allocation.
339*dd23d762SRobert Mustacchi  */
340*dd23d762SRobert Mustacchi #define	FM_MAX_CACHES	0x10
341*dd23d762SRobert Mustacchi 
342*dd23d762SRobert Mustacchi static int
fm_cmi_cache_err_to_errno(cmi_errno_t cmi)343*dd23d762SRobert Mustacchi fm_cmi_cache_err_to_errno(cmi_errno_t cmi)
344*dd23d762SRobert Mustacchi {
345*dd23d762SRobert Mustacchi 	switch (cmi) {
346*dd23d762SRobert Mustacchi 	case CMIERR_C_NODATA:
347*dd23d762SRobert Mustacchi 		return (ENOTSUP);
348*dd23d762SRobert Mustacchi 	/*
349*dd23d762SRobert Mustacchi 	 * Right now, CMIERR_C_BADCACHENO is explicitly not mapped to EINVAL
350*dd23d762SRobert Mustacchi 	 * (which is what it maps to in cmi_hw.c.). This discrepancy exists
351*dd23d762SRobert Mustacchi 	 * because there's nothing in a user request here that'd end up
352*dd23d762SRobert Mustacchi 	 * resulting in an invalid value, it can only occur because we asked
353*dd23d762SRobert Mustacchi 	 * for a cache that we were told exists, but doesn't actually. If we
354*dd23d762SRobert Mustacchi 	 * returned EINVAL, the user would be wondering what was invalid about
355*dd23d762SRobert Mustacchi 	 * their request.
356*dd23d762SRobert Mustacchi 	 */
357*dd23d762SRobert Mustacchi 	case CMIERR_C_BADCACHENO:
358*dd23d762SRobert Mustacchi 	default:
359*dd23d762SRobert Mustacchi 		return (EIO);
360*dd23d762SRobert Mustacchi 	}
361*dd23d762SRobert Mustacchi }
362*dd23d762SRobert Mustacchi 
363*dd23d762SRobert Mustacchi static int
fm_populate_cache(cmi_hdl_t hdl,nvlist_t * nvl,uint_t cpuno)364*dd23d762SRobert Mustacchi fm_populate_cache(cmi_hdl_t hdl, nvlist_t *nvl, uint_t cpuno)
365*dd23d762SRobert Mustacchi {
366*dd23d762SRobert Mustacchi 	int ret;
367*dd23d762SRobert Mustacchi 	cmi_errno_t err;
368*dd23d762SRobert Mustacchi 	uint32_t ncache;
369*dd23d762SRobert Mustacchi 	nvlist_t *caches[FM_MAX_CACHES];
370*dd23d762SRobert Mustacchi 	char buf[32];
371*dd23d762SRobert Mustacchi 
372*dd23d762SRobert Mustacchi 	err = cmi_cache_ncaches(hdl, &ncache);
373*dd23d762SRobert Mustacchi 	if (err != CMI_SUCCESS) {
374*dd23d762SRobert Mustacchi 		return (fm_cmi_cache_err_to_errno(err));
375*dd23d762SRobert Mustacchi 	}
376*dd23d762SRobert Mustacchi 
377*dd23d762SRobert Mustacchi 	/*
378*dd23d762SRobert Mustacchi 	 * Our promise to userland is that if we skip a value here then there
379*dd23d762SRobert Mustacchi 	 * are no caches.
380*dd23d762SRobert Mustacchi 	 */
381*dd23d762SRobert Mustacchi 	if (ncache == 0) {
382*dd23d762SRobert Mustacchi 		return (0);
383*dd23d762SRobert Mustacchi 	} else if (ncache > FM_MAX_CACHES) {
384*dd23d762SRobert Mustacchi 		return (EOVERFLOW);
385*dd23d762SRobert Mustacchi 	}
386*dd23d762SRobert Mustacchi 
387*dd23d762SRobert Mustacchi 	bzero(caches, sizeof (caches));
388*dd23d762SRobert Mustacchi 	for (uint32_t i = 0; i < ncache; i++) {
389*dd23d762SRobert Mustacchi 		x86_cache_t c;
390*dd23d762SRobert Mustacchi 		fm_cache_info_type_t type = 0;
391*dd23d762SRobert Mustacchi 
392*dd23d762SRobert Mustacchi 		(void) nvlist_alloc(&caches[i], NV_UNIQUE_NAME, KM_SLEEP);
393*dd23d762SRobert Mustacchi 		err = cmi_cache_info(hdl, i, &c);
394*dd23d762SRobert Mustacchi 		if (err != CMI_SUCCESS) {
395*dd23d762SRobert Mustacchi 			ret = fm_cmi_cache_err_to_errno(err);
396*dd23d762SRobert Mustacchi 			goto cleanup;
397*dd23d762SRobert Mustacchi 		}
398*dd23d762SRobert Mustacchi 
399*dd23d762SRobert Mustacchi 		fnvlist_add_uint32(caches[i], FM_CACHE_INFO_LEVEL, c.xc_level);
400*dd23d762SRobert Mustacchi 		switch (c.xc_type) {
401*dd23d762SRobert Mustacchi 		case X86_CACHE_TYPE_DATA:
402*dd23d762SRobert Mustacchi 			type = FM_CACHE_INFO_T_DATA;
403*dd23d762SRobert Mustacchi 			break;
404*dd23d762SRobert Mustacchi 		case X86_CACHE_TYPE_INST:
405*dd23d762SRobert Mustacchi 			type = FM_CACHE_INFO_T_INSTR;
406*dd23d762SRobert Mustacchi 			break;
407*dd23d762SRobert Mustacchi 		case X86_CACHE_TYPE_UNIFIED:
408*dd23d762SRobert Mustacchi 			type = FM_CACHE_INFO_T_DATA | FM_CACHE_INFO_T_INSTR |
409*dd23d762SRobert Mustacchi 			    FM_CACHE_INFO_T_UNIFIED;
410*dd23d762SRobert Mustacchi 			break;
411*dd23d762SRobert Mustacchi 		default:
412*dd23d762SRobert Mustacchi 			break;
413*dd23d762SRobert Mustacchi 		}
414*dd23d762SRobert Mustacchi 		fnvlist_add_uint32(caches[i], FM_CACHE_INFO_TYPE,
415*dd23d762SRobert Mustacchi 		    (uint32_t)type);
416*dd23d762SRobert Mustacchi 		fnvlist_add_uint64(caches[i], FM_CACHE_INFO_NSETS, c.xc_nsets);
417*dd23d762SRobert Mustacchi 		fnvlist_add_uint32(caches[i], FM_CACHE_INFO_NWAYS, c.xc_nways);
418*dd23d762SRobert Mustacchi 		fnvlist_add_uint32(caches[i], FM_CACHE_INFO_LINE_SIZE,
419*dd23d762SRobert Mustacchi 		    c.xc_line_size);
420*dd23d762SRobert Mustacchi 		fnvlist_add_uint64(caches[i], FM_CACHE_INFO_TOTAL_SIZE,
421*dd23d762SRobert Mustacchi 		    c.xc_size);
422*dd23d762SRobert Mustacchi 		if ((c.xc_flags & X86_CACHE_F_FULL_ASSOC) != 0) {
423*dd23d762SRobert Mustacchi 			fnvlist_add_boolean(caches[i],
424*dd23d762SRobert Mustacchi 			    FM_CACHE_INFO_FULLY_ASSOC);
425*dd23d762SRobert Mustacchi 		}
426*dd23d762SRobert Mustacchi 		fnvlist_add_uint64(caches[i], FM_CACHE_INFO_ID, c.xc_id);
427*dd23d762SRobert Mustacchi 		fnvlist_add_uint32(caches[i], FM_CACHE_INFO_X86_APIC_SHIFT,
428*dd23d762SRobert Mustacchi 		    c.xc_apic_shift);
429*dd23d762SRobert Mustacchi 	}
430*dd23d762SRobert Mustacchi 
431*dd23d762SRobert Mustacchi 	(void) snprintf(buf, sizeof (buf), "%u", cpuno);
432*dd23d762SRobert Mustacchi 	fnvlist_add_nvlist_array(nvl, buf, caches, (uint_t)ncache);
433*dd23d762SRobert Mustacchi 	ret = 0;
434*dd23d762SRobert Mustacchi 
435*dd23d762SRobert Mustacchi cleanup:
436*dd23d762SRobert Mustacchi 	for (uint32_t i = 0; i < ncache; i++) {
437*dd23d762SRobert Mustacchi 		nvlist_free(caches[i]);
438*dd23d762SRobert Mustacchi 	}
439*dd23d762SRobert Mustacchi 	return (ret);
440*dd23d762SRobert Mustacchi }
441*dd23d762SRobert Mustacchi 
442*dd23d762SRobert Mustacchi /*
443*dd23d762SRobert Mustacchi  * Gather all of the different per-CPU leaves and return them as a series of
444*dd23d762SRobert Mustacchi  * nvlists.
445*dd23d762SRobert Mustacchi  */
446*dd23d762SRobert Mustacchi int
fm_ioctl_cache_info(int cmd,nvlist_t * invl,nvlist_t ** onvlp)447*dd23d762SRobert Mustacchi fm_ioctl_cache_info(int cmd, nvlist_t *invl, nvlist_t **onvlp)
448*dd23d762SRobert Mustacchi {
449*dd23d762SRobert Mustacchi 	int ret = 0;
450*dd23d762SRobert Mustacchi 	fm_cmi_walk_t walk;
451*dd23d762SRobert Mustacchi 	nvlist_t *nvl;
452*dd23d762SRobert Mustacchi 
453*dd23d762SRobert Mustacchi 	if (cmd != FM_IOC_CACHE_INFO) {
454*dd23d762SRobert Mustacchi 		return (ENOTTY);
455*dd23d762SRobert Mustacchi 	}
456*dd23d762SRobert Mustacchi 
457*dd23d762SRobert Mustacchi 	walk_init(&walk, ANY_ID, ANY_ID, ANY_ID, NULL);
458*dd23d762SRobert Mustacchi 	cmi_hdl_walk(select_cmi_hdl, &walk, NULL, NULL);
459*dd23d762SRobert Mustacchi 	if (walk.nhdl == 0) {
460*dd23d762SRobert Mustacchi 		walk_fini(&walk);
461*dd23d762SRobert Mustacchi 		return (ENOENT);
462*dd23d762SRobert Mustacchi 	}
463*dd23d762SRobert Mustacchi 
464*dd23d762SRobert Mustacchi 	(void) nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP);
465*dd23d762SRobert Mustacchi 	fnvlist_add_uint32(nvl, FM_CACHE_INFO_NCPUS, walk.nhdl);
466*dd23d762SRobert Mustacchi 
467*dd23d762SRobert Mustacchi 	for (uint_t i = 0; i < walk.nhdl; i++) {
468*dd23d762SRobert Mustacchi 		if ((ret = fm_populate_cache(walk.hdls[i], nvl, i)) != 0) {
469*dd23d762SRobert Mustacchi 			break;
470*dd23d762SRobert Mustacchi 		}
471*dd23d762SRobert Mustacchi 		cmi_hdl_rele(walk.hdls[i]);
472*dd23d762SRobert Mustacchi 	}
473*dd23d762SRobert Mustacchi 	walk_fini(&walk);
474*dd23d762SRobert Mustacchi 
475*dd23d762SRobert Mustacchi 	if (ret == 0) {
476*dd23d762SRobert Mustacchi 		*onvlp = nvl;
477*dd23d762SRobert Mustacchi 	} else {
478*dd23d762SRobert Mustacchi 		nvlist_free(nvl);
479*dd23d762SRobert Mustacchi 	}
480*dd23d762SRobert Mustacchi 
481*dd23d762SRobert Mustacchi 	return (ret);
482*dd23d762SRobert Mustacchi }
483