|
root / base / usr / src / uts / common / sys / sensors.h
sensors.h C 133 lines 4.2 KB
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2019, Joyent, Inc.
 * Copyright 2024 Oxide Computer Company
 */

#ifndef _SYS_SENSORS_H
#define	_SYS_SENSORS_H

/*
 * Consolidated sensor ioctls for various parts of the operating system. These
 * interfaces should not be relied on at all. They are evolving and will change
 * as we add more to the system for this. This may eventually become a larger
 * framework, though it's more likely we'll consolidate that in userland.
 */

#ifdef __cplusplus
extern "C" {
#endif

/*
 * List of different possible kinds of sensors.
 */
#define	SENSOR_KIND_UNKNOWN		0x00
#define	SENSOR_KIND_TEMPERATURE		0x01
#define	SENSOR_KIND_VOLTAGE		0x02
#define	SENSOR_KIND_CURRENT		0x03
#define	SENSOR_KIND_SYNTHETIC		0x04

/*
 * Lists of units that sensors may have. The none type is intended for unitless
 * sensors such as general control sensors. These sensors are generally derived
 * from a secondary unit. A prime example is AMD's CPU control temperature,
 * which is a unitless measure that is derived from temperature.
 */
#define	SENSOR_UNIT_UNKNOWN		0x00
#define	SENSOR_UNIT_CELSIUS		0x01
#define	SENSOR_UNIT_FAHRENHEIT		0x02
#define	SENSOR_UNIT_KELVIN		0x03
#define	SENSOR_UNIT_VOLTS		0x04
#define	SENSOR_UNIT_AMPS		0x05
#define	SENSOR_UNIT_NONE		0x06

#define	SENSOR_IOCTL	(('s' << 24) | ('e' << 16) | ('n' << 8))

/*
 * Ask the sensor what kind of sensor it is.
 */
#define	SENSOR_IOCTL_KIND	(SENSOR_IOCTL | 0x01)

typedef struct sensor_ioctl_kind {
	uint64_t	sik_kind;
	uint64_t	sik_derive;
} sensor_ioctl_kind_t;

/*
 * Ask the sensor for a scalar measurement. The sensor is responsible for
 * returning the units it's in.  A scalar measurement is broken down into a
 * signed value and a notion of its granularity. The sit_gran member indicates
 * the granularity: the number of increments per unit in the measurement (the
 * sit_value member). sit_gran is signed and the sign indicates whether one
 * needs to multiply or divide the granularity. The sit_prec member describes a
 * +/- value (taking sit_gran into account) that describes the precision of the
 * sensor.
 *
 * For example, consider a temperature sensor that set sit_gran to 10. This
 * would mean that the value in sit_value was in 10ths of a degree and that to
 * get the actual value in degrees, one would divide by 10. On the other hand, a
 * negative value means that we effectively have to multiply to get there. For
 * example, a value of -2 would indicate that each value in sit_value indicated
 * two degrees and to get the temperature in degrees you would multiply
 * sit_value * by two.
 */
#define	SENSOR_IOCTL_SCALAR	(SENSOR_IOCTL | 0x02)

typedef struct sensor_ioctl_scalar {
	uint32_t	sis_unit;
	int32_t		sis_gran;
	uint32_t	sis_prec;
	uint32_t	sis_pad;
	int64_t		sis_value;
} sensor_ioctl_scalar_t;

#ifdef	_KERNEL
typedef int (*ksensor_kind_f)(void *, sensor_ioctl_kind_t *);
typedef int (*ksensor_scalar_f)(void *, sensor_ioctl_scalar_t *);

typedef struct {
	ksensor_kind_f		kso_kind;
	ksensor_scalar_f	kso_scalar;
} ksensor_ops_t;

extern int ksensor_kind_temperature(void *, sensor_ioctl_kind_t *);
extern int ksensor_kind_voltage(void *, sensor_ioctl_kind_t *);
extern int ksensor_kind_current(void *, sensor_ioctl_kind_t *);

/*
 * Create a sensor where the class and name is supplied.
 */
extern int ksensor_create(dev_info_t *, const ksensor_ops_t *, void *,
    const char *, const char *, id_t *);

/*
 * Create a scalar sensor for a PCI device. If this is not a device-wide
 * (e.g. per-function) sensor, this should not be used.
 */
extern int ksensor_create_scalar_pcidev(dev_info_t *, uint64_t,
    const ksensor_ops_t *, void *, const char *, id_t *);

/*
 * Remove a named or all sensors from this driver.
 */
#define	KSENSOR_ALL_IDS	INT_MIN
extern int ksensor_remove(dev_info_t *, id_t);

#endif

#ifdef __cplusplus
}
#endif

#endif /* _SYS_SENSORS_H */