4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright (c) 2018, Joyent, Inc.
25 * Copyright 2023 Oxide Computer Company
29 #include <sys/types.h>
32 #include <sys/fm/protocol.h>
33 #include <sys/fm/smb/fmsmb.h>
34 #include <sys/devfm.h>
36 #include <sys/cpu_module.h>
38 #define ANY_ID (uint_t)-1
41 * INIT_HDLS is the initial size of cmi_hdl_t array. We fill the array
42 * during cmi_hdl_walk, if the array overflows, we will reallocate
43 * a new array twice the size of the old one.
47 typedef struct fm_cmi_walk_t
49 uint_t chipid
; /* chipid to match during walk */
50 uint_t coreid
; /* coreid to match */
51 uint_t strandid
; /* strandid to match */
52 int (*cbfunc
)(cmi_hdl_t
, void *, void *); /* callback function */
53 cmi_hdl_t
*hdls
; /* allocated array to save the handles */
54 uint_t nhdl_max
; /* allocated array size */
55 uint_t nhdl
; /* handles saved */
58 extern int x86gentopo_legacy
;
61 fm_get_paddr(nvlist_t
*nvl
, uint64_t *paddr
)
68 /* Verify FMRI scheme name and version number */
69 if ((nvlist_lookup_string(nvl
, FM_FMRI_SCHEME
, &scheme
) != 0) ||
70 (strcmp(scheme
, FM_FMRI_SCHEME_HC
) != 0) ||
71 (nvlist_lookup_uint8(nvl
, FM_VERSION
, &version
) != 0) ||
72 version
> FM_HC_SCHEME_VERSION
) {
76 if ((err
= cmi_mc_unumtopa(NULL
, nvl
, &pa
)) != CMI_SUCCESS
&&
77 err
!= CMIERR_MC_PARTIALUNUMTOPA
)
85 * Routines for cmi handles walk.
89 walk_init(fm_cmi_walk_t
*wp
, uint_t chipid
, uint_t coreid
, uint_t strandid
,
90 int (*cbfunc
)(cmi_hdl_t
, void *, void *))
94 wp
->strandid
= strandid
;
96 * If callback is not set, we allocate an array to save the
99 if ((wp
->cbfunc
= cbfunc
) == NULL
) {
100 wp
->hdls
= kmem_alloc(sizeof (cmi_hdl_t
) * INIT_HDLS
, KM_SLEEP
);
101 wp
->nhdl_max
= INIT_HDLS
;
107 walk_fini(fm_cmi_walk_t
*wp
)
109 if (wp
->cbfunc
== NULL
)
110 kmem_free(wp
->hdls
, sizeof (cmi_hdl_t
) * wp
->nhdl_max
);
114 select_cmi_hdl(cmi_hdl_t hdl
, void *arg1
, void *arg2
, void *arg3
)
116 fm_cmi_walk_t
*wp
= (fm_cmi_walk_t
*)arg1
;
118 if (wp
->chipid
!= ANY_ID
&& wp
->chipid
!= cmi_hdl_chipid(hdl
))
119 return (CMI_HDL_WALK_NEXT
);
120 if (wp
->coreid
!= ANY_ID
&& wp
->coreid
!= cmi_hdl_coreid(hdl
))
121 return (CMI_HDL_WALK_NEXT
);
122 if (wp
->strandid
!= ANY_ID
&& wp
->strandid
!= cmi_hdl_strandid(hdl
))
123 return (CMI_HDL_WALK_NEXT
);
126 * Call the callback function if any exists, otherwise we hold a
127 * reference of the handle and push it to preallocated array.
128 * If the allocated array is going to overflow, reallocate a
129 * bigger one to replace it.
131 if (wp
->cbfunc
!= NULL
)
132 return (wp
->cbfunc(hdl
, arg2
, arg3
));
134 if (wp
->nhdl
== wp
->nhdl_max
) {
135 size_t sz
= sizeof (cmi_hdl_t
) * wp
->nhdl_max
;
136 cmi_hdl_t
*newarray
= kmem_alloc(sz
<< 1, KM_SLEEP
);
138 bcopy(wp
->hdls
, newarray
, sz
);
139 kmem_free(wp
->hdls
, sz
);
145 wp
->hdls
[wp
->nhdl
++] = hdl
;
147 return (CMI_HDL_WALK_NEXT
);
151 populate_cpu(nvlist_t
**nvlp
, cmi_hdl_t hdl
)
157 (void) nvlist_alloc(nvlp
, NV_UNIQUE_NAME
, KM_SLEEP
);
160 * If SMBIOS satisfies FMA Topology needs, gather
161 * more information on the chip's physical roots
162 * like /chassis=x/motherboard=y/cpuboard=z and
163 * set the chip_id to match the SMBIOS' Type 4
164 * ordering & this has to match the ereport's chip
165 * resource instance derived off of SMBIOS.
166 * Multi-Chip-Module support should set the chipid
167 * in terms of the processor package rather than
168 * the die/node in the processor package, for FM.
171 if (!x86gentopo_legacy
) {
172 smbios_id
= cmi_hdl_smbiosid(hdl
);
173 fm_chipid
= cmi_hdl_smb_chipid(hdl
);
174 (void) nvlist_add_nvlist(*nvlp
, FM_PHYSCPU_INFO_CHIP_ROOTS
,
175 cmi_hdl_smb_bboard(hdl
));
176 (void) nvlist_add_uint16(*nvlp
, FM_PHYSCPU_INFO_SMBIOS_ID
,
177 (uint16_t)smbios_id
);
179 fm_chipid
= cmi_hdl_chipid(hdl
);
181 fm_payload_set(*nvlp
,
182 FM_PHYSCPU_INFO_VENDOR_ID
, DATA_TYPE_STRING
,
183 cmi_hdl_vendorstr(hdl
),
184 FM_PHYSCPU_INFO_FAMILY
, DATA_TYPE_INT32
,
185 (int32_t)cmi_hdl_family(hdl
),
186 FM_PHYSCPU_INFO_MODEL
, DATA_TYPE_INT32
,
187 (int32_t)cmi_hdl_model(hdl
),
188 FM_PHYSCPU_INFO_STEPPING
, DATA_TYPE_INT32
,
189 (int32_t)cmi_hdl_stepping(hdl
),
190 FM_PHYSCPU_INFO_CHIP_ID
, DATA_TYPE_INT32
,
192 FM_PHYSCPU_INFO_NPROCNODES
, DATA_TYPE_INT32
,
193 (int32_t)cmi_hdl_procnodes_per_pkg(hdl
),
194 FM_PHYSCPU_INFO_PROCNODE_ID
, DATA_TYPE_INT32
,
195 (int32_t)cmi_hdl_procnodeid(hdl
),
196 FM_PHYSCPU_INFO_CORE_ID
, DATA_TYPE_INT32
,
197 (int32_t)cmi_hdl_coreid(hdl
),
198 FM_PHYSCPU_INFO_STRAND_ID
, DATA_TYPE_INT32
,
199 (int32_t)cmi_hdl_strandid(hdl
),
200 FM_PHYSCPU_INFO_STRAND_APICID
, DATA_TYPE_INT32
,
201 (int32_t)cmi_hdl_strand_apicid(hdl
),
202 FM_PHYSCPU_INFO_CHIP_REV
, DATA_TYPE_STRING
,
203 cmi_hdl_chiprevstr(hdl
),
204 FM_PHYSCPU_INFO_SOCKET_TYPE
, DATA_TYPE_UINT32
,
205 (uint32_t)cmi_hdl_getsockettype(hdl
),
206 FM_PHYSCPU_INFO_CPU_ID
, DATA_TYPE_INT32
,
207 (int32_t)cmi_hdl_logical_id(hdl
),
211 * Do this separately so that way if there is no ident string we do not
214 if ((idstr
= cmi_hdl_chipident(hdl
)) != NULL
) {
215 fm_payload_set(*nvlp
,
216 FM_PHYSCPU_INFO_CHIP_IDENTSTR
, DATA_TYPE_STRING
, idstr
,
223 fm_ioctl_physcpu_info(int cmd
, nvlist_t
*invl
, nvlist_t
**onvlp
)
225 nvlist_t
**cpus
, *nvl
;
230 * Do a walk to save all the cmi handles in the array.
232 walk_init(&wk
, ANY_ID
, ANY_ID
, ANY_ID
, NULL
);
233 cmi_hdl_walk(select_cmi_hdl
, &wk
, NULL
, NULL
);
240 cpus
= kmem_alloc(sizeof (nvlist_t
*) * wk
.nhdl
, KM_SLEEP
);
241 for (i
= 0; i
< wk
.nhdl
; i
++) {
242 populate_cpu(cpus
+ i
, wk
.hdls
[i
]);
243 cmi_hdl_rele(wk
.hdls
[i
]);
248 (void) nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
);
249 err
= nvlist_add_nvlist_array(nvl
, FM_PHYSCPU_INFO_CPUS
,
252 for (i
= 0; i
< wk
.nhdl
; i
++)
253 nvlist_free(cpus
[i
]);
254 kmem_free(cpus
, sizeof (nvlist_t
*) * wk
.nhdl
);
266 fm_ioctl_cpu_retire(int cmd
, nvlist_t
*invl
, nvlist_t
**onvlp
)
268 int32_t chipid
, coreid
, strandid
;
269 int rc
, new_status
, old_status
;
274 case FM_IOC_CPU_RETIRE
:
275 new_status
= P_FAULTED
;
277 case FM_IOC_CPU_STATUS
:
278 new_status
= P_STATUS
;
280 case FM_IOC_CPU_UNRETIRE
:
281 new_status
= P_ONLINE
;
287 if (nvlist_lookup_int32(invl
, FM_CPU_RETIRE_CHIP_ID
, &chipid
) != 0 ||
288 nvlist_lookup_int32(invl
, FM_CPU_RETIRE_CORE_ID
, &coreid
) != 0 ||
289 nvlist_lookup_int32(invl
, FM_CPU_RETIRE_STRAND_ID
, &strandid
) != 0)
292 hdl
= cmi_hdl_lookup(CMI_HDL_NEUTRAL
, chipid
, coreid
, strandid
);
296 rc
= cmi_hdl_online(hdl
, new_status
, &old_status
);
300 (void) nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
);
301 (void) nvlist_add_int32(nvl
, FM_CPU_RETIRE_OLDSTATUS
,
310 * Retrun the value of x86gentopo_legacy variable as an nvpair.
312 * The caller is responsible for freeing the nvlist.
316 fm_ioctl_gentopo_legacy(int cmd
, nvlist_t
*invl
, nvlist_t
**onvlp
)
320 if (cmd
!= FM_IOC_GENTOPO_LEGACY
) {
325 * Inform the caller of the intentions of the ereport generators to
326 * generate either a "generic" or "legacy" x86 topology.
329 (void) nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
);
330 (void) nvlist_add_int32(nvl
, FM_GENTOPO_LEGACY
, x86gentopo_legacy
);
337 * This is an internal bound on the maximum number of caches that we expect to
338 * encounter to reduce dynamic allocation.
340 #define FM_MAX_CACHES 0x10
343 fm_cmi_cache_err_to_errno(cmi_errno_t cmi
)
346 case CMIERR_C_NODATA
:
349 * Right now, CMIERR_C_BADCACHENO is explicitly not mapped to EINVAL
350 * (which is what it maps to in cmi_hw.c.). This discrepancy exists
351 * because there's nothing in a user request here that'd end up
352 * resulting in an invalid value, it can only occur because we asked
353 * for a cache that we were told exists, but doesn't actually. If we
354 * returned EINVAL, the user would be wondering what was invalid about
357 case CMIERR_C_BADCACHENO
:
364 fm_populate_cache(cmi_hdl_t hdl
, nvlist_t
*nvl
, uint_t cpuno
)
369 nvlist_t
*caches
[FM_MAX_CACHES
];
372 err
= cmi_cache_ncaches(hdl
, &ncache
);
373 if (err
!= CMI_SUCCESS
) {
374 return (fm_cmi_cache_err_to_errno(err
));
378 * Our promise to userland is that if we skip a value here then there
383 } else if (ncache
> FM_MAX_CACHES
) {
387 bzero(caches
, sizeof (caches
));
388 for (uint32_t i
= 0; i
< ncache
; i
++) {
390 fm_cache_info_type_t type
= 0;
392 (void) nvlist_alloc(&caches
[i
], NV_UNIQUE_NAME
, KM_SLEEP
);
393 err
= cmi_cache_info(hdl
, i
, &c
);
394 if (err
!= CMI_SUCCESS
) {
395 ret
= fm_cmi_cache_err_to_errno(err
);
399 fnvlist_add_uint32(caches
[i
], FM_CACHE_INFO_LEVEL
, c
.xc_level
);
401 case X86_CACHE_TYPE_DATA
:
402 type
= FM_CACHE_INFO_T_DATA
;
404 case X86_CACHE_TYPE_INST
:
405 type
= FM_CACHE_INFO_T_INSTR
;
407 case X86_CACHE_TYPE_UNIFIED
:
408 type
= FM_CACHE_INFO_T_DATA
| FM_CACHE_INFO_T_INSTR
|
409 FM_CACHE_INFO_T_UNIFIED
;
414 fnvlist_add_uint32(caches
[i
], FM_CACHE_INFO_TYPE
,
416 fnvlist_add_uint64(caches
[i
], FM_CACHE_INFO_NSETS
, c
.xc_nsets
);
417 fnvlist_add_uint32(caches
[i
], FM_CACHE_INFO_NWAYS
, c
.xc_nways
);
418 fnvlist_add_uint32(caches
[i
], FM_CACHE_INFO_LINE_SIZE
,
420 fnvlist_add_uint64(caches
[i
], FM_CACHE_INFO_TOTAL_SIZE
,
422 if ((c
.xc_flags
& X86_CACHE_F_FULL_ASSOC
) != 0) {
423 fnvlist_add_boolean(caches
[i
],
424 FM_CACHE_INFO_FULLY_ASSOC
);
426 fnvlist_add_uint64(caches
[i
], FM_CACHE_INFO_ID
, c
.xc_id
);
427 fnvlist_add_uint32(caches
[i
], FM_CACHE_INFO_X86_APIC_SHIFT
,
431 (void) snprintf(buf
, sizeof (buf
), "%u", cpuno
);
432 fnvlist_add_nvlist_array(nvl
, buf
, caches
, (uint_t
)ncache
);
436 for (uint32_t i
= 0; i
< ncache
; i
++) {
437 nvlist_free(caches
[i
]);
443 * Gather all of the different per-CPU leaves and return them as a series of
447 fm_ioctl_cache_info(int cmd
, nvlist_t
*invl
, nvlist_t
**onvlp
)
453 if (cmd
!= FM_IOC_CACHE_INFO
) {
457 walk_init(&walk
, ANY_ID
, ANY_ID
, ANY_ID
, NULL
);
458 cmi_hdl_walk(select_cmi_hdl
, &walk
, NULL
, NULL
);
459 if (walk
.nhdl
== 0) {
464 (void) nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
);
465 fnvlist_add_uint32(nvl
, FM_CACHE_INFO_NCPUS
, walk
.nhdl
);
467 for (uint_t i
= 0; i
< walk
.nhdl
; i
++) {
468 if ((ret
= fm_populate_cache(walk
.hdls
[i
], nvl
, i
)) != 0) {
471 cmi_hdl_rele(walk
.hdls
[i
]);