1f2dbfd32SRobert Mustacchi /*
2f2dbfd32SRobert Mustacchi  * This file and its contents are supplied under the terms of the
3f2dbfd32SRobert Mustacchi  * Common Development and Distribution License ("CDDL"), version 1.0.
4f2dbfd32SRobert Mustacchi  * You may only use this file in accordance with the terms of version
5f2dbfd32SRobert Mustacchi  * 1.0 of the CDDL.
6f2dbfd32SRobert Mustacchi  *
7f2dbfd32SRobert Mustacchi  * A full copy of the text of the CDDL should have accompanied this
8f2dbfd32SRobert Mustacchi  * source.  A copy of the CDDL is also available via the Internet at
9f2dbfd32SRobert Mustacchi  * http://www.illumos.org/license/CDDL.
10f2dbfd32SRobert Mustacchi  */
11f2dbfd32SRobert Mustacchi 
12f2dbfd32SRobert Mustacchi /*
13f2dbfd32SRobert Mustacchi  * Copyright 2019, Joyent, Inc.
14f2dbfd32SRobert Mustacchi  */
15f2dbfd32SRobert Mustacchi 
16f2dbfd32SRobert Mustacchi #include <sys/types.h>
17f2dbfd32SRobert Mustacchi #include <sys/stat.h>
18f2dbfd32SRobert Mustacchi #include <fcntl.h>
19f2dbfd32SRobert Mustacchi #include <stdio.h>
20f2dbfd32SRobert Mustacchi #include <string.h>
21f2dbfd32SRobert Mustacchi #include <unistd.h>
22f2dbfd32SRobert Mustacchi #include <libnvpair.h>
23f2dbfd32SRobert Mustacchi #include <sys/sensors.h>
24f2dbfd32SRobert Mustacchi #include <sys/fm/protocol.h>
25f2dbfd32SRobert Mustacchi #include <fm/topo_mod.h>
26f2dbfd32SRobert Mustacchi #include <topo_sensor.h>
27f2dbfd32SRobert Mustacchi 
28f2dbfd32SRobert Mustacchi #include "chip.h"
29f2dbfd32SRobert Mustacchi 
30f2dbfd32SRobert Mustacchi static const char *chip_sensor_base = "/dev/sensors/temperature/cpu";
31f2dbfd32SRobert Mustacchi 
32f2dbfd32SRobert Mustacchi int
chip_create_core_temp_sensor(topo_mod_t * mod,tnode_t * pnode)33f2dbfd32SRobert Mustacchi chip_create_core_temp_sensor(topo_mod_t *mod, tnode_t *pnode)
34f2dbfd32SRobert Mustacchi {
35f2dbfd32SRobert Mustacchi 	int err;
36f2dbfd32SRobert Mustacchi 	int32_t chip, core;
37f2dbfd32SRobert Mustacchi 	char buf[PATH_MAX];
38f2dbfd32SRobert Mustacchi 	struct stat st;
39f2dbfd32SRobert Mustacchi 
40f2dbfd32SRobert Mustacchi 	core = topo_node_instance(pnode);
41f2dbfd32SRobert Mustacchi 	if (topo_prop_get_int32(pnode, PGNAME(CORE), CORE_CHIP_ID, &chip,
42f2dbfd32SRobert Mustacchi 	    &err) != 0) {
43f2dbfd32SRobert Mustacchi 		return (topo_mod_seterrno(mod, err));
44f2dbfd32SRobert Mustacchi 	}
45f2dbfd32SRobert Mustacchi 
46f2dbfd32SRobert Mustacchi 	if (snprintf(buf, sizeof (buf), "%s/chip%d.core%d", chip_sensor_base,
47f2dbfd32SRobert Mustacchi 	    chip, core) >= sizeof (buf)) {
48f2dbfd32SRobert Mustacchi 		return (topo_mod_seterrno(mod, EMOD_UNKNOWN));
49f2dbfd32SRobert Mustacchi 	}
50f2dbfd32SRobert Mustacchi 
51f2dbfd32SRobert Mustacchi 	/*
52f2dbfd32SRobert Mustacchi 	 * Some systems have per-core sensors. Others have it on a per-die aka
53f2dbfd32SRobert Mustacchi 	 * procnode basis. Check to see if the file exists before we attempt to
54f2dbfd32SRobert Mustacchi 	 * do something.
55f2dbfd32SRobert Mustacchi 	 */
56f2dbfd32SRobert Mustacchi 	if (stat(buf, &st) != 0) {
57f2dbfd32SRobert Mustacchi 		int32_t procnode;
58f2dbfd32SRobert Mustacchi 
59f2dbfd32SRobert Mustacchi 		if (errno != ENOENT) {
60f2dbfd32SRobert Mustacchi 			return (topo_mod_seterrno(mod, EMOD_UNKNOWN));
61f2dbfd32SRobert Mustacchi 		}
62f2dbfd32SRobert Mustacchi 
63f2dbfd32SRobert Mustacchi 		if (topo_prop_get_int32(pnode, PGNAME(CORE), CORE_PROCNODE_ID,
64f2dbfd32SRobert Mustacchi 		    &procnode, &err) != 0) {
65f2dbfd32SRobert Mustacchi 			return (topo_mod_seterrno(mod, err));
66f2dbfd32SRobert Mustacchi 		}
67f2dbfd32SRobert Mustacchi 
68f2dbfd32SRobert Mustacchi 		if (snprintf(buf, sizeof (buf), "%s/procnode.%d",
69f2dbfd32SRobert Mustacchi 		    chip_sensor_base, procnode) >= sizeof (buf)) {
70f2dbfd32SRobert Mustacchi 			return (topo_mod_seterrno(mod, EMOD_UNKNOWN));
71f2dbfd32SRobert Mustacchi 		}
72f2dbfd32SRobert Mustacchi 	}
73f2dbfd32SRobert Mustacchi 
74*1045e13aSRobert Mustacchi 	return (topo_sensor_create_scalar_sensor(mod, pnode, buf, "temp"));
75f2dbfd32SRobert Mustacchi }
76f2dbfd32SRobert Mustacchi 
77f2dbfd32SRobert Mustacchi int
chip_create_chip_temp_sensor(topo_mod_t * mod,tnode_t * pnode)78f2dbfd32SRobert Mustacchi chip_create_chip_temp_sensor(topo_mod_t *mod, tnode_t *pnode)
79f2dbfd32SRobert Mustacchi {
80f2dbfd32SRobert Mustacchi 	int32_t chip;
81f2dbfd32SRobert Mustacchi 	char buf[PATH_MAX];
82f2dbfd32SRobert Mustacchi 
83f2dbfd32SRobert Mustacchi 	chip = topo_node_instance(pnode);
84f2dbfd32SRobert Mustacchi 
85f2dbfd32SRobert Mustacchi 	if (snprintf(buf, sizeof (buf), "%s/chip%d", chip_sensor_base,
86f2dbfd32SRobert Mustacchi 	    chip) >= sizeof (buf)) {
87f2dbfd32SRobert Mustacchi 		return (topo_mod_seterrno(mod, EMOD_UNKNOWN));
88f2dbfd32SRobert Mustacchi 	}
89f2dbfd32SRobert Mustacchi 
90*1045e13aSRobert Mustacchi 	return (topo_sensor_create_scalar_sensor(mod, pnode, buf, "temp"));
91f2dbfd32SRobert Mustacchi }
92