mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
* hwmon: close unregister drain race and add CONFIG_HWMON=n register_dev stubs * hwmon: preserve failure invalidation and return -ENODATA on removal
856 lines
28 KiB
C
856 lines
28 KiB
C
// SPDX-License-Identifier: MIT
|
|
/*
|
|
* Native Linux /sys/class/hwmon interface for nvidia_rs.ko.
|
|
*
|
|
* Exposes standard lm-sensors / btop / nvtop / Prometheus node_exporter channels:
|
|
* - temp1_input (millidegrees C), temp1_max (slowdown mC), temp1_crit (shutdown mC)
|
|
* - power1_average (microwatts), power1_cap (active TGP limit microwatts)
|
|
* - in0_input (millivolts)
|
|
* plus live GPU clock, utilization, and P-state sysfs attributes:
|
|
* - gpc_clock_mhz, mem_clock_mhz, gpu_util_pct, mem_util_pct, pstate
|
|
*
|
|
* On GA10x+ GSP-RM GPUs, reading any sensor transparently refreshes telemetry from
|
|
* GSP-RM (rate-limited by `hwmon_poll_ms`, default 500 ms, 0 = passive NVML mirror only),
|
|
* making /sys/class/hwmon standalone without requiring an NVML daemon.
|
|
*/
|
|
#include <linux/atomic.h>
|
|
#include <linux/build_bug.h>
|
|
#include <linux/device.h>
|
|
#include <linux/hwmon.h>
|
|
#include <linux/hwmon-sysfs.h>
|
|
#include <linux/jiffies.h>
|
|
#include <linux/limits.h>
|
|
#include <linux/minmax.h>
|
|
#include <linux/moduleparam.h>
|
|
#include <linux/mutex.h>
|
|
#include <linux/pci.h>
|
|
#include <linux/spinlock.h>
|
|
#include <linux/sysfs.h>
|
|
#include "hwmon_adapter.h"
|
|
|
|
#define NV_RS_HWMON_MAX_GPUS 32
|
|
#define NV_RS_HWMON_POLL_MAX_MS 60000u
|
|
/* Served-age bounds. Both are below the 2.5 s acquisition window the
|
|
* owner's thermal controller accepts (its stale threshold), so a
|
|
* successful hwmon read can satisfy the controller's freshness
|
|
* contract. The bounds are never extended to bridge a long polling
|
|
* interval: a configured hwmon_poll_ms above the active bound
|
|
* legitimately returns -ENODATA between eligible acquisitions rather
|
|
* than serving stale data. */
|
|
#define NV_RS_HWMON_MIRROR_MAX_AGE_MS 2000u
|
|
#define NV_RS_HWMON_ACTIVE_MAX_AGE_MS 2000u
|
|
#define NV_RS_HWMON_LIVE_MASK \
|
|
(NV_RS_HWMON_VALID_TEMP | NV_RS_HWMON_VALID_POWER | \
|
|
NV_RS_HWMON_VALID_VOLT | NV_RS_HWMON_VALID_GPC_CLK | \
|
|
NV_RS_HWMON_VALID_MEM_CLK | NV_RS_HWMON_VALID_GPU_UTIL | \
|
|
NV_RS_HWMON_VALID_MEM_UTIL | NV_RS_HWMON_VALID_PSTATE)
|
|
|
|
enum nv_rs_hwmon_live_channel {
|
|
NV_RS_HWMON_LIVE_TEMP = 0,
|
|
NV_RS_HWMON_LIVE_POWER,
|
|
NV_RS_HWMON_LIVE_VOLT,
|
|
NV_RS_HWMON_LIVE_GPC_CLK,
|
|
NV_RS_HWMON_LIVE_MEM_CLK,
|
|
NV_RS_HWMON_LIVE_GPU_UTIL,
|
|
NV_RS_HWMON_LIVE_MEM_UTIL,
|
|
NV_RS_HWMON_LIVE_PSTATE,
|
|
NV_RS_HWMON_LIVE_COUNT,
|
|
};
|
|
|
|
static const u32 nv_rs_hwmon_live_bits[NV_RS_HWMON_LIVE_COUNT] = {
|
|
NV_RS_HWMON_VALID_TEMP, NV_RS_HWMON_VALID_POWER,
|
|
NV_RS_HWMON_VALID_VOLT, NV_RS_HWMON_VALID_GPC_CLK,
|
|
NV_RS_HWMON_VALID_MEM_CLK, NV_RS_HWMON_VALID_GPU_UTIL,
|
|
NV_RS_HWMON_VALID_MEM_UTIL, NV_RS_HWMON_VALID_PSTATE,
|
|
};
|
|
|
|
static_assert(sizeof(struct nv_rs_hwmon_sample) == 56,
|
|
"nv_rs_hwmon_sample ABI mismatch with Rust HwmonSample");
|
|
|
|
/*
|
|
* CONFIG_HWMON is optional: Debian's cloud-amd64 kernel ships with it unset, and
|
|
* hwmon_device_register_with_info()/hwmon_device_unregister() are then not built
|
|
* at all, so modpost fails on the module. The telemetry plumbing below is a
|
|
* convenience view over RM data that already exists -- compile it out and let the
|
|
* Rust side mirror telemetry into nothing rather than refusing to build.
|
|
*/
|
|
#if IS_ENABLED(CONFIG_HWMON)
|
|
|
|
static unsigned int hwmon_poll_ms = 500;
|
|
module_param(hwmon_poll_ms, uint, 0644);
|
|
MODULE_PARM_DESC(hwmon_poll_ms,
|
|
"Minimum interval (ms) between active GSP-RM hwmon polls on sysfs read (default 500, 0 = passive mirror only; intervals above the 2 s served-age bound can return -ENODATA between polls once cached samples expire - freshness is never extended to bridge the gap)");
|
|
|
|
extern void nv_rs_rmapi_node_lock(void);
|
|
extern void nv_rs_rmapi_node_unlock(void);
|
|
extern int nv_rs_ga10x_hwmon_poll(u32 gpu_id, struct nv_rs_hwmon_sample *out);
|
|
|
|
struct nv_rs_hwmon_slot {
|
|
bool in_use;
|
|
bool removing;
|
|
u32 gpu_id;
|
|
struct device *dev;
|
|
struct device *hwmon_dev;
|
|
struct mutex poll_lock;
|
|
spinlock_t sample_lock;
|
|
u64 mirror_seq;
|
|
bool has_poll;
|
|
bool attempted;
|
|
unsigned long last_poll_jiffies;
|
|
unsigned long live_stamps[NV_RS_HWMON_LIVE_COUNT];
|
|
u64 pub_seq[NV_RS_HWMON_LIVE_COUNT];
|
|
atomic_t valid_mask;
|
|
atomic_t temp_mc;
|
|
atomic_t temp_max_mc;
|
|
atomic_t temp_crit_mc;
|
|
atomic64_t power_uw;
|
|
atomic64_t power_cap_uw;
|
|
atomic_t volt_mv;
|
|
atomic_t gpc_mhz;
|
|
atomic_t mem_mhz;
|
|
atomic_t gpu_util_pct;
|
|
atomic_t mem_util_pct;
|
|
atomic_t pstate;
|
|
};
|
|
|
|
static DEFINE_MUTEX(nv_rs_hwmon_lock);
|
|
static struct nv_rs_hwmon_slot nv_rs_hwmon_slots[NV_RS_HWMON_MAX_GPUS];
|
|
|
|
static u32 nv_rs_hwmon_gpu_id(struct pci_dev *pdev)
|
|
{
|
|
return (((u32)pci_domain_nr(pdev->bus) & 0xffffu) << 16) |
|
|
((u32)pdev->bus->number << 8) |
|
|
(u32)PCI_SLOT(pdev->devfn);
|
|
}
|
|
|
|
static void nv_rs_hwmon_apply_sample(struct nv_rs_hwmon_slot *slot,
|
|
const struct nv_rs_hwmon_sample *s)
|
|
{
|
|
u32 m = s->valid_mask;
|
|
|
|
if (!m)
|
|
return;
|
|
if (m & NV_RS_HWMON_VALID_TEMP)
|
|
atomic_set(&slot->temp_mc, s->temp_mc);
|
|
if (m & NV_RS_HWMON_VALID_TEMP_MAX)
|
|
atomic_set(&slot->temp_max_mc, s->temp_max_mc);
|
|
if (m & NV_RS_HWMON_VALID_TEMP_CRIT)
|
|
atomic_set(&slot->temp_crit_mc, s->temp_crit_mc);
|
|
if (m & NV_RS_HWMON_VALID_POWER)
|
|
atomic64_set(&slot->power_uw, (s64)s->power_uw);
|
|
if (m & NV_RS_HWMON_VALID_POWER_CAP)
|
|
atomic64_set(&slot->power_cap_uw, (s64)s->power_cap_uw);
|
|
if (m & NV_RS_HWMON_VALID_VOLT)
|
|
atomic_set(&slot->volt_mv, (int)s->volt_mv);
|
|
if (m & NV_RS_HWMON_VALID_GPC_CLK)
|
|
atomic_set(&slot->gpc_mhz, (int)s->gpc_mhz);
|
|
if (m & NV_RS_HWMON_VALID_MEM_CLK)
|
|
atomic_set(&slot->mem_mhz, (int)s->mem_mhz);
|
|
if (m & NV_RS_HWMON_VALID_GPU_UTIL)
|
|
atomic_set(&slot->gpu_util_pct, (int)s->gpu_util_pct);
|
|
if (m & NV_RS_HWMON_VALID_MEM_UTIL)
|
|
atomic_set(&slot->mem_util_pct, (int)s->mem_util_pct);
|
|
if (m & NV_RS_HWMON_VALID_PSTATE)
|
|
atomic_set(&slot->pstate, (int)s->pstate);
|
|
atomic_or((int)m, &slot->valid_mask);
|
|
}
|
|
|
|
static unsigned long nv_rs_hwmon_live_ttl_jiffies(unsigned int poll_ms)
|
|
{
|
|
unsigned long active_ms;
|
|
|
|
if (poll_ms == 0)
|
|
return msecs_to_jiffies(NV_RS_HWMON_MIRROR_MAX_AGE_MS);
|
|
if (poll_ms > NV_RS_HWMON_POLL_MAX_MS)
|
|
poll_ms = NV_RS_HWMON_POLL_MAX_MS;
|
|
active_ms = 2u * poll_ms + 1000u;
|
|
if (active_ms > NV_RS_HWMON_ACTIVE_MAX_AGE_MS)
|
|
active_ms = NV_RS_HWMON_ACTIVE_MAX_AGE_MS;
|
|
return msecs_to_jiffies(active_ms);
|
|
}
|
|
|
|
/* A live reading is only served while the last publication that
|
|
* carried its channel is younger than the bounded served-age window.
|
|
* The unsigned jiffies difference is wrap-safe over the full counter
|
|
* width; on every supported target (x86_64, arm64) that is 64 bits
|
|
* of jiffies, so an ancient stamp can never compare young. */
|
|
static bool nv_rs_hwmon_age_fresh(const struct nv_rs_hwmon_slot *slot,
|
|
unsigned long stamp)
|
|
{
|
|
unsigned long ttl = nv_rs_hwmon_live_ttl_jiffies(READ_ONCE(hwmon_poll_ms));
|
|
|
|
return (unsigned long)(jiffies - stamp) < ttl;
|
|
}
|
|
|
|
static void nv_rs_hwmon_maybe_refresh(struct nv_rs_hwmon_slot *slot)
|
|
{
|
|
unsigned int poll_ms = READ_ONCE(hwmon_poll_ms);
|
|
unsigned long ttl;
|
|
unsigned long flags;
|
|
struct nv_rs_hwmon_sample sample;
|
|
u64 seq_before[NV_RS_HWMON_LIVE_COUNT];
|
|
u32 gpu_id;
|
|
int rc;
|
|
int i;
|
|
|
|
if (!slot || poll_ms == 0 || READ_ONCE(slot->removing))
|
|
return;
|
|
if (poll_ms > NV_RS_HWMON_POLL_MAX_MS)
|
|
poll_ms = NV_RS_HWMON_POLL_MAX_MS;
|
|
ttl = msecs_to_jiffies(poll_ms);
|
|
|
|
/* Rate-limit repeated attempts with the poll interval, but never
|
|
* gate the very first attempt: a freshly registered slot must
|
|
* acquire immediately (the zero-initialized attempt timestamp is
|
|
* a sentinel, not a real attempt). Failures and unsupported
|
|
* providers stay rate-limited once an attempt has happened. */
|
|
if (READ_ONCE(slot->attempted) &&
|
|
time_before(jiffies, READ_ONCE(slot->last_poll_jiffies) + ttl))
|
|
return;
|
|
|
|
mutex_lock(&slot->poll_lock);
|
|
if (READ_ONCE(slot->removing) ||
|
|
(READ_ONCE(slot->attempted) &&
|
|
time_before(jiffies, READ_ONCE(slot->last_poll_jiffies) + ttl))) {
|
|
mutex_unlock(&slot->poll_lock);
|
|
return;
|
|
}
|
|
|
|
gpu_id = READ_ONCE(slot->gpu_id);
|
|
memset(&sample, 0, sizeof(sample));
|
|
/* Lock order: poll_lock -> rmapi_node_lock -> sample_lock. */
|
|
nv_rs_rmapi_node_lock();
|
|
if (READ_ONCE(slot->removing)) {
|
|
nv_rs_rmapi_node_unlock();
|
|
mutex_unlock(&slot->poll_lock);
|
|
return;
|
|
}
|
|
spin_lock_irqsave(&slot->sample_lock, flags);
|
|
for (i = 0; i < NV_RS_HWMON_LIVE_COUNT; i++)
|
|
seq_before[i] = slot->pub_seq[i];
|
|
spin_unlock_irqrestore(&slot->sample_lock, flags);
|
|
rc = nv_rs_ga10x_hwmon_poll(gpu_id, &sample);
|
|
if (READ_ONCE(slot->removing)) {
|
|
if (rc != 0 && rc != -ENODEV) {
|
|
spin_lock_irqsave(&slot->sample_lock, flags);
|
|
for (i = 0; i < NV_RS_HWMON_LIVE_COUNT; i++)
|
|
atomic_andnot(nv_rs_hwmon_live_bits[i], &slot->valid_mask);
|
|
spin_unlock_irqrestore(&slot->sample_lock, flags);
|
|
}
|
|
nv_rs_rmapi_node_unlock();
|
|
mutex_unlock(&slot->poll_lock);
|
|
return;
|
|
}
|
|
if (rc == -ENODEV) {
|
|
/* No active acquisition attempt was possible: no GA10x/GSP
|
|
* owner exists, either because this GPU is mirror-only (e.g.
|
|
* GP100) or because the runtime has not started yet. This is
|
|
* not an acquired-channel error: recent mirror publications
|
|
* are retained and expire by their own per-channel age
|
|
* bound. The provider is retried on the next window and is
|
|
* never latched, so a runtime that appears later acquires
|
|
* normally. */
|
|
nv_rs_rmapi_node_unlock();
|
|
WRITE_ONCE(slot->last_poll_jiffies, jiffies);
|
|
WRITE_ONCE(slot->attempted, true);
|
|
mutex_unlock(&slot->poll_lock);
|
|
return;
|
|
}
|
|
|
|
/* The attempted acquisition is authoritative per channel, with
|
|
* publication ordering by per-channel publication sequence: any
|
|
* publication that touched a channel while the poll was in flight
|
|
* (including in the same jiffy as the poll start) wins for that
|
|
* channel. A failed result invalidates every live channel it
|
|
* covers. A successful result applies the channels it carries and
|
|
* conservatively invalidates the channels it did not acquire:
|
|
* with the current provider API an absent channel cannot be told
|
|
* apart from a failed acquisition, so error semantics are
|
|
* preserved until the provider supplies per-channel
|
|
* acquisition/error status. Provider sample contents are never
|
|
* consulted on failure. Static limit attributes are preserved
|
|
* across failures and are applied from successful poll results
|
|
* as before. */
|
|
spin_lock_irqsave(&slot->sample_lock, flags);
|
|
for (i = 0; i < NV_RS_HWMON_LIVE_COUNT; i++) {
|
|
u32 bit = nv_rs_hwmon_live_bits[i];
|
|
|
|
if (slot->pub_seq[i] != seq_before[i])
|
|
continue;
|
|
if (rc == 0 && (sample.valid_mask & bit)) {
|
|
struct nv_rs_hwmon_sample s = sample;
|
|
|
|
s.valid_mask = bit;
|
|
nv_rs_hwmon_apply_sample(slot, &s);
|
|
slot->live_stamps[i] = jiffies;
|
|
} else {
|
|
atomic_andnot(bit, &slot->valid_mask);
|
|
}
|
|
}
|
|
if (rc == 0) {
|
|
struct nv_rs_hwmon_sample s = sample;
|
|
|
|
s.valid_mask &= (NV_RS_HWMON_VALID_TEMP_MAX |
|
|
NV_RS_HWMON_VALID_TEMP_CRIT |
|
|
NV_RS_HWMON_VALID_POWER_CAP);
|
|
nv_rs_hwmon_apply_sample(slot, &s);
|
|
}
|
|
spin_unlock_irqrestore(&slot->sample_lock, flags);
|
|
nv_rs_rmapi_node_unlock();
|
|
|
|
/* Rate-limit the next attempt regardless of the outcome. */
|
|
WRITE_ONCE(slot->last_poll_jiffies, jiffies);
|
|
WRITE_ONCE(slot->attempted, true);
|
|
if (rc == 0)
|
|
WRITE_ONCE(slot->has_poll, true);
|
|
mutex_unlock(&slot->poll_lock);
|
|
}
|
|
|
|
static umode_t nv_rs_hwmon_is_visible(const void *drvdata,
|
|
enum hwmon_sensor_types type,
|
|
u32 attr,
|
|
int channel)
|
|
{
|
|
(void)drvdata;
|
|
if (channel != 0)
|
|
return 0;
|
|
switch (type) {
|
|
case hwmon_temp:
|
|
if (attr == hwmon_temp_input || attr == hwmon_temp_max || attr == hwmon_temp_crit)
|
|
return 0444;
|
|
return 0;
|
|
case hwmon_power:
|
|
if (attr == hwmon_power_average || attr == hwmon_power_cap)
|
|
return 0444;
|
|
return 0;
|
|
case hwmon_in:
|
|
if (attr == hwmon_in_input)
|
|
return 0444;
|
|
return 0;
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
/* Coherent live-channel read: validity bit, acquisition stamp and
|
|
* value are taken in one critical section so a publication can never
|
|
* pair an old value with a new stamp. The age is then evaluated
|
|
* against the channel's own stamp. */
|
|
static int nv_rs_hwmon_read_live(struct nv_rs_hwmon_slot *slot, int idx,
|
|
long *val)
|
|
{
|
|
u32 bit = nv_rs_hwmon_live_bits[idx];
|
|
unsigned long stamp;
|
|
unsigned long flags;
|
|
u32 mask;
|
|
long value = 0;
|
|
|
|
nv_rs_hwmon_maybe_refresh(slot);
|
|
if (READ_ONCE(slot->removing))
|
|
return -ENODATA;
|
|
spin_lock_irqsave(&slot->sample_lock, flags);
|
|
if (READ_ONCE(slot->removing)) {
|
|
spin_unlock_irqrestore(&slot->sample_lock, flags);
|
|
return -ENODATA;
|
|
}
|
|
mask = (u32)atomic_read(&slot->valid_mask);
|
|
stamp = slot->live_stamps[idx];
|
|
switch (idx) {
|
|
case NV_RS_HWMON_LIVE_TEMP:
|
|
value = atomic_read(&slot->temp_mc);
|
|
break;
|
|
case NV_RS_HWMON_LIVE_POWER:
|
|
value = (long)atomic64_read(&slot->power_uw);
|
|
break;
|
|
case NV_RS_HWMON_LIVE_VOLT:
|
|
value = atomic_read(&slot->volt_mv);
|
|
break;
|
|
case NV_RS_HWMON_LIVE_GPC_CLK:
|
|
value = atomic_read(&slot->gpc_mhz);
|
|
break;
|
|
case NV_RS_HWMON_LIVE_MEM_CLK:
|
|
value = atomic_read(&slot->mem_mhz);
|
|
break;
|
|
case NV_RS_HWMON_LIVE_GPU_UTIL:
|
|
value = atomic_read(&slot->gpu_util_pct);
|
|
break;
|
|
case NV_RS_HWMON_LIVE_MEM_UTIL:
|
|
value = atomic_read(&slot->mem_util_pct);
|
|
break;
|
|
case NV_RS_HWMON_LIVE_PSTATE:
|
|
value = atomic_read(&slot->pstate);
|
|
break;
|
|
}
|
|
if (!(mask & bit) || !nv_rs_hwmon_age_fresh(slot, stamp)) {
|
|
/* A stale channel stays invalid: clear its valid bit so a
|
|
* jiffies wraparound cannot revive an ancient reading. The
|
|
* clear happens inside the same critical section that
|
|
* observed the stale age, so a concurrently published sample
|
|
* (poll or mirror) is never discarded. */
|
|
if (mask & bit)
|
|
atomic_andnot(bit, &slot->valid_mask);
|
|
spin_unlock_irqrestore(&slot->sample_lock, flags);
|
|
return -ENODATA;
|
|
}
|
|
spin_unlock_irqrestore(&slot->sample_lock, flags);
|
|
|
|
if (bit == NV_RS_HWMON_VALID_POWER && value <= 0)
|
|
return -ENODATA;
|
|
*val = value;
|
|
return 0;
|
|
}
|
|
|
|
static int nv_rs_hwmon_read(struct device *dev,
|
|
enum hwmon_sensor_types type,
|
|
u32 attr,
|
|
int channel,
|
|
long *val)
|
|
{
|
|
struct nv_rs_hwmon_slot *slot = dev_get_drvdata(dev);
|
|
u32 mask;
|
|
|
|
if (!slot || channel != 0)
|
|
return -EINVAL;
|
|
|
|
switch (type) {
|
|
case hwmon_temp:
|
|
if (attr == hwmon_temp_input)
|
|
return nv_rs_hwmon_read_live(slot, NV_RS_HWMON_LIVE_TEMP, val);
|
|
nv_rs_hwmon_maybe_refresh(slot);
|
|
mask = (u32)atomic_read(&slot->valid_mask);
|
|
if (attr == hwmon_temp_max) {
|
|
if (!(mask & NV_RS_HWMON_VALID_TEMP_MAX))
|
|
return -ENODATA;
|
|
*val = atomic_read(&slot->temp_max_mc);
|
|
return 0;
|
|
}
|
|
if (attr == hwmon_temp_crit) {
|
|
if (!(mask & NV_RS_HWMON_VALID_TEMP_CRIT))
|
|
return -ENODATA;
|
|
*val = atomic_read(&slot->temp_crit_mc);
|
|
return 0;
|
|
}
|
|
return -EOPNOTSUPP;
|
|
case hwmon_power:
|
|
if (attr == hwmon_power_average)
|
|
return nv_rs_hwmon_read_live(slot, NV_RS_HWMON_LIVE_POWER, val);
|
|
nv_rs_hwmon_maybe_refresh(slot);
|
|
mask = (u32)atomic_read(&slot->valid_mask);
|
|
if (attr == hwmon_power_cap) {
|
|
s64 cap;
|
|
|
|
if (!(mask & NV_RS_HWMON_VALID_POWER_CAP))
|
|
return -ENODATA;
|
|
cap = atomic64_read(&slot->power_cap_uw);
|
|
if (cap <= 0)
|
|
return -ENODATA;
|
|
*val = (long)cap;
|
|
return 0;
|
|
}
|
|
return -EOPNOTSUPP;
|
|
case hwmon_in:
|
|
if (attr == hwmon_in_input)
|
|
return nv_rs_hwmon_read_live(slot, NV_RS_HWMON_LIVE_VOLT, val);
|
|
return -EOPNOTSUPP;
|
|
default:
|
|
return -EOPNOTSUPP;
|
|
}
|
|
}
|
|
|
|
static const struct hwmon_channel_info *nv_rs_hwmon_info[] = {
|
|
HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT),
|
|
HWMON_CHANNEL_INFO(power, HWMON_P_AVERAGE | HWMON_P_CAP),
|
|
HWMON_CHANNEL_INFO(in, HWMON_I_INPUT),
|
|
NULL,
|
|
};
|
|
|
|
static const struct hwmon_ops nv_rs_hwmon_ops = {
|
|
.is_visible = nv_rs_hwmon_is_visible,
|
|
.read = nv_rs_hwmon_read,
|
|
};
|
|
|
|
static const struct hwmon_chip_info nv_rs_hwmon_chip_info = {
|
|
.ops = &nv_rs_hwmon_ops,
|
|
.info = nv_rs_hwmon_info,
|
|
};
|
|
|
|
static ssize_t gpc_clock_mhz_show(struct device *dev,
|
|
struct device_attribute *attr,
|
|
char *buf)
|
|
{
|
|
struct nv_rs_hwmon_slot *slot = dev_get_drvdata(dev);
|
|
long v;
|
|
int rc;
|
|
(void)attr;
|
|
|
|
if (!slot)
|
|
return -EINVAL;
|
|
rc = nv_rs_hwmon_read_live(slot, NV_RS_HWMON_LIVE_GPC_CLK, &v);
|
|
if (rc)
|
|
return rc;
|
|
return sysfs_emit(buf, "%d\n", (int)v);
|
|
}
|
|
static DEVICE_ATTR_RO(gpc_clock_mhz);
|
|
|
|
static ssize_t mem_clock_mhz_show(struct device *dev,
|
|
struct device_attribute *attr,
|
|
char *buf)
|
|
{
|
|
struct nv_rs_hwmon_slot *slot = dev_get_drvdata(dev);
|
|
long v;
|
|
int rc;
|
|
(void)attr;
|
|
|
|
if (!slot)
|
|
return -EINVAL;
|
|
rc = nv_rs_hwmon_read_live(slot, NV_RS_HWMON_LIVE_MEM_CLK, &v);
|
|
if (rc)
|
|
return rc;
|
|
return sysfs_emit(buf, "%d\n", (int)v);
|
|
}
|
|
static DEVICE_ATTR_RO(mem_clock_mhz);
|
|
|
|
static ssize_t gpu_util_pct_show(struct device *dev,
|
|
struct device_attribute *attr,
|
|
char *buf)
|
|
{
|
|
struct nv_rs_hwmon_slot *slot = dev_get_drvdata(dev);
|
|
long v;
|
|
int rc;
|
|
(void)attr;
|
|
|
|
if (!slot)
|
|
return -EINVAL;
|
|
rc = nv_rs_hwmon_read_live(slot, NV_RS_HWMON_LIVE_GPU_UTIL, &v);
|
|
if (rc)
|
|
return rc;
|
|
return sysfs_emit(buf, "%d\n", (int)v);
|
|
}
|
|
static DEVICE_ATTR_RO(gpu_util_pct);
|
|
|
|
static ssize_t mem_util_pct_show(struct device *dev,
|
|
struct device_attribute *attr,
|
|
char *buf)
|
|
{
|
|
struct nv_rs_hwmon_slot *slot = dev_get_drvdata(dev);
|
|
long v;
|
|
int rc;
|
|
(void)attr;
|
|
|
|
if (!slot)
|
|
return -EINVAL;
|
|
rc = nv_rs_hwmon_read_live(slot, NV_RS_HWMON_LIVE_MEM_UTIL, &v);
|
|
if (rc)
|
|
return rc;
|
|
return sysfs_emit(buf, "%d\n", (int)v);
|
|
}
|
|
static DEVICE_ATTR_RO(mem_util_pct);
|
|
|
|
static ssize_t pstate_show(struct device *dev,
|
|
struct device_attribute *attr,
|
|
char *buf)
|
|
{
|
|
struct nv_rs_hwmon_slot *slot = dev_get_drvdata(dev);
|
|
long v;
|
|
int rc;
|
|
(void)attr;
|
|
|
|
if (!slot)
|
|
return -EINVAL;
|
|
rc = nv_rs_hwmon_read_live(slot, NV_RS_HWMON_LIVE_PSTATE, &v);
|
|
if (rc)
|
|
return rc;
|
|
return sysfs_emit(buf, "P%d\n", (int)v);
|
|
}
|
|
static DEVICE_ATTR_RO(pstate);
|
|
|
|
static struct attribute *nv_rs_hwmon_extra_attrs[] = {
|
|
&dev_attr_gpc_clock_mhz.attr,
|
|
&dev_attr_mem_clock_mhz.attr,
|
|
&dev_attr_gpu_util_pct.attr,
|
|
&dev_attr_mem_util_pct.attr,
|
|
&dev_attr_pstate.attr,
|
|
NULL,
|
|
};
|
|
ATTRIBUTE_GROUPS(nv_rs_hwmon_extra);
|
|
|
|
int nv_rs_hwmon_register_dev(struct device *dev, u32 gpu_id)
|
|
{
|
|
int i;
|
|
struct nv_rs_hwmon_slot *slot = NULL;
|
|
struct device *hwmon;
|
|
|
|
if (!dev)
|
|
return -EINVAL;
|
|
|
|
mutex_lock(&nv_rs_hwmon_lock);
|
|
for (i = 0; i < NV_RS_HWMON_MAX_GPUS; i++) {
|
|
if (nv_rs_hwmon_slots[i].in_use && nv_rs_hwmon_slots[i].dev == dev) {
|
|
mutex_unlock(&nv_rs_hwmon_lock);
|
|
return -EEXIST;
|
|
}
|
|
if (!slot && !nv_rs_hwmon_slots[i].in_use)
|
|
slot = &nv_rs_hwmon_slots[i];
|
|
}
|
|
if (!slot) {
|
|
mutex_unlock(&nv_rs_hwmon_lock);
|
|
return -ENOSPC;
|
|
}
|
|
|
|
mutex_init(&slot->poll_lock);
|
|
spin_lock_init(&slot->sample_lock);
|
|
slot->removing = false;
|
|
slot->mirror_seq = 0;
|
|
slot->gpu_id = gpu_id;
|
|
slot->dev = dev;
|
|
/* Start with valid_mask = 0 (-ENODATA for every sensor) until live telemetry
|
|
* arrives; never publish fabricated placeholder readings (#141). */
|
|
slot->has_poll = false;
|
|
slot->attempted = false;
|
|
slot->last_poll_jiffies = 0;
|
|
memset(slot->live_stamps, 0, sizeof(slot->live_stamps));
|
|
memset(slot->pub_seq, 0, sizeof(slot->pub_seq));
|
|
atomic_set(&slot->valid_mask, 0);
|
|
atomic_set(&slot->temp_mc, 0);
|
|
atomic_set(&slot->temp_max_mc, 0);
|
|
atomic_set(&slot->temp_crit_mc, 0);
|
|
atomic64_set(&slot->power_uw, 0);
|
|
atomic64_set(&slot->power_cap_uw, 0);
|
|
atomic_set(&slot->volt_mv, 0);
|
|
atomic_set(&slot->gpc_mhz, 0);
|
|
atomic_set(&slot->mem_mhz, 0);
|
|
atomic_set(&slot->gpu_util_pct, 0);
|
|
atomic_set(&slot->mem_util_pct, 0);
|
|
atomic_set(&slot->pstate, 0);
|
|
|
|
slot->in_use = true;
|
|
hwmon = hwmon_device_register_with_info(dev, "nvdriver", slot,
|
|
&nv_rs_hwmon_chip_info,
|
|
nv_rs_hwmon_extra_groups);
|
|
if (IS_ERR(hwmon)) {
|
|
int err = PTR_ERR(hwmon);
|
|
slot->in_use = false;
|
|
slot->dev = NULL;
|
|
slot->gpu_id = 0;
|
|
mutex_unlock(&nv_rs_hwmon_lock);
|
|
return err;
|
|
}
|
|
|
|
slot->hwmon_dev = hwmon;
|
|
smp_wmb();
|
|
WRITE_ONCE(slot->in_use, true);
|
|
mutex_unlock(&nv_rs_hwmon_lock);
|
|
return 0;
|
|
}
|
|
|
|
void nv_rs_hwmon_unregister_dev(struct device *dev)
|
|
{
|
|
int i;
|
|
if (!dev)
|
|
return;
|
|
|
|
mutex_lock(&nv_rs_hwmon_lock);
|
|
for (i = 0; i < NV_RS_HWMON_MAX_GPUS; i++) {
|
|
if (nv_rs_hwmon_slots[i].in_use && nv_rs_hwmon_slots[i].dev == dev) {
|
|
struct nv_rs_hwmon_slot *slot = &nv_rs_hwmon_slots[i];
|
|
struct device *hwmon = slot->hwmon_dev;
|
|
/* Keep gpu_id and in_use intact while hwmon_device_unregister()
|
|
* drains sysfs readers; in-flight polls see removing (never
|
|
* gpu_id == 0) and register() cannot reuse this slot prematurely. */
|
|
WRITE_ONCE(slot->removing, true);
|
|
slot->hwmon_dev = NULL;
|
|
slot->dev = NULL;
|
|
mutex_unlock(&nv_rs_hwmon_lock);
|
|
if (hwmon)
|
|
hwmon_device_unregister(hwmon);
|
|
mutex_lock(&nv_rs_hwmon_lock);
|
|
WRITE_ONCE(slot->gpu_id, 0);
|
|
WRITE_ONCE(slot->in_use, false);
|
|
mutex_unlock(&nv_rs_hwmon_lock);
|
|
return;
|
|
}
|
|
}
|
|
mutex_unlock(&nv_rs_hwmon_lock);
|
|
}
|
|
|
|
int nv_rs_hwmon_register(struct pci_dev *pdev)
|
|
{
|
|
if (!pdev)
|
|
return -EINVAL;
|
|
return nv_rs_hwmon_register_dev(&pdev->dev, nv_rs_hwmon_gpu_id(pdev));
|
|
}
|
|
|
|
void nv_rs_hwmon_unregister(struct pci_dev *pdev)
|
|
{
|
|
if (pdev)
|
|
nv_rs_hwmon_unregister_dev(&pdev->dev);
|
|
}
|
|
|
|
void nv_rs_hwmon_update(u32 gpu_id,
|
|
u32 temp_c,
|
|
u32 power_mw,
|
|
u32 volt_uv,
|
|
u32 gpc_mhz,
|
|
u32 mem_mhz,
|
|
u32 gpu_util_pct,
|
|
u32 mem_util_pct)
|
|
{
|
|
struct nv_rs_hwmon_sample s;
|
|
|
|
memset(&s, 0, sizeof(s));
|
|
if (temp_c > 0 && temp_c <= 150) {
|
|
s.temp_mc = (s32)(temp_c * 1000u);
|
|
s.valid_mask |= NV_RS_HWMON_VALID_TEMP;
|
|
}
|
|
if (power_mw > 0) {
|
|
s.power_uw = (u64)power_mw * 1000ULL;
|
|
s.valid_mask |= NV_RS_HWMON_VALID_POWER;
|
|
}
|
|
if (volt_uv >= 1000u) {
|
|
s.volt_mv = min_t(u32, volt_uv / 1000u, (u32)INT_MAX);
|
|
s.valid_mask |= NV_RS_HWMON_VALID_VOLT;
|
|
}
|
|
if (gpc_mhz > 0) {
|
|
s.gpc_mhz = min_t(u32, gpc_mhz, (u32)INT_MAX);
|
|
s.valid_mask |= NV_RS_HWMON_VALID_GPC_CLK;
|
|
}
|
|
if (mem_mhz > 0) {
|
|
s.mem_mhz = min_t(u32, mem_mhz, (u32)INT_MAX);
|
|
s.valid_mask |= NV_RS_HWMON_VALID_MEM_CLK;
|
|
}
|
|
s.gpu_util_pct = gpu_util_pct > 100 ? 100 : gpu_util_pct;
|
|
s.mem_util_pct = mem_util_pct > 100 ? 100 : mem_util_pct;
|
|
s.valid_mask |= NV_RS_HWMON_VALID_GPU_UTIL | NV_RS_HWMON_VALID_MEM_UTIL;
|
|
nv_rs_hwmon_update_sample(gpu_id, &s);
|
|
}
|
|
|
|
void nv_rs_hwmon_update_sample(u32 gpu_id, const struct nv_rs_hwmon_sample *sample)
|
|
{
|
|
int i;
|
|
int c;
|
|
|
|
if (!sample || !sample->valid_mask)
|
|
return;
|
|
for (i = 0; i < NV_RS_HWMON_MAX_GPUS; i++) {
|
|
struct nv_rs_hwmon_slot *slot = &nv_rs_hwmon_slots[i];
|
|
if (READ_ONCE(slot->in_use)) {
|
|
smp_rmb();
|
|
if (!READ_ONCE(slot->removing) && READ_ONCE(slot->gpu_id) == gpu_id) {
|
|
unsigned long flags;
|
|
spin_lock_irqsave(&slot->sample_lock, flags);
|
|
/* Mirror snapshots are incremental: they set the
|
|
* channels they carry and stamp those channels only;
|
|
* they never invalidate channels they do not carry,
|
|
* so an unrelated field update cannot age out a fresh
|
|
* temperature. */
|
|
for (c = 0; c < NV_RS_HWMON_LIVE_COUNT; c++)
|
|
if (sample->valid_mask & nv_rs_hwmon_live_bits[c]) {
|
|
slot->live_stamps[c] = jiffies;
|
|
slot->pub_seq[c]++;
|
|
}
|
|
slot->mirror_seq++;
|
|
nv_rs_hwmon_apply_sample(slot, sample);
|
|
spin_unlock_irqrestore(&slot->sample_lock, flags);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void nv_rs_hwmon_mark_polled(u32 gpu_id)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < NV_RS_HWMON_MAX_GPUS; i++) {
|
|
struct nv_rs_hwmon_slot *slot = &nv_rs_hwmon_slots[i];
|
|
if (READ_ONCE(slot->in_use)) {
|
|
smp_rmb();
|
|
if (!READ_ONCE(slot->removing) && READ_ONCE(slot->gpu_id) == gpu_id) {
|
|
/* Rate-limit only: a poll completion does not claim a
|
|
* fresh temperature. Temperature validity and age come
|
|
* exclusively from a publication that carries
|
|
* VALID_TEMP. */
|
|
WRITE_ONCE(slot->last_poll_jiffies, jiffies);
|
|
WRITE_ONCE(slot->attempted, true);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void nv_rs_hwmon_set_limits(u32 gpu_id,
|
|
u32 power_cap_mw,
|
|
u32 temp_max_c,
|
|
u32 temp_crit_c)
|
|
{
|
|
struct nv_rs_hwmon_sample s;
|
|
|
|
memset(&s, 0, sizeof(s));
|
|
if (power_cap_mw != 0) {
|
|
s.power_cap_uw = (u64)power_cap_mw * 1000ULL;
|
|
s.valid_mask |= NV_RS_HWMON_VALID_POWER_CAP;
|
|
}
|
|
if (temp_max_c != 0) {
|
|
s.temp_max_mc = (s32)(min_t(u32, temp_max_c, (u32)(INT_MAX / 1000)) * 1000u);
|
|
s.valid_mask |= NV_RS_HWMON_VALID_TEMP_MAX;
|
|
}
|
|
if (temp_crit_c != 0) {
|
|
s.temp_crit_mc = (s32)(min_t(u32, temp_crit_c, (u32)(INT_MAX / 1000)) * 1000u);
|
|
s.valid_mask |= NV_RS_HWMON_VALID_TEMP_CRIT;
|
|
}
|
|
nv_rs_hwmon_update_sample(gpu_id, &s);
|
|
}
|
|
|
|
#else /* !IS_ENABLED(CONFIG_HWMON) */
|
|
|
|
/*
|
|
* No in-kernel hwmon class. Registration is a no-op success so probe continues;
|
|
* all telemetry sinks discard their input. NVML/nvidia-smi paths are unaffected.
|
|
*/
|
|
int nv_rs_hwmon_register_dev(struct device *dev, u32 gpu_id)
|
|
{
|
|
(void)dev;
|
|
(void)gpu_id;
|
|
return 0;
|
|
}
|
|
|
|
void nv_rs_hwmon_unregister_dev(struct device *dev)
|
|
{
|
|
(void)dev;
|
|
}
|
|
|
|
int nv_rs_hwmon_register(struct pci_dev *pdev)
|
|
{
|
|
(void)pdev;
|
|
return 0;
|
|
}
|
|
|
|
void nv_rs_hwmon_unregister(struct pci_dev *pdev)
|
|
{
|
|
(void)pdev;
|
|
}
|
|
|
|
void nv_rs_hwmon_update(u32 gpu_id, u32 temp_c, u32 power_mw, u32 volt_uv,
|
|
u32 gpc_mhz, u32 mem_mhz, u32 gpu_util_pct, u32 mem_util_pct)
|
|
{
|
|
(void)gpu_id; (void)temp_c; (void)power_mw; (void)volt_uv;
|
|
(void)gpc_mhz; (void)mem_mhz; (void)gpu_util_pct; (void)mem_util_pct;
|
|
}
|
|
|
|
void nv_rs_hwmon_update_sample(u32 gpu_id, const struct nv_rs_hwmon_sample *sample)
|
|
{
|
|
(void)gpu_id; (void)sample;
|
|
}
|
|
|
|
void nv_rs_hwmon_mark_polled(u32 gpu_id)
|
|
{
|
|
(void)gpu_id;
|
|
}
|
|
|
|
void nv_rs_hwmon_set_limits(u32 gpu_id, u32 power_cap_mw, u32 temp_max_c, u32 temp_crit_c)
|
|
{
|
|
(void)gpu_id; (void)power_cap_mw; (void)temp_max_c; (void)temp_crit_c;
|
|
}
|
|
|
|
#endif /* IS_ENABLED(CONFIG_HWMON) */
|