2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <unistd.h>
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <strings.h>
2N/A#include <limits.h>
2N/A#include <alloca.h>
2N/A#include <kstat.h>
2N/A#include <errno.h>
2N/A#include <libnvpair.h>
2N/A#include <sys/types.h>
2N/A#include <sys/bitmap.h>
2N/A#include <sys/processor.h>
2N/A#include <sys/param.h>
2N/A#include <sys/fm/protocol.h>
2N/A#include <sys/systeminfo.h>
2N/A#include <fm/topo_mod.h>
2N/A
2N/A/*
2N/A * Enumerates the processing chips, or sockets, (as distinct from cores) in a
2N/A * system. For each chip found, the necessary nodes (one or more cores, and
2N/A * possibly a memory controller) are constructed underneath.
2N/A */
2N/A
2N/A#ifdef __cplusplus
2N/Aextern "C" {
2N/A#endif
2N/A
2N/A#define CHIP_VERSION TOPO_VERSION
2N/A#define CPU_NODE_NAME "cpu"
2N/A#define CHIP_NODE_NAME "chip"
2N/A
2N/Atypedef struct chip {
2N/A kstat_ctl_t *chip_kc;
2N/A kstat_t **chip_cpustats;
2N/A uint_t chip_ncpustats;
2N/A} chip_t;
2N/A
2N/Astatic int chip_enum(topo_mod_t *, tnode_t *, const char *, topo_instance_t,
2N/A topo_instance_t, void *, void *);
2N/A
2N/Astatic const topo_modops_t chip_ops =
2N/A { chip_enum, NULL};
2N/Astatic const topo_modinfo_t chip_info =
2N/A { "chip", FM_FMRI_SCHEME_HC, CHIP_VERSION, &chip_ops };
2N/A
2N/Aint
2N/A_topo_init(topo_mod_t *mod)
2N/A{
2N/A chip_t *chip;
2N/A
2N/A if (getenv("TOPOCHIPDBG"))
2N/A topo_mod_setdebug(mod);
2N/A topo_mod_dprintf(mod, "initializing chip enumerator\n");
2N/A
2N/A if ((chip = topo_mod_zalloc(mod, sizeof (chip_t))) == NULL)
2N/A return (-1);
2N/A
2N/A if ((chip->chip_kc = kstat_open()) == NULL) {
2N/A topo_mod_dprintf(mod, "kstat_open failed: %s\n",
2N/A strerror(errno));
2N/A topo_mod_free(mod, chip, sizeof (chip_t));
2N/A return (-1);
2N/A }
2N/A
2N/A chip->chip_ncpustats = sysconf(_SC_CPUID_MAX);
2N/A if ((chip->chip_cpustats = topo_mod_zalloc(mod, (
2N/A chip->chip_ncpustats + 1) * sizeof (kstat_t *))) == NULL) {
2N/A (void) kstat_close(chip->chip_kc);
2N/A topo_mod_free(mod, chip, sizeof (chip_t));
2N/A return (-1);
2N/A }
2N/A
2N/A if (topo_mod_register(mod, &chip_info, TOPO_VERSION) != 0) {
2N/A topo_mod_dprintf(mod, "failed to register hc: "
2N/A "%s\n", topo_mod_errmsg(mod));
2N/A topo_mod_free(mod, chip->chip_cpustats,
2N/A (chip->chip_ncpustats + 1) * sizeof (kstat_t *));
2N/A (void) kstat_close(chip->chip_kc);
2N/A topo_mod_free(mod, chip, sizeof (chip_t));
2N/A return (-1);
2N/A }
2N/A topo_mod_setspecific(mod, (void *)chip);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Avoid
2N/A_topo_fini(topo_mod_t *mod)
2N/A{
2N/A chip_t *chip;
2N/A
2N/A chip = topo_mod_getspecific(mod);
2N/A
2N/A if (chip->chip_cpustats != NULL)
2N/A topo_mod_free(mod, chip->chip_cpustats,
2N/A (chip->chip_ncpustats + 1) * sizeof (kstat_t *));
2N/A
2N/A (void) kstat_close(chip->chip_kc);
2N/A topo_mod_free(mod, chip, sizeof (chip_t));
2N/A
2N/A topo_mod_unregister(mod);
2N/A}
2N/A
2N/Astatic int
2N/Acpu_kstat_init(chip_t *chip, int i)
2N/A{
2N/A kstat_t *ksp;
2N/A
2N/A if (chip->chip_cpustats[i] == NULL) {
2N/A if ((ksp = kstat_lookup(chip->chip_kc, "cpu_info", i, NULL)) ==
2N/A NULL || kstat_read(chip->chip_kc, ksp, NULL) < 0)
2N/A return (-1);
2N/A
2N/A chip->chip_cpustats[i] = ksp;
2N/A } else {
2N/A ksp = chip->chip_cpustats[i];
2N/A }
2N/A
2N/A return (ksp->ks_instance);
2N/A}
2N/A
2N/Astatic nvlist_t *
2N/Acpu_fmri_create(topo_mod_t *mod, uint32_t cpuid, char *s, uint8_t cpumask)
2N/A{
2N/A int err;
2N/A nvlist_t *asru;
2N/A
2N/A if (topo_mod_nvalloc(mod, &asru, NV_UNIQUE_NAME) != 0)
2N/A return (NULL);
2N/A
2N/A err = nvlist_add_uint8(asru, FM_VERSION, FM_CPU_SCHEME_VERSION);
2N/A err |= nvlist_add_string(asru, FM_FMRI_SCHEME, FM_FMRI_SCHEME_CPU);
2N/A err |= nvlist_add_uint32(asru, FM_FMRI_CPU_ID, cpuid);
2N/A err |= nvlist_add_uint8(asru, FM_FMRI_CPU_MASK, cpumask);
2N/A if (s != NULL)
2N/A err |= nvlist_add_string(asru, FM_FMRI_CPU_SERIAL_ID, s);
2N/A if (err != 0) {
2N/A nvlist_free(asru);
2N/A (void) topo_mod_seterrno(mod, EMOD_FMRI_NVL);
2N/A return (NULL);
2N/A }
2N/A
2N/A return (asru);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic int
2N/Acpu_create(topo_mod_t *mod, tnode_t *rnode, const char *name,
2N/A topo_instance_t min, topo_instance_t max, chip_t *chip)
2N/A{
2N/A int i, err, chip_id, nerr = 0;
2N/A char *s, sbuf[21];
2N/A tnode_t *cnode;
2N/A kstat_named_t *ks, *kf;
2N/A nvlist_t *fmri, *asru;
2N/A nvlist_t *auth = topo_mod_auth(mod, rnode);
2N/A
2N/A /*
2N/A * Override what was created for us
2N/A */
2N/A topo_node_range_destroy(rnode, name);
2N/A if (topo_node_range_create(mod, rnode, name, 0, chip->chip_ncpustats)
2N/A < 0)
2N/A return (topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM));
2N/A
2N/A for (i = 0; i <= chip->chip_ncpustats; i++) {
2N/A
2N/A if ((chip_id = cpu_kstat_init(chip, i)) < 0)
2N/A continue;
2N/A
2N/A if ((ks = kstat_data_lookup(chip->chip_cpustats[i],
2N/A "device_ID")) != NULL) {
2N/A (void) snprintf(sbuf, 21, "%llX", ks->value.ui64);
2N/A s = sbuf;
2N/A } else {
2N/A s = NULL;
2N/A }
2N/A
2N/A fmri = topo_mod_hcfmri(mod, rnode, FM_HC_SCHEME_VERSION, name,
2N/A (topo_instance_t)chip_id, NULL, auth, NULL, NULL, s);
2N/A if (fmri == NULL || (cnode = topo_node_bind(mod,
2N/A rnode, name, i, fmri)) == NULL) {
2N/A ++nerr;
2N/A nvlist_free(fmri);
2N/A continue;
2N/A }
2N/A nvlist_free(fmri);
2N/A
2N/A if ((asru = cpu_fmri_create(mod, i, s, 0)) != NULL) {
2N/A (void) topo_node_asru_set(cnode, asru, 0, &err);
2N/A nvlist_free(asru);
2N/A } else {
2N/A ++nerr;
2N/A }
2N/A
2N/A /*
2N/A * We look for a cpu_fru kstat. If one is available and
2N/A * it contains something useful, use it as the label and
2N/A * and the FRU.
2N/A *
2N/A * This is a problem for platforms that do not properly
2N/A * support the cpu_fru kstat like Ontario or if
2N/A * we start exporting a different type of FRU label
2N/A */
2N/A if ((kf = kstat_data_lookup(chip->chip_cpustats[i], "cpu_fru"))
2N/A != NULL && strcmp(KSTAT_NAMED_STR_PTR(kf),
2N/A "hc:///component=") != 0) {
2N/A nvlist_t *fru;
2N/A char *lp;
2N/A
2N/A if (topo_mod_str2nvl(mod, KSTAT_NAMED_STR_PTR(kf),
2N/A &fru) == 0) {
2N/A (void) topo_node_fru_set(cnode, fru, 0, &err);
2N/A nvlist_free(fru);
2N/A }
2N/A
2N/A if ((lp = strchr(KSTAT_NAMED_STR_PTR(kf), '='))
2N/A == NULL) {
2N/A (void) topo_node_label_set(cnode, NULL, &err);
2N/A } else {
2N/A ++lp;
2N/A (void) topo_node_label_set(cnode, lp, &err);
2N/A }
2N/A } else {
2N/A (void) topo_node_label_set(cnode, NULL, &err);
2N/A }
2N/A }
2N/A
2N/A nvlist_free(auth);
2N/A
2N/A if (nerr != 0)
2N/A return (topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM));
2N/A else
2N/A return (0);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic int
2N/Achip_enum(topo_mod_t *mod, tnode_t *rnode, const char *name,
2N/A topo_instance_t min, topo_instance_t max, void *arg, void *notused)
2N/A{
2N/A chip_t *chip = (chip_t *)arg;
2N/A
2N/A if (strcmp(name, CPU_NODE_NAME) == 0)
2N/A return (cpu_create(mod, rnode, name, min, max, chip));
2N/A
2N/A return (0);
2N/A}